diff options
author | Dag-Erling Smørgrav <des@FreeBSD.org> | 2007-05-10 14:52:57 +0000 |
---|---|---|
committer | Dag-Erling Smørgrav <des@FreeBSD.org> | 2007-05-10 14:52:57 +0000 |
commit | 646ed68361906798da95a36805f44df7a03f8f17 (patch) | |
tree | ef9fc68624a56deb5353f0d16ca030faa99d30fa /libutil | |
parent | 82c0a122ffb4ff24e53e2a3ef557704add29659c (diff) | |
download | pw-darwin-646ed68361906798da95a36805f44df7a03f8f17.tar.gz pw-darwin-646ed68361906798da95a36805f44df7a03f8f17.tar.zst pw-darwin-646ed68361906798da95a36805f44df7a03f8f17.zip |
DTRT when O_NONBLOCK is specified.
MFC after: 3 weeks
Diffstat (limited to 'libutil')
-rw-r--r-- | libutil/flopen.c | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/libutil/flopen.c b/libutil/flopen.c index 66d4e47..39cb81f 100644 --- a/libutil/flopen.c +++ b/libutil/flopen.c @@ -39,29 +39,32 @@ __FBSDID("$FreeBSD$"); int flopen(const char *path, int flags, ...) { + int fd, operation, serrno; struct stat sb, fsb; mode_t mode; - int fd, serrno; #ifdef O_EXLOCK flags &= ~O_EXLOCK; #endif + mode = 0; if (flags & O_CREAT) { va_list ap; va_start(ap, flags); mode = va_arg(ap, int); /* mode_t promoted to int */ va_end(ap); - } else { - mode = 0; } + operation = LOCK_EX; + if (flags & O_NONBLOCK) + operation |= LOCK_NB; + for (;;) { if ((fd = open(path, flags, mode)) == -1) /* non-existent or no access */ return (-1); - if (flock(fd, LOCK_EX) == -1) { + if (flock(fd, operation) == -1) { /* unsupported or interrupted */ serrno = errno; close(fd); |