io.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #include "nssm.h"
  2. /* Get path, share mode, creation disposition and flags for a stream. */
  3. 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) {
  4. TCHAR value[NSSM_STDIO_LENGTH];
  5. /* Path. */
  6. if (_sntprintf_s(value, _countof(value), _TRUNCATE, _T("%s"), prefix) < 0) {
  7. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, prefix, _T("get_createfile_parameters()"), 0);
  8. return 1;
  9. }
  10. switch (expand_parameter(key, value, path, MAX_PATH, true, false)) {
  11. case 0: if (! path[0]) return 0; break; /* OK. */
  12. default: return 2; /* Error. */
  13. }
  14. /* ShareMode. */
  15. if (_sntprintf_s(value, _countof(value), _TRUNCATE, _T("%s%s"), prefix, NSSM_REG_STDIO_SHARING) < 0) {
  16. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, NSSM_REG_STDIO_SHARING, _T("get_createfile_parameters()"), 0);
  17. return 3;
  18. }
  19. switch (get_number(key, value, sharing, false)) {
  20. case 0: *sharing = default_sharing; break; /* Missing. */
  21. case 1: break; /* Found. */
  22. case -2: return 4; break; /* Error. */
  23. }
  24. /* CreationDisposition. */
  25. if (_sntprintf_s(value, _countof(value), _TRUNCATE, _T("%s%s"), prefix, NSSM_REG_STDIO_DISPOSITION) < 0) {
  26. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, NSSM_REG_STDIO_DISPOSITION, _T("get_createfile_parameters()"), 0);
  27. return 5;
  28. }
  29. switch (get_number(key, value, disposition, false)) {
  30. case 0: *disposition = default_disposition; break; /* Missing. */
  31. case 1: break; /* Found. */
  32. case -2: return 6; break; /* Error. */
  33. }
  34. /* Flags. */
  35. if (_sntprintf_s(value, _countof(value), _TRUNCATE, _T("%s%s"), prefix, NSSM_REG_STDIO_FLAGS) < 0) {
  36. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, NSSM_REG_STDIO_FLAGS, _T("get_createfile_parameters()"), 0);
  37. return 7;
  38. }
  39. switch (get_number(key, value, flags, false)) {
  40. case 0: *flags = default_flags; break; /* Missing. */
  41. case 1: break; /* Found. */
  42. case -2: return 8; break; /* Error. */
  43. }
  44. return 0;
  45. }
  46. int set_createfile_parameter(HKEY key, TCHAR *prefix, TCHAR *suffix, unsigned long number) {
  47. TCHAR value[NSSM_STDIO_LENGTH];
  48. if (_sntprintf_s(value, _countof(value), _TRUNCATE, _T("%s%s"), prefix, suffix) < 0) {
  49. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, suffix, _T("set_createfile_parameter()"), 0);
  50. return 1;
  51. }
  52. return set_number(key, value, number);
  53. }
  54. int delete_createfile_parameter(HKEY key, TCHAR *prefix, TCHAR *suffix) {
  55. TCHAR value[NSSM_STDIO_LENGTH];
  56. if (_sntprintf_s(value, _countof(value), _TRUNCATE, _T("%s%s"), prefix, suffix) < 0) {
  57. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, suffix, _T("delete_createfile_parameter()"), 0);
  58. return 1;
  59. }
  60. if (RegDeleteValue(key, value)) return 0;
  61. return 1;
  62. }
  63. HANDLE append_to_file(TCHAR *path, unsigned long sharing, SECURITY_ATTRIBUTES *attributes, unsigned long disposition, unsigned long flags) {
  64. HANDLE ret;
  65. /* Try to append to the file first. */
  66. ret = CreateFile(path, FILE_APPEND_DATA, sharing, attributes, disposition, flags, 0);
  67. if (ret) {
  68. SetEndOfFile(ret);
  69. return ret;
  70. }
  71. unsigned long error = GetLastError();
  72. if (error != ERROR_FILE_NOT_FOUND) {
  73. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, path, error_string(error), 0);
  74. return (HANDLE) 0;
  75. }
  76. /* It didn't exist. Create it. */
  77. return CreateFile(path, FILE_WRITE_DATA, sharing, attributes, disposition, flags, 0);
  78. }
  79. void rotate_file(TCHAR *service_name, TCHAR *path, unsigned long seconds, unsigned long low, unsigned long high) {
  80. unsigned long error;
  81. /* Now. */
  82. SYSTEMTIME st;
  83. GetSystemTime(&st);
  84. BY_HANDLE_FILE_INFORMATION info;
  85. /* Try to open the file to check if it exists and to get attributes. */
  86. HANDLE file = CreateFile(path, 0, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  87. if (file) {
  88. /* Get file attributes. */
  89. if (! GetFileInformationByHandle(file, &info)) {
  90. /* Reuse current time for rotation timestamp. */
  91. seconds = low = high = 0;
  92. SystemTimeToFileTime(&st, &info.ftLastWriteTime);
  93. }
  94. CloseHandle(file);
  95. }
  96. else {
  97. error = GetLastError();
  98. if (error == ERROR_FILE_NOT_FOUND) return;
  99. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_ROTATE_FILE_FAILED, service_name, path, _T("CreateFile()"), path, error_string(error), 0);
  100. /* Reuse current time for rotation timestamp. */
  101. seconds = low = high = 0;
  102. SystemTimeToFileTime(&st, &info.ftLastWriteTime);
  103. }
  104. /* Check file age. */
  105. if (seconds) {
  106. FILETIME ft;
  107. SystemTimeToFileTime(&st, &ft);
  108. ULARGE_INTEGER s;
  109. s.LowPart = ft.dwLowDateTime;
  110. s.HighPart = ft.dwHighDateTime;
  111. s.QuadPart -= seconds * 10000000LL;
  112. ft.dwLowDateTime = s.LowPart;
  113. ft.dwHighDateTime = s.HighPart;
  114. if (CompareFileTime(&info.ftLastWriteTime, &ft) > 0) return;
  115. }
  116. /* Check file size. */
  117. if (low || high) {
  118. if (info.nFileSizeHigh < high) return;
  119. if (info.nFileSizeHigh == high && info.nFileSizeLow < low) return;
  120. }
  121. /* Get new filename. */
  122. FileTimeToSystemTime(&info.ftLastWriteTime, &st);
  123. TCHAR buffer[MAX_PATH];
  124. memmove(buffer, path, sizeof(buffer));
  125. TCHAR *ext = PathFindExtension(buffer);
  126. TCHAR extension[MAX_PATH];
  127. _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);
  128. *ext = _T('\0');
  129. TCHAR rotated[MAX_PATH];
  130. _sntprintf_s(rotated, _countof(rotated), _TRUNCATE, _T("%s%s"), buffer, extension);
  131. /* Rotate. */
  132. if (MoveFile(path, rotated)) return;
  133. error = GetLastError();
  134. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_ROTATE_FILE_FAILED, service_name, path, _T("MoveFile()"), rotated, error_string(error), 0);
  135. return;
  136. }
  137. int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) {
  138. bool set_flags = false;
  139. /* Standard security attributes allowing inheritance. */
  140. SECURITY_ATTRIBUTES attributes;
  141. ZeroMemory(&attributes, sizeof(attributes));
  142. attributes.bInheritHandle = true;
  143. /* stdin */
  144. 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)) {
  145. service->stdin_sharing = service->stdin_disposition = service->stdin_flags = 0;
  146. ZeroMemory(service->stdin_path, _countof(service->stdin_path) * sizeof(TCHAR));
  147. return 1;
  148. }
  149. if (si && service->stdin_path[0]) {
  150. si->hStdInput = CreateFile(service->stdin_path, FILE_READ_DATA, service->stdin_sharing, &attributes, service->stdin_disposition, service->stdin_flags, 0);
  151. if (! si->hStdInput) {
  152. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, service->stdin_path, error_string(GetLastError()), 0);
  153. return 2;
  154. }
  155. set_flags = true;
  156. }
  157. /* stdout */
  158. 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)) {
  159. service->stdout_sharing = service->stdout_disposition = service->stdout_flags = 0;
  160. ZeroMemory(service->stdout_path, _countof(service->stdout_path) * sizeof(TCHAR));
  161. return 3;
  162. }
  163. if (si && service->stdout_path[0]) {
  164. if (service->rotate_files) rotate_file(service->name, service->stdout_path, service->rotate_seconds, service->rotate_bytes_low, service->rotate_bytes_high);
  165. si->hStdOutput = append_to_file(service->stdout_path, service->stdout_sharing, &attributes, service->stdout_disposition, service->stdout_flags);
  166. if (! si->hStdOutput) return 4;
  167. set_flags = true;
  168. }
  169. /* stderr */
  170. 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)) {
  171. service->stderr_sharing = service->stderr_disposition = service->stderr_flags = 0;
  172. ZeroMemory(service->stderr_path, _countof(service->stderr_path) * sizeof(TCHAR));
  173. return 5;
  174. }
  175. if (service->stderr_path[0]) {
  176. /* Same as stdout? */
  177. if (str_equiv(service->stderr_path, service->stdout_path)) {
  178. service->stderr_sharing = service->stdout_sharing;
  179. service->stderr_disposition = service->stdout_disposition;
  180. service->stderr_flags = service->stdout_flags;
  181. if (si) {
  182. /* Two handles to the same file will create a race. */
  183. if (! DuplicateHandle(GetCurrentProcess(), si->hStdOutput, GetCurrentProcess(), &si->hStdError, 0, true, DUPLICATE_SAME_ACCESS)) {
  184. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DUPLICATEHANDLE_FAILED, NSSM_REG_STDOUT, error_string(GetLastError()), 0);
  185. return 6;
  186. }
  187. }
  188. }
  189. else if (si) {
  190. if (service->rotate_files) rotate_file(service->name, service->stderr_path, service->rotate_seconds, service->rotate_bytes_low, service->rotate_bytes_high);
  191. si->hStdError = append_to_file(service->stderr_path, service->stdout_sharing, &attributes, service->stdout_disposition, service->stdout_flags);
  192. if (! si->hStdError) {
  193. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, service->stderr_path, error_string(GetLastError()), 0);
  194. return 7;
  195. }
  196. SetEndOfFile(si->hStdError);
  197. }
  198. set_flags = true;
  199. }
  200. if (! set_flags) return 0;
  201. /*
  202. We need to set the startup_info flags to make the new handles
  203. inheritable by the new process.
  204. */
  205. if (si) si->dwFlags |= STARTF_USESTDHANDLES;
  206. return 0;
  207. }
  208. void close_output_handles(STARTUPINFO *si) {
  209. if (si->hStdInput) CloseHandle(si->hStdInput);
  210. if (si->hStdOutput) CloseHandle(si->hStdOutput);
  211. if (si->hStdError) CloseHandle(si->hStdError);
  212. }