Fixed crash when stopping the service.
[nssm.git] / service.cpp
index d44998a..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
@@ -39,7 +40,7 @@ SC_HANDLE open_service_manager() {
 \r
 /* About to install the service */\r
 int pre_install_service(int argc, char **argv) {\r
-  /* Show the dialogue box if we didn't give the */\r
+  /* Show the dialogue box if we didn't give the service name and path */\r
   if (argc < 2) return nssm_gui(IDD_INSTALL, argv[0]);\r
 \r
   /* Arguments are optional */\r
@@ -78,7 +79,7 @@ int pre_remove_service(int argc, char **argv) {
   /* Show dialogue box if we didn't pass service name and "confirm" */\r
   if (argc < 2) return nssm_gui(IDD_REMOVE, argv[0]);\r
   if (str_equiv(argv[1], "confirm")) return remove_service(argv[0]);\r
-  fprintf(stderr, "To remove a service without confirmation: nssm remove <servicename> confirm\n");\r
+  print_message(stderr, NSSM_MESSAGE_PRE_REMOVE_SERVICE);\r
   return 100;\r
 }\r
 \r
@@ -87,10 +88,10 @@ int install_service(char *name, char *exe, char *flags) {
   /* Open service manager */\r
   SC_HANDLE services = open_service_manager();\r
   if (! services) {\r
-    fprintf(stderr, "Error opening service manager!\n");\r
+    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
@@ -99,11 +100,11 @@ int install_service(char *name, char *exe, char *flags) {
   char command[CMD_LENGTH];\r
   size_t pathlen = strlen(path);\r
   if (pathlen + 1 >= VALUE_LENGTH) {\r
-    fprintf(stderr, "The full path to " NSSM " is too long!\n");\r
+    print_message(stderr, NSSM_MESSAGE_PATH_TOO_LONG, NSSM);\r
     return 3;\r
   }\r
   if (_snprintf(command, sizeof(command), "\"%s\"", path) < 0) {\r
-    fprintf(stderr, "Out of memory for ImagePath!\n");\r
+    print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY_FOR_IMAGEPATH);\r
     return 4;\r
   }\r
 \r
@@ -118,14 +119,14 @@ int install_service(char *name, char *exe, char *flags) {
   /* Create the service */\r
   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);\r
   if (! service) {\r
-    fprintf(stderr, "Error creating service!\n");\r
+    print_message(stderr, NSSM_MESSAGE_CREATESERVICE_FAILED);\r
     CloseServiceHandle(services);\r
     return 5;\r
   }\r
 \r
   /* Now we need to put the parameters into the registry */\r
   if (create_parameters(name, exe, flags, dir)) {\r
-    fprintf(stderr, "Error setting startup parameters for the service!\n");\r
+    print_message(stderr, NSSM_MESSAGE_CREATE_PARAMETERS_FAILED);\r
     DeleteService(service);\r
     CloseServiceHandle(services);\r
     return 6;\r
@@ -137,7 +138,7 @@ int install_service(char *name, char *exe, char *flags) {
   CloseServiceHandle(service);\r
   CloseServiceHandle(services);\r
 \r
-  printf("Service \"%s\" installed successfully!\n", name);\r
+  print_message(stdout, NSSM_MESSAGE_SERVICE_INSTALLED, name);\r
   return 0;\r
 }\r
 \r
@@ -146,21 +147,21 @@ int remove_service(char *name) {
   /* Open service manager */\r
   SC_HANDLE services = open_service_manager();\r
   if (! services) {\r
-    fprintf(stderr, "Error opening service manager!\n");\r
+    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
-    fprintf(stderr, "Can't open service!");\r
+    print_message(stderr, NSSM_MESSAGE_OPENSERVICE_FAILED);\r
     CloseServiceHandle(services);\r
     return 3;\r
   }\r
 \r
   /* Try to delete the service */\r
   if (! DeleteService(service)) {\r
-    fprintf(stderr, "Error deleting service!\n");\r
+    print_message(stderr, NSSM_MESSAGE_DELETESERVICE_FAILED);\r
     CloseServiceHandle(service);\r
     CloseServiceHandle(services);\r
     return 4;\r
@@ -170,7 +171,7 @@ int remove_service(char *name) {
   CloseServiceHandle(service);\r
   CloseServiceHandle(services);\r
 \r
-  printf("Service \"%s\" removed successfully!\n", name);\r
+  print_message(stdout, NSSM_MESSAGE_SERVICE_REMOVED, name);\r
   return 0;\r
 }\r
 \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