]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
pidfd-util: dont open a new pidfd unnecessarily in pidfd_check_pidfs()
authorLennart Poettering <lennart@poettering.net>
Fri, 17 Jan 2025 13:01:30 +0000 (14:01 +0100)
committerLennart Poettering <lennart@poettering.net>
Mon, 20 Jan 2025 20:49:38 +0000 (21:49 +0100)
src/basic/pidfd-util.c

index c90699d066ee10d5c731f05fad5cb2aa6650d1df..62db26d536c6666f8e71a971fea6c323c3f72e59 100644 (file)
 
 static int have_pidfs = -1;
 
-static int pidfd_check_pidfs(void) {
+static int pidfd_check_pidfs(int pid_fd) {
+
+        /* NB: the passed fd *must* be acquired via pidfd_open(), i.e. must be a true pidfd! */
 
         if (have_pidfs >= 0)
                 return have_pidfs;
 
-        _cleanup_close_ int fd = pidfd_open(getpid_cached(), 0);
-        if (fd < 0)
-                return -errno;
-
-        return (have_pidfs = fd_is_fs_type(fd, PID_FS_MAGIC));
+        return (have_pidfs = fd_is_fs_type(pid_fd, PID_FS_MAGIC));
 }
 
 int pidfd_get_namespace(int fd, unsigned long ns_type_cmd) {
@@ -231,7 +229,7 @@ int pidfd_get_inode_id(int fd, uint64_t *ret) {
 
         assert(fd >= 0);
 
-        r = pidfd_check_pidfs();
+        r = pidfd_check_pidfs(fd);
         if (r < 0)
                 return r;
         if (r == 0)