[Devel] [PATCH RHEL COMMIT] ve: Replace 0 ppid with 1 (workaround for bad utils)

Konstantin Khorenko khorenko at virtuozzo.com
Tue Sep 28 19:31:12 MSK 2021


The commit is pushed to "branch-rh9-5.14.vz9.1.x-ovz" and will appear at https://src.openvz.org/scm/ovz/vzkernel.git
after ark-5.14
------>
commit 83a59be566957a5f6ba1aed191740d5335ccb333
Author: Kirill Tkhai <ktkhai at virtuozzo.com>
Date:   Tue Sep 28 19:31:12 2021 +0300

    ve: Replace 0 ppid with 1 (workaround for bad utils)
    
    Extracted from "Initial patch".
    
    Signed-off-by: Kirill Tkhai <ktkhai at virtuozzo.com>
    
    (cherry picked from vz7 commit 762f3e6a33f389a9bf8cfbd05278ce587520ca94)
    Signed-off-by: Konstantin Khorenko <khorenko at virtuozzo.com>
    
    vz8 rebase notes:
    * use proper pidns in sys_getppid() - not children's one as in
      original vz7 patch
    
    (cherry picked from vz8 commit 5d8f6f582aef318399ee826a4bc0f096a176c049)
    Signed-off-by: Pavel Tikhomirov <ptikhomirov at virtuozzo.com>
---
 fs/proc/array.c     |  5 ++---
 include/linux/pid.h |  1 +
 kernel/pid.c        | 12 ++++++++++++
 kernel/sys.c        |  2 +-
 4 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/fs/proc/array.c b/fs/proc/array.c
index 481139b434c5..24e5c06c2ed0 100644
--- a/fs/proc/array.c
+++ b/fs/proc/array.c
@@ -174,8 +174,7 @@ static inline void task_state(struct seq_file *m, struct pid_namespace *ns,
 	unsigned int max_fds = 0;
 
 	rcu_read_lock();
-	ppid = pid_alive(p) ?
-		task_tgid_nr_ns(rcu_dereference(p->real_parent), ns) : 0;
+	ppid = pid_alive(p) ? ve_task_ppid_nr_ns(p, ns) : 0;
 
 	tracer = ptrace_parent(p);
 	if (tracer)
@@ -565,7 +564,7 @@ static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
 		}
 
 		sid = task_session_nr_ns(task, ns);
-		ppid = task_tgid_nr_ns(task->real_parent, ns);
+		ppid = ve_task_ppid_nr_ns(task, ns);
 		pgid = task_pgrp_nr_ns(task, ns);
 
 		unlock_task_sighand(task, &flags);
diff --git a/include/linux/pid.h b/include/linux/pid.h
index fa10acb8d6a4..518fcfccb1ed 100644
--- a/include/linux/pid.h
+++ b/include/linux/pid.h
@@ -186,6 +186,7 @@ static inline pid_t pid_nr(struct pid *pid)
 
 pid_t pid_nr_ns(struct pid *pid, struct pid_namespace *ns);
 pid_t pid_vnr(struct pid *pid);
+pid_t ve_task_ppid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns);
 
 #define do_each_pid_task(pid, type, task)				\
 	do {								\
diff --git a/kernel/pid.c b/kernel/pid.c
index ebdf9c60cd0b..8d0b41e4f735 100644
--- a/kernel/pid.c
+++ b/kernel/pid.c
@@ -504,6 +504,18 @@ pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type,
 }
 EXPORT_SYMBOL(__task_pid_nr_ns);
 
+pid_t ve_task_ppid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
+{
+	pid_t ppid;
+	rcu_read_lock();
+	ppid = task_tgid_nr_ns(rcu_dereference(tsk->real_parent), ns);
+	rcu_read_unlock();
+	/* It's dirty hack. Some old utils don't work if ppid is zero*/
+	if (ppid == 0 && ns->child_reaper != tsk)
+		ppid = 1;
+	return ppid;
+}
+
 struct pid_namespace *task_active_pid_ns(struct task_struct *tsk)
 {
 	return ns_of_pid(task_pid(tsk));
diff --git a/kernel/sys.c b/kernel/sys.c
index cc01e2d60e08..da8e44e1525d 100644
--- a/kernel/sys.c
+++ b/kernel/sys.c
@@ -943,7 +943,7 @@ SYSCALL_DEFINE0(getppid)
 	int pid;
 
 	rcu_read_lock();
-	pid = task_tgid_vnr(rcu_dereference(current->real_parent));
+	pid = ve_task_ppid_nr_ns(current, task_active_pid_ns(current));
 	rcu_read_unlock();
 
 	return pid;


More information about the Devel mailing list