Don't suicide on exit status 0.
[nssm.git] / service.cpp
index ccc8e89..8c42972 100644 (file)
@@ -225,7 +225,7 @@ unsigned long WINAPI service_control_handler(unsigned long control, unsigned lon
   switch (control) {\r
     case SERVICE_CONTROL_SHUTDOWN:\r
     case SERVICE_CONTROL_STOP:\r
-      stop_service(0, true);\r
+      stop_service(0, true, true);\r
       return NO_ERROR;\r
   }\r
 \r
@@ -250,11 +250,11 @@ int start_service() {
   char cmd[CMD_LENGTH];\r
   if (_snprintf(cmd, sizeof(cmd), "%s %s", exe, flags) < 0) {\r
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "command line", "start_service", 0);\r
-    return stop_service(2, true);\r
+    return stop_service(2, true, true);\r
   }\r
   if (! CreateProcess(0, cmd, 0, 0, 0, 0, 0, dir, &si, &pi)) {\r
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPROCESS_FAILED, service_name, exe, GetLastError(), 0);\r
-    return stop_service(3, true);\r
+    return stop_service(3, true, true);\r
   }\r
   pid = pi.hProcess;\r
 \r
@@ -266,7 +266,12 @@ int start_service() {
 }\r
 \r
 /* Stop the service */\r
-int stop_service(unsigned long exitcode, bool graceful) {\r
+int stop_service(unsigned long exitcode, bool graceful, bool default_action) {\r
+  if (default_action && ! exitcode && ! graceful) {\r
+    log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_GRACEFUL_SUICIDE, service_name, exe, exit_action_strings[NSSM_EXIT_UNCLEAN], exit_action_strings[NSSM_EXIT_UNCLEAN], exit_action_strings[NSSM_EXIT_UNCLEAN], exit_action_strings[NSSM_EXIT_REALLY] ,0);\r
+    graceful = true;\r
+  }\r
+\r
   /* Signal we are stopping */\r
   if (graceful) {\r
     service_status.dwCurrentState = SERVICE_STOP_PENDING;\r
@@ -312,7 +317,8 @@ void CALLBACK end_service(void *arg, unsigned char why) {
   /* What action should we take? */\r
   int action = NSSM_EXIT_RESTART;\r
   unsigned char action_string[ACTION_LEN];\r
-  if (! get_exit_action(service_name, &ret, action_string)) {\r
+  bool default_action;\r
+  if (! get_exit_action(service_name, &ret, action_string, &default_action)) {\r
     for (int i = 0; exit_action_strings[i]; i++) {\r
       if (! _strnicmp((const char *) action_string, exit_action_strings[i], ACTION_LEN)) {\r
         action = i;\r
@@ -341,13 +347,13 @@ void CALLBACK end_service(void *arg, unsigned char why) {
     /* Tell the service manager we are finished */\r
     case NSSM_EXIT_REALLY:\r
       log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_EXIT_REALLY, service_name, code, exit_action_strings[action], 0);\r
-      stop_service(ret, true);\r
+      stop_service(ret, true, default_action);\r
     break;\r
 \r
     /* Fake a crash so pre-Vista service managers will run recovery actions. */\r
     case NSSM_EXIT_UNCLEAN:\r
       log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_EXIT_UNCLEAN, service_name, code, exit_action_strings[action], 0);\r
-      exit(stop_service(ret, false));\r
+      exit(stop_service(ret, false, default_action));\r
     break;\r
   }\r
 }\r