Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions php/EE/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ public function run() {

$proc = Utils\proc_open_compat( $this->command, self::$descriptors, $pipes, $this->cwd, $this->env );

$stdout = stream_get_contents( $pipes[1] );
$output = Utils\read_pipes( $pipes );
$stdout = $output[1];
$stderr = $output[2];
fclose( $pipes[1] );

$stderr = stream_get_contents( $pipes[2] );
fclose( $pipes[2] );

$return_code = proc_close( $proc );
Expand Down
5 changes: 3 additions & 2 deletions php/class-ee.php
Original file line number Diff line number Diff line change
Expand Up @@ -1081,9 +1081,10 @@ public static function runcommand( $command, $options = array() ) {
$proc = Utils\proc_open_compat( $runcommand, $descriptors, $pipes, getcwd() );

if ( $return ) {
$stdout = stream_get_contents( $pipes[1] );
$output = Utils\read_pipes( $pipes );
$stdout = $output[1];
$stderr = $output[2];
fclose( $pipes[1] );
$stderr = stream_get_contents( $pipes[2] );
fclose( $pipes[2] );
}
$return_code = proc_close( $proc );
Expand Down
53 changes: 53 additions & 0 deletions php/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,59 @@ function proc_open_compat( $cmd, $descriptorspec, &$pipes, $cwd = null, $env = n
return proc_open( $cmd, $descriptorspec, $pipes, $cwd, $env, $other_options );
}

/**
* Read process pipes to EOF together, so a child can't block on a full pipe that isn't being read.
*
* @access public
*
* @param resource[] $pipes Output (read) pipes from `proc_open()`, keyed by descriptor number.
*
* @return string[] Contents of each pipe, with the same keys.
*/
function read_pipes( $pipes ) {
Comment thread
mrrobot47 marked this conversation as resolved.
$output = array_fill_keys( array_keys( $pipes ), '' );
$open = $pipes;

// stream_select() can't wait on process pipes on Windows.
if ( ! is_windows() ) {
foreach ( $open as $pipe ) {
stream_set_blocking( $pipe, false );
}
while ( $open ) {
$read = $open;
$write = null;
$except = null;
error_clear_last();
// @codingStandardsIgnoreLine
if ( false === @stream_select( $read, $write, $except, null ) ) {
// A caught signal (e.g. site-command's pcntl handlers) interrupts select(); retry instead of falling back.
$error = error_get_last();
if ( $error && false !== stripos( $error['message'], 'interrupted system call' ) ) {
continue;
}
break;
}
foreach ( $read as $key => $pipe ) {
$chunk = fread( $pipe, 65536 );
if ( false !== $chunk ) {
$output[ $key ] .= $chunk;
}
if ( false === $chunk || feof( $pipe ) ) {
unset( $open[ $key ] );
}
}
}
}

// Blocking reads for whatever is left, as before.
foreach ( $open as $key => $pipe ) {
stream_set_blocking( $pipe, true );
$output[ $key ] .= stream_get_contents( $pipe );
}

return $output;
}

/**
* For use by `proc_open_compat()` only. Separated out for ease of testing. Windows only.
* Turns *nix-like `ENV_VAR=blah command` environment variable prefixes into stripped `cmd` with prefixed environment variables added to passed in environment array.
Expand Down
Loading