X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=registry.cpp;h=228ef8d232fe53a6bce1c5ccc1f8573f186cfb6d;hb=b6f7fe3b11fd130f46eb6b5009391cc3bb0cad49;hp=5953f8a9f867d09ec6a489e292be085c64483796;hpb=66861c12e7d515f4d0e779102780d89e3fcd2a12;p=nssm.git diff --git a/registry.cpp b/registry.cpp index 5953f8a..228ef8d 100644 --- a/registry.cpp +++ b/registry.cpp @@ -2,6 +2,39 @@ extern const TCHAR *exit_action_strings[]; +static int service_registry_path(const TCHAR *service_name, bool parameters, const TCHAR *sub, TCHAR *buffer, unsigned long buflen) { + int ret; + + if (parameters) { + if (sub) ret = _sntprintf_s(buffer, buflen, _TRUNCATE, NSSM_REGISTRY _T("\\") NSSM_REG_PARAMETERS _T("\\%s"), service_name, sub); + else ret = _sntprintf_s(buffer, buflen, _TRUNCATE, NSSM_REGISTRY _T("\\") NSSM_REG_PARAMETERS, service_name); + } + else ret = _sntprintf_s(buffer, buflen, _TRUNCATE, NSSM_REGISTRY, service_name); + + return ret; +} + +static HKEY open_registry_key(const TCHAR *registry, REGSAM sam, bool must_exist) { + HKEY key; + + if (sam & KEY_SET_VALUE) { + if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, registry, 0, 0, REG_OPTION_NON_VOLATILE, sam, 0, &key, 0) != ERROR_SUCCESS) { + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENKEY_FAILED, registry, error_string(GetLastError()), 0); + return 0; + } + } + else { + long error = RegOpenKeyEx(HKEY_LOCAL_MACHINE, registry, 0, sam, &key); + if (error != ERROR_SUCCESS) { + if (error == ERROR_FILE_NOT_FOUND && ! must_exist) return 0; + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENKEY_FAILED, registry, error_string(GetLastError()), 0); + return 0; + } + } + + return key; +} + int create_messages() { HKEY key; @@ -17,7 +50,7 @@ int create_messages() { } /* Get path of this program */ - const TCHAR *path = nssm_imagepath(); + const TCHAR *path = nssm_unquoted_imagepath(); /* Try to register the module but don't worry so much on failure */ RegSetValueEx(key, _T("EventMessageFile"), 0, REG_SZ, (const unsigned char *) path, (unsigned long) (_tcslen(path) + 1) * sizeof(TCHAR)); @@ -32,19 +65,23 @@ int create_parameters(nssm_service_t *service, bool editing) { HKEY key = open_registry(service->name, KEY_WRITE); if (! key) return 1; + /* Remember parameters in case we need to delete them. */ + TCHAR registry[KEY_LENGTH]; + int ret = service_registry_path(service->name, true, 0, registry, _countof(registry)); + /* Try to create the parameters */ if (set_expand_string(key, NSSM_REG_EXE, service->exe)) { - RegDeleteKey(HKEY_LOCAL_MACHINE, NSSM_REGISTRY); + if (ret > 0) RegDeleteKey(HKEY_LOCAL_MACHINE, registry); RegCloseKey(key); return 2; } if (set_expand_string(key, NSSM_REG_FLAGS, service->flags)) { - RegDeleteKey(HKEY_LOCAL_MACHINE, NSSM_REGISTRY); + if (ret > 0) RegDeleteKey(HKEY_LOCAL_MACHINE, registry); RegCloseKey(key); return 3; } if (set_expand_string(key, NSSM_REG_DIR, service->dir)) { - RegDeleteKey(HKEY_LOCAL_MACHINE, NSSM_REGISTRY); + if (ret > 0) RegDeleteKey(HKEY_LOCAL_MACHINE, registry); RegCloseKey(key); return 4; } @@ -152,7 +189,7 @@ int create_parameters(nssm_service_t *service, bool editing) { int create_exit_action(TCHAR *service_name, const TCHAR *action_string, bool editing) { /* Get registry */ TCHAR registry[KEY_LENGTH]; - if (_sntprintf_s(registry, _countof(registry), _TRUNCATE, NSSM_REGISTRY _T("\\%s"), service_name, NSSM_REG_EXIT) < 0) { + if (service_registry_path(service_name, true, NSSM_REG_EXIT, registry, _countof(registry)) < 0) { log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("NSSM_REG_EXIT"), _T("create_exit_action()"), 0); return 1; } @@ -456,39 +493,6 @@ void override_milliseconds(TCHAR *service_name, HKEY key, TCHAR *value, unsigned if (! ok) *buffer = default_value; } -static int service_registry_path(const TCHAR *service_name, bool parameters, const TCHAR *sub, TCHAR *buffer, unsigned long buflen) { - int ret; - - if (parameters) { - if (sub) ret = _sntprintf_s(buffer, buflen, _TRUNCATE, NSSM_REGISTRY _T("\\") NSSM_REG_PARAMETERS _T("\\%s"), service_name, sub); - else ret = _sntprintf_s(buffer, buflen, _TRUNCATE, NSSM_REGISTRY _T("\\") NSSM_REG_PARAMETERS, service_name); - } - else ret = _sntprintf_s(buffer, buflen, _TRUNCATE, NSSM_REGISTRY, service_name); - - return ret; -} - -static HKEY open_registry_key(const TCHAR *registry, REGSAM sam, bool must_exist) { - HKEY key; - - if (sam & KEY_SET_VALUE) { - if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, registry, 0, 0, REG_OPTION_NON_VOLATILE, sam, 0, &key, 0) != ERROR_SUCCESS) { - log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENKEY_FAILED, registry, error_string(GetLastError()), 0); - return 0; - } - } - else { - long error = RegOpenKeyEx(HKEY_LOCAL_MACHINE, registry, 0, sam, &key); - if (error != ERROR_SUCCESS) { - if (error == ERROR_FILE_NOT_FOUND && ! must_exist) return 0; - log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENKEY_FAILED, registry, error_string(GetLastError()), 0); - return 0; - } - } - - return key; -} - HKEY open_service_registry(const TCHAR *service_name, REGSAM sam, bool must_exist) { /* Get registry */ TCHAR registry[KEY_LENGTH];