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