Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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...
commit e5f3397d03d1a26a4447ea297409eca6ed2fea1a 1 parent 9f46b26
Craig Constantine authored

Showing 3 changed files with 51 additions and 24 deletions. Show diff stats Hide diff stats

  1. +44 15 Command.php
  2. +3 4 package.xml
  3. +4 5 package2.xml
59 Command.php
@@ -41,6 +41,7 @@
41 41 define('SYSTEM_COMMAND_NOHUP_MISSING', -9);
42 42 define('SYSTEM_COMMAND_NO_OUTPUT', -10);
43 43 define('SYSTEM_COMMAND_STDERR', -11);
  44 +define('SYSTEM_COMMAND_NONZERO_EXIT', -12);
44 45
45 46 // }}}
46 47
@@ -147,6 +148,7 @@ function System_Command($in_shell = null)
147 148 'OUTPUT' => true,
148 149 'NOHUP' => false,
149 150 'BACKGROUND' => false,
  151 + 'STDERR' => false
150 152 );
151 153
152 154 // prepare the available control operators
@@ -199,20 +201,32 @@ function System_Command($in_shell = null)
199 201 // {{{ setOption()
200 202
201 203 /**
202   - * Sets any option
  204 + * Sets the value for an option. Each option should be set to true
  205 + * or false; except the 'SHELL' option which should be a string
  206 + * naming a shell. The options are:
203 207 *
204   - * The options are currently:
205   - * SEQUENCE : Allow a sequence command or not (right now this is always on)
206   - * SHUTDOWN : Execute commands via a shutdown function
207   - * SHELL : Path to shell
208   - * OUTPUT : Output stdout from process
209   - * NOHUP : Use nohup to detach process
210   - * BACKGROUND : Run as a background process with &
  208 + * 'SEQUENCE' Allow a sequence command or not (right now this is always on);
211 209 *
212   - * @param $in_option is a constant, which corresponds to the
213   - * option that should be changed
214   - * @param $in_setting is the value of the option currently
215   - * being toggled.
  210 + * 'SHUTDOWN' Execute commands via a shutdown function;
  211 + *
  212 + * 'SHELL' Path to shell;
  213 + *
  214 + * 'OUTPUT' Output stdout from process;
  215 + *
  216 + * 'NOHUP' Use nohup to detach process;
  217 + *
  218 + * 'BACKGROUND' Run as a background process with &;
  219 + *
  220 + * 'STDERR' Output on stderr will raise an error, even if
  221 + * the command's exit value is zero. The output from
  222 + * stderr can be retrieved using the getDebugInfo()
  223 + * method of the Pear_ERROR object returned by
  224 + * execute().;
  225 + *
  226 + * @param string $in_option is a case-sensitive string,
  227 + * corresponding to the option
  228 + * that should be changed
  229 + * @param mixed $in_setting is the new value for the option
216 230 * @access public
217 231 * @return bool true if succes, else false
218 232 */
@@ -234,6 +248,7 @@ function setOption($in_option, $in_setting)
234 248 case 'SHUTDOWN':
235 249 case 'SEQUENCE':
236 250 case 'BACKGROUND':
  251 + case 'STDERR':
237 252 $this->options[$option] = !empty($in_setting);
238 253 return true;
239 254 break;
@@ -403,12 +418,25 @@ function execute()
403 418 exec($shellPipe, $result, $returnVal);
404 419
405 420 if ($returnVal !== 0) {
406   - $error = implode('', file($tmpFile));
407   - $return = PEAR::raiseError(null, SYSTEM_COMMAND_STDERR, null, E_USER_WARNING, null, 'System_Command_Error', true);
  421 + // command returned nonzero; that's always an error
  422 + $return = PEAR::raiseError(null, SYSTEM_COMMAND_NONZERO_EXIT, null, E_USER_WARNING, null, 'System_Command_Error', true);
408 423 }
409   - else {
  424 + else if (!$this->options['STDERR']) {
  425 + // caller does not care about stderr; return success
410 426 $return = implode("\n", $result);
411 427 }
  428 + else {
  429 + // our caller cares about stderr; check stderr output
  430 + clearstatcache();
  431 + if (filesize($tmpFile) > 0) {
  432 + // the command actually wrote to stderr
  433 + $stderr_output = file_get_contents($tmpFile);
  434 + $return = PEAR::raiseError(null, SYSTEM_COMMAND_STDERR, null, E_USER_WARNING, $stderr_output, 'System_Command_Error', true);
  435 + } else {
  436 + // total success; return stdout gathered by exec()
  437 + $return = implode("\n", $result);
  438 + }
  439 + }
412 440
413 441 unlink($tmpFile);
414 442 return $return;
@@ -482,6 +510,7 @@ function errorMessage($in_value)
482 510 SYSTEM_COMMAND_NOHUP_MISSING => 'nohup not found on system',
483 511 SYSTEM_COMMAND_NO_OUTPUT => 'output not allowed',
484 512 SYSTEM_COMMAND_STDERR => 'command wrote to stderr',
  513 + SYSTEM_COMMAND_NONZERO_EXIT => 'non-zero exit value from command',
485 514 );
486 515 }
487 516
7 package.xml
@@ -26,11 +26,10 @@
26 26 <release>
27 27 <license>PHP License</license>
28 28 <state>stable</state>
29   - <version>1.0.4</version>
30   - <date>2006-01-18</date>
  29 + <version>1.0.5</version>
  30 + <date>2006-03-14</date>
31 31 <notes>
32   -- fix #6199 System_Command::execute() fails if object instantiated without an argument
33   -- introduce v2 of Package XML file
  32 +- fix #6268 execute() raises SYSTEM_COMMAND_STDERR error when command exits non-zero
34 33 </notes>
35 34 <filelist>
36 35 <dir name="/" baseinstalldir="System">
9 package2.xml
@@ -19,10 +19,10 @@
19 19 <email>cconstantine@php.net</email>
20 20 <active>yes</active>
21 21 </lead>
22   - <date>2006-01-18</date>
23   - <time>16:35:00</time>
  22 + <date>2006-03-14</date>
  23 + <time>16:22:00</time>
24 24 <version>
25   - <release>1.0.4</release>
  25 + <release>1.0.5</release>
26 26 <api>1.0.3</api>
27 27 </version>
28 28 <stability>
@@ -31,8 +31,7 @@
31 31 </stability>
32 32 <license uri="http://www.php.net/license">PHP License</license>
33 33 <notes><![CDATA[
34   -- fix #6199 System_Command::execute() fails if object instantiated without an argument
35   -- introduce v2 of Package XML file
  34 +- fix #6199 execute() raises SYSTEM_COMMAND_STDERR error when command exits non-zero
36 35 ]]></notes>
37 36 <contents>
38 37 <dir name="/">

0 comments on commit e5f3397

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