support sending signals to processes on windows

This commit is contained in:
tionis 2023-05-16 16:58:42 +02:00
parent 71d51c160d
commit 56d72ec4c5
1 changed files with 18 additions and 13 deletions

View File

@ -624,7 +624,6 @@ JANET_CORE_FN(os_proc_wait,
#endif
}
#ifndef JANET_WINDOWS
struct keyword_signal {
const char *keyword;
int signal;
@ -704,29 +703,19 @@ static const struct keyword_signal signal_keywords[] = {
#endif
{NULL, 0},
};
#endif
JANET_CORE_FN(os_proc_kill,
"(os/proc-kill proc &opt wait signal)",
"Kill a subprocess by sending SIGKILL to it on posix systems, or by closing the process "
"handle on windows. If `wait` is truthy, will wait for the process to finish and "
"returns the exit code. Otherwise, returns `proc`. If signal is specified send it instead."
"Signal is ignored on windows. Signal keywords are named after their C counterparts but in"
"lowercase with the leading `SIG` stripped") {
"Signal keywords are named after their C counterparts but in lowercase with the leading "
"`SIG` stripped") {
janet_arity(argc, 1, 3);
JanetProc *proc = janet_getabstract(argv, 0, &ProcAT);
if (proc->flags & JANET_PROC_WAITED) {
janet_panicf("cannot kill process that has already finished");
}
#ifdef JANET_WINDOWS
if (proc->flags & JANET_PROC_CLOSED) {
janet_panicf("cannot close process handle that is already closed");
}
proc->flags |= JANET_PROC_CLOSED;
TerminateProcess(proc->pHandle, 1);
CloseHandle(proc->pHandle);
CloseHandle(proc->tHandle);
#else
int signal = SIGKILL;
if(argc == 3){
int signal = -1;
@ -743,6 +732,22 @@ JANET_CORE_FN(os_proc_kill,
janet_panic("undefined signal");
}
}
#ifdef JANET_WINDOWS
if (proc->flags & JANET_PROC_CLOSED) {
janet_panicf("cannot close process handle that is already closed");
}
proc->flags |= JANET_PROC_CLOSED;
if(signal == SIGKILL){
TerminateProcess(proc->pHandle, 1);
}else{
int status = kill(proc->pid, signal);
if (status) {
janet_panic(strerror(errno));
}
}
CloseHandle(proc->pHandle);
CloseHandle(proc->tHandle);
#else
int status = kill(proc->pid, signal);
if (status) {
janet_panic(strerror(errno));