Enhance ServiceManager with socket unit names 14/37214/4
authorLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Fri, 20 Mar 2015 17:44:40 +0000 (18:44 +0100)
committerZbigniew Jasinski <z.jasinski@samsung.com>
Fri, 27 Mar 2015 10:28:39 +0000 (03:28 -0700)
In case of socket activated services additional units responsible
for listening on sockets can be passed to ServiceManager constructor.

Starting, stopping and restarting a service can affect now not only
service unit but also socket units.

This enhancement is used for implementation of cynara restart methods
with and without restarting socket units.

Change-Id: I619c94d3fc75b398240e85cd39ae78e757e03f1a

src/common/service_manager.cpp
src/common/service_manager.h
src/cynara-tests/common/cynara_test_commons.cpp
src/cynara-tests/common/cynara_test_commons.h

index 5349171..c571d5f 100644 (file)
@@ -26,6 +26,7 @@
 
 #include <dpl/test/test_runner.h>
 
+#include <algorithm>
 #include <sstream>
 #include <unistd.h>
 #include <vector>
@@ -45,9 +46,11 @@ const std::string MATCH_RELOADING("Reloading");
 
 }
 
-ServiceManager::ServiceManager(const std::string &serviceName)
+ServiceManager::ServiceManager(const std::string &serviceName,
+                               const std::vector<std::string>& socketsNames)
     : m_connection(DBUS_BUS_SYSTEM, true)
     , m_serviceName(serviceName)
+    , m_socketsNames(socketsNames)
 {
     addBusMatch(MATCH_JOB_REMOVED);
     addBusMatch(MATCH_JOB_NEW);
@@ -126,10 +129,10 @@ uint64_t ServiceManager::handleVariantUInt64MsgReply(DBus::MessageIn &messageIn)
     return iteratorSub.getArgUint64();
 }
 
