Handle startup failure.
authorIain Patterson <me@iain.cx>
Sat, 25 Sep 2010 09:05:09 +0000 (10:05 +0100)
committerIain Patterson <me@iain.cx>
Sat, 25 Sep 2010 09:05:09 +0000 (10:05 +0100)
Don't hang if startup parameters couldn't be determined.
Instead, signal that the service entered the STOPPED state.
Set START_PENDING state prior to actual startup.

service.cpp

index 0bcc30c..ccc8e89 100644 (file)
@@ -146,7 +146,6 @@ void WINAPI service_main(unsigned long argc, char **argv) {
   /* Initialise status */\r
   ZeroMemory(&service_status, sizeof(service_status));\r
   service_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS;\r
-  service_status.dwCurrentState = SERVICE_RUNNING;\r
   service_status.dwControlsAccepted = SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_STOP;\r
   service_status.dwWin32ExitCode = NO_ERROR;\r
   service_status.dwServiceSpecificExitCode = 0;\r
@@ -156,19 +155,26 @@ void WINAPI service_main(unsigned long argc, char **argv) {
   /* Signal we AREN'T running the server */\r
   pid = 0;\r
 \r
+  /* Register control handler */\r
+  service_handle = RegisterServiceCtrlHandlerEx(NSSM, service_control_handler, 0);\r
+  if (! service_handle) {\r
+    log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_REGISTERSERVICECTRLHANDER_FAILED, GetLastError(), 0);\r
+    return;\r
+  }\r
+\r
   /* Get startup parameters */\r
   int ret = get_parameters(argv[0], exe, sizeof(exe), flags, sizeof(flags), dir, sizeof(dir));\r
   if (ret) {\r
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_GET_PARAMETERS_FAILED, argv[0], 0);\r
+    service_status.dwCurrentState = SERVICE_STOPPED;\r
+    /* An accurate, if not particularly helpful, status */\r
+    service_status.dwWin32ExitCode = ERROR_SERVICE_NOT_ACTIVE;\r
+    SetServiceStatus(service_handle, &service_status);\r
     return;\r
   }\r
 \r
-  /* Register control handler */\r
-  service_handle = RegisterServiceCtrlHandlerEx(NSSM, service_control_handler, 0);\r
-  if (! service_handle) {\r
-    log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_REGISTERSERVICECTRLHANDER_FAILED, GetLastError(), 0);\r
-    return;\r
-  }\r
+  service_status.dwCurrentState = SERVICE_START_PENDING;\r
+  SetServiceStatus(service_handle, &service_status);\r
 \r
   /* Try to create the exit action parameters; we don't care if it fails */\r
   create_exit_action(argv[0], exit_action_strings[0]);\r