From 34201e3a2cb93b437dc7cf324ae6112a31b3243b Mon Sep 17 00:00:00 2001 From: ndurner Date: Mon, 20 Dec 2010 14:57:34 +0000 Subject: [PATCH] check if process is already terminated git-svn-id: https://plibc.svn.sourceforge.net/svnroot/plibc/trunk/plibc@76 1596915d-9e13-0410-ab7f-96e59408e0f0 --- src/kill.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/kill.c b/src/kill.c index 89b4fc5..48035d6 100644 --- a/src/kill.c +++ b/src/kill.c @@ -32,17 +32,30 @@ int _win_kill(pid_t pid, int sig) if (sig == SIGKILL || sig == SIGTERM) { HANDLE h; + DWORD dw; + BOOL terminated; - h = OpenProcess(PROCESS_TERMINATE, FALSE, (DWORD) pid); + h = OpenProcess(PROCESS_TERMINATE | PROCESS_QUERY_INFORMATION, FALSE, (DWORD) pid); if (!h) { SetErrnoFromWinError(GetLastError()); return -1; } - if (!TerminateProcess(h, 0)) + if (GetExitCodeProcess(h, &dw) && dw != STILL_ACTIVE) { - SetErrnoFromWinError(GetLastError()); + /* already killed */ + CloseHandle(h); + return 0; + } + + terminated = TerminateProcess(h, 0); + dw = GetLastError(); + CloseHandle(h); + + if (!terminated) + { + SetErrnoFromWinError(dw); return -1; } else