php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #70419 proc_close return code is unreliable
Submitted: 2015-09-03 12:02 UTC Modified: 2016-08-25 00:13 UTC
Votes:2
Avg. Score:4.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (50.0%)
From: uwe at ohse dot de Assigned:
Status: Open Package: Program Execution
PHP Version: 7.0.0RC1 OS: Unix
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: uwe at ohse dot de
New email:
PHP Version: OS:

 

 [2015-09-03 12:02 UTC] uwe at ohse dot de
Description:
------------
The documentation of proc_close states that the function "Returns the termination status of the process that was run. In case of an error then -1 is returned."
The documentation of pclose() goes even further and recommends to use pcntl_wexitstatus() in a (unix only) note.

What the functions return can be seen in ext/standard/proc_open.c, proc_open_rsrc_dtor(), with status being the status parameter of waitpid:

                if (WIFEXITED(wstatus))
                        wstatus = WEXITSTATUS(wstatus);
                FG(pclose_ret) = wstatus;

The return code this is either the exitstatus of the process, if it didn't die by a signal, or the status returned by waitpid, but the caller can't tell anymore, because exit(1) and a SIGHUP are both mapped to 1.
This reduces the value of the exit code to a trinary (-1, 0, all other).

I propose to fix this mess by:
1) remove the first two lines above in PHP 7 (do it fast, do it *now*), and
2) document that the return code of the two functions is unusable before that, and
3) document that the return code has to be treated the very same as the status parameter of waitpid (unless an error happened, of course), and
4) remove the note in pclose, and 
5) have someone with windows knowledge look into it, too.

That might be seen as breaking backwards compatibility, but code using the exit status of the two functions is broken anyway.

If the proposed change can or will not be done, then at least please document the current behaviour clearly, as any misunderstanding can cause a very inappropriate error handling.

This problem has been reported already (#53518) in 2010 for PHP 5.3. I report it again because PHP 7 seems to be a very good point to change the behaviour, and because the documentation should be changed.

Test script:
---------------
<?php
$p1 = proc_open ("exit 1", array(), $pipes);
$x1 = proc_close($p1);
print "$x1\n";

$p2 = proc_open ('kill -HUP $$', array(), $pipes);
$x2 = proc_close($p2);
print "$x2\n";
?>



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-08-25 00:13 UTC] cmb@php.net
Unfortunately, it's too late for PHP 7.0, so some discussion would
have to happen whether this can go into 7.2 or would have to be
postponed until 8.0. A PR *might* help to start this discussion.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 05:01:29 2024 UTC