/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
* Copyright (c) 2007-2009 Dag-Erling Coïdan Smørgrav
* All rights reserved.
*
/*
* Reliably open and lock a file.
*
- * DO NOT, UNDER PAIN OF DEATH, 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 as easy to get right as you think.
+ * 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.
*/
-int
-flopen(const char *path, int flags, ...)
+static int
+vflopenat(int dirfd, const char *path, int flags, va_list ap)
{
int fd, operation, serrno, trunc;
struct stat sb, fsb;
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;
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) {
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;
errno = serrno;
return (-1);
}
-#ifdef DONT_EVEN_THINK_ABOUT_IT
+ /*
+ * 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);
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);
+}