X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=registry.cpp;h=725844b615fc0e7456e8423c7b75e5f6bf2c9fd7;hb=a90eade7d022451e92bedcebe816651df7bf0d17;hp=1e801ee06dfd7253e7a518a80f97016b34c1208d;hpb=5380a4a53d9b34d6c56ef3e8a022fcdc184593a3;p=nssm.git diff --git a/registry.cpp b/registry.cpp index 1e801ee..725844b 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); @@ -214,10 +222,10 @@ int get_environment(TCHAR *service_name, HKEY key, TCHAR *value, TCHAR **env, un /* Actually get the strings */ ret = RegQueryValueEx(key, value, 0, &type, (unsigned char *) *env, envlen); if (ret != ERROR_SUCCESS) { + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, value, error_string(GetLastError()), 0); HeapFree(GetProcessHeap(), 0, *env); *env = 0; *envlen = 0; - log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, value, error_string(GetLastError()), 0); return 4; } @@ -392,6 +400,27 @@ int unformat_double_null(TCHAR *dn, unsigned long dnlen, TCHAR **unformatted, un } for (i = 0; i < dnlen; i++) if (dn[i] != _T('\r')) ++*newlen; + + /* Skip blank lines. */ + for (i = 0; i < dnlen; i++) { + if (dn[i] == _T('\r') && dn[i + 1] == _T('\n')) { + /* This is the last CRLF. */ + if (i >= dnlen - 2) break; + + /* + Strip at the start of the block or if the next characters are + CRLF too. + */ + if (! i || (dn[i + 2] == _T('\r') && dn[i + 3] == _T('\n'))) { + for (j = i + 2; j < dnlen; j++) dn[j - 2] = dn[j]; + dn[dnlen--] = _T('\0'); + dn[dnlen--] = _T('\0'); + i--; + --*newlen; + } + } + } + /* Must end with two NULLs. */ *newlen += 2; @@ -428,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; @@ -441,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; } @@ -457,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; @@ -577,6 +612,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; @@ -628,6 +664,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;