-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Deno.kill(pid, signo) and process.kill(signo) (Unix only) #2177
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#[cfg(unix)] | ||
use nix::sys::signal::{kill as unix_kill, Signal}; | ||
#[cfg(unix)] | ||
use nix::unistd::Pid; | ||
|
||
use crate::errors::DenoResult; | ||
|
||
#[cfg(unix)] | ||
pub fn kill(pid: i32, signo: i32) -> DenoResult<()> { | ||
use crate::errors::DenoError; | ||
let sig = Signal::from_c_int(signo)?; | ||
unix_kill(Pid::from_raw(pid), Option::Some(sig)).map_err(DenoError::from) | ||
} | ||
|
||
#[cfg(not(unix))] | ||
pub fn kill(_pid: i32, _signal: i32) -> DenoResult<()> { | ||
// NOOP | ||
// TODO: implement this for windows | ||
Ok(()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,6 +51,16 @@ async function runStatus(rid: number): Promise<ProcessStatus> { | |
} | ||
} | ||
|
||
/** Send a signal to process under given PID. Unix only at this moment. | ||
* If pid is negative, the signal will be sent to the process group identified | ||
* by -pid. | ||
*/ | ||
export function kill(pid: number, signo: number): void { | ||
const builder = flatbuffers.createBuilder(); | ||
const inner = msg.Kill.createKill(builder, pid, signo); | ||
dispatch.sendSync(builder, msg.Any.Kill, inner); | ||
} | ||
|
||
export class Process { | ||
readonly rid: number; | ||
readonly pid: number; | ||
|
@@ -113,6 +123,10 @@ export class Process { | |
close(): void { | ||
close(this.rid); | ||
} | ||
|
||
kill(signo: number): void { | ||
kill(this.pid, signo); | ||
} | ||
} | ||
|
||
export interface ProcessStatus { | ||
|
@@ -179,3 +193,37 @@ export function run(opt: RunOptions): Process { | |
|
||
return new Process(res); | ||
} | ||
|
||
export enum Signal { | ||
SIGHUP = 1, | ||
SIGINT, | ||
SIGQUIT, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Although it's equivalent, I would prefer explicitly assigning values to each of these. It just makes things a bit more obvious if it ever needs to be debugged. SIGINT = 2,
SIGQUIT = 3,
/* .. */ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually I've just realized that certain signal numbers are platform dependent (like 10 is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah too bad. I thought maybe posix defined those - apparently not.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently resorting to export const Signal = platform.os === "mac" ? MacOSSignal : LinuxSignal; Using string names also not quite ideal unless we build a similar mapping on the Rust side, since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok that works |
||
SIGILL, | ||
SIGTRAP, | ||
SIGABRT, | ||
SIGBUS, | ||
SIGFPE, | ||
SIGKILL, | ||
SIGUSR1, | ||
SIGSEGV, | ||
SIGUSR2, | ||
SIGPIPE, | ||
SIGALRM, | ||
SIGTERM, | ||
SIGSTKFLT, | ||
SIGCHLD, | ||
SIGCONT, | ||
SIGSTOP, | ||
SIGTSTP, | ||
SIGTTIN, | ||
SIGTTOU, | ||
SIGURG, | ||
SIGXCPU, | ||
SIGXFSZ, | ||
SIGVTALRM, | ||
SIGPROF, | ||
SIGWINCH, | ||
SIGIO, | ||
SIGPWR, | ||
SIGSYS | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated to this commit, but I think we should rename this to
Subprocess
.