char flags[CMD_LENGTH];\r
char dir[MAX_PATH];\r
\r
-static enum { NSSM_EXIT_RESTART, NSSM_EXIT_IGNORE, NSSM_EXIT_REALLY } exit_actions;\r
-static const char *exit_action_strings[] = { "Restart", "Ignore", "Exit", 0 };\r
+static enum { NSSM_EXIT_RESTART, NSSM_EXIT_IGNORE, NSSM_EXIT_REALLY, NSSM_EXIT_UNCLEAN } exit_actions;\r
+static const char *exit_action_strings[] = { "Restart", "Ignore", "Exit", "Suicide", 0 };\r
\r
/* Connect to the service manager */\r
SC_HANDLE open_service_manager() {\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
\r
+ set_service_recovery(service_name);\r
+\r
monitor_service();\r
}\r
\r
+/* Make sure service recovery actions are taken where necessary */\r
+void set_service_recovery(char *service_name) {\r
+ SC_HANDLE services = open_service_manager();\r
+ if (! services) return;\r
+\r
+ SC_HANDLE service = OpenService(services, service_name, SC_MANAGER_ALL_ACCESS);\r
+ if (! service) return;\r
+ return;\r
+\r
+ SERVICE_FAILURE_ACTIONS_FLAG flag;\r
+ ZeroMemory(&flag, sizeof(flag));\r
+ flag.fFailureActionsOnNonCrashFailures = true;\r
+\r
+ /* This functionality was added in Vista so the call may fail */\r
+ ChangeServiceConfig2(service, SERVICE_CONFIG_FAILURE_ACTIONS_FLAG, &flag);\r
+}\r
+\r
int monitor_service() {\r
/* Set service status to started */\r
int ret = start_service();\r
switch (control) {\r
case SERVICE_CONTROL_SHUTDOWN:\r
case SERVICE_CONTROL_STOP:\r
- stop_service(0);\r
+ stop_service(0, 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);\r
+ return stop_service(2, 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);\r
+ return stop_service(3, true);\r
}\r
pid = pi.hProcess;\r
\r
}\r
\r
/* Stop the service */\r
-int stop_service(unsigned long exitcode) {\r
+int stop_service(unsigned long exitcode, bool graceful) {\r
/* Signal we are stopping */\r
- service_status.dwCurrentState = SERVICE_STOP_PENDING;\r
- SetServiceStatus(service_handle, &service_status);\r
+ if (graceful) {\r
+ service_status.dwCurrentState = SERVICE_STOP_PENDING;\r
+ SetServiceStatus(service_handle, &service_status);\r
+ }\r
\r
/* Nothing to do if server isn't running */\r
if (pid) {\r
else log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_PROCESS_ALREADY_STOPPED, service_name, exe, 0);\r
\r
/* Signal we stopped */\r
- service_status.dwCurrentState = SERVICE_STOPPED;\r
- if (exitcode) {\r
- service_status.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;\r
- service_status.dwServiceSpecificExitCode = exitcode;\r
- }\r
- else {\r
- service_status.dwWin32ExitCode = NO_ERROR;\r
- service_status.dwServiceSpecificExitCode = 0;\r
+ if (graceful) {\r
+ service_status.dwCurrentState = SERVICE_STOPPED;\r
+ if (exitcode) {\r
+ service_status.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;\r
+ service_status.dwServiceSpecificExitCode = exitcode;\r
+ }\r
+ else {\r
+ service_status.dwWin32ExitCode = NO_ERROR;\r
+ service_status.dwServiceSpecificExitCode = 0;\r
+ }\r
+ SetServiceStatus(service_handle, &service_status);\r
}\r
- SetServiceStatus(service_handle, &service_status);\r
\r
return exitcode;\r
}\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);\r
+ stop_service(ret, true);\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
break;\r
}\r
}\r