Write more tests and improve kill() on Windows

This commit is contained in:
Justine Tunney 2023-10-13 02:22:48 -07:00
parent b81a1bd9a8
commit d458642790
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
6 changed files with 183 additions and 13 deletions

View file

@ -20,9 +20,14 @@
#include "libc/calls/syscall-nt.internal.h"
#include "libc/errno.h"
#include "libc/intrin/strace.internal.h"
#include "libc/nt/console.h"
#include "libc/nt/enum/ctrlevent.h"
#include "libc/nt/enum/processaccess.h"
#include "libc/nt/errors.h"
#include "libc/nt/process.h"
#include "libc/nt/runtime.h"
#include "libc/proc/proc.internal.h"
#include "libc/sysv/consts/sig.h"
#include "libc/sysv/errfuns.h"
#ifdef __x86_64__
@ -55,17 +60,26 @@ textwindows int sys_kill_nt(int pid, int sig) {
}
// find existing handle we own for process
int64_t handle;
int64_t handle, closeme = 0;
if (!(handle = __proc_handle(pid))) {
return esrch();
if ((handle = OpenProcess(kNtProcessTerminate, false, pid))) {
closeme = handle;
} else {
goto OnError;
}
}
// perform actual kill
// process will report WIFSIGNALED with WTERMSIG(sig)
if (TerminateProcess(handle, sig)) return 0;
STRACE("TerminateProcess() failed with %d", GetLastError());
bool32 ok = TerminateProcess(handle, sig);
if (closeme) CloseHandle(closeme);
if (ok) return 0;
// handle error
OnError:
switch (GetLastError()) {
case kNtErrorInvalidHandle:
case kNtErrorInvalidParameter:
return esrch();
default:
return eperm();