#include <tests_common.h>
#include <cstring>
+#include <unistd.h>
#include "dbus_access.h"
-DBusAccess::DBusAccess()
+DBusAccess::DBusAccess(const char *service_name)
: m_conn(NULL)
, m_msg(NULL)
, m_pending(NULL)
"',member='" + m_signal_member + "',path='" + m_signal_path + "'";
m_dbus_client_name = "tests.dbus.client";
+
+ m_service_name = service_name;
+
+ connectToDBus();
}
void DBusAccess::connect() {
"Error in dbus_bus_request_name: " << m_err.message);
}
-void DBusAccess::newMethodCall(const std::string& method) {
+void DBusAccess::newMethodCall(const char *method) {
const std::string dbus_systemd_name = "org.freedesktop.systemd1";
- const std::string dbus_systemd_object =
- "/org/freedesktop/systemd1/unit/security_2dserver_2eservice";
- const std::string dbus_systemd_interface = "org.freedesktop.systemd1.Unit";
-
+ const std::string dbus_systemd_object = "/org/freedesktop/systemd1";
+ const std::string dbus_systemd_interface = "org.freedesktop.systemd1.Manager";
m_msg = dbus_message_new_method_call(dbus_systemd_name.c_str(),
dbus_systemd_object.c_str(),
dbus_systemd_interface.c_str(),
- method.c_str());
+ method);
RUNNER_ASSERT_MSG_BT(NULL != m_msg,
"Error in dbus_message_new_method_call");
}
-void DBusAccess::appendToMsg() {
+void DBusAccess::appendToMsg(const char *argument) {
DBusMessageIter iter;
- const char *dbus_systemd_srv_unit_mode = "fail";
dbus_message_iter_init_append(m_msg, &iter);
int ret = dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING,
- &dbus_systemd_srv_unit_mode);
+ &argument);
RUNNER_ASSERT_MSG_BT(ret != 0,
"Error in dbus_message_iter_append_basic");
}
dbus_connection_flush(m_conn);
dbus_pending_call_block(m_pending);
+
dbus_message_unref(m_msg);
m_msg = NULL;
}
"Error in dbus_pending_call_steal_reply");
}
-void DBusAccess::handleMsgRestartReply() {
- char *object_path = NULL;
+void DBusAccess::handleMsgReply() {
DBusMessageIter iter;
RUNNER_ASSERT_MSG_BT(dbus_message_iter_init(m_msg, &iter) != 0,
"Message has no arguments");
- if (DBUS_TYPE_OBJECT_PATH == dbus_message_iter_get_arg_type(&iter)) {
- dbus_message_iter_get_basic(&iter, &object_path);
- m_jobID = std::strrchr(object_path, '/') + 1;
- } else {
+ if (DBUS_TYPE_OBJECT_PATH != dbus_message_iter_get_arg_type(&iter)) {
RUNNER_ASSERT_MSG_BT(false, "No job path in msg");
}
dbus_message_unref(m_msg);
m_pending = NULL;
}
-DBusHandlerResult DBusAccess::signalFilter(DBusConnection *, DBusMessage *message, void *This) {
- DBusMessageIter iter;
- dbus_uint32_t id;
- DBusAccess *a = reinterpret_cast<DBusAccess *>(This);
-
- if (dbus_message_is_signal(message, a->m_signal_interface.c_str(), a->m_signal_member.c_str()))
- {
- RUNNER_ASSERT_MSG_BT(dbus_message_iter_init(message, &iter) != 0,
- "No messages in reply");
-
- RUNNER_ASSERT_MSG_BT(dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_UINT32,
- "Argument is not integer");
-
- dbus_message_iter_get_basic(&iter, &id);
-
- if (id == (unsigned int)std::stoi(a->m_jobID)) {
- while (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_INVALID) {
- if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING) {
- char *str = NULL;
- dbus_message_iter_get_basic(&iter, &str);
- if (!strncmp(str, "done", strlen("done")))
- a->m_handled = TRUE;
- }
- dbus_message_iter_next(&iter);
- }
- }
- }
- return (a->m_handled ? DBUS_HANDLER_RESULT_HANDLED : DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
-}
-
-void DBusAccess::handleMsgRestartSignal() {
- dbus_bus_add_match(m_conn, m_signal_match.c_str(), &m_err);
- RUNNER_ASSERT_MSG_BT(dbus_error_is_set(&m_err) != 1,
- "Error in dbus_bus_add_match: " << m_err.message);
-
- dbus_connection_add_filter(m_conn, signalFilter, reinterpret_cast<void *>(this), NULL);
-
- while(dbus_connection_read_write_dispatch(m_conn, 1000)) {
- if(m_handled)
- break;
- }
-}
-
void DBusAccess::connectToDBus() {
connect();
requestName();
}
-void DBusAccess::sendRestartToSS() {
- const std::string method = "Restart";
-
+void DBusAccess::sendToService(const char *method) {
newMethodCall(method);
- appendToMsg();
+ appendToMsg(m_service_name.c_str());
+ appendToMsg("fail");
sendMsgWithReply();
getMsgReply();
- handleMsgRestartReply();
- handleMsgRestartSignal();
+ handleMsgReply();
+ usleep(50000);
}
-void DBusAccess::sendResetFailedToSS() {
- const std::string method = "ResetFailed";
-
- newMethodCall(method);
+void DBusAccess::sendResetFailedToService() {
+ newMethodCall("ResetFailedUnit");
+ appendToMsg(m_service_name.c_str());
sendMsgWithReply();
getMsgReply();
}
-void DBusAccess::restartSS() {
- connectToDBus();
- sendRestartToSS();
- sendResetFailedToSS();
+void DBusAccess::startService() {
+ sendToService("StartUnit");
+ sendResetFailedToService();
+}
+
+void DBusAccess::stopService() {
+ sendToService("StopUnit");
+ sendResetFailedToService();
+}
+
+void DBusAccess::restartService() {
+ sendToService("RestartUnit");
+ sendResetFailedToService();
}
DBusAccess::~DBusAccess() {
* @file dbus_access.h
* @author Zbigniew Jasinski (z.jasinski@samsung.com)
* @version 1.0
- * @brief Functions used in security-tests package for restarting Security Server through
- * SystemD DBuS interface.
+ * @brief Starts, stops or restarts services (e.g. security-server.service)
+ * using dbus interface to communicate with systemd.
*/
#ifndef _DBUS_ACCESS_H_
#define _DBUS_ACCESS_H_
class DBusAccess {
public:
- DBusAccess();
+ DBusAccess() = delete;
+ DBusAccess(const char *service_name);
- void restartSS();
+ void startService();
+ void stopService();
+ void restartService();
virtual ~DBusAccess();
private:
void connect();
void requestName();
- void newMethodCall(const std::string& method);
- void appendToMsg();
+ void newMethodCall(const char *method);
+ void appendToMsg(const char *argument);
void sendMsgWithReply();
void getMsgReply();
- void handleMsgRestartReply();
- void handleMsgRestartSignal();
- static DBusHandlerResult signalFilter(DBusConnection *, DBusMessage *message, void *);
+ void handleMsgReply();
void connectToDBus();
- void sendRestartToSS();
- void sendResetFailedToSS();
+ void sendGetUnitToSystemd(const char *service_name);
+ void sendToService(const char *method);
+ void sendResetFailedToService();
DBusConnection *m_conn;
DBusMessage *m_msg;
DBusError m_err;
DBusPendingCall *m_pending;
- std::string m_jobID;
std::string m_signal_type;
std::string m_signal_interface;
std::string m_dbus_client_name;
+ std::string m_service_name;
+
bool m_handled;
};