]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib: (pidfd-utils.c) add a helper routine to check the pidfd fs type
authorChristian Goeschel Ndjomouo <cgoesc2@wgu.edu>
Thu, 4 Dec 2025 03:59:54 +0000 (22:59 -0500)
committerChristian Goeschel Ndjomouo <cgoesc2@wgu.edu>
Thu, 4 Dec 2025 18:45:03 +0000 (13:45 -0500)
Signed-off-by: Christian Goeschel Ndjomouo <cgoesc2@wgu.edu>
include/pidfd-utils.h
lib/pidfd-utils.c

index cfa1d862c515d33f121994f1e4e87b89b29457e4..bd83f790d4d75610397db9dc1e0b7e3e2c8e2e97 100644 (file)
@@ -97,6 +97,7 @@ static inline int pidfd_getfd(int pidfd __attribute__((unused)),
 }
 #endif
 
+int pfd_is_pidfs(int pfd);
 int ul_get_valid_pidfd_or_err(pid_t pid, ino_t pfd_ino);
 
 #endif /* UTIL_LINUX_PIDFD_UTILS */
index a0cc16953ff8a93bfa7213a696d8d6604a533e4d..cf8286928d4812e2463bc7c4a419c1dfe4d8c993 100644 (file)
@@ -4,8 +4,11 @@
  *
  * Authors: Christian Goeschel Ndjomouo <cgoesc2@wgu.edu> [2025]
  */
+#define _GNU_SOURCE 1
+
 #include <stdlib.h>
 #include <sys/stat.h>
+#include <sys/vfs.h>
 #include <sys/types.h>
 #include <errno.h>
 #include <err.h>
 #include "nls.h"
 #include "strutils.h"
 #include "pidfd-utils.h"
+#include "statfs_magic.h"
+
+/*
+ * Returns 1, if the pidfd has the pidfs file system type, otherwise 0.
+ */
+int pfd_is_pidfs(int pfd)
+{
+       struct statfs stfs;
+       int rc;
+
+       rc = fstatfs(pfd, &stfs);
+       if (rc < 0)
+               return 0;
+
+       return F_TYPE_EQUAL(stfs.f_type, STATFS_PIDFS_MAGIC);
+}
 
 /*
  * ul_get_valid_pidfd_or_err() - Return a valid file descriptor for a PID
 int ul_get_valid_pidfd_or_err(pid_t pid, ino_t pfd_ino)
 {
         int pfd, rc;
-        struct stat f;
+        struct statx stx;
 
         pfd = pidfd_open(pid, 0);
         if (pfd < 0)
-                err(EXIT_FAILURE, _("pidfd_open() failed"));
+                err(EXIT_FAILURE, N_("pidfd_open() failed"));
+
+       /* the file descriptor has to have the pidfs file system type
+        * otherwise the inode assigned to it will not be useful.
+        */
+       if (!pfd_is_pidfs(pfd)) {
+               close(pfd);
+               errx(EXIT_FAILURE, N_("pidfd needs to have the pidfs file system type"));
+       }
 
         if (pfd_ino) {
-                rc = fstat(pfd, &f);
+                rc = statx(pfd, NULL, AT_EMPTY_PATH, STATX_INO, &stx);
                 if (rc < 0)
-                        err(EXIT_FAILURE, _("failed to fstat() pidfd"));
+                        err(EXIT_FAILURE, N_("failed to statx() pidfd"));
 
-                if (f.st_ino != pfd_ino) {
+                if (stx.stx_ino != pfd_ino) {
                         close(pfd);
-                        errx(EXIT_FAILURE, _("pidfd inode %"PRIu64" not found for pid %d"),
+                        errx(EXIT_FAILURE, N_("pidfd inode %"PRIu64" not found for pid %d"),
                                 pfd_ino, pid);
                 }
         }
         return pfd;
-}
\ No newline at end of file
+}