summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDag-Erling Smørgrav <des@FreeBSD.org>2007-05-10 14:52:57 +0000
committerDag-Erling Smørgrav <des@FreeBSD.org>2007-05-10 14:52:57 +0000
commit3dea5934000e33ef21d9784a9575a89f9a7f557b (patch)
treef0b7fc85fde1f8d40a730e691489335d8a05a91e
parent9667055264d5c60d857fd6c525793c083c1aad06 (diff)
Notes
-rw-r--r--lib/libutil/flopen.322
-rw-r--r--lib/libutil/flopen.c11
2 files changed, 29 insertions, 4 deletions
diff --git a/lib/libutil/flopen.3 b/lib/libutil/flopen.3
index 53a2132aec4b..0e5fbadacf12 100644
--- a/lib/libutil/flopen.3
+++ b/lib/libutil/flopen.3
@@ -60,6 +60,17 @@ Thus, it is well suited for opening lock files, PID files, spool
files, mailboxes and other kinds of files which are used for
synchronization between processes.
.Pp
+If
+.Va flags
+includes
+.Dv O_NONBLOCK
+and the file is already locked,
+.Fn flopen
+will fail and set
+.Va errno
+to
+.Dv EWOULDBLOCK .
+.Pp
As with
.Fn flopen ,
the additional
@@ -68,7 +79,18 @@ argument is required if
.Va flags
includes
.Dv O_CREAT .
+.Sh RETURN VALUES
+If successful,
+.Fn flopen
+returns a valid file descriptor.
+Otherwise, it returns -1, and sets
+.Va errno
+as described in
+.Xr flock 2
+and
+.Xr open 2 .
.Sh SEE ALSO
+.Xr errno 2 ,
.Xr flock 2 ,
.Xr open 2
.Sh AUTHORS
diff --git a/lib/libutil/flopen.c b/lib/libutil/flopen.c
index 66d4e470f901..39cb81f1483e 100644
--- a/lib/libutil/flopen.c
+++ b/lib/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);