Skip to content
Browse files

fix bug #6268: execute() raises SYSTEM_COMMAND_STDERR error when comm…

…and exits non-zero

git-svn-id: http://svn.php.net/repository/pear/packages/System_Command/trunk@209358 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information...
1 parent 9f46b26 commit e5f3397d03d1a26a4447ea297409eca6ed2fea1a Craig Constantine committed Mar 14, 2006
Showing with 51 additions and 24 deletions.
  1. +44 −15 Command.php
  2. +3 −4 package.xml
  3. +4 −5 package2.xml
View
59 Command.php
@@ -41,6 +41,7 @@
define('SYSTEM_COMMAND_NOHUP_MISSING', -9);
define('SYSTEM_COMMAND_NO_OUTPUT', -10);
define('SYSTEM_COMMAND_STDERR', -11);
+define('SYSTEM_COMMAND_NONZERO_EXIT', -12);
// }}}
@@ -147,6 +148,7 @@ function System_Command($in_shell = null)
'OUTPUT' => true,
'NOHUP' => false,
'BACKGROUND' => false,
+ 'STDERR' => false
);
// prepare the available control operators
@@ -199,20 +201,32 @@ function System_Command($in_shell = null)
// {{{ setOption()
/**
- * Sets any option
+ * Sets the value for an option. Each option should be set to true
+ * or false; except the 'SHELL' option which should be a string
+ * naming a shell. The options are:
*
- * The options are currently:
- * SEQUENCE : Allow a sequence command or not (right now this is always on)
- * SHUTDOWN : Execute commands via a shutdown function
- * SHELL : Path to shell
- * OUTPUT : Output stdout from process
- * NOHUP : Use nohup to detach process
- * BACKGROUND : Run as a background process with &
+ * 'SEQUENCE' Allow a sequence command or not (right now this is always on);
*
- * @param $in_option is a constant, which corresponds to the
- * option that should be changed
- * @param $in_setting is the value of the option currently
- * being toggled.
+ * 'SHUTDOWN' Execute commands via a shutdown function;
+ *
+ * 'SHELL' Path to shell;
+ *
+ * 'OUTPUT' Output stdout from process;
+ *
+ * 'NOHUP' Use nohup to detach process;
+ *
+ * 'BACKGROUND' Run as a background process with &;
+ *
+ * 'STDERR' Output on stderr will raise an error, even if
+ * the command's exit value is zero. The output from
+ * stderr can be retrieved using the getDebugInfo()
+ * method of the Pear_ERROR object returned by
+ * execute().;
+ *
+ * @param string $in_option is a case-sensitive string,
+ * corresponding to the option
+ * that should be changed
+ * @param mixed $in_setting is the new value for the option
* @access public
* @return bool true if succes, else false
*/
@@ -234,6 +248,7 @@ function setOption($in_option, $in_setting)
case 'SHUTDOWN':
case 'SEQUENCE':
case 'BACKGROUND':
+ case 'STDERR':
$this->options[$option] = !empty($in_setting);
return true;
break;
@@ -403,12 +418,25 @@ function execute()
exec($shellPipe, $result, $returnVal);
if ($returnVal !== 0) {
- $error = implode('', file($tmpFile));
- $return = PEAR::raiseError(null, SYSTEM_COMMAND_STDERR, null, E_USER_WARNING, null, 'System_Command_Error', true);
+ // command returned nonzero; that's always an error
+ $return = PEAR::raiseError(null, SYSTEM_COMMAND_NONZERO_EXIT, null, E_USER_WARNING, null, 'System_Command_Error', true);
}
- else {
+ else if (!$this->options['STDERR']) {
+ // caller does not care about stderr; return success
$return = implode("\n", $result);
}
+ else {
+ // our caller cares about stderr; check stderr output
+ clearstatcache();
+ if (filesize($tmpFile) > 0) {
+ // the command actually wrote to stderr
+ $stderr_output = file_get_contents($tmpFile);
+ $return = PEAR::raiseError(null, SYSTEM_COMMAND_STDERR, null, E_USER_WARNING, $stderr_output, 'System_Command_Error', true);
+ } else {
+ // total success; return stdout gathered by exec()
+ $return = implode("\n", $result);
+ }
+ }
unlink($tmpFile);
return $return;
@@ -482,6 +510,7 @@ function errorMessage($in_value)
SYSTEM_COMMAND_NOHUP_MISSING => 'nohup not found on system',
SYSTEM_COMMAND_NO_OUTPUT => 'output not allowed',
SYSTEM_COMMAND_STDERR => 'command wrote to stderr',
+ SYSTEM_COMMAND_NONZERO_EXIT => 'non-zero exit value from command',
);
}
View
7 package.xml
@@ -26,11 +26,10 @@
<release>
<license>PHP License</license>
<state>stable</state>
- <version>1.0.4</version>
- <date>2006-01-18</date>
+ <version>1.0.5</version>
+ <date>2006-03-14</date>
<notes>
-- fix #6199 System_Command::execute() fails if object instantiated without an argument
-- introduce v2 of Package XML file
+- fix #6268 execute() raises SYSTEM_COMMAND_STDERR error when command exits non-zero
</notes>
<filelist>
<dir name="/" baseinstalldir="System">
View
9 package2.xml
@@ -19,10 +19,10 @@
<email>cconstantine@php.net</email>
<active>yes</active>
</lead>
- <date>2006-01-18</date>
- <time>16:35:00</time>
+ <date>2006-03-14</date>
+ <time>16:22:00</time>
<version>
- <release>1.0.4</release>
+ <release>1.0.5</release>
<api>1.0.3</api>
</version>
<stability>
@@ -31,8 +31,7 @@
</stability>
<license uri="http://www.php.net/license">PHP License</license>
<notes><![CDATA[
-- fix #6199 System_Command::execute() fails if object instantiated without an argument
-- introduce v2 of Package XML file
+- fix #6199 execute() raises SYSTEM_COMMAND_STDERR error when command exits non-zero
]]></notes>
<contents>
<dir name="/">

0 comments on commit e5f3397

Please sign in to comment.
Something went wrong with that request. Please try again.