settings.cpp 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450
  1. #include "nssm.h"
  2. /* XXX: (value && value->string) is probably bogus because value is probably never null */
  3. /* Affinity. */
  4. #define NSSM_AFFINITY_ALL _T("All")
  5. /* Default value. */
  6. #define NSSM_DEFAULT_STRING _T("Default")
  7. extern const TCHAR *exit_action_strings[];
  8. extern const TCHAR *startup_strings[];
  9. extern const TCHAR *priority_strings[];
  10. extern const TCHAR *hook_event_strings[];
  11. extern const TCHAR *hook_action_strings[];
  12. /* Does the parameter refer to the default value of the setting? */
  13. static inline int is_default(const TCHAR *value) {
  14. return (str_equiv(value, NSSM_DEFAULT_STRING) || str_equiv(value, _T("*")) || ! value[0]);
  15. }
  16. /* What type of parameter is it parameter? */
  17. static inline bool is_string_type(const unsigned long type) {
  18. return (type == REG_MULTI_SZ || type == REG_EXPAND_SZ || type == REG_SZ);
  19. }
  20. static inline bool is_numeric_type(const unsigned long type) {
  21. return (type == REG_DWORD);
  22. }
  23. static int value_from_string(const TCHAR *name, value_t *value, const TCHAR *string) {
  24. size_t len = _tcslen(string);
  25. if (! len++) {
  26. value->string = 0;
  27. return 0;
  28. }
  29. value->string = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, len * sizeof(TCHAR));
  30. if (! value->string) {
  31. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, name, _T("value_from_string()"));
  32. return -1;
  33. }
  34. if (_sntprintf_s(value->string, len, _TRUNCATE, _T("%s"), string) < 0) {
  35. HeapFree(GetProcessHeap(), 0, value->string);
  36. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, name, _T("value_from_string()"));
  37. return -1;
  38. }
  39. return 1;
  40. }
  41. /* Functions to manage NSSM-specific settings in the registry. */
  42. static int setting_set_number(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  43. HKEY key = (HKEY) param;
  44. if (! key) return -1;
  45. unsigned long number;
  46. long error;
  47. /* Resetting to default? */
  48. if (! value || ! value->string) {
  49. error = RegDeleteValue(key, name);
  50. if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0;
  51. print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error));
  52. return -1;
  53. }
  54. if (str_number(value->string, &number)) return -1;
  55. if (default_value && number == PtrToUlong(default_value)) {
  56. error = RegDeleteValue(key, name);
  57. if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0;
  58. print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error));
  59. return -1;
  60. }
  61. if (set_number(key, (TCHAR *) name, number)) return -1;
  62. return 1;
  63. }
  64. static int setting_get_number(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  65. HKEY key = (HKEY) param;
  66. return get_number(key, (TCHAR *) name, &value->numeric, false);
  67. }
  68. static int setting_set_string(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  69. HKEY key = (HKEY) param;
  70. if (! key) return -1;
  71. long error;
  72. /* Resetting to default? */
  73. if (! value || ! value->string) {
  74. if (default_value) value->string = (TCHAR *) default_value;
  75. else {
  76. error = RegDeleteValue(key, name);
  77. if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0;
  78. print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error));
  79. return -1;
  80. }
  81. }
  82. if (default_value && _tcslen((TCHAR *) default_value) && str_equiv(value->string, (TCHAR *) default_value)) {
  83. error = RegDeleteValue(key, name);
  84. if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0;
  85. print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error));
  86. return -1;
  87. }
  88. if (set_expand_string(key, (TCHAR *) name, value->string)) return -1;
  89. return 1;
  90. }
  91. static int setting_get_string(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  92. HKEY key = (HKEY) param;
  93. TCHAR buffer[VALUE_LENGTH];
  94. if (get_string(key, (TCHAR *) name, (TCHAR *) buffer, (unsigned long) sizeof(buffer), false, false, false)) return -1;
  95. return value_from_string(name, value, buffer);
  96. }
  97. static int setting_not_dumpable(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  98. return 0;
  99. }
  100. static int setting_dump_string(const TCHAR *service_name, void *param, const TCHAR *name, const value_t *value, const TCHAR *additional) {
  101. TCHAR quoted_service_name[SERVICE_NAME_LENGTH * 2];
  102. TCHAR quoted_value[VALUE_LENGTH * 2];
  103. TCHAR quoted_additional[VALUE_LENGTH * 2];
  104. TCHAR quoted_nssm[EXE_LENGTH * 2];
  105. if (quote(service_name, quoted_service_name, _countof(quoted_service_name))) return 1;
  106. if (additional) {
  107. if (_tcslen(additional)) {
  108. if (quote(additional, quoted_additional, _countof(quoted_additional))) return 3;
  109. }
  110. else _sntprintf_s(quoted_additional, _countof(quoted_additional), _TRUNCATE, _T("\"\""));
  111. }
  112. else quoted_additional[0] = _T('\0');
  113. unsigned long type = (unsigned long) param;
  114. if (is_string_type(type)) {
  115. if (_tcslen(value->string)) {
  116. if (quote(value->string, quoted_value, _countof(quoted_value))) return 2;
  117. }
  118. else _sntprintf_s(quoted_value, _countof(quoted_value), _TRUNCATE, _T("\"\""));
  119. }
  120. else if (is_numeric_type(type)) _sntprintf_s(quoted_value, _countof(quoted_value), _TRUNCATE, _T("%lu"), value->numeric);
  121. else return 2;
  122. if (quote(nssm_exe(), quoted_nssm, _countof(quoted_nssm))) return 3;
  123. if (_tcslen(quoted_additional)) _tprintf(_T("%s set %s %s %s %s\n"), quoted_nssm, quoted_service_name, name, quoted_additional, quoted_value);
  124. else _tprintf(_T("%s set %s %s %s\n"), quoted_nssm, quoted_service_name, name, quoted_value);
  125. return 0;
  126. }
  127. static int setting_set_exit_action(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  128. unsigned long exitcode;
  129. TCHAR *code;
  130. TCHAR action_string[ACTION_LEN];
  131. if (additional) {
  132. /* Default action? */
  133. if (is_default(additional)) code = 0;
  134. else {
  135. if (str_number(additional, &exitcode)) return -1;
  136. code = (TCHAR *) additional;
  137. }
  138. }
  139. HKEY key = open_registry(service_name, name, KEY_WRITE);
  140. if (! key) return -1;
  141. long error;
  142. int ret = 1;
  143. /* Resetting to default? */
  144. if (value && value->string) _sntprintf_s(action_string, _countof(action_string), _TRUNCATE, _T("%s"), value->string);
  145. else {
  146. if (code) {
  147. /* Delete explicit action. */
  148. error = RegDeleteValue(key, code);
  149. RegCloseKey(key);
  150. if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0;
  151. print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, code, service_name, error_string(error));
  152. return -1;
  153. }
  154. else {
  155. /* Explicitly keep the default action. */
  156. if (default_value) _sntprintf_s(action_string, _countof(action_string), _TRUNCATE, _T("%s"), (TCHAR *) default_value);
  157. ret = 0;
  158. }
  159. }
  160. /* Validate the string. */
  161. for (int i = 0; exit_action_strings[i]; i++) {
  162. if (! _tcsnicmp((const TCHAR *) action_string, exit_action_strings[i], ACTION_LEN)) {
  163. if (default_value && str_equiv(action_string, (TCHAR *) default_value)) ret = 0;
  164. if (RegSetValueEx(key, code, 0, REG_SZ, (const unsigned char *) exit_action_strings[i], (unsigned long) (_tcslen(action_string) + 1) * sizeof(TCHAR)) != ERROR_SUCCESS) {
  165. print_message(stderr, NSSM_MESSAGE_SETVALUE_FAILED, code, service_name, error_string(GetLastError()));
  166. RegCloseKey(key);
  167. return -1;
  168. }
  169. RegCloseKey(key);
  170. return ret;
  171. }
  172. }
  173. print_message(stderr, NSSM_MESSAGE_INVALID_EXIT_ACTION, action_string);
  174. for (int i = 0; exit_action_strings[i]; i++) _ftprintf(stderr, _T("%s\n"), exit_action_strings[i]);
  175. return -1;
  176. }
  177. static int setting_get_exit_action(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  178. unsigned long exitcode = 0;
  179. unsigned long *code = 0;
  180. if (additional) {
  181. if (! is_default(additional)) {
  182. if (str_number(additional, &exitcode)) return -1;
  183. code = &exitcode;
  184. }
  185. }
  186. TCHAR action_string[ACTION_LEN];
  187. bool default_action;
  188. if (get_exit_action(service_name, code, action_string, &default_action)) return -1;
  189. value_from_string(name, value, action_string);
  190. if (default_action && ! _tcsnicmp((const TCHAR *) action_string, (TCHAR *) default_value, ACTION_LEN)) return 0;
  191. return 1;
  192. }
  193. static int setting_dump_exit_action(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  194. int errors = 0;
  195. HKEY key = open_registry(service_name, NSSM_REG_EXIT, KEY_READ);
  196. if (! key) return -1;
  197. TCHAR code[16];
  198. unsigned long index = 0;
  199. while (true) {
  200. int ret = enumerate_registry_values(key, &index, code, _countof(code));
  201. if (ret == ERROR_NO_MORE_ITEMS) break;
  202. if (ret != ERROR_SUCCESS) continue;
  203. bool valid = true;
  204. int i;
  205. for (i = 0; i < _countof(code); i++) {
  206. if (! code[i]) break;
  207. if (code[i] >= _T('0') || code[i] <= _T('9')) continue;
  208. valid = false;
  209. break;
  210. }
  211. if (! valid) continue;
  212. TCHAR *additional = (code[i]) ? code : NSSM_DEFAULT_STRING;
  213. ret = setting_get_exit_action(service_name, 0, name, default_value, value, additional);
  214. if (ret == 1) {
  215. if (setting_dump_string(service_name, (void *) REG_SZ, name, value, additional)) errors++;
  216. }
  217. else if (ret < 0) errors++;
  218. }
  219. if (errors) return -1;
  220. return 0;
  221. }
  222. static inline bool split_hook_name(const TCHAR *hook_name, TCHAR *hook_event, TCHAR *hook_action) {
  223. TCHAR *s;
  224. for (s = (TCHAR *) hook_name; *s; s++) {
  225. if (*s == _T('/')) {
  226. *s = _T('\0');
  227. _sntprintf_s(hook_event, HOOK_NAME_LENGTH, _TRUNCATE, _T("%s"), hook_name);
  228. *s++ = _T('/');
  229. _sntprintf_s(hook_action, HOOK_NAME_LENGTH, _TRUNCATE, _T("%s"), s);
  230. return valid_hook_name(hook_event, hook_action, false);
  231. }
  232. }
  233. print_message(stderr, NSSM_MESSAGE_INVALID_HOOK_NAME, hook_name);
  234. return false;
  235. }
  236. static int setting_set_hook(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  237. TCHAR hook_event[HOOK_NAME_LENGTH];
  238. TCHAR hook_action[HOOK_NAME_LENGTH];
  239. if (! split_hook_name(additional, hook_event, hook_action)) return -1;
  240. TCHAR *cmd;
  241. if (value && value->string) cmd = value->string;
  242. else cmd = _T("");
  243. if (set_hook(service_name, hook_event, hook_action, cmd)) return -1;
  244. if (! _tcslen(cmd)) return 0;
  245. return 1;
  246. }
  247. static int setting_get_hook(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  248. TCHAR hook_event[HOOK_NAME_LENGTH];
  249. TCHAR hook_action[HOOK_NAME_LENGTH];
  250. if (! split_hook_name(additional, hook_event, hook_action)) return -1;
  251. TCHAR cmd[CMD_LENGTH];
  252. if (get_hook(service_name, hook_event, hook_action, cmd, sizeof(cmd))) return -1;
  253. value_from_string(name, value, cmd);
  254. if (! _tcslen(cmd)) return 0;
  255. return 1;
  256. }
  257. static int setting_dump_hooks(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  258. int i, j;
  259. int errors = 0;
  260. for (i = 0; hook_event_strings[i]; i++) {
  261. const TCHAR *hook_event = hook_event_strings[i];
  262. for (j = 0; hook_action_strings[j]; j++) {
  263. const TCHAR *hook_action = hook_action_strings[j];
  264. if (! valid_hook_name(hook_event, hook_action, true)) continue;
  265. TCHAR hook_name[HOOK_NAME_LENGTH];
  266. _sntprintf_s(hook_name, _countof(hook_name), _TRUNCATE, _T("%s/%s"), hook_event, hook_action);
  267. int ret = setting_get_hook(service_name, param, name, default_value, value, hook_name);
  268. if (ret != 1) {
  269. if (ret < 0) errors++;
  270. continue;
  271. }
  272. if (setting_dump_string(service_name, (void *) REG_SZ, name, value, hook_name)) errors++;
  273. }
  274. }
  275. if (errors) return -1;
  276. return 0;
  277. }
  278. static int setting_set_affinity(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  279. HKEY key = (HKEY) param;
  280. if (! key) return -1;
  281. long error;
  282. __int64 mask;
  283. __int64 system_affinity = 0LL;
  284. if (value && value->string) {
  285. DWORD_PTR affinity;
  286. if (! GetProcessAffinityMask(GetCurrentProcess(), &affinity, (DWORD_PTR *) &system_affinity)) system_affinity = ~0;
  287. if (is_default(value->string) || str_equiv(value->string, NSSM_AFFINITY_ALL)) mask = 0LL;
  288. else if (affinity_string_to_mask(value->string, &mask)) {
  289. print_message(stderr, NSSM_MESSAGE_BOGUS_AFFINITY_MASK, value->string, num_cpus() - 1);
  290. return -1;
  291. }
  292. }
  293. else mask = 0LL;
  294. if (! mask) {
  295. error = RegDeleteValue(key, name);
  296. if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0;
  297. print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error));
  298. return -1;
  299. }
  300. /* Canonicalise. */
  301. TCHAR *canon = 0;
  302. if (affinity_mask_to_string(mask, &canon)) canon = value->string;
  303. __int64 effective_affinity = mask & system_affinity;
  304. if (effective_affinity != mask) {
  305. /* Requested CPUs did not intersect with available CPUs? */
  306. if (! effective_affinity) mask = effective_affinity = system_affinity;
  307. TCHAR *system = 0;
  308. if (! affinity_mask_to_string(system_affinity, &system)) {
  309. TCHAR *effective = 0;
  310. if (! affinity_mask_to_string(effective_affinity, &effective)) {
  311. print_message(stderr, NSSM_MESSAGE_EFFECTIVE_AFFINITY_MASK, value->string, system, effective);
  312. HeapFree(GetProcessHeap(), 0, effective);
  313. }
  314. HeapFree(GetProcessHeap(), 0, system);
  315. }
  316. }
  317. if (RegSetValueEx(key, name, 0, REG_SZ, (const unsigned char *) canon, (unsigned long) (_tcslen(canon) + 1) * sizeof(TCHAR)) != ERROR_SUCCESS) {
  318. if (canon != value->string) HeapFree(GetProcessHeap(), 0, canon);
  319. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, name, error_string(GetLastError()), 0);
  320. return -1;
  321. }
  322. if (canon != value->string) HeapFree(GetProcessHeap(), 0, canon);
  323. return 1;
  324. }
  325. static int setting_get_affinity(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  326. HKEY key = (HKEY) param;
  327. if (! key) return -1;
  328. unsigned long type;
  329. TCHAR *buffer = 0;
  330. unsigned long buflen = 0;
  331. int ret = RegQueryValueEx(key, name, 0, &type, 0, &buflen);
  332. if (ret == ERROR_FILE_NOT_FOUND) {
  333. if (value_from_string(name, value, NSSM_AFFINITY_ALL) == 1) return 0;
  334. return -1;
  335. }
  336. if (ret != ERROR_SUCCESS) return -1;
  337. if (type != REG_SZ) return -1;
  338. buffer = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, buflen);
  339. if (! buffer) {
  340. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("affinity"), _T("setting_get_affinity"));
  341. return -1;
  342. }
  343. if (get_string(key, (TCHAR *) name, buffer, buflen, false, false, true)) {
  344. HeapFree(GetProcessHeap(), 0, buffer);
  345. return -1;
  346. }
  347. __int64 affinity;
  348. if (affinity_string_to_mask(buffer, &affinity)) {
  349. print_message(stderr, NSSM_MESSAGE_BOGUS_AFFINITY_MASK, buffer, num_cpus() - 1);
  350. HeapFree(GetProcessHeap(), 0, buffer);
  351. return -1;
  352. }
  353. HeapFree(GetProcessHeap(), 0, buffer);
  354. /* Canonicalise. */
  355. if (affinity_mask_to_string(affinity, &buffer)) {
  356. if (buffer) HeapFree(GetProcessHeap(), 0, buffer);
  357. return -1;
  358. }
  359. ret = value_from_string(name, value, buffer);
  360. HeapFree(GetProcessHeap(), 0, buffer);
  361. return ret;
  362. }
  363. static int setting_set_environment(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  364. HKEY key = (HKEY) param;
  365. if (! param) return -1;
  366. TCHAR *string = 0;
  367. TCHAR *unformatted = 0;
  368. unsigned long envlen;
  369. unsigned long newlen = 0;
  370. int op = 0;
  371. if (value && value->string && value->string[0]) {
  372. string = value->string;
  373. switch (string[0]) {
  374. case _T('+'): op = 1; break;
  375. case _T('-'): op = -1; break;
  376. case _T(':'): string++; break;
  377. }
  378. }
  379. if (op) {
  380. string++;
  381. TCHAR *env = 0;
  382. if (get_environment((TCHAR *) service_name, key, (TCHAR *) name, &env, &envlen)) return -1;
  383. if (env) {
  384. int ret;
  385. if (op > 0) ret = append_to_environment_block(env, envlen, string, &unformatted, &newlen);
  386. else ret = remove_from_environment_block(env, envlen, string, &unformatted, &newlen);
  387. if (envlen) HeapFree(GetProcessHeap(), 0, env);
  388. if (ret) return -1;
  389. string = unformatted;
  390. }
  391. else {
  392. /*
  393. No existing environment.
  394. We can't remove from an empty environment so just treat an add
  395. operation as setting a new string.
  396. */
  397. if (op < 0) return 0;
  398. op = 0;
  399. }
  400. }
  401. if (! string || ! string[0]) {
  402. long error = RegDeleteValue(key, name);
  403. if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0;
  404. print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error));
  405. return -1;
  406. }
  407. if (! op) {
  408. if (unformat_double_null(string, (unsigned long) _tcslen(string), &unformatted, &newlen)) return -1;
  409. }
  410. if (test_environment(unformatted)) {
  411. HeapFree(GetProcessHeap(), 0, unformatted);
  412. print_message(stderr, NSSM_GUI_INVALID_ENVIRONMENT);
  413. return -1;
  414. }
  415. if (RegSetValueEx(key, name, 0, REG_MULTI_SZ, (const unsigned char *) unformatted, (unsigned long) newlen * sizeof(TCHAR)) != ERROR_SUCCESS) {
  416. if (newlen) HeapFree(GetProcessHeap(), 0, unformatted);
  417. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, NSSM_REG_ENV, error_string(GetLastError()), 0);
  418. return -1;
  419. }
  420. if (newlen) HeapFree(GetProcessHeap(), 0, unformatted);
  421. return 1;
  422. }
  423. static int setting_get_environment(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  424. HKEY key = (HKEY) param;
  425. if (! param) return -1;
  426. TCHAR *env = 0;
  427. unsigned long envlen;
  428. if (get_environment((TCHAR *) service_name, key, (TCHAR *) name, &env, &envlen)) return -1;
  429. if (! envlen) return 0;
  430. TCHAR *formatted;
  431. unsigned long newlen;
  432. if (format_double_null(env, envlen, &formatted, &newlen)) return -1;
  433. int ret;
  434. if (additional) {
  435. /* Find named environment variable. */
  436. TCHAR *s;
  437. size_t len = _tcslen(additional);
  438. for (s = env; *s; s++) {
  439. /* Look for <additional>=<string> NULL NULL */
  440. if (! _tcsnicmp(s, additional, len) && s[len] == _T('=')) {
  441. /* Strip <key>= */
  442. s += len + 1;
  443. ret = value_from_string(name, value, s);
  444. HeapFree(GetProcessHeap(), 0, env);
  445. return ret;
  446. }
  447. /* Skip this string. */
  448. for ( ; *s; s++);
  449. }
  450. HeapFree(GetProcessHeap(), 0, env);
  451. return 0;
  452. }
  453. HeapFree(GetProcessHeap(), 0, env);
  454. ret = value_from_string(name, value, formatted);
  455. if (newlen) HeapFree(GetProcessHeap(), 0, formatted);
  456. return ret;
  457. }
  458. static int setting_dump_environment(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  459. int errors = 0;
  460. HKEY key = (HKEY) param;
  461. if (! param) return -1;
  462. TCHAR *env = 0;
  463. unsigned long envlen;
  464. if (get_environment((TCHAR *) service_name, key, (TCHAR *) name, &env, &envlen)) return -1;
  465. if (! envlen) return 0;
  466. TCHAR *s;
  467. for (s = env; *s; s++) {
  468. size_t len = _tcslen(s) + 2;
  469. value->string = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, len * sizeof(TCHAR));
  470. if (! value->string) {
  471. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("dump"), _T("setting_dump_environment"));
  472. break;
  473. }
  474. _sntprintf_s(value->string, len, _TRUNCATE, _T("%c%s"), (s > env) ? _T('+') : _T(':'), s);
  475. if (setting_dump_string(service_name, (void *) REG_SZ, name, value, 0)) errors++;
  476. HeapFree(GetProcessHeap(), 0, value->string);
  477. value->string = 0;
  478. for ( ; *s; s++);
  479. }
  480. HeapFree(GetProcessHeap(), 0, env);
  481. if (errors) return 1;
  482. return 0;
  483. }
  484. static int setting_set_priority(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  485. HKEY key = (HKEY) param;
  486. if (! param) return -1;
  487. TCHAR *priority_string;
  488. int i;
  489. long error;
  490. if (value && value->string) priority_string = value->string;
  491. else if (default_value) priority_string = (TCHAR *) default_value;
  492. else {
  493. error = RegDeleteValue(key, name);
  494. if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0;
  495. print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error));
  496. return -1;
  497. }
  498. for (i = 0; priority_strings[i]; i++) {
  499. if (! str_equiv(priority_strings[i], priority_string)) continue;
  500. if (default_value && str_equiv(priority_string, (TCHAR *) default_value)) {
  501. error = RegDeleteValue(key, name);
  502. if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0;
  503. print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error));
  504. return -1;
  505. }
  506. if (set_number(key, (TCHAR *) name, priority_index_to_constant(i))) return -1;
  507. return 1;
  508. }
  509. print_message(stderr, NSSM_MESSAGE_INVALID_PRIORITY, priority_string);
  510. for (i = 0; priority_strings[i]; i++) _ftprintf(stderr, _T("%s\n"), priority_strings[i]);
  511. return -1;
  512. }
  513. static int setting_get_priority(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  514. HKEY key = (HKEY) param;
  515. if (! param) return -1;
  516. unsigned long constant;
  517. switch (get_number(key, (TCHAR *) name, &constant, false)) {
  518. case 0:
  519. if (value_from_string(name, value, (const TCHAR *) default_value) == -1) return -1;
  520. return 0;
  521. case -1: return -1;
  522. }
  523. return value_from_string(name, value, priority_strings[priority_constant_to_index(constant)]);
  524. }
  525. static int setting_dump_priority(const TCHAR *service_name, void *key_ptr, const TCHAR *name, void *setting_ptr, value_t *value, const TCHAR *additional) {
  526. settings_t *setting = (settings_t *) setting_ptr;
  527. int ret = setting_get_priority(service_name, key_ptr, name, (void *) setting->default_value, value, 0);
  528. if (ret != 1) return ret;
  529. return setting_dump_string(service_name, (void *) REG_SZ, name, value, 0);
  530. }
  531. /* Functions to manage native service settings. */
  532. static int native_set_dependon(const TCHAR *service_name, SC_HANDLE service_handle, TCHAR **dependencies, unsigned long *dependencieslen, value_t *value, int type) {
  533. *dependencieslen = 0;
  534. if (! value || ! value->string || ! value->string[0]) return 0;
  535. TCHAR *string = value->string;
  536. unsigned long buflen;
  537. int op = 0;
  538. switch (string[0]) {
  539. case _T('+'): op = 1; break;
  540. case _T('-'): op = -1; break;
  541. case _T(':'): string++; break;
  542. }
  543. if (op) {
  544. string++;
  545. TCHAR *buffer = 0;
  546. if (get_service_dependencies(service_name, service_handle, &buffer, &buflen, type)) return -1;
  547. if (buffer) {
  548. int ret;
  549. if (op > 0) ret = append_to_dependencies(buffer, buflen, string, dependencies, dependencieslen, type);
  550. else ret = remove_from_dependencies(buffer, buflen, string, dependencies, dependencieslen, type);
  551. if (buflen) HeapFree(GetProcessHeap(), 0, buffer);
  552. return ret;
  553. }
  554. else {
  555. /*
  556. No existing list.
  557. We can't remove from an empty list so just treat an add
  558. operation as setting a new string.
  559. */
  560. if (op < 0) return 0;
  561. op = 0;
  562. }
  563. }
  564. if (! op) {
  565. TCHAR *unformatted = 0;
  566. unsigned long newlen;
  567. if (unformat_double_null(string, (unsigned long) _tcslen(string), &unformatted, &newlen)) return -1;
  568. if (type == DEPENDENCY_GROUPS) {
  569. /* Prepend group identifier. */
  570. unsigned long missing = 0;
  571. TCHAR *canon = unformatted;
  572. size_t canonlen = 0;
  573. TCHAR *s;
  574. for (s = unformatted; *s; s++) {
  575. if (*s != SC_GROUP_IDENTIFIER) missing++;
  576. size_t len = _tcslen(s);
  577. canonlen += len + 1;
  578. s += len;
  579. }
  580. if (missing) {
  581. /* Missing identifiers plus double NULL terminator. */
  582. canonlen += missing + 1;
  583. canon = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, canonlen * sizeof(TCHAR));
  584. if (! canon) {
  585. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("canon"), _T("native_set_dependon"));
  586. if (unformatted) HeapFree(GetProcessHeap(), 0, unformatted);
  587. return -1;
  588. }
  589. size_t i = 0;
  590. for (s = unformatted; *s; s++) {
  591. if (*s != SC_GROUP_IDENTIFIER) canon[i++] = SC_GROUP_IDENTIFIER;
  592. size_t len = _tcslen(s);
  593. memmove(canon + i, s, (len + 1) * sizeof(TCHAR));
  594. i += len + 1;
  595. s += len;
  596. }
  597. HeapFree(GetProcessHeap(), 0, unformatted);
  598. unformatted = canon;
  599. newlen = (unsigned long) canonlen;
  600. }
  601. }
  602. *dependencies = unformatted;
  603. *dependencieslen = newlen;
  604. }
  605. return 0;
  606. }
  607. static int native_set_dependongroup(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  608. SC_HANDLE service_handle = (SC_HANDLE) param;
  609. if (! service_handle) return -1;
  610. /*
  611. Get existing service dependencies because we must set both types together.
  612. */
  613. TCHAR *services_buffer;
  614. unsigned long services_buflen;
  615. if (get_service_dependencies(service_name, service_handle, &services_buffer, &services_buflen, DEPENDENCY_SERVICES)) return -1;
  616. if (! value || ! value->string || ! value->string[0]) {
  617. if (! ChangeServiceConfig(service_handle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, 0, 0, 0, services_buffer, 0, 0, 0)) {
  618. print_message(stderr, NSSM_MESSAGE_CHANGESERVICECONFIG_FAILED, error_string(GetLastError()));
  619. if (services_buffer) HeapFree(GetProcessHeap(), 0, services_buffer);
  620. return -1;
  621. }
  622. if (services_buffer) HeapFree(GetProcessHeap(), 0, services_buffer);
  623. return 0;
  624. }
  625. /* Update the group list. */
  626. TCHAR *groups_buffer;
  627. unsigned long groups_buflen;
  628. if (native_set_dependon(service_name, service_handle, &groups_buffer, &groups_buflen, value, DEPENDENCY_GROUPS)) return -1;
  629. TCHAR *dependencies;
  630. if (services_buflen > 2) {
  631. dependencies = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, (groups_buflen + services_buflen) * sizeof(TCHAR));
  632. if (! dependencies) {
  633. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("dependencies"), _T("native_set_dependongroup"));
  634. if (groups_buffer) HeapFree(GetProcessHeap(), 0, groups_buffer);
  635. if (services_buffer) HeapFree(GetProcessHeap(), 0, services_buffer);
  636. return -1;
  637. }
  638. memmove(dependencies, services_buffer, services_buflen * sizeof(TCHAR));
  639. memmove(dependencies + services_buflen - 1, groups_buffer, groups_buflen * sizeof(TCHAR));
  640. }
  641. else dependencies = groups_buffer;
  642. int ret = 1;
  643. if (set_service_dependencies(service_name, service_handle, dependencies)) ret = -1;
  644. if (dependencies != groups_buffer) HeapFree(GetProcessHeap(), 0, dependencies);
  645. if (groups_buffer) HeapFree(GetProcessHeap(), 0, groups_buffer);
  646. if (services_buffer) HeapFree(GetProcessHeap(), 0, services_buffer);
  647. return ret;
  648. }
  649. static int native_get_dependongroup(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  650. SC_HANDLE service_handle = (SC_HANDLE) param;
  651. if (! service_handle) return -1;
  652. TCHAR *buffer;
  653. unsigned long buflen;
  654. if (get_service_dependencies(service_name, service_handle, &buffer, &buflen, DEPENDENCY_GROUPS)) return -1;
  655. int ret;
  656. if (buflen) {
  657. TCHAR *formatted;
  658. unsigned long newlen;
  659. if (format_double_null(buffer, buflen, &formatted, &newlen)) {
  660. HeapFree(GetProcessHeap(), 0, buffer);
  661. return -1;
  662. }
  663. ret = value_from_string(name, value, formatted);
  664. HeapFree(GetProcessHeap(), 0, formatted);
  665. HeapFree(GetProcessHeap(), 0, buffer);
  666. }
  667. else {
  668. value->string = 0;
  669. ret = 0;
  670. }
  671. return ret;
  672. }
  673. static int setting_dump_dependon(const TCHAR *service_name, SC_HANDLE service_handle, const TCHAR *name, int type, value_t *value) {
  674. int errors = 0;
  675. TCHAR *dependencies = 0;
  676. unsigned long dependencieslen;
  677. if (get_service_dependencies(service_name, service_handle, &dependencies, &dependencieslen, type)) return -1;
  678. if (! dependencieslen) return 0;
  679. TCHAR *s;
  680. for (s = dependencies; *s; s++) {
  681. size_t len = _tcslen(s) + 2;
  682. value->string = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, len * sizeof(TCHAR));
  683. if (! value->string) {
  684. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("dump"), _T("setting_dump_dependon"));
  685. break;
  686. }
  687. _sntprintf_s(value->string, len, _TRUNCATE, _T("%c%s"), (s > dependencies) ? _T('+') : _T(':'), s);
  688. if (setting_dump_string(service_name, (void *) REG_SZ, name, value, 0)) errors++;
  689. HeapFree(GetProcessHeap(), 0, value->string);
  690. value->string = 0;
  691. for ( ; *s; s++);
  692. }
  693. HeapFree(GetProcessHeap(), 0, dependencies);
  694. if (errors) return 1;
  695. return 0;
  696. }
  697. static int native_dump_dependongroup(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  698. return setting_dump_dependon(service_name, (SC_HANDLE) param, name, DEPENDENCY_GROUPS, value);
  699. }
  700. static int native_set_dependonservice(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  701. SC_HANDLE service_handle = (SC_HANDLE) param;
  702. if (! service_handle) return -1;
  703. /*
  704. Get existing group dependencies because we must set both types together.
  705. */
  706. TCHAR *groups_buffer;
  707. unsigned long groups_buflen;
  708. if (get_service_dependencies(service_name, service_handle, &groups_buffer, &groups_buflen, DEPENDENCY_GROUPS)) return -1;
  709. if (! value || ! value->string || ! value->string[0]) {
  710. if (! ChangeServiceConfig(service_handle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, 0, 0, 0, groups_buffer, 0, 0, 0)) {
  711. print_message(stderr, NSSM_MESSAGE_CHANGESERVICECONFIG_FAILED, error_string(GetLastError()));
  712. if (groups_buffer) HeapFree(GetProcessHeap(), 0, groups_buffer);
  713. return -1;
  714. }
  715. if (groups_buffer) HeapFree(GetProcessHeap(), 0, groups_buffer);
  716. return 0;
  717. }
  718. /* Update the service list. */
  719. TCHAR *services_buffer;
  720. unsigned long services_buflen;
  721. if (native_set_dependon(service_name, service_handle, &services_buffer, &services_buflen, value, DEPENDENCY_SERVICES)) return -1;
  722. TCHAR *dependencies;
  723. if (groups_buflen > 2) {
  724. dependencies = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, (services_buflen + groups_buflen) * sizeof(TCHAR));
  725. if (! dependencies) {
  726. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("dependencies"), _T("native_set_dependonservice"));
  727. if (groups_buffer) HeapFree(GetProcessHeap(), 0, groups_buffer);
  728. if (services_buffer) HeapFree(GetProcessHeap(), 0, services_buffer);
  729. return -1;
  730. }
  731. memmove(dependencies, services_buffer, services_buflen * sizeof(TCHAR));
  732. memmove(dependencies + services_buflen - 1, groups_buffer, groups_buflen * sizeof(TCHAR));
  733. }
  734. else dependencies = services_buffer;
  735. int ret = 1;
  736. if (set_service_dependencies(service_name, service_handle, dependencies)) ret = -1;
  737. if (dependencies != services_buffer) HeapFree(GetProcessHeap(), 0, dependencies);
  738. if (groups_buffer) HeapFree(GetProcessHeap(), 0, groups_buffer);
  739. if (services_buffer) HeapFree(GetProcessHeap(), 0, services_buffer);
  740. return ret;
  741. }
  742. static int native_get_dependonservice(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  743. SC_HANDLE service_handle = (SC_HANDLE) param;
  744. if (! service_handle) return -1;
  745. TCHAR *buffer;
  746. unsigned long buflen;
  747. if (get_service_dependencies(service_name, service_handle, &buffer, &buflen, DEPENDENCY_SERVICES)) return -1;
  748. int ret;
  749. if (buflen) {
  750. TCHAR *formatted;
  751. unsigned long newlen;
  752. if (format_double_null(buffer, buflen, &formatted, &newlen)) {
  753. HeapFree(GetProcessHeap(), 0, buffer);
  754. return -1;
  755. }
  756. ret = value_from_string(name, value, formatted);
  757. HeapFree(GetProcessHeap(), 0, formatted);
  758. HeapFree(GetProcessHeap(), 0, buffer);
  759. }
  760. else {
  761. value->string = 0;
  762. ret = 0;
  763. }
  764. return ret;
  765. }
  766. static int native_dump_dependonservice(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  767. return setting_dump_dependon(service_name, (SC_HANDLE) param, name, DEPENDENCY_SERVICES, value);
  768. }
  769. int native_set_description(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  770. SC_HANDLE service_handle = (SC_HANDLE) param;
  771. if (! service_handle) return -1;
  772. TCHAR *description = 0;
  773. if (value) description = value->string;
  774. if (set_service_description(service_name, service_handle, description)) return -1;
  775. if (description && description[0]) return 1;
  776. return 0;
  777. }
  778. int native_get_description(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  779. SC_HANDLE service_handle = (SC_HANDLE) param;
  780. if (! service_handle) return -1;
  781. TCHAR buffer[VALUE_LENGTH];
  782. if (get_service_description(service_name, service_handle, _countof(buffer), buffer)) return -1;
  783. if (buffer[0]) return value_from_string(name, value, buffer);
  784. value->string = 0;
  785. return 0;
  786. }
  787. int native_set_displayname(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  788. SC_HANDLE service_handle = (SC_HANDLE) param;
  789. if (! service_handle) return -1;
  790. TCHAR *displayname = 0;
  791. if (value && value->string) displayname = value->string;
  792. else displayname = (TCHAR *) service_name;
  793. if (! ChangeServiceConfig(service_handle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, 0, 0, 0, 0, 0, 0, displayname)) {
  794. print_message(stderr, NSSM_MESSAGE_CHANGESERVICECONFIG_FAILED, error_string(GetLastError()));
  795. return -1;
  796. }
  797. /*
  798. If the display name and service name differ only in case,
  799. ChangeServiceConfig() will return success but the display name will be
  800. set to the service name, NOT the value passed to the function.
  801. This appears to be a quirk of Windows rather than a bug here.
  802. */
  803. if (displayname != service_name && ! str_equiv(displayname, service_name)) return 1;
  804. return 0;
  805. }
  806. int native_get_displayname(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  807. SC_HANDLE service_handle = (SC_HANDLE) param;
  808. if (! service_handle) return -1;
  809. QUERY_SERVICE_CONFIG *qsc = query_service_config(service_name, service_handle);
  810. if (! qsc) return -1;
  811. int ret = value_from_string(name, value, qsc->lpDisplayName);
  812. HeapFree(GetProcessHeap(), 0, qsc);
  813. return ret;
  814. }
  815. int native_set_environment(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  816. HKEY key = open_service_registry(service_name, KEY_SET_VALUE, true);
  817. if (! key) return -1;
  818. int ret = setting_set_environment(service_name, (void *) key, name, default_value, value, additional);
  819. RegCloseKey(key);
  820. return ret;
  821. }
  822. int native_get_environment(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  823. HKEY key = open_service_registry(service_name, KEY_READ, true);
  824. if (! key) return -1;
  825. ZeroMemory(value, sizeof(value_t));
  826. int ret = setting_get_environment(service_name, (void *) key, name, default_value, value, additional);
  827. RegCloseKey(key);
  828. return ret;
  829. }
  830. static int native_dump_environment(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  831. HKEY key = open_service_registry(service_name, KEY_READ, true);
  832. if (! key) return -1;
  833. int ret = setting_dump_environment(service_name, (void *) key, name, default_value, value, additional);
  834. RegCloseKey(key);
  835. return ret;
  836. }
  837. int native_set_imagepath(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  838. SC_HANDLE service_handle = (SC_HANDLE) param;
  839. if (! service_handle) return -1;
  840. /* It makes no sense to try to reset the image path. */
  841. if (! value || ! value->string) {
  842. print_message(stderr, NSSM_MESSAGE_NO_DEFAULT_VALUE, name);
  843. return -1;
  844. }
  845. if (! ChangeServiceConfig(service_handle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, value->string, 0, 0, 0, 0, 0, 0)) {
  846. print_message(stderr, NSSM_MESSAGE_CHANGESERVICECONFIG_FAILED, error_string(GetLastError()));
  847. return -1;
  848. }
  849. return 1;
  850. }
  851. int native_get_imagepath(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  852. SC_HANDLE service_handle = (SC_HANDLE) param;
  853. if (! service_handle) return -1;
  854. QUERY_SERVICE_CONFIG *qsc = query_service_config(service_name, service_handle);
  855. if (! qsc) return -1;
  856. int ret = value_from_string(name, value, qsc->lpBinaryPathName);
  857. HeapFree(GetProcessHeap(), 0, qsc);
  858. return ret;
  859. }
  860. int native_set_name(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  861. print_message(stderr, NSSM_MESSAGE_CANNOT_RENAME_SERVICE);
  862. return -1;
  863. }
  864. int native_get_name(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  865. return value_from_string(name, value, service_name);
  866. }
  867. int native_set_objectname(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  868. SC_HANDLE service_handle = (SC_HANDLE) param;
  869. if (! service_handle) return -1;
  870. /*
  871. Logical syntax is: nssm set <service> ObjectName <username> <password>
  872. That means the username is actually passed in the additional parameter.
  873. */
  874. bool localsystem = false;
  875. bool virtual_account = false;
  876. TCHAR *username = NSSM_LOCALSYSTEM_ACCOUNT;
  877. TCHAR *password = 0;
  878. if (additional) {
  879. username = (TCHAR *) additional;
  880. if (value && value->string) password = value->string;
  881. }
  882. else if (value && value->string) username = value->string;
  883. const TCHAR *well_known = well_known_username(username);
  884. size_t passwordsize = 0;
  885. if (well_known) {
  886. if (str_equiv(well_known, NSSM_LOCALSYSTEM_ACCOUNT)) localsystem = true;
  887. username = (TCHAR *) well_known;
  888. password = _T("");
  889. }
  890. else if (is_virtual_account(service_name, username)) virtual_account = true;
  891. else if (! password) {
  892. /* We need a password if the account requires it. */
  893. print_message(stderr, NSSM_MESSAGE_MISSING_PASSWORD, name);
  894. return -1;
  895. }
  896. else passwordsize = _tcslen(password) * sizeof(TCHAR);
  897. /*
  898. ChangeServiceConfig() will fail to set the username if the service is set
  899. to interact with the desktop.
  900. */
  901. unsigned long type = SERVICE_NO_CHANGE;
  902. if (! localsystem) {
  903. QUERY_SERVICE_CONFIG *qsc = query_service_config(service_name, service_handle);
  904. if (! qsc) {
  905. if (passwordsize) SecureZeroMemory(password, passwordsize);
  906. return -1;
  907. }
  908. type = qsc->dwServiceType & ~SERVICE_INTERACTIVE_PROCESS;
  909. HeapFree(GetProcessHeap(), 0, qsc);
  910. }
  911. if (! well_known && ! virtual_account) {
  912. if (grant_logon_as_service(username)) {
  913. if (passwordsize) SecureZeroMemory(password, passwordsize);
  914. print_message(stderr, NSSM_MESSAGE_GRANT_LOGON_AS_SERVICE_FAILED, username);
  915. return -1;
  916. }
  917. }
  918. if (! ChangeServiceConfig(service_handle, type, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, 0, 0, 0, 0, username, password, 0)) {
  919. if (passwordsize) SecureZeroMemory(password, passwordsize);
  920. print_message(stderr, NSSM_MESSAGE_CHANGESERVICECONFIG_FAILED, error_string(GetLastError()));
  921. return -1;
  922. }
  923. if (passwordsize) SecureZeroMemory(password, passwordsize);
  924. if (localsystem) return 0;
  925. return 1;
  926. }
  927. int native_get_objectname(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  928. SC_HANDLE service_handle = (SC_HANDLE) param;
  929. if (! service_handle) return -1;
  930. QUERY_SERVICE_CONFIG *qsc = query_service_config(service_name, service_handle);
  931. if (! qsc) return -1;
  932. int ret = value_from_string(name, value, qsc->lpServiceStartName);
  933. HeapFree(GetProcessHeap(), 0, qsc);
  934. return ret;
  935. }
  936. int native_dump_objectname(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  937. int ret = native_get_objectname(service_name, param, name, default_value, value, additional);
  938. if (ret != 1) return ret;
  939. /* Properly checking for a virtual account requires the actual service name. */
  940. if (! _tcsnicmp(NSSM_VIRTUAL_SERVICE_ACCOUNT_DOMAIN, value->string, _tcslen(NSSM_VIRTUAL_SERVICE_ACCOUNT_DOMAIN))) {
  941. TCHAR *name = virtual_account(service_name);
  942. if (! name) return -1;
  943. HeapFree(GetProcessHeap(), 0, value->string);
  944. value->string = name;
  945. }
  946. else {
  947. /* Do we need to dump a dummy password? */
  948. if (! well_known_username(value->string)) {
  949. /* Parameters are the other way round. */
  950. value_t inverted;
  951. inverted.string = _T("****");
  952. return setting_dump_string(service_name, (void *) REG_SZ, name, &inverted, value->string);
  953. }
  954. }
  955. return setting_dump_string(service_name, (void *) REG_SZ, name, value, 0);
  956. }
  957. int native_set_startup(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  958. SC_HANDLE service_handle = (SC_HANDLE) param;
  959. if (! service_handle) return -1;
  960. /* It makes no sense to try to reset the startup type. */
  961. if (! value || ! value->string) {
  962. print_message(stderr, NSSM_MESSAGE_NO_DEFAULT_VALUE, name);
  963. return -1;
  964. }
  965. /* Map NSSM_STARTUP_* constant to Windows SERVICE_*_START constant. */
  966. int service_startup = -1;
  967. int i;
  968. for (i = 0; startup_strings[i]; i++) {
  969. if (str_equiv(value->string, startup_strings[i])) {
  970. service_startup = i;
  971. break;
  972. }
  973. }
  974. if (service_startup < 0) {
  975. print_message(stderr, NSSM_MESSAGE_INVALID_SERVICE_STARTUP, value->string);
  976. for (i = 0; startup_strings[i]; i++) _ftprintf(stderr, _T("%s\n"), startup_strings[i]);
  977. return -1;
  978. }
  979. unsigned long startup;
  980. switch (service_startup) {
  981. case NSSM_STARTUP_MANUAL: startup = SERVICE_DEMAND_START; break;
  982. case NSSM_STARTUP_DISABLED: startup = SERVICE_DISABLED; break;
  983. default: startup = SERVICE_AUTO_START;
  984. }
  985. if (! ChangeServiceConfig(service_handle, SERVICE_NO_CHANGE, startup, SERVICE_NO_CHANGE, 0, 0, 0, 0, 0, 0, 0)) {
  986. print_message(stderr, NSSM_MESSAGE_CHANGESERVICECONFIG_FAILED, error_string(GetLastError()));
  987. return -1;
  988. }
  989. SERVICE_DELAYED_AUTO_START_INFO delayed;
  990. ZeroMemory(&delayed, sizeof(delayed));
  991. if (service_startup == NSSM_STARTUP_DELAYED) delayed.fDelayedAutostart = 1;
  992. else delayed.fDelayedAutostart = 0;
  993. if (! ChangeServiceConfig2(service_handle, SERVICE_CONFIG_DELAYED_AUTO_START_INFO, &delayed)) {
  994. unsigned long error = GetLastError();
  995. /* Pre-Vista we expect to fail with ERROR_INVALID_LEVEL */
  996. if (error != ERROR_INVALID_LEVEL) {
  997. log_event(EVENTLOG_ERROR_TYPE, NSSM_MESSAGE_SERVICE_CONFIG_DELAYED_AUTO_START_INFO_FAILED, service_name, error_string(error), 0);
  998. }
  999. }
  1000. return 1;
  1001. }
  1002. int native_get_startup(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  1003. SC_HANDLE service_handle = (SC_HANDLE) param;
  1004. if (! service_handle) return -1;
  1005. QUERY_SERVICE_CONFIG *qsc = query_service_config(service_name, service_handle);
  1006. if (! qsc) return -1;
  1007. unsigned long startup;
  1008. int ret = get_service_startup(service_name, service_handle, qsc, &startup);
  1009. HeapFree(GetProcessHeap(), 0, qsc);
  1010. if (ret) return -1;
  1011. unsigned long i;
  1012. for (i = 0; startup_strings[i]; i++);
  1013. if (startup >= i) return -1;
  1014. return value_from_string(name, value, startup_strings[startup]);
  1015. }
  1016. int native_set_type(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  1017. SC_HANDLE service_handle = (SC_HANDLE) param;
  1018. if (! service_handle) return -1;
  1019. /* It makes no sense to try to reset the service type. */
  1020. if (! value || ! value->string) {
  1021. print_message(stderr, NSSM_MESSAGE_NO_DEFAULT_VALUE, name);
  1022. return -1;
  1023. }
  1024. /*
  1025. We can only manage services of type SERVICE_WIN32_OWN_PROCESS
  1026. and SERVICE_INTERACTIVE_PROCESS.
  1027. */
  1028. unsigned long type = SERVICE_WIN32_OWN_PROCESS;
  1029. if (str_equiv(value->string, NSSM_INTERACTIVE_PROCESS)) type |= SERVICE_INTERACTIVE_PROCESS;
  1030. else if (! str_equiv(value->string, NSSM_WIN32_OWN_PROCESS)) {
  1031. print_message(stderr, NSSM_MESSAGE_INVALID_SERVICE_TYPE, value->string);
  1032. _ftprintf(stderr, _T("%s\n"), NSSM_WIN32_OWN_PROCESS);
  1033. _ftprintf(stderr, _T("%s\n"), NSSM_INTERACTIVE_PROCESS);
  1034. return -1;
  1035. }
  1036. /*
  1037. ChangeServiceConfig() will fail if the service runs under an account
  1038. other than LOCALSYSTEM and we try to make it interactive.
  1039. */
  1040. if (type & SERVICE_INTERACTIVE_PROCESS) {
  1041. QUERY_SERVICE_CONFIG *qsc = query_service_config(service_name, service_handle);
  1042. if (! qsc) return -1;
  1043. if (! str_equiv(qsc->lpServiceStartName, NSSM_LOCALSYSTEM_ACCOUNT)) {
  1044. HeapFree(GetProcessHeap(), 0, qsc);
  1045. print_message(stderr, NSSM_MESSAGE_INTERACTIVE_NOT_LOCALSYSTEM, value->string, service_name, NSSM_LOCALSYSTEM_ACCOUNT);
  1046. return -1;
  1047. }
  1048. HeapFree(GetProcessHeap(), 0, qsc);
  1049. }
  1050. if (! ChangeServiceConfig(service_handle, type, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, 0, 0, 0, 0, 0, 0, 0)) {
  1051. print_message(stderr, NSSM_MESSAGE_CHANGESERVICECONFIG_FAILED, error_string(GetLastError()));
  1052. return -1;
  1053. }
  1054. return 1;
  1055. }
  1056. int native_get_type(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  1057. SC_HANDLE service_handle = (SC_HANDLE) param;
  1058. if (! service_handle) return -1;
  1059. QUERY_SERVICE_CONFIG *qsc = query_service_config(service_name, service_handle);
  1060. if (! qsc) return -1;
  1061. value->numeric = qsc->dwServiceType;
  1062. HeapFree(GetProcessHeap(), 0, qsc);
  1063. const TCHAR *string;
  1064. switch (value->numeric) {
  1065. case SERVICE_KERNEL_DRIVER: string = NSSM_KERNEL_DRIVER; break;
  1066. case SERVICE_FILE_SYSTEM_DRIVER: string = NSSM_FILE_SYSTEM_DRIVER; break;
  1067. case SERVICE_WIN32_OWN_PROCESS: string = NSSM_WIN32_OWN_PROCESS; break;
  1068. case SERVICE_WIN32_SHARE_PROCESS: string = NSSM_WIN32_SHARE_PROCESS; break;
  1069. case SERVICE_WIN32_OWN_PROCESS|SERVICE_INTERACTIVE_PROCESS: string = NSSM_INTERACTIVE_PROCESS; break;
  1070. case SERVICE_WIN32_SHARE_PROCESS|SERVICE_INTERACTIVE_PROCESS: string = NSSM_SHARE_INTERACTIVE_PROCESS; break;
  1071. default: string = NSSM_UNKNOWN;
  1072. }
  1073. return value_from_string(name, value, string);
  1074. }
  1075. int set_setting(const TCHAR *service_name, HKEY key, settings_t *setting, value_t *value, const TCHAR *additional) {
  1076. if (! key) return -1;
  1077. int ret;
  1078. if (setting->set) ret = setting->set(service_name, (void *) key, setting->name, setting->default_value, value, additional);
  1079. else ret = -1;
  1080. if (! ret) print_message(stdout, NSSM_MESSAGE_RESET_SETTING, setting->name, service_name);
  1081. else if (ret > 0) print_message(stdout, NSSM_MESSAGE_SET_SETTING, setting->name, service_name);
  1082. else print_message(stderr, NSSM_MESSAGE_SET_SETTING_FAILED, setting->name, service_name);
  1083. return ret;
  1084. }
  1085. int set_setting(const TCHAR *service_name, SC_HANDLE service_handle, settings_t *setting, value_t *value, const TCHAR *additional) {
  1086. if (! service_handle) return -1;
  1087. int ret;
  1088. if (setting->set) ret = setting->set(service_name, service_handle, setting->name, setting->default_value, value, additional);
  1089. else ret = -1;
  1090. if (! ret) print_message(stdout, NSSM_MESSAGE_RESET_SETTING, setting->name, service_name);
  1091. else if (ret > 0) print_message(stdout, NSSM_MESSAGE_SET_SETTING, setting->name, service_name);
  1092. else print_message(stderr, NSSM_MESSAGE_SET_SETTING_FAILED, setting->name, service_name);
  1093. return ret;
  1094. }
  1095. /*
  1096. Returns: 1 if the value was retrieved.
  1097. 0 if the default value was retrieved.
  1098. -1 on error.
  1099. */
  1100. int get_setting(const TCHAR *service_name, HKEY key, settings_t *setting, value_t *value, const TCHAR *additional) {
  1101. if (! key) return -1;
  1102. int ret;
  1103. if (is_string_type(setting->type)) {
  1104. value->string = (TCHAR *) setting->default_value;
  1105. if (setting->get) ret = setting->get(service_name, (void *) key, setting->name, setting->default_value, value, additional);
  1106. else ret = -1;
  1107. }
  1108. else if (is_numeric_type(setting->type)) {
  1109. value->numeric = PtrToUlong(setting->default_value);
  1110. if (setting->get) ret = setting->get(service_name, (void *) key, setting->name, setting->default_value, value, additional);
  1111. else ret = -1;
  1112. }
  1113. else ret = -1;
  1114. if (ret < 0) print_message(stderr, NSSM_MESSAGE_GET_SETTING_FAILED, setting->name, service_name);
  1115. return ret;
  1116. }
  1117. int get_setting(const TCHAR *service_name, SC_HANDLE service_handle, settings_t *setting, value_t *value, const TCHAR *additional) {
  1118. if (! service_handle) return -1;
  1119. return setting->get(service_name, service_handle, setting->name, 0, value, additional);
  1120. }
  1121. int dump_setting(const TCHAR *service_name, HKEY key, SC_HANDLE service_handle, settings_t *setting) {
  1122. void *param;
  1123. if (setting->native) {
  1124. if (! service_handle) return -1;
  1125. param = (void *) service_handle;
  1126. }
  1127. else {
  1128. /* Will be null for native services. */
  1129. param = (void *) key;
  1130. }
  1131. value_t value = { 0 };
  1132. int ret;
  1133. if (setting->dump) return setting->dump(service_name, param, setting->name, (void *) setting, &value, 0);
  1134. if (setting->native) ret = get_setting(service_name, service_handle, setting, &value, 0);
  1135. else ret = get_setting(service_name, key, setting, &value, 0);
  1136. if (ret != 1) return ret;
  1137. return setting_dump_string(service_name, (void *) setting->type, setting->name, &value, 0);
  1138. }
  1139. settings_t settings[] = {
  1140. { NSSM_REG_EXE, REG_EXPAND_SZ, (void *) _T(""), false, 0, setting_set_string, setting_get_string, setting_not_dumpable },
  1141. { NSSM_REG_FLAGS, REG_EXPAND_SZ, (void *) _T(""), false, 0, setting_set_string, setting_get_string, 0 },
  1142. { NSSM_REG_DIR, REG_EXPAND_SZ, (void *) _T(""), false, 0, setting_set_string, setting_get_string, 0 },
  1143. { NSSM_REG_EXIT, REG_SZ, (void *) exit_action_strings[NSSM_EXIT_RESTART], false, ADDITIONAL_MANDATORY, setting_set_exit_action, setting_get_exit_action, setting_dump_exit_action },
  1144. { NSSM_REG_HOOK, REG_SZ, (void *) _T(""), false, ADDITIONAL_MANDATORY, setting_set_hook, setting_get_hook, setting_dump_hooks },
  1145. { NSSM_REG_AFFINITY, REG_SZ, 0, false, 0, setting_set_affinity, setting_get_affinity, 0 },
  1146. { NSSM_REG_ENV, REG_MULTI_SZ, NULL, false, ADDITIONAL_CRLF, setting_set_environment, setting_get_environment, setting_dump_environment },
  1147. { NSSM_REG_ENV_EXTRA, REG_MULTI_SZ, NULL, false, ADDITIONAL_CRLF, setting_set_environment, setting_get_environment, setting_dump_environment },
  1148. { NSSM_REG_NO_CONSOLE, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number, 0 },
  1149. { NSSM_REG_PRIORITY, REG_SZ, (void *) priority_strings[NSSM_NORMAL_PRIORITY], false, 0, setting_set_priority, setting_get_priority, setting_dump_priority },
  1150. { NSSM_REG_RESTART_DELAY, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number, 0 },
  1151. { NSSM_REG_STDIN, REG_EXPAND_SZ, NULL, false, 0, setting_set_string, setting_get_string, 0 },
  1152. { NSSM_REG_STDIN NSSM_REG_STDIO_SHARING, REG_DWORD, (void *) NSSM_STDIN_SHARING, false, 0, setting_set_number, setting_get_number, 0 },
  1153. { NSSM_REG_STDIN NSSM_REG_STDIO_DISPOSITION, REG_DWORD, (void *) NSSM_STDIN_DISPOSITION, false, 0, setting_set_number, setting_get_number, 0 },
  1154. { NSSM_REG_STDIN NSSM_REG_STDIO_FLAGS, REG_DWORD, (void *) NSSM_STDIN_FLAGS, false, 0, setting_set_number, setting_get_number, 0 },
  1155. { NSSM_REG_STDOUT, REG_EXPAND_SZ, NULL, false, 0, setting_set_string, setting_get_string, 0 },
  1156. { NSSM_REG_STDOUT NSSM_REG_STDIO_SHARING, REG_DWORD, (void *) NSSM_STDOUT_SHARING, false, 0, setting_set_number, setting_get_number, 0 },
  1157. { NSSM_REG_STDOUT NSSM_REG_STDIO_DISPOSITION, REG_DWORD, (void *) NSSM_STDOUT_DISPOSITION, false, 0, setting_set_number, setting_get_number, 0 },
  1158. { NSSM_REG_STDOUT NSSM_REG_STDIO_FLAGS, REG_DWORD, (void *) NSSM_STDOUT_FLAGS, false, 0, setting_set_number, setting_get_number, 0 },
  1159. { NSSM_REG_STDOUT NSSM_REG_STDIO_COPY_AND_TRUNCATE, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number, 0 },
  1160. { NSSM_REG_STDERR, REG_EXPAND_SZ, NULL, false, 0, setting_set_string, setting_get_string, 0 },
  1161. { NSSM_REG_STDERR NSSM_REG_STDIO_SHARING, REG_DWORD, (void *) NSSM_STDERR_SHARING, false, 0, setting_set_number, setting_get_number, 0 },
  1162. { NSSM_REG_STDERR NSSM_REG_STDIO_DISPOSITION, REG_DWORD, (void *) NSSM_STDERR_DISPOSITION, false, 0, setting_set_number, setting_get_number, 0 },
  1163. { NSSM_REG_STDERR NSSM_REG_STDIO_FLAGS, REG_DWORD, (void *) NSSM_STDERR_FLAGS, false, 0, setting_set_number, setting_get_number, 0 },
  1164. { NSSM_REG_STDERR NSSM_REG_STDIO_COPY_AND_TRUNCATE, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number, 0 },
  1165. { NSSM_REG_STOP_METHOD_SKIP, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number, 0 },
  1166. { NSSM_REG_KILL_CONSOLE_GRACE_PERIOD, REG_DWORD, (void *) NSSM_KILL_CONSOLE_GRACE_PERIOD, false, 0, setting_set_number, setting_get_number, 0 },
  1167. { NSSM_REG_KILL_WINDOW_GRACE_PERIOD, REG_DWORD, (void *) NSSM_KILL_WINDOW_GRACE_PERIOD, false, 0, setting_set_number, setting_get_number, 0 },
  1168. { NSSM_REG_KILL_THREADS_GRACE_PERIOD, REG_DWORD, (void *) NSSM_KILL_THREADS_GRACE_PERIOD, false, 0, setting_set_number, setting_get_number, 0 },
  1169. { NSSM_REG_KILL_PROCESS_TREE, REG_DWORD, (void *) 1, false, 0, setting_set_number, setting_get_number, 0 },
  1170. { NSSM_REG_THROTTLE, REG_DWORD, (void *) NSSM_RESET_THROTTLE_RESTART, false, 0, setting_set_number, setting_get_number, 0 },
  1171. { NSSM_REG_HOOK_SHARE_OUTPUT_HANDLES, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number, 0 },
  1172. { NSSM_REG_ROTATE, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number, 0 },
  1173. { NSSM_REG_ROTATE_ONLINE, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number, 0 },
  1174. { NSSM_REG_ROTATE_SECONDS, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number, 0 },
  1175. { NSSM_REG_ROTATE_BYTES_LOW, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number, 0 },
  1176. { NSSM_REG_ROTATE_BYTES_HIGH, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number, 0 },
  1177. { NSSM_REG_ROTATE_DELAY, REG_DWORD, (void *) NSSM_ROTATE_DELAY, false, 0, setting_set_number, setting_get_number, 0 },
  1178. { NSSM_REG_TIMESTAMP_LOG, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number, 0 },
  1179. { NSSM_NATIVE_DEPENDONGROUP, REG_MULTI_SZ, NULL, true, ADDITIONAL_CRLF, native_set_dependongroup, native_get_dependongroup, native_dump_dependongroup },
  1180. { NSSM_NATIVE_DEPENDONSERVICE, REG_MULTI_SZ, NULL, true, ADDITIONAL_CRLF, native_set_dependonservice, native_get_dependonservice, native_dump_dependonservice },
  1181. { NSSM_NATIVE_DESCRIPTION, REG_SZ, _T(""), true, 0, native_set_description, native_get_description, 0 },
  1182. { NSSM_NATIVE_DISPLAYNAME, REG_SZ, NULL, true, 0, native_set_displayname, native_get_displayname, 0 },
  1183. { NSSM_NATIVE_ENVIRONMENT, REG_MULTI_SZ, NULL, true, ADDITIONAL_CRLF, native_set_environment, native_get_environment, native_dump_environment },
  1184. { NSSM_NATIVE_IMAGEPATH, REG_EXPAND_SZ, NULL, true, 0, native_set_imagepath, native_get_imagepath, setting_not_dumpable },
  1185. { NSSM_NATIVE_OBJECTNAME, REG_SZ, NSSM_LOCALSYSTEM_ACCOUNT, true, 0, native_set_objectname, native_get_objectname, native_dump_objectname },
  1186. { NSSM_NATIVE_NAME, REG_SZ, NULL, true, 0, native_set_name, native_get_name, setting_not_dumpable },
  1187. { NSSM_NATIVE_STARTUP, REG_SZ, NULL, true, 0, native_set_startup, native_get_startup, 0 },
  1188. { NSSM_NATIVE_TYPE, REG_SZ, NULL, true, 0, native_set_type, native_get_type, 0 },
  1189. { NULL, NULL, NULL, NULL, NULL }
  1190. };