From 646ed68361906798da95a36805f44df7a03f8f17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Thu, 10 May 2007 14:52:57 +0000 Subject: DTRT when O_NONBLOCK is specified. MFC after: 3 weeks --- libutil/flopen.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'libutil/flopen.c') diff --git a/libutil/flopen.c b/libutil/flopen.c index 66d4e47..39cb81f 100644 --- a/libutil/flopen.c +++ b/libutil/flopen.c @@ -39,29 +39,32 @@ __FBSDID("$FreeBSD$"); int flopen(const char *path, int flags, ...) { + int fd, operation, serrno; struct stat sb, fsb; mode_t mode; - int fd, serrno; #ifdef O_EXLOCK flags &= ~O_EXLOCK; #endif + mode = 0; if (flags & O_CREAT) { va_list ap; va_start(ap, flags); mode = va_arg(ap, int); /* mode_t promoted to int */ va_end(ap); - } else { - mode = 0; } + operation = LOCK_EX; + if (flags & O_NONBLOCK) + operation |= LOCK_NB; + for (;;) { if ((fd = open(path, flags, mode)) == -1) /* non-existent or no access */ return (-1); - if (flock(fd, LOCK_EX) == -1) { + if (flock(fd, operation) == -1) { /* unsupported or interrupted */ serrno = errno; close(fd); -- cgit v1.2.3