1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-26 03:22:00 +03:00

pidref: add PIDREF_MAKE_FROM_PID()

This helper truns a pid_t into a PidRef. It's different from
pidref_set_pid() in being "passive", i.e. it does not attempt to acquire
a pidfd for the pid.

This is useful when using the PidRef as a lookup key that shall also
work after a process is already dead, and hence no conversion to a pidfd
is possible anymore.
This commit is contained in:
Lennart Poettering 2023-09-19 16:05:42 +02:00
parent 12c7d27b65
commit dcfcea6d02
2 changed files with 7 additions and 5 deletions

View File

@ -68,11 +68,7 @@ int pidref_set_pidfd(PidRef *pidref, int fd) {
if (r < 0)
return r;
*pidref = (PidRef) {
.fd = -EBADF,
.pid = pid,
};
*pidref = PIDREF_MAKE_FROM_PID(pid);
return 0;
}

View File

@ -11,6 +11,10 @@ typedef struct PidRef {
#define PIDREF_NULL (PidRef) { .fd = -EBADF }
/* Turns a pid_t into a PidRef structure on-the-fly *without* acquiring a pidfd for it. (As opposed to
* pidref_set_pid() which does so *with* acquiring one, see below) */
#define PIDREF_MAKE_FROM_PID(x) (PidRef) { .pid = (x), .fd = -EBADF }
static inline bool pidref_is_set(const PidRef *pidref) {
return pidref && pidref->pid > 0;
}
@ -27,6 +31,8 @@ static inline bool pidref_equal(const PidRef *a, const PidRef *b) {
return !pidref_is_set(b);
}
/* This turns a pid_t into a PidRef structure, and acquires a pidfd for it, if possible. (As opposed to
* 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_pidfd(PidRef *pidref, int fd);