From: Iain Patterson Date: Sun, 26 Jan 2014 14:11:57 +0000 (+0000) Subject: Split get_output_handles() into get_io_parameters(). X-Git-Tag: v2.22~34 X-Git-Url: http://git.iain.cx/?a=commitdiff_plain;h=2f68d60ad478ec2bf14086b7ef3726824af462b8;p=nssm.git Split get_output_handles() into get_io_parameters(). Separate retrieving parameters from the registry and opening output handles. Restart throttling happens after calling get_output_handles(), so if the service was throttled we'd end up with a useless console window appearing during the pause. --- diff --git a/io.cpp b/io.cpp index 3ba919a..cf30cd4 100644 --- a/io.cpp +++ b/io.cpp @@ -230,17 +230,14 @@ void rotate_file(TCHAR *service_name, TCHAR *path, unsigned long seconds, unsign return; } -int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) { +int get_output_handles(nssm_service_t *service, STARTUPINFO *si) { + if (! si) return 1; + /* Allocate a new console so we get a fresh stdin, stdout and stderr. */ - if (si) alloc_console(service); + alloc_console(service); /* 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)) { - service->stdin_sharing = service->stdin_disposition = service->stdin_flags = 0; - ZeroMemory(service->stdin_path, _countof(service->stdin_path) * sizeof(TCHAR)); - return 1; - } - if (si && service->stdin_path[0]) { + if (service->stdin_path[0]) { si->hStdInput = CreateFile(service->stdin_path, FILE_READ_DATA, service->stdin_sharing, 0, service->stdin_disposition, service->stdin_flags, 0); if (! si->hStdInput) { log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, service->stdin_path, error_string(GetLastError()), 0); @@ -249,12 +246,7 @@ int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) { } /* 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)) { - service->stdout_sharing = service->stdout_disposition = service->stdout_flags = 0; - ZeroMemory(service->stdout_path, _countof(service->stdout_path) * sizeof(TCHAR)); - return 3; - } - if (si && service->stdout_path[0]) { + if (service->stdout_path[0]) { if (service->rotate_files) rotate_file(service->name, service->stdout_path, service->rotate_seconds, service->rotate_bytes_low, service->rotate_bytes_high); HANDLE stdout_handle = write_to_file(service->stdout_path, service->stdout_sharing, 0, service->stdout_disposition, service->stdout_flags); if (! stdout_handle) return 4; @@ -279,11 +271,6 @@ int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) { } /* 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)) { - service->stderr_sharing = service->stderr_disposition = service->stderr_flags = 0; - ZeroMemory(service->stderr_path, _countof(service->stderr_path) * sizeof(TCHAR)); - return 5; - } if (service->stderr_path[0]) { /* Same as stdout? */ if (str_equiv(service->stderr_path, service->stdout_path)) { @@ -292,15 +279,13 @@ int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) { service->stderr_flags = service->stdout_flags; service->rotate_stderr_online = NSSM_ROTATE_OFFLINE; - if (si) { - /* Two handles to the same file will create a race. */ - if (! DuplicateHandle(GetCurrentProcess(), si->hStdOutput, GetCurrentProcess(), &si->hStdError, 0, true, DUPLICATE_SAME_ACCESS)) { - log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DUPLICATEHANDLE_FAILED, NSSM_REG_STDOUT, _T("stderr"), error_string(GetLastError()), 0); - return 6; - } + /* Two handles to the same file will create a race. */ + if (! DuplicateHandle(GetCurrentProcess(), si->hStdOutput, GetCurrentProcess(), &si->hStdError, 0, true, DUPLICATE_SAME_ACCESS)) { + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DUPLICATEHANDLE_FAILED, NSSM_REG_STDOUT, _T("stderr"), error_string(GetLastError()), 0); + return 6; } } - else if (si) { + else { if (service->rotate_files) rotate_file(service->name, service->stderr_path, service->rotate_seconds, service->rotate_bytes_low, service->rotate_bytes_high); HANDLE stderr_handle = write_to_file(service->stderr_path, service->stderr_sharing, 0, service->stderr_disposition, service->stderr_flags); if (! stderr_handle) return 7; @@ -325,8 +310,6 @@ int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) { } } - if (! si) return 0; - /* We need to set the startup_info flags to make the new handles inheritable by the new process. diff --git a/io.h b/io.h index cb067eb..b262db1 100644 --- a/io.h +++ b/io.h @@ -29,7 +29,7 @@ int set_createfile_parameter(HKEY, TCHAR *, TCHAR *, unsigned long); int delete_createfile_parameter(HKEY, TCHAR *, TCHAR *); HANDLE write_to_file(TCHAR *, unsigned long, SECURITY_ATTRIBUTES *, unsigned long, unsigned long); void rotate_file(TCHAR *, TCHAR *, unsigned long, unsigned long, unsigned long); -int get_output_handles(nssm_service_t *, HKEY, STARTUPINFO *); +int get_output_handles(nssm_service_t *, STARTUPINFO *); void close_output_handles(STARTUPINFO *); unsigned long WINAPI log_and_rotate(void *); diff --git a/registry.cpp b/registry.cpp index 3410e4a..98b354b 100644 --- a/registry.cpp +++ b/registry.cpp @@ -402,6 +402,31 @@ HKEY open_registry(const TCHAR *service_name, REGSAM sam) { return open_registry(service_name, 0, sam); } +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)) { + 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)) { + 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)) { + service->stderr_sharing = service->stderr_disposition = service->stderr_flags = 0; + ZeroMemory(service->stderr_path, _countof(service->stderr_path) * sizeof(TCHAR)); + return 3; + } + + return 0; +} + int get_parameters(nssm_service_t *service, STARTUPINFO *si) { unsigned long ret; @@ -500,7 +525,7 @@ int get_parameters(nssm_service_t *service, STARTUPINFO *si) { SetCurrentDirectory(service->dir); /* Try to get stdout and stderr */ - if (get_output_handles(service, key, si)) { + if (get_io_parameters(service, key)) { log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_GET_OUTPUT_HANDLES_FAILED, service->name, 0); RegCloseKey(key); SetCurrentDirectory(cwd); diff --git a/registry.h b/registry.h index 1734d4e..34d75f3 100644 --- a/registry.h +++ b/registry.h @@ -47,6 +47,7 @@ int set_number(HKEY, TCHAR *, unsigned long); int get_number(HKEY, TCHAR *, unsigned long *, bool); int get_number(HKEY, TCHAR *, unsigned long *); void override_milliseconds(TCHAR *, HKEY, TCHAR *, unsigned long *, unsigned long, unsigned long); +int get_io_parameters(nssm_service_t *, HKEY); int get_parameters(nssm_service_t *, STARTUPINFO *); int get_exit_action(const TCHAR *, unsigned long *, TCHAR *, bool *); diff --git a/service.cpp b/service.cpp index e9fd109..1477ba7 100644 --- a/service.cpp +++ b/service.cpp @@ -1517,7 +1517,6 @@ int start_service(nssm_service_t *service) { TCHAR cmd[CMD_LENGTH]; if (_sntprintf_s(cmd, _countof(cmd), _TRUNCATE, _T("\"%s\" %s"), service->exe, service->flags) < 0) { log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("command line"), _T("start_service"), 0); - close_output_handles(&si); return stop_service(service, 2, true, true); } @@ -1527,6 +1526,14 @@ int start_service(nssm_service_t *service) { if (service->env) duplicate_environment(service->env); if (service->env_extra) set_environment_block(service->env_extra); + /* Set up I/O redirection. */ + if (get_output_handles(service, &si)) { + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_GET_OUTPUT_HANDLES_FAILED, service->name, 0); + if (! service->no_console) FreeConsole(); + close_output_handles(&si); + return stop_service(service, 4, true, true); + } + bool inherit_handles = false; if (si.dwFlags & STARTF_USESTDHANDLES) inherit_handles = true; unsigned long flags = service->priority & priority_mask();