2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2021-01-14 23:32:27 -05:00
|
|
|
use super::io::ChildStderrResource;
|
|
|
|
use super::io::ChildStdinResource;
|
|
|
|
use super::io::ChildStdoutResource;
|
|
|
|
use super::io::StdFileResource;
|
2020-09-19 19:17:35 -04:00
|
|
|
use crate::permissions::Permissions;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2022-03-14 13:44:15 -04:00
|
|
|
use deno_core::op;
|
|
|
|
|
2022-04-26 09:26:05 -04:00
|
|
|
use deno_core::serde_json;
|
2020-12-16 11:14:12 -05:00
|
|
|
use deno_core::AsyncMutFuture;
|
|
|
|
use deno_core::AsyncRefCell;
|
2021-05-02 19:22:57 -04:00
|
|
|
use deno_core::Extension;
|
2020-09-10 09:57:45 -04:00
|
|
|
use deno_core::OpState;
|
2020-12-16 11:14:12 -05:00
|
|
|
use deno_core::RcRef;
|
|
|
|
use deno_core::Resource;
|
2021-03-19 13:25:37 -04:00
|
|
|
use deno_core::ResourceId;
|
2020-09-16 12:43:08 -04:00
|
|
|
use serde::Deserialize;
|
2021-04-05 12:40:24 -04:00
|
|
|
use serde::Serialize;
|
2020-12-16 11:14:12 -05:00
|
|
|
use std::borrow::Cow;
|
2020-09-10 09:57:45 -04:00
|
|
|
use std::cell::RefCell;
|
2020-08-18 12:30:13 -04:00
|
|
|
use std::rc::Rc;
|
2019-12-30 08:57:17 -05:00
|
|
|
use tokio::process::Command;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
use std::os::unix::process::ExitStatusExt;
|
|
|
|
|
2021-05-02 19:22:57 -04:00
|
|
|
pub fn init() -> Extension {
|
|
|
|
Extension::builder()
|
2022-03-14 13:44:15 -04:00
|
|
|
.ops(vec![op_run::decl(), op_run_status::decl(), op_kill::decl()])
|
2021-05-02 19:22:57 -04:00
|
|
|
.build()
|
2019-10-11 14:41:54 -04:00
|
|
|
}
|
|
|
|
|
2022-04-26 09:26:05 -04:00
|
|
|
#[derive(Copy, Clone, PartialEq, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub enum Stdio {
|
|
|
|
Inherit,
|
|
|
|
Piped,
|
|
|
|
Null,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Stdio {
|
|
|
|
pub fn as_stdio(&self) -> std::process::Stdio {
|
|
|
|
match &self {
|
|
|
|
Stdio::Inherit => std::process::Stdio::inherit(),
|
|
|
|
Stdio::Piped => std::process::Stdio::piped(),
|
|
|
|
Stdio::Null => std::process::Stdio::null(),
|
|
|
|
}
|
|
|
|
}
|
2019-11-14 12:10:25 -05:00
|
|
|
}
|
|
|
|
|
2022-04-26 09:26:05 -04:00
|
|
|
#[derive(Copy, Clone, PartialEq)]
|
|
|
|
pub enum StdioOrRid {
|
|
|
|
Stdio(Stdio),
|
|
|
|
Rid(ResourceId),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'de> Deserialize<'de> for StdioOrRid {
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
|
|
where
|
|
|
|
D: serde::Deserializer<'de>,
|
|
|
|
{
|
|
|
|
use serde_json::Value;
|
|
|
|
let value = Value::deserialize(deserializer)?;
|
|
|
|
match value {
|
|
|
|
Value::String(val) => match val.as_str() {
|
|
|
|
"inherit" => Ok(StdioOrRid::Stdio(Stdio::Inherit)),
|
|
|
|
"piped" => Ok(StdioOrRid::Stdio(Stdio::Piped)),
|
|
|
|
"null" => Ok(StdioOrRid::Stdio(Stdio::Null)),
|
|
|
|
val => Err(serde::de::Error::unknown_variant(
|
|
|
|
val,
|
|
|
|
&["inherit", "piped", "null"],
|
|
|
|
)),
|
|
|
|
},
|
|
|
|
Value::Number(val) => match val.as_u64() {
|
|
|
|
Some(val) if val <= ResourceId::MAX as u64 => {
|
|
|
|
Ok(StdioOrRid::Rid(val as ResourceId))
|
|
|
|
}
|
|
|
|
_ => Err(serde::de::Error::custom("Expected a positive integer")),
|
|
|
|
},
|
|
|
|
_ => Err(serde::de::Error::custom(
|
|
|
|
r#"Expected a resource id, "inherit", "piped", or "null""#,
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl StdioOrRid {
|
|
|
|
pub fn as_stdio(
|
|
|
|
&self,
|
|
|
|
state: &mut OpState,
|
|
|
|
) -> Result<std::process::Stdio, AnyError> {
|
|
|
|
match &self {
|
|
|
|
StdioOrRid::Stdio(val) => Ok(val.as_stdio()),
|
2022-05-11 12:48:38 -04:00
|
|
|
StdioOrRid::Rid(rid) => StdFileResource::as_stdio(state, *rid),
|
2022-04-26 09:26:05 -04:00
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-03-18 14:42:01 -04:00
|
|
|
pub struct RunArgs {
|
2020-03-21 17:44:18 -04:00
|
|
|
cmd: Vec<String>,
|
2019-08-26 08:50:21 -04:00
|
|
|
cwd: Option<String>,
|
2021-08-04 15:47:43 -04:00
|
|
|
clear_env: bool,
|
2019-08-26 08:50:21 -04:00
|
|
|
env: Vec<(String, String)>,
|
2021-09-14 15:38:35 -04:00
|
|
|
#[cfg(unix)]
|
2021-09-13 13:26:23 -04:00
|
|
|
gid: Option<u32>,
|
2021-09-14 15:38:35 -04:00
|
|
|
#[cfg(unix)]
|
2021-09-13 13:26:23 -04:00
|
|
|
uid: Option<u32>,
|
2022-04-26 09:26:05 -04:00
|
|
|
stdin: StdioOrRid,
|
|
|
|
stdout: StdioOrRid,
|
|
|
|
stderr: StdioOrRid,
|
2019-08-26 08:50:21 -04:00
|
|
|
}
|
|
|
|
|
2019-11-07 18:52:21 -05:00
|
|
|
struct ChildResource {
|
2020-12-16 11:14:12 -05:00
|
|
|
child: AsyncRefCell<tokio::process::Child>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Resource for ChildResource {
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"child".into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ChildResource {
|
|
|
|
fn borrow_mut(self: Rc<Self>) -> AsyncMutFuture<tokio::process::Child> {
|
|
|
|
RcRef::map(self, |r| &r.child).borrow_mut()
|
|
|
|
}
|
2019-11-07 18:52:21 -05:00
|
|
|
}
|
|
|
|
|
2021-04-05 12:40:24 -04:00
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
// TODO(@AaronO): maybe find a more descriptive name or a convention for return structs
|
|
|
|
struct RunInfo {
|
|
|
|
rid: ResourceId,
|
|
|
|
pid: Option<u32>,
|
|
|
|
stdin_rid: Option<ResourceId>,
|
|
|
|
stdout_rid: Option<ResourceId>,
|
|
|
|
stderr_rid: Option<ResourceId>,
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-03-14 18:38:53 -04:00
|
|
|
fn op_run(state: &mut OpState, run_args: RunArgs) -> Result<RunInfo, AnyError> {
|
2020-03-21 17:44:18 -04:00
|
|
|
let args = run_args.cmd;
|
2021-04-11 22:15:43 -04:00
|
|
|
state.borrow_mut::<Permissions>().run.check(&args[0])?;
|
2019-08-26 08:50:21 -04:00
|
|
|
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));
|
2021-08-04 15:47:43 -04:00
|
|
|
|
|
|
|
if run_args.clear_env {
|
|
|
|
super::check_unstable(state, "Deno.run.clearEnv");
|
|
|
|
c.env_clear();
|
|
|
|
}
|
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
|
|
|
|
2021-09-13 13:26:23 -04:00
|
|
|
#[cfg(unix)]
|
|
|
|
if let Some(gid) = run_args.gid {
|
|
|
|
super::check_unstable(state, "Deno.run.gid");
|
|
|
|
c.gid(gid);
|
|
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
|
|
if let Some(uid) = run_args.uid {
|
|
|
|
super::check_unstable(state, "Deno.run.uid");
|
|
|
|
c.uid(uid);
|
|
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
|
|
unsafe {
|
|
|
|
c.pre_exec(|| {
|
|
|
|
libc::setgroups(0, std::ptr::null());
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-14 11:03:02 -04:00
|
|
|
// TODO: make this work with other resources, eg. sockets
|
2022-04-26 09:26:05 -04:00
|
|
|
c.stdin(run_args.stdin.as_stdio(state)?);
|
2022-04-26 14:46:49 -04:00
|
|
|
c.stdout(
|
|
|
|
match run_args.stdout {
|
|
|
|
StdioOrRid::Stdio(Stdio::Inherit) => StdioOrRid::Rid(1),
|
|
|
|
value => value,
|
|
|
|
}
|
|
|
|
.as_stdio(state)?,
|
|
|
|
);
|
|
|
|
c.stderr(
|
|
|
|
match run_args.stderr {
|
|
|
|
StdioOrRid::Stdio(Stdio::Inherit) => StdioOrRid::Rid(2),
|
|
|
|
value => value,
|
|
|
|
}
|
|
|
|
.as_stdio(state)?,
|
|
|
|
);
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-12-30 08:57:17 -05:00
|
|
|
// We want to kill child when it's closed
|
|
|
|
c.kill_on_drop(true);
|
|
|
|
|
2019-08-14 11:03:02 -04:00
|
|
|
// Spawn the command.
|
2019-12-30 08:57:17 -05:00
|
|
|
let mut child = c.spawn()?;
|
2019-08-14 11:03:02 -04:00
|
|
|
let pid = child.id();
|
2019-11-07 18:52:21 -05:00
|
|
|
|
2020-01-12 04:21:53 -05:00
|
|
|
let stdin_rid = match child.stdin.take() {
|
2019-11-14 12:10:25 -05:00
|
|
|
Some(child_stdin) => {
|
2020-12-16 11:14:12 -05:00
|
|
|
let rid = state
|
|
|
|
.resource_table
|
2021-01-14 23:32:27 -05:00
|
|
|
.add(ChildStdinResource::from(child_stdin));
|
2019-11-14 12:10:25 -05:00
|
|
|
Some(rid)
|
|
|
|
}
|
|
|
|
None => None,
|
2019-11-07 18:52:21 -05:00
|
|
|
};
|
|
|
|
|
2020-01-12 04:21:53 -05:00
|
|
|
let stdout_rid = match child.stdout.take() {
|
2019-11-14 12:10:25 -05:00
|
|
|
Some(child_stdout) => {
|
2020-12-16 11:14:12 -05:00
|
|
|
let rid = state
|
|
|
|
.resource_table
|
2021-01-14 23:32:27 -05:00
|
|
|
.add(ChildStdoutResource::from(child_stdout));
|
2019-11-14 12:10:25 -05:00
|
|
|
Some(rid)
|
|
|
|
}
|
|
|
|
None => None,
|
2019-11-07 18:52:21 -05:00
|
|
|
};
|
|
|
|
|
2020-01-12 04:21:53 -05:00
|
|
|
let stderr_rid = match child.stderr.take() {
|
2019-11-14 12:10:25 -05:00
|
|
|
Some(child_stderr) => {
|
2020-12-16 11:14:12 -05:00
|
|
|
let rid = state
|
|
|
|
.resource_table
|
2021-01-14 23:32:27 -05:00
|
|
|
.add(ChildStderrResource::from(child_stderr));
|
2019-11-14 12:10:25 -05:00
|
|
|
Some(rid)
|
|
|
|
}
|
|
|
|
None => None,
|
2019-11-07 18:52:21 -05:00
|
|
|
};
|
|
|
|
|
2020-12-16 11:14:12 -05:00
|
|
|
let child_resource = ChildResource {
|
|
|
|
child: AsyncRefCell::new(child),
|
|
|
|
};
|
|
|
|
let child_rid = state.resource_table.add(child_resource);
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(RunInfo {
|
|
|
|
rid: child_rid,
|
|
|
|
pid,
|
|
|
|
stdin_rid,
|
|
|
|
stdout_rid,
|
|
|
|
stderr_rid,
|
|
|
|
})
|
2019-08-26 08:50:21 -04:00
|
|
|
}
|
2019-08-24 16:20:48 -04:00
|
|
|
|
2021-04-05 12:40:24 -04:00
|
|
|
#[derive(Serialize)]
|
2019-08-26 08:50:21 -04:00
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-09-06 10:05:33 -04:00
|
|
|
struct ProcessStatus {
|
2021-04-05 12:40:24 -04:00
|
|
|
got_signal: bool,
|
|
|
|
exit_code: i32,
|
|
|
|
exit_signal: i32,
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_run_status(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-04-05 12:40:24 -04:00
|
|
|
rid: ResourceId,
|
2021-09-06 10:05:33 -04:00
|
|
|
) -> Result<ProcessStatus, 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::<ChildResource>(rid)?;
|
2020-12-16 11:14:12 -05:00
|
|
|
let mut child = resource.borrow_mut().await;
|
2021-01-12 02:50:02 -05:00
|
|
|
let run_status = child.wait().await?;
|
2020-08-28 11:08:24 -04:00
|
|
|
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();
|
|
|
|
|
2021-09-06 10:05:33 -04:00
|
|
|
Ok(ProcessStatus {
|
2021-04-05 12:40:24 -04:00
|
|
|
got_signal,
|
|
|
|
exit_code: code.unwrap_or(-1),
|
|
|
|
exit_signal: signal.unwrap_or(-1),
|
|
|
|
})
|
2019-08-26 08:50:21 -04:00
|
|
|
}
|
|
|
|
|
2020-12-13 13:45:53 -05:00
|
|
|
#[cfg(unix)]
|
2022-05-14 23:57:09 -04:00
|
|
|
pub fn kill(pid: i32, signal: &str) -> Result<(), AnyError> {
|
|
|
|
let signo = super::signal::signal_str_to_int(signal)?;
|
2020-12-13 13:45:53 -05:00
|
|
|
use nix::sys::signal::{kill as unix_kill, Signal};
|
|
|
|
use nix::unistd::Pid;
|
2022-05-14 23:57:09 -04:00
|
|
|
let sig = Signal::try_from(signo)?;
|
2020-12-13 13:45:53 -05:00
|
|
|
unix_kill(Pid::from_raw(pid), Option::Some(sig)).map_err(AnyError::from)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(unix))]
|
2022-05-14 23:57:09 -04:00
|
|
|
pub fn kill(pid: i32, signal: &str) -> Result<(), AnyError> {
|
2022-04-26 09:26:05 -04:00
|
|
|
use deno_core::error::type_error;
|
2020-12-13 13:45:53 -05:00
|
|
|
use std::io::Error;
|
2021-01-13 23:52:12 -05:00
|
|
|
use std::io::ErrorKind::NotFound;
|
2022-06-14 15:31:50 -04:00
|
|
|
use winapi::shared::minwindef::DWORD;
|
2021-01-13 23:52:12 -05:00
|
|
|
use winapi::shared::minwindef::FALSE;
|
|
|
|
use winapi::shared::minwindef::TRUE;
|
|
|
|
use winapi::shared::winerror::ERROR_INVALID_PARAMETER;
|
|
|
|
use winapi::um::errhandlingapi::GetLastError;
|
|
|
|
use winapi::um::handleapi::CloseHandle;
|
|
|
|
use winapi::um::processthreadsapi::OpenProcess;
|
|
|
|
use winapi::um::processthreadsapi::TerminateProcess;
|
|
|
|
use winapi::um::winnt::PROCESS_TERMINATE;
|
|
|
|
|
2022-06-14 15:31:50 -04:00
|
|
|
if !matches!(signal, "SIGKILL" | "SIGTERM") {
|
|
|
|
Err(type_error(format!("Invalid signal: {}", signal)))
|
|
|
|
} else if pid <= 0 {
|
|
|
|
Err(type_error("Invalid pid"))
|
|
|
|
} else {
|
|
|
|
let handle = unsafe { OpenProcess(PROCESS_TERMINATE, FALSE, pid as DWORD) };
|
2022-06-13 16:39:46 -04:00
|
|
|
|
2021-01-13 23:52:12 -05:00
|
|
|
if handle.is_null() {
|
|
|
|
let err = match unsafe { GetLastError() } {
|
|
|
|
ERROR_INVALID_PARAMETER => Error::from(NotFound), // Invalid `pid`.
|
|
|
|
errno => Error::from_raw_os_error(errno as i32),
|
|
|
|
};
|
|
|
|
Err(err.into())
|
|
|
|
} else {
|
2022-06-13 16:39:46 -04:00
|
|
|
let is_terminated = unsafe { TerminateProcess(handle, 1) };
|
2021-01-13 23:52:12 -05:00
|
|
|
unsafe { CloseHandle(handle) };
|
2022-06-13 16:39:46 -04:00
|
|
|
match is_terminated {
|
2021-01-13 23:52:12 -05:00
|
|
|
FALSE => Err(Error::last_os_error().into()),
|
|
|
|
TRUE => Ok(()),
|
|
|
|
_ => unreachable!(),
|
2020-12-13 13:45:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-09-24 19:33:15 -04:00
|
|
|
fn op_kill(
|
|
|
|
state: &mut OpState,
|
2019-08-26 08:50:21 -04:00
|
|
|
pid: i32,
|
2022-05-14 23:57:09 -04:00
|
|
|
signal: String,
|
2021-09-24 19:33:15 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2021-04-11 22:15:43 -04:00
|
|
|
state.borrow_mut::<Permissions>().run.check_all()?;
|
2022-05-14 23:57:09 -04:00
|
|
|
kill(pid, &signal)?;
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(())
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|