]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
pidref: add pidref_set_pid_and_pidfd_id()
authorLennart Poettering <lennart@poettering.net>
Wed, 2 Jul 2025 09:12:23 +0000 (11:12 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 11 Jul 2025 07:06:41 +0000 (09:06 +0200)
This new helper takes both a PID and and a pidfd ID, and initializes a
PidRef from it. It ensures they actually belong together and returns an
error if not.

src/basic/pidref.c
src/basic/pidref.h

index e4c51dd9527ee47e852a81d9936ef9aca727cd84..7f66496dd3f9c82c1d73cafe2e61dd4547db36e3 100644 (file)
@@ -118,6 +118,31 @@ int pidref_set_pid(PidRef *pidref, pid_t pid) {
         return 0;
 }
 
+int pidref_set_pid_and_pidfd_id(
+                PidRef *pidref,
+                pid_t pid,
+                uint64_t pidfd_id) {
+
+        int r;
+
+        assert(pidref);
+
+        _cleanup_(pidref_done) PidRef n = PIDREF_NULL;
+        r = pidref_set_pid(&n, pid);
+        if (r < 0)
+                return r;
+
+        if (pidfd_id > 0) {
+                pidref_acquire_pidfd_id(&n);
+
+                if (n.fd_id != pidfd_id)
+                        return -ESRCH;
+        }
+
+        *pidref = TAKE_PIDREF(n);
+        return 0;
+}
+
 int pidref_set_pidstr(PidRef *pidref, const char *pid) {
         pid_t nr;
         int r;
index 724871842b92535b43a8afd8ce86035d3658beb8..4d8a084c4b940f7d45c0632049aac9259c023646 100644 (file)
@@ -68,6 +68,7 @@ bool pidref_equal(PidRef *a, PidRef *b);
  * PIDREF_MAKE_FROM_PID() above, which does not acquire a pidfd.) */
 int pidref_set_pid(PidRef *pidref, pid_t pid);
 int pidref_set_pidstr(PidRef *pidref, const char *pid);
+int pidref_set_pid_and_pidfd_id(PidRef *pidref, pid_t pid, uint64_t pidfd_id);
 int pidref_set_pidfd(PidRef *pidref, int fd);
 int pidref_set_pidfd_take(PidRef *pidref, int fd); /* takes ownership of the passed pidfd on success */
 int pidref_set_pidfd_consume(PidRef *pidref, int fd); /* takes ownership of the passed pidfd in both success and failure */