From: Christian Goeschel Ndjomouo Date: Thu, 4 Dec 2025 03:59:54 +0000 (-0500) Subject: lib: (pidfd-utils.c) add a helper routine to check the pidfd fs type X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f90de571343efdd108047ec94b4cfd277accf126;p=thirdparty%2Futil-linux.git lib: (pidfd-utils.c) add a helper routine to check the pidfd fs type Signed-off-by: Christian Goeschel Ndjomouo --- diff --git a/include/pidfd-utils.h b/include/pidfd-utils.h index cfa1d862c..bd83f790d 100644 --- a/include/pidfd-utils.h +++ b/include/pidfd-utils.h @@ -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 */ diff --git a/lib/pidfd-utils.c b/lib/pidfd-utils.c index a0cc16953..cf8286928 100644 --- a/lib/pidfd-utils.c +++ b/lib/pidfd-utils.c @@ -4,8 +4,11 @@ * * Authors: Christian Goeschel Ndjomouo [2025] */ +#define _GNU_SOURCE 1 + #include #include +#include #include #include #include @@ -14,6 +17,22 @@ #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 @@ -33,22 +52,30 @@ 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 +}