Browse Source

Command to dump service configuration.

Output commands which can be used to recreate the service configuration
with:

    nssm dump <servicename>

Commands are quoted and escaped as best we can manage.
Iain Patterson 5 years ago
parent
commit
3c9d8f85d3
5 changed files with 339 additions and 79 deletions
  1. 16 0
      README.txt
  2. 1 1
      nssm.cpp
  3. 34 12
      service.cpp
  4. 286 66
      settings.cpp
  5. 2 0
      settings.h

+ 16 - 0
README.txt

@@ -70,6 +70,8 @@ Since version 2.25, NSSM can execute commands in response to service events.
 
 Since version 2.25, NSSM can list services it manages.
 
+Since version 2.25, NSSM can dump the configuration of services it manages.
+
 
 Usage
 -----
@@ -867,6 +869,20 @@ The following command will print the names of all services managed by NSSM:
     nssm list
 
 
+Exporting service configuration
+-------------------------------
+NSSM can dump commands which would recreate the configuration of a service.
+The output can be pasted into a batch script to back up the service or
+transfer to another computer.
+
+    nssm dump <servicename>
+
+Because the service configuration may contain characters which need to be
+quoted or escaped from the command prompt, NSSM tries hard to produce
+output which will work correctly when run as a script, by adding quotes
+and caret escapes as appropriate.
+
+
 Example usage
 -------------
 To install an Unreal Tournament server:

+ 1 - 1
nssm.cpp

@@ -265,7 +265,7 @@ int _tmain(int argc, TCHAR **argv) {
       create_messages();
       exit(pre_install_service(argc - 2, argv + 2));
     }
