2019-01-02 00:58:40 +00:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2018-10-01 23:37:18 +00:00
|
|
|
|
|
|
|
// Think of Resources as File Descriptors. They are integers that are allocated
|
2018-10-24 15:54:34 +00:00
|
|
|
// by the privileged side of Deno to refer to various resources. The simplest
|
2018-10-01 23:37:18 +00:00
|
|
|
// example are standard file system files and stdio - but there will be other
|
|
|
|
// resources added in the future that might not correspond to operating system
|
|
|
|
// level File Descriptors. To avoid confusion we call them "resources" not "file
|
|
|
|
// descriptors". This module implements a global resource table. Ops (AKA
|
|
|
|
// handlers) look up resources by their integer id here.
|
|
|
|
|
2019-06-20 02:07:01 +00:00
|
|
|
use crate::deno_error;
|
|
|
|
use crate::deno_error::bad_resource;
|
|
|
|
use crate::deno_error::DenoError;
|
|
|
|
use crate::deno_error::DenoResult;
|
2019-01-14 06:30:38 +00:00
|
|
|
use crate::http_body::HttpBody;
|
|
|
|
use crate::repl::Repl;
|
2019-04-09 17:11:25 +00:00
|
|
|
use crate::state::WorkerChannels;
|
2018-10-05 16:16:24 +00:00
|
|
|
|
2019-03-30 23:30:40 +00:00
|
|
|
use deno::Buf;
|
2019-03-19 20:47:35 +00:00
|
|
|
|
2018-10-01 23:37:18 +00:00
|
|
|
use futures;
|
2018-11-16 04:07:40 +00:00
|
|
|
use futures::Future;
|
2018-10-01 23:37:18 +00:00
|
|
|
use futures::Poll;
|
2019-01-08 19:44:06 +00:00
|
|
|
use futures::Sink;
|
|
|
|
use futures::Stream;
|
2018-10-26 16:14:06 +00:00
|
|
|
use hyper;
|
2018-10-01 23:37:18 +00:00
|
|
|
use std;
|
|
|
|
use std::collections::HashMap;
|
2019-02-18 23:26:41 +00:00
|
|
|
use std::io::{Error, Read, Seek, SeekFrom, Write};
|
2018-10-05 16:16:24 +00:00
|
|
|
use std::net::{Shutdown, SocketAddr};
|
2018-11-16 04:07:40 +00:00
|
|
|
use std::process::ExitStatus;
|
2018-11-05 06:21:21 +00:00
|
|
|
use std::sync::atomic::AtomicUsize;
|
2018-10-01 23:37:18 +00:00
|
|
|
use std::sync::atomic::Ordering;
|
2019-02-09 21:55:40 +00:00
|
|
|
use std::sync::{Arc, Mutex};
|
2018-10-01 23:37:18 +00:00
|
|
|
use tokio;
|
|
|
|
use tokio::io::{AsyncRead, AsyncWrite};
|
2018-10-04 03:58:29 +00:00
|
|
|
use tokio::net::TcpStream;
|
2019-04-08 21:10:00 +00:00
|
|
|
use tokio::sync::mpsc;
|
2018-11-16 04:07:40 +00:00
|
|
|
use tokio_process;
|
2018-10-01 23:37:18 +00:00
|
|
|
|
2018-11-05 06:21:21 +00:00
|
|
|
pub type ResourceId = u32; // Sometimes referred to RID.
|
2018-10-01 23:37:18 +00:00
|
|
|
|
|
|
|
// These store Deno's file descriptors. These are not necessarily the operating
|
|
|
|
// system ones.
|
|
|
|
type ResourceTable = HashMap<ResourceId, Repr>;
|
|
|
|
|
2018-12-23 04:53:29 +00:00
|
|
|
#[cfg(not(windows))]
|
|
|
|
use std::os::unix::io::FromRawFd;
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
use std::os::windows::io::FromRawHandle;
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
extern crate winapi;
|
|
|
|
|
2018-10-01 23:37:18 +00:00
|
|
|
lazy_static! {
|
|
|
|
// Starts at 3 because stdio is [0-2].
|
2018-11-05 06:21:21 +00:00
|
|
|
static ref NEXT_RID: AtomicUsize = AtomicUsize::new(3);
|
2018-10-01 23:37:18 +00:00
|
|
|
static ref RESOURCE_TABLE: Mutex<ResourceTable> = Mutex::new({
|
|
|
|
let mut m = HashMap::new();
|
|
|
|
// TODO Load these lazily during lookup?
|
|
|
|
m.insert(0, Repr::Stdin(tokio::io::stdin()));
|
2018-12-23 04:53:29 +00:00
|
|
|
|
|
|
|
m.insert(1, Repr::Stdout({
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
let stdout = unsafe { std::fs::File::from_raw_fd(1) };
|
|
|
|
#[cfg(windows)]
|
|
|
|
let stdout = unsafe {
|
2019-01-10 01:19:26 +00:00
|
|
|
std::fs::File::from_raw_handle(winapi::um::processenv::GetStdHandle(
|
2018-12-23 04:53:29 +00:00
|
|
|
winapi::um::winbase::STD_OUTPUT_HANDLE))
|
|
|
|
};
|
|
|
|
tokio::fs::File::from_std(stdout)
|
|
|
|
}));
|
|
|
|
|
2018-10-01 23:37:18 +00:00
|
|
|
m.insert(2, Repr::Stderr(tokio::io::stderr()));
|
|
|
|
m
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Internal representation of Resource.
|
|
|
|
enum Repr {
|
|
|
|
Stdin(tokio::io::Stdin),
|
2018-12-23 04:53:29 +00:00
|
|
|
Stdout(tokio::fs::File),
|
2018-10-01 23:37:18 +00:00
|
|
|
Stderr(tokio::io::Stderr),
|
|
|
|
FsFile(tokio::fs::File),
|
2019-01-16 01:36:51 +00:00
|
|
|
// Since TcpListener might be closed while there is a pending accept task,
|
|
|
|
// we need to track the task so that when the listener is closed,
|
|
|
|
// this pending task could be notified and die.
|
|
|
|
// Currently TcpListener itself does not take care of this issue.
|
|
|
|
// See: https://github.com/tokio-rs/tokio/issues/846
|
|
|
|
TcpListener(tokio::net::TcpListener, Option<futures::task::Task>),
|
2018-10-04 03:58:29 +00:00
|
|
|
TcpStream(tokio::net::TcpStream),
|
2018-10-26 16:14:06 +00:00
|
|
|
HttpBody(HttpBody),
|
2019-02-09 21:55:40 +00:00
|
|
|
Repl(Arc<Mutex<Repl>>),
|
2018-11-30 08:30:49 +00:00
|
|
|
// Enum size is bounded by the largest variant.
|
|
|
|
// Use `Box` around large `Child` struct.
|
|
|
|
// https://rust-lang.github.io/rust-clippy/master/index.html#large_enum_variant
|
|
|
|
Child(Box<tokio_process::Child>),
|
2018-11-16 04:07:40 +00:00
|
|
|
ChildStdin(tokio_process::ChildStdin),
|
|
|
|
ChildStdout(tokio_process::ChildStdout),
|
|
|
|
ChildStderr(tokio_process::ChildStderr),
|
2019-01-08 19:44:06 +00:00
|
|
|
Worker(WorkerChannels),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If the given rid is open, this returns the type of resource, E.G. "worker".
|
|
|
|
/// If the rid is closed or was never open, it returns None.
|
|
|
|
pub fn get_type(rid: ResourceId) -> Option<String> {
|
|
|
|
let table = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
table.get(&rid).map(inspect_repr)
|
2018-10-01 23:37:18 +00:00
|
|
|
}
|
|
|
|
|
2018-11-05 06:21:21 +00:00
|
|
|
pub fn table_entries() -> Vec<(u32, String)> {
|
2018-10-30 19:58:55 +00:00
|
|
|
let table = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
|
2018-11-04 14:04:24 +00:00
|
|
|
table
|
2018-10-30 19:58:55 +00:00
|
|
|
.iter()
|
2018-11-04 14:04:24 +00:00
|
|
|
.map(|(key, value)| (*key, inspect_repr(&value)))
|
|
|
|
.collect()
|
2018-10-30 19:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_table_entries() {
|
|
|
|
let mut entries = table_entries();
|
|
|
|
entries.sort();
|
|
|
|
assert_eq!(entries[0], (0, String::from("stdin")));
|
|
|
|
assert_eq!(entries[1], (1, String::from("stdout")));
|
|
|
|
assert_eq!(entries[2], (2, String::from("stderr")));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn inspect_repr(repr: &Repr) -> String {
|
|
|
|
let h_repr = match repr {
|
|
|
|
Repr::Stdin(_) => "stdin",
|
|
|
|
Repr::Stdout(_) => "stdout",
|
|
|
|
Repr::Stderr(_) => "stderr",
|
|
|
|
Repr::FsFile(_) => "fsFile",
|
2019-01-16 01:36:51 +00:00
|
|
|
Repr::TcpListener(_, _) => "tcpListener",
|
2018-10-30 19:58:55 +00:00
|
|
|
Repr::TcpStream(_) => "tcpStream",
|
2018-10-26 16:14:06 +00:00
|
|
|
Repr::HttpBody(_) => "httpBody",
|
2018-11-05 17:55:59 +00:00
|
|
|
Repr::Repl(_) => "repl",
|
2018-11-16 04:07:40 +00:00
|
|
|
Repr::Child(_) => "child",
|
|
|
|
Repr::ChildStdin(_) => "childStdin",
|
|
|
|
Repr::ChildStdout(_) => "childStdout",
|
|
|
|
Repr::ChildStderr(_) => "childStderr",
|
2019-01-08 19:44:06 +00:00
|
|
|
Repr::Worker(_) => "worker",
|
2018-10-30 19:58:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
String::from(h_repr)
|
|
|
|
}
|
|
|
|
|
2018-10-01 23:37:18 +00:00
|
|
|
// Abstract async file interface.
|
|
|
|
// Ideally in unix, if Resource represents an OS rid, it will be the same.
|
2019-01-08 19:44:06 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2018-10-01 23:37:18 +00:00
|
|
|
pub struct Resource {
|
|
|
|
pub rid: ResourceId,
|
|
|
|
}
|
|
|
|
|
2018-10-04 03:58:29 +00:00
|
|
|
impl Resource {
|
|
|
|
// TODO Should it return a Resource instead of net::TcpStream?
|
|
|
|
pub fn poll_accept(&mut self) -> Poll<(TcpStream, SocketAddr), Error> {
|
|
|
|
let mut table = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
let maybe_repr = table.get_mut(&self.rid);
|
|
|
|
match maybe_repr {
|
2019-01-16 01:36:51 +00:00
|
|
|
None => Err(std::io::Error::new(
|
|
|
|
std::io::ErrorKind::Other,
|
|
|
|
"Listener has been closed",
|
|
|
|
)),
|
2018-10-04 03:58:29 +00:00
|
|
|
Some(repr) => match repr {
|
2019-01-16 01:36:51 +00:00
|
|
|
Repr::TcpListener(ref mut s, _) => s.poll_accept(),
|
2018-10-04 03:58:29 +00:00
|
|
|
_ => panic!("Cannot accept"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-23 18:22:52 +00:00
|
|
|
/// Track the current task (for TcpListener resource).
|
|
|
|
/// Throws an error if another task is already tracked.
|
|
|
|
pub fn track_task(&mut self) -> Result<(), std::io::Error> {
|
|
|
|
let mut table = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
// Only track if is TcpListener.
|
|
|
|
if let Some(Repr::TcpListener(_, t)) = table.get_mut(&self.rid) {
|
|
|
|
// Currently, we only allow tracking a single accept task for a listener.
|
|
|
|
// This might be changed in the future with multiple workers.
|
|
|
|
// Caveat: TcpListener by itself also only tracks an accept task at a time.
|
|
|
|
// See https://github.com/tokio-rs/tokio/issues/846#issuecomment-454208883
|
|
|
|
if t.is_some() {
|
|
|
|
return Err(std::io::Error::new(
|
|
|
|
std::io::ErrorKind::Other,
|
|
|
|
"Another accept task is ongoing",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
t.replace(futures::task::current());
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Stop tracking a task (for TcpListener resource).
|
|
|
|
/// Happens when the task is done and thus no further tracking is needed.
|
|
|
|
pub fn untrack_task(&mut self) {
|
|
|
|
let mut table = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
// Only untrack if is TcpListener.
|
|
|
|
if let Some(Repr::TcpListener(_, t)) = table.get_mut(&self.rid) {
|
|
|
|
if t.is_some() {
|
|
|
|
t.take();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-04 03:58:29 +00:00
|
|
|
// close(2) is done by dropping the value. Therefore we just need to remove
|
|
|
|
// the resource from the RESOURCE_TABLE.
|
2019-01-09 17:59:46 +00:00
|
|
|
pub fn close(&self) {
|
2018-10-04 03:58:29 +00:00
|
|
|
let mut table = RESOURCE_TABLE.lock().unwrap();
|
2019-05-23 18:22:52 +00:00
|
|
|
let r = table.remove(&self.rid).unwrap();
|
|
|
|
// If TcpListener, we must kill all pending accepts!
|
|
|
|
if let Repr::TcpListener(_, Some(t)) = r {
|
|
|
|
// Call notify on the tracked task, so that they would error out.
|
|
|
|
t.notify();
|
|
|
|
}
|
2018-10-04 03:58:29 +00:00
|
|
|
}
|
2018-10-05 16:16:24 +00:00
|
|
|
|
|
|
|
pub fn shutdown(&mut self, how: Shutdown) -> Result<(), DenoError> {
|
|
|
|
let mut table = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
let maybe_repr = table.get_mut(&self.rid);
|
|
|
|
match maybe_repr {
|
|
|
|
None => panic!("bad rid"),
|
|
|
|
Some(repr) => match repr {
|
|
|
|
Repr::TcpStream(ref mut f) => {
|
2018-11-04 14:04:24 +00:00
|
|
|
TcpStream::shutdown(f, how).map_err(DenoError::from)
|
2018-10-05 16:16:24 +00:00
|
|
|
}
|
|
|
|
_ => panic!("Cannot shutdown"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2018-10-04 03:58:29 +00:00
|
|
|
}
|
|
|
|
|
2018-10-01 23:37:18 +00:00
|
|
|
impl Read for Resource {
|
|
|
|
fn read(&mut self, _buf: &mut [u8]) -> std::io::Result<usize> {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsyncRead for Resource {
|
|
|
|
fn poll_read(&mut self, buf: &mut [u8]) -> Poll<usize, Error> {
|
|
|
|
let mut table = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
let maybe_repr = table.get_mut(&self.rid);
|
|
|
|
match maybe_repr {
|
|
|
|
None => panic!("bad rid"),
|
|
|
|
Some(repr) => match repr {
|
|
|
|
Repr::FsFile(ref mut f) => f.poll_read(buf),
|
|
|
|
Repr::Stdin(ref mut f) => f.poll_read(buf),
|
2018-10-04 03:58:29 +00:00
|
|
|
Repr::TcpStream(ref mut f) => f.poll_read(buf),
|
2018-10-26 16:14:06 +00:00
|
|
|
Repr::HttpBody(ref mut f) => f.poll_read(buf),
|
2018-11-16 04:07:40 +00:00
|
|
|
Repr::ChildStdout(ref mut f) => f.poll_read(buf),
|
|
|
|
Repr::ChildStderr(ref mut f) => f.poll_read(buf),
|
2018-11-05 17:55:59 +00:00
|
|
|
_ => panic!("Cannot read"),
|
2018-10-01 23:37:18 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Write for Resource {
|
|
|
|
fn write(&mut self, _buf: &[u8]) -> std::io::Result<usize> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> std::io::Result<()> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsyncWrite for Resource {
|
|
|
|
fn poll_write(&mut self, buf: &[u8]) -> Poll<usize, Error> {
|
|
|
|
let mut table = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
let maybe_repr = table.get_mut(&self.rid);
|
|
|
|
match maybe_repr {
|
|
|
|
None => panic!("bad rid"),
|
|
|
|
Some(repr) => match repr {
|
|
|
|
Repr::FsFile(ref mut f) => f.poll_write(buf),
|
|
|
|
Repr::Stdout(ref mut f) => f.poll_write(buf),
|
|
|
|
Repr::Stderr(ref mut f) => f.poll_write(buf),
|
2018-10-04 03:58:29 +00:00
|
|
|
Repr::TcpStream(ref mut f) => f.poll_write(buf),
|
2018-11-16 04:07:40 +00:00
|
|
|
Repr::ChildStdin(ref mut f) => f.poll_write(buf),
|
2018-11-05 17:55:59 +00:00
|
|
|
_ => panic!("Cannot write"),
|
2018-10-01 23:37:18 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn shutdown(&mut self) -> futures::Poll<(), std::io::Error> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new_rid() -> ResourceId {
|
|
|
|
let next_rid = NEXT_RID.fetch_add(1, Ordering::SeqCst);
|
|
|
|
next_rid as ResourceId
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_fs_file(fs_file: tokio::fs::File) -> Resource {
|
|
|
|
let rid = new_rid();
|
|
|
|
let mut tg = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
match tg.insert(rid, Repr::FsFile(fs_file)) {
|
|
|
|
Some(_) => panic!("There is already a file with that rid"),
|
|
|
|
None => Resource { rid },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-04 03:58:29 +00:00
|
|
|
pub fn add_tcp_listener(listener: tokio::net::TcpListener) -> Resource {
|
|
|
|
let rid = new_rid();
|
|
|
|
let mut tg = RESOURCE_TABLE.lock().unwrap();
|
2019-01-16 01:36:51 +00:00
|
|
|
let r = tg.insert(rid, Repr::TcpListener(listener, None));
|
2018-10-04 03:58:29 +00:00
|
|
|
assert!(r.is_none());
|
|
|
|
Resource { rid }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_tcp_stream(stream: tokio::net::TcpStream) -> Resource {
|
|
|
|
let rid = new_rid();
|
|
|
|
let mut tg = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
let r = tg.insert(rid, Repr::TcpStream(stream));
|
|
|
|
assert!(r.is_none());
|
|
|
|
Resource { rid }
|
|
|
|
}
|
|
|
|
|
2018-10-26 16:14:06 +00:00
|
|
|
pub fn add_hyper_body(body: hyper::Body) -> Resource {
|
|
|
|
let rid = new_rid();
|
|
|
|
let mut tg = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
let body = HttpBody::from(body);
|
|
|
|
let r = tg.insert(rid, Repr::HttpBody(body));
|
|
|
|
assert!(r.is_none());
|
|
|
|
Resource { rid }
|
|
|
|
}
|
|
|
|
|
2018-11-05 17:55:59 +00:00
|
|
|
pub fn add_repl(repl: Repl) -> Resource {
|
|
|
|
let rid = new_rid();
|
|
|
|
let mut tg = RESOURCE_TABLE.lock().unwrap();
|
2019-02-09 21:55:40 +00:00
|
|
|
let r = tg.insert(rid, Repr::Repl(Arc::new(Mutex::new(repl))));
|
2018-11-05 17:55:59 +00:00
|
|
|
assert!(r.is_none());
|
|
|
|
Resource { rid }
|
|
|
|
}
|
|
|
|
|
2019-01-08 19:44:06 +00:00
|
|
|
pub fn add_worker(wc: WorkerChannels) -> Resource {
|
|
|
|
let rid = new_rid();
|
|
|
|
let mut tg = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
let r = tg.insert(rid, Repr::Worker(wc));
|
|
|
|
assert!(r.is_none());
|
|
|
|
Resource { rid }
|
|
|
|
}
|
|
|
|
|
2019-04-01 19:09:59 +00:00
|
|
|
/// Post message to worker as a host or privilged overlord
|
|
|
|
pub fn post_message_to_worker(
|
2019-01-08 19:44:06 +00:00
|
|
|
rid: ResourceId,
|
|
|
|
buf: Buf,
|
2019-04-08 21:10:00 +00:00
|
|
|
) -> futures::sink::Send<mpsc::Sender<Buf>> {
|
2019-01-08 19:44:06 +00:00
|
|
|
let mut table = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
let maybe_repr = table.get_mut(&rid);
|
|
|
|
match maybe_repr {
|
|
|
|
Some(Repr::Worker(ref mut wc)) => {
|
|
|
|
// unwrap here is incorrect, but doing it anyway
|
|
|
|
wc.0.clone().send(buf)
|
|
|
|
}
|
|
|
|
_ => panic!("bad resource"), // futures::future::err(bad_resource()).into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct WorkerReceiver {
|
|
|
|
rid: ResourceId,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invert the dumbness that tokio_process causes by making Child itself a future.
|
|
|
|
impl Future for WorkerReceiver {
|
|
|
|
type Item = Option<Buf>;
|
|
|
|
type Error = DenoError;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Option<Buf>, DenoError> {
|
|
|
|
let mut table = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
let maybe_repr = table.get_mut(&self.rid);
|
|
|
|
match maybe_repr {
|
2019-06-20 02:07:01 +00:00
|
|
|
Some(Repr::Worker(ref mut wc)) => wc.1.poll().map_err(|err| {
|
|
|
|
deno_error::new(deno_error::ErrorKind::Other, err.to_string())
|
|
|
|
}),
|
2019-01-08 19:44:06 +00:00
|
|
|
_ => Err(bad_resource()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-01 19:09:59 +00:00
|
|
|
pub fn get_message_from_worker(rid: ResourceId) -> WorkerReceiver {
|
2019-01-08 19:44:06 +00:00
|
|
|
WorkerReceiver { rid }
|
|
|
|
}
|
|
|
|
|
2019-04-04 09:33:32 +00:00
|
|
|
pub struct WorkerReceiverStream {
|
|
|
|
rid: ResourceId,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invert the dumbness that tokio_process causes by making Child itself a future.
|
|
|
|
impl Stream for WorkerReceiverStream {
|
|
|
|
type Item = Buf;
|
|
|
|
type Error = DenoError;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Option<Buf>, DenoError> {
|
|
|
|
let mut table = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
let maybe_repr = table.get_mut(&self.rid);
|
|
|
|
match maybe_repr {
|
2019-06-20 02:07:01 +00:00
|
|
|
Some(Repr::Worker(ref mut wc)) => wc.1.poll().map_err(|err| {
|
|
|
|
deno_error::new(deno_error::ErrorKind::Other, err.to_string())
|
|
|
|
}),
|
2019-04-04 09:33:32 +00:00
|
|
|
_ => Err(bad_resource()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_message_stream_from_worker(rid: ResourceId) -> WorkerReceiverStream {
|
|
|
|
WorkerReceiverStream { rid }
|
|
|
|
}
|
|
|
|
|
2018-11-16 04:07:40 +00:00
|
|
|
pub struct ChildResources {
|
|
|
|
pub child_rid: ResourceId,
|
|
|
|
pub stdin_rid: Option<ResourceId>,
|
|
|
|
pub stdout_rid: Option<ResourceId>,
|
|
|
|
pub stderr_rid: Option<ResourceId>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_child(mut c: tokio_process::Child) -> ChildResources {
|
|
|
|
let child_rid = new_rid();
|
|
|
|
let mut tg = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
|
|
|
|
let mut resources = ChildResources {
|
|
|
|
child_rid,
|
|
|
|
stdin_rid: None,
|
|
|
|
stdout_rid: None,
|
|
|
|
stderr_rid: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
if c.stdin().is_some() {
|
|
|
|
let stdin = c.stdin().take().unwrap();
|
|
|
|
let rid = new_rid();
|
|
|
|
let r = tg.insert(rid, Repr::ChildStdin(stdin));
|
|
|
|
assert!(r.is_none());
|
|
|
|
resources.stdin_rid = Some(rid);
|
|
|
|
}
|
|
|
|
if c.stdout().is_some() {
|
|
|
|
let stdout = c.stdout().take().unwrap();
|
|
|
|
let rid = new_rid();
|
|
|
|
let r = tg.insert(rid, Repr::ChildStdout(stdout));
|
|
|
|
assert!(r.is_none());
|
|
|
|
resources.stdout_rid = Some(rid);
|
|
|
|
}
|
|
|
|
if c.stderr().is_some() {
|
|
|
|
let stderr = c.stderr().take().unwrap();
|
|
|
|
let rid = new_rid();
|
|
|
|
let r = tg.insert(rid, Repr::ChildStderr(stderr));
|
|
|
|
assert!(r.is_none());
|
|
|
|
resources.stderr_rid = Some(rid);
|
|
|
|
}
|
|
|
|
|
2018-11-30 08:30:49 +00:00
|
|
|
let r = tg.insert(child_rid, Repr::Child(Box::new(c)));
|
2018-11-16 04:07:40 +00:00
|
|
|
assert!(r.is_none());
|
|
|
|
|
2018-11-30 08:30:49 +00:00
|
|
|
resources
|
2018-11-16 04:07:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ChildStatus {
|
|
|
|
rid: ResourceId,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invert the dumbness that tokio_process causes by making Child itself a future.
|
|
|
|
impl Future for ChildStatus {
|
|
|
|
type Item = ExitStatus;
|
|
|
|
type Error = DenoError;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<ExitStatus, DenoError> {
|
|
|
|
let mut table = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
let maybe_repr = table.get_mut(&self.rid);
|
|
|
|
match maybe_repr {
|
|
|
|
Some(Repr::Child(ref mut child)) => child.poll().map_err(DenoError::from),
|
|
|
|
_ => Err(bad_resource()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn child_status(rid: ResourceId) -> DenoResult<ChildStatus> {
|
|
|
|
let mut table = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
let maybe_repr = table.get_mut(&rid);
|
|
|
|
match maybe_repr {
|
|
|
|
Some(Repr::Child(ref mut _child)) => Ok(ChildStatus { rid }),
|
|
|
|
_ => Err(bad_resource()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-09 21:55:40 +00:00
|
|
|
pub fn get_repl(rid: ResourceId) -> DenoResult<Arc<Mutex<Repl>>> {
|
2018-11-05 17:55:59 +00:00
|
|
|
let mut table = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
let maybe_repr = table.get_mut(&rid);
|
|
|
|
match maybe_repr {
|
2019-02-09 21:55:40 +00:00
|
|
|
Some(Repr::Repl(ref mut r)) => Ok(r.clone()),
|
2018-11-05 17:55:59 +00:00
|
|
|
_ => Err(bad_resource()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-01 23:37:18 +00:00
|
|
|
pub fn lookup(rid: ResourceId) -> Option<Resource> {
|
2018-10-26 16:14:06 +00:00
|
|
|
debug!("resource lookup {}", rid);
|
2018-10-01 23:37:18 +00:00
|
|
|
let table = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
table.get(&rid).map(|_| Resource { rid })
|
|
|
|
}
|
2018-10-19 15:40:54 +00:00
|
|
|
|
2019-02-18 23:26:41 +00:00
|
|
|
// TODO(kevinkassimo): revamp this after the following lands:
|
|
|
|
// https://github.com/tokio-rs/tokio/pull/785
|
|
|
|
pub fn seek(
|
|
|
|
resource: Resource,
|
|
|
|
offset: i32,
|
|
|
|
whence: u32,
|
|
|
|
) -> Box<dyn Future<Item = (), Error = DenoError> + Send> {
|
|
|
|
let mut table = RESOURCE_TABLE.lock().unwrap();
|
|
|
|
// We take ownership of File here.
|
|
|
|
// It is put back below while still holding the lock.
|
|
|
|
let maybe_repr = table.remove(&resource.rid);
|
|
|
|
match maybe_repr {
|
|
|
|
None => panic!("bad rid"),
|
|
|
|
Some(Repr::FsFile(f)) => {
|
|
|
|
// Trait Clone not implemented on tokio::fs::File,
|
|
|
|
// so convert to std File first.
|
|
|
|
let std_file = f.into_std();
|
|
|
|
// Create a copy and immediately put back.
|
|
|
|
// We don't want to block other resource ops.
|
|
|
|
// try_clone() would yield a copy containing the same
|
|
|
|
// underlying fd, so operations on the copy would also
|
|
|
|
// affect the one in resource table, and we don't need
|
|
|
|
// to write back.
|
|
|
|
let maybe_std_file_copy = std_file.try_clone();
|
|
|
|
// Insert the entry back with the same rid.
|
|
|
|
table.insert(
|
|
|
|
resource.rid,
|
|
|
|
Repr::FsFile(tokio_fs::File::from_std(std_file)),
|
|
|
|
);
|
2019-03-27 02:47:17 +00:00
|
|
|
// Translate seek mode to Rust repr.
|
|
|
|
let seek_from = match whence {
|
|
|
|
0 => SeekFrom::Start(offset as u64),
|
|
|
|
1 => SeekFrom::Current(i64::from(offset)),
|
|
|
|
2 => SeekFrom::End(i64::from(offset)),
|
|
|
|
_ => {
|
2019-06-20 02:07:01 +00:00
|
|
|
return Box::new(futures::future::err(deno_error::new(
|
|
|
|
deno_error::ErrorKind::InvalidSeekMode,
|
2019-03-27 02:47:17 +00:00
|
|
|
format!("Invalid seek mode: {}", whence),
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
};
|
2019-02-18 23:26:41 +00:00
|
|
|
if maybe_std_file_copy.is_err() {
|
|
|
|
return Box::new(futures::future::err(DenoError::from(
|
|
|
|
maybe_std_file_copy.unwrap_err(),
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
let mut std_file_copy = maybe_std_file_copy.unwrap();
|
2019-03-20 22:55:52 +00:00
|
|
|
Box::new(futures::future::lazy(move || {
|
2019-02-18 23:26:41 +00:00
|
|
|
let result = std_file_copy
|
|
|
|
.seek(seek_from)
|
2019-03-20 22:55:52 +00:00
|
|
|
.map(|_| {})
|
|
|
|
.map_err(DenoError::from);
|
2019-02-18 23:26:41 +00:00
|
|
|
futures::future::result(result)
|
2019-03-20 22:55:52 +00:00
|
|
|
}))
|
2019-02-18 23:26:41 +00:00
|
|
|
}
|
|
|
|
_ => panic!("cannot seek"),
|
|
|
|
}
|
|
|
|
}
|