Use close_handle().
authorIain Patterson <me@iain.cx>
Tue, 30 Aug 2016 07:35:35 +0000 (08:35 +0100)
committerIain Patterson <me@iain.cx>
Mon, 5 Sep 2016 07:44:58 +0000 (08:44 +0100)
Call new close_handle() function when tidying up trying to set up I/O.

io.cpp

diff --git a/io.cpp b/io.cpp
index 3460cdc..0c904be 100644 (file)
--- a/io.cpp
+++ b/io.cpp
@@ -336,12 +336,7 @@ int get_output_handles(nssm_service_t *service, STARTUPINFO *si) {
       service->rotate_stdout_online = NSSM_ROTATE_OFFLINE;\r
     }\r
 \r
-    if (dup_handle(service->stdout_si, &si->hStdOutput, _T("stdout_si"), _T("stdout"))) {\r
-      if (service->stdout_thread) {\r
-        CloseHandle(service->stdout_thread);\r
-        service->stdout_thread = 0;\r
-      }\r
-    }\r
+    if (dup_handle(service->stdout_si, &si->hStdOutput, _T("stdout_si"), _T("stdout"))) close_handle(&service->stdout_thread);\r
   }\r
 \r
   /* stderr */\r
@@ -379,12 +374,7 @@ int get_output_handles(nssm_service_t *service, STARTUPINFO *si) {
       }\r
     }\r
 \r
-    if (dup_handle(service->stderr_si, &si->hStdError, _T("stderr_si"), _T("stderr"))) {\r
-      if (service->stderr_thread) {\r
-        CloseHandle(service->stderr_thread);\r
-        service->stderr_thread = 0;\r
-      }\r
-    }\r
+    if (dup_handle(service->stderr_si, &si->hStdError, _T("stderr_si"), _T("stderr"))) close_handle(&service->stderr_thread);\r
   }\r
 \r
   /*\r
@@ -555,8 +545,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
+      close_handle(&logger->read_handle);\r
+      close_handle(&logger->write_handle);\r
       HeapFree(GetProcessHeap(), 0, logger);\r
       return 2;\r
     }\r
@@ -573,8 +563,8 @@ unsigned long WINAPI log_and_rotate(void *arg) {
           /* Write up to the newline. */\r
           ret = try_write(logger, address, i, &out, &complained);\r
           if (ret < 0) {\r
-            CloseHandle(logger->read_handle);\r
-            CloseHandle(logger->write_handle);\r
+            close_handle(&logger->read_handle);\r
+            close_handle(&logger->write_handle);\r
             HeapFree(GetProcessHeap(), 0, logger);\r
             return 3;\r
           }\r
@@ -591,7 +581,7 @@ unsigned long WINAPI log_and_rotate(void *arg) {
             risk losing everything.\r
           */\r
           if (logger->copy_and_truncate) FlushFileBuffers(logger->write_handle);\r
-          CloseHandle(logger->write_handle);\r
+          close_handle(&logger->write_handle);\r
           bool ok = true;\r
           TCHAR *function;\r
           if (logger->copy_and_truncate) {\r
@@ -629,8 +619,8 @@ unsigned long WINAPI log_and_rotate(void *arg) {
             error = GetLastError();\r
             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
-            CloseHandle(logger->read_handle);\r
-            CloseHandle(logger->write_handle);\r
+            close_handle(&logger->read_handle);\r
+            close_handle(&logger->write_handle);\r
             HeapFree(GetProcessHeap(), 0, logger);\r
             return 4;\r
           }\r
@@ -655,15 +645,15 @@ unsigned long WINAPI log_and_rotate(void *arg) {
     ret = try_write(logger, address, in, &out, &complained);\r
     size += (__int64) out;\r
     if (ret < 0) {\r
-      CloseHandle(logger->read_handle);\r
-      CloseHandle(logger->write_handle);\r
+      close_handle(&logger->read_handle);\r
+      close_handle(&logger->write_handle);\r
       HeapFree(GetProcessHeap(), 0, logger);\r
       return 3;\r
     }\r
   }\r
 \r
-  CloseHandle(logger->read_handle);\r
-  CloseHandle(logger->write_handle);\r
+  close_handle(&logger->read_handle);\r
+  close_handle(&logger->write_handle);\r
   HeapFree(GetProcessHeap(), 0, logger);\r
   return 0;\r
 }\r