xref: /openbsd-src/sys/kern/kern_exit.c (revision f84b1df5a16cdd762c93854218de246e79975d3b)
1 /*	$OpenBSD: kern_exit.c,v 1.203 2022/03/31 01:41:22 millert Exp $	*/
2 /*	$NetBSD: kern_exit.c,v 1.39 1996/04/22 01:38:25 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1986, 1989, 1991, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  * (c) UNIX System Laboratories, Inc.
8  * All or some portions of this file are derived from material licensed
9  * to the University of California by American Telephone and Telegraph
10  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11  * the permission of UNIX System Laboratories, Inc.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	@(#)kern_exit.c	8.7 (Berkeley) 2/12/94
38  */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/ioctl.h>
43 #include <sys/proc.h>
44 #include <sys/tty.h>
45 #include <sys/time.h>
46 #include <sys/resource.h>
47 #include <sys/kernel.h>
48 #include <sys/sysctl.h>
49 #include <sys/wait.h>
50 #include <sys/vnode.h>
51 #include <sys/syslog.h>
52 #include <sys/malloc.h>
53 #include <sys/resourcevar.h>
54 #include <sys/ptrace.h>
55 #include <sys/acct.h>
56 #include <sys/filedesc.h>
57 #include <sys/signalvar.h>
58 #include <sys/sched.h>
59 #include <sys/ktrace.h>
60 #include <sys/pool.h>
61 #include <sys/mutex.h>
62 #include <sys/pledge.h>
63 #ifdef SYSVSEM
64 #include <sys/sem.h>
65 #endif
66 #include <sys/witness.h>
67 
68 #include <sys/mount.h>
69 #include <sys/syscallargs.h>
70 
71 #include <uvm/uvm_extern.h>
72 
73 #include "kcov.h"
74 #if NKCOV > 0
75 #include <sys/kcov.h>
76 #endif
77 
78 void	proc_finish_wait(struct proc *, struct proc *);
79 void	process_clear_orphan(struct process *);
80 void	process_zap(struct process *);
81 void	proc_free(struct proc *);
82 void	unveil_destroy(struct process *ps);
83 
84 /*
85  * exit --
86  *	Death of process.
87  */
88 int
89 sys_exit(struct proc *p, void *v, register_t *retval)
90 {
91 	struct sys_exit_args /* {
92 		syscallarg(int) rval;
93 	} */ *uap = v;
94 
95 	exit1(p, SCARG(uap, rval), 0, EXIT_NORMAL);
96 	/* NOTREACHED */
97 	return (0);
98 }
99 
100 int
101 sys___threxit(struct proc *p, void *v, register_t *retval)
102 {
103 	struct sys___threxit_args /* {
104 		syscallarg(pid_t *) notdead;
105 	} */ *uap = v;
106 
107 	if (SCARG(uap, notdead) != NULL) {
108 		pid_t zero = 0;
109 		if (copyout(&zero, SCARG(uap, notdead), sizeof(zero)))
110 			psignal(p, SIGSEGV);
111 	}
112 	exit1(p, 0, 0, EXIT_THREAD);
113 
114 	return (0);
115 }
116 
117 /*
118  * Exit: deallocate address space and other resources, change proc state
119  * to zombie, and unlink proc from allproc and parent's lists.  Save exit
120  * status and rusage for wait().  Check for child processes and orphan them.
121  */
122 void
123 exit1(struct proc *p, int xexit, int xsig, int flags)
124 {
125 	struct process *pr, *qr, *nqr;
126 	struct rusage *rup;
127 	int s;
128 
129 	atomic_setbits_int(&p->p_flag, P_WEXIT);
130 
131 	pr = p->p_p;
132 
133 	/* single-threaded? */
134 	if (!P_HASSIBLING(p)) {
135 		flags = EXIT_NORMAL;
136 	} else {
137 		/* nope, multi-threaded */
138 		if (flags == EXIT_NORMAL)
139 			single_thread_set(p, SINGLE_EXIT, 1);
140 		else if (flags == EXIT_THREAD)
141 			single_thread_check(p, 0);
142 	}
143 
144 	if (flags == EXIT_NORMAL && !(pr->ps_flags & PS_EXITING)) {
145 		if (pr->ps_pid == 1)
146 			panic("init died (signal %d, exit %d)", xsig, xexit);
147 
148 		atomic_setbits_int(&pr->ps_flags, PS_EXITING);
149 		pr->ps_xexit = xexit;
150 		pr->ps_xsig  = xsig;
151 
152 		/*
153 		 * If parent is waiting for us to exit or exec, PS_PPWAIT
154 		 * is set; we wake up the parent early to avoid deadlock.
155 		 */
156 		if (pr->ps_flags & PS_PPWAIT) {
157 			atomic_clearbits_int(&pr->ps_flags, PS_PPWAIT);
158 			atomic_clearbits_int(&pr->ps_pptr->ps_flags,
159 			    PS_ISPWAIT);
160 			wakeup(pr->ps_pptr);
161 		}
162 	}
163 
164 	/* unlink ourselves from the active threads */
165 	SCHED_LOCK(s);
166 	TAILQ_REMOVE(&pr->ps_threads, p, p_thr_link);
167 	SCHED_UNLOCK(s);
168 	if ((p->p_flag & P_THREAD) == 0) {
169 		/* main thread gotta wait because it has the pid, et al */
170 		while (pr->ps_refcnt > 1)
171 			tsleep_nsec(&pr->ps_threads, PWAIT, "thrdeath", INFSLP);
172 		if (pr->ps_flags & PS_PROFIL)
173 			stopprofclock(pr);
174 	}
175 
176 	rup = pr->ps_ru;
177 	if (rup == NULL) {
178 		rup = pool_get(&rusage_pool, PR_WAITOK | PR_ZERO);
179 		if (pr->ps_ru == NULL) {
180 			pr->ps_ru = rup;
181 		} else {
182 			pool_put(&rusage_pool, rup);
183 			rup = pr->ps_ru;
184 		}
185 	}
186 	p->p_siglist = 0;
187 	if ((p->p_flag & P_THREAD) == 0)
188 		pr->ps_siglist = 0;
189 
190 	kqpoll_exit();
191 
192 #if NKCOV > 0
193 	kcov_exit(p);
194 #endif
195 
196 	if ((p->p_flag & P_THREAD) == 0) {
197 		sigio_freelist(&pr->ps_sigiolst);
198 
199 		/* close open files and release open-file table */
200 		fdfree(p);
201 
202 		cancel_all_itimers();
203 
204 		timeout_del(&pr->ps_rucheck_to);
205 #ifdef SYSVSEM
206 		semexit(pr);
207 #endif
208 		killjobc(pr);
209 #ifdef ACCOUNTING
210 		acct_process(p);
211 #endif
212 
213 #ifdef KTRACE
214 		/* release trace file */
215 		if (pr->ps_tracevp)
216 			ktrcleartrace(pr);
217 #endif
218 
219 		unveil_destroy(pr);
220 
221 		/*
222 		 * If parent has the SAS_NOCLDWAIT flag set, we're not
223 		 * going to become a zombie.
224 		 */
225 		if (pr->ps_pptr->ps_sigacts->ps_sigflags & SAS_NOCLDWAIT)
226 			atomic_setbits_int(&pr->ps_flags, PS_NOZOMBIE);
227 	}
228 
229 	p->p_fd = NULL;		/* zap the thread's copy */
230 
231         /*
232 	 * Remove proc from pidhash chain and allproc so looking
233 	 * it up won't work.  We will put the proc on the
234 	 * deadproc list later (using the p_hash member), and
235 	 * wake up the reaper when we do.  If this is the last
236 	 * thread of a process that isn't PS_NOZOMBIE, we'll put
237 	 * the process on the zombprocess list below.
238 	 */
239 	/*
240 	 * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP!
241 	 */
242 	p->p_stat = SDEAD;
243 
244 	LIST_REMOVE(p, p_hash);
245 	LIST_REMOVE(p, p_list);
246 
247 	if ((p->p_flag & P_THREAD) == 0) {
248 		LIST_REMOVE(pr, ps_hash);
249 		LIST_REMOVE(pr, ps_list);
250 
251 		if ((pr->ps_flags & PS_NOZOMBIE) == 0)
252 			LIST_INSERT_HEAD(&zombprocess, pr, ps_list);
253 		else {
254 			/*
255 			 * Not going to be a zombie, so it's now off all
256 			 * the lists scanned by ispidtaken(), so block
257 			 * fast reuse of the pid now.
258 			 */
259 			freepid(pr->ps_pid);
260 		}
261 
262 		/*
263 		 * Reparent children to their original parent, in case
264 		 * they were being traced, or to init(8).
265 		 */
266 		qr = LIST_FIRST(&pr->ps_children);
267 		if (qr)		/* only need this if any child is S_ZOMB */
268 			wakeup(initprocess);
269 		for (; qr != NULL; qr = nqr) {
270 			nqr = LIST_NEXT(qr, ps_sibling);
271 			/*
272 			 * Traced processes are killed since their
273 			 * existence means someone is screwing up.
274 			 */
275 			if (qr->ps_flags & PS_TRACED &&
276 			    !(qr->ps_flags & PS_EXITING)) {
277 				process_untrace(qr);
278 
279 				/*
280 				 * If single threading is active,
281 				 * direct the signal to the active
282 				 * thread to avoid deadlock.
283 				 */
284 				if (qr->ps_single)
285 					ptsignal(qr->ps_single, SIGKILL,
286 					    STHREAD);
287 				else
288 					prsignal(qr, SIGKILL);
289 			} else {
290 				process_reparent(qr, initprocess);
291 			}
292 		}
293 
294 		/*
295 		 * Make sure orphans won't remember the exiting process.
296 		 */
297 		while ((qr = LIST_FIRST(&pr->ps_orphans)) != NULL) {
298 			KASSERT(qr->ps_oppid == pr->ps_pid);
299 			qr->ps_oppid = 0;
300 			process_clear_orphan(qr);
301 		}
302 	}
303 
304 	/* add thread's accumulated rusage into the process's total */
305 	ruadd(rup, &p->p_ru);
306 	tuagg(pr, p);
307 
308 	/*
309 	 * clear %cpu usage during swap
310 	 */
311 	p->p_pctcpu = 0;
312 
313 	if ((p->p_flag & P_THREAD) == 0) {
314 		/*
315 		 * Final thread has died, so add on our children's rusage
316 		 * and calculate the total times
317 		 */
318 		calcru(&pr->ps_tu, &rup->ru_utime, &rup->ru_stime, NULL);
319 		ruadd(rup, &pr->ps_cru);
320 
321 		/*
322 		 * Notify parent that we're gone.  If we're not going to
323 		 * become a zombie, reparent to process 1 (init) so that
324 		 * we can wake our original parent to possibly unblock
325 		 * wait4() to return ECHILD.
326 		 */
327 		if (pr->ps_flags & PS_NOZOMBIE) {
328 			struct process *ppr = pr->ps_pptr;
329 			process_reparent(pr, initprocess);
330 			wakeup(ppr);
331 		}
332 	}
333 
334 	/* just a thread? detach it from its process */
335 	if (p->p_flag & P_THREAD) {
336 		/* scheduler_wait_hook(pr->ps_mainproc, p); XXX */
337 		if (--pr->ps_refcnt == 1)
338 			wakeup(&pr->ps_threads);
339 		KASSERT(pr->ps_refcnt > 0);
340 	}
341 
342 	/* Release the thread's read reference of resource limit structure. */
343 	if (p->p_limit != NULL) {
344 		struct plimit *limit;
345 
346 		limit = p->p_limit;
347 		p->p_limit = NULL;
348 		lim_free(limit);
349 	}
350 
351 	/*
352 	 * Other substructures are freed from reaper and wait().
353 	 */
354 
355 	/*
356 	 * Finally, call machine-dependent code to switch to a new
357 	 * context (possibly the idle context).  Once we are no longer
358 	 * using the dead process's vmspace and stack, exit2() will be
359 	 * called to schedule those resources to be released by the
360 	 * reaper thread.
361 	 *
362 	 * Note that cpu_exit() will end with a call equivalent to
363 	 * cpu_switch(), finishing our execution (pun intended).
364 	 */
365 	uvmexp.swtch++;
366 	cpu_exit(p);
367 	panic("cpu_exit returned");
368 }
369 
370 /*
371  * Locking of this proclist is special; it's accessed in a
372  * critical section of process exit, and thus locking it can't
373  * modify interrupt state.  We use a simple spin lock for this
374  * proclist.  We use the p_hash member to linkup to deadproc.
375  */
376 struct mutex deadproc_mutex =
377     MUTEX_INITIALIZER_FLAGS(IPL_NONE, "deadproc", MTX_NOWITNESS);
378 struct proclist deadproc = LIST_HEAD_INITIALIZER(deadproc);
379 
380 /*
381  * We are called from cpu_exit() once it is safe to schedule the
382  * dead process's resources to be freed.
383  *
384  * NOTE: One must be careful with locking in this routine.  It's
385  * called from a critical section in machine-dependent code, so
386  * we should refrain from changing any interrupt state.
387  *
388  * We lock the deadproc list, place the proc on that list (using
389  * the p_hash member), and wake up the reaper.
390  */
391 void
392 exit2(struct proc *p)
393 {
394 	mtx_enter(&deadproc_mutex);
395 	LIST_INSERT_HEAD(&deadproc, p, p_hash);
396 	mtx_leave(&deadproc_mutex);
397 
398 	wakeup(&deadproc);
399 }
400 
401 void
402 proc_free(struct proc *p)
403 {
404 	crfree(p->p_ucred);
405 	pool_put(&proc_pool, p);
406 	nthreads--;
407 }
408 
409 /*
410  * Process reaper.  This is run by a kernel thread to free the resources
411  * of a dead process.  Once the resources are free, the process becomes
412  * a zombie, and the parent is allowed to read the undead's status.
413  */
414 void
415 reaper(void *arg)
416 {
417 	struct proc *p;
418 
419 	KERNEL_UNLOCK();
420 
421 	SCHED_ASSERT_UNLOCKED();
422 
423 	for (;;) {
424 		mtx_enter(&deadproc_mutex);
425 		while ((p = LIST_FIRST(&deadproc)) == NULL)
426 			msleep_nsec(&deadproc, &deadproc_mutex, PVM, "reaper",
427 			    INFSLP);
428 
429 		/* Remove us from the deadproc list. */
430 		LIST_REMOVE(p, p_hash);
431 		mtx_leave(&deadproc_mutex);
432 
433 		WITNESS_THREAD_EXIT(p);
434 
435 		KERNEL_LOCK();
436 
437 		/*
438 		 * Free the VM resources we're still holding on to.
439 		 * We must do this from a valid thread because doing
440 		 * so may block.
441 		 */
442 		uvm_uarea_free(p);
443 		p->p_vmspace = NULL;		/* zap the thread's copy */
444 
445 		if (p->p_flag & P_THREAD) {
446 			/* Just a thread */
447 			proc_free(p);
448 		} else {
449 			struct process *pr = p->p_p;
450 
451 			/* Release the rest of the process's vmspace */
452 			uvm_exit(pr);
453 
454 			if ((pr->ps_flags & PS_NOZOMBIE) == 0) {
455 				/* Process is now a true zombie. */
456 				atomic_setbits_int(&pr->ps_flags, PS_ZOMBIE);
457 			}
458 
459 			/* Notify listeners of our demise and clean up. */
460 			knote_processexit(pr);
461 
462 			if (pr->ps_flags & PS_ZOMBIE) {
463 				/* Post SIGCHLD and wake up parent. */
464 				prsignal(pr->ps_pptr, SIGCHLD);
465 				wakeup(pr->ps_pptr);
466 			} else {
467 				/* No one will wait for us, just zap it. */
468 				process_zap(pr);
469 			}
470 		}
471 
472 		KERNEL_UNLOCK();
473 	}
474 }
475 
476 int
477 sys_wait4(struct proc *q, void *v, register_t *retval)
478 {
479 	struct sys_wait4_args /* {
480 		syscallarg(pid_t) pid;
481 		syscallarg(int *) status;
482 		syscallarg(int) options;
483 		syscallarg(struct rusage *) rusage;
484 	} */ *uap = v;
485 	struct rusage ru;
486 	int status, error;
487 
488 	error = dowait4(q, SCARG(uap, pid),
489 	    SCARG(uap, status) ? &status : NULL,
490 	    SCARG(uap, options), SCARG(uap, rusage) ? &ru : NULL, retval);
491 	if (error == 0 && retval[0] > 0 && SCARG(uap, status)) {
492 		error = copyout(&status, SCARG(uap, status), sizeof(status));
493 	}
494 	if (error == 0 && retval[0] > 0 && SCARG(uap, rusage)) {
495 		error = copyout(&ru, SCARG(uap, rusage), sizeof(ru));
496 #ifdef KTRACE
497 		if (error == 0 && KTRPOINT(q, KTR_STRUCT))
498 			ktrrusage(q, &ru);
499 #endif
500 	}
501 	return (error);
502 }
503 
504 int
505 dowait4(struct proc *q, pid_t pid, int *statusp, int options,
506     struct rusage *rusage, register_t *retval)
507 {
508 	int nfound;
509 	struct process *pr;
510 	struct proc *p;
511 	int error;
512 
513 	if (pid == 0)
514 		pid = -q->p_p->ps_pgid;
515 	if (options &~ (WUNTRACED|WNOHANG|WCONTINUED))
516 		return (EINVAL);
517 
518 loop:
519 	nfound = 0;
520 	LIST_FOREACH(pr, &q->p_p->ps_children, ps_sibling) {
521 		if ((pr->ps_flags & PS_NOZOMBIE) ||
522 		    (pid != WAIT_ANY &&
523 		    pr->ps_pid != pid &&
524 		    pr->ps_pgid != -pid))
525 			continue;
526 
527 		p = pr->ps_mainproc;
528 
529 		nfound++;
530 		if (pr->ps_flags & PS_ZOMBIE) {
531 			retval[0] = pr->ps_pid;
532 
533 			if (statusp != NULL)
534 				*statusp = W_EXITCODE(pr->ps_xexit,
535 				    pr->ps_xsig);
536 			if (rusage != NULL)
537 				memcpy(rusage, pr->ps_ru, sizeof(*rusage));
538 			proc_finish_wait(q, p);
539 			return (0);
540 		}
541 		if (pr->ps_flags & PS_TRACED &&
542 		    (pr->ps_flags & PS_WAITED) == 0 && pr->ps_single &&
543 		    pr->ps_single->p_stat == SSTOP &&
544 		    (pr->ps_single->p_flag & P_SUSPSINGLE) == 0) {
545 			if (single_thread_wait(pr, 0))
546 				goto loop;
547 
548 			atomic_setbits_int(&pr->ps_flags, PS_WAITED);
549 			retval[0] = pr->ps_pid;
550 
551 			if (statusp != NULL)
552 				*statusp = W_STOPCODE(pr->ps_xsig);
553 			if (rusage != NULL)
554 				memset(rusage, 0, sizeof(*rusage));
555 			return (0);
556 		}
557 		if (p->p_stat == SSTOP &&
558 		    (pr->ps_flags & PS_WAITED) == 0 &&
559 		    (p->p_flag & P_SUSPSINGLE) == 0 &&
560 		    (pr->ps_flags & PS_TRACED ||
561 		    options & WUNTRACED)) {
562 			atomic_setbits_int(&pr->ps_flags, PS_WAITED);
563 			retval[0] = pr->ps_pid;
564 
565 			if (statusp != NULL)
566 				*statusp = W_STOPCODE(pr->ps_xsig);
567 			if (rusage != NULL)
568 				memset(rusage, 0, sizeof(*rusage));
569 			return (0);
570 		}
571 		if ((options & WCONTINUED) && (p->p_flag & P_CONTINUED)) {
572 			atomic_clearbits_int(&p->p_flag, P_CONTINUED);
573 			retval[0] = pr->ps_pid;
574 
575 			if (statusp != NULL)
576 				*statusp = _WCONTINUED;
577 			if (rusage != NULL)
578 				memset(rusage, 0, sizeof(*rusage));
579 			return (0);
580 		}
581 	}
582 	/*
583 	 * Look in the orphans list too, to allow the parent to
584 	 * collect its child's exit status even if child is being
585 	 * debugged.
586 	 *
587 	 * Debugger detaches from the parent upon successful
588 	 * switch-over from parent to child.  At this point due to
589 	 * re-parenting the parent loses the child to debugger and a
590 	 * wait4(2) call would report that it has no children to wait
591 	 * for.  By maintaining a list of orphans we allow the parent
592 	 * to successfully wait until the child becomes a zombie.
593 	 */
594 	if (nfound == 0) {
595 		LIST_FOREACH(pr, &q->p_p->ps_orphans, ps_orphan) {
596 			if ((pr->ps_flags & PS_NOZOMBIE) ||
597 			    (pid != WAIT_ANY &&
598 			    pr->ps_pid != pid &&
599 			    pr->ps_pgid != -pid))
600 				continue;
601 			nfound++;
602 			break;
603 		}
604 	}
605 	if (nfound == 0)
606 		return (ECHILD);
607 	if (options & WNOHANG) {
608 		retval[0] = 0;
609 		return (0);
610 	}
611 	if ((error = tsleep_nsec(q->p_p, PWAIT | PCATCH, "wait", INFSLP)) != 0)
612 		return (error);
613 	goto loop;
614 }
615 
616 void
617 proc_finish_wait(struct proc *waiter, struct proc *p)
618 {
619 	struct process *pr, *tr;
620 	struct rusage *rup;
621 
622 	/*
623 	 * If we got the child via a ptrace 'attach',
624 	 * we need to give it back to the old parent.
625 	 */
626 	pr = p->p_p;
627 	if (pr->ps_oppid != 0 && (pr->ps_oppid != pr->ps_pptr->ps_pid) &&
628 	   (tr = prfind(pr->ps_oppid))) {
629 		pr->ps_oppid = 0;
630 		atomic_clearbits_int(&pr->ps_flags, PS_TRACED);
631 		process_reparent(pr, tr);
632 		prsignal(tr, SIGCHLD);
633 		wakeup(tr);
634 	} else {
635 		scheduler_wait_hook(waiter, p);
636 		rup = &waiter->p_p->ps_cru;
637 		ruadd(rup, pr->ps_ru);
638 		LIST_REMOVE(pr, ps_list);	/* off zombprocess */
639 		freepid(pr->ps_pid);
640 		process_zap(pr);
641 	}
642 }
643 
644 /*
645  * give process back to original parent or init(8)
646  */
647 void
648 process_untrace(struct process *pr)
649 {
650 	struct process *ppr = NULL;
651 
652 	KASSERT(pr->ps_flags & PS_TRACED);
653 
654 	if (pr->ps_oppid != 0 &&
655 	    (pr->ps_oppid != pr->ps_pptr->ps_pid))
656 		ppr = prfind(pr->ps_oppid);
657 
658 	/* not being traced any more */
659 	pr->ps_oppid = 0;
660 	atomic_clearbits_int(&pr->ps_flags, PS_TRACED);
661 	process_reparent(pr, ppr ? ppr : initprocess);
662 }
663 
664 void
665 process_clear_orphan(struct process *pr)
666 {
667 	if (pr->ps_flags & PS_ORPHAN) {
668 		LIST_REMOVE(pr, ps_orphan);
669 		atomic_clearbits_int(&pr->ps_flags, PS_ORPHAN);
670 	}
671 }
672 
673 /*
674  * make process 'parent' the new parent of process 'child'.
675  */
676 void
677 process_reparent(struct process *child, struct process *parent)
678 {
679 
680 	if (child->ps_pptr == parent)
681 		return;
682 
683 	KASSERT(child->ps_oppid == 0 ||
684 		child->ps_oppid == child->ps_pptr->ps_pid);
685 
686 	LIST_REMOVE(child, ps_sibling);
687 	LIST_INSERT_HEAD(&parent->ps_children, child, ps_sibling);
688 
689 	process_clear_orphan(child);
690 	if (child->ps_flags & PS_TRACED) {
691 		atomic_setbits_int(&child->ps_flags, PS_ORPHAN);
692 		LIST_INSERT_HEAD(&child->ps_pptr->ps_orphans, child, ps_orphan);
693 	}
694 
695 	child->ps_pptr = parent;
696 	child->ps_ppid = parent->ps_pid;
697 }
698 
699 void
700 process_zap(struct process *pr)
701 {
702 	struct vnode *otvp;
703 	struct proc *p = pr->ps_mainproc;
704 
705 	/*
706 	 * Finally finished with old proc entry.
707 	 * Unlink it from its process group and free it.
708 	 */
709 	leavepgrp(pr);
710 	LIST_REMOVE(pr, ps_sibling);
711 	process_clear_orphan(pr);
712 
713 	/*
714 	 * Decrement the count of procs running with this uid.
715 	 */
716 	(void)chgproccnt(pr->ps_ucred->cr_ruid, -1);
717 
718 	/*
719 	 * Release reference to text vnode
720 	 */
721 	otvp = pr->ps_textvp;
722 	pr->ps_textvp = NULL;
723 	if (otvp)
724 		vrele(otvp);
725 
726 	KASSERT(pr->ps_refcnt == 1);
727 	if (pr->ps_ptstat != NULL)
728 		free(pr->ps_ptstat, M_SUBPROC, sizeof(*pr->ps_ptstat));
729 	pool_put(&rusage_pool, pr->ps_ru);
730 	KASSERT(TAILQ_EMPTY(&pr->ps_threads));
731 	sigactsfree(pr->ps_sigacts);
732 	lim_free(pr->ps_limit);
733 	crfree(pr->ps_ucred);
734 	pool_put(&process_pool, pr);
735 	nprocesses--;
736 
737 	proc_free(p);
738 }
739