Browse Source

Allow relative paths for I/O redirection.

Change to the startup directory before redirecting I/O streams so that
relative paths can be specified.  Then change back so we don't tie up
the directory.
Iain Patterson 8 years ago
parent
commit
9f3d08676d
2 changed files with 10 additions and 1 deletions
  1. 1 1
      io.cpp
  2. 9 0
      registry.cpp

+ 1 - 1
io.cpp

@@ -121,7 +121,7 @@ int get_output_handles(HKEY key, STARTUPINFO *si) {
   /* stderr */
   if (get_createfile_parameters(key, NSSM_REG_STDERR, path, &sharing, NSSM_STDERR_SHARING, &disposition, NSSM_STDERR_DISPOSITION, &flags, NSSM_STDERR_FLAGS)) return 6;
   if (path[0]) {
-    /* Same as stdin? */
+    /* Same as stdout? */
     if (str_equiv(path, stdout_path)) {
       /* Two handles to the same file will create a race. */
       if (! DuplicateHandle(GetCurrentProcess(), si->hStdOutput, GetCurrentProcess(), &si->hStdError, 0, true, DUPLICATE_SAME_ACCESS)) {

+ 9 - 0
registry.cpp

@@ -387,13 +387,22 @@ int get_parameters(nssm_service_t *service, STARTUPINFO *si) {
     }
   }
 
+  /* Change to startup directory in case stdout/stderr are relative paths. */
+  TCHAR cwd[MAX_PATH];
+  GetCurrentDirectory(_countof(cwd), cwd);
+  SetCurrentDirectory(service->dir);
+
   /* Try to get stdout and stderr */
   if (get_output_handles(key, si)) {
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_GET_OUTPUT_HANDLES_FAILED, service->name, 0);
     RegCloseKey(key);
+    SetCurrentDirectory(cwd);
     return 5;
   }
 
+  /* Change back in case the startup directory needs to be deleted. */
+  SetCurrentDirectory(cwd);
+
   /* Try to get throttle restart delay */
   override_milliseconds(service->name, key, NSSM_REG_THROTTLE, &service->throttle_delay, NSSM_RESET_THROTTLE_RESTART, NSSM_EVENT_BOGUS_THROTTLE);