registry.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. #include "nssm.h"
  2. extern const TCHAR *exit_action_strings[];
  3. static int service_registry_path(const TCHAR *service_name, bool parameters, const TCHAR *sub, TCHAR *buffer, unsigned long buflen) {
  4. int ret;
  5. if (parameters) {
  6. if (sub) ret = _sntprintf_s(buffer, buflen, _TRUNCATE, NSSM_REGISTRY _T("\\") NSSM_REG_PARAMETERS _T("\\%s"), service_name, sub);
  7. else ret = _sntprintf_s(buffer, buflen, _TRUNCATE, NSSM_REGISTRY _T("\\") NSSM_REG_PARAMETERS, service_name);
  8. }
  9. else ret = _sntprintf_s(buffer, buflen, _TRUNCATE, NSSM_REGISTRY, service_name);
  10. return ret;
  11. }
  12. static long open_registry_key(const TCHAR *registry, REGSAM sam, HKEY *key, bool must_exist) {
  13. long error;
  14. if (sam & KEY_SET_VALUE) {
  15. error = RegCreateKeyEx(HKEY_LOCAL_MACHINE, registry, 0, 0, REG_OPTION_NON_VOLATILE, sam, 0, key, 0);
  16. if (error != ERROR_SUCCESS) {
  17. *key = 0;
  18. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENKEY_FAILED, registry, error_string(GetLastError()), 0);
  19. return error;
  20. }
  21. }
  22. else {
  23. error = RegOpenKeyEx(HKEY_LOCAL_MACHINE, registry, 0, sam, key);
  24. if (error != ERROR_SUCCESS) {
  25. *key = 0;
  26. if (error != ERROR_FILE_NOT_FOUND || must_exist) log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENKEY_FAILED, registry, error_string(GetLastError()), 0);
  27. }
  28. }
  29. return error;
  30. }
  31. static HKEY open_registry_key(const TCHAR *registry, REGSAM sam, bool must_exist) {
  32. HKEY key;
  33. long error = open_registry_key(registry, sam, &key, must_exist);
  34. return key;
  35. }
  36. int create_messages() {
  37. HKEY key;
  38. TCHAR registry[KEY_LENGTH];
  39. if (_sntprintf_s(registry, _countof(registry), _TRUNCATE, _T("SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s"), NSSM) < 0) {
  40. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("eventlog registry"), _T("create_messages()"), 0);
  41. return 1;
  42. }
  43. if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, registry, 0, 0, REG_OPTION_NON_VOLATILE, KEY_WRITE, 0, &key, 0) != ERROR_SUCCESS) {
  44. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENKEY_FAILED, registry, error_string(GetLastError()), 0);
  45. return 2;
  46. }
  47. /* Get path of this program */
  48. const TCHAR *path = nssm_unquoted_imagepath();
  49. /* Try to register the module but don't worry so much on failure */
  50. RegSetValueEx(key, _T("EventMessageFile"), 0, REG_SZ, (const unsigned char *) path, (unsigned long) (_tcslen(path) + 1) * sizeof(TCHAR));
  51. unsigned long types = EVENTLOG_INFORMATION_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_ERROR_TYPE;
  52. RegSetValueEx(key, _T("TypesSupported"), 0, REG_DWORD, (const unsigned char *) &types, sizeof(types));
  53. return 0;
  54. }
  55. int create_parameters(nssm_service_t *service, bool editing) {
  56. /* Try to open the registry */
  57. HKEY key = open_registry(service->name, KEY_WRITE);
  58. if (! key) return 1;
  59. /* Remember parameters in case we need to delete them. */
  60. TCHAR registry[KEY_LENGTH];
  61. int ret = service_registry_path(service->name, true, 0, registry, _countof(registry));
  62. /* Try to create the parameters */
  63. if (set_expand_string(key, NSSM_REG_EXE, service->exe)) {
  64. if (ret > 0) RegDeleteKey(HKEY_LOCAL_MACHINE, registry);
  65. RegCloseKey(key);
  66. return 2;
  67. }
  68. if (set_expand_string(key, NSSM_REG_FLAGS, service->flags)) {
  69. if (ret > 0) RegDeleteKey(HKEY_LOCAL_MACHINE, registry);
  70. RegCloseKey(key);
  71. return 3;
  72. }
  73. if (set_expand_string(key, NSSM_REG_DIR, service->dir)) {
  74. if (ret > 0) RegDeleteKey(HKEY_LOCAL_MACHINE, registry);
  75. RegCloseKey(key);
  76. return 4;
  77. }
  78. /* Other non-default parameters. May fail. */
  79. if (service->priority != NORMAL_PRIORITY_CLASS) set_number(key, NSSM_REG_PRIORITY, service->priority);
  80. else if (editing) RegDeleteValue(key, NSSM_REG_PRIORITY);
  81. if (service->affinity) {
  82. TCHAR *string;
  83. if (! affinity_mask_to_string(service->affinity, &string)) {
  84. if (RegSetValueEx(key, NSSM_REG_AFFINITY, 0, REG_SZ, (const unsigned char *) string, (unsigned long) (_tcslen(string) + 1) * sizeof(TCHAR)) != ERROR_SUCCESS) {
  85. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, NSSM_REG_AFFINITY, error_string(GetLastError()), 0);
  86. HeapFree(GetProcessHeap(), 0, string);
  87. return 5;
  88. }
  89. }
  90. if (string) HeapFree(GetProcessHeap(), 0, string);
  91. }
  92. else if (editing) RegDeleteValue(key, NSSM_REG_AFFINITY);
  93. unsigned long stop_method_skip = ~service->stop_method;
  94. if (stop_method_skip) set_number(key, NSSM_REG_STOP_METHOD_SKIP, stop_method_skip);
  95. else if (editing) RegDeleteValue(key, NSSM_REG_STOP_METHOD_SKIP);
  96. if (service->default_exit_action < NSSM_NUM_EXIT_ACTIONS) create_exit_action(service->name, exit_action_strings[service->default_exit_action], editing);
  97. if (service->restart_delay) set_number(key, NSSM_REG_RESTART_DELAY, service->restart_delay);
  98. else if (editing) RegDeleteValue(key, NSSM_REG_RESTART_DELAY);
  99. if (service->throttle_delay != NSSM_RESET_THROTTLE_RESTART) set_number(key, NSSM_REG_THROTTLE, service->throttle_delay);
  100. else if (editing) RegDeleteValue(key, NSSM_REG_THROTTLE);
  101. if (service->kill_console_delay != NSSM_KILL_CONSOLE_GRACE_PERIOD) set_number(key, NSSM_REG_KILL_CONSOLE_GRACE_PERIOD, service->kill_console_delay);
  102. else if (editing) RegDeleteValue(key, NSSM_REG_KILL_CONSOLE_GRACE_PERIOD);
  103. if (service->kill_window_delay != NSSM_KILL_WINDOW_GRACE_PERIOD) set_number(key, NSSM_REG_KILL_WINDOW_GRACE_PERIOD, service->kill_window_delay);
  104. else if (editing) RegDeleteValue(key, NSSM_REG_KILL_WINDOW_GRACE_PERIOD);
  105. if (service->kill_threads_delay != NSSM_KILL_THREADS_GRACE_PERIOD) set_number(key, NSSM_REG_KILL_THREADS_GRACE_PERIOD, service->kill_threads_delay);
  106. else if (editing) RegDeleteValue(key, NSSM_REG_KILL_THREADS_GRACE_PERIOD);
  107. if (! service->kill_process_tree) set_number(key, NSSM_REG_KILL_PROCESS_TREE, 0);
  108. else if (editing) RegDeleteValue(key, NSSM_REG_KILL_PROCESS_TREE);
  109. if (service->stdin_path[0] || editing) {
  110. if (service->stdin_path[0]) set_expand_string(key, NSSM_REG_STDIN, service->stdin_path);
  111. else if (editing) RegDeleteValue(key, NSSM_REG_STDIN);
  112. if (service->stdin_sharing != NSSM_STDIN_SHARING) set_createfile_parameter(key, NSSM_REG_STDIN, NSSM_REG_STDIO_SHARING, service->stdin_sharing);
  113. else if (editing) delete_createfile_parameter(key, NSSM_REG_STDIN, NSSM_REG_STDIO_SHARING);
  114. if (service->stdin_disposition != NSSM_STDIN_DISPOSITION) set_createfile_parameter(key, NSSM_REG_STDIN, NSSM_REG_STDIO_DISPOSITION, service->stdin_disposition);
  115. else if (editing) delete_createfile_parameter(key, NSSM_REG_STDIN, NSSM_REG_STDIO_DISPOSITION);
  116. if (service->stdin_flags != NSSM_STDIN_FLAGS) set_createfile_parameter(key, NSSM_REG_STDIN, NSSM_REG_STDIO_FLAGS, service->stdin_flags);
  117. else if (editing) delete_createfile_parameter(key, NSSM_REG_STDIN, NSSM_REG_STDIO_FLAGS);
  118. }
  119. if (service->stdout_path[0] || editing) {
  120. if (service->stdout_path[0]) set_expand_string(key, NSSM_REG_STDOUT, service->stdout_path);
  121. else if (editing) RegDeleteValue(key, NSSM_REG_STDOUT);
  122. if (service->stdout_sharing != NSSM_STDOUT_SHARING) set_createfile_parameter(key, NSSM_REG_STDOUT, NSSM_REG_STDIO_SHARING, service->stdout_sharing);
  123. else if (editing) delete_createfile_parameter(key, NSSM_REG_STDOUT, NSSM_REG_STDIO_SHARING);
  124. if (service->stdout_disposition != NSSM_STDOUT_DISPOSITION) set_createfile_parameter(key, NSSM_REG_STDOUT, NSSM_REG_STDIO_DISPOSITION, service->stdout_disposition);
  125. else if (editing) delete_createfile_parameter(key, NSSM_REG_STDOUT, NSSM_REG_STDIO_DISPOSITION);
  126. if (service->stdout_flags != NSSM_STDOUT_FLAGS) set_createfile_parameter(key, NSSM_REG_STDOUT, NSSM_REG_STDIO_FLAGS, service->stdout_flags);
  127. else if (editing) delete_createfile_parameter(key, NSSM_REG_STDOUT, NSSM_REG_STDIO_FLAGS);
  128. if (service->stdout_copy_and_truncate) set_createfile_parameter(key, NSSM_REG_STDOUT, NSSM_REG_STDIO_COPY_AND_TRUNCATE, 1);
  129. else if (editing) delete_createfile_parameter(key, NSSM_REG_STDOUT, NSSM_REG_STDIO_COPY_AND_TRUNCATE);
  130. }
  131. if (service->stderr_path[0] || editing) {
  132. if (service->stderr_path[0]) set_expand_string(key, NSSM_REG_STDERR, service->stderr_path);
  133. else if (editing) RegDeleteValue(key, NSSM_REG_STDERR);
  134. if (service->stderr_sharing != NSSM_STDERR_SHARING) set_createfile_parameter(key, NSSM_REG_STDERR, NSSM_REG_STDIO_SHARING, service->stderr_sharing);
  135. else if (editing) delete_createfile_parameter(key, NSSM_REG_STDERR, NSSM_REG_STDIO_SHARING);
  136. if (service->stderr_disposition != NSSM_STDERR_DISPOSITION) set_createfile_parameter(key, NSSM_REG_STDERR, NSSM_REG_STDIO_DISPOSITION, service->stderr_disposition);
  137. else if (editing) delete_createfile_parameter(key, NSSM_REG_STDERR, NSSM_REG_STDIO_DISPOSITION);
  138. if (service->stderr_flags != NSSM_STDERR_FLAGS) set_createfile_parameter(key, NSSM_REG_STDERR, NSSM_REG_STDIO_FLAGS, service->stderr_flags);
  139. else if (editing) delete_createfile_parameter(key, NSSM_REG_STDERR, NSSM_REG_STDIO_FLAGS);
  140. if (service->stderr_copy_and_truncate) set_createfile_parameter(key, NSSM_REG_STDERR, NSSM_REG_STDIO_COPY_AND_TRUNCATE, 1);
  141. else if (editing) delete_createfile_parameter(key, NSSM_REG_STDERR, NSSM_REG_STDIO_COPY_AND_TRUNCATE);
  142. }
  143. if (service->rotate_files) set_number(key, NSSM_REG_ROTATE, 1);
  144. else if (editing) RegDeleteValue(key, NSSM_REG_ROTATE);
  145. if (service->rotate_stdout_online) set_number(key, NSSM_REG_ROTATE_ONLINE, 1);
  146. else if (editing) RegDeleteValue(key, NSSM_REG_ROTATE_ONLINE);
  147. if (service->rotate_seconds) set_number(key, NSSM_REG_ROTATE_SECONDS, service->rotate_seconds);
  148. else if (editing) RegDeleteValue(key, NSSM_REG_ROTATE_SECONDS);
  149. if (service->rotate_bytes_low) set_number(key, NSSM_REG_ROTATE_BYTES_LOW, service->rotate_bytes_low);
  150. else if (editing) RegDeleteValue(key, NSSM_REG_ROTATE_BYTES_LOW);
  151. if (service->rotate_bytes_high) set_number(key, NSSM_REG_ROTATE_BYTES_HIGH, service->rotate_bytes_high);
  152. else if (editing) RegDeleteValue(key, NSSM_REG_ROTATE_BYTES_HIGH);
  153. if (service->rotate_delay != NSSM_ROTATE_DELAY) set_number(key, NSSM_REG_ROTATE_DELAY, service->rotate_delay);
  154. else if (editing) RegDeleteValue(key, NSSM_REG_ROTATE_DELAY);
  155. if (service->no_console) set_number(key, NSSM_REG_NO_CONSOLE, 1);
  156. else if (editing) RegDeleteValue(key, NSSM_REG_NO_CONSOLE);
  157. /* Environment */
  158. if (service->env) {
  159. if (RegSetValueEx(key, NSSM_REG_ENV, 0, REG_MULTI_SZ, (const unsigned char *) service->env, (unsigned long) service->envlen * sizeof(TCHAR)) != ERROR_SUCCESS) {
  160. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, NSSM_REG_ENV, error_string(GetLastError()), 0);
  161. }
  162. }
  163. else if (editing) RegDeleteValue(key, NSSM_REG_ENV);
  164. if (service->env_extra) {
  165. 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) {
  166. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, NSSM_REG_ENV_EXTRA, error_string(GetLastError()), 0);
  167. }
  168. }
  169. else if (editing) RegDeleteValue(key, NSSM_REG_ENV_EXTRA);
  170. /* Close registry. */
  171. RegCloseKey(key);
  172. return 0;
  173. }
  174. int create_exit_action(TCHAR *service_name, const TCHAR *action_string, bool editing) {
  175. /* Get registry */
  176. TCHAR registry[KEY_LENGTH];
  177. if (service_registry_path(service_name, true, NSSM_REG_EXIT, registry, _countof(registry)) < 0) {
  178. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("NSSM_REG_EXIT"), _T("create_exit_action()"), 0);
  179. return 1;
  180. }
  181. /* Try to open the registry */
  182. HKEY key;
  183. unsigned long disposition;
  184. if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, registry, 0, 0, REG_OPTION_NON_VOLATILE, KEY_WRITE, 0, &key, &disposition) != ERROR_SUCCESS) {
  185. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENKEY_FAILED, registry, error_string(GetLastError()), 0);
  186. return 2;
  187. }
  188. /* Do nothing if the key already existed */
  189. if (disposition == REG_OPENED_EXISTING_KEY && ! editing) {
  190. RegCloseKey(key);
  191. return 0;
  192. }
  193. /* Create the default value */
  194. if (RegSetValueEx(key, 0, 0, REG_SZ, (const unsigned char *) action_string, (unsigned long) (_tcslen(action_string) + 1) * sizeof(TCHAR)) != ERROR_SUCCESS) {
  195. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, NSSM_REG_EXIT, error_string(GetLastError()), 0);
  196. RegCloseKey(key);
  197. return 3;
  198. }
  199. /* Close registry */
  200. RegCloseKey(key);
  201. return 0;
  202. }
  203. int get_environment(TCHAR *service_name, HKEY key, TCHAR *value, TCHAR **env, unsigned long *envlen) {
  204. unsigned long type = REG_MULTI_SZ;
  205. /* Dummy test to find buffer size */
  206. unsigned long ret = RegQueryValueEx(key, value, 0, &type, NULL, envlen);
  207. if (ret != ERROR_SUCCESS) {
  208. *env = 0;
  209. *envlen = 0;
  210. /* The service probably doesn't have any environment configured */
  211. if (ret == ERROR_FILE_NOT_FOUND) return 0;
  212. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, value, error_string(GetLastError()), 0);
  213. return 1;
  214. }
  215. if (type != REG_MULTI_SZ) {
  216. *env = 0;
  217. *envlen = 0;
  218. log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_INVALID_ENVIRONMENT_STRING_TYPE, value, service_name, 0);
  219. return 2;
  220. }
  221. /* Probably not possible */
  222. if (! *envlen) return 0;
  223. /* Previously initialised? */
  224. if (*env) HeapFree(GetProcessHeap(), 0, *env);
  225. *env = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, *envlen);
  226. if (! *env) {
  227. *envlen = 0;
  228. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, value, _T("get_environment()"), 0);
  229. return 3;
  230. }
  231. /* Actually get the strings */
  232. ret = RegQueryValueEx(key, value, 0, &type, (unsigned char *) *env, envlen);
  233. if (ret != ERROR_SUCCESS) {
  234. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, value, error_string(GetLastError()), 0);
  235. HeapFree(GetProcessHeap(), 0, *env);
  236. *env = 0;
  237. *envlen = 0;
  238. return 4;
  239. }
  240. return 0;
  241. }
  242. int get_string(HKEY key, TCHAR *value, TCHAR *data, unsigned long datalen, bool expand, bool sanitise, bool must_exist) {
  243. TCHAR *buffer = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, datalen);
  244. if (! buffer) {
  245. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, value, _T("get_string()"), 0);
  246. return 1;
  247. }
  248. ZeroMemory(data, datalen);
  249. unsigned long type = REG_EXPAND_SZ;
  250. unsigned long buflen = datalen;
  251. unsigned long ret = RegQueryValueEx(key, value, 0, &type, (unsigned char *) buffer, &buflen);
  252. if (ret != ERROR_SUCCESS) {
  253. unsigned long error = GetLastError();
  254. HeapFree(GetProcessHeap(), 0, buffer);
  255. if (ret == ERROR_FILE_NOT_FOUND) {
  256. if (! must_exist) return 0;
  257. }
  258. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, value, error_string(error), 0);
  259. return 2;
  260. }
  261. /* Paths aren't allowed to contain quotes. */
  262. if (sanitise) PathUnquoteSpaces(buffer);
  263. /* Do we want to expand the string? */
  264. if (! expand) {
  265. if (type == REG_EXPAND_SZ) type = REG_SZ;
  266. }
  267. /* Technically we shouldn't expand environment strings from REG_SZ values */
  268. if (type != REG_EXPAND_SZ) {
  269. memmove(data, buffer, buflen);
  270. HeapFree(GetProcessHeap(), 0, buffer);
  271. return 0;
  272. }
  273. ret = ExpandEnvironmentStrings((TCHAR *) buffer, data, datalen);
  274. if (! ret || ret > datalen) {
  275. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_EXPANDENVIRONMENTSTRINGS_FAILED, buffer, error_string(GetLastError()), 0);
  276. HeapFree(GetProcessHeap(), 0, buffer);
  277. return 3;
  278. }
  279. HeapFree(GetProcessHeap(), 0, buffer);
  280. return 0;
  281. }
  282. int get_string(HKEY key, TCHAR *value, TCHAR *data, unsigned long datalen, bool sanitise) {
  283. return get_string(key, value, data, datalen, false, sanitise, true);
  284. }
  285. int expand_parameter(HKEY key, TCHAR *value, TCHAR *data, unsigned long datalen, bool sanitise, bool must_exist) {
  286. return get_string(key, value, data, datalen, true, sanitise, must_exist);
  287. }
  288. int expand_parameter(HKEY key, TCHAR *value, TCHAR *data, unsigned long datalen, bool sanitise) {
  289. return expand_parameter(key, value, data, datalen, sanitise, true);
  290. }
  291. /*
  292. Sets a string in the registry.
  293. Returns: 0 if it was set.
  294. 1 on error.
  295. */
  296. int set_string(HKEY key, TCHAR *value, TCHAR *string, bool expand) {
  297. unsigned long type = expand ? REG_EXPAND_SZ : REG_SZ;
  298. if (RegSetValueEx(key, value, 0, type, (const unsigned char *) string, (unsigned long) (_tcslen(string) + 1) * sizeof(TCHAR)) == ERROR_SUCCESS) return 0;
  299. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, value, error_string(GetLastError()), 0);
  300. return 1;
  301. }
  302. int set_string(HKEY key, TCHAR *value, TCHAR *string) {
  303. return set_string(key, value, string, false);
  304. return 1;
  305. }
  306. int set_expand_string(HKEY key, TCHAR *value, TCHAR *string) {
  307. return set_string(key, value, string, true);
  308. return 1;
  309. }
  310. /*
  311. Set an unsigned long in the registry.
  312. Returns: 0 if it was set.
  313. 1 on error.
  314. */
  315. int set_number(HKEY key, TCHAR *value, unsigned long number) {
  316. if (RegSetValueEx(key, value, 0, REG_DWORD, (const unsigned char *) &number, sizeof(number)) == ERROR_SUCCESS) return 0;
  317. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, value, error_string(GetLastError()), 0);
  318. return 1;
  319. }
  320. /*
  321. Query an unsigned long from the registry.
  322. Returns: 1 if a number was retrieved.
  323. 0 if none was found and must_exist is false.
  324. -1 if none was found and must_exist is true.
  325. -2 otherwise.
  326. */
  327. int get_number(HKEY key, TCHAR *value, unsigned long *number, bool must_exist) {
  328. unsigned long type = REG_DWORD;
  329. unsigned long number_len = sizeof(unsigned long);
  330. int ret = RegQueryValueEx(key, value, 0, &type, (unsigned char *) number, &number_len);
  331. if (ret == ERROR_SUCCESS) return 1;
  332. if (ret == ERROR_FILE_NOT_FOUND) {
  333. if (! must_exist) return 0;
  334. }
  335. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, value, error_string(GetLastError()), 0);
  336. if (ret == ERROR_FILE_NOT_FOUND) return -1;
  337. return -2;
  338. }
  339. int get_number(HKEY key, TCHAR *value, unsigned long *number) {
  340. return get_number(key, value, number, true);
  341. }
  342. /* Replace NULL with CRLF. Leave NULL NULL as the end marker. */
  343. int format_double_null(TCHAR *dn, unsigned long dnlen, TCHAR **formatted, unsigned long *newlen) {
  344. unsigned long i, j;
  345. *newlen = dnlen;
  346. if (! *newlen) {
  347. *formatted = 0;
  348. return 0;
  349. }
  350. for (i = 0; i < dnlen; i++) if (! dn[i] && dn[i + 1]) ++*newlen;
  351. *formatted = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *newlen * sizeof(TCHAR));
  352. if (! *formatted) {
  353. *newlen = 0;
  354. return 1;
  355. }
  356. for (i = 0, j = 0; i < dnlen; i++) {
  357. (*formatted)[j] = dn[i];
  358. if (! dn[i]) {
  359. if (dn[i + 1]) {
  360. (*formatted)[j] = _T('\r');
  361. (*formatted)[++j] = _T('\n');
  362. }
  363. }
  364. j++;
  365. }
  366. return 0;
  367. }
  368. /* Strip CR and replace LF with NULL. */
  369. int unformat_double_null(TCHAR *dn, unsigned long dnlen, TCHAR **unformatted, unsigned long *newlen) {
  370. unsigned long i, j;
  371. *newlen = 0;
  372. if (! dnlen) {
  373. *unformatted = 0;
  374. return 0;
  375. }
  376. for (i = 0; i < dnlen; i++) if (dn[i] != _T('\r')) ++*newlen;
  377. /* Skip blank lines. */
  378. for (i = 0; i < dnlen; i++) {
  379. if (dn[i] == _T('\r') && dn[i + 1] == _T('\n')) {
  380. /* This is the last CRLF. */
  381. if (i >= dnlen - 2) break;
  382. /*
  383. Strip at the start of the block or if the next characters are
  384. CRLF too.
  385. */
  386. if (! i || (dn[i + 2] == _T('\r') && dn[i + 3] == _T('\n'))) {
  387. for (j = i + 2; j < dnlen; j++) dn[j - 2] = dn[j];
  388. dn[dnlen--] = _T('\0');
  389. dn[dnlen--] = _T('\0');
  390. i--;
  391. --*newlen;
  392. }
  393. }
  394. }
  395. /* Must end with two NULLs. */
  396. *newlen += 2;
  397. *unformatted = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *newlen * sizeof(TCHAR));
  398. if (! *unformatted) return 1;
  399. for (i = 0, j = 0; i < dnlen; i++) {
  400. if (dn[i] == _T('\r')) continue;
  401. if (dn[i] == _T('\n')) (*unformatted)[j] = _T('\0');
  402. else (*unformatted)[j] = dn[i];
  403. j++;
  404. }
  405. return 0;
  406. }
  407. void override_milliseconds(TCHAR *service_name, HKEY key, TCHAR *value, unsigned long *buffer, unsigned long default_value, unsigned long event) {
  408. unsigned long type = REG_DWORD;
  409. unsigned long buflen = sizeof(unsigned long);
  410. bool ok = false;
  411. unsigned long ret = RegQueryValueEx(key, value, 0, &type, (unsigned char *) buffer, &buflen);
  412. if (ret != ERROR_SUCCESS) {
  413. if (ret != ERROR_FILE_NOT_FOUND) {
  414. if (type != REG_DWORD) {
  415. TCHAR milliseconds[16];
  416. _sntprintf_s(milliseconds, _countof(milliseconds), _TRUNCATE, _T("%lu"), default_value);
  417. log_event(EVENTLOG_WARNING_TYPE, event, service_name, value, milliseconds, 0);
  418. }
  419. else log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, value, error_string(GetLastError()), 0);
  420. }
  421. }
  422. else ok = true;
  423. if (! ok) *buffer = default_value;
  424. }
  425. HKEY open_service_registry(const TCHAR *service_name, REGSAM sam, bool must_exist) {
  426. /* Get registry */
  427. TCHAR registry[KEY_LENGTH];
  428. if (service_registry_path(service_name, false, 0, registry, _countof(registry)) < 0) {
  429. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, NSSM_REGISTRY, _T("open_service_registry()"), 0);
  430. return 0;
  431. }
  432. return open_registry_key(registry, sam, must_exist);
  433. }
  434. long open_registry(const TCHAR *service_name, const TCHAR *sub, REGSAM sam, HKEY *key, bool must_exist) {
  435. /* Get registry */
  436. TCHAR registry[KEY_LENGTH];
  437. if (service_registry_path(service_name, true, sub, registry, _countof(registry)) < 0) {
  438. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, NSSM_REGISTRY, _T("open_registry()"), 0);
  439. return 0;
  440. }
  441. return open_registry_key(registry, sam, key, must_exist);
  442. }
  443. HKEY open_registry(const TCHAR *service_name, const TCHAR *sub, REGSAM sam, bool must_exist) {
  444. HKEY key;
  445. long error = open_registry(service_name, sub, sam, &key, true);
  446. return key;
  447. }
  448. HKEY open_registry(const TCHAR *service_name, const TCHAR *sub, REGSAM sam) {
  449. return open_registry(service_name, sub, sam, true);
  450. }
  451. HKEY open_registry(const TCHAR *service_name, REGSAM sam) {
  452. return open_registry(service_name, 0, sam, true);
  453. }
  454. int get_io_parameters(nssm_service_t *service, HKEY key) {
  455. /* stdin */
  456. 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, 0)) {
  457. service->stdin_sharing = service->stdin_disposition = service->stdin_flags = 0;
  458. ZeroMemory(service->stdin_path, _countof(service->stdin_path) * sizeof(TCHAR));
  459. return 1;
  460. }
  461. /* stdout */
  462. 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, &service->stdout_copy_and_truncate)) {
  463. service->stdout_sharing = service->stdout_disposition = service->stdout_flags = 0;
  464. ZeroMemory(service->stdout_path, _countof(service->stdout_path) * sizeof(TCHAR));
  465. return 2;
  466. }
  467. /* stderr */
  468. if (get_createfile_parameters(key, NSSM_REG_STDERR, service->stderr_path, &service->stderr_sharing, NSSM_STDERR_SHARING, &service->stderr_disposition, NSSM_STDERR_DISPOSITION, &service->stderr_flags, NSSM_STDERR_FLAGS, &service->stderr_copy_and_truncate)) {
  469. service->stderr_sharing = service->stderr_disposition = service->stderr_flags = 0;
  470. ZeroMemory(service->stderr_path, _countof(service->stderr_path) * sizeof(TCHAR));
  471. return 3;
  472. }
  473. return 0;
  474. }
  475. int get_parameters(nssm_service_t *service, STARTUPINFO *si) {
  476. unsigned long ret;
  477. /* Try to open the registry */
  478. HKEY key = open_registry(service->name, KEY_READ);
  479. if (! key) return 1;
  480. /* Don't expand parameters when retrieving for the GUI. */
  481. bool expand = si ? true : false;
  482. /* Try to get environment variables - may fail */
  483. get_environment(service->name, key, NSSM_REG_ENV, &service->env, &service->envlen);
  484. /* Environment variables to add to existing rather than replace - may fail. */
  485. get_environment(service->name, key, NSSM_REG_ENV_EXTRA, &service->env_extra, &service->env_extralen);
  486. /* Set environment if we are starting the service. */
  487. if (si) set_service_environment(service);
  488. /* Try to get executable file - MUST succeed */
  489. if (get_string(key, NSSM_REG_EXE, service->exe, sizeof(service->exe), expand, false, true)) {
  490. RegCloseKey(key);
  491. return 3;
  492. }
  493. /* Try to get flags - may fail and we don't care */
  494. if (get_string(key, NSSM_REG_FLAGS, service->flags, sizeof(service->flags), expand, false, true)) {
  495. log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_NO_FLAGS, NSSM_REG_FLAGS, service->name, service->exe, 0);
  496. ZeroMemory(service->flags, sizeof(service->flags));
  497. }
  498. /* Try to get startup directory - may fail and we fall back to a default */
  499. if (get_string(key, NSSM_REG_DIR, service->dir, sizeof(service->dir), expand, true, true) || ! service->dir[0]) {
  500. _sntprintf_s(service->dir, _countof(service->dir), _TRUNCATE, _T("%s"), service->exe);
  501. strip_basename(service->dir);
  502. if (service->dir[0] == _T('\0')) {
  503. /* Help! */
  504. ret = GetWindowsDirectory(service->dir, sizeof(service->dir));
  505. if (! ret || ret > sizeof(service->dir)) {
  506. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_NO_DIR_AND_NO_FALLBACK, NSSM_REG_DIR, service->name, 0);
  507. RegCloseKey(key);
  508. return 4;
  509. }
  510. }
  511. log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_NO_DIR, NSSM_REG_DIR, service->name, service->dir, 0);
  512. }
  513. /* Try to get processor affinity - may fail. */
  514. TCHAR buffer[512];
  515. if (get_string(key, NSSM_REG_AFFINITY, buffer, sizeof(buffer), false, false, false) || ! buffer[0]) service->affinity = 0LL;
  516. else if (affinity_string_to_mask(buffer, &service->affinity)) {
  517. log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_BOGUS_AFFINITY_MASK, service->name, buffer);
  518. service->affinity = 0LL;
  519. }
  520. else {
  521. DWORD_PTR affinity, system_affinity;
  522. if (GetProcessAffinityMask(GetCurrentProcess(), &affinity, &system_affinity)) {
  523. _int64 effective_affinity = service->affinity & system_affinity;
  524. if (effective_affinity != service->affinity) {
  525. TCHAR *system = 0;
  526. if (! affinity_mask_to_string(system_affinity, &system)) {
  527. TCHAR *effective = 0;
  528. if (! affinity_mask_to_string(effective_affinity, &effective)) {
  529. log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_EFFECTIVE_AFFINITY_MASK, service->name, buffer, system, effective, 0);
  530. }
  531. HeapFree(GetProcessHeap(), 0, effective);
  532. }
  533. HeapFree(GetProcessHeap(), 0, system);
  534. }
  535. }
  536. }
  537. /* Try to get priority - may fail. */
  538. unsigned long priority;
  539. if (get_number(key, NSSM_REG_PRIORITY, &priority, false) == 1) {
  540. if (priority == (priority & priority_mask())) service->priority = priority;
  541. else log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_BOGUS_PRIORITY, service->name, NSSM_REG_PRIORITY, 0);
  542. }
  543. /* Try to get file rotation settings - may fail. */
  544. unsigned long rotate_files;
  545. if (get_number(key, NSSM_REG_ROTATE, &rotate_files, false) == 1) {
  546. if (rotate_files) service->rotate_files = true;
  547. else service->rotate_files = false;
  548. }
  549. else service->rotate_files = false;
  550. if (get_number(key, NSSM_REG_ROTATE_ONLINE, &rotate_files, false) == 1) {
  551. if (rotate_files) service->rotate_stdout_online = service->rotate_stderr_online = true;
  552. else service->rotate_stdout_online = service->rotate_stderr_online = false;
  553. }
  554. else service->rotate_stdout_online = service->rotate_stderr_online = false;
  555. if (get_number(key, NSSM_REG_ROTATE_SECONDS, &service->rotate_seconds, false) != 1) service->rotate_seconds = 0;
  556. if (get_number(key, NSSM_REG_ROTATE_BYTES_LOW, &service->rotate_bytes_low, false) != 1) service->rotate_bytes_low = 0;
  557. if (get_number(key, NSSM_REG_ROTATE_BYTES_HIGH, &service->rotate_bytes_high, false) != 1) service->rotate_bytes_high = 0;
  558. override_milliseconds(service->name, key, NSSM_REG_ROTATE_DELAY, &service->rotate_delay, NSSM_ROTATE_DELAY, NSSM_EVENT_BOGUS_THROTTLE);
  559. /* Try to get force new console setting - may fail. */
  560. if (get_number(key, NSSM_REG_NO_CONSOLE, &service->no_console, false) != 1) service->no_console = 0;
  561. /* Change to startup directory in case stdout/stderr are relative paths. */
  562. TCHAR cwd[PATH_LENGTH];
  563. GetCurrentDirectory(_countof(cwd), cwd);
  564. SetCurrentDirectory(service->dir);
  565. /* Try to get stdout and stderr */
  566. if (get_io_parameters(service, key)) {
  567. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_GET_OUTPUT_HANDLES_FAILED, service->name, 0);
  568. RegCloseKey(key);
  569. SetCurrentDirectory(cwd);
  570. return 5;
  571. }
  572. /* Change back in case the startup directory needs to be deleted. */
  573. SetCurrentDirectory(cwd);
  574. /* Try to get mandatory restart delay */
  575. override_milliseconds(service->name, key, NSSM_REG_RESTART_DELAY, &service->restart_delay, 0, NSSM_EVENT_BOGUS_RESTART_DELAY);
  576. /* Try to get throttle restart delay */
  577. override_milliseconds(service->name, key, NSSM_REG_THROTTLE, &service->throttle_delay, NSSM_RESET_THROTTLE_RESTART, NSSM_EVENT_BOGUS_THROTTLE);
  578. /* Try to get service stop flags. */
  579. unsigned long type = REG_DWORD;
  580. unsigned long stop_method_skip;
  581. unsigned long buflen = sizeof(stop_method_skip);
  582. bool stop_ok = false;
  583. ret = RegQueryValueEx(key, NSSM_REG_STOP_METHOD_SKIP, 0, &type, (unsigned char *) &stop_method_skip, &buflen);
  584. if (ret != ERROR_SUCCESS) {
  585. if (ret != ERROR_FILE_NOT_FOUND) {
  586. if (type != REG_DWORD) {
  587. log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_BOGUS_STOP_METHOD_SKIP, service->name, NSSM_REG_STOP_METHOD_SKIP, NSSM, 0);
  588. }
  589. else log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, NSSM_REG_STOP_METHOD_SKIP, error_string(GetLastError()), 0);
  590. }
  591. }
  592. else stop_ok = true;
  593. /* Try all methods except those requested to be skipped. */
  594. service->stop_method = ~0;
  595. if (stop_ok) service->stop_method &= ~stop_method_skip;
  596. /* Try to get kill delays - may fail. */
  597. 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);
  598. 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);
  599. 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);
  600. /* Try to get process tree settings - may fail. */
  601. unsigned long kill_process_tree;
  602. if (get_number(key, NSSM_REG_KILL_PROCESS_TREE, &kill_process_tree, false) == 1) {
  603. if (kill_process_tree) service->kill_process_tree = true;
  604. else service->kill_process_tree = false;
  605. }
  606. else service->kill_process_tree = true;
  607. /* Try to get default exit action. */
  608. bool default_action;
  609. service->default_exit_action = NSSM_EXIT_RESTART;
  610. TCHAR action_string[ACTION_LEN];
  611. if (! get_exit_action(service->name, 0, action_string, &default_action)) {
  612. for (int i = 0; exit_action_strings[i]; i++) {
  613. if (! _tcsnicmp((const TCHAR *) action_string, exit_action_strings[i], ACTION_LEN)) {
  614. service->default_exit_action = i;
  615. break;
  616. }
  617. }
  618. }
  619. /* Close registry */
  620. RegCloseKey(key);
  621. return 0;
  622. }
  623. /*
  624. Sets the string for the exit action corresponding to the exit code.
  625. ret is a pointer to an unsigned long containing the exit code.
  626. If ret is NULL, we retrieve the default exit action unconditionally.
  627. action is a buffer which receives the string.
  628. default_action is a pointer to a bool which is set to false if there
  629. was an explicit string for the given exit code, or true if we are
  630. returning the default action.
  631. Returns: 0 on success.
  632. 1 on error.
  633. */
  634. int get_exit_action(const TCHAR *service_name, unsigned long *ret, TCHAR *action, bool *default_action) {
  635. /* Are we returning the default action or a status-specific one? */
  636. *default_action = ! ret;
  637. /* Try to open the registry */
  638. HKEY key = open_registry(service_name, NSSM_REG_EXIT, KEY_READ);
  639. if (! key) return 1;
  640. unsigned long type = REG_SZ;
  641. unsigned long action_len = ACTION_LEN;
  642. TCHAR code[16];
  643. if (! ret) code[0] = _T('\0');
  644. else if (_sntprintf_s(code, _countof(code), _TRUNCATE, _T("%lu"), *ret) < 0) {
  645. RegCloseKey(key);
  646. return get_exit_action(service_name, 0, action, default_action);
  647. }
  648. if (RegQueryValueEx(key, code, 0, &type, (unsigned char *) action, &action_len) != ERROR_SUCCESS) {
  649. RegCloseKey(key);
  650. /* Try again with * as the key if an exit code was defined */
  651. if (ret) return get_exit_action(service_name, 0, action, default_action);
  652. return 0;
  653. }
  654. /* Close registry */
  655. RegCloseKey(key);
  656. return 0;
  657. }
  658. int set_hook(const TCHAR *service_name, const TCHAR *hook_event, const TCHAR *hook_action, TCHAR *cmd) {
  659. /* Try to open the registry */
  660. TCHAR registry[KEY_LENGTH];
  661. if (_sntprintf_s(registry, _countof(registry), _TRUNCATE, _T("%s\\%s"), NSSM_REG_HOOK, hook_event) < 0) {
  662. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("hook registry"), _T("set_hook()"), 0);
  663. return 1;
  664. }
  665. HKEY key;
  666. long error;
  667. /* Don't create keys needlessly. */
  668. if (! _tcslen(cmd)) {
  669. key = open_registry(service_name, registry, KEY_READ, false);
  670. if (! key) return 0;
  671. error = RegQueryValueEx(key, hook_action, 0, 0, 0, 0);
  672. RegCloseKey(key);
  673. if (error == ERROR_FILE_NOT_FOUND) return 0;
  674. }
  675. key = open_registry(service_name, registry, KEY_WRITE);
  676. if (! key) return 1;
  677. int ret = 1;
  678. if (_tcslen(cmd)) ret = set_string(key, (TCHAR *) hook_action, cmd, true);
  679. else {
  680. error = RegDeleteValue(key, hook_action);
  681. if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) ret = 0;
  682. }
  683. /* Close registry */
  684. RegCloseKey(key);
  685. return ret;
  686. }
  687. int get_hook(const TCHAR *service_name, const TCHAR *hook_event, const TCHAR *hook_action, TCHAR *buffer, unsigned long buflen) {
  688. /* Try to open the registry */
  689. TCHAR registry[KEY_LENGTH];
  690. if (_sntprintf_s(registry, _countof(registry), _TRUNCATE, _T("%s\\%s"), NSSM_REG_HOOK, hook_event) < 0) {
  691. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("hook registry"), _T("get_hook()"), 0);
  692. return 1;
  693. }
  694. HKEY key;
  695. long error = open_registry(service_name, registry, KEY_READ, &key, false);
  696. if (! key) {
  697. if (error == ERROR_FILE_NOT_FOUND) {
  698. ZeroMemory(buffer, buflen);
  699. return 0;
  700. }
  701. return 1;
  702. }
  703. int ret = expand_parameter(key, (TCHAR *) hook_action, buffer, buflen, true, false);
  704. /* Close registry */
  705. RegCloseKey(key);
  706. return ret;
  707. }