Connecting Tech Pros Worldwide Help | Site Map

Calling gcc from within PHP code

Newbie
 
Join Date: Dec 2006
Posts: 4
#1: Dec 13 '06
I am currently trying to create a php script that will call gcc and pass various arguments so that you can compile a c program on a remote server after uploading your raw code. I'm using something along the following lines but as of yet it's not working:

[PHP]
$command = "gcc /home/username/sourcefile.c";
$output = shell_exec($command);
echo $output;
[/PHP]

When I run this I get a blank browser window, and the executable file that should be produced is not. I have tried creating a batch file containing the $command string and then replacing $command in my script with
[PHP]
$command = "sh batchfile";
[/PHP]
but I get the same result.

Any ideas? I know the shell_exec command works (i.e. PHP safe mode is not on) as I can run an "ls" command just fine, it appears only the call to gcc does not work, even if I put in the full path (/usr/bin/gcc). The script is running on a web hosting site Linux server and I've verified that gcc exists. I have also tried running this on a different Linux server at work with the same results.

Many thanks for any help!
Jon
b1randon's Avatar
Expert
 
Join Date: Dec 2006
Location: Pittsburgh
Posts: 171
#2: Dec 14 '06

re: Calling gcc from within PHP code


Quote:

Originally Posted by joxtoby

I am currently trying to create a php script that will call gcc and pass various arguments so that you can compile a c program on a remote server after uploading your raw code. I'm using something along the following lines but as of yet it's not working:

[PHP]
$command = "gcc /home/username/sourcefile.c";
$output = shell_exec($command);
echo $output;
[/PHP]

When I run this I get a blank browser window, and the executable file that should be produced is not. I have tried creating a batch file containing the $command string and then replacing $command in my script with
[PHP]
$command = "sh batchfile";
[/PHP]
but I get the same result.

Any ideas? I know the shell_exec command works (i.e. PHP safe mode is not on) as I can run an "ls" command just fine, it appears only the call to gcc does not work, even if I put in the full path (/usr/bin/gcc). The script is running on a web hosting site Linux server and I've verified that gcc exists. I have also tried running this on a different Linux server at work with the same results.

Many thanks for any help!
Jon

This might be a long shot but have you checked for a permissions problem? I think php usually runs things in a really underprivileged account. See if you can change the permissions or verify that they are adequate. Good luck! This is a neat task.
Newbie
 
Join Date: Dec 2006
Posts: 4
#3: Dec 14 '06

re: Calling gcc from within PHP code


Quote:

Originally Posted by b1randon

This might be a long shot but have you checked for a permissions problem? I think php usually runs things in a really underprivileged account. See if you can change the permissions or verify that they are adequate. Good luck! This is a neat task.

That was the problem! Can't believe I overlooked it, many thanks!
Familiar Sight
 
Join Date: Aug 2005
Location: Houston, TX
Posts: 143
#4: Dec 14 '06

re: Calling gcc from within PHP code


I've noticed similar permissions issues when you want PHP to write a log, for instance.

I have a question, though: what's the best way to elevate the permissions so that PHP can do what it needs without overly compromising security? (Assuming a *nix environment.) Certainly, doing a chmod a+w isn't the greatest way to go. ;)

Thanks for posting on a fun topic. :) -- Paul
Reply