]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
include: add helper routines for opening and validating pidfds
authorChristian Goeschel Ndjomouo <cgoesc2@wgu.edu>
Tue, 25 Nov 2025 04:26:48 +0000 (23:26 -0500)
committerChristian Goeschel Ndjomouo <cgoesc2@wgu.edu>
Thu, 4 Dec 2025 18:45:03 +0000 (13:45 -0500)
With the new ul_get_valid_pidfd_or_err() routine util-linux
tools can now simply validate pidfd inode numbers before
opening a file descriptor for a given PID.

Signed-off-by: Christian Goeschel Ndjomouo <cgoesc2@wgu.edu>
include/pidfd-utils.h
lib/Makemodule.am
lib/meson.build
lib/pidfd-utils.c [new file with mode: 0644]

index 08627ca8d286e023c0c9dc48f0b6290d2fc45aa5..1b9f4dc4fddcc3075705710dc701a3d92fe75d20 100644 (file)
@@ -94,4 +94,6 @@ static inline int pidfd_getfd(int pidfd __attribute__((unused)),
 }
 #endif
 
+int ul_get_valid_pidfd_or_err(pid_t pid, ino_t pfd_ino);
+
 #endif /* UTIL_LINUX_PIDFD_UTILS */
index 1d598faa28414a5d0c49a58bc2fe376be6b15723..aeab09f76c3f7b6116612abaa6b93209760c0589 100644 (file)
@@ -34,6 +34,7 @@ libcommon_la_SOURCES = \
        lib/netaddrq.c \
        lib/netlink.c \
        lib/pidutils.c \
+       lib/pidfd-utils.c \
        lib/pwdutils.c \
        lib/randutils.c \
        lib/sha1.c \
index cb35ecbd60f4be3831ca38e6d9146d35349c24b4..db871913d299e8460336b32dd12b4f9e2a7e5b78 100644 (file)
@@ -20,6 +20,7 @@ lib_common_sources = '''
        netaddrq.c
        netlink.c
        pidutils.c
+       pidfd-utils.c
        procfs.c
        pwdutils.c
        randutils.c
diff --git a/lib/pidfd-utils.c b/lib/pidfd-utils.c
new file mode 100644 (file)
index 0000000..a0cc169
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * No copyright is claimed.  This code is in the public domain; do with
+ * it what you wish.
+ *
+ * Authors: Christian Goeschel Ndjomouo <cgoesc2@wgu.edu> [2025]
+ */
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <errno.h>
+#include <err.h>
+
+#include "c.h"
+#include "nls.h"
+#include "strutils.h"
+#include "pidfd-utils.h"
+
+/*
+ * ul_get_valid_pidfd_or_err() - Return a valid file descriptor for a PID
+ *                               or exit the process with an error message.
+ *
+ * @pid:     PID number for which to get a file descriptor
+ * @pfd_ino: A pidfd inode number that is expected to be the
+ *           same as for the new file descriptor.
+ *
+ * Pass @pfd_ino as NULL, if the pidfd should not be validated.
+ *
+ * Return: On success, a file descriptor is returned.
+ *         On failure, err() or errx() is called to
+ *         print an error message and kill the program.
+ *
+ */
+int ul_get_valid_pidfd_or_err(pid_t pid, ino_t pfd_ino)
+{
+        int pfd, rc;
+        struct stat f;
+
+        pfd = pidfd_open(pid, 0);
+        if (pfd < 0)
+                err(EXIT_FAILURE, _("pidfd_open() failed"));
+
+        if (pfd_ino) {
+                rc = fstat(pfd, &f);
+                if (rc < 0)
+                        err(EXIT_FAILURE, _("failed to fstat() pidfd"));
+
+                if (f.st_ino != pfd_ino) {
+                        close(pfd);
+                        errx(EXIT_FAILURE, _("pidfd inode %"PRIu64" not found for pid %d"),
+                                pfd_ino, pid);
+                }
+        }
+        return pfd;
+}
\ No newline at end of file