|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2003-11-06 20:26 UTC] mattias at sudac dot org
Description:
------------
On some operating systems (BSD?) it is possible to open a arbitrary file descriptor with fopen like this:
fopen("/dev/fd/<fd>", "r");
But on at least Linux this fails. So here is patch that makes it possible to open fd:s by useing fopen("php://<fd>", "r").
in ext/standard/php_fopen_wrapper.c:php_stream_url_wrap_php add two lines:
} else if (!strcasecmp(path, "stderr")) {
fd = STDERR_FILENO;
+} else
+ fd = atoi(path);
Maybe some checks are needed.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 22:00:02 2025 UTC |
Why does fopen("/dev/fd/x", "r") fail ?It seams to work if the file descriptor is a pipe, but it fails on network sockets. Example: testfd.php: #!/usr/local/bin/php -q <?php echo `ls -l /dev/fd/`; $stdin = fopen('/dev/fd/0', 'r'); $stdout = fopen('/dev/fd/1', 'w'); ?> Server: tcpserver 127.0.0.1 8000 ./testfd.php Client: mattias@icebox:~$ nc localhost 8000 total 0 lrwx------ 1 mattias mattias 64 2003-11-07 13:35 0 -> socket:[2611215] l-wx------ 1 mattias mattias 64 2003-11-07 13:35 1 -> pipe:[2611234] lrwx------ 1 mattias mattias 64 2003-11-07 13:35 2 -> /dev/pts/3 lr-x------ 1 mattias mattias 64 2003-11-07 13:35 3 -> /proc/12706/fd lrwx------ 1 mattias mattias 64 2003-11-07 13:35 4 -> socket:[2611215] lrwx------ 1 mattias mattias 64 2003-11-07 13:35 5 -> socket:[2611215] lrwx------ 1 mattias mattias 64 2003-11-07 13:35 6 -> /dev/pts/3 Warning: fopen(/dev/fd/0): failed to open stream: No such device or address in /home/mattias/projects/jssocket/tests/testfd on line 6 Warning: fopen(/dev/fd/1): failed to open stream: No such device or address in /home/mattias/projects/jssocket/tests/testfd on line 7 If we run the script without tcpserver we get this: mattias@icebox:~$ ./testfd total 0 lrwx------ 1 mattias mattias 64 2003-11-07 13:37 0 -> /dev/pts/3 l-wx------ 1 mattias mattias 64 2003-11-07 13:37 1 -> pipe:[2611379] lrwx------ 1 mattias mattias 64 2003-11-07 13:37 2 -> /dev/pts/3 lr-x------ 1 mattias mattias 64 2003-11-07 13:37 3 -> /proc/12709/fd lrwx------ 1 mattias mattias 64 2003-11-07 13:37 4 -> /dev/pts/3 lrwx------ 1 mattias mattias 64 2003-11-07 13:37 5 -> /dev/pts/3 lrwx------ 1 mattias mattias 64 2003-11-07 13:37 6 -> /dev/pts/3 Notice the difference, also notice that /dev/fd is a symlink to /proc/self/fd: mattias@icebox:~$ ls -l /dev/fd lrwxrwxrwx 1 root root 13 2001-12-21 18:29 /dev/fd -> /proc/self/fd This is on Debian unstable, Linux 2.4.21. I noticed this when i moved a script from FreeBSD 4.8 to Linux.