xref: /openbsd-src/sys/kern/kern_exit.c (revision b21c774f9611a2013048bb9dfc02468e6860dad1)
1 /*	$OpenBSD: kern_exit.c,v 1.192 2020/12/07 16:55: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/smr.h>
67 #include <sys/witness.h>
68 
69 #include <sys/mount.h>
70 #include <sys/syscallargs.h>
71 
72 #include <uvm/uvm_extern.h>
73 
74 #include "kcov.h"
75 #if NKCOV > 0
76 #include <sys/kcov.h>
77 #endif
78 
79 void	proc_finish_wait(struct proc *, struct proc *);
80 void	process_clear_orphan(struct process *);
81 void	process_zap(struct process *);
82 void	proc_free(struct proc *);
83 void	unveil_destroy(struct process *ps);
84 
85 /*
86  * exit --
87  *	Death of process.
88  */
89 int
90 sys_exit(struct proc *p, void *v, register_t *retval)
91 {
92 	struct sys_exit_args /* {
93 		syscallarg(int) rval;
94 	} */ *uap = v;
95 
96 	exit1(p, SCARG(uap, rval), 0, EXIT_NORMAL);
97 	/* NOTREACHED */
98 	return (0);
99 }
100 
101 int
102 sys___threxit(struct proc *p, void *v, register_t *retval)
103 {
104 	struct sys___threxit_args /* {
105 		syscallarg(pid_t *) notdead;
106 	} */ *uap = v;
107 
108 	if (SCARG(uap, notdead) != NULL) {
109 		pid_t zero = 0;
110 		if (copyout(&zero, SCARG(uap, notdead), sizeof(zero)))
111 			psignal(p, SIGSEGV);
112 	}
113 	exit1(p, 0, 0, EXIT_THREAD);
114 
115 	return (0);
116 }
117 
118 /*
119  * Exit: deallocate address space and other resources, change proc state
120  * to zombie, and unlink proc from allproc and parent's lists.  Save exit
121  * status and rusage for wait().  Check for child processes and orphan them.
122  */
123 void
124 exit1(struct proc *p, int xexit, int xsig, int flags)
125 {
126 	struct process *pr, *qr, *nqr;
127 	struct rusage *rup;
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, 0);
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 	SMR_TAILQ_REMOVE_LOCKED(&pr->ps_threads, p, p_thr_link);
166 	if ((p->p_flag & P_THREAD) == 0) {
167 		/* main thread gotta wait because it has the pid, et al */
168 		while (pr->ps_refcnt > 1)
169 			tsleep_nsec(&pr->ps_threads, PWAIT, "thrdeath", INFSLP);
170 		if (pr->ps_flags & PS_PROFIL)
171 			stopprofclock(pr);
172 	}
173 
174 	rup = pr->ps_ru;
175 	if (rup == NULL) {
176 		rup = pool_get(&rusage_pool, PR_WAITOK | PR_ZERO);
177 		if (pr->ps_ru == NULL) {
178 			pr->ps_ru = rup;
179 		} else {
180 			pool_put(&rusage_pool, rup);
181 			rup = pr->ps_ru;
182 		}
183 	}
184 	p->p_siglist = 0;
185 	if ((p->p_flag & P_THREAD) == 0)
186 		pr->ps_siglist = 0;
187 
188 #if NKCOV > 0
189 	kcov_exit(p);
190 #endif
191 
192 	if ((p->p_flag & P_THREAD) == 0) {
193 		sigio_freelist(&pr->ps_sigiolst);
194 
195 		/* close open files and release open-file table */
196 		fdfree(p);
197 
198 		cancel_all_itimers();
199 
200 		timeout_del(&pr->ps_rucheck_to);
201 #ifdef SYSVSEM
202 		semexit(pr);
203 #endif
204 		killjobc(pr);
205 #ifdef ACCOUNTING
206 		acct_process(p);
207 #endif
208 
209 #ifdef KTRACE
210 		/* release trace file */
211 		if (pr->ps_tracevp)
212 			ktrcleartrace(pr);
213 #endif
214 
215 		unveil_destroy(pr);
216 
217 		/*
218 		 * If parent has the SAS_NOCLDWAIT flag set, we're not
219 		 * going to become a zombie.
220 		 */
221 		if (pr->ps_pptr->ps_sigacts->ps_sigflags & SAS_NOCLDWAIT)
222 			atomic_setbits_int(&pr->ps_flags, PS_NOZOMBIE);
223 	}
224 
225 	p->p_fd = NULL;		/* zap the thread's copy */
226 
227         /*
228 	 * Remove proc from pidhash chain and allproc so looking
229 	 * it up won't work.  We will put the proc on the
230 	 * deadproc list later (using the p_hash member), and
231 	 * wake up the reaper when we do.  If this is the last
232 	 * thread of a process that isn't PS_NOZOMBIE, we'll put
233 	 * the process on the zombprocess list below.
234 	 */
235 	/*
236 	 * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP!
237 	 */
238 	p->p_stat = SDEAD;
239 
240 	LIST_REMOVE(p, p_hash);
241 	LIST_REMOVE(p, p_list);
242 
243 	if ((p->p_flag & P_THREAD) == 0) {
244 		LIST_REMOVE(pr, ps_hash);
245 		LIST_REMOVE(pr, ps_list);
246 
247 		if ((pr->ps_flags & PS_NOZOMBIE) == 0)
248 			LIST_INSERT_HEAD(&zombprocess, pr, ps_list);
249 		else {
250 			/*
251 			 * Not going to be a zombie, so it's now off all
252 			 * the lists scanned by ispidtaken(), so block
253 			 * fast reuse of the pid now.
254 			 */
255 			freepid(pr->ps_pid);
256 		}
257 
258 		/*
259 		 * Reparent children to their original parent, in case
260 		 * they were being traced, or to init(8).
261 		 */
262 		qr = LIST_FIRST(&pr->ps_children);
263 		if (qr)		/* only need this if any child is S_ZOMB */
264 			wakeup(initprocess);
265 		for (; qr != 0; qr = nqr) {
266 			nqr = LIST_NEXT(qr, ps_sibling);
267 			/*
268 			 * Traced processes are killed since their
269 			 * existence means someone is screwing up.
270 			 */
271 			if (qr->ps_flags & PS_TRACED &&
272 			    !(qr->ps_flags & PS_EXITING)) {
273 				process_untrace(qr);
274 
275 				/*
276 				 * If single threading is active,
277 				 * direct the signal to the active
278 				 * thread to avoid deadlock.
279 				 */
280 				if (qr->ps_single)
281 					ptsignal(qr->ps_single, SIGKILL,
282 					    STHREAD);
283 				else
284 					prsignal(qr, SIGKILL);
285 			} else {
286 				process_reparent(qr, initprocess);
287 			}
288 		}
289 
290 		/*
291 		 * Make sure orphans won't remember the exiting process.
292 		 */
293 		while ((qr = LIST_FIRST(&pr->ps_orphans)) != NULL) {
294 			KASSERT(qr->ps_oppid == pr->ps_pid);
295 			qr->ps_oppid = 0;
296 			process_clear_orphan(qr);
297 		}
298 	}
299 
300 	/* add thread's accumulated rusage into the process's total */
301 	ruadd(rup, &p->p_ru);
302 	tuagg(pr, p);
303 
304 	/*
305 	 * clear %cpu usage during swap
306 	 */
307 	p->p_pctcpu = 0;
308 
309 	if ((p->p_flag & P_THREAD) == 0) {
310 		/*
311 		 * Final thread has died, so add on our children's rusage
312 		 * and calculate the total times
313 		 */
314 		calcru(&pr->ps_tu, &rup->ru_utime, &rup->ru_stime, NULL);
315 		ruadd(rup, &pr->ps_cru);
316 
317 		/* notify interested parties of our demise and clean up */
318 		knote_processexit(p);
319 
320 		/*
321 		 * Notify parent that we're gone.  If we're not going to
322 		 * become a zombie, reparent to process 1 (init) so that
323 		 * we can wake our original parent to possibly unblock
324 		 * wait4() to return ECHILD.
325 		 */
326 		if (pr->ps_flags & PS_NOZOMBIE) {
327 			struct process *ppr = pr->ps_pptr;
328 			process_reparent(pr, initprocess);
329 			wakeup(ppr);
330 		}
331 
332 		/*
333 		 * Release the process's signal state.
334 		 */
335 		sigactsfree(pr);
336 	}
337 
338 	/* just a thread? detach it from its process */
339 	if (p->p_flag & P_THREAD) {
340 		/* scheduler_wait_hook(pr->ps_mainproc, p); XXX */
341 		if (--pr->ps_refcnt == 1)
342 			wakeup(&pr->ps_threads);
343 		KASSERT(pr->ps_refcnt > 0);
344 	}
345 
346 	/* Release the thread's read reference of resource limit structure. */
347 	if (p->p_limit != NULL) {
348 		struct plimit *limit;
349 
350 		limit = p->p_limit;
351 		p->p_limit = NULL;
352 		lim_free(limit);
353 	}
354 
355 	/*
356 	 * Other substructures are freed from reaper and wait().
357 	 */
358 
359 	/*
360 	 * Finally, call machine-dependent code to switch to a new
361 	 * context (possibly the idle context).  Once we are no longer
362 	 * using the dead process's vmspace and stack, exit2() will be
363 	 * called to schedule those resources to be released by the
364 	 * reaper thread.
365 	 *
366 	 * Note that cpu_exit() will end with a call equivalent to
367 	 * cpu_switch(), finishing our execution (pun intended).
368 	 */
369 	uvmexp.swtch++;
370 	cpu_exit(p);
371 	panic("cpu_exit returned");
372 }
373 
374 /*
375  * Locking of this proclist is special; it's accessed in a
376  * critical section of process exit, and thus locking it can't
377  * modify interrupt state.  We use a simple spin lock for this
378  * proclist.  We use the p_hash member to linkup to deadproc.
379  */
380 struct mutex deadproc_mutex =
381     MUTEX_INITIALIZER_FLAGS(IPL_NONE, "deadproc", MTX_NOWITNESS);
382 struct proclist deadproc = LIST_HEAD_INITIALIZER(deadproc);
383 
384 /*
385  * We are called from cpu_exit() once it is safe to schedule the
386  * dead process's resources to be freed.
387  *
388  * NOTE: One must be careful with locking in this routine.  It's
389  * called from a critical section in machine-dependent code, so
390  * we should refrain from changing any interrupt state.
391  *
392  * We lock the deadproc list, place the proc on that list (using
393  * the p_hash member), and wake up the reaper.
394  */
395 void
396 exit2(struct proc *p)
397 {
398 	mtx_enter(&deadproc_mutex);
399 	LIST_INSERT_HEAD(&deadproc, p, p_hash);
400 	mtx_leave(&deadproc_mutex);
401 
402 	wakeup(&deadproc);
403 }
404 
405 void
406 proc_free(struct proc *p)
407 {
408 	crfree(p->p_ucred);
409 	pool_put(&proc_pool, p);
410 	nthreads--;
411 }
412 
413 /*
414  * Process reaper.  This is run by a kernel thread to free the resources
415  * of a dead process.  Once the resources are free, the process becomes
416  * a zombie, and the parent is allowed to read the undead's status.
417  */
418 void
419 reaper(void *arg)
420 {
421 	struct proc *p;
422 
423 	KERNEL_UNLOCK();
424 
425 	SCHED_ASSERT_UNLOCKED();
426 
427 	for (;;) {
428 		mtx_enter(&deadproc_mutex);
429 		while ((p = LIST_FIRST(&deadproc)) == NULL)
430 			msleep_nsec(&deadproc, &deadproc_mutex, PVM, "reaper",
431 			    INFSLP);
432 
433 		/* Remove us from the deadproc list. */
434 		LIST_REMOVE(p, p_hash);
435 		mtx_leave(&deadproc_mutex);
436 
437 		WITNESS_THREAD_EXIT(p);
438 
439 		KERNEL_LOCK();
440 
441 		/*
442 		 * Free the VM resources we're still holding on to.
443 		 * We must do this from a valid thread because doing
444 		 * so may block.
445 		 */
446 		uvm_uarea_free(p);
447 		p->p_vmspace = NULL;		/* zap the thread's copy */
448 
449 		if (p->p_flag & P_THREAD) {
450 			/* Just a thread */
451 			proc_free(p);
452 		} else {
453 			struct process *pr = p->p_p;
454 
455 			/* Release the rest of the process's vmspace */
456 			uvm_exit(pr);
457 
458 			if ((pr->ps_flags & PS_NOZOMBIE) == 0) {
459 				/* Process is now a true zombie. */
460 				atomic_setbits_int(&pr->ps_flags, PS_ZOMBIE);
461 				prsignal(pr->ps_pptr, SIGCHLD);
462 
463 				/* Wake up the parent so it can get exit status. */
464 				wakeup(pr->ps_pptr);
465 			} else {
466 				/* No one will wait for us. Just zap the process now */
467 				process_zap(pr);
468 			}
469 		}
470 
471 		KERNEL_UNLOCK();
472 	}
473 }
474 
475 int
476 sys_wait4(struct proc *q, void *v, register_t *retval)
477 {
478 	struct sys_wait4_args /* {
479 		syscallarg(pid_t) pid;
480 		syscallarg(int *) status;
481 		syscallarg(int) options;
482 		syscallarg(struct rusage *) rusage;
483 	} */ *uap = v;
484 	struct rusage ru;
485 	int status, error;
486 
487 	error = dowait4(q, SCARG(uap, pid),
488 	    SCARG(uap, status) ? &status : NULL,
489 	    SCARG(uap, options), SCARG(uap, rusage) ? &ru : NULL, retval);
490 	if (error == 0 && retval[0] > 0 && SCARG(uap, status)) {
491 		error = copyout(&status, SCARG(uap, status), sizeof(status));
492 	}
493 	if (error == 0 && retval[0] > 0 && SCARG(uap, rusage)) {
494 		error = copyout(&ru, SCARG(uap, rusage), sizeof(ru));
495 #ifdef KTRACE
496 		if (error == 0 && KTRPOINT(q, KTR_STRUCT))
497 			ktrrusage(q, &ru);
498 #endif
499 	}
500 	return (error);
501 }
502 
503 int
504 dowait4(struct proc *q, pid_t pid, int *statusp, int options,
505     struct rusage *rusage, register_t *retval)
506 {
507 	int nfound;
508 	struct process *pr;
509 	struct proc *p;
510 	int error;
511 
512 	if (pid == 0)
513 		pid = -q->p_p->ps_pgid;
514 	if (options &~ (WUNTRACED|WNOHANG|WCONTINUED))
515 		return (EINVAL);
516 
517 loop:
518 	nfound = 0;
519 	LIST_FOREACH(pr, &q->p_p->ps_children, ps_sibling) {
520 		if ((pr->ps_flags & PS_NOZOMBIE) ||
521 		    (pid != WAIT_ANY &&
522 		    pr->ps_pid != pid &&
523 		    pr->ps_pgid != -pid))
524 			continue;
525 
526 		p = pr->ps_mainproc;
527 
528 		nfound++;
529 		if (pr->ps_flags & PS_ZOMBIE) {
530 			retval[0] = pr->ps_pid;
531 
532 			if (statusp != NULL)
533 				*statusp = W_EXITCODE(pr->ps_xexit,
534 				    pr->ps_xsig);
535 			if (rusage != NULL)
536 				memcpy(rusage, pr->ps_ru, sizeof(*rusage));
537 			proc_finish_wait(q, p);
538 			return (0);
539 		}
540 		if (pr->ps_flags & PS_TRACED &&
541 		    (pr->ps_flags & PS_WAITED) == 0 && pr->ps_single &&
542 		    pr->ps_single->p_stat == SSTOP &&
543 		    (pr->ps_single->p_flag & P_SUSPSINGLE) == 0) {
544 			if (single_thread_wait(pr, 0))
545 				goto loop;
546 
547 			atomic_setbits_int(&pr->ps_flags, PS_WAITED);
548 			retval[0] = pr->ps_pid;
549 
550 			if (statusp != NULL)
551 				*statusp = W_STOPCODE(pr->ps_xsig);
552 			if (rusage != NULL)
553 				memset(rusage, 0, sizeof(*rusage));
554 			return (0);
555 		}
556 		if (p->p_stat == SSTOP &&
557 		    (pr->ps_flags & PS_WAITED) == 0 &&
558 		    (p->p_flag & P_SUSPSINGLE) == 0 &&
559 		    (pr->ps_flags & PS_TRACED ||
560 		    options & WUNTRACED)) {
561 			atomic_setbits_int(&pr->ps_flags, PS_WAITED);
562 			retval[0] = pr->ps_pid;
563 
564 			if (statusp != NULL)
565 				*statusp = W_STOPCODE(pr->ps_xsig);
566 			if (rusage != NULL)
567 				memset(rusage, 0, sizeof(*rusage));
568 			return (0);
569 		}
570 		if ((options & WCONTINUED) && (p->p_flag & P_CONTINUED)) {
571 			atomic_clearbits_int(&p->p_flag, P_CONTINUED);
572 			retval[0] = pr->ps_pid;
573 
574 			if (statusp != NULL)
575 				*statusp = _WCONTINUED;
576 			if (rusage != NULL)
577 				memset(rusage, 0, sizeof(*rusage));
578 			return (0);
579 		}
580 	}
581 	/*
582 	 * Look in the orphans list too, to allow the parent to
583 	 * collect it's child exit status even if child is being
584 	 * debugged.
585 	 *
586 	 * Debugger detaches from the parent upon successful
587 	 * switch-over from parent to child.  At this point due to
588 	 * re-parenting the parent loses the child to debugger and a
589 	 * wait4(2) call would report that it has no children to wait
590 	 * for.  By maintaining a list of orphans we allow the parent
591 	 * to successfully wait until the child becomes a zombie.
592 	 */
593 	if (nfound == 0) {
594 		LIST_FOREACH(pr, &q->p_p->ps_orphans, ps_orphan) {
595 			if ((pr->ps_flags & PS_NOZOMBIE) ||
596 			    (pid != WAIT_ANY &&
597 			    pr->ps_pid != pid &&
598 			    pr->ps_pgid != -pid))
599 				continue;
600 			nfound++;
601 			break;
602 		}
603 	}
604 	if (nfound == 0)
605 		return (ECHILD);
606 	if (options & WNOHANG) {
607 		retval[0] = 0;
608 		return (0);
609 	}
610 	if ((error = tsleep_nsec(q->p_p, PWAIT | PCATCH, "wait", INFSLP)) != 0)
611 		return (error);
612 	goto loop;
613 }
614 
615 void
616 proc_finish_wait(struct proc *waiter, struct proc *p)
617 {
618 	struct process *pr, *tr;
619 	struct rusage *rup;
620 
621 	/*
622 	 * If we got the child via a ptrace 'attach',
623 	 * we need to give it back to the old parent.
624 	 */
625 	pr = p->p_p;
626 	if (pr->ps_oppid != 0 && (pr->ps_oppid != pr->ps_pptr->ps_pid) &&
627 	   (tr = prfind(pr->ps_oppid))) {
628 		pr->ps_oppid = 0;
629 		atomic_clearbits_int(&pr->ps_flags, PS_TRACED);
630 		process_reparent(pr, tr);
631 		prsignal(tr, SIGCHLD);
632 		wakeup(tr);
633 	} else {
634 		scheduler_wait_hook(waiter, p);
635 		rup = &waiter->p_p->ps_cru;
636 		ruadd(rup, pr->ps_ru);
637 		LIST_REMOVE(pr, ps_list);	/* off zombprocess */
638 		freepid(pr->ps_pid);
639 		process_zap(pr);
640 	}
641 }
642 
643 /*
644  * give process back to original parent or init(8)
645  */
646 void
647 process_untrace(struct process *pr)
648 {
649 	struct process *ppr = NULL;
650 
651 	KASSERT(pr->ps_flags & PS_TRACED);
652 
653 	if (pr->ps_oppid != 0 &&
654 	    (pr->ps_oppid != pr->ps_pptr->ps_pid))
655 		ppr = prfind(pr->ps_oppid);
656 
657 	/* not being traced any more */
658 	pr->ps_oppid = 0;
659 	atomic_clearbits_int(&pr->ps_flags, PS_TRACED);
660 	process_reparent(pr, ppr ? ppr : initprocess);
661 }
662 
663 void
664 process_clear_orphan(struct process *pr)
665 {
666 	if (pr->ps_flags & PS_ORPHAN) {
667 		LIST_REMOVE(pr, ps_orphan);
668 		atomic_clearbits_int(&pr->ps_flags, PS_ORPHAN);
669 	}
670 }
671 
672 /*
673  * make process 'parent' the new parent of process 'child'.
674  */
675 void
676 process_reparent(struct process *child, struct process *parent)
677 {
678 
679 	if (child->ps_pptr == parent)
680 		return;
681 
682 	KASSERT(child->ps_oppid == 0 ||
683 		child->ps_oppid == child->ps_pptr->ps_pid);
684 
685 	LIST_REMOVE(child, ps_sibling);
686 	LIST_INSERT_HEAD(&parent->ps_children, child, ps_sibling);
687 
688 	process_clear_orphan(child);
689 	if (child->ps_flags & PS_TRACED) {
690 		atomic_setbits_int(&child->ps_flags, PS_ORPHAN);
691 		LIST_INSERT_HEAD(&child->ps_pptr->ps_orphans, child, ps_orphan);
692 	}
693 
694 	child->ps_pptr = parent;
695 }
696 
697 void
698 process_zap(struct process *pr)
699 {
700 	struct vnode *otvp;
701 	struct proc *p = pr->ps_mainproc;
702 
703 	/*
704 	 * Finally finished with old proc entry.
705 	 * Unlink it from its process group and free it.
706 	 */
707 	leavepgrp(pr);
708 	LIST_REMOVE(pr, ps_sibling);
709 	process_clear_orphan(pr);
710 
711 	/*
712 	 * Decrement the count of procs running with this uid.
713 	 */
714 	(void)chgproccnt(pr->ps_ucred->cr_ruid, -1);
715 
716 	/*
717 	 * Release reference to text vnode
718 	 */
719 	otvp = pr->ps_textvp;
720 	pr->ps_textvp = NULL;
721 	if (otvp)
722 		vrele(otvp);
723 
724 	KASSERT(pr->ps_refcnt == 1);
725 	if (pr->ps_ptstat != NULL)
726 		free(pr->ps_ptstat, M_SUBPROC, sizeof(*pr->ps_ptstat));
727 	pool_put(&rusage_pool, pr->ps_ru);
728 	KASSERT(SMR_TAILQ_EMPTY_LOCKED(&pr->ps_threads));
729 	lim_free(pr->ps_limit);
730 	crfree(pr->ps_ucred);
731 	pool_put(&process_pool, pr);
732 	nprocesses--;
733 
734 	proc_free(p);
735 }
736