Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP system call function does not properly export variables

Tags:

bash

php

apache

I'm using task-spooler to queue up some video encoding tasks (See: http://manpages.ubuntu.com/manpages/artful/en/man1/tsp.1.html and http://vicerveza.homeunix.net/~viric/soft/ts/)

In order to select a specific queue (rather than the default per-user queue), the binary requires that you set an environment variable pointing to a socket. In my case: TS_SOCKET=/tmp/reencode.socket. Then running tsp shows the status of that specific queue.

This command works in the terminal, as any user: export TS_SOCKET=/tmp/reencode.socket ; tsp

I want to create a web-based status utility that shows this status. Using php on apache2, however, calling a bash script with exec() with that same exact command does not work. Nor does putenv('TS_SOCKET=/tmp/reencode.socket'); echo exec('tsp'); I've alse tried echo `export TS_SOCKET=/tmp/reencode.socket ; tsp`

Running passthru('whoami') on the server shows that it's running as user www-root, the apache server user. If I log in as that user and run the command, it works fine as expected.

Interestingly, if I run the php interactive shell (php -a), I can do any of the above php code and it works just fine, so it seems to be some interaction with apache.

This is all running on Ubuntu 17.10 with PHP 7.1.11 on Apache 2.4.27

like image 289
Kayson Avatar asked Feb 15 '18 01:02

Kayson


2 Answers

Turns out the problem wasn't the setting of the environment variable, but accessing the socket. Since apache is running through systemd, it has an isolated /tmp (see https://unix.stackexchange.com/questions/345122/why-php-can-not-see-tmp-files) that is not the same as the /tmp where I was putting the socket. Moving the location made everything work just fine!

like image 68
Kayson Avatar answered Sep 24 '22 02:09

Kayson


Hm, this simple test case seems to work via apache + mod_php (7.0)

<?php

header('content-type: text/plain; charset=utf-8');
$out = [];
exec('SOME_VAR=FOO && echo $SOME_VAR', $out);
// works too: exec('export SOME_VAR=FOO && echo $SOME_VAR', $out);
var_dump($out);

output:

array(1) {
  [0]=>
  string(3) "FOO"
}

In your case this would be

<?php

header('content-type: text/plain; charset=utf-8');
$out = [];
exec('TS_SOCKET=/tmp/reencode.socket && tsp', $out);
// also try: exec('export TS_SOCKET=/tmp/reencode.socket && tsp', $out);
var_dump($out);
like image 23
Philipp Wrann Avatar answered Sep 26 '22 02:09

Philipp Wrann