Tidy up online rotation.
authorIain Patterson <me@iain.cx>
Mon, 13 Jan 2014 23:11:45 +0000 (23:11 +0000)
committerIain Patterson <me@iain.cx>
Mon, 13 Jan 2014 23:11:45 +0000 (23:11 +0000)
Move the messy copy/paste of the logging thread setup into a separate function.
No longer incorrectly tread longs as bools.
Fixed some typos and incorrect logic.

io.cpp
io.h
messages.mc
service.cpp

diff --git a/io.cpp b/io.cpp
index 79848e9..5efffe0 100644 (file)
--- a/io.cpp
+++ b/io.cpp
@@ -4,9 +4,49 @@
 #define COMPLAINED_WRITE (1 << 1)\r
 #define COMPLAINED_ROTATE (1 << 2)\r
 \r
-static HANDLE create_logging_thread(logger_t *logger) {\r
+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 *tid_ptr, unsigned long *rotate_online) {\r
+  *tid_ptr = 0;\r
+\r
+  /* Pipe between application's stdout/stderr and our logging handle. */\r
+  if (read_handle_ptr && ! *read_handle_ptr) {\r
+    if (pipe_handle_ptr && ! *pipe_handle_ptr) {\r
+      if (CreatePipe(read_handle_ptr, pipe_handle_ptr, 0, 0)) {\r
+        SetHandleInformation(*pipe_handle_ptr, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT);\r
+      }\r
+      else {\r
+        log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPIPE_FAILED, service_name, path, error_string(GetLastError()));\r
+        return (HANDLE) 0;\r
+      }\r
+    }\r
+  }\r
+\r
+  logger_t *logger = (logger_t *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(logger_t));\r
+  if (! logger) {\r
+    log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("logger"), _T("create_logging_thread()"), 0);\r
+    return (HANDLE) 0;\r
+  }\r
+\r
+  ULARGE_INTEGER size;\r
+  size.LowPart = rotate_bytes_low;\r
+  size.HighPart = rotate_bytes_high;\r
+\r
+  logger->service_name = service_name;\r
+  logger->path = path;\r
+  logger->sharing = sharing;\r
+  logger->disposition = disposition;\r
+  logger->flags = flags;\r
+  logger->read_handle = *read_handle_ptr;\r
+  logger->write_handle = *write_handle_ptr;\r
+  logger->size = (__int64) size.QuadPart;\r
+  logger->tid_ptr = tid_ptr;\r
+  logger->rotate_online = rotate_online;\r
+\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
+  if (! thread_handle) {\r
+    log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATETHREAD_FAILED, error_string(GetLastError()), 0);\r
+    HeapFree(GetProcessHeap(), 0, logger);\r
+  }\r
+\r
   return thread_handle;\r
 }\r
 \r
@@ -112,7 +152,10 @@ HANDLE append_to_file(TCHAR *path, unsigned long sharing, SECURITY_ATTRIBUTES *a
   }\r
 \r
   /* It didn't exist.  Create it. */\r
-  return CreateFile(path, FILE_WRITE_DATA, sharing, attributes, disposition, flags, 0);\r
+  ret = CreateFile(path, FILE_WRITE_DATA, sharing, attributes, disposition, flags, 0);\r
+  if (! ret) log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, path, error_string(GetLastError()), 0);\r
+\r
+  return ret;\r
 }\r
 \r
 static void rotated_filename(TCHAR *path, TCHAR *rotated, unsigned long rotated_len, SYSTEMTIME *st) {\r
@@ -222,10 +265,6 @@ int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) {
     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
@@ -235,56 +274,20 @@ int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) {
   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
     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
+    if (! stdout_handle) return 4;\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) {\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
-          stdout_logger->rotate_online = &service->rotate_stdout_online;\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
+      service->stdout_pipe = si->hStdOutput = 0;\r
+      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->stdout_tid, &service->rotate_stdout_online);\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
+      if (! service->stdout_thread) {\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 = NSSM_ROTATE_OFFLINE;\r
       }\r
-      service->rotate_stdout_online = NSSM_ROTATE_OFFLINE;\r
     }\r
 \r
     set_flags = true;\r
@@ -302,7 +305,7 @@ int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) {
       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
+      service->rotate_stderr_online = NSSM_ROTATE_OFFLINE;\r
 \r
       if (si) {\r
         /* Two handles to the same file will create a race. */\r
@@ -315,60 +318,23 @@ int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) {
     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
       HANDLE stderr_handle = append_to_file(service->stderr_path, service->stderr_sharing, 0, service->stderr_disposition, service->stderr_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
+      if (! stderr_handle) return 7;\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) {\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
-            stderr_logger->rotate_online = &service->rotate_stderr_online;\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
+        service->stderr_pipe = si->hStdError = 0;\r
+        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->stderr_tid, &service->rotate_stderr_online);\r
+\r
+        if (! service->stderr_thread) {\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
-          else service->stderr_tid = 0;\r
+          service->rotate_stderr_online = NSSM_ROTATE_OFFLINE;\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 = NSSM_ROTATE_OFFLINE;\r
-      }\r
+      set_flags = true;\r
     }\r
-\r
-    set_flags = true;\r
   }\r
 \r
   if (! set_flags) return 0;\r
