php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41805 sem_get / sem_aquire cannot ensure exclusive access
Submitted: 2007-06-25 18:20 UTC Modified: 2007-06-25 18:49 UTC
From: ein at anti-logic dot com Assigned:
Status: Not a bug Package: Filesystem function related
PHP Version: 4.4.7 OS: linux 2.6
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: ein at anti-logic dot com
New email:
PHP Version: OS:

 

 [2007-06-25 18:20 UTC] ein at anti-logic dot com
Description:
------------
It is impossible to duplicate flock functionality with the sem controls due to sem_get's and sem_aquire's inability to ensure exclusive access.

For example with flock, if we create a non exclusive (shared) lock, followed by an exclusive lock, it will block on the second lock creation. This is the expected behaviour, and allows us to ensure that there is exclusive access to the file.

Reproduce code:
---------------
$fp = sem_get(fileinode('lock_file', 100, 666);
sem_acquire($fp);
echo "one lock is in place, we should Block on the second lock request";
$fp2 = sem_get(fileinode('lock_file', 1, 666);
sem_acquire($fp2);
echo "but we have not";

$fp3 = fopen('lock_file', 'w');
flock($fp3, LOCK_SH);
echo "one flock is in place, we should block on the next lock request";
$fp4 = fopen('lock_file', 'w');
flock($fp4, LOCK_EX);
echo "we will never see this text due to proper indefinite blocking";

Expected result:
----------------
sem_aquire should block on the second lock creation.

Actual result:
--------------
It will not block, nor will sem_get or sem_aquire return an error.

I believe the confusion may be caused by this line in the documentation, "Actually this value is set only if the process finds it is the only process currently attached to the semaphore."  However, that is reffering only to the perm field, and not max_aquire.  Either the documentation should be updated to reflect the (broken) implementation, or the sem_aquire/sem_get functions should be modified to ensure an exclusive lock request (max_aquire=1) will block when an existing lock is in place.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-06-25 18:37 UTC] tony2001@php.net
>$fp = sem_get(fileinode('lock_file', 100, 666); 
Why would it block if you set max_acquire to 100.
Change it to 1 and it will block.
 [2007-06-25 18:42 UTC] ein at anti-logic dot com
The second lock is attempting to aquire EXCLUSIVE ACCESS.  There is not way to ensure that another process is not using the file ahead of time.  max_aquire=1 is synonymous with LOCK_EX as far as I am concerned.
 [2007-06-25 18:45 UTC] tony2001@php.net
http://php.net/sem_get

--
max_acquire

The number of processes that can acquire the semaphore simultaneously is set to max_acquire (defaults to 1).
--


 [2007-06-25 18:49 UTC] ein at anti-logic dot com
I understand how max_aquire works, but as per my example above, max_aquire=100 is providing a similiar result as flock with LOCK_SH.  Therefore, the second sem_aquire should block with max_aquire=1 (on the second sem_get request), just as flock with LOCK_EX does.

Have you tried the example code?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Jul 05 16:01:31 2024 UTC