Call LLVM-GCC in PHP - php

I tried to generate LLVM intermediate code by calling
llvm-gcc -emit-llvm -I/mypath/ -c main.c -o main.o
It works perfectly without any warnings or errors if I manually type this command in the terminal.
However I have built a website that can automate this process by calling exec function in PHP like below.
exec("llvm-gcc -emit-llvm -I/mypath/ -c main.c -o main.o",$msg,$ret);
It will still generate .o file with a warning. The warning says that
Potential incompatible plugin version. GCC: 4.5.3. Expected: 4.5.4
Defines 'dragonegg_disable_version_check' as env variable to remove this warning.
Please note that unexpected errors might occur.
The php command will still return 0, which is successful. However when I run the generated .o file, it will throw invalid bitcode signature error.
The server is running ubuntu and Apache. My llvm-gcc version is 2.9 which uses 4.2.1 gcc.
Any help will do. Thank you!

The best thing would be to update the GCC to 4.5.4. But if you just want to hide the warning, simply define dragonegg_disable_version_check.
$command = 'dragonegg_disable_version_check=1'
. ' llvm-gcc -emit-llvm -I/mypath/'
. ' -c main.c -o main.o';
exec($command,$msg,$ret);
For the invalid bit code signature error -- see this questions.

Related

/usr/bin/less: unrecognized option: X laravel docker error

i am using laravel on docker . i run my project when i exec into container and use
php artisan tinker
no matter what command i run i receive this error :
bash-5.1$ php artisan tinker
Psy Shell v0.11.8 (PHP 8.0.14 — cli) by Justin Hileman
>>> App\Models\User::where('id',12)->first()->createToken('testToken');
/usr/bin/less: unrecognized option: X
BusyBox v1.34.1 (2021-11-23 00:57:35 UTC) multi-call binary.
Usage: less [-EFIMmNSRh~] [FILE]...
View FILE (or stdin) one screenful at a time
-E Quit once the end of a file is reached
-F Quit if entire file fits on first screen
-I Ignore case in all searches
-M,-m Display status line with line numbers
and percentage through the file
-N Prefix line number to each line
-S Truncate long lines
-R Remove color escape codes in input
-~ Suppress ~s displayed past EOF
RuntimeException with message 'Error closing output stream'
any idea what can be wrong here ?
Busybox contains cutdown versions of many Unix/Linux utilities, including less. Either remove it and install the less package, as well as the other utilities it mimics, or hack artisan and remove the -X switch against /usr/bin/less. All the -X switch does is sort alphabetically by entry extension
Ugh just spent a ton of time messing with this one myself.
The root cause of this issue is the upgrading of the psypsh shell package that tinker users.
You can see in the release notes here https://github.com/bobthecow/psysh/releases/tag/v0.11.3
There are a few ways around this, as mentioned above, you can just install the less package using apk or apt-get.
You can also set the cli.pager property in your php.ini file to explicitly call less without the -X switch.
More info here: https://github.com/bobthecow/psysh/issues/717

How i can run function exec in PHP

I can't add * to my code to find file
this code work
exec("mediaconvert -t wav -i /home/20220228/11/23401.rec -o /var/www/html/test.mp3");
if i add a the *, it don't work
exec("mediaconvert -t wav -i /home/20220228/11/*01.rec -o /var/www/html/test.mp3");
p.s. in path is only one file, when i try execute this code from shell it work. Pls help me)
Filename expansion and other bash-specific features may/will not work in other shells (e.g. standard POSIX). If your command with * is not executed in bash/compatible, it won't work as expected. You need to verify the environment/shell that your PHP installation executes commands in.
Run the following test script:
<?php
exec('echo "$SHELL"', $out);
var_dump($out);
When I run the PHP script directly on CLI, I get "/bin/bash" for the shell that's called. When I run it via browser, curiously I get "/sbin/nologin" instead. There are different environments for user apache that executes PHP via browser calls, and the "actual" user logging in via SSH. Bash shell is not available for the Apache user by default.
These results are from a Centos7 server with Apache 2.4/PHP 8.1.4 running. Your mileage may vary. Bottom line: if the command you are executing depends on bash-specific features, it must execute in a bash environment, or another shell that supports the required features.
If bash is not available, your other option is using e.g. glob to get files matching the pattern in your directory, and then loop over them while executing the command for each specific file.
Edit: As pointed out by #Sammitch (see comments), /sbin/nologin is a common "shell name" choice for non-login users, and most likely uses /bin/sh. This should still allow for filename expansion/globbing. Testing browser script call with exec('ls *.php', $out); the wildcard functions as expected.
You may find this question/answer relevant: Use php exec to launch a linux command with brace expansion.
I recommend you do the opposite. First, get the files you want to input then you exec. For instance:
$input_files = ...
exec("mediaconvert -t wav -i " . $input_files . " -o /var/www/html/test.mp3");
You can try to find files with glob() function and after that you can use exec(). You can try a similiar solution with the following code:
$input_files = '/home/20220228/11/*01.rec';
foreach (glob($input_files) as $filename) {
exec("mediaconvert -t wav -i " . $filename . " -o /var/www/html/test.mp3");
}

Not able to run wkhtmltopdf commad through shell_exec() function in php but same command works on command line

