Skip to content

Handle Ctrl+C (and related) signals on Windows for proper disconnect and cleanup

Currently, when pressing Ctrl+C or Ctrl+Break in a connected OpenConnect client on Windows, the client immediately exits without any notification to the OpenConnect code itself. Under the hood, Windows calls the default signal handler function, which simply calls ExitProcess(). This means that the VPN connection isn't properly disconnected and that subsequently the vpnc-script isn't being called either (with reason disconnect, for cleanup of any routes that were installed while connecting).

This merge request aims to improve this by installing a custom signal handler using the SetConsoleCtrlHandler() function on Windows, similar to how sigaction() is already being used on POSIX.

Some special care had to be taken to not modify the current behavior when pressing Ctrl+C/Ctrl+Break while the client is waiting for user input. Installing a custom signal handler means that the default signal handler doesn't get called anymore, which means that ExitProcess() doesn't get called anymore either, and as a result the client wouldn't exit anymore when receiving these signals on user input. This was resolved by modifying the client's read_stdin() function. This function now immediately returns when no characters were read by the ReadConsole() call and additionally checks GetLastError() for ERROR_OPERATION_ABORTED.

Merge request reports