service.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. #include "nssm.h"
  2. bool is_admin;
  3. SERVICE_STATUS service_status;
  4. SERVICE_STATUS_HANDLE service_handle;
  5. HANDLE process_handle;
  6. HANDLE wait_handle;
  7. unsigned long pid;
  8. static char service_name[SERVICE_NAME_LENGTH];
  9. char exe[EXE_LENGTH];
  10. char flags[CMD_LENGTH];
  11. char dir[MAX_PATH];
  12. bool stopping;
  13. bool allow_restart;
  14. unsigned long throttle_delay;
  15. unsigned long stop_method;
  16. HANDLE throttle_timer;
  17. LARGE_INTEGER throttle_duetime;
  18. FILETIME creation_time;
  19. static enum { NSSM_EXIT_RESTART, NSSM_EXIT_IGNORE, NSSM_EXIT_REALLY, NSSM_EXIT_UNCLEAN } exit_actions;
  20. static const char *exit_action_strings[] = { "Restart", "Ignore", "Exit", "Suicide", 0 };
  21. static unsigned long throttle;
  22. static inline int throttle_milliseconds() {
  23. /* pow() operates on doubles. */
  24. int ret = 1; for (unsigned long i = 1; i < throttle; i++) ret *= 2;
  25. return ret * 1000;
  26. }
  27. /* Connect to the service manager */
  28. SC_HANDLE open_service_manager() {
  29. SC_HANDLE ret = OpenSCManager(0, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS);
  30. if (! ret) {
  31. if (is_admin) log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENSCMANAGER_FAILED, 0);
  32. return 0;
  33. }
  34. return ret;
  35. }
  36. /* About to install the service */
  37. int pre_install_service(int argc, char **argv) {
  38. /* Show the dialogue box if we didn't give the service name and path */
  39. if (argc < 2) return nssm_gui(IDD_INSTALL, argv[0]);
  40. /* Arguments are optional */
  41. char *flags;
  42. size_t flagslen = 0;
  43. size_t s = 0;
  44. int i;
  45. for (i = 2; i < argc; i++) flagslen += strlen(argv[i]) + 1;
  46. if (! flagslen) flagslen = 1;
  47. flags = (char *) HeapAlloc(GetProcessHeap(), 0, flagslen);
  48. if (! flags) {
  49. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "flags", "pre_install_service()", 0);
  50. return 2;
  51. }
  52. ZeroMemory(flags, flagslen);
  53. /*
  54. This probably isn't UTF8-safe and should use std::string or something
  55. but it's been broken for the best part of a decade and due for a rewrite
  56. anyway so it'll do as a quick-'n'-dirty fix. Note that we don't free
  57. the flags buffer but as the program exits that isn't a big problem.
  58. */
  59. for (i = 2; i < argc; i++) {
  60. size_t len = strlen(argv[i]);
  61. memmove(flags + s, argv[i], len);
  62. s += len;
  63. if (i < argc - 1) flags[s++] = ' ';
  64. }
  65. return install_service(argv[0], argv[1], flags);
  66. }
  67. /* About to remove the service */
  68. int pre_remove_service(int argc, char **argv) {
  69. /* Show dialogue box if we didn't pass service name and "confirm" */
  70. if (argc < 2) return nssm_gui(IDD_REMOVE, argv[0]);
  71. if (str_equiv(argv[1], "confirm")) return remove_service(argv[0]);
  72. print_message(stderr, NSSM_MESSAGE_PRE_REMOVE_SERVICE);
  73. return 100;
  74. }
  75. /* Install the service */
  76. int install_service(char *name, char *exe, char *flags) {
  77. /* Open service manager */
  78. SC_HANDLE services = open_service_manager();
  79. if (! services) {
  80. print_message(stderr, NSSM_MESSAGE_OPEN_SERVICE_MANAGER_FAILED);
  81. return 2;
  82. }
  83. /* Get path of this program */
  84. char path[MAX_PATH];
  85. GetModuleFileName(0, path, MAX_PATH);
  86. /* Construct command */
  87. char command[CMD_LENGTH];
  88. size_t pathlen = strlen(path);
  89. if (pathlen + 1 >= VALUE_LENGTH) {
  90. print_message(stderr, NSSM_MESSAGE_PATH_TOO_LONG, NSSM);
  91. return 3;
  92. }
  93. if (_snprintf_s(command, sizeof(command), _TRUNCATE, "\"%s\"", path) < 0) {
  94. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY_FOR_IMAGEPATH);
  95. return 4;
  96. }
  97. /* Work out directory name */
  98. size_t len = strlen(exe);
  99. size_t i;
  100. for (i = len; i && exe[i] != '\\' && exe[i] != '/'; i--);
  101. char dir[MAX_PATH];
  102. memmove(dir, exe, i);
  103. dir[i] = '\0';
  104. /* Create the service */
  105. SC_HANDLE service = CreateService(services, name, name, SC_MANAGER_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, command, 0, 0, 0, 0, 0);
  106. if (! service) {
  107. print_message(stderr, NSSM_MESSAGE_CREATESERVICE_FAILED);
  108. CloseServiceHandle(services);
  109. return 5;
  110. }
  111. /* Now we need to put the parameters into the registry */
  112. if (create_parameters(name, exe, flags, dir)) {
  113. print_message(stderr, NSSM_MESSAGE_CREATE_PARAMETERS_FAILED);
  114. DeleteService(service);
  115. CloseServiceHandle(services);
  116. return 6;
  117. }
  118. set_service_recovery(service, name);
  119. /* Cleanup */
  120. CloseServiceHandle(service);
  121. CloseServiceHandle(services);
  122. print_message(stdout, NSSM_MESSAGE_SERVICE_INSTALLED, name);
  123. return 0;
  124. }
  125. /* Remove the service */
  126. int remove_service(char *name) {
  127. /* Open service manager */
  128. SC_HANDLE services = open_service_manager();
  129. if (! services) {
  130. print_message(stderr, NSSM_MESSAGE_OPEN_SERVICE_MANAGER_FAILED);
  131. return 2;
  132. }
  133. /* Try to open the service */
  134. SC_HANDLE service = OpenService(services, name, SC_MANAGER_ALL_ACCESS);
  135. if (! service) {
  136. print_message(stderr, NSSM_MESSAGE_OPENSERVICE_FAILED);
  137. CloseServiceHandle(services);
  138. return 3;
  139. }
  140. /* Try to delete the service */
  141. if (! DeleteService(service)) {
  142. print_message(stderr, NSSM_MESSAGE_DELETESERVICE_FAILED);
  143. CloseServiceHandle(service);
  144. CloseServiceHandle(services);
  145. return 4;
  146. }
  147. /* Cleanup */
  148. CloseServiceHandle(service);
  149. CloseServiceHandle(services);
  150. print_message(stdout, NSSM_MESSAGE_SERVICE_REMOVED, name);
  151. return 0;
  152. }
  153. /* Service initialisation */
  154. void WINAPI service_main(unsigned long argc, char **argv) {
  155. if (_snprintf_s(service_name, sizeof(service_name), _TRUNCATE, "%s", argv[0]) < 0) {
  156. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "service_name", "service_main()", 0);
  157. return;
  158. }
  159. /* Initialise status */
  160. ZeroMemory(&service_status, sizeof(service_status));
  161. service_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS;
  162. service_status.dwControlsAccepted = SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE;
  163. service_status.dwWin32ExitCode = NO_ERROR;
  164. service_status.dwServiceSpecificExitCode = 0;
  165. service_status.dwCheckPoint = 0;
  166. service_status.dwWaitHint = NSSM_WAITHINT_MARGIN;
  167. /* Signal we AREN'T running the server */
  168. process_handle = 0;
  169. pid = 0;
  170. /* Register control handler */
  171. service_handle = RegisterServiceCtrlHandlerEx(NSSM, service_control_handler, 0);
  172. if (! service_handle) {
  173. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_REGISTERSERVICECTRLHANDER_FAILED, error_string(GetLastError()), 0);
  174. return;
  175. }
  176. log_service_control(service_name, 0, true);
  177. service_status.dwCurrentState = SERVICE_START_PENDING;
  178. service_status.dwWaitHint = throttle_delay + NSSM_WAITHINT_MARGIN;
  179. SetServiceStatus(service_handle, &service_status);
  180. if (is_admin) {
  181. /* Try to create the exit action parameters; we don't care if it fails */
  182. create_exit_action(argv[0], exit_action_strings[0]);
  183. set_service_recovery(0, service_name);
  184. }
  185. /* Used for signalling a resume if the service pauses when throttled. */
  186. throttle_timer = CreateWaitableTimer(0, 1, 0);
  187. if (! throttle_timer) {
  188. log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_CREATEWAITABLETIMER_FAILED, service_name, error_string(GetLastError()), 0);
  189. }
  190. monitor_service();
  191. }
  192. /* Make sure service recovery actions are taken where necessary */
  193. void set_service_recovery(SC_HANDLE service, char *service_name) {
  194. SC_HANDLE services = 0;
  195. if (! service) {
  196. services = open_service_manager();
  197. if (! services) return;
  198. service = OpenService(services, service_name, SC_MANAGER_ALL_ACCESS);
  199. if (! service) return;
  200. }
  201. SERVICE_FAILURE_ACTIONS_FLAG flag;
  202. ZeroMemory(&flag, sizeof(flag));
  203. flag.fFailureActionsOnNonCrashFailures = true;
  204. /* This functionality was added in Vista so the call may fail */
  205. if (! ChangeServiceConfig2(service, SERVICE_CONFIG_FAILURE_ACTIONS_FLAG, &flag)) {
  206. unsigned long error = GetLastError();
  207. /* Pre-Vista we expect to fail with ERROR_INVALID_LEVEL */
  208. if (error != ERROR_INVALID_LEVEL) {
  209. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CHANGESERVICECONFIG2_FAILED, service_name, error_string(error), 0);
  210. }
  211. }
  212. if (services) {
  213. CloseServiceHandle(service);
  214. CloseServiceHandle(services);
  215. }
  216. }
  217. int monitor_service() {
  218. /* Set service status to started */
  219. int ret = start_service();
  220. if (ret) {
  221. char code[16];
  222. _snprintf_s(code, sizeof(code), _TRUNCATE, "%d", ret);
  223. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_START_SERVICE_FAILED, exe, service_name, ret, 0);
  224. return ret;
  225. }
  226. log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_STARTED_SERVICE, exe, flags, service_name, dir, 0);
  227. /* Monitor service service */
  228. if (! RegisterWaitForSingleObject(&wait_handle, process_handle, end_service, (void *) pid, INFINITE, WT_EXECUTEONLYONCE | WT_EXECUTELONGFUNCTION)) {
  229. log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_REGISTERWAITFORSINGLEOBJECT_FAILED, service_name, exe, error_string(GetLastError()), 0);
  230. }
  231. return 0;
  232. }
  233. char *service_control_text(unsigned long control) {
  234. switch (control) {
  235. /* HACK: there is no SERVICE_CONTROL_START constant */
  236. case 0: return "START";
  237. case SERVICE_CONTROL_STOP: return "STOP";
  238. case SERVICE_CONTROL_SHUTDOWN: return "SHUTDOWN";
  239. case SERVICE_CONTROL_PAUSE: return "PAUSE";
  240. case SERVICE_CONTROL_CONTINUE: return "CONTINUE";
  241. case SERVICE_CONTROL_INTERROGATE: return "INTERROGATE";
  242. default: return 0;
  243. }
  244. }
  245. void log_service_control(char *service_name, unsigned long control, bool handled) {
  246. char *text = service_control_text(control);
  247. unsigned long event;
  248. if (! text) {
  249. /* "0x" + 8 x hex + NULL */
  250. text = (char *) HeapAlloc(GetProcessHeap(), 0, 11);
  251. if (! text) {
  252. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "control code", "log_service_control", 0);
  253. return;
  254. }
  255. if (_snprintf_s(text, 11, _TRUNCATE, "0x%08x", control) < 0) {
  256. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "control code", "log_service_control", 0);
  257. HeapFree(GetProcessHeap(), 0, text);
  258. return;
  259. }
  260. event = NSSM_EVENT_SERVICE_CONTROL_UNKNOWN;
  261. }
  262. else if (handled) event = NSSM_EVENT_SERVICE_CONTROL_HANDLED;
  263. else event = NSSM_EVENT_SERVICE_CONTROL_NOT_HANDLED;
  264. log_event(EVENTLOG_INFORMATION_TYPE, event, service_name, text, 0);
  265. if (event == NSSM_EVENT_SERVICE_CONTROL_UNKNOWN) {
  266. HeapFree(GetProcessHeap(), 0, text);
  267. }
  268. }
  269. /* Service control handler */
  270. unsigned long WINAPI service_control_handler(unsigned long control, unsigned long event, void *data, void *context) {
  271. switch (control) {
  272. case SERVICE_CONTROL_INTERROGATE:
  273. /* We always keep the service status up-to-date so this is a no-op. */
  274. return NO_ERROR;
  275. case SERVICE_CONTROL_SHUTDOWN:
  276. case SERVICE_CONTROL_STOP:
  277. log_service_control(service_name, control, true);
  278. stop_service(0, true, true);
  279. return NO_ERROR;
  280. case SERVICE_CONTROL_CONTINUE:
  281. log_service_control(service_name, control, true);
  282. if (! throttle_timer) return ERROR_CALL_NOT_IMPLEMENTED;
  283. throttle = 0;
  284. ZeroMemory(&throttle_duetime, sizeof(throttle_duetime));
  285. SetWaitableTimer(throttle_timer, &throttle_duetime, 0, 0, 0, 0);
  286. service_status.dwCurrentState = SERVICE_CONTINUE_PENDING;
  287. service_status.dwWaitHint = throttle_milliseconds() + NSSM_WAITHINT_MARGIN;
  288. log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_RESET_THROTTLE, service_name, 0);
  289. SetServiceStatus(service_handle, &service_status);
  290. return NO_ERROR;
  291. case SERVICE_CONTROL_PAUSE:
  292. /*
  293. We don't accept pause messages but it isn't possible to register
  294. only for continue messages so we have to handle this case.
  295. */
  296. log_service_control(service_name, control, false);
  297. return ERROR_CALL_NOT_IMPLEMENTED;
  298. }
  299. /* Unknown control */
  300. log_service_control(service_name, control, false);
  301. return ERROR_CALL_NOT_IMPLEMENTED;
  302. }
  303. /* Start the service */
  304. int start_service() {
  305. stopping = false;
  306. allow_restart = true;
  307. if (process_handle) return 0;
  308. /* Allocate a STARTUPINFO structure for a new process */
  309. STARTUPINFO si;
  310. ZeroMemory(&si, sizeof(si));
  311. si.cb = sizeof(si);
  312. /* Allocate a PROCESSINFO structure for the process */
  313. PROCESS_INFORMATION pi;
  314. ZeroMemory(&pi, sizeof(pi));
  315. /* Get startup parameters */
  316. char *env = 0;
  317. int ret = get_parameters(service_name, exe, sizeof(exe), flags, sizeof(flags), dir, sizeof(dir), &env, &throttle_delay, &stop_method, &si);
  318. if (ret) {
  319. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_GET_PARAMETERS_FAILED, service_name, 0);
  320. return stop_service(2, true, true);
  321. }
  322. /* Launch executable with arguments */
  323. char cmd[CMD_LENGTH];
  324. if (_snprintf_s(cmd, sizeof(cmd), _TRUNCATE, "\"%s\" %s", exe, flags) < 0) {
  325. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "command line", "start_service", 0);
  326. close_output_handles(&si);
  327. return stop_service(2, true, true);
  328. }
  329. throttle_restart();
  330. bool inherit_handles = false;
  331. if (si.dwFlags & STARTF_USESTDHANDLES) inherit_handles = true;
  332. if (! CreateProcess(0, cmd, 0, 0, inherit_handles, 0, env, dir, &si, &pi)) {
  333. unsigned long error = GetLastError();
  334. if (error == ERROR_INVALID_PARAMETER && env) log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPROCESS_FAILED_INVALID_ENVIRONMENT, service_name, exe, NSSM_REG_ENV, 0);
  335. else log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPROCESS_FAILED, service_name, exe, error_string(error), 0);
  336. close_output_handles(&si);
  337. return stop_service(3, true, true);
  338. }
  339. process_handle = pi.hProcess;
  340. pid = pi.dwProcessId;
  341. if (get_process_creation_time(process_handle, &creation_time)) ZeroMemory(&creation_time, sizeof(creation_time));
  342. close_output_handles(&si);
  343. /* Wait for a clean startup. */
  344. if (WaitForSingleObject(process_handle, throttle_delay) == WAIT_TIMEOUT) throttle = 0;
  345. /* Signal successful start */
  346. service_status.dwCurrentState = SERVICE_RUNNING;
  347. SetServiceStatus(service_handle, &service_status);
  348. return 0;
  349. }
  350. /* Stop the service */
  351. int stop_service(unsigned long exitcode, bool graceful, bool default_action) {
  352. allow_restart = false;
  353. if (wait_handle) UnregisterWait(wait_handle);
  354. if (default_action && ! exitcode && ! graceful) {
  355. log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_GRACEFUL_SUICIDE, service_name, exe, exit_action_strings[NSSM_EXIT_UNCLEAN], exit_action_strings[NSSM_EXIT_UNCLEAN], exit_action_strings[NSSM_EXIT_UNCLEAN], exit_action_strings[NSSM_EXIT_REALLY] ,0);
  356. graceful = true;
  357. }
  358. /* Signal we are stopping */
  359. if (graceful) {
  360. service_status.dwCurrentState = SERVICE_STOP_PENDING;
  361. service_status.dwWaitHint = NSSM_KILL_WINDOW_GRACE_PERIOD + NSSM_KILL_THREADS_GRACE_PERIOD + NSSM_WAITHINT_MARGIN;
  362. SetServiceStatus(service_handle, &service_status);
  363. }
  364. /* Nothing to do if service isn't running */
  365. if (pid) {
  366. /* Shut down service */
  367. log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_TERMINATEPROCESS, service_name, exe, 0);
  368. kill_process(service_name, stop_method, process_handle, pid, 0);
  369. }
  370. else log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_PROCESS_ALREADY_STOPPED, service_name, exe, 0);
  371. end_service((void *) pid, true);
  372. /* Signal we stopped */
  373. if (graceful) {
  374. service_status.dwCurrentState = SERVICE_STOPPED;
  375. if (exitcode) {
  376. service_status.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;
  377. service_status.dwServiceSpecificExitCode = exitcode;
  378. }
  379. else {
  380. service_status.dwWin32ExitCode = NO_ERROR;
  381. service_status.dwServiceSpecificExitCode = 0;
  382. }
  383. SetServiceStatus(service_handle, &service_status);
  384. }
  385. return exitcode;
  386. }
  387. /* Callback function triggered when the server exits */
  388. void CALLBACK end_service(void *arg, unsigned char why) {
  389. if (stopping) return;
  390. stopping = true;
  391. pid = (unsigned long) arg;
  392. /* Check exit code */
  393. unsigned long exitcode = 0;
  394. char code[16];
  395. FILETIME exit_time;
  396. GetExitCodeProcess(process_handle, &exitcode);
  397. if (exitcode == STILL_ACTIVE || get_process_exit_time(process_handle, &exit_time)) GetSystemTimeAsFileTime(&exit_time);
  398. CloseHandle(process_handle);
  399. /*
  400. Log that the service ended BEFORE logging about killing the process
  401. tree. See below for the possible values of the why argument.
  402. */
  403. if (! why) {
  404. _snprintf_s(code, sizeof(code), _TRUNCATE, "%d", exitcode);
  405. log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_ENDED_SERVICE, exe, service_name, code, 0);
  406. }
  407. /* Clean up. */
  408. kill_process_tree(service_name, stop_method, pid, exitcode, pid, &creation_time, &exit_time);
  409. /*
  410. The why argument is true if our wait timed out or false otherwise.
  411. Our wait is infinite so why will never be true when called by the system.
  412. If it is indeed true, assume we were called from stop_service() because
  413. this is a controlled shutdown, and don't take any restart action.
  414. */
  415. if (why) return;
  416. if (! allow_restart) return;
  417. /* What action should we take? */
  418. int action = NSSM_EXIT_RESTART;
  419. unsigned char action_string[ACTION_LEN];
  420. bool default_action;
  421. if (! get_exit_action(service_name, &exitcode, action_string, &default_action)) {
  422. for (int i = 0; exit_action_strings[i]; i++) {
  423. if (! _strnicmp((const char *) action_string, exit_action_strings[i], ACTION_LEN)) {
  424. action = i;
  425. break;
  426. }
  427. }
  428. }
  429. process_handle = 0;
  430. pid = 0;
  431. switch (action) {
  432. /* Try to restart the service or return failure code to service manager */
  433. case NSSM_EXIT_RESTART:
  434. log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_EXIT_RESTART, service_name, code, exit_action_strings[action], exe, 0);
  435. while (monitor_service()) {
  436. log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_RESTART_SERVICE_FAILED, exe, service_name, 0);
  437. Sleep(30000);
  438. }
  439. break;
  440. /* Do nothing, just like srvany would */
  441. case NSSM_EXIT_IGNORE:
  442. log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_EXIT_IGNORE, service_name, code, exit_action_strings[action], exe, 0);
  443. Sleep(INFINITE);
  444. break;
  445. /* Tell the service manager we are finished */
  446. case NSSM_EXIT_REALLY:
  447. log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_EXIT_REALLY, service_name, code, exit_action_strings[action], 0);
  448. stop_service(exitcode, true, default_action);
  449. break;
  450. /* Fake a crash so pre-Vista service managers will run recovery actions. */
  451. case NSSM_EXIT_UNCLEAN:
  452. log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_EXIT_UNCLEAN, service_name, code, exit_action_strings[action], 0);
  453. exit(stop_service(exitcode, false, default_action));
  454. break;
  455. }
  456. }
  457. void throttle_restart() {
  458. /* This can't be a restart if the service is already running. */
  459. if (! throttle++) return;
  460. int ms = throttle_milliseconds();
  461. if (throttle > 7) throttle = 8;
  462. char threshold[8], milliseconds[8];
  463. _snprintf_s(threshold, sizeof(threshold), _TRUNCATE, "%d", throttle_delay);
  464. _snprintf_s(milliseconds, sizeof(milliseconds), _TRUNCATE, "%d", ms);
  465. log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_THROTTLED, service_name, threshold, milliseconds, 0);
  466. if (throttle_timer) {
  467. ZeroMemory(&throttle_duetime, sizeof(throttle_duetime));
  468. throttle_duetime.QuadPart = 0 - (ms * 10000LL);
  469. SetWaitableTimer(throttle_timer, &throttle_duetime, 0, 0, 0, 0);
  470. }
  471. service_status.dwCurrentState = SERVICE_PAUSED;
  472. SetServiceStatus(service_handle, &service_status);
  473. if (throttle_timer) WaitForSingleObject(throttle_timer, INFINITE);
  474. else Sleep(ms);
  475. }