]> git.ipfire.org Git - thirdparty/libbsd.git/commitdiff
Add flopenat() function from FreeBSD
authorGuillem Jover <guillem@hadrons.org>
Sun, 20 May 2018 23:11:46 +0000 (01:11 +0200)
committerGuillem Jover <guillem@hadrons.org>
Mon, 21 May 2018 01:49:26 +0000 (03:49 +0200)
COPYING
include/bsd/libutil.h
man/flopen.3bsd
src/flopen.c
src/libbsd.map

diff --git a/COPYING b/COPYING
index 9ffc6b46bd67f43bbf3d1fd03edd0283e087c77b..021e67ee41cf40581ca0c013485ec0b26eb26766 100644 (file)
--- 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
index e5f148ace6b7c56a647ca39a9cd2340a374701d2..2c4c73164e27a82ba0b94a158cc5402dc6283ec1 100644 (file)
@@ -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);
index ab58b2872496a9fb006124098ea5826361420a2c..b3cd8c9ef5e7dac61566272bc76a22749be79ec7 100644 (file)
 .\"
 .\" $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 .
index aa506f5c5bda8bc306d289815de38319a70313fd..b9972c94ec90e227fff65b0bc416f873956a5b0d 100644 (file)
@@ -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
 #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);
+}
index 304c5933eb5c12b6a7a0c68cb22afaba78cf98ac..47d7df5d86e5b83992a991f2f4c3649380751fba 100644 (file)
@@ -139,3 +139,7 @@ LIBBSD_0.7 {
 LIBBSD_0.8 {
     explicit_bzero;
 } LIBBSD_0.7;
+
+LIBBSD_0.9 {
+    flopenat;
+} LIBBSD_0.8;