1
0
mirror of https://github.com/danog/process.git synced 2024-11-26 20:24:43 +01:00

Fix reading exit code on Windows

This commit is contained in:
Aaron Piotrowski 2017-02-09 11:17:05 -06:00
parent 37cf53aa27
commit 96399025bb

View File

@ -132,9 +132,11 @@ class Process {
["pipe", "w"], // exit code pipe ["pipe", "w"], // exit code pipe
]; ];
$nd = \strncasecmp(\PHP_OS, "WIN", 3) === 0 ? "NUL" : "/dev/null"; if (\strncasecmp(\PHP_OS, "WIN", 3) === 0) {
$command = $this->command;
$command = \sprintf('(%s) 3>%s; code=$?; echo $code >&3; exit $code', $this->command, $nd); } else {
$command = \sprintf('(%s) 3>/dev/null; code=$?; echo $code >&3; exit $code', $this->command);
}
$this->process = @\proc_open($command, $fd, $pipes, $this->cwd ?: null, $this->env ?: null, $this->options); $this->process = @\proc_open($command, $fd, $pipes, $this->cwd ?: null, $this->env ?: null, $this->options);
@ -164,22 +166,30 @@ class Process {
$this->stdin = $stdin = $pipes[0]; $this->stdin = $stdin = $pipes[0];
$this->stdout = $pipes[1]; $this->stdout = $pipes[1];
$this->stderr = $pipes[2]; $this->stderr = $pipes[2];
$stream = $pipes[3];
$this->running = true; $this->running = true;
$process = &$this->process;
$running = &$this->running; $running = &$this->running;
$this->watcher = Loop::onReadable($stream, static function ($watcher, $resource) use ( $this->watcher = Loop::onReadable($pipes[3], static function ($watcher, $resource) use (
&$running, $deferred, $stdin &$process, &$running, $deferred, $stdin
) { ) {
Loop::cancel($watcher); Loop::cancel($watcher);
$running = false; $running = false;
try { try {
try { try {
if (!\is_resource($resource) || \feof($resource)) { if (\strncasecmp(\PHP_OS, "WIN", 3) === 0) {
throw new ProcessException("Process ended unexpectedly"); $code = proc_get_status($process)["exitcode"];
} else {
if (!\is_resource($resource) || \feof($resource)) {
throw new ProcessException("Process ended unexpectedly");
}
$code = \rtrim(@\stream_get_contents($resource));
if (!\strlen($code) || !\is_numeric($code)) {
throw new ProcessException("Unable to read exit code");
}
} }
$code = \rtrim(@\fread($resource, 3)); // Single byte written as string
} finally { } finally {
if (\is_resource($resource)) { if (\is_resource($resource)) {
\fclose($resource); \fclose($resource);