-void ServiceManager::sendToService(const std::string &method)
+void ServiceManager::sendToService(const std::string &method, const std::string &unit)
 {
     DBus::MessageOut messageOut = newMethodCall(method);
-    messageOut.append(m_serviceName);
+    messageOut.append(unit);
     messageOut.append("fail");
     DBus::MessageIn messageIn = m_connection.sendWithReplyAndBlock(messageOut);
     m_runningJobs.insert(handleObjectPathMsgReply(messageIn));
@@ -222,7 +225,8 @@ void ServiceManager::signalJobRemovedHandler(DBus::MessageIn &messageIn)
     iterator.expectArgType(DBUS_TYPE_STRING);
     std::string result = iterator.getArgString();
 
-    if(m_serviceName == unit) {
+    if (unit == m_serviceName
+        || std::count(m_socketsNames.begin(), m_socketsNames.end(), unit) > 0) {
         RUNNER_ASSERT_MSG(result == "done" || result == "canceled",
                           "RemoveJob signal delivered bad news. Job wasn't completed successfully: "
                               << "expected job results = {done, canceled}, "
@@ -270,25 +274,40 @@ void ServiceManager::waitForRunningJobsFinish()
         m_connection.readWriteDispatch();
 }
 
-void ServiceManager::startService()
+void ServiceManager::executeMethod(const std::string &method, const std::string &unit)
 {
-    sendToService("StartUnit");
+    sendToService(method, unit);
     waitForRunningJobsFinish();
     sendResetFailedToService();
 }
 
-void ServiceManager::stopService()
+void ServiceManager::startService(bool withSockets)
 {
-    sendToService("StopUnit");
-    waitForRunningJobsFinish();
-    sendResetFailedToService();
+    executeMethod("StartUnit", m_serviceName);
+    if (withSockets)
+        for (const auto &socket : m_socketsNames)
+            executeMethod("StartUnit", socket);
 }
 
-void ServiceManager::restartService()
+void ServiceManager::stopService(bool withSockets)
 {
-    sendToService("RestartUnit");
-    waitForRunningJobsFinish();
-    sendResetFailedToService();
+    if (withSockets)
+        for (const auto &socket : m_socketsNames)
+            executeMethod("StopUnit", socket);
+    executeMethod("StopUnit", m_serviceName);
+}
+
+void ServiceManager::restartService(bool withSockets)
+{
+    if (withSockets)
+        for (const auto &socket : m_socketsNames)
+            executeMethod("StopUnit", socket);
+
+    executeMethod("RestartUnit", m_serviceName);
+
+    if (withSockets)
+        for (const auto &socket : m_socketsNames)
+            executeMethod("StartUnit", socket);
 }
 
 pid_t ServiceManager::getServicePid()
index f1f492f..7dbdecf 100644 (file)
 class ServiceManager {
 public:
     ServiceManager() = delete;
-    ServiceManager(const std::string &serviceName);
+    ServiceManager(const std::string &serviceName,
+                   const std::vector<std::string>& socketsNames = {});
     ~ServiceManager() = default;
 
-    void startService();
-    void stopService();
-    void restartService();
+    void startService(bool withSockets = false);
+    void stopService(bool withSockets = false);
+    void restartService(bool withSockets = false);
     pid_t getServicePid();
     timeval getServiceStartTimestamp();
     void maskService();
@@ -59,7 +60,7 @@ private:
     uint32_t handleVariantUIntMsgReply(DBus::MessageIn &messageIn);
     uint64_t handleVariantUInt64MsgReply(DBus::MessageIn &messageIn);
 
-    void sendToService(const std::string &method);
+    void sendToService(const std::string &method, const std::string &unit);
     void sendMaskToService();
     void sendUnmaskToService();
     DBus::MessageIn sendPropertyGetMsg(const std::string &interface, const std::string &property);
@@ -73,9 +74,12 @@ private:
     void signalReloadingHandler(DBus::MessageIn &messageIn);
     void waitForRunningJobsFinish();
 
+    void executeMethod(const std::string &method, const std::string &unit);
+
     DBus::Connection m_connection;
 
     const std::string m_serviceName;
+    const std::vector<std::string> m_socketsNames;
     std::string m_unitPath;
 
     std::set<std::string> m_runningJobs;
index 8c2f5a1..38c17dd 100644 (file)
@@ -31,6 +31,10 @@ const std::string DB_DIR(CYNARA_DB_DIR);
 const std::string USER("cynara");
 const std::string LABEL("System");
 const std::string SERVICE("cynara.service");
+const std::string SOCKET_CLIENT("cynara.socket");
+const std::string SOCKET_ADMIN("cynara-admin.socket");
+const std::string SOCKET_AGENT("cynara-agent.socket");
+
 const std::string SERVICE_PLUGINS_DIR("/usr/lib/cynara/plugin/service/");
 
 }
@@ -67,3 +71,18 @@ void loadServicePlugins(const DirectoryPaths &pluginDirectories)
     for (const auto &dir : pluginDirectories)
         FileOperations::copyCynaraFiles(dir.c_str(), CynaraTestConsts::SERVICE_PLUGINS_DIR);
 }
+
+void restartCynaraService()
+{
+    ServiceManager service(CynaraTestConsts::SERVICE);
+    service.restartService();
+}
+
+void restartCynaraServiceAndSockets()
+{
+    ServiceManager service(CynaraTestConsts::SERVICE, { CynaraTestConsts::SOCKET_CLIENT,
+                                                        CynaraTestConsts::SOCKET_ADMIN,
+                                                        CynaraTestConsts::SOCKET_AGENT });
+
+    service.restartService(true);
+}
index 8ea8123..af8ac96 100644 (file)
@@ -36,6 +36,9 @@ extern const std::string USER;
 extern const std::string LABEL;
 extern const std::string SERVICE;
 extern const std::string SERVICE_PLUGINS_DIR;
+extern const std::string SOCKET_CLIENT;
+extern const std::string SOCKET_ADMIN;
+extern const std::string SOCKET_AGENT;
 
 }
 
@@ -57,4 +60,7 @@ void environmentWrap(const char *testName, const std::function<void(void)> &func
 typedef std::vector<std::string> DirectoryPaths;
 void loadServicePlugins(const DirectoryPaths &pluginDirectories);
 
+void restartCynaraService();
+void restartCynaraServiceAndSockets();
+
 #endif /* CYNARA_TEST_COMMONS_H_ */