Req #70932 [Com]: Add ability to prevent handles inheritance in popen
| From: | speller at yandex dot ru | Date: | Wed, 18 Nov 2015 05:14:51 +0000 |
| Subject: | Req #70932 [Com]: Add ability to prevent handles inheritance in popen | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-197310@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=70932&edit=1
ID: 70932
Comment by: speller at yandex dot ru
Reported by: speller at yandex dot ru
Summary: Add ability to prevent handles inheritance in popen
Status: Open
Type: Feature/Change Request
Package: Filesystem function related
Operating System: windows
PHP Version: 5.6.15
Block user comment: N
Private report: N
New Comment:
I tried the proc_open() function but it has the same issue - file handles are inherited from the
parent process. So all files opened in the parent process at the time of the child process starting
remain opened all the time while child process is running. This is an important issue if child
process should work much longer than parent process.
Previous Comments:
------------------------------------------------------------------------
[2015-11-18 04:22:40] requinix@php.net
popen is supposed to behave like a fork. The Windows behavior mirrors the Unix behavior.
http://linux.die.net/man/3/popen
Maybe you should be using proc_open?
------------------------------------------------------------------------
[2015-11-18 03:14:47] speller at yandex dot ru
Description:
------------
Currently, any child process started from PHP using popen(), proc_open() or exec() etc. will inherit
opened file handles. This is caused by TRUE value of the bInheritHandles parameter of the
CreateProcess WinAPI function.
This lead to the following issue. Example pseudocode:
strater.php
<?
$f = fopen('file.txt');
popen('php worker.php');
fclose('file.txt');
unlink('file.txt');
?>
worker.php
<?
sleep(600);
?>
If you will try to run these scripts you will get the exception on unlink('file.txt') line
saying you don't have such permissions to remove the file. Because the opened file handle was
inherited by the child process 'php worker.php' and even if you fclose() the
starter's file handle the file can not be deleted while there is any other handle referenced to
it in any other process in the system.
So in this case the worker process do nothing in its code to lock the file, but system internals do
it and the script can not prevent it.
So I suggest to add an ability to set the bInheritHandles flag to FALSE when creating child
processes to prevent the issue when child process keeps file handles implicitly.
Expected result:
----------------
The file.txt shold be deleted by the unlink() function.
Actual result:
--------------
unlink() function fail to delete file.txt with message of lack permissions.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=70932&edit=1