Shell\Command: Move code that builds final shell command into separate method
[lhc/web/wiklou.git] / includes / shell / Command.php
index 864e69a..1816c5a 100644 (file)
@@ -24,6 +24,8 @@ use Exception;
 use MediaWiki\ProcOpenError;
 use MediaWiki\ShellDisabledError;
 use Profiler;
+use Psr\Log\LoggerAwareTrait;
+use Psr\Log\NullLogger;
 
 /**
  * Class used for executing shell commands
@@ -31,11 +33,22 @@ use Profiler;
  * @since 1.30
  */
 class Command {
+       use LoggerAwareTrait;
+
        /** @var string */
        private $command = '';
 
        /** @var array */
-       private $limits = [];
+       private $limits = [
+               // seconds
+               'time' => 180,
+               // seconds
+               'walltime' => 180,
+               // KB
+               'memory' => 307200,
+               // KB
+               'filesize' => 102400,
+       ];
 
        /** @var string[] */
        private $env = [];
@@ -49,13 +62,20 @@ class Command {
        /** @var bool */
        private $everExecuted = false;
 
+       /** @var string|false */
+       private $cgroup = false;
+
        /**
         * Constructor. Don't call directly, instead use Shell::command()
+        *
+        * @throws ShellDisabledError
         */
        public function __construct() {
                if ( Shell::isDisabled() ) {
                        throw new ShellDisabledError();
                }
+
+               $this->setLogger( new NullLogger() );
        }
 
        /**
@@ -63,12 +83,14 @@ class Command {
         */
        public function __destruct() {
                if ( !$this->everExecuted ) {
+                       $context = [ 'command' => $this->command ];
                        $message = __CLASS__ . " was instantiated, but execute() was never called.";
                        if ( $this->method ) {
-                               $message .= " Calling method: {$this->method}.";
+                               $message .= ' Calling method: {method}.';
+                               $context['method'] = $this->method;
                        }
-                       $message .= " Command: {$this->command}";
-                       trigger_error( $message, E_USER_NOTICE );
+                       $message .= ' Command: {command}';
+                       $this->logger->warning( $message, $context );
                }
        }
 
@@ -111,12 +133,17 @@ class Command {
        /**
         * Sets execution limits
         *
-        * @param array $limits Optional array with limits(filesize, memory, time, walltime).
-        *   This overrides the global wgMaxShell* limits.
+        * @param array $limits Associative array of limits. Keys (all optional):
+        *   filesize (for ulimit -f), memory, time, walltime.
         * @return $this
         */
        public function limits( array $limits ) {
-               $this->limits = $limits;
+               if ( !isset( $limits['walltime'] ) && isset( $limits['time'] ) ) {
+                       // Emulate the behavior of old wfShellExec() where walltime fell back on time
+                       // if the latter was overridden and the former wasn't
+                       $limits['walltime'] = $limits['time'];
+               }
+               $this->limits = $limits + $this->limits;
 
                return $this;
        }
@@ -159,22 +186,24 @@ class Command {
        }
 
        /**
-        * Executes command. Afterwards, getExitCode() and getOutput() can be used to access execution
-        * results.
+        * Sets cgroup for this command
         *
-        * @return Result
-        * @throws Exception
-        * @throws ProcOpenError
-        * @throws ShellDisabledError
+        * @param string|false $cgroup Absolute file path to the cgroup, or false to not use a cgroup
+        * @return $this
         */
-       public function execute() {
-               global $IP, $wgMaxShellMemory, $wgMaxShellFileSize, $wgMaxShellTime,
-                          $wgMaxShellWallClockTime, $wgShellCgroup;
-
-               $this->everExecuted = true;
+       public function cgroup( $cgroup ) {
+               $this->cgroup = $cgroup;
 
-               $profileMethod = $this->method ?: wfGetCaller();
+               return $this;
+       }
 
+       /**
+        * String together all the options and build the final command
+        * to execute
+        *
+        * @return array [ command, whether to use log pipe ]
+        */
+       protected function buildFinalCommand() {
                $envcmd = '';
                foreach ( $this->env as $k => $v ) {
                        if ( wfIsWindows() ) {
@@ -193,43 +222,54 @@ class Command {
                        }
                }
 
+               $useLogPipe = false;
                $cmd = $envcmd . trim( $this->command );
 
-               $useLogPipe = false;
                if ( is_executable( '/bin/bash' ) ) {
-                       $time = intval( isset( $this->limits['time'] ) ? $this->limits['time'] : $wgMaxShellTime );
-                       if ( isset( $this->limits['walltime'] ) ) {
-                               $wallTime = intval( $this->limits['walltime'] );
-                       } elseif ( isset( $this->limits['time'] ) ) {
-                               $wallTime = $time;
-                       } else {
-                               $wallTime = intval( $wgMaxShellWallClockTime );
-                       }
-                       $mem = intval( isset( $this->limits['memory'] ) ? $this->limits['memory'] : $wgMaxShellMemory );
-                       $filesize = intval( isset( $this->limits['filesize'] )
-                               ? $this->limits['filesize']
-                               : $wgMaxShellFileSize );
+                       $time = intval( $this->limits['time'] );
+                       $wallTime = intval( $this->limits['walltime'] );
+                       $mem = intval( $this->limits['memory'] );
+                       $filesize = intval( $this->limits['filesize'] );
 
                        if ( $time > 0 || $mem > 0 || $filesize > 0 || $wallTime > 0 ) {
-                               $cmd = '/bin/bash ' . escapeshellarg( "$IP/includes/limit.sh" ) . ' ' .
-                                          escapeshellarg( $cmd ) . ' ' .
-                                          escapeshellarg(
-                                                  "MW_INCLUDE_STDERR=" . ( $this->useStderr ? '1' : '' ) . ';' .
-                                                  "MW_CPU_LIMIT=$time; " .
-                                                  'MW_CGROUP=' . escapeshellarg( $wgShellCgroup ) . '; ' .
-                                                  "MW_MEM_LIMIT=$mem; " .
-                                                  "MW_FILE_SIZE_LIMIT=$filesize; " .
-                                                  "MW_WALL_CLOCK_LIMIT=$wallTime; " .
-                                                  "MW_USE_LOG_PIPE=yes"
-                                          );
+                               $cmd = '/bin/bash ' . escapeshellarg( __DIR__ . '/limit.sh' ) . ' ' .
+                                       escapeshellarg( $cmd ) . ' ' .
+                                       escapeshellarg(
+                                               "MW_INCLUDE_STDERR=" . ( $this->useStderr ? '1' : '' ) . ';' .
+                                               "MW_CPU_LIMIT=$time; " .
+                                               'MW_CGROUP=' . escapeshellarg( $this->cgroup ) . '; ' .
+                                               "MW_MEM_LIMIT=$mem; " .
+                                               "MW_FILE_SIZE_LIMIT=$filesize; " .
+                                               "MW_WALL_CLOCK_LIMIT=$wallTime; " .
+                                               "MW_USE_LOG_PIPE=yes"
+                                       );
                                $useLogPipe = true;
-                       } elseif ( $this->useStderr ) {
-                               $cmd .= ' 2>&1';
                        }
-               } elseif ( $this->useStderr ) {
+               }
+               if ( !$useLogPipe && $this->useStderr ) {
                        $cmd .= ' 2>&1';
                }
-               wfDebug( __METHOD__ . ": $cmd\n" );
+
+               return [ $cmd, $useLogPipe ];
+       }
+
+       /**
+        * Executes command. Afterwards, getExitCode() and getOutput() can be used to access execution
+        * results.
+        *
+        * @return Result
+        * @throws Exception
+        * @throws ProcOpenError
+        * @throws ShellDisabledError
+        */
+       public function execute() {
+               $this->everExecuted = true;
+
+               $profileMethod = $this->method ?: wfGetCaller();
+
+               list( $cmd, $useLogPipe ) = $this->buildFinalCommand();
+
+               $this->logger->debug( __METHOD__ . ": $cmd" );
 
                // Don't try to execute commands that exceed Linux's MAX_ARG_STRLEN.
                // Other platforms may be more accomodating, but we don't want to be
@@ -237,13 +277,13 @@ class Command {
                // input. See T129506.
                if ( strlen( $cmd ) > SHELL_MAX_ARG_STRLEN ) {
                        throw new Exception( __METHOD__ .
-                                                                '(): total length of $cmd must not exceed SHELL_MAX_ARG_STRLEN' );
+                               '(): total length of $cmd must not exceed SHELL_MAX_ARG_STRLEN' );
                }
 
                $desc = [
                        0 => [ 'file', 'php://stdin', 'r' ],
                        1 => [ 'pipe', 'w' ],
-                       2 => [ 'file', 'php://stderr', 'w' ],
+                       2 => [ 'pipe', 'w' ],
                ];
                if ( $useLogPipe ) {
                        $desc[3] = [ 'pipe', 'w' ];
@@ -252,10 +292,11 @@ class Command {
                $scoped = Profiler::instance()->scopedProfileIn( __FUNCTION__ . '-' . $profileMethod );
                $proc = proc_open( $cmd, $desc, $pipes );
                if ( !$proc ) {
-                       wfDebugLog( 'exec', "proc_open() failed: $cmd" );
+                       $this->logger->error( "proc_open() failed: {command}", [ 'command' => $cmd ] );
                        throw new ProcOpenError();
                }
                $outBuffer = $logBuffer = '';
+               $errBuffer = null;
                $emptyArray = [];
                $status = false;
                $logMsg = false;
@@ -291,12 +332,20 @@ class Command {
 
                        $readyPipes = $pipes;
 
-                       // Clear last error
+                       // clear get_last_error without actually raising an error
+                       // from http://php.net/manual/en/function.error-get-last.php#113518
+                       // TODO replace with clear_last_error when requirements are bumped to PHP7
+                       set_error_handler( function () {
+                       }, 0 );
                        // @codingStandardsIgnoreStart Generic.PHP.NoSilencedErrors.Discouraged
                        @trigger_error( '' );
+                       // @codingStandardsIgnoreEnd
+                       restore_error_handler();
+
+                       // @codingStandardsIgnoreStart Generic.PHP.NoSilencedErrors.Discouraged
                        $numReadyPipes = @stream_select( $readyPipes, $emptyArray, $emptyArray, $timeout );
+                       // @codingStandardsIgnoreEnd
                        if ( $numReadyPipes === false ) {
-                               // @codingStandardsIgnoreEnd
                                $error = error_get_last();
                                if ( strncmp( $error['message'], $eintrMessage, strlen( $eintrMessage ) ) == 0 ) {
                                        continue;
@@ -322,6 +371,9 @@ class Command {
                                } elseif ( $fd == 1 ) {
                                        // From stdout
                                        $outBuffer .= $block;
+                               } elseif ( $fd == 2 ) {
+                                       // From stderr
+                                       $errBuffer .= $block;
                                } elseif ( $fd == 3 ) {
                                        // From log FD
                                        $logBuffer .= $block;
@@ -329,7 +381,7 @@ class Command {
                                                $lines = explode( "\n", $logBuffer );
                                                $logBuffer = array_pop( $lines );
                                                foreach ( $lines as $line ) {
-                                                       wfDebugLog( 'exec', $line );
+                                                       $this->logger->info( $line );
                                                }
                                        }
                                }
@@ -369,9 +421,9 @@ class Command {
                }
 
                if ( $logMsg !== false ) {
-                       wfDebugLog( 'exec', "$logMsg: $cmd" );
+                       $this->logger->warning( "$logMsg: {command}", [ 'command' => $cmd ] );
                }
 
-               return new Result( $retval, $outBuffer );
+               return new Result( $retval, $outBuffer, $errBuffer );
        }
 }