Allocate a new console for stdin.
[nssm.git] / service.cpp
index fbbab20..284b5ec 100644 (file)
@@ -681,6 +681,7 @@ void cleanup_nssm_service(nssm_service_t *service) {
   if (service->wait_handle) UnregisterWait(service->process_handle);\r
   if (service->throttle_section_initialised) DeleteCriticalSection(&service->throttle_section);\r
   if (service->throttle_timer) CloseHandle(service->throttle_timer);\r
+  if (service->initial_env) FreeEnvironmentStrings(service->initial_env);\r
   HeapFree(GetProcessHeap(), 0, service);\r
 }\r
 \r
@@ -1213,7 +1214,10 @@ int control_service(unsigned long control, int argc, TCHAR **argv) {
     }\r
     else {\r
       CloseHandle(service_handle);\r
-      _ftprintf(stderr, _T("%s: %s %s"), canonical_name, service_control_text(control), error_string(error));\r
+      _ftprintf(stderr, _T("%s: %s: %s"), canonical_name, service_control_text(control), error_string(error));\r
+      if (error == ERROR_SERVICE_NOT_ACTIVE) {\r
+        if (control == SERVICE_CONTROL_SHUTDOWN || control == SERVICE_CONTROL_STOP) return 0;\r
+      }\r
       return 1;\r
     }\r
   }\r
@@ -1321,6 +1325,9 @@ void WINAPI service_main(unsigned long argc, TCHAR **argv) {
     }\r
   }\r
 \r
+  /* Remember our initial environment. */\r
+  service->initial_env = GetEnvironmentStrings();\r
+\r
   monitor_service(service);\r
 }\r
 \r
@@ -1456,7 +1463,8 @@ unsigned long WINAPI service_control_handler(unsigned long control, unsigned lon
         ZeroMemory(&service->throttle_duetime, sizeof(service->throttle_duetime));\r
         SetWaitableTimer(service->throttle_timer, &service->throttle_duetime, 0, 0, 0, 0);\r
       }\r
-      service->status.dwCurrentState = SERVICE_CONTINUE_PENDING;\r
+      /* We can't continue if the application is running! */\r
+      if (! service->process_handle) service->status.dwCurrentState = SERVICE_CONTINUE_PENDING;\r
       service->status.dwWaitHint = throttle_milliseconds(service->throttle) + NSSM_WAITHINT_MARGIN;\r
       log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_RESET_THROTTLE, service->name, 0);\r
       SetServiceStatus(service->status_handle, &service->status);\r
@@ -1472,8 +1480,8 @@ unsigned long WINAPI service_control_handler(unsigned long control, unsigned lon
 \r
     case NSSM_SERVICE_CONTROL_ROTATE:\r
       log_service_control(service->name, control, true);\r
-      if (service->rotate_stdout_online) service->rotate_stdout_online = NSSM_ROTATE_ONLINE_ASAP;\r
-      if (service->rotate_stdout_online) service->rotate_stderr_online = NSSM_ROTATE_ONLINE_ASAP;\r
+      if (service->rotate_stdout_online == NSSM_ROTATE_ONLINE) service->rotate_stdout_online = NSSM_ROTATE_ONLINE_ASAP;\r
+      if (service->rotate_stderr_online == NSSM_ROTATE_ONLINE) service->rotate_stderr_online = NSSM_ROTATE_ONLINE_ASAP;\r
       return NO_ERROR;\r
   }\r
 \r
@@ -1515,22 +1523,20 @@ int start_service(nssm_service_t *service) {
 \r
   throttle_restart(service);\r
 \r
+  /* Set the environment. */\r
+  if (service->env) duplicate_environment(service->env);\r
+  if (service->env_extra) set_environment_block(service->env_extra);\r
+\r
   bool inherit_handles = false;\r
   if (si.dwFlags & STARTF_USESTDHANDLES) inherit_handles = true;\r
   unsigned long flags = service->priority & priority_mask();\r
   if (service->affinity) flags |= CREATE_SUSPENDED;\r
-#ifdef UNICODE\r
-  flags |= CREATE_UNICODE_ENVIRONMENT;\r
-#endif\r
-  if (! CreateProcess(0, cmd, 0, 0, inherit_handles, flags, service->env, service->dir, &si, &pi)) {\r
+  if (! CreateProcess(0, cmd, 0, 0, inherit_handles, flags, 0, service->dir, &si, &pi)) {\r
     unsigned long exitcode = 3;\r
     unsigned long error = GetLastError();\r
-    if (error == ERROR_INVALID_PARAMETER && service->env) {\r
-      log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPROCESS_FAILED_INVALID_ENVIRONMENT, service->name, service->exe, NSSM_REG_ENV, 0);\r
-      if (test_environment(service->env)) exitcode = 4;\r
-    }\r
-    else log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPROCESS_FAILED, service->name, service->exe, error_string(error), 0);\r
+    log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPROCESS_FAILED, service->name, service->exe, error_string(error), 0);\r
     close_output_handles(&si);\r
+    duplicate_environment(service->initial_env);\r
     return stop_service(service, exitcode, true, true);\r
   }\r
   service->process_handle = pi.hProcess;\r
@@ -1538,7 +1544,10 @@ int start_service(nssm_service_t *service) {
 \r
   if (get_process_creation_time(service->process_handle, &service->creation_time)) ZeroMemory(&service->creation_time, sizeof(service->creation_time));\r
 \r
-  close_output_handles(&si, ! service->rotate_stdout_online, ! service->rotate_stderr_online);\r
+  close_output_handles(&si);\r
+\r
+  /* Restore our environment. */\r
+  duplicate_environment(service->initial_env);\r
 \r
   if (service->affinity) {\r
     /*\r
@@ -1611,6 +1620,8 @@ int stop_service(nssm_service_t *service, unsigned long exitcode, bool graceful,
     service->wait_handle = 0;\r
   }\r
 \r
+  service->rotate_stdout_online = service->rotate_stderr_online = NSSM_ROTATE_OFFLINE;\r
+\r
   if (default_action && ! exitcode && ! graceful) {\r
     log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_GRACEFUL_SUICIDE, service->name, service->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);\r
     graceful = true;\r
@@ -1658,6 +1669,8 @@ void CALLBACK end_service(void *arg, unsigned char why) {
 \r
   service->stopping = true;\r
 \r
+  service->rotate_stdout_online = service->rotate_stderr_online = NSSM_ROTATE_OFFLINE;\r
+\r
   /* Check exit code */\r
   unsigned long exitcode = 0;\r
   TCHAR code[16];\r