work. Remember, however, that the path must be accessible to the user\r
running the service.\r
\r
+Note that if you set AppStdout and/or AppStderr, applications which attempt\r
+to read stdin will fail due to a combination of factors including the way I/O\r
+redirection is configured on Windows and how a console application starts in\r
+a service context. NSSM can fake a stdin stream so that applications can\r
+still work when they would otherwise exit when at end of file on stdin. Set\r
+AppStdin to "|" (a single pipe character) to invoke the fake stdin.\r
+\r
\r
File rotation\r
-------------\r
Thanks to Robert Middleton for suggestion and draft implementation of process\r
affinity support.\r
Thanks to Andrew RedzMax for suggesting an unconditional restart delay.\r
+Thanks to Bryan Senseman for noticing that applications with redirected stdout\r
+and/or stderr which attempt to read from stdin would fail.\r
\r
Licence\r
-------\r
ZeroMemory(&attributes, sizeof(attributes));\r
attributes.bInheritHandle = true;\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
- 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, 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, 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
}\r
}\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 (str_equiv(service->stdin_path, _T("|"))) {\r
+ /* Fake stdin with a pipe. */\r
+ if (set_flags) {\r
+ /*\r
+ None of this is necessary if we aren't redirecting stdout and/or\r
+ stderr as well.\r
+\r
+ If we don't redirect any handles the application will start and be\r
+ quite happy with its console. If we start it with\r
+ STARTF_USESTDHANDLES set it will interpret a NULL value for\r
+ hStdInput as meaning no input. Because the service starts with\r
+ no stdin we can't just pass GetStdHandle(STD_INPUT_HANDLE) to\r
+ the application.\r
+\r
+ The only way we can successfully redirect the application's output\r
+ while preventing programs which exit after reading all input from\r
+ exiting prematurely is to create a pipe between ourselves and the\r
+ application but write nothing to it.\r
+ */\r
+ if (! CreatePipe(&si->hStdInput, &service->stdin_pipe, 0, 0)) {\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_STDIN_CREATEPIPE_FAILED, service->name, error_string(GetLastError()), 0);\r
+ return 2;\r
+ }\r
+ SetHandleInformation(si->hStdInput, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT);\r
+ }\r
+ }\r
+ else {\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, service->stdin_path, error_string(GetLastError()), 0);\r
+ return 2;\r
+ }\r
+\r
+ set_flags = true;\r
+ }\r
+ }\r
+\r
if (! set_flags) return 0;\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
+ if (service->stdin_pipe) flags |= DETACHED_PROCESS;\r
if (service->affinity) flags |= CREATE_SUSPENDED;\r
#ifdef UNICODE\r
flags |= CREATE_UNICODE_ENVIRONMENT;\r
UnregisterWait(service->wait_handle);\r
service->wait_handle = 0;\r
}\r
+ if (service->stdin_pipe) {\r
+ CloseHandle(service->stdin_pipe);\r
+ service->stdin_pipe = 0;\r
+ }\r
\r
service->rotate_stdout_online = service->rotate_stderr_online = NSSM_ROTATE_OFFLINE;\r
\r