|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2018-12-28 23:35 UTC] lagodichyuriy at gmail dot com
[2019-09-06 12:22 UTC] nikic@php.net
-Status: Open
+Status: Not a bug
[2019-09-06 12:22 UTC] nikic@php.net
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Nov 06 15:00:02 2025 UTC |
Description: ------------ I was writing PHP daemon when faced with a very weird fclose bug. According to Linux daemons, they should not use STDIN/STDOUT/STDERR streams, so I must close them. The only one way I see is to use fclose(STDIN) + fclose(STDOUT) + fclose(STDERR) and reopen them later in the same order (when you close the standard input, output and error file descriptors, the first three new descriptors will become the NEW standard input, output and error file descriptors). In the same time, I got segfaults each time when tried to use "echo" inside my code. I installed strace and checked the calls (see "Actual result" section): in the end of class method all my fopens became closed automatically, so any usage of STDIN/STDOUT/STDERR triggers fatal error. This behavior of fclose should fixed or documentation have to be updated. Test script: --------------- <?php class sample { public function method() { fclose(STDIN); fclose(STDOUT); fclose(STDERR); $stdin = fopen('/dev/null', 'r'); $stdout = fopen('/dev/null', 'w'); $stderr = fopen('php://stdout', 'w'); } } $sample = new sample(); $sample->method(); echo 'segfault!'; Expected result: ---------------- close(0) = 0 close(1) = 0 close(2) = 0 lstat("/dev/null", {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 3), ...}) = 0 lstat("/dev", {st_mode=S_IFDIR|0755, st_size=4000, ...}) = 0 open("/dev/null", O_RDONLY) = 0 fstat(0, {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 3), ...}) = 0 lseek(0, 0, SEEK_CUR) = 0 open("/dev/null", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 1 fstat(1, {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 3), ...}) = 0 lseek(1, 0, SEEK_CUR) = 0 dup(1) = 2 fstat(2, {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 3), ...}) = 0 fstat(2, {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 3), ...}) = 0 lseek(2, 0, SEEK_CUR) = 0 write(1, "segfault!", 9) = 9 close(2) = 0 close(1) = 0 close(0) = 0 Actual result: -------------- close(0) = 0 close(1) = 0 close(2) = 0 lstat("/dev/null", {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 3), ...}) = 0 lstat("/dev", {st_mode=S_IFDIR|0755, st_size=4000, ...}) = 0 open("/dev/null", O_RDONLY) = 0 fstat(0, {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 3), ...}) = 0 lseek(0, 0, SEEK_CUR) = 0 open("/dev/null", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 1 fstat(1, {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 3), ...}) = 0 lseek(1, 0, SEEK_CUR) = 0 dup(1) = 2 fstat(2, {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 3), ...}) = 0 fstat(2, {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 3), ...}) = 0 lseek(2, 0, SEEK_CUR) = 0 close(0) = 0 close(1) = 0 close(2) = 0 write(1, "segfault!", 9) = -1 EBADF (Bad file descriptor)