php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77168 chgrp returns true but doesn't immediately change the group
Submitted: 2018-11-17 14:19 UTC Modified: 2018-11-17 19:03 UTC
From: rkaiser at gmail dot com Assigned:
Status: Not a bug Package: POSIX related
PHP Version: 7.0.32 OS: CentOS 7
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: rkaiser at gmail dot com
New email:
PHP Version: OS:

 

 [2018-11-17 14:19 UTC] rkaiser at gmail dot com
Description:
------------
Note: I only have PHP 7.0.5 CLI to test, not 7.0.32

Even though chgrp($file, $user) returns true, if you immediately check the permissions via posix_getgrgid(filegroup($file)) nothing has changed.  

Re-running the script immediately shows the group has changed.  So maybe there is a time lag or file lock involved.

Seems that chown() behaves normally.



Test script:
---------------
- log in as root
- make sure you have an "apache" user, or any other user works too
- echo "stuff" > my.log
- run the following

<?php

$logpath = "./my.log";

$owner = posix_getpwuid(fileowner($logpath));
$group = posix_getgrgid(filegroup($logpath));

if ($owner['name'] != 'apache' || $group['name'] != 'apache')
{
		// Attempt to fix the perms
		if (!chown($logpath, "apache"))
				throw new exception("Cannot change owner of %s to apache", $logpath);
		if (!chgrp($logpath, "apache"))
				throw new exception("Cannot change group of %s to apache", $logpath);

		// Even though chgrp() returned true, the permissions might not have actually changed.  Or else posix_getgrgid(filegroup($logpath)) is incorrect.
		$owner = posix_getpwuid(fileowner($logpath));
                
		$group = posix_getgrgid(filegroup($logpath));
		if ($owner['name'] != 'apache' || $group['name'] != 'apache')
				throw new exception(sprintf("Wrong permissions on %s - user:group is %s:%s.  Try re-running.\n", $logpath, $owner['name'], $group['name']));
}

Expected result:
----------------
The call to posix_getgrgid(filegroup($logpath)) would reflect the group change that was just made.

Actual result:
--------------
It doesn't reflect the group change that was made.  Re-running the script works fine.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-11-17 14:25 UTC] spam2 at rhsoft dot net
posix_* are system calls and PHP is hardly repsonsilble what the underlying OS gives back - sounds more like some caching in glibc
 [2018-11-17 18:22 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2018-11-17 18:22 UTC] requinix@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

fileowner() and filegroup() are subject to PHP's stat cache. See clearstatcache().
 [2018-11-17 19:03 UTC] rkaiser at gmail dot com
Sorry about that, somehow I missed clearstatcache(). That did the trick.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 18:01:29 2024 UTC