Decide how to handle application exit.
[nssm.git] / service.cpp
index 05ca7cd..4dc6fa2 100644 (file)
@@ -4,10 +4,14 @@ SERVICE_STATUS service_status;
 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
@@ -56,8 +60,8 @@ int install_service(char *name, char *exe, char *flags) {
 \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
@@ -68,8 +72,8 @@ int install_service(char *name, char *exe, char *flags) {
   }\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
@@ -134,6 +138,11 @@ int remove_service(char *name) {
 \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
@@ -161,6 +170,9 @@ void WINAPI service_main(unsigned long argc, char **argv) {
     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
@@ -168,10 +180,10 @@ int monitor_service() {
   /* 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
@@ -260,17 +272,41 @@ void CALLBACK end_service(void *arg, unsigned char why) {
   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