3 /* Get path, share mode, creation disposition and flags for a stream. */
\r
4 int get_createfile_parameters(HKEY key, TCHAR *prefix, TCHAR *path, unsigned long *sharing, unsigned long default_sharing, unsigned long *disposition, unsigned long default_disposition, unsigned long *flags, unsigned long default_flags) {
\r
5 TCHAR value[NSSM_STDIO_LENGTH];
\r
8 if (_sntprintf_s(value, _countof(value), _TRUNCATE, _T("%s"), prefix) < 0) {
\r
9 log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, prefix, _T("get_createfile_parameters()"), 0);
\r
12 switch (expand_parameter(key, value, path, MAX_PATH, true, false)) {
\r
13 case 0: if (! path[0]) return 0; break; /* OK. */
\r
14 default: return 2; /* Error. */
\r
18 if (_sntprintf_s(value, _countof(value), _TRUNCATE, _T("%s%s"), prefix, NSSM_REG_STDIO_SHARING) < 0) {
\r
19 log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, NSSM_REG_STDIO_SHARING, _T("get_createfile_parameters()"), 0);
\r
22 switch (get_number(key, value, sharing, false)) {
\r
23 case 0: *sharing = default_sharing; break; /* Missing. */
\r
24 case 1: break; /* Found. */
\r
25 case -2: return 4; break; /* Error. */
\r
28 /* CreationDisposition. */
\r
29 if (_sntprintf_s(value, _countof(value), _TRUNCATE, _T("%s%s"), prefix, NSSM_REG_STDIO_DISPOSITION) < 0) {
\r
30 log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, NSSM_REG_STDIO_DISPOSITION, _T("get_createfile_parameters()"), 0);
\r
33 switch (get_number(key, value, disposition, false)) {
\r
34 case 0: *disposition = default_disposition; break; /* Missing. */
\r
35 case 1: break; /* Found. */
\r
36 case -2: return 6; break; /* Error. */
\r
40 if (_sntprintf_s(value, _countof(value), _TRUNCATE, _T("%s%s"), prefix, NSSM_REG_STDIO_FLAGS) < 0) {
\r
41 log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, NSSM_REG_STDIO_FLAGS, _T("get_createfile_parameters()"), 0);
\r
44 switch (get_number(key, value, flags, false)) {
\r
45 case 0: *flags = default_flags; break; /* Missing. */
\r
46 case 1: break; /* Found. */
\r
47 case -2: return 8; break; /* Error. */
\r
53 int set_createfile_parameter(HKEY key, TCHAR *prefix, TCHAR *suffix, unsigned long number) {
\r
54 TCHAR value[NSSM_STDIO_LENGTH];
\r
56 if (_sntprintf_s(value, _countof(value), _TRUNCATE, _T("%s%s"), prefix, suffix) < 0) {
\r
57 log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, suffix, _T("set_createfile_parameter()"), 0);
\r
61 return set_number(key, value, number);
\r
64 HANDLE append_to_file(TCHAR *path, unsigned long sharing, SECURITY_ATTRIBUTES *attributes, unsigned long disposition, unsigned long flags) {
\r
67 /* Try to append to the file first. */
\r
68 ret = CreateFile(path, FILE_APPEND_DATA, sharing, attributes, disposition, flags, 0);
\r
74 unsigned long error = GetLastError();
\r
75 if (error != ERROR_FILE_NOT_FOUND) {
\r
76 log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, path, error_string(error), 0);
\r
80 /* It didn't exist. Create it. */
\r
81 return CreateFile(path, FILE_WRITE_DATA, sharing, attributes, disposition, flags, 0);
\r
84 void rotate_file(TCHAR *service_name, TCHAR *path, unsigned long seconds, unsigned long low, unsigned long high) {
\r
85 unsigned long error;
\r
91 BY_HANDLE_FILE_INFORMATION info;
\r
93 /* Try to open the file to check if it exists and to get attributes. */
\r
94 HANDLE file = CreateFile(path, 0, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
\r
96 /* Get file attributes. */
\r
97 if (! GetFileInformationByHandle(file, &info)) {
\r
98 /* Reuse current time for rotation timestamp. */
\r
99 seconds = low = high = 0;
\r
100 SystemTimeToFileTime(&st, &info.ftLastWriteTime);
\r
106 error = GetLastError();
\r
107 if (error == ERROR_FILE_NOT_FOUND) return;
\r
108 log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_ROTATE_FILE_FAILED, service_name, path, _T("CreateFile()"), path, error_string(error), 0);
\r
109 /* Reuse current time for rotation timestamp. */
\r
110 seconds = low = high = 0;
\r
111 SystemTimeToFileTime(&st, &info.ftLastWriteTime);
\r
114 /* Check file age. */
\r
117 SystemTimeToFileTime(&st, &ft);
\r
120 s.LowPart = ft.dwLowDateTime;
\r
121 s.HighPart = ft.dwHighDateTime;
\r
122 s.QuadPart -= seconds * 10000000LL;
\r
123 ft.dwLowDateTime = s.LowPart;
\r
124 ft.dwHighDateTime = s.HighPart;
\r
125 if (CompareFileTime(&info.ftLastWriteTime, &ft) > 0) return;
\r
128 /* Check file size. */
\r
130 if (info.nFileSizeHigh < high) return;
\r
131 if (info.nFileSizeHigh == high && info.nFileSizeLow < low) return;
\r
134 /* Get new filename. */
\r
135 FileTimeToSystemTime(&info.ftLastWriteTime, &st);
\r
137 TCHAR buffer[MAX_PATH];
\r
138 memmove(buffer, path, sizeof(buffer));
\r
139 TCHAR *ext = PathFindExtension(buffer);
\r
140 TCHAR extension[MAX_PATH];
\r
141 _sntprintf_s(extension, _countof(extension), _TRUNCATE, _T("-%04u%02u%02uT%02u%02u%02u.%03u%s"), st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, ext);
\r
143 TCHAR rotated[MAX_PATH];
\r
144 _sntprintf_s(rotated, _countof(rotated), _TRUNCATE, _T("%s%s"), buffer, extension);
\r
147 if (MoveFile(path, rotated)) return;
\r
148 error = GetLastError();
\r
150 log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_ROTATE_FILE_FAILED, service_name, path, _T("MoveFile()"), rotated, error_string(error), 0);
\r
154 int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) {
\r
155 bool set_flags = false;
\r
157 /* Standard security attributes allowing inheritance. */
\r
158 SECURITY_ATTRIBUTES attributes;
\r
159 ZeroMemory(&attributes, sizeof(attributes));
\r
160 attributes.bInheritHandle = true;
\r
163 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
164 service->stdin_sharing = service->stdin_disposition = service->stdin_flags = 0;
\r
165 ZeroMemory(service->stdin_path, _countof(service->stdin_path) * sizeof(TCHAR));
\r
168 if (si && service->stdin_path[0]) {
\r
169 si->hStdInput = CreateFile(service->stdin_path, FILE_READ_DATA, service->stdin_sharing, &attributes, service->stdin_disposition, service->stdin_flags, 0);
\r
170 if (! si->hStdInput) {
\r
171 log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, service->stdin_path, error_string(GetLastError()), 0);
\r
178 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
179 service->stdout_sharing = service->stdout_disposition = service->stdout_flags = 0;
\r
180 ZeroMemory(service->stdout_path, _countof(service->stdout_path) * sizeof(TCHAR));
\r
183 if (si && service->stdout_path[0]) {
\r
184 if (service->rotate_files) rotate_file(service->name, service->stdout_path, service->rotate_seconds, service->rotate_bytes_low, service->rotate_bytes_high);
\r
185 si->hStdOutput = append_to_file(service->stdout_path, service->stdout_sharing, &attributes, service->stdout_disposition, service->stdout_flags);
\r
186 if (! si->hStdOutput) return 4;
\r
191 if (get_createfile_parameters(key, NSSM_REG_STDERR, service->stderr_path, &service->stdout_sharing, NSSM_STDERR_SHARING, &service->stdout_disposition, NSSM_STDERR_DISPOSITION, &service->stdout_flags, NSSM_STDERR_FLAGS)) {
\r
192 service->stderr_sharing = service->stderr_disposition = service->stderr_flags = 0;
\r
193 ZeroMemory(service->stderr_path, _countof(service->stderr_path) * sizeof(TCHAR));
\r
196 if (service->stderr_path[0]) {
\r
197 /* Same as stdout? */
\r
198 if (str_equiv(service->stderr_path, service->stdout_path)) {
\r
199 service->stderr_sharing = service->stdout_sharing;
\r
200 service->stderr_disposition = service->stdout_disposition;
\r
201 service->stderr_flags = service->stdout_flags;
\r
204 /* Two handles to the same file will create a race. */
\r
205 if (! DuplicateHandle(GetCurrentProcess(), si->hStdOutput, GetCurrentProcess(), &si->hStdError, 0, true, DUPLICATE_SAME_ACCESS)) {
\r
206 log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DUPLICATEHANDLE_FAILED, NSSM_REG_STDOUT, error_string(GetLastError()), 0);
\r
212 if (service->rotate_files) rotate_file(service->name, service->stderr_path, service->rotate_seconds, service->rotate_bytes_low, service->rotate_bytes_high);
\r
213 si->hStdError = append_to_file(service->stderr_path, service->stdout_sharing, &attributes, service->stdout_disposition, service->stdout_flags);
\r
214 if (! si->hStdError) {
\r
215 log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, service->stderr_path, error_string(GetLastError()), 0);
\r
218 SetEndOfFile(si->hStdError);
\r
223 if (! set_flags) return 0;
\r
226 We need to set the startup_info flags to make the new handles
\r
227 inheritable by the new process.
\r
229 if (si) si->dwFlags |= STARTF_USESTDHANDLES;
\r
234 void close_output_handles(STARTUPINFO *si) {
\r
235 if (si->hStdInput) CloseHandle(si->hStdInput);
\r
236 if (si->hStdOutput) CloseHandle(si->hStdOutput);
\r
237 if (si->hStdError) CloseHandle(si->hStdError);
\r