return;\r
}\r
\r
-int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) {\r
+int get_output_handles(nssm_service_t *service, STARTUPINFO *si) {\r
+ if (! si) return 1;\r
+\r
/* Allocate a new console so we get a fresh stdin, stdout and stderr. */\r
- if (si) alloc_console(service);\r
+ alloc_console(service);\r
\r
/* stdin */\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
+ if (service->stdin_path[0]) {\r
si->hStdInput = CreateFile(service->stdin_path, FILE_READ_DATA, service->stdin_sharing, 0, service->stdin_disposition, service->stdin_flags, 0);\r
if (! si->hStdInput) {\r
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, service->stdin_path, error_string(GetLastError()), 0);\r
}\r
\r
/* stdout */\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->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
HANDLE stdout_handle = write_to_file(service->stdout_path, service->stdout_sharing, 0, service->stdout_disposition, service->stdout_flags);\r
if (! stdout_handle) return 4;\r
}\r
\r
/* stderr */\r
- if (get_createfile_parameters(key, NSSM_REG_STDERR, service->stderr_path, &service->stderr_sharing, NSSM_STDERR_SHARING, &service->stderr_disposition, NSSM_STDERR_DISPOSITION, &service->stderr_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(service->stderr_path, service->stdout_path)) {\r
service->stderr_flags = service->stdout_flags;\r
service->rotate_stderr_online = NSSM_ROTATE_OFFLINE;\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, _T("stderr"), error_string(GetLastError()), 0);\r
- return 6;\r
- }\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, _T("stderr"), error_string(GetLastError()), 0);\r
+ return 6;\r
}\r
}\r
- else if (si) {\r
+ else {\r
if (service->rotate_files) rotate_file(service->name, service->stderr_path, service->rotate_seconds, service->rotate_bytes_low, service->rotate_bytes_high);\r
HANDLE stderr_handle = write_to_file(service->stderr_path, service->stderr_sharing, 0, service->stderr_disposition, service->stderr_flags);\r
if (! stderr_handle) return 7;\r
}\r
}\r
\r
- if (! si) return 0;\r
-\r
/*\r
We need to set the startup_info flags to make the new handles\r
inheritable by the new process.\r
int delete_createfile_parameter(HKEY, TCHAR *, TCHAR *);\r
HANDLE write_to_file(TCHAR *, unsigned long, SECURITY_ATTRIBUTES *, unsigned long, unsigned long);\r
void rotate_file(TCHAR *, TCHAR *, unsigned long, unsigned long, unsigned long);\r
-int get_output_handles(nssm_service_t *, HKEY, STARTUPINFO *);\r
+int get_output_handles(nssm_service_t *, STARTUPINFO *);\r
void close_output_handles(STARTUPINFO *);\r
unsigned long WINAPI log_and_rotate(void *);\r
\r
return open_registry(service_name, 0, sam);\r
}\r
\r
+int get_io_parameters(nssm_service_t *service, HKEY key) {\r
+ /* stdin */\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
+\r
+ /* stdout */\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 2;\r
+ }\r
+\r
+ /* stderr */\r
+ if (get_createfile_parameters(key, NSSM_REG_STDERR, service->stderr_path, &service->stderr_sharing, NSSM_STDERR_SHARING, &service->stderr_disposition, NSSM_STDERR_DISPOSITION, &service->stderr_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 3;\r
+ }\r
+\r
+ return 0;\r
+}\r
+\r
int get_parameters(nssm_service_t *service, STARTUPINFO *si) {\r
unsigned long ret;\r
\r
SetCurrentDirectory(service->dir);\r
\r
/* Try to get stdout and stderr */\r
- if (get_output_handles(service, key, si)) {\r
+ if (get_io_parameters(service, key)) {\r
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_GET_OUTPUT_HANDLES_FAILED, service->name, 0);\r
RegCloseKey(key);\r
SetCurrentDirectory(cwd);\r
int get_number(HKEY, TCHAR *, unsigned long *, bool);\r
int get_number(HKEY, TCHAR *, unsigned long *);\r
void override_milliseconds(TCHAR *, HKEY, TCHAR *, unsigned long *, unsigned long, unsigned long);\r
+int get_io_parameters(nssm_service_t *, HKEY);\r
int get_parameters(nssm_service_t *, STARTUPINFO *);\r
int get_exit_action(const TCHAR *, unsigned long *, TCHAR *, bool *);\r
\r
TCHAR cmd[CMD_LENGTH];\r
if (_sntprintf_s(cmd, _countof(cmd), _TRUNCATE, _T("\"%s\" %s"), service->exe, service->flags) < 0) {\r
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("command line"), _T("start_service"), 0);\r
- close_output_handles(&si);\r
return stop_service(service, 2, true, true);\r
}\r
\r
if (service->env) duplicate_environment(service->env);\r
if (service->env_extra) set_environment_block(service->env_extra);\r
\r
+ /* Set up I/O redirection. */\r
+ if (get_output_handles(service, &si)) {\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_GET_OUTPUT_HANDLES_FAILED, service->name, 0);\r
+ if (! service->no_console) FreeConsole();\r
+ close_output_handles(&si);\r
+ return stop_service(service, 4, true, true);\r
+ }\r
+\r
bool inherit_handles = false;\r
if (si.dwFlags & STARTF_USESTDHANDLES) inherit_handles = true;\r
unsigned long flags = service->priority & priority_mask();\r