diff --git a/src/basic/pidref.h b/src/basic/pidref.h index 65c7719938..84f3dee129 100644 --- a/src/basic/pidref.h +++ b/src/basic/pidref.h @@ -15,6 +15,18 @@ static inline bool pidref_is_set(const PidRef *pidref) { return pidref && pidref->pid > 0; } +static inline bool pidref_equal(const PidRef *a, const PidRef *b) { + + if (pidref_is_set(a)) { + if (!pidref_is_set(b)) + return false; + + return a->pid == b->pid; + } + + return !pidref_is_set(b); +} + 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); diff --git a/src/core/service.c b/src/core/service.c index 05a2bb6ddf..65278dae4a 100644 --- a/src/core/service.c +++ b/src/core/service.c @@ -193,12 +193,12 @@ static int service_set_main_pidref(Service *s, PidRef *pidref) { if (pidref->pid == getpid_cached()) return -EINVAL; - if (s->main_pid.pid == pidref->pid && s->main_pid_known) { + if (pidref_equal(&s->main_pid, pidref) && s->main_pid_known) { pidref_done(pidref); return 0; } - if (s->main_pid.pid != pidref->pid) { + if (!pidref_equal(&s->main_pid, pidref)) { service_unwatch_main_pid(s); exec_status_start(&s->main_exec_status, pidref->pid); }