2019-05-05 05:27:57 +00:00
|
|
|
/**
|
2020-05-18 14:42:28 +00:00
|
|
|
Functions and structures for dealing with subprocesses and pipes.
|
2019-05-05 05:27:57 +00:00
|
|
|
|
2020-05-18 14:42:28 +00:00
|
|
|
This module is modeled after std.process, but provides a fiber-aware
|
|
|
|
alternative to it. All blocking operations will yield the calling fiber
|
|
|
|
instead of blocking it.
|
2019-05-05 05:27:57 +00:00
|
|
|
*/
|
|
|
|
module vibe.core.process;
|
|
|
|
|
|
|
|
public import std.process : Pid, Redirect;
|
|
|
|
static import std.process;
|
|
|
|
|
|
|
|
import core.time;
|
|
|
|
import std.array;
|
|
|
|
import std.typecons;
|
|
|
|
import std.exception : enforce;
|
|
|
|
import std.algorithm;
|
|
|
|
import eventcore.core;
|
|
|
|
import vibe.core.path;
|
|
|
|
import vibe.core.log;
|
|
|
|
import vibe.core.stream;
|
|
|
|
import vibe.internal.async;
|
|
|
|
import vibe.internal.array : BatchBuffer;
|
|
|
|
import vibe.core.internal.release;
|
|
|
|
|
|
|
|
@safe:
|
|
|
|
|
|
|
|
/**
|
2020-05-18 14:42:28 +00:00
|
|
|
Register a process with vibe for fibre-aware handling. This process can be
|
|
|
|
started from anywhere including external libraries or std.process.
|
2019-05-05 05:27:57 +00:00
|
|
|
|
2020-05-18 14:42:28 +00:00
|
|
|
Params:
|
|
|
|
pid = A Pid or OS process id
|
2019-05-05 05:27:57 +00:00
|
|
|
*/
|
2019-06-04 00:13:00 +00:00
|
|
|
Process adoptProcessID(Pid pid)
|
2019-05-05 05:27:57 +00:00
|
|
|
@trusted {
|
2020-05-18 14:42:28 +00:00
|
|
|
return adoptProcessID(pid.processID);
|
2019-05-05 05:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ditto
|
2019-06-04 00:13:00 +00:00
|
|
|
Process adoptProcessID(int pid)
|
2019-05-05 05:27:57 +00:00
|
|
|
{
|
2020-05-18 14:44:11 +00:00
|
|
|
auto p = eventDriver.processes.adopt(pid);
|
|
|
|
if (p == ProcessID.invalid)
|
|
|
|
throw new Exception("Failed to adopt process ID");
|
|
|
|
return Process(p);
|
2019-05-05 05:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-18 14:42:28 +00:00
|
|
|
Path to the user's preferred command interpreter.
|
2019-05-05 05:27:57 +00:00
|
|
|
|
2020-05-18 14:42:28 +00:00
|
|
|
See_Also: `nativeShell`
|
2019-05-05 05:27:57 +00:00
|
|
|
*/
|
|
|
|
@property NativePath userShell() { return NativePath(std.process.userShell); }
|
|
|
|
|
|
|
|
/**
|
2020-05-18 14:42:28 +00:00
|
|
|
The platform specific native shell path.
|
2019-05-05 05:27:57 +00:00
|
|
|
|
2020-05-18 14:42:28 +00:00
|
|
|
See_Also: `userShell`
|
2019-05-05 05:27:57 +00:00
|
|
|
*/
|
|
|
|
const NativePath nativeShell = NativePath(std.process.nativeShell);
|
|
|
|
|
|
|
|
/**
|
2020-05-18 14:42:28 +00:00
|
|
|
Equivalent to `std.process.Config` except with less flag support
|
2019-05-05 05:27:57 +00:00
|
|
|
*/
|
|
|
|
enum Config {
|
2020-05-18 14:42:28 +00:00
|
|
|
none = ProcessConfig.none,
|
|
|
|
newEnv = ProcessConfig.newEnv,
|
|
|
|
suppressConsole = ProcessConfig.suppressConsole,
|
|
|
|
detached = ProcessConfig.detached,
|
2019-05-05 05:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-18 14:42:28 +00:00
|
|
|
Equivalent to `std.process.spawnProcess`.
|
2019-05-05 05:27:57 +00:00
|
|
|
|
2020-05-18 14:42:28 +00:00
|
|
|
Returns:
|
|
|
|
A reference to the running process.
|
2019-05-05 05:27:57 +00:00
|
|
|
|
2020-05-18 14:42:28 +00:00
|
|
|
See_Also: `pipeProcess`, `execute`
|
2019-05-05 05:27:57 +00:00
|
|
|
*/
|
|
|
|
Process spawnProcess(
|
2020-05-18 14:42:28 +00:00
|
|
|
scope string[] args,
|
|
|
|
const string[string] env = null,
|
|
|
|
Config config = Config.none,
|
|
|
|
scope NativePath workDir = NativePath.init)
|
2019-05-05 05:27:57 +00:00
|
|
|
@trusted {
|
2020-05-18 14:44:11 +00:00
|
|
|
auto process = eventDriver.processes.spawn(
|
2020-05-18 14:42:28 +00:00
|
|
|
args,
|
|
|
|
ProcessStdinFile(ProcessRedirect.inherit),
|
|
|
|
ProcessStdoutFile(ProcessRedirect.inherit),
|
|
|
|
ProcessStderrFile(ProcessRedirect.inherit),
|
|
|
|
env,
|
|
|
|
config,
|
2020-05-18 14:44:11 +00:00
|
|
|
workDir.toNativeString());
|
|
|
|
|
|
|
|
if (process.pid == ProcessID.invalid)
|
|
|
|
throw new Exception("Failed to spawn process");
|
|
|
|
|
|
|
|
return Process(process.pid);
|
2019-05-05 05:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ditto
|
|
|
|
Process spawnProcess(
|
2020-05-18 14:42:28 +00:00
|
|
|
scope string program,
|
|
|
|
const string[string] env = null,
|
|
|
|
Config config = Config.none,
|
|
|
|
scope NativePath workDir = NativePath.init)
|
2019-05-05 05:27:57 +00:00
|
|
|
{
|
2020-05-18 14:42:28 +00:00
|
|
|
return spawnProcess(
|
|
|
|
[program],
|
|
|
|
env,
|
|
|
|
config,
|
|
|
|
workDir
|
|
|
|
);
|
2019-05-05 05:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ditto
|
|
|
|
Process spawnShell(
|
2020-05-18 14:42:28 +00:00
|
|
|
scope string command,
|
|
|
|
const string[string] env = null,
|
|
|
|
Config config = Config.none,
|
|
|
|
scope NativePath workDir = NativePath.init,
|
|
|
|
scope NativePath shellPath = nativeShell)
|
2019-05-05 05:27:57 +00:00
|
|
|
{
|
2020-05-18 14:42:28 +00:00
|
|
|
return spawnProcess(
|
|
|
|
shellCommand(command, shellPath),
|
|
|
|
env,
|
|
|
|
config,
|
|
|
|
workDir);
|
2019-05-05 05:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private string[] shellCommand(string command, NativePath shellPath)
|
|
|
|
{
|
2020-05-18 14:42:28 +00:00
|
|
|
version (Windows)
|
|
|
|
{
|
|
|
|
// CMD does not parse its arguments like other programs.
|
|
|
|
// It does not use CommandLineToArgvW.
|
|
|
|
// Instead, it treats the first and last quote specially.
|
|
|
|
// See CMD.EXE /? for details.
|
|
|
|
return [
|
|
|
|
std.process.escapeShellFileName(shellPath.toNativeString())
|
|
|
|
~ ` /C "` ~ command ~ `"`
|
|
|
|
];
|
|
|
|
}
|
|
|
|
else version (Posix)
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
shellPath.toNativeString(),
|
|
|
|
"-c",
|
|
|
|
command,
|
|
|
|
];
|
|
|
|
}
|
2019-05-05 05:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-18 14:42:28 +00:00
|
|
|
Represents a running process.
|
2019-05-05 05:27:57 +00:00
|
|
|
*/
|
|
|
|
struct Process {
|
2020-05-18 14:42:28 +00:00
|
|
|
private static struct Context {
|
|
|
|
//Duration waitTimeout;
|
|
|
|
shared(NativeEventDriver) driver;
|
|
|
|
}
|
|
|
|
|
|
|
|
private {
|
|
|
|
ProcessID m_pid;
|
|
|
|
Context* m_context;
|
|
|
|
}
|
|
|
|
|
|
|
|
private this(ProcessID p)
|
|
|
|
nothrow {
|
|
|
|
assert(p != ProcessID.invalid);
|
|
|
|
m_pid = p;
|
|
|
|
m_context = () @trusted { return &eventDriver.processes.userData!Context(p); } ();
|
|
|
|
m_context.driver = () @trusted { return cast(shared)eventDriver; } ();
|
|
|
|
}
|
|
|
|
|
|
|
|
this(this)
|
|
|
|
nothrow {
|
|
|
|
if (m_pid != ProcessID.invalid)
|
|
|
|
eventDriver.processes.addRef(m_pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
~this()
|
|
|
|
nothrow {
|
|
|
|
if (m_pid != ProcessID.invalid)
|
|
|
|
releaseHandle!"processes"(m_pid, m_context.driver);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Check whether this is a valid process handle. The process may have
|
|
|
|
exited already.
|
|
|
|
*/
|
|
|
|
bool opCast(T)() const nothrow if (is(T == bool)) { return m_pid != ProcessID.invalid; }
|
|
|
|
|
|
|
|
///
|
|
|
|
unittest {
|
|
|
|
Process p;
|
|
|
|
|
|
|
|
assert(!p);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
An operating system handle to the process.
|
|
|
|
*/
|
|
|
|
@property int pid() const nothrow @nogc { return cast(int)m_pid; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
Whether the process has exited.
|
|
|
|
*/
|
|
|
|
@property bool exited() const nothrow { return eventDriver.processes.hasExited(m_pid); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
Wait for the process to exit, allowing other fibers to continue in the
|
|
|
|
meantime.
|
|
|
|
|
|
|
|
Params:
|
|
|
|
timeout = Optionally wait until a timeout is reached.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The exit code of the process. If a timeout is given and reached, a
|
|
|
|
null value is returned.
|
|
|
|
*/
|
|
|
|
int wait()
|
|
|
|
@blocking {
|
|
|
|
return asyncAwaitUninterruptible!(ProcessWaitCallback,
|
|
|
|
cb => eventDriver.processes.wait(m_pid, cb)
|
|
|
|
)[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Ditto
|
|
|
|
Nullable!int wait(Duration timeout)
|
|
|
|
@blocking {
|
|
|
|
size_t waitId;
|
|
|
|
bool cancelled = false;
|
|
|
|
|
|
|
|
int code = asyncAwaitUninterruptible!(ProcessWaitCallback,
|
|
|
|
(cb) nothrow @safe {
|
|
|
|
waitId = eventDriver.processes.wait(m_pid, cb);
|
|
|
|
},
|
|
|
|
(cb) nothrow @safe {
|
|
|
|
eventDriver.processes.cancelWait(m_pid, waitId);
|
|
|
|
cancelled = true;
|
|
|
|
},
|
|
|
|
)(timeout)[1];
|
|
|
|
|
|
|
|
if (cancelled) {
|
|
|
|
return Nullable!int.init;
|
|
|
|
} else {
|
|
|
|
return code.nullable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Kill the process.
|
|
|
|
|
|
|
|
By default on Linux this sends SIGTERM to the process.
|
|
|
|
|
|
|
|
Params:
|
|
|
|
signal = Optional parameter for the signal to send to the process.
|
|
|
|
*/
|
|
|
|
void kill()
|
|
|
|
{
|
|
|
|
version (Posix)
|
|
|
|
{
|
|
|
|
import core.sys.posix.signal : SIGTERM;
|
|
|
|
eventDriver.processes.kill(m_pid, SIGTERM);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
eventDriver.processes.kill(m_pid, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ditto
|
|
|
|
void kill(int signal)
|
|
|
|
{
|
|
|
|
eventDriver.processes.kill(m_pid, signal);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Terminate the process immediately.
|
|
|
|
|
|
|
|
On Linux this sends SIGKILL to the process.
|
|
|
|
*/
|
|
|
|
void forceKill()
|
|
|
|
{
|
|
|
|
version (Posix)
|
|
|
|
{
|
|
|
|
import core.sys.posix.signal : SIGKILL;
|
|
|
|
eventDriver.processes.kill(m_pid, SIGKILL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
eventDriver.processes.kill(m_pid, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Wait for the process to exit until a timeout is reached. If the process
|
|
|
|
doesn't exit before the timeout, force kill it.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The process exit code.
|
|
|
|
*/
|
|
|
|
int waitOrForceKill(Duration timeout)
|
|
|
|
@blocking {
|
|
|
|
auto code = wait(timeout);
|
|
|
|
|
|
|
|
if (code.isNull) {
|
|
|
|
forceKill();
|
|
|
|
return wait();
|
|
|
|
} else {
|
|
|
|
return code.get;
|
|
|
|
}
|
|
|
|
}
|
2019-05-05 05:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-18 14:42:28 +00:00
|
|
|
A stream for tBatchBufferhe write end of a pipe.
|
2019-05-05 05:27:57 +00:00
|
|
|
*/
|
|
|
|
struct PipeInputStream {
|
2020-05-18 14:42:28 +00:00
|
|
|
private static struct Context {
|
|
|
|
BatchBuffer!ubyte readBuffer;
|
|
|
|
shared(NativeEventDriver) driver;
|
|
|
|
}
|
|
|
|
|
|
|
|
private {
|
|
|
|
PipeFD m_pipe;
|
|
|
|
Context* m_context;
|
|
|
|
}
|
|
|
|
|
|
|
|
private this(PipeFD pipe)
|
|
|
|
nothrow {
|
|
|
|
m_pipe = pipe;
|
|
|
|
if (pipe != PipeFD.invalid) {
|
|
|
|
m_context = () @trusted { return &eventDriver.pipes.userData!Context(pipe); } ();
|
|
|
|
m_context.readBuffer.capacity = 4096;
|
|
|
|
m_context.driver = () @trusted { return cast(shared)eventDriver; } ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this(this)
|
|
|
|
nothrow {
|
|
|
|
if (m_pipe != PipeFD.invalid)
|
|
|
|
eventDriver.pipes.addRef(m_pipe);
|
|
|
|
}
|
|
|
|
|
|
|
|
~this()
|
|
|
|
nothrow {
|
|
|
|
if (m_pipe != PipeFD.invalid)
|
|
|
|
releaseHandle!"pipes"(m_pipe, m_context.driver);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool opCast(T)() const nothrow if (is(T == bool)) { return m_pipes != PipeFD.invalid; }
|
|
|
|
|
|
|
|
@property bool empty() @blocking { return leastSize == 0; }
|
|
|
|
@property ulong leastSize()
|
|
|
|
@blocking {
|
|
|
|
waitForData();
|
|
|
|
return m_context ? m_context.readBuffer.length : 0;
|
|
|
|
}
|
|
|
|
@property bool dataAvailableForRead() { return waitForData(0.seconds); }
|
|
|
|
|
|
|
|
bool waitForData(Duration timeout = Duration.max)
|
|
|
|
@blocking {
|
|
|
|
if (!m_context) return false;
|
|
|
|
if (m_context.readBuffer.length > 0) return true;
|
|
|
|
auto mode = timeout <= 0.seconds ? IOMode.immediate : IOMode.once;
|
|
|
|
|
|
|
|
bool cancelled;
|
|
|
|
IOStatus status;
|
|
|
|
size_t nbytes;
|
|
|
|
|
|
|
|
alias waiter = Waitable!(PipeIOCallback,
|
|
|
|
cb => eventDriver.pipes.read(m_pipe, m_context.readBuffer.peekDst(), mode, cb),
|
|
|
|
(cb) {
|
|
|
|
cancelled = true;
|
|
|
|
eventDriver.pipes.cancelRead(m_pipe);
|
|
|
|
},
|
|
|
|
(pipe, st, nb) {
|
|
|
|
// Handle closed pipes
|
|
|
|
if (m_pipe == PipeFD.invalid) {
|
|
|
|
cancelled = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(pipe == m_pipe);
|
|
|
|
status = st;
|
|
|
|
nbytes = nb;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
asyncAwaitAny!(true, waiter)(timeout);
|
|
|
|
|
|
|
|
if (cancelled || !m_context) return false;
|
|
|
|
|
|
|
|
logTrace("Pipe %s, read %s bytes: %s", m_pipe, nbytes, status);
|
|
|
|
|
|
|
|
assert(m_context.readBuffer.length == 0);
|
|
|
|
m_context.readBuffer.putN(nbytes);
|
|
|
|
switch (status) {
|
|
|
|
case IOStatus.ok: break;
|
|
|
|
case IOStatus.disconnected: break;
|
|
|
|
case IOStatus.wouldBlock:
|
|
|
|
assert(mode == IOMode.immediate);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
logDebug("Error status when waiting for data: %s", status);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_context.readBuffer.length > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const(ubyte)[] peek() { return m_context ? m_context.readBuffer.peek() : null; }
|
|
|
|
|
|
|
|
size_t read(scope ubyte[] dst, IOMode mode)
|
|
|
|
@blocking {
|
|
|
|
if (dst.empty) return 0;
|
|
|
|
|
|
|
|
if (m_context.readBuffer.length >= dst.length) {
|
|
|
|
m_context.readBuffer.read(dst);
|
|
|
|
return dst.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t nbytes = 0;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
if (m_context.readBuffer.length == 0) {
|
|
|
|
if (mode == IOMode.immediate || mode == IOMode.once && nbytes > 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
enforce(waitForData(), "Reached end of stream while reading data.");
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(m_context.readBuffer.length > 0);
|
|
|
|
auto l = min(dst.length, m_context.readBuffer.length);
|
|
|
|
m_context.readBuffer.read(dst[0 .. l]);
|
|
|
|
dst = dst[l .. $];
|
|
|
|
nbytes += l;
|
|
|
|
if (dst.length == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
void read(scope ubyte[] dst)
|
|
|
|
@blocking {
|
|
|
|
auto r = read(dst, IOMode.all);
|
|
|
|
assert(r == dst.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Close the read end of the pipe immediately.
|
|
|
|
|
|
|
|
Make sure that the pipe is not used after this is called and is released
|
|
|
|
as soon as possible. Due to implementation detail in eventcore this
|
|
|
|
reference could conflict with future pipes.
|
|
|
|
*/
|
|
|
|
void close()
|
|
|
|
nothrow {
|
2020-05-17 13:10:01 +00:00
|
|
|
if (m_pipe == PipeFD.invalid) return;
|
|
|
|
|
|
|
|
asyncAwaitUninterruptible!(PipeCloseCallback,
|
|
|
|
cb => eventDriver.pipes.close(m_pipe, cb)
|
|
|
|
);
|
|
|
|
|
|
|
|
eventDriver.pipes.releaseRef(m_pipe);
|
|
|
|
m_pipe = PipeFD.invalid;
|
2020-05-18 14:42:28 +00:00
|
|
|
}
|
2019-05-05 05:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mixin validateInputStream!PipeInputStream;
|
|
|
|
|
|
|
|
/**
|
2020-05-18 14:42:28 +00:00
|
|
|
Stream for the read end of a pipe.
|
2019-05-05 05:27:57 +00:00
|
|
|
*/
|
|
|
|
struct PipeOutputStream {
|
2020-05-18 14:42:28 +00:00
|
|
|
private static struct Context {
|
|
|
|
shared(NativeEventDriver) driver;
|
|
|
|
}
|
|
|
|
|
|
|
|
private {
|
|
|
|
PipeFD m_pipe;
|
|
|
|
Context* m_context;
|
|
|
|
}
|
|
|
|
|
|
|
|
private this(PipeFD pipe)
|
|
|
|
nothrow {
|
|
|
|
m_pipe = pipe;
|
|
|
|
if (pipe != PipeFD.invalid) {
|
|
|
|
m_context = () @trusted { return &eventDriver.pipes.userData!Context(pipe); } ();
|
|
|
|
m_context.driver = () @trusted { return cast(shared)eventDriver; } ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this(this)
|
|
|
|
nothrow {
|
|
|
|
if (m_pipe != PipeFD.invalid)
|
|
|
|
eventDriver.pipes.addRef(m_pipe);
|
|
|
|
}
|
|
|
|
|
|
|
|
~this()
|
|
|
|
nothrow {
|
|
|
|
if (m_pipe != PipeFD.invalid)
|
|
|
|
releaseHandle!"pipes"(m_pipe, m_context.driver);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool opCast(T)() const nothrow if (is(T == bool)) { return m_pipes != PipeFD.invalid; }
|
|
|
|
|
|
|
|
size_t write(in ubyte[] bytes, IOMode mode)
|
|
|
|
@blocking {
|
|
|
|
if (bytes.empty) return 0;
|
|
|
|
|
|
|
|
auto res = asyncAwait!(PipeIOCallback,
|
|
|
|
cb => eventDriver.pipes.write(m_pipe, bytes, mode, cb),
|
|
|
|
cb => eventDriver.pipes.cancelWrite(m_pipe));
|
|
|
|
|
|
|
|
switch (res[1]) {
|
|
|
|
case IOStatus.ok: break;
|
|
|
|
case IOStatus.disconnected: break;
|
|
|
|
default:
|
|
|
|
throw new Exception("Error writing data to pipe.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return res[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
void write(in ubyte[] bytes) @blocking { auto r = write(bytes, IOMode.all); assert(r == bytes.length); }
|
|
|
|
void write(in char[] bytes) @blocking { write(cast(const(ubyte)[])bytes); }
|
|
|
|
|
|
|
|
void flush() {}
|
|
|
|
void finalize() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Close the write end of the pipe immediately.
|
|
|
|
|
|
|
|
Make sure that the pipe is not used after this is called and is released
|
|
|
|
as soon as possible. Due to implementation detail in eventcore this
|
|
|
|
reference could conflict with future pipes.
|
|
|
|
*/
|
|
|
|
void close()
|
|
|
|
nothrow {
|
2020-05-17 13:10:01 +00:00
|
|
|
if (m_pipe == PipeFD.invalid) return;
|
|
|
|
|
|
|
|
asyncAwaitUninterruptible!(PipeCloseCallback,
|
|
|
|
cb => eventDriver.pipes.close(m_pipe, cb)
|
|
|
|
);
|
|
|
|
|
|
|
|
eventDriver.pipes.releaseRef(m_pipe);
|
|
|
|
m_pipe = PipeFD.invalid;
|
2020-05-18 14:42:28 +00:00
|
|
|
}
|
2019-05-05 05:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mixin validateOutputStream!PipeOutputStream;
|
|
|
|
|
|
|
|
/**
|
2020-05-18 14:42:28 +00:00
|
|
|
A pipe created by `pipe`.
|
2019-05-05 05:27:57 +00:00
|
|
|
*/
|
|
|
|
struct Pipe {
|
2020-05-18 14:42:28 +00:00
|
|
|
/// Read end of the pipe
|
|
|
|
PipeInputStream readEnd;
|
|
|
|
/// Write end of the pipe
|
|
|
|
PipeOutputStream writeEnd;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Close both ends of the pipe
|
|
|
|
*/
|
|
|
|
void close()
|
|
|
|
nothrow {
|
|
|
|
writeEnd.close();
|
|
|
|
readEnd.close();
|
|
|
|
}
|
2019-05-05 05:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-18 14:42:28 +00:00
|
|
|
Create a pipe, async equivalent of `std.process.pipe`.
|
2019-05-05 05:27:57 +00:00
|
|
|
|
2020-05-18 14:42:28 +00:00
|
|
|
Returns:
|
|
|
|
A stream for each end of the pipe.
|
2019-05-05 05:27:57 +00:00
|
|
|
*/
|
|
|
|
Pipe pipe()
|
|
|
|
{
|
2020-05-18 14:42:28 +00:00
|
|
|
auto p = std.process.pipe();
|
2019-05-05 05:27:57 +00:00
|
|
|
|
2020-05-18 14:42:28 +00:00
|
|
|
auto read = eventDriver.pipes.adopt(p.readEnd.fileno);
|
|
|
|
auto write = eventDriver.pipes.adopt(p.writeEnd.fileno);
|
2019-05-05 05:27:57 +00:00
|
|
|
|
2020-05-18 14:42:28 +00:00
|
|
|
return Pipe(PipeInputStream(read), PipeOutputStream(write));
|
2019-05-05 05:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-18 14:42:28 +00:00
|
|
|
Returned from `pipeProcess`.
|
2019-05-05 05:27:57 +00:00
|
|
|
|
2020-05-18 14:42:28 +00:00
|
|
|
See_Also: `pipeProcess`, `pipeShell`
|
2019-05-05 05:27:57 +00:00
|
|
|
*/
|
|
|
|
struct ProcessPipes {
|
2020-05-18 14:42:28 +00:00
|
|
|
Process process;
|
|
|
|
PipeOutputStream stdin;
|
|
|
|
PipeInputStream stdout;
|
|
|
|
PipeInputStream stderr;
|
2019-05-05 05:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-18 14:42:28 +00:00
|
|
|
Equivalent to `std.process.pipeProcess`.
|
2019-05-05 05:27:57 +00:00
|
|
|
|
2020-05-18 14:42:28 +00:00
|
|
|
Returns:
|
|
|
|
A struct containing the process and created pipes.
|
2019-05-05 05:27:57 +00:00
|
|
|
|
2020-05-18 14:42:28 +00:00
|
|
|
See_Also: `spawnProcess`, `execute`
|
2019-05-05 05:27:57 +00:00
|
|
|
*/
|
|
|
|
ProcessPipes pipeProcess(
|
2020-05-18 14:42:28 +00:00
|
|
|
scope string[] args,
|
|
|
|
Redirect redirect = Redirect.all,
|
|
|
|
const string[string] env = null,
|
|
|
|
Config config = Config.none,
|
|
|
|
scope NativePath workDir = NativePath.init)
|
2019-05-05 05:27:57 +00:00
|
|
|
@trusted {
|
2020-05-18 14:42:28 +00:00
|
|
|
auto stdin = ProcessStdinFile(ProcessRedirect.inherit);
|
|
|
|
if (Redirect.stdin & redirect) {
|
|
|
|
stdin = ProcessStdinFile(ProcessRedirect.pipe);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto stdout = ProcessStdoutFile(ProcessRedirect.inherit);
|
|
|
|
if (Redirect.stdoutToStderr & redirect) {
|
|
|
|
stdout = ProcessStdoutFile(ProcessStdoutRedirect.toStderr);
|
|
|
|
} else if (Redirect.stdout & redirect) {
|
|
|
|
stdout = ProcessStdoutFile(ProcessRedirect.pipe);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto stderr = ProcessStderrFile(ProcessRedirect.inherit);
|
|
|
|
if (Redirect.stderrToStdout & redirect) {
|
|
|
|
stderr = ProcessStderrFile(ProcessStderrRedirect.toStdout);
|
|
|
|
} else if (Redirect.stderr & redirect) {
|
|
|
|
stderr = ProcessStderrFile(ProcessRedirect.pipe);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto process = eventDriver.processes.spawn(
|
|
|
|
args,
|
|
|
|
stdin,
|
|
|
|
stdout,
|
|
|
|
stderr,
|
|
|
|
env,
|
|
|
|
config,
|
|
|
|
workDir.toNativeString());
|
|
|
|
|
2020-05-18 14:44:11 +00:00
|
|
|
if (process.pid == ProcessID.invalid)
|
|
|
|
throw new Exception("Failed to spawn process");
|
|
|
|
|
2020-05-18 14:42:28 +00:00
|
|
|
return ProcessPipes(
|
|
|
|
Process(process.pid),
|
|
|
|
PipeOutputStream(cast(PipeFD)process.stdin),
|
|
|
|
PipeInputStream(cast(PipeFD)process.stdout),
|
|
|
|
PipeInputStream(cast(PipeFD)process.stderr)
|
|
|
|
);
|
2019-05-05 05:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ditto
|
|
|
|
ProcessPipes pipeProcess(
|
2020-05-18 14:42:28 +00:00
|
|
|
scope string program,
|
|
|
|
Redirect redirect = Redirect.all,
|
|
|
|
const string[string] env = null,
|
|
|
|
Config config = Config.none,
|
|
|
|
scope NativePath workDir = NativePath.init)
|
2019-05-05 05:27:57 +00:00
|
|
|
{
|
2020-05-18 14:42:28 +00:00
|
|
|
return pipeProcess(
|
|
|
|
[program],
|
|
|
|
redirect,
|
|
|
|
env,
|
|
|
|
config,
|
|
|
|
workDir
|
|
|
|
);
|
2019-05-05 05:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ditto
|
|
|
|
ProcessPipes pipeShell(
|
2020-05-18 14:42:28 +00:00
|
|
|
scope string command,
|
|
|
|
Redirect redirect = Redirect.all,
|
|
|
|
const string[string] env = null,
|
|
|
|
Config config = Config.none,
|
|
|
|
scope NativePath workDir = NativePath.init,
|
|
|
|
scope NativePath shellPath = nativeShell)
|
2019-05-05 05:27:57 +00:00
|
|
|
{
|
2020-05-18 14:42:28 +00:00
|
|
|
return pipeProcess(
|
|
|
|
shellCommand(command, nativeShell),
|
|
|
|
redirect,
|
|
|
|
env,
|
|
|
|
config,
|
|
|
|
workDir);
|
2019-05-05 05:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-18 14:42:28 +00:00
|
|
|
Equivalent to `std.process.execute`.
|
2019-05-05 05:27:57 +00:00
|
|
|
|
2020-05-18 14:42:28 +00:00
|
|
|
Returns:
|
|
|
|
Tuple containing the exit status and process output.
|
2019-05-05 05:27:57 +00:00
|
|
|
|
2020-05-18 14:42:28 +00:00
|
|
|
See_Also: `spawnProcess`, `pipeProcess`
|
2019-05-05 05:27:57 +00:00
|
|
|
*/
|
|
|
|
auto execute(
|
2020-05-18 14:42:28 +00:00
|
|
|
scope string[] args,
|
|
|
|
const string[string] env = null,
|
|
|
|
Config config = Config.none,
|
|
|
|
size_t maxOutput = size_t.max,
|
|
|
|
scope NativePath workDir = NativePath.init)
|
2019-05-05 05:27:57 +00:00
|
|
|
@blocking {
|
2020-05-18 14:42:28 +00:00
|
|
|
return executeImpl!pipeProcess(args, env, config, maxOutput, workDir);
|
2019-05-05 05:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ditto
|
|
|
|
auto execute(
|
2020-05-18 14:42:28 +00:00
|
|
|
scope string program,
|
|
|
|
const string[string] env = null,
|
|
|
|
Config config = Config.none,
|
|
|
|
size_t maxOutput = size_t.max,
|
|
|
|
scope NativePath workDir = NativePath.init)
|
2019-05-05 05:27:57 +00:00
|
|
|
@blocking @trusted {
|
2020-05-18 14:42:28 +00:00
|
|
|
return executeImpl!pipeProcess(program, env, config, maxOutput, workDir);
|
2019-05-05 05:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ditto
|
|
|
|
auto executeShell(
|
2020-05-18 14:42:28 +00:00
|
|
|
scope string command,
|
|
|
|
const string[string] env = null,
|
|
|
|
Config config = Config.none,
|
|
|
|
size_t maxOutput = size_t.max,
|
|
|
|
scope NativePath workDir = null,
|
|
|
|
NativePath shellPath = nativeShell)
|
2019-05-05 05:27:57 +00:00
|
|
|
@blocking {
|
2020-05-18 14:42:28 +00:00
|
|
|
return executeImpl!pipeShell(command, env, config, maxOutput, workDir, shellPath);
|
2019-05-05 05:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private auto executeImpl(alias spawn, Cmd, Args...)(
|
2020-05-18 14:42:28 +00:00
|
|
|
Cmd command,
|
|
|
|
const string[string] env,
|
|
|
|
Config config,
|
|
|
|
size_t maxOutput,
|
|
|
|
scope NativePath workDir,
|
|
|
|
Args args)
|
2019-05-05 05:27:57 +00:00
|
|
|
@blocking {
|
2020-05-18 14:42:28 +00:00
|
|
|
Redirect redirect = Redirect.stdout;
|
2019-05-05 05:27:57 +00:00
|
|
|
|
2020-05-18 14:42:28 +00:00
|
|
|
auto processPipes = spawn(command, redirect, env, config, workDir, args);
|
2019-05-05 05:27:57 +00:00
|
|
|
|
2020-05-18 14:42:28 +00:00
|
|
|
auto stringOutput = processPipes.stdout.collectOutput(maxOutput);
|
2019-05-05 05:27:57 +00:00
|
|
|
|
2020-05-18 14:42:28 +00:00
|
|
|
return Tuple!(int, "status", string, "output")(processPipes.process.wait(), stringOutput);
|
2019-05-05 05:27:57 +00:00
|
|
|
}
|
|
|
|
|
2019-06-02 04:42:41 +00:00
|
|
|
/*
|
2020-05-18 14:42:28 +00:00
|
|
|
Collect the string output of a stream in a blocking fashion.
|
2019-05-05 05:27:57 +00:00
|
|
|
|
2020-05-18 14:42:28 +00:00
|
|
|
Params:
|
|
|
|
stream = The input stream to collect from.
|
|
|
|
nbytes = The maximum number of bytes to collect.
|
2019-05-05 05:27:57 +00:00
|
|
|
|
2020-05-18 14:42:28 +00:00
|
|
|
Returns:
|
|
|
|
The collected data from the stream as a string.
|
2019-05-05 05:27:57 +00:00
|
|
|
*/
|
2019-06-02 04:42:41 +00:00
|
|
|
/// private
|
2019-05-05 05:27:57 +00:00
|
|
|
string collectOutput(InputStream)(InputStream stream, size_t nbytes = size_t.max)
|
|
|
|
@blocking @trusted if (isInputStream!InputStream) {
|
2020-05-18 14:42:28 +00:00
|
|
|
auto output = appender!string();
|
|
|
|
if (nbytes != size_t.max) {
|
|
|
|
output.reserve(nbytes);
|
|
|
|
}
|
2019-05-05 05:27:57 +00:00
|
|
|
|
2020-05-18 14:42:28 +00:00
|
|
|
import vibe.internal.allocator : theAllocator, dispose;
|
2019-05-05 05:27:57 +00:00
|
|
|
|
2020-05-18 14:42:28 +00:00
|
|
|
scope buffer = cast(ubyte[]) theAllocator.allocate(64*1024);
|
|
|
|
scope (exit) theAllocator.dispose(buffer);
|
2019-05-05 05:27:57 +00:00
|
|
|
|
2020-05-18 14:42:28 +00:00
|
|
|
while (!stream.empty && nbytes > 0) {
|
|
|
|
size_t chunk = min(nbytes, stream.leastSize, buffer.length);
|
|
|
|
assert(chunk > 0, "leastSize returned zero for non-empty stream.");
|
2019-05-05 05:27:57 +00:00
|
|
|
|
2020-05-18 14:42:28 +00:00
|
|
|
stream.read(buffer[0..chunk]);
|
|
|
|
output.put(buffer[0..chunk]);
|
|
|
|
}
|
2019-05-05 05:27:57 +00:00
|
|
|
|
2020-05-18 14:42:28 +00:00
|
|
|
return output.data;
|
2019-05-05 05:27:57 +00:00
|
|
|
}
|