Browse Source

Decide how to handle application exit.

When the service exits with exit code n look in
HKLM\SYSTEM\CurrentControlSet\Services\<service>\Parameters\AppExit\<n>,
falling back to the unnamed value if no such code is listed.  Parse the
(string) value of this entry as follows:

    Restart: Start the application again (NSSM default).
    Ignore:  Do nothing (srvany default).
    Exit:    Stop the service.
Iain Patterson 12 years ago
parent
commit
c425384408
4 changed files with 129 additions and 15 deletions
  1. 73 0
      registry.cpp
  2. 3 0
      registry.h
  3. 51 15
      service.cpp
  4. 2 0
      service.h

+ 73 - 0
registry.cpp

@@ -41,6 +41,41 @@ int create_parameters(char *service_name, char *exe, char *flags, char *dir) {
   return 0;
 }
 
+int create_exit_action(char *service_name, const char *action_string) {
+  /* Get registry */
+  char registry[MAX_PATH];
+  if (_snprintf(registry, sizeof(registry), NSSM_REGISTRY "\\%s", service_name, NSSM_REG_EXIT) < 0) {
+    eventprintf(EVENTLOG_ERROR_TYPE, "Out of memory for NSSM_REG_EXIT in create_exit_action()!");
+    return 1;
+  }
+
+  /* Try to open the registry */
+  HKEY key;
+  unsigned long disposition;
+  if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, registry, 0, 0, REG_OPTION_NON_VOLATILE, KEY_WRITE, 0, &key, &disposition) != ERROR_SUCCESS) {
+    eventprintf(EVENTLOG_ERROR_TYPE, "Can't open service exit action registry settings!");
+    return 2;
+  }
+
+  /* Do nothing if the key already existed */
+  if (disposition == REG_OPENED_EXISTING_KEY) {
+    RegCloseKey(key);
+    return 0;
+  }
+
+  /* Create the default value */
+  if (RegSetValueEx(key, 0, 0, REG_SZ, (const unsigned char *) action_string, strlen(action_string) + 1) != ERROR_SUCCESS) {
+    eventprintf(EVENTLOG_ERROR_TYPE, "Can't add default registry value %s: %s", NSSM_REG_EXIT, error_string(GetLastError()));
+    RegCloseKey(key);
+    return 3;
+  }
+
+  /* Close registry */
+  RegCloseKey(key);
+
+  return 0;
+}
+
 int get_parameters(char *service_name, char *exe, int exelen, char *flags, int flagslen, char *dir, int dirlen) {
   /* Get registry */
   char registry[MAX_PATH];
@@ -84,3 +119,41 @@ int get_parameters(char *service_name, char *exe, int exelen, char *flags, int f
 
   return 0;
 }
+
+int get_exit_action(char *service_name, unsigned long *ret, unsigned char *action) {
+  /* Get registry */
+  char registry[MAX_PATH];
+  if (_snprintf(registry, sizeof(registry), NSSM_REGISTRY "\\%s", service_name, NSSM_REG_EXIT) < 0) {
+    eventprintf(EVENTLOG_ERROR_TYPE, "Out of memory for NSSM_REG_EXIT in get_exit_action()!");
+    return 1;
+  }
+
+  /* Try to open the registry */
+  HKEY key;
+  long error = RegOpenKeyEx(HKEY_LOCAL_MACHINE, registry, 0, KEY_READ, &key);
+  if (error != ERROR_SUCCESS && error != ERROR_FILE_NOT_FOUND) {
+    eventprintf(EVENTLOG_ERROR_TYPE, "Can't open registry %s!", registry);
+    return 2;
+  }
+
+  unsigned long type = REG_SZ;
+  unsigned long action_len = ACTION_LEN;
+
+  char code[64];
+  if (! ret) code[0] = '\0';
+  else if (_snprintf(code, sizeof(code), "%lu", *ret) < 0) {
+    RegCloseKey(key);
+    return get_exit_action(service_name, 0, action);
+  }
+  if (RegQueryValueEx(key, code, 0, &type, action, &action_len) != ERROR_SUCCESS) {
+    RegCloseKey(key);
+    /* Try again with * as the key if an exit code was defined */
+    if (ret) return get_exit_action(service_name, 0, action);
+    return 0;
+  }
+
+  /* Close registry */
+  RegCloseKey(key);
+
+  return 0;
+}

+ 3 - 0
registry.h

@@ -5,8 +5,11 @@
 #define NSSM_REG_EXE "Application"
 #define NSSM_REG_FLAGS "AppParameters"
 #define NSSM_REG_DIR "AppDirectory"
+#define NSSM_REG_EXIT "AppExit"
 
 int create_parameters(char *, char *, char *, char *);
+int create_exit_action(char *, const char *);
 int get_parameters(char *, char *, int, char *, int, char *, int);
+int get_exit_action(char *, unsigned long *, unsigned char *);
 
 #endif

+ 51 - 15
service.cpp

@@ -4,10 +4,14 @@ SERVICE_STATUS service_status;
 SERVICE_STATUS_HANDLE service_handle;
 HANDLE wait_handle;
 HANDLE pid;
+static char service_name[MAX_PATH];
 char exe[MAX_PATH];
 char flags[MAX_PATH];
 char dir[MAX_PATH];
 
+static enum { NSSM_EXIT_RESTART, NSSM_EXIT_IGNORE, NSSM_EXIT_REALLY } exit_actions;
+static const char *exit_action_strings[] = { "Restart", "Ignore", "Exit", 0 };
+
 /* Connect to the service manager */
 SC_HANDLE open_service_manager() {
   SC_HANDLE ret = OpenSCManager(0, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS);
@@ -56,8 +60,8 @@ int install_service(char *name, char *exe, char *flags) {
 
   /* Construct command */
   char command[MAX_PATH];
-  int runlen = strlen(NSSM_RUN);
-  int pathlen = strlen(path);
+  size_t runlen = strlen(NSSM_RUN);
+  size_t pathlen = strlen(path);
   if (pathlen + runlen + 2 >= MAX_PATH) {
     fprintf(stderr, "The full path to " NSSM " is too long!\n");
     return 3;
@@ -68,8 +72,8 @@ int install_service(char *name, char *exe, char *flags) {
   }
 
   /* Work out directory name */
-  unsigned int len = strlen(exe);
-  unsigned int i;
+  size_t len = strlen(exe);
+  size_t i;
   for (i = len; i && exe[i] != '\\' && exe[i] != '/'; i--);
   char dir[MAX_PATH];
   memmove(dir, exe, i);
@@ -134,6 +138,11 @@ int remove_service(char *name) {
 
 /* Service initialisation */
 void WINAPI service_main(unsigned long argc, char **argv) {
+  if (_snprintf(service_name, sizeof(service_name), "%s", argv[0]) < 0) {
+    eventprintf(EVENTLOG_ERROR_TYPE, "service_main(): Out of memory for service_name!");
+    return;
+  }
+
   /* Initialise status */
   ZeroMemory(&service_status, sizeof(service_status));
   service_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS;
@@ -161,6 +170,9 @@ void WINAPI service_main(unsigned long argc, char **argv) {
     return;
   }
 
+  /* Try to create the exit action parameters; we don't care if it fails */
+  create_exit_action(argv[0], exit_action_strings[0]);
+
   monitor_service();
 }
 
@@ -168,10 +180,10 @@ int monitor_service() {
   /* Set service status to started */
   int ret = start_service();
   if (ret) {
-    eventprintf(EVENTLOG_ERROR_TYPE, "Can't start service: error code %d", ret);
+    eventprintf(EVENTLOG_ERROR_TYPE, "Can't start service %s: error code %d", service_name, ret);
     return ret;
   }
-  eventprintf(EVENTLOG_INFORMATION_TYPE, "Started process %s %s in %s", exe, flags, dir);
+  eventprintf(EVENTLOG_INFORMATION_TYPE, "Started process %s %s in %s for service %s", exe, flags, dir, service_name);
 
   /* Monitor service service */
   if (! RegisterWaitForSingleObject(&wait_handle, pid, end_service, 0, INFINITE, WT_EXECUTEONLYONCE | WT_EXECUTELONGFUNCTION)) {
@@ -260,17 +272,41 @@ void CALLBACK end_service(void *arg, unsigned char why) {
   unsigned long ret = 0;
   GetExitCodeProcess(pid, &ret);
 
-  /* Force an error code if none given, so system can restart this service */
-  /*if (! ret) {
-    eventprintf(EVENTLOG_INFORMATION_TYPE, "Process exited with return code 0 - overriding with return code 111 so the service manager will notice the failure");
-    ret = 111;
+  eventprintf(EVENTLOG_INFORMATION_TYPE, "Process %s for service %s exited with return code %u", exe, service_name, ret);
+
+  /* What action should we take? */
+  int action = NSSM_EXIT_RESTART;
+  unsigned char action_string[ACTION_LEN];
+  if (! get_exit_action(service_name, &ret, action_string)) {
+    for (int i = 0; exit_action_strings[i]; i++) {
+      if (! _strnicmp((const char *) action_string, exit_action_strings[i], ACTION_LEN)) {
+        action = i;
+        break;
+      }
+    }
   }
-  else */eventprintf(EVENTLOG_INFORMATION_TYPE, "Process %s exited with return code %u", exe, ret);
 
-  /* Try to restart the service or return failure code to service manager */
   pid = 0;
-  while (monitor_service()) {
-    eventprintf(EVENTLOG_INFORMATION_TYPE, "Failed to restart %s - sleeping ", exe, ret);
-    Sleep(30000);
+  switch (action) {
+    /* Try to restart the service or return failure code to service manager */
+    case NSSM_EXIT_RESTART:
+      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);
+      while (monitor_service()) {
+        eventprintf(EVENTLOG_INFORMATION_TYPE, "Failed to restart %s - sleeping ", exe, ret);
+        Sleep(30000);
+      }
+    break;
+
+    /* Do nothing, just like srvany would */
+    case NSSM_EXIT_IGNORE:
+      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);
+      Sleep(INFINITE);
+    break;
+
+    /* Tell the service manager we are finished */
+    case NSSM_EXIT_REALLY:
+      eventprintf(EVENTLOG_INFORMATION_TYPE, "Action for exit code %lu is %s: Stopping service %s", ret, exit_action_strings[action], service_name);
+      stop_service(ret);
+    break;
   }
 }

+ 2 - 0
service.h

@@ -1,6 +1,8 @@
 #ifndef SERVICE_H
 #define SERVICE_H
 
+#define ACTION_LEN 16
+
 void WINAPI service_main(unsigned long, char **);
 unsigned long WINAPI service_control_handler(unsigned long, unsigned long, void *, void *);