1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 07:14:47 -05:00

refactor(runtime): merge "spawn" into "process" (#18022)

This commit merges "runtime/js/40_spawn.js" into
"runtime/js/40_process.js", and "runtime::ops::spawn" 
into "runtime::ops::process".

It makes little sense to have them separated given that we want to
factor out these APIs into a separate extension crate.
This commit is contained in:
Bartek Iwańczuk 2023-03-05 08:19:34 -04:00 committed by GitHub
parent d4807f458e
commit de0d148d93
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 792 additions and 827 deletions

View file

@ -207,7 +207,6 @@ mod startup_snapshot {
"40_http.js", "40_http.js",
"40_process.js", "40_process.js",
"40_signals.js", "40_signals.js",
"40_spawn.js",
"40_tty.js", "40_tty.js",
"41_prompt.js", "41_prompt.js",
"90_deno_ns.js", "90_deno_ns.js",

View file

@ -2,10 +2,6 @@
const core = globalThis.Deno.core; const core = globalThis.Deno.core;
const ops = core.ops; const ops = core.ops;
import { FsFile } from "internal:runtime/30_fs.js";
import { readAll } from "internal:deno_io/12_io.js";
import { pathFromURL } from "internal:runtime/06_util.js";
import { assert } from "internal:deno_web/00_infra.js";
const primordials = globalThis.__bootstrap.primordials; const primordials = globalThis.__bootstrap.primordials;
const { const {
ArrayPrototypeMap, ArrayPrototypeMap,
@ -14,7 +10,25 @@ const {
ObjectEntries, ObjectEntries,
SafeArrayIterator, SafeArrayIterator,
String, String,
ObjectPrototypeIsPrototypeOf,
PromisePrototypeThen,
SafePromiseAll,
SymbolFor,
Symbol,
} = primordials; } = primordials;
import { FsFile } from "internal:runtime/30_fs.js";
import { readAll } from "internal:deno_io/12_io.js";
import { pathFromURL } from "internal:runtime/06_util.js";
import { assert } from "internal:deno_web/00_infra.js";
import * as abortSignal from "internal:deno_web/03_abort_signal.js";
import {
readableStreamCollectIntoUint8Array,
readableStreamForRidUnrefable,
readableStreamForRidUnrefableRef,
readableStreamForRidUnrefableUnref,
ReadableStreamPrototype,
writableStreamForRid,
} from "internal:deno_web/06_streams.js";
function opKill(pid, signo, apiName) { function opKill(pid, signo, apiName) {
ops.op_kill(pid, signo, apiName); ops.op_kill(pid, signo, apiName);
@ -130,4 +144,301 @@ function run({
return new Process(res); return new Process(res);
} }
export { kill, Process, run }; const illegalConstructorKey = Symbol("illegalConstructorKey");
const promiseIdSymbol = SymbolFor("Deno.core.internalPromiseId");
function spawnChildInner(opFn, command, apiName, {
args = [],
cwd = undefined,
clearEnv = false,
env = {},
uid = undefined,
gid = undefined,
stdin = "null",
stdout = "piped",
stderr = "piped",
signal = undefined,
windowsRawArguments = false,
} = {}) {
const child = opFn({
cmd: pathFromURL(command),
args: ArrayPrototypeMap(args, String),
cwd: pathFromURL(cwd),
clearEnv,
env: ObjectEntries(env),
uid,
gid,
stdin,
stdout,
stderr,
windowsRawArguments,
}, apiName);
return new ChildProcess(illegalConstructorKey, {
...child,
signal,
});
}
function spawnChild(command, options = {}) {
return spawnChildInner(
ops.op_spawn_child,
command,
"Deno.Command().spawn()",
options,
);
}
function collectOutput(readableStream) {
if (
!(ObjectPrototypeIsPrototypeOf(ReadableStreamPrototype, readableStream))
) {
return null;
}
return readableStreamCollectIntoUint8Array(readableStream);
}
class ChildProcess {
#rid;
#waitPromiseId;
#unrefed = false;
#pid;
get pid() {
return this.#pid;
}
#stdin = null;
get stdin() {
if (this.#stdin == null) {
throw new TypeError("stdin is not piped");
}
return this.#stdin;
}
#stdoutRid;
#stdout = null;
get stdout() {
if (this.#stdout == null) {
throw new TypeError("stdout is not piped");
}
return this.#stdout;
}
#stderrRid;
#stderr = null;
get stderr() {
if (this.#stderr == null) {
throw new TypeError("stderr is not piped");
}
return this.#stderr;
}
constructor(key = null, {
signal,
rid,
pid,
stdinRid,
stdoutRid,
stderrRid,
} = null) {
if (key !== illegalConstructorKey) {
throw new TypeError("Illegal constructor.");
}
this.#rid = rid;
this.#pid = pid;
if (stdinRid !== null) {
this.#stdin = writableStreamForRid(stdinRid);
}
if (stdoutRid !== null) {
this.#stdoutRid = stdoutRid;
this.#stdout = readableStreamForRidUnrefable(stdoutRid);
}
if (stderrRid !== null) {
this.#stderrRid = stderrRid;
this.#stderr = readableStreamForRidUnrefable(stderrRid);
}
const onAbort = () => this.kill("SIGTERM");
signal?.[abortSignal.add](onAbort);
const waitPromise = core.opAsync("op_spawn_wait", this.#rid);
this.#waitPromiseId = waitPromise[promiseIdSymbol];
this.#status = PromisePrototypeThen(waitPromise, (res) => {
this.#rid = null;
signal?.[abortSignal.remove](onAbort);
return res;
});
}
#status;
get status() {
return this.#status;
}
async output() {
if (this.#stdout?.locked) {
throw new TypeError(
"Can't collect output because stdout is locked",
);
}
if (this.#stderr?.locked) {
throw new TypeError(
"Can't collect output because stderr is locked",
);
}
const { 0: status, 1: stdout, 2: stderr } = await SafePromiseAll([
this.#status,
collectOutput(this.#stdout),
collectOutput(this.#stderr),
]);
return {
success: status.success,
code: status.code,
signal: status.signal,
get stdout() {
if (stdout == null) {
throw new TypeError("stdout is not piped");
}
return stdout;
},
get stderr() {
if (stderr == null) {
throw new TypeError("stderr is not piped");
}
return stderr;
},
};
}
kill(signo = "SIGTERM") {
if (this.#rid === null) {
throw new TypeError("Child process has already terminated.");
}
ops.op_kill(this.#pid, signo, "Deno.Child.kill()");
}
ref() {
this.#unrefed = false;
core.refOp(this.#waitPromiseId);
if (this.#stdout) readableStreamForRidUnrefableRef(this.#stdout);
if (this.#stderr) readableStreamForRidUnrefableRef(this.#stderr);
}
unref() {
this.#unrefed = true;
core.unrefOp(this.#waitPromiseId);
if (this.#stdout) readableStreamForRidUnrefableUnref(this.#stdout);
if (this.#stderr) readableStreamForRidUnrefableUnref(this.#stderr);
}
}
function spawn(command, options) {
if (options?.stdin === "piped") {
throw new TypeError(
"Piped stdin is not supported for this function, use 'Deno.Command().spawn()' instead",
);
}
return spawnChildInner(
ops.op_spawn_child,
command,
"Deno.Command().output()",
options,
)
.output();
}
function spawnSync(command, {
args = [],
cwd = undefined,
clearEnv = false,
env = {},
uid = undefined,
gid = undefined,
stdin = "null",
stdout = "piped",
stderr = "piped",
windowsRawArguments = false,
} = {}) {
if (stdin === "piped") {
throw new TypeError(
"Piped stdin is not supported for this function, use 'Deno.Command().spawn()' instead",
);
}
const result = ops.op_spawn_sync({
cmd: pathFromURL(command),
args: ArrayPrototypeMap(args, String),
cwd: pathFromURL(cwd),
clearEnv,
env: ObjectEntries(env),
uid,
gid,
stdin,
stdout,
stderr,
windowsRawArguments,
});
return {
success: result.status.success,
code: result.status.code,
signal: result.status.signal,
get stdout() {
if (result.stdout == null) {
throw new TypeError("stdout is not piped");
}
return result.stdout;
},
get stderr() {
if (result.stderr == null) {
throw new TypeError("stderr is not piped");
}
return result.stderr;
},
};
}
class Command {
#command;
#options;
constructor(command, options) {
this.#command = command;
this.#options = options;
}
output() {
if (this.#options?.stdin === "piped") {
throw new TypeError(
"Piped stdin is not supported for this function, use 'Deno.Command.spawn()' instead",
);
}
return spawn(this.#command, this.#options);
}
outputSync() {
if (this.#options?.stdin === "piped") {
throw new TypeError(
"Piped stdin is not supported for this function, use 'Deno.Command.spawn()' instead",
);
}
return spawnSync(this.#command, this.#options);
}
spawn() {
const options = {
...(this.#options ?? {}),
stdout: this.#options?.stdout ?? "inherit",
stderr: this.#options?.stderr ?? "inherit",
stdin: this.#options?.stdin ?? "inherit",
};
return spawnChild(this.#command, options);
}
}
export { ChildProcess, Command, kill, Process, run };

View file

@ -1,326 +0,0 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
const core = globalThis.Deno.core;
const ops = core.ops;
const primordials = globalThis.__bootstrap.primordials;
import { pathFromURL } from "internal:runtime/06_util.js";
import { add, remove } from "internal:deno_web/03_abort_signal.js";
const {
ArrayPrototypeMap,
ObjectEntries,
ObjectPrototypeIsPrototypeOf,
String,
TypeError,
PromisePrototypeThen,
SafePromiseAll,
SymbolFor,
Symbol,
} = primordials;
import {
readableStreamCollectIntoUint8Array,
readableStreamForRidUnrefable,
readableStreamForRidUnrefableRef,
readableStreamForRidUnrefableUnref,
ReadableStreamPrototype,
writableStreamForRid,
} from "internal:deno_web/06_streams.js";
const illegalConstructorKey = Symbol("illegalConstructorKey");
const promiseIdSymbol = SymbolFor("Deno.core.internalPromiseId");
function spawnChildInner(opFn, command, apiName, {
args = [],
cwd = undefined,
clearEnv = false,
env = {},
uid = undefined,
gid = undefined,
stdin = "null",
stdout = "piped",
stderr = "piped",
signal = undefined,
windowsRawArguments = false,
} = {}) {
const child = opFn({
cmd: pathFromURL(command),
args: ArrayPrototypeMap(args, String),
cwd: pathFromURL(cwd),
clearEnv,
env: ObjectEntries(env),
uid,
gid,
stdin,
stdout,
stderr,
windowsRawArguments,
}, apiName);
return new ChildProcess(illegalConstructorKey, {
...child,
signal,
});
}
function spawnChild(command, options = {}) {
return spawnChildInner(
ops.op_spawn_child,
command,
"Deno.Command().spawn()",
options,
);
}
function collectOutput(readableStream) {
if (
!(ObjectPrototypeIsPrototypeOf(ReadableStreamPrototype, readableStream))
) {
return null;
}
return readableStreamCollectIntoUint8Array(readableStream);
}
class ChildProcess {
#rid;
#waitPromiseId;
#unrefed = false;
#pid;
get pid() {
return this.#pid;
}
#stdin = null;
get stdin() {
if (this.#stdin == null) {
throw new TypeError("stdin is not piped");
}
return this.#stdin;
}
#stdoutRid;
#stdout = null;
get stdout() {
if (this.#stdout == null) {
throw new TypeError("stdout is not piped");
}
return this.#stdout;
}
#stderrRid;
#stderr = null;
get stderr() {
if (this.#stderr == null) {
throw new TypeError("stderr is not piped");
}
return this.#stderr;
}
constructor(key = null, {
signal,
rid,
pid,
stdinRid,
stdoutRid,
stderrRid,
} = null) {
if (key !== illegalConstructorKey) {
throw new TypeError("Illegal constructor.");
}
this.#rid = rid;
this.#pid = pid;
if (stdinRid !== null) {
this.#stdin = writableStreamForRid(stdinRid);
}
if (stdoutRid !== null) {
this.#stdoutRid = stdoutRid;
this.#stdout = readableStreamForRidUnrefable(stdoutRid);
}
if (stderrRid !== null) {
this.#stderrRid = stderrRid;
this.#stderr = readableStreamForRidUnrefable(stderrRid);
}
const onAbort = () => this.kill("SIGTERM");
signal?.[add](onAbort);
const waitPromise = core.opAsync("op_spawn_wait", this.#rid);
this.#waitPromiseId = waitPromise[promiseIdSymbol];
this.#status = PromisePrototypeThen(waitPromise, (res) => {
this.#rid = null;
signal?.[remove](onAbort);
return res;
});
}
#status;
get status() {
return this.#status;
}
async output() {
if (this.#stdout?.locked) {
throw new TypeError(
"Can't collect output because stdout is locked",
);
}
if (this.#stderr?.locked) {
throw new TypeError(
"Can't collect output because stderr is locked",
);
}
const { 0: status, 1: stdout, 2: stderr } = await SafePromiseAll([
this.#status,
collectOutput(this.#stdout),
collectOutput(this.#stderr),
]);
return {
success: status.success,
code: status.code,
signal: status.signal,
get stdout() {
if (stdout == null) {
throw new TypeError("stdout is not piped");
}
return stdout;
},
get stderr() {
if (stderr == null) {
throw new TypeError("stderr is not piped");
}
return stderr;
},
};
}
kill(signo = "SIGTERM") {
if (this.#rid === null) {
throw new TypeError("Child process has already terminated.");
}
ops.op_kill(this.#pid, signo, "Deno.Child.kill()");
}
ref() {
this.#unrefed = false;
core.refOp(this.#waitPromiseId);
if (this.#stdout) readableStreamForRidUnrefableRef(this.#stdout);
if (this.#stderr) readableStreamForRidUnrefableRef(this.#stderr);
}
unref() {
this.#unrefed = true;
core.unrefOp(this.#waitPromiseId);
if (this.#stdout) readableStreamForRidUnrefableUnref(this.#stdout);
if (this.#stderr) readableStreamForRidUnrefableUnref(this.#stderr);
}
}
function spawn(command, options) {
if (options?.stdin === "piped") {
throw new TypeError(
"Piped stdin is not supported for this function, use 'Deno.Command().spawn()' instead",
);
}
return spawnChildInner(
ops.op_spawn_child,
command,
"Deno.Command().output()",
options,
)
.output();
}
function spawnSync(command, {
args = [],
cwd = undefined,
clearEnv = false,
env = {},
uid = undefined,
gid = undefined,
stdin = "null",
stdout = "piped",
stderr = "piped",
windowsRawArguments = false,
} = {}) {
if (stdin === "piped") {
throw new TypeError(
"Piped stdin is not supported for this function, use 'Deno.Command().spawn()' instead",
);
}
const result = ops.op_spawn_sync({
cmd: pathFromURL(command),
args: ArrayPrototypeMap(args, String),
cwd: pathFromURL(cwd),
clearEnv,
env: ObjectEntries(env),
uid,
gid,
stdin,
stdout,
stderr,
windowsRawArguments,
});
return {
success: result.status.success,
code: result.status.code,
signal: result.status.signal,
get stdout() {
if (result.stdout == null) {
throw new TypeError("stdout is not piped");
}
return result.stdout;
},
get stderr() {
if (result.stderr == null) {
throw new TypeError("stderr is not piped");
}
return result.stderr;
},
};
}
class Command {
#command;
#options;
constructor(command, options) {
this.#command = command;
this.#options = options;
}
output() {
if (this.#options?.stdin === "piped") {
throw new TypeError(
"Piped stdin is not supported for this function, use 'Deno.Command.spawn()' instead",
);
}
return spawn(this.#command, this.#options);
}
outputSync() {
if (this.#options?.stdin === "piped") {
throw new TypeError(
"Piped stdin is not supported for this function, use 'Deno.Command.spawn()' instead",
);
}
return spawnSync(this.#command, this.#options);
}
spawn() {
const options = {
...(this.#options ?? {}),
stdout: this.#options?.stdout ?? "inherit",
stderr: this.#options?.stderr ?? "inherit",
stdin: this.#options?.stdin ?? "inherit",
};
return spawnChild(this.#command, options);
}
}
export { ChildProcess, Command };

View file

@ -22,7 +22,6 @@ import * as fsEvents from "internal:runtime/40_fs_events.js";
import * as process from "internal:runtime/40_process.js"; import * as process from "internal:runtime/40_process.js";
import * as signals from "internal:runtime/40_signals.js"; import * as signals from "internal:runtime/40_signals.js";
import * as tty from "internal:runtime/40_tty.js"; import * as tty from "internal:runtime/40_tty.js";
import * as spawn from "internal:runtime/40_spawn.js";
// TODO(bartlomieju): this is funky we have two `http` imports // TODO(bartlomieju): this is funky we have two `http` imports
import * as httpRuntime from "internal:runtime/40_http.js"; import * as httpRuntime from "internal:runtime/40_http.js";
@ -148,9 +147,9 @@ const denoNs = {
consoleSize: tty.consoleSize, consoleSize: tty.consoleSize,
gid: os.gid, gid: os.gid,
uid: os.uid, uid: os.uid,
Command: spawn.Command, Command: process.Command,
// TODO(bartlomieju): why is this exported? // TODO(bartlomieju): why is this exported?
ChildProcess: spawn.ChildProcess, ChildProcess: process.ChildProcess,
}; };
const denoNsUnstable = { const denoNsUnstable = {

View file

@ -8,7 +8,6 @@ pub mod permissions;
pub mod process; pub mod process;
pub mod runtime; pub mod runtime;
pub mod signal; pub mod signal;
pub mod spawn;
pub mod tty; pub mod tty;
mod utils; mod utils;
pub mod web_worker; pub mod web_worker;

View file

@ -1,13 +1,10 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use super::check_unstable;
use super::signal;
use crate::permissions::PermissionsContainer; use crate::permissions::PermissionsContainer;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::op; use deno_core::op;
use deno_io::ChildStderrResource;
use deno_io::ChildStdinResource;
use deno_io::ChildStdoutResource;
use deno_io::StdFileResource;
use deno_core::serde_json; use deno_core::serde_json;
use deno_core::AsyncMutFuture; use deno_core::AsyncMutFuture;
use deno_core::AsyncRefCell; use deno_core::AsyncRefCell;
@ -16,21 +13,26 @@ use deno_core::OpState;
use deno_core::RcRef; use deno_core::RcRef;
use deno_core::Resource; use deno_core::Resource;
use deno_core::ResourceId; use deno_core::ResourceId;
use deno_core::ZeroCopyBuf;
use deno_io::ChildStderrResource;
use deno_io::ChildStdinResource;
use deno_io::ChildStdoutResource;
use deno_io::StdFileResource;
use serde::Deserialize; use serde::Deserialize;
use serde::Serialize; use serde::Serialize;
use std::borrow::Cow; use std::borrow::Cow;
use std::cell::RefCell; use std::cell::RefCell;
use std::process::ExitStatus;
use std::rc::Rc; use std::rc::Rc;
use tokio::process::Command; use tokio::process::Command;
#[cfg(unix)] #[cfg(windows)]
use std::os::unix::process::ExitStatusExt; use std::os::windows::process::CommandExt;
pub fn init() -> Extension { #[cfg(unix)]
Extension::builder("deno_process") use std::os::unix::prelude::ExitStatusExt;
.ops(vec![op_run::decl(), op_run_status::decl(), op_kill::decl()]) #[cfg(unix)]
.build() use std::os::unix::process::CommandExt;
}
#[derive(Copy, Clone, Eq, PartialEq, Deserialize)] #[derive(Copy, Clone, Eq, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
@ -98,25 +100,20 @@ impl StdioOrRid {
} }
} }
#[derive(Deserialize)] pub fn init_ops() -> Extension {
#[serde(rename_all = "camelCase")] Extension::builder("deno_process")
pub struct RunArgs { .ops(vec![
cmd: Vec<String>, op_spawn_child::decl(),
cwd: Option<String>, op_spawn_wait::decl(),
clear_env: bool, op_spawn_sync::decl(),
env: Vec<(String, String)>, deprecated::op_run::decl(),
#[cfg(unix)] deprecated::op_run_status::decl(),
gid: Option<u32>, deprecated::op_kill::decl(),
#[cfg(unix)] ])
uid: Option<u32>, .build()
stdin: StdioOrRid,
stdout: StdioOrRid,
stderr: StdioOrRid,
} }
struct ChildResource { struct ChildResource(tokio::process::Child);
child: AsyncRefCell<tokio::process::Child>,
}
impl Resource for ChildResource { impl Resource for ChildResource {
fn name(&self) -> Cow<str> { fn name(&self) -> Cow<str> {
@ -124,127 +121,188 @@ impl Resource for ChildResource {
} }
} }
impl ChildResource { #[derive(Deserialize)]
fn borrow_mut(self: Rc<Self>) -> AsyncMutFuture<tokio::process::Child> { #[serde(rename_all = "camelCase")]
RcRef::map(self, |r| &r.child).borrow_mut() 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>,
#[cfg(windows)]
windows_raw_arguments: bool,
#[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,
signal: Option<String>,
}
impl TryFrom<ExitStatus> for ChildStatus {
type Error = AnyError;
fn try_from(status: ExitStatus) -> Result<Self, Self::Error> {
let code = status.code();
#[cfg(unix)]
let signal = status.signal();
#[cfg(not(unix))]
let signal: Option<i32> = None;
let status = if let Some(signal) = signal {
ChildStatus {
success: false,
code: 128 + signal,
#[cfg(unix)]
signal: Some(
crate::ops::signal::signal_int_to_str(signal)?.to_string(),
),
#[cfg(not(unix))]
signal: None,
}
} else {
let code = code.expect("Should have either an exit code or a signal.");
ChildStatus {
success: code == 0,
code,
signal: None,
}
};
Ok(status)
} }
} }
#[derive(Serialize)] #[derive(Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
// TODO(@AaronO): maybe find a more descriptive name or a convention for return structs pub struct SpawnOutput {
struct RunInfo { status: ChildStatus,
rid: ResourceId, stdout: Option<ZeroCopyBuf>,
pid: Option<u32>, stderr: Option<ZeroCopyBuf>,
stdin_rid: Option<ResourceId>,
stdout_rid: Option<ResourceId>,
stderr_rid: Option<ResourceId>,
} }
#[op] fn create_command(
fn op_run(state: &mut OpState, run_args: RunArgs) -> Result<RunInfo, AnyError> { state: &mut OpState,
let args = run_args.cmd; args: SpawnArgs,
api_name: &str,
) -> Result<std::process::Command, AnyError> {
state state
.borrow_mut::<PermissionsContainer>() .borrow_mut::<PermissionsContainer>()
.check_run(&args[0], "Deno.run()")?; .check_run(&args.cmd, api_name)?;
let env = run_args.env;
let cwd = run_args.cwd;
let mut c = Command::new(args.get(0).unwrap()); let mut command = std::process::Command::new(args.cmd);
(1..args.len()).for_each(|i| {
let arg = args.get(i).unwrap();
c.arg(arg);
});
cwd.map(|d| c.current_dir(d));
if run_args.clear_env { #[cfg(windows)]
super::check_unstable(state, "Deno.run.clearEnv"); if args.windows_raw_arguments {
c.env_clear(); for arg in args.args.iter() {
command.raw_arg(arg);
}
} else {
command.args(args.args);
} }
for (key, value) in &env {
c.env(key, value); #[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)] #[cfg(unix)]
if let Some(gid) = run_args.gid { if let Some(gid) = args.gid {
super::check_unstable(state, "Deno.run.gid"); command.gid(gid);
c.gid(gid);
} }
#[cfg(unix)] #[cfg(unix)]
if let Some(uid) = run_args.uid { if let Some(uid) = args.uid {
super::check_unstable(state, "Deno.run.uid"); command.uid(uid);
c.uid(uid);
} }
#[cfg(unix)] #[cfg(unix)]
// TODO(bartlomieju): // TODO(bartlomieju):
#[allow(clippy::undocumented_unsafe_blocks)] #[allow(clippy::undocumented_unsafe_blocks)]
unsafe { unsafe {
c.pre_exec(|| { command.pre_exec(|| {
libc::setgroups(0, std::ptr::null()); libc::setgroups(0, std::ptr::null());
Ok(()) Ok(())
}); });
} }
// TODO: make this work with other resources, eg. sockets command.stdin(args.stdio.stdin.as_stdio());
c.stdin(run_args.stdin.as_stdio(state)?); command.stdout(match args.stdio.stdout {
c.stdout( Stdio::Inherit => StdioOrRid::Rid(1).as_stdio(state)?,
match run_args.stdout { value => value.as_stdio(),
StdioOrRid::Stdio(Stdio::Inherit) => StdioOrRid::Rid(1), });
value => value, command.stderr(match args.stdio.stderr {
} Stdio::Inherit => StdioOrRid::Rid(2).as_stdio(state)?,
.as_stdio(state)?, value => value.as_stdio(),
); });
c.stderr(
match run_args.stderr {
StdioOrRid::Stdio(Stdio::Inherit) => StdioOrRid::Rid(2),
value => value,
}
.as_stdio(state)?,
);
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>,
}
fn spawn_child(
state: &mut OpState,
command: std::process::Command,
) -> Result<Child, AnyError> {
let mut command = tokio::process::Command::from(command);
// 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 // We want to kill child when it's closed
c.kill_on_drop(true); command.kill_on_drop(true);
// Spawn the command. let mut child = command.spawn()?;
let mut child = c.spawn()?; let pid = child.id().expect("Process ID should be set.");
let pid = child.id();
let stdin_rid = match child.stdin.take() { let stdin_rid = child
Some(child_stdin) => { .stdin
let rid = state .take()
.resource_table .map(|stdin| state.resource_table.add(ChildStdinResource::from(stdin)));
.add(ChildStdinResource::from(child_stdin));
Some(rid)
}
None => None,
};
let stdout_rid = match child.stdout.take() { let stdout_rid = child
Some(child_stdout) => { .stdout
let rid = state .take()
.resource_table .map(|stdout| state.resource_table.add(ChildStdoutResource::from(stdout)));
.add(ChildStdoutResource::from(child_stdout));
Some(rid)
}
None => None,
};
let stderr_rid = match child.stderr.take() { let stderr_rid = child
Some(child_stderr) => { .stderr
let rid = state .take()
.resource_table .map(|stderr| state.resource_table.add(ChildStderrResource::from(stderr)));
.add(ChildStderrResource::from(child_stderr));
Some(rid)
}
None => None,
};
let child_resource = ChildResource { let child_rid = state.resource_table.add(ChildResource(child));
child: AsyncRefCell::new(child),
};
let child_rid = state.resource_table.add(child_resource);
Ok(RunInfo { Ok(Child {
rid: child_rid, rid: child_rid,
pid, pid,
stdin_rid, stdin_rid,
@ -253,109 +311,325 @@ fn op_run(state: &mut OpState, run_args: RunArgs) -> Result<RunInfo, AnyError> {
}) })
} }
#[derive(Serialize)] #[op]
#[serde(rename_all = "camelCase")] fn op_spawn_child(
struct ProcessStatus { state: &mut OpState,
got_signal: bool, args: SpawnArgs,
exit_code: i32, api_name: String,
exit_signal: i32, ) -> Result<Child, AnyError> {
let command = create_command(state, args, &api_name)?;
spawn_child(state, command)
} }
#[op] #[op]
async fn op_run_status( async fn op_spawn_wait(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
rid: ResourceId, rid: ResourceId,
) -> Result<ProcessStatus, AnyError> { ) -> Result<ChildStatus, AnyError> {
let resource = state let resource = state
.borrow_mut() .borrow_mut()
.resource_table .resource_table
.get::<ChildResource>(rid)?; .take::<ChildResource>(rid)?;
let mut child = resource.borrow_mut().await; Rc::try_unwrap(resource)
let run_status = child.wait().await?; .ok()
let code = run_status.code(); .unwrap()
.0
.wait()
.await?
.try_into()
}
#[cfg(unix)] #[op]
let signal = run_status.signal(); fn op_spawn_sync(
#[cfg(not(unix))] state: &mut OpState,
let signal = None; args: SpawnArgs,
) -> Result<SpawnOutput, AnyError> {
let stdout = matches!(args.stdio.stdout, Stdio::Piped);
let stderr = matches!(args.stdio.stderr, Stdio::Piped);
let output =
create_command(state, args, "Deno.Command().outputSync()")?.output()?;
code Ok(SpawnOutput {
.or(signal) status: output.status.try_into()?,
.expect("Should have either an exit code or a signal."); stdout: if stdout {
let got_signal = signal.is_some(); Some(output.stdout.into())
} else {
Ok(ProcessStatus { None
got_signal, },
exit_code: code.unwrap_or(-1), stderr: if stderr {
exit_signal: signal.unwrap_or(-1), Some(output.stderr.into())
} else {
None
},
}) })
} }
#[cfg(unix)] mod deprecated {
pub fn kill(pid: i32, signal: &str) -> Result<(), AnyError> { use super::*;
let signo = super::signal::signal_str_to_int(signal)?;
use nix::sys::signal::kill as unix_kill;
use nix::sys::signal::Signal;
use nix::unistd::Pid;
let sig = Signal::try_from(signo)?;
unix_kill(Pid::from_raw(pid), Option::Some(sig)).map_err(AnyError::from)
}
#[cfg(not(unix))] #[derive(Deserialize)]
pub fn kill(pid: i32, signal: &str) -> Result<(), AnyError> { #[serde(rename_all = "camelCase")]
use deno_core::error::type_error; pub struct RunArgs {
use std::io::Error; cmd: Vec<String>,
use std::io::ErrorKind::NotFound; cwd: Option<String>,
use winapi::shared::minwindef::DWORD; clear_env: bool,
use winapi::shared::minwindef::FALSE; env: Vec<(String, String)>,
use winapi::shared::minwindef::TRUE; #[cfg(unix)]
use winapi::shared::winerror::ERROR_INVALID_PARAMETER; gid: Option<u32>,
use winapi::um::errhandlingapi::GetLastError; #[cfg(unix)]
use winapi::um::handleapi::CloseHandle; uid: Option<u32>,
use winapi::um::processthreadsapi::OpenProcess; stdin: StdioOrRid,
use winapi::um::processthreadsapi::TerminateProcess; stdout: StdioOrRid,
use winapi::um::winnt::PROCESS_TERMINATE; stderr: StdioOrRid,
}
if !matches!(signal, "SIGKILL" | "SIGTERM") { struct ChildResource {
Err(type_error(format!("Invalid signal: {signal}"))) child: AsyncRefCell<tokio::process::Child>,
} else if pid <= 0 { }
Err(type_error("Invalid pid"))
} else {
// SAFETY: winapi call
let handle = unsafe { OpenProcess(PROCESS_TERMINATE, FALSE, pid as DWORD) };
if handle.is_null() { impl Resource for ChildResource {
// SAFETY: winapi call fn name(&self) -> Cow<str> {
let err = match unsafe { GetLastError() } { "child".into()
ERROR_INVALID_PARAMETER => Error::from(NotFound), // Invalid `pid`. }
errno => Error::from_raw_os_error(errno as i32), }
};
Err(err.into()) impl ChildResource {
fn borrow_mut(self: Rc<Self>) -> AsyncMutFuture<tokio::process::Child> {
RcRef::map(self, |r| &r.child).borrow_mut()
}
}
#[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>,
}
#[op]
fn op_run(
state: &mut OpState,
run_args: RunArgs,
) -> Result<RunInfo, AnyError> {
let args = run_args.cmd;
state
.borrow_mut::<PermissionsContainer>()
.check_run(&args[0], "Deno.run()")?;
let env = run_args.env;
let cwd = run_args.cwd;
let mut c = Command::new(args.get(0).unwrap());
(1..args.len()).for_each(|i| {
let arg = args.get(i).unwrap();
c.arg(arg);
});
cwd.map(|d| c.current_dir(d));
if run_args.clear_env {
super::check_unstable(state, "Deno.run.clearEnv");
c.env_clear();
}
for (key, value) in &env {
c.env(key, value);
}
#[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)]
// TODO(bartlomieju):
#[allow(clippy::undocumented_unsafe_blocks)]
unsafe {
c.pre_exec(|| {
libc::setgroups(0, std::ptr::null());
Ok(())
});
}
// TODO: make this work with other resources, eg. sockets
c.stdin(run_args.stdin.as_stdio(state)?);
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)?,
);
// We want to kill child when it's closed
c.kill_on_drop(true);
// Spawn the command.
let mut child = c.spawn()?;
let pid = child.id();
let stdin_rid = match child.stdin.take() {
Some(child_stdin) => {
let rid = state
.resource_table
.add(ChildStdinResource::from(child_stdin));
Some(rid)
}
None => None,
};
let stdout_rid = match child.stdout.take() {
Some(child_stdout) => {
let rid = state
.resource_table
.add(ChildStdoutResource::from(child_stdout));
Some(rid)
}
None => None,
};
let stderr_rid = match child.stderr.take() {
Some(child_stderr) => {
let rid = state
.resource_table
.add(ChildStderrResource::from(child_stderr));
Some(rid)
}
None => None,
};
let child_resource = ChildResource {
child: AsyncRefCell::new(child),
};
let child_rid = state.resource_table.add(child_resource);
Ok(RunInfo {
rid: child_rid,
pid,
stdin_rid,
stdout_rid,
stderr_rid,
})
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct ProcessStatus {
got_signal: bool,
exit_code: i32,
exit_signal: i32,
}
#[op]
async fn op_run_status(
state: Rc<RefCell<OpState>>,
rid: ResourceId,
) -> Result<ProcessStatus, AnyError> {
let resource = state
.borrow_mut()
.resource_table
.get::<ChildResource>(rid)?;
let mut child = resource.borrow_mut().await;
let run_status = child.wait().await?;
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();
Ok(ProcessStatus {
got_signal,
exit_code: code.unwrap_or(-1),
exit_signal: signal.unwrap_or(-1),
})
}
#[cfg(unix)]
pub fn kill(pid: i32, signal: &str) -> Result<(), AnyError> {
let signo = super::signal::signal_str_to_int(signal)?;
use nix::sys::signal::kill as unix_kill;
use nix::sys::signal::Signal;
use nix::unistd::Pid;
let sig = Signal::try_from(signo)?;
unix_kill(Pid::from_raw(pid), Option::Some(sig)).map_err(AnyError::from)
}
#[cfg(not(unix))]
pub fn kill(pid: i32, signal: &str) -> Result<(), AnyError> {
use deno_core::error::type_error;
use std::io::Error;
use std::io::ErrorKind::NotFound;
use winapi::shared::minwindef::DWORD;
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;
if !matches!(signal, "SIGKILL" | "SIGTERM") {
Err(type_error(format!("Invalid signal: {signal}")))
} else if pid <= 0 {
Err(type_error("Invalid pid"))
} else { } else {
// SAFETY: winapi calls // SAFETY: winapi call
unsafe { let handle =
let is_terminated = TerminateProcess(handle, 1); unsafe { OpenProcess(PROCESS_TERMINATE, FALSE, pid as DWORD) };
CloseHandle(handle);
match is_terminated { if handle.is_null() {
FALSE => Err(Error::last_os_error().into()), // SAFETY: winapi call
TRUE => Ok(()), let err = match unsafe { GetLastError() } {
_ => unreachable!(), ERROR_INVALID_PARAMETER => Error::from(NotFound), // Invalid `pid`.
errno => Error::from_raw_os_error(errno as i32),
};
Err(err.into())
} else {
// SAFETY: winapi calls
unsafe {
let is_terminated = TerminateProcess(handle, 1);
CloseHandle(handle);
match is_terminated {
FALSE => Err(Error::last_os_error().into()),
TRUE => Ok(()),
_ => unreachable!(),
}
} }
} }
} }
} }
}
#[op] #[op]
fn op_kill( fn op_kill(
state: &mut OpState, state: &mut OpState,
pid: i32, pid: i32,
signal: String, signal: String,
api_name: String, api_name: String,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
state state
.borrow_mut::<PermissionsContainer>() .borrow_mut::<PermissionsContainer>()
.check_run_all(&api_name)?; .check_run_all(&api_name)?;
kill(pid, &signal)?; kill(pid, &signal)?;
Ok(()) Ok(())
}
} }

View file

@ -1,289 +0,0 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use super::process::Stdio;
use super::process::StdioOrRid;
use crate::permissions::PermissionsContainer;
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 deno_io::ChildStderrResource;
use deno_io::ChildStdinResource;
use deno_io::ChildStdoutResource;
use serde::Deserialize;
use serde::Serialize;
use std::borrow::Cow;
use std::cell::RefCell;
#[cfg(windows)]
use std::os::windows::process::CommandExt;
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("deno_spawn")
.ops(vec![
op_spawn_child::decl(),
op_spawn_wait::decl(),
op_spawn_sync::decl(),
])
.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>,
#[cfg(windows)]
windows_raw_arguments: bool,
#[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,
signal: Option<String>,
}
impl TryFrom<ExitStatus> for ChildStatus {
type Error = AnyError;
fn try_from(status: ExitStatus) -> Result<Self, Self::Error> {
let code = status.code();
#[cfg(unix)]
let signal = status.signal();
#[cfg(not(unix))]
let signal: Option<i32> = None;
let status = if let Some(signal) = signal {
ChildStatus {
success: false,
code: 128 + signal,
#[cfg(unix)]
signal: Some(
crate::ops::signal::signal_int_to_str(signal)?.to_string(),
),
#[cfg(not(unix))]
signal: None,
}
} else {
let code = code.expect("Should have either an exit code or a signal.");
ChildStatus {
success: code == 0,
code,
signal: None,
}
};
Ok(status)
}
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SpawnOutput {
status: ChildStatus,
stdout: Option<ZeroCopyBuf>,
stderr: Option<ZeroCopyBuf>,
}
fn create_command(
state: &mut OpState,
args: SpawnArgs,
api_name: &str,
) -> Result<std::process::Command, AnyError> {
state
.borrow_mut::<PermissionsContainer>()
.check_run(&args.cmd, 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)
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct Child {
rid: ResourceId,
pid: u32,
stdin_rid: Option<ResourceId>,
stdout_rid: Option<ResourceId>,
stderr_rid: Option<ResourceId>,
}
fn spawn_child(
state: &mut OpState,
command: std::process::Command,
) -> Result<Child, AnyError> {
let mut command = tokio::process::Command::from(command);
// 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,
})
}
#[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]
async fn op_spawn_wait(
state: Rc<RefCell<OpState>>,
rid: ResourceId,
) -> Result<ChildStatus, AnyError> {
let resource = state
.borrow_mut()
.resource_table
.take::<ChildResource>(rid)?;
Rc::try_unwrap(resource)
.ok()
.unwrap()
.0
.wait()
.await?
.try_into()
}
#[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);
let output =
create_command(state, args, "Deno.Command().outputSync()")?.output()?;
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
},
})
}

View file

@ -438,8 +438,7 @@ impl WebWorker {
deno_node::init::<PermissionsContainer>(options.npm_resolver), deno_node::init::<PermissionsContainer>(options.npm_resolver),
ops::os::init_for_worker(), ops::os::init_for_worker(),
ops::permissions::init(), ops::permissions::init(),
ops::process::init(), ops::process::init_ops(),
ops::spawn::init(),
ops::signal::init(), ops::signal::init(),
ops::tty::init(), ops::tty::init(),
deno_http::init(), deno_http::init(),

View file

@ -254,7 +254,6 @@ impl MainWorker {
options.web_worker_pre_execute_module_cb.clone(), options.web_worker_pre_execute_module_cb.clone(),
options.format_js_error_fn.clone(), options.format_js_error_fn.clone(),
), ),
ops::spawn::init(),
ops::fs_events::init(), ops::fs_events::init(),
ops::fs::init::<PermissionsContainer>(), ops::fs::init::<PermissionsContainer>(),
deno_io::init(options.stdio), deno_io::init(options.stdio),
@ -269,7 +268,7 @@ impl MainWorker {
deno_node::init::<PermissionsContainer>(options.npm_resolver), deno_node::init::<PermissionsContainer>(options.npm_resolver),
ops::os::init(exit_code.clone()), ops::os::init(exit_code.clone()),
ops::permissions::init(), ops::permissions::init(),
ops::process::init(), ops::process::init_ops(),
ops::signal::init(), ops::signal::init(),
ops::tty::init(), ops::tty::init(),
deno_http::init(), deno_http::init(),