From e157bea893c9d5afa20af02b26baf566f5eadb99 Mon Sep 17 00:00:00 2001 From: Marko Kreen Date: Thu, 4 Dec 2008 10:57:38 +0000 Subject: [PATCH] win32: implement kill() Patch by Hiroshi Saito, applied with some modifications. --- win32/compat_win32.h | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/win32/compat_win32.h b/win32/compat_win32.h index 382a677..5f2dde7 100644 --- a/win32/compat_win32.h +++ b/win32/compat_win32.h @@ -169,10 +169,38 @@ static inline int getrlimit(int res, struct rlimit *dst) return 0; } -/* kill is only used to detect if process is running, be always successful */ +/* kill is only used to detect if process is running (ESRCH->not) */ static inline int kill(int pid, int sig) { - return (sig == 0) ? 0 : -1; + HANDLE hProcess; + DWORD exitCode; + int ret = 0; + + if (sig != 0) { + errno = EINVAL; + return -1; + } + + hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid); + if (hProcess == NULL) { + if (GetLastError() == ERROR_INVALID_PARAMETER) + ret = ESRCH; + else + ret = EPERM; + } else { + /* OpenProcess may succed for exited processes */ + if (GetExitCodeProcess(hProcess, &exitCode)) { + if (exitCode != STILL_ACTIVE) + ret = ESRCH; + } + CloseHandle(hProcess); + } + + if (ret) { + errno = ret; + return -1; + } else + return 0; } /* sendmsg is not used */ -- 2.40.0