]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
pidref: add pidref_equal() helper
authorLennart Poettering <lennart@poettering.net>
Tue, 19 Sep 2023 14:09:26 +0000 (16:09 +0200)
committerMike Yuan <me@yhndnzj.com>
Wed, 20 Sep 2023 05:02:21 +0000 (13:02 +0800)
This compares two PidRef structures via the pid_t field. Ideally we'd do
a stricter comparison here, that is safe towards PID reuse, but so far
the pidfd API lacks suitable mechanisms for that, hence do the best we
can do.

src/basic/pidref.h
src/core/service.c

index 65c771993808874a5745cdcfd7795b9d3671392f..84f3dee12980f4145c9c2e0ed80fe765d1afb22a 100644 (file)
@@ -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);
index 05a2bb6ddf1a58ce6bc9620ae6acb0c49219e35e..65278dae4aeea082182ae604d47f7d07dd1df714 100644 (file)
@@ -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);
         }