aboutsummaryrefslogtreecommitdiffstats
path: root/includes/shell/Command.php
diff options
context:
space:
mode:
authorMax Semenik <maxsem.wiki@gmail.com>2017-09-08 23:10:09 -0700
committerMax Semenik <maxsem.wiki@gmail.com>2017-10-03 20:01:59 -0700
commit1bb9a223d268518cedc16cfe4d67a293989d7aa5 (patch)
treec76d70fd0077b6005c928b30f7d8de8fc697b312 /includes/shell/Command.php
parentf5f817fc58bf429a48fac1b9a7fb7ad642e3f9f2 (diff)
downloadmediawikicore-1bb9a223d268518cedc16cfe4d67a293989d7aa5.tar.gz
mediawikicore-1bb9a223d268518cedc16cfe4d67a293989d7aa5.zip
Inject dependencies into Shell\Command
This slightly changes how execution time limits fall back on each other. Change-Id: I7754a9e6be9638eebe90cb953adb8e2a6ee97cef
Diffstat (limited to 'includes/shell/Command.php')
-rw-r--r--includes/shell/Command.php66
1 files changed, 45 insertions, 21 deletions
diff --git a/includes/shell/Command.php b/includes/shell/Command.php
index 864e69a115d6..fd8f6a0ca7a5 100644
--- a/includes/shell/Command.php
+++ b/includes/shell/Command.php
@@ -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() );
}
/**
@@ -112,11 +132,10 @@ class Command {
* Sets execution limits
*
* @param array $limits Optional array with limits(filesize, memory, time, walltime).
- * This overrides the global wgMaxShell* limits.
* @return $this
*/
public function limits( array $limits ) {
- $this->limits = $limits;
+ $this->limits = $limits + $this->limits;
return $this;
}
@@ -159,6 +178,18 @@ class Command {
}
/**
+ * Sets cgroup for this command
+ *
+ * @param string|false $cgroup
+ * @return $this
+ */
+ public function cgroup( $cgroup ) {
+ $this->cGroup = $cgroup;
+
+ return $this;
+ }
+
+ /**
* Executes command. Afterwards, getExitCode() and getOutput() can be used to access execution
* results.
*
@@ -168,8 +199,7 @@ class Command {
* @throws ShellDisabledError
*/
public function execute() {
- global $IP, $wgMaxShellMemory, $wgMaxShellFileSize, $wgMaxShellTime,
- $wgMaxShellWallClockTime, $wgShellCgroup;
+ global $IP;
$this->everExecuted = true;
@@ -197,18 +227,12 @@ class 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'] );
+ // for b/c, wall time falls back to time
+ $wallTime = min( $time, $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" ) . ' ' .
@@ -216,7 +240,7 @@ class Command {
escapeshellarg(
"MW_INCLUDE_STDERR=" . ( $this->useStderr ? '1' : '' ) . ';' .
"MW_CPU_LIMIT=$time; " .
- 'MW_CGROUP=' . escapeshellarg( $wgShellCgroup ) . '; ' .
+ 'MW_CGROUP=' . escapeshellarg( $this->cGroup ) . '; ' .
"MW_MEM_LIMIT=$mem; " .
"MW_FILE_SIZE_LIMIT=$filesize; " .
"MW_WALL_CLOCK_LIMIT=$wallTime; " .
@@ -252,7 +276,7 @@ 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 = '';
@@ -329,7 +353,7 @@ class Command {
$lines = explode( "\n", $logBuffer );
$logBuffer = array_pop( $lines );
foreach ( $lines as $line ) {
- wfDebugLog( 'exec', $line );
+ $this->logger->info( $line );
}
}
}
@@ -369,7 +393,7 @@ class Command {
}
if ( $logMsg !== false ) {
- wfDebugLog( 'exec', "$logMsg: $cmd" );
+ $this->logger->warning( "$logMsg: {command}", [ 'command' => $cmd ] );
}
return new Result( $retval, $outBuffer );