MediaWiki  1.34.0
Command.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\Shell;
22 
23 use Exception;
26 use Profiler;
27 use Psr\Log\LoggerAwareTrait;
28 use Psr\Log\NullLogger;
29 use Wikimedia\AtEase\AtEase;
30 
36 class Command {
37  use LoggerAwareTrait;
38 
40  protected $command = '';
41 
43  private $limits = [
44  // seconds
45  'time' => 180,
46  // seconds
47  'walltime' => 180,
48  // KB
49  'memory' => 307200,
50  // KB
51  'filesize' => 102400,
52  ];
53 
55  private $env = [];
56 
58  private $method;
59 
61  private $inputString;
62 
64  private $doIncludeStderr = false;
65 
67  private $doLogStderr = false;
68 
70  private $everExecuted = false;
71 
73  private $cgroup = false;
74 
80  protected $restrictions = 0;
81 
87  public function __construct() {
88  if ( Shell::isDisabled() ) {
89  throw new ShellDisabledError();
90  }
91 
92  $this->setLogger( new NullLogger() );
93  }
94 
98  public function __destruct() {
99  if ( !$this->everExecuted ) {
100  $context = [ 'command' => $this->command ];
101  $message = __CLASS__ . " was instantiated, but execute() was never called.";
102  if ( $this->method ) {
103  $message .= ' Calling method: {method}.';
104  $context['method'] = $this->method;
105  }
106  $message .= ' Command: {command}';
107  $this->logger->warning( $message, $context );
108  }
109  }
110 
118  public function params( ...$args ): Command {
119  if ( count( $args ) === 1 && is_array( reset( $args ) ) ) {
120  // If only one argument has been passed, and that argument is an array,
121  // treat it as a list of arguments
122  $args = reset( $args );
123  }
124  $this->command = trim( $this->command . ' ' . Shell::escape( $args ) );
125 
126  return $this;
127  }
128 
136  public function unsafeParams( ...$args ): Command {
137  if ( count( $args ) === 1 && is_array( reset( $args ) ) ) {
138  // If only one argument has been passed, and that argument is an array,
139  // treat it as a list of arguments
140  $args = reset( $args );
141  }
142  $args = array_filter( $args,
143  function ( $value ) {
144  return $value !== null;
145  }
146  );
147  $this->command = trim( $this->command . ' ' . implode( ' ', $args ) );
148 
149  return $this;
150  }
151 
159  public function limits( array $limits ): Command {
160  if ( !isset( $limits['walltime'] ) && isset( $limits['time'] ) ) {
161  // Emulate the behavior of old wfShellExec() where walltime fell back on time
162  // if the latter was overridden and the former wasn't
163  $limits['walltime'] = $limits['time'];
164  }
165  $this->limits = $limits + $this->limits;
166 
167  return $this;
168  }
169 
176  public function environment( array $env ): Command {
177  $this->env = $env;
178 
179  return $this;
180  }
181 
188  public function profileMethod( $method ): Command {
189  $this->method = $method;
190 
191  return $this;
192  }
193 
200  public function input( $inputString ): Command {
201  $this->inputString = is_null( $inputString ) ? null : (string)$inputString;
202 
203  return $this;
204  }
205 
213  public function includeStderr( $yesno = true ): Command {
214  $this->doIncludeStderr = $yesno;
215 
216  return $this;
217  }
218 
225  public function logStderr( $yesno = true ): Command {
226  $this->doLogStderr = $yesno;
227 
228  return $this;
229  }
230 
237  public function cgroup( $cgroup ): Command {
238  $this->cgroup = $cgroup;
239 
240  return $this;
241  }
242 
250  public function restrict( $restrictions ): Command {
251  $this->restrictions |= $restrictions;
252 
253  return $this;
254  }
255 
263  protected function hasRestriction( $restriction ) {
264  return ( $this->restrictions & $restriction ) === $restriction;
265  }
266 
277  public function whitelistPaths( array $paths ): Command {
278  // Default implementation is a no-op
279  return $this;
280  }
281 
289  protected function buildFinalCommand( $command ) {
290  $envcmd = '';
291  foreach ( $this->env as $k => $v ) {
292  if ( wfIsWindows() ) {
293  /* Surrounding a set in quotes (method used by wfEscapeShellArg) makes the quotes themselves
294  * appear in the environment variable, so we must use carat escaping as documented in
295  * https://technet.microsoft.com/en-us/library/cc723564.aspx
296  * Note however that the quote isn't listed there, but is needed, and the parentheses
297  * are listed there but doesn't appear to need it.
298  */
299  $envcmd .= "set $k=" . preg_replace( '/([&|()<>^"])/', '^\\1', $v ) . '&& ';
300  } else {
301  /* Assume this is a POSIX shell, thus required to accept variable assignments before the command
302  * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_09_01
303  */
304  $envcmd .= "$k=" . escapeshellarg( $v ) . ' ';
305  }
306  }
307 
308  $useLogPipe = false;
309  $cmd = $envcmd . trim( $command );
310 
311  if ( is_executable( '/bin/bash' ) ) {
312  $time = intval( $this->limits['time'] );
313  $wallTime = intval( $this->limits['walltime'] );
314  $mem = intval( $this->limits['memory'] );
315  $filesize = intval( $this->limits['filesize'] );
316 
317  if ( $time > 0 || $mem > 0 || $filesize > 0 || $wallTime > 0 ) {
318  $cmd = '/bin/bash ' . escapeshellarg( __DIR__ . '/limit.sh' ) . ' ' .
319  escapeshellarg( $cmd ) . ' ' .
320  escapeshellarg(
321  "MW_INCLUDE_STDERR=" . ( $this->doIncludeStderr ? '1' : '' ) . ';' .
322  "MW_CPU_LIMIT=$time; " .
323  'MW_CGROUP=' . escapeshellarg( $this->cgroup ) . '; ' .
324  "MW_MEM_LIMIT=$mem; " .
325  "MW_FILE_SIZE_LIMIT=$filesize; " .
326  "MW_WALL_CLOCK_LIMIT=$wallTime; " .
327  "MW_USE_LOG_PIPE=yes"
328  );
329  $useLogPipe = true;
330  }
331  }
332  if ( !$useLogPipe && $this->doIncludeStderr ) {
333  $cmd .= ' 2>&1';
334  }
335 
336  return [ $cmd, $useLogPipe ];
337  }
338 
348  public function execute() {
349  $this->everExecuted = true;
350 
351  $profileMethod = $this->method ?: wfGetCaller();
352 
353  list( $cmd, $useLogPipe ) = $this->buildFinalCommand( $this->command );
354 
355  $this->logger->debug( __METHOD__ . ": $cmd" );
356 
357  // Don't try to execute commands that exceed Linux's MAX_ARG_STRLEN.
358  // Other platforms may be more accomodating, but we don't want to be
359  // accomodating, because very long commands probably include user
360  // input. See T129506.
361  if ( strlen( $cmd ) > SHELL_MAX_ARG_STRLEN ) {
362  throw new Exception( __METHOD__ .
363  '(): total length of $cmd must not exceed SHELL_MAX_ARG_STRLEN' );
364  }
365 
366  $desc = [
367  0 => $this->inputString === null ? [ 'file', 'php://stdin', 'r' ] : [ 'pipe', 'r' ],
368  1 => [ 'pipe', 'w' ],
369  2 => [ 'pipe', 'w' ],
370  ];
371  if ( $useLogPipe ) {
372  $desc[3] = [ 'pipe', 'w' ];
373  }
374  $pipes = null;
375  $scoped = Profiler::instance()->scopedProfileIn( __FUNCTION__ . '-' . $profileMethod );
376  $proc = proc_open( $cmd, $desc, $pipes );
377  if ( !$proc ) {
378  $this->logger->error( "proc_open() failed: {command}", [ 'command' => $cmd ] );
379  throw new ProcOpenError();
380  }
381 
382  $buffers = [
383  0 => $this->inputString, // input
384  1 => '', // stdout
385  2 => null, // stderr
386  3 => '', // log
387  ];
388  $emptyArray = [];
389  $status = false;
390  $logMsg = false;
391 
392  /* According to the documentation, it is possible for stream_select()
393  * to fail due to EINTR. I haven't managed to induce this in testing
394  * despite sending various signals. If it did happen, the error
395  * message would take the form:
396  *
397  * stream_select(): unable to select [4]: Interrupted system call (max_fd=5)
398  *
399  * where [4] is the value of the macro EINTR and "Interrupted system
400  * call" is string which according to the Linux manual is "possibly"
401  * localised according to LC_MESSAGES.
402  */
403  $eintr = defined( 'SOCKET_EINTR' ) ? SOCKET_EINTR : 4;
404  $eintrMessage = "stream_select(): unable to select [$eintr]";
405 
406  /* The select(2) system call only guarantees a "sufficiently small write"
407  * can be made without blocking. And on Linux the read might block too
408  * in certain cases, although I don't know if any of them can occur here.
409  * Regardless, set all the pipes to non-blocking to avoid T184171.
410  */
411  foreach ( $pipes as $pipe ) {
412  stream_set_blocking( $pipe, false );
413  }
414 
415  $running = true;
416  $timeout = null;
417  $numReadyPipes = 0;
418 
419  while ( $pipes && ( $running === true || $numReadyPipes !== 0 ) ) {
420  if ( $running ) {
421  $status = proc_get_status( $proc );
422  // If the process has terminated, switch to nonblocking selects
423  // for getting any data still waiting to be read.
424  if ( !$status['running'] ) {
425  $running = false;
426  $timeout = 0;
427  }
428  }
429 
430  // clear get_last_error without actually raising an error
431  // from https://www.php.net/manual/en/function.error-get-last.php#113518
432  // TODO replace with error_clear_last after dropping HHVM
433  // @phan-suppress-next-line PhanTypeMismatchArgumentInternal
434  set_error_handler( function () {
435  }, 0 );
436  AtEase::suppressWarnings();
437  trigger_error( '' );
438  AtEase::restoreWarnings();
439  restore_error_handler();
440 
441  $readPipes = array_filter( $pipes, function ( $fd ) use ( $desc ) {
442  return $desc[$fd][0] === 'pipe' && $desc[$fd][1] === 'r';
443  }, ARRAY_FILTER_USE_KEY );
444  $writePipes = array_filter( $pipes, function ( $fd ) use ( $desc ) {
445  return $desc[$fd][0] === 'pipe' && $desc[$fd][1] === 'w';
446  }, ARRAY_FILTER_USE_KEY );
447  // stream_select parameter names are from the POV of us being able to do the operation;
448  // proc_open desriptor types are from the POV of the process doing it.
449  // So $writePipes is passed as the $read parameter and $readPipes as $write.
450  AtEase::suppressWarnings();
451  $numReadyPipes = stream_select( $writePipes, $readPipes, $emptyArray, $timeout );
452  AtEase::restoreWarnings();
453  if ( $numReadyPipes === false ) {
454  $error = error_get_last();
455  if ( strncmp( $error['message'], $eintrMessage, strlen( $eintrMessage ) ) == 0 ) {
456  continue;
457  } else {
458  trigger_error( $error['message'], E_USER_WARNING );
459  $logMsg = $error['message'];
460  break;
461  }
462  }
463  foreach ( $writePipes + $readPipes as $fd => $pipe ) {
464  // True if a pipe is unblocked for us to write into, false if for reading from
465  $isWrite = array_key_exists( $fd, $readPipes );
466 
467  if ( $isWrite ) {
468  // Don't bother writing if the buffer is empty
469  if ( $buffers[$fd] === '' ) {
470  fclose( $pipes[$fd] );
471  unset( $pipes[$fd] );
472  continue;
473  }
474  $res = fwrite( $pipe, $buffers[$fd], 65536 );
475  } else {
476  $res = fread( $pipe, 65536 );
477  }
478 
479  if ( $res === false ) {
480  $logMsg = 'Error ' . ( $isWrite ? 'writing to' : 'reading from' ) . ' pipe';
481  break 2;
482  }
483 
484  if ( $res === '' || $res === 0 ) {
485  // End of file?
486  if ( feof( $pipe ) ) {
487  fclose( $pipes[$fd] );
488  unset( $pipes[$fd] );
489  }
490  } elseif ( $isWrite ) {
491  $buffers[$fd] = (string)substr( $buffers[$fd], $res );
492  if ( $buffers[$fd] === '' ) {
493  fclose( $pipes[$fd] );
494  unset( $pipes[$fd] );
495  }
496  } else {
497  $buffers[$fd] .= $res;
498  if ( $fd === 3 && strpos( $res, "\n" ) !== false ) {
499  // For the log FD, every line is a separate log entry.
500  $lines = explode( "\n", $buffers[3] );
501  $buffers[3] = array_pop( $lines );
502  foreach ( $lines as $line ) {
503  $this->logger->info( $line );
504  }
505  }
506  }
507  }
508  }
509 
510  foreach ( $pipes as $pipe ) {
511  fclose( $pipe );
512  }
513 
514  // Use the status previously collected if possible, since proc_get_status()
515  // just calls waitpid() which will not return anything useful the second time.
516  if ( $running ) {
517  $status = proc_get_status( $proc );
518  }
519 
520  if ( $logMsg !== false ) {
521  // Read/select error
522  $retval = -1;
523  proc_close( $proc );
524  } elseif ( $status['signaled'] ) {
525  $logMsg = "Exited with signal {$status['termsig']}";
526  $retval = 128 + $status['termsig'];
527  proc_close( $proc );
528  } else {
529  if ( $status['running'] ) {
530  $retval = proc_close( $proc );
531  } else {
532  $retval = $status['exitcode'];
533  proc_close( $proc );
534  }
535  if ( $retval == 127 ) {
536  $logMsg = "Possibly missing executable file";
537  } elseif ( $retval >= 129 && $retval <= 192 ) {
538  $logMsg = "Probably exited with signal " . ( $retval - 128 );
539  }
540  }
541 
542  if ( $logMsg !== false ) {
543  $this->logger->warning( "$logMsg: {command}", [ 'command' => $cmd ] );
544  }
545 
546  if ( $buffers[2] && $this->doLogStderr ) {
547  $this->logger->error( "Error running {command}: {error}", [
548  'command' => $cmd,
549  'error' => $buffers[2],
550  'exitcode' => $retval,
551  'exception' => new Exception( 'Shell error' ),
552  ] );
553  }
554 
555  return new Result( $retval, $buffers[1], $buffers[2] );
556  }
557 
565  public function __toString() {
566  return "#Command: {$this->command}";
567  }
568 }
MediaWiki\Shell\Command\$everExecuted
bool $everExecuted
Definition: Command.php:70
MediaWiki\Shell\Result
Returned by MediaWiki\Shell\Command::execute()
Definition: Result.php:28
MediaWiki\Shell\Command\$doIncludeStderr
bool $doIncludeStderr
Definition: Command.php:64
MediaWiki\Shell\Command\__destruct
__destruct()
Makes sure the programmer didn't forget to execute the command after all.
Definition: Command.php:98
MediaWiki\Shell\Command\unsafeParams
unsafeParams(... $args)
Adds unsafe parameters to the command.
Definition: Command.php:136
MediaWiki\ProcOpenError
Definition: ProcOpenError.php:25
MediaWiki\Shell\Command\profileMethod
profileMethod( $method)
Sets calling function for profiler.
Definition: Command.php:188
MediaWiki\Shell\Command\restrict
restrict( $restrictions)
Set additional restrictions for this request.
Definition: Command.php:250
Profiler\instance
static instance()
Singleton.
Definition: Profiler.php:63
MediaWiki\Shell\Command
Class used for executing shell commands.
Definition: Command.php:36
MediaWiki\Shell\Command\$inputString
string null $inputString
Definition: Command.php:61
MediaWiki\Shell\Command\cgroup
cgroup( $cgroup)
Sets cgroup for this command.
Definition: Command.php:237
MediaWiki\Shell\Command\limits
limits(array $limits)
Sets execution limits.
Definition: Command.php:159
MediaWiki\Shell\Command\hasRestriction
hasRestriction( $restriction)
Bitfield helper on whether a specific restriction is enabled.
Definition: Command.php:263
MediaWiki\Shell\Command\$restrictions
int $restrictions
Bitfield with restrictions.
Definition: Command.php:80
$res
$res
Definition: testCompression.php:52
MediaWiki\Shell\Command\logStderr
logStderr( $yesno=true)
When enabled, text sent to stderr will be logged with a level of 'error'.
Definition: Command.php:225
Profiler
Profiler base class that defines the interface and some trivial functionality.
Definition: Profiler.php:33
SHELL_MAX_ARG_STRLEN
const SHELL_MAX_ARG_STRLEN
Definition: Defines.php:250
MediaWiki\Shell\Command\$env
string[] $env
Definition: Command.php:55
MediaWiki\Shell\Command\__toString
__toString()
Returns the final command line before environment/limiting, etc are applied.
Definition: Command.php:565
$lines
$lines
Definition: router.php:61
MediaWiki\Shell\Command\includeStderr
includeStderr( $yesno=true)
Controls whether stderr should be included in stdout, including errors from limit....
Definition: Command.php:213
MediaWiki\Shell\Command\environment
environment(array $env)
Sets environment variables which should be added to the executed command environment.
Definition: Command.php:176
MediaWiki\Shell\Shell\isDisabled
static isDisabled()
Check if this class is effectively disabled via php.ini config.
Definition: Shell.php:137
$line
$line
Definition: cdb.php:59
MediaWiki\Shell\Command\$command
string $command
Definition: Command.php:40
MediaWiki\Shell\Command\$doLogStderr
bool $doLogStderr
Definition: Command.php:67
wfIsWindows
wfIsWindows()
Check if the operating system is Windows.
Definition: GlobalFunctions.php:1907
MediaWiki\ShellDisabledError
Definition: ShellDisabledError.php:28
MediaWiki\Shell\Command\input
input( $inputString)
Sends the provided input to the command.
Definition: Command.php:200
MediaWiki\Shell\Shell\escape
static escape(... $args)
Version of escapeshellarg() that works better on Windows.
Definition: Shell.php:163
$args
if( $line===false) $args
Definition: cdb.php:64
$status
return $status
Definition: SyntaxHighlight.php:347
MediaWiki\Shell
Copyright (C) 2017 Kunal Mehta legoktm@member.fsf.org
Definition: Command.php:21
MediaWiki\Shell\Command\$limits
array $limits
Definition: Command.php:43
MediaWiki\Shell\Command\$cgroup
string false $cgroup
Definition: Command.php:73
MediaWiki\Shell\Command\$method
string $method
Definition: Command.php:58
MediaWiki\$context
IContextSource $context
Definition: MediaWiki.php:38
MediaWiki\Shell\Command\whitelistPaths
whitelistPaths(array $paths)
If called, only the files/directories that are whitelisted will be available to the shell command.
Definition: Command.php:277
MediaWiki\Shell\Command\buildFinalCommand
buildFinalCommand( $command)
String together all the options and build the final command to execute.
Definition: Command.php:289
MediaWiki\Shell\Command\execute
execute()
Executes command.
Definition: Command.php:348
wfGetCaller
wfGetCaller( $level=2)
Get the name of the function which called this function wfGetCaller( 1 ) is the function with the wfG...
Definition: GlobalFunctions.php:1454
MediaWiki\Shell\Command\__construct
__construct()
Don't call directly, instead use Shell::command()
Definition: Command.php:87
MediaWiki\Shell\Command\params
params(... $args)
Adds parameters to the command.
Definition: Command.php:118