2019-01-21 14:03:30 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-08-24 16:20:48 -04:00
|
|
|
import { sendSync, sendAsync, msg, flatbuffers } from "./dispatch_flatbuffers";
|
|
|
|
|
2018-11-30 13:44:05 -05:00
|
|
|
import { File, close } from "./files";
|
2018-11-15 23:07:40 -05:00
|
|
|
import { ReadCloser, WriteCloser } from "./io";
|
2018-11-30 13:44:05 -05:00
|
|
|
import { readAll } from "./buffer";
|
|
|
|
import { assert, unreachable } from "./util";
|
2019-04-21 21:26:56 -04:00
|
|
|
import { platform } from "./build";
|
2018-11-15 23:07:40 -05:00
|
|
|
|
2019-02-21 15:52:35 -05:00
|
|
|
/** How to handle subprocess stdio.
|
2018-11-15 23:07:40 -05:00
|
|
|
*
|
|
|
|
* "inherit" The default if unspecified. The child inherits from the
|
|
|
|
* corresponding parent descriptor.
|
|
|
|
*
|
|
|
|
* "piped" A new pipe should be arranged to connect the parent and child
|
|
|
|
* subprocesses.
|
|
|
|
*
|
|
|
|
* "null" This stream will be ignored. This is the equivalent of attaching the
|
|
|
|
* stream to /dev/null.
|
|
|
|
*/
|
|
|
|
export type ProcessStdio = "inherit" | "piped" | "null";
|
|
|
|
|
|
|
|
// TODO Maybe extend VSCode's 'CommandOptions'?
|
|
|
|
// See https://code.visualstudio.com/docs/editor/tasks-appendix#_schema-for-tasksjson
|
|
|
|
export interface RunOptions {
|
|
|
|
args: string[];
|
|
|
|
cwd?: string;
|
2019-02-15 10:37:04 -05:00
|
|
|
env?: { [key: string]: string };
|
2019-06-21 19:00:14 -04:00
|
|
|
stdout?: ProcessStdio | number;
|
|
|
|
stderr?: ProcessStdio | number;
|
|
|
|
stdin?: ProcessStdio | number;
|
2018-11-15 23:07:40 -05:00
|
|
|
}
|
|
|
|
|
2019-08-24 11:31:14 -04:00
|
|
|
async function runStatus(rid: number): Promise<ProcessStatus> {
|
2019-08-24 16:20:48 -04:00
|
|
|
const builder = flatbuffers.createBuilder();
|
|
|
|
const inner = msg.RunStatus.createRunStatus(builder, rid);
|
|
|
|
|
|
|
|
const baseRes = await sendAsync(builder, msg.Any.RunStatus, inner);
|
|
|
|
assert(baseRes != null);
|
|
|
|
assert(msg.Any.RunStatusRes === baseRes!.innerType());
|
|
|
|
const res = new msg.RunStatusRes();
|
|
|
|
assert(baseRes!.inner(res) != null);
|
2019-03-09 12:30:38 -05:00
|
|
|
|
2019-08-24 16:20:48 -04:00
|
|
|
if (res.gotSignal()) {
|
|
|
|
const signal = res.exitSignal();
|
2019-03-09 12:30:38 -05:00
|
|
|
return { signal, success: false };
|
|
|
|
} else {
|
2019-08-24 16:20:48 -04:00
|
|
|
const code = res.exitCode();
|
2019-03-09 12:30:38 -05:00
|
|
|
return { code, success: code === 0 };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-21 21:26:56 -04:00
|
|
|
/** Send a signal to process under given PID. Unix only at this moment.
|
|
|
|
* If pid is negative, the signal will be sent to the process group identified
|
|
|
|
* by -pid.
|
2019-08-06 01:45:36 -04:00
|
|
|
* Requires the `--allow-run` flag.
|
2019-04-21 21:26:56 -04:00
|
|
|
*/
|
|
|
|
export function kill(pid: number, signo: number): void {
|
2019-08-24 16:20:48 -04:00
|
|
|
const builder = flatbuffers.createBuilder();
|
|
|
|
const inner = msg.Kill.createKill(builder, pid, signo);
|
|
|
|
sendSync(builder, msg.Any.Kill, inner);
|
2019-04-21 21:26:56 -04:00
|
|
|
}
|
|
|
|
|
2018-11-15 23:07:40 -05:00
|
|
|
export class Process {
|
|
|
|
readonly rid: number;
|
|
|
|
readonly pid: number;
|
|
|
|
readonly stdin?: WriteCloser;
|
|
|
|
readonly stdout?: ReadCloser;
|
|
|
|
readonly stderr?: ReadCloser;
|
|
|
|
|
|
|
|
// @internal
|
2019-08-24 16:20:48 -04:00
|
|
|
constructor(res: msg.RunRes) {
|
|
|
|
this.rid = res.rid();
|
|
|
|
this.pid = res.pid();
|
2018-11-15 23:07:40 -05:00
|
|
|
|
2019-08-24 16:20:48 -04:00
|
|
|
if (res.stdinRid() > 0) {
|
|
|
|
this.stdin = new File(res.stdinRid());
|
2018-11-15 23:07:40 -05:00
|
|
|
}
|
|
|
|
|
2019-08-24 16:20:48 -04:00
|
|
|
if (res.stdoutRid() > 0) {
|
|
|
|
this.stdout = new File(res.stdoutRid());
|
2018-11-15 23:07:40 -05:00
|
|
|
}
|
|
|
|
|
2019-08-24 16:20:48 -04:00
|
|
|
if (res.stderrRid() > 0) {
|
|
|
|
this.stderr = new File(res.stderrRid());
|
2018-11-15 23:07:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async status(): Promise<ProcessStatus> {
|
|
|
|
return await runStatus(this.rid);
|
|
|
|
}
|
|
|
|
|
2018-11-30 13:44:05 -05:00
|
|
|
/** Buffer the stdout and return it as Uint8Array after EOF.
|
2019-03-28 16:09:46 -04:00
|
|
|
* You must set stdout to "piped" when creating the process.
|
2018-11-30 13:44:05 -05:00
|
|
|
* This calls close() on stdout after its done.
|
|
|
|
*/
|
|
|
|
async output(): Promise<Uint8Array> {
|
|
|
|
if (!this.stdout) {
|
|
|
|
throw new Error("Process.output: stdout is undefined");
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
return await readAll(this.stdout);
|
|
|
|
} finally {
|
|
|
|
this.stdout.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-28 16:09:46 -04:00
|
|
|
/** Buffer the stderr and return it as Uint8Array after EOF.
|
|
|
|
* You must set stderr to "piped" when creating the process.
|
|
|
|
* This calls close() on stderr after its done.
|
|
|
|
*/
|
|
|
|
async stderrOutput(): Promise<Uint8Array> {
|
|
|
|
if (!this.stderr) {
|
|
|
|
throw new Error("Process.stderrOutput: stderr is undefined");
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
return await readAll(this.stderr);
|
|
|
|
} finally {
|
|
|
|
this.stderr.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-15 23:07:40 -05:00
|
|
|
close(): void {
|
|
|
|
close(this.rid);
|
|
|
|
}
|
2019-04-21 21:26:56 -04:00
|
|
|
|
|
|
|
kill(signo: number): void {
|
|
|
|
kill(this.pid, signo);
|
|
|
|
}
|
2018-11-15 23:07:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface ProcessStatus {
|
|
|
|
success: boolean;
|
|
|
|
code?: number;
|
|
|
|
signal?: number; // TODO: Make this a string, e.g. 'SIGTERM'.
|
|
|
|
}
|
|
|
|
|
2019-08-24 16:20:48 -04:00
|
|
|
function stdioMap(s: ProcessStdio): msg.ProcessStdio {
|
2018-11-15 23:07:40 -05:00
|
|
|
switch (s) {
|
|
|
|
case "inherit":
|
2019-08-24 16:20:48 -04:00
|
|
|
return msg.ProcessStdio.Inherit;
|
2018-11-15 23:07:40 -05:00
|
|
|
case "piped":
|
2019-08-24 16:20:48 -04:00
|
|
|
return msg.ProcessStdio.Piped;
|
2018-11-15 23:07:40 -05:00
|
|
|
case "null":
|
2019-08-24 16:20:48 -04:00
|
|
|
return msg.ProcessStdio.Null;
|
2018-11-15 23:07:40 -05:00
|
|
|
default:
|
|
|
|
return unreachable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-21 19:00:14 -04:00
|
|
|
function isRid(arg: unknown): arg is number {
|
|
|
|
return !isNaN(arg as number);
|
|
|
|
}
|
|
|
|
|
2019-02-21 15:52:35 -05:00
|
|
|
/**
|
|
|
|
* Spawns new subprocess.
|
|
|
|
*
|
|
|
|
* Subprocess uses same working directory as parent process unless `opt.cwd`
|
|
|
|
* is specified.
|
|
|
|
*
|
|
|
|
* Environmental variables for subprocess can be specified using `opt.env`
|
|
|
|
* mapping.
|
|
|
|
*
|
|
|
|
* By default subprocess inherits stdio of parent process. To change that
|
2019-06-21 19:00:14 -04:00
|
|
|
* `opt.stdout`, `opt.stderr` and `opt.stdin` can be specified independently -
|
|
|
|
* they can be set to either `ProcessStdio` or `rid` of open file.
|
2019-02-21 15:52:35 -05:00
|
|
|
*/
|
2018-11-15 23:07:40 -05:00
|
|
|
export function run(opt: RunOptions): Process {
|
2019-08-24 16:20:48 -04:00
|
|
|
const builder = flatbuffers.createBuilder();
|
|
|
|
const argsOffset = msg.Run.createArgsVector(
|
|
|
|
builder,
|
|
|
|
opt.args.map((a): number => builder.createString(a))
|
|
|
|
);
|
|
|
|
const cwdOffset = opt.cwd == null ? 0 : builder.createString(opt.cwd);
|
|
|
|
const kvOffset: flatbuffers.Offset[] = [];
|
2019-02-15 10:37:04 -05:00
|
|
|
if (opt.env) {
|
2019-08-24 16:20:48 -04:00
|
|
|
for (const [key, val] of Object.entries(opt.env)) {
|
|
|
|
const keyOffset = builder.createString(key);
|
|
|
|
const valOffset = builder.createString(String(val));
|
|
|
|
kvOffset.push(msg.KeyValue.createKeyValue(builder, keyOffset, valOffset));
|
|
|
|
}
|
2019-02-15 10:37:04 -05:00
|
|
|
}
|
2019-08-24 16:20:48 -04:00
|
|
|
const envOffset = msg.Run.createEnvVector(builder, kvOffset);
|
2019-06-21 19:00:14 -04:00
|
|
|
|
2019-08-24 16:20:48 -04:00
|
|
|
let stdInOffset = stdioMap("inherit");
|
|
|
|
let stdOutOffset = stdioMap("inherit");
|
|
|
|
let stdErrOffset = stdioMap("inherit");
|
|
|
|
let stdinRidOffset = 0;
|
|
|
|
let stdoutRidOffset = 0;
|
|
|
|
let stderrRidOffset = 0;
|
2019-06-21 19:00:14 -04:00
|
|
|
|
|
|
|
if (opt.stdin) {
|
|
|
|
if (isRid(opt.stdin)) {
|
2019-08-24 16:20:48 -04:00
|
|
|
stdinRidOffset = opt.stdin;
|
2019-06-21 19:00:14 -04:00
|
|
|
} else {
|
2019-08-24 16:20:48 -04:00
|
|
|
stdInOffset = stdioMap(opt.stdin);
|
2019-06-21 19:00:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opt.stdout) {
|
|
|
|
if (isRid(opt.stdout)) {
|
2019-08-24 16:20:48 -04:00
|
|
|
stdoutRidOffset = opt.stdout;
|
2019-06-21 19:00:14 -04:00
|
|
|
} else {
|
2019-08-24 16:20:48 -04:00
|
|
|
stdOutOffset = stdioMap(opt.stdout);
|
2019-06-21 19:00:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opt.stderr) {
|
|
|
|
if (isRid(opt.stderr)) {
|
2019-08-24 16:20:48 -04:00
|
|
|
stderrRidOffset = opt.stderr;
|
2019-06-21 19:00:14 -04:00
|
|
|
} else {
|
2019-08-24 16:20:48 -04:00
|
|
|
stdErrOffset = stdioMap(opt.stderr);
|
2019-06-21 19:00:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-24 16:20:48 -04:00
|
|
|
const inner = msg.Run.createRun(
|
|
|
|
builder,
|
|
|
|
argsOffset,
|
|
|
|
cwdOffset,
|
|
|
|
envOffset,
|
|
|
|
stdInOffset,
|
|
|
|
stdOutOffset,
|
|
|
|
stdErrOffset,
|
|
|
|
stdinRidOffset,
|
|
|
|
stdoutRidOffset,
|
|
|
|
stderrRidOffset
|
|
|
|
);
|
|
|
|
const baseRes = sendSync(builder, msg.Any.Run, inner);
|
|
|
|
assert(baseRes != null);
|
|
|
|
assert(msg.Any.RunRes === baseRes!.innerType());
|
|
|
|
const res = new msg.RunRes();
|
|
|
|
assert(baseRes!.inner(res) != null);
|
|
|
|
|
2018-11-15 23:07:40 -05:00
|
|
|
return new Process(res);
|
|
|
|
}
|
2019-04-21 21:26:56 -04:00
|
|
|
|
|
|
|
// From `kill -l`
|
|
|
|
enum LinuxSignal {
|
|
|
|
SIGHUP = 1,
|
|
|
|
SIGINT = 2,
|
|
|
|
SIGQUIT = 3,
|
|
|
|
SIGILL = 4,
|
|
|
|
SIGTRAP = 5,
|
|
|
|
SIGABRT = 6,
|
|
|
|
SIGBUS = 7,
|
|
|
|
SIGFPE = 8,
|
|
|
|
SIGKILL = 9,
|
|
|
|
SIGUSR1 = 10,
|
|
|
|
SIGSEGV = 11,
|
|
|
|
SIGUSR2 = 12,
|
|
|
|
SIGPIPE = 13,
|
|
|
|
SIGALRM = 14,
|
|
|
|
SIGTERM = 15,
|
|
|
|
SIGSTKFLT = 16,
|
|
|
|
SIGCHLD = 17,
|
|
|
|
SIGCONT = 18,
|
|
|
|
SIGSTOP = 19,
|
|
|
|
SIGTSTP = 20,
|
|
|
|
SIGTTIN = 21,
|
|
|
|
SIGTTOU = 22,
|
|
|
|
SIGURG = 23,
|
|
|
|
SIGXCPU = 24,
|
|
|
|
SIGXFSZ = 25,
|
|
|
|
SIGVTALRM = 26,
|
|
|
|
SIGPROF = 27,
|
|
|
|
SIGWINCH = 28,
|
|
|
|
SIGIO = 29,
|
|
|
|
SIGPWR = 30,
|
|
|
|
SIGSYS = 31
|
|
|
|
}
|
|
|
|
|
|
|
|
// From `kill -l`
|
|
|
|
enum MacOSSignal {
|
|
|
|
SIGHUP = 1,
|
|
|
|
SIGINT = 2,
|
|
|
|
SIGQUIT = 3,
|
|
|
|
SIGILL = 4,
|
|
|
|
SIGTRAP = 5,
|
|
|
|
SIGABRT = 6,
|
|
|
|
SIGEMT = 7,
|
|
|
|
SIGFPE = 8,
|
|
|
|
SIGKILL = 9,
|
|
|
|
SIGBUS = 10,
|
|
|
|
SIGSEGV = 11,
|
|
|
|
SIGSYS = 12,
|
|
|
|
SIGPIPE = 13,
|
|
|
|
SIGALRM = 14,
|
|
|
|
SIGTERM = 15,
|
|
|
|
SIGURG = 16,
|
|
|
|
SIGSTOP = 17,
|
|
|
|
SIGTSTP = 18,
|
|
|
|
SIGCONT = 19,
|
|
|
|
SIGCHLD = 20,
|
|
|
|
SIGTTIN = 21,
|
|
|
|
SIGTTOU = 22,
|
|
|
|
SIGIO = 23,
|
|
|
|
SIGXCPU = 24,
|
|
|
|
SIGXFSZ = 25,
|
|
|
|
SIGVTALRM = 26,
|
|
|
|
SIGPROF = 27,
|
|
|
|
SIGWINCH = 28,
|
|
|
|
SIGINFO = 29,
|
|
|
|
SIGUSR1 = 30,
|
|
|
|
SIGUSR2 = 31
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Signals numbers. This is platform dependent.
|
|
|
|
*/
|
|
|
|
export const Signal = platform.os === "mac" ? MacOSSignal : LinuxSignal;
|