mirror of
https://git.code.sf.net/p/seeddms/code
synced 2025-09-09 19:38:57 +00:00
execWithTimeout() closes all pipes and also returns return value of cmd
This commit is contained in:
parent
c37802fdca
commit
4f59c3b4e8
|
@ -40,9 +40,11 @@ class SeedDMS_Lucene_IndexedDocument extends Zend_Search_Lucene_Document {
|
||||||
protected $cmd;
|
protected $cmd;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Run a shell command
|
||||||
|
*
|
||||||
* @param $cmd
|
* @param $cmd
|
||||||
* @param int $timeout
|
* @param int $timeout
|
||||||
* @return string
|
* @return array
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
static function execWithTimeout($cmd, $timeout=2) { /* {{{ */
|
static function execWithTimeout($cmd, $timeout=2) { /* {{{ */
|
||||||
|
@ -54,7 +56,11 @@ class SeedDMS_Lucene_IndexedDocument extends Zend_Search_Lucene_Document {
|
||||||
$pipes = array();
|
$pipes = array();
|
||||||
|
|
||||||
$timeout += time();
|
$timeout += time();
|
||||||
$process = proc_open($cmd, $descriptorspec, $pipes);
|
// Putting an 'exec' before the command will not fork the command
|
||||||
|
// and therefore not create any child process. proc_terminate will
|
||||||
|
// then reliably terminate the cmd and not just shell. See notes of
|
||||||
|
// https://www.php.net/manual/de/function.proc-terminate.php
|
||||||
|
$process = proc_open('exec '.$cmd, $descriptorspec, $pipes);
|
||||||
if (!is_resource($process)) {
|
if (!is_resource($process)) {
|
||||||
throw new Exception("proc_open failed on: " . $cmd);
|
throw new Exception("proc_open failed on: " . $cmd);
|
||||||
}
|
}
|
||||||
|
@ -79,11 +85,15 @@ class SeedDMS_Lucene_IndexedDocument extends Zend_Search_Lucene_Document {
|
||||||
$timeleft = $timeout - time();
|
$timeleft = $timeout - time();
|
||||||
} while (!feof($pipes[1]) && $timeleft > 0);
|
} while (!feof($pipes[1]) && $timeleft > 0);
|
||||||
|
|
||||||
|
fclose($pipes[0]);
|
||||||
|
fclose($pipes[1]);
|
||||||
|
fclose($pipes[2]);
|
||||||
if ($timeleft <= 0) {
|
if ($timeleft <= 0) {
|
||||||
proc_terminate($process);
|
proc_terminate($process);
|
||||||
throw new Exception("command timeout on: " . $cmd);
|
throw new Exception("command timeout on: " . $cmd);
|
||||||
} else {
|
} else {
|
||||||
return array('stdout'=>$output, 'stderr'=>$error);
|
$return_value = proc_close($process);
|
||||||
|
return array('stdout'=>$output, 'stderr'=>$error, 'return'=>$return_value);
|
||||||
}
|
}
|
||||||
} /* }}} */
|
} /* }}} */
|
||||||
|
|
||||||
|
|
|
@ -70,6 +70,14 @@ class SeedDMS_Preview_Base {
|
||||||
$this->xsendfile = $xsendfile;
|
$this->xsendfile = $xsendfile;
|
||||||
} /* }}} */
|
} /* }}} */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run a shell command
|
||||||
|
*
|
||||||
|
* @param $cmd
|
||||||
|
* @param int $timeout
|
||||||
|
* @return array
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
static function execWithTimeout($cmd, $timeout=5) { /* {{{ */
|
static function execWithTimeout($cmd, $timeout=5) { /* {{{ */
|
||||||
$descriptorspec = array(
|
$descriptorspec = array(
|
||||||
0 => array("pipe", "r"),
|
0 => array("pipe", "r"),
|
||||||
|
@ -108,11 +116,15 @@ class SeedDMS_Preview_Base {
|
||||||
$timeleft = $timeout - time();
|
$timeleft = $timeout - time();
|
||||||
} while (!feof($pipes[1]) && $timeleft > 0);
|
} while (!feof($pipes[1]) && $timeleft > 0);
|
||||||
|
|
||||||
|
fclose($pipes[0]);
|
||||||
|
fclose($pipes[1]);
|
||||||
|
fclose($pipes[2]);
|
||||||
if ($timeleft <= 0) {
|
if ($timeleft <= 0) {
|
||||||
proc_terminate($process);
|
proc_terminate($process);
|
||||||
throw new Exception("command timeout on: " . $cmd);
|
throw new Exception("command timeout on: " . $cmd);
|
||||||
} else {
|
} else {
|
||||||
return array('stdout'=>$output, 'stderr'=>$error);
|
$return_value = proc_close($process);
|
||||||
|
return array('stdout'=>$output, 'stderr'=>$error, 'return'=>$return_value);
|
||||||
}
|
}
|
||||||
} /* }}} */
|
} /* }}} */
|
||||||
|
|
||||||
|
|
|
@ -128,6 +128,7 @@ class SeedDMS_Preview_Previewer extends SeedDMS_Preview_Base {
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
$new = false;
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
} /* }}} */
|
} /* }}} */
|
||||||
|
|
|
@ -44,6 +44,14 @@ class SeedDMS_SQLiteFTS_IndexedDocument extends SeedDMS_SQLiteFTS_Document {
|
||||||
*/
|
*/
|
||||||
protected $cmd;
|
protected $cmd;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run a shell command
|
||||||
|
*
|
||||||
|
* @param $cmd
|
||||||
|
* @param int $timeout
|
||||||
|
* @return array
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
static function execWithTimeout($cmd, $timeout=2) { /* {{{ */
|
static function execWithTimeout($cmd, $timeout=2) { /* {{{ */
|
||||||
$descriptorspec = array(
|
$descriptorspec = array(
|
||||||
0 => array("pipe", "r"),
|
0 => array("pipe", "r"),
|
||||||
|
@ -53,7 +61,11 @@ class SeedDMS_SQLiteFTS_IndexedDocument extends SeedDMS_SQLiteFTS_Document {
|
||||||
$pipes = array();
|
$pipes = array();
|
||||||
|
|
||||||
$timeout += time();
|
$timeout += time();
|
||||||
$process = proc_open($cmd, $descriptorspec, $pipes);
|
// Putting an 'exec' before the command will not fork the command
|
||||||
|
// and therefore not create any child process. proc_terminate will
|
||||||
|
// then reliably terminate the cmd and not just shell. See notes of
|
||||||
|
// https://www.php.net/manual/de/function.proc-terminate.php
|
||||||
|
$process = proc_open('exec '.$cmd, $descriptorspec, $pipes);
|
||||||
if (!is_resource($process)) {
|
if (!is_resource($process)) {
|
||||||
throw new Exception("proc_open failed on: " . $cmd);
|
throw new Exception("proc_open failed on: " . $cmd);
|
||||||
}
|
}
|
||||||
|
@ -78,11 +90,15 @@ class SeedDMS_SQLiteFTS_IndexedDocument extends SeedDMS_SQLiteFTS_Document {
|
||||||
$timeleft = $timeout - time();
|
$timeleft = $timeout - time();
|
||||||
} while (!feof($pipes[1]) && $timeleft > 0);
|
} while (!feof($pipes[1]) && $timeleft > 0);
|
||||||
|
|
||||||
|
fclose($pipes[0]);
|
||||||
|
fclose($pipes[1]);
|
||||||
|
fclose($pipes[2]);
|
||||||
if ($timeleft <= 0) {
|
if ($timeleft <= 0) {
|
||||||
proc_terminate($process);
|
proc_terminate($process);
|
||||||
throw new Exception("command timeout on: " . $cmd);
|
throw new Exception("command timeout on: " . $cmd);
|
||||||
} else {
|
} else {
|
||||||
return array('stdout'=>$output, 'stderr'=>$error);
|
$return_value = proc_close($process);
|
||||||
|
return array('stdout'=>$output, 'stderr'=>$error, 'return'=>$return_value);
|
||||||
}
|
}
|
||||||
} /* }}} */
|
} /* }}} */
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user