|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-12-03 18:24 UTC] dchurch at sciencelogic dot com
Description:
------------
PHP cannot open, read from, or write to arbitrary file descriptors, a feature that is necessary for communications with certain utilities that expect input or output on certain file descriptors. When programming in C, this functionality is provided by fdopen(). A feature request was filed over seven years ago for PHP to have a similar capability through the php:// stream wrapper (#26158), but the bug was marked as bogus for no adequately defined reason.
The alternative to being able to use a fdopen()-like function is to open the file descriptor as a named file through /dev/fd/ or /proc/self/fd/. This works in C using fopen("/dev/fd/<descriptor>", "w"), but it does not work in PHP because PHP incorrectly attempts to dereference the pseudo-symlink before opening it.
I have a C program and a PHP script that both attempt to do the same thing: open file descriptor 3 for writing using /dev/fd/3, read until EOF from standard in and write to the file descriptor. Both should be run as:
(executable) 3>&1 | cat
Test script:
---------------
PHP script:
<?php
$file = fopen("/dev/fd/3", "w");
if (!$file) exit(1);
$input = stream_get_contents(STDIN);
fwrite($file, $input);
fclose($file);
C program:
#include <stdio.h>
int main() {
char buf[8192];
int count;
FILE *file;
file = fopen("/dev/fd/3", "w");
if (file == NULL) {
perror("Opening /dev/fd/3");
return 1;
}
while ((count = fread(buf, 1, sizeof(buf), stdin)) > 0) {
fwrite(buf, count, 1, file);
if (feof(stdin)) break;
}
return 0;
}
Expected result:
----------------
Input should be mirrored to file descriptor 3; when called using the command line above, it should be mirrored to standard output.
Actual result:
--------------
I get the following warning:
PHP Warning: fopen(/dev/fd/3): failed to open stream: No such file or directory in test.php on line 2
An strace of the PHP process shows the incorrect behavior:
lstat("/dev/fd/3", {st_mode=S_IFLNK|0300, st_size=64, ...}) = 0
readlink("/dev/fd/3", "pipe:[637257]", 4096) = 13
lstat("/dev/fd/pipe:[637257]", 0x7fff76ae8140) = -1 ENOENT (No such file or directory)
lstat("/dev/fd", {st_mode=S_IFLNK|0777, st_size=13, ...}) = 0
readlink("/dev/fd", "/proc/self/fd"..., 4096) = 13
lstat("/proc/self/fd", {st_mode=S_IFDIR|0500, st_size=0, ...}) = 0
lstat("/proc/self", {st_mode=S_IFLNK|0777, st_size=64, ...}) = 0
readlink("/proc/self", "28234"..., 4096) = 5
lstat("/proc/28234", {st_mode=S_IFDIR|0555, st_size=0, ...}) = 0
lstat("/proc", {st_mode=S_IFDIR|0555, st_size=0, ...}) = 0
open("/proc/28234/fd/pipe:[637257]", O_WRONLY|O_CREAT|O_TRUNC, 0666) = -1 ENOENT (No such file or directory)
Patchesphp_fd_open.diff (last revision 2010-12-06 22:24 UTC by cataphract@php.net)Pull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Nov 13 13:00:01 2025 UTC |
I've attached a patch (against trunk) that allows duping+open arbitrary file descriptor. /dev/fd doesn't seem like a good option because it's not portable. Example: <?php $f = fopen("php://fd/1", "w"); fwrite($f, "test"); Can you see if this is enough? Thanks.The problem with "php://fd/3" instead of "/dev/fd/3" is that when I use anonymous fifos in bash like this: php -r 'var_dump($argv);' <(echo "hello") then the output is as follows: array(2) { [0]=> string(1) "-" [1]=> string(10) "/dev/fd/63" } which means that if you have an existing PHP program which accepts filenames, you have to add extra "if" to it, with special case just to handle /dev/fd/63 -> php://fd/3 conversion. I think that this adds extra complexity for the developer in a place where Linux tried hard to eliminate it.It's a bit of a crazy work around, but if you're stuck with 5.3.3 for now (because it's the supported version in RHEL6 still) and you really really really want to do this: $pid = getmypid(); $writeFd = popen("cat - > /proc/$pid/fd/3", 'w');