简体   繁体   中英

Why shell_exec of php is not giving output of some a.out file

I am working with a php script that can compile and run c++ code . maximum kinds of c++ code is being compiled and giving output by shell_exec command. But some kinds of c++ code having structure, class are being compiled but are unable to fetch output from a.out file by shell_exec in apache server. As a result it's output is showing null in php. It is noted that in ubuntu terminal it works but when i run it from php it can not be run. i am running it on ubuntu, apache server. Here is my php code that can run c++ code

<?php
$CC="g++";
$out="timeout 5s ./a.out";
$code=$_POST["code"];
$input=$_POST["input"];
$filename_code="main.cpp";
$filename_in="input.txt";
$filename_error="error.txt";
$executable="a.out";
$command=$CC." -lm ".$filename_code;    
$command_error=$command." 2>".$filename_error;
$check=0;

//if(trim($code)=="")
//die("The code area is empty");

$file_code=fopen($filename_code,"w+");
fwrite($file_code,$code);
fclose($file_code);
$file_in=fopen($filename_in,"w+");
fwrite($file_in,$input);
fclose($file_in);
exec("chmod -R 777 $filename_in");
exec("chmod -R 777 $filename_code");  
exec("chmod 777 $filename_error");  

shell_exec($command_error);
exec("chmod -R 777 $executable");
$error=file_get_contents($filename_error);
$executionStartTime = microtime(true);

if(trim($error)=="")
{
    if(trim($input)=="")
    {
        $output=shell_exec($out);
    }
    else
    {
        $out=$out." < ".$filename_in;
        $output=shell_exec($out);

    }
    //echo "<pre>$output</pre>";
          echo "<textarea id='div' class=\"form-control\" name=\"output\" rows=\"10\" cols=\"50\">$output</textarea><br><br>";
}
else if(!strpos($error,"error"))
{
    echo "<pre>$error</pre>";
    if(trim($input)=="")
    {
        $output=shell_exec($out);
    }
    else
    {
        $out=$out." < ".$filename_in;
        $output=shell_exec($out);
    }
                    echo "<textarea id='div' class=\"form-control\" name=\"output\" rows=\"10\" cols=\"50\">$output</textarea><br><br>";
}
else
{
    echo "<pre>$error</pre>";
    $check=1;
}
$executionEndTime = microtime(true);
$seconds = $executionEndTime - $executionStartTime;
$seconds = sprintf('%0.2f', $seconds);
echo "<pre>Compiled And Executed In: $seconds s</pre>";


if($check==1)
{
    echo "<pre>Verdict : CE</pre>";
}
else if($check==0 && $seconds>3)
{
    echo "<pre>Verdict : TLE</pre>";
}
else if(trim($output)=="")
{
    echo "<pre>Verdict : WA</pre>";
}
else if($check==0)
{
    echo "<pre>Verdict : AC</pre>";
}

exec("rm $filename_code");
exec("rm *.o");
exec("rm *.txt");
exec("rm $executable");
?>

While running through terminal, you are executing PHP script with your user permission. But while running the script using apache, you are running the script under www-data user. Some executable can not be run under www-data user. A solution that you can try is to add www-data to your user group.

You can find an interesting read at https://unix.stackexchange.com/a/127529

At last i have found my solution. i have just changed my g++ version to 4.9 and then shell_exec() has started working.

Install g++ 4.9

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install g++-4.9

Set g++ 4.9

sudo ln -f -s /usr/bin/g++-4.9 /usr/bin/g++

The c++ structure and class codes that were not being able to show output previously are showing output now. I don't know why in terminal the g++ 5 version was working but not working in shell_exec() but g++ 4.9 is working in both terminal and shell_exec()

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