I'm in trouble and that much confused about a php shell_exec command.
When the command is execute by PHP I have no error but the execution fails. If I use exactly the same command from a terminal it works.
Here's the command :
/usr/bin/wkhtmltopdf --lowquality --dpi 300 --encoding utf-8 "/tmp/knplabs_snappyxa9otq.html" "/tmp/knplabs_snappyv3pD7h.pdf"
When I lauch this from a terminal :
$ /usr/bin/wkhtmltopdf --lowquality --dpi 300 --encoding utf-8 "/tmp/knplabs_snappyWG9XTd.html" "/tmp/knplabs_snappyv3pD7h.pdf"
Loading page (1/2)
Printing pages (2/2)
Done
But from my php script :
// Construct the previous command
$command = $this->buildCommand($url, $path);
../..
shell_exec($command);
../..
$content = file_get_contents($path);
../..
I've test the output of shell_exec, it's empty.
The log :
Warning: file_get_contents(/tmp/knplabs_snappyv3pD7h.pdf): failed to open stream: No such file or directory in /*****/lib/snappy/SnappyMedia.class.php on line 64
No permission pb in the /tmp directory :
$ ls -la /tmp
total 448
drwxrwxrwt 16 root root 4096 mars 12 21:51 .
../..
I've tried avec the PHP exec() function to get error informations, I just get an "1" error code in return_var and nothing in output.
For information this issue appear on my test server, my desktop computer but not on my notebook. All the 3 are with sames PHP, Apache, Mysql versions.
I don't understand anything ...
Thanks for any help, I'm loosing my mind.
David.
I've found the solution here : Executing wkhtmltopdf from PHP fails
Thanks to Krzychu.
First to get information from the shell_exec command add " 2>&1" at the end of the command. In that way you will get information in return of the command :
$no_output = shell_exec($command);
echo $no_output; // nothing
$output = shell_exec($command . ' 2>&1');
echo $output; // in my case : "cannot connect to X server"
The solution :
Not use the wkhtmltopdf ubuntu package (0.9.9-4)
Use the official package from the Wkhtmltopdf download page
So no need to install xvfb ! (I've seen this advice many times)
Looks like a user's permissions issue.
When you run the command from the terminal, it is the user account, currently used, which does have the right permissions, to run a command in /usr/bin, and execute the specific file.
When you run it from the php script, it is the http server account on your system, which needs the permission to execute the file in /usr/bin. Usually this is the apache user.
How you should setup permissions depends on your system. Just remember that what is allowed for apache, is allowed for anyone accessing your http server.
I have had this problem for ages and adding . ' 2>&1' after the $command has somehow solved the problem.
this:
$output = shell_exec($command . ' 2>&1');
instead of:
$output = shell_exec($command);
No idea why but it works and I'm grateful.
Is it a shared hosting? It seems like shell_exec is a restricted function. Try running error_reporting(E_ALL); ini_set('display_errors', 1); before calling shell_exec.
I stumbled upon the same Problem, in my case an absolut Path in the exec Command like /var/www did not work, I had to use relative Paths from the point where I executed the php File.
I also wanted to notice, that it did not work using shell_exec, however it worked using normal exec command, not sure wheres the difference here.

PHP calls to git command via system(), fails with exit code 128

CentOS 5.9, PHP 5.4.21, Tomcat 7.0.42, Safe mode is off.
I need to commit some code to repository from php. but failed with exit code 128.
Codes are below, and command_exec is 'cd /data/project && git add .'
ob_start();
$this->system_call_detail = system($this->command_exec, $this->output);
$logger->debug('ExecuteCMD system call result : '.$this->system_call_detail);
ob_end_clean();
I can run git command as the apache user account from cmd, but programs run from PHP fail with exit code 128.
I guessed this cause from PHP. So, I tried this git command, "php -r 'system("cd /data/project && git add .", $test); echo $test;'" from cmd as apache user account and it success.
I solved this problem..
First, I couldn't get contents when run command, because of some php bug(?).
http://kr1.php.net/manual/en/function.system.php#108713
I fixed the command belows:
$this->system_call_detail = system($this->command_exec.' 2>&1', $this->output);
and then, I could get error msgs belows:
fatal: unable to access '/home/{username}/.config/git/config': Permission denied
But, there were no such files or directory.
So, I googled with the error msgs. and found some blog.
http://www.jethrocarr.com/2013/08/25/the-apache-that-wanted-to-be-root/
And I edited '/etc/sysconfig/httpd' file with the blogs posted.
And Solved. :)
Thank for helping me.
deceze and ojrask

Calling git from PHP: Broken pipe error

I have the following PHP script:
#!/usr/bin/php
<?php
echo shell_exec(
"/usr/bin/git clone --bare ".
"/home/dave/create_project/template_project ".
"/home/dave/create_project/my_test_project.git"
);
About 7 in 10 times that I run it, git gives the following error:
find: write error: Broken pipe
This error never occurs if I run the equivalent command directly from the shell.
I have already tried:
using other PHP execution functions: exec, system, popen;
passing the whole command as an argument to bash, i.e., exec('bash -c '.$cmd);
Does anyone have any idea what might be going on?
It may depend on your exact platform, but findutils has been known to throw that kind of error message before.
On Fedora, that rpm package version 4.2.33-2.fc9 fixed the issue.
Does PHP throw any errors? Maybe max_execution_time is too low? Mu guess PHP app exites prematurely.

Categories