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.
This commit is contained in:
Lennart Poettering
2025-07-11 09:06:41 +02:00
parent 869b44e0f7
commit 571867ffa7
2 changed files with 26 additions and 0 deletions
+25
View 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;
+1
View 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 */