-    if (str_equiv(argv[1], _T("edit")) || str_equiv(argv[1], _T("get")) || str_equiv(argv[1], _T("set")) || str_equiv(argv[1], _T("reset")) || str_equiv(argv[1], _T("unset"))) {
+    if (str_equiv(argv[1], _T("edit")) || str_equiv(argv[1], _T("get")) || str_equiv(argv[1], _T("set")) || str_equiv(argv[1], _T("reset")) || str_equiv(argv[1], _T("unset")) || str_equiv(argv[1], _T("dump"))) {
       int ret = pre_edit_service(argc - 1, argv + 1);
       if (ret == 3 && ! is_admin && argc == 3) exit(elevate(argc, argv, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_EDIT));
       /* There might be a password here. */

+ 34 - 12
service.cpp

@@ -883,7 +883,7 @@ int pre_edit_service(int argc, TCHAR **argv) {
   if (argc < 2) return usage(1);
 
   /* Are we editing on the command line? */
-  enum { MODE_EDITING, MODE_GETTING, MODE_SETTING, MODE_RESETTING } mode = MODE_EDITING;
+  enum { MODE_EDITING, MODE_GETTING, MODE_SETTING, MODE_RESETTING, MODE_DUMPING } mode = MODE_EDITING;
   const TCHAR *verb = argv[0];
   const TCHAR *service_name = argv[1];
   bool getting = false;
@@ -906,6 +906,7 @@ int pre_edit_service(int argc, TCHAR **argv) {
     mandatory = 3;
     mode = MODE_RESETTING;
   }
+  else if (str_equiv(verb, _T("dump"))) mode = MODE_DUMPING;
   if (argc < mandatory) return usage(1);
 
   const TCHAR *parameter = 0;
@@ -980,7 +981,7 @@ int pre_edit_service(int argc, TCHAR **argv) {
 
   service->type = qsc->dwServiceType;
   if (! (service->type & SERVICE_WIN32_OWN_PROCESS)) {
-    if (mode != MODE_GETTING) {
+    if (mode != MODE_GETTING && mode != MODE_DUMPING) {
       HeapFree(GetProcessHeap(), 0, qsc);
       CloseServiceHandle(service->handle);
       CloseServiceHandle(services);
@@ -990,7 +991,7 @@ int pre_edit_service(int argc, TCHAR **argv) {
   }
 
   if (get_service_startup(service->name, service->handle, qsc, &service->startup)) {
-    if (mode != MODE_GETTING) {
+    if (mode != MODE_GETTING && mode != MODE_DUMPING) {
       HeapFree(GetProcessHeap(), 0, qsc);
       CloseServiceHandle(service->handle);
       CloseServiceHandle(services);
@@ -999,7 +1000,7 @@ int pre_edit_service(int argc, TCHAR **argv) {
   }
 
   if (get_service_username(service->name, qsc, &service->username, &service->usernamelen)) {
-    if (mode != MODE_GETTING) {
+    if (mode != MODE_GETTING && mode != MODE_DUMPING) {
       HeapFree(GetProcessHeap(), 0, qsc);
       CloseServiceHandle(service->handle);
       CloseServiceHandle(services);
@@ -1019,7 +1020,7 @@ int pre_edit_service(int argc, TCHAR **argv) {
 
   /* Get extended system details. */
   if (get_service_description(service->name, service->handle, _countof(service->description), service->description)) {
-    if (mode != MODE_GETTING) {
+    if (mode != MODE_GETTING && mode != MODE_DUMPING) {
       CloseServiceHandle(service->handle);
       CloseServiceHandle(services);
       return 6;
@@ -1027,7 +1028,7 @@ int pre_edit_service(int argc, TCHAR **argv) {
   }
 
   if (get_service_dependencies(service->name, service->handle, &service->dependencies, &service->dependencieslen)) {
-    if (mode != MODE_GETTING) {
+    if (mode != MODE_GETTING && mode != MODE_DUMPING) {
       CloseServiceHandle(service->handle);
       CloseServiceHandle(services);
       return 7;
@@ -1041,7 +1042,7 @@ int pre_edit_service(int argc, TCHAR **argv) {
 
   if (! service->exe[0]) {
     service->native = true;
-    if (mode != MODE_GETTING) print_message(stderr, NSSM_MESSAGE_INVALID_SERVICE, service->name, NSSM, service->image);
+    if (mode != MODE_GETTING && mode != MODE_DUMPING) print_message(stderr, NSSM_MESSAGE_INVALID_SERVICE, service->name, NSSM, service->image);
   }
 
   /* Editing with the GUI. */
@@ -1050,6 +1051,31 @@ int pre_edit_service(int argc, TCHAR **argv) {
     return 0;
   }
 
+  HKEY key;
+  value_t value;
+  int ret;
+
+  if (mode == MODE_DUMPING) {
+    if (service->native) key = 0;
+    else {
+      key = open_registry(service->name, KEY_READ);
+      if (! key) return 4;
+    }
+
+    ret = 0;
+    for (i = 0; settings[i].name; i++) {
+      setting = &settings[i];
+      if (! setting->native && service->native) continue;
+      if (dump_setting(service->name, key, service->handle, setting)) ret++;
+    }
+
+    if (! service->native) RegCloseKey(key);
+    CloseServiceHandle(service->handle);
+
+    if (ret) return 1;
+    return 0;
+  }
+
   /* Trying to manage App* parameters for a non-NSSM service. */
   if (! setting->native && service->native) {
     CloseServiceHandle(service->handle);
@@ -1057,10 +1083,6 @@ int pre_edit_service(int argc, TCHAR **argv) {
     return 1;
   }
 
-  HKEY key;
-  value_t value;
-  int ret;
-
   if (mode == MODE_GETTING) {
     if (! service->native) {
       key = open_registry(service->name, KEY_READ);
@@ -1083,7 +1105,7 @@ int pre_edit_service(int argc, TCHAR **argv) {
         break;
 
       case REG_DWORD:
-        _tprintf(_T("%u\n"), value.numeric);
+        _tprintf(_T("%lu\n"), value.numeric);
         break;
     }
 

+ 286 - 66
settings.cpp

@@ -17,6 +17,14 @@ static inline int is_default(const TCHAR *value) {
   return (str_equiv(value, NSSM_DEFAULT_STRING) || str_equiv(value, _T("*")) || ! value[0]);
 }
 
+/* What type of parameter is it parameter? */
+static inline bool is_string_type(const unsigned long type) {
+  return (type == REG_MULTI_SZ || type == REG_EXPAND_SZ || type == REG_SZ);
+}
+static inline bool is_numeric_type(const unsigned long type) {
+  return (type == REG_DWORD);
+}
+
 static int value_from_string(const TCHAR *name, value_t *value, const TCHAR *string) {
   size_t len = _tcslen(string);
   if (! len++) {
@@ -110,6 +118,42 @@ static int setting_get_string(const TCHAR *service_name, void *param, const TCHA
   return value_from_string(name, value, buffer);
 }
 
+static int setting_not_dumpable(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
+  return 0;
+}
+
+static int setting_dump_string(const TCHAR *service_name, void *param, const TCHAR *name, const value_t *value, const TCHAR *additional) {
+  TCHAR quoted_service_name[SERVICE_NAME_LENGTH * 2];
+  TCHAR quoted_value[VALUE_LENGTH * 2];
+  TCHAR quoted_additional[VALUE_LENGTH * 2];
+  TCHAR quoted_nssm[EXE_LENGTH * 2];
+
+  if (quote(service_name, quoted_service_name, _countof(quoted_service_name))) return 1;
+
+  if (additional) {
+    if (_tcslen(additional)) {
+      if (quote(additional, quoted_additional, _countof(quoted_additional))) return 3;
+    }
+  else _sntprintf_s(quoted_additional, _countof(quoted_additional), _TRUNCATE, _T("\"\""));
+  }
+  else quoted_additional[0] = _T('\0');
+
+  unsigned long type = (unsigned long) param;
+  if (is_string_type(type)) {
+    if (_tcslen(value->string)) {
+      if (quote(value->string, quoted_value, _countof(quoted_value))) return 2;
+    }
+    else _sntprintf_s(quoted_value, _countof(quoted_value), _TRUNCATE, _T("\"\""));
+  }
+  else if (is_numeric_type(type)) _sntprintf_s(quoted_value, _countof(quoted_value), _TRUNCATE, _T("%lu"), value->numeric);
+  else return 2;
+
+  if (quote(nssm_exe(), quoted_nssm, _countof(quoted_nssm))) return 3;
+  if (_tcslen(quoted_additional)) _tprintf(_T("%s set %s %s %s %s\n"), quoted_nssm, quoted_service_name, name, quoted_additional, quoted_value);
+  else _tprintf(_T("%s set %s %s %s\n"), quoted_nssm, quoted_service_name, name, quoted_value);
+  return 0;
+}
+
 static int setting_set_exit_action(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
   unsigned long exitcode;
   TCHAR *code;
@@ -190,6 +234,40 @@ static int setting_get_exit_action(const TCHAR *service_name, void *param, const
   return 1;
 }
 
+static int setting_dump_exit_action(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
+  int errors = 0;
+  HKEY key = open_registry(service_name, NSSM_REG_EXIT, KEY_READ);
+  if (! key) return -1;
+
+  TCHAR code[16];
+  unsigned long index = 0;
+  while (true) {
+    int ret = enumerate_registry_values(key, &index, code, _countof(code));
+    if (ret == ERROR_NO_MORE_ITEMS) break;
+    if (ret != ERROR_SUCCESS) continue;
+    bool valid = true;
+    int i;
+    for (i = 0; i < _countof(code); i++) {
+      if (! code[i]) break;
+      if (code[i] >= _T('0') || code[i] <= _T('9')) continue;
+      valid = false;
+      break;
+    }
+    if (! valid) continue;
+
+    TCHAR *additional = (code[i]) ? code : NSSM_DEFAULT_STRING;
+
+    ret = setting_get_exit_action(service_name, 0, name, default_value, value, additional);
+    if (ret == 1) {
+      if (setting_dump_string(service_name, (void *) REG_SZ, name, value, additional)) errors++;
+    }
+    else if (ret < 0) errors++;
+  }
+
+  if (errors) return -1;
+  return 0;
+}
+
 static inline bool split_hook_name(const TCHAR *hook_name, TCHAR *hook_event, TCHAR *hook_action) {
   TCHAR *s;
 
@@ -235,6 +313,33 @@ static int setting_get_hook(const TCHAR *service_name, void *param, const TCHAR
   return 1;
 }
 
+static int setting_dump_hooks(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
+  int i, j;
+
+  int errors = 0;
+  for (i = 0; hook_event_strings[i]; i++) {
+    const TCHAR *hook_event = hook_event_strings[i];
+    for (j = 0; hook_action_strings[j]; j++) {
+      const TCHAR *hook_action = hook_action_strings[j];
+      if (! valid_hook_name(hook_event, hook_action, true)) continue;
+
+      TCHAR hook_name[HOOK_NAME_LENGTH];
+      _sntprintf_s(hook_name, _countof(hook_name), _TRUNCATE, _T("%s/%s"), hook_event, hook_action);
+
+      int ret = setting_get_hook(service_name, param, name, default_value, value, hook_name);
+      if (ret != 1) {
+        if (ret < 0) errors++;
+        continue;
+      }
+
+      if (setting_dump_string(service_name, (void *) REG_SZ, name, value, hook_name)) errors++;
+    }
+  }
+
+  if (errors) return -1;
+  return 0;
+}
+
 static int setting_set_affinity(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
   HKEY key = (HKEY) param;
   if (! key) return -1;
@@ -451,6 +556,39 @@ static int setting_get_environment(const TCHAR *service_name, void *param, const
   return ret;
 }
 
+static int setting_dump_environment(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
+  int errors = 0;
+  HKEY key = (HKEY) param;
+  if (! param) return -1;
+
+  TCHAR *env = 0;
+  unsigned long envlen;
+  if (get_environment((TCHAR *) service_name, key, (TCHAR *) name, &env, &envlen)) return -1;
+  if (! envlen) return 0;
+
+  TCHAR *s;
+  for (s = env; *s; s++) {
+    size_t len = _tcslen(s) + 2;
+    value->string = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, len * sizeof(TCHAR));
+    if (! value->string) {
+      print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("dump"), _T("setting_dump_environment"));
+      break;
+    }
+
+    _sntprintf_s(value->string, len, _TRUNCATE, _T("%c%s"), (s > env) ? _T('+') : _T(':'), s);
+    if (setting_dump_string(service_name, (void *) REG_SZ, name, value, 0)) errors++;
+    HeapFree(GetProcessHeap(), 0, value->string);
+    value->string = 0;
+
+    for ( ; *s; s++);
+  }
+
+  HeapFree(GetProcessHeap(), 0, env);
+
+  if (errors) return 1;
+  return 0;
+}
+
 static int setting_set_priority(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
   HKEY key = (HKEY) param;
   if (! param) return -1;
@@ -503,6 +641,13 @@ static int setting_get_priority(const TCHAR *service_name, void *param, const TC
   return value_from_string(name, value, priority_strings[priority_constant_to_index(constant)]);
 }
 
+static int setting_dump_priority(const TCHAR *service_name, void *key_ptr, const TCHAR *name, void *setting_ptr, value_t *value, const TCHAR *additional) {
+  settings_t *setting = (settings_t *) setting_ptr;
+  int ret = setting_get_priority(service_name, key_ptr, name, (void *) setting->default_value, value, 0);
+  if (ret != 1) return ret;
+  return setting_dump_string(service_name, (void *) REG_SZ, name, value, 0);
+}
+
 /* Functions to manage native service settings. */
 static int native_set_dependon(const TCHAR *service_name, SC_HANDLE service_handle, TCHAR **dependencies, unsigned long *dependencieslen, value_t *value, int type) {
   *dependencieslen = 0;
@@ -670,6 +815,41 @@ static int native_get_dependongroup(const TCHAR *service_name, void *param, cons
   return ret;
 }
 
+static int setting_dump_dependon(const TCHAR *service_name, SC_HANDLE service_handle, const TCHAR *name, int type, value_t *value) {
+  int errors = 0;
+
+  TCHAR *dependencies = 0;
+  unsigned long dependencieslen;
+  if (get_service_dependencies(service_name, service_handle, &dependencies, &dependencieslen, type)) return -1;
+  if (! dependencieslen) return 0;
+
+  TCHAR *s;
+  for (s = dependencies; *s; s++) {
+    size_t len = _tcslen(s) + 2;
+    value->string = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, len * sizeof(TCHAR));
+    if (! value->string) {
+      print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("dump"), _T("setting_dump_dependon"));
+      break;
+    }
+
+    _sntprintf_s(value->string, len, _TRUNCATE, _T("%c%s"), (s > dependencies) ? _T('+') : _T(':'), s);
+    if (setting_dump_string(service_name, (void *) REG_SZ, name, value, 0)) errors++;
+    HeapFree(GetProcessHeap(), 0, value->string);
+    value->string = 0;
+
+    for ( ; *s; s++);
+  }
+
+  HeapFree(GetProcessHeap(), 0, dependencies);
+
+  if (errors) return 1;
+  return 0;
+}
+
+static int native_dump_dependongroup(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
+  return setting_dump_dependon(service_name, (SC_HANDLE) param, name, DEPENDENCY_GROUPS, value);
+}
+
 static int native_set_dependonservice(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
   SC_HANDLE service_handle = (SC_HANDLE) param;
   if (! service_handle) return -1;
@@ -750,6 +930,10 @@ static int native_get_dependonservice(const TCHAR *service_name, void *param, co
   return ret;
 }
 
+static int native_dump_dependonservice(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
+  return setting_dump_dependon(service_name, (SC_HANDLE) param, name, DEPENDENCY_SERVICES, value);
+}
+
 int native_set_description(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
   SC_HANDLE service_handle = (SC_HANDLE) param;
   if (! service_handle) return -1;
@@ -832,6 +1016,15 @@ int native_get_environment(const TCHAR *service_name, void *param, const TCHAR *
   return ret;
 }
 
+static int native_dump_environment(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
+  HKEY key = open_service_registry(service_name, KEY_READ, true);
+  if (! key) return -1;
+
+  int ret = setting_dump_environment(service_name, (void *) key, name, default_value, value, additional);
+  RegCloseKey(key);
+  return ret;
+}
+
 int native_set_imagepath(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
   SC_HANDLE service_handle = (SC_HANDLE) param;
   if (! service_handle) return -1;
@@ -953,6 +1146,20 @@ int native_get_objectname(const TCHAR *service_name, void *param, const TCHAR *n
   return ret;
 }
 
+int native_dump_objectname(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
+  int ret = native_get_objectname(service_name, param, name, default_value, value, additional);
+  if (ret != 1) return ret;
+
+  /* Do we need to dump a dummy password? */
+  if (! well_known_username(value->string)) {
+    /* Parameters are the other way round. */
+    value_t inverted;
+    inverted.string = _T("****");
+    return setting_dump_string(service_name, (void *) REG_SZ, name, &inverted, value->string);
+  }
+  return setting_dump_string(service_name, (void *) REG_SZ, name, value, 0);
+}
+
 int native_set_startup(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
   SC_HANDLE service_handle = (SC_HANDLE) param;
   if (! service_handle) return -1;
@@ -1135,25 +1342,17 @@ int get_setting(const TCHAR *service_name, HKEY key, settings_t *setting, value_
   if (! key) return -1;
   int ret;
 
-  switch (setting->type) {
-    case REG_EXPAND_SZ:
-    case REG_MULTI_SZ:
-    case REG_SZ:
-      value->string = (TCHAR *) setting->default_value;
-      if (setting->get) ret = setting->get(service_name, (void *) key, setting->name, setting->default_value, value, additional);
-      else ret = -1;
-      break;
-
-    case REG_DWORD:
-      value->numeric = PtrToUlong(setting->default_value);
-      if (setting->get) ret = setting->get(service_name, (void *) key, setting->name, setting->default_value, value, additional);
-      else ret = -1;
-      break;
-
-    default:
-      ret = -1;
-      break;
+  if (is_string_type(setting->type)) {
+    value->string = (TCHAR *) setting->default_value;
+    if (setting->get) ret = setting->get(service_name, (void *) key, setting->name, setting->default_value, value, additional);
+    else ret = -1;
+  }
+  else if (is_numeric_type(setting->type)) {
+    value->numeric = PtrToUlong(setting->default_value);
+    if (setting->get) ret = setting->get(service_name, (void *) key, setting->name, setting->default_value, value, additional);
+    else ret = -1;
   }
+  else ret = -1;
 
   if (ret < 0) print_message(stderr, NSSM_MESSAGE_GET_SETTING_FAILED, setting->name, service_name);
 
@@ -1165,54 +1364,75 @@ int get_setting(const TCHAR *service_name, SC_HANDLE service_handle, settings_t
   return setting->get(service_name, service_handle, setting->name, 0, value, additional);
 }
 
+int dump_setting(const TCHAR *service_name, HKEY key, SC_HANDLE service_handle, settings_t *setting) {
+  void *param;
+  if (setting->native) {
+    if (! service_handle) return -1;
+    param = (void *) service_handle;
+  }
+  else {
+    /* Will be null for native services. */
+    param = (void *) key;
+  }
+
+  value_t value = { 0 };
+  int ret;
+
+  if (setting->dump) return setting->dump(service_name, param, setting->name, (void *) setting, &value, 0);
+  if (setting->native) ret = get_setting(service_name, service_handle, setting, &value, 0);
+  else ret = get_setting(service_name, key, setting, &value, 0);
+  if (ret != 1) return ret;
+  return setting_dump_string(service_name, (void *) setting->type, setting->name, &value, 0);
+}
+
 settings_t settings[] = {
-  { NSSM_REG_EXE, REG_EXPAND_SZ, (void *) _T(""), false, 0, setting_set_string, setting_get_string },
-  { NSSM_REG_FLAGS, REG_EXPAND_SZ, (void *) _T(""), false, 0, setting_set_string, setting_get_string },
-  { NSSM_REG_DIR, REG_EXPAND_SZ, (void *) _T(""), false, 0, setting_set_string, setting_get_string },
-  { NSSM_REG_EXIT, REG_SZ, (void *) exit_action_strings[NSSM_EXIT_RESTART], false, ADDITIONAL_MANDATORY, setting_set_exit_action, setting_get_exit_action },
-  { NSSM_REG_HOOK, REG_SZ, (void *) _T(""), false, ADDITIONAL_MANDATORY, setting_set_hook, setting_get_hook },
-  { NSSM_REG_AFFINITY, REG_SZ, 0, false, 0, setting_set_affinity, setting_get_affinity },
-  { NSSM_REG_ENV, REG_MULTI_SZ, NULL, false, ADDITIONAL_CRLF, setting_set_environment, setting_get_environment },
-  { NSSM_REG_ENV_EXTRA, REG_MULTI_SZ, NULL, false, ADDITIONAL_CRLF, setting_set_environment, setting_get_environment },
-  { NSSM_REG_NO_CONSOLE, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number },
-  { NSSM_REG_PRIORITY, REG_SZ, (void *) priority_strings[NSSM_NORMAL_PRIORITY], false, 0, setting_set_priority, setting_get_priority },
-  { NSSM_REG_RESTART_DELAY, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number },
-  { NSSM_REG_STDIN, REG_EXPAND_SZ, NULL, false, 0, setting_set_string, setting_get_string },
-  { NSSM_REG_STDIN NSSM_REG_STDIO_SHARING, REG_DWORD, (void *) NSSM_STDIN_SHARING, false, 0, setting_set_number, setting_get_number },
-  { NSSM_REG_STDIN NSSM_REG_STDIO_DISPOSITION, REG_DWORD, (void *) NSSM_STDIN_DISPOSITION, false, 0, setting_set_number, setting_get_number },
-  { NSSM_REG_STDIN NSSM_REG_STDIO_FLAGS, REG_DWORD, (void *) NSSM_STDIN_FLAGS, false, 0, setting_set_number, setting_get_number },
-  { NSSM_REG_STDOUT, REG_EXPAND_SZ, NULL, false, 0, setting_set_string, setting_get_string },
-  { NSSM_REG_STDOUT NSSM_REG_STDIO_SHARING, REG_DWORD, (void *) NSSM_STDOUT_SHARING, false, 0, setting_set_number, setting_get_number },
-  { NSSM_REG_STDOUT NSSM_REG_STDIO_DISPOSITION, REG_DWORD, (void *) NSSM_STDOUT_DISPOSITION, false, 0, setting_set_number, setting_get_number },
-  { NSSM_REG_STDOUT NSSM_REG_STDIO_FLAGS, REG_DWORD, (void *) NSSM_STDOUT_FLAGS, false, 0, setting_set_number, setting_get_number },
-  { NSSM_REG_STDOUT NSSM_REG_STDIO_COPY_AND_TRUNCATE, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number },
-  { NSSM_REG_STDERR, REG_EXPAND_SZ, NULL, false, 0, setting_set_string, setting_get_string },
-  { NSSM_REG_STDERR NSSM_REG_STDIO_SHARING, REG_DWORD, (void *) NSSM_STDERR_SHARING, false, 0, setting_set_number, setting_get_number },
-  { NSSM_REG_STDERR NSSM_REG_STDIO_DISPOSITION, REG_DWORD, (void *) NSSM_STDERR_DISPOSITION, false, 0, setting_set_number, setting_get_number },
-  { NSSM_REG_STDERR NSSM_REG_STDIO_FLAGS, REG_DWORD, (void *) NSSM_STDERR_FLAGS, false, 0, setting_set_number, setting_get_number },
-  { NSSM_REG_STDERR NSSM_REG_STDIO_COPY_AND_TRUNCATE, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number },
-  { NSSM_REG_STOP_METHOD_SKIP, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number },
-  { NSSM_REG_KILL_CONSOLE_GRACE_PERIOD, REG_DWORD, (void *) NSSM_KILL_CONSOLE_GRACE_PERIOD, false, 0, setting_set_number, setting_get_number },
-  { NSSM_REG_KILL_WINDOW_GRACE_PERIOD, REG_DWORD, (void *) NSSM_KILL_WINDOW_GRACE_PERIOD, false, 0, setting_set_number, setting_get_number },
-  { NSSM_REG_KILL_THREADS_GRACE_PERIOD, REG_DWORD, (void *) NSSM_KILL_THREADS_GRACE_PERIOD, false, 0, setting_set_number, setting_get_number },
-  { NSSM_REG_KILL_PROCESS_TREE, REG_DWORD, (void *) 1, false, 0, setting_set_number, setting_get_number },
-  { NSSM_REG_THROTTLE, REG_DWORD, (void *) NSSM_RESET_THROTTLE_RESTART, false, 0, setting_set_number, setting_get_number },
-  { NSSM_REG_HOOK_SHARE_OUTPUT_HANDLES, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number },
-  { NSSM_REG_ROTATE, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number },
-  { NSSM_REG_ROTATE_ONLINE, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number },
-  { NSSM_REG_ROTATE_SECONDS, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number },
-  { NSSM_REG_ROTATE_BYTES_LOW, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number },
-  { NSSM_REG_ROTATE_BYTES_HIGH, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number },
-  { NSSM_REG_ROTATE_DELAY, REG_DWORD, (void *) NSSM_ROTATE_DELAY, false, 0, setting_set_number, setting_get_number },
-  { NSSM_NATIVE_DEPENDONGROUP, REG_MULTI_SZ, NULL, true, ADDITIONAL_CRLF, native_set_dependongroup, native_get_dependongroup },
-  { NSSM_NATIVE_DEPENDONSERVICE, REG_MULTI_SZ, NULL, true, ADDITIONAL_CRLF, native_set_dependonservice, native_get_dependonservice },
-  { NSSM_NATIVE_DESCRIPTION, REG_SZ, _T(""), true, 0, native_set_description, native_get_description },
-  { NSSM_NATIVE_DISPLAYNAME, REG_SZ, NULL, true, 0, native_set_displayname, native_get_displayname },
-  { NSSM_NATIVE_ENVIRONMENT, REG_MULTI_SZ, NULL, true, ADDITIONAL_CRLF, native_set_environment, native_get_environment },
-  { NSSM_NATIVE_IMAGEPATH, REG_EXPAND_SZ, NULL, true, 0, native_set_imagepath, native_get_imagepath },
-  { NSSM_NATIVE_OBJECTNAME, REG_SZ, NSSM_LOCALSYSTEM_ACCOUNT, true, 0, native_set_objectname, native_get_objectname },
-  { NSSM_NATIVE_NAME, REG_SZ, NULL, true, 0, native_set_name, native_get_name },
-  { NSSM_NATIVE_STARTUP, REG_SZ, NULL, true, 0, native_set_startup, native_get_startup },
-  { NSSM_NATIVE_TYPE, REG_SZ, NULL, true, 0, native_set_type, native_get_type },
+  { NSSM_REG_EXE, REG_EXPAND_SZ, (void *) _T(""), false, 0, setting_set_string, setting_get_string, 0 },
+  { NSSM_REG_FLAGS, REG_EXPAND_SZ, (void *) _T(""), false, 0, setting_set_string, setting_get_string, 0 },
+  { NSSM_REG_DIR, REG_EXPAND_SZ, (void *) _T(""), false, 0, setting_set_string, setting_get_string, 0 },
+  { NSSM_REG_EXIT, REG_SZ, (void *) exit_action_strings[NSSM_EXIT_RESTART], false, ADDITIONAL_MANDATORY, setting_set_exit_action, setting_get_exit_action, setting_dump_exit_action },
+  { NSSM_REG_HOOK, REG_SZ, (void *) _T(""), false, ADDITIONAL_MANDATORY, setting_set_hook, setting_get_hook, setting_dump_hooks },
+  { NSSM_REG_AFFINITY, REG_SZ, 0, false, 0, setting_set_affinity, setting_get_affinity, 0 },
+  { NSSM_REG_ENV, REG_MULTI_SZ, NULL, false, ADDITIONAL_CRLF, setting_set_environment, setting_get_environment, setting_dump_environment },
+  { NSSM_REG_ENV_EXTRA, REG_MULTI_SZ, NULL, false, ADDITIONAL_CRLF, setting_set_environment, setting_get_environment, setting_dump_environment },
+  { NSSM_REG_NO_CONSOLE, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number, 0 },
+  { NSSM_REG_PRIORITY, REG_SZ, (void *) priority_strings[NSSM_NORMAL_PRIORITY], false, 0, setting_set_priority, setting_get_priority, setting_dump_priority },
+  { NSSM_REG_RESTART_DELAY, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number, 0 },
+  { NSSM_REG_STDIN, REG_EXPAND_SZ, NULL, false, 0, setting_set_string, setting_get_string, 0 },
+  { NSSM_REG_STDIN NSSM_REG_STDIO_SHARING, REG_DWORD, (void *) NSSM_STDIN_SHARING, false, 0, setting_set_number, setting_get_number, 0 },
+  { NSSM_REG_STDIN NSSM_REG_STDIO_DISPOSITION, REG_DWORD, (void *) NSSM_STDIN_DISPOSITION, false, 0, setting_set_number, setting_get_number, 0 },
+  { NSSM_REG_STDIN NSSM_REG_STDIO_FLAGS, REG_DWORD, (void *) NSSM_STDIN_FLAGS, false, 0, setting_set_number, setting_get_number, 0 },
+  { NSSM_REG_STDOUT, REG_EXPAND_SZ, NULL, false, 0, setting_set_string, setting_get_string, 0 },
+  { NSSM_REG_STDOUT NSSM_REG_STDIO_SHARING, REG_DWORD, (void *) NSSM_STDOUT_SHARING, false, 0, setting_set_number, setting_get_number, 0 },
+  { NSSM_REG_STDOUT NSSM_REG_STDIO_DISPOSITION, REG_DWORD, (void *) NSSM_STDOUT_DISPOSITION, false, 0, setting_set_number, setting_get_number, 0 },
+  { NSSM_REG_STDOUT NSSM_REG_STDIO_FLAGS, REG_DWORD, (void *) NSSM_STDOUT_FLAGS, false, 0, setting_set_number, setting_get_number, 0 },
+  { NSSM_REG_STDOUT NSSM_REG_STDIO_COPY_AND_TRUNCATE, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number, 0 },
+  { NSSM_REG_STDERR, REG_EXPAND_SZ, NULL, false, 0, setting_set_string, setting_get_string, 0 },
+  { NSSM_REG_STDERR NSSM_REG_STDIO_SHARING, REG_DWORD, (void *) NSSM_STDERR_SHARING, false, 0, setting_set_number, setting_get_number, 0 },
+  { NSSM_REG_STDERR NSSM_REG_STDIO_DISPOSITION, REG_DWORD, (void *) NSSM_STDERR_DISPOSITION, false, 0, setting_set_number, setting_get_number, 0 },
+  { NSSM_REG_STDERR NSSM_REG_STDIO_FLAGS, REG_DWORD, (void *) NSSM_STDERR_FLAGS, false, 0, setting_set_number, setting_get_number, 0 },
+  { NSSM_REG_STDERR NSSM_REG_STDIO_COPY_AND_TRUNCATE, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number, 0 },
+  { NSSM_REG_STOP_METHOD_SKIP, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number, 0 },
+  { NSSM_REG_KILL_CONSOLE_GRACE_PERIOD, REG_DWORD, (void *) NSSM_KILL_CONSOLE_GRACE_PERIOD, false, 0, setting_set_number, setting_get_number, 0 },
+  { NSSM_REG_KILL_WINDOW_GRACE_PERIOD, REG_DWORD, (void *) NSSM_KILL_WINDOW_GRACE_PERIOD, false, 0, setting_set_number, setting_get_number, 0 },
+  { NSSM_REG_KILL_THREADS_GRACE_PERIOD, REG_DWORD, (void *) NSSM_KILL_THREADS_GRACE_PERIOD, false, 0, setting_set_number, setting_get_number, 0 },
+  { NSSM_REG_KILL_PROCESS_TREE, REG_DWORD, (void *) 1, false, 0, setting_set_number, setting_get_number, 0 },
+  { NSSM_REG_THROTTLE, REG_DWORD, (void *) NSSM_RESET_THROTTLE_RESTART, false, 0, setting_set_number, setting_get_number, 0 },
+  { NSSM_REG_HOOK_SHARE_OUTPUT_HANDLES, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number, 0 },
+  { NSSM_REG_ROTATE, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number, 0 },
+  { NSSM_REG_ROTATE_ONLINE, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number, 0 },
+  { NSSM_REG_ROTATE_SECONDS, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number, 0 },
+  { NSSM_REG_ROTATE_BYTES_LOW, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number, 0 },
+  { NSSM_REG_ROTATE_BYTES_HIGH, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number, 0 },
+  { NSSM_REG_ROTATE_DELAY, REG_DWORD, (void *) NSSM_ROTATE_DELAY, false, 0, setting_set_number, setting_get_number, 0 },
+  { NSSM_NATIVE_DEPENDONGROUP, REG_MULTI_SZ, NULL, true, ADDITIONAL_CRLF, native_set_dependongroup, native_get_dependongroup, native_dump_dependongroup },
+  { NSSM_NATIVE_DEPENDONSERVICE, REG_MULTI_SZ, NULL, true, ADDITIONAL_CRLF, native_set_dependonservice, native_get_dependonservice, native_dump_dependonservice },
+  { NSSM_NATIVE_DESCRIPTION, REG_SZ, _T(""), true, 0, native_set_description, native_get_description, 0 },
+  { NSSM_NATIVE_DISPLAYNAME, REG_SZ, NULL, true, 0, native_set_displayname, native_get_displayname, 0 },
+  { NSSM_NATIVE_ENVIRONMENT, REG_MULTI_SZ, NULL, true, ADDITIONAL_CRLF, native_set_environment, native_get_environment, native_dump_environment },
+  { NSSM_NATIVE_IMAGEPATH, REG_EXPAND_SZ, NULL, true, 0, native_set_imagepath, native_get_imagepath, setting_not_dumpable },
+  { NSSM_NATIVE_OBJECTNAME, REG_SZ, NSSM_LOCALSYSTEM_ACCOUNT, true, 0, native_set_objectname, native_get_objectname, native_dump_objectname },
+  { NSSM_NATIVE_NAME, REG_SZ, NULL, true, 0, native_set_name, native_get_name, setting_not_dumpable },
+  { NSSM_NATIVE_STARTUP, REG_SZ, NULL, true, 0, native_set_startup, native_get_startup, 0 },
+  { NSSM_NATIVE_TYPE, REG_SZ, NULL, true, 0, native_set_type, native_get_type, 0 },
   { NULL, NULL, NULL, NULL, NULL }
 };

+ 2 - 0
settings.h

@@ -38,11 +38,13 @@ typedef struct {
   int additional;
   setting_function_t set;
   setting_function_t get;
+  setting_function_t dump;
 } settings_t;
 
 int set_setting(const TCHAR *, HKEY, settings_t *, value_t *, const TCHAR *);
 int set_setting(const TCHAR *, SC_HANDLE, settings_t *, value_t *, const TCHAR *);
 int get_setting(const TCHAR *, HKEY, settings_t *, value_t *, const TCHAR *);
 int get_setting(const TCHAR *, SC_HANDLE, settings_t *, value_t *, const TCHAR *);
+int dump_setting(const TCHAR *, HKEY, SC_HANDLE, settings_t *);
 
 #endif