Home > Archive > PHP Programming > March 2005 > exec() doesn't work on C/C++ executables
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
exec() doesn't work on C/C++ executables
|
|
| Hemanth 2005-03-28, 8:56 pm |
| 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
| |
|
| Hi Memanth, is the php script in the same directory as the executable?
-Conrad
| |
| Hemanth 2005-03-29, 3:57 am |
| 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
| |
| Michał Woźniak 2005-03-29, 3:57 pm |
| 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
| |
| Hemanth 2005-03-29, 8:56 pm |
| 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?
| |
| Michał Woźniak 2005-03-29, 8:56 pm |
| 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
| |
| Andy Hassall 2005-03-29, 8:56 pm |
| On Tue, 29 Mar 2005 10:43:49 +0200, Michał Woźniak <mikiwoz@yahoo.co.uk> wrote:
>Hemanth napisał:
>
>
>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 / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
| |
| Hemanth 2005-03-29, 8:56 pm |
| 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
| |
| Malcolm Dew-Jones 2005-03-29, 8:56 pm |
| Hemanth (hemanth.singamsetty@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.
| |
| Michał Woźniak 2005-03-30, 3:58 am |
| 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
| |
| Allan Savolainen 2005-03-30, 8:58 am |
| On 29 Mar 2005 14:17:22 -0800, "Hemanth"
<hemanth.singamsetty@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
| |
| Gordon Burditt 2005-03-30, 3:56 pm |
| >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
| |
| Hemanth 2005-03-30, 3:56 pm |
| 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
| |
| Michał Woźniak 2005-03-30, 8:56 pm |
| 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
| |
| Hemanth 2005-03-31, 3:57 pm |
| 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
|
|
|
|
|