xref: /openbsd-src/sys/kern/kern_exit.c (revision d0fc3bb68efd6c434b4053cd7adb29023cbec341)
1 /*	$OpenBSD: kern_exit.c,v 1.199 2021/03/12 10:13:28 mpi 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 != 0; 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 		/* notify interested parties of our demise and clean up */
322 		knote_processexit(p);
323 
324 		/*
325 		 * Notify parent that we're gone.  If we're not going to
326 		 * become a zombie, reparent to process 1 (init) so that
327 		 * we can wake our original parent to possibly unblock
328 		 * wait4() to return ECHILD.
329 		 */
330 		if (pr->ps_flags & PS_NOZOMBIE) {
331 			struct process *ppr = pr->ps_pptr;
332 			process_reparent(pr, initprocess);
333 			wakeup(ppr);
334 		}
335 
336 		/*
337 		 * Release the process's signal state.
338 		 */
339 		sigactsfree(pr);
340 	}
341 
342 	/* just a thread? detach it from its process */
343 	if (p->p_flag & P_THREAD) {
344 		/* scheduler_wait_hook(pr->ps_mainproc, p); XXX */
345 		if (--pr->ps_refcnt == 1)
346 			wakeup(&pr->ps_threads);
347 		KASSERT(pr->ps_refcnt > 0);
348 	}
349 
350 	/* Release the thread's read reference of resource limit structure. */
351 	if (p->p_limit != NULL) {
352 		struct plimit *limit;
353 
354 		limit = p->p_limit;
355 		p->p_limit = NULL;
356 		lim_free(limit);
357 	}
358 
359 	/*
360 	 * Other substructures are freed from reaper and wait().
361 	 */
362 
363 	/*
364 	 * Finally, call machine-dependent code to switch to a new
365 	 * context (possibly the idle context).  Once we are no longer
366 	 * using the dead process's vmspace and stack, exit2() will be
367 	 * called to schedule those resources to be released by the
368 	 * reaper thread.
369 	 *
370 	 * Note that cpu_exit() will end with a call equivalent to
371 	 * cpu_switch(), finishing our execution (pun intended).
372 	 */
373 	uvmexp.swtch++;
374 	cpu_exit(p);
375 	panic("cpu_exit returned");
376 }
377 
378 /*
379  * Locking of this proclist is special; it's accessed in a
380  * critical section of process exit, and thus locking it can't
381  * modify interrupt state.  We use a simple spin lock for this
382  * proclist.  We use the p_hash member to linkup to deadproc.
383  */
384 struct mutex deadproc_mutex =
385     MUTEX_INITIALIZER_FLAGS(IPL_NONE, "deadproc", MTX_NOWITNESS);
386 struct proclist deadproc = LIST_HEAD_INITIALIZER(deadproc);
387 
388 /*
389  * We are called from cpu_exit() once it is safe to schedule the
390  * dead process's resources to be freed.
391  *
392  * NOTE: One must be careful with locking in this routine.  It's
393  * called from a critical section in machine-dependent code, so
394  * we should refrain from changing any interrupt state.
395  *
396  * We lock the deadproc list, place the proc on that list (using
397  * the p_hash member), and wake up the reaper.
398  */
399 void
400 exit2(struct proc *p)
401 {
402 	mtx_enter(&deadproc_mutex);
403 	LIST_INSERT_HEAD(&deadproc, p, p_hash);
404 	mtx_leave(&deadproc_mutex);
405 
406 	wakeup(&deadproc);
407 }
408 
409 void
410 proc_free(struct proc *p)
411 {
412 	crfree(p->p_ucred);
413 	pool_put(&proc_pool, p);
414 	nthreads--;
415 }
416 
417 /*
418  * Process reaper.  This is run by a kernel thread to free the resources
419  * of a dead process.  Once the resources are free, the process becomes
420  * a zombie, and the parent is allowed to read the undead's status.
421  */
422 void
423 reaper(void *arg)
424 {
425 	struct proc *p;
426 
427 	KERNEL_UNLOCK();
428 
429 	SCHED_ASSERT_UNLOCKED();
430 
431 	for (;;) {
432 		mtx_enter(&deadproc_mutex);
433 		while ((p = LIST_FIRST(&deadproc)) == NULL)
434 			msleep_nsec(&deadproc, &deadproc_mutex, PVM, "reaper",
435 			    INFSLP);
436 
437 		/* Remove us from the deadproc list. */
438 		LIST_REMOVE(p, p_hash);
439 		mtx_leave(&deadproc_mutex);
440 
441 		WITNESS_THREAD_EXIT(p);
442 
443 		KERNEL_LOCK();
444 
445 		/*
446 		 * Free the VM resources we're still holding on to.
447 		 * We must do this from a valid thread because doing
448 		 * so may block.
449 		 */
450 		uvm_uarea_free(p);
451 		p->p_vmspace = NULL;		/* zap the thread's copy */
452 
453 		if (p->p_flag & P_THREAD) {
454 			/* Just a thread */
455 			proc_free(p);
456 		} else {
457 			struct process *pr = p->p_p;
458 
459 			/* Release the rest of the process's vmspace */
460 			uvm_exit(pr);
461 
462 			if ((pr->ps_flags & PS_NOZOMBIE) == 0) {
463 				/* Process is now a true zombie. */
464 				atomic_setbits_int(&pr->ps_flags, PS_ZOMBIE);
465 				prsignal(pr->ps_pptr, SIGCHLD);
466 
467 				/* Wake up the parent so it can get exit status. */
468 				wakeup(pr->ps_pptr);
469 			} else {
470 				/* No one will wait for us. Just zap the process now */
471 				process_zap(pr);
472 			}
473 		}
474 
475 		KERNEL_UNLOCK();
476 	}
477 }
478 
479 int
480 sys_wait4(struct proc *q, void *v, register_t *retval)
481 {
482 	struct sys_wait4_args /* {
483 		syscallarg(pid_t) pid;
484 		syscallarg(int *) status;
485 		syscallarg(int) options;
486 		syscallarg(struct rusage *) rusage;
487 	} */ *uap = v;
488 	struct rusage ru;
489 	int status, error;
490 
491 	error = dowait4(q, SCARG(uap, pid),
492 	    SCARG(uap, status) ? &status : NULL,
493 	    SCARG(uap, options), SCARG(uap, rusage) ? &ru : NULL, retval);
494 	if (error == 0 && retval[0] > 0 && SCARG(uap, status)) {
495 		error = copyout(&status, SCARG(uap, status), sizeof(status));
496 	}
497 	if (error == 0 && retval[0] > 0 && SCARG(uap, rusage)) {
498 		error = copyout(&ru, SCARG(uap, rusage), sizeof(ru));
499 #ifdef KTRACE
500 		if (error == 0 && KTRPOINT(q, KTR_STRUCT))
501 			ktrrusage(q, &ru);
502 #endif
503 	}
504 	return (error);
505 }
506 
507 int
508 dowait4(struct proc *q, pid_t pid, int *statusp, int options,
509     struct rusage *rusage, register_t *retval)
510 {
511 	int nfound;
512 	struct process *pr;
513 	struct proc *p;
514 	int error;
515 
516 	if (pid == 0)
517 		pid = -q->p_p->ps_pgid;
518 	if (options &~ (WUNTRACED|WNOHANG|WCONTINUED))
519 		return (EINVAL);
520 
521 loop:
522 	nfound = 0;
523 	LIST_FOREACH(pr, &q->p_p->ps_children, ps_sibling) {
524 		if ((pr->ps_flags & PS_NOZOMBIE) ||
525 		    (pid != WAIT_ANY &&
526 		    pr->ps_pid != pid &&
527 		    pr->ps_pgid != -pid))
528 			continue;
529 
530 		p = pr->ps_mainproc;
531 
532 		nfound++;
533 		if (pr->ps_flags & PS_ZOMBIE) {
534 			retval[0] = pr->ps_pid;
535 
536 			if (statusp != NULL)
537 				*statusp = W_EXITCODE(pr->ps_xexit,
538 				    pr->ps_xsig);
539 			if (rusage != NULL)
540 				memcpy(rusage, pr->ps_ru, sizeof(*rusage));
541 			proc_finish_wait(q, p);
542 			return (0);
543 		}
544 		if (pr->ps_flags & PS_TRACED &&
545 		    (pr->ps_flags & PS_WAITED) == 0 && pr->ps_single &&
546 		    pr->ps_single->p_stat == SSTOP &&
547 		    (pr->ps_single->p_flag & P_SUSPSINGLE) == 0) {
548 			if (single_thread_wait(pr, 0))
549 				goto loop;
550 
551 			atomic_setbits_int(&pr->ps_flags, PS_WAITED);
552 			retval[0] = pr->ps_pid;
553 
554 			if (statusp != NULL)
555 				*statusp = W_STOPCODE(pr->ps_xsig);
556 			if (rusage != NULL)
557 				memset(rusage, 0, sizeof(*rusage));
558 			return (0);
559 		}
560 		if (p->p_stat == SSTOP &&
561 		    (pr->ps_flags & PS_WAITED) == 0 &&
562 		    (p->p_flag & P_SUSPSINGLE) == 0 &&
563 		    (pr->ps_flags & PS_TRACED ||
564 		    options & WUNTRACED)) {
565 			atomic_setbits_int(&pr->ps_flags, PS_WAITED);
566 			retval[0] = pr->ps_pid;
567 
568 			if (statusp != NULL)
569 				*statusp = W_STOPCODE(pr->ps_xsig);
570 			if (rusage != NULL)
571 				memset(rusage, 0, sizeof(*rusage));
572 			return (0);
573 		}
574 		if ((options & WCONTINUED) && (p->p_flag & P_CONTINUED)) {
575 			atomic_clearbits_int(&p->p_flag, P_CONTINUED);
576 			retval[0] = pr->ps_pid;
577 
578 			if (statusp != NULL)
579 				*statusp = _WCONTINUED;
580 			if (rusage != NULL)
581 				memset(rusage, 0, sizeof(*rusage));
582 			return (0);
583 		}
584 	}
585 	/*
586 	 * Look in the orphans list too, to allow the parent to
587 	 * collect it's child exit status even if child is being
588 	 * debugged.
589 	 *
590 	 * Debugger detaches from the parent upon successful
591 	 * switch-over from parent to child.  At this point due to
592 	 * re-parenting the parent loses the child to debugger and a
593 	 * wait4(2) call would report that it has no children to wait
594 	 * for.  By maintaining a list of orphans we allow the parent
595 	 * to successfully wait until the child becomes a zombie.
596 	 */
597 	if (nfound == 0) {
598 		LIST_FOREACH(pr, &q->p_p->ps_orphans, ps_orphan) {
599 			if ((pr->ps_flags & PS_NOZOMBIE) ||
600 			    (pid != WAIT_ANY &&
601 			    pr->ps_pid != pid &&
602 			    pr->ps_pgid != -pid))
603 				continue;
604 			nfound++;
605 			break;
606 		}
607 	}
608 	if (nfound == 0)
609 		return (ECHILD);
610 	if (options & WNOHANG) {
611 		retval[0] = 0;
612 		return (0);
613 	}
614 	if ((error = tsleep_nsec(q->p_p, PWAIT | PCATCH, "wait", INFSLP)) != 0)
615 		return (error);
616 	goto loop;
617 }
618 
619 void
620 proc_finish_wait(struct proc *waiter, struct proc *p)
621 {
622 	struct process *pr, *tr;
623 	struct rusage *rup;
624 
625 	/*
626 	 * If we got the child via a ptrace 'attach',
627 	 * we need to give it back to the old parent.
628 	 */
629 	pr = p->p_p;
630 	if (pr->ps_oppid != 0 && (pr->ps_oppid != pr->ps_pptr->ps_pid) &&
631 	   (tr = prfind(pr->ps_oppid))) {
632 		pr->ps_oppid = 0;
633 		atomic_clearbits_int(&pr->ps_flags, PS_TRACED);
634 		process_reparent(pr, tr);
635 		prsignal(tr, SIGCHLD);
636 		wakeup(tr);
637 	} else {
638 		scheduler_wait_hook(waiter, p);
639 		rup = &waiter->p_p->ps_cru;
640 		ruadd(rup, pr->ps_ru);
641 		LIST_REMOVE(pr, ps_list);	/* off zombprocess */
642 		freepid(pr->ps_pid);
643 		process_zap(pr);
644 	}
645 }
646 
647 /*
648  * give process back to original parent or init(8)
649  */
650 void
651 process_untrace(struct process *pr)
652 {
653 	struct process *ppr = NULL;
654 
655 	KASSERT(pr->ps_flags & PS_TRACED);
656 
657 	if (pr->ps_oppid != 0 &&
658 	    (pr->ps_oppid != pr->ps_pptr->ps_pid))
659 		ppr = prfind(pr->ps_oppid);
660 
661 	/* not being traced any more */
662 	pr->ps_oppid = 0;
663 	atomic_clearbits_int(&pr->ps_flags, PS_TRACED);
664 	process_reparent(pr, ppr ? ppr : initprocess);
665 }
666 
667 void
668 process_clear_orphan(struct process *pr)
669 {
670 	if (pr->ps_flags & PS_ORPHAN) {
671 		LIST_REMOVE(pr, ps_orphan);
672 		atomic_clearbits_int(&pr->ps_flags, PS_ORPHAN);
673 	}
674 }
675 
676 /*
677  * make process 'parent' the new parent of process 'child'.
678  */
679 void
680 process_reparent(struct process *child, struct process *parent)
681 {
682 
683 	if (child->ps_pptr == parent)
684 		return;
685 
686 	KASSERT(child->ps_oppid == 0 ||
687 		child->ps_oppid == child->ps_pptr->ps_pid);
688 
689 	LIST_REMOVE(child, ps_sibling);
690 	LIST_INSERT_HEAD(&parent->ps_children, child, ps_sibling);
691 
692 	process_clear_orphan(child);
693 	if (child->ps_flags & PS_TRACED) {
694 		atomic_setbits_int(&child->ps_flags, PS_ORPHAN);
695 		LIST_INSERT_HEAD(&child->ps_pptr->ps_orphans, child, ps_orphan);
696 	}
697 
698 	child->ps_pptr = parent;
699 	child->ps_ppid = parent->ps_pid;
700 }
701 
702 void
703 process_zap(struct process *pr)
704 {
705 	struct vnode *otvp;
706 	struct proc *p = pr->ps_mainproc;
707 
708 	/*
709 	 * Finally finished with old proc entry.
710 	 * Unlink it from its process group and free it.
711 	 */
712 	leavepgrp(pr);
713 	LIST_REMOVE(pr, ps_sibling);
714 	process_clear_orphan(pr);
715 
716 	/*
717 	 * Decrement the count of procs running with this uid.
718 	 */
719 	(void)chgproccnt(pr->ps_ucred->cr_ruid, -1);
720 
721 	/*
722 	 * Release reference to text vnode
723 	 */
724 	otvp = pr->ps_textvp;
725 	pr->ps_textvp = NULL;
726 	if (otvp)
727 		vrele(otvp);
728 
729 	KASSERT(pr->ps_refcnt == 1);
730 	if (pr->ps_ptstat != NULL)
731 		free(pr->ps_ptstat, M_SUBPROC, sizeof(*pr->ps_ptstat));
732 	pool_put(&rusage_pool, pr->ps_ru);
733 	KASSERT(TAILQ_EMPTY(&pr->ps_threads));
734 	lim_free(pr->ps_limit);
735 	crfree(pr->ps_ucred);
736 	pool_put(&process_pool, pr);
737 	nprocesses--;
738 
739 	proc_free(p);
740 }
741