daandemeyer / rpms / systemd

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