2020-01-24 08:15:31 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-08-25 18:22:15 -04:00
|
|
|
use deno_core::ErrBox;
|
2019-04-21 21:26:56 -04:00
|
|
|
|
2020-05-17 13:11:24 -04:00
|
|
|
#[cfg(not(unix))]
|
|
|
|
const SIGINT: i32 = 2;
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
const SIGKILL: i32 = 9;
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
const SIGTERM: i32 = 15;
|
|
|
|
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
use winapi::{
|
|
|
|
shared::minwindef::DWORD,
|
|
|
|
um::{
|
|
|
|
handleapi::CloseHandle,
|
|
|
|
processthreadsapi::{OpenProcess, TerminateProcess},
|
|
|
|
winnt::PROCESS_TERMINATE,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-04-21 21:26:56 -04:00
|
|
|
#[cfg(unix)]
|
2020-08-25 18:22:15 -04:00
|
|
|
pub fn kill(pid: i32, signo: i32) -> Result<(), ErrBox> {
|
2019-07-10 18:53:48 -04:00
|
|
|
use nix::sys::signal::{kill as unix_kill, Signal};
|
|
|
|
use nix::unistd::Pid;
|
2020-04-08 14:29:42 -04:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
let sig = Signal::try_from(signo)?;
|
2020-08-25 18:22:15 -04:00
|
|
|
unix_kill(Pid::from_raw(pid), Option::Some(sig)).map_err(ErrBox::from)
|
2019-04-21 21:26:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(unix))]
|
2020-08-25 18:22:15 -04:00
|
|
|
pub fn kill(pid: i32, signal: i32) -> Result<(), ErrBox> {
|
2020-05-17 13:11:24 -04:00
|
|
|
match signal {
|
|
|
|
SIGINT | SIGKILL | SIGTERM => {
|
|
|
|
if pid <= 0 {
|
2020-08-25 18:22:15 -04:00
|
|
|
return Err(ErrBox::type_error("unsupported pid"));
|
2020-05-17 13:11:24 -04:00
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
let handle = OpenProcess(PROCESS_TERMINATE, 0, pid as DWORD);
|
|
|
|
if handle.is_null() {
|
2020-08-25 18:22:15 -04:00
|
|
|
return Err(ErrBox::last_os_error());
|
2020-05-17 13:11:24 -04:00
|
|
|
}
|
|
|
|
if TerminateProcess(handle, 1) == 0 {
|
|
|
|
CloseHandle(handle);
|
2020-08-25 18:22:15 -04:00
|
|
|
return Err(ErrBox::last_os_error());
|
2020-05-17 13:11:24 -04:00
|
|
|
}
|
|
|
|
if CloseHandle(handle) == 0 {
|
2020-08-25 18:22:15 -04:00
|
|
|
return Err(ErrBox::last_os_error());
|
2020-05-17 13:11:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
2020-08-25 18:22:15 -04:00
|
|
|
return Err(ErrBox::type_error("unsupported signal".to_string()));
|
2020-05-17 13:11:24 -04:00
|
|
|
}
|
|
|
|
}
|
2019-04-21 21:26:56 -04:00
|
|
|
Ok(())
|
|
|
|
}
|