#include "nssm.h"\r
\r
+static HANDLE create_logging_thread(logger_t *logger) {\r
+ HANDLE thread_handle = CreateThread(NULL, 0, log_and_rotate, (void *) logger, 0, logger->tid_ptr);\r
+ if (! thread_handle) log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATETHREAD_FAILED, error_string(GetLastError()), 0);\r
+ return thread_handle;\r
+}\r
+\r
+static inline void write_bom(logger_t *logger) {\r
+ wchar_t bom = L'\ufeff';\r
+ unsigned long out;\r
+ if (! WriteFile(logger->write_handle, (void *) &bom, sizeof(bom), &out, 0)) {\r
+ log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_SOMEBODY_SET_UP_US_THE_BOM, logger->service_name, logger->path, error_string(GetLastError()), 0);\r
+ }\r
+}\r
+\r
/* Get path, share mode, creation disposition and flags for a stream. */\r
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) {\r
TCHAR value[NSSM_STDIO_LENGTH];\r
return CreateFile(path, FILE_WRITE_DATA, sharing, attributes, disposition, flags, 0);\r
}\r
\r
+static void rotated_filename(TCHAR *path, TCHAR *rotated, unsigned long rotated_len, SYSTEMTIME *st) {\r
+ if (! st) {\r
+ SYSTEMTIME now;\r
+ st = &now;\r
+ GetSystemTime(st);\r
+ }\r
+\r
+ TCHAR buffer[MAX_PATH];\r
+ memmove(buffer, path, sizeof(buffer));\r
+ TCHAR *ext = PathFindExtension(buffer);\r
+ TCHAR extension[MAX_PATH];\r
+ _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);\r
+ *ext = _T('\0');\r
+ _sntprintf_s(rotated, rotated_len, _TRUNCATE, _T("%s%s"), buffer, extension);\r
+}\r
+\r
void rotate_file(TCHAR *service_name, TCHAR *path, unsigned long seconds, unsigned long low, unsigned long high) {\r
unsigned long error;\r
\r
/* Get new filename. */\r
FileTimeToSystemTime(&info.ftLastWriteTime, &st);\r
\r
- TCHAR buffer[MAX_PATH];\r
- memmove(buffer, path, sizeof(buffer));\r
- TCHAR *ext = PathFindExtension(buffer);\r
- TCHAR extension[MAX_PATH];\r
- _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);\r
- *ext = _T('\0');\r
TCHAR rotated[MAX_PATH];\r
- _sntprintf_s(rotated, _countof(rotated), _TRUNCATE, _T("%s%s"), buffer, extension);\r
+ rotated_filename(path, rotated, _countof(rotated), &st);\r
\r
/* Rotate. */\r
if (MoveFile(path, rotated)) return;\r
set_flags = true;\r
}\r
\r
+ ULARGE_INTEGER size;\r
+ size.LowPart = service->rotate_bytes_low;\r
+ size.HighPart = service->rotate_bytes_high;\r
+\r
/* stdout */\r
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)) {\r
service->stdout_sharing = service->stdout_disposition = service->stdout_flags = 0;\r
}\r
if (si && service->stdout_path[0]) {\r
if (service->rotate_files) rotate_file(service->name, service->stdout_path, service->rotate_seconds, service->rotate_bytes_low, service->rotate_bytes_high);\r
- si->hStdOutput = append_to_file(service->stdout_path, service->stdout_sharing, &attributes, service->stdout_disposition, service->stdout_flags);\r
- if (! si->hStdOutput) return 4;\r
+ HANDLE stdout_handle = append_to_file(service->stdout_path, service->stdout_sharing, 0, service->stdout_disposition, service->stdout_flags);\r
+ if (! stdout_handle) {\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, service->stdout_path, error_string(GetLastError()), 0);\r
+ return 4;\r
+ }\r
+\r
+ /* Try online rotation only if a size threshold is set. */\r
+ logger_t *stdout_logger = 0;\r
+ if (service->rotate_files && service->rotate_stdout_online && size.QuadPart) {\r
+ stdout_logger = (logger_t *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(logger_t));\r
+ if (stdout_logger) {\r
+ /* Pipe between application's stdout and our logging handle. */\r
+ if (CreatePipe(&service->stdout_pipe, &si->hStdOutput, &attributes, 0)) {\r
+ stdout_logger->service_name = service->name;\r
+ stdout_logger->path = service->stdout_path;\r
+ stdout_logger->sharing = service->stdout_sharing;\r
+ stdout_logger->disposition = service->stdout_disposition;\r
+ stdout_logger->flags = service->stdout_flags;\r
+ stdout_logger->read_handle = service->stdout_pipe;\r
+ stdout_logger->write_handle = stdout_handle;\r
+ stdout_logger->size = (__int64) size.QuadPart;\r
+ stdout_logger->tid_ptr = &service->stdout_tid;\r
+\r
+ /* Logging thread. */\r
+ service->stdout_thread = create_logging_thread(stdout_logger);\r
+ if (! service->stdout_thread) {\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPIPE_FAILED, service->name, service->stdout_path, error_string(GetLastError()));\r
+ CloseHandle(service->stdout_pipe);\r
+ CloseHandle(si->hStdOutput);\r
+ service->stdout_tid = 0;\r
+ }\r
+ }\r
+ else service->stdout_tid = 0;\r
+ }\r
+ else {\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("stdout_logger"), _T("get_output_handles()"), 0);\r
+ service->stdout_tid = 0;\r
+ }\r
+\r
+ /* Fall through to try direct I/O. */\r
+ if (! service->stdout_tid) log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPIPE_FAILED, service->name, service->stdout_path, error_string(GetLastError()));\r
+ }\r
+\r
+ if (! service->stdout_tid) {\r
+ if (stdout_logger) HeapFree(GetProcessHeap(), 0, stdout_logger);\r
+ if (! DuplicateHandle(GetCurrentProcess(), stdout_handle, GetCurrentProcess(), &si->hStdOutput, 0, true, DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) {\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DUPLICATEHANDLE_FAILED, NSSM_REG_STDOUT, error_string(GetLastError()), 0);\r
+ return 4;\r
+ }\r
+ service->rotate_stdout_online = false;\r
+ }\r
+\r
set_flags = true;\r
}\r
\r
service->stderr_sharing = service->stdout_sharing;\r
service->stderr_disposition = service->stdout_disposition;\r
service->stderr_flags = service->stdout_flags;\r
+ service->rotate_stderr_online = false;\r
\r
if (si) {\r
/* Two handles to the same file will create a race. */\r
}\r
else if (si) {\r
if (service->rotate_files) rotate_file(service->name, service->stderr_path, service->rotate_seconds, service->rotate_bytes_low, service->rotate_bytes_high);\r
- si->hStdError = append_to_file(service->stderr_path, service->stdout_sharing, &attributes, service->stdout_disposition, service->stdout_flags);\r
- if (! si->hStdError) {\r
+ HANDLE stderr_handle = append_to_file(service->stderr_path, service->stdout_sharing, 0, service->stdout_disposition, service->stdout_flags);\r
+ if (! stderr_handle) {\r
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, service->stderr_path, error_string(GetLastError()), 0);\r
return 7;\r
}\r
- SetEndOfFile(si->hStdError);\r
+\r
+ /* Try online rotation only if a size threshold is set. */\r
+ logger_t *stderr_logger = 0;\r
+ if (service->rotate_files && service->rotate_stderr_online && size.QuadPart) {\r
+ stderr_logger = (logger_t *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(logger_t));\r
+ if (stderr_logger) {\r
+ /* Pipe between application's stderr and our logging handle. */\r
+ if (CreatePipe(&service->stderr_pipe, &si->hStdError, &attributes, 0)) {\r
+ stderr_logger->service_name = service->name;\r
+ stderr_logger->path = service->stderr_path;\r
+ stderr_logger->sharing = service->stderr_sharing;\r
+ stderr_logger->disposition = service->stderr_disposition;\r
+ stderr_logger->flags = service->stderr_flags;\r
+ stderr_logger->read_handle = service->stderr_pipe;\r
+ stderr_logger->write_handle = stderr_handle;\r
+ stderr_logger->size = (__int64) size.QuadPart;\r
+ stderr_logger->tid_ptr = &service->stderr_tid;\r
+\r
+ /* Logging thread. */\r
+ service->stderr_thread = create_logging_thread(stderr_logger);\r
+ if (! service->stderr_thread) {\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPIPE_FAILED, service->name, service->stderr_path, error_string(GetLastError()));\r
+ CloseHandle(service->stderr_pipe);\r
+ CloseHandle(si->hStdError);\r
+ service->stderr_tid = 0;\r
+ }\r
+ }\r
+ else service->stderr_tid = 0;\r
+ }\r
+ else {\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("stderr_logger"), _T("get_output_handles()"), 0);\r
+ service->stderr_tid = 0;\r
+ }\r
+\r
+ /* Fall through to try direct I/O. */\r
+ if (! service->stderr_tid) log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPIPE_FAILED, service->name, service->stderr_path, error_string(GetLastError()));\r
+ }\r
+\r
+ if (! service->stderr_tid) {\r
+ if (stderr_logger) HeapFree(GetProcessHeap(), 0, stderr_logger);\r
+ if (! DuplicateHandle(GetCurrentProcess(), stderr_handle, GetCurrentProcess(), &si->hStdError, 0, true, DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) {\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DUPLICATEHANDLE_FAILED, NSSM_REG_STDERR, error_string(GetLastError()), 0);\r
+ return 7;\r
+ }\r
+ service->rotate_stderr_online = false;\r
+ }\r
}\r
+\r
set_flags = true;\r
}\r
\r
return 0;\r
}\r
\r
-void close_output_handles(STARTUPINFO *si) {\r
+void close_output_handles(STARTUPINFO *si, bool close_stdout, bool close_stderr) {\r
if (si->hStdInput) CloseHandle(si->hStdInput);\r
- if (si->hStdOutput) CloseHandle(si->hStdOutput);\r
- if (si->hStdError) CloseHandle(si->hStdError);\r
+ if (si->hStdOutput && close_stdout) CloseHandle(si->hStdOutput);\r
+ if (si->hStdError && close_stderr) CloseHandle(si->hStdError);\r
+}\r
+\r
+void close_output_handles(STARTUPINFO *si) {\r
+ return close_output_handles(si, true, true);\r
+}\r
+\r
+/* Wrapper to be called in a new thread for logging. */\r
+unsigned long WINAPI log_and_rotate(void *arg) {\r
+ logger_t *logger = (logger_t *) arg;\r
+ if (! logger) return 1;\r
+\r
+ __int64 size;\r
+ BY_HANDLE_FILE_INFORMATION info;\r
+\r
+ /* Find initial file size. */\r
+ if (! GetFileInformationByHandle(logger->write_handle, &info)) logger->size = 0LL;\r
+ else {\r
+ ULARGE_INTEGER l;\r
+ l.HighPart = info.nFileSizeHigh;\r
+ l.LowPart = info.nFileSizeLow;\r
+ size = l.QuadPart;\r
+ }\r
+\r
+ char buffer[80];\r
+ void *address;\r
+ unsigned long in, out;\r
+ while (true) {\r
+ /* Read data from the pipe. */\r
+ address = &buffer;\r
+ if (! ReadFile(logger->read_handle, address, sizeof(buffer), &in, 0)) {\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_READFILE_FAILED, logger->service_name, logger->path, error_string(GetLastError()), 0);\r
+ return 2;\r
+ }\r
+\r
+ if (size + (__int64) in >= logger->size) {\r
+ /* Look for newline. */\r
+ unsigned long i;\r
+ for (i = 0; i < in; i++) {\r
+ if (buffer[i] == '\n') {\r
+ unsigned char unicode = IsTextUnicode(address, sizeof(buffer), 0);\r
+ if (unicode) i += sizeof(wchar_t);\r
+ else i += sizeof(char);\r
+\r
+ /* Write up to the newline. */\r
+ if (! WriteFile(logger->write_handle, address, i, &out, 0)) {\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_WRITEFILE_FAILED, logger->service_name, logger->path, error_string(GetLastError()), 0);\r
+ return 3;\r
+ }\r
+\r
+ /* Rotate. */\r
+ TCHAR rotated[MAX_PATH];\r
+ rotated_filename(logger->path, rotated, _countof(rotated), 0);\r
+\r
+ /*\r
+ Ideally we'd try the rename first then close the handle but\r
+ MoveFile() will fail if the handle is still open so we must\r
+ risk losing everything.\r
+ */\r
+ CloseHandle(logger->write_handle);\r
+ if (! MoveFile(logger->path, rotated)) {\r
+ unsigned long error = GetLastError();\r
+ if (error != ERROR_FILE_NOT_FOUND) {\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_ROTATE_FILE_FAILED, logger->service_name, logger->path, _T("MoveFile()"), rotated, error_string(error), 0);\r
+ /* We can at least try to re-open the existing file. */\r
+ logger->disposition = OPEN_ALWAYS;\r
+ }\r
+ }\r
+\r
+ /* Reopen. */\r
+ logger->write_handle = append_to_file(logger->path, logger->sharing, 0, logger->disposition, logger->flags);\r
+ if (! logger->write_handle) {\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, logger->path, error_string(GetLastError()), 0);\r
+ /* Oh dear. Now we can't log anything further. */\r
+ return 4;\r
+ }\r
+\r
+ /* Unicode files need a new BOM. */\r
+ if (unicode) write_bom(logger);\r
+\r
+ /* Resume writing after the newline. */\r
+ size = 0LL;\r
+ address = (void *) ((char *) address + i);\r
+ in -= i;\r
+\r
+ break;\r
+ }\r
+ }\r
+ }\r
+ else if (! size) {\r
+ /* Write a BOM to the new file. */\r
+ if (IsTextUnicode(address, sizeof(buffer), 0)) write_bom(logger);\r
+ }\r
+\r
+ /* Write the data. */\r
+ if (! WriteFile(logger->write_handle, address, in, &out, 0)) {\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_WRITEFILE_FAILED, logger->service_name, logger->path, error_string(GetLastError()), 0);\r
+ return 3;\r
+ }\r
+\r
+ size += (__int64) out;\r
+ }\r
+\r
+ return 0;\r
}\r