2020-01-24 08:15:31 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::serde_json::Value;
|
2020-08-28 11:08:24 -04:00
|
|
|
use deno_core::BufVec;
|
2020-09-10 09:57:45 -04:00
|
|
|
use deno_core::OpState;
|
2020-04-23 05:51:07 -04:00
|
|
|
use deno_core::ZeroCopyBuf;
|
2020-09-10 09:57:45 -04:00
|
|
|
use std::cell::RefCell;
|
2020-08-18 12:30:13 -04:00
|
|
|
use std::rc::Rc;
|
2020-01-24 08:15:31 -05:00
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
#[cfg(unix)]
|
|
|
|
use deno_core::error::bad_resource_id;
|
2020-01-24 08:15:31 -05:00
|
|
|
#[cfg(unix)]
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::futures::future::poll_fn;
|
|
|
|
#[cfg(unix)]
|
|
|
|
use deno_core::serde_json;
|
|
|
|
#[cfg(unix)]
|
|
|
|
use deno_core::serde_json::json;
|
2020-01-24 08:15:31 -05:00
|
|
|
#[cfg(unix)]
|
2020-09-16 12:43:08 -04:00
|
|
|
use serde::Deserialize;
|
2020-09-05 20:34:02 -04:00
|
|
|
#[cfg(unix)]
|
2020-01-24 08:15:31 -05:00
|
|
|
use std::task::Waker;
|
|
|
|
#[cfg(unix)]
|
|
|
|
use tokio::signal::unix::{signal, Signal, SignalKind};
|
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
pub fn init(rt: &mut deno_core::JsRuntime) {
|
|
|
|
super::reg_json_sync(rt, "op_signal_bind", op_signal_bind);
|
|
|
|
super::reg_json_sync(rt, "op_signal_unbind", op_signal_unbind);
|
|
|
|
super::reg_json_async(rt, "op_signal_poll", op_signal_poll);
|
2020-01-24 08:15:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
/// The resource for signal stream.
|
|
|
|
/// The second element is the waker of polling future.
|
|
|
|
pub struct SignalStreamResource(pub Signal, pub Option<Waker>);
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct BindSignalArgs {
|
|
|
|
signo: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct SignalArgs {
|
|
|
|
rid: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
fn op_signal_bind(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2020-01-24 08:15:31 -05:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-09-26 14:26:51 -04:00
|
|
|
super::check_unstable(state, "Deno.signal");
|
2020-01-24 08:15:31 -05:00
|
|
|
let args: BindSignalArgs = serde_json::from_value(args)?;
|
2020-09-10 09:57:45 -04:00
|
|
|
let rid = state.resource_table.add(
|
2020-01-24 08:15:31 -05:00
|
|
|
"signal",
|
|
|
|
Box::new(SignalStreamResource(
|
|
|
|
signal(SignalKind::from_raw(args.signo)).expect(""),
|
|
|
|
None,
|
|
|
|
)),
|
|
|
|
);
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!({
|
2020-01-24 08:15:31 -05:00
|
|
|
"rid": rid,
|
2020-08-28 11:08:24 -04:00
|
|
|
}))
|
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>>,
|
2020-01-24 08:15:31 -05:00
|
|
|
args: Value,
|
2020-08-28 11:08:24 -04:00
|
|
|
_zero_copy: BufVec,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-09-26 14:26:51 -04:00
|
|
|
super::check_unstable2(&state, "Deno.signal");
|
2020-01-24 08:15:31 -05:00
|
|
|
let args: SignalArgs = serde_json::from_value(args)?;
|
|
|
|
let rid = args.rid as u32;
|
|
|
|
|
|
|
|
let future = poll_fn(move |cx| {
|
2020-09-10 09:57:45 -04:00
|
|
|
let mut state = state.borrow_mut();
|
2020-02-08 14:34:31 -05:00
|
|
|
if let Some(mut signal) =
|
2020-09-10 09:57:45 -04:00
|
|
|
state.resource_table.get_mut::<SignalStreamResource>(rid)
|
2020-02-08 14:34:31 -05:00
|
|
|
{
|
2020-01-24 08:15:31 -05:00
|
|
|
signal.1 = Some(cx.waker().clone());
|
|
|
|
return signal.0.poll_recv(cx);
|
|
|
|
}
|
|
|
|
std::task::Poll::Ready(None)
|
2020-08-28 11:08:24 -04:00
|
|
|
});
|
|
|
|
let result = future.await;
|
|
|
|
Ok(json!({ "done": result.is_none() }))
|
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,
|
2020-01-24 08:15:31 -05:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-09-26 14:26:51 -04:00
|
|
|
super::check_unstable(state, "Deno.signal");
|
2020-01-24 08:15:31 -05:00
|
|
|
let args: SignalArgs = serde_json::from_value(args)?;
|
|
|
|
let rid = args.rid as u32;
|
2020-09-10 09:57:45 -04:00
|
|
|
let resource = state.resource_table.get_mut::<SignalStreamResource>(rid);
|
2020-01-24 08:15:31 -05:00
|
|
|
if let Some(signal) = resource {
|
|
|
|
if let Some(waker) = &signal.1 {
|
|
|
|
// Wakes up the pending poll if exists.
|
|
|
|
// This prevents the poll future from getting stuck forever.
|
|
|
|
waker.clone().wake();
|
|
|
|
}
|
|
|
|
}
|
2020-09-10 09:57:45 -04:00
|
|
|
state
|
|
|
|
.resource_table
|
2020-02-23 14:51:29 -05:00
|
|
|
.close(rid)
|
2020-09-14 12:48:57 -04:00
|
|
|
.ok_or_else(bad_resource_id)?;
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!({}))
|
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,
|
2020-01-24 08:15:31 -05:00
|
|
|
_args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-01-24 08:15:31 -05:00
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
fn op_signal_unbind(
|
2020-09-10 09:57:45 -04:00
|
|
|
_state: &mut OpState,
|
2020-01-24 08:15:31 -05:00
|
|
|
_args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-01-24 08:15:31 -05:00
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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>>,
|
2020-01-24 08:15:31 -05:00
|
|
|
_args: Value,
|
2020-08-28 11:08:24 -04:00
|
|
|
_zero_copy: BufVec,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-01-24 08:15:31 -05:00
|
|
|
unimplemented!();
|
|
|
|
}
|