PHP flock() - what's under the hood?

Go To StackoverFlow.com

6

After wrestling with PHP source for a half an hour, I gave up. :P The question is - what system call does the PHP flock() function call boil down to on a Gentoo Linux system? I'm having some issues with it (like block-for-30-seconds-in-every-one-of-20-loop-iterations kind of issues) and I would like to know why that is so.

2009-06-16 14:08
by Vilx-


3

// example: $stream = fopen(FILE, 'rb') or die('???');
$md = stream_get_meta_data($stream);
echo $md['wrapper_type'];
flock($stream);
if this prints plainfile then the call to the php function flock() is handled by php_stdiop_set_option(...) which calls flock(). Depending on whether PHP was compiled with HAVE_FLOCK or not this may be the system call flock() or a function defined in flock_compat.c which utilizes fcntl(). On my gentoo system PHP was compiled with HAVE_FLOCK.

main/streams/plain_wrapper.c @ static int php_stdiop_set_option(...):

       case PHP_STREAM_OPTION_LOCKING:
            if (fd == -1) {
                return -1;
            }

            if ((zend_uintptr_t) ptrparam == PHP_STREAM_LOCK_SUPPORTED) {
                return 0;
            }

            if (!flock(fd, value)) {
                data->lock_flag = value;
                return 0;
            } else {
                return -1;
            }
            break;
2009-06-16 14:49
by VolkerK


2

http://www.opengroup.org/onlinepubs/009695399/functions/fcntl.html

/etc/standard/flock_compat.c [line 66]    
ret = fcntl(fd, operation & LOCK_NB ? F_SETLK : F_SETLKW, &flck);
2009-06-16 15:00
by racerror
If this function is called or not depends on the compile time define HAVE_FLOC - VolkerK 2009-06-16 16:44


0

Unless I'm misunderstanding what you're asking, PHP's flock() is a call to the Unix system function flock() on Gentoo. They have identical semantics.

2009-06-16 14:12
by John Feminella
Semantics - yes. But does it really call flock() - Vilx- 2009-06-16 14:16
I can't prove it because I'm not running a copy of PHP right now, but I believe so, yes. Why not try it out yourself with strace - John Feminella 2009-06-16 14:25
I have only FTP access to the server - Vilx- 2009-06-16 14:28
Why not just check the PHP source code - Powerlord 2009-06-16 15:17
@Powerlord, You need to check the compiler settings/flags too - Pacerier 2017-11-23 03:41


0

Are you using it on a networked or mounted drive? I wouldn't be surprised if what you are experiencing is deadlock, and some of the comments in the documentation talk about that.

The documentation for flock.

2009-06-16 14:12
by cgp
Read carefully - Gentoo Linux here. But the full setup is a bit complicated so I just want to know what it calls underneath, so that I can put the full picture together myself - Vilx- 2009-06-16 14:15
And no, it's not a deadlock, that I'm sure of. But there are other things I'm suspecting - Vilx- 2009-06-16 14:17
Ahh, I just found out that the file is on an NFS mount. And the host box is also trying to flock() it (load balanced webservers, you see) - Vilx- 2009-06-16 14:33
Ads