All of lore.kernel.org
 help / color / mirror / Atom feed
From: Petr Uzel <petr.uzel@suse.cz>
To: util-linux <util-linux@vger.kernel.org>
Cc: Michal Kubecek <mkubecek@suse.cz>
Subject: [PATCH] flock: make flock(3) work on NFSv4
Date: Thu, 15 Sep 2011 10:19:17 +0200	[thread overview]
Message-ID: <20110915081916.GA11152@foxbat.suse.cz> (raw)

[-- Attachment #1: Type: text/plain, Size: 1477 bytes --]

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 |    7 +++++--
 1 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/sys-utils/flock.c b/sys-utils/flock.c
index 7d321de..a500319 100644
--- a/sys-utils/flock.c
+++ b/sys-utils/flock.c
@@ -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);
 
-- 
1.7.3.4


Petr

--
Petr Uzel
IRC: ptr_uzl @ freenode

[-- Attachment #2: Type: application/pgp-signature, Size: 198 bytes --]

             reply	other threads:[~2011-09-15  8:19 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-15  8:19 Petr Uzel [this message]
2011-09-15  8:23 ` [PATCH] flock: make flock(3) work on NFSv4 Petr Uzel
2011-09-27 13:03 ` Karel Zak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20110915081916.GA11152@foxbat.suse.cz \
    --to=petr.uzel@suse.cz \
    --cc=mkubecek@suse.cz \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.