return 0;\r
}\r
\r
-int get_exit_action(char *service_name, unsigned long *ret, unsigned char *action) {\r
+int get_exit_action(char *service_name, unsigned long *ret, unsigned char *action, bool *default_action) {\r
+ /* Are we returning the default action or a status-specific one? */\r
+ *default_action = ! ret;\r
+\r
/* Get registry */\r
char registry[KEY_LENGTH];\r
if (_snprintf(registry, sizeof(registry), NSSM_REGISTRY "\\%s", service_name, NSSM_REG_EXIT) < 0) {\r
if (! ret) code[0] = '\0';\r
else if (_snprintf(code, sizeof(code), "%lu", *ret) < 0) {\r
RegCloseKey(key);\r
- return get_exit_action(service_name, 0, action);\r
+ return get_exit_action(service_name, 0, action, default_action);\r
}\r
if (RegQueryValueEx(key, code, 0, &type, action, &action_len) != ERROR_SUCCESS) {\r
RegCloseKey(key);\r
/* Try again with * as the key if an exit code was defined */\r
- if (ret) return get_exit_action(service_name, 0, action);\r
+ if (ret) return get_exit_action(service_name, 0, action, default_action);\r
return 0;\r
}\r
\r
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
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
}\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
/* 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
/* 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