Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

My server uses NFS (Network File System), and I'm unable to use PHP's flock() -function. Is there a way to lock files on an NFS or is there a need to do so?

share|improve this question

4 Answers 4

flock() works just fine on Linux NFS, including from PHP. We use it extensively and have tested it thoroughly to verify it's working as desired. Check to see if you're running all of the necessary services on both the client and server. Look for "portmapper" and "rpc.statd". If they're not running, you need to figure out which init script starts them on your distro. On Debian-based distros it's "/etc/init.d/portmap" and "/etc/init.d/nfs-common".

From the client, run "rpcinfo -u $NFSSERVER status" and see if you get a response. On my setup, I get "program 100024 version 1 ready and waiting" as the result.

Oh, also bear in mind that in some circumstances NFS and statd can get upset if both the client and the server don't have reliable hostname entries for each other. Double check /etc/hosts on both machines.

share|improve this answer
    
I'm not really in a position to alter server specific details. The flock() -function is even disabled from the php.ini, because it would not work, at least that's what I've been told to. –  rFactor Sep 19 '09 at 8:13
up vote 1 down vote accepted

Just wanted to answer to myself. The solution can be found here: http://us3.php.net/manual/en/function.flock.php#82521

share|improve this answer
1  
The second option listed is exactly what I describe: using the built-in lock server in Linux NFS. The troubleshooting steps were designed to determine why it (apparently) wasn't working... –  Insyte Sep 20 '09 at 2:43

I don't know how the PHP flock() function is implemented, but assuming it's an interface to the flock() syscall, then it does not work at all over NFS. From the flock() manpage:

   flock(2) does not lock files over NFS.  Use fcntl(2) instead: that does work over NFS, given
   a sufficiently recent version of Linux and a server which supports locking.

And, of course, everything what a man page says, no matter how outdated, is the ultimate truth.

share|improve this answer
    
+1, sarcasm! Item D10 in the NFS FAQ elaborates. –  themel Mar 14 '13 at 12:29

This is meant to be a comment on janneb's answer above, but I don't have the !"#$% reputation:

The manual page was out of date for a long time, but has since been updated to say (emphasis mine):

In Linux kernels up to 2.6.11, flock() does not lock files over NFS (i.e., the scope of locks was limited to the local system). Instead, one could use fcntl(2) byte-range locking, which does work over NFS, given a sufficiently recent version of Linux and a server which supports locking. Since Linux 2.6.12, NFS clients support flock() locks by emulating them as byte-range locks on the entire file. This means that fcntl(2) and flock() locks do interact with one another over NFS. Since Linux 2.6.37, the kernel supports a compatibility mode that allows flock() locks (and also fcntl(2) byte region locks) to be treated as local; see the discussion of the local_lock option in nfs(5).

The official man-pages web site leads to http://man7.org/linux/man-pages/man2/flock.2.html which shows the new version from man-pages 4.00

Doc update happened in 2014: http://git.kernel.org/cgit/docs/man-pages/man-pages.git/commit/man2/flock.2?id=e449654fdb3f19aafc569df47d12bffdf6276236

Linux 2.6.12 was released on 18 June 2005 - almost a decade ago now.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.