2017-01-11 19:24:02 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp\Process;
|
|
|
|
|
2017-06-15 19:52:07 +02:00
|
|
|
use Amp\ByteStream\InputStream;
|
|
|
|
use Amp\ByteStream\OutputStream;
|
|
|
|
use Amp\ByteStream\ResourceInputStream;
|
|
|
|
use Amp\ByteStream\ResourceOutputStream;
|
2017-06-15 19:02:50 +02:00
|
|
|
use Amp\Deferred;
|
|
|
|
use Amp\Loop;
|
|
|
|
use Amp\Promise;
|
2017-06-15 19:52:07 +02:00
|
|
|
use function Amp\call;
|
2017-01-11 19:24:02 +01:00
|
|
|
|
|
|
|
class Process {
|
2017-03-09 06:24:03 +01:00
|
|
|
/** @var bool */
|
|
|
|
private static $onWindows;
|
|
|
|
|
2017-01-11 19:24:02 +01:00
|
|
|
/** @var resource|null */
|
|
|
|
private $process;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $command;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $cwd = "";
|
|
|
|
|
|
|
|
/** @var array */
|
|
|
|
private $env = [];
|
|
|
|
|
|
|
|
/** @var array */
|
|
|
|
private $options;
|
|
|
|
|
2017-06-15 19:52:07 +02:00
|
|
|
/** @var \Amp\ByteStream\ResourceOutputStream|null */
|
2017-01-11 19:24:02 +01:00
|
|
|
private $stdin;
|
|
|
|
|
2017-06-15 19:52:07 +02:00
|
|
|
/** @var \Amp\ByteStream\ResourceInputStream|null */
|
2017-01-11 19:24:02 +01:00
|
|
|
private $stdout;
|
|
|
|
|
2017-06-15 19:52:07 +02:00
|
|
|
/** @var \Amp\ByteStream\ResourceInputStream|null */
|
2017-01-11 19:24:02 +01:00
|
|
|
private $stderr;
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
private $pid = 0;
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
private $oid = 0;
|
|
|
|
|
|
|
|
/** @var \Amp\Deferred|null */
|
|
|
|
private $deferred;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $watcher;
|
|
|
|
|
2017-01-16 19:37:00 +01:00
|
|
|
/** @var bool */
|
|
|
|
private $running = false;
|
|
|
|
|
2017-01-11 19:24:02 +01:00
|
|
|
/**
|
|
|
|
* @param string|array $command Command to run.
|
|
|
|
* @param string|null $cwd Working directory or use an empty string to use the working directory of the current
|
|
|
|
* PHP process.
|
|
|
|
* @param mixed[] $env Environment variables or use an empty array to inherit from the current PHP process.
|
|
|
|
* @param mixed[] $options Options for proc_open().
|
|
|
|
*/
|
|
|
|
public function __construct($command, string $cwd = null, array $env = [], array $options = []) {
|
2017-03-09 06:24:03 +01:00
|
|
|
if (self::$onWindows === null) {
|
|
|
|
self::$onWindows = \strncasecmp(\PHP_OS, "WIN", 3) === 0;
|
|
|
|
}
|
|
|
|
|
2017-01-11 19:24:02 +01:00
|
|
|
if (\is_array($command)) {
|
|
|
|
$command = \implode(" ", \array_map("escapeshellarg", $command));
|
|
|
|
}
|
|
|
|
$this->command = $command;
|
2017-01-15 16:23:32 +01:00
|
|
|
$this->cwd = $cwd ?? "";
|
2017-01-11 19:24:02 +01:00
|
|
|
|
|
|
|
foreach ($env as $key => $value) {
|
2017-01-15 16:23:32 +01:00
|
|
|
if (\is_array($value)) {
|
|
|
|
throw new \Error("\$env cannot accept array values");
|
2017-01-11 19:24:02 +01:00
|
|
|
}
|
2017-01-15 16:23:32 +01:00
|
|
|
|
|
|
|
$this->env[(string) $key] = (string) $value;
|
2017-01-11 19:24:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->options = $options;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops the process if it is still running.
|
|
|
|
*/
|
|
|
|
public function __destruct() {
|
|
|
|
if (\getmypid() === $this->oid) {
|
|
|
|
$this->kill(); // Will only terminate if the process is still running.
|
|
|
|
}
|
|
|
|
|
2017-03-16 16:04:52 +01:00
|
|
|
if ($this->watcher !== null) {
|
|
|
|
Loop::cancel($this->watcher);
|
|
|
|
}
|
2017-01-11 19:24:02 +01:00
|
|
|
|
2017-01-16 19:37:00 +01:00
|
|
|
if (\is_resource($this->process)) {
|
|
|
|
\proc_close($this->process);
|
|
|
|
}
|
2017-01-11 19:24:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets process values.
|
|
|
|
*/
|
|
|
|
public function __clone() {
|
|
|
|
$this->process = null;
|
|
|
|
$this->deferred = null;
|
|
|
|
$this->watcher = null;
|
|
|
|
$this->pid = 0;
|
|
|
|
$this->oid = 0;
|
|
|
|
$this->stdin = null;
|
|
|
|
$this->stdout = null;
|
|
|
|
$this->stderr = null;
|
2017-01-16 19:37:00 +01:00
|
|
|
$this->running = false;
|
2017-01-11 19:24:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws \Amp\Process\ProcessException If starting the process fails.
|
|
|
|
* @throws \Amp\Process\StatusError If the process is already running.
|
|
|
|
*/
|
2017-03-17 04:19:15 +01:00
|
|
|
public function start() {
|
2017-01-11 19:24:02 +01:00
|
|
|
if ($this->deferred !== null) {
|
|
|
|
throw new StatusError("The process has already been started");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->deferred = $deferred = new Deferred;
|
|
|
|
|
|
|
|
$fd = [
|
|
|
|
["pipe", "r"], // stdin
|
|
|
|
["pipe", "w"], // stdout
|
|
|
|
["pipe", "w"], // stderr
|
|
|
|
["pipe", "w"], // exit code pipe
|
|
|
|
];
|
|
|
|
|
2017-03-09 06:24:03 +01:00
|
|
|
if (self::$onWindows) {
|
2017-03-05 17:48:27 +01:00
|
|
|
$command = $this->command;
|
2017-02-09 18:17:05 +01:00
|
|
|
} else {
|
2017-03-05 17:48:27 +01:00
|
|
|
$command = \sprintf(
|
|
|
|
'{ (%s) <&3 3<&- 3>/dev/null & } 3<&0;' .
|
|
|
|
'pid=$!; echo $pid >&3; wait $pid; RC=$?; echo $RC >&3; exit $RC',
|
|
|
|
$this->command
|
|
|
|
);
|
2017-02-09 18:17:05 +01:00
|
|
|
}
|
2017-01-11 19:24:02 +01:00
|
|
|
|
|
|
|
$this->process = @\proc_open($command, $fd, $pipes, $this->cwd ?: null, $this->env ?: null, $this->options);
|
|
|
|
|
|
|
|
if (!\is_resource($this->process)) {
|
|
|
|
$message = "Could not start process";
|
|
|
|
if ($error = \error_get_last()) {
|
|
|
|
$message .= \sprintf(" Errno: %d; %s", $error["type"], $error["message"]);
|
|
|
|
}
|
2017-03-09 06:24:03 +01:00
|
|
|
$deferred->fail(new ProcessException($message));
|
2017-03-17 04:19:15 +01:00
|
|
|
return;
|
2017-01-11 19:24:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->oid = \getmypid();
|
|
|
|
$status = \proc_get_status($this->process);
|
|
|
|
|
|
|
|
if (!$status) {
|
|
|
|
\proc_close($this->process);
|
|
|
|
$this->process = null;
|
2017-03-09 06:24:03 +01:00
|
|
|
$deferred->fail(new ProcessException("Could not get process status"));
|
2017-03-17 04:19:15 +01:00
|
|
|
return;
|
2017-01-11 19:24:02 +01:00
|
|
|
}
|
|
|
|
|
2017-03-09 06:24:03 +01:00
|
|
|
if (self::$onWindows) {
|
|
|
|
$this->pid = $status["pid"];
|
|
|
|
} else {
|
|
|
|
// This blocking read will only block until the process scheduled, generally a few microseconds.
|
2017-03-10 06:33:23 +01:00
|
|
|
$pid = \rtrim(@\fgets($pipes[3]));
|
2017-03-09 06:24:03 +01:00
|
|
|
|
|
|
|
if (!$pid || !\is_numeric($pid)) {
|
|
|
|
$deferred->fail(new ProcessException("Could not determine PID"));
|
2017-03-17 04:19:15 +01:00
|
|
|
return;
|
2017-03-09 06:24:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->pid = (int) $pid;
|
|
|
|
}
|
2017-01-11 19:24:02 +01:00
|
|
|
|
2017-06-15 19:52:07 +02:00
|
|
|
$this->stdin = new ResourceOutputStream($pipes[0]);
|
|
|
|
$this->stdout = new ResourceInputStream($pipes[1]);
|
|
|
|
$this->stderr = new ResourceInputStream($pipes[2]);
|
|
|
|
\stream_set_blocking($pipes[3], false);
|
2017-01-11 19:24:02 +01:00
|
|
|
|
2017-01-16 19:37:00 +01:00
|
|
|
$this->running = true;
|
2017-02-09 18:17:05 +01:00
|
|
|
|
|
|
|
$process = &$this->process;
|
2017-01-16 19:37:00 +01:00
|
|
|
$running = &$this->running;
|
2017-02-09 18:17:05 +01:00
|
|
|
$this->watcher = Loop::onReadable($pipes[3], static function ($watcher, $resource) use (
|
2017-06-15 19:52:07 +02:00
|
|
|
&$process, &$running, $deferred
|
2017-01-11 19:24:02 +01:00
|
|
|
) {
|
|
|
|
Loop::cancel($watcher);
|
2017-01-16 19:37:00 +01:00
|
|
|
$running = false;
|
2017-01-11 19:24:02 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
if (!\is_resource($resource) || \feof($resource)) {
|
|
|
|
throw new ProcessException("Process ended unexpectedly");
|
|
|
|
}
|
2017-03-09 06:24:03 +01:00
|
|
|
if (self::$onWindows) {
|
|
|
|
$code = \proc_get_status($process)["exitcode"];
|
|
|
|
} else {
|
|
|
|
$code = \rtrim(@\stream_get_contents($resource));
|
|
|
|
}
|
2017-01-11 19:24:02 +01:00
|
|
|
} finally {
|
|
|
|
if (\is_resource($resource)) {
|
|
|
|
\fclose($resource);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
$deferred->fail($exception);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$deferred->resolve((int) $code);
|
|
|
|
});
|
|
|
|
|
2017-03-17 04:19:15 +01:00
|
|
|
Loop::unreference($this->watcher);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Amp\Promise<int> Succeeds with exit code of the process or fails if the process is killed.
|
|
|
|
*/
|
|
|
|
public function join(): Promise {
|
|
|
|
if ($this->deferred === null) {
|
|
|
|
throw new StatusError("The process is not running");
|
|
|
|
}
|
|
|
|
|
2017-06-15 19:52:07 +02:00
|
|
|
return call(function () {
|
|
|
|
if ($this->watcher !== null && $this->running) {
|
|
|
|
Loop::reference($this->watcher);
|
|
|
|
}
|
2017-03-17 04:19:15 +01:00
|
|
|
|
2017-06-15 19:52:07 +02:00
|
|
|
try {
|
|
|
|
return yield $this->deferred->promise();
|
|
|
|
} finally {
|
|
|
|
$this->stdin->close();
|
|
|
|
$this->stdout->close();
|
|
|
|
$this->stderr->close();
|
|
|
|
}
|
|
|
|
});
|
2017-01-11 19:24:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function kill() {
|
2017-01-16 19:37:00 +01:00
|
|
|
if ($this->running && \is_resource($this->process)) {
|
|
|
|
$this->running = false;
|
|
|
|
|
2017-01-11 19:24:02 +01:00
|
|
|
// Forcefully kill the process using SIGKILL.
|
|
|
|
\proc_terminate($this->process, 9);
|
|
|
|
|
|
|
|
Loop::cancel($this->watcher);
|
|
|
|
|
|
|
|
$this->deferred->fail(new ProcessException("The process was killed"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends the given signal to the process.
|
|
|
|
*
|
|
|
|
* @param int $signo Signal number to send to process.
|
|
|
|
*
|
|
|
|
* @throws \Amp\Process\StatusError If the process is not running.
|
|
|
|
*/
|
|
|
|
public function signal(int $signo) {
|
|
|
|
if (!$this->isRunning()) {
|
|
|
|
throw new StatusError("The process is not running");
|
|
|
|
}
|
|
|
|
|
2017-01-15 16:23:32 +01:00
|
|
|
\proc_terminate($this->process, $signo);
|
2017-01-11 19:24:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-15 16:23:32 +01:00
|
|
|
* Returns the PID of the child process. Value is only meaningful if PHP was not compiled with --enable-sigchild.
|
2017-01-11 19:24:02 +01:00
|
|
|
*
|
|
|
|
* @return int
|
2017-01-15 16:23:32 +01:00
|
|
|
*
|
|
|
|
* @throws \Amp\Process\StatusError
|
2017-01-11 19:24:02 +01:00
|
|
|
*/
|
|
|
|
public function getPid(): int {
|
2017-01-15 16:23:32 +01:00
|
|
|
if ($this->pid === 0) {
|
|
|
|
throw new StatusError("The process has not been started");
|
|
|
|
}
|
|
|
|
|
2017-01-11 19:24:02 +01:00
|
|
|
return $this->pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the command to execute.
|
|
|
|
*
|
|
|
|
* @return string The command to execute.
|
|
|
|
*/
|
|
|
|
public function getCommand(): string {
|
|
|
|
return $this->command;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the current working directory.
|
|
|
|
*
|
2017-01-15 16:23:32 +01:00
|
|
|
* @return string The current working directory an empty string if inherited from the current PHP process.
|
2017-01-11 19:24:02 +01:00
|
|
|
*/
|
|
|
|
public function getWorkingDirectory(): string {
|
|
|
|
if ($this->cwd === "") {
|
|
|
|
return \getcwd() ?: "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->cwd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the environment variables array.
|
|
|
|
*
|
2017-01-15 16:23:32 +01:00
|
|
|
* @return string[] Array of environment variables.
|
2017-01-11 19:24:02 +01:00
|
|
|
*/
|
|
|
|
public function getEnv(): array {
|
|
|
|
return $this->env;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the options to pass to proc_open().
|
|
|
|
*
|
|
|
|
* @return mixed[] Array of options.
|
|
|
|
*/
|
|
|
|
public function getOptions(): array {
|
|
|
|
return $this->options;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if the process is still running.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isRunning(): bool {
|
2017-01-16 19:37:00 +01:00
|
|
|
return $this->running;
|
2017-01-11 19:24:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the process input stream (STDIN).
|
|
|
|
*
|
2017-06-15 19:52:07 +02:00
|
|
|
* @return \Amp\ByteStream\OutputStream
|
2017-01-11 19:24:02 +01:00
|
|
|
*
|
|
|
|
* @throws \Amp\Process\StatusError If the process is not running.
|
|
|
|
*/
|
2017-06-15 19:52:07 +02:00
|
|
|
public function getStdin(): OutputStream {
|
2017-01-11 19:24:02 +01:00
|
|
|
if ($this->stdin === null) {
|
|
|
|
throw new StatusError("The process has not been started");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->stdin;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the process output stream (STDOUT).
|
|
|
|
*
|
2017-06-15 19:52:07 +02:00
|
|
|
* @return \Amp\ByteStream\InputStream
|
2017-01-11 19:24:02 +01:00
|
|
|
*
|
|
|
|
* @throws \Amp\Process\StatusError If the process is not running.
|
|
|
|
*/
|
2017-06-15 19:52:07 +02:00
|
|
|
public function getStdout(): InputStream {
|
2017-01-11 19:24:02 +01:00
|
|
|
if ($this->stdout === null) {
|
|
|
|
throw new StatusError("The process has not been started");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->stdout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the process error stream (STDERR).
|
|
|
|
*
|
2017-06-15 19:52:07 +02:00
|
|
|
* @return \Amp\ByteStream\InputStream
|
2017-01-11 19:24:02 +01:00
|
|
|
*
|
|
|
|
* @throws \Amp\Process\StatusError If the process is not running.
|
|
|
|
*/
|
2017-06-15 19:52:07 +02:00
|
|
|
public function getStderr(): InputStream {
|
2017-01-11 19:24:02 +01:00
|
|
|
if ($this->stderr === null) {
|
|
|
|
throw new StatusError("The process has not been started");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->stderr;
|
|
|
|
}
|
|
|
|
}
|