From 571867ffa76c7829d3901386aa43294852a0363c Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 2 Jul 2025 11:12:23 +0200 Subject: [PATCH] pidref: add pidref_set_pid_and_pidfd_id() 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 | 25 +++++++++++++++++++++++++ src/basic/pidref.h | 1 + 2 files changed, 26 insertions(+) diff --git a/src/basic/pidref.c b/src/basic/pidref.c index e4c51dd9527..7f66496dd3f 100644 --- a/src/basic/pidref.c +++ b/src/basic/pidref.c @@ -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; diff --git a/src/basic/pidref.h b/src/basic/pidref.h index 724871842b9..4d8a084c4b9 100644 --- a/src/basic/pidref.h +++ b/src/basic/pidref.h @@ -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 */ -- 2.47.3