X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;ds=sidebyside;f=io.cpp;h=cf30cd414652c15b56367232737625468b97877c;hb=0dfa0fdb3a851b1ff69a0f0b308a3e1371286a57;hp=464e290fe59795b8381e65702cb1c6507f37e9d7;hpb=ba4831e5eb179e415282a957604f039bceb4804f;p=nssm.git diff --git a/io.cpp b/io.cpp index 464e290..cf30cd4 100644 --- a/io.cpp +++ b/io.cpp @@ -230,21 +230,23 @@ 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) { - bool set_flags = false; +int get_output_handles(nssm_service_t *service, STARTUPINFO *si) { + if (! si) return 1; - /* Standard security attributes allowing inheritance. */ - SECURITY_ATTRIBUTES attributes; - ZeroMemory(&attributes, sizeof(attributes)); - attributes.bInheritHandle = true; + /* Allocate a new console so we get a fresh stdin, stdout and stderr. */ + alloc_console(service); - /* 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; + /* stdin */ + 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); + return 2; + } } - if (si && service->stdout_path[0]) { + + /* stdout */ + 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; @@ -266,16 +268,9 @@ int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) { } service->rotate_stdout_online = NSSM_ROTATE_OFFLINE; } - - set_flags = true; } /* 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)) { @@ -284,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; @@ -314,62 +307,36 @@ int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) { } service->rotate_stderr_online = NSSM_ROTATE_OFFLINE; } - - set_flags = true; - } - } - - /* 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 (str_equiv(service->stdin_path, _T("|"))) { - /* Fake stdin with a pipe. */ - if (set_flags) { - /* - None of this is necessary if we aren't redirecting stdout and/or - stderr as well. - - If we don't redirect any handles the application will start and be - quite happy with its console. If we start it with - STARTF_USESTDHANDLES set it will interpret a NULL value for - hStdInput as meaning no input. Because the service starts with - no stdin we can't just pass GetStdHandle(STD_INPUT_HANDLE) to - the application. - - The only way we can successfully redirect the application's output - while preventing programs which exit after reading all input from - exiting prematurely is to create a pipe between ourselves and the - application but write nothing to it. - */ - if (! CreatePipe(&si->hStdInput, &service->stdin_pipe, 0, 0)) { - log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_STDIN_CREATEPIPE_FAILED, service->name, error_string(GetLastError()), 0); - return 2; - } - SetHandleInformation(si->hStdInput, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT); - } - } - else { - si->hStdInput = CreateFile(service->stdin_path, FILE_READ_DATA, service->stdin_sharing, &attributes, 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); - return 2; - } - - set_flags = true; } } - if (! set_flags) return 0; - /* We need to set the startup_info flags to make the new handles inheritable by the new process. */ - if (si) si->dwFlags |= STARTF_USESTDHANDLES; + si->dwFlags |= STARTF_USESTDHANDLES; + + if (service->no_console) return 0; + + /* Redirect other handles. */ + if (! si->hStdInput) { + if (! DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_INPUT_HANDLE), GetCurrentProcess(), &si->hStdInput, 0, true, DUPLICATE_SAME_ACCESS)) { + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DUPLICATEHANDLE_FAILED, _T("STD_INPUT_HANDLE"), _T("stdin"), error_string(GetLastError()), 0); + return 8; + } + } + if (! si->hStdOutput) { + if (! DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_OUTPUT_HANDLE), GetCurrentProcess(), &si->hStdOutput, 0, true, DUPLICATE_SAME_ACCESS)) { + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DUPLICATEHANDLE_FAILED, _T("STD_OUTPUT_HANDLE"), _T("stdout"), error_string(GetLastError()), 0); + return 9; + } + } + if (! si->hStdError) { + if (! DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_ERROR_HANDLE), GetCurrentProcess(), &si->hStdError, 0, true, DUPLICATE_SAME_ACCESS)) { + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DUPLICATEHANDLE_FAILED, _T("STD_ERROR_HANDLE"), _T("stderr"), error_string(GetLastError()), 0); + return 10; + } + } return 0; }