user6889435
user6889435

Reputation:

PHP proc_open return value is always equal to 127

I am facing really big issue with PHP. I am creating PHP application, that will "use" another application. I want to redirect input and output. I'm using proc_open to do so. File gsqasm is inside /var/www/http. The working directory is the same, so ./gsqasm should do the trick. Too bad, that it doesn't. Return code always is equal to 127. After googling for a while, i figured out that 127 means that file couldn't be found. Then, to make sure, I checked /tmp/error-output.txt file. It's content was:

sh: 1: gsqasm: not found
sh: 1: /var/www/http/gsqasm: not found
sh: 1: /var/www/http/gsqasm: not found
sh: 1: /var/www/http/gsqasm: not found
sh: 1: ./gsqasm: not found
sh: 1: ./gsqasm: not found
sh: 1: .gsqasm: not found
sh: 1: /var/www/http/gsqasm: not found
sh: 1: /var/www/http/gsqasm: not found

As you can see, I tried maaany times, without luck. I am propably missing something obvious, but I can't figure it out. Here is my code:

<?php
$descriptorspec = array(
    0 => array(
        "pipe",
        "r"
    ), // stdin is a pipe that the child will read from
    1 => array(
        "pipe",
        "w"
    ), // stdout is a pipe that the child will write to
    2 => array(
        "file",
        "/tmp/error-output.txt",
        "a"
    ) // stderr is a file to write to
);

$cwd = '/tmp';
$env = array(
    'some_option' => 'aeiou'
);

passthru('echo $PWD');
echo '<br>';
$process = proc_open('./gsqasm', $descriptorspec, $pipes, $cwd, $env);

if (!isset($_POST['input'])) {
    echo "Input is empty.";
}

if (is_resource($process)) {
    // $pipes now looks like this:
    // 0 => writeable handle connected to child stdin
    // 1 => readable handle connected to child stdout
    // Any error output will be appended to /tmp/erroroutput.txt

    fwrite($pipes[0], $_POST['input']);
    fclose($pipes[0]);

    echo stream_get_contents($pipes[1]);
    fclose($pipes[1]);

    // It is important that you close any pipes before calling
    // proc_close in order to avoid a deadlock
    $return_value = proc_close($process);

    echo "command returned $return_value\n";
}
?>

How do I change this code, to execute application correctly, while redirecting streams? This is not code designed to run on production, It's some quick and dirty thing that will get solid after some time.

Upvotes: 0

Views: 886

Answers (1)

user1861458
user1861458

Reputation:

Exit code 127 does in fact mean "command not found". The file may exist at /var/www/http/gsqasm, but you're telling it to look at /tmp/gsqasm. You have $cwd, the working directory that the command will be run from, set to /tmp. The command is also prepended by ./, telling it to look in the (incorrectly provided) current working directory for gsqasm.

To fix this, you can change $cwd to be null (see documentation, this defaults to the script's directory) or an absolute path to the program's location, which you said is also /var/www/http. You also could change ./ to include that absolute path. Either of those three options will work, I personally would recommend the first because of simplicity.

Upvotes: 2

Related Questions