]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Add support for console control signals in Windows
authorDamon Harris <TheDcoder@protonmail.com>
Wed, 13 May 2020 09:35:54 +0000 (15:05 +0530)
committerDamon Harris <TheDcoder@protonmail.com>
Wed, 20 May 2020 17:58:39 +0000 (23:28 +0530)
changes/ticket34211 [new file with mode: 0644]
src/app/main/main.c

diff --git a/changes/ticket34211 b/changes/ticket34211
new file mode 100644 (file)
index 0000000..b454873
--- /dev/null
@@ -0,0 +1,3 @@
+  o Minor features (windows):
+    - Add support for console control signals like Ctrl+C in Windows
+      Closes ticket 34211. Patch from Damon Harris (TheDcoder).
index 689bc526ab500e63215fa228015cf986727d83a4..c2300f7a38378647cd36ba4a1f1a9de521a315e6 100644 (file)
@@ -294,6 +294,19 @@ process_signal(int sig)
   }
 }
 
+#ifdef _WIN32
+/** Activate SIGINT on reciving a control signal in console */
+static BOOL WINAPI
+process_win32_console_ctrl(DWORD ctrl_type)
+{
+  /* Ignore type of the ctrl signal */
+  (void) ctrl_type;
+
+  activate_signal(SIGINT);
+  return TRUE;
+}
+#endif
+
 /**
  * Write current memory usage information to the log.
  */
@@ -496,6 +509,13 @@ handle_signals(void)
                       &signal_handlers[i].signal_value);
     }
   }
+
+#ifdef _WIN32
+    /* Windows lacks traditional POSIX signals but WinAPI provides a function
+     * to handle control signals like Ctrl+C in the console, we can use this to
+     * simulate the SIGINT signal */
+    if (enabled) SetConsoleCtrlHandler(process_win32_console_ctrl, TRUE);
+#endif
 }
 
 /* Cause the signal handler for signal_num to be called in the event loop. */