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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hemanth

    #1

    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>Workin g 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($outpu t);
    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

  • Con

    #2
    Re: exec() doesn't work on C/C++ executables

    Hi Memanth, is the php script in the same directory as the executable?

    -Conrad

    Comment

    • Hemanth

      #3
      Re: exec() doesn't work on C/C++ executables

      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

      Comment

      • Micha³ Wo¼niak

        #4
        Re: exec() doesn't work on C/C++ executables

        Hemanth napisa³:
        [color=blue]
        > 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[/color]

        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

        Comment

        • Hemanth

          #5
          Re: exec() doesn't work on C/C++ executables

          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?

          Comment

          • Micha³ Wo¼niak

            #6
            Re: exec() doesn't work on C/C++ executables

            Hemanth napisa³:
            [color=blue]
            > 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?[/color]

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

            Ah, yes, here we are:

            Execute an external program and display the output

            "system -- Execute an external program and display the output"

            Execute command via shell and return the complete output as a string

            "shell_exec -- Execute command via shell and return the complete output
            as a string"


            "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

            Comment

            • Andy Hassall

              #7
              Re: exec() doesn't work on C/C++ executables

              On Tue, 29 Mar 2005 10:43:49 +0200, Micha³ Wo¼niak <mikiwoz@yahoo. co.uk> wrote:
              [color=blue]
              >Hemanth napisa³:
              >[color=green]
              >> 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[/color]
              >
              >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. :)[/color]

              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.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

              Comment

              • Hemanth

                #8
                Re: exec() doesn't work on C/C++ executables

                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

                Comment

                • Malcolm Dew-Jones

                  #9
                  Re: exec() doesn't work on C/C++ executables

                  Hemanth (hemanth.singam setty@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.

                  Comment

                  • Micha³ Wo¼niak

                    #10
                    Re: exec() doesn't work on C/C++ executables

                    Hemanth napisa³:
                    [color=blue]
                    > 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.[/color]

                    [Note to myself: start reading the posts]
                    [color=blue]
                    > 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[/color]

                    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

                    Comment

                    • Allan Savolainen

                      #11
                      Re: exec() doesn't work on C/C++ executables

                      On 29 Mar 2005 14:17:22 -0800, "Hemanth"
                      <hemanth.singam setty@gmail.com > wrote:
                      [color=blue]
                      >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.[/color]

                      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

                      Comment

                      • Gordon Burditt

                        #12
                        Re: exec() doesn't work on C/C++ executables

                        >Thanks for the pointers but I did read the manual and tried all the[color=blue]
                        >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.[/color]

                        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

                        Comment

                        • Hemanth

                          #13
                          Re: exec() doesn't work on C/C++ executables

                          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

                          Comment

                          • Micha³ Wo¼niak

                            #14
                            Re: exec() doesn't work on C/C++ executables

                            Hemanth napisa³:
                            [color=blue]
                            > 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[/color]

                            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

                            Comment

                            • Hemanth

                              #15
                              Re: exec() doesn't work on C/C++ executables

                              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

                              Comment

                              Working...