472,800 Members | 1,289 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,800 software developers and data experts.

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 11563
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Paolo Scolamacchia | last post by:
Hi everybody, this is my environment : Apache is installed on a Linux server (RedHat9A) and I launch the browser on a PC running WindowsXP to execute php scripts on the Linux server. I would run...
2
by: Greg Chapman | last post by:
I am at my wit's end trying to get information out of Streamline.net's support dept about my problem. They reply quickly enough, but seem to try and give out the least possible amount of info each...
6
by: Hal Vaughan | last post by:
I have a script used to find and run Java on a Windows system.  It worked fine on original tests (on a Windows XP system).  It's now running on a Windows 2000 (sp3) system, and it won't work -- the...
2
by: Jonathan | last post by:
I'm puzzled by Python's behavior when binding local variables which are introduced within exec() or execfile() statements. First, consider this simple Python program: # main.py def f() : x = 1...
11
by: Chris Mantoulidis | last post by:
Out of curiosity, I wanted to test executable file sizes of the same program from different compilers... The 2 compilers I tested (both old, on purpose) were Borland C++ 3.1 and DJGPP 2.03... ...
5
by: TPJ | last post by:
I have the following code: ----------------------------------- def f(): def g(): a = 'a' # marked line 1 exec 'a = "b"' in globals(), locals() print "g: a =", a
3
by: Mike | last post by:
Hi all, In my recent project (using ASP.NET 2.0 and MSSQL), I need to scheduled a certain operation to be executed, for example, on beginning of a month. Is there anyway to do this on...
4
by: vol30w60 | last post by:
Hi folks, I am trying to launch a program with PHP code. I am running Apache on Windows XP SP2. I am using the method noted here: http://us2.php.net/manual/en/function.exec.php#59428 To...
23
by: Maarten | last post by:
Howdy, Recently I switched from a Windows PC to Mac OS-X 10.5 (php v5.2.6) and I have a little problem with one function within my cd-management script. For extracting a bit of info from my...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.