|
| 1 | +// vim: ts=99 sw=2 |
| 2 | +#ifndef EXIT_PROCESS_H |
| 3 | +#define EXIT_PROCESS_H |
| 4 | + |
| 5 | +/* |
| 6 | + * This file contains functions to terminate a Win32 process, as gently as |
| 7 | + * possible. |
| 8 | + * |
| 9 | + * At first, we will attempt to inject a thread that calls ExitProcess(). If |
| 10 | + * that fails, we will fall back to terminating the entire process tree. |
| 11 | + * |
| 12 | + * As we do not want to export this function in the MSYS2 runtime, these |
| 13 | + * functions are marked as file-local. |
| 14 | + */ |
| 15 | + |
| 16 | +#include <tlhelp32.h> |
| 17 | + |
| 18 | +/** |
| 19 | + * Terminates the process corresponding to the process ID and all of its |
| 20 | + * directly and indirectly spawned subprocesses. |
| 21 | + */ |
| 22 | +static int |
| 23 | +terminate_process_tree(HANDLE main_process, int exit_code) |
| 24 | +{ |
| 25 | + HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 26 | + PROCESSENTRY32 entry; |
| 27 | + DWORD pids[16384]; |
| 28 | + int max_len = sizeof(pids) / sizeof(*pids), i, len, ret = 0; |
| 29 | + pid_t pid = GetProcessId(main_process); |
| 30 | + |
| 31 | + pids[0] = (DWORD) pid; |
| 32 | + len = 1; |
| 33 | + |
| 34 | + /* |
| 35 | + * Even if Process32First()/Process32Next() seem to traverse the |
| 36 | + * processes in topological order (i.e. parent processes before |
| 37 | + * child processes), there is nothing in the Win32 API documentation |
| 38 | + * suggesting that this is guaranteed. |
| 39 | + * |
| 40 | + * Therefore, run through them at least twice and stop when no more |
| 41 | + * process IDs were added to the list. |
| 42 | + */ |
| 43 | + for (;;) |
| 44 | + { |
| 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 | + { |
| 55 | + for (i = len - 1; i >= 0; i--) |
| 56 | + { |
| 57 | + if (pids[i] == entry.th32ProcessID) |
| 58 | + break; |
| 59 | + if (pids[i] == entry.th32ParentProcessID) |
| 60 | + pids[len++] = entry.th32ProcessID; |
| 61 | + } |
| 62 | + } |
| 63 | + while (len < max_len && Process32Next(snapshot, &entry)); |
| 64 | + |
| 65 | + if (orig_len == len || len >= max_len) |
| 66 | + break; |
| 67 | + } |
| 68 | + |
| 69 | + for (i = len - 1; i >= 0; i--) |
| 70 | + { |
| 71 | + HANDLE process = i == 0 ? main_process : |
| 72 | + OpenProcess(PROCESS_TERMINATE, FALSE, pids[i]); |
| 73 | + |
| 74 | + if (process) |
| 75 | + { |
| 76 | + if (!TerminateProcess(process, exit_code)) |
| 77 | + ret = -1; |
| 78 | + CloseHandle(process); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return ret; |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Inject a thread into the given process that runs ExitProcess(). |
| 87 | + * |
| 88 | + * Note: as kernel32.dll is loaded before any process, the other process and |
| 89 | + * this process will have ExitProcess() at the same address. |
| 90 | + * |
| 91 | + * This function expects the process handle to have the access rights for |
| 92 | + * CreateRemoteThread(): PROCESS_CREATE_THREAD, PROCESS_QUERY_INFORMATION, |
| 93 | + * PROCESS_VM_OPERATION, PROCESS_VM_WRITE, and PROCESS_VM_READ. |
| 94 | + * |
| 95 | + * If this method fails, we fall back to running terminate_process_tree(). |
| 96 | + */ |
| 97 | +static int |
| 98 | +exit_process(HANDLE process, int exit_code) |
| 99 | +{ |
| 100 | + DWORD code; |
| 101 | + |
| 102 | + if (GetExitCodeProcess (process, &code) && code == STILL_ACTIVE) |
| 103 | + { |
| 104 | + HINSTANCE kernel32 = GetModuleHandle("kernel32"); |
| 105 | + LPTHREAD_START_ROUTINE exit_process_address = |
| 106 | + (LPTHREAD_START_ROUTINE) GetProcAddress (kernel32, "ExitProcess"); |
| 107 | + DWORD thread_id; |
| 108 | + HANDLE thread = CreateRemoteThread (process, NULL, 0, |
| 109 | + exit_process_address, |
| 110 | + (PVOID)exit_code, 0, &thread_id); |
| 111 | + |
| 112 | + if (!thread) |
| 113 | + return terminate_process_tree(process, exit_code); |
| 114 | + CloseHandle (thread); |
| 115 | + if (WaitForSingleObject (process, 10000) == WAIT_OBJECT_0) |
| 116 | + return 0; |
| 117 | + } |
| 118 | + return -1; |
| 119 | +} |
| 120 | + |
| 121 | +#endif |
0 commit comments