468,315 Members | 1,400 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,315 developers. It's quick & easy.

exec() doesn't work on C/C++ executables

Hello there,

I'm new to PHP. I'm trying to run a simple php
script (on a Red hat linux machine with apache web server )
through a windows web browser (IE).
I'm using an exec() function in my php script.
The code is given below (with line numbers).

01 <?php
02 $output = exec("pwd");
03 echo "<br>Working dir: $output";

04 //$prgfile="touch out/testfile";
05 //$prgfile="ls";
06 //$prgfile="./hello.pl";
07 $prgfile="./hello";

08 exec($prgfile, $output, $rc);
09 echo "<br>";
10 print_r($output);
11 echo "<br> Return Value: ";
12 print_r($rc);

13 //$output = system($prgfile, $rc);
14 //var_dump($output);
15 //var_dump($rc);
16 ?>
The exec() and system() functions works when I
use any shell command (as in lines 2, 4, 5)
or a perl script (line 6). hello.pl prints a
"hello world" to the browser. "pwd" prints
the current working dir.

But when I use a C/C++ binary (line 7), "hello"
executable should print "hello world" to browser,
the script gives me a return code of 126 and the
$output is empty. But when I run the script from
linux shell prompt I can see the output.

The apache error log says
sh: ./hello: cannot execute binary file.

I guess, the fix should be simple but I don't
know what I'm missing here. I changed the permissions
of "hello" to 777, gave absolute path but still no output.

=================================================

The C/C++ code I used to create "hello"
(I compiled them with gcc/g++ and both runs).

#include <iostream>
using namespace std;
int main()
{
cout << "Hello World" << endl;
return 0;
}

#include <stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}

=================================================
Any help is appreciated.

Thanks,
Hemanth

Jul 17 '05 #1
14 11072
Con
Hi Memanth, is the php script in the same directory as the executable?

-Conrad

Jul 17 '05 #2
Yes the script and the executable are in the same directory. As I
mentioned before, I also tried with the absolute path to executable,
but it still doesn't seem to work.

- Hemanth

Jul 17 '05 #3
Hemanth napisa³:
Yes the script and the executable are in the same directory. As I
mentioned before, I also tried with the absolute path to executable,
but it still doesn't seem to work.

- Hemanth


Can't remember right now exactly, but I think there is a php.ini
directive, which (for security reasons, obviously) disables the
possibility toexecute binary files; and since you get
"sh: ./hello: cannot execute binary file."
I'd say that this might be it. Try to browse www.php.net on php.ini, you
might find something.
Providing, of course , I remember things right. :)

Cheers
-- Mike
Jul 17 '05 #4
Hello Mike,

I searched on php.net. The only thing I found in php.ini related to
exec is the safe_mode directive (which is already turned "off", i.e.,
executables can be placed in any directory).
Are you referring to some other directive?

Jul 17 '05 #5
Hemanth napisa³:
Hello Mike,

I searched on php.net. The only thing I found in php.ini related to
exec is the safe_mode directive (which is already turned "off", i.e.,
executables can be placed in any directory).
Are you referring to some other directive?


Hmmm... maybe I remember something thet's not there... But then again, I
think I remember something like this...

Ah, yes, here we are:

http://www.php.net/manual/en/function.system.php
"system -- Execute an external program and display the output"

http://www.php.net/manual/en/function.shell-exec.php
"shell_exec -- Execute command via shell and return the complete output
as a string"

http://www.php.net/manual/en/function.exec.php
"exec -- Execute an external program"

I think you should try "system()", and make sure that the executable is
actually executable for the user running the script (like "php" or
"apache").

HTH
Mike
Jul 17 '05 #6
On Tue, 29 Mar 2005 10:43:49 +0200, Micha³ Wo¼niak <mi*****@yahoo.co.uk> wrote:
Hemanth napisa³:
Yes the script and the executable are in the same directory. As I
mentioned before, I also tried with the absolute path to executable,
but it still doesn't seem to work.

- Hemanth


Can't remember right now exactly, but I think there is a php.ini
directive, which (for security reasons, obviously) disables the
possibility toexecute binary files; and since you get
"sh: ./hello: cannot execute binary file."
I'd say that this might be it. Try to browse www.php.net on php.ini, you
might find something.
Providing, of course , I remember things right. :)


I don't think that's it, since it's not PHP that's raising that message. If
exec() etc. were disabled, you'd get a PHP exception and the other examples
would not run. The OP claims that other executables do run.

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #7
Mike,

