From 9f3d08676d86c939b864b4868d99b6e5772d95c8 Mon Sep 17 00:00:00 2001 From: Iain Patterson Date: Fri, 20 Dec 2013 19:09:38 +0000 Subject: [PATCH] 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. --- io.cpp | 2 +- registry.cpp | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/io.cpp b/io.cpp index 501f605..5331188 100644 --- a/io.cpp +++ b/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)) { diff --git a/registry.cpp b/registry.cpp index 59b8c48..e192529 100644 --- a/registry.cpp +++ b/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); -- 2.20.1