Bug #77366 [NEW]: invalid fclose behavior

From: Date: Fri, 28 Dec 2018 23:27:54 +0000
Subject: Bug #77366 [NEW]: invalid fclose behavior
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-218669@lists.php.net to get a copy of this message
From:             lagodichyuriy at gmail dot com
Operating system: Archlinux
PHP version:      7.2.13
Package:          Streams related
Bug Type:         Bug
Bug description:invalid fclose behavior

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)


-- 
Edit bug report at https://bugs.php.net/bug.php?id=77366&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=77366&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=77366&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=77366&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=77366&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=77366&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=77366&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=77366&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=77366&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=77366&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=77366&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=77366&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=77366&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=77366&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=77366&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=77366&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=77366&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=77366&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=77366&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=77366&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=77366&r=mysqlcfg



Thread (3 messages)

« previous php.bugs (#218669) next »