]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib: (pidfd-utils) new helper function to retrieve pidfd inode number
authorChristian Goeschel Ndjomouo <cgoesc2@wgu.edu>
Thu, 4 Dec 2025 04:47:16 +0000 (23:47 -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 bd83f790d4d75610397db9dc1e0b7e3e2c8e2e97..af67b865b0a5a40822a84eafda5f66b6275bd5ac 100644 (file)
@@ -31,7 +31,8 @@
 # define PIDFD_GET_UTS_NAMESPACE               _IO(PIDFS_IOCTL_MAGIC, 10)
 #endif
 
-#if (defined(HAVE_PIDFD_OPEN) || defined(SYS_pidfd_open)) && defined(HAVE_STATX)
+#if (defined(HAVE_PIDFD_OPEN) || defined(SYS_pidfd_open)) && defined(HAVE_STATX) \
+       && defined(HAVE_STRUCT_STATX)
 #define USE_PIDFD_INO_SUPPORT 1
 #endif
 
@@ -97,7 +98,8 @@ 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);
+int pfd_is_pidfs(int pidfd);
+ino_t pidfd_get_inode(int pidfd);
+int ul_get_valid_pidfd_or_err(pid_t pid, uint64_t pidfd_ino __attribute__((__unused__)));
 
 #endif /* UTIL_LINUX_PIDFD_UTILS */
index cf8286928d4812e2463bc7c4a419c1dfe4d8c993..55f05a437f12d10637dc96e17936a2c9aa23ae59 100644 (file)
 /*
  * Returns 1, if the pidfd has the pidfs file system type, otherwise 0.
  */
-int pfd_is_pidfs(int pfd)
+int pfd_is_pidfs(int pidfd)
 {
        struct statfs stfs;
        int rc;
 
-       rc = fstatfs(pfd, &stfs);
+       rc = fstatfs(pidfd, &stfs);
        if (rc < 0)
                return 0;
 
        return F_TYPE_EQUAL(stfs.f_type, STATFS_PIDFS_MAGIC);
 }
 
+#ifdef USE_PIDFD_INO_SUPPORT
+uint64_t pidfd_get_inode(int pidfd)
+{
+       struct statx stx;
+       int rc;
+
+       rc = statx(pidfd, "", AT_EMPTY_PATH, STATX_INO, &stx);
+       if (rc < 0) {
+               close(pidfd);
+               err(EXIT_FAILURE, N_("failed to statx() pidfd"));
+       }
+       return stx.stx_ino;
+}
+#endif
+
 /*
  * ul_get_valid_pidfd_or_err() - Return a valid file descriptor for a PID
  *                               or exit the process with an error message.
@@ -49,14 +64,13 @@ int pfd_is_pidfs(int pfd)
  *         print an error message and kill the program.
  *
  */
-int ul_get_valid_pidfd_or_err(pid_t pid, ino_t pfd_ino)
+int ul_get_valid_pidfd_or_err(pid_t pid, uint64_t pidfd_ino __attribute__((__unused__)))
 {
-        int pfd, rc;
-        struct statx stx;
+       int pfd;
 
-        pfd = pidfd_open(pid, 0);
-        if (pfd < 0)
-                err(EXIT_FAILURE, N_("pidfd_open() failed"));
+       pfd = pidfd_open(pid, 0);
+       if (pfd < 0)
+               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.
@@ -66,16 +80,16 @@ int ul_get_valid_pidfd_or_err(pid_t pid, ino_t pfd_ino)
                errx(EXIT_FAILURE, N_("pidfd needs to have the pidfs file system type"));
        }
 
-        if (pfd_ino) {
-                rc = statx(pfd, NULL, AT_EMPTY_PATH, STATX_INO, &stx);
-                if (rc < 0)
-                        err(EXIT_FAILURE, N_("failed to statx() pidfd"));
-
-                if (stx.stx_ino != pfd_ino) {
-                        close(pfd);
-                        errx(EXIT_FAILURE, N_("pidfd inode %"PRIu64" not found for pid %d"),
-                                pfd_ino, pid);
-                }
-        }
-        return pfd;
+#ifdef USE_PIDFD_INO_SUPPORT
+       uint64_t real_pidfd_ino;
+       if (pidfd_ino) {
+               real_pidfd_ino = pidfd_get_inode(pfd);
+               if (real_pidfd_ino != pidfd_ino) {
+                       close(pfd);
+                       errx(EXIT_FAILURE, N_("pidfd inode %"PRIu64" not found for pid %d"),
+                               pidfd_ino, pid);
+               }
+       }
+#endif
+       return pfd;
 }