diff --git a/src/basic/pidref.c b/src/basic/pidref.c index e1ad697af0c..05f1b60e0ef 100644 --- a/src/basic/pidref.c +++ b/src/basic/pidref.c @@ -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; } diff --git a/src/basic/pidref.h b/src/basic/pidref.h index 84f3dee1298..0942d974b9b 100644 --- a/src/basic/pidref.h +++ b/src/basic/pidref.h @@ -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);