Skip to content

Commit 24cbca5

Browse files
committed
kill: kill Win32 processes more gently
This change is the equivalent to the change to the Ctrl+C handling we just made. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 2469941 commit 24cbca5

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

@@ -171,10 +172,20 @@ forcekill (int pid, int sig, int wait)
171172
return;
172173
}
173174
if (!wait || WaitForSingleObject (h, 200) != WAIT_OBJECT_0)
174-
if (sig && !TerminateProcess (h, sig << 8)
175-
&& WaitForSingleObject (h, 200) != WAIT_OBJECT_0)
176-
fprintf (stderr, "%s: couldn't kill pid %u, %u\n",
177-
prog_name, (unsigned) dwpid, (unsigned int) GetLastError ());
175+
{
176+
HANDLE cur = GetCurrentProcess (), h2;
177+
/* duplicate handle with access rights required for exit_process() */
178+
if (DuplicateHandle (cur, h, cur, &h2, PROCESS_CREATE_THREAD |
179+
PROCESS_QUERY_INFORMATION |
180+
PROCESS_VM_OPERATION |
181+
PROCESS_VM_WRITE | PROCESS_VM_READ |
182+
PROCESS_TERMINATE, FALSE, 0))
183+
{
184+
CloseHandle(h);
185+
h = h2;
186+
}
187+
exit_process (h2, 128 + sig, 1);
188+
}
178189
CloseHandle (h);
179190
}
180191

0 commit comments

Comments
 (0)