X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=registry.cpp;h=de3127fc9ca8428699430d4d4eacd222683d74d5;hb=32a1d0fab47a050086106aac346342c70f4d985b;hp=6c1567ce152bb5806e329456f0ea542703849ced;hpb=1afadf5332638a0526feefaa4151b240224ab036;p=nssm.git diff --git a/registry.cpp b/registry.cpp index 6c1567c..de3127f 100644 --- a/registry.cpp +++ b/registry.cpp @@ -79,6 +79,8 @@ int create_parameters(nssm_service_t *service, bool editing) { else if (editing) RegDeleteValue(key, NSSM_REG_KILL_WINDOW_GRACE_PERIOD); if (service->kill_threads_delay != NSSM_KILL_THREADS_GRACE_PERIOD) set_number(key, NSSM_REG_KILL_THREADS_GRACE_PERIOD, service->kill_threads_delay); else if (editing) RegDeleteValue(key, NSSM_REG_KILL_THREADS_GRACE_PERIOD); + if (! service->kill_process_tree) set_number(key, NSSM_REG_KILL_PROCESS_TREE, 0); + else if (editing) RegDeleteValue(key, NSSM_REG_KILL_PROCESS_TREE); if (service->stdin_path[0] || editing) { if (service->stdin_path[0]) set_expand_string(key, NSSM_REG_STDIN, service->stdin_path); else if (editing) RegDeleteValue(key, NSSM_REG_STDIN); @@ -98,6 +100,8 @@ int create_parameters(nssm_service_t *service, bool editing) { else if (editing) delete_createfile_parameter(key, NSSM_REG_STDOUT, NSSM_REG_STDIO_DISPOSITION); if (service->stdout_flags != NSSM_STDOUT_FLAGS) set_createfile_parameter(key, NSSM_REG_STDOUT, NSSM_REG_STDIO_FLAGS, service->stdout_flags); else if (editing) delete_createfile_parameter(key, NSSM_REG_STDOUT, NSSM_REG_STDIO_FLAGS); + if (service->stdout_copy_and_truncate) set_createfile_parameter(key, NSSM_REG_STDOUT, NSSM_REG_STDIO_COPY_AND_TRUNCATE, 1); + else if (editing) delete_createfile_parameter(key, NSSM_REG_STDOUT, NSSM_REG_STDIO_COPY_AND_TRUNCATE); } if (service->stderr_path[0] || editing) { if (service->stderr_path[0]) set_expand_string(key, NSSM_REG_STDERR, service->stderr_path); @@ -108,6 +112,8 @@ int create_parameters(nssm_service_t *service, bool editing) { else if (editing) delete_createfile_parameter(key, NSSM_REG_STDERR, NSSM_REG_STDIO_DISPOSITION); if (service->stderr_flags != NSSM_STDERR_FLAGS) set_createfile_parameter(key, NSSM_REG_STDERR, NSSM_REG_STDIO_FLAGS, service->stderr_flags); else if (editing) delete_createfile_parameter(key, NSSM_REG_STDERR, NSSM_REG_STDIO_FLAGS); + if (service->stderr_copy_and_truncate) set_createfile_parameter(key, NSSM_REG_STDERR, NSSM_REG_STDIO_COPY_AND_TRUNCATE, 1); + else if (editing) delete_createfile_parameter(key, NSSM_REG_STDERR, NSSM_REG_STDIO_COPY_AND_TRUNCATE); } if (service->rotate_files) set_number(key, NSSM_REG_ROTATE, 1); else if (editing) RegDeleteValue(key, NSSM_REG_ROTATE); @@ -119,6 +125,8 @@ int create_parameters(nssm_service_t *service, bool editing) { else if (editing) RegDeleteValue(key, NSSM_REG_ROTATE_BYTES_LOW); if (service->rotate_bytes_high) set_number(key, NSSM_REG_ROTATE_BYTES_HIGH, service->rotate_bytes_high); else if (editing) RegDeleteValue(key, NSSM_REG_ROTATE_BYTES_HIGH); + if (service->rotate_delay != NSSM_ROTATE_DELAY) set_number(key, NSSM_REG_ROTATE_DELAY, service->rotate_delay); + else if (editing) RegDeleteValue(key, NSSM_REG_ROTATE_DELAY); if (service->no_console) set_number(key, NSSM_REG_NO_CONSOLE, 1); else if (editing) RegDeleteValue(key, NSSM_REG_NO_CONSOLE); @@ -449,7 +457,7 @@ void override_milliseconds(TCHAR *service_name, HKEY key, TCHAR *value, unsigned if (! ok) *buffer = default_value; } -HKEY open_registry(const TCHAR *service_name, const TCHAR *sub, REGSAM sam) { +HKEY open_registry(const TCHAR *service_name, const TCHAR *sub, REGSAM sam, bool must_exist) { /* Get registry */ TCHAR registry[KEY_LENGTH]; HKEY key; @@ -462,14 +470,16 @@ HKEY open_registry(const TCHAR *service_name, const TCHAR *sub, REGSAM sam) { return 0; } - if (sam & KEY_WRITE) { + 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 { - if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, registry, 0, sam, &key) != ERROR_SUCCESS) { + 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; } @@ -478,27 +488,31 @@ HKEY open_registry(const TCHAR *service_name, const TCHAR *sub, REGSAM sam) { return key; } +HKEY open_registry(const TCHAR *service_name, const TCHAR *sub, REGSAM sam) { + return open_registry(service_name, sub, sam, true); +} + HKEY open_registry(const TCHAR *service_name, REGSAM sam) { - return open_registry(service_name, 0, sam); + return open_registry(service_name, 0, sam, true); } int get_io_parameters(nssm_service_t *service, HKEY key) { /* stdin */ - if (get_createfile_parameters(key, NSSM_REG_STDIN, service->stdin_path, &service->stdin_sharing, NSSM_STDIN_SHARING, &service->stdin_disposition, NSSM_STDIN_DISPOSITION, &service->stdin_flags, NSSM_STDIN_FLAGS)) { + if (get_createfile_parameters(key, NSSM_REG_STDIN, service->stdin_path, &service->stdin_sharing, NSSM_STDIN_SHARING, &service->stdin_disposition, NSSM_STDIN_DISPOSITION, &service->stdin_flags, NSSM_STDIN_FLAGS, 0)) { service->stdin_sharing = service->stdin_disposition = service->stdin_flags = 0; ZeroMemory(service->stdin_path, _countof(service->stdin_path) * sizeof(TCHAR)); return 1; } /* stdout */ - if (get_createfile_parameters(key, NSSM_REG_STDOUT, service->stdout_path, &service->stdout_sharing, NSSM_STDOUT_SHARING, &service->stdout_disposition, NSSM_STDOUT_DISPOSITION, &service->stdout_flags, NSSM_STDOUT_FLAGS)) { + if (get_createfile_parameters(key, NSSM_REG_STDOUT, service->stdout_path, &service->stdout_sharing, NSSM_STDOUT_SHARING, &service->stdout_disposition, NSSM_STDOUT_DISPOSITION, &service->stdout_flags, NSSM_STDOUT_FLAGS, &service->stdout_copy_and_truncate)) { service->stdout_sharing = service->stdout_disposition = service->stdout_flags = 0; ZeroMemory(service->stdout_path, _countof(service->stdout_path) * sizeof(TCHAR)); return 2; } /* stderr */ - if (get_createfile_parameters(key, NSSM_REG_STDERR, service->stderr_path, &service->stderr_sharing, NSSM_STDERR_SHARING, &service->stderr_disposition, NSSM_STDERR_DISPOSITION, &service->stderr_flags, NSSM_STDERR_FLAGS)) { + if (get_createfile_parameters(key, NSSM_REG_STDERR, service->stderr_path, &service->stderr_sharing, NSSM_STDERR_SHARING, &service->stderr_disposition, NSSM_STDERR_DISPOSITION, &service->stderr_flags, NSSM_STDERR_FLAGS, &service->stderr_copy_and_truncate)) { service->stderr_sharing = service->stderr_disposition = service->stderr_flags = 0; ZeroMemory(service->stderr_path, _countof(service->stderr_path) * sizeof(TCHAR)); return 3; @@ -517,6 +531,14 @@ int get_parameters(nssm_service_t *service, STARTUPINFO *si) { /* Don't expand parameters when retrieving for the GUI. */ bool expand = si ? true : false; + /* Try to get environment variables - may fail */ + get_environment(service->name, key, NSSM_REG_ENV, &service->env, &service->envlen); + /* Environment variables to add to existing rather than replace - may fail. */ + get_environment(service->name, key, NSSM_REG_ENV_EXTRA, &service->env_extra, &service->env_extralen); + + /* Set environment if we are starting the service. */ + if (si) set_service_environment(service); + /* Try to get executable file - MUST succeed */ if (get_string(key, NSSM_REG_EXE, service->exe, sizeof(service->exe), expand, false, true)) { RegCloseKey(key); @@ -571,11 +593,6 @@ int get_parameters(nssm_service_t *service, STARTUPINFO *si) { } } - /* Try to get environment variables - may fail */ - get_environment(service->name, key, NSSM_REG_ENV, &service->env, &service->envlen); - /* Environment variables to add to existing rather than replace - may fail. */ - get_environment(service->name, key, NSSM_REG_ENV_EXTRA, &service->env_extra, &service->env_extralen); - /* Try to get priority - may fail. */ unsigned long priority; if (get_number(key, NSSM_REG_PRIORITY, &priority, false) == 1) { @@ -598,6 +615,7 @@ int get_parameters(nssm_service_t *service, STARTUPINFO *si) { if (get_number(key, NSSM_REG_ROTATE_SECONDS, &service->rotate_seconds, false) != 1) service->rotate_seconds = 0; if (get_number(key, NSSM_REG_ROTATE_BYTES_LOW, &service->rotate_bytes_low, false) != 1) service->rotate_bytes_low = 0; if (get_number(key, NSSM_REG_ROTATE_BYTES_HIGH, &service->rotate_bytes_high, false) != 1) service->rotate_bytes_high = 0; + override_milliseconds(service->name, key, NSSM_REG_ROTATE_DELAY, &service->rotate_delay, NSSM_ROTATE_DELAY, NSSM_EVENT_BOGUS_THROTTLE); /* Try to get force new console setting - may fail. */ if (get_number(key, NSSM_REG_NO_CONSOLE, &service->no_console, false) != 1) service->no_console = 0; @@ -649,6 +667,14 @@ int get_parameters(nssm_service_t *service, STARTUPINFO *si) { override_milliseconds(service->name, key, NSSM_REG_KILL_WINDOW_GRACE_PERIOD, &service->kill_window_delay, NSSM_KILL_WINDOW_GRACE_PERIOD, NSSM_EVENT_BOGUS_KILL_WINDOW_GRACE_PERIOD); override_milliseconds(service->name, key, NSSM_REG_KILL_THREADS_GRACE_PERIOD, &service->kill_threads_delay, NSSM_KILL_THREADS_GRACE_PERIOD, NSSM_EVENT_BOGUS_KILL_THREADS_GRACE_PERIOD); + /* Try to get process tree settings - may fail. */ + unsigned long kill_process_tree; + if (get_number(key, NSSM_REG_KILL_PROCESS_TREE, &kill_process_tree, false) == 1) { + if (kill_process_tree) service->kill_process_tree = true; + else service->kill_process_tree = false; + } + else service->kill_process_tree = true; + /* Try to get default exit action. */ bool default_action; service->default_exit_action = NSSM_EXIT_RESTART; @@ -712,3 +738,57 @@ int get_exit_action(const TCHAR *service_name, unsigned long *ret, TCHAR *action return 0; } + +int set_hook(const TCHAR *service_name, const TCHAR *hook_event, const TCHAR *hook_action, TCHAR *cmd) { + /* Try to open the registry */ + TCHAR registry[KEY_LENGTH]; + if (_sntprintf_s(registry, _countof(registry), _TRUNCATE, _T("%s\\%s"), NSSM_REG_HOOK, hook_event) < 0) { + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("hook registry"), _T("set_hook()"), 0); + return 1; + } + + HKEY key; + long error; + + /* Don't create keys needlessly. */ + if (! _tcslen(cmd)) { + key = open_registry(service_name, registry, KEY_READ, false); + if (! key) return 0; + error = RegQueryValueEx(key, hook_action, 0, 0, 0, 0); + RegCloseKey(key); + if (error == ERROR_FILE_NOT_FOUND) return 0; + } + + key = open_registry(service_name, registry, KEY_WRITE); + if (! key) return 1; + + int ret = 1; + if (_tcslen(cmd)) ret = set_string(key, (TCHAR *) hook_action, cmd, true); + else { + error = RegDeleteValue(key, hook_action); + if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) ret = 0; + } + + /* Close registry */ + RegCloseKey(key); + + return ret; +} + +int get_hook(const TCHAR *service_name, const TCHAR *hook_event, const TCHAR *hook_action, TCHAR *buffer, unsigned long buflen) { + /* Try to open the registry */ + TCHAR registry[KEY_LENGTH]; + if (_sntprintf_s(registry, _countof(registry), _TRUNCATE, _T("%s\\%s"), NSSM_REG_HOOK, hook_event) < 0) { + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("hook registry"), _T("get_hook()"), 0); + return 1; + } + HKEY key = open_registry(service_name, registry, KEY_READ, false); + if (! key) return 1; + + int ret = expand_parameter(key, (TCHAR *) hook_action, buffer, buflen, true, false); + + /* Close registry */ + RegCloseKey(key); + + return ret; +}