]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
pidfd-util: extract pidfd_get_inode_id_impl() and make it thread safe
authorMike Yuan <me@yhndnzj.com>
Sun, 1 Jun 2025 06:57:49 +0000 (08:57 +0200)
committerMike Yuan <me@yhndnzj.com>
Wed, 4 Jun 2025 22:28:01 +0000 (00:28 +0200)
Preparation for later commits.

src/basic/pidfd-util.c
src/basic/pidfd-util.h

index 629ffe95961fca7e3443e5a21a29d8693b79362a..78f8dc41c6bb1128fa66e90c77972ba03ab33d08 100644 (file)
@@ -17,9 +17,9 @@
 #include "stdio-util.h"
 #include "string-util.h"
 
-static int have_pidfs = -1;
+static thread_local int have_pidfs = -1;
 
-static int pidfd_check_pidfs(int pid_fd) {
+int pidfd_check_pidfs(int pid_fd) {
 
         /* NB: the passed fd *must* be acquired via pidfd_open(), i.e. must be a true pidfd! */
 
@@ -229,18 +229,12 @@ int pidfd_get_cgroupid(int fd, uint64_t *ret) {
         return 0;
 }
 
-int pidfd_get_inode_id(int fd, uint64_t *ret) {
-        static bool file_handle_supported = true;
+int pidfd_get_inode_id_impl(int fd, uint64_t *ret) {
+        static thread_local bool file_handle_supported = true;
         int r;
 
         assert(fd >= 0);
 
-        r = pidfd_check_pidfs(fd);
-        if (r < 0)
-                return r;
-        if (r == 0)
-                return -EOPNOTSUPP;
-
         if (file_handle_supported) {
                 union {
                         struct file_handle file_handle;
@@ -284,6 +278,20 @@ int pidfd_get_inode_id(int fd, uint64_t *ret) {
 #endif
 }
 
+int pidfd_get_inode_id(int fd, uint64_t *ret) {
+        int r;
+
+        assert(fd >= 0);
+
+        r = pidfd_check_pidfs(fd);
+        if (r < 0)
+                return r;
+        if (r == 0)
+                return -EOPNOTSUPP;
+
+        return pidfd_get_inode_id_impl(fd, ret);
+}
+
 int pidfd_get_inode_id_self_cached(uint64_t *ret) {
         static thread_local uint64_t cached = 0;
         static thread_local pid_t initialized = 0; /* < 0: cached error; == 0: invalid; > 0: valid and pid that was current */
index 774f97018a71a433df5a29b532d7a20efd61e53c..e998b8a3bcfb1a1edb2208d962467fb16164a56a 100644 (file)
@@ -14,6 +14,8 @@ int pidfd_get_ppid(int fd, pid_t *ret);
 int pidfd_get_uid(int fd, uid_t *ret);
 int pidfd_get_cgroupid(int fd, uint64_t *ret);
 
+int pidfd_get_inode_id_impl(int fd, uint64_t *ret);
 int pidfd_get_inode_id(int fd, uint64_t *ret);
-
 int pidfd_get_inode_id_self_cached(uint64_t *ret);
+
+int pidfd_check_pidfs(int pid_fd);