3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 namespace MediaWiki\Shell
;
24 use MediaWiki\ProcOpenError
;
25 use MediaWiki\ShellDisabledError
;
27 use Psr\Log\LoggerAwareTrait
;
28 use Psr\Log\NullLogger
;
31 * Class used for executing shell commands
39 protected $command = '';
59 /** @var string|null */
63 private $doIncludeStderr = false;
66 private $doLogStderr = false;
69 private $everExecuted = false;
71 /** @var string|false */
72 private $cgroup = false;
75 * bitfield with restrictions
79 protected $restrictions = 0;
82 * Constructor. Don't call directly, instead use Shell::command()
84 * @throws ShellDisabledError
86 public function __construct() {
87 if ( Shell
::isDisabled() ) {
88 throw new ShellDisabledError();
91 $this->setLogger( new NullLogger() );
95 * Destructor. Makes sure programmer didn't forget to execute the command after all
97 public function __destruct() {
98 if ( !$this->everExecuted
) {
99 $context = [ 'command' => $this->command
];
100 $message = __CLASS__
. " was instantiated, but execute() was never called.";
101 if ( $this->method
) {
102 $message .= ' Calling method: {method}.';
103 $context['method'] = $this->method
;
105 $message .= ' Command: {command}';
106 $this->logger
->warning( $message, $context );
111 * Adds parameters to the command. All parameters are sanitized via Shell::escape().
112 * Null values are ignored.
114 * @param string|string[] ...$args
117 public function params( ...$args ) {
118 if ( count( $args ) === 1 && is_array( reset( $args ) ) ) {
119 // If only one argument has been passed, and that argument is an array,
120 // treat it as a list of arguments
121 $args = reset( $args );
123 $this->command
= trim( $this->command
. ' ' . Shell
::escape( $args ) );
129 * Adds unsafe parameters to the command. These parameters are NOT sanitized in any way.
130 * Null values are ignored.
132 * @param string|string[] ...$args
135 public function unsafeParams( ...$args ) {
136 if ( count( $args ) === 1 && is_array( reset( $args ) ) ) {
137 // If only one argument has been passed, and that argument is an array,
138 // treat it as a list of arguments
139 $args = reset( $args );
141 $args = array_filter( $args,
142 function ( $value ) {
143 return $value !== null;
146 $this->command
= trim( $this->command
. ' ' . implode( ' ', $args ) );
152 * Sets execution limits
154 * @param array $limits Associative array of limits. Keys (all optional):
155 * filesize (for ulimit -f), memory, time, walltime.
158 public function limits( array $limits ) {
159 if ( !isset( $limits['walltime'] ) && isset( $limits['time'] ) ) {
160 // Emulate the behavior of old wfShellExec() where walltime fell back on time
161 // if the latter was overridden and the former wasn't
162 $limits['walltime'] = $limits['time'];
164 $this->limits
= $limits +
$this->limits
;
170 * Sets environment variables which should be added to the executed command environment
172 * @param string[] $env array of variable name => value
175 public function environment( array $env ) {
182 * Sets calling function for profiler. By default, the caller for execute() will be used.
184 * @param string $method
187 public function profileMethod( $method ) {
188 $this->method
= $method;
194 * Sends the provided input to the command.
195 * When set to null (default), the command will use the standard input.
196 * @param string|null $inputString
199 public function input( $inputString ) {
200 $this->inputString
= is_null( $inputString ) ?
null : (string)$inputString;
206 * Controls whether stderr should be included in stdout, including errors from limit.sh.
207 * Default: don't include.
212 public function includeStderr( $yesno = true ) {
213 $this->doIncludeStderr
= $yesno;
219 * When enabled, text sent to stderr will be logged with a level of 'error'.
224 public function logStderr( $yesno = true ) {
225 $this->doLogStderr
= $yesno;
231 * Sets cgroup for this command
233 * @param string|false $cgroup Absolute file path to the cgroup, or false to not use a cgroup
236 public function cgroup( $cgroup ) {
237 $this->cgroup
= $cgroup;
243 * Set additional restrictions for this request
246 * @param int $restrictions
249 public function restrict( $restrictions ) {
250 $this->restrictions |
= $restrictions;
256 * Bitfield helper on whether a specific restriction is enabled
258 * @param int $restriction
262 protected function hasRestriction( $restriction ) {
263 return ( $this->restrictions
& $restriction ) === $restriction;
267 * If called, only the files/directories that are
268 * whitelisted will be available to the shell command.
270 * limit.sh will always be whitelisted
272 * @param string[] $paths
276 public function whitelistPaths( array $paths ) {
277 // Default implementation is a no-op
282 * String together all the options and build the final command
285 * @param string $command Already-escaped command to run
286 * @return array [ command, whether to use log pipe ]
288 protected function buildFinalCommand( $command ) {
290 foreach ( $this->env
as $k => $v ) {
291 if ( wfIsWindows() ) {
292 /* Surrounding a set in quotes (method used by wfEscapeShellArg) makes the quotes themselves
293 * appear in the environment variable, so we must use carat escaping as documented in
294 * https://technet.microsoft.com/en-us/library/cc723564.aspx
295 * Note however that the quote isn't listed there, but is needed, and the parentheses
296 * are listed there but doesn't appear to need it.
298 $envcmd .= "set $k=" . preg_replace( '/([&|()<>^"])/', '^\\1', $v ) . '&& ';
300 /* Assume this is a POSIX shell, thus required to accept variable assignments before the command
301 * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_09_01
303 $envcmd .= "$k=" . escapeshellarg( $v ) . ' ';
308 $cmd = $envcmd . trim( $command );
310 if ( is_executable( '/bin/bash' ) ) {
311 $time = intval( $this->limits
['time'] );
312 $wallTime = intval( $this->limits
['walltime'] );
313 $mem = intval( $this->limits
['memory'] );
314 $filesize = intval( $this->limits
['filesize'] );
316 if ( $time > 0 ||
$mem > 0 ||
$filesize > 0 ||
$wallTime > 0 ) {
317 $cmd = '/bin/bash ' . escapeshellarg( __DIR__
. '/limit.sh' ) . ' ' .
318 escapeshellarg( $cmd ) . ' ' .
320 "MW_INCLUDE_STDERR=" . ( $this->doIncludeStderr ?
'1' : '' ) . ';' .
321 "MW_CPU_LIMIT=$time; " .
322 'MW_CGROUP=' . escapeshellarg( $this->cgroup
) . '; ' .
323 "MW_MEM_LIMIT=$mem; " .
324 "MW_FILE_SIZE_LIMIT=$filesize; " .
325 "MW_WALL_CLOCK_LIMIT=$wallTime; " .
326 "MW_USE_LOG_PIPE=yes"
331 if ( !$useLogPipe && $this->doIncludeStderr
) {
335 return [ $cmd, $useLogPipe ];
339 * Executes command. Afterwards, getExitCode() and getOutput() can be used to access execution
344 * @throws ProcOpenError
345 * @throws ShellDisabledError
347 public function execute() {
348 $this->everExecuted
= true;
350 $profileMethod = $this->method ?
: wfGetCaller();
352 list( $cmd, $useLogPipe ) = $this->buildFinalCommand( $this->command
);
354 $this->logger
->debug( __METHOD__
. ": $cmd" );
356 // Don't try to execute commands that exceed Linux's MAX_ARG_STRLEN.
357 // Other platforms may be more accomodating, but we don't want to be
358 // accomodating, because very long commands probably include user
359 // input. See T129506.
360 if ( strlen( $cmd ) > SHELL_MAX_ARG_STRLEN
) {
361 throw new Exception( __METHOD__
.
362 '(): total length of $cmd must not exceed SHELL_MAX_ARG_STRLEN' );
366 0 => $this->inputString
=== null ?
[ 'file', 'php://stdin', 'r' ] : [ 'pipe', 'r' ],
367 1 => [ 'pipe', 'w' ],
368 2 => [ 'pipe', 'w' ],
371 $desc[3] = [ 'pipe', 'w' ];
374 $scoped = Profiler
::instance()->scopedProfileIn( __FUNCTION__
. '-' . $profileMethod );
375 $proc = proc_open( $cmd, $desc, $pipes );
377 $this->logger
->error( "proc_open() failed: {command}", [ 'command' => $cmd ] );
378 throw new ProcOpenError();
382 0 => $this->inputString
, // input
391 /* According to the documentation, it is possible for stream_select()
392 * to fail due to EINTR. I haven't managed to induce this in testing
393 * despite sending various signals. If it did happen, the error
394 * message would take the form:
396 * stream_select(): unable to select [4]: Interrupted system call (max_fd=5)
398 * where [4] is the value of the macro EINTR and "Interrupted system
399 * call" is string which according to the Linux manual is "possibly"
400 * localised according to LC_MESSAGES.
402 $eintr = defined( 'SOCKET_EINTR' ) ? SOCKET_EINTR
: 4;
403 $eintrMessage = "stream_select(): unable to select [$eintr]";
405 /* The select(2) system call only guarantees a "sufficiently small write"
406 * can be made without blocking. And on Linux the read might block too
407 * in certain cases, although I don't know if any of them can occur here.
408 * Regardless, set all the pipes to non-blocking to avoid T184171.
410 foreach ( $pipes as $pipe ) {
411 stream_set_blocking( $pipe, false );
418 while ( $pipes && ( $running === true ||
$numReadyPipes !== 0 ) ) {
420 $status = proc_get_status( $proc );
421 // If the process has terminated, switch to nonblocking selects
422 // for getting any data still waiting to be read.
423 if ( !$status['running'] ) {
429 // clear get_last_error without actually raising an error
430 // from https://www.php.net/manual/en/function.error-get-last.php#113518
431 // TODO replace with clear_last_error when requirements are bumped to PHP7
432 set_error_handler( function () {
434 \Wikimedia\
suppressWarnings();
436 \Wikimedia\restoreWarnings
();
437 restore_error_handler();
439 $readPipes = array_filter( $pipes, function ( $fd ) use ( $desc ) {
440 return $desc[$fd][0] === 'pipe' && $desc[$fd][1] === 'r';
441 }, ARRAY_FILTER_USE_KEY
);
442 $writePipes = array_filter( $pipes, function ( $fd ) use ( $desc ) {
443 return $desc[$fd][0] === 'pipe' && $desc[$fd][1] === 'w';
444 }, ARRAY_FILTER_USE_KEY
);
445 // stream_select parameter names are from the POV of us being able to do the operation;
446 // proc_open desriptor types are from the POV of the process doing it.
447 // So $writePipes is passed as the $read parameter and $readPipes as $write.
448 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
449 $numReadyPipes = @stream_select
( $writePipes, $readPipes, $emptyArray, $timeout );
450 if ( $numReadyPipes === false ) {
451 $error = error_get_last();
452 if ( strncmp( $error['message'], $eintrMessage, strlen( $eintrMessage ) ) == 0 ) {
455 trigger_error( $error['message'], E_USER_WARNING
);
456 $logMsg = $error['message'];
460 foreach ( $writePipes +
$readPipes as $fd => $pipe ) {
461 // True if a pipe is unblocked for us to write into, false if for reading from
462 $isWrite = array_key_exists( $fd, $readPipes );
465 // Don't bother writing if the buffer is empty
466 if ( $buffers[$fd] === '' ) {
467 fclose( $pipes[$fd] );
468 unset( $pipes[$fd] );
471 $res = fwrite( $pipe, $buffers[$fd], 65536 );
473 $res = fread( $pipe, 65536 );
476 if ( $res === false ) {
477 $logMsg = 'Error ' . ( $isWrite ?
'writing to' : 'reading from' ) . ' pipe';
481 if ( $res === '' ||
$res === 0 ) {
483 if ( feof( $pipe ) ) {
484 fclose( $pipes[$fd] );
485 unset( $pipes[$fd] );
487 } elseif ( $isWrite ) {
488 $buffers[$fd] = (string)substr( $buffers[$fd], $res );
489 if ( $buffers[$fd] === '' ) {
490 fclose( $pipes[$fd] );
491 unset( $pipes[$fd] );
494 $buffers[$fd] .= $res;
495 if ( $fd === 3 && strpos( $res, "\n" ) !== false ) {
496 // For the log FD, every line is a separate log entry.
497 $lines = explode( "\n", $buffers[3] );
498 $buffers[3] = array_pop( $lines );
499 foreach ( $lines as $line ) {
500 $this->logger
->info( $line );
507 foreach ( $pipes as $pipe ) {
511 // Use the status previously collected if possible, since proc_get_status()
512 // just calls waitpid() which will not return anything useful the second time.
514 $status = proc_get_status( $proc );
517 if ( $logMsg !== false ) {
521 } elseif ( $status['signaled'] ) {
522 $logMsg = "Exited with signal {$status['termsig']}";
523 $retval = 128 +
$status['termsig'];
526 if ( $status['running'] ) {
527 $retval = proc_close( $proc );
529 $retval = $status['exitcode'];
532 if ( $retval == 127 ) {
533 $logMsg = "Possibly missing executable file";
534 } elseif ( $retval >= 129 && $retval <= 192 ) {
535 $logMsg = "Probably exited with signal " . ( $retval - 128 );
539 if ( $logMsg !== false ) {
540 $this->logger
->warning( "$logMsg: {command}", [ 'command' => $cmd ] );
543 if ( $buffers[2] && $this->doLogStderr
) {
544 $this->logger
->error( "Error running {command}: {error}", [
546 'error' => $buffers[2],
547 'exitcode' => $retval,
548 'exception' => new Exception( 'Shell error' ),
552 return new Result( $retval, $buffers[1], $buffers[2] );
556 * Returns the final command line before environment/limiting, etc are applied.
557 * Use string conversion only for debugging, don't try to pass this to
558 * some other execution medium.
562 public function __toString() {
563 return "#Command: {$this->command}";