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