Allow specifying output streams.
[nssm.git] / service.cpp
index 766031e..21726df 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
@@ -318,6 +319,10 @@ void log_service_control(char *service_name, unsigned long control, bool handled
 /* Service control handler */\r
 unsigned long WINAPI service_control_handler(unsigned long control, unsigned long event, void *data, void *context) {\r
   switch (control) {\r
+    case SERVICE_CONTROL_INTERROGATE:\r
+      /* We always keep the service status up-to-date so this is a no-op. */\r
+      return NO_ERROR;\r
+\r
     case SERVICE_CONTROL_SHUTDOWN:\r
     case SERVICE_CONTROL_STOP:\r
       log_service_control(service_name, control, true);\r
@@ -367,7 +372,7 @@ int start_service() {
 \r
   /* Get startup parameters */\r
   char *env = 0;\r
-  int ret = get_parameters(service_name, exe, sizeof(exe), flags, sizeof(flags), dir, sizeof(dir), &env, &throttle_delay);\r
+  int ret = get_parameters(service_name, exe, sizeof(exe), flags, sizeof(flags), dir, sizeof(dir), &env, &throttle_delay, &si);\r
   if (ret) {\r
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_GET_PARAMETERS_FAILED, service_name, 0);\r
     return stop_service(2, true, true);\r
@@ -377,20 +382,27 @@ int start_service() {
   char cmd[CMD_LENGTH];\r
   if (_snprintf(cmd, sizeof(cmd), "\"%s\" %s", exe, flags) < 0) {\r
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "command line", "start_service", 0);\r
+    close_output_handles(&si);\r
     return stop_service(2, true, true);\r
   }\r
 \r
   throttle_restart();\r
 \r
-  if (! CreateProcess(0, cmd, 0, 0, false, 0, env, dir, &si, &pi)) {\r
+  bool inherit_handles = (si.dwFlags & STARTF_USESTDHANDLES);\r
+  if (! CreateProcess(0, cmd, 0, 0, inherit_handles, 0, env, dir, &si, &pi)) {\r
     unsigned long error = GetLastError();\r
     if (error == ERROR_INVALID_PARAMETER && env) log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPROCESS_FAILED_INVALID_ENVIRONMENT, service_name, exe, NSSM_REG_ENV, 0);\r
     else log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPROCESS_FAILED, service_name, exe, error_string(error), 0);\r
+    close_output_handles(&si);\r
     return stop_service(3, true, true);\r
   }\r
   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
+  close_output_handles(&si);\r
+\r
   /* Signal successful start */\r
   service_status.dwCurrentState = SERVICE_RUNNING;\r
   SetServiceStatus(service_handle, &service_status);\r
@@ -415,12 +427,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 +464,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 +490,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