998b3ed905c2c75460bd247850c59d5addaeab19
[lhc/web/wiklou.git] / includes / shell / Command.php
1 <?php
2 /**
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.
7 *
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.
12 *
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
17 *
18 * @file
19 */
20
21 namespace MediaWiki\Shell;
22
23 use Exception;
24 use MediaWiki\ProcOpenError;
25 use MediaWiki\ShellDisabledError;
26 use Profiler;
27 use Psr\Log\LoggerAwareTrait;
28 use Psr\Log\NullLogger;
29
30 /**
31 * Class used for executing shell commands
32 *
33 * @since 1.30
34 */
35 class Command {
36 use LoggerAwareTrait;
37
38 /** @var string */
39 private $command = '';
40
41 /** @var array */
42 private $limits = [
43 // seconds
44 'time' => 180,
45 // seconds
46 'walltime' => 180,
47 // KB
48 'memory' => 307200,
49 // KB
50 'filesize' => 102400,
51 ];
52
53 /** @var string[] */
54 private $env = [];
55
56 /** @var string */
57 private $method;
58
59 /** @var bool */
60 private $doIncludeStderr = false;
61
62 /** @var bool */
63 private $doLogStderr = false;
64
65 /** @var bool */
66 private $everExecuted = false;
67
68 /** @var string|false */
69 private $cgroup = false;
70
71 /**
72 * bitfield with restrictions
73 *
74 * @var int
75 */
76 protected $restrictions = 0;
77
78 /**
79 * Constructor. Don't call directly, instead use Shell::command()
80 *
81 * @throws ShellDisabledError
82 */
83 public function __construct() {
84 if ( Shell::isDisabled() ) {
85 throw new ShellDisabledError();
86 }
87
88 $this->setLogger( new NullLogger() );
89 }
90
91 /**
92 * Destructor. Makes sure programmer didn't forget to execute the command after all
93 */
94 public function __destruct() {
95 if ( !$this->everExecuted ) {
96 $context = [ 'command' => $this->command ];
97 $message = __CLASS__ . " was instantiated, but execute() was never called.";
98 if ( $this->method ) {
99 $message .= ' Calling method: {method}.';
100 $context['method'] = $this->method;
101 }
102 $message .= ' Command: {command}';
103 $this->logger->warning( $message, $context );
104 }
105 }
106
107 /**
108 * Adds parameters to the command. All parameters are sanitized via Shell::escape().
109 * Null values are ignored.
110 *
111 * @param string|string[] $args,...
112 * @return $this
113 */
114 public function params( /* ... */ ) {
115 $args = func_get_args();
116 if ( count( $args ) === 1 && is_array( reset( $args ) ) ) {
117 // If only one argument has been passed, and that argument is an array,
118 // treat it as a list of arguments
119 $args = reset( $args );
120 }
121 $this->command = trim( $this->command . ' ' . Shell::escape( $args ) );
122
123 return $this;
124 }
125
126 /**
127 * Adds unsafe parameters to the command. These parameters are NOT sanitized in any way.
128 * Null values are ignored.
129 *
130 * @param string|string[] $args,...
131 * @return $this
132 */
133 public function unsafeParams( /* ... */ ) {
134 $args = func_get_args();
135 if ( count( $args ) === 1 && is_array( reset( $args ) ) ) {
136 // If only one argument has been passed, and that argument is an array,
137 // treat it as a list of arguments
138 $args = reset( $args );
139 }
140 $args = array_filter( $args,
141 function ( $value ) {
142 return $value !== null;
143 }
144 );
145 $this->command = trim( $this->command . ' ' . implode( ' ', $args ) );
146
147 return $this;
148 }
149
150 /**
151 * Sets execution limits
152 *
153 * @param array $limits Associative array of limits. Keys (all optional):
154 * filesize (for ulimit -f), memory, time, walltime.
155 * @return $this
156 */
157 public function limits( array $limits ) {
158 if ( !isset( $limits['walltime'] ) && isset( $limits['time'] ) ) {
159 // Emulate the behavior of old wfShellExec() where walltime fell back on time
160 // if the latter was overridden and the former wasn't
161 $limits['walltime'] = $limits['time'];
162 }
163 $this->limits = $limits + $this->limits;
164
165 return $this;
166 }
167
168 /**
169 * Sets environment variables which should be added to the executed command environment
170 *
171 * @param string[] $env array of variable name => value
172 * @return $this
173 */
174 public function environment( array $env ) {
175 $this->env = $env;
176
177 return $this;
178 }
179
180 /**
181 * Sets calling function for profiler. By default, the caller for execute() will be used.
182 *
183 * @param string $method
184 * @return $this
185 */
186 public function profileMethod( $method ) {
187 $this->method = $method;
188
189 return $this;
190 }
191
192 /**
193 * Controls whether stderr should be included in stdout, including errors from limit.sh.
194 * Default: don't include.
195 *
196 * @param bool $yesno
197 * @return $this
198 */
199 public function includeStderr( $yesno = true ) {
200 $this->doIncludeStderr = $yesno;
201
202 return $this;
203 }
204
205 /**
206 * When enabled, text sent to stderr will be logged with a level of 'error'.
207 *
208 * @param bool $yesno
209 * @return $this
210 */
211 public function logStderr( $yesno = true ) {
212 $this->doLogStderr = $yesno;
213
214 return $this;
215 }
216
217 /**
218 * Sets cgroup for this command
219 *
220 * @param string|false $cgroup Absolute file path to the cgroup, or false to not use a cgroup
221 * @return $this
222 */
223 public function cgroup( $cgroup ) {
224 $this->cgroup = $cgroup;
225
226 return $this;
227 }
228
229 /**
230 * Set additional restrictions for this request
231 *
232 * @since 1.31
233 * @param int $restrictions
234 * @return $this
235 */
236 public function restrict( $restrictions ) {
237 $this->restrictions |= $restrictions;
238
239 return $this;
240 }
241
242 /**
243 * Bitfield helper on whether a specific restriction is enabled
244 *
245 * @param int $restriction
246 *
247 * @return bool
248 */
249 protected function hasRestriction( $restriction ) {
250 return ( $this->restrictions & $restriction ) === $restriction;
251 }
252
253 /**
254 * If called, only the files/directories that are
255 * whitelisted will be available to the shell command.
256 *
257 * limit.sh will always be whitelisted
258 *
259 * @param string[] $paths
260 *
261 * @return $this
262 */
263 public function whitelistPaths( array $paths ) {
264 // Default implementation is a no-op
265 return $this;
266 }
267
268 /**
269 * String together all the options and build the final command
270 * to execute
271 *
272 * @return array [ command, whether to use log pipe ]
273 */
274 protected function buildFinalCommand() {
275 $envcmd = '';
276 foreach ( $this->env as $k => $v ) {
277 if ( wfIsWindows() ) {
278 /* Surrounding a set in quotes (method used by wfEscapeShellArg) makes the quotes themselves
279 * appear in the environment variable, so we must use carat escaping as documented in
280 * https://technet.microsoft.com/en-us/library/cc723564.aspx
281 * Note however that the quote isn't listed there, but is needed, and the parentheses
282 * are listed there but doesn't appear to need it.
283 */
284 $envcmd .= "set $k=" . preg_replace( '/([&|()<>^"])/', '^\\1', $v ) . '&& ';
285 } else {
286 /* Assume this is a POSIX shell, thus required to accept variable assignments before the command
287 * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_09_01
288 */
289 $envcmd .= "$k=" . escapeshellarg( $v ) . ' ';
290 }
291 }
292
293 $useLogPipe = false;
294 $cmd = $envcmd . trim( $this->command );
295
296 if ( is_executable( '/bin/bash' ) ) {
297 $time = intval( $this->limits['time'] );
298 $wallTime = intval( $this->limits['walltime'] );
299 $mem = intval( $this->limits['memory'] );
300 $filesize = intval( $this->limits['filesize'] );
301
302 if ( $time > 0 || $mem > 0 || $filesize > 0 || $wallTime > 0 ) {
303 $cmd = '/bin/bash ' . escapeshellarg( __DIR__ . '/limit.sh' ) . ' ' .
304 escapeshellarg( $cmd ) . ' ' .
305 escapeshellarg(
306 "MW_INCLUDE_STDERR=" . ( $this->doIncludeStderr ? '1' : '' ) . ';' .
307 "MW_CPU_LIMIT=$time; " .
308 'MW_CGROUP=' . escapeshellarg( $this->cgroup ) . '; ' .
309 "MW_MEM_LIMIT=$mem; " .
310 "MW_FILE_SIZE_LIMIT=$filesize; " .
311 "MW_WALL_CLOCK_LIMIT=$wallTime; " .
312 "MW_USE_LOG_PIPE=yes"
313 );
314 $useLogPipe = true;
315 }
316 }
317 if ( !$useLogPipe && $this->doIncludeStderr ) {
318 $cmd .= ' 2>&1';
319 }
320
321 return [ $cmd, $useLogPipe ];
322 }
323
324 /**
325 * Executes command. Afterwards, getExitCode() and getOutput() can be used to access execution
326 * results.
327 *
328 * @return Result
329 * @throws Exception
330 * @throws ProcOpenError
331 * @throws ShellDisabledError
332 */
333 public function execute() {
334 $this->everExecuted = true;
335
336 $profileMethod = $this->method ?: wfGetCaller();
337
338 list( $cmd, $useLogPipe ) = $this->buildFinalCommand();
339
340 $this->logger->debug( __METHOD__ . ": $cmd" );
341
342 // Don't try to execute commands that exceed Linux's MAX_ARG_STRLEN.
343 // Other platforms may be more accomodating, but we don't want to be
344 // accomodating, because very long commands probably include user
345 // input. See T129506.
346 if ( strlen( $cmd ) > SHELL_MAX_ARG_STRLEN ) {
347 throw new Exception( __METHOD__ .
348 '(): total length of $cmd must not exceed SHELL_MAX_ARG_STRLEN' );
349 }
350
351 $desc = [
352 0 => [ 'file', 'php://stdin', 'r' ],
353 1 => [ 'pipe', 'w' ],
354 2 => [ 'pipe', 'w' ],
355 ];
356 if ( $useLogPipe ) {
357 $desc[3] = [ 'pipe', 'w' ];
358 }
359 $pipes = null;
360 $scoped = Profiler::instance()->scopedProfileIn( __FUNCTION__ . '-' . $profileMethod );
361 $proc = proc_open( $cmd, $desc, $pipes );
362 if ( !$proc ) {
363 $this->logger->error( "proc_open() failed: {command}", [ 'command' => $cmd ] );
364 throw new ProcOpenError();
365 }
366 $outBuffer = $logBuffer = '';
367 $errBuffer = null;
368 $emptyArray = [];
369 $status = false;
370 $logMsg = false;
371
372 /* According to the documentation, it is possible for stream_select()
373 * to fail due to EINTR. I haven't managed to induce this in testing
374 * despite sending various signals. If it did happen, the error
375 * message would take the form:
376 *
377 * stream_select(): unable to select [4]: Interrupted system call (max_fd=5)
378 *
379 * where [4] is the value of the macro EINTR and "Interrupted system
380 * call" is string which according to the Linux manual is "possibly"
381 * localised according to LC_MESSAGES.
382 */
383 $eintr = defined( 'SOCKET_EINTR' ) ? SOCKET_EINTR : 4;
384 $eintrMessage = "stream_select(): unable to select [$eintr]";
385
386 $running = true;
387 $timeout = null;
388 $numReadyPipes = 0;
389
390 while ( $running === true || $numReadyPipes !== 0 ) {
391 if ( $running ) {
392 $status = proc_get_status( $proc );
393 // If the process has terminated, switch to nonblocking selects
394 // for getting any data still waiting to be read.
395 if ( !$status['running'] ) {
396 $running = false;
397 $timeout = 0;
398 }
399 }
400
401 $readyPipes = $pipes;
402
403 // clear get_last_error without actually raising an error
404 // from http://php.net/manual/en/function.error-get-last.php#113518
405 // TODO replace with clear_last_error when requirements are bumped to PHP7
406 set_error_handler( function () {
407 }, 0 );
408 // @codingStandardsIgnoreStart Generic.PHP.NoSilencedErrors.Discouraged
409 @trigger_error( '' );
410 // @codingStandardsIgnoreEnd
411 restore_error_handler();
412
413 // @codingStandardsIgnoreStart Generic.PHP.NoSilencedErrors.Discouraged
414 $numReadyPipes = @stream_select( $readyPipes, $emptyArray, $emptyArray, $timeout );
415 // @codingStandardsIgnoreEnd
416 if ( $numReadyPipes === false ) {
417 $error = error_get_last();
418 if ( strncmp( $error['message'], $eintrMessage, strlen( $eintrMessage ) ) == 0 ) {
419 continue;
420 } else {
421 trigger_error( $error['message'], E_USER_WARNING );
422 $logMsg = $error['message'];
423 break;
424 }
425 }
426 foreach ( $readyPipes as $fd => $pipe ) {
427 $block = fread( $pipe, 65536 );
428 if ( $block === '' ) {
429 // End of file
430 fclose( $pipes[$fd] );
431 unset( $pipes[$fd] );
432 if ( !$pipes ) {
433 break 2;
434 }
435 } elseif ( $block === false ) {
436 // Read error
437 $logMsg = "Error reading from pipe";
438 break 2;
439 } elseif ( $fd == 1 ) {
440 // From stdout
441 $outBuffer .= $block;
442 } elseif ( $fd == 2 ) {
443 // From stderr
444 $errBuffer .= $block;
445 } elseif ( $fd == 3 ) {
446 // From log FD
447 $logBuffer .= $block;
448 if ( strpos( $block, "\n" ) !== false ) {
449 $lines = explode( "\n", $logBuffer );
450 $logBuffer = array_pop( $lines );
451 foreach ( $lines as $line ) {
452 $this->logger->info( $line );
453 }
454 }
455 }
456 }
457 }
458
459 foreach ( $pipes as $pipe ) {
460 fclose( $pipe );
461 }
462
463 // Use the status previously collected if possible, since proc_get_status()
464 // just calls waitpid() which will not return anything useful the second time.
465 if ( $running ) {
466 $status = proc_get_status( $proc );
467 }
468
469 if ( $logMsg !== false ) {
470 // Read/select error
471 $retval = -1;
472 proc_close( $proc );
473 } elseif ( $status['signaled'] ) {
474 $logMsg = "Exited with signal {$status['termsig']}";
475 $retval = 128 + $status['termsig'];
476 proc_close( $proc );
477 } else {
478 if ( $status['running'] ) {
479 $retval = proc_close( $proc );
480 } else {
481 $retval = $status['exitcode'];
482 proc_close( $proc );
483 }
484 if ( $retval == 127 ) {
485 $logMsg = "Possibly missing executable file";
486 } elseif ( $retval >= 129 && $retval <= 192 ) {
487 $logMsg = "Probably exited with signal " . ( $retval - 128 );
488 }
489 }
490
491 if ( $logMsg !== false ) {
492 $this->logger->warning( "$logMsg: {command}", [ 'command' => $cmd ] );
493 }
494
495 if ( $errBuffer && $this->doLogStderr ) {
496 $this->logger->error( "Error running {command}: {error}", [
497 'command' => $cmd,
498 'error' => $errBuffer,
499 'exitcode' => $retval,
500 'exception' => new Exception( 'Shell error' ),
501 ] );
502 }
503
504 return new Result( $retval, $outBuffer, $errBuffer );
505 }
506 }