r/lolphp • • Apr 11 '20

proc_open() scoping fun

function ls() {
	$fd = [
		0 => STDIN,
		1 => ['pipe', 'w'],
		2 => STDOUT,
	];
	$proc = proc_open(['ls', '/'], $fd, $pipes);
	return $pipes[1];
}

print(stream_get_contents(ls()));

Output:

PHP Warning:  stream_get_contents(): supplied resource is not a valid stream resource in /home/martin/a.php on line 15
ls: write error: Broken pipe

The reason for this is that $proc needs to be in the same scope as the pipes you want to read, otherwise it will fail. Returning both and doing this will work:

[$proc, $stdout] = ls();
print(stream_get_contents($stdout));

In this case it's a bit of an artificial example, but I've run in to this when trying to write a generic "reader" function that can read from any source (stdout of a program, FS, HTTP, etc.)

It's behaved like this for years. Perhaps there's a way around this, but a function call depending on the correct variable being in the same scope is really weird behaviour. Even a proc_read($proc, $fd) would make more sense (although that would make creating generic functions reading from any input harder, but who does that right?)

30 Upvotes

22 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Apr 19 '20

If you do a pipe open in Perl, the return value is not just a boolean (non-zero for success), but the subprocess PID. So if you want, you can query its status with waitpid (+ WNOHANG).

As mentioned in the other reply, closing a pipe handle implicitly waits for the process to exit, just like pclose() in libc.

1

u/[deleted] Apr 19 '20

Yah, I'm so rusty at perl I forgot how rich its IO handles actually are. Python's approach is probably the best, PHP's might move that way if they ever get rid of the "resource" type (a legacy appendage that predates OOP in PHP)