Add flopenat() function from FreeBSD
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
diff --git a/COPYING b/COPYING
index 9ffc6b4..021e67e 100644
--- a/COPYING
+++ b/COPYING
@@ -367,7 +367,7 @@ License: BSD-2-clause
Files:
src/flopen.c
Copyright:
- Copyright © 2007 Dag-Erling Coïdan Smørgrav
+ Copyright © 2007-2009 Dag-Erling Coïdan Smørgrav
All rights reserved.
License: BSD-2-clause-verbatim
Redistribution and use in source and binary forms, with or without
diff --git a/include/bsd/libutil.h b/include/bsd/libutil.h
index e5f148a..2c4c731 100644
--- a/include/bsd/libutil.h
+++ b/include/bsd/libutil.h
@@ -62,6 +62,7 @@ int humanize_number(char *buf, size_t len, int64_t bytes,
int expand_number(const char *_buf, uint64_t *_num);
int flopen(const char *_path, int _flags, ...);
+int flopenat(int dirfd, const char *path, int flags, ...);
struct pidfh *pidfile_open(const char *path, mode_t mode, pid_t *pidptr);
int pidfile_write(struct pidfh *pfh);
diff --git a/man/flopen.3bsd b/man/flopen.3bsd
index ab58b28..b3cd8c9 100644
--- a/man/flopen.3bsd
+++ b/man/flopen.3bsd
@@ -25,12 +25,13 @@
.\"
.\" $FreeBSD$
.\"
-.Dd June 6, 2009
+.Dd July 28, 2017
.Dt FLOPEN 3bsd
.Os
.Sh NAME
-.Nm flopen
-.Nd reliably open and lock a file
+.Nm flopen ,
+.Nm flopenat
+.Nd "Reliably open and lock a file"
.Sh LIBRARY
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
.Lb libbsd
@@ -44,6 +45,10 @@ for include usage.)
.Fn flopen "const char *path" "int flags"
.Ft int
.Fn flopen "const char *path" "int flags" "mode_t mode"
+.Ft int
+.Fn flopenat "int fd" "const char *path" "int flags"
+.Ft int
+.Fn flopenat "int fd" "const char *path" "int flags" "mode_t mode"
.Sh DESCRIPTION
The
.Fn flopen
@@ -53,7 +58,7 @@ It is essentially equivalent with calling
with the same parameters followed by
.Fn flock
with an
-.Va operation
+.Fa operation
argument of
.Dv LOCK_EX ,
except that
@@ -65,7 +70,7 @@ files, mailboxes and other kinds of files which are used for
synchronization between processes.
.Pp
If
-.Va flags
+.Fa flags
includes
.Dv O_NONBLOCK
and the file is already locked,
@@ -78,11 +83,32 @@ to
As with
.Fn open ,
the additional
-.Va mode
+.Fa mode
argument is required if
-.Va flags
+.Fa flags
includes
.Dv O_CREAT .
+.Pp
+The
+.Fn flopenat
+function is equivalent to the
+.Fn flopen
+function except in the case where the
+.Fa path
+specifies a relative path.
+In this case the file to be opened is determined relative to the directory
+associated with the file descriptor
+.Fa fd
+instead of the current working directory.
+If
+.Fn flopenat
+is passed the special value
+.Dv AT_FDCWD
+in the
+.Fa fd
+parameter, the current working directory is used
+and the behavior is identical to a call to
+.Fn flopen .
.Sh RETURN VALUES
If successful,
.Fn flopen
@@ -102,4 +128,4 @@ and
The
.Nm
function and this manual page were written by
-.An Dag-Erling Sm\(/orgrav Aq des@FreeBSD.org .
+.An Dag-Erling Sm\(/orgrav Aq Mt des@FreeBSD.org .
diff --git a/src/flopen.c b/src/flopen.c
index aa506f5..b9972c9 100644
--- a/src/flopen.c
+++ b/src/flopen.c
@@ -1,5 +1,7 @@
/*-
- * Copyright (c) 2007 Dag-Erling Coïdan Smørgrav
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * Copyright (c) 2007-2009 Dag-Erling Coïdan Smørgrav
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -30,13 +32,21 @@
#include <sys/stat.h>
#include <errno.h>
-#include <fcntl.h>
#include <stdarg.h>
#include <unistd.h>
+
#include <libutil.h>
-int
-flopen(const char *path, int flags, ...)
+/*
+ * Reliably open and lock a file.
+ *
+ * Please do not modify this code without first reading the revision history
+ * and discussing your changes with <des@freebsd.org>. Don't be fooled by the
+ * code's apparent simplicity; there would be no need for this function if it
+ * was easy to get right.
+ */
+static int
+vflopenat(int dirfd, const char *path, int flags, va_list ap)
{
int fd, operation, serrno, trunc;
struct stat sb, fsb;
@@ -48,11 +58,7 @@ flopen(const char *path, int flags, ...)
mode = 0;
if (flags & O_CREAT) {
- va_list ap;
-
- va_start(ap, flags);
mode = (mode_t)va_arg(ap, int); /* mode_t promoted to int */
- va_end(ap);
}
operation = LOCK_EX;
@@ -63,7 +69,7 @@ flopen(const char *path, int flags, ...)
flags &= ~O_TRUNC;
for (;;) {
- if ((fd = open(path, flags, mode)) == -1)
+ if ((fd = openat(dirfd, path, flags, mode)) == -1)
/* non-existent or no access */
return (-1);
if (flock(fd, operation) == -1) {
@@ -73,7 +79,7 @@ flopen(const char *path, int flags, ...)
errno = serrno;
return (-1);
}
- if (stat(path, &sb) == -1) {
+ if (fstatat(dirfd, path, &sb, 0) == -1) {
/* disappeared from under our feet */
(void)close(fd);
continue;
@@ -98,6 +104,42 @@ flopen(const char *path, int flags, ...)
errno = serrno;
return (-1);
}
+ /*
+ * The following change is provided as a specific example to
+ * avoid.
+ */
+#if 0
+ if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0) {
+ serrno = errno;
+ (void)close(fd);
+ errno = serrno;
+ return (-1);
+ }
+#endif
return (fd);
}
}
+
+int
+flopen(const char *path, int flags, ...)
+{
+ va_list ap;
+ int ret;
+
+ va_start(ap, flags);
+ ret = vflopenat(AT_FDCWD, path, flags, ap);
+ va_end(ap);
+ return (ret);
+}
+
+int
+flopenat(int dirfd, const char *path, int flags, ...)
+{
+ va_list ap;
+ int ret;
+
+ va_start(ap, flags);
+ ret = vflopenat(dirfd, path, flags, ap);
+ va_end(ap);
+ return (ret);
+}
diff --git a/src/libbsd.map b/src/libbsd.map
index 304c593..47d7df5 100644
--- a/src/libbsd.map
+++ b/src/libbsd.map
@@ -139,3 +139,7 @@ LIBBSD_0.7 {
LIBBSD_0.8 {
explicit_bzero;
} LIBBSD_0.7;
+
+LIBBSD_0.9 {
+ flopenat;
+} LIBBSD_0.8;