Improve socket-activated service handling

This patch provides a more generic way of handling socket-activated
services like dropbear. A socket-activated service only has base socket
unit and spawns service unit with instance name on incoming connection,
so the instanced units of it need to be ignored. And whether a service
is socket-activated can be determined by checking the existance of its
service object path.

Tested:
* Two socket-activated service, dropbear and obmc-console-ssh, can be
  controlled as expected.
* Instances of socket-activated services are no loger added to monitor
  list.

Change-Id: If8faa2248e5794a410f804125410608aa69b858d
Signed-off-by: Jiaqing Zhao <jiaqing.zhao@intel.com>
diff --git a/src/srvcfg_manager.cpp b/src/srvcfg_manager.cpp
index 9840cc4..36857b8 100644
--- a/src/srvcfg_manager.cpp
+++ b/src/srvcfg_manager.cpp
@@ -231,7 +231,7 @@
 void ServiceConfig::queryAndUpdateProperties()
 {
     std::string objectPath =
-        isDropBearService ? socketObjectPath : serviceObjectPath;
+        isSocketActivatedService ? socketObjectPath : serviceObjectPath;
     if (objectPath.empty())
     {
         return;
@@ -333,10 +333,7 @@
     instanceName(instanceName_), serviceObjectPath(serviceObjPath_),
     socketObjectPath(socketObjPath_)
 {
-    if (baseUnitName == "dropbear")
-    {
-        isDropBearService = true;
-    }
+    isSocketActivatedService = serviceObjectPath.empty();
     instantiatedUnitName = baseUnitName + addInstanceName(instanceName, "@");
     updatedFlag = 0;
     queryAndUpdateProperties();
@@ -375,15 +372,16 @@
         {
             systemdUnitAction(conn, yield, getSocketUnitName(), sysdStopUnit);
         }
-        if (!isDropBearService)
+        if (!isSocketActivatedService)
         {
             systemdUnitAction(conn, yield, getServiceUnitName(), sysdStopUnit);
         }
         else
         {
-            // Get the ListUnits property, find all the services of
-            // `dropbear@<ip><port>.service` and stop the service through
-            // the systemdUnitAction method
+            // For socket-activated service, each connection will spawn a
+            // service instance from template. Need to find all spawned service
+            // `<unitName>@<attribute>.service` and stop them through the
+            // systemdUnitAction method
             boost::system::error_code ec;
             auto listUnits =
                 conn->yield_method_call<std::vector<ListUnitsType>>(
@@ -400,7 +398,7 @@
                 const auto& status =
                     std::get<static_cast<int>(ListUnitElements::subState)>(
                         unit);
-                if (service.find("dropbear@") != std::string::npos &&
+                if (service.find(baseUnitName + "@") != std::string::npos &&
                     service.find(".service") != std::string::npos &&
                     status == subStateRunning)
                 {
@@ -451,7 +449,7 @@
         {
             unitFiles = {getServiceUnitName()};
         }
-        else if (!socketObjectPath.empty() && isDropBearService)
+        else if (serviceObjectPath.empty())
         {
             unitFiles = {getSocketUnitName()};
         }
@@ -479,7 +477,7 @@
             systemdUnitAction(conn, yield, getSocketUnitName(),
                               sysdRestartUnit);
         }
-        if (!isDropBearService)
+        if (!serviceObjectPath.empty())
         {
             systemdUnitAction(conn, yield, getServiceUnitName(),
                               sysdRestartUnit);