From 86961ef741cc24178ecb54f42e4ba78eaff708d6 Mon Sep 17 00:00:00 2001 From: Dag-Erling Smørgrav Date: Mon, 20 Oct 2008 18:02:16 +0000 Subject: Reimplement flopen(3) using fcntl(2) locks instead of flock(2) locks. --- libutil/flopen.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'libutil') diff --git a/libutil/flopen.c b/libutil/flopen.c index 23742f7..a5bf8c6 100644 --- a/libutil/flopen.c +++ b/libutil/flopen.c @@ -28,12 +28,12 @@ #include __FBSDID("$FreeBSD$"); -#include #include #include #include #include +#include #include #include @@ -42,6 +42,7 @@ int flopen(const char *path, int flags, ...) { int fd, operation, serrno, trunc; + struct flock flock; struct stat sb, fsb; mode_t mode; @@ -58,9 +59,10 @@ flopen(const char *path, int flags, ...) va_end(ap); } - operation = LOCK_EX; - if (flags & O_NONBLOCK) - operation |= LOCK_NB; + memset(&flock, 0, sizeof flock); + flock.l_type = F_WRLCK; + flock.l_whence = SEEK_SET; + operation = (flags & O_NONBLOCK) ? F_SETLK : F_SETLKW; trunc = (flags & O_TRUNC); flags &= ~O_TRUNC; @@ -69,7 +71,7 @@ flopen(const char *path, int flags, ...) if ((fd = open(path, flags, mode)) == -1) /* non-existent or no access */ return (-1); - if (flock(fd, operation) == -1) { + if (fcntl(fd, operation, &flock) == -1) { /* unsupported or interrupted */ serrno = errno; close(fd); -- cgit v1.2.3-56-ge451