Allow relative paths for I/O redirection.
authorIain Patterson <me@iain.cx>
Fri, 20 Dec 2013 19:09:38 +0000 (19:09 +0000)
committerIain Patterson <me@iain.cx>
Fri, 20 Dec 2013 19:19:05 +0000 (19:19 +0000)
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.

io.cpp
registry.cpp

diff --git a/io.cpp b/io.cpp
index 501f605..5331188 100644 (file)
--- a/io.cpp
+++ b/io.cpp
@@ -121,7 +121,7 @@ int get_output_handles(HKEY key, STARTUPINFO *si) {
   /* 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
-    /* Same as stdin? */\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
index 59b8c48..e192529 100644 (file)
@@ -387,13 +387,22 @@ int get_parameters(nssm_service_t *service, STARTUPINFO *si) {
     }\r
   }\r
 \r
+  /* Change to startup directory in case stdout/stderr are relative paths. */\r
+  TCHAR cwd[MAX_PATH];\r
+  GetCurrentDirectory(_countof(cwd), cwd);\r
+  SetCurrentDirectory(service->dir);\r
+\r
   /* Try to get stdout and stderr */\r
   if (get_output_handles(key, si)) {\r
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_GET_OUTPUT_HANDLES_FAILED, service->name, 0);\r
     RegCloseKey(key);\r
+    SetCurrentDirectory(cwd);\r
     return 5;\r
   }\r
 \r
+  /* Change back in case the startup directory needs to be deleted. */\r
+  SetCurrentDirectory(cwd);\r
+\r
   /* Try to get throttle restart delay */\r
   override_milliseconds(service->name, key, NSSM_REG_THROTTLE, &service->throttle_delay, NSSM_RESET_THROTTLE_RESTART, NSSM_EVENT_BOGUS_THROTTLE);\r
 \r