From 6db3d395faa82f597c8bf4a7cba85a57ee3f2f2a Mon Sep 17 00:00:00 2001 From: Erik Faye-Lund Date: Wed, 23 Dec 2009 02:56:59 +0100 Subject: [PATCH] hack: make kill terminate sub-process tree The code iterates hierarchically through a snapshot of all the processes in the system to kill the entire process sub-tree (that is, it also terminates the subprocesses, which TerminateProcess() doesn't do for us). Signed-off-by: Erik Faye-Lund --- compat/mingw.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/compat/mingw.c b/compat/mingw.c index 035c38485b..a0aa4fbe37 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -2,6 +2,8 @@ #include "win32.h" #include #include "../strbuf.h" +#include +#include int err_win_to_posix(DWORD winerr) { @@ -873,16 +875,41 @@ void mingw_execvp(const char *cmd, char *const *argv) free_path_split(path); } +static void kill_pid_subtree(HANDLE snap, DWORD pid) +{ + PROCESSENTRY32 pe = { 0 }; + pe.dwSize = sizeof(pe); + + if (!Process32First(snap, &pe)) + return; + + do { + if (pe.th32ParentProcessID == pid) { + HANDLE h = OpenProcess(PROCESS_TERMINATE, FALSE, + pe.th32ProcessID); + + if (TerminateProcess(h, -1)) + kill_pid_subtree(snap, pe.th32ProcessID); + + CloseHandle(h); + } + } while (Process32Next(snap, &pe)); +} + int mingw_kill(pid_t pid, int sig) { if (pid > 0 && sig == SIGTERM) { + HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); HANDLE h = OpenProcess(PROCESS_TERMINATE, FALSE, pid); if (TerminateProcess(h, -1)) { + kill_pid_subtree(snap, pid); + CloseHandle(snap); CloseHandle(h); return 0; } + CloseHandle(snap); CloseHandle(h); errno = err_win_to_posix(GetLastError()); return -1; -- 2.11.4.GIT