X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=io.cpp;h=79848e94fc2a28bc17cdf2cdd2960f3469d42026;hb=2cd1c7c29ef4d2d3df3c5afd3ca6c788aede2bef;hp=9dd21bf631929a285f0309543d61e9ce87f79ff6;hpb=aec569efa48339e6e886b1420af6bb101699d33b;p=nssm.git diff --git a/io.cpp b/io.cpp index 9dd21bf..79848e9 100644 --- a/io.cpp +++ b/io.cpp @@ -1,5 +1,27 @@ #include "nssm.h" +#define COMPLAINED_READ (1 << 0) +#define COMPLAINED_WRITE (1 << 1) +#define COMPLAINED_ROTATE (1 << 2) + +static HANDLE create_logging_thread(logger_t *logger) { + HANDLE thread_handle = CreateThread(NULL, 0, log_and_rotate, (void *) logger, 0, logger->tid_ptr); + if (! thread_handle) log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATETHREAD_FAILED, error_string(GetLastError()), 0); + return thread_handle; +} + +static inline unsigned long guess_charsize(void *address, unsigned long bufsize) { + if (IsTextUnicode(address, bufsize, 0)) return (unsigned long) sizeof(wchar_t); + else return (unsigned long) sizeof(char); +} + +static inline void write_bom(logger_t *logger, unsigned long *out) { + wchar_t bom = L'\ufeff'; + if (! WriteFile(logger->write_handle, (void *) &bom, sizeof(bom), out, 0)) { + log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_SOMEBODY_SET_UP_US_THE_BOM, logger->service_name, logger->path, error_string(GetLastError()), 0); + } +} + /* Get path, share mode, creation disposition and flags for a stream. */ int get_createfile_parameters(HKEY key, TCHAR *prefix, TCHAR *path, unsigned long *sharing, unsigned long default_sharing, unsigned long *disposition, unsigned long default_disposition, unsigned long *flags, unsigned long default_flags) { TCHAR value[NSSM_STDIO_LENGTH]; @@ -93,6 +115,22 @@ HANDLE append_to_file(TCHAR *path, unsigned long sharing, SECURITY_ATTRIBUTES *a return CreateFile(path, FILE_WRITE_DATA, sharing, attributes, disposition, flags, 0); } +static void rotated_filename(TCHAR *path, TCHAR *rotated, unsigned long rotated_len, SYSTEMTIME *st) { + if (! st) { + SYSTEMTIME now; + st = &now; + GetSystemTime(st); + } + + TCHAR buffer[MAX_PATH]; + memmove(buffer, path, sizeof(buffer)); + TCHAR *ext = PathFindExtension(buffer); + TCHAR extension[MAX_PATH]; + _sntprintf_s(extension, _countof(extension), _TRUNCATE, _T("-%04u%02u%02uT%02u%02u%02u.%03u%s"), st->wYear, st->wMonth, st->wDay, st->wHour, st->wMinute, st->wSecond, st->wMilliseconds, ext); + *ext = _T('\0'); + _sntprintf_s(rotated, rotated_len, _TRUNCATE, _T("%s%s"), buffer, extension); +} + void rotate_file(TCHAR *service_name, TCHAR *path, unsigned long seconds, unsigned long low, unsigned long high) { unsigned long error; @@ -146,19 +184,17 @@ void rotate_file(TCHAR *service_name, TCHAR *path, unsigned long seconds, unsign /* Get new filename. */ FileTimeToSystemTime(&info.ftLastWriteTime, &st); - TCHAR buffer[MAX_PATH]; - memmove(buffer, path, sizeof(buffer)); - TCHAR *ext = PathFindExtension(buffer); - TCHAR extension[MAX_PATH]; - _sntprintf_s(extension, _countof(extension), _TRUNCATE, _T("-%04u%02u%02uT%02u%02u%02u.%03u%s"), st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, ext); - *ext = _T('\0'); TCHAR rotated[MAX_PATH]; - _sntprintf_s(rotated, _countof(rotated), _TRUNCATE, _T("%s%s"), buffer, extension); + rotated_filename(path, rotated, _countof(rotated), &st); /* Rotate. */ - if (MoveFile(path, rotated)) return; + if (MoveFile(path, rotated)) { + log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_ROTATED, service_name, path, rotated, 0); + return; + } error = GetLastError(); + if (error == ERROR_FILE_NOT_FOUND) return; log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_ROTATE_FILE_FAILED, service_name, path, _T("MoveFile()"), rotated, error_string(error), 0); return; } @@ -186,6 +222,10 @@ int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) { set_flags = true; } + ULARGE_INTEGER size; + size.LowPart = service->rotate_bytes_low; + size.HighPart = service->rotate_bytes_high; + /* 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; @@ -194,13 +234,64 @@ int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) { } if (si && 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); - si->hStdOutput = append_to_file(service->stdout_path, service->stdout_sharing, &attributes, service->stdout_disposition, service->stdout_flags); - if (! si->hStdOutput) return 4; + HANDLE stdout_handle = append_to_file(service->stdout_path, service->stdout_sharing, 0, service->stdout_disposition, service->stdout_flags); + if (! stdout_handle) { + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, service->stdout_path, error_string(GetLastError()), 0); + return 4; + } + + /* Try online rotation only if a size threshold is set. */ + logger_t *stdout_logger = 0; + if (service->rotate_files && service->rotate_stdout_online) { + stdout_logger = (logger_t *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(logger_t)); + if (stdout_logger) { + /* Pipe between application's stdout and our logging handle. */ + if (CreatePipe(&service->stdout_pipe, &si->hStdOutput, &attributes, 0)) { + stdout_logger->service_name = service->name; + stdout_logger->path = service->stdout_path; + stdout_logger->sharing = service->stdout_sharing; + stdout_logger->disposition = service->stdout_disposition; + stdout_logger->flags = service->stdout_flags; + stdout_logger->read_handle = service->stdout_pipe; + stdout_logger->write_handle = stdout_handle; + stdout_logger->size = (__int64) size.QuadPart; + stdout_logger->tid_ptr = &service->stdout_tid; + stdout_logger->rotate_online = &service->rotate_stdout_online; + + /* Logging thread. */ + service->stdout_thread = create_logging_thread(stdout_logger); + if (! service->stdout_thread) { + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPIPE_FAILED, service->name, service->stdout_path, error_string(GetLastError())); + CloseHandle(service->stdout_pipe); + CloseHandle(si->hStdOutput); + service->stdout_tid = 0; + } + } + else service->stdout_tid = 0; + } + else { + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("stdout_logger"), _T("get_output_handles()"), 0); + service->stdout_tid = 0; + } + + /* Fall through to try direct I/O. */ + if (! service->stdout_tid) log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPIPE_FAILED, service->name, service->stdout_path, error_string(GetLastError())); + } + + if (! service->stdout_tid) { + if (stdout_logger) HeapFree(GetProcessHeap(), 0, stdout_logger); + 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, error_string(GetLastError()), 0); + return 4; + } + service->rotate_stdout_online = NSSM_ROTATE_OFFLINE; + } + set_flags = true; } /* stderr */ - if (get_createfile_parameters(key, NSSM_REG_STDERR, service->stderr_path, &service->stdout_sharing, NSSM_STDERR_SHARING, &service->stdout_disposition, NSSM_STDERR_DISPOSITION, &service->stdout_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_sharing = service->stderr_disposition = service->stderr_flags = 0; ZeroMemory(service->stderr_path, _countof(service->stderr_path) * sizeof(TCHAR)); return 5; @@ -211,6 +302,7 @@ int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) { service->stderr_sharing = service->stdout_sharing; service->stderr_disposition = service->stdout_disposition; service->stderr_flags = service->stdout_flags; + service->rotate_stderr_online = false; if (si) { /* Two handles to the same file will create a race. */ @@ -222,13 +314,60 @@ int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) { } else if (si) { if (service->rotate_files) rotate_file(service->name, service->stderr_path, service->rotate_seconds, service->rotate_bytes_low, service->rotate_bytes_high); - si->hStdError = append_to_file(service->stderr_path, service->stdout_sharing, &attributes, service->stdout_disposition, service->stdout_flags); - if (! si->hStdError) { + HANDLE stderr_handle = append_to_file(service->stderr_path, service->stderr_sharing, 0, service->stderr_disposition, service->stderr_flags); + if (! stderr_handle) { log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, service->stderr_path, error_string(GetLastError()), 0); return 7; } - SetEndOfFile(si->hStdError); + + /* Try online rotation only if a size threshold is set. */ + logger_t *stderr_logger = 0; + if (service->rotate_files && service->rotate_stderr_online) { + stderr_logger = (logger_t *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(logger_t)); + if (stderr_logger) { + /* Pipe between application's stderr and our logging handle. */ + if (CreatePipe(&service->stderr_pipe, &si->hStdError, &attributes, 0)) { + stderr_logger->service_name = service->name; + stderr_logger->path = service->stderr_path; + stderr_logger->sharing = service->stderr_sharing; + stderr_logger->disposition = service->stderr_disposition; + stderr_logger->flags = service->stderr_flags; + stderr_logger->read_handle = service->stderr_pipe; + stderr_logger->write_handle = stderr_handle; + stderr_logger->size = (__int64) size.QuadPart; + stderr_logger->tid_ptr = &service->stderr_tid; + stderr_logger->rotate_online = &service->rotate_stderr_online; + + /* Logging thread. */ + service->stderr_thread = create_logging_thread(stderr_logger); + if (! service->stderr_thread) { + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPIPE_FAILED, service->name, service->stderr_path, error_string(GetLastError())); + CloseHandle(service->stderr_pipe); + CloseHandle(si->hStdError); + service->stderr_tid = 0; + } + } + else service->stderr_tid = 0; + } + else { + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("stderr_logger"), _T("get_output_handles()"), 0); + service->stderr_tid = 0; + } + + /* Fall through to try direct I/O. */ + if (! service->stderr_tid) log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPIPE_FAILED, service->name, service->stderr_path, error_string(GetLastError())); + } + + if (! service->stderr_tid) { + if (stderr_logger) HeapFree(GetProcessHeap(), 0, stderr_logger); + 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, error_string(GetLastError()), 0); + return 7; + } + service->rotate_stderr_online = NSSM_ROTATE_OFFLINE; + } } + set_flags = true; } @@ -243,8 +382,212 @@ int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) { return 0; } -void close_output_handles(STARTUPINFO *si) { +void close_output_handles(STARTUPINFO *si, bool close_stdout, bool close_stderr) { if (si->hStdInput) CloseHandle(si->hStdInput); - if (si->hStdOutput) CloseHandle(si->hStdOutput); - if (si->hStdError) CloseHandle(si->hStdError); + if (si->hStdOutput && close_stdout) CloseHandle(si->hStdOutput); + if (si->hStdError && close_stderr) CloseHandle(si->hStdError); +} + +void close_output_handles(STARTUPINFO *si) { + return close_output_handles(si, true, true); +} + +/* + Try multiple times to read from a file. + Returns: 0 on success. + 1 on non-fatal error. + -1 on fatal error. +*/ +static int try_read(logger_t *logger, void *address, unsigned long bufsize, unsigned long *in, int *complained) { + int ret = 1; + unsigned long error; + for (int tries = 0; tries < 5; tries++) { + if (ReadFile(logger->read_handle, address, bufsize, in, 0)) return 0; + + error = GetLastError(); + switch (error) { + /* Other end closed the pipe. */ + case ERROR_BROKEN_PIPE: + ret = -1; + goto complain_read; + + /* Couldn't lock the buffer. */ + case ERROR_NOT_ENOUGH_QUOTA: + Sleep(2000 + tries * 3000); + ret = 1; + continue; + + /* Write was cancelled by the other end. */ + case ERROR_OPERATION_ABORTED: + ret = 1; + goto complain_read; + + default: + ret = -1; + } + } + +complain_read: + if (! (*complained & COMPLAINED_READ)) log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_READFILE_FAILED, logger->service_name, logger->path, error_string(error), 0); + *complained |= COMPLAINED_READ; + return ret; +} + +/* + Try multiple times to write to a file. + Returns: 0 on success. + 1 on non-fatal error. + -1 on fatal error. +*/ +static int try_write(logger_t *logger, void *address, unsigned long bufsize, unsigned long *out, int *complained) { + int ret = 1; + unsigned long error; + for (int tries = 0; tries < 5; tries++) { + if (WriteFile(logger->write_handle, address, bufsize, out, 0)) return 0; + + error = GetLastError(); + if (error == ERROR_IO_PENDING) { + /* Operation was successful pending flush to disk. */ + return 0; + } + + switch (error) { + /* Other end closed the pipe. */ + case ERROR_BROKEN_PIPE: + ret = -1; + goto complain_write; + + /* Couldn't lock the buffer. */ + case ERROR_NOT_ENOUGH_QUOTA: + /* Out of disk space. */ + case ERROR_DISK_FULL: + Sleep(2000 + tries * 3000); + ret = 1; + continue; + + default: + /* We'll lose this line but try to read and write subsequent ones. */ + ret = 1; + } + } + +complain_write: + if (! (*complained & COMPLAINED_WRITE)) log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_WRITEFILE_FAILED, logger->service_name, logger->path, error_string(error), 0); + *complained |= COMPLAINED_WRITE; + return ret; +} + +/* Wrapper to be called in a new thread for logging. */ +unsigned long WINAPI log_and_rotate(void *arg) { + logger_t *logger = (logger_t *) arg; + if (! logger) return 1; + + __int64 size; + BY_HANDLE_FILE_INFORMATION info; + + /* Find initial file size. */ + if (! GetFileInformationByHandle(logger->write_handle, &info)) logger->size = 0LL; + else { + ULARGE_INTEGER l; + l.HighPart = info.nFileSizeHigh; + l.LowPart = info.nFileSizeLow; + size = l.QuadPart; + } + + char buffer[1024]; + void *address; + unsigned long in, out; + unsigned long charsize = 0; + unsigned long error; + int ret; + int complained = 0; + + while (true) { + /* Read data from the pipe. */ + address = &buffer; + ret = try_read(logger, address, sizeof(buffer), &in, &complained); + if (ret < 0) { + HeapFree(GetProcessHeap(), 0, logger); + return 2; + } + else if (ret) continue; + + if (*logger->rotate_online == NSSM_ROTATE_ONLINE_ASAP || (logger->size && size + (__int64) in >= logger->size)) { + /* Look for newline. */ + unsigned long i; + for (i = 0; i < in; i++) { + if (buffer[i] == '\n') { + if (! charsize) charsize = guess_charsize(address, in); + i += charsize; + + /* Write up to the newline. */ + ret = try_write(logger, address, i, &out, &complained); + if (ret < 0) { + HeapFree(GetProcessHeap(), 0, logger); + return 3; + } + size += (__int64) out; + + /* Rotate. */ + *logger->rotate_online = NSSM_ROTATE_ONLINE; + TCHAR rotated[MAX_PATH]; + rotated_filename(logger->path, rotated, _countof(rotated), 0); + + /* + Ideally we'd try the rename first then close the handle but + MoveFile() will fail if the handle is still open so we must + risk losing everything. + */ + CloseHandle(logger->write_handle); + if (MoveFile(logger->path, rotated)) { + log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_ROTATED, logger->service_name, logger->path, rotated, 0); + size = 0LL; + } + else { + error = GetLastError(); + if (error != ERROR_FILE_NOT_FOUND) { + if (! (complained & COMPLAINED_ROTATE)) log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_ROTATE_FILE_FAILED, logger->service_name, logger->path, _T("MoveFile()"), rotated, error_string(error), 0); + complained |= COMPLAINED_ROTATE; + /* We can at least try to re-open the existing file. */ + logger->disposition = OPEN_ALWAYS; + } + } + + /* Reopen. */ + logger->write_handle = append_to_file(logger->path, logger->sharing, 0, logger->disposition, logger->flags); + if (! logger->write_handle) { + 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. */ + HeapFree(GetProcessHeap(), 0, logger); + return 4; + } + + /* Resume writing after the newline. */ + address = (void *) ((char *) address + i); + in -= i; + } + } + } + + if (! size) { + /* Write a BOM to the new file. */ + if (! charsize) charsize = guess_charsize(address, in); + if (charsize == sizeof(wchar_t)) write_bom(logger, &out); + size += (__int64) out; + } + + /* Write the data, if any. */ + if (! in) continue; + + ret = try_write(logger, address, in, &out, &complained); + size += (__int64) out; + if (ret < 0) { + HeapFree(GetProcessHeap(), 0, logger); + return 3; + } + } + + HeapFree(GetProcessHeap(), 0, logger); + return 0; }