git: 651b82e64ef4 - stable/15 - lockf: Avoid spinning when operating on an fd

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Wed, 06 May 2026 05:38:02 UTC
The branch stable/15 has been updated by des:

URL: https://cgit.FreeBSD.org/src/commit/?id=651b82e64ef4f753d8f4c1793f359de39744526c

commit 651b82e64ef4f753d8f4c1793f359de39744526c
Author:     Christian Ullrich <chris@chrullrich.net>
AuthorDate: 2026-05-03 15:35:10 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2026-05-06 05:37:26 +0000

    lockf: Avoid spinning when operating on an fd
    
    When operating on a file descriptor, acquire_lock() would ignore the
    flags argument and always operate in non-blocking mode, resulting in
    unnecessary busy-looping.
    
    PR:             294832
    MFC after:      1 week
    Reviewed by:    markj
    Differential Revision:  https://reviews.freebsd.org/D56722
    
    (cherry picked from commit d90513ea85693da0ca5955173609f4e81e38ae16)
---
 usr.bin/lockf/lockf.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/usr.bin/lockf/lockf.c b/usr.bin/lockf/lockf.c
index 16bae36a21e0..076d28b7ddd5 100644
--- a/usr.bin/lockf/lockf.c
+++ b/usr.bin/lockf/lockf.c
@@ -320,10 +320,14 @@ acquire_lock(union lock_subject *subj, int flags, int silent)
 	int fd;
 
 	if (fdlock) {
+		int lflags = LOCK_EX;
+
 		assert(subj->subj_fd >= 0 && subj->subj_fd <= INT_MAX);
 		fd = (int)subj->subj_fd;
 
-		if (flock(fd, LOCK_EX | LOCK_NB) == -1) {
+		if ((flags & O_NONBLOCK) == O_NONBLOCK)
+			lflags |= LOCK_NB;
+		if (flock(fd, lflags) == -1) {
 			if (errno == EAGAIN || errno == EINTR)
 				return (-1);
 			err(EX_CANTCREAT, "cannot lock fd %d", fd);