More robust options for controlling a service.
authorIain Patterson <me@iain.cx>
Mon, 13 Jan 2014 11:56:53 +0000 (11:56 +0000)
committerIain Patterson <me@iain.cx>
Mon, 13 Jan 2014 12:08:05 +0000 (12:08 +0000)
Wait for the service status to change, eg from START_PENDING to RUNNING
before deciding whether a service control operation was successful.

messages.mc
service.cpp

index faa05ad..30eb165 100644 (file)
Binary files a/messages.mc and b/messages.mc differ
index e952dbd..fbbab20 100644 (file)
@@ -18,6 +18,85 @@ typedef struct {
   int last;\r
 } list_t;\r
 \r
+/*\r
+  Check the status in response to a control.\r
+  Returns:  1 if the status is expected, eg STOP following CONTROL_STOP.\r
+            0 if the status is desired, eg STOPPED following CONTROL_STOP.\r
+           -1 if the status is undesired, eg STOPPED following CONTROL_START.\r
+*/\r
+static inline int service_control_response(unsigned long control, unsigned long status) {\r
+  switch (control) {\r
+    case NSSM_SERVICE_CONTROL_START:\r
+      switch (status) {\r
+        case SERVICE_START_PENDING:\r
+          return 1;\r
+\r
+        case SERVICE_RUNNING:\r
+          return 0;\r
+\r
+        default:\r
+          return -1;\r
+      }\r
+\r
+    case SERVICE_CONTROL_STOP:\r
+    case SERVICE_CONTROL_SHUTDOWN:\r
+      switch (status) {\r
+        case SERVICE_STOP_PENDING:\r
+          return 1;\r
+\r
+        case SERVICE_STOPPED:\r
+          return 0;\r
+\r
+        default:\r
+          return -1;\r
+      }\r
+\r
+    case SERVICE_CONTROL_PAUSE:\r
+      switch (status) {\r
+        case SERVICE_PAUSE_PENDING:\r
+          return 1;\r
+\r
+        case SERVICE_PAUSED:\r
+          return 0;\r
+\r
+        default:\r
+          return -1;\r
+      }\r
+\r
+    case SERVICE_CONTROL_CONTINUE:\r
+      switch (status) {\r
+        case SERVICE_CONTINUE_PENDING:\r
+          return 1;\r
+\r
+        case SERVICE_RUNNING:\r
+          return 0;\r
+\r
+        default:\r
+          return -1;\r
+      }\r
+\r
+    case SERVICE_CONTROL_INTERROGATE:\r
+      return 0;\r
+  }\r
+\r
+  return 0;\r
+}\r
+\r
+static inline int await_service_control_response(unsigned long control, SC_HANDLE service_handle, SERVICE_STATUS *service_status, unsigned long initial_status) {\r
+  int tries = 0;\r
+  while (QueryServiceStatus(service_handle, service_status)) {\r
+    int response = service_control_response(control, service_status->dwCurrentState);\r
+    /* Alas we can't WaitForSingleObject() on an SC_HANDLE. */\r
+    if (! response) return response;\r
+    if (response > 0 || service_status->dwCurrentState == initial_status) {\r
+      if (++tries > 10) return response;\r
+      Sleep(50 * tries);\r
+    }\r
+    else return response;\r
+  }\r
+  return -1;\r
+}\r
+\r
 int affinity_mask_to_string(__int64 mask, TCHAR **string) {\r
   if (! string) return 1;\r
   if (! mask) {\r
@@ -1060,32 +1139,35 @@ int control_service(unsigned long control, int argc, TCHAR **argv) {
   unsigned long error;\r
   SERVICE_STATUS service_status;\r
   if (control == NSSM_SERVICE_CONTROL_START) {\r
+    unsigned long initial_status = SERVICE_STOPPED;\r
     ret = StartService(service_handle, (unsigned long) argc, (const TCHAR **) argv);\r
     error = GetLastError();\r
-    CloseHandle(service_handle);\r
     CloseServiceHandle(services);\r
 \r
     if (error == ERROR_IO_PENDING) {\r
       /*\r
         Older versions of Windows return immediately with ERROR_IO_PENDING\r
         indicate that the operation is still in progress.  Newer versions\r
-        will return it if there really is a delay.  As far as we're\r
-        concerned the operation is a success.  We don't claim to offer a\r
-        fully-feature service control method; it's just a quick 'n' dirty\r
-        interface.\r
-\r
-        In the future we may identify and handle this situation properly.\r
+        will return it if there really is a delay.\r
       */\r
       ret = 1;\r
       error = ERROR_SUCCESS;\r
     }\r
 \r
     if (ret) {\r
-      _tprintf(_T("%s: %s"), canonical_name, error_string(error));\r
+      int response = await_service_control_response(control, service_handle, &service_status, initial_status);\r
+      CloseHandle(service_handle);\r
+\r
+      if (response) {\r
+        print_message(stderr, NSSM_MESSAGE_BAD_CONTROL_RESPONSE, canonical_name, service_status_text(service_status.dwCurrentState), service_control_text(control));\r
+        return 1;\r
+      }\r
+      else _tprintf(_T("%s: %s: %s"), canonical_name, service_control_text(control), error_string(error));\r
       return 0;\r
     }\r
     else {\r
-      _ftprintf(stderr, _T("%s: %s"), canonical_name, error_string(error));\r
+      CloseHandle(service_handle);\r
+      _ftprintf(stderr, _T("%s: %s: %s"), canonical_name, service_control_text(control), error_string(error));\r
       return 1;\r
     }\r
   }\r
@@ -1099,16 +1181,7 @@ int control_service(unsigned long control, int argc, TCHAR **argv) {
     error = GetLastError();\r
 \r
     if (ret) {\r
-      switch (service_status.dwCurrentState) {\r
-        case SERVICE_STOPPED: _tprintf(_T("SERVICE_STOPPED\n")); break;\r
-        case SERVICE_START_PENDING: _tprintf(_T("SERVICE_START_PENDING\n")); break;\r
-        case SERVICE_STOP_PENDING: _tprintf(_T("SERVICE_STOP_PENDING\n")); break;\r
-        case SERVICE_RUNNING: _tprintf(_T("SERVICE_RUNNING\n")); break;\r
-        case SERVICE_CONTINUE_PENDING: _tprintf(_T("SERVICE_CONTINUE_PENDING\n")); break;\r
-        case SERVICE_PAUSE_PENDING: _tprintf(_T("SERVICE_PAUSE_PENDING\n")); break;\r
-        case SERVICE_PAUSED: _tprintf(_T("SERVICE_PAUSED\n")); break;\r
-        default: _tprintf(_T("?\n")); return 1;\r
-      }\r
+      _tprintf(_T("%s\n"), service_status_text(service_status.dwCurrentState));\r
       return 0;\r
     }\r
     else {\r
@@ -1118,8 +1191,8 @@ int control_service(unsigned long control, int argc, TCHAR **argv) {
   }\r
   else {\r
     ret = ControlService(service_handle, control, &service_status);\r
+    unsigned long initial_status = service_status.dwCurrentState;\r
     error = GetLastError();\r
-    CloseHandle(service_handle);\r
     CloseServiceHandle(services);\r
 \r
     if (error == ERROR_IO_PENDING) {\r
@@ -1128,11 +1201,19 @@ int control_service(unsigned long control, int argc, TCHAR **argv) {
     }\r
 \r
     if (ret) {\r
-      _tprintf(_T("%s: %s"), canonical_name, error_string(error));\r
+      int response = await_service_control_response(control, service_handle, &service_status, initial_status);\r
+      CloseHandle(service_handle);\r
+\r
+      if (response) {\r
+        print_message(stderr, NSSM_MESSAGE_BAD_CONTROL_RESPONSE, canonical_name, service_status_text(service_status.dwCurrentState), service_control_text(control));\r
+        return 1;\r
+      }\r
+      else _tprintf(_T("%s: %s: %s"), canonical_name, service_control_text(control), error_string(error));\r
       return 0;\r
     }\r
     else {\r
-      _ftprintf(stderr, _T("%s: %s"), canonical_name, error_string(error));\r
+      CloseHandle(service_handle);\r
+      _ftprintf(stderr, _T("%s: %s %s"), canonical_name, service_control_text(control), error_string(error));\r
       return 1;\r
     }\r
   }\r