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
This commit is contained in:
Christian Ullrich
2026-05-03 17:35:10 +02:00
committed by Dag-Erling Smørgrav
parent 0095c14256
commit d90513ea85
+5 -1
View File
@@ -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);