registry.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. #include "nssm.h"
  2. extern const TCHAR *exit_action_strings[];
  3. int create_messages() {
  4. HKEY key;
  5. TCHAR registry[KEY_LENGTH];
  6. if (_sntprintf_s(registry, _countof(registry), _TRUNCATE, _T("SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s"), NSSM) < 0) {
  7. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("eventlog registry"), _T("create_messages()"), 0);
  8. return 1;
  9. }
  10. if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, registry, 0, 0, REG_OPTION_NON_VOLATILE, KEY_WRITE, 0, &key, 0) != ERROR_SUCCESS) {
  11. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENKEY_FAILED, registry, error_string(GetLastError()), 0);
  12. return 2;
  13. }
  14. /* Get path of this program */
  15. TCHAR path[MAX_PATH];
  16. GetModuleFileName(0, path, _countof(path));
  17. /* Try to register the module but don't worry so much on failure */
  18. RegSetValueEx(key, _T("EventMessageFile"), 0, REG_SZ, (const unsigned char *) path, (unsigned long) (_tcslen(path) + 1) * sizeof(TCHAR));
  19. unsigned long types = EVENTLOG_INFORMATION_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_ERROR_TYPE;
  20. RegSetValueEx(key, _T("TypesSupported"), 0, REG_DWORD, (const unsigned char *) &types, sizeof(types));
  21. return 0;
  22. }
  23. int create_parameters(nssm_service_t *service) {
  24. /* Get registry */
  25. TCHAR registry[KEY_LENGTH];
  26. if (_sntprintf_s(registry, _countof(registry), _TRUNCATE, NSSM_REGISTRY, service->name) < 0) {
  27. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("NSSM_REGISTRY"), _T("create_parameters()"), 0);
  28. return 1;
  29. }
  30. /* Try to open the registry */
  31. HKEY key;
  32. if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, registry, 0, 0, REG_OPTION_NON_VOLATILE, KEY_WRITE, 0, &key, 0) != ERROR_SUCCESS) {
  33. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENKEY_FAILED, registry, error_string(GetLastError()), 0);
  34. return 2;
  35. }
  36. /* Try to create the parameters */
  37. if (set_expand_string(key, NSSM_REG_EXE, service->exe)) {
  38. RegDeleteKey(HKEY_LOCAL_MACHINE, NSSM_REGISTRY);
  39. RegCloseKey(key);
  40. return 3;
  41. }
  42. if (set_expand_string(key, NSSM_REG_FLAGS, service->flags)) {
  43. RegDeleteKey(HKEY_LOCAL_MACHINE, NSSM_REGISTRY);
  44. RegCloseKey(key);
  45. return 4;
  46. }
  47. if (set_expand_string(key, NSSM_REG_DIR, service->dir)) {
  48. RegDeleteKey(HKEY_LOCAL_MACHINE, NSSM_REGISTRY);
  49. RegCloseKey(key);
  50. return 5;
  51. }
  52. /* Other non-default parameters. May fail. */
  53. unsigned long stop_method_skip = ~service->stop_method;
  54. if (stop_method_skip) set_number(key, NSSM_REG_STOP_METHOD_SKIP, stop_method_skip);
  55. if (service->default_exit_action < NSSM_NUM_EXIT_ACTIONS) create_exit_action(service->name, exit_action_strings[service->default_exit_action]);
  56. if (service->throttle_delay != NSSM_RESET_THROTTLE_RESTART) set_number(key, NSSM_REG_THROTTLE, service->throttle_delay);
  57. if (service->kill_console_delay != NSSM_KILL_CONSOLE_GRACE_PERIOD) set_number(key, NSSM_REG_KILL_CONSOLE_GRACE_PERIOD, service->kill_console_delay);
  58. if (service->kill_window_delay != NSSM_KILL_WINDOW_GRACE_PERIOD) set_number(key, NSSM_REG_KILL_WINDOW_GRACE_PERIOD, service->kill_window_delay);
  59. if (service->kill_threads_delay != NSSM_KILL_THREADS_GRACE_PERIOD) set_number(key, NSSM_REG_KILL_THREADS_GRACE_PERIOD, service->kill_threads_delay);
  60. if (service->stdin_path[0]) {
  61. set_expand_string(key, NSSM_REG_STDIN, service->stdin_path);
  62. if (service->stdin_sharing != NSSM_STDIN_SHARING) set_createfile_parameter(key, NSSM_REG_STDIN, NSSM_REG_STDIO_SHARING, service->stdin_sharing);
  63. if (service->stdin_disposition != NSSM_STDIN_DISPOSITION) set_createfile_parameter(key, NSSM_REG_STDIN, NSSM_REG_STDIO_DISPOSITION, service->stdin_disposition);
  64. if (service->stdin_flags != NSSM_STDIN_FLAGS) set_createfile_parameter(key, NSSM_REG_STDIN, NSSM_REG_STDIO_FLAGS, service->stdin_flags);
  65. }
  66. if (service->stdout_path[0]) {
  67. set_expand_string(key, NSSM_REG_STDOUT, service->stdout_path);
  68. if (service->stdout_sharing != NSSM_STDOUT_SHARING) set_createfile_parameter(key, NSSM_REG_STDOUT, NSSM_REG_STDIO_SHARING, service->stdout_sharing);
  69. if (service->stdout_disposition != NSSM_STDOUT_DISPOSITION) set_createfile_parameter(key, NSSM_REG_STDOUT, NSSM_REG_STDIO_DISPOSITION, service->stdout_disposition);
  70. if (service->stdout_flags != NSSM_STDOUT_FLAGS) set_createfile_parameter(key, NSSM_REG_STDOUT, NSSM_REG_STDIO_FLAGS, service->stdout_flags);
  71. }
  72. if (service->stderr_path[0]) {
  73. set_expand_string(key, NSSM_REG_STDERR, service->stderr_path);
  74. if (service->stderr_sharing != NSSM_STDERR_SHARING) set_createfile_parameter(key, NSSM_REG_STDERR, NSSM_REG_STDIO_SHARING, service->stderr_sharing);
  75. if (service->stderr_disposition != NSSM_STDERR_DISPOSITION) set_createfile_parameter(key, NSSM_REG_STDERR, NSSM_REG_STDIO_DISPOSITION, service->stderr_disposition);
  76. if (service->stderr_flags != NSSM_STDERR_FLAGS) set_createfile_parameter(key, NSSM_REG_STDERR, NSSM_REG_STDIO_FLAGS, service->stderr_flags);
  77. }
  78. if (service->rotate_files) set_number(key, NSSM_REG_ROTATE, 1);
  79. if (service->rotate_seconds) set_number(key, NSSM_REG_ROTATE_SECONDS, service->rotate_seconds);
  80. if (service->rotate_bytes_low) set_number(key, NSSM_REG_ROTATE_BYTES_LOW, service->rotate_bytes_low);
  81. if (service->rotate_bytes_high) set_number(key, NSSM_REG_ROTATE_BYTES_HIGH, service->rotate_bytes_high);
  82. /* Environment */
  83. if (service->env) {
  84. if (RegSetValueEx(key, NSSM_REG_ENV, 0, REG_MULTI_SZ, (const unsigned char *) service->env, (unsigned long) service->envlen * sizeof(TCHAR)) != ERROR_SUCCESS) {
  85. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, NSSM_REG_ENV, error_string(GetLastError()), 0);
  86. }
  87. }
  88. if (service->env_extra) {
  89. if (RegSetValueEx(key, NSSM_REG_ENV_EXTRA, 0, REG_MULTI_SZ, (const unsigned char *) service->env_extra, (unsigned long) service->env_extralen * sizeof(TCHAR)) != ERROR_SUCCESS) {
  90. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, NSSM_REG_ENV_EXTRA, error_string(GetLastError()), 0);
  91. }
  92. }
  93. /* Close registry. */
  94. RegCloseKey(key);
  95. return 0;
  96. }
  97. int create_exit_action(TCHAR *service_name, const TCHAR *action_string) {
  98. /* Get registry */
  99. TCHAR registry[KEY_LENGTH];
  100. if (_sntprintf_s(registry, _countof(registry), _TRUNCATE, NSSM_REGISTRY _T("\\%s"), service_name, NSSM_REG_EXIT) < 0) {
  101. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("NSSM_REG_EXIT"), _T("create_exit_action()"), 0);
  102. return 1;
  103. }
  104. /* Try to open the registry */
  105. HKEY key;
  106. unsigned long disposition;
  107. if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, registry, 0, 0, REG_OPTION_NON_VOLATILE, KEY_WRITE, 0, &key, &disposition) != ERROR_SUCCESS) {
  108. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENKEY_FAILED, registry, error_string(GetLastError()), 0);
  109. return 2;
  110. }
  111. /* Do nothing if the key already existed */
  112. if (disposition == REG_OPENED_EXISTING_KEY) {
  113. RegCloseKey(key);
  114. return 0;
  115. }
  116. /* Create the default value */
  117. if (RegSetValueEx(key, 0, 0, REG_SZ, (const unsigned char *) action_string, (unsigned long) (_tcslen(action_string) + 1) * sizeof(TCHAR)) != ERROR_SUCCESS) {
  118. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, NSSM_REG_EXIT, error_string(GetLastError()), 0);
  119. RegCloseKey(key);
  120. return 3;
  121. }
  122. /* Close registry */
  123. RegCloseKey(key);
  124. return 0;
  125. }
  126. int set_environment(TCHAR *service_name, HKEY key, TCHAR *value, TCHAR **env, unsigned long *envlen) {
  127. unsigned long type = REG_MULTI_SZ;
  128. /* Dummy test to find buffer size */
  129. unsigned long ret = RegQueryValueEx(key, value, 0, &type, NULL, envlen);
  130. if (ret != ERROR_SUCCESS) {
  131. *envlen = 0;
  132. /* The service probably doesn't have any environment configured */
  133. if (ret == ERROR_FILE_NOT_FOUND) return 0;
  134. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, value, error_string(GetLastError()), 0);
  135. return 1;
  136. }
  137. if (type != REG_MULTI_SZ) {
  138. log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_INVALID_ENVIRONMENT_STRING_TYPE, value, service_name, 0);
  139. return 2;
  140. }
  141. /* Probably not possible */
  142. if (! *envlen) return 0;
  143. /* Previously initialised? */
  144. if (*env) HeapFree(GetProcessHeap(), 0, *env);
  145. *env = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, *envlen);
  146. if (! *env) {
  147. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, value, _T("set_environment()"), 0);
  148. return 3;
  149. }
  150. /* Actually get the strings */
  151. ret = RegQueryValueEx(key, value, 0, &type, (unsigned char *) *env, envlen);
  152. if (ret != ERROR_SUCCESS) {
  153. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, value, error_string(GetLastError()), 0);
  154. HeapFree(GetProcessHeap(), 0, *env);
  155. *env = 0;
  156. *envlen = 0;
  157. return 4;
  158. }
  159. return 0;
  160. }
  161. int expand_parameter(HKEY key, TCHAR *value, TCHAR *data, unsigned long datalen, bool sanitise, bool must_exist) {
  162. TCHAR *buffer = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, datalen);
  163. if (! buffer) {
  164. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, value, _T("expand_parameter()"), 0);
  165. return 1;
  166. }
  167. ZeroMemory(data, datalen);
  168. unsigned long type = REG_EXPAND_SZ;
  169. unsigned long buflen = datalen;
  170. unsigned long ret = RegQueryValueEx(key, value, 0, &type, (unsigned char *) buffer, &buflen);
  171. if (ret != ERROR_SUCCESS) {
  172. unsigned long error = GetLastError();
  173. HeapFree(GetProcessHeap(), 0, buffer);
  174. if (ret == ERROR_FILE_NOT_FOUND) {
  175. if (! must_exist) return 0;
  176. }
  177. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, value, error_string(error), 0);
  178. return 2;
  179. }
  180. /* Paths aren't allowed to contain quotes. */
  181. if (sanitise) PathUnquoteSpaces(buffer);
  182. /* Technically we shouldn't expand environment strings from REG_SZ values */
  183. if (type != REG_EXPAND_SZ) {
  184. memmove(data, buffer, buflen);
  185. HeapFree(GetProcessHeap(), 0, buffer);
  186. return 0;
  187. }
  188. ret = ExpandEnvironmentStrings((TCHAR *) buffer, data, datalen);
  189. if (! ret || ret > datalen) {
  190. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_EXPANDENVIRONMENTSTRINGS_FAILED, buffer, error_string(GetLastError()), 0);
  191. HeapFree(GetProcessHeap(), 0, buffer);
  192. return 3;
  193. }
  194. HeapFree(GetProcessHeap(), 0, buffer);
  195. return 0;
  196. }
  197. int expand_parameter(HKEY key, TCHAR *value, TCHAR *data, unsigned long datalen, bool sanitise) {
  198. return expand_parameter(key, value, data, datalen, sanitise, true);
  199. }
  200. /*
  201. Sets a string in the registry.
  202. Returns: 0 if it was set.
  203. 1 on error.
  204. */
  205. int set_expand_string(HKEY key, TCHAR *value, TCHAR *string) {
  206. if (RegSetValueEx(key, value, 0, REG_EXPAND_SZ, (const unsigned char *) string, (unsigned long) (_tcslen(string) + 1) * sizeof(TCHAR)) == ERROR_SUCCESS) return 0;
  207. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, value, error_string(GetLastError()), 0);
  208. return 1;
  209. }
  210. /*
  211. Set an unsigned long in the registry.
  212. Returns: 0 if it was set.
  213. 1 on error.
  214. */
  215. int set_number(HKEY key, TCHAR *value, unsigned long number) {
  216. if (RegSetValueEx(key, value, 0, REG_DWORD, (const unsigned char *) &number, sizeof(number)) == ERROR_SUCCESS) return 0;
  217. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, value, error_string(GetLastError()), 0);
  218. return 1;
  219. }
  220. /*
  221. Query an unsigned long from the registry.
  222. Returns: 1 if a number was retrieved.
  223. 0 if none was found and must_exist is false.
  224. -1 if none was found and must_exist is true.
  225. -2 otherwise.
  226. */
  227. int get_number(HKEY key, TCHAR *value, unsigned long *number, bool must_exist) {
  228. unsigned long type = REG_DWORD;
  229. unsigned long number_len = sizeof(unsigned long);
  230. int ret = RegQueryValueEx(key, value, 0, &type, (unsigned char *) number, &number_len);
  231. if (ret == ERROR_SUCCESS) return 1;
  232. if (ret == ERROR_FILE_NOT_FOUND) {
  233. if (! must_exist) return 0;
  234. }
  235. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, value, error_string(GetLastError()), 0);
  236. if (ret == ERROR_FILE_NOT_FOUND) return -1;
  237. return -2;
  238. }
  239. int get_number(HKEY key, TCHAR *value, unsigned long *number) {
  240. return get_number(key, value, number, true);
  241. }
  242. void override_milliseconds(TCHAR *service_name, HKEY key, TCHAR *value, unsigned long *buffer, unsigned long default_value, unsigned long event) {
  243. unsigned long type = REG_DWORD;
  244. unsigned long buflen = sizeof(unsigned long);
  245. bool ok = false;
  246. unsigned long ret = RegQueryValueEx(key, value, 0, &type, (unsigned char *) buffer, &buflen);
  247. if (ret != ERROR_SUCCESS) {
  248. if (ret != ERROR_FILE_NOT_FOUND) {
  249. if (type != REG_DWORD) {
  250. TCHAR milliseconds[16];
  251. _sntprintf_s(milliseconds, _countof(milliseconds), _TRUNCATE, _T("%lu"), default_value);
  252. log_event(EVENTLOG_WARNING_TYPE, event, service_name, value, milliseconds, 0);
  253. }
  254. else log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, value, error_string(GetLastError()), 0);
  255. }
  256. }
  257. else ok = true;
  258. if (! ok) *buffer = default_value;
  259. }
  260. int get_parameters(nssm_service_t *service, STARTUPINFO *si) {
  261. unsigned long ret;
  262. /* Get registry */
  263. TCHAR registry[KEY_LENGTH];
  264. if (_sntprintf_s(registry, _countof(registry), _TRUNCATE, NSSM_REGISTRY, service->name) < 0) {
  265. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("NSSM_REGISTRY"), _T("get_parameters()"), 0);
  266. return 1;
  267. }
  268. /* Try to open the registry */
  269. HKEY key;
  270. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, registry, 0, KEY_READ, &key) != ERROR_SUCCESS) {
  271. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENKEY_FAILED, registry, error_string(GetLastError()), 0);
  272. return 2;
  273. }
  274. /* Try to get executable file - MUST succeed */
  275. if (expand_parameter(key, NSSM_REG_EXE, service->exe, sizeof(service->exe), false)) {
  276. RegCloseKey(key);
  277. return 3;
  278. }
  279. /* Try to get flags - may fail and we don't care */
  280. if (expand_parameter(key, NSSM_REG_FLAGS, service->flags, sizeof(service->flags), false)) {
  281. log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_NO_FLAGS, NSSM_REG_FLAGS, service->name, service->exe, 0);
  282. ZeroMemory(service->flags, sizeof(service->flags));
  283. }
  284. /* Try to get startup directory - may fail and we fall back to a default */
  285. if (expand_parameter(key, NSSM_REG_DIR, service->dir, sizeof(service->dir), true) || ! service->dir[0]) {
  286. _sntprintf_s(service->dir, _countof(service->dir), _TRUNCATE, _T("%s"), service->exe);
  287. strip_basename(service->dir);
  288. if (service->dir[0] == _T('\0')) {
  289. /* Help! */
  290. ret = GetWindowsDirectory(service->dir, sizeof(service->dir));
  291. if (! ret || ret > sizeof(service->dir)) {
  292. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_NO_DIR_AND_NO_FALLBACK, NSSM_REG_DIR, service->name, 0);
  293. RegCloseKey(key);
  294. return 4;
  295. }
  296. }
  297. log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_NO_DIR, NSSM_REG_DIR, service->name, service->dir, 0);
  298. }
  299. /* Try to get environment variables - may fail */
  300. set_environment(service->name, key, NSSM_REG_ENV, &service->env, &service->envlen);
  301. /* Environment variables to add to existing rather than replace - may fail. */
  302. set_environment(service->name, key, NSSM_REG_ENV_EXTRA, &service->env_extra, &service->env_extralen);
  303. if (service->env_extra) {
  304. /* Append these to any other environment variables set. */
  305. if (service->env) {
  306. /* Append extra variables to configured variables. */
  307. unsigned long envlen = service->envlen + service->env_extralen - 1;
  308. TCHAR *env = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, envlen);
  309. if (env) {
  310. memmove(env, service->env, service->envlen - sizeof(TCHAR));
  311. /* envlen is in bytes. */
  312. memmove(env + (service->envlen / sizeof(TCHAR)) - 1, service->env_extra, service->env_extralen);
  313. HeapFree(GetProcessHeap(), 0, service->env);
  314. service->env = env;
  315. service->envlen = envlen;
  316. }
  317. else log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("environment"), _T("get_parameters()"), 0);
  318. }
  319. else {
  320. /* Append extra variables to our environment. */
  321. TCHAR *env, *s;
  322. size_t envlen, len;
  323. env = service->env_extra;
  324. len = 0;
  325. while (*env) {
  326. envlen = _tcslen(env) + 1;
  327. for (s = env; *s && *s != _T('='); s++);
  328. if (*s == _T('=')) *s++ = _T('\0');
  329. if (! SetEnvironmentVariable(env, s)) log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_SETENVIRONMENTVARIABLE_FAILED, env, s, error_string(GetLastError()), 0);
  330. env += envlen;
  331. }
  332. }
  333. }
  334. /* Try to get file rotation settings - may fail. */
  335. unsigned long rotate_files;
  336. if (get_number(key, NSSM_REG_ROTATE, &rotate_files, false) == 1) {
  337. if (rotate_files) service->rotate_files = true;
  338. else service->rotate_files = false;
  339. }
  340. else service->rotate_files = false;
  341. if (get_number(key, NSSM_REG_ROTATE_SECONDS, &service->rotate_seconds, false) != 1) service->rotate_seconds = 0;
  342. if (get_number(key, NSSM_REG_ROTATE_BYTES_LOW, &service->rotate_bytes_low, false) != 1) service->rotate_bytes_low = 0;
  343. if (get_number(key, NSSM_REG_ROTATE_BYTES_HIGH, &service->rotate_bytes_high, false) != 1) service->rotate_bytes_high = 0;
  344. /* Change to startup directory in case stdout/stderr are relative paths. */
  345. TCHAR cwd[MAX_PATH];
  346. GetCurrentDirectory(_countof(cwd), cwd);
  347. SetCurrentDirectory(service->dir);
  348. /* Try to get stdout and stderr */
  349. if (get_output_handles(service, key, si)) {
  350. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_GET_OUTPUT_HANDLES_FAILED, service->name, 0);
  351. RegCloseKey(key);
  352. SetCurrentDirectory(cwd);
  353. return 5;
  354. }
  355. /* Change back in case the startup directory needs to be deleted. */
  356. SetCurrentDirectory(cwd);
  357. /* Try to get throttle restart delay */
  358. override_milliseconds(service->name, key, NSSM_REG_THROTTLE, &service->throttle_delay, NSSM_RESET_THROTTLE_RESTART, NSSM_EVENT_BOGUS_THROTTLE);
  359. /* Try to get service stop flags. */
  360. unsigned long type = REG_DWORD;
  361. unsigned long stop_method_skip;
  362. unsigned long buflen = sizeof(stop_method_skip);
  363. bool stop_ok = false;
  364. ret = RegQueryValueEx(key, NSSM_REG_STOP_METHOD_SKIP, 0, &type, (unsigned char *) &stop_method_skip, &buflen);
  365. if (ret != ERROR_SUCCESS) {
  366. if (ret != ERROR_FILE_NOT_FOUND) {
  367. if (type != REG_DWORD) {
  368. log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_BOGUS_STOP_METHOD_SKIP, service->name, NSSM_REG_STOP_METHOD_SKIP, NSSM, 0);
  369. }
  370. else log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, NSSM_REG_STOP_METHOD_SKIP, error_string(GetLastError()), 0);
  371. }
  372. }
  373. else stop_ok = true;
  374. /* Try all methods except those requested to be skipped. */
  375. service->stop_method = ~0;
  376. if (stop_ok) service->stop_method &= ~stop_method_skip;
  377. /* Try to get kill delays - may fail. */
  378. override_milliseconds(service->name, key, NSSM_REG_KILL_CONSOLE_GRACE_PERIOD, &service->kill_console_delay, NSSM_KILL_CONSOLE_GRACE_PERIOD, NSSM_EVENT_BOGUS_KILL_CONSOLE_GRACE_PERIOD);
  379. override_milliseconds(service->name, key, NSSM_REG_KILL_WINDOW_GRACE_PERIOD, &service->kill_window_delay, NSSM_KILL_WINDOW_GRACE_PERIOD, NSSM_EVENT_BOGUS_KILL_WINDOW_GRACE_PERIOD);
  380. override_milliseconds(service->name, key, NSSM_REG_KILL_THREADS_GRACE_PERIOD, &service->kill_threads_delay, NSSM_KILL_THREADS_GRACE_PERIOD, NSSM_EVENT_BOGUS_KILL_THREADS_GRACE_PERIOD);
  381. /* Close registry */
  382. RegCloseKey(key);
  383. return 0;
  384. }
  385. int get_exit_action(TCHAR *service_name, unsigned long *ret, TCHAR *action, bool *default_action) {
  386. /* Are we returning the default action or a status-specific one? */
  387. *default_action = ! ret;
  388. /* Get registry */
  389. TCHAR registry[KEY_LENGTH];
  390. if (_sntprintf_s(registry, _countof(registry), _TRUNCATE, NSSM_REGISTRY _T("\\%s"), service_name, NSSM_REG_EXIT) < 0) {
  391. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("NSSM_REG_EXIT"), _T("get_exit_action()"), 0);
  392. return 1;
  393. }
  394. /* Try to open the registry */
  395. HKEY key;
  396. long error = RegOpenKeyEx(HKEY_LOCAL_MACHINE, registry, 0, KEY_READ, &key);
  397. if (error != ERROR_SUCCESS && error != ERROR_FILE_NOT_FOUND) {
  398. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENKEY_FAILED, registry, error_string(GetLastError()), 0);
  399. return 2;
  400. }
  401. unsigned long type = REG_SZ;
  402. unsigned long action_len = ACTION_LEN;
  403. TCHAR code[16];
  404. if (! ret) code[0] = _T('\0');
  405. else if (_sntprintf_s(code, _countof(code), _TRUNCATE, _T("%lu"), *ret) < 0) {
  406. RegCloseKey(key);
  407. return get_exit_action(service_name, 0, action, default_action);
  408. }
  409. if (RegQueryValueEx(key, code, 0, &type, (unsigned char *) action, &action_len) != ERROR_SUCCESS) {
  410. RegCloseKey(key);
  411. /* Try again with * as the key if an exit code was defined */
  412. if (ret) return get_exit_action(service_name, 0, action, default_action);
  413. return 0;
  414. }
  415. /* Close registry */
  416. RegCloseKey(key);
  417. return 0;
  418. }