]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib: (pidfd-utils) provide a more liberal variant of ul_get_valid_pidfd_or_err()
authorChristian Goeschel Ndjomouo <cgoesc2@wgu.edu>
Sat, 10 Jan 2026 01:53:00 +0000 (20:53 -0500)
committerChristian Goeschel Ndjomouo <cgoesc2@wgu.edu>
Sun, 1 Feb 2026 20:08:23 +0000 (15:08 -0500)
The new function ul_get_valid_pidfd() behaves less opinionated
in an error case and instead of terminating the calling process
it returns an error.

Signed-off-by: Christian Goeschel Ndjomouo <cgoesc2@wgu.edu>
include/pidfd-utils.h
lib/pidfd-utils.c

index af67b865b0a5a40822a84eafda5f66b6275bd5ac..4c3f954d4d5d3ee117b3c143759b757aac45f369 100644 (file)
@@ -100,6 +100,13 @@ static inline int pidfd_getfd(int pidfd __attribute__((unused)),
 
 int pfd_is_pidfs(int pidfd);
 ino_t pidfd_get_inode(int pidfd);
+
+#ifdef USE_PIDFD_INO_SUPPORT
+int ul_get_valid_pidfd_or_err(pid_t pid, uint64_t pidfd_ino);
+int ul_get_valid_pidfd(pid_t pid, uint64_t pidfd_ino);
+#else
 int ul_get_valid_pidfd_or_err(pid_t pid, uint64_t pidfd_ino __attribute__((__unused__)));
+int ul_get_valid_pidfd(pid_t pid, uint64_t pidfd_ino __attribute__((__unused__)));
+#endif
 
 #endif /* UTIL_LINUX_PIDFD_UTILS */
index 895f6e75239f112a3496156b5b0b4e278a8248cf..a837359201821dff2b906194381b228dd1a4a926 100644 (file)
@@ -58,8 +58,8 @@ uint64_t pidfd_get_inode(int pidfd)
  * Pass @pfd_ino as 0, 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.
+ *         On failure, err() is called with an error message
+ *         and the processes is terminated.
  *
  */
 #ifdef USE_PIDFD_INO_SUPPORT
@@ -70,16 +70,45 @@ int ul_get_valid_pidfd_or_err(pid_t pid, uint64_t pidfd_ino __attribute__((__unu
 {
        int pfd;
 
+       pfd = ul_get_valid_pidfd(pid, pidfd_ino);
+       if (pfd < 0)
+               err(EXIT_FAILURE, N_("failed to obtain a valid file descriptor for PID %d"), pid);
+
+       return pfd;
+}
+
+/*
+ * ul_get_valid_pidfd() - Return a valid file descriptor for a PID
+ *
+ * @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 0, if the pidfd should not be validated.
+ *
+ * Return: On success, a file descriptor is returned.
+ *         On failure, a negative errno is returned and errno
+ *         is set accordingly.
+ *
+ */
+#ifdef USE_PIDFD_INO_SUPPORT
+int ul_get_valid_pidfd(pid_t pid, uint64_t pidfd_ino)
+#else
+int ul_get_valid_pidfd(pid_t pid, uint64_t pidfd_ino __attribute__((__unused__)))
+#endif
+{
+       int pfd;
+
        pfd = pidfd_open(pid, 0);
        if (pfd < 0)
-               err(EXIT_FAILURE, N_("pidfd_open() failed"));
+               return -errno;
 
        /* 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"));
+               return -(errno = ENOTSUP);
        }
 
 #ifdef USE_PIDFD_INO_SUPPORT
@@ -88,8 +117,7 @@ int ul_get_valid_pidfd_or_err(pid_t pid, uint64_t pidfd_ino __attribute__((__unu
                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);
+                       return -(errno = ESRCH);
                }
        }
 #endif