From 931a19e8b1541f3af53f5c13bf5f0ff746311bdd Mon Sep 17 00:00:00 2001 From: christos Date: Sun, 13 Nov 2016 15:25:01 +0000 Subject: [PATCH] Make p_ppid contain the original parent's pid even for traced processes. Only change it when we are being permanently reparented to init. Since p_ppid is only used as a cached value to retrieve the parent's process id from userland, this change makes it correct at all times. Idea from kre@ Revert specialized logic from getpid/getppid now that it is not needed. --- sys/kern/kern_exit.c | 9 +++++---- sys/kern/kern_prot.c | 25 ++++++------------------- 2 files changed, 11 insertions(+), 23 deletions(-) diff --git a/sys/kern/kern_exit.c b/sys/kern/kern_exit.c index d475e90c8a62..2123bf6bb81f 100644 --- a/sys/kern/kern_exit.c +++ b/sys/kern/kern_exit.c @@ -1,4 +1,4 @@ -/* $NetBSD: kern_exit.c,v 1.266 2016/11/10 17:07:14 christos Exp $ */ +/* $NetBSD: kern_exit.c,v 1.267 2016/11/13 15:25:01 christos Exp $ */ /*- * Copyright (c) 1998, 1999, 2006, 2007, 2008 The NetBSD Foundation, Inc. @@ -67,7 +67,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: kern_exit.c,v 1.266 2016/11/10 17:07:14 christos Exp $"); +__KERNEL_RCSID(0, "$NetBSD: kern_exit.c,v 1.267 2016/11/13 15:25:01 christos Exp $"); #include "opt_ktrace.h" #include "opt_dtrace.h" @@ -1320,11 +1320,12 @@ proc_reparent(struct proc *child, struct proc *parent) child->p_pptr->p_nstopchild--; parent->p_nstopchild++; } - if (parent == initproc) + if (parent == initproc) { child->p_exitsig = SIGCHLD; + child->p_ppid = parent->p_pid; + } LIST_REMOVE(child, p_sibling); LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); child->p_pptr = parent; - child->p_ppid = parent->p_pid; } diff --git a/sys/kern/kern_prot.c b/sys/kern/kern_prot.c index 7d8a5014f627..0c642122638c 100644 --- a/sys/kern/kern_prot.c +++ b/sys/kern/kern_prot.c @@ -1,4 +1,4 @@ -/* $NetBSD: kern_prot.c,v 1.120 2016/11/12 19:42:47 christos Exp $ */ +/* $NetBSD: kern_prot.c,v 1.121 2016/11/13 15:25:01 christos Exp $ */ /* * Copyright (c) 1982, 1986, 1989, 1990, 1991, 1993 @@ -41,7 +41,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: kern_prot.c,v 1.120 2016/11/12 19:42:47 christos Exp $"); +__KERNEL_RCSID(0, "$NetBSD: kern_prot.c,v 1.121 2016/11/13 15:25:01 christos Exp $"); #ifdef _KERNEL_OPT #include "opt_compat_43.h" @@ -80,21 +80,6 @@ sys_getpid(struct lwp *l, const void *v, register_t *retval) return (0); } -static pid_t -kern_getppid(struct lwp *l) -{ - struct proc *p = l->l_proc; - pid_t ppid; - - mutex_enter(proc_lock); - mutex_enter(p->p_lock); - ppid = (p->p_slflag & PSL_TRACED) && p->p_opptr ? p->p_opptr->p_pid : - p->p_pptr->p_pid; - mutex_exit(p->p_lock); - mutex_exit(proc_lock); - return ppid; -} - /* ARGSUSED */ int sys_getpid_with_ppid(struct lwp *l, const void *v, register_t *retval) @@ -102,7 +87,7 @@ sys_getpid_with_ppid(struct lwp *l, const void *v, register_t *retval) struct proc *p = l->l_proc; retval[0] = p->p_pid; - retval[1] = kern_getppid(l); + retval[1] = p->p_ppid; return (0); } @@ -110,7 +95,9 @@ sys_getpid_with_ppid(struct lwp *l, const void *v, register_t *retval) int sys_getppid(struct lwp *l, const void *v, register_t *retval) { - *retval = kern_getppid(l); + struct proc *p = l->l_proc; + + *retval = p->p_ppid; return (0); }