Skip to contentSkip to conversation

flock: make flock(3) work on NFSv4

PATCH Applied Petr Uzel 2 participants 3 messages Sep 15, 2011 – Sep 27, 2011 Util-Linux package

lore
Share & tools
lore

Petr Uzel

author
#0 lore raw

PATCH 1/1 flock: make flock(3) work on NFSv4

To pleace an exclusive lock on a file, NFSv4 requires the file to be opened RW because of the emulation of flock() by fcntl():

http://www.spinics.net/lists/linux-nfs/msg18502.html

So instead of O_RDONLY, open the file in O_RDWR if access() indicates it is possible (unless shared lock is requested).

From: Michal Kubecek <[email protected]>

---
 sys-utils/flock.c |    7 +++++--
 1 files changed, 5 insertions(+), 2 deletions(-)
sys-utils/flock.c +5 -2
index 7d321de..a500319 100644
@@ -129,6 +129,7 @@ int main(int argc, char *argv[])
   int have_timeout = 0;
   int type = LOCK_EX;
   int block = 0;
+  int open_accmode;
   int fd = -1;
   int opt, ix;
   int do_close = 0;
@@ -211,9 +212,11 @@ int main(int argc, char *argv[])
     }
 
     filename = argv[optind];
-    fd = open(filename, O_RDONLY|O_NOCTTY|O_CREAT, 0666);
+    open_accmode = ((type == LOCK_SH || access(filename, R_OK|W_OK) < 0) ?
+                    O_RDONLY : O_RDWR);
+    fd = open(filename, open_accmode|O_NOCTTY|O_CREAT, 0666);
     /* Linux doesn't like O_CREAT on a directory, even though it should be a
-       no-op */
+       no-op; POSIX doesn't allow O_RDWR or O_WRONLY */
     if (fd < 0 && errno == EISDIR)
         fd = open(filename, O_RDONLY|O_NOCTTY);
 
Signature · 8 lines
1.7.3.4


Petr

--
Petr Uzel
IRC: ptr_uzl @ freenode
author
#1 lore raw
Quoted 3 lines · Petr Uzel

To pleace an exclusive lock on a file, NFSv4 requires the file to be opened RW because of the emulation of flock() by fcntl():

Argh, the subject be:

s/flock(3)/flock(1)

... sorry

Petr

--
Petr Uzel
IRC: ptr_uzl @ freenode

maintainer Applied
#2 lore raw
In reply to Petr Uzel
Quoted 2 lines · Petr Uzel
 sys-utils/flock.c |    7 +++++--
 1 files changed, 5 insertions(+), 2 deletions(-)

Applied, thanks.

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com