Skip to content

Commit 5649afd

Browse files
committed
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.
1 parent a27c52a commit 5649afd

File tree

2 files changed

+14
-23
lines changed

2 files changed

+14
-23
lines changed

lib/libutil/flopen.3

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
.\"
2626
.\" $FreeBSD$
2727
.\"
28-
.Dd May 10, 2007
28+
.Dd June 6, 2009
2929
.Dt FLOPEN 3
3030
.Os
3131
.Sh NAME
@@ -46,13 +46,12 @@ The
4646
function opens or creates a file and acquires an exclusive lock on it.
4747
It is essentially equivalent with calling
4848
.Fn open
49-
with the same parameters followed by an
50-
.Fn fcntl
51-
.Dv F_SETLK
52-
or
53-
.Dv F_SETLKW
54-
operation with lock type
55-
.Dv F_WRLCK ,
49+
with the same parameters followed by
50+
.Fn flock
51+
with an
52+
.Va operation
53+
argument of
54+
.Dv LOCK_EX ,
5655
except that
5756
.Fn flopen
5857
will attempt to detect and handle races that may occur between opening
@@ -87,18 +86,13 @@ returns a valid file descriptor.
8786
Otherwise, it returns -1, and sets
8887
.Va errno
8988
as described in
90-
.Xr fcntl 2
89+
.Xr flock 2
9190
and
9291
.Xr open 2 .
9392
.Sh SEE ALSO
9493
.Xr errno 2 ,
95-
.Xr fcntl 2 ,
94+
.Xr flock 2 ,
9695
.Xr open 2
97-
.Sh HISTORY
98-
The
99-
.Fn flopen
100-
function first appeared in
101-
.Fx 6.3 .
10296
.Sh AUTHORS
10397
.An -nosplit
10498
The

lib/libutil/flopen.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,11 @@
2828
#include <sys/cdefs.h>
2929
__FBSDID("$FreeBSD$");
3030

31+
#include <sys/file.h>
3132
#include <sys/stat.h>
3233

3334
#include <errno.h>
34-
#include <fcntl.h>
3535
#include <stdarg.h>
36-
#include <string.h>
3736
#include <unistd.h>
3837

3938
#include <libutil.h>
@@ -42,7 +41,6 @@ int
4241
flopen(const char *path, int flags, ...)
4342
{
4443
int fd, operation, serrno, trunc;
45-
struct flock lock;
4644
struct stat sb, fsb;
4745
mode_t mode;
4846

@@ -59,10 +57,9 @@ flopen(const char *path, int flags, ...)
5957
va_end(ap);
6058
}
6159

62-
memset(&lock, 0, sizeof lock);
63-
lock.l_type = ((flags & O_ACCMODE) == O_RDONLY) ? F_RDLCK : F_WRLCK;
64-
lock.l_whence = SEEK_SET;
65-
operation = (flags & O_NONBLOCK) ? F_SETLK : F_SETLKW;
60+
operation = LOCK_EX;
61+
if (flags & O_NONBLOCK)
62+
operation |= LOCK_NB;
6663

6764
trunc = (flags & O_TRUNC);
6865
flags &= ~O_TRUNC;
@@ -71,7 +68,7 @@ flopen(const char *path, int flags, ...)
7168
if ((fd = open(path, flags, mode)) == -1)
7269
/* non-existent or no access */
7370
return (-1);
74-
if (fcntl(fd, operation, &lock) == -1) {
71+
if (flock(fd, operation) == -1) {
7572
/* unsupported or interrupted */
7673
serrno = errno;
7774
(void)close(fd);

0 commit comments

Comments
 (0)