2021-01-10 21:59:07 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2021-09-07 11:34:46 -04:00
|
|
|
#[cfg(not(unix))]
|
|
|
|
use deno_core::error::generic_error;
|
2021-09-07 18:45:13 -04:00
|
|
|
#[cfg(not(target_os = "windows"))]
|
2021-09-06 10:05:33 -04:00
|
|
|
use deno_core::error::type_error;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2021-11-25 13:49:09 -05:00
|
|
|
use deno_core::op_async;
|
2021-05-02 19:22:57 -04:00
|
|
|
use deno_core::op_sync;
|
|
|
|
use deno_core::Extension;
|
2020-09-10 09:57:45 -04:00
|
|
|
use deno_core::OpState;
|
|
|
|
use std::cell::RefCell;
|
2020-08-18 12:30:13 -04:00
|
|
|
use std::rc::Rc;
|
2020-01-24 08:15:31 -05:00
|
|
|
|
|
|
|
#[cfg(unix)]
|
2020-12-16 11:14:12 -05:00
|
|
|
use deno_core::AsyncRefCell;
|
|
|
|
#[cfg(unix)]
|
|
|
|
use deno_core::CancelFuture;
|
|
|
|
#[cfg(unix)]
|
|
|
|
use deno_core::CancelHandle;
|
|
|
|
#[cfg(unix)]
|
|
|
|
use deno_core::RcRef;
|
|
|
|
#[cfg(unix)]
|
|
|
|
use deno_core::Resource;
|
|
|
|
#[cfg(unix)]
|
2021-04-05 12:40:24 -04:00
|
|
|
use deno_core::ResourceId;
|
2020-09-05 20:34:02 -04:00
|
|
|
#[cfg(unix)]
|
2020-12-16 11:14:12 -05:00
|
|
|
use std::borrow::Cow;
|
2020-01-24 08:15:31 -05:00
|
|
|
#[cfg(unix)]
|
|
|
|
use tokio::signal::unix::{signal, Signal, SignalKind};
|
|
|
|
|
2021-05-02 19:22:57 -04:00
|
|
|
pub fn init() -> Extension {
|
|
|
|
Extension::builder()
|
|
|
|
.ops(vec![
|
|
|
|
("op_signal_bind", op_sync(op_signal_bind)),
|
|
|
|
("op_signal_unbind", op_sync(op_signal_unbind)),
|
2021-11-25 13:49:09 -05:00
|
|
|
("op_signal_poll", op_async(op_signal_poll)),
|
2021-05-02 19:22:57 -04:00
|
|
|
])
|
|
|
|
.build()
|
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 {
|
|
|
|
signal: AsyncRefCell<Signal>,
|
|
|
|
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
|
|
|
|
2021-09-21 09:16:05 -04:00
|
|
|
#[cfg(target_os = "freebsd")]
|
|
|
|
pub fn signal_str_to_int(s: &str) -> Result<libc::c_int, AnyError> {
|
|
|
|
match s {
|
|
|
|
"SIGHUP" => Ok(1),
|
|
|
|
"SIGINT" => Ok(2),
|
|
|
|
"SIGQUIT" => Ok(3),
|
|
|
|
"SIGILL" => Ok(4),
|
|
|
|
"SIGTRAP" => Ok(5),
|
|
|
|
"SIGABRT" => Ok(6),
|
|
|
|
"SIGEMT" => Ok(7),
|
|
|
|
"SIGFPE" => Ok(8),
|
|
|
|
"SIGKILL" => Ok(9),
|
|
|
|
"SIGBUS" => Ok(10),
|
|
|
|
"SIGSEGV" => Ok(11),
|
|
|
|
"SIGSYS" => Ok(12),
|
|
|
|
"SIGPIPE" => Ok(13),
|
|
|
|
"SIGALRM" => Ok(14),
|
|
|
|
"SIGTERM" => Ok(15),
|
|
|
|
"SIGURG" => Ok(16),
|
|
|
|
"SIGSTOP" => Ok(17),
|
|
|
|
"SIGTSTP" => Ok(18),
|
|
|
|
"SIGCONT" => Ok(19),
|
|
|
|
"SIGCHLD" => Ok(20),
|
|
|
|
"SIGTTIN" => Ok(21),
|
|
|
|
"SIGTTOU" => Ok(22),
|
|
|
|
"SIGIO" => Ok(23),
|
|
|
|
"SIGXCPU" => Ok(24),
|
|
|
|
"SIGXFSZ" => Ok(25),
|
|
|
|
"SIGVTALRM" => Ok(26),
|
|
|
|
"SIGPROF" => Ok(27),
|
|
|
|
"SIGWINCH" => Ok(28),
|
|
|
|
"SIGINFO" => Ok(29),
|
|
|
|
"SIGUSR1" => Ok(30),
|
|
|
|
"SIGUSR2" => Ok(31),
|
|
|
|
"SIGTHR" => Ok(32),
|
|
|
|
"SIGLIBRT" => Ok(33),
|
|
|
|
_ => Err(type_error(format!("Invalid signal : {}", s))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-06 10:05:33 -04:00
|
|
|
#[cfg(target_os = "linux")]
|
2021-09-07 18:45:13 -04:00
|
|
|
pub fn signal_str_to_int(s: &str) -> Result<libc::c_int, AnyError> {
|
2021-09-06 10:05:33 -04:00
|
|
|
match s {
|
2021-09-07 18:45:13 -04:00
|
|
|
"SIGHUP" => Ok(1),
|
|
|
|
"SIGINT" => Ok(2),
|
|
|
|
"SIGQUIT" => Ok(3),
|
|
|
|
"SIGILL" => Ok(4),
|
|
|
|
"SIGTRAP" => Ok(5),
|
|
|
|
"SIGABRT" => Ok(6),
|
|
|
|
"SIGBUS" => Ok(7),
|
|
|
|
"SIGFPE" => Ok(8),
|
|
|
|
"SIGKILL" => Ok(9),
|
|
|
|
"SIGUSR1" => Ok(10),
|
|
|
|
"SIGSEGV" => Ok(11),
|
|
|
|
"SIGUSR2" => Ok(12),
|
|
|
|
"SIGPIPE" => Ok(13),
|
|
|
|
"SIGALRM" => Ok(14),
|
|
|
|
"SIGTERM" => Ok(15),
|
|
|
|
"SIGSTKFLT" => Ok(16),
|
|
|
|
"SIGCHLD" => Ok(17),
|
|
|
|
"SIGCONT" => Ok(18),
|
|
|
|
"SIGSTOP" => Ok(19),
|
|
|
|
"SIGTSTP" => Ok(20),
|
|
|
|
"SIGTTIN" => Ok(21),
|
|
|
|
"SIGTTOU" => Ok(22),
|
|
|
|
"SIGURG" => Ok(23),
|
|
|
|
"SIGXCPU" => Ok(24),
|
|
|
|
"SIGXFSZ" => Ok(25),
|
|
|
|
"SIGVTALRM" => Ok(26),
|
|
|
|
"SIGPROF" => Ok(27),
|
|
|
|
"SIGWINCH" => Ok(28),
|
|
|
|
"SIGIO" => Ok(29),
|
|
|
|
"SIGPWR" => Ok(30),
|
|
|
|
"SIGSYS" => Ok(31),
|
|
|
|
_ => Err(type_error(format!("Invalid signal : {}", s))),
|
2021-09-06 10:05:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
2021-09-07 18:45:13 -04:00
|
|
|
pub fn signal_str_to_int(s: &str) -> Result<libc::c_int, AnyError> {
|
2021-09-06 10:05:33 -04:00
|
|
|
match s {
|
2021-09-07 18:45:13 -04:00
|
|
|
"SIGHUP" => Ok(1),
|
|
|
|
"SIGINT" => Ok(2),
|
|
|
|
"SIGQUIT" => Ok(3),
|
|
|
|
"SIGILL" => Ok(4),
|
|
|
|
"SIGTRAP" => Ok(5),
|
|
|
|
"SIGABRT" => Ok(6),
|
|
|
|
"SIGEMT" => Ok(7),
|
|
|
|
"SIGFPE" => Ok(8),
|
|
|
|
"SIGKILL" => Ok(9),
|
|
|
|
"SIGBUS" => Ok(10),
|
|
|
|
"SIGSEGV" => Ok(11),
|
|
|
|
"SIGSYS" => Ok(12),
|
|
|
|
"SIGPIPE" => Ok(13),
|
|
|
|
"SIGALRM" => Ok(14),
|
|
|
|
"SIGTERM" => Ok(15),
|
|
|
|
"SIGURG" => Ok(16),
|
|
|
|
"SIGSTOP" => Ok(17),
|
|
|
|
"SIGTSTP" => Ok(18),
|
|
|
|
"SIGCONT" => Ok(19),
|
|
|
|
"SIGCHLD" => Ok(20),
|
|
|
|
"SIGTTIN" => Ok(21),
|
|
|
|
"SIGTTOU" => Ok(22),
|
|
|
|
"SIGIO" => Ok(23),
|
|
|
|
"SIGXCPU" => Ok(24),
|
|
|
|
"SIGXFSZ" => Ok(25),
|
|
|
|
"SIGVTALRM" => Ok(26),
|
|
|
|
"SIGPROF" => Ok(27),
|
|
|
|
"SIGWINCH" => Ok(28),
|
|
|
|
"SIGINFO" => Ok(29),
|
|
|
|
"SIGUSR1" => Ok(30),
|
|
|
|
"SIGUSR2" => Ok(31),
|
2021-09-27 06:18:02 -04:00
|
|
|
_ => Err(type_error(format!("Invalid signal: {}", s))),
|
2021-09-06 10:05:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 08:15:31 -05:00
|
|
|
#[cfg(unix)]
|
|
|
|
fn op_signal_bind(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-09-06 10:05:33 -04:00
|
|
|
sig: String,
|
2021-05-08 08:37:42 -04:00
|
|
|
_: (),
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<ResourceId, AnyError> {
|
2020-09-26 14:26:51 -04:00
|
|
|
super::check_unstable(state, "Deno.signal");
|
2021-09-07 18:45:13 -04:00
|
|
|
let signo = signal_str_to_int(&sig)?;
|
2020-12-16 11:14:12 -05:00
|
|
|
let resource = SignalStreamResource {
|
2021-09-06 10:05:33 -04:00
|
|
|
signal: AsyncRefCell::new(signal(SignalKind::from_raw(signo)).unwrap()),
|
2020-12-16 11:14:12 -05:00
|
|
|
cancel: Default::default(),
|
|
|
|
};
|
|
|
|
let rid = state.resource_table.add(resource);
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(rid)
|
2020-01-24 08:15:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
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>>,
|
2021-04-05 12:40:24 -04:00
|
|
|
rid: ResourceId,
|
2021-05-08 08:37:42 -04:00
|
|
|
_: (),
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<bool, AnyError> {
|
2020-09-26 14:26:51 -04:00
|
|
|
super::check_unstable2(&state, "Deno.signal");
|
2020-01-24 08:15:31 -05:00
|
|
|
|
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)?;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
pub fn op_signal_unbind(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-04-05 12:40:24 -04:00
|
|
|
rid: ResourceId,
|
2021-05-08 08:37:42 -04:00
|
|
|
_: (),
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2020-09-26 14:26:51 -04:00
|
|
|
super::check_unstable(state, "Deno.signal");
|
2021-08-15 07:29:19 -04:00
|
|
|
state.resource_table.close(rid)?;
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(())
|
2020-01-24 08:15:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
pub fn op_signal_bind(
|
2020-09-10 09:57:45 -04:00
|
|
|
_state: &mut OpState,
|
2021-10-05 16:38:27 -04:00
|
|
|
_: (),
|
2021-05-08 08:37:42 -04:00
|
|
|
_: (),
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2021-09-07 11:34:46 -04:00
|
|
|
Err(generic_error("not implemented"))
|
2020-01-24 08:15:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
fn op_signal_unbind(
|
2020-09-10 09:57:45 -04:00
|
|
|
_state: &mut OpState,
|
2021-10-05 16:38:27 -04:00
|
|
|
_: (),
|
2021-05-08 08:37:42 -04:00
|
|
|
_: (),
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2021-09-07 11:34:46 -04:00
|
|
|
Err(generic_error("not implemented"))
|
2020-01-24 08:15:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(unix))]
|
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>>,
|
2021-10-05 16:38:27 -04:00
|
|
|
_: (),
|
2021-05-08 08:37:42 -04:00
|
|
|
_: (),
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2021-09-07 11:34:46 -04:00
|
|
|
Err(generic_error("not implemented"))
|
2020-01-24 08:15:31 -05:00
|
|
|
}
|