Module Name:    src
Committed By:   christos
Date:           Sun Nov 13 15:25:01 UTC 2016

Modified Files:
        src/sys/kern: kern_exit.c kern_prot.c

Log Message:
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.


To generate a diff of this commit:
cvs rdiff -u -r1.266 -r1.267 src/sys/kern/kern_exit.c
cvs rdiff -u -r1.120 -r1.121 src/sys/kern/kern_prot.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/kern/kern_exit.c
diff -u src/sys/kern/kern_exit.c:1.266 src/sys/kern/kern_exit.c:1.267
--- src/sys/kern/kern_exit.c:1.266	Thu Nov 10 12:07:14 2016
+++ src/sys/kern/kern_exit.c	Sun Nov 13 10:25:01 2016
@@ -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 <sys/cdefs.h>
-__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
 		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;
 }

Index: src/sys/kern/kern_prot.c
diff -u src/sys/kern/kern_prot.c:1.120 src/sys/kern/kern_prot.c:1.121
--- src/sys/kern/kern_prot.c:1.120	Sat Nov 12 14:42:47 2016
+++ src/sys/kern/kern_prot.c	Sun Nov 13 10:25:01 2016
@@ -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 <sys/cdefs.h>
-__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,
 	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, cons
 	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, cons
 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);
 }
 

Reply via email to