From: eric dot saintetienne at gmail dot com Operating system: Linux PHP version: 5.3.19 Package: Filesystem function related Bug Type: Bug Bug description:flock() doesn't trigger mandatory locks on linux
Description: ------------ Locking exclusively via flock a file opened for writing doesn't trigger a mandatory lock. The python script below could trigger the mandatory lock. Maybe it's because PHP flock() is built on the C function call flock(): "When a program attempts to lock a file with lockf or fcntl that has mandatory locking set, the kernel will prevent all other programs from accessing the file. Processes which use flock will not trigger a mandatory lock." source: http://www.hackinglinuxexposed.com/articles/20030623.html Python script: #!/usr/bin/python import os, fcntl fd = os.open('mandlock-file', os.O_RDWR, 0755) print 'fd=', fd fcntl.lockf(fd, fcntl.LOCK_EX) a = raw_input() # during that time, any attempt to open the file will hang os.write(fd, a+'\n') fcntl.lockf(fd, fcntl.LOCK_UN) # now any attempt to open the file will succeed os.close(fd) # eof Test script: --------------- <?php define('FILENAME', 'mandlock-file'); /* first create a file with a mandatory lock: * $ touch mandlock-file * $ chmod g+s,g-x mandlock-file * $ sudo mount -o remount,mand / # my file is inside the '/' mountpoint */ function openlockedfile($filename) { $fp = fopen($filename, 'w+'); while (($fp === false) || (flock($fp, LOCK_EX) !== true)) { sleep(1); echo "spin lock\n"; if ($fp === false) $fp = fopen($filename, 'w+'); } return $fp; } $fd = openlockedfile(FILENAME); // open + lock EX sleep(10); /* During this sleep time, accessing the file should hang until the * php script releases the exclusive lock: * $ cat mandlock-file # should hang until php script termination * Practically, cat doesn't hang (and works) proving the file isn't * exclusively locked by the PHP script. */ closefile($fd); // unlock + close /* eof */ ?> Expected result: ---------------- once the file is created and the mandatory lock setup for it: during the 10 sec timer, opening the file (with or without explicit locking) should hand until the php script terminates. Actual result: -------------- it's possible to opening the locked file during the 10s timer for reading and writing. -- Edit bug report at https://bugs.php.net/bug.php?id=63709&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=63709&r=trysnapshot54 Try a snapshot (PHP 5.3): https://bugs.php.net/fix.php?id=63709&r=trysnapshot53 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=63709&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=63709&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=63709&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=63709&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=63709&r=needscript Try newer version: https://bugs.php.net/fix.php?id=63709&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=63709&r=support Expected behavior: https://bugs.php.net/fix.php?id=63709&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=63709&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=63709&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=63709&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=63709&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=63709&r=dst IIS Stability: https://bugs.php.net/fix.php?id=63709&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=63709&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=63709&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=63709&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=63709&r=mysqlcfg