2019-08-14 11:03:02 -04:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-08-26 08:50:21 -04:00
|
|
|
use super::dispatch_json::{Deserialize, JsonOp, Value};
|
2019-11-07 18:52:21 -05:00
|
|
|
use crate::deno_error::bad_resource;
|
2019-10-11 14:41:54 -04:00
|
|
|
use crate::ops::json_op;
|
2019-08-14 11:03:02 -04:00
|
|
|
use crate::resources;
|
2019-11-07 18:52:21 -05:00
|
|
|
use crate::resources::CloneFileFuture;
|
2019-08-14 11:03:02 -04:00
|
|
|
use crate::signal::kill;
|
|
|
|
use crate::state::ThreadSafeState;
|
|
|
|
use deno::*;
|
|
|
|
use futures;
|
|
|
|
use futures::Future;
|
2019-11-07 18:52:21 -05:00
|
|
|
use futures::Poll;
|
2019-08-14 11:03:02 -04:00
|
|
|
use std;
|
|
|
|
use std::convert::From;
|
|
|
|
use std::process::Command;
|
2019-11-07 18:52:21 -05:00
|
|
|
use std::process::ExitStatus;
|
2019-08-14 11:03:02 -04:00
|
|
|
use tokio_process::CommandExt;
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
use std::os::unix::process::ExitStatusExt;
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
pub fn init(i: &mut Isolate, s: &ThreadSafeState) {
|
|
|
|
i.register_op("run", s.core_op(json_op(s.stateful_op(op_run))));
|
|
|
|
i.register_op(
|
|
|
|
"run_status",
|
|
|
|
s.core_op(json_op(s.stateful_op(op_run_status))),
|
|
|
|
);
|
|
|
|
i.register_op("kill", s.core_op(json_op(s.stateful_op(op_kill))));
|
|
|
|
}
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
fn subprocess_stdio_map(s: &str) -> std::process::Stdio {
|
|
|
|
match s {
|
|
|
|
"inherit" => std::process::Stdio::inherit(),
|
|
|
|
"piped" => std::process::Stdio::piped(),
|
|
|
|
"null" => std::process::Stdio::null(),
|
|
|
|
_ => unreachable!(),
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct RunArgs {
|
|
|
|
args: Vec<String>,
|
|
|
|
cwd: Option<String>,
|
|
|
|
env: Vec<(String, String)>,
|
|
|
|
stdin: String,
|
|
|
|
stdout: String,
|
|
|
|
stderr: String,
|
|
|
|
stdin_rid: u32,
|
|
|
|
stdout_rid: u32,
|
|
|
|
stderr_rid: u32,
|
|
|
|
}
|
|
|
|
|
2019-11-07 18:52:21 -05:00
|
|
|
struct ChildResource {
|
|
|
|
child: tokio_process::Child,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Resource for ChildResource {}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_run(
|
2019-08-14 11:03:02 -04:00
|
|
|
state: &ThreadSafeState,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
|
|
|
_zero_copy: Option<PinnedBuf>,
|
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let run_args: RunArgs = serde_json::from_value(args)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
|
|
|
state.check_run()?;
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
let args = run_args.args;
|
|
|
|
let env = run_args.env;
|
|
|
|
let cwd = run_args.cwd;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
let mut c = Command::new(args.get(0).unwrap());
|
2019-08-14 11:03:02 -04:00
|
|
|
(1..args.len()).for_each(|i| {
|
2019-08-26 08:50:21 -04:00
|
|
|
let arg = args.get(i).unwrap();
|
2019-08-14 11:03:02 -04:00
|
|
|
c.arg(arg);
|
|
|
|
});
|
|
|
|
cwd.map(|d| c.current_dir(d));
|
2019-08-26 08:50:21 -04:00
|
|
|
for (key, value) in &env {
|
|
|
|
c.env(key, value);
|
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
|
|
|
|
// TODO: make this work with other resources, eg. sockets
|
2019-08-26 08:50:21 -04:00
|
|
|
let stdin_rid = run_args.stdin_rid;
|
2019-08-14 11:03:02 -04:00
|
|
|
if stdin_rid > 0 {
|
2019-11-07 18:52:21 -05:00
|
|
|
let file = (CloneFileFuture { rid: stdin_rid }).wait()?.into_std();
|
|
|
|
c.stdin(file);
|
2019-08-14 11:03:02 -04:00
|
|
|
} else {
|
2019-08-26 08:50:21 -04:00
|
|
|
c.stdin(subprocess_stdio_map(run_args.stdin.as_ref()));
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
let stdout_rid = run_args.stdout_rid;
|
2019-08-14 11:03:02 -04:00
|
|
|
if stdout_rid > 0 {
|
2019-11-07 18:52:21 -05:00
|
|
|
let file = (CloneFileFuture { rid: stdout_rid }).wait()?.into_std();
|
|
|
|
c.stdout(file);
|
2019-08-14 11:03:02 -04:00
|
|
|
} else {
|
2019-08-26 08:50:21 -04:00
|
|
|
c.stdout(subprocess_stdio_map(run_args.stdout.as_ref()));
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
let stderr_rid = run_args.stderr_rid;
|
2019-08-14 11:03:02 -04:00
|
|
|
if stderr_rid > 0 {
|
2019-11-07 18:52:21 -05:00
|
|
|
let file = (CloneFileFuture { rid: stderr_rid }).wait()?.into_std();
|
|
|
|
c.stderr(file);
|
2019-08-14 11:03:02 -04:00
|
|
|
} else {
|
2019-08-26 08:50:21 -04:00
|
|
|
c.stderr(subprocess_stdio_map(run_args.stderr.as_ref()));
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Spawn the command.
|
2019-11-07 18:52:21 -05:00
|
|
|
let mut child = c.spawn_async().map_err(ErrBox::from)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
let pid = child.id();
|
2019-11-07 18:52:21 -05:00
|
|
|
|
|
|
|
let stdin_rid = if child.stdin().is_some() {
|
|
|
|
let rid = resources::add_child_stdin(child.stdin().take().unwrap());
|
|
|
|
Some(rid)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let stdout_rid = if child.stdout().is_some() {
|
|
|
|
let rid = resources::add_child_stdout(child.stdout().take().unwrap());
|
|
|
|
Some(rid)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let stderr_rid = if child.stderr().is_some() {
|
|
|
|
let rid = resources::add_child_stderr(child.stderr().take().unwrap());
|
|
|
|
Some(rid)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let child_resource = ChildResource { child };
|
|
|
|
let mut table = resources::lock_resource_table();
|
|
|
|
let child_rid = table.add("child", Box::new(child_resource));
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
Ok(JsonOp::Sync(json!({
|
2019-11-07 18:52:21 -05:00
|
|
|
"rid": child_rid,
|
2019-08-26 08:50:21 -04:00
|
|
|
"pid": pid,
|
2019-11-07 18:52:21 -05:00
|
|
|
"stdinRid": stdin_rid,
|
|
|
|
"stdoutRid": stdout_rid,
|
|
|
|
"stderrRid": stderr_rid,
|
2019-08-26 08:50:21 -04:00
|
|
|
})))
|
|
|
|
}
|
2019-08-24 16:20:48 -04:00
|
|
|
|
2019-11-07 18:52:21 -05:00
|
|
|
pub struct ChildStatus {
|
|
|
|
rid: ResourceId,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Future for ChildStatus {
|
|
|
|
type Item = ExitStatus;
|
|
|
|
type Error = ErrBox;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<ExitStatus, ErrBox> {
|
|
|
|
let mut table = resources::lock_resource_table();
|
|
|
|
let child_resource = table
|
|
|
|
.get_mut::<ChildResource>(self.rid)
|
|
|
|
.ok_or_else(bad_resource)?;
|
|
|
|
let child = &mut child_resource.child;
|
|
|
|
child.poll().map_err(ErrBox::from)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct RunStatusArgs {
|
|
|
|
rid: i32,
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_run_status(
|
2019-08-14 11:03:02 -04:00
|
|
|
state: &ThreadSafeState,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
|
|
|
_zero_copy: Option<PinnedBuf>,
|
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: RunStatusArgs = serde_json::from_value(args)?;
|
|
|
|
let rid = args.rid as u32;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
|
|
|
state.check_run()?;
|
|
|
|
|
2019-11-07 18:52:21 -05:00
|
|
|
let future = ChildStatus { rid };
|
2019-08-14 11:03:02 -04:00
|
|
|
|
|
|
|
let future = future.and_then(move |run_status| {
|
|
|
|
let code = run_status.code();
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
let signal = run_status.signal();
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
let signal = None;
|
|
|
|
|
|
|
|
code
|
|
|
|
.or(signal)
|
|
|
|
.expect("Should have either an exit code or a signal.");
|
|
|
|
let got_signal = signal.is_some();
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
futures::future::ok(json!({
|
2019-11-07 18:52:21 -05:00
|
|
|
"gotSignal": got_signal,
|
2019-08-26 08:50:21 -04:00
|
|
|
"exitCode": code.unwrap_or(-1),
|
|
|
|
"exitSignal": signal.unwrap_or(-1),
|
|
|
|
}))
|
2019-08-14 11:03:02 -04:00
|
|
|
});
|
2019-08-26 08:50:21 -04:00
|
|
|
|
|
|
|
Ok(JsonOp::Async(Box::new(future)))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct KillArgs {
|
|
|
|
pid: i32,
|
|
|
|
signo: i32,
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_kill(
|
2019-08-14 11:03:02 -04:00
|
|
|
state: &ThreadSafeState,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
|
|
|
_zero_copy: Option<PinnedBuf>,
|
|
|
|
) -> Result<JsonOp, ErrBox> {
|
2019-08-14 11:03:02 -04:00
|
|
|
state.check_run()?;
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
let args: KillArgs = serde_json::from_value(args)?;
|
|
|
|
kill(args.pid, args.signo)?;
|
|
|
|
Ok(JsonOp::Sync(json!({})))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|