简体   繁体   中英

Running a C compiled program through a PHP webpage under linux

My page.php file looks like this at the moment:

    <?php

    $cmd = './foo < input.txt > output.txt';

    $result = shell_exec($cmd);

    echo system('date'); //just to see change when refreshing the page

    ?>

Whenever I type the same command in the shell, it works perfectly (with the input.txt file ready with an example input).

My idea was to create an interface using php to communicate with the input.txt file and then communicate with the output.txt file to return the results on the page. But, first of all, I need to make sure I can run the program I have.

I've compiled with

    gcc -Wall -o foo foo.c

The foo executable is on the same directory as the page.php.

I've tried with 777 permissions on all the files (page.php, input.txt and foo)

The site is up and running and the date changes when I refresh the page but there's no output.txt on the directory.

I've tried

    $cmd = 'ls -la';

    $result = shell_exec($cmd);

    echo $result;

and it works as intended, showing the contents of the proper directory.

One thing which can cause this type of problem is if Apache does not have write access to the directory where output.txt is supposed to go. Do an ls -l ../ to see whether or not you actually have the requisite permissions. I would also echo out the result, as that might give you more of an idea of what is going on.

As stated in a comment, the problem was permission based. The minimum permissions required for this to work are as follows

"711 on the executable files, 644 on input file, 666 on output file and 755 on containing folder."

Thanks for all the contributions.

Another possible problem is that if you have SELinux enabled (as is the default in Fedora and Red Hat), it will block Apache from running commands which have not been marked as being in the context of Apache. This is a security feature meant to make it so that if someone hacks your web server or web application they can't use it to run arbitrary commands on your server.

To set the correct context for the application, you'd want to run the command chcon -t httpd_sys_script_exec_t execname (where execname is replaced with the name of your executable). You'll also need to change the context of the input and output files to allow them to be written by apache scripts via chcon. You'll want to use type httpd_sys_script_ro_t for read-only input files and type httpd_sys_script_rw_t for read/write output files or type httpd_sys_script_ra_t for read/append output files. The chcon command generally only changes things until the next server reboot, though. File contexts are remapped based on directories at that time, so in the long run, you'll want to adjust your policy to set those contexts as the default contexts for those files. I don't remember how to do that off the top of my head, however, so you'll need to look that up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM