X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=io.cpp;h=b6f9d37c3e55b4127a423a9261d3e57cc0da8edb;hb=2a259b54caa08ef6d4dbc99f6fe7ec97837c25e8;hp=508d7db1ccb0c3d8fc440061e451b66f9487201e;hpb=2bc7df40fe52fe9e86e382926f7653b55ee02038;p=nssm.git diff --git a/io.cpp b/io.cpp index 508d7db..b6f9d37 100644 --- a/io.cpp +++ b/io.cpp @@ -4,6 +4,25 @@ #define COMPLAINED_WRITE (1 << 1) #define COMPLAINED_ROTATE (1 << 2) +static int dup_handle(HANDLE source_handle, HANDLE *dest_handle_ptr, TCHAR *source_description, TCHAR *dest_description, unsigned long flags) { + if (! dest_handle_ptr) return 1; + + if (! DuplicateHandle(GetCurrentProcess(), source_handle, GetCurrentProcess(), dest_handle_ptr, 0, true, flags)) { + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DUPLICATEHANDLE_FAILED, source_description, dest_description, error_string(GetLastError()), 0); + return 2; + } + return 0; +} + +static int dup_handle(HANDLE source_handle, HANDLE *dest_handle_ptr, TCHAR *source_description, TCHAR *dest_description) { + return dup_handle(source_handle, dest_handle_ptr, source_description, dest_description, DUPLICATE_SAME_ACCESS); +} + +/* + read_handle: read from application + pipe_handle: stdout of application + write_handle: to file +*/ static HANDLE create_logging_thread(TCHAR *service_name, TCHAR *path, unsigned long sharing, unsigned long disposition, unsigned long flags, HANDLE *read_handle_ptr, HANDLE *pipe_handle_ptr, HANDLE *write_handle_ptr, unsigned long rotate_bytes_low, unsigned long rotate_bytes_high, unsigned long rotate_delay, unsigned long *tid_ptr, unsigned long *rotate_online, bool copy_and_truncate) { *tid_ptr = 0; @@ -86,7 +105,7 @@ int get_createfile_parameters(HKEY key, TCHAR *prefix, TCHAR *path, unsigned lon switch (get_number(key, value, sharing, false)) { case 0: *sharing = default_sharing; break; /* Missing. */ case 1: break; /* Found. */ - case -2: return 4; break; /* Error. */ + case -2: return 4; /* Error. */ } /* CreationDisposition. */ @@ -97,7 +116,7 @@ int get_createfile_parameters(HKEY key, TCHAR *prefix, TCHAR *path, unsigned lon switch (get_number(key, value, disposition, false)) { case 0: *disposition = default_disposition; break; /* Missing. */ case 1: break; /* Found. */ - case -2: return 6; break; /* Error. */ + case -2: return 6; /* Error. */ } /* Flags. */ @@ -108,7 +127,7 @@ int get_createfile_parameters(HKEY key, TCHAR *prefix, TCHAR *path, unsigned lon switch (get_number(key, value, flags, false)) { case 0: *flags = default_flags; break; /* Missing. */ case 1: break; /* Found. */ - case -2: return 8; break; /* Error. */ + case -2: return 8; /* Error. */ } /* Rotate with CopyFile() and SetEndOfFile(). */ @@ -124,7 +143,7 @@ int get_createfile_parameters(HKEY key, TCHAR *prefix, TCHAR *path, unsigned lon if (data) *copy_and_truncate = true; else *copy_and_truncate = false; break; - case -2: return 9; break; /* Error. */ + case -2: return 9; /* Error. */ } } @@ -156,7 +175,7 @@ int delete_createfile_parameter(HKEY key, TCHAR *prefix, TCHAR *suffix) { HANDLE write_to_file(TCHAR *path, unsigned long sharing, SECURITY_ATTRIBUTES *attributes, unsigned long disposition, unsigned long flags) { HANDLE ret = CreateFile(path, FILE_WRITE_DATA, sharing, attributes, disposition, flags, 0); - if (ret) { + if (ret!= INVALID_HANDLE_VALUE) { if (SetFilePointer(ret, 0, 0, FILE_END) != INVALID_SET_FILE_POINTER) SetEndOfFile(ret); return ret; } @@ -192,7 +211,7 @@ void rotate_file(TCHAR *service_name, TCHAR *path, unsigned long seconds, unsign /* Try to open the file to check if it exists and to get attributes. */ HANDLE file = CreateFile(path, 0, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); - if (file) { + if (file != INVALID_HANDLE_VALUE) { /* Get file attributes. */ if (! GetFileInformationByHandle(file, &info)) { /* Reuse current time for rotation timestamp. */ @@ -275,7 +294,7 @@ int get_output_handles(nssm_service_t *service, STARTUPINFO *si) { /* 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) { + if (si->hStdInput == INVALID_HANDLE_VALUE) { log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, service->stdin_path, error_string(GetLastError()), 0); return 2; } @@ -285,25 +304,30 @@ int get_output_handles(nssm_service_t *service, STARTUPINFO *si) { 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, service->rotate_delay, service->stdout_copy_and_truncate); HANDLE stdout_handle = write_to_file(service->stdout_path, service->stdout_sharing, 0, service->stdout_disposition, service->stdout_flags); - if (! stdout_handle) return 4; + if (stdout_handle == INVALID_HANDLE_VALUE) return 4; + service->stdout_si = 0; - if (service->rotate_files && service->rotate_stdout_online) { + if (service->use_stdout_pipe) { service->stdout_pipe = si->hStdOutput = 0; - service->stdout_thread = create_logging_thread(service->name, service->stdout_path, service->stdout_sharing, service->stdout_disposition, service->stdout_flags, &service->stdout_pipe, &si->hStdOutput, &stdout_handle, service->rotate_bytes_low, service->rotate_bytes_high, service->rotate_delay, &service->stdout_tid, &service->rotate_stdout_online, service->stdout_copy_and_truncate); + service->stdout_thread = create_logging_thread(service->name, service->stdout_path, service->stdout_sharing, service->stdout_disposition, service->stdout_flags, &service->stdout_pipe, &service->stdout_si, &stdout_handle, service->rotate_bytes_low, service->rotate_bytes_high, service->rotate_delay, &service->stdout_tid, &service->rotate_stdout_online, service->stdout_copy_and_truncate); if (! service->stdout_thread) { CloseHandle(service->stdout_pipe); - CloseHandle(si->hStdOutput); + CloseHandle(service->stdout_si); } } else service->stdout_thread = 0; if (! service->stdout_thread) { - if (! DuplicateHandle(GetCurrentProcess(), stdout_handle, GetCurrentProcess(), &si->hStdOutput, 0, true, DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) { - log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DUPLICATEHANDLE_FAILED, NSSM_REG_STDOUT, _T("stdout"), error_string(GetLastError()), 0); - return 4; - } + if (dup_handle(stdout_handle, &service->stdout_si, NSSM_REG_STDOUT, _T("stdout"), DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) return 4; service->rotate_stdout_online = NSSM_ROTATE_OFFLINE; } + + if (dup_handle(service->stdout_si, &si->hStdOutput, _T("stdout_si"), _T("stdout"))) { + if (service->stdout_thread) { + CloseHandle(service->stdout_thread); + service->stdout_thread = 0; + } + } } /* stderr */ @@ -316,34 +340,37 @@ int get_output_handles(nssm_service_t *service, STARTUPINFO *si) { service->rotate_stderr_online = NSSM_ROTATE_OFFLINE; /* 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; - } + /* XXX: Here we assume that either both or neither handle must be a pipe. */ + if (dup_handle(service->stdout_si, &service->stderr_si, _T("stdout"), _T("stderr"))) return 6; } else { if (service->rotate_files) rotate_file(service->name, service->stderr_path, service->rotate_seconds, service->rotate_bytes_low, service->rotate_bytes_high, service->rotate_delay, service->stderr_copy_and_truncate); HANDLE stderr_handle = write_to_file(service->stderr_path, service->stderr_sharing, 0, service->stderr_disposition, service->stderr_flags); - if (! stderr_handle) return 7; + if (stderr_handle == INVALID_HANDLE_VALUE) return 7; + service->stderr_si = 0; - if (service->rotate_files && service->rotate_stderr_online) { + if (service->use_stderr_pipe) { service->stderr_pipe = si->hStdError = 0; - service->stderr_thread = create_logging_thread(service->name, service->stderr_path, service->stderr_sharing, service->stderr_disposition, service->stderr_flags, &service->stderr_pipe, &si->hStdError, &stderr_handle, service->rotate_bytes_low, service->rotate_bytes_high, service->rotate_delay, &service->stderr_tid, &service->rotate_stderr_online, service->stderr_copy_and_truncate); + service->stderr_thread = create_logging_thread(service->name, service->stderr_path, service->stderr_sharing, service->stderr_disposition, service->stderr_flags, &service->stderr_pipe, &service->stderr_si, &stderr_handle, service->rotate_bytes_low, service->rotate_bytes_high, service->rotate_delay, &service->stderr_tid, &service->rotate_stderr_online, service->stderr_copy_and_truncate); if (! service->stderr_thread) { CloseHandle(service->stderr_pipe); - CloseHandle(si->hStdError); + CloseHandle(service->stderr_si); } } else service->stderr_thread = 0; if (! service->stderr_thread) { - if (! DuplicateHandle(GetCurrentProcess(), stderr_handle, GetCurrentProcess(), &si->hStdError, 0, true, DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) { - log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DUPLICATEHANDLE_FAILED, NSSM_REG_STDERR, _T("stderr"), error_string(GetLastError()), 0); - return 7; - } + if (dup_handle(stderr_handle, &service->stderr_si, NSSM_REG_STDERR, _T("stderr"), DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) return 7; service->rotate_stderr_online = NSSM_ROTATE_OFFLINE; } } + + if (dup_handle(service->stderr_si, &si->hStdError, _T("stderr_si"), _T("stderr"))) { + if (service->stderr_thread) { + CloseHandle(service->stderr_thread); + service->stderr_thread = 0; + } + } } /* @@ -356,22 +383,36 @@ int get_output_handles(nssm_service_t *service, STARTUPINFO *si) { /* 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 (dup_handle(GetStdHandle(STD_INPUT_HANDLE), &si->hStdInput, _T("STD_INPUT_HANDLE"), _T("stdin"))) 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 (dup_handle(GetStdHandle(STD_OUTPUT_HANDLE), &si->hStdOutput, _T("STD_OUTPUT_HANDLE"), _T("stdout"))) 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; + if (dup_handle(GetStdHandle(STD_ERROR_HANDLE), &si->hStdError, _T("STD_ERROR_HANDLE"), _T("stderr"))) return 10; + } + + return 0; +} + +/* Reuse output handles for a hook. */ +int use_output_handles(nssm_service_t *service, STARTUPINFO *si) { + si->dwFlags &= ~STARTF_USESTDHANDLES; + + if (service->stdout_si) { + if (dup_handle(service->stdout_si, &si->hStdOutput, _T("stdout_pipe"), _T("hStdOutput"))) return 1; + si->dwFlags |= STARTF_USESTDHANDLES; + } + + if (service->stderr_si) { + if (dup_handle(service->stderr_si, &si->hStdError, _T("stderr_pipe"), _T("hStdError"))) { + if (si->hStdOutput) { + si->dwFlags &= ~STARTF_USESTDHANDLES; + CloseHandle(si->hStdOutput); + } + return 2; } + si->dwFlags |= STARTF_USESTDHANDLES; } return 0; @@ -570,7 +611,7 @@ unsigned long WINAPI log_and_rotate(void *arg) { /* Reopen. */ logger->write_handle = write_to_file(logger->path, logger->sharing, 0, logger->disposition, logger->flags); - if (! logger->write_handle) { + if (logger->write_handle == INVALID_HANDLE_VALUE) { error = GetLastError(); log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, logger->path, error_string(error), 0); /* Oh dear. Now we can't log anything further. */