]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
process-util: add pidref_is_my_child()
authorLennart Poettering <lennart@poettering.net>
Tue, 17 Oct 2023 10:20:16 +0000 (12:20 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 18 Oct 2023 12:49:40 +0000 (14:49 +0200)
src/basic/process-util.c
src/basic/process-util.h
src/core/cgroup.c
src/core/service.c

index 2414f263dd2bd7282d361b0115d0b52676fb5970..c15460d8c0c0392f0c39a875aae7cd3644e7ba2b 100644 (file)
@@ -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 */
 
index 7824127a6de0bea4b19c06bca745ed177f42ccde..437a0ccd3062347b6e77eca3ebf8957b65a76bd5 100644 (file)
@@ -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);
index bb867944360204ffc3c4dd0f627146e7b752efa0..65851ae6e82b16d351993f75eee3c8c33e2351c1 100644 (file)
@@ -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)
index a2f8bc7c87015963e660304e9787544cbd0a95a2..fbd09844598d4658fa99ceaf3615ad4847149afa 100644 (file)
@@ -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;
 }