anitazha / rpms / systemd

Forked from rpms/systemd 3 years ago
Clone

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

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