Index: head/usr.bin/lockf/lockf.1 =================================================================== --- head/usr.bin/lockf/lockf.1 +++ head/usr.bin/lockf/lockf.1 @@ -1,4 +1,4 @@ -.\" + .\" .\" Copyright (C) 1998 John D. Polstra. All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without @@ -24,7 +24,7 @@ .\" .\" $FreeBSD$ .\" -.Dd June 18, 2020 +.Dd August 26, 2020 .Dt LOCKF 1 .Os .Sh NAME @@ -32,7 +32,7 @@ .Nd execute a command while holding a file lock .Sh SYNOPSIS .Nm -.Op Fl kns +.Op Fl knsw .Op Fl t Ar seconds .Ar file .Ar command @@ -121,6 +121,14 @@ is .Em not executed. +.It Fl w +Causes +.Nm +to open +.Ar file +for writing rather than reading. +This is necessary on filesystems (including NFSv4) where a file which +has been opened read-only cannot be exclusively locked. .El .Pp In no event will Index: head/usr.bin/lockf/lockf.c =================================================================== --- head/usr.bin/lockf/lockf.c +++ head/usr.bin/lockf/lockf.c @@ -62,9 +62,9 @@ pid_t child; silent = keep = 0; - flags = O_CREAT; + flags = O_CREAT | O_RDONLY; waitsec = -1; /* Infinite. */ - while ((ch = getopt(argc, argv, "sknt:")) != -1) { + while ((ch = getopt(argc, argv, "sknt:w")) != -1) { switch (ch) { case 'k': keep = 1; @@ -84,6 +84,9 @@ "invalid timeout \"%s\"", optarg); } break; + case 'w': + flags = (flags & ~O_RDONLY) | O_WRONLY; + break; default: usage(); } @@ -171,7 +174,7 @@ { int fd; - if ((fd = open(name, O_RDONLY|O_EXLOCK|flags, 0666)) == -1) { + if ((fd = open(name, O_EXLOCK|flags, 0666)) == -1) { if (errno == EAGAIN || errno == EINTR) return (-1); else if (errno == ENOENT && (flags & O_CREAT) == 0)