]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
pidfd-util: add helper for getting our own pidfdid
authorLennart Poettering <lennart@poettering.net>
Mon, 18 Nov 2024 10:23:07 +0000 (11:23 +0100)
committerLennart Poettering <lennart@poettering.net>
Mon, 20 Jan 2025 20:51:29 +0000 (21:51 +0100)
let's start caching our own pidfd id, since it's somewhat involved to
get but a much better identifier for things than the classic PID is.

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

index 62db26d536c6666f8e71a971fea6c323c3f72e59..36fe224324aa03d4e8531044206f707a888f1855 100644 (file)
@@ -9,6 +9,7 @@
 #include "macro.h"
 #include "memory-util.h"
 #include "missing_magic.h"
+#include "missing_threads.h"
 #include "parse-util.h"
 #include "path-util.h"
 #include "pidfd-util.h"
@@ -243,3 +244,32 @@ int pidfd_get_inode_id(int fd, uint64_t *ret) {
                 *ret = (uint64_t) st.st_ino;
         return 0;
 }
+
+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 */
+        int r;
+
+        assert(ret);
+
+        if (initialized == getpid_cached()) {
+                *ret = cached;
+                return 0;
+        }
+        if (initialized < 0)
+                return initialized;
+
+        _cleanup_close_ int fd = pidfd_open(getpid_cached(), 0);
+        if (fd < 0)
+                return -errno;
+
+        r = pidfd_get_inode_id(fd, &cached);
+        if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
+                return (initialized = -EOPNOTSUPP);
+        if (r < 0)
+                return r;
+
+        *ret = cached;
+        initialized = getpid_cached();
+        return 0;
+}
index 374e96261b02d18e40e412bf867d77eb78a6150f..c20de6df6762443dc76af1c8fce156d7599e85e9 100644 (file)
@@ -17,3 +17,5 @@ int pidfd_get_uid(int fd, uid_t *ret);
 int pidfd_get_cgroupid(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);
index 166c0fa2c9ebd897c8a76406972d0a5a072b9c1a..0db49fe3a8fc5822bf02b6e53413919f317162cb 100644 (file)
@@ -28,6 +28,7 @@
 #include "missing_syscall.h"
 #include "namespace-util.h"
 #include "parse-util.h"
+#include "pidfd-util.h"
 #include "process-util.h"
 #include "procfs-util.h"
 #include "rlimit-util.h"
@@ -1065,6 +1066,21 @@ TEST(pidref_from_same_root_fs) {
         ASSERT_OK_ZERO(pidref_from_same_root_fs(&child2, &self));
 }
 
+TEST(pidfd_get_inode_id_self_cached) {
+        int r;
+
+        log_info("pid=" PID_FMT, getpid_cached());
+
+        uint64_t id;
+        r = pidfd_get_inode_id_self_cached(&id);
+        if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
+                log_info("pidfdid not supported");
+        else {
+                assert(r >= 0);
+                log_info("pidfdid=%" PRIu64, id);
+        }
+}
+
 static int intro(void) {
         log_show_color(true);
         return EXIT_SUCCESS;