]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
flock: make flock(1) work on NFSv4
authorPetr Uzel <petr.uzel@suse.cz>
Thu, 15 Sep 2011 08:19:17 +0000 (10:19 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 18 Oct 2011 12:22:27 +0000 (14:22 +0200)
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 <mkubecek@suse.cz>
Signed-off-by: Petr Uzel <petr.uzel@suse.cz>
sys-utils/flock.c

index 7d321de0d9a00abca5d3bac53ce18239ae1d0f4d..a5003193ee117c673ead71a6003756a18c7b7987 100644 (file)
@@ -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);