xref: /netbsd-src/sys/kern/kern_exit.c (revision 7330f729ccf0bd976a06f95fad452fe774fc7fd1)
1 /*	$NetBSD: kern_exit.c,v 1.277 2019/10/03 22:48:44 kamil Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998, 1999, 2006, 2007, 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center, and by Andrew Doran.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * Copyright (c) 1982, 1986, 1989, 1991, 1993
35  *	The Regents of the University of California.  All rights reserved.
36  * (c) UNIX System Laboratories, Inc.
37  * All or some portions of this file are derived from material licensed
38  * to the University of California by American Telephone and Telegraph
39  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
40  * the permission of UNIX System Laboratories, Inc.
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions
44  * are met:
45  * 1. Redistributions of source code must retain the above copyright
46  *    notice, this list of conditions and the following disclaimer.
47  * 2. Redistributions in binary form must reproduce the above copyright
48  *    notice, this list of conditions and the following disclaimer in the
49  *    documentation and/or other materials provided with the distribution.
50  * 3. Neither the name of the University nor the names of its contributors
51  *    may be used to endorse or promote products derived from this software
52  *    without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64  * SUCH DAMAGE.
65  *
66  *	@(#)kern_exit.c	8.10 (Berkeley) 2/23/95
67  */
68 
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: kern_exit.c,v 1.277 2019/10/03 22:48:44 kamil Exp $");
71 
72 #include "opt_ktrace.h"
73 #include "opt_dtrace.h"
74 #include "opt_sysv.h"
75 
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/ioctl.h>
79 #include <sys/tty.h>
80 #include <sys/time.h>
81 #include <sys/resource.h>
82 #include <sys/kernel.h>
83 #include <sys/proc.h>
84 #include <sys/buf.h>
85 #include <sys/wait.h>
86 #include <sys/file.h>
87 #include <sys/fstrans.h>
88 #include <sys/vnode.h>
89 #include <sys/syslog.h>
90 #include <sys/pool.h>
91 #include <sys/uidinfo.h>
92 #include <sys/ptrace.h>
93 #include <sys/acct.h>
94 #include <sys/filedesc.h>
95 #include <sys/ras.h>
96 #include <sys/signalvar.h>
97 #include <sys/sched.h>
98 #include <sys/mount.h>
99 #include <sys/syscallargs.h>
100 #include <sys/kauth.h>
101 #include <sys/sleepq.h>
102 #include <sys/lockdebug.h>
103 #include <sys/ktrace.h>
104 #include <sys/cpu.h>
105 #include <sys/lwpctl.h>
106 #include <sys/atomic.h>
107 #include <sys/sdt.h>
108 #include <sys/psref.h>
109 
110 #include <uvm/uvm_extern.h>
111 
112 #ifdef DEBUG_EXIT
113 int debug_exit = 0;
114 #define DPRINTF(x) if (debug_exit) printf x
115 #else
116 #define DPRINTF(x)
117 #endif
118 
119 static int find_stopped_child(struct proc *, idtype_t, id_t, int,
120     struct proc **, struct wrusage *, siginfo_t *);
121 static void proc_free(struct proc *, struct wrusage *);
122 
123 /*
124  * DTrace SDT provider definitions
125  */
126 SDT_PROVIDER_DECLARE(proc);
127 SDT_PROBE_DEFINE1(proc, kernel, , exit, "int");
128 
129 /*
130  * Fill in the appropriate signal information, and signal the parent.
131  */
132 /* XXX noclone works around a gcc 4.5 bug on arm */
133 static void __noclone
134 exit_psignal(struct proc *p, struct proc *pp, ksiginfo_t *ksi)
135 {
136 
137 	KSI_INIT(ksi);
138 	if ((ksi->ksi_signo = P_EXITSIG(p)) == SIGCHLD) {
139 		if (p->p_xsig) {
140 			if (p->p_sflag & PS_COREDUMP)
141 				ksi->ksi_code = CLD_DUMPED;
142 			else
143 				ksi->ksi_code = CLD_KILLED;
144 			ksi->ksi_status = p->p_xsig;
145 		} else {
146 			ksi->ksi_code = CLD_EXITED;
147 			ksi->ksi_status = p->p_xexit;
148 		}
149 	} else {
150 		ksi->ksi_code = SI_USER;
151 		ksi->ksi_status = p->p_xsig;
152 	}
153 	/*
154 	 * We fill those in, even for non-SIGCHLD.
155 	 * It's safe to access p->p_cred unlocked here.
156 	 */
157 	ksi->ksi_pid = p->p_pid;
158 	ksi->ksi_uid = kauth_cred_geteuid(p->p_cred);
159 	/* XXX: is this still valid? */
160 	ksi->ksi_utime = p->p_stats->p_ru.ru_utime.tv_sec;
161 	ksi->ksi_stime = p->p_stats->p_ru.ru_stime.tv_sec;
162 }
163 
164 /*
165  * exit --
166  *	Death of process.
167  */
168 int
169 sys_exit(struct lwp *l, const struct sys_exit_args *uap, register_t *retval)
170 {
171 	/* {
172 		syscallarg(int)	rval;
173 	} */
174 	struct proc *p = l->l_proc;
175 
176 	/* Don't call exit1() multiple times in the same process. */
177 	mutex_enter(p->p_lock);
178 	if (p->p_sflag & PS_WEXIT) {
179 		mutex_exit(p->p_lock);
180 		lwp_exit(l);
181 	}
182 
183 	/* exit1() will release the mutex. */
184 	exit1(l, SCARG(uap, rval), 0);
185 	/* NOTREACHED */
186 	return (0);
187 }
188 
189 /*
190  * Exit: deallocate address space and other resources, change proc state
191  * to zombie, and unlink proc from allproc and parent's lists.  Save exit
192  * status and rusage for wait().  Check for child processes and orphan them.
193  *
194  * Must be called with p->p_lock held.  Does not return.
195  */
196 void
197 exit1(struct lwp *l, int exitcode, int signo)
198 {
199 	struct proc	*p, *child, *next_child, *old_parent, *new_parent;
200 	struct pgrp	*pgrp;
201 	ksiginfo_t	ksi;
202 	ksiginfoq_t	kq;
203 	int		wakeinit;
204 
205 	p = l->l_proc;
206 
207 	KASSERT(mutex_owned(p->p_lock));
208 	KASSERT(p->p_vmspace != NULL);
209 
210 	if (__predict_false(p == initproc)) {
211 		panic("init died (signal %d, exit %d)", signo, exitcode);
212 	}
213 
214 	p->p_sflag |= PS_WEXIT;
215 
216 	/*
217 	 * Force all other LWPs to exit before we do.  Only then can we
218 	 * begin to tear down the rest of the process state.
219 	 */
220 	if (p->p_nlwps > 1) {
221 		exit_lwps(l);
222 	}
223 
224 	ksiginfo_queue_init(&kq);
225 
226 	/*
227 	 * If we have been asked to stop on exit, do so now.
228 	 */
229 	if (__predict_false(p->p_sflag & PS_STOPEXIT)) {
230 		KERNEL_UNLOCK_ALL(l, &l->l_biglocks);
231 		sigclearall(p, &contsigmask, &kq);
232 
233 		if (!mutex_tryenter(proc_lock)) {
234 			mutex_exit(p->p_lock);
235 			mutex_enter(proc_lock);
236 			mutex_enter(p->p_lock);
237 		}
238 		p->p_waited = 0;
239 		p->p_pptr->p_nstopchild++;
240 		p->p_stat = SSTOP;
241 		mutex_exit(proc_lock);
242 		lwp_lock(l);
243 		p->p_nrlwps--;
244 		l->l_stat = LSSTOP;
245 		lwp_unlock(l);
246 		mutex_exit(p->p_lock);
247 		lwp_lock(l);
248 		mi_switch(l);
249 		KERNEL_LOCK(l->l_biglocks, l);
250 		mutex_enter(p->p_lock);
251 	}
252 
253 	/*
254 	 * Bin any remaining signals and mark the process as dying so it will
255 	 * not be found for, e.g. signals.
256 	 */
257 	sigfillset(&p->p_sigctx.ps_sigignore);
258 	sigclearall(p, NULL, &kq);
259 	p->p_stat = SDYING;
260 	mutex_exit(p->p_lock);
261 	ksiginfo_queue_drain(&kq);
262 
263 	/* Destroy any lwpctl info. */
264 	if (p->p_lwpctl != NULL)
265 		lwp_ctl_exit();
266 
267 	/*
268 	 * Drain all remaining references that procfs, ptrace and others may
269 	 * have on the process.
270 	 */
271 	rw_enter(&p->p_reflock, RW_WRITER);
272 
273 	DPRINTF(("%s: %d.%d exiting.\n", __func__, p->p_pid, l->l_lid));
274 
275 	timers_free(p, TIMERS_ALL);
276 #if defined(__HAVE_RAS)
277 	ras_purgeall();
278 #endif
279 
280 	/*
281 	 * Close open files, release open-file table and free signal
282 	 * actions.  This may block!
283 	 */
284 	fd_free();
285 	cwdfree(p->p_cwdi);
286 	p->p_cwdi = NULL;
287 	doexithooks(p);
288 	sigactsfree(p->p_sigacts);
289 
290 	/*
291 	 * Write out accounting data.
292 	 */
293 	(void)acct_process(l);
294 
295 #ifdef KTRACE
296 	/*
297 	 * Release trace file.
298 	 */
299 	if (p->p_tracep != NULL) {
300 		mutex_enter(&ktrace_lock);
301 		ktrderef(p);
302 		mutex_exit(&ktrace_lock);
303 	}
304 #endif
305 
306 	p->p_xexit = exitcode;
307 	p->p_xsig = signo;
308 
309 	/*
310 	 * If emulation has process exit hook, call it now.
311 	 * Set the exit status now so that the exit hook has
312 	 * an opportunity to tweak it (COMPAT_LINUX requires
313 	 * this for thread group emulation)
314 	 */
315 	if (p->p_emul->e_proc_exit)
316 		(*p->p_emul->e_proc_exit)(p);
317 
318 	/*
319 	 * Free the VM resources we're still holding on to.
320 	 * We must do this from a valid thread because doing
321 	 * so may block. This frees vmspace, which we don't
322 	 * need anymore. The only remaining lwp is the one
323 	 * we run at this moment, nothing runs in userland
324 	 * anymore.
325 	 */
326 	ruspace(p);	/* Update our vm resource use */
327 	uvm_proc_exit(p);
328 
329 	/*
330 	 * Stop profiling.
331 	 */
332 	if (__predict_false((p->p_stflag & PST_PROFIL) != 0)) {
333 		mutex_spin_enter(&p->p_stmutex);
334 		stopprofclock(p);
335 		mutex_spin_exit(&p->p_stmutex);
336 	}
337 
338 	/*
339 	 * If parent is waiting for us to exit or exec, PL_PPWAIT is set; we
340 	 * wake up the parent early to avoid deadlock.  We can do this once
341 	 * the VM resources are released.
342 	 */
343 	mutex_enter(proc_lock);
344 	if (p->p_lflag & PL_PPWAIT) {
345 		lwp_t *lp;
346 
347 		l->l_lwpctl = NULL; /* was on loan from blocked parent */
348 		p->p_lflag &= ~PL_PPWAIT;
349 
350 		lp = p->p_vforklwp;
351 		p->p_vforklwp = NULL;
352 		lp->l_vforkwaiting = false;
353 		cv_broadcast(&lp->l_waitcv);
354 	}
355 
356 	if (SESS_LEADER(p)) {
357 		struct vnode *vprele = NULL, *vprevoke = NULL;
358 		struct session *sp = p->p_session;
359 		struct tty *tp;
360 
361 		if (sp->s_ttyvp) {
362 			/*
363 			 * Controlling process.
364 			 * Signal foreground pgrp,
365 			 * drain controlling terminal
366 			 * and revoke access to controlling terminal.
367 			 */
368 			tp = sp->s_ttyp;
369 			mutex_spin_enter(&tty_lock);
370 			if (tp->t_session == sp) {
371 				/* we can't guarantee the revoke will do this */
372 				pgrp = tp->t_pgrp;
373 				tp->t_pgrp = NULL;
374 				tp->t_session = NULL;
375 				mutex_spin_exit(&tty_lock);
376 				if (pgrp != NULL) {
377 					pgsignal(pgrp, SIGHUP, 1);
378 				}
379 				mutex_exit(proc_lock);
380 				(void) ttywait(tp);
381 				mutex_enter(proc_lock);
382 
383 				/* The tty could have been revoked. */
384 				vprevoke = sp->s_ttyvp;
385 			} else
386 				mutex_spin_exit(&tty_lock);
387 			vprele = sp->s_ttyvp;
388 			sp->s_ttyvp = NULL;
389 			/*
390 			 * s_ttyp is not zero'd; we use this to indicate
391 			 * that the session once had a controlling terminal.
392 			 * (for logging and informational purposes)
393 			 */
394 		}
395 		sp->s_leader = NULL;
396 
397 		if (vprevoke != NULL || vprele != NULL) {
398 			if (vprevoke != NULL) {
399 				/* Releases proc_lock. */
400 				proc_sessrele(sp);
401 				VOP_REVOKE(vprevoke, REVOKEALL);
402 			} else
403 				mutex_exit(proc_lock);
404 			if (vprele != NULL)
405 				vrele(vprele);
406 			mutex_enter(proc_lock);
407 		}
408 	}
409 	fixjobc(p, p->p_pgrp, 0);
410 
411 	/* Release fstrans private data. */
412 	fstrans_lwp_dtor(l);
413 
414 	/*
415 	 * Finalize the last LWP's specificdata, as well as the
416 	 * specificdata for the proc itself.
417 	 */
418 	lwp_finispecific(l);
419 	proc_finispecific(p);
420 
421 	/*
422 	 * Notify interested parties of our demise.
423 	 */
424 	KNOTE(&p->p_klist, NOTE_EXIT);
425 
426 	SDT_PROBE(proc, kernel, , exit,
427 		((p->p_sflag & PS_COREDUMP) ? CLD_DUMPED :
428 		 (p->p_xsig ? CLD_KILLED : CLD_EXITED)),
429 		0,0,0,0);
430 
431 	/*
432 	 * Reset p_opptr pointer of all former children which got
433 	 * traced by another process and were reparented. We reset
434 	 * it to NULL here; the trace detach code then reparents
435 	 * the child to initproc. We only check allproc list, since
436 	 * eventual former children on zombproc list won't reference
437 	 * p_opptr anymore.
438 	 */
439 	if (__predict_false(p->p_slflag & PSL_CHTRACED)) {
440 		struct proc *q;
441 		PROCLIST_FOREACH(q, &allproc) {
442 			if (q->p_opptr == p)
443 				q->p_opptr = NULL;
444 		}
445 		PROCLIST_FOREACH(q, &zombproc) {
446 			if (q->p_opptr == p)
447 				q->p_opptr = NULL;
448 		}
449 	}
450 
451 	/*
452 	 * Give orphaned children to init(8).
453 	 */
454 	child = LIST_FIRST(&p->p_children);
455 	wakeinit = (child != NULL);
456 	for (; child != NULL; child = next_child) {
457 		next_child = LIST_NEXT(child, p_sibling);
458 
459 		/*
460 		 * Traced processes are killed since their existence
461 		 * means someone is screwing up. Since we reset the
462 		 * trace flags, the logic in sys_wait4() would not be
463 		 * triggered to reparent the process to its
464 		 * original parent, so we must do this here.
465 		 */
466 		if (__predict_false(child->p_slflag & PSL_TRACED)) {
467 			mutex_enter(p->p_lock);
468 			child->p_slflag &=
469 			    ~(PSL_TRACED|PSL_SYSCALL);
470 			mutex_exit(p->p_lock);
471 			if (child->p_opptr != child->p_pptr) {
472 				struct proc *t = child->p_opptr;
473 				proc_reparent(child, t ? t : initproc);
474 				child->p_opptr = NULL;
475 			} else
476 				proc_reparent(child, initproc);
477 			killproc(child, "orphaned traced process");
478 		} else
479 			proc_reparent(child, initproc);
480 	}
481 
482 	/*
483 	 * Move proc from allproc to zombproc, it's now nearly ready to be
484 	 * collected by parent.
485 	 */
486 	LIST_REMOVE(l, l_list);
487 	LIST_REMOVE(p, p_list);
488 	LIST_INSERT_HEAD(&zombproc, p, p_list);
489 
490 	/*
491 	 * Mark the process as dead.  We must do this before we signal
492 	 * the parent.
493 	 */
494 	p->p_stat = SDEAD;
495 
496 	/* Put in front of parent's sibling list for parent to collect it */
497 	old_parent = p->p_pptr;
498 	old_parent->p_nstopchild++;
499 	if (LIST_FIRST(&old_parent->p_children) != p) {
500 		/* Put child where it can be found quickly */
501 		LIST_REMOVE(p, p_sibling);
502 		LIST_INSERT_HEAD(&old_parent->p_children, p, p_sibling);
503 	}
504 
505 	/*
506 	 * Notify parent that we're gone.  If parent has the P_NOCLDWAIT
507 	 * flag set, notify init instead (and hope it will handle
508 	 * this situation).
509 	 */
510 	if (old_parent->p_flag & (PK_NOCLDWAIT|PK_CLDSIGIGN)) {
511 		proc_reparent(p, initproc);
512 		wakeinit = 1;
513 
514 		/*
515 		 * If this was the last child of our parent, notify
516 		 * parent, so in case he was wait(2)ing, he will
517 		 * continue.
518 		 */
519 		if (LIST_FIRST(&old_parent->p_children) == NULL)
520 			cv_broadcast(&old_parent->p_waitcv);
521 	}
522 
523 	/* Reload parent pointer, since p may have been reparented above */
524 	new_parent = p->p_pptr;
525 
526 	if (__predict_false(p->p_exitsig != 0)) {
527 		exit_psignal(p, new_parent, &ksi);
528 		kpsignal(new_parent, &ksi, NULL);
529 	}
530 
531 	/* Calculate the final rusage info.  */
532 	calcru(p, &p->p_stats->p_ru.ru_utime, &p->p_stats->p_ru.ru_stime,
533 	    NULL, NULL);
534 
535 	if (wakeinit)
536 		cv_broadcast(&initproc->p_waitcv);
537 
538 	callout_destroy(&l->l_timeout_ch);
539 
540 	/*
541 	 * Release any PCU resources before becoming a zombie.
542 	 */
543 	pcu_discard_all(l);
544 
545 	mutex_enter(p->p_lock);
546 	/* Free the linux lwp id */
547 	if ((l->l_pflag & LP_PIDLID) != 0 && l->l_lid != p->p_pid)
548 		proc_free_pid(l->l_lid);
549 	lwp_drainrefs(l);
550 	lwp_lock(l);
551 	l->l_prflag &= ~LPR_DETACHED;
552 	l->l_stat = LSZOMB;
553 	lwp_unlock(l);
554 	KASSERT(curlwp == l);
555 	KASSERT(p->p_nrlwps == 1);
556 	KASSERT(p->p_nlwps == 1);
557 	p->p_stat = SZOMB;
558 	p->p_nrlwps--;
559 	p->p_nzlwps++;
560 	p->p_ndlwps = 0;
561 	mutex_exit(p->p_lock);
562 
563 	/*
564 	 * Signal the parent to collect us, and drop the proclist lock.
565 	 * Drop debugger/procfs lock; no new references can be gained.
566 	 */
567 	cv_broadcast(&p->p_pptr->p_waitcv);
568 	rw_exit(&p->p_reflock);
569 	mutex_exit(proc_lock);
570 
571 	/* Verify that we hold no locks other than the kernel lock. */
572 	LOCKDEBUG_BARRIER(&kernel_lock, 0);
573 
574 	/*
575 	 * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP!
576 	 */
577 
578 	/*
579 	 * Give machine-dependent code a chance to free any MD LWP
580 	 * resources.  This must be done before uvm_lwp_exit(), in
581 	 * case these resources are in the PCB.
582 	 */
583 	cpu_lwp_free(l, 1);
584 
585 	pmap_deactivate(l);
586 
587 	/* This process no longer needs to hold the kernel lock. */
588 #ifdef notyet
589 	/* XXXSMP hold in lwp_userret() */
590 	KERNEL_UNLOCK_LAST(l);
591 #else
592 	KERNEL_UNLOCK_ALL(l, NULL);
593 #endif
594 
595 	lwp_exit_switchaway(l);
596 }
597 
598 void
599 exit_lwps(struct lwp *l)
600 {
601 	proc_t *p = l->l_proc;
602 	lwp_t *l2;
603 	int nlocks;
604 
605 	KERNEL_UNLOCK_ALL(l, &nlocks);
606 retry:
607 	KASSERT(mutex_owned(p->p_lock));
608 
609 	/*
610 	 * Interrupt LWPs in interruptable sleep, unsuspend suspended
611 	 * LWPs and then wait for everyone else to finish.
612 	 */
613 	LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
614 		if (l2 == l)
615 			continue;
616 		lwp_lock(l2);
617 		l2->l_flag |= LW_WEXIT;
618 		if ((l2->l_stat == LSSLEEP && (l2->l_flag & LW_SINTR)) ||
619 		    l2->l_stat == LSSUSPENDED || l2->l_stat == LSSTOP) {
620 			l2->l_flag &= ~LW_DBGSUSPEND;
621 		    	/* setrunnable() will release the lock. */
622 			setrunnable(l2);
623 			continue;
624 		}
625 		lwp_unlock(l2);
626 	}
627 
628 	/*
629 	 * Wait for every LWP to exit.  Note: LWPs can get suspended/slept
630 	 * behind us or there may even be new LWPs created.  Therefore, a
631 	 * full retry is required on error.
632 	 */
633 	while (p->p_nlwps > 1) {
634 		if (lwp_wait(l, 0, NULL, true)) {
635 			goto retry;
636 		}
637 	}
638 
639 	KERNEL_LOCK(nlocks, l);
640 	KASSERT(p->p_nlwps == 1);
641 }
642 
643 int
644 do_sys_waitid(idtype_t idtype, id_t id, int *pid, int *status, int options,
645     struct wrusage *wru, siginfo_t *si)
646 {
647 	proc_t *child;
648 	int error;
649 
650 
651 	if (wru != NULL)
652 		memset(wru, 0, sizeof(*wru));
653 	if (si != NULL)
654 		memset(si, 0, sizeof(*si));
655 
656 	mutex_enter(proc_lock);
657 	error = find_stopped_child(curproc, idtype, id, options, &child,
658 	    wru, si);
659 	if (child == NULL) {
660 		mutex_exit(proc_lock);
661 		*pid = 0;
662 		*status = 0;
663 		return error;
664 	}
665 	*pid = child->p_pid;
666 
667 	if (child->p_stat == SZOMB) {
668 		/* Child is exiting */
669 		*status = P_WAITSTATUS(child);
670 		/* proc_free() will release the proc_lock. */
671 		if (options & WNOWAIT) {
672 			mutex_exit(proc_lock);
673 		} else {
674 			proc_free(child, wru);
675 		}
676 	} else {
677 		/* Don't mark SIGCONT if we are being stopped */
678 		*status = (child->p_xsig == SIGCONT && child->p_stat != SSTOP) ?
679 		    W_CONTCODE() : W_STOPCODE(child->p_xsig);
680 		mutex_exit(proc_lock);
681 	}
682 	return 0;
683 }
684 
685 int
686 do_sys_wait(int *pid, int *status, int options, struct rusage *ru)
687 {
688 	idtype_t idtype;
689 	id_t id;
690 	int ret;
691 	struct wrusage wru;
692 
693 	/*
694 	 * Translate the special pid values into the (idtype, pid)
695 	 * pair for wait6. The WAIT_MYPGRP case is handled by
696 	 * find_stopped_child() on its own.
697 	 */
698 	if (*pid == WAIT_ANY) {
699 		idtype = P_ALL;
700 		id = 0;
701 	} else if (*pid < 0) {
702 		idtype = P_PGID;
703 		id = (id_t)-*pid;
704 	} else {
705 		idtype = P_PID;
706 		id = (id_t)*pid;
707 	}
708 	options |= WEXITED | WTRAPPED;
709 	ret = do_sys_waitid(idtype, id, pid, status, options, ru ? &wru : NULL,
710 	    NULL);
711 	if (ru)
712 		*ru = wru.wru_self;
713 	return ret;
714 }
715 
716 int
717 sys___wait450(struct lwp *l, const struct sys___wait450_args *uap,
718     register_t *retval)
719 {
720 	/* {
721 		syscallarg(int)			pid;
722 		syscallarg(int *)		status;
723 		syscallarg(int)			options;
724 		syscallarg(struct rusage *)	rusage;
725 	} */
726 	int error, status, pid = SCARG(uap, pid);
727 	struct rusage ru;
728 
729 	error = do_sys_wait(&pid, &status, SCARG(uap, options),
730 	    SCARG(uap, rusage) != NULL ? &ru : NULL);
731 
732 	retval[0] = pid;
733 	if (pid == 0) {
734 		return error;
735 	}
736 	if (SCARG(uap, status)) {
737 		error = copyout(&status, SCARG(uap, status), sizeof(status));
738 	}
739 	if (SCARG(uap, rusage) && error == 0) {
740 		error = copyout(&ru, SCARG(uap, rusage), sizeof(ru));
741 	}
742 	return error;
743 }
744 
745 int
746 sys_wait6(struct lwp *l, const struct sys_wait6_args *uap, register_t *retval)
747 {
748 	/* {
749 		syscallarg(idtype_t)		idtype;
750 		syscallarg(id_t)		id;
751 		syscallarg(int *)		status;
752 		syscallarg(int)			options;
753 		syscallarg(struct wrusage *)	wru;
754 		syscallarg(siginfo_t *)		si;
755 	} */
756 	struct wrusage wru, *wrup;
757 	siginfo_t si, *sip;
758 	idtype_t idtype;
759 	int pid;
760 	id_t id;
761 	int error, status;
762 
763 	idtype = SCARG(uap, idtype);
764 	id = SCARG(uap, id);
765 
766 	if (SCARG(uap, wru) != NULL)
767 		wrup = &wru;
768 	else
769 		wrup = NULL;
770 
771 	if (SCARG(uap, info) != NULL)
772 		sip = &si;
773 	else
774 		sip = NULL;
775 
776 	/*
777 	 *  We expect all callers of wait6() to know about WEXITED and
778 	 *  WTRAPPED.
779 	 */
780 	error = do_sys_waitid(idtype, id, &pid, &status, SCARG(uap, options),
781 	    wrup, sip);
782 
783 	retval[0] = pid; 	/* tell userland who it was */
784 
785 #if 0
786 	/*
787 	 * should we copyout if there was no process, hence no useful data?
788 	 * We don't for an old sytle wait4() (etc) but I believe
789 	 * FreeBSD does for wait6(), so a tossup...  Go with FreeBSD for now.
790 	 */
791 	if (pid == 0)
792 		return error;
793 #endif
794 
795 	if (SCARG(uap, status) != NULL && error == 0)
796 		error = copyout(&status, SCARG(uap, status), sizeof(status));
797 	if (SCARG(uap, wru) != NULL && error == 0)
798 		error = copyout(&wru, SCARG(uap, wru), sizeof(wru));
799 	if (SCARG(uap, info) != NULL && error == 0)
800 		error = copyout(&si, SCARG(uap, info), sizeof(si));
801 	return error;
802 }
803 
804 
805 /*
806  * Find a process that matches the provided criteria, and fill siginfo
807  * and resources if found.
808  * Returns:
809  *	-1: 	Not found, abort early
810  *	 0:	Not matched
811  *	 1:	Matched, there might be more matches
812  *	 2:	This is the only match
813  */
814 static int
815 match_process(const struct proc *pp, struct proc **q, idtype_t idtype, id_t id,
816     int options, struct wrusage *wrusage, siginfo_t *siginfo)
817 {
818 	struct rusage *rup;
819 	struct proc *p = *q;
820 	int rv = 1;
821 
822 	mutex_enter(p->p_lock);
823 	switch (idtype) {
824 	case P_ALL:
825 		break;
826 	case P_PID:
827 		if (p->p_pid != (pid_t)id) {
828 			mutex_exit(p->p_lock);
829 			p = *q = proc_find_raw((pid_t)id);
830 			if (p == NULL || p->p_stat == SIDL || p->p_pptr != pp) {
831 				*q = NULL;
832 				return -1;
833 			}
834 			mutex_enter(p->p_lock);
835 		}
836 		rv++;
837 		break;
838 	case P_PGID:
839 		if (p->p_pgid != (pid_t)id)
840 			goto out;
841 		break;
842 	case P_SID:
843 		if (p->p_session->s_sid != (pid_t)id)
844 			goto out;
845 		break;
846 	case P_UID:
847 		if (kauth_cred_geteuid(p->p_cred) != (uid_t)id)
848 			goto out;
849 		break;
850 	case P_GID:
851 		if (kauth_cred_getegid(p->p_cred) != (gid_t)id)
852 			goto out;
853 		break;
854 	case P_CID:
855 	case P_PSETID:
856 	case P_CPUID:
857 		/* XXX: Implement me */
858 	default:
859 	out:
860 		mutex_exit(p->p_lock);
861 		return 0;
862 	}
863 
864 	if ((options & WEXITED) == 0 && p->p_stat == SZOMB)
865 		goto out;
866 
867 	if (siginfo != NULL) {
868 		siginfo->si_errno = 0;
869 
870 		/*
871 		 * SUSv4 requires that the si_signo value is always
872 		 * SIGCHLD. Obey it despite the rfork(2) interface
873 		 * allows to request other signal for child exit
874 		 * notification.
875 		 */
876 		siginfo->si_signo = SIGCHLD;
877 
878 		/*
879 		 *  This is still a rough estimate.  We will fix the
880 		 *  cases TRAPPED, STOPPED, and CONTINUED later.
881 		 */
882 		if (p->p_sflag & PS_COREDUMP) {
883 			siginfo->si_code = CLD_DUMPED;
884 			siginfo->si_status = p->p_xsig;
885 		} else if (p->p_xsig) {
886 			siginfo->si_code = CLD_KILLED;
887 			siginfo->si_status = p->p_xsig;
888 		} else {
889 			siginfo->si_code = CLD_EXITED;
890 			siginfo->si_status = p->p_xexit;
891 		}
892 
893 		siginfo->si_pid = p->p_pid;
894 		siginfo->si_uid = kauth_cred_geteuid(p->p_cred);
895 		siginfo->si_utime = p->p_stats->p_ru.ru_utime.tv_sec;
896 		siginfo->si_stime = p->p_stats->p_ru.ru_stime.tv_sec;
897 	}
898 
899 	/*
900 	 * There should be no reason to limit resources usage info to
901 	 * exited processes only.  A snapshot about any resources used
902 	 * by a stopped process may be exactly what is needed.
903 	 */
904 	if (wrusage != NULL) {
905 		rup = &wrusage->wru_self;
906 		*rup = p->p_stats->p_ru;
907 		calcru(p, &rup->ru_utime, &rup->ru_stime, NULL, NULL);
908 
909 		rup = &wrusage->wru_children;
910 		*rup = p->p_stats->p_cru;
911 		calcru(p, &rup->ru_utime, &rup->ru_stime, NULL, NULL);
912 	}
913 
914 	mutex_exit(p->p_lock);
915 	return rv;
916 }
917 
918 /*
919  * Determine if there are existing processes being debugged
920  * that used to be (and sometime later will be again) children
921  * of a specific parent (while matching wait criteria)
922  */
923 static bool
924 debugged_child_exists(idtype_t idtype, id_t id, int options, siginfo_t *si,
925     const struct proc *parent)
926 {
927 	struct proc *pp;
928 
929 	/*
930 	 * If we are searching for a specific pid, we can optimise a little
931 	 */
932 	if (idtype == P_PID) {
933 		/*
934 		 * Check the specific process to see if its real parent is us
935 		 */
936 		pp = proc_find_raw((pid_t)id);
937 		if (pp != NULL && pp->p_stat != SIDL && pp->p_opptr == parent) {
938 			/*
939 			 * using P_ALL here avoids match_process() doing the
940 			 * same work that we just did, but incorrectly for
941 			 * this scenario.
942 			 */
943 			if (match_process(parent, &pp, P_ALL, id, options,
944 			    NULL, si))
945 				return true;
946 		}
947 		return false;
948 	}
949 
950 	/*
951 	 * For the hard cases, just look everywhere to see if some
952 	 * stolen (reparented) process is really our lost child.
953 	 * Then check if that process could satisfy the wait conditions.
954 	 */
955 
956 	/*
957 	 * XXX inefficient, but hopefully fairly rare.
958 	 * XXX should really use a list of reparented processes.
959 	 */
960 	PROCLIST_FOREACH(pp, &allproc) {
961 		if (pp->p_stat == SIDL)		/* XXX impossible ?? */
962 			continue;
963 		if (pp->p_opptr == parent &&
964 		    match_process(parent, &pp, idtype, id, options, NULL, si))
965 			return true;
966 	}
967 	PROCLIST_FOREACH(pp, &zombproc) {
968 		if (pp->p_stat == SIDL)		/* XXX impossible ?? */
969 			continue;
970 		if (pp->p_opptr == parent &&
971 		    match_process(parent, &pp, idtype, id, options, NULL, si))
972 			return true;
973 	}
974 
975 	return false;
976 }
977 
978 /*
979  * Scan list of child processes for a child process that has stopped or
980  * exited.  Used by sys_wait4 and 'compat' equivalents.
981  *
982  * Must be called with the proc_lock held, and may release while waiting.
983  */
984 static int
985 find_stopped_child(struct proc *parent, idtype_t idtype, id_t id, int options,
986     struct proc **child_p, struct wrusage *wru, siginfo_t *si)
987 {
988 	struct proc *child, *dead;
989 	int error;
990 
991 	KASSERT(mutex_owned(proc_lock));
992 
993 	if (options & ~WALLOPTS) {
994 		*child_p = NULL;
995 		return EINVAL;
996 	}
997 
998 	if ((options & WSELECTOPTS) == 0) {
999 		/*
1000 		 * We will be unable to find any matching processes,
1001 		 * because there are no known events to look for.
1002 		 * Prefer to return error instead of blocking
1003 		 * indefinitely.
1004 		 */
1005 		*child_p = NULL;
1006 		return EINVAL;
1007 	}
1008 
1009 	if ((pid_t)id == WAIT_MYPGRP && (idtype == P_PID || idtype == P_PGID)) {
1010 		mutex_enter(parent->p_lock);
1011 		id = (id_t)parent->p_pgid;
1012 		mutex_exit(parent->p_lock);
1013 		idtype = P_PGID;
1014 	}
1015 
1016 	for (;;) {
1017 		error = ECHILD;
1018 		dead = NULL;
1019 
1020 		LIST_FOREACH(child, &parent->p_children, p_sibling) {
1021 			int rv = match_process(parent, &child, idtype, id,
1022 			    options, wru, si);
1023 			if (rv == -1)
1024 				break;
1025 			if (rv == 0)
1026 				continue;
1027 
1028 			/*
1029 			 * Wait for processes with p_exitsig != SIGCHLD
1030 			 * processes only if WALTSIG is set; wait for
1031 			 * processes with p_exitsig == SIGCHLD only
1032 			 * if WALTSIG is clear.
1033 			 */
1034 			if (((options & WALLSIG) == 0) &&
1035 			    (options & WALTSIG ? child->p_exitsig == SIGCHLD
1036 						: P_EXITSIG(child) != SIGCHLD)){
1037 				if (rv == 2) {
1038 					child = NULL;
1039 					break;
1040 				}
1041 				continue;
1042 			}
1043 
1044 			error = 0;
1045 			if ((options & WNOZOMBIE) == 0) {
1046 				if (child->p_stat == SZOMB)
1047 					break;
1048 				if (child->p_stat == SDEAD) {
1049 					/*
1050 					 * We may occasionally arrive here
1051 					 * after receiving a signal, but
1052 					 * immediately before the child
1053 					 * process is zombified.  The wait
1054 					 * will be short, so avoid returning
1055 					 * to userspace.
1056 					 */
1057 					dead = child;
1058 				}
1059 			}
1060 
1061 			if ((options & WCONTINUED) != 0 &&
1062 			    child->p_xsig == SIGCONT &&
1063 			    (child->p_sflag & PS_CONTINUED)) {
1064 				if ((options & WNOWAIT) == 0) {
1065 					child->p_sflag &= ~PS_CONTINUED;
1066 					child->p_waited = 1;
1067 					parent->p_nstopchild--;
1068 				}
1069 				if (si) {
1070 					si->si_status = child->p_xsig;
1071 					si->si_code = CLD_CONTINUED;
1072 				}
1073 				break;
1074 			}
1075 
1076 			if ((options & (WTRAPPED|WSTOPPED)) != 0 &&
1077 			    child->p_stat == SSTOP &&
1078 			    child->p_waited == 0 &&
1079 			    ((child->p_slflag & PSL_TRACED) ||
1080 			    options & (WUNTRACED|WSTOPPED))) {
1081 				if ((options & WNOWAIT) == 0) {
1082 					child->p_waited = 1;
1083 					parent->p_nstopchild--;
1084 				}
1085 				if (si) {
1086 					si->si_status = child->p_xsig;
1087 					si->si_code =
1088 					    (child->p_slflag & PSL_TRACED) ?
1089 					    CLD_TRAPPED : CLD_STOPPED;
1090 				}
1091 				break;
1092 			}
1093 			if (parent->p_nstopchild == 0 || rv == 2) {
1094 				child = NULL;
1095 				break;
1096 			}
1097 		}
1098 
1099 		/*
1100 		 * If we found nothing, but we are the bereaved parent
1101 		 * of a stolen child, look and see if that child (or
1102 		 * one of them) meets our search criteria.   If so, then
1103 		 * we cannot succeed, but we can hang (wait...),
1104 		 * or if WNOHANG, return 0 instead of ECHILD
1105 		 */
1106 		if (child == NULL && error == ECHILD &&
1107 		    (parent->p_slflag & PSL_CHTRACED) &&
1108 		    debugged_child_exists(idtype, id, options, si, parent))
1109 			error = 0;
1110 
1111 		if (child != NULL || error != 0 ||
1112 		    ((options & WNOHANG) != 0 && dead == NULL)) {
1113 			*child_p = child;
1114 			return error;
1115 		}
1116 
1117 		/*
1118 		 * Wait for another child process to stop.
1119 		 */
1120 		error = cv_wait_sig(&parent->p_waitcv, proc_lock);
1121 
1122 		if (error != 0) {
1123 			*child_p = NULL;
1124 			return error;
1125 		}
1126 	}
1127 }
1128 
1129 /*
1130  * Free a process after parent has taken all the state info.  Must be called
1131  * with the proclist lock held, and will release before returning.
1132  *
1133  * *ru is returned to the caller, and must be freed by the caller.
1134  */
1135 static void
1136 proc_free(struct proc *p, struct wrusage *wru)
1137 {
1138 	struct proc *parent = p->p_pptr;
1139 	struct lwp *l;
1140 	ksiginfo_t ksi;
1141 	kauth_cred_t cred1, cred2;
1142 	uid_t uid;
1143 
1144 	KASSERT(mutex_owned(proc_lock));
1145 	KASSERT(p->p_nlwps == 1);
1146 	KASSERT(p->p_nzlwps == 1);
1147 	KASSERT(p->p_nrlwps == 0);
1148 	KASSERT(p->p_stat == SZOMB);
1149 
1150 	/*
1151 	 * If we got the child via ptrace(2) or procfs, and
1152 	 * the parent is different (meaning the process was
1153 	 * attached, rather than run as a child), then we need
1154 	 * to give it back to the old parent, and send the
1155 	 * parent the exit signal.  The rest of the cleanup
1156 	 * will be done when the old parent waits on the child.
1157 	 */
1158 	if ((p->p_slflag & PSL_TRACED) != 0 && p->p_opptr != parent) {
1159 		mutex_enter(p->p_lock);
1160 		p->p_slflag &= ~(PSL_TRACED|PSL_SYSCALL);
1161 		mutex_exit(p->p_lock);
1162 		parent = (p->p_opptr == NULL) ? initproc : p->p_opptr;
1163 		proc_reparent(p, parent);
1164 		p->p_opptr = NULL;
1165 		if (p->p_exitsig != 0) {
1166 			exit_psignal(p, parent, &ksi);
1167 			kpsignal(parent, &ksi, NULL);
1168 		}
1169 		cv_broadcast(&parent->p_waitcv);
1170 		mutex_exit(proc_lock);
1171 		return;
1172 	}
1173 
1174 	sched_proc_exit(parent, p);
1175 
1176 	/*
1177 	 * Add child times of exiting process onto its own times.
1178 	 * This cannot be done any earlier else it might get done twice.
1179 	 */
1180 	l = LIST_FIRST(&p->p_lwps);
1181 	p->p_stats->p_ru.ru_nvcsw += (l->l_ncsw - l->l_nivcsw);
1182 	p->p_stats->p_ru.ru_nivcsw += l->l_nivcsw;
1183 	ruadd(&p->p_stats->p_ru, &l->l_ru);
1184 	ruadd(&p->p_stats->p_ru, &p->p_stats->p_cru);
1185 	ruadd(&parent->p_stats->p_cru, &p->p_stats->p_ru);
1186 	if (wru != NULL) {
1187 		wru->wru_self = p->p_stats->p_ru;
1188 		wru->wru_children = p->p_stats->p_cru;
1189 	}
1190 	p->p_xsig = 0;
1191 	p->p_xexit = 0;
1192 
1193 	/*
1194 	 * At this point we are going to start freeing the final resources.
1195 	 * If anyone tries to access the proc structure after here they will
1196 	 * get a shock - bits are missing.  Attempt to make it hard!  We
1197 	 * don't bother with any further locking past this point.
1198 	 */
1199 	p->p_stat = SIDL;		/* not even a zombie any more */
1200 	LIST_REMOVE(p, p_list);	/* off zombproc */
1201 	parent->p_nstopchild--;
1202 	LIST_REMOVE(p, p_sibling);
1203 
1204 	/*
1205 	 * Let pid be reallocated.
1206 	 */
1207 	proc_free_pid(p->p_pid);
1208 
1209 	/*
1210 	 * Unlink process from its process group.
1211 	 * Releases the proc_lock.
1212 	 */
1213 	proc_leavepgrp(p);
1214 
1215 	/*
1216 	 * Delay release until after lwp_free.
1217 	 */
1218 	cred2 = l->l_cred;
1219 
1220 	/*
1221 	 * Free the last LWP's resources.
1222 	 *
1223 	 * lwp_free ensures the LWP is no longer running on another CPU.
1224 	 */
1225 	lwp_free(l, false, true);
1226 
1227 	/*
1228 	 * Now no one except us can reach the process p.
1229 	 */
1230 
1231 	/*
1232 	 * Decrement the count of procs running with this uid.
1233 	 */
1234 	cred1 = p->p_cred;
1235 	uid = kauth_cred_getuid(cred1);
1236 	(void)chgproccnt(uid, -1);
1237 
1238 	/*
1239 	 * Release substructures.
1240 	 */
1241 
1242 	lim_free(p->p_limit);
1243 	pstatsfree(p->p_stats);
1244 	kauth_cred_free(cred1);
1245 	kauth_cred_free(cred2);
1246 
1247 	/*
1248 	 * Release reference to text vnode
1249 	 */
1250 	if (p->p_textvp)
1251 		vrele(p->p_textvp);
1252 	kmem_strfree(p->p_path);
1253 
1254 	mutex_destroy(&p->p_auxlock);
1255 	mutex_obj_free(p->p_lock);
1256 	mutex_destroy(&p->p_stmutex);
1257 	cv_destroy(&p->p_waitcv);
1258 	cv_destroy(&p->p_lwpcv);
1259 	rw_destroy(&p->p_reflock);
1260 
1261 	proc_free_mem(p);
1262 }
1263 
1264 /*
1265  * Change the parent of a process for tracing purposes.
1266  */
1267 void
1268 proc_changeparent(struct proc *t, struct proc *p)
1269 {
1270 	SET(t->p_slflag, PSL_TRACED);
1271 	t->p_opptr = t->p_pptr;
1272 	if (t->p_pptr == p)
1273 		return;
1274 	struct proc *parent = t->p_pptr;
1275 
1276 	if (parent->p_lock < t->p_lock) {
1277 		if (!mutex_tryenter(parent->p_lock)) {
1278 			mutex_exit(t->p_lock);
1279 			mutex_enter(parent->p_lock);
1280 			mutex_enter(t->p_lock);
1281 		}
1282 	} else if (parent->p_lock > t->p_lock) {
1283 		mutex_enter(parent->p_lock);
1284 	}
1285 	parent->p_slflag |= PSL_CHTRACED;
1286 	proc_reparent(t, p);
1287 	if (parent->p_lock != t->p_lock)
1288 		mutex_exit(parent->p_lock);
1289 }
1290 
1291 /*
1292  * make process 'parent' the new parent of process 'child'.
1293  *
1294  * Must be called with proc_lock held.
1295  */
1296 void
1297 proc_reparent(struct proc *child, struct proc *parent)
1298 {
1299 
1300 	KASSERT(mutex_owned(proc_lock));
1301 
1302 	if (child->p_pptr == parent)
1303 		return;
1304 
1305 	if (child->p_stat == SZOMB || child->p_stat == SDEAD ||
1306 	    (child->p_stat == SSTOP && !child->p_waited)) {
1307 		child->p_pptr->p_nstopchild--;
1308 		parent->p_nstopchild++;
1309 	}
1310 	if (parent == initproc) {
1311 		child->p_exitsig = SIGCHLD;
1312 		child->p_ppid = parent->p_pid;
1313 	}
1314 
1315 	LIST_REMOVE(child, p_sibling);
1316 	LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
1317 	child->p_pptr = parent;
1318 }
1319