@@ -382,14 +348,10 @@ int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) {
   return 0;\r
 }\r
 \r
-void close_output_handles(STARTUPINFO *si, bool close_stdout, bool close_stderr) {\r
-  if (si->hStdInput) CloseHandle(si->hStdInput);\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
+  if (si->hStdInput) CloseHandle(si->hStdInput);\r
+  if (si->hStdOutput) CloseHandle(si->hStdOutput);\r
+  if (si->hStdError) CloseHandle(si->hStdError);\r
 }\r
 \r
 /*\r
@@ -428,6 +390,8 @@ static int try_read(logger_t *logger, void *address, unsigned long bufsize, unsi
   }\r
 \r
 complain_read:\r
+  /* Ignore the error if we've been requested to exit for rotation anyway. */\r
+  if (*logger->rotate_online == NSSM_ROTATE_ONLINE_ASAP) return ret;\r
   if (! (*complained & COMPLAINED_READ)) log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_READFILE_FAILED, logger->service_name, logger->path, error_string(error), 0);\r
   *complained |= COMPLAINED_READ;\r
   return ret;\r
@@ -507,6 +471,8 @@ unsigned long WINAPI log_and_rotate(void *arg) {
     address = &buffer;\r
     ret = try_read(logger, address, sizeof(buffer), &in, &complained);\r
     if (ret < 0) {\r
+      CloseHandle(logger->read_handle);\r
+      CloseHandle(logger->write_handle);\r
       HeapFree(GetProcessHeap(), 0, logger);\r
       return 2;\r
     }\r
@@ -524,6 +490,8 @@ unsigned long WINAPI log_and_rotate(void *arg) {
           ret = try_write(logger, address, i, &out, &complained);\r
           if (ret < 0) {\r
             HeapFree(GetProcessHeap(), 0, logger);\r
+            CloseHandle(logger->read_handle);\r
+            CloseHandle(logger->write_handle);\r
             return 3;\r
           }\r
           size += (__int64) out;\r
@@ -560,6 +528,8 @@ unsigned long WINAPI log_and_rotate(void *arg) {
             log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, logger->path, error_string(error), 0);\r
             /* Oh dear.  Now we can't log anything further. */\r
             HeapFree(GetProcessHeap(), 0, logger);\r
+            CloseHandle(logger->read_handle);\r
+            CloseHandle(logger->write_handle);\r
             return 4;\r
           }\r
 \r
@@ -584,10 +554,14 @@ unsigned long WINAPI log_and_rotate(void *arg) {
     size += (__int64) out;\r
     if (ret < 0) {\r
       HeapFree(GetProcessHeap(), 0, logger);\r
+      CloseHandle(logger->read_handle);\r
+      CloseHandle(logger->write_handle);\r
       return 3;\r
     }\r
   }\r
 \r
   HeapFree(GetProcessHeap(), 0, logger);\r
+  CloseHandle(logger->read_handle);\r
+  CloseHandle(logger->write_handle);\r
   return 0;\r
 }\r
diff --git a/io.h b/io.h
index 60c3fd5..d91d39c 100644 (file)
--- a/io.h
+++ b/io.h
@@ -30,7 +30,6 @@ int delete_createfile_parameter(HKEY, TCHAR *, TCHAR *);
 HANDLE append_to_file(TCHAR *, unsigned long, SECURITY_ATTRIBUTES *, unsigned long, unsigned long);\r
 void rotate_file(TCHAR *, TCHAR *, unsigned long, unsigned long, unsigned long);\r
 int get_output_handles(nssm_service_t *, HKEY, STARTUPINFO *);\r
-void close_output_handles(STARTUPINFO *, bool, bool);\r
 void close_output_handles(STARTUPINFO *);\r
 unsigned long WINAPI log_and_rotate(void *);\r
 \r
index 5bb5adc..49ade07 100644 (file)
Binary files a/messages.mc and b/messages.mc differ
index 1c82a38..9aa9b01 100644 (file)
@@ -1473,8 +1473,8 @@ unsigned long WINAPI service_control_handler(unsigned long control, unsigned lon
 \r
     case NSSM_SERVICE_CONTROL_ROTATE:\r
       log_service_control(service->name, control, true);\r
-      if (service->rotate_stdout_online) service->rotate_stdout_online = NSSM_ROTATE_ONLINE_ASAP;\r
-      if (service->rotate_stderr_online) service->rotate_stderr_online = NSSM_ROTATE_ONLINE_ASAP;\r
+      if (service->rotate_stdout_online == NSSM_ROTATE_ONLINE) service->rotate_stdout_online = NSSM_ROTATE_ONLINE_ASAP;\r
+      if (service->rotate_stderr_online == NSSM_ROTATE_ONLINE) service->rotate_stderr_online = NSSM_ROTATE_ONLINE_ASAP;\r
       return NO_ERROR;\r
   }\r
 \r
@@ -1539,7 +1539,7 @@ int start_service(nssm_service_t *service) {
 \r
   if (get_process_creation_time(service->process_handle, &service->creation_time)) ZeroMemory(&service->creation_time, sizeof(service->creation_time));\r
 \r
-  close_output_handles(&si, ! service->rotate_stdout_online, ! service->rotate_stderr_online);\r
+  close_output_handles(&si);\r
 \r
   if (service->affinity) {\r
     /*\r