Skip to content

Commit 4148d00

Browse files
dschonaveen521kk
andcommitted
kill: kill Win32 processes more gently
This change is the equivalent to the change to the Ctrl+C handling we just made. Co-authored-by: Naveen M K <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 803f234 commit 4148d00

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

winsup/utils/kill.cc

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ details. */
1717
#include <cygwin/version.h>
1818
#include <getopt.h>
1919
#include <limits.h>
20+
#include <cygwin/exit_process.h>
2021

2122
static char *prog_name;
2223

@@ -300,10 +301,20 @@ forcekill (pid_t pid, DWORD winpid, int sig, int wait)
300301
return;
301302
}
302303
if (!wait || WaitForSingleObject (h, 200) != WAIT_OBJECT_0)
303-
if (sig && !TerminateProcess (h, sig << 8)
304-
&& WaitForSingleObject (h, 200) != WAIT_OBJECT_0)
305-
fprintf (stderr, "%s: couldn't kill pid %u, %u\n",
306-
prog_name, (unsigned int) dwpid, (unsigned int) GetLastError ());
304+
{
305+
HANDLE cur = GetCurrentProcess (), h2;
306+
/* duplicate handle with access rights required for exit_process_tree() */
307+
if (DuplicateHandle (cur, h, cur, &h2, PROCESS_CREATE_THREAD |
308+
PROCESS_QUERY_INFORMATION |
309+
PROCESS_VM_OPERATION |
310+
PROCESS_VM_WRITE | PROCESS_VM_READ |
311+
PROCESS_TERMINATE, FALSE, 0))
312+
{
313+
CloseHandle(h);
314+
h = h2;
315+
}
316+
exit_process_tree (h, 128 + sig);
317+
}
307318
CloseHandle (h);
308319
}
309320

0 commit comments

Comments
 (0)