--- /dev/null
+#include "nssm.h"\r
+\r
+/* Get path, share mode, creation disposition and flags for a stream. */\r
+int get_createfile_parameters(HKEY key, char *prefix, char *path, unsigned long *sharing, unsigned long default_sharing, unsigned long *disposition, unsigned long default_disposition, unsigned long *flags, unsigned long default_flags) {\r
+ char value[NSSM_STDIO_LENGTH];\r
+\r
+ /* Path. */\r
+ if (_snprintf(value, sizeof(value), "%s", prefix) < 0) {\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, prefix, "get_createfile_parameters()", 0);\r
+ return 1;\r
+ }\r
+ switch (expand_parameter(key, value, path, MAX_PATH, true, false)) {\r
+ case 0: if (! path[0]) return 0; break; /* OK. */\r
+ default: return 2; /* Error. */\r
+ }\r
+\r
+ /* ShareMode. */\r
+ if (_snprintf(value, sizeof(value), "%s%s", prefix, NSSM_REG_STDIO_SHARING) < 0) {\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, NSSM_REG_STDIO_SHARING, "get_createfile_parameters()", 0);\r
+ return 3;\r
+ }\r
+ switch (get_number(key, value, sharing, false)) {\r
+ case 0: *sharing = default_sharing; break; /* Missing. */\r
+ case 1: break; /* Found. */\r
+ case -2: return 4; break; /* Error. */\r
+ }\r
+\r
+ /* CreationDisposition. */\r
+ if (_snprintf(value, sizeof(value), "%s%s", prefix, NSSM_REG_STDIO_DISPOSITION) < 0) {\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, NSSM_REG_STDIO_DISPOSITION, "get_createfile_parameters()", 0);\r
+ return 5;\r
+ }\r
+ switch (get_number(key, value, disposition, false)) {\r
+ case 0: *disposition = default_disposition; break; /* Missing. */\r
+ case 1: break; /* Found. */\r
+ case -2: return 6; break; /* Error. */\r
+ }\r
+\r
+ /* Flags. */\r
+ if (_snprintf(value, sizeof(value), "%s%s", prefix, NSSM_REG_STDIO_FLAGS) < 0) {\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, NSSM_REG_STDIO_FLAGS, "get_createfile_parameters()", 0);\r
+ return 7;\r
+ }\r
+ switch (get_number(key, value, flags, false)) {\r
+ case 0: *flags = default_flags; break; /* Missing. */\r
+ case 1: break; /* Found. */\r
+ case -2: return 8; break; /* Error. */\r
+ }\r
+\r
+ return 0;\r
+}\r
+\r
+HANDLE append_to_file(char *path, unsigned long sharing, SECURITY_ATTRIBUTES *attributes, unsigned long disposition, unsigned long flags) {\r
+ HANDLE ret;\r
+\r
+ /* Try to append to the file first. */\r
+ ret = CreateFile(path, FILE_APPEND_DATA, sharing, attributes, disposition, flags, 0);\r
+ if (ret) {\r
+ SetEndOfFile(ret);\r
+ return ret;\r
+ }\r
+\r
+ unsigned long error = GetLastError();\r
+ if (error != ERROR_FILE_NOT_FOUND) {\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, path, error_string(error));\r
+ return (HANDLE) 0;\r
+ }\r
+\r
+ /* It didn't exist. Create it. */\r
+ return CreateFile(path, FILE_WRITE_DATA, sharing, attributes, disposition, flags, 0);\r
+}\r
+\r
+int get_output_handles(HKEY key, STARTUPINFO *si) {\r
+ char path[MAX_PATH];\r
+ char stdout_path[MAX_PATH];\r
+ unsigned long sharing, disposition, flags;\r
+ bool set_flags = false;\r
+\r
+ /* Standard security attributes allowing inheritance. */\r
+ SECURITY_ATTRIBUTES attributes;\r
+ ZeroMemory(&attributes, sizeof(attributes));\r
+ attributes.bInheritHandle = true;\r
+\r
+ /* stdin */\r
+ if (get_createfile_parameters(key, NSSM_REG_STDIN, path, &sharing, FILE_SHARE_WRITE, &disposition, OPEN_EXISTING, &flags, FILE_ATTRIBUTE_NORMAL)) return 1;\r
+ if (path[0]) {\r
+ si->hStdInput = CreateFile(path, FILE_READ_DATA, sharing, &attributes, disposition, flags, 0);\r
+ if (! si->hStdInput) {\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, path, error_string(GetLastError()));\r
+ return 2;\r
+ }\r
+ set_flags = true;\r
+ }\r
+\r
+ /* stdout */\r
+ if (get_createfile_parameters(key, NSSM_REG_STDOUT, path, &sharing, FILE_SHARE_READ | FILE_SHARE_WRITE, &disposition, OPEN_ALWAYS, &flags, FILE_ATTRIBUTE_NORMAL)) return 3;\r
+ if (path[0]) {\r
+ /* Remember path for comparison with stderr. */\r
+ if (_snprintf(stdout_path, sizeof(stdout_path), "%s", path) < 0) {\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "stdout_path", "get_output_handles", 0);\r
+ return 4;\r
+ }\r
+\r
+ si->hStdOutput = append_to_file(path, sharing, &attributes, disposition, flags);\r
+ if (! si->hStdOutput) return 5;\r
+ set_flags = true;\r
+ }\r
+ else ZeroMemory(stdout_path, sizeof(stdout_path));\r
+\r
+ /* stderr */\r
+ if (get_createfile_parameters(key, NSSM_REG_STDERR, path, &sharing, FILE_SHARE_READ | FILE_SHARE_WRITE, &disposition, OPEN_ALWAYS, &flags, FILE_ATTRIBUTE_NORMAL)) return 6;\r
+ if (path[0]) {\r
+ /* Same as stdin? */\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
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DUPLICATEHANDLE_FAILED, NSSM_REG_STDOUT, error_string(GetLastError()));\r
+ return 7;\r
+ }\r
+ }\r
+ else {\r
+ si->hStdError = append_to_file(path, sharing, &attributes, disposition, flags);\r
+ if (! si->hStdError) {\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, path, error_string(GetLastError()));\r
+ return 8;\r
+ }\r
+ SetEndOfFile(si->hStdError);\r
+ }\r
+ set_flags = true;\r
+ }\r
+\r
+ if (! set_flags) 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
+ */\r
+ si->dwFlags |= STARTF_USESTDHANDLES;\r
+\r
+ return 0;\r
+}\r
+\r
+void close_output_handles(STARTUPINFO *si) {\r
+ if (si->hStdInput) CloseHandle(si->hStdInput);\r
+ if (si->hStdOutput) CloseHandle(si->hStdOutput);\r
+ if (si->hStdError) CloseHandle(si->hStdError);\r
+}\r