Browse Source

Technically we should stop ignoring Control-C.

Before signalling our application we implement a null Control-C handler
so we don't get killed ourselves when signalling the process group.  We
should probably restore the default behaviour after signalling.
Iain Patterson 6 years ago
parent
commit
76673b917b
1 changed files with 7 additions and 1 deletions
  1. 7 1
      process.cpp

+ 7 - 1
process.cpp

@@ -241,7 +241,8 @@ int kill_console(nssm_service_t *service, kill_t *k) {
 
   /* Ignore the event ourselves. */
   ret = 0;
-  if (! SetConsoleCtrlHandler(0, TRUE)) {
+  bool ignored = SetConsoleCtrlHandler(0, TRUE);
+  if (! ignored) {
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETCONSOLECTRLHANDLER_FAILED, k->name, error_string(GetLastError()), 0);
     ret = 4;
   }
@@ -262,6 +263,11 @@ int kill_console(nssm_service_t *service, kill_t *k) {
   /* Wait for process to exit. */
   if (await_single_handle(k->status_handle, k->status, k->process_handle, k->name, _T(__FUNCTION__), k->kill_console_delay)) ret = 6;
 
+  /* Remove our handler. */
+  if (ignored && ! SetConsoleCtrlHandler(0, FALSE)) {
+    log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETCONSOLECTRLHANDLER_FAILED, k->name, error_string(GetLastError()), 0);
+  }
+
   return ret;
 }