2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-09-11 18:10:43 -04:00
|
|
|
use deno_core::op2;
|
2020-12-16 11:14:12 -05:00
|
|
|
use deno_core::AsyncRefCell;
|
|
|
|
use deno_core::CancelFuture;
|
|
|
|
use deno_core::CancelHandle;
|
2022-06-13 16:39:46 -04:00
|
|
|
use deno_core::OpState;
|
2020-12-16 11:14:12 -05:00
|
|
|
use deno_core::RcRef;
|
|
|
|
use deno_core::Resource;
|
2021-04-05 12:40:24 -04:00
|
|
|
use deno_core::ResourceId;
|
2022-06-13 16:39:46 -04:00
|
|
|
|
2020-12-16 11:14:12 -05:00
|
|
|
use std::borrow::Cow;
|
2022-06-13 16:39:46 -04:00
|
|
|
use std::cell::RefCell;
|
2024-03-11 13:22:28 -04:00
|
|
|
#[cfg(unix)]
|
|
|
|
use std::collections::BTreeMap;
|
2022-06-13 16:39:46 -04:00
|
|
|
use std::rc::Rc;
|
2024-03-11 13:22:28 -04:00
|
|
|
#[cfg(unix)]
|
|
|
|
use std::sync::atomic::AtomicBool;
|
|
|
|
#[cfg(unix)]
|
|
|
|
use std::sync::Arc;
|
2022-06-13 16:39:46 -04:00
|
|
|
|
2020-01-24 08:15:31 -05:00
|
|
|
#[cfg(unix)]
|
2023-01-14 23:18:58 -05:00
|
|
|
use tokio::signal::unix::signal;
|
|
|
|
#[cfg(unix)]
|
|
|
|
use tokio::signal::unix::Signal;
|
|
|
|
#[cfg(unix)]
|
|
|
|
use tokio::signal::unix::SignalKind;
|
|
|
|
#[cfg(windows)]
|
|
|
|
use tokio::signal::windows::ctrl_break;
|
|
|
|
#[cfg(windows)]
|
|
|
|
use tokio::signal::windows::ctrl_c;
|
|
|
|
#[cfg(windows)]
|
|
|
|
use tokio::signal::windows::CtrlBreak;
|
2022-06-13 16:39:46 -04:00
|
|
|
#[cfg(windows)]
|
2023-01-14 23:18:58 -05:00
|
|
|
use tokio::signal::windows::CtrlC;
|
2020-01-24 08:15:31 -05:00
|
|
|
|
2023-03-17 14:22:15 -04:00
|
|
|
deno_core::extension!(
|
|
|
|
deno_signal,
|
2023-03-18 18:30:04 -04:00
|
|
|
ops = [op_signal_bind, op_signal_unbind, op_signal_poll],
|
2024-03-11 13:22:28 -04:00
|
|
|
state = |state| {
|
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
state.put(SignalState::default());
|
|
|
|
}
|
|
|
|
}
|
2023-03-17 14:22:15 -04:00
|
|
|
);
|
2020-01-24 08:15:31 -05:00
|
|
|
|
2024-10-22 04:41:08 -04:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
pub enum SignalError {
|
2024-11-29 17:36:43 -05:00
|
|
|
#[error(transparent)]
|
|
|
|
InvalidSignalStr(#[from] crate::signal::InvalidSignalStrError),
|
|
|
|
#[error(transparent)]
|
|
|
|
InvalidSignalInt(#[from] crate::signal::InvalidSignalIntError),
|
2024-10-22 04:41:08 -04:00
|
|
|
#[error("Binding to signal '{0}' is not allowed")]
|
|
|
|
SignalNotAllowed(String),
|
|
|
|
#[error("{0}")]
|
|
|
|
Io(#[from] std::io::Error),
|
|
|
|
}
|
|
|
|
|
2024-03-11 13:22:28 -04:00
|
|
|
#[cfg(unix)]
|
|
|
|
#[derive(Default)]
|
|
|
|
struct SignalState {
|
|
|
|
enable_default_handlers: BTreeMap<libc::c_int, Arc<AtomicBool>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
impl SignalState {
|
|
|
|
/// Disable the default signal handler for the given signal.
|
|
|
|
///
|
|
|
|
/// Returns the shared flag to enable the default handler later, and whether a default handler already existed.
|
|
|
|
fn disable_default_handler(
|
|
|
|
&mut self,
|
|
|
|
signo: libc::c_int,
|
|
|
|
) -> (Arc<AtomicBool>, bool) {
|
|
|
|
use std::collections::btree_map::Entry;
|
|
|
|
|
|
|
|
match self.enable_default_handlers.entry(signo) {
|
|
|
|
Entry::Occupied(entry) => {
|
|
|
|
let enable = entry.get();
|
|
|
|
enable.store(false, std::sync::atomic::Ordering::Release);
|
|
|
|
(enable.clone(), true)
|
|
|
|
}
|
|
|
|
Entry::Vacant(entry) => {
|
|
|
|
let enable = Arc::new(AtomicBool::new(false));
|
|
|
|
entry.insert(enable.clone());
|
|
|
|
(enable, false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 08:15:31 -05:00
|
|
|
#[cfg(unix)]
|
|
|
|
/// The resource for signal stream.
|
|
|
|
/// The second element is the waker of polling future.
|
2020-12-16 11:14:12 -05:00
|
|
|
struct SignalStreamResource {
|
2022-05-14 23:57:09 -04:00
|
|
|
signal: AsyncRefCell<Signal>,
|
2024-03-11 13:22:28 -04:00
|
|
|
enable_default_handler: Arc<AtomicBool>,
|
2020-12-16 11:14:12 -05:00
|
|
|
cancel: CancelHandle,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
impl Resource for SignalStreamResource {
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"signal".into()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn close(self: Rc<Self>) {
|
|
|
|
self.cancel.cancel();
|
|
|
|
}
|
|
|
|
}
|
2020-01-24 08:15:31 -05:00
|
|
|
|
2022-06-13 16:39:46 -04:00
|
|
|
// TODO: CtrlClose could be mapped to SIGHUP but that needs a
|
|
|
|
// tokio::windows::signal::CtrlClose type, or something from a different crate
|
|
|
|
#[cfg(windows)]
|
|
|
|
enum WindowsSignal {
|
|
|
|
Sigint(CtrlC),
|
|
|
|
Sigbreak(CtrlBreak),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
impl From<CtrlC> for WindowsSignal {
|
|
|
|
fn from(ctrl_c: CtrlC) -> Self {
|
|
|
|
WindowsSignal::Sigint(ctrl_c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
impl From<CtrlBreak> for WindowsSignal {
|
|
|
|
fn from(ctrl_break: CtrlBreak) -> Self {
|
|
|
|
WindowsSignal::Sigbreak(ctrl_break)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
impl WindowsSignal {
|
|
|
|
pub async fn recv(&mut self) -> Option<()> {
|
|
|
|
match self {
|
|
|
|
WindowsSignal::Sigint(ctrl_c) => ctrl_c.recv().await,
|
|
|
|
WindowsSignal::Sigbreak(ctrl_break) => ctrl_break.recv().await,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
struct SignalStreamResource {
|
|
|
|
signal: AsyncRefCell<WindowsSignal>,
|
|
|
|
cancel: CancelHandle,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
impl Resource for SignalStreamResource {
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"signal".into()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn close(self: Rc<Self>) {
|
|
|
|
self.cancel.cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 08:15:31 -05:00
|
|
|
#[cfg(unix)]
|
2023-09-13 18:01:31 -04:00
|
|
|
#[op2(fast)]
|
|
|
|
#[smi]
|
2020-01-24 08:15:31 -05:00
|
|
|
fn op_signal_bind(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2023-09-13 18:01:31 -04:00
|
|
|
#[string] sig: &str,
|
2024-10-22 04:41:08 -04:00
|
|
|
) -> Result<ResourceId, SignalError> {
|
2024-11-29 17:36:43 -05:00
|
|
|
let signo = crate::signal::signal_str_to_int(sig)?;
|
2022-01-04 15:55:06 -05:00
|
|
|
if signal_hook_registry::FORBIDDEN.contains(&signo) {
|
2024-10-22 04:41:08 -04:00
|
|
|
return Err(SignalError::SignalNotAllowed(sig.to_string()));
|
2022-01-04 15:55:06 -05:00
|
|
|
}
|
2024-03-11 13:22:28 -04:00
|
|
|
|
|
|
|
let signal = AsyncRefCell::new(signal(SignalKind::from_raw(signo))?);
|
|
|
|
|
|
|
|
let (enable_default_handler, has_default_handler) = state
|
|
|
|
.borrow_mut::<SignalState>()
|
|
|
|
.disable_default_handler(signo);
|
|
|
|
|
2020-12-16 11:14:12 -05:00
|
|
|
let resource = SignalStreamResource {
|
2024-03-11 13:22:28 -04:00
|
|
|
signal,
|
2020-12-16 11:14:12 -05:00
|
|
|
cancel: Default::default(),
|
2024-03-11 13:22:28 -04:00
|
|
|
enable_default_handler: enable_default_handler.clone(),
|
2020-12-16 11:14:12 -05:00
|
|
|
};
|
|
|
|
let rid = state.resource_table.add(resource);
|
2024-03-11 13:22:28 -04:00
|
|
|
|
|
|
|
if !has_default_handler {
|
|
|
|
// restore default signal handler when the signal is unbound
|
|
|
|
// this can error if the signal is not supported, if so let's just leave it as is
|
|
|
|
let _ = signal_hook::flag::register_conditional_default(
|
|
|
|
signo,
|
|
|
|
enable_default_handler,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(rid)
|
2020-01-24 08:15:31 -05:00
|
|
|
}
|
|
|
|
|
2022-06-13 16:39:46 -04:00
|
|
|
#[cfg(windows)]
|
2023-09-13 18:01:31 -04:00
|
|
|
#[op2(fast)]
|
|
|
|
#[smi]
|
2022-06-13 16:39:46 -04:00
|
|
|
fn op_signal_bind(
|
|
|
|
state: &mut OpState,
|
2023-09-13 18:01:31 -04:00
|
|
|
#[string] sig: &str,
|
2024-10-22 04:41:08 -04:00
|
|
|
) -> Result<ResourceId, SignalError> {
|
2024-11-29 17:36:43 -05:00
|
|
|
let signo = crate::signal::signal_str_to_int(sig)?;
|
2022-06-13 16:39:46 -04:00
|
|
|
let resource = SignalStreamResource {
|
|
|
|
signal: AsyncRefCell::new(match signo {
|
|
|
|
// SIGINT
|
|
|
|
2 => ctrl_c()
|
|
|
|
.expect("There was an issue creating ctrl+c event stream.")
|
|
|
|
.into(),
|
|
|
|
// SIGBREAK
|
|
|
|
21 => ctrl_break()
|
|
|
|
.expect("There was an issue creating ctrl+break event stream.")
|
|
|
|
.into(),
|
|
|
|
_ => unimplemented!(),
|
|
|
|
}),
|
|
|
|
cancel: Default::default(),
|
|
|
|
};
|
|
|
|
let rid = state.resource_table.add(resource);
|
|
|
|
Ok(rid)
|
|
|
|
}
|
|
|
|
|
2023-09-11 18:10:43 -04:00
|
|
|
#[op2(async)]
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_signal_poll(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2023-09-11 18:10:43 -04:00
|
|
|
#[smi] rid: ResourceId,
|
2024-10-22 04:41:08 -04:00
|
|
|
) -> Result<bool, deno_core::error::AnyError> {
|
2020-12-16 11:14:12 -05:00
|
|
|
let resource = state
|
|
|
|
.borrow_mut()
|
|
|
|
.resource_table
|
2021-08-15 07:29:19 -04:00
|
|
|
.get::<SignalStreamResource>(rid)?;
|
2022-06-13 16:39:46 -04:00
|
|
|
|
2020-12-16 11:14:12 -05:00
|
|
|
let cancel = RcRef::map(&resource, |r| &r.cancel);
|
|
|
|
let mut signal = RcRef::map(&resource, |r| &r.signal).borrow_mut().await;
|
|
|
|
|
|
|
|
match signal.recv().or_cancel(cancel).await {
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(result) => Ok(result.is_none()),
|
|
|
|
Err(_) => Ok(true),
|
2020-12-16 11:14:12 -05:00
|
|
|
}
|
2020-01-24 08:15:31 -05:00
|
|
|
}
|
|
|
|
|
2023-09-11 18:10:43 -04:00
|
|
|
#[op2(fast)]
|
2020-01-24 08:15:31 -05:00
|
|
|
pub fn op_signal_unbind(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2023-09-11 18:10:43 -04:00
|
|
|
#[smi] rid: ResourceId,
|
2024-10-22 04:41:08 -04:00
|
|
|
) -> Result<(), deno_core::error::AnyError> {
|
2024-03-11 13:22:28 -04:00
|
|
|
let resource = state.resource_table.take::<SignalStreamResource>(rid)?;
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
resource
|
|
|
|
.enable_default_handler
|
|
|
|
.store(true, std::sync::atomic::Ordering::Release);
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.close();
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(())
|
2020-01-24 08:15:31 -05:00
|
|
|
}
|