svn commit: r184093 - head/lib/libutil

Dag-Erling Smorgrav des at FreeBSD.org
Mon Oct 20 18:02:17 UTC 2008


Author: des
Date: Mon Oct 20 18:02:16 2008
New Revision: 184093
URL: http://svn.freebsd.org/changeset/base/184093

Log:
  Reimplement flopen(3) using fcntl(2) locks instead of flock(2) locks.

Modified:
  head/lib/libutil/flopen.3
  head/lib/libutil/flopen.c

Modified: head/lib/libutil/flopen.3
==============================================================================
--- head/lib/libutil/flopen.3	Mon Oct 20 18:00:11 2008	(r184092)
+++ head/lib/libutil/flopen.3	Mon Oct 20 18:02:16 2008	(r184093)
@@ -46,12 +46,13 @@ The
 function opens or creates a file and acquires an exclusive lock on it.
 It is essentially equivalent with calling
 .Fn open
-with the same parameters followed by
-.Fn flock
-with an
-.Va operation
-argument of
-.Dv LOCK_EX ,
+with the same parameters followed by an
+.Fn fcntl
+.Dv F_SETLK
+or
+.Dv F_SETLKW
+operation with lock type
+.Dv F_WRLCK ,
 except that
 .Fn flopen
 will attempt to detect and handle races that may occur between opening
@@ -86,12 +87,12 @@ returns a valid file descriptor.
 Otherwise, it returns -1, and sets
 .Va errno
 as described in
-.Xr flock 2
+.Xr fcntl 2
 and
 .Xr open 2 .
 .Sh SEE ALSO
 .Xr errno 2 ,
-.Xr flock 2 ,
+.Xr fcntl 2 ,
 .Xr open 2
 .Sh HISTORY
 The

Modified: head/lib/libutil/flopen.c
==============================================================================
--- head/lib/libutil/flopen.c	Mon Oct 20 18:00:11 2008	(r184092)
+++ head/lib/libutil/flopen.c	Mon Oct 20 18:02:16 2008	(r184093)
@@ -28,12 +28,12 @@
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 
-#include <sys/file.h>
 #include <sys/stat.h>
 
 #include <errno.h>
 #include <fcntl.h>
 #include <stdarg.h>
+#include <string.h>
 #include <unistd.h>
 
 #include <libutil.h>
@@ -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);


More information about the svn-src-all mailing list