settings.cpp 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  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. extern const TCHAR *exit_action_strings[];
  6. extern const TCHAR *startup_strings[];
  7. extern const TCHAR *priority_strings[];
  8. /* Does the parameter refer to the default value of the setting? */
  9. static inline int is_default(const TCHAR *value) {
  10. return (str_equiv(value, _T("default")) || str_equiv(value, _T("*")) || ! value[0]);
  11. }
  12. static int value_from_string(const TCHAR *name, value_t *value, const TCHAR *string) {
  13. size_t len = _tcslen(string);
  14. if (! len++) {
  15. value->string = 0;
  16. return 0;
  17. }
  18. value->string = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, len * sizeof(TCHAR));
  19. if (! value->string) {
  20. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, name, _T("value_from_string()"));
  21. return -1;
  22. }
  23. if (_sntprintf_s(value->string, len, _TRUNCATE, _T("%s"), string) < 0) {
  24. HeapFree(GetProcessHeap(), 0, value->string);
  25. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, name, _T("value_from_string()"));
  26. return -1;
  27. }
  28. return 1;
  29. }
  30. /* Functions to manage NSSM-specific settings in the registry. */
  31. static int setting_set_number(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  32. HKEY key = (HKEY) param;
  33. if (! key) return -1;
  34. unsigned long number;
  35. long error;
  36. /* Resetting to default? */
  37. if (! value || ! value->string) {
  38. error = RegDeleteValue(key, name);
  39. if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0;
  40. print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error));
  41. return -1;
  42. }
  43. if (str_number(value->string, &number)) return -1;
  44. if (default_value && number == PtrToUlong(default_value)) {
  45. error = RegDeleteValue(key, name);
  46. if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0;
  47. print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error));
  48. return -1;
  49. }
  50. if (set_number(key, (TCHAR *) name, number)) return -1;
  51. return 1;
  52. }
  53. static int setting_get_number(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  54. HKEY key = (HKEY) param;
  55. return get_number(key, (TCHAR *) name, &value->numeric, false);
  56. }
  57. static int setting_set_string(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  58. HKEY key = (HKEY) param;
  59. if (! key) return -1;
  60. long error;
  61. /* Resetting to default? */
  62. if (! value || ! value->string) {
  63. if (default_value) value->string = (TCHAR *) default_value;
  64. else {
  65. error = RegDeleteValue(key, name);
  66. if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0;
  67. print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error));
  68. return -1;
  69. }
  70. }
  71. if (default_value && _tcslen((TCHAR *) default_value) && str_equiv(value->string, (TCHAR *) default_value)) {
  72. error = RegDeleteValue(key, name);
  73. if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0;
  74. print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error));
  75. return -1;
  76. }
  77. if (set_expand_string(key, (TCHAR *) name, value->string)) return -1;
  78. return 1;
  79. }
  80. static int setting_get_string(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  81. HKEY key = (HKEY) param;
  82. TCHAR buffer[VALUE_LENGTH];
  83. if (get_string(key, (TCHAR *) name, (TCHAR *) buffer, (unsigned long) sizeof(buffer), false, false, false)) return -1;
  84. return value_from_string(name, value, buffer);
  85. }
  86. static int setting_set_exit_action(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  87. unsigned long exitcode;
  88. TCHAR *code;
  89. TCHAR action_string[ACTION_LEN];
  90. if (additional) {
  91. /* Default action? */
  92. if (is_default(additional)) code = 0;
  93. else {
  94. if (str_number(additional, &exitcode)) return -1;
  95. code = (TCHAR *) additional;
  96. }
  97. }
  98. HKEY key = open_registry(service_name, name, KEY_WRITE);
  99. if (! key) return -1;
  100. long error;
  101. int ret = 1;
  102. /* Resetting to default? */
  103. if (value && value->string) _sntprintf_s(action_string, _countof(action_string), _TRUNCATE, _T("%s"), value->string);
  104. else {
  105. if (code) {
  106. /* Delete explicit action. */
  107. error = RegDeleteValue(key, code);
  108. RegCloseKey(key);
  109. if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0;
  110. print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, code, service_name, error_string(error));
  111. return -1;
  112. }
  113. else {
  114. /* Explicitly keep the default action. */
  115. if (default_value) _sntprintf_s(action_string, _countof(action_string), _TRUNCATE, _T("%s"), (TCHAR *) default_value);
  116. ret = 0;
  117. }
  118. }
  119. /* Validate the string. */
  120. for (int i = 0; exit_action_strings[i]; i++) {
  121. if (! _tcsnicmp((const TCHAR *) action_string, exit_action_strings[i], ACTION_LEN)) {
  122. if (default_value && str_equiv(action_string, (TCHAR *) default_value)) ret = 0;
  123. if (RegSetValueEx(key, code, 0, REG_SZ, (const unsigned char *) exit_action_strings[i], (unsigned long) (_tcslen(action_string) + 1) * sizeof(TCHAR)) != ERROR_SUCCESS) {
  124. print_message(stderr, NSSM_MESSAGE_SETVALUE_FAILED, code, service_name, error_string(GetLastError()));
  125. RegCloseKey(key);
  126. return -1;
  127. }
  128. RegCloseKey(key);
  129. return ret;
  130. }
  131. }
  132. print_message(stderr, NSSM_MESSAGE_INVALID_EXIT_ACTION, action_string);
  133. for (int i = 0; exit_action_strings[i]; i++) _ftprintf(stderr, _T("%s\n"), exit_action_strings[i]);
  134. return -1;
  135. }
  136. static int setting_get_exit_action(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  137. unsigned long exitcode = 0;
  138. unsigned long *code = 0;
  139. if (additional) {
  140. if (! is_default(additional)) {
  141. if (str_number(additional, &exitcode)) return -1;
  142. code = &exitcode;
  143. }
  144. }
  145. TCHAR action_string[ACTION_LEN];
  146. bool default_action;
  147. if (get_exit_action(service_name, code, action_string, &default_action)) return -1;
  148. value_from_string(name, value, action_string);
  149. if (default_action && ! _tcsnicmp((const TCHAR *) action_string, (TCHAR *) default_value, ACTION_LEN)) return 0;
  150. return 1;
  151. }
  152. static inline bool split_hook_name(const TCHAR *hook_name, TCHAR *hook_event, TCHAR *hook_action) {
  153. TCHAR *s;
  154. for (s = (TCHAR *) hook_name; *s; s++) {
  155. if (*s == _T('/')) {
  156. *s = _T('\0');
  157. _sntprintf_s(hook_event, HOOK_NAME_LENGTH, _TRUNCATE, _T("%s"), hook_name);
  158. _sntprintf_s(hook_action, HOOK_NAME_LENGTH, _TRUNCATE, _T("%s"), ++s);
  159. return valid_hook_name(hook_event, hook_action, false);
  160. }
  161. }
  162. print_message(stderr, NSSM_MESSAGE_INVALID_HOOK_NAME, hook_name);
  163. return false;
  164. }
  165. static int setting_set_hook(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  166. TCHAR hook_event[HOOK_NAME_LENGTH];
  167. TCHAR hook_action[HOOK_NAME_LENGTH];
  168. if (! split_hook_name(additional, hook_event, hook_action)) return -1;
  169. TCHAR *cmd;
  170. if (value && value->string) cmd = value->string;
  171. else cmd = _T("");
  172. if (set_hook(service_name, hook_event, hook_action, cmd)) return -1;
  173. if (! _tcslen(cmd)) return 0;
  174. return 1;
  175. }
  176. static int setting_get_hook(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  177. TCHAR hook_event[HOOK_NAME_LENGTH];
  178. TCHAR hook_action[HOOK_NAME_LENGTH];
  179. if (! split_hook_name(additional, hook_event, hook_action)) return -1;
  180. TCHAR cmd[CMD_LENGTH];
  181. if (get_hook(service_name, hook_event, hook_action, cmd, sizeof(cmd))) return -1;
  182. value_from_string(name, value, cmd);
  183. if (! _tcslen(cmd)) return 0;
  184. return 1;
  185. }
  186. static int setting_set_affinity(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  187. HKEY key = (HKEY) param;
  188. if (! key) return -1;
  189. long error;
  190. __int64 mask;
  191. __int64 system_affinity = 0LL;
  192. if (value && value->string) {
  193. DWORD_PTR affinity;
  194. if (! GetProcessAffinityMask(GetCurrentProcess(), &affinity, (DWORD_PTR *) &system_affinity)) system_affinity = ~0;
  195. if (is_default(value->string) || str_equiv(value->string, NSSM_AFFINITY_ALL)) mask = 0LL;
  196. else if (affinity_string_to_mask(value->string, &mask)) {
  197. print_message(stderr, NSSM_MESSAGE_BOGUS_AFFINITY_MASK, value->string, num_cpus() - 1);
  198. return -1;
  199. }
  200. }
  201. else mask = 0LL;
  202. if (! mask) {
  203. error = RegDeleteValue(key, name);
  204. if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0;
  205. print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error));
  206. return -1;
  207. }
  208. /* Canonicalise. */
  209. TCHAR *canon = 0;
  210. if (affinity_mask_to_string(mask, &canon)) canon = value->string;
  211. __int64 effective_affinity = mask & system_affinity;
  212. if (effective_affinity != mask) {
  213. /* Requested CPUs did not intersect with available CPUs? */
  214. if (! effective_affinity) mask = effective_affinity = system_affinity;
  215. TCHAR *system = 0;
  216. if (! affinity_mask_to_string(system_affinity, &system)) {
  217. TCHAR *effective = 0;
  218. if (! affinity_mask_to_string(effective_affinity, &effective)) {
  219. print_message(stderr, NSSM_MESSAGE_EFFECTIVE_AFFINITY_MASK, value->string, system, effective);
  220. HeapFree(GetProcessHeap(), 0, effective);
  221. }
  222. HeapFree(GetProcessHeap(), 0, system);
  223. }
  224. }
  225. if (RegSetValueEx(key, name, 0, REG_SZ, (const unsigned char *) canon, (unsigned long) (_tcslen(canon) + 1) * sizeof(TCHAR)) != ERROR_SUCCESS) {
  226. if (canon != value->string) HeapFree(GetProcessHeap(), 0, canon);
  227. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, name, error_string(GetLastError()), 0);
  228. return -1;
  229. }
  230. if (canon != value->string) HeapFree(GetProcessHeap(), 0, canon);
  231. return 1;
  232. }
  233. static int setting_get_affinity(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  234. HKEY key = (HKEY) param;
  235. if (! key) return -1;
  236. unsigned long type;
  237. TCHAR *buffer = 0;
  238. unsigned long buflen = 0;
  239. int ret = RegQueryValueEx(key, name, 0, &type, 0, &buflen);
  240. if (ret == ERROR_FILE_NOT_FOUND) {
  241. if (value_from_string(name, value, NSSM_AFFINITY_ALL) == 1) return 0;
  242. return -1;
  243. }
  244. if (ret != ERROR_SUCCESS) return -1;
  245. if (type != REG_SZ) return -1;
  246. buffer = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, buflen);
  247. if (! buffer) {
  248. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("affinity"), _T("setting_get_affinity"));
  249. return -1;
  250. }
  251. if (get_string(key, (TCHAR *) name, buffer, buflen, false, false, true)) {
  252. HeapFree(GetProcessHeap(), 0, buffer);
  253. return -1;
  254. }
  255. __int64 affinity;
  256. if (affinity_string_to_mask(buffer, &affinity)) {
  257. print_message(stderr, NSSM_MESSAGE_BOGUS_AFFINITY_MASK, buffer, num_cpus() - 1);
  258. HeapFree(GetProcessHeap(), 0, buffer);
  259. return -1;
  260. }
  261. HeapFree(GetProcessHeap(), 0, buffer);
  262. /* Canonicalise. */
  263. if (affinity_mask_to_string(affinity, &buffer)) {
  264. if (buffer) HeapFree(GetProcessHeap(), 0, buffer);
  265. return -1;
  266. }
  267. ret = value_from_string(name, value, buffer);
  268. HeapFree(GetProcessHeap(), 0, buffer);
  269. return ret;
  270. }
  271. static int setting_set_environment(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  272. HKEY key = (HKEY) param;
  273. if (! param) return -1;
  274. if (! value || ! value->string || ! value->string[0]) {
  275. long error = RegDeleteValue(key, name);
  276. if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0;
  277. print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error));
  278. return -1;
  279. }
  280. unsigned long envlen = (unsigned long) _tcslen(value->string) + 1;
  281. TCHAR *unformatted = 0;
  282. unsigned long newlen;
  283. if (unformat_double_null(value->string, envlen, &unformatted, &newlen)) return -1;
  284. if (test_environment(unformatted)) {
  285. HeapFree(GetProcessHeap(), 0, unformatted);
  286. print_message(stderr, NSSM_GUI_INVALID_ENVIRONMENT);
  287. return -1;
  288. }
  289. if (RegSetValueEx(key, name, 0, REG_MULTI_SZ, (const unsigned char *) unformatted, (unsigned long) newlen * sizeof(TCHAR)) != ERROR_SUCCESS) {
  290. if (newlen) HeapFree(GetProcessHeap(), 0, unformatted);
  291. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, NSSM_REG_ENV, error_string(GetLastError()), 0);
  292. return -1;
  293. }
  294. if (newlen) HeapFree(GetProcessHeap(), 0, unformatted);
  295. return 1;
  296. }
  297. static int setting_get_environment(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  298. HKEY key = (HKEY) param;
  299. if (! param) return -1;
  300. TCHAR *env = 0;
  301. unsigned long envlen;
  302. if (get_environment((TCHAR *) service_name, key, (TCHAR *) name, &env, &envlen)) return -1;
  303. if (! envlen) return 0;
  304. TCHAR *formatted;
  305. unsigned long newlen;
  306. if (format_double_null(env, envlen, &formatted, &newlen)) return -1;
  307. int ret;
  308. if (additional) {
  309. /* Find named environment variable. */
  310. TCHAR *s;
  311. size_t len = _tcslen(additional);
  312. for (s = env; *s; s++) {
  313. /* Look for <additional>=<string> NULL NULL */
  314. if (! _tcsnicmp(s, additional, len) && s[len] == _T('=')) {
  315. /* Strip <key>= */
  316. s += len + 1;
  317. ret = value_from_string(name, value, s);
  318. HeapFree(GetProcessHeap(), 0, env);
  319. return ret;
  320. }
  321. /* Skip this string. */
  322. for ( ; *s; s++);
  323. }
  324. HeapFree(GetProcessHeap(), 0, env);
  325. return 0;
  326. }
  327. HeapFree(GetProcessHeap(), 0, env);
  328. ret = value_from_string(name, value, formatted);
  329. if (newlen) HeapFree(GetProcessHeap(), 0, formatted);
  330. return ret;
  331. }
  332. static int setting_set_priority(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  333. HKEY key = (HKEY) param;
  334. if (! param) return -1;
  335. TCHAR *priority_string;
  336. int i;
  337. long error;
  338. if (value && value->string) priority_string = value->string;
  339. else if (default_value) priority_string = (TCHAR *) default_value;
  340. else {
  341. error = RegDeleteValue(key, name);
  342. if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0;
  343. print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error));
  344. return -1;
  345. }
  346. for (i = 0; priority_strings[i]; i++) {
  347. if (! str_equiv(priority_strings[i], priority_string)) continue;
  348. if (default_value && str_equiv(priority_string, (TCHAR *) default_value)) {
  349. error = RegDeleteValue(key, name);
  350. if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0;
  351. print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error));
  352. return -1;
  353. }
  354. if (set_number(key, (TCHAR *) name, priority_index_to_constant(i))) return -1;
  355. return 1;
  356. }
  357. print_message(stderr, NSSM_MESSAGE_INVALID_PRIORITY, priority_string);
  358. for (i = 0; priority_strings[i]; i++) _ftprintf(stderr, _T("%s\n"), priority_strings[i]);
  359. return -1;
  360. }
  361. static int setting_get_priority(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  362. HKEY key = (HKEY) param;
  363. if (! param) return -1;
  364. unsigned long constant;
  365. switch (get_number(key, (TCHAR *) name, &constant, false)) {
  366. case 0: return value_from_string(name, value, (const TCHAR *) default_value);
  367. case -1: return -1;
  368. }
  369. return value_from_string(name, value, priority_strings[priority_constant_to_index(constant)]);
  370. }
  371. /* Functions to manage native service settings. */
  372. static int native_set_dependongroup(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  373. SC_HANDLE service_handle = (SC_HANDLE) param;
  374. if (! service_handle) return -1;
  375. /*
  376. Get existing service dependencies because we must set both types together.
  377. */
  378. TCHAR *buffer;
  379. unsigned long buflen;
  380. if (get_service_dependencies(service_name, service_handle, &buffer, &buflen, DEPENDENCY_SERVICES)) return -1;
  381. if (! value || ! value->string || ! value->string[0]) {
  382. if (! ChangeServiceConfig(service_handle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, 0, 0, 0, buffer, 0, 0, 0)) {
  383. print_message(stderr, NSSM_MESSAGE_CHANGESERVICECONFIG_FAILED, error_string(GetLastError()));
  384. if (buffer) HeapFree(GetProcessHeap(), 0, buffer);
  385. return -1;
  386. }
  387. if (buffer) HeapFree(GetProcessHeap(), 0, buffer);
  388. return 0;
  389. }
  390. unsigned long len = (unsigned long) _tcslen(value->string) + 1;
  391. TCHAR *unformatted = 0;
  392. unsigned long newlen;
  393. if (unformat_double_null(value->string, len, &unformatted, &newlen)) {
  394. if (buffer) HeapFree(GetProcessHeap(), 0, buffer);
  395. return -1;
  396. }
  397. /* Prepend group identifier. */
  398. unsigned long missing = 0;
  399. TCHAR *canon = unformatted;
  400. size_t canonlen = 0;
  401. TCHAR *s;
  402. for (s = unformatted; *s; s++) {
  403. if (*s != SC_GROUP_IDENTIFIER) missing++;
  404. size_t len = _tcslen(s);
  405. canonlen += len + 1;
  406. s += len;
  407. }
  408. if (missing) {
  409. /* Missing identifiers plus double NULL terminator. */
  410. canonlen += missing + 1;
  411. newlen = (unsigned long) canonlen;
  412. canon = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, canonlen * sizeof(TCHAR));
  413. if (! canon) {
  414. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("canon"), _T("native_set_dependongroup"));
  415. if (unformatted) HeapFree(GetProcessHeap(), 0, unformatted);
  416. if (buffer) HeapFree(GetProcessHeap(), 0, buffer);
  417. return -1;
  418. }
  419. size_t i = 0;
  420. for (s = unformatted; *s; s++) {
  421. if (*s != SC_GROUP_IDENTIFIER) canon[i++] = SC_GROUP_IDENTIFIER;
  422. size_t len = _tcslen(s);
  423. memmove(canon + i, s, (len + 1) * sizeof(TCHAR));
  424. i += len + 1;
  425. s += len;
  426. }
  427. }
  428. TCHAR *dependencies;
  429. if (buflen > 2) {
  430. dependencies = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, (newlen + buflen) * sizeof(TCHAR));
  431. if (! dependencies) {
  432. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("dependencies"), _T("native_set_dependongroup"));
  433. if (canon != unformatted) HeapFree(GetProcessHeap(), 0, canon);
  434. if (unformatted) HeapFree(GetProcessHeap(), 0, unformatted);
  435. if (buffer) HeapFree(GetProcessHeap(), 0, buffer);
  436. return -1;
  437. }
  438. memmove(dependencies, buffer, buflen * sizeof(TCHAR));
  439. memmove(dependencies + buflen - 1, canon, newlen * sizeof(TCHAR));
  440. }
  441. else dependencies = canon;
  442. int ret = 1;
  443. if (set_service_dependencies(service_name, service_handle, dependencies)) ret = -1;
  444. if (dependencies != unformatted) HeapFree(GetProcessHeap(), 0, dependencies);
  445. if (canon != unformatted) HeapFree(GetProcessHeap(), 0, canon);
  446. if (unformatted) HeapFree(GetProcessHeap(), 0, unformatted);
  447. if (buffer) HeapFree(GetProcessHeap(), 0, buffer);
  448. return ret;
  449. }
  450. static int native_get_dependongroup(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  451. SC_HANDLE service_handle = (SC_HANDLE) param;
  452. if (! service_handle) return -1;
  453. TCHAR *buffer;
  454. unsigned long buflen;
  455. if (get_service_dependencies(service_name, service_handle, &buffer, &buflen, DEPENDENCY_GROUPS)) return -1;
  456. int ret;
  457. if (buflen) {
  458. TCHAR *formatted;
  459. unsigned long newlen;
  460. if (format_double_null(buffer, buflen, &formatted, &newlen)) {
  461. HeapFree(GetProcessHeap(), 0, buffer);
  462. return -1;
  463. }
  464. ret = value_from_string(name, value, formatted);
  465. HeapFree(GetProcessHeap(), 0, formatted);
  466. HeapFree(GetProcessHeap(), 0, buffer);
  467. }
  468. else {
  469. value->string = 0;
  470. ret = 0;
  471. }
  472. return ret;
  473. }
  474. static int native_set_dependonservice(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  475. SC_HANDLE service_handle = (SC_HANDLE) param;
  476. if (! service_handle) return -1;
  477. /*
  478. Get existing group dependencies because we must set both types together.
  479. */
  480. TCHAR *buffer;
  481. unsigned long buflen;
  482. if (get_service_dependencies(service_name, service_handle, &buffer, &buflen, DEPENDENCY_GROUPS)) return -1;
  483. if (! value || ! value->string || ! value->string[0]) {
  484. if (! ChangeServiceConfig(service_handle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, 0, 0, 0, buffer, 0, 0, 0)) {
  485. print_message(stderr, NSSM_MESSAGE_CHANGESERVICECONFIG_FAILED, error_string(GetLastError()));
  486. if (buffer) HeapFree(GetProcessHeap(), 0, buffer);
  487. return -1;
  488. }
  489. if (buffer) HeapFree(GetProcessHeap(), 0, buffer);
  490. return 0;
  491. }
  492. unsigned long len = (unsigned long) _tcslen(value->string) + 1;
  493. TCHAR *unformatted = 0;
  494. unsigned long newlen;
  495. if (unformat_double_null(value->string, len, &unformatted, &newlen)) {
  496. if (buffer) HeapFree(GetProcessHeap(), 0, buffer);
  497. return -1;
  498. }
  499. TCHAR *dependencies;
  500. if (buflen > 2) {
  501. dependencies = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, (newlen + buflen) * sizeof(TCHAR));
  502. if (! dependencies) {
  503. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("dependencies"), _T("native_set_dependonservice"));
  504. if (unformatted) HeapFree(GetProcessHeap(), 0, unformatted);
  505. if (buffer) HeapFree(GetProcessHeap(), 0, buffer);
  506. return -1;
  507. }
  508. memmove(dependencies, buffer, buflen * sizeof(TCHAR));
  509. memmove(dependencies + buflen - 1, unformatted, newlen * sizeof(TCHAR));
  510. }
  511. else dependencies = unformatted;
  512. int ret = 1;
  513. if (set_service_dependencies(service_name, service_handle, dependencies)) ret = -1;
  514. if (dependencies != unformatted) HeapFree(GetProcessHeap(), 0, dependencies);
  515. if (unformatted) HeapFree(GetProcessHeap(), 0, unformatted);
  516. if (buffer) HeapFree(GetProcessHeap(), 0, buffer);
  517. return ret;
  518. }
  519. static int native_get_dependonservice(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  520. SC_HANDLE service_handle = (SC_HANDLE) param;
  521. if (! service_handle) return -1;
  522. TCHAR *buffer;
  523. unsigned long buflen;
  524. if (get_service_dependencies(service_name, service_handle, &buffer, &buflen, DEPENDENCY_SERVICES)) return -1;
  525. int ret;
  526. if (buflen) {
  527. TCHAR *formatted;
  528. unsigned long newlen;
  529. if (format_double_null(buffer, buflen, &formatted, &newlen)) {
  530. HeapFree(GetProcessHeap(), 0, buffer);
  531. return -1;
  532. }
  533. ret = value_from_string(name, value, formatted);
  534. HeapFree(GetProcessHeap(), 0, formatted);
  535. HeapFree(GetProcessHeap(), 0, buffer);
  536. }
  537. else {
  538. value->string = 0;
  539. ret = 0;
  540. }
  541. return ret;
  542. }
  543. int native_set_description(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  544. SC_HANDLE service_handle = (SC_HANDLE) param;
  545. if (! service_handle) return -1;
  546. TCHAR *description = 0;
  547. if (value) description = value->string;
  548. if (set_service_description(service_name, service_handle, description)) return -1;
  549. if (description && description[0]) return 1;
  550. return 0;
  551. }
  552. int native_get_description(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  553. SC_HANDLE service_handle = (SC_HANDLE) param;
  554. if (! service_handle) return -1;
  555. TCHAR buffer[VALUE_LENGTH];
  556. if (get_service_description(service_name, service_handle, _countof(buffer), buffer)) return -1;
  557. if (buffer[0]) return value_from_string(name, value, buffer);
  558. value->string = 0;
  559. return 0;
  560. }
  561. int native_set_displayname(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  562. SC_HANDLE service_handle = (SC_HANDLE) param;
  563. if (! service_handle) return -1;
  564. TCHAR *displayname = 0;
  565. if (value && value->string) displayname = value->string;
  566. else displayname = (TCHAR *) service_name;
  567. if (! ChangeServiceConfig(service_handle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, 0, 0, 0, 0, 0, 0, displayname)) {
  568. print_message(stderr, NSSM_MESSAGE_CHANGESERVICECONFIG_FAILED, error_string(GetLastError()));
  569. return -1;
  570. }
  571. /*
  572. If the display name and service name differ only in case,
  573. ChangeServiceConfig() will return success but the display name will be
  574. set to the service name, NOT the value passed to the function.
  575. This appears to be a quirk of Windows rather than a bug here.
  576. */
  577. if (displayname != service_name && ! str_equiv(displayname, service_name)) return 1;
  578. return 0;
  579. }
  580. int native_get_displayname(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  581. SC_HANDLE service_handle = (SC_HANDLE) param;
  582. if (! service_handle) return -1;
  583. QUERY_SERVICE_CONFIG *qsc = query_service_config(service_name, service_handle);
  584. if (! qsc) return -1;
  585. int ret = value_from_string(name, value, qsc->lpDisplayName);
  586. HeapFree(GetProcessHeap(), 0, qsc);
  587. return ret;
  588. }
  589. int native_set_environment(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  590. HKEY key = open_service_registry(service_name, KEY_SET_VALUE, false);
  591. if (! key) return -1;
  592. int ret = setting_set_environment(service_name, (void *) key, NSSM_NATIVE_ENVIRONMENT, default_value, value, additional);
  593. RegCloseKey(key);
  594. return ret;
  595. }
  596. int native_get_environment(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  597. HKEY key = open_service_registry(service_name, KEY_READ, false);
  598. if (! key) return -1;
  599. ZeroMemory(value, sizeof(value_t));
  600. int ret = setting_get_environment(service_name, (void *) key, NSSM_NATIVE_ENVIRONMENT, default_value, value, additional);
  601. RegCloseKey(key);
  602. return ret;
  603. }
  604. int native_set_imagepath(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  605. SC_HANDLE service_handle = (SC_HANDLE) param;
  606. if (! service_handle) return -1;
  607. /* It makes no sense to try to reset the image path. */
  608. if (! value || ! value->string) {
  609. print_message(stderr, NSSM_MESSAGE_NO_DEFAULT_VALUE, name);
  610. return -1;
  611. }
  612. if (! ChangeServiceConfig(service_handle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, value->string, 0, 0, 0, 0, 0, 0)) {
  613. print_message(stderr, NSSM_MESSAGE_CHANGESERVICECONFIG_FAILED, error_string(GetLastError()));
  614. return -1;
  615. }
  616. return 1;
  617. }
  618. int native_get_imagepath(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  619. SC_HANDLE service_handle = (SC_HANDLE) param;
  620. if (! service_handle) return -1;
  621. QUERY_SERVICE_CONFIG *qsc = query_service_config(service_name, service_handle);
  622. if (! qsc) return -1;
  623. int ret = value_from_string(name, value, qsc->lpBinaryPathName);
  624. HeapFree(GetProcessHeap(), 0, qsc);
  625. return ret;
  626. }
  627. int native_set_name(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  628. print_message(stderr, NSSM_MESSAGE_CANNOT_RENAME_SERVICE);
  629. return -1;
  630. }
  631. int native_get_name(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  632. return value_from_string(name, value, service_name);
  633. }
  634. int native_set_objectname(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  635. SC_HANDLE service_handle = (SC_HANDLE) param;
  636. if (! service_handle) return -1;
  637. /*
  638. Logical syntax is: nssm set <service> ObjectName <username> <password>
  639. That means the username is actually passed in the additional parameter.
  640. */
  641. bool localsystem = false;
  642. TCHAR *username = NSSM_LOCALSYSTEM_ACCOUNT;
  643. TCHAR *password = 0;
  644. if (additional) {
  645. username = (TCHAR *) additional;
  646. if (value && value->string) password = value->string;
  647. }
  648. else if (value && value->string) username = value->string;
  649. const TCHAR *well_known = well_known_username(username);
  650. size_t passwordsize = 0;
  651. if (well_known) {
  652. if (str_equiv(well_known, NSSM_LOCALSYSTEM_ACCOUNT)) localsystem = true;
  653. username = (TCHAR *) well_known;
  654. password = _T("");
  655. }
  656. else if (! password) {
  657. /* We need a password if the account requires it. */
  658. print_message(stderr, NSSM_MESSAGE_MISSING_PASSWORD, name);
  659. return -1;
  660. }
  661. else passwordsize = _tcslen(password) * sizeof(TCHAR);
  662. /*
  663. ChangeServiceConfig() will fail to set the username if the service is set
  664. to interact with the desktop.
  665. */
  666. unsigned long type = SERVICE_NO_CHANGE;
  667. if (! localsystem) {
  668. QUERY_SERVICE_CONFIG *qsc = query_service_config(service_name, service_handle);
  669. if (! qsc) {
  670. if (passwordsize) SecureZeroMemory(password, passwordsize);
  671. return -1;
  672. }
  673. type = qsc->dwServiceType & ~SERVICE_INTERACTIVE_PROCESS;
  674. HeapFree(GetProcessHeap(), 0, qsc);
  675. }
  676. if (! well_known) {
  677. if (grant_logon_as_service(username)) {
  678. if (passwordsize) SecureZeroMemory(password, passwordsize);
  679. print_message(stderr, NSSM_MESSAGE_GRANT_LOGON_AS_SERVICE_FAILED, username);
  680. return -1;
  681. }
  682. }
  683. if (! ChangeServiceConfig(service_handle, type, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, 0, 0, 0, 0, username, password, 0)) {
  684. if (passwordsize) SecureZeroMemory(password, passwordsize);
  685. print_message(stderr, NSSM_MESSAGE_CHANGESERVICECONFIG_FAILED, error_string(GetLastError()));
  686. return -1;
  687. }
  688. if (passwordsize) SecureZeroMemory(password, passwordsize);
  689. if (localsystem) return 0;
  690. return 1;
  691. }
  692. int native_get_objectname(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  693. SC_HANDLE service_handle = (SC_HANDLE) param;
  694. if (! service_handle) return -1;
  695. QUERY_SERVICE_CONFIG *qsc = query_service_config(service_name, service_handle);
  696. if (! qsc) return -1;
  697. int ret = value_from_string(name, value, qsc->lpServiceStartName);
  698. HeapFree(GetProcessHeap(), 0, qsc);
  699. return ret;
  700. }
  701. int native_set_startup(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  702. SC_HANDLE service_handle = (SC_HANDLE) param;
  703. if (! service_handle) return -1;
  704. /* It makes no sense to try to reset the startup type. */
  705. if (! value || ! value->string) {
  706. print_message(stderr, NSSM_MESSAGE_NO_DEFAULT_VALUE, name);
  707. return -1;
  708. }
  709. /* Map NSSM_STARTUP_* constant to Windows SERVICE_*_START constant. */
  710. int service_startup = -1;
  711. int i;
  712. for (i = 0; startup_strings[i]; i++) {
  713. if (str_equiv(value->string, startup_strings[i])) {
  714. service_startup = i;
  715. break;
  716. }
  717. }
  718. if (service_startup < 0) {
  719. print_message(stderr, NSSM_MESSAGE_INVALID_SERVICE_STARTUP, value->string);
  720. for (i = 0; startup_strings[i]; i++) _ftprintf(stderr, _T("%s\n"), startup_strings[i]);
  721. return -1;
  722. }
  723. unsigned long startup;
  724. switch (service_startup) {
  725. case NSSM_STARTUP_MANUAL: startup = SERVICE_DEMAND_START; break;
  726. case NSSM_STARTUP_DISABLED: startup = SERVICE_DISABLED; break;
  727. default: startup = SERVICE_AUTO_START;
  728. }
  729. if (! ChangeServiceConfig(service_handle, SERVICE_NO_CHANGE, startup, SERVICE_NO_CHANGE, 0, 0, 0, 0, 0, 0, 0)) {
  730. print_message(stderr, NSSM_MESSAGE_CHANGESERVICECONFIG_FAILED, error_string(GetLastError()));
  731. return -1;
  732. }
  733. SERVICE_DELAYED_AUTO_START_INFO delayed;
  734. ZeroMemory(&delayed, sizeof(delayed));
  735. if (service_startup == NSSM_STARTUP_DELAYED) delayed.fDelayedAutostart = 1;
  736. else delayed.fDelayedAutostart = 0;
  737. if (! ChangeServiceConfig2(service_handle, SERVICE_CONFIG_DELAYED_AUTO_START_INFO, &delayed)) {
  738. unsigned long error = GetLastError();
  739. /* Pre-Vista we expect to fail with ERROR_INVALID_LEVEL */
  740. if (error != ERROR_INVALID_LEVEL) {
  741. log_event(EVENTLOG_ERROR_TYPE, NSSM_MESSAGE_SERVICE_CONFIG_DELAYED_AUTO_START_INFO_FAILED, service_name, error_string(error), 0);
  742. }
  743. }
  744. return 1;
  745. }
  746. int native_get_startup(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  747. SC_HANDLE service_handle = (SC_HANDLE) param;
  748. if (! service_handle) return -1;
  749. QUERY_SERVICE_CONFIG *qsc = query_service_config(service_name, service_handle);
  750. if (! qsc) return -1;
  751. unsigned long startup;
  752. int ret = get_service_startup(service_name, service_handle, qsc, &startup);
  753. HeapFree(GetProcessHeap(), 0, qsc);
  754. if (ret) return -1;
  755. unsigned long i;
  756. for (i = 0; startup_strings[i]; i++);
  757. if (startup >= i) return -1;
  758. return value_from_string(name, value, startup_strings[startup]);
  759. }
  760. int native_set_type(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  761. SC_HANDLE service_handle = (SC_HANDLE) param;
  762. if (! service_handle) return -1;
  763. /* It makes no sense to try to reset the service type. */
  764. if (! value || ! value->string) {
  765. print_message(stderr, NSSM_MESSAGE_NO_DEFAULT_VALUE, name);
  766. return -1;
  767. }
  768. /*
  769. We can only manage services of type SERVICE_WIN32_OWN_PROCESS
  770. and SERVICE_INTERACTIVE_PROCESS.
  771. */
  772. unsigned long type = SERVICE_WIN32_OWN_PROCESS;
  773. if (str_equiv(value->string, NSSM_INTERACTIVE_PROCESS)) type |= SERVICE_INTERACTIVE_PROCESS;
  774. else if (! str_equiv(value->string, NSSM_WIN32_OWN_PROCESS)) {
  775. print_message(stderr, NSSM_MESSAGE_INVALID_SERVICE_TYPE, value->string);
  776. _ftprintf(stderr, _T("%s\n"), NSSM_WIN32_OWN_PROCESS);
  777. _ftprintf(stderr, _T("%s\n"), NSSM_INTERACTIVE_PROCESS);
  778. return -1;
  779. }
  780. /*
  781. ChangeServiceConfig() will fail if the service runs under an account
  782. other than LOCALSYSTEM and we try to make it interactive.
  783. */
  784. if (type & SERVICE_INTERACTIVE_PROCESS) {
  785. QUERY_SERVICE_CONFIG *qsc = query_service_config(service_name, service_handle);
  786. if (! qsc) return -1;
  787. if (! str_equiv(qsc->lpServiceStartName, NSSM_LOCALSYSTEM_ACCOUNT)) {
  788. HeapFree(GetProcessHeap(), 0, qsc);
  789. print_message(stderr, NSSM_MESSAGE_INTERACTIVE_NOT_LOCALSYSTEM, value->string, service_name, NSSM_LOCALSYSTEM_ACCOUNT);
  790. return -1;
  791. }
  792. HeapFree(GetProcessHeap(), 0, qsc);
  793. }
  794. if (! ChangeServiceConfig(service_handle, type, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, 0, 0, 0, 0, 0, 0, 0)) {
  795. print_message(stderr, NSSM_MESSAGE_CHANGESERVICECONFIG_FAILED, error_string(GetLastError()));
  796. return -1;
  797. }
  798. return 1;
  799. }
  800. int native_get_type(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  801. SC_HANDLE service_handle = (SC_HANDLE) param;
  802. if (! service_handle) return -1;
  803. QUERY_SERVICE_CONFIG *qsc = query_service_config(service_name, service_handle);
  804. if (! qsc) return -1;
  805. value->numeric = qsc->dwServiceType;
  806. HeapFree(GetProcessHeap(), 0, qsc);
  807. const TCHAR *string;
  808. switch (value->numeric) {
  809. case SERVICE_KERNEL_DRIVER: string = NSSM_KERNEL_DRIVER; break;
  810. case SERVICE_FILE_SYSTEM_DRIVER: string = NSSM_FILE_SYSTEM_DRIVER; break;
  811. case SERVICE_WIN32_OWN_PROCESS: string = NSSM_WIN32_OWN_PROCESS; break;
  812. case SERVICE_WIN32_SHARE_PROCESS: string = NSSM_WIN32_SHARE_PROCESS; break;
  813. case SERVICE_WIN32_OWN_PROCESS|SERVICE_INTERACTIVE_PROCESS: string = NSSM_INTERACTIVE_PROCESS; break;
  814. case SERVICE_WIN32_SHARE_PROCESS|SERVICE_INTERACTIVE_PROCESS: string = NSSM_SHARE_INTERACTIVE_PROCESS; break;
  815. default: string = NSSM_UNKNOWN;
  816. }
  817. return value_from_string(name, value, string);
  818. }
  819. int set_setting(const TCHAR *service_name, HKEY key, settings_t *setting, value_t *value, const TCHAR *additional) {
  820. if (! key) return -1;
  821. int ret;
  822. if (setting->set) ret = setting->set(service_name, (void *) key, setting->name, setting->default_value, value, additional);
  823. else ret = -1;
  824. if (! ret) print_message(stdout, NSSM_MESSAGE_RESET_SETTING, setting->name, service_name);
  825. else if (ret > 0) print_message(stdout, NSSM_MESSAGE_SET_SETTING, setting->name, service_name);
  826. else print_message(stderr, NSSM_MESSAGE_SET_SETTING_FAILED, setting->name, service_name);
  827. return ret;
  828. }
  829. int set_setting(const TCHAR *service_name, SC_HANDLE service_handle, settings_t *setting, value_t *value, const TCHAR *additional) {
  830. if (! service_handle) return -1;
  831. int ret;
  832. if (setting->set) ret = setting->set(service_name, service_handle, setting->name, setting->default_value, value, additional);
  833. else ret = -1;
  834. if (! ret) print_message(stdout, NSSM_MESSAGE_RESET_SETTING, setting->name, service_name);
  835. else if (ret > 0) print_message(stdout, NSSM_MESSAGE_SET_SETTING, setting->name, service_name);
  836. else print_message(stderr, NSSM_MESSAGE_SET_SETTING_FAILED, setting->name, service_name);
  837. return ret;
  838. }
  839. /*
  840. Returns: 1 if the value was retrieved.
  841. 0 if the default value was retrieved.
  842. -1 on error.
  843. */
  844. int get_setting(const TCHAR *service_name, HKEY key, settings_t *setting, value_t *value, const TCHAR *additional) {
  845. if (! key) return -1;
  846. int ret;
  847. switch (setting->type) {
  848. case REG_EXPAND_SZ:
  849. case REG_MULTI_SZ:
  850. case REG_SZ:
  851. value->string = (TCHAR *) setting->default_value;
  852. if (setting->get) ret = setting->get(service_name, (void *) key, setting->name, setting->default_value, value, additional);
  853. else ret = -1;
  854. break;
  855. case REG_DWORD:
  856. value->numeric = PtrToUlong(setting->default_value);
  857. if (setting->get) ret = setting->get(service_name, (void *) key, setting->name, setting->default_value, value, additional);
  858. else ret = -1;
  859. break;
  860. default:
  861. ret = -1;
  862. break;
  863. }
  864. if (ret < 0) print_message(stderr, NSSM_MESSAGE_GET_SETTING_FAILED, setting->name, service_name);
  865. return ret;
  866. }
  867. int get_setting(const TCHAR *service_name, SC_HANDLE service_handle, settings_t *setting, value_t *value, const TCHAR *additional) {
  868. if (! service_handle) return -1;
  869. return setting->get(service_name, service_handle, setting->name, 0, value, additional);
  870. }
  871. settings_t settings[] = {
  872. { NSSM_REG_EXE, REG_EXPAND_SZ, (void *) _T(""), false, 0, setting_set_string, setting_get_string },
  873. { NSSM_REG_FLAGS, REG_EXPAND_SZ, (void *) _T(""), false, 0, setting_set_string, setting_get_string },
  874. { NSSM_REG_DIR, REG_EXPAND_SZ, (void *) _T(""), false, 0, setting_set_string, setting_get_string },
  875. { NSSM_REG_EXIT, REG_SZ, (void *) exit_action_strings[NSSM_EXIT_RESTART], false, ADDITIONAL_MANDATORY, setting_set_exit_action, setting_get_exit_action },
  876. { NSSM_REG_HOOK, REG_SZ, (void *) _T(""), false, ADDITIONAL_MANDATORY, setting_set_hook, setting_get_hook },
  877. { NSSM_REG_AFFINITY, REG_SZ, 0, false, 0, setting_set_affinity, setting_get_affinity },
  878. { NSSM_REG_ENV, REG_MULTI_SZ, NULL, false, ADDITIONAL_CRLF, setting_set_environment, setting_get_environment },
  879. { NSSM_REG_ENV_EXTRA, REG_MULTI_SZ, NULL, false, ADDITIONAL_CRLF, setting_set_environment, setting_get_environment },
  880. { NSSM_REG_NO_CONSOLE, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number },
  881. { NSSM_REG_PRIORITY, REG_SZ, (void *) priority_strings[NSSM_NORMAL_PRIORITY], false, 0, setting_set_priority, setting_get_priority },
  882. { NSSM_REG_RESTART_DELAY, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number },
  883. { NSSM_REG_STDIN, REG_EXPAND_SZ, NULL, false, 0, setting_set_string, setting_get_string },
  884. { NSSM_REG_STDIN NSSM_REG_STDIO_SHARING, REG_DWORD, (void *) NSSM_STDIN_SHARING, false, 0, setting_set_number, setting_get_number },
  885. { NSSM_REG_STDIN NSSM_REG_STDIO_DISPOSITION, REG_DWORD, (void *) NSSM_STDIN_DISPOSITION, false, 0, setting_set_number, setting_get_number },
  886. { NSSM_REG_STDIN NSSM_REG_STDIO_FLAGS, REG_DWORD, (void *) NSSM_STDIN_FLAGS, false, 0, setting_set_number, setting_get_number },
  887. { NSSM_REG_STDOUT, REG_EXPAND_SZ, NULL, false, 0, setting_set_string, setting_get_string },
  888. { NSSM_REG_STDOUT NSSM_REG_STDIO_SHARING, REG_DWORD, (void *) NSSM_STDOUT_SHARING, false, 0, setting_set_number, setting_get_number },
  889. { NSSM_REG_STDOUT NSSM_REG_STDIO_DISPOSITION, REG_DWORD, (void *) NSSM_STDOUT_DISPOSITION, false, 0, setting_set_number, setting_get_number },
  890. { NSSM_REG_STDOUT NSSM_REG_STDIO_FLAGS, REG_DWORD, (void *) NSSM_STDOUT_FLAGS, false, 0, setting_set_number, setting_get_number },
  891. { NSSM_REG_STDOUT NSSM_REG_STDIO_COPY_AND_TRUNCATE, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number },
  892. { NSSM_REG_STDERR, REG_EXPAND_SZ, NULL, false, 0, setting_set_string, setting_get_string },
  893. { NSSM_REG_STDERR NSSM_REG_STDIO_SHARING, REG_DWORD, (void *) NSSM_STDERR_SHARING, false, 0, setting_set_number, setting_get_number },
  894. { NSSM_REG_STDERR NSSM_REG_STDIO_DISPOSITION, REG_DWORD, (void *) NSSM_STDERR_DISPOSITION, false, 0, setting_set_number, setting_get_number },
  895. { NSSM_REG_STDERR NSSM_REG_STDIO_FLAGS, REG_DWORD, (void *) NSSM_STDERR_FLAGS, false, 0, setting_set_number, setting_get_number },
  896. { NSSM_REG_STDERR NSSM_REG_STDIO_COPY_AND_TRUNCATE, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number },
  897. { NSSM_REG_STOP_METHOD_SKIP, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number },
  898. { NSSM_REG_KILL_CONSOLE_GRACE_PERIOD, REG_DWORD, (void *) NSSM_KILL_CONSOLE_GRACE_PERIOD, false, 0, setting_set_number, setting_get_number },
  899. { NSSM_REG_KILL_WINDOW_GRACE_PERIOD, REG_DWORD, (void *) NSSM_KILL_WINDOW_GRACE_PERIOD, false, 0, setting_set_number, setting_get_number },
  900. { NSSM_REG_KILL_THREADS_GRACE_PERIOD, REG_DWORD, (void *) NSSM_KILL_THREADS_GRACE_PERIOD, false, 0, setting_set_number, setting_get_number },
  901. { NSSM_REG_KILL_PROCESS_TREE, REG_DWORD, (void *) 1, false, 0, setting_set_number, setting_get_number },
  902. { NSSM_REG_THROTTLE, REG_DWORD, (void *) NSSM_RESET_THROTTLE_RESTART, false, 0, setting_set_number, setting_get_number },
  903. { NSSM_REG_ROTATE, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number },
  904. { NSSM_REG_ROTATE_ONLINE, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number },
  905. { NSSM_REG_ROTATE_SECONDS, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number },
  906. { NSSM_REG_ROTATE_BYTES_LOW, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number },
  907. { NSSM_REG_ROTATE_BYTES_HIGH, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number },
  908. { NSSM_REG_ROTATE_DELAY, REG_DWORD, (void *) NSSM_ROTATE_DELAY, false, 0, setting_set_number, setting_get_number },
  909. { NSSM_NATIVE_DEPENDONGROUP, REG_MULTI_SZ, NULL, true, ADDITIONAL_CRLF, native_set_dependongroup, native_get_dependongroup },
  910. { NSSM_NATIVE_DEPENDONSERVICE, REG_MULTI_SZ, NULL, true, ADDITIONAL_CRLF, native_set_dependonservice, native_get_dependonservice },
  911. { NSSM_NATIVE_DESCRIPTION, REG_SZ, _T(""), true, 0, native_set_description, native_get_description },
  912. { NSSM_NATIVE_DISPLAYNAME, REG_SZ, NULL, true, 0, native_set_displayname, native_get_displayname },
  913. { NSSM_NATIVE_ENVIRONMENT, REG_MULTI_SZ, NULL, true, ADDITIONAL_CRLF, native_set_environment, native_get_environment },
  914. { NSSM_NATIVE_IMAGEPATH, REG_EXPAND_SZ, NULL, true, 0, native_set_imagepath, native_get_imagepath },
  915. { NSSM_NATIVE_OBJECTNAME, REG_SZ, NSSM_LOCALSYSTEM_ACCOUNT, true, 0, native_set_objectname, native_get_objectname },
  916. { NSSM_NATIVE_NAME, REG_SZ, NULL, true, 0, native_set_name, native_get_name },
  917. { NSSM_NATIVE_STARTUP, REG_SZ, NULL, true, 0, native_set_startup, native_get_startup },
  918. { NSSM_NATIVE_TYPE, REG_SZ, NULL, true, 0, native_set_type, native_get_type },
  919. { NULL, NULL, NULL, NULL, NULL }
  920. };