note 56798 added to function.proc-close

From: Date: Thu, 15 Sep 2005 14:06:47 +0000
Subject: note 56798 added to function.proc-close
Groups: php.notes 
Request: Send a blank email to php-notes+get-95225@lists.php.net to get a copy of this message
From various Internet posts and recent experience, I have observed that you cannot rely on proc_close returning the accurate return code of the child process. The return code also depends on wether or not you read from the stdout/stderr pipes, as my example shows. I work around this by writing the exit code to an additional file descriptor. <? $descriptorspec = array( 0 => array('pipe', 'r'), // stdin is a pipe that the child will read from 1 => array('pipe', 'w'), // stdout is a pipe that the child will write to 2 => array('pipe', 'w'), // stderr is a pipe that the child will write to ); $proc = @proc_open("/bin/ls -l /etc/passwd", $descriptorspec, $pipes); fclose($pipes[0]); $output = array(); while (!feof($pipes[1])) array_push($output, rtrim(fgets($pipes[1],1024),"\n")); fclose($pipes[1]); while (!feof($pipes[2])) array_push($output, rtrim(fgets($pipes[2],1024),"\n")); fclose($pipes[2]); $exit=proc_close($proc); print_r($output); echo "exitcode $exit\n\n"; $descriptorspec = array( 0 => array('pipe', 'r'), // stdin is a pipe that the child will read from 1 => array('pipe', 'w'), // stdout is a pipe that the child will write to 2 => array('pipe', 'w'), // stderr is a pipe that the child will write to ); $proc = @proc_open("/bin/ls -l /etc/passwd", $descriptorspec, $pipes); fclose($pipes[0]); fclose($pipes[1]); fclose($pipes[2]); $exit=proc_close($proc); echo "exitcode $exit\n\n"; $descriptorspec = array( 0 => array('pipe', 'r'), // stdin is a pipe that the child will read from 1 => array('pipe', 'w'), // stdout is a pipe that the child will write to 2 => array('pipe', 'w'), // stderr is a pipe that the child will write to 3 => array('pipe', 'w'), // stderr is a pipe that the child will write to ); $proc = @proc_open("/bin/ls -l /etc/passwd;echo $? >&3", $descriptorspec, $pipes); fclose($pipes[0]); $output = array(); //comment next line to get correct exicode while (!feof($pipes[1])) array_push($output, rtrim(fgets($pipes[1],1024),"\n")); fclose($pipes[1]); while (!feof($pipes[2])) array_push($output, rtrim(fgets($pipes[2],1024),"\n")); fclose($pipes[2]); if (!feof($pipes[3])) $output['exitcode']=rtrim(fgets($pipes[3],5),"\n"); fclose($pipes[3]); proc_close($proc); print_r($output); ?> Outputs on my system: Array ( [0] => -rw-r--r-- 1 root root 1460 2005-09-02 09:52 /etc/passwd [1] => [2] => ) exitcode -1 exitcode 1 Array ( [0] => -rw-r--r-- 1 root root 1460 2005-09-02 09:52 /etc/passwd [1] => [2] => [exitcode] => 0 ) ---- Manual Page -- http://www.php.net/manual/en/function.proc-close.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+56798 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56798&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56798&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56798&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56798&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56798&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+56798&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56798&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+56798&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#95225) next »