Thanks for the pointers but I did read the manual and tried all the
functions (passthru, exec, system, backtick etc.). Also as I mentioned
in my original posting, I changed the executable permissions to 777 but
it still doesn't work.

It seems really wierd to me why the script doesn't execute a binary
file when run from a browser but works when I run the command <php
scriptname> from linux shell prompt.
I'm not able figure out exactly what's going wrong.

- Hemanth

Jul 17 '05 #8
Hemanth (he*****************@gmail.com) wrote:
: Mike,

: Thanks for the pointers but I did read the manual and tried all the
: functions (passthru, exec, system, backtick etc.). Also as I mentioned
: in my original posting, I changed the executable permissions to 777 but
: it still doesn't work.

: It seems really wierd to me why the script doesn't execute a binary
: file when run from a browser but works when I run the command <php
: scriptname> from linux shell prompt.
: I'm not able figure out exactly what's going wrong.

$0.02

Does the C++ program have to access any shared (i.e. dynamically loaded)
libraries?

If so then it has to be able to access them. Various environment
variables may come into play, as well as file permissions on the
libraries. I'm not sure of the exact details.
--

This space not for rent.
Jul 17 '05 #9
Hemanth napisa³:
Mike,

Thanks for the pointers but I did read the manual and tried all the
functions (passthru, exec, system, backtick etc.). Also as I mentioned
in my original posting, I changed the executable permissions to 777 but
it still doesn't work.
[Note to myself: start reading the posts]
It seems really wierd to me why the script doesn't execute a binary
file when run from a browser but works when I run the command <php
scriptname> from linux shell prompt.
I'm not able figure out exactly what's going wrong.

- Hemanth


That's strange to me too. To be honest, I have run out of ideas. Just one
more wild thought: maybe it has something to do with the Apache config?
It's brainstorming though, I haven't got a clue what could it be.

Cheers
Mike
Jul 17 '05 #10
On 29 Mar 2005 14:17:22 -0800, "Hemanth"
<he*****************@gmail.com> wrote:
It seems really wierd to me why the script doesn't execute a binary
file when run from a browser but works when I run the command <php
scriptname> from linux shell prompt.
I'm not able figure out exactly what's going wrong.


Sounds like there might be some permissions wrong somewhere. To rule
that out, try 'su www-data' (replace www-data with the correct user
that your webserver uses) and try to run the executable file. If it
doesn't work, then you have wrong permissions on some lib or
something.

- allan savolainen

Jul 17 '05 #11
>Thanks for the pointers but I did read the manual and tried all the
functions (passthru, exec, system, backtick etc.). Also as I mentioned
in my original posting, I changed the executable permissions to 777 but
it still doesn't work.


Apache typically will refuse to run a program with excess write permission
as a CGI. Perhaps PHP has a restriction like this also. In any case,
giving an executable program mode 777 is a bad idea. If you just want
to run it, 755 should be plenty.

Gordon L. Burditt
Jul 17 '05 #12
It worked finally. I rebooted the linux server, recompiled the C/C++
programs and gave absolute path to the execuatble in the script. I'm
not sure how it got fixed b'cos I did the same steps before (except for
the reboot).

I appreciate everyone's help and patience.

- Hemanth

Jul 17 '05 #13
Hemanth napisa³:
It worked finally. I rebooted the linux server, recompiled the C/C++
programs and gave absolute path to the execuatble in the script. I'm
not sure how it got fixed b'cos I did the same steps before (except for
the reboot).

I appreciate everyone's help and patience.

- Hemanth


Just to make things clear: you did restart apache/php after modifying the
configs, didn't you? ;)
I know, dumb question, but I had some of such "mistakes" myself. :)

Cheers
Mike
Jul 17 '05 #14
I didn't modify the config files (php.ini or apache conf file). I
looked in the php.ini file to see if the safe_mode directive is off or
on. It's already 'off' so I didn't make any changes.
But as you said, restarting the apache would have been a help, instead
of rebooting the machine.

- Hemanth

Jul 17 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Paolo Scolamacchia | last post: by
2 posts views Thread by Greg Chapman | last post: by
6 posts views Thread by Hal Vaughan | last post: by
2 posts views Thread by Jonathan | last post: by
11 posts views Thread by Chris Mantoulidis | last post: by
5 posts views Thread by TPJ | last post: by
3 posts views Thread by Mike | last post: by
4 posts views Thread by vol30w60 | last post: by
23 posts views Thread by Maarten | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.