From owner-svn-src-head@FreeBSD.ORG Sat Jun 6 18:47:04 2009 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 56C641065689; Sat, 6 Jun 2009 18:47:04 +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 29CC68FC13; Sat, 6 Jun 2009 18:47:04 +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 n56Il4ok068852; Sat, 6 Jun 2009 18:47:04 GMT (envelope-from des@svn.freebsd.org) Received: (from des@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id n56Il4l9068850; Sat, 6 Jun 2009 18:47:04 GMT (envelope-from des@svn.freebsd.org) Message-Id: <200906061847.n56Il4l9068850@svn.freebsd.org> From: Dag-Erling Smorgrav Date: Sat, 6 Jun 2009 18:47:04 +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: r193591 - head/lib/libutil X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 06 Jun 2009 18:47:04 -0000 Author: des Date: Sat Jun 6 18:47:03 2009 New Revision: 193591 URL: http://svn.freebsd.org/changeset/base/193591 Log: Revert (once again, and hopefully for the last time) to flock(2) locks. The problem with fcntl(2) locks is that they are not inherited by child processes. This breaks pidfile(3), where the common idiom is to open and lock the PID file before daemonizing. Modified: head/lib/libutil/flopen.3 head/lib/libutil/flopen.c Modified: head/lib/libutil/flopen.3 ============================================================================== --- head/lib/libutil/flopen.3 Sat Jun 6 17:55:54 2009 (r193590) +++ head/lib/libutil/flopen.3 Sat Jun 6 18:47:03 2009 (r193591) @@ -25,7 +25,7 @@ .\" .\" $FreeBSD$ .\" -.Dd May 10, 2007 +.Dd June 6, 2009 .Dt FLOPEN 3 .Os .Sh NAME @@ -46,13 +46,12 @@ 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 an -.Fn fcntl -.Dv F_SETLK -or -.Dv F_SETLKW -operation with lock type -.Dv F_WRLCK , +with the same parameters followed by +.Fn flock +with an +.Va operation +argument of +.Dv LOCK_EX , except that .Fn flopen will attempt to detect and handle races that may occur between opening @@ -87,18 +86,13 @@ returns a valid file descriptor. Otherwise, it returns -1, and sets .Va errno as described in -.Xr fcntl 2 +.Xr flock 2 and .Xr open 2 . .Sh SEE ALSO .Xr errno 2 , -.Xr fcntl 2 , +.Xr flock 2 , .Xr open 2 -.Sh HISTORY -The -.Fn flopen -function first appeared in -.Fx 6.3 . .Sh AUTHORS .An -nosplit The Modified: head/lib/libutil/flopen.c ============================================================================== --- head/lib/libutil/flopen.c Sat Jun 6 17:55:54 2009 (r193590) +++ head/lib/libutil/flopen.c Sat Jun 6 18:47:03 2009 (r193591) @@ -28,12 +28,11 @@ #include __FBSDID("$FreeBSD$"); +#include #include #include -#include #include -#include #include #include @@ -42,7 +41,6 @@ int flopen(const char *path, int flags, ...) { int fd, operation, serrno, trunc; - struct flock lock; struct stat sb, fsb; mode_t mode; @@ -59,10 +57,9 @@ flopen(const char *path, int flags, ...) va_end(ap); } - memset(&lock, 0, sizeof lock); - lock.l_type = ((flags & O_ACCMODE) == O_RDONLY) ? F_RDLCK : F_WRLCK; - lock.l_whence = SEEK_SET; - operation = (flags & O_NONBLOCK) ? F_SETLK : F_SETLKW; + operation = LOCK_EX; + if (flags & O_NONBLOCK) + operation |= LOCK_NB; trunc = (flags & O_TRUNC); flags &= ~O_TRUNC; @@ -71,7 +68,7 @@ flopen(const char *path, int flags, ...) if ((fd = open(path, flags, mode)) == -1) /* non-existent or no access */ return (-1); - if (fcntl(fd, operation, &lock) == -1) { + if (flock(fd, operation) == -1) { /* unsupported or interrupted */ serrno = errno; (void)close(fd);