Minor vcproj tidying.
[nssm.git] / service.cpp
index fbbab20..c920af8 100644 (file)
@@ -1213,7 +1213,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
@@ -1456,7 +1459,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 +1476,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
@@ -1518,6 +1522,7 @@ int start_service(nssm_service_t *service) {
   bool inherit_handles = false;\r
   if (si.dwFlags & STARTF_USESTDHANDLES) inherit_handles = true;\r
   unsigned long flags = service->priority & priority_mask();\r
+  if (service->stdin_pipe) flags |= DETACHED_PROCESS;\r
   if (service->affinity) flags |= CREATE_SUSPENDED;\r
 #ifdef UNICODE\r
   flags |= CREATE_UNICODE_ENVIRONMENT;\r
@@ -1538,7 +1543,7 @@ 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
   if (service->affinity) {\r
     /*\r
@@ -1610,6 +1615,12 @@ int stop_service(nssm_service_t *service, unsigned long exitcode, bool graceful,
     UnregisterWait(service->wait_handle);\r
     service->wait_handle = 0;\r
   }\r
+  if (service->stdin_pipe) {\r
+    CloseHandle(service->stdin_pipe);\r
+    service->stdin_pipe = 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
@@ -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