forked from git/git
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle Ctrl+C in Git Bash nicely #1170
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
#ifndef EXIT_PROCESS_H | ||
#define EXIT_PROCESS_H | ||
|
||
/* | ||
* This file contains functions to terminate a Win32 process, as gently as | ||
* possible. | ||
* | ||
* At first, we will attempt to inject a thread that calls ExitProcess(). If | ||
* that fails, we will fall back to terminating the entire process tree. | ||
* | ||
* For simplicity, these functions are marked as file-local. | ||
*/ | ||
|
||
#include <tlhelp32.h> | ||
|
||
/* | ||
* Terminates the process corresponding to the process ID and all of its | ||
* directly and indirectly spawned subprocesses. | ||
* | ||
* This way of terminating the processes is not gentle: the processes get | ||
* no chance of cleaning up after themselves (closing file handles, removing | ||
* .lock files, terminating spawned processes (if any), etc). | ||
*/ | ||
static int terminate_process_tree(HANDLE main_process, int exit_status) | ||
{ | ||
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); | ||
PROCESSENTRY32 entry; | ||
DWORD pids[16384]; | ||
int max_len = sizeof(pids) / sizeof(*pids), i, len, ret = 0; | ||
pid_t pid = GetProcessId(main_process); | ||
|
||
pids[0] = (DWORD)pid; | ||
len = 1; | ||
|
||
/* | ||
* Even if Process32First()/Process32Next() seem to traverse the | ||
* processes in topological order (i.e. parent processes before | ||
* child processes), there is nothing in the Win32 API documentation | ||
* suggesting that this is guaranteed. | ||
* | ||
* Therefore, run through them at least twice and stop when no more | ||
* process IDs were added to the list. | ||
*/ | ||
for (;;) { | ||
int orig_len = len; | ||
|
||
memset(&entry, 0, sizeof(entry)); | ||
entry.dwSize = sizeof(entry); | ||
|
||
if (!Process32First(snapshot, &entry)) | ||
break; | ||
|
||
do { | ||
for (i = len - 1; i >= 0; i--) { | ||
if (pids[i] == entry.th32ProcessID) | ||
break; | ||
if (pids[i] == entry.th32ParentProcessID) | ||
pids[len++] = entry.th32ProcessID; | ||
} | ||
} while (len < max_len && Process32Next(snapshot, &entry)); | ||
|
||
if (orig_len == len || len >= max_len) | ||
break; | ||
} | ||
|
||
for (i = len - 1; i >= 0; i--) { | ||
HANDLE process = pids[i] == pid ? main_process : | ||
OpenProcess(PROCESS_TERMINATE, FALSE, pids[i]); | ||
|
||
if (process) { | ||
if (!TerminateProcess(process, exit_status << 8)) | ||
ret = -1; | ||
CloseHandle(process); | ||
} | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
/** | ||
* Determine whether a process runs in the same architecture as the current | ||
* one. That test is required before we assume that GetProcAddress() returns | ||
* a valid address *for the target process*. | ||
*/ | ||
static inline int process_architecture_matches_current(HANDLE process) | ||
{ | ||
static BOOL current_is_wow = -1; | ||
BOOL is_wow; | ||
|
||
if (current_is_wow == -1 && | ||
!IsWow64Process (GetCurrentProcess(), ¤t_is_wow)) | ||
current_is_wow = -2; | ||
if (current_is_wow == -2) | ||
return 0; /* could not determine current process' WoW-ness */ | ||
if (!IsWow64Process (process, &is_wow)) | ||
return 0; /* cannot determine */ | ||
return is_wow == current_is_wow; | ||
} | ||
|
||
/** | ||
* Inject a thread into the given process that runs ExitProcess(). | ||
* | ||
* Note: as kernel32.dll is loaded before any process, the other process and | ||
* this process will have ExitProcess() at the same address. | ||
* | ||
* This function expects the process handle to have the access rights for | ||
* CreateRemoteThread(): PROCESS_CREATE_THREAD, PROCESS_QUERY_INFORMATION, | ||
* PROCESS_VM_OPERATION, PROCESS_VM_WRITE, and PROCESS_VM_READ. | ||
* | ||
* The idea comes from the Dr Dobb's article "A Safer Alternative to | ||
* TerminateProcess()" by Andrew Tucker (July 1, 1999), | ||
* http://www.drdobbs.com/a-safer-alternative-to-terminateprocess/184416547 | ||
* | ||
* If this method fails, we fall back to running terminate_process_tree(). | ||
*/ | ||
static int exit_process(HANDLE process, int exit_code) | ||
{ | ||
DWORD code; | ||
|
||
if (GetExitCodeProcess(process, &code) && code == STILL_ACTIVE) { | ||
static int initialized; | ||
static LPTHREAD_START_ROUTINE exit_process_address; | ||
PVOID arg = (PVOID)(intptr_t)exit_code; | ||
DWORD thread_id; | ||
HANDLE thread = NULL; | ||
|
||
if (!initialized) { | ||
HINSTANCE kernel32 = GetModuleHandle("kernel32"); | ||
if (!kernel32) | ||
die("BUG: cannot find kernel32"); | ||
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
Sorry, something went wrong. |
||
exit_process_address = (LPTHREAD_START_ROUTINE) | ||
GetProcAddress(kernel32, "ExitProcess"); | ||
initialized = 1; | ||
} | ||
if (!exit_process_address || | ||
!process_architecture_matches_current(process)) | ||
return terminate_process_tree(process, exit_code); | ||
|
||
thread = CreateRemoteThread(process, NULL, 0, | ||
exit_process_address, | ||
arg, 0, &thread_id); | ||
if (thread) { | ||
CloseHandle(thread); | ||
/* | ||
* If the process survives for 10 seconds (a completely | ||
* arbitrary value picked from thin air), fall back to | ||
* killing the process tree via TerminateProcess(). | ||
*/ | ||
if (WaitForSingleObject(process, 10000) == | ||
WAIT_OBJECT_0) | ||
return 0; | ||
} | ||
|
||
return terminate_process_tree(process, exit_code); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
#endif |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
Sorry, something went wrong.