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