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.
This commit is contained in:
parent
a27c52a943
commit
5649afd028
@ -25,7 +25,7 @@
|
|||||||
.\"
|
.\"
|
||||||
.\" $FreeBSD$
|
.\" $FreeBSD$
|
||||||
.\"
|
.\"
|
||||||
.Dd May 10, 2007
|
.Dd June 6, 2009
|
||||||
.Dt FLOPEN 3
|
.Dt FLOPEN 3
|
||||||
.Os
|
.Os
|
||||||
.Sh NAME
|
.Sh NAME
|
||||||
@ -46,13 +46,12 @@ The
|
|||||||
function opens or creates a file and acquires an exclusive lock on it.
|
function opens or creates a file and acquires an exclusive lock on it.
|
||||||
It is essentially equivalent with calling
|
It is essentially equivalent with calling
|
||||||
.Fn open
|
.Fn open
|
||||||
with the same parameters followed by an
|
with the same parameters followed by
|
||||||
.Fn fcntl
|
.Fn flock
|
||||||
.Dv F_SETLK
|
with an
|
||||||
or
|
.Va operation
|
||||||
.Dv F_SETLKW
|
argument of
|
||||||
operation with lock type
|
.Dv LOCK_EX ,
|
||||||
.Dv F_WRLCK ,
|
|
||||||
except that
|
except that
|
||||||
.Fn flopen
|
.Fn flopen
|
||||||
will attempt to detect and handle races that may occur between opening
|
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
|
Otherwise, it returns -1, and sets
|
||||||
.Va errno
|
.Va errno
|
||||||
as described in
|
as described in
|
||||||
.Xr fcntl 2
|
.Xr flock 2
|
||||||
and
|
and
|
||||||
.Xr open 2 .
|
.Xr open 2 .
|
||||||
.Sh SEE ALSO
|
.Sh SEE ALSO
|
||||||
.Xr errno 2 ,
|
.Xr errno 2 ,
|
||||||
.Xr fcntl 2 ,
|
.Xr flock 2 ,
|
||||||
.Xr open 2
|
.Xr open 2
|
||||||
.Sh HISTORY
|
|
||||||
The
|
|
||||||
.Fn flopen
|
|
||||||
function first appeared in
|
|
||||||
.Fx 6.3 .
|
|
||||||
.Sh AUTHORS
|
.Sh AUTHORS
|
||||||
.An -nosplit
|
.An -nosplit
|
||||||
The
|
The
|
||||||
|
@ -28,12 +28,11 @@
|
|||||||
#include <sys/cdefs.h>
|
#include <sys/cdefs.h>
|
||||||
__FBSDID("$FreeBSD$");
|
__FBSDID("$FreeBSD$");
|
||||||
|
|
||||||
|
#include <sys/file.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <string.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include <libutil.h>
|
#include <libutil.h>
|
||||||
@ -42,7 +41,6 @@ int
|
|||||||
flopen(const char *path, int flags, ...)
|
flopen(const char *path, int flags, ...)
|
||||||
{
|
{
|
||||||
int fd, operation, serrno, trunc;
|
int fd, operation, serrno, trunc;
|
||||||
struct flock lock;
|
|
||||||
struct stat sb, fsb;
|
struct stat sb, fsb;
|
||||||
mode_t mode;
|
mode_t mode;
|
||||||
|
|
||||||
@ -59,10 +57,9 @@ flopen(const char *path, int flags, ...)
|
|||||||
va_end(ap);
|
va_end(ap);
|
||||||
}
|
}
|
||||||
|
|
||||||
memset(&lock, 0, sizeof lock);
|
operation = LOCK_EX;
|
||||||
lock.l_type = ((flags & O_ACCMODE) == O_RDONLY) ? F_RDLCK : F_WRLCK;
|
if (flags & O_NONBLOCK)
|
||||||
lock.l_whence = SEEK_SET;
|
operation |= LOCK_NB;
|
||||||
operation = (flags & O_NONBLOCK) ? F_SETLK : F_SETLKW;
|
|
||||||
|
|
||||||
trunc = (flags & O_TRUNC);
|
trunc = (flags & O_TRUNC);
|
||||||
flags &= ~O_TRUNC;
|
flags &= ~O_TRUNC;
|
||||||
@ -71,7 +68,7 @@ flopen(const char *path, int flags, ...)
|
|||||||
if ((fd = open(path, flags, mode)) == -1)
|
if ((fd = open(path, flags, mode)) == -1)
|
||||||
/* non-existent or no access */
|
/* non-existent or no access */
|
||||||
return (-1);
|
return (-1);
|
||||||
if (fcntl(fd, operation, &lock) == -1) {
|
if (flock(fd, operation) == -1) {
|
||||||
/* unsupported or interrupted */
|
/* unsupported or interrupted */
|
||||||
serrno = errno;
|
serrno = errno;
|
||||||
(void)close(fd);
|
(void)close(fd);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user