From 6774be42067007ec2c0872482a51d202691d5fdd Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 17 Oct 2023 12:20:16 +0200 Subject: [PATCH] process-util: add pidref_is_my_child() --- src/basic/process-util.c | 20 ++++++++++++++++++++ src/basic/process-util.h | 1 + src/core/cgroup.c | 2 +- src/core/service.c | 9 +++++++-- 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/basic/process-util.c b/src/basic/process-util.c index 2414f263dd..c15460d8c0 100644 --- a/src/basic/process-util.c +++ b/src/basic/process-util.c @@ -1015,6 +1015,9 @@ int pid_is_my_child(pid_t pid) { pid_t ppid; int r; + if (pid < 0) + return -ESRCH; + if (pid <= 1) return false; @@ -1025,6 +1028,23 @@ int pid_is_my_child(pid_t pid) { return ppid == getpid_cached(); } +int pidref_is_my_child(const PidRef *pid) { + int r, result; + + if (!pidref_is_set(pid)) + return -ESRCH; + + result = pid_is_my_child(pid->pid); + if (result < 0) + return result; + + r = pidref_verify(pid); + if (r < 0) + return r; + + return result; +} + bool pid_is_unwaited(pid_t pid) { /* Checks whether a PID is still valid at all, including a zombie */ diff --git a/src/basic/process-util.h b/src/basic/process-util.h index 7824127a6d..437a0ccd30 100644 --- a/src/basic/process-util.h +++ b/src/basic/process-util.h @@ -90,6 +90,7 @@ int pid_is_alive(pid_t pid); int pidref_is_alive(const PidRef *pidref); bool pid_is_unwaited(pid_t pid); int pid_is_my_child(pid_t pid); +int pidref_is_my_child(const PidRef *pidref); int pid_from_same_root_fs(pid_t pid); bool is_main_thread(void); diff --git a/src/core/cgroup.c b/src/core/cgroup.c index bb86794436..65851ae6e8 100644 --- a/src/core/cgroup.c +++ b/src/core/cgroup.c @@ -3175,7 +3175,7 @@ int unit_search_main_pid(Unit *u, PidRef *ret) { if (pidref_equal(&pidref, &npidref)) /* seen already, cgroupfs reports duplicates! */ continue; - if (pid_is_my_child(npidref.pid) == 0) /* ignore processes further down the tree */ + if (pidref_is_my_child(&npidref) <= 0) /* ignore processes further down the tree */ continue; if (pidref_is_set(&pidref) != 0) diff --git a/src/core/service.c b/src/core/service.c index a2f8bc7c87..fbd0984459 100644 --- a/src/core/service.c +++ b/src/core/service.c @@ -180,6 +180,8 @@ static void service_unwatch_pid_file(Service *s) { } static int service_set_main_pidref(Service *s, PidRef *pidref) { + int r; + assert(s); /* Takes ownership of the specified pidref on success, but not on failure. */ @@ -205,11 +207,14 @@ static int service_set_main_pidref(Service *s, PidRef *pidref) { s->main_pid = TAKE_PIDREF(*pidref); s->main_pid_known = true; - s->main_pid_alien = pid_is_my_child(s->main_pid.pid) == 0; - if (s->main_pid_alien) + r = pidref_is_my_child(&s->main_pid); + if (r < 0) + log_unit_warning_errno(UNIT(s), r, "Can't determine if process "PID_FMT" is our child, assuming it is not: %m", s->main_pid.pid); + else if (r == 0) log_unit_warning(UNIT(s), "Supervising process "PID_FMT" which is not our child. We'll most likely not notice when it exits.", s->main_pid.pid); + s->main_pid_alien = r <= 0; return 0; } -- 2.25.1