From 6e1e4b59f9ba89518382c7389da2d87911ec1d76 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 18 Aug 2021 09:43:07 +0200 Subject: [PATCH] fd-util: add macro for generating /proc/self/fd/ paths on the fly --- src/basic/fd-util.h | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/basic/fd-util.h b/src/basic/fd-util.h index 61b6684cb3c..2382d52d40c 100644 --- a/src/basic/fd-util.h +++ b/src/basic/fd-util.h @@ -7,6 +7,7 @@ #include #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/ 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))