]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fd-util: add new FORMAT_PROC_PID_FD_PATH() helper
authorLennart Poettering <lennart@poettering.net>
Thu, 2 Nov 2023 10:31:13 +0000 (11:31 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 2 Nov 2023 13:09:23 +0000 (14:09 +0100)
This is just like FORMAT_PROC_FD_PATH() but goes via the PID number
rather than the "self" symlink.

This is useful whenever we want to generate a path that is useful
outside of our local scope.

src/basic/fd-util.c
src/basic/fd-util.h

index f2b606ea9b485f6e906a6cf5cfc0ffcd2f0c6a9d..32289cfd09ea8233d1f0f98ed14ad271b8232540 100644 (file)
@@ -988,3 +988,11 @@ const char *accmode_to_string(int flags) {
                 return NULL;
         }
 }
+
+char *format_proc_pid_fd_path(char buf[static PROC_PID_FD_PATH_MAX], pid_t pid, int fd) {
+        assert(buf);
+        assert(fd >= 0);
+        assert(pid >= 0);
+        assert_se(snprintf_ok(buf, PROC_PID_FD_PATH_MAX, "/proc/" PID_FMT "/fd/%i", pid == 0 ? getpid_cached() : pid, fd));
+        return buf;
+}
index aba8f5f5af9dc7cfb9ffc769270e6ca9074ce260..5061e32196ac782cabea87e56c3eee9a480b0966 100644 (file)
@@ -131,6 +131,16 @@ static inline char *format_proc_fd_path(char buf[static PROC_FD_PATH_MAX], int f
 #define FORMAT_PROC_FD_PATH(fd) \
         format_proc_fd_path((char[PROC_FD_PATH_MAX]) {}, (fd))
 
+/* The maximum length a buffer for a /proc/<pid>/fd/<fd> path needs */
+#define PROC_PID_FD_PATH_MAX \
+        (STRLEN("/proc//fd/") + DECIMAL_STR_MAX(pid_t) + DECIMAL_STR_MAX(int))
+
+char *format_proc_pid_fd_path(char buf[static PROC_PID_FD_PATH_MAX], pid_t pid, int fd);
+
+/* Kinda the same as FORMAT_PROC_FD_PATH(), but goes by PID rather than "self" symlink */
+#define FORMAT_PROC_PID_FD_PATH(pid, fd)                                \
+        format_proc_pid_fd_path((char[PROC_PID_FD_PATH_MAX]) {}, (pid), (fd))
+
 const char *accmode_to_string(int flags);
 
 /* Like ASSERT_PTR, but for fds */