git: 6128f0c28db5 - stable/14 - lockf: hide unavailable error with -n -s

From: Kyle Evans <kevans_at_FreeBSD.org>
Date: Fri, 15 Dec 2023 16:50:58 UTC
The branch stable/14 has been updated by kevans:

URL: https://cgit.FreeBSD.org/src/commit/?id=6128f0c28db5c77374e1898e16f44131ebebc861

commit 6128f0c28db5c77374e1898e16f44131ebebc861
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2023-11-22 04:41:36 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2023-12-15 00:58:15 +0000

    lockf: hide unavailable error with -n -s
    
    The error message is expected, allow -s to suppress just that one since
    it would loosely fall under the definition of "failure to acquire the
    lock" described in the manpage for the -s option.
    
    Reviewed by:    0mp, allanjude
    Feedback from:  des
    Sponsored by:   Klara, Inc.
    
    (cherry picked from commit 3041e6950d07f0d11c9f91fefbf3c273cbbe4407)
---
 usr.bin/lockf/lockf.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/usr.bin/lockf/lockf.c b/usr.bin/lockf/lockf.c
index 4fbabea8c08f..ff1e7a6d6216 100644
--- a/usr.bin/lockf/lockf.c
+++ b/usr.bin/lockf/lockf.c
@@ -38,7 +38,7 @@
 #include <sysexits.h>
 #include <unistd.h>
 
-static int acquire_lock(const char *name, int flags);
+static int acquire_lock(const char *name, int flags, int silent);
 static void cleanup(void);
 static void killed(int sig);
 static void timeout(int sig);
@@ -125,13 +125,14 @@ main(int argc, char **argv)
 	 * avoiding the separate step of waiting for the lock.  This
 	 * yields fairness and improved performance.
 	 */
-	lockfd = acquire_lock(lockname, flags | O_NONBLOCK);
+	lockfd = acquire_lock(lockname, flags | O_NONBLOCK, silent);
 	while (lockfd == -1 && !timed_out && waitsec != 0) {
 		if (keep)
-			lockfd = acquire_lock(lockname, flags);
+			lockfd = acquire_lock(lockname, flags, silent);
 		else {
 			wait_for_lock(lockname);
-			lockfd = acquire_lock(lockname, flags | O_NONBLOCK);
+			lockfd = acquire_lock(lockname, flags | O_NONBLOCK,
+			    silent);
 		}
 	}
 	if (waitsec > 0)
@@ -168,15 +169,18 @@ main(int argc, char **argv)
  * on success, or -1 on failure.
  */
 static int
-acquire_lock(const char *name, int flags)
+acquire_lock(const char *name, int flags, int silent)
 {
 	int fd;
 
 	if ((fd = open(name, O_EXLOCK|flags, 0666)) == -1) {
 		if (errno == EAGAIN || errno == EINTR)
 			return (-1);
-		else if (errno == ENOENT && (flags & O_CREAT) == 0)
-			err(EX_UNAVAILABLE, "%s", name);
+		else if (errno == ENOENT && (flags & O_CREAT) == 0) {
+			if (!silent)
+				warn("%s", name);
+			exit(EX_UNAVAILABLE);
+		}
 		err(EX_CANTCREAT, "cannot open %s", name);
 	}
 	return (fd);