]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fd-util: add macro for generating /proc/self/fd/ paths on the fly
authorLennart Poettering <lennart@poettering.net>
Wed, 18 Aug 2021 07:43:07 +0000 (09:43 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 19 Aug 2021 07:19:11 +0000 (09:19 +0200)
src/basic/fd-util.h

index 61b6684cb3c156a8d9040bf6acce590d406d1c44..2382d52d40ce6fd8000477f5011f03ed43a223bf 100644 (file)
@@ -7,6 +7,7 @@
 #include <sys/socket.h>
 
 #include "macro.h"
+#include "stdio-util.h"
 
 /* maximum length of fdname */
 #define FDNAME_MAX 255
@@ -104,7 +105,20 @@ static inline int make_null_stdio(void) {
                 0;                              \
         })
 
-
 int fd_reopen(int fd, int flags);
 int read_nr_open(void);
 int btrfs_defrag_fd(int fd);
+
+/* The maximum length a buffer for a /proc/self/fd/<fd> path needs */
+#define PROC_FD_PATH_MAX \
+        (STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int))
+
+static inline char *format_proc_fd_path(char buf[static PROC_FD_PATH_MAX], int fd) {
+        assert(buf);
+        assert(fd >= 0);
+        assert_se(snprintf_ok(buf, PROC_FD_PATH_MAX, "/proc/self/fd/%i", fd));
+        return buf;
+}
+
+#define FORMAT_PROC_FD_PATH(fd) \
+        format_proc_fd_path((char[PROC_FD_PATH_MAX]) {}, (fd))