summaryrefslogtreecommitdiffstats
path: root/lib/libutil/flopen.c
diff options
context:
space:
mode:
authordes <des@FreeBSD.org>2009-06-06 18:47:03 +0000
committerdes <des@FreeBSD.org>2009-06-06 18:47:03 +0000
commit94e760567d654ada216cd794352e9deca31dff08 (patch)
tree31cf1578dd1fe8b1672c288942548b816d815953 /lib/libutil/flopen.c
parent78eb93ba011f848dcde1cab4b81989885ad564bf (diff)
downloadFreeBSD-src-94e760567d654ada216cd794352e9deca31dff08.zip
FreeBSD-src-94e760567d654ada216cd794352e9deca31dff08.tar.gz
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.
Diffstat (limited to 'lib/libutil/flopen.c')
-rw-r--r--lib/libutil/flopen.c13
1 files changed, 5 insertions, 8 deletions
diff --git a/lib/libutil/flopen.c b/lib/libutil/flopen.c
index ae98daf..754c9c0 100644
--- a/lib/libutil/flopen.c
+++ b/lib/libutil/flopen.c
@@ -28,12 +28,11 @@
#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,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);
OpenPOWER on IntegriCloud