php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46477 exec() returns the wrong status
Submitted: 2008-11-04 09:49 UTC Modified: 2008-11-06 18:06 UTC
From: rachmel at avaya dot com Assigned:
Status: Not a bug Package: CGI/CLI related
PHP Version: 5.2CVS-2008-11-05 OS: Linux 2.6.14.7
Private report: No CVE-ID: None
 [2008-11-04 09:49 UTC] rachmel at avaya dot com
Description:
------------
PHP's exec function returns the wrong return value.

When running script from CLI, works perfectly. When running the same script from the web-server context - 90% of the times it returns the wrong value - (-1).

I am using the appWeb embedded server, which is a slim apache-like webserver.

I validated there are no permission running issues, and I couldn't see any log messages sent by PHP that might indicate what the problem is.

Reproduce code:
---------------
<?php
print "<pre>";
for ($i = 0; $i < 10; ++$i) {
        exec("/bin/true", $output, $status);
        print "exec('/bin/true') returns with <b>$status</b> (expected 
0)\n";
}
for ($i = 0; $i < 10; ++$i) {
        exec("/bin/false", $output, $status);
        print "exec('/bin/false') returns with <b>$status</b> (expected 
1)\n";
}
print "</pre>";
?>


Expected result:
----------------
All executions of "true" should print "0" to the screen
All executions of "false" should print "1" to the screen.

Actual result:
--------------
Most of the times, all executions result in an error code of "-1" being printed to the screen.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-11-04 22:31 UTC] jani@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5.2-latest.tar.gz
 
For Windows:

  http://windows.php.net/snapshots/

And what SAPI are you using? I guess CGI or FastCGI since there is no 
"native" SAPI for this odd webserver.
 [2008-11-05 08:09 UTC] rachmel at avaya dot com
Tried with the latest CVS snapshot you attached, and the result is the same.

I am not working in CGI mode. As my web-server is apache like, it supports running php scripts natively. It links to the libphp5.so created after the installation of php and makes use of it directly.
 [2008-11-06 01:18 UTC] jani@php.net
What exactly is the configure line you used? Can you reproduce this 
problem using real Apache?
 [2008-11-06 01:20 UTC] jani@php.net
And you should really check what $output contains using this:

var_dump($output);
 [2008-11-06 09:36 UTC] rachmel at avaya dot com
Here's the configure line used:
Configure Command =>  './configure'  
'--host=i686-linux' 
'--build=i686-linux' 
'--prefix=/usr/local' 
'--disable-all' 
'--enable-static' 
'--disable-shared' 
'--enable-memory-limit' 
'--disable-safe-mode' 
'--disable-rpath' 
'--disable-ipv6' 
'--disable-wddx' 
'--disable-bcmath' 
'--enable-debug' 
'--disable-calendar' 
'--enable-ftp' 
'--without-aolserver' 
'--without-apache' 
'--without-continuity' 
'--without-pi3web' 
'--enable-releasemode' 
'--enable-xml' 
'--cache-file=config.cache' 
'--enable-libxml' 
'--with-libxml-dir=/usr/local' 
'--disable-simplexml' 
'--enable-dom' 
'--enable-soap' 
'--with-db' 
'--enable-sockets' 
'--enable-track-vars' 
'--enable-trans-sid' 
'--enable-magic-quotes' 
'--without-pear' 
'--with-zlib' 
'--with-exec-dir=/usr/local/appweb' 
'--sysconfdir=/usr/local/appweb' 
'--with-gnu-ld' 
'--with-openssl=/usr' 
'--enable-mbstring' 
'--disable-mbregex' 
'--enable-session' 
'--enable-pcntl' 
'--enable-pdo' 
'--with-pdo-sqlite=/usr/local' 
'--with-pcre-regex' 
'--enable-spl' 
'--enable-tokenizer' 
'--with-snmp=/usr/local' 
'--enable-ctype' 
'--enable-apc' 
'--with-ctype' 
'--without-tsrm-pthreads' 
'--disable-threadsafe' 
'--disable-maintainer-zts' 
'--enable-embed=shared' 
'--enable-cgi' 
'--enable-cli' 
'--enable-inline-optimization' 
'--with-curl=/usr/local' 
'--with-curlwrappers' 
'--enable-cipher'
 [2008-11-06 09:49 UTC] rachmel at avaya dot com
1. I tried using "var_dump()" - got the same results.
(it prints "int(-1)" all the time).

2. regarding the apache server - I don't have one installed/configured on my system but I will give it a try.
 [2008-11-06 09:51 UTC] rachmel at avaya dot com
Another important comment - The funny thing is that the command finishes succesfuly!

I tried using a small c program that I wrote:
#include <stdio.h>
int main(int argc, char **argv)
{
        printf("true was called!");

        return 0;
}

So when printing the $output var, it holds the correct string. It is just the $status var that holds the wrong value.
 [2008-11-06 11:23 UTC] jani@php.net
First of all: You're obviously running PHP as CGI since that's the only one you're compiling with that configure line. Also, try this script instead:

<?php
for ($i = 0; $i < 10; ++$i) {
  exec("/bin/true", $out, $status);
  var_dump($out, $status);
  exec("/bin/false", $out, $status);
  var_dump($out, $status);
}
?>



 [2008-11-06 11:47 UTC] rachmel at avaya dot com
1. Here's the output of your script:
array(0) { } int(-1) array(0) { } int(-1) array(0) { } int(-1) array(0) { } int(-1) array(0) { } int(-1) array(0) { } int(-1) array(0) { } int(-1) array(0) { } int(-1) array(0) { } int(-1) array(0) { } int(-1) array(0) { } int(-1) array(0) { } int(-1) array(0) { } int(-1) array(0) { } int(-1) array(0) { } int(-1) array(0) { } int(1) array(0) { } int(-1) array(0) { } int(-1) array(0) { } int(-1) array(0) { } int(-1) 

2. Regarding the CGI mode. Maybe I don't understand the concept of CGI well enough - I will try to explain my setting:
a. I compile php and create the libphp5.so. Notice the "'--enable-embed=shared'" directive in the configure line.
b. I compile my web-server (appWeb). It has an internal module for running php scripts which dynamically links to the libphp5.so.
Is this referred to as CGI mode or SAPI mode?
 [2008-11-06 18:03 UTC] rachmel at avaya dot com
Hi,

Tried with the latest version of apache2 and it works OK.
Something with the mode I am using is wrong. Do you have any ideas? Are you familiar with such errors maybe?
 [2008-11-06 18:06 UTC] jani@php.net
There's something wrong with your webserver. This is in no way any PHP bug.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 16 22:01:31 2024 UTC