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