From d90513ea85693da0ca5955173609f4e81e38ae16 Mon Sep 17 00:00:00 2001 From: Christian Ullrich Date: Sun, 3 May 2026 17:35:10 +0200 Subject: [PATCH] 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 --- 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 16bae36a21e..076d28b7ddd 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);