return 0;\r
}\r
\r
+int create_exit_action(char *service_name, const char *action_string) {\r
+ /* Get registry */\r
+ char registry[MAX_PATH];\r
+ if (_snprintf(registry, sizeof(registry), NSSM_REGISTRY "\\%s", service_name, NSSM_REG_EXIT) < 0) {\r
+ eventprintf(EVENTLOG_ERROR_TYPE, "Out of memory for NSSM_REG_EXIT in create_exit_action()!");\r
+ return 1;\r
+ }\r
+\r
+ /* Try to open the registry */\r
+ HKEY key;\r
+ unsigned long disposition;\r
+ if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, registry, 0, 0, REG_OPTION_NON_VOLATILE, KEY_WRITE, 0, &key, &disposition) != ERROR_SUCCESS) {\r
+ eventprintf(EVENTLOG_ERROR_TYPE, "Can't open service exit action registry settings!");\r
+ return 2;\r
+ }\r
+\r
+ /* Do nothing if the key already existed */\r
+ if (disposition == REG_OPENED_EXISTING_KEY) {\r
+ RegCloseKey(key);\r
+ return 0;\r
+ }\r
+\r
+ /* Create the default value */\r
+ if (RegSetValueEx(key, 0, 0, REG_SZ, (const unsigned char *) action_string, strlen(action_string) + 1) != ERROR_SUCCESS) {\r
+ eventprintf(EVENTLOG_ERROR_TYPE, "Can't add default registry value %s: %s", NSSM_REG_EXIT, error_string(GetLastError()));\r
+ RegCloseKey(key);\r
+ return 3;\r
+ }\r
+\r
+ /* Close registry */\r
+ RegCloseKey(key);\r
+\r
+ return 0;\r
+}\r
+\r
int get_parameters(char *service_name, char *exe, int exelen, char *flags, int flagslen, char *dir, int dirlen) {\r
/* Get registry */\r
char registry[MAX_PATH];\r
\r
return 0;\r
}\r
+\r
+int get_exit_action(char *service_name, unsigned long *ret, unsigned char *action) {\r
+ /* Get registry */\r
+ char registry[MAX_PATH];\r
+ if (_snprintf(registry, sizeof(registry), NSSM_REGISTRY "\\%s", service_name, NSSM_REG_EXIT) < 0) {\r
+ eventprintf(EVENTLOG_ERROR_TYPE, "Out of memory for NSSM_REG_EXIT in get_exit_action()!");\r
+ return 1;\r
+ }\r
+\r
+ /* Try to open the registry */\r
+ HKEY key;\r
+ long error = RegOpenKeyEx(HKEY_LOCAL_MACHINE, registry, 0, KEY_READ, &key);\r
+ if (error != ERROR_SUCCESS && error != ERROR_FILE_NOT_FOUND) {\r
+ eventprintf(EVENTLOG_ERROR_TYPE, "Can't open registry %s!", registry);\r
+ return 2;\r
+ }\r
+\r
+ unsigned long type = REG_SZ;\r
+ unsigned long action_len = ACTION_LEN;\r
+\r
+ char code[64];\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
+ }\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
+ return 0;\r
+ }\r
+\r
+ /* Close registry */\r
+ RegCloseKey(key);\r
+\r
+ return 0;\r
+}\r
SERVICE_STATUS_HANDLE service_handle;\r
HANDLE wait_handle;\r
HANDLE pid;\r
+static char service_name[MAX_PATH];\r
char exe[MAX_PATH];\r
char flags[MAX_PATH];\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
+\r
/* Connect to the service manager */\r
SC_HANDLE open_service_manager() {\r
SC_HANDLE ret = OpenSCManager(0, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS);\r
\r
/* Construct command */\r
char command[MAX_PATH];\r
- int runlen = strlen(NSSM_RUN);\r
- int pathlen = strlen(path);\r
+ size_t runlen = strlen(NSSM_RUN);\r
+ size_t pathlen = strlen(path);\r
if (pathlen + runlen + 2 >= MAX_PATH) {\r
fprintf(stderr, "The full path to " NSSM " is too long!\n");\r
return 3;\r
}\r
\r
/* Work out directory name */\r
- unsigned int len = strlen(exe);\r
- unsigned int i;\r
+ size_t len = strlen(exe);\r
+ size_t i;\r
for (i = len; i && exe[i] != '\\' && exe[i] != '/'; i--);\r
char dir[MAX_PATH];\r
memmove(dir, exe, i);\r
\r
/* Service initialisation */\r
void WINAPI service_main(unsigned long argc, char **argv) {\r
+ if (_snprintf(service_name, sizeof(service_name), "%s", argv[0]) < 0) {\r
+ eventprintf(EVENTLOG_ERROR_TYPE, "service_main(): Out of memory for service_name!");\r
+ return;\r
+ }\r
+\r
/* Initialise status */\r
ZeroMemory(&service_status, sizeof(service_status));\r
service_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS;\r
return;\r
}\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
+\r
monitor_service();\r
}\r
\r
/* Set service status to started */\r
int ret = start_service();\r
if (ret) {\r
- eventprintf(EVENTLOG_ERROR_TYPE, "Can't start service: error code %d", ret);\r
+ eventprintf(EVENTLOG_ERROR_TYPE, "Can't start service %s: error code %d", service_name, ret);\r
return ret;\r
}\r
- eventprintf(EVENTLOG_INFORMATION_TYPE, "Started process %s %s in %s", exe, flags, dir);\r
+ eventprintf(EVENTLOG_INFORMATION_TYPE, "Started process %s %s in %s for service %s", exe, flags, dir, service_name);\r
\r
/* Monitor service service */\r
if (! RegisterWaitForSingleObject(&wait_handle, pid, end_service, 0, INFINITE, WT_EXECUTEONLYONCE | WT_EXECUTELONGFUNCTION)) {\r
unsigned long ret = 0;\r
GetExitCodeProcess(pid, &ret);\r
\r
- /* Force an error code if none given, so system can restart this service */\r
- /*if (! ret) {\r
- eventprintf(EVENTLOG_INFORMATION_TYPE, "Process exited with return code 0 - overriding with return code 111 so the service manager will notice the failure");\r
- ret = 111;\r
+ eventprintf(EVENTLOG_INFORMATION_TYPE, "Process %s for service %s exited with return code %u", exe, service_name, ret);\r
+\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
+ 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
+ break;\r
+ }\r
+ }\r
}\r
- else */eventprintf(EVENTLOG_INFORMATION_TYPE, "Process %s exited with return code %u", exe, ret);\r
\r
- /* Try to restart the service or return failure code to service manager */\r
pid = 0;\r
- while (monitor_service()) {\r
- eventprintf(EVENTLOG_INFORMATION_TYPE, "Failed to restart %s - sleeping ", exe, ret);\r
- Sleep(30000);\r
+ switch (action) {\r
+ /* Try to restart the service or return failure code to service manager */\r
+ case NSSM_EXIT_RESTART:\r
+ eventprintf(EVENTLOG_INFORMATION_TYPE, "Action for exit code %lu is %s: Attempting to restart %s for service %s", ret, exit_action_strings[action], exe, service_name);\r
+ while (monitor_service()) {\r
+ eventprintf(EVENTLOG_INFORMATION_TYPE, "Failed to restart %s - sleeping ", exe, ret);\r
+ Sleep(30000);\r
+ }\r
+ break;\r
+\r
+ /* Do nothing, just like srvany would */\r
+ case NSSM_EXIT_IGNORE:\r
+ eventprintf(EVENTLOG_INFORMATION_TYPE, "Action for exit code %lu is %s: Not attempting to restart %s for service %s", ret, exit_action_strings[action], exe, service_name);\r
+ Sleep(INFINITE);\r
+ break;\r
+\r
+ /* Tell the service manager we are finished */\r
+ case NSSM_EXIT_REALLY:\r
+ eventprintf(EVENTLOG_INFORMATION_TYPE, "Action for exit code %lu is %s: Stopping service %s", ret, exit_action_strings[action], service_name);\r
+ stop_service(ret);\r
+ break;\r
}\r
}\r