php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77366 invalid fclose behavior
Submitted: 2018-12-28 23:27 UTC Modified: -
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: lagodichyuriy at gmail dot com Assigned:
Status: Open Package: Streams related
PHP Version: 7.2.13 OS: Archlinux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: lagodichyuriy at gmail dot com
New email:
PHP Version: OS:

 

 [2018-12-28 23:27 UTC] lagodichyuriy at gmail dot com
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)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-12-28 23:35 UTC] lagodichyuriy at gmail dot com
Extra info: it's possible to avoid this behavior by using the "global $stdin, $stdout, $stderr;" right after the "fclose" section, however it looks like a dirty hotfix to avoid unexpected "fclose" behavior which closes main streams and interrupts the execution.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sat Mar 23 12:01:27 2019 UTC