Blame SOURCES/0231-process-util-introduce-pid_is_my_child-helper.patch

ddca0b
From f057aa6bb604845fa10ad569bca306e5e1e8fe0d Mon Sep 17 00:00:00 2001
ddca0b
From: Franck Bui <fbui@suse.com>
ddca0b
Date: Mon, 18 Mar 2019 11:48:34 +0100
ddca0b
Subject: [PATCH] process-util: introduce pid_is_my_child() helper
ddca0b
MIME-Version: 1.0
ddca0b
Content-Type: text/plain; charset=UTF-8
ddca0b
Content-Transfer-Encoding: 8bit
ddca0b
ddca0b
No functional changes.
ddca0b
ddca0b
Thanks Renaud Métrich for backporting this to RHEL.
ddca0b
Resolves: #1744972
ddca0b
---
ddca0b
 src/basic/process-util.c | 14 ++++++++++++++
ddca0b
 src/basic/process-util.h |  1 +
ddca0b
 src/core/cgroup.c        |  7 ++-----
ddca0b
 src/core/service.c       |  8 ++------
ddca0b
 4 files changed, 19 insertions(+), 11 deletions(-)
ddca0b
ddca0b
diff --git a/src/basic/process-util.c b/src/basic/process-util.c
ddca0b
index aa3eff779a..6dbeee9dda 100644
ddca0b
--- a/src/basic/process-util.c
ddca0b
+++ b/src/basic/process-util.c
ddca0b
@@ -903,6 +903,20 @@ int getenv_for_pid(pid_t pid, const char *field, char **ret) {
ddca0b
         return 0;
ddca0b
 }
ddca0b
 
ddca0b
+int pid_is_my_child(pid_t pid) {
ddca0b
+        pid_t ppid;
ddca0b
+        int r;
ddca0b
+
ddca0b
+        if (pid <= 1)
ddca0b
+                return false;
ddca0b
+
ddca0b
+        r = get_process_ppid(pid, &ppid);
ddca0b
+        if (r < 0)
ddca0b
+                return r;
ddca0b
+
ddca0b
+        return ppid == getpid_cached();
ddca0b
+}
ddca0b
+
ddca0b
 bool pid_is_unwaited(pid_t pid) {
ddca0b
         /* Checks whether a PID is still valid at all, including a zombie */
ddca0b
 
ddca0b
diff --git a/src/basic/process-util.h b/src/basic/process-util.h
ddca0b
index a5bb072b25..a3bd2851b4 100644
ddca0b
--- a/src/basic/process-util.h
ddca0b
+++ b/src/basic/process-util.h
ddca0b
@@ -68,6 +68,7 @@ int getenv_for_pid(pid_t pid, const char *field, char **_value);
ddca0b
 
ddca0b
 bool pid_is_alive(pid_t pid);
ddca0b
 bool pid_is_unwaited(pid_t pid);
ddca0b
+int pid_is_my_child(pid_t pid);
ddca0b
 int pid_from_same_root_fs(pid_t pid);
ddca0b
 
ddca0b
 bool is_main_thread(void);
ddca0b
diff --git a/src/core/cgroup.c b/src/core/cgroup.c
ddca0b
index 62ab41a288..b7ed07e65b 100644
ddca0b
--- a/src/core/cgroup.c
ddca0b
+++ b/src/core/cgroup.c
ddca0b
@@ -1876,7 +1876,7 @@ void unit_prune_cgroup(Unit *u) {
ddca0b
 
ddca0b
 int unit_search_main_pid(Unit *u, pid_t *ret) {
ddca0b
         _cleanup_fclose_ FILE *f = NULL;
ddca0b
-        pid_t pid = 0, npid, mypid;
ddca0b
+        pid_t pid = 0, npid;
ddca0b
         int r;
ddca0b
 
ddca0b
         assert(u);
ddca0b
@@ -1889,15 +1889,12 @@ int unit_search_main_pid(Unit *u, pid_t *ret) {
ddca0b
         if (r < 0)
ddca0b
                 return r;
ddca0b
 
ddca0b
-        mypid = getpid_cached();
ddca0b
         while (cg_read_pid(f, &npid) > 0)  {
ddca0b
-                pid_t ppid;
ddca0b
 
ddca0b
                 if (npid == pid)
ddca0b
                         continue;
ddca0b
 
ddca0b
-                /* Ignore processes that aren't our kids */
ddca0b
-                if (get_process_ppid(npid, &ppid) >= 0 && ppid != mypid)
ddca0b
+                if (pid_is_my_child(npid) == 0)
ddca0b
                         continue;
ddca0b
 
ddca0b
                 if (pid != 0)
ddca0b
diff --git a/src/core/service.c b/src/core/service.c
ddca0b
index 24f167572a..614ba05d89 100644
ddca0b
--- a/src/core/service.c
ddca0b
+++ b/src/core/service.c
ddca0b
@@ -139,8 +139,6 @@ static void service_unwatch_pid_file(Service *s) {
ddca0b
 }
ddca0b
 
ddca0b
 static int service_set_main_pid(Service *s, pid_t pid) {
ddca0b
-        pid_t ppid;
ddca0b
-
ddca0b
         assert(s);
ddca0b
 
ddca0b
         if (pid <= 1)
ddca0b
@@ -159,12 +157,10 @@ static int service_set_main_pid(Service *s, pid_t pid) {
ddca0b
 
ddca0b
         s->main_pid = pid;
ddca0b
         s->main_pid_known = true;
ddca0b
+        s->main_pid_alien = pid_is_my_child(pid) == 0;
ddca0b
 
ddca0b
-        if (get_process_ppid(pid, &ppid) >= 0 && ppid != getpid_cached()) {
ddca0b
+        if (s->main_pid_alien)
ddca0b
                 log_unit_warning(UNIT(s), "Supervising process "PID_FMT" which is not our child. We'll most likely not notice when it exits.", pid);
ddca0b
-                s->main_pid_alien = true;
ddca0b
-        } else
ddca0b
-                s->main_pid_alien = false;
ddca0b
 
ddca0b
         return 0;
ddca0b
 }