}\r
\r
int create_parameters(nssm_service_t *service, bool editing) {\r
- /* Get registry */\r
- TCHAR registry[KEY_LENGTH];\r
- if (_sntprintf_s(registry, _countof(registry), _TRUNCATE, NSSM_REGISTRY, service->name) < 0) {\r
- log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("NSSM_REGISTRY"), _T("create_parameters()"), 0);\r
- return 1;\r
- }\r
-\r
/* Try to open the registry */\r
- HKEY key;\r
- if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, registry, 0, 0, REG_OPTION_NON_VOLATILE, KEY_WRITE, 0, &key, 0) != ERROR_SUCCESS) {\r
- log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENKEY_FAILED, registry, error_string(GetLastError()), 0);\r
- return 2;\r
- }\r
+ HKEY key = open_registry(service->name, KEY_WRITE);\r
+ if (! key) return 1;\r
\r
/* Try to create the parameters */\r
if (set_expand_string(key, NSSM_REG_EXE, service->exe)) {\r
RegDeleteKey(HKEY_LOCAL_MACHINE, NSSM_REGISTRY);\r
RegCloseKey(key);\r
- return 3;\r
+ return 2;\r
}\r
if (set_expand_string(key, NSSM_REG_FLAGS, service->flags)) {\r
RegDeleteKey(HKEY_LOCAL_MACHINE, NSSM_REGISTRY);\r
RegCloseKey(key);\r
- return 4;\r
+ return 3;\r
}\r
if (set_expand_string(key, NSSM_REG_DIR, service->dir)) {\r
RegDeleteKey(HKEY_LOCAL_MACHINE, NSSM_REGISTRY);\r
RegCloseKey(key);\r
- return 5;\r
+ return 4;\r
}\r
\r
/* Other non-default parameters. May fail. */\r
if (! ok) *buffer = default_value;\r
}\r
\r
-int get_parameters(nssm_service_t *service, STARTUPINFO *si) {\r
- unsigned long ret;\r
-\r
+HKEY open_registry(const TCHAR *service_name, const TCHAR *sub, REGSAM sam) {\r
/* Get registry */\r
TCHAR registry[KEY_LENGTH];\r
- if (_sntprintf_s(registry, _countof(registry), _TRUNCATE, NSSM_REGISTRY, service->name) < 0) {\r
- log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("NSSM_REGISTRY"), _T("get_parameters()"), 0);\r
- return 1;\r
+ HKEY key;\r
+ int ret;\r
+\r
+ if (sub) ret = _sntprintf_s(registry, _countof(registry), _TRUNCATE, NSSM_REGISTRY _T("\\%s"), service_name, sub);\r
+ else ret = _sntprintf_s(registry, _countof(registry), _TRUNCATE, NSSM_REGISTRY, service_name);\r
+ if (ret < 0) {\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("NSSM_REGISTRY"), _T("open_registry()"), 0);\r
+ return 0;\r
}\r
\r
- /* Try to open the registry */\r
- HKEY key;\r
- if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, registry, 0, KEY_READ, &key) != ERROR_SUCCESS) {\r
- log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENKEY_FAILED, registry, error_string(GetLastError()), 0);\r
- return 2;\r
+ if (sam & KEY_WRITE) {\r
+ if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, registry, 0, 0, REG_OPTION_NON_VOLATILE, sam, 0, &key, 0) != ERROR_SUCCESS) {\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENKEY_FAILED, registry, error_string(GetLastError()), 0);\r
+ return 0;\r
+ }\r
}\r
+ else {\r
+ if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, registry, 0, sam, &key) != ERROR_SUCCESS) {\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENKEY_FAILED, registry, error_string(GetLastError()), 0);\r
+ return 0;\r
+ }\r
+ }\r
+\r
+ return key;\r
+}\r
+\r
+HKEY open_registry(const TCHAR *service_name, REGSAM sam) {\r
+ return open_registry(service_name, 0, sam);\r
+}\r
+\r
+int get_parameters(nssm_service_t *service, STARTUPINFO *si) {\r
+ unsigned long ret;\r
+\r
+ /* Try to open the registry */\r
+ HKEY key = open_registry(service->name, KEY_READ);\r
+ if (! key) return 1;\r
\r
/* Try to get executable file - MUST succeed */\r
if (expand_parameter(key, NSSM_REG_EXE, service->exe, sizeof(service->exe), false)) {\r
return 0;\r
}\r
\r
-int get_exit_action(TCHAR *service_name, unsigned long *ret, TCHAR *action, bool *default_action) {\r
+/*\r
+ Sets the string for the exit action corresponding to the exit code.\r
+\r
+ ret is a pointer to an unsigned long containing the exit code.\r
+ If ret is NULL, we retrieve the default exit action unconditionally.\r
+\r
+ action is a buffer which receives the string.\r
+\r
+ default_action is a pointer to a bool which is set to false if there\r
+ was an explicit string for the given exit code, or true if we are\r
+ returning the default action.\r
+\r
+ Returns: 0 on success.\r
+ 1 on error.\r
+*/\r
+int get_exit_action(const TCHAR *service_name, unsigned long *ret, TCHAR *action, bool *default_action) {\r
/* Are we returning the default action or a status-specific one? */\r
*default_action = ! ret;\r
\r
- /* Get registry */\r
- TCHAR registry[KEY_LENGTH];\r
- if (_sntprintf_s(registry, _countof(registry), _TRUNCATE, NSSM_REGISTRY _T("\\%s"), service_name, NSSM_REG_EXIT) < 0) {\r
- log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("NSSM_REG_EXIT"), _T("get_exit_action()"), 0);\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
- log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENKEY_FAILED, registry, error_string(GetLastError()), 0);\r
- return 2;\r
- }\r
+ HKEY key = open_registry(service_name, NSSM_REG_EXIT, KEY_READ);\r
+ if (! key) return 1;\r
\r
unsigned long type = REG_SZ;\r
unsigned long action_len = ACTION_LEN;\r