]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
pidref: move generic pidfd_get_inode_id() to pidfd-util
authorMike Yuan <me@yhndnzj.com>
Tue, 19 Nov 2024 20:30:47 +0000 (21:30 +0100)
committerMike Yuan <me@yhndnzj.com>
Sat, 4 Jan 2025 16:07:58 +0000 (17:07 +0100)
Prompted by https://github.com/systemd/systemd/pull/35224/commits/221d6e54c60389f26c2c79dbfa4e83204d2775d7

Also generalize pidfd_check_pidfs() and expose have_pidfs for later use.

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

index 351107ab5c1b5ff08061bbd7f351af6d4345effc..53f2a688a740717c49b8a807df5694c1ee0e539e 100644 (file)
@@ -7,11 +7,32 @@
 #include "fileio.h"
 #include "macro.h"
 #include "memory-util.h"
+#include "missing_magic.h"
 #include "parse-util.h"
 #include "path-util.h"
 #include "pidfd-util.h"
+#include "process-util.h"
+#include "stat-util.h"
 #include "string-util.h"
 
+static int have_pidfs = -1;
+
+static int pidfd_check_pidfs(void) {
+
+        if (have_pidfs >= 0)
+                return have_pidfs;
+
+        _cleanup_close_ int fd = pidfd_open(getpid_cached(), 0);
+        if (fd < 0) {
+                if (ERRNO_IS_NOT_SUPPORTED(errno))
+                        return (have_pidfs = false);
+
+                return -errno;
+        }
+
+        return (have_pidfs = fd_is_fs_type(fd, PID_FS_MAGIC));
+}
+
 int pidfd_get_pid(int fd, pid_t *ret) {
         char path[STRLEN("/proc/self/fdinfo/") + DECIMAL_STR_MAX(int)];
         _cleanup_free_ char *fdinfo = NULL;
@@ -64,3 +85,23 @@ int pidfd_verify_pid(int pidfd, pid_t pid) {
 
         return current_pid != pid ? -ESRCH : 0;
 }
+
+int pidfd_get_inode_id(int fd, uint64_t *ret) {
+        int r;
+
+        assert(fd >= 0);
+
+        r = pidfd_check_pidfs();
+        if (r < 0)
+                return r;
+        if (r == 0)
+                return -EOPNOTSUPP;
+
+        struct stat st;
+        if (fstat(fd, &st) < 0)
+                return -errno;
+
+        if (ret)
+                *ret = (uint64_t) st.st_ino;
+        return 0;
+}
index 4304c5aa6a587b19c4590bba812409f04a7e924d..4db9368e4072bae215752863646a2c95ad68808d 100644 (file)
@@ -9,3 +9,5 @@
 
 int pidfd_get_pid(int fd, pid_t *ret);
 int pidfd_verify_pid(int pidfd, pid_t pid);
+
+int pidfd_get_inode_id(int fd, uint64_t *ret);
index ecea98fe6d984c4922a4372ba0e3c8e6d47cb23b..f665438375be36a87b28e6e9ba47e5719a645313 100644 (file)
@@ -2,7 +2,6 @@
 
 #include "errno-util.h"
 #include "fd-util.h"
-#include "missing_magic.h"
 #include "missing_syscall.h"
 #include "missing_wait.h"
 #include "parse-util.h"
@@ -10,24 +9,6 @@
 #include "pidref.h"
 #include "process-util.h"
 #include "signal-util.h"
-#include "stat-util.h"
-
-static int pidfd_inode_ids_supported(void) {
-        static int cached = -1;
-
-        if (cached >= 0)
-                return cached;
-
-        _cleanup_close_ int fd = pidfd_open(getpid_cached(), 0);
-        if (fd < 0) {
-                if (ERRNO_IS_NOT_SUPPORTED(errno))
-                        return (cached = false);
-
-                return -errno;
-        }
-
-        return (cached = fd_is_fs_type(fd, PID_FS_MAGIC));
-}
 
 int pidref_acquire_pidfd_id(PidRef *pidref) {
         int r;
@@ -46,19 +27,14 @@ int pidref_acquire_pidfd_id(PidRef *pidref) {
         if (pidref->fd_id > 0)
                 return 0;
 
-        r = pidfd_inode_ids_supported();
-        if (r < 0)
+        r = pidfd_get_inode_id(pidref->fd, &pidref->fd_id);
+        if (r < 0) {
+                if (!ERRNO_IS_NEG_NOT_SUPPORTED(r))
+                        log_debug_errno(r, "Failed to get inode number of pidfd for pid " PID_FMT ": %m",
+                                        pidref->pid);
                 return r;
-        if (r == 0)
-                return -EOPNOTSUPP;
-
-        struct stat st;
-
-        if (fstat(pidref->fd, &st) < 0)
-                return log_debug_errno(errno, "Failed to get inode number of pidfd for pid " PID_FMT ": %m",
-                                       pidref->pid);
+        }
 
-        pidref->fd_id = (uint64_t) st.st_ino;
         return 0;
 }