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