From owner-svn-src-all@FreeBSD.ORG Mon Oct 20 18:02:17 2008 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 20A3C1065670; Mon, 20 Oct 2008 18:02:17 +0000 (UTC) (envelope-from des@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 0EC0A8FC19; Mon, 20 Oct 2008 18:02:17 +0000 (UTC) (envelope-from des@FreeBSD.org) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id m9KI2Gr0077365; Mon, 20 Oct 2008 18:02:16 GMT (envelope-from des@svn.freebsd.org) Received: (from des@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id m9KI2GrQ077363; Mon, 20 Oct 2008 18:02:16 GMT (envelope-from des@svn.freebsd.org) Message-Id: <200810201802.m9KI2GrQ077363@svn.freebsd.org> From: Dag-Erling Smorgrav Date: Mon, 20 Oct 2008 18:02:16 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r184093 - head/lib/libutil X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 20 Oct 2008 18:02:17 -0000 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 __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);