1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-11 16:42:21 -05:00

refactor(runtime): change from signal_str_to_int function to enum (#14539)

This commit is contained in:
Leo Kettmeir 2022-05-14 14:10:39 +02:00 committed by GitHub
parent bd4256262a
commit c496639d5d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 166 additions and 170 deletions

View file

@ -473,10 +473,10 @@ Deno.test(
Deno.test({ permissions: { run: false } }, function killPermissions() { Deno.test({ permissions: { run: false } }, function killPermissions() {
assertThrows(() => { assertThrows(() => {
// Unlike the other test cases, we don't have permission to spawn a // Unlike the other test cases, we don't have permission to spawn a
// subprocess we can safely kill. Instead we send SIGCONT to the current // subprocess we can safely kill. Instead we send SIGTERM to the current
// process - assuming that Deno does not have a special handler set for it // process - assuming that Deno does not have a special handler set for it
// and will just continue even if a signal is erroneously sent. // and will just continue even if a signal is erroneously sent.
Deno.kill(Deno.pid, "SIGCONT"); Deno.kill(Deno.pid, "SIGTERM");
}, Deno.errors.PermissionDenied); }, Deno.errors.PermissionDenied);
}); });

View file

@ -288,16 +288,15 @@ async fn op_run_status(
} }
#[cfg(unix)] #[cfg(unix)]
pub fn kill(pid: i32, signal: &str) -> Result<(), AnyError> { pub fn kill(pid: i32, signal: super::signal::Signal) -> Result<(), AnyError> {
let signo = super::signal::signal_str_to_int(signal)?;
use nix::sys::signal::{kill as unix_kill, Signal}; use nix::sys::signal::{kill as unix_kill, Signal};
use nix::unistd::Pid; use nix::unistd::Pid;
let sig = Signal::try_from(signo)?; let sig = Signal::try_from(signal as libc::c_int)?;
unix_kill(Pid::from_raw(pid), Option::Some(sig)).map_err(AnyError::from) unix_kill(Pid::from_raw(pid), Option::Some(sig)).map_err(AnyError::from)
} }
#[cfg(not(unix))] #[cfg(not(unix))]
pub fn kill(pid: i32, signal: &str) -> Result<(), AnyError> { pub fn kill(pid: i32, _signal: super::signal::Signal) -> Result<(), AnyError> {
use deno_core::error::type_error; use deno_core::error::type_error;
use std::io::Error; use std::io::Error;
use std::io::ErrorKind::NotFound; use std::io::ErrorKind::NotFound;
@ -311,9 +310,7 @@ pub fn kill(pid: i32, signal: &str) -> Result<(), AnyError> {
use winapi::um::processthreadsapi::TerminateProcess; use winapi::um::processthreadsapi::TerminateProcess;
use winapi::um::winnt::PROCESS_TERMINATE; use winapi::um::winnt::PROCESS_TERMINATE;
if !matches!(signal, "SIGKILL" | "SIGTERM") { if pid <= 0 {
Err(type_error(format!("Invalid signal: {}", signal)))
} else if pid <= 0 {
Err(type_error("Invalid pid")) Err(type_error("Invalid pid"))
} else { } else {
let handle = unsafe { OpenProcess(PROCESS_TERMINATE, FALSE, pid as DWORD) }; let handle = unsafe { OpenProcess(PROCESS_TERMINATE, FALSE, pid as DWORD) };
@ -339,9 +336,9 @@ pub fn kill(pid: i32, signal: &str) -> Result<(), AnyError> {
fn op_kill( fn op_kill(
state: &mut OpState, state: &mut OpState,
pid: i32, pid: i32,
signal: String, signal: super::signal::Signal,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
state.borrow_mut::<Permissions>().run.check_all()?; state.borrow_mut::<Permissions>().run.check_all()?;
kill(pid, &signal)?; kill(pid, signal)?;
Ok(()) Ok(())
} }

View file

@ -5,6 +5,8 @@ use deno_core::error::generic_error;
use deno_core::error::type_error; use deno_core::error::type_error;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::op; use deno_core::op;
use serde::Deserialize;
use serde::Serialize;
use deno_core::Extension; use deno_core::Extension;
#[cfg(unix)] #[cfg(unix)]
@ -29,7 +31,7 @@ use deno_core::ResourceId;
#[cfg(unix)] #[cfg(unix)]
use std::borrow::Cow; use std::borrow::Cow;
#[cfg(unix)] #[cfg(unix)]
use tokio::signal::unix::{signal, Signal, SignalKind}; use tokio::signal::unix::{signal, Signal as TokioSignal, SignalKind};
pub fn init() -> Extension { pub fn init() -> Extension {
Extension::builder() Extension::builder()
@ -45,7 +47,7 @@ pub fn init() -> Extension {
/// The resource for signal stream. /// The resource for signal stream.
/// The second element is the waker of polling future. /// The second element is the waker of polling future.
struct SignalStreamResource { struct SignalStreamResource {
signal: AsyncRefCell<Signal>, signal: AsyncRefCell<TokioSignal>,
cancel: CancelHandle, cancel: CancelHandle,
} }
@ -61,181 +63,178 @@ impl Resource for SignalStreamResource {
} }
#[cfg(target_os = "freebsd")] #[cfg(target_os = "freebsd")]
pub fn signal_str_to_int(s: &str) -> Result<libc::c_int, AnyError> { #[derive(Debug, Serialize, Deserialize, Clone, Copy)]
match s { pub enum Signal {
"SIGHUP" => Ok(1), SIGHUP = 1,
"SIGINT" => Ok(2), SIGINT = 2,
"SIGQUIT" => Ok(3), SIGQUIT = 3,
"SIGILL" => Ok(4), SIGILL = 4,
"SIGTRAP" => Ok(5), SIGTRAP = 5,
"SIGABRT" => Ok(6), SIGABRT = 6,
"SIGEMT" => Ok(7), SIGEMT = 7,
"SIGFPE" => Ok(8), SIGFPE = 8,
"SIGKILL" => Ok(9), SIGKILL = 9,
"SIGBUS" => Ok(10), SIGBUS = 10,
"SIGSEGV" => Ok(11), SIGSEGV = 11,
"SIGSYS" => Ok(12), SIGSYS = 12,
"SIGPIPE" => Ok(13), SIGPIPE = 13,
"SIGALRM" => Ok(14), SIGALRM = 14,
"SIGTERM" => Ok(15), SIGTERM = 15,
"SIGURG" => Ok(16), SIGURG = 16,
"SIGSTOP" => Ok(17), SIGSTOP = 17,
"SIGTSTP" => Ok(18), SIGTSTP = 18,
"SIGCONT" => Ok(19), SIGCONT = 19,
"SIGCHLD" => Ok(20), SIGCHLD = 20,
"SIGTTIN" => Ok(21), SIGTTIN = 21,
"SIGTTOU" => Ok(22), SIGTTOU = 22,
"SIGIO" => Ok(23), SIGIO = 23,
"SIGXCPU" => Ok(24), SIGXCPU = 24,
"SIGXFSZ" => Ok(25), SIGXFSZ = 25,
"SIGVTALRM" => Ok(26), SIGVTALRM = 26,
"SIGPROF" => Ok(27), SIGPROF = 27,
"SIGWINCH" => Ok(28), SIGWINCH = 28,
"SIGINFO" => Ok(29), SIGINFO = 29,
"SIGUSR1" => Ok(30), SIGUSR1 = 30,
"SIGUSR2" => Ok(31), SIGUSR2 = 31,
"SIGTHR" => Ok(32), SIGTHR = 32,
"SIGLIBRT" => Ok(33), SIGLIBRT = 33,
_ => Err(type_error(format!("Invalid signal : {}", s))),
}
} }
#[cfg(any(target_os = "android", target_os = "linux"))] #[cfg(any(target_os = "android", target_os = "linux"))]
pub fn signal_str_to_int(s: &str) -> Result<libc::c_int, AnyError> { #[derive(Debug, Serialize, Deserialize, Clone, Copy)]
match s { pub enum Signal {
"SIGHUP" => Ok(1), SIGHUP = 1,
"SIGINT" => Ok(2), SIGINT = 2,
"SIGQUIT" => Ok(3), SIGQUIT = 3,
"SIGILL" => Ok(4), SIGILL = 4,
"SIGTRAP" => Ok(5), SIGTRAP = 5,
"SIGABRT" => Ok(6), SIGABRT = 6,
"SIGBUS" => Ok(7), SIGBUS = 7,
"SIGFPE" => Ok(8), SIGFPE = 8,
"SIGKILL" => Ok(9), SIGKILL = 9,
"SIGUSR1" => Ok(10), SIGUSR1 = 10,
"SIGSEGV" => Ok(11), SIGSEGV = 11,
"SIGUSR2" => Ok(12), SIGUSR2 = 12,
"SIGPIPE" => Ok(13), SIGPIPE = 13,
"SIGALRM" => Ok(14), SIGALRM = 14,
"SIGTERM" => Ok(15), SIGTERM = 15,
"SIGSTKFLT" => Ok(16), SIGSTKFLT = 16,
"SIGCHLD" => Ok(17), SIGCHLD = 17,
"SIGCONT" => Ok(18), SIGCONT = 18,
"SIGSTOP" => Ok(19), SIGSTOP = 19,
"SIGTSTP" => Ok(20), SIGTSTP = 20,
"SIGTTIN" => Ok(21), SIGTTIN = 21,
"SIGTTOU" => Ok(22), SIGTTOU = 22,
"SIGURG" => Ok(23), SIGURG = 23,
"SIGXCPU" => Ok(24), SIGXCPU = 24,
"SIGXFSZ" => Ok(25), SIGXFSZ = 25,
"SIGVTALRM" => Ok(26), SIGVTALRM = 26,
"SIGPROF" => Ok(27), SIGPROF = 27,
"SIGWINCH" => Ok(28), SIGWINCH = 28,
"SIGIO" => Ok(29), SIGIO = 29,
"SIGPWR" => Ok(30), SIGPWR = 30,
"SIGSYS" => Ok(31), SIGSYS = 31,
_ => Err(type_error(format!("Invalid signal : {}", s))),
}
} }
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
pub fn signal_str_to_int(s: &str) -> Result<libc::c_int, AnyError> { #[derive(Debug, Serialize, Deserialize, Clone, Copy)]
match s { pub enum Signal {
"SIGHUP" => Ok(1), SIGHUP = 1,
"SIGINT" => Ok(2), SIGINT = 2,
"SIGQUIT" => Ok(3), SIGQUIT = 3,
"SIGILL" => Ok(4), SIGILL = 4,
"SIGTRAP" => Ok(5), SIGTRAP = 5,
"SIGABRT" => Ok(6), SIGABRT = 6,
"SIGEMT" => Ok(7), SIGEMT = 7,
"SIGFPE" => Ok(8), SIGFPE = 8,
"SIGKILL" => Ok(9), SIGKILL = 9,
"SIGBUS" => Ok(10), SIGBUS = 10,
"SIGSEGV" => Ok(11), SIGSEGV = 11,
"SIGSYS" => Ok(12), SIGSYS = 12,
"SIGPIPE" => Ok(13), SIGPIPE = 13,
"SIGALRM" => Ok(14), SIGALRM = 14,
"SIGTERM" => Ok(15), SIGTERM = 15,
"SIGURG" => Ok(16), SIGURG = 16,
"SIGSTOP" => Ok(17), SIGSTOP = 17,
"SIGTSTP" => Ok(18), SIGTSTP = 18,
"SIGCONT" => Ok(19), SIGCONT = 19,
"SIGCHLD" => Ok(20), SIGCHLD = 20,
"SIGTTIN" => Ok(21), SIGTTIN = 21,
"SIGTTOU" => Ok(22), SIGTTOU = 22,
"SIGIO" => Ok(23), SIGIO = 23,
"SIGXCPU" => Ok(24), SIGXCPU = 24,
"SIGXFSZ" => Ok(25), SIGXFSZ = 25,
"SIGVTALRM" => Ok(26), SIGVTALRM = 26,
"SIGPROF" => Ok(27), SIGPROF = 27,
"SIGWINCH" => Ok(28), SIGWINCH = 28,
"SIGINFO" => Ok(29), SIGINFO = 29,
"SIGUSR1" => Ok(30), SIGUSR1 = 30,
"SIGUSR2" => Ok(31), SIGUSR2 = 31,
_ => Err(type_error(format!("Invalid signal: {}", s))),
}
} }
#[cfg(any(target_os = "solaris", target_os = "illumos"))] #[cfg(any(target_os = "solaris", target_os = "illumos"))]
pub fn signal_str_to_int(s: &str) -> Result<libc::c_int, AnyError> { #[derive(Debug, Serialize, Deserialize, Clone, Copy)]
match s { pub enum Signal {
"SIGHUP" => Ok(1), SIGHUP = 1,
"SIGINT" => Ok(2), SIGINT = 2,
"SIGQUIT" => Ok(3), SIGQUIT = 3,
"SIGILL" => Ok(4), SIGILL = 4,
"SIGTRAP" => Ok(5), SIGTRAP = 5,
"SIGIOT" => Ok(6), SIGIOT = 6,
"SIGABRT" => Ok(6), SIGABRT = 6,
"SIGEMT" => Ok(7), SIGEMT = 7,
"SIGFPE" => Ok(8), SIGFPE = 8,
"SIGKILL" => Ok(9), SIGKILL = 9,
"SIGBUS" => Ok(10), SIGBUS = 10,
"SIGSEGV" => Ok(11), SIGSEGV = 11,
"SIGSYS" => Ok(12), SIGSYS = 12,
"SIGPIPE" => Ok(13), SIGPIPE = 13,
"SIGALRM" => Ok(14), SIGALRM = 14,
"SIGTERM" => Ok(15), SIGTERM = 15,
"SIGUSR1" => Ok(16), SIGUSR1 = 16,
"SIGUSR2" => Ok(17), SIGUSR2 = 17,
"SIGCLD" => Ok(18), SIGCHLD = 18,
"SIGCHLD" => Ok(18), SIGPWR = 19,
"SIGPWR" => Ok(19), SIGWINCH = 20,
"SIGWINCH" => Ok(20), SIGURG = 21,
"SIGURG" => Ok(21), SIGPOLL = 22,
"SIGPOLL" => Ok(22), SIGSTOP = 23,
"SIGIO" => Ok(22), SIGTSTP = 24,
"SIGSTOP" => Ok(23), SIGCONT = 25,
"SIGTSTP" => Ok(24), SIGTTIN = 26,
"SIGCONT" => Ok(25), SIGTTOU = 27,
"SIGTTIN" => Ok(26), SIGVTALRM = 28,
"SIGTTOU" => Ok(27), SIGPROF = 29,
"SIGVTALRM" => Ok(28), SIGXCPU = 30,
"SIGPROF" => Ok(29), SIGXFSZ = 31,
"SIGXCPU" => Ok(30), SIGWAITING = 32,
"SIGXFSZ" => Ok(31), SIGLWP = 33,
"SIGWAITING" => Ok(32), SIGFREEZE = 34,
"SIGLWP" => Ok(33), SIGTHAW = 35,
"SIGFREEZE" => Ok(34), SIGCANCEL = 36,
"SIGTHAW" => Ok(35), SIGLOST = 37,
"SIGCANCEL" => Ok(36), SIGXRES = 38,
"SIGLOST" => Ok(37), SIGJVM1 = 39,
"SIGXRES" => Ok(38), SIGJVM2 = 40,
"SIGJVM1" => Ok(39), }
"SIGJVM2" => Ok(40),
_ => Err(type_error(format!("Invalid signal : {}", s))), #[cfg(target_os = "windows")]
} #[derive(Debug, Serialize, Deserialize, Clone, Copy)]
pub enum Signal {
SIGKILL,
SIGTERM,
} }
#[cfg(unix)] #[cfg(unix)]
#[op] #[op]
fn op_signal_bind( fn op_signal_bind(
state: &mut OpState, state: &mut OpState,
sig: String, sig: Signal,
) -> Result<ResourceId, AnyError> { ) -> Result<ResourceId, AnyError> {
let signo = signal_str_to_int(&sig)?; let signo = sig as libc::c_int;
if signal_hook_registry::FORBIDDEN.contains(&signo) { if signal_hook_registry::FORBIDDEN.contains(&signo) {
return Err(type_error(format!( return Err(type_error(format!(
"Binding to signal '{}' is not allowed", "Binding to signal '{:?}' is not allowed",
sig sig
))); )));
} }