Skip to content

Commit e5b2d64

Browse files
dschoGit for Windows Build Agent
authored and
Git for Windows Build Agent
committed
Merge pull request #1170 from dscho/mingw-kill-process
Handle Ctrl+C in Git Bash nicely Signed-off-by: Johannes Schindelin <[email protected]>
2 parents 6ff0561 + 2239445 commit e5b2d64

File tree

2 files changed

+195
-8
lines changed

2 files changed

+195
-8
lines changed

compat/mingw.c

+30-8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "../run-command.h"
1313
#include "../abspath.h"
1414
#include "../alloc.h"
15+
#include "win32/exit-process.h"
1516
#include "win32/lazyload.h"
1617
#include "../config.h"
1718
#include "../environment.h"
@@ -2384,16 +2385,28 @@ int mingw_execvp(const char *cmd, char *const *argv)
23842385
int mingw_kill(pid_t pid, int sig)
23852386
{
23862387
if (pid > 0 && sig == SIGTERM) {
2387-
HANDLE h = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
2388-
2389-
if (TerminateProcess(h, -1)) {
2388+
HANDLE h = OpenProcess(PROCESS_CREATE_THREAD |
2389+
PROCESS_QUERY_INFORMATION |
2390+
PROCESS_VM_OPERATION | PROCESS_VM_WRITE |
2391+
PROCESS_VM_READ | PROCESS_TERMINATE,
2392+
FALSE, pid);
2393+
int ret;
2394+
2395+
if (h)
2396+
ret = exit_process(h, 128 + sig);
2397+
else {
2398+
h = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
2399+
if (!h) {
2400+
errno = err_win_to_posix(GetLastError());
2401+
return -1;
2402+
}
2403+
ret = terminate_process_tree(h, 128 + sig);
2404+
}
2405+
if (ret) {
2406+
errno = err_win_to_posix(GetLastError());
23902407
CloseHandle(h);
2391-
return 0;
23922408
}
2393-
2394-
errno = err_win_to_posix(GetLastError());
2395-
CloseHandle(h);
2396-
return -1;
2409+
return ret;
23972410
} else if (pid > 0 && sig == 0) {
23982411
HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
23992412
if (h) {
@@ -4249,7 +4262,14 @@ static void adjust_symlink_flags(void)
42494262
symlink_file_flags |= 2;
42504263
symlink_directory_flags |= 2;
42514264
}
4265+
}
42524266

4267+
static BOOL WINAPI handle_ctrl_c(DWORD ctrl_type)
4268+
{
4269+
if (ctrl_type != CTRL_C_EVENT)
4270+
return FALSE; /* we did not handle this */
4271+
mingw_raise(SIGINT);
4272+
return TRUE; /* we did handle this */
42534273
}
42544274

42554275
#ifdef _MSC_VER
@@ -4286,6 +4306,8 @@ int wmain(int argc, const wchar_t **wargv)
42864306
#endif
42874307
#endif
42884308

4309+
SetConsoleCtrlHandler(handle_ctrl_c, TRUE);
4310+
42894311
maybe_redirect_std_handles();
42904312
adjust_symlink_flags();
42914313
fsync_object_files = 1;

compat/win32/exit-process.h

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
#ifndef EXIT_PROCESS_H
2+
#define EXIT_PROCESS_H
3+
4+
/*
5+
* This file contains functions to terminate a Win32 process, as gently as
6+
* possible.
7+
*
8+
* At first, we will attempt to inject a thread that calls ExitProcess(). If
9+
* that fails, we will fall back to terminating the entire process tree.
10+
*
11+
* For simplicity, these functions are marked as file-local.
12+
*/
13+
14+
#include <tlhelp32.h>
15+
16+
/*
17+
* Terminates the process corresponding to the process ID and all of its
18+
* directly and indirectly spawned subprocesses.
19+
*
20+
* This way of terminating the processes is not gentle: the processes get
21+
* no chance of cleaning up after themselves (closing file handles, removing
22+
* .lock files, terminating spawned processes (if any), etc).
23+
*/
24+
static int terminate_process_tree(HANDLE main_process, int exit_status)
25+
{
26+
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
27+
PROCESSENTRY32 entry;
28+
DWORD pids[16384];
29+
int max_len = sizeof(pids) / sizeof(*pids), i, len, ret = 0;
30+
pid_t pid = GetProcessId(main_process);
31+
32+
pids[0] = (DWORD)pid;
33+
len = 1;
34+
35+
/*
36+
* Even if Process32First()/Process32Next() seem to traverse the
37+
* processes in topological order (i.e. parent processes before
38+
* child processes), there is nothing in the Win32 API documentation
39+
* suggesting that this is guaranteed.
40+
*
41+
* Therefore, run through them at least twice and stop when no more
42+
* process IDs were added to the list.
43+
*/
44+
for (;;) {
45+
int orig_len = len;
46+
47+
memset(&entry, 0, sizeof(entry));
48+
entry.dwSize = sizeof(entry);
49+
50+
if (!Process32First(snapshot, &entry))
51+
break;
52+
53+
do {
54+
for (i = len - 1; i >= 0; i--) {
55+
if (pids[i] == entry.th32ProcessID)
56+
break;
57+
if (pids[i] == entry.th32ParentProcessID)
58+
pids[len++] = entry.th32ProcessID;
59+
}
60+
} while (len < max_len && Process32Next(snapshot, &entry));
61+
62+
if (orig_len == len || len >= max_len)
63+
break;
64+
}
65+
66+
for (i = len - 1; i > 0; i--) {
67+
HANDLE process = OpenProcess(PROCESS_TERMINATE, FALSE, pids[i]);
68+
69+
if (process) {
70+
if (!TerminateProcess(process, exit_status))
71+
ret = -1;
72+
CloseHandle(process);
73+
}
74+
}
75+
if (!TerminateProcess(main_process, exit_status))
76+
ret = -1;
77+
CloseHandle(main_process);
78+
79+
return ret;
80+
}
81+
82+
/**
83+
* Determine whether a process runs in the same architecture as the current
84+
* one. That test is required before we assume that GetProcAddress() returns
85+
* a valid address *for the target process*.
86+
*/
87+
static inline int process_architecture_matches_current(HANDLE process)
88+
{
89+
static BOOL current_is_wow = -1;
90+
BOOL is_wow;
91+
92+
if (current_is_wow == -1 &&
93+
!IsWow64Process (GetCurrentProcess(), &current_is_wow))
94+
current_is_wow = -2;
95+
if (current_is_wow == -2)
96+
return 0; /* could not determine current process' WoW-ness */
97+
if (!IsWow64Process (process, &is_wow))
98+
return 0; /* cannot determine */
99+
return is_wow == current_is_wow;
100+
}
101+
102+
/**
103+
* Inject a thread into the given process that runs ExitProcess().
104+
*
105+
* Note: as kernel32.dll is loaded before any process, the other process and
106+
* this process will have ExitProcess() at the same address.
107+
*
108+
* This function expects the process handle to have the access rights for
109+
* CreateRemoteThread(): PROCESS_CREATE_THREAD, PROCESS_QUERY_INFORMATION,
110+
* PROCESS_VM_OPERATION, PROCESS_VM_WRITE, and PROCESS_VM_READ.
111+
*
112+
* The idea comes from the Dr Dobb's article "A Safer Alternative to
113+
* TerminateProcess()" by Andrew Tucker (July 1, 1999),
114+
* http://www.drdobbs.com/a-safer-alternative-to-terminateprocess/184416547
115+
*
116+
* If this method fails, we fall back to running terminate_process_tree().
117+
*/
118+
static int exit_process(HANDLE process, int exit_code)
119+
{
120+
DWORD code;
121+
122+
if (GetExitCodeProcess(process, &code) && code == STILL_ACTIVE) {
123+
static int initialized;
124+
static LPTHREAD_START_ROUTINE exit_process_address;
125+
PVOID arg = (PVOID)(intptr_t)exit_code;
126+
DWORD thread_id;
127+
HANDLE thread = NULL;
128+
129+
if (!initialized) {
130+
HINSTANCE kernel32 = GetModuleHandleA("kernel32");
131+
if (!kernel32)
132+
die("BUG: cannot find kernel32");
133+
exit_process_address =
134+
(LPTHREAD_START_ROUTINE)(void (*)(void))
135+
GetProcAddress(kernel32, "ExitProcess");
136+
initialized = 1;
137+
}
138+
if (!exit_process_address ||
139+
!process_architecture_matches_current(process))
140+
return terminate_process_tree(process, exit_code);
141+
142+
thread = CreateRemoteThread(process, NULL, 0,
143+
exit_process_address,
144+
arg, 0, &thread_id);
145+
if (thread) {
146+
CloseHandle(thread);
147+
/*
148+
* If the process survives for 10 seconds (a completely
149+
* arbitrary value picked from thin air), fall back to
150+
* killing the process tree via TerminateProcess().
151+
*/
152+
if (WaitForSingleObject(process, 10000) ==
153+
WAIT_OBJECT_0) {
154+
CloseHandle(process);
155+
return 0;
156+
}
157+
}
158+
159+
return terminate_process_tree(process, exit_code);
160+
}
161+
162+
return 0;
163+
}
164+
165+
#endif

0 commit comments

Comments
 (0)