1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-25 15:29:32 -05:00

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

This reverts commit c496639d5d.
This commit is contained in:
Leo Kettmeir 2022-05-15 05:57:09 +02:00 committed by GitHub
parent c496639d5d
commit b08b1da6f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 170 additions and 166 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 SIGTERM to the current // subprocess we can safely kill. Instead we send SIGCONT 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, "SIGTERM"); Deno.kill(Deno.pid, "SIGCONT");
}, Deno.errors.PermissionDenied); }, Deno.errors.PermissionDenied);
}); });

View file

@ -288,15 +288,16 @@ async fn op_run_status(
} }
#[cfg(unix)] #[cfg(unix)]
pub fn kill(pid: i32, signal: super::signal::Signal) -> Result<(), AnyError> { pub fn kill(pid: i32, signal: &str) -> 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(signal as libc::c_int)?; let sig = Signal::try_from(signo)?;
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: super::signal::Signal) -> Result<(), AnyError> { pub fn kill(pid: i32, signal: &str) -> 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;
@ -310,7 +311,9 @@ pub fn kill(pid: i32, _signal: super::signal::Signal) -> 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 pid <= 0 { if !matches!(signal, "SIGKILL" | "SIGTERM") {
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) };
@ -336,9 +339,9 @@ pub fn kill(pid: i32, _signal: super::signal::Signal) -> Result<(), AnyError> {
fn op_kill( fn op_kill(
state: &mut OpState, state: &mut OpState,
pid: i32, pid: i32,
signal: super::signal::Signal, signal: String,
) -> 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,8 +5,6 @@ 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)]
@ -31,7 +29,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 as TokioSignal, SignalKind}; use tokio::signal::unix::{signal, Signal, SignalKind};
pub fn init() -> Extension { pub fn init() -> Extension {
Extension::builder() Extension::builder()
@ -47,7 +45,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<TokioSignal>, signal: AsyncRefCell<Signal>,
cancel: CancelHandle, cancel: CancelHandle,
} }
@ -63,178 +61,181 @@ impl Resource for SignalStreamResource {
} }
#[cfg(target_os = "freebsd")] #[cfg(target_os = "freebsd")]
#[derive(Debug, Serialize, Deserialize, Clone, Copy)] pub fn signal_str_to_int(s: &str) -> Result<libc::c_int, AnyError> {
pub enum Signal { match s {
SIGHUP = 1, "SIGHUP" => Ok(1),
SIGINT = 2, "SIGINT" => Ok(2),
SIGQUIT = 3, "SIGQUIT" => Ok(3),
SIGILL = 4, "SIGILL" => Ok(4),
SIGTRAP = 5, "SIGTRAP" => Ok(5),
SIGABRT = 6, "SIGABRT" => Ok(6),
SIGEMT = 7, "SIGEMT" => Ok(7),
SIGFPE = 8, "SIGFPE" => Ok(8),
SIGKILL = 9, "SIGKILL" => Ok(9),
SIGBUS = 10, "SIGBUS" => Ok(10),
SIGSEGV = 11, "SIGSEGV" => Ok(11),
SIGSYS = 12, "SIGSYS" => Ok(12),
SIGPIPE = 13, "SIGPIPE" => Ok(13),
SIGALRM = 14, "SIGALRM" => Ok(14),
SIGTERM = 15, "SIGTERM" => Ok(15),
SIGURG = 16, "SIGURG" => Ok(16),
SIGSTOP = 17, "SIGSTOP" => Ok(17),
SIGTSTP = 18, "SIGTSTP" => Ok(18),
SIGCONT = 19, "SIGCONT" => Ok(19),
SIGCHLD = 20, "SIGCHLD" => Ok(20),
SIGTTIN = 21, "SIGTTIN" => Ok(21),
SIGTTOU = 22, "SIGTTOU" => Ok(22),
SIGIO = 23, "SIGIO" => Ok(23),
SIGXCPU = 24, "SIGXCPU" => Ok(24),
SIGXFSZ = 25, "SIGXFSZ" => Ok(25),
SIGVTALRM = 26, "SIGVTALRM" => Ok(26),
SIGPROF = 27, "SIGPROF" => Ok(27),
SIGWINCH = 28, "SIGWINCH" => Ok(28),
SIGINFO = 29, "SIGINFO" => Ok(29),
SIGUSR1 = 30, "SIGUSR1" => Ok(30),
SIGUSR2 = 31, "SIGUSR2" => Ok(31),
SIGTHR = 32, "SIGTHR" => Ok(32),
SIGLIBRT = 33, "SIGLIBRT" => Ok(33),
_ => Err(type_error(format!("Invalid signal : {}", s))),
}
} }
#[cfg(any(target_os = "android", target_os = "linux"))] #[cfg(any(target_os = "android", target_os = "linux"))]
#[derive(Debug, Serialize, Deserialize, Clone, Copy)] pub fn signal_str_to_int(s: &str) -> Result<libc::c_int, AnyError> {
pub enum Signal { match s {
SIGHUP = 1, "SIGHUP" => Ok(1),
SIGINT = 2, "SIGINT" => Ok(2),
SIGQUIT = 3, "SIGQUIT" => Ok(3),
SIGILL = 4, "SIGILL" => Ok(4),
SIGTRAP = 5, "SIGTRAP" => Ok(5),
SIGABRT = 6, "SIGABRT" => Ok(6),
SIGBUS = 7, "SIGBUS" => Ok(7),
SIGFPE = 8, "SIGFPE" => Ok(8),
SIGKILL = 9, "SIGKILL" => Ok(9),
SIGUSR1 = 10, "SIGUSR1" => Ok(10),
SIGSEGV = 11, "SIGSEGV" => Ok(11),
SIGUSR2 = 12, "SIGUSR2" => Ok(12),
SIGPIPE = 13, "SIGPIPE" => Ok(13),
SIGALRM = 14, "SIGALRM" => Ok(14),
SIGTERM = 15, "SIGTERM" => Ok(15),
SIGSTKFLT = 16, "SIGSTKFLT" => Ok(16),
SIGCHLD = 17, "SIGCHLD" => Ok(17),
SIGCONT = 18, "SIGCONT" => Ok(18),
SIGSTOP = 19, "SIGSTOP" => Ok(19),
SIGTSTP = 20, "SIGTSTP" => Ok(20),
SIGTTIN = 21, "SIGTTIN" => Ok(21),
SIGTTOU = 22, "SIGTTOU" => Ok(22),
SIGURG = 23, "SIGURG" => Ok(23),
SIGXCPU = 24, "SIGXCPU" => Ok(24),
SIGXFSZ = 25, "SIGXFSZ" => Ok(25),
SIGVTALRM = 26, "SIGVTALRM" => Ok(26),
SIGPROF = 27, "SIGPROF" => Ok(27),
SIGWINCH = 28, "SIGWINCH" => Ok(28),
SIGIO = 29, "SIGIO" => Ok(29),
SIGPWR = 30, "SIGPWR" => Ok(30),
SIGSYS = 31, "SIGSYS" => Ok(31),
_ => Err(type_error(format!("Invalid signal : {}", s))),
}
} }
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
#[derive(Debug, Serialize, Deserialize, Clone, Copy)] pub fn signal_str_to_int(s: &str) -> Result<libc::c_int, AnyError> {
pub enum Signal { match s {
SIGHUP = 1, "SIGHUP" => Ok(1),
SIGINT = 2, "SIGINT" => Ok(2),
SIGQUIT = 3, "SIGQUIT" => Ok(3),
SIGILL = 4, "SIGILL" => Ok(4),
SIGTRAP = 5, "SIGTRAP" => Ok(5),
SIGABRT = 6, "SIGABRT" => Ok(6),
SIGEMT = 7, "SIGEMT" => Ok(7),
SIGFPE = 8, "SIGFPE" => Ok(8),
SIGKILL = 9, "SIGKILL" => Ok(9),
SIGBUS = 10, "SIGBUS" => Ok(10),
SIGSEGV = 11, "SIGSEGV" => Ok(11),
SIGSYS = 12, "SIGSYS" => Ok(12),
SIGPIPE = 13, "SIGPIPE" => Ok(13),
SIGALRM = 14, "SIGALRM" => Ok(14),
SIGTERM = 15, "SIGTERM" => Ok(15),
SIGURG = 16, "SIGURG" => Ok(16),
SIGSTOP = 17, "SIGSTOP" => Ok(17),
SIGTSTP = 18, "SIGTSTP" => Ok(18),
SIGCONT = 19, "SIGCONT" => Ok(19),
SIGCHLD = 20, "SIGCHLD" => Ok(20),
SIGTTIN = 21, "SIGTTIN" => Ok(21),
SIGTTOU = 22, "SIGTTOU" => Ok(22),
SIGIO = 23, "SIGIO" => Ok(23),
SIGXCPU = 24, "SIGXCPU" => Ok(24),
SIGXFSZ = 25, "SIGXFSZ" => Ok(25),
SIGVTALRM = 26, "SIGVTALRM" => Ok(26),
SIGPROF = 27, "SIGPROF" => Ok(27),
SIGWINCH = 28, "SIGWINCH" => Ok(28),
SIGINFO = 29, "SIGINFO" => Ok(29),
SIGUSR1 = 30, "SIGUSR1" => Ok(30),
SIGUSR2 = 31, "SIGUSR2" => Ok(31),
_ => Err(type_error(format!("Invalid signal: {}", s))),
}
} }
#[cfg(any(target_os = "solaris", target_os = "illumos"))] #[cfg(any(target_os = "solaris", target_os = "illumos"))]
#[derive(Debug, Serialize, Deserialize, Clone, Copy)] pub fn signal_str_to_int(s: &str) -> Result<libc::c_int, AnyError> {
pub enum Signal { match s {
SIGHUP = 1, "SIGHUP" => Ok(1),
SIGINT = 2, "SIGINT" => Ok(2),
SIGQUIT = 3, "SIGQUIT" => Ok(3),
SIGILL = 4, "SIGILL" => Ok(4),
SIGTRAP = 5, "SIGTRAP" => Ok(5),
SIGIOT = 6, "SIGIOT" => Ok(6),
SIGABRT = 6, "SIGABRT" => Ok(6),
SIGEMT = 7, "SIGEMT" => Ok(7),
SIGFPE = 8, "SIGFPE" => Ok(8),
SIGKILL = 9, "SIGKILL" => Ok(9),
SIGBUS = 10, "SIGBUS" => Ok(10),
SIGSEGV = 11, "SIGSEGV" => Ok(11),
SIGSYS = 12, "SIGSYS" => Ok(12),
SIGPIPE = 13, "SIGPIPE" => Ok(13),
SIGALRM = 14, "SIGALRM" => Ok(14),
SIGTERM = 15, "SIGTERM" => Ok(15),
SIGUSR1 = 16, "SIGUSR1" => Ok(16),
SIGUSR2 = 17, "SIGUSR2" => Ok(17),
SIGCHLD = 18, "SIGCLD" => Ok(18),
SIGPWR = 19, "SIGCHLD" => Ok(18),
SIGWINCH = 20, "SIGPWR" => Ok(19),
SIGURG = 21, "SIGWINCH" => Ok(20),
SIGPOLL = 22, "SIGURG" => Ok(21),
SIGSTOP = 23, "SIGPOLL" => Ok(22),
SIGTSTP = 24, "SIGIO" => Ok(22),
SIGCONT = 25, "SIGSTOP" => Ok(23),
SIGTTIN = 26, "SIGTSTP" => Ok(24),
SIGTTOU = 27, "SIGCONT" => Ok(25),
SIGVTALRM = 28, "SIGTTIN" => Ok(26),
SIGPROF = 29, "SIGTTOU" => Ok(27),
SIGXCPU = 30, "SIGVTALRM" => Ok(28),
SIGXFSZ = 31, "SIGPROF" => Ok(29),
SIGWAITING = 32, "SIGXCPU" => Ok(30),
SIGLWP = 33, "SIGXFSZ" => Ok(31),
SIGFREEZE = 34, "SIGWAITING" => Ok(32),
SIGTHAW = 35, "SIGLWP" => Ok(33),
SIGCANCEL = 36, "SIGFREEZE" => Ok(34),
SIGLOST = 37, "SIGTHAW" => Ok(35),
SIGXRES = 38, "SIGCANCEL" => Ok(36),
SIGJVM1 = 39, "SIGLOST" => Ok(37),
SIGJVM2 = 40, "SIGXRES" => Ok(38),
} "SIGJVM1" => Ok(39),
"SIGJVM2" => Ok(40),
#[cfg(target_os = "windows")] _ => Err(type_error(format!("Invalid signal : {}", s))),
#[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: Signal, sig: String,
) -> Result<ResourceId, AnyError> { ) -> Result<ResourceId, AnyError> {
let signo = sig as libc::c_int; let signo = signal_str_to_int(&sig)?;
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
))); )));
} }