From 6c8d12989f57d1f6949934eedee53bf0b0b39325 Mon Sep 17 00:00:00 2001 From: Christophe Bucher Date: Wed, 17 Jun 2015 18:04:41 +0200 Subject: [PATCH] git-wrapper: do not let the Ctrl-C event kill the wrapper ... while waiting for the child process to finish. The Git wrapper serves, among other things, as git-cmd.exe. In that role, its primary purpose is to provide an interactive cmd window that knows where to find Git. A secondary use of git-cmd.exe is to be able to launch other console processes that know about Git, e.g. when ConsoleZ wants to call an interactive Bash (it cannot call git-bash.exe because that would open a new MinTTY window). To this end, git-cmd.exe supports the --command=... command-line option. The interactive bash would be called like this: git-cmd --command=usr\bin\bash.exe -l -i The command-line arguments after the --command=... options are simply passed through to the command itself. If no --command=... option is specified, git-cmd.exe defaults to cmd.exe. Once git-cmd.exe is launched, it finds the top-level directory of the Git for Windows installation and then launches the command as a child process. And this is where things get a little bit tricky: When the user presses CTRL-C, the cmd window receives WM_KEYDOWN/WM_KEYUP messages which are then handled by the TranslateMessage function that generates a CTRL-C event that is sent to the console processes running in the console window (i.e. both git-cmd.exe and the child process). If no Console Ctrl Handlers have been registered, the git-cmd.exe process will simply be terminated, without having waited for the interactive Bash to quit (it does not quit, of course, because it handles Ctrl+C by terminating any process launched from within the Bash). Now both cmd and the Bash compete for user input. Luckily, the solution is very easy: the Win32 API sports a SetConsoleCtrlHandler() function to register/unregister Console Ctrl Handlers. When the NULL pointer is registered as "handler", it "causes the calling process to ignore CTRL+C input": https://msdn.microsoft.com/en-us/library/windows/desktop/ms686016.aspx This is exactly what we need here: while waiting for the child processes to finish, the git-cmd.exe process itself should not be interruptible by the user. Immediately after the child process terminates, we unregister the Console Ctrl Handler. Note: we need to be careful with changes to the Git wrapper as it serves many other purposes in addition to git-cmd.exe. For example, it serves as the cmd\git.exe as well as all of the git-.exe stand-ins. So do we want the same Ctrl+C behavior even in those instances? Yes: If the user interrupts using Ctrl+C, the child process should terminate before the Git wrapper. Also note: We cannot override the Console Ctrl Handler with a function that simply always returns TRUE: this would prevent the console window opened via git-cmd.exe from closing, since the Console Ctrl Handler *also* handles "signals generated by the system when the user closes the console, logs off, or shuts down the system." [jes: changed the patch to conform with the surrounding coding style, to pass NULL as Console Ctrl Handler and unregister it as soon as appropriate, fixed commit message to be more accurate and informative, added link to the SetConsoleCtrlHandler() documentation.] This fixes https://github.com/git-for-windows/git/pull/205 Signed-off-by: Christophe Bucher Developer Signed-off-by: Johannes Schindelin --- compat/win32/git-wrapper.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/compat/win32/git-wrapper.c b/compat/win32/git-wrapper.c index 588604b8813c94..42ea1290a43baf 100644 --- a/compat/win32/git-wrapper.c +++ b/compat/win32/git-wrapper.c @@ -642,8 +642,16 @@ int main(void) working_directory, /* use parent's */ &si, &pi); if (br) { - if (wait) + if (wait) { + /* + * Ignore Ctrl+C: the called process needs + * to handle this event correctly, then we + * quit, too. + */ + SetConsoleCtrlHandler(NULL, TRUE); WaitForSingleObject(pi.hProcess, INFINITE); + SetConsoleCtrlHandler(NULL, FALSE); + } if (!GetExitCodeProcess(pi.hProcess, (DWORD *)&r)) print_error(L"error reading exit code", GetLastError());