Legacy quick'n'dirtiness.
[nssm.git] / io.cpp
diff --git a/io.cpp b/io.cpp
index 4757e73..9dd21bf 100644 (file)
--- a/io.cpp
+++ b/io.cpp
@@ -61,6 +61,18 @@ int set_createfile_parameter(HKEY key, TCHAR *prefix, TCHAR *suffix, unsigned lo
   return set_number(key, value, number);\r
 }\r
 \r
+int delete_createfile_parameter(HKEY key, TCHAR *prefix, TCHAR *suffix) {\r
+  TCHAR value[NSSM_STDIO_LENGTH];\r
+\r
+  if (_sntprintf_s(value, _countof(value), _TRUNCATE, _T("%s%s"), prefix, suffix) < 0) {\r
+    log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, suffix, _T("delete_createfile_parameter()"), 0);\r
+    return 1;\r
+  }\r
+\r
+  if (RegDeleteValue(key, value)) return 0;\r
+  return 1;\r
+}\r
+\r
 HANDLE append_to_file(TCHAR *path, unsigned long sharing, SECURITY_ATTRIBUTES *attributes, unsigned long disposition, unsigned long flags) {\r
   HANDLE ret;\r
 \r
@@ -152,9 +164,6 @@ void rotate_file(TCHAR *service_name, TCHAR *path, unsigned long seconds, unsign
 }\r
 \r
 int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) {\r
-  TCHAR path[MAX_PATH];\r
-  TCHAR stdout_path[MAX_PATH];\r
-  unsigned long sharing, disposition, flags;\r
   bool set_flags = false;\r
 \r
   /* Standard security attributes allowing inheritance. */\r
@@ -163,49 +172,60 @@ int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) {
   attributes.bInheritHandle = true;\r
 \r
   /* stdin */\r
-  if (get_createfile_parameters(key, NSSM_REG_STDIN, path, &sharing, NSSM_STDIN_SHARING, &disposition, NSSM_STDIN_DISPOSITION, &flags, NSSM_STDIN_FLAGS)) return 1;\r
-  if (path[0]) {\r
-    si->hStdInput = CreateFile(path, FILE_READ_DATA, sharing, &attributes, disposition, flags, 0);\r
+  if (get_createfile_parameters(key, NSSM_REG_STDIN, service->stdin_path, &service->stdin_sharing, NSSM_STDIN_SHARING, &service->stdin_disposition, NSSM_STDIN_DISPOSITION, &service->stdin_flags, NSSM_STDIN_FLAGS)) {\r
+    service->stdin_sharing = service->stdin_disposition = service->stdin_flags = 0;\r
+    ZeroMemory(service->stdin_path, _countof(service->stdin_path) * sizeof(TCHAR));\r
+    return 1;\r
+  }\r
+  if (si && service->stdin_path[0]) {\r
+    si->hStdInput = CreateFile(service->stdin_path, FILE_READ_DATA, service->stdin_sharing, &attributes, service->stdin_disposition, service->stdin_flags, 0);\r
     if (! si->hStdInput) {\r
-      log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, path, error_string(GetLastError()));\r
+      log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, service->stdin_path, error_string(GetLastError()), 0);\r
       return 2;\r
     }\r
     set_flags = true;\r
   }\r
 \r
   /* stdout */\r
-  if (get_createfile_parameters(key, NSSM_REG_STDOUT, path, &sharing, NSSM_STDOUT_SHARING, &disposition, NSSM_STDOUT_DISPOSITION, &flags, NSSM_STDOUT_FLAGS)) return 3;\r
-  if (path[0]) {\r
-    /* Remember path for comparison with stderr. */\r
-    if (_sntprintf_s(stdout_path, _countof(stdout_path), _TRUNCATE, _T("%s"), path) < 0) {\r
-      log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("stdout_path"), _T("get_output_handles"), 0);\r
-      return 4;\r
-    }\r
-\r
-    if (service->rotate_files) rotate_file(service->name, path, service->rotate_seconds, service->rotate_bytes_low, service->rotate_bytes_high);\r
-    si->hStdOutput = append_to_file(path, sharing, &attributes, disposition, flags);\r
-    if (! si->hStdOutput) return 5;\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
+    ZeroMemory(service->stdout_path, _countof(service->stdout_path) * sizeof(TCHAR));\r
+    return 3;\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
     set_flags = true;\r
   }\r
-  else ZeroMemory(stdout_path, sizeof(stdout_path));\r
 \r
   /* stderr */\r
-  if (get_createfile_parameters(key, NSSM_REG_STDERR, path, &sharing, NSSM_STDERR_SHARING, &disposition, NSSM_STDERR_DISPOSITION, &flags, NSSM_STDERR_FLAGS)) return 6;\r
-  if (path[0]) {\r
+  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)) {\r
+    service->stderr_sharing = service->stderr_disposition = service->stderr_flags = 0;\r
+    ZeroMemory(service->stderr_path, _countof(service->stderr_path) * sizeof(TCHAR));\r
+    return 5;\r
+  }\r
+  if (service->stderr_path[0]) {\r
     /* Same as stdout? */\r
-    if (str_equiv(path, stdout_path)) {\r
-      /* Two handles to the same file will create a race. */\r
-      if (! DuplicateHandle(GetCurrentProcess(), si->hStdOutput, GetCurrentProcess(), &si->hStdError, 0, true, DUPLICATE_SAME_ACCESS)) {\r
-        log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DUPLICATEHANDLE_FAILED, NSSM_REG_STDOUT, error_string(GetLastError()), 0);\r
-        return 7;\r
+    if (str_equiv(service->stderr_path, service->stdout_path)) {\r
+      service->stderr_sharing = service->stdout_sharing;\r
+      service->stderr_disposition = service->stdout_disposition;\r
+      service->stderr_flags = service->stdout_flags;\r
+\r
+      if (si) {\r
+        /* Two handles to the same file will create a race. */\r
+        if (! DuplicateHandle(GetCurrentProcess(), si->hStdOutput, GetCurrentProcess(), &si->hStdError, 0, true, DUPLICATE_SAME_ACCESS)) {\r
+          log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DUPLICATEHANDLE_FAILED, NSSM_REG_STDOUT, error_string(GetLastError()), 0);\r
+          return 6;\r
+        }\r
       }\r
     }\r
-    else {\r
-      if (service->rotate_files) rotate_file(service->name, path, service->rotate_seconds, service->rotate_bytes_low, service->rotate_bytes_high);\r
-      si->hStdError = append_to_file(path, sharing, &attributes, disposition, flags);\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
-        log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, path, error_string(GetLastError()), 0);\r
-        return 8;\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
@@ -218,7 +238,7 @@ int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) {
     We need to set the startup_info flags to make the new handles\r
     inheritable by the new process.\r
   */\r
-  si->dwFlags |= STARTF_USESTDHANDLES;\r
+  if (si) si->dwFlags |= STARTF_USESTDHANDLES;\r
 \r
   return 0;\r
 }\r