mirror of
https://github.com/janet-lang/janet
synced 2025-10-23 03:37:40 +00:00
support sending signals to processes on windows
This commit is contained in:
@@ -624,7 +624,6 @@ JANET_CORE_FN(os_proc_wait,
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef JANET_WINDOWS
|
|
||||||
struct keyword_signal {
|
struct keyword_signal {
|
||||||
const char *keyword;
|
const char *keyword;
|
||||||
int signal;
|
int signal;
|
||||||
@@ -704,29 +703,19 @@ static const struct keyword_signal signal_keywords[] = {
|
|||||||
#endif
|
#endif
|
||||||
{NULL, 0},
|
{NULL, 0},
|
||||||
};
|
};
|
||||||
#endif
|
|
||||||
|
|
||||||
JANET_CORE_FN(os_proc_kill,
|
JANET_CORE_FN(os_proc_kill,
|
||||||
"(os/proc-kill proc &opt wait signal)",
|
"(os/proc-kill proc &opt wait signal)",
|
||||||
"Kill a subprocess by sending SIGKILL to it on posix systems, or by closing the process "
|
"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 "
|
"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."
|
"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"
|
"Signal keywords are named after their C counterparts but in lowercase with the leading "
|
||||||
"lowercase with the leading `SIG` stripped") {
|
"`SIG` stripped") {
|
||||||
janet_arity(argc, 1, 3);
|
janet_arity(argc, 1, 3);
|
||||||
JanetProc *proc = janet_getabstract(argv, 0, &ProcAT);
|
JanetProc *proc = janet_getabstract(argv, 0, &ProcAT);
|
||||||
if (proc->flags & JANET_PROC_WAITED) {
|
if (proc->flags & JANET_PROC_WAITED) {
|
||||||
janet_panicf("cannot kill process that has already finished");
|
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;
|
int signal = SIGKILL;
|
||||||
if(argc == 3){
|
if(argc == 3){
|
||||||
int signal = -1;
|
int signal = -1;
|
||||||
@@ -743,6 +732,22 @@ JANET_CORE_FN(os_proc_kill,
|
|||||||
janet_panic("undefined signal");
|
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);
|
int status = kill(proc->pid, signal);
|
||||||
if (status) {
|
if (status) {
|
||||||
janet_panic(strerror(errno));
|
janet_panic(strerror(errno));
|
||||||
|
Reference in New Issue
Block a user