Fixed crash when stopping the service.
[nssm.git] / service.cpp
index 766031e..81e46ef 100644 (file)
@@ -14,6 +14,7 @@ bool stopping;
 unsigned long throttle_delay;\r
 HANDLE throttle_timer;\r
 LARGE_INTEGER throttle_duetime;\r
+FILETIME creation_time;\r
 \r
 static enum { NSSM_EXIT_RESTART, NSSM_EXIT_IGNORE, NSSM_EXIT_REALLY, NSSM_EXIT_UNCLEAN } exit_actions;\r
 static const char *exit_action_strings[] = { "Restart", "Ignore", "Exit", "Suicide", 0 };\r
@@ -90,7 +91,7 @@ int install_service(char *name, char *exe, char *flags) {
     print_message(stderr, NSSM_MESSAGE_OPEN_SERVICE_MANAGER_FAILED);\r
     return 2;\r
   }\r
-  \r
+\r
   /* Get path of this program */\r
   char path[MAX_PATH];\r
   GetModuleFileName(0, path, MAX_PATH);\r
@@ -149,7 +150,7 @@ int remove_service(char *name) {
     print_message(stderr, NSSM_MESSAGE_OPEN_SERVICE_MANAGER_FAILED);\r
     return 2;\r
   }\r
-  \r
+\r
   /* Try to open the service */\r
   SC_HANDLE service = OpenService(services, name, SC_MANAGER_ALL_ACCESS);\r
   if (! service) {\r
@@ -391,6 +392,8 @@ int start_service() {
   process_handle = pi.hProcess;\r
   pid = pi.dwProcessId;\r
 \r
+  if (get_process_creation_time(process_handle, &creation_time)) ZeroMemory(&creation_time, sizeof(creation_time));\r
+\r
   /* Signal successful start */\r
   service_status.dwCurrentState = SERVICE_RUNNING;\r
   SetServiceStatus(service_handle, &service_status);\r
@@ -415,12 +418,11 @@ int stop_service(unsigned long exitcode, bool graceful, bool default_action) {
     SetServiceStatus(service_handle, &service_status);\r
   }\r
 \r
-  /* Nothing to do if server isn't running */\r
+  /* Nothing to do if service isn't running */\r
   if (pid) {\r
-    /* Shut down server */\r
+    /* Shut down service */\r
     log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_TERMINATEPROCESS, service_name, exe, 0);\r
     kill_process(service_name, process_handle, pid, 0);\r
-    process_handle = 0;\r
   }\r
   else log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_PROCESS_ALREADY_STOPPED, service_name, exe, 0);\r
 \r
@@ -453,10 +455,23 @@ void CALLBACK end_service(void *arg, unsigned char why) {
 \r
   /* Check exit code */\r
   unsigned long exitcode = 0;\r
+  char code[16];\r
+  FILETIME exit_time;\r
   GetExitCodeProcess(process_handle, &exitcode);\r
+  if (exitcode == STILL_ACTIVE || get_process_exit_time(process_handle, &exit_time)) GetSystemTimeAsFileTime(&exit_time);\r
+  CloseHandle(process_handle);\r
+\r
+  /*\r
+    Log that the service ended BEFORE logging about killing the process\r
+    tree.  See below for the possible values of the why argument.\r
+  */\r
+  if (! why) {\r
+    _snprintf(code, sizeof(code), "%d", exitcode);\r
+    log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_ENDED_SERVICE, exe, service_name, code, 0);\r
+  }\r
 \r
   /* Clean up. */\r
-  kill_process_tree(service_name, pid, exitcode, pid);\r
+  kill_process_tree(service_name, pid, exitcode, pid, &creation_time, &exit_time);\r
 \r
   /*\r
     The why argument is true if our wait timed out or false otherwise.\r
@@ -466,10 +481,6 @@ void CALLBACK end_service(void *arg, unsigned char why) {
   */\r
   if (why) return;\r
 \r
-  char code[16];\r
-  _snprintf(code, sizeof(code), "%d", exitcode);\r
-  log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_ENDED_SERVICE, exe, service_name, code, 0);\r
-\r
   /* What action should we take? */\r
   int action = NSSM_EXIT_RESTART;\r
   unsigned char action_string[ACTION_LEN];\r