2022-04-20 18:20:33 -04:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
use super::io::ChildStderrResource;
|
|
|
|
use super::io::ChildStdinResource;
|
|
|
|
use super::io::ChildStdoutResource;
|
2022-04-26 09:26:05 -04:00
|
|
|
use super::process::Stdio;
|
2022-04-26 14:46:49 -04:00
|
|
|
use super::process::StdioOrRid;
|
2022-04-20 18:20:33 -04:00
|
|
|
use crate::permissions::Permissions;
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::op;
|
|
|
|
use deno_core::Extension;
|
|
|
|
use deno_core::OpState;
|
|
|
|
use deno_core::Resource;
|
|
|
|
use deno_core::ResourceId;
|
|
|
|
use deno_core::ZeroCopyBuf;
|
|
|
|
use serde::Deserialize;
|
|
|
|
use serde::Serialize;
|
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::cell::RefCell;
|
2022-10-17 06:51:25 -04:00
|
|
|
#[cfg(windows)]
|
|
|
|
use std::os::windows::process::CommandExt;
|
2022-04-20 18:20:33 -04:00
|
|
|
use std::process::ExitStatus;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
use std::os::unix::prelude::ExitStatusExt;
|
|
|
|
#[cfg(unix)]
|
|
|
|
use std::os::unix::process::CommandExt;
|
|
|
|
|
|
|
|
pub fn init() -> Extension {
|
|
|
|
Extension::builder()
|
|
|
|
.ops(vec![
|
|
|
|
op_spawn_child::decl(),
|
2022-11-10 16:03:28 -05:00
|
|
|
op_node_unstable_spawn_child::decl(),
|
2022-04-20 18:20:33 -04:00
|
|
|
op_spawn_wait::decl(),
|
|
|
|
op_spawn_sync::decl(),
|
2022-11-10 16:03:28 -05:00
|
|
|
op_node_unstable_spawn_sync::decl(),
|
2022-04-20 18:20:33 -04:00
|
|
|
])
|
|
|
|
.build()
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ChildResource(tokio::process::Child);
|
|
|
|
|
|
|
|
impl Resource for ChildResource {
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"child".into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct SpawnArgs {
|
|
|
|
cmd: String,
|
|
|
|
args: Vec<String>,
|
|
|
|
cwd: Option<String>,
|
|
|
|
clear_env: bool,
|
|
|
|
env: Vec<(String, String)>,
|
|
|
|
#[cfg(unix)]
|
|
|
|
gid: Option<u32>,
|
|
|
|
#[cfg(unix)]
|
|
|
|
uid: Option<u32>,
|
2022-10-17 06:51:25 -04:00
|
|
|
#[cfg(windows)]
|
|
|
|
windows_raw_arguments: bool,
|
2022-04-20 18:20:33 -04:00
|
|
|
|
|
|
|
#[serde(flatten)]
|
|
|
|
stdio: ChildStdio,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct ChildStdio {
|
|
|
|
stdin: Stdio,
|
|
|
|
stdout: Stdio,
|
|
|
|
stderr: Stdio,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct ChildStatus {
|
|
|
|
success: bool,
|
|
|
|
code: i32,
|
2022-05-18 01:32:45 -04:00
|
|
|
signal: Option<String>,
|
2022-04-20 18:20:33 -04:00
|
|
|
}
|
|
|
|
|
2022-07-18 16:24:35 -04:00
|
|
|
impl TryFrom<ExitStatus> for ChildStatus {
|
2022-05-18 01:32:45 -04:00
|
|
|
type Error = AnyError;
|
|
|
|
|
|
|
|
fn try_from(status: ExitStatus) -> Result<Self, Self::Error> {
|
2022-04-20 18:20:33 -04:00
|
|
|
let code = status.code();
|
|
|
|
#[cfg(unix)]
|
|
|
|
let signal = status.signal();
|
|
|
|
#[cfg(not(unix))]
|
2022-05-18 01:32:45 -04:00
|
|
|
let signal: Option<i32> = None;
|
2022-04-20 18:20:33 -04:00
|
|
|
|
2022-05-18 01:32:45 -04:00
|
|
|
let status = if let Some(signal) = signal {
|
2022-04-20 18:20:33 -04:00
|
|
|
ChildStatus {
|
|
|
|
success: false,
|
|
|
|
code: 128 + signal,
|
2022-05-18 01:32:45 -04:00
|
|
|
#[cfg(unix)]
|
|
|
|
signal: Some(
|
|
|
|
crate::ops::signal::signal_int_to_str(signal)?.to_string(),
|
|
|
|
),
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
signal: None,
|
2022-04-20 18:20:33 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let code = code.expect("Should have either an exit code or a signal.");
|
|
|
|
|
|
|
|
ChildStatus {
|
|
|
|
success: code == 0,
|
|
|
|
code,
|
|
|
|
signal: None,
|
|
|
|
}
|
2022-05-18 01:32:45 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(status)
|
2022-04-20 18:20:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct SpawnOutput {
|
|
|
|
status: ChildStatus,
|
|
|
|
stdout: Option<ZeroCopyBuf>,
|
|
|
|
stderr: Option<ZeroCopyBuf>,
|
|
|
|
}
|
|
|
|
|
2022-11-10 16:03:28 -05:00
|
|
|
fn node_unstable_create_command(
|
|
|
|
state: &mut OpState,
|
|
|
|
args: SpawnArgs,
|
|
|
|
api_name: &str,
|
|
|
|
) -> Result<std::process::Command, AnyError> {
|
|
|
|
state
|
|
|
|
.borrow_mut::<Permissions>()
|
|
|
|
.run
|
|
|
|
.check(&args.cmd, Some(api_name))?;
|
|
|
|
|
|
|
|
let mut command = std::process::Command::new(args.cmd);
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
if args.windows_raw_arguments {
|
|
|
|
for arg in args.args.iter() {
|
|
|
|
command.raw_arg(arg);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
command.args(args.args);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
command.args(args.args);
|
|
|
|
|
|
|
|
if let Some(cwd) = args.cwd {
|
|
|
|
command.current_dir(cwd);
|
|
|
|
}
|
|
|
|
|
|
|
|
if args.clear_env {
|
|
|
|
command.env_clear();
|
|
|
|
}
|
|
|
|
command.envs(args.env);
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
if let Some(gid) = args.gid {
|
|
|
|
command.gid(gid);
|
|
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
|
|
if let Some(uid) = args.uid {
|
|
|
|
command.uid(uid);
|
|
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
|
|
// TODO(bartlomieju):
|
|
|
|
#[allow(clippy::undocumented_unsafe_blocks)]
|
|
|
|
unsafe {
|
|
|
|
command.pre_exec(|| {
|
|
|
|
libc::setgroups(0, std::ptr::null());
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
command.stdin(args.stdio.stdin.as_stdio());
|
|
|
|
command.stdout(match args.stdio.stdout {
|
|
|
|
Stdio::Inherit => StdioOrRid::Rid(1).as_stdio(state)?,
|
|
|
|
value => value.as_stdio(),
|
|
|
|
});
|
|
|
|
command.stderr(match args.stdio.stderr {
|
|
|
|
Stdio::Inherit => StdioOrRid::Rid(2).as_stdio(state)?,
|
|
|
|
value => value.as_stdio(),
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(command)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:20:33 -04:00
|
|
|
fn create_command(
|
|
|
|
state: &mut OpState,
|
|
|
|
args: SpawnArgs,
|
2022-09-27 16:36:33 -04:00
|
|
|
api_name: &str,
|
2022-04-20 18:20:33 -04:00
|
|
|
) -> Result<std::process::Command, AnyError> {
|
|
|
|
super::check_unstable(state, "Deno.spawn");
|
2022-09-27 16:36:33 -04:00
|
|
|
state
|
|
|
|
.borrow_mut::<Permissions>()
|
|
|
|
.run
|
|
|
|
.check(&args.cmd, Some(api_name))?;
|
2022-04-20 18:20:33 -04:00
|
|
|
|
|
|
|
let mut command = std::process::Command::new(args.cmd);
|
2022-10-17 06:51:25 -04:00
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
if args.windows_raw_arguments {
|
|
|
|
for arg in args.args.iter() {
|
|
|
|
command.raw_arg(arg);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
command.args(args.args);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(windows))]
|
2022-04-20 18:20:33 -04:00
|
|
|
command.args(args.args);
|
|
|
|
|
|
|
|
if let Some(cwd) = args.cwd {
|
|
|
|
command.current_dir(cwd);
|
|
|
|
}
|
|
|
|
|
|
|
|
if args.clear_env {
|
|
|
|
command.env_clear();
|
|
|
|
}
|
|
|
|
command.envs(args.env);
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
if let Some(gid) = args.gid {
|
|
|
|
super::check_unstable(state, "Deno.spawn.gid");
|
|
|
|
command.gid(gid);
|
|
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
|
|
if let Some(uid) = args.uid {
|
|
|
|
super::check_unstable(state, "Deno.spawn.uid");
|
|
|
|
command.uid(uid);
|
|
|
|
}
|
|
|
|
#[cfg(unix)]
|
2022-06-25 18:13:24 -04:00
|
|
|
// TODO(bartlomieju):
|
|
|
|
#[allow(clippy::undocumented_unsafe_blocks)]
|
2022-04-20 18:20:33 -04:00
|
|
|
unsafe {
|
|
|
|
command.pre_exec(|| {
|
|
|
|
libc::setgroups(0, std::ptr::null());
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-26 09:26:05 -04:00
|
|
|
command.stdin(args.stdio.stdin.as_stdio());
|
2022-04-26 14:46:49 -04:00
|
|
|
command.stdout(match args.stdio.stdout {
|
|
|
|
Stdio::Inherit => StdioOrRid::Rid(1).as_stdio(state)?,
|
|
|
|
value => value.as_stdio(),
|
|
|
|
});
|
|
|
|
command.stderr(match args.stdio.stderr {
|
|
|
|
Stdio::Inherit => StdioOrRid::Rid(2).as_stdio(state)?,
|
|
|
|
value => value.as_stdio(),
|
|
|
|
});
|
2022-04-20 18:20:33 -04:00
|
|
|
|
|
|
|
Ok(command)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct Child {
|
|
|
|
rid: ResourceId,
|
|
|
|
pid: u32,
|
|
|
|
stdin_rid: Option<ResourceId>,
|
|
|
|
stdout_rid: Option<ResourceId>,
|
|
|
|
stderr_rid: Option<ResourceId>,
|
|
|
|
}
|
|
|
|
|
2022-11-10 16:03:28 -05:00
|
|
|
fn spawn_child(
|
2022-04-20 18:20:33 -04:00
|
|
|
state: &mut OpState,
|
2022-11-10 16:03:28 -05:00
|
|
|
command: std::process::Command,
|
2022-04-20 18:20:33 -04:00
|
|
|
) -> Result<Child, AnyError> {
|
2022-11-10 16:03:28 -05:00
|
|
|
let mut command = tokio::process::Command::from(command);
|
2022-04-20 18:20:33 -04:00
|
|
|
// TODO(@crowlkats): allow detaching processes.
|
|
|
|
// currently deno will orphan a process when exiting with an error or Deno.exit()
|
|
|
|
// We want to kill child when it's closed
|
|
|
|
command.kill_on_drop(true);
|
|
|
|
|
|
|
|
let mut child = command.spawn()?;
|
|
|
|
let pid = child.id().expect("Process ID should be set.");
|
|
|
|
|
|
|
|
let stdin_rid = child
|
|
|
|
.stdin
|
|
|
|
.take()
|
|
|
|
.map(|stdin| state.resource_table.add(ChildStdinResource::from(stdin)));
|
|
|
|
|
|
|
|
let stdout_rid = child
|
|
|
|
.stdout
|
|
|
|
.take()
|
|
|
|
.map(|stdout| state.resource_table.add(ChildStdoutResource::from(stdout)));
|
|
|
|
|
|
|
|
let stderr_rid = child
|
|
|
|
.stderr
|
|
|
|
.take()
|
|
|
|
.map(|stderr| state.resource_table.add(ChildStderrResource::from(stderr)));
|
|
|
|
|
|
|
|
let child_rid = state.resource_table.add(ChildResource(child));
|
|
|
|
|
|
|
|
Ok(Child {
|
|
|
|
rid: child_rid,
|
|
|
|
pid,
|
|
|
|
stdin_rid,
|
|
|
|
stdout_rid,
|
|
|
|
stderr_rid,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-10 16:03:28 -05:00
|
|
|
#[op]
|
|
|
|
fn op_spawn_child(
|
|
|
|
state: &mut OpState,
|
|
|
|
args: SpawnArgs,
|
|
|
|
api_name: String,
|
|
|
|
) -> Result<Child, AnyError> {
|
|
|
|
let command = create_command(state, args, &api_name)?;
|
|
|
|
spawn_child(state, command)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[op]
|
|
|
|
fn op_node_unstable_spawn_child(
|
|
|
|
state: &mut OpState,
|
|
|
|
args: SpawnArgs,
|
|
|
|
api_name: String,
|
|
|
|
) -> Result<Child, AnyError> {
|
|
|
|
let command = node_unstable_create_command(state, args, &api_name)?;
|
|
|
|
spawn_child(state, command)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:20:33 -04:00
|
|
|
#[op]
|
|
|
|
async fn op_spawn_wait(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
|
|
|
rid: ResourceId,
|
|
|
|
) -> Result<ChildStatus, AnyError> {
|
|
|
|
let resource = state
|
|
|
|
.borrow_mut()
|
|
|
|
.resource_table
|
|
|
|
.take::<ChildResource>(rid)?;
|
2022-05-18 01:32:45 -04:00
|
|
|
Rc::try_unwrap(resource)
|
|
|
|
.ok()
|
|
|
|
.unwrap()
|
|
|
|
.0
|
|
|
|
.wait()
|
|
|
|
.await?
|
|
|
|
.try_into()
|
2022-04-20 18:20:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[op]
|
|
|
|
fn op_spawn_sync(
|
|
|
|
state: &mut OpState,
|
|
|
|
args: SpawnArgs,
|
|
|
|
) -> Result<SpawnOutput, AnyError> {
|
|
|
|
let stdout = matches!(args.stdio.stdout, Stdio::Piped);
|
|
|
|
let stderr = matches!(args.stdio.stderr, Stdio::Piped);
|
2022-12-09 10:43:36 -05:00
|
|
|
let output =
|
|
|
|
create_command(state, args, "Deno.Command().outputSync()")?.output()?;
|
2022-04-20 18:20:33 -04:00
|
|
|
|
|
|
|
Ok(SpawnOutput {
|
2022-05-18 01:32:45 -04:00
|
|
|
status: output.status.try_into()?,
|
2022-04-20 18:20:33 -04:00
|
|
|
stdout: if stdout {
|
|
|
|
Some(output.stdout.into())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
|
|
|
stderr: if stderr {
|
|
|
|
Some(output.stderr.into())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2022-11-10 16:03:28 -05:00
|
|
|
|
|
|
|
#[op]
|
|
|
|
fn op_node_unstable_spawn_sync(
|
|
|
|
state: &mut OpState,
|
|
|
|
args: SpawnArgs,
|
|
|
|
) -> Result<SpawnOutput, AnyError> {
|
|
|
|
let stdout = matches!(args.stdio.stdout, Stdio::Piped);
|
|
|
|
let stderr = matches!(args.stdio.stderr, Stdio::Piped);
|
|
|
|
let output =
|
2022-12-09 10:43:36 -05:00
|
|
|
node_unstable_create_command(state, args, "Deno.Command().outputSync()")?
|
|
|
|
.output()?;
|
2022-11-10 16:03:28 -05:00
|
|
|
|
|
|
|
Ok(SpawnOutput {
|
|
|
|
status: output.status.try_into()?,
|
|
|
|
stdout: if stdout {
|
|
|
|
Some(output.stdout.into())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
|
|
|
stderr: if stderr {
|
|
|
|
Some(output.stderr.into())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|