|
| 1 | +From c967bd8e37af7fa86f8ed1ded2625071612b808a Mon Sep 17 00:00:00 2001 |
| 2 | +From: Johannes Schindelin < [email protected]> |
| 3 | +Date: Fri, 12 Jan 2018 20:54:06 +0100 |
| 4 | +Subject: [PATCH 61/N] squash! Try to kill Win32 processes gently upon Ctrl+C |
| 5 | + |
| 6 | +Let's redo this. We will need to squash this into the original Ctrl+C |
| 7 | +patch during the next merging-rebase. |
| 8 | + |
| 9 | +Handle SIGINT/SIGTERM specifically via Ctrl events |
| 10 | + |
| 11 | +This assumes that we want to send the signal to a console process. It |
| 12 | +would not make sense in any other case, anyway, to try to emulate a |
| 13 | +Ctrl+C: where there is no Console, there are no keystrokes. |
| 14 | + |
| 15 | +[expand explanation here] |
| 16 | + |
| 17 | +Signed-off-by: Johannes Schindelin < [email protected]> |
| 18 | +--- |
| 19 | + winsup/cygwin/exceptions.cc | 2 +- |
| 20 | + winsup/cygwin/include/cygwin/exit_process.h | 120 ++++++++++++---------------- |
| 21 | + winsup/utils/kill.cc | 7 +- |
| 22 | + 3 files changed, 53 insertions(+), 76 deletions(-) |
| 23 | + |
| 24 | +diff --git a/winsup/cygwin/exceptions.cc b/winsup/cygwin/exceptions.cc |
| 25 | +index 7b4a163..8afcf00 100644 |
| 26 | +--- a/winsup/cygwin/exceptions.cc |
| 27 | ++++ b/winsup/cygwin/exceptions.cc |
| 28 | +@@ -1561,7 +1561,7 @@ dosig: |
| 29 | + goto done; |
| 30 | + default: |
| 31 | + sigproc_printf ("terminating captive process"); |
| 32 | +- exit_process (ch_spawn, 128 + (sigExeced = si.si_signo)); |
| 33 | ++ exit_process (ch_spawn, 128 + (sigExeced = si.si_signo), 0); |
| 34 | + break; |
| 35 | + } |
| 36 | + } |
| 37 | +diff --git a/winsup/cygwin/include/cygwin/exit_process.h b/winsup/cygwin/include/cygwin/exit_process.h |
| 38 | +index 01c0a47..6783950 100644 |
| 39 | +--- a/winsup/cygwin/include/cygwin/exit_process.h |
| 40 | ++++ b/winsup/cygwin/include/cygwin/exit_process.h |
| 41 | +@@ -5,8 +5,8 @@ |
| 42 | + * This file contains functions to terminate a Win32 process, as gently as |
| 43 | + * possible. |
| 44 | + * |
| 45 | +- * At first, we will attempt to inject a thread that calls ExitProcess(). If |
| 46 | +- * that fails, we will fall back to terminating the entire process tree. |
| 47 | ++ * If appropriate, we will attempt to generate a console Ctrl event. |
| 48 | ++ * Otherwise we will fall back to terminating the entire process tree. |
| 49 | + * |
| 50 | + * As we do not want to export this function in the MSYS2 runtime, these |
| 51 | + * functions are marked as file-local. |
| 52 | +@@ -14,36 +14,6 @@ |
| 53 | + |
| 54 | + #include <tlhelp32.h> |
| 55 | + |
| 56 | +-static int |
| 57 | +-terminate_process_with_remote_thread(HANDLE process, int exit_code) |
| 58 | +-{ |
| 59 | +- static LPTHREAD_START_ROUTINE exit_process_address; |
| 60 | +- if (!exit_process_address) |
| 61 | +- { |
| 62 | +- HINSTANCE kernel32 = GetModuleHandle ("kernel32"); |
| 63 | +- exit_process_address = (LPTHREAD_START_ROUTINE) |
| 64 | +- GetProcAddress (kernel32, "ExitProcess"); |
| 65 | +- } |
| 66 | +- DWORD thread_id; |
| 67 | +- HANDLE thread = !exit_process_address ? NULL : |
| 68 | +- CreateRemoteThread (process, NULL, 0, exit_process_address, |
| 69 | +- (PVOID)exit_code, 0, &thread_id); |
| 70 | +- |
| 71 | +- if (thread) |
| 72 | +- { |
| 73 | +- CloseHandle (thread); |
| 74 | +- /* |
| 75 | +- * Wait 10 seconds (arbitrary constant) for the process to |
| 76 | +- * finish; After that grace period, fall back to terminating |
| 77 | +- * non-gently. |
| 78 | +- */ |
| 79 | +- if (WaitForSingleObject (process, 10000) == WAIT_OBJECT_0) |
| 80 | +- return 0; |
| 81 | +- } |
| 82 | +- |
| 83 | +- return -1; |
| 84 | +-} |
| 85 | +- |
| 86 | + /** |
| 87 | + * Terminates the process corresponding to the process ID |
| 88 | + * |
| 89 | +@@ -133,52 +103,62 @@ terminate_process_tree(HANDLE main_process, int exit_code, int (*terminate)(HAND |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | +- * Determine whether a process runs in the same architecture as the current |
| 94 | +- * one. That test is required before we assume that GetProcAddress() returns |
| 95 | +- * a valid address *for the target process*. |
| 96 | +- */ |
| 97 | +-static inline bool |
| 98 | +-process_architecture_matches_current(HANDLE process) |
| 99 | +-{ |
| 100 | +- static BOOL current_is_wow = -1; |
| 101 | +- BOOL is_wow; |
| 102 | +- |
| 103 | +- if (current_is_wow == -1 && |
| 104 | +- !IsWow64Process (GetCurrentProcess (), ¤t_is_wow)) |
| 105 | +- current_is_wow = -2; |
| 106 | +- if (current_is_wow == -2) |
| 107 | +- return false; /* could not determine current process' WoW-ness */ |
| 108 | +- if (!IsWow64Process (process, &is_wow)) |
| 109 | +- return false; /* cannot determine */ |
| 110 | +- return is_wow == current_is_wow; |
| 111 | +-} |
| 112 | +- |
| 113 | +-/** |
| 114 | +- * Inject a thread into the given process that runs ExitProcess(). |
| 115 | +- * |
| 116 | +- * Note: as kernel32.dll is loaded before any process, the other process and |
| 117 | +- * this process will have ExitProcess() at the same address. |
| 118 | +- * |
| 119 | +- * This function expects the process handle to have the access rights for |
| 120 | +- * CreateRemoteThread(): PROCESS_CREATE_THREAD, PROCESS_QUERY_INFORMATION, |
| 121 | +- * PROCESS_VM_OPERATION, PROCESS_VM_WRITE, and PROCESS_VM_READ. |
| 122 | +- * |
| 123 | +- * The idea comes from the Dr Dobb's article "A Safer Alternative to |
| 124 | +- * TerminateProcess()" by Andrew Tucker (July 1, 1999), |
| 125 | +- * http://www.drdobbs.com/a-safer-alternative-to-terminateprocess/184416547 |
| 126 | +- * |
| 127 | +- * If this method fails, we fall back to running terminate_process_tree(). |
| 128 | ++ * For SIGINT/SIGTERM, call GenerateConsoleCtrlEven(). Otherwise fall back to |
| 129 | ++ * running terminate_process_tree(). |
| 130 | + */ |
| 131 | + static int |
| 132 | +-exit_process(HANDLE process, int exit_code) |
| 133 | ++exit_process(HANDLE process, int exit_code, int okay_to_kill_this_process) |
| 134 | + { |
| 135 | + DWORD code; |
| 136 | + |
| 137 | + if (GetExitCodeProcess (process, &code) && code == STILL_ACTIVE) |
| 138 | + { |
| 139 | + int signal = exit_code & 0x7f; |
| 140 | +- if (process_architecture_matches_current(process) && (signal == SIGINT || signal == SIGTERM)) |
| 141 | +- return terminate_process_tree (process, exit_code, terminate_process_with_remote_thread); |
| 142 | ++ if (signal == SIGINT || signal == SIGTERM) |
| 143 | ++ { |
| 144 | ++#ifndef __INSIDE_CYGWIN__ |
| 145 | ++ if (!okay_to_kill_this_process) |
| 146 | ++ return -1; |
| 147 | ++ FreeConsole(); |
| 148 | ++ if (!AttachConsole(GetProcessId(process))) |
| 149 | ++ return -1; |
| 150 | ++ if (GenerateConsoleCtrlEvent(signal == SIGINT ? CTRL_C_EVENT : CTRL_BREAK_EVENT, 0)) |
| 151 | ++ return 0; |
| 152 | ++#else |
| 153 | ++ path_conv helper ("/bin/kill.exe"); |
| 154 | ++ if (helper.exists ()) |
| 155 | ++ { |
| 156 | ++ STARTUPINFOW si = {}; |
| 157 | ++ PROCESS_INFORMATION pi; |
| 158 | ++ size_t len = helper.get_wide_win32_path_len (); |
| 159 | ++ WCHAR cmd[len + (2 * strlen (" -f -32 0xffffffff")) + 1]; |
| 160 | ++ WCHAR title[] = L"kill"; |
| 161 | ++ |
| 162 | ++ helper.get_wide_win32_path (cmd); |
| 163 | ++ __small_swprintf (cmd + len, L" -f -%d %d", signal, (int)GetProcessId(ch_spawn)); |
| 164 | ++ |
| 165 | ++ si.cb = sizeof (si); |
| 166 | ++ si.dwFlags = STARTF_USESHOWWINDOW; |
| 167 | ++ si.wShowWindow = SW_HIDE; |
| 168 | ++ si.lpTitle = title; |
| 169 | ++ |
| 170 | ++ /* Create a new hidden process. Use the two event handles as |
| 171 | ++ argv[1] and argv[2]. */ |
| 172 | ++ BOOL x = CreateProcessW (NULL, cmd, &sec_none_nih, &sec_none_nih, |
| 173 | ++ true, CREATE_NO_WINDOW, NULL, NULL, &si, &pi); |
| 174 | ++ if (x) |
| 175 | ++ { |
| 176 | ++ CloseHandle (pi.hThread); |
| 177 | ++ if (WaitForSingleObject (pi.hProcess, 10000) == WAIT_OBJECT_0) |
| 178 | ++ { |
| 179 | ++ CloseHandle (pi.hProcess); |
| 180 | ++ return 0; |
| 181 | ++ } |
| 182 | ++ CloseHandle (pi.hProcess); |
| 183 | ++ } |
| 184 | ++ } |
| 185 | ++#endif |
| 186 | ++ } |
| 187 | + |
| 188 | + return terminate_process_tree (process, exit_code, terminate_process); |
| 189 | + } |
| 190 | +diff --git a/winsup/utils/kill.cc b/winsup/utils/kill.cc |
| 191 | +index a771df6..911eedb 100644 |
| 192 | +--- a/winsup/utils/kill.cc |
| 193 | ++++ b/winsup/utils/kill.cc |
| 194 | +@@ -181,13 +181,10 @@ forcekill (int pid, int sig, int wait) |
| 195 | + PROCESS_VM_WRITE | PROCESS_VM_READ | |
| 196 | + PROCESS_TERMINATE, FALSE, 0)) |
| 197 | + { |
| 198 | ++ CloseHandle(h); |
| 199 | + h = h2; |
| 200 | +- CloseHandle (h); |
| 201 | + } |
| 202 | +- exit_process (h, 128 + sig); |
| 203 | +- if (WaitForSingleObject (h, 200) != WAIT_OBJECT_0) |
| 204 | +- fprintf (stderr, "%s: couldn't kill pid %u, %u\n", |
| 205 | +- prog_name, (unsigned) dwpid, (unsigned int) GetLastError ()); |
| 206 | ++ exit_process (h2, 128 + sig, 1); |
| 207 | + } |
| 208 | + CloseHandle (h); |
| 209 | + } |
| 210 | +-- |
| 211 | +2.9.0 |
| 212 | + |
0 commit comments