registry.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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, bool editing) {
  24. /* Try to open the registry */
  25. HKEY key = open_registry(service->name, KEY_WRITE);
  26. if (! key) return 1;
  27. /* Try to create the parameters */
  28. if (set_expand_string(key, NSSM_REG_EXE, service->exe)) {
  29. RegDeleteKey(HKEY_LOCAL_MACHINE, NSSM_REGISTRY);
  30. RegCloseKey(key);
  31. return 2;
  32. }
  33. if (set_expand_string(key, NSSM_REG_FLAGS, service->flags)) {
  34. RegDeleteKey(HKEY_LOCAL_MACHINE, NSSM_REGISTRY);
  35. RegCloseKey(key);
  36. return 3;
  37. }
  38. if (set_expand_string(key, NSSM_REG_DIR, service->dir)) {
  39. RegDeleteKey(HKEY_LOCAL_MACHINE, NSSM_REGISTRY);
  40. RegCloseKey(key);
  41. return 4;
  42. }
  43. /* Other non-default parameters. May fail. */
  44. if (service->priority != NORMAL_PRIORITY_CLASS) set_number(key, NSSM_REG_PRIORITY, service->priority);
  45. else if (editing) RegDeleteValue(key, NSSM_REG_PRIORITY);
  46. if (service->affinity) {
  47. TCHAR *string;
  48. if (! affinity_mask_to_string(service->affinity, &string)) {
  49. if (RegSetValueEx(key, NSSM_REG_AFFINITY, 0, REG_SZ, (const unsigned char *) string, (unsigned long) (_tcslen(string) + 1) * sizeof(TCHAR)) != ERROR_SUCCESS) {
  50. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, NSSM_REG_AFFINITY, error_string(GetLastError()), 0);
  51. HeapFree(GetProcessHeap(), 0, string);
  52. return 5;
  53. }
  54. }
  55. if (string) HeapFree(GetProcessHeap(), 0, string);
  56. }
  57. else if (editing) RegDeleteValue(key, NSSM_REG_AFFINITY);
  58. unsigned long stop_method_skip = ~service->stop_method;
  59. if (stop_method_skip) set_number(key, NSSM_REG_STOP_METHOD_SKIP, stop_method_skip);
  60. else if (editing) RegDeleteValue(key, NSSM_REG_STOP_METHOD_SKIP);
  61. if (service->default_exit_action < NSSM_NUM_EXIT_ACTIONS) create_exit_action(service->name, exit_action_strings[service->default_exit_action], editing);
  62. if (service->throttle_delay != NSSM_RESET_THROTTLE_RESTART) set_number(key, NSSM_REG_THROTTLE, service->throttle_delay);
  63. else if (editing) RegDeleteValue(key, NSSM_REG_THROTTLE);
  64. if (service->kill_console_delay != NSSM_KILL_CONSOLE_GRACE_PERIOD) set_number(key, NSSM_REG_KILL_CONSOLE_GRACE_PERIOD, service->kill_console_delay);
  65. else if (editing) RegDeleteValue(key, NSSM_REG_KILL_CONSOLE_GRACE_PERIOD);
  66. if (service->kill_window_delay != NSSM_KILL_WINDOW_GRACE_PERIOD) set_number(key, NSSM_REG_KILL_WINDOW_GRACE_PERIOD, service->kill_window_delay);
  67. else if (editing) RegDeleteValue(key, NSSM_REG_KILL_WINDOW_GRACE_PERIOD);
  68. if (service->kill_threads_delay != NSSM_KILL_THREADS_GRACE_PERIOD) set_number(key, NSSM_REG_KILL_THREADS_GRACE_PERIOD, service->kill_threads_delay);
  69. else if (editing) RegDeleteValue(key, NSSM_REG_KILL_THREADS_GRACE_PERIOD);
  70. if (service->stdin_path[0] || editing) {
  71. if (service->stdin_path[0]) set_expand_string(key, NSSM_REG_STDIN, service->stdin_path);
  72. else if (editing) RegDeleteValue(key, NSSM_REG_STDIN);
  73. if (service->stdin_sharing != NSSM_STDIN_SHARING) set_createfile_parameter(key, NSSM_REG_STDIN, NSSM_REG_STDIO_SHARING, service->stdin_sharing);
  74. else if (editing) delete_createfile_parameter(key, NSSM_REG_STDIN, NSSM_REG_STDIO_SHARING);
  75. if (service->stdin_disposition != NSSM_STDIN_DISPOSITION) set_createfile_parameter(key, NSSM_REG_STDIN, NSSM_REG_STDIO_DISPOSITION, service->stdin_disposition);
  76. else if (editing) delete_createfile_parameter(key, NSSM_REG_STDIN, NSSM_REG_STDIO_DISPOSITION);
  77. if (service->stdin_flags != NSSM_STDIN_FLAGS) set_createfile_parameter(key, NSSM_REG_STDIN, NSSM_REG_STDIO_FLAGS, service->stdin_flags);
  78. else if (editing) delete_createfile_parameter(key, NSSM_REG_STDIN, NSSM_REG_STDIO_FLAGS);
  79. }
  80. if (service->stdout_path[0] || editing) {
  81. if (service->stdout_path[0]) set_expand_string(key, NSSM_REG_STDOUT, service->stdout_path);
  82. else if (editing) RegDeleteValue(key, NSSM_REG_STDOUT);
  83. if (service->stdout_sharing != NSSM_STDOUT_SHARING) set_createfile_parameter(key, NSSM_REG_STDOUT, NSSM_REG_STDIO_SHARING, service->stdout_sharing);
  84. else if (editing) delete_createfile_parameter(key, NSSM_REG_STDOUT, NSSM_REG_STDIO_SHARING);
  85. if (service->stdout_disposition != NSSM_STDOUT_DISPOSITION) set_createfile_parameter(key, NSSM_REG_STDOUT, NSSM_REG_STDIO_DISPOSITION, service->stdout_disposition);
  86. else if (editing) delete_createfile_parameter(key, NSSM_REG_STDOUT, NSSM_REG_STDIO_DISPOSITION);
  87. if (service->stdout_flags != NSSM_STDOUT_FLAGS) set_createfile_parameter(key, NSSM_REG_STDOUT, NSSM_REG_STDIO_FLAGS, service->stdout_flags);
  88. else if (editing) delete_createfile_parameter(key, NSSM_REG_STDOUT, NSSM_REG_STDIO_FLAGS);
  89. }
  90. if (service->stderr_path[0] || editing) {
  91. if (service->stderr_path[0]) set_expand_string(key, NSSM_REG_STDERR, service->stderr_path);
  92. else if (editing) RegDeleteValue(key, NSSM_REG_STDERR);
  93. if (service->stderr_sharing != NSSM_STDERR_SHARING) set_createfile_parameter(key, NSSM_REG_STDERR, NSSM_REG_STDIO_SHARING, service->stderr_sharing);
  94. else if (editing) delete_createfile_parameter(key, NSSM_REG_STDERR, NSSM_REG_STDIO_SHARING);
  95. if (service->stderr_disposition != NSSM_STDERR_DISPOSITION) set_createfile_parameter(key, NSSM_REG_STDERR, NSSM_REG_STDIO_DISPOSITION, service->stderr_disposition);
  96. else if (editing) delete_createfile_parameter(key, NSSM_REG_STDERR, NSSM_REG_STDIO_DISPOSITION);
  97. if (service->stderr_flags != NSSM_STDERR_FLAGS) set_createfile_parameter(key, NSSM_REG_STDERR, NSSM_REG_STDIO_FLAGS, service->stderr_flags);
  98. else if (editing) delete_createfile_parameter(key, NSSM_REG_STDERR, NSSM_REG_STDIO_FLAGS);
  99. }
  100. if (service->rotate_files) set_number(key, NSSM_REG_ROTATE, 1);
  101. else if (editing) RegDeleteValue(key, NSSM_REG_ROTATE);
  102. if (service->rotate_seconds) set_number(key, NSSM_REG_ROTATE_SECONDS, service->rotate_seconds);
  103. else if (editing) RegDeleteValue(key, NSSM_REG_ROTATE_SECONDS);
  104. if (service->rotate_bytes_low) set_number(key, NSSM_REG_ROTATE_BYTES_LOW, service->rotate_bytes_low);
  105. else if (editing) RegDeleteValue(key, NSSM_REG_ROTATE_BYTES_LOW);
  106. if (service->rotate_bytes_high) set_number(key, NSSM_REG_ROTATE_BYTES_HIGH, service->rotate_bytes_high);
  107. else if (editing) RegDeleteValue(key, NSSM_REG_ROTATE_BYTES_HIGH);
  108. /* Environment */
  109. if (service->env) {
  110. if (RegSetValueEx(key, NSSM_REG_ENV, 0, REG_MULTI_SZ, (const unsigned char *) service->env, (unsigned long) service->envlen * sizeof(TCHAR)) != ERROR_SUCCESS) {
  111. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, NSSM_REG_ENV, error_string(GetLastError()), 0);
  112. }
  113. }
  114. else if (editing) RegDeleteValue(key, NSSM_REG_ENV);
  115. if (service->env_extra) {
  116. 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) {
  117. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, NSSM_REG_ENV_EXTRA, error_string(GetLastError()), 0);
  118. }
  119. }
  120. else if (editing) RegDeleteValue(key, NSSM_REG_ENV_EXTRA);
  121. /* Close registry. */
  122. RegCloseKey(key);
  123. return 0;
  124. }
  125. int create_exit_action(TCHAR *service_name, const TCHAR *action_string, bool editing) {
  126. /* Get registry */
  127. TCHAR registry[KEY_LENGTH];
  128. if (_sntprintf_s(registry, _countof(registry), _TRUNCATE, NSSM_REGISTRY _T("\\%s"), service_name, NSSM_REG_EXIT) < 0) {
  129. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("NSSM_REG_EXIT"), _T("create_exit_action()"), 0);
  130. return 1;
  131. }
  132. /* Try to open the registry */
  133. HKEY key;
  134. unsigned long disposition;
  135. if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, registry, 0, 0, REG_OPTION_NON_VOLATILE, KEY_WRITE, 0, &key, &disposition) != ERROR_SUCCESS) {
  136. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENKEY_FAILED, registry, error_string(GetLastError()), 0);
  137. return 2;
  138. }
  139. /* Do nothing if the key already existed */
  140. if (disposition == REG_OPENED_EXISTING_KEY && ! editing) {
  141. RegCloseKey(key);
  142. return 0;
  143. }
  144. /* Create the default value */
  145. if (RegSetValueEx(key, 0, 0, REG_SZ, (const unsigned char *) action_string, (unsigned long) (_tcslen(action_string) + 1) * sizeof(TCHAR)) != ERROR_SUCCESS) {
  146. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, NSSM_REG_EXIT, error_string(GetLastError()), 0);
  147. RegCloseKey(key);
  148. return 3;
  149. }
  150. /* Close registry */
  151. RegCloseKey(key);
  152. return 0;
  153. }
  154. int set_environment(TCHAR *service_name, HKEY key, TCHAR *value, TCHAR **env, unsigned long *envlen) {
  155. unsigned long type = REG_MULTI_SZ;
  156. /* Dummy test to find buffer size */
  157. unsigned long ret = RegQueryValueEx(key, value, 0, &type, NULL, envlen);
  158. if (ret != ERROR_SUCCESS) {
  159. *env = 0;
  160. *envlen = 0;
  161. /* The service probably doesn't have any environment configured */
  162. if (ret == ERROR_FILE_NOT_FOUND) return 0;
  163. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, value, error_string(GetLastError()), 0);
  164. return 1;
  165. }
  166. if (type != REG_MULTI_SZ) {
  167. *env = 0;
  168. *envlen = 0;
  169. log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_INVALID_ENVIRONMENT_STRING_TYPE, value, service_name, 0);
  170. return 2;
  171. }
  172. /* Probably not possible */
  173. if (! *envlen) return 0;
  174. /* Previously initialised? */
  175. if (*env) HeapFree(GetProcessHeap(), 0, *env);
  176. *env = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, *envlen);
  177. if (! *env) {
  178. *envlen = 0;
  179. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, value, _T("set_environment()"), 0);
  180. return 3;
  181. }
  182. /* Actually get the strings */
  183. ret = RegQueryValueEx(key, value, 0, &type, (unsigned char *) *env, envlen);
  184. if (ret != ERROR_SUCCESS) {
  185. HeapFree(GetProcessHeap(), 0, *env);
  186. *env = 0;
  187. *envlen = 0;
  188. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, value, error_string(GetLastError()), 0);
  189. return 4;
  190. }
  191. return 0;
  192. }
  193. /* Replace NULL with CRLF. Leave NULL NULL as the end marker. */
  194. int format_environment(TCHAR *env, unsigned long envlen, TCHAR **formatted, unsigned long *newlen) {
  195. unsigned long i, j;
  196. *newlen = envlen;
  197. if (! *newlen) {
  198. *formatted = 0;
  199. return 0;
  200. }
  201. for (i = 0; i < envlen; i++) if (! env[i] && env[i + 1]) ++*newlen;
  202. *formatted = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *newlen * sizeof(TCHAR));
  203. if (! *formatted) {
  204. *newlen = 0;
  205. return 1;
  206. }
  207. for (i = 0, j = 0; i < envlen; i++) {
  208. (*formatted)[j] = env[i];
  209. if (! env[i]) {
  210. if (env[i + 1]) {
  211. (*formatted)[j] = _T('\r');
  212. (*formatted)[++j] = _T('\n');
  213. }
  214. }
  215. j++;
  216. }
  217. return 0;
  218. }
  219. /* Strip CR and replace LF with NULL. */
  220. int unformat_environment(TCHAR *env, unsigned long envlen, TCHAR **unformatted, unsigned long *newlen) {
  221. unsigned long i, j;
  222. *newlen = 0;
  223. if (! envlen) {
  224. *unformatted = 0;
  225. return 0;
  226. }
  227. for (i = 0; i < envlen; i++) if (env[i] != _T('\r')) ++*newlen;
  228. /* Must end with two NULLs. */
  229. *newlen += 2;
  230. *unformatted = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *newlen * sizeof(TCHAR));
  231. if (! *unformatted) return 1;
  232. for (i = 0, j = 0; i < envlen; i++) {
  233. if (env[i] == _T('\r')) continue;
  234. if (env[i] == _T('\n')) (*unformatted)[j] = _T('\0');
  235. else (*unformatted)[j] = env[i];
  236. j++;
  237. }
  238. return 0;
  239. }
  240. int expand_parameter(HKEY key, TCHAR *value, TCHAR *data, unsigned long datalen, bool sanitise, bool must_exist) {
  241. TCHAR *buffer = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, datalen);
  242. if (! buffer) {
  243. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, value, _T("expand_parameter()"), 0);
  244. return 1;
  245. }
  246. ZeroMemory(data, datalen);
  247. unsigned long type = REG_EXPAND_SZ;
  248. unsigned long buflen = datalen;
  249. unsigned long ret = RegQueryValueEx(key, value, 0, &type, (unsigned char *) buffer, &buflen);
  250. if (ret != ERROR_SUCCESS) {
  251. unsigned long error = GetLastError();
  252. HeapFree(GetProcessHeap(), 0, buffer);
  253. if (ret == ERROR_FILE_NOT_FOUND) {
  254. if (! must_exist) return 0;
  255. }
  256. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, value, error_string(error), 0);
  257. return 2;
  258. }
  259. /* Paths aren't allowed to contain quotes. */
  260. if (sanitise) PathUnquoteSpaces(buffer);
  261. /* Technically we shouldn't expand environment strings from REG_SZ values */
  262. if (type != REG_EXPAND_SZ) {
  263. memmove(data, buffer, buflen);
  264. HeapFree(GetProcessHeap(), 0, buffer);
  265. return 0;
  266. }
  267. ret = ExpandEnvironmentStrings((TCHAR *) buffer, data, datalen);
  268. if (! ret || ret > datalen) {
  269. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_EXPANDENVIRONMENTSTRINGS_FAILED, buffer, error_string(GetLastError()), 0);
  270. HeapFree(GetProcessHeap(), 0, buffer);
  271. return 3;
  272. }
  273. HeapFree(GetProcessHeap(), 0, buffer);
  274. return 0;
  275. }
  276. int expand_parameter(HKEY key, TCHAR *value, TCHAR *data, unsigned long datalen, bool sanitise) {
  277. return expand_parameter(key, value, data, datalen, sanitise, true);
  278. }
  279. /*
  280. Sets a string in the registry.
  281. Returns: 0 if it was set.
  282. 1 on error.
  283. */
  284. int set_expand_string(HKEY key, TCHAR *value, TCHAR *string) {
  285. if (RegSetValueEx(key, value, 0, REG_EXPAND_SZ, (const unsigned char *) string, (unsigned long) (_tcslen(string) + 1) * sizeof(TCHAR)) == ERROR_SUCCESS) return 0;
  286. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, value, error_string(GetLastError()), 0);
  287. return 1;
  288. }
  289. /*
  290. Set an unsigned long in the registry.
  291. Returns: 0 if it was set.
  292. 1 on error.
  293. */
  294. int set_number(HKEY key, TCHAR *value, unsigned long number) {
  295. if (RegSetValueEx(key, value, 0, REG_DWORD, (const unsigned char *) &number, sizeof(number)) == ERROR_SUCCESS) return 0;
  296. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, value, error_string(GetLastError()), 0);
  297. return 1;
  298. }
  299. /*
  300. Query an unsigned long from the registry.
  301. Returns: 1 if a number was retrieved.
  302. 0 if none was found and must_exist is false.
  303. -1 if none was found and must_exist is true.
  304. -2 otherwise.
  305. */
  306. int get_number(HKEY key, TCHAR *value, unsigned long *number, bool must_exist) {
  307. unsigned long type = REG_DWORD;
  308. unsigned long number_len = sizeof(unsigned long);
  309. int ret = RegQueryValueEx(key, value, 0, &type, (unsigned char *) number, &number_len);
  310. if (ret == ERROR_SUCCESS) return 1;
  311. if (ret == ERROR_FILE_NOT_FOUND) {
  312. if (! must_exist) return 0;
  313. }
  314. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, value, error_string(GetLastError()), 0);
  315. if (ret == ERROR_FILE_NOT_FOUND) return -1;
  316. return -2;
  317. }
  318. int get_number(HKEY key, TCHAR *value, unsigned long *number) {
  319. return get_number(key, value, number, true);
  320. }
  321. void override_milliseconds(TCHAR *service_name, HKEY key, TCHAR *value, unsigned long *buffer, unsigned long default_value, unsigned long event) {
  322. unsigned long type = REG_DWORD;
  323. unsigned long buflen = sizeof(unsigned long);
  324. bool ok = false;
  325. unsigned long ret = RegQueryValueEx(key, value, 0, &type, (unsigned char *) buffer, &buflen);
  326. if (ret != ERROR_SUCCESS) {
  327. if (ret != ERROR_FILE_NOT_FOUND) {
  328. if (type != REG_DWORD) {
  329. TCHAR milliseconds[16];
  330. _sntprintf_s(milliseconds, _countof(milliseconds), _TRUNCATE, _T("%lu"), default_value);
  331. log_event(EVENTLOG_WARNING_TYPE, event, service_name, value, milliseconds, 0);
  332. }
  333. else log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, value, error_string(GetLastError()), 0);
  334. }
  335. }
  336. else ok = true;
  337. if (! ok) *buffer = default_value;
  338. }
  339. HKEY open_registry(const TCHAR *service_name, const TCHAR *sub, REGSAM sam) {
  340. /* Get registry */
  341. TCHAR registry[KEY_LENGTH];
  342. HKEY key;
  343. int ret;
  344. if (sub) ret = _sntprintf_s(registry, _countof(registry), _TRUNCATE, NSSM_REGISTRY _T("\\%s"), service_name, sub);
  345. else ret = _sntprintf_s(registry, _countof(registry), _TRUNCATE, NSSM_REGISTRY, service_name);
  346. if (ret < 0) {
  347. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("NSSM_REGISTRY"), _T("open_registry()"), 0);
  348. return 0;
  349. }
  350. if (sam & KEY_WRITE) {
  351. if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, registry, 0, 0, REG_OPTION_NON_VOLATILE, sam, 0, &key, 0) != ERROR_SUCCESS) {
  352. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENKEY_FAILED, registry, error_string(GetLastError()), 0);
  353. return 0;
  354. }
  355. }
  356. else {
  357. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, registry, 0, sam, &key) != ERROR_SUCCESS) {
  358. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENKEY_FAILED, registry, error_string(GetLastError()), 0);
  359. return 0;
  360. }
  361. }
  362. return key;
  363. }
  364. HKEY open_registry(const TCHAR *service_name, REGSAM sam) {
  365. return open_registry(service_name, 0, sam);
  366. }
  367. int get_parameters(nssm_service_t *service, STARTUPINFO *si) {
  368. unsigned long ret;
  369. /* Try to open the registry */
  370. HKEY key = open_registry(service->name, KEY_READ);
  371. if (! key) return 1;
  372. /* Try to get executable file - MUST succeed */
  373. if (expand_parameter(key, NSSM_REG_EXE, service->exe, sizeof(service->exe), false)) {
  374. RegCloseKey(key);
  375. return 3;
  376. }
  377. /* Try to get flags - may fail and we don't care */
  378. if (expand_parameter(key, NSSM_REG_FLAGS, service->flags, sizeof(service->flags), false)) {
  379. log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_NO_FLAGS, NSSM_REG_FLAGS, service->name, service->exe, 0);
  380. ZeroMemory(service->flags, sizeof(service->flags));
  381. }
  382. /* Try to get startup directory - may fail and we fall back to a default */
  383. if (expand_parameter(key, NSSM_REG_DIR, service->dir, sizeof(service->dir), true) || ! service->dir[0]) {
  384. _sntprintf_s(service->dir, _countof(service->dir), _TRUNCATE, _T("%s"), service->exe);
  385. strip_basename(service->dir);
  386. if (service->dir[0] == _T('\0')) {
  387. /* Help! */
  388. ret = GetWindowsDirectory(service->dir, sizeof(service->dir));
  389. if (! ret || ret > sizeof(service->dir)) {
  390. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_NO_DIR_AND_NO_FALLBACK, NSSM_REG_DIR, service->name, 0);
  391. RegCloseKey(key);
  392. return 4;
  393. }
  394. }
  395. log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_NO_DIR, NSSM_REG_DIR, service->name, service->dir, 0);
  396. }
  397. /* Try to get processor affinity - may fail. */
  398. TCHAR buffer[512];
  399. if (expand_parameter(key, NSSM_REG_AFFINITY, buffer, sizeof(buffer), false, false) || ! buffer[0]) service->affinity = 0LL;
  400. else if (affinity_string_to_mask(buffer, &service->affinity)) {
  401. log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_BOGUS_AFFINITY_MASK, service->name, buffer);
  402. service->affinity = 0LL;
  403. }
  404. else {
  405. DWORD_PTR affinity, system_affinity;
  406. if (GetProcessAffinityMask(GetCurrentProcess(), &affinity, &system_affinity)) {
  407. _int64 effective_affinity = service->affinity & system_affinity;
  408. if (effective_affinity != service->affinity) {
  409. TCHAR *system = 0;
  410. if (! affinity_mask_to_string(system_affinity, &system)) {
  411. TCHAR *effective = 0;
  412. if (! affinity_mask_to_string(effective_affinity, &effective)) {
  413. log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_EFFECTIVE_AFFINITY_MASK, service->name, buffer, system, effective, 0);
  414. }
  415. HeapFree(GetProcessHeap(), 0, effective);
  416. }
  417. HeapFree(GetProcessHeap(), 0, system);
  418. }
  419. }
  420. }
  421. /* Try to get environment variables - may fail */
  422. set_environment(service->name, key, NSSM_REG_ENV, &service->env, &service->envlen);
  423. /* Environment variables to add to existing rather than replace - may fail. */
  424. set_environment(service->name, key, NSSM_REG_ENV_EXTRA, &service->env_extra, &service->env_extralen);
  425. if (si) {
  426. if (service->env_extra) {
  427. /* Append these to any other environment variables set. */
  428. if (service->env) {
  429. /* Append extra variables to configured variables. */
  430. unsigned long envlen = service->envlen + service->env_extralen - 1;
  431. TCHAR *env = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, envlen);
  432. if (env) {
  433. memmove(env, service->env, service->envlen - sizeof(TCHAR));
  434. /* envlen is in bytes. */
  435. memmove(env + (service->envlen / sizeof(TCHAR)) - 1, service->env_extra, service->env_extralen);
  436. HeapFree(GetProcessHeap(), 0, service->env);
  437. service->env = env;
  438. service->envlen = envlen;
  439. }
  440. else log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("environment"), _T("get_parameters()"), 0);
  441. }
  442. else {
  443. /* Append extra variables to our environment. */
  444. TCHAR *env, *s;
  445. size_t envlen, len;
  446. env = service->env_extra;
  447. len = 0;
  448. while (*env) {
  449. envlen = _tcslen(env) + 1;
  450. for (s = env; *s && *s != _T('='); s++);
  451. if (*s == _T('=')) *s++ = _T('\0');
  452. if (! SetEnvironmentVariable(env, s)) log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_SETENVIRONMENTVARIABLE_FAILED, env, s, error_string(GetLastError()), 0);
  453. env += envlen;
  454. }
  455. }
  456. }
  457. }
  458. /* Try to get priority - may fail. */
  459. unsigned long priority;
  460. if (get_number(key, NSSM_REG_PRIORITY, &priority, false) == 1) {
  461. if (priority == (priority & priority_mask())) service->priority = priority;
  462. else log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_BOGUS_PRIORITY, service->name, NSSM_REG_PRIORITY, 0);
  463. }
  464. /* Try to get file rotation settings - may fail. */
  465. unsigned long rotate_files;
  466. if (get_number(key, NSSM_REG_ROTATE, &rotate_files, false) == 1) {
  467. if (rotate_files) service->rotate_files = true;
  468. else service->rotate_files = false;
  469. }
  470. else service->rotate_files = false;
  471. if (get_number(key, NSSM_REG_ROTATE_SECONDS, &service->rotate_seconds, false) != 1) service->rotate_seconds = 0;
  472. if (get_number(key, NSSM_REG_ROTATE_BYTES_LOW, &service->rotate_bytes_low, false) != 1) service->rotate_bytes_low = 0;
  473. if (get_number(key, NSSM_REG_ROTATE_BYTES_HIGH, &service->rotate_bytes_high, false) != 1) service->rotate_bytes_high = 0;
  474. /* Change to startup directory in case stdout/stderr are relative paths. */
  475. TCHAR cwd[MAX_PATH];
  476. GetCurrentDirectory(_countof(cwd), cwd);
  477. SetCurrentDirectory(service->dir);
  478. /* Try to get stdout and stderr */
  479. if (get_output_handles(service, key, si)) {
  480. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_GET_OUTPUT_HANDLES_FAILED, service->name, 0);
  481. RegCloseKey(key);
  482. SetCurrentDirectory(cwd);
  483. return 5;
  484. }
  485. /* Change back in case the startup directory needs to be deleted. */
  486. SetCurrentDirectory(cwd);
  487. /* Try to get throttle restart delay */
  488. override_milliseconds(service->name, key, NSSM_REG_THROTTLE, &service->throttle_delay, NSSM_RESET_THROTTLE_RESTART, NSSM_EVENT_BOGUS_THROTTLE);
  489. /* Try to get service stop flags. */
  490. unsigned long type = REG_DWORD;
  491. unsigned long stop_method_skip;
  492. unsigned long buflen = sizeof(stop_method_skip);
  493. bool stop_ok = false;
  494. ret = RegQueryValueEx(key, NSSM_REG_STOP_METHOD_SKIP, 0, &type, (unsigned char *) &stop_method_skip, &buflen);
  495. if (ret != ERROR_SUCCESS) {
  496. if (ret != ERROR_FILE_NOT_FOUND) {
  497. if (type != REG_DWORD) {
  498. log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_BOGUS_STOP_METHOD_SKIP, service->name, NSSM_REG_STOP_METHOD_SKIP, NSSM, 0);
  499. }
  500. else log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, NSSM_REG_STOP_METHOD_SKIP, error_string(GetLastError()), 0);
  501. }
  502. }
  503. else stop_ok = true;
  504. /* Try all methods except those requested to be skipped. */
  505. service->stop_method = ~0;
  506. if (stop_ok) service->stop_method &= ~stop_method_skip;
  507. /* Try to get kill delays - may fail. */
  508. 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);
  509. 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);
  510. 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);
  511. /* Try to get default exit action. */
  512. bool default_action;
  513. service->default_exit_action = NSSM_EXIT_RESTART;
  514. TCHAR action_string[ACTION_LEN];
  515. if (! get_exit_action(service->name, 0, action_string, &default_action)) {
  516. for (int i = 0; exit_action_strings[i]; i++) {
  517. if (! _tcsnicmp((const TCHAR *) action_string, exit_action_strings[i], ACTION_LEN)) {
  518. service->default_exit_action = i;
  519. break;
  520. }
  521. }
  522. }
  523. /* Close registry */
  524. RegCloseKey(key);
  525. return 0;
  526. }
  527. /*
  528. Sets the string for the exit action corresponding to the exit code.
  529. ret is a pointer to an unsigned long containing the exit code.
  530. If ret is NULL, we retrieve the default exit action unconditionally.
  531. action is a buffer which receives the string.
  532. default_action is a pointer to a bool which is set to false if there
  533. was an explicit string for the given exit code, or true if we are
  534. returning the default action.
  535. Returns: 0 on success.
  536. 1 on error.
  537. */
  538. int get_exit_action(const TCHAR *service_name, unsigned long *ret, TCHAR *action, bool *default_action) {
  539. /* Are we returning the default action or a status-specific one? */
  540. *default_action = ! ret;
  541. /* Try to open the registry */
  542. HKEY key = open_registry(service_name, NSSM_REG_EXIT, KEY_READ);
  543. if (! key) return 1;
  544. unsigned long type = REG_SZ;
  545. unsigned long action_len = ACTION_LEN;
  546. TCHAR code[16];
  547. if (! ret) code[0] = _T('\0');
  548. else if (_sntprintf_s(code, _countof(code), _TRUNCATE, _T("%lu"), *ret) < 0) {
  549. RegCloseKey(key);
  550. return get_exit_action(service_name, 0, action, default_action);
  551. }
  552. if (RegQueryValueEx(key, code, 0, &type, (unsigned char *) action, &action_len) != ERROR_SUCCESS) {
  553. RegCloseKey(key);
  554. /* Try again with * as the key if an exit code was defined */
  555. if (ret) return get_exit_action(service_name, 0, action, default_action);
  556. return 0;
  557. }
  558. /* Close registry */
  559. RegCloseKey(key);
  560. return 0;
  561. }