From 56d72ec4c5d49f4e5bd8d6af8e59cadf421fcef9 Mon Sep 17 00:00:00 2001 From: tionis Date: Tue, 16 May 2023 16:58:42 +0200 Subject: [PATCH] support sending signals to processes on windows --- src/core/os.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/core/os.c b/src/core/os.c index 0c348466..425785b0 100644 --- a/src/core/os.c +++ b/src/core/os.c @@ -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));