xref: /openbsd-src/sys/kern/kern_exit.c (revision b99ef4df7fac99f3475b694d6cd4990521c99ae6)
1 /*	$OpenBSD: kern_exit.c,v 1.195 2021/02/08 10:51:01 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 
128 	atomic_setbits_int(&p->p_flag, P_WEXIT);
129 
130 	pr = p->p_p;
131 
132 	/* single-threaded? */
133 	if (!P_HASSIBLING(p)) {
134 		flags = EXIT_NORMAL;
135 	} else {
136 		/* nope, multi-threaded */
137 		if (flags == EXIT_NORMAL)
138 			single_thread_set(p, SINGLE_EXIT, 0);
139 		else if (flags == EXIT_THREAD)
140 			single_thread_check(p, 0);
141 	}
142 
143 	if (flags == EXIT_NORMAL && !(pr->ps_flags & PS_EXITING)) {
144 		if (pr->ps_pid == 1)
145 			panic("init died (signal %d, exit %d)", xsig, xexit);
146 
147 		atomic_setbits_int(&pr->ps_flags, PS_EXITING);
148 		pr->ps_xexit = xexit;
149 		pr->ps_xsig  = xsig;
150 
151 		/*
152 		 * If parent is waiting for us to exit or exec, PS_PPWAIT
153 		 * is set; we wake up the parent early to avoid deadlock.
154 		 */
155 		if (pr->ps_flags & PS_PPWAIT) {
156 			atomic_clearbits_int(&pr->ps_flags, PS_PPWAIT);
157 			atomic_clearbits_int(&pr->ps_pptr->ps_flags,
158 			    PS_ISPWAIT);
159 			wakeup(pr->ps_pptr);
160 		}
161 	}
162 
163 	/* unlink ourselves from the active threads */
164 	TAILQ_REMOVE(&pr->ps_threads, p, p_thr_link);
165 	if ((p->p_flag & P_THREAD) == 0) {
166 		/* main thread gotta wait because it has the pid, et al */
167 		while (pr->ps_refcnt > 1)
168 			tsleep_nsec(&pr->ps_threads, PWAIT, "thrdeath", INFSLP);
169 		if (pr->ps_flags & PS_PROFIL)
170 			stopprofclock(pr);
171 	}
172 
173 	rup = pr->ps_ru;
174 	if (rup == NULL) {
175 		rup = pool_get(&rusage_pool, PR_WAITOK | PR_ZERO);
176 		if (pr->ps_ru == NULL) {
177 			pr->ps_ru = rup;
178 		} else {
179 			pool_put(&rusage_pool, rup);
180 			rup = pr->ps_ru;
181 		}
182 	}
183 	p->p_siglist = 0;
184 	if ((p->p_flag & P_THREAD) == 0)
185 		pr->ps_siglist = 0;
186 
187 	kqpoll_exit();
188 
189 #if NKCOV > 0
190 	kcov_exit(p);
191 #endif
192 
193 	if ((p->p_flag & P_THREAD) == 0) {
194 		sigio_freelist(&pr->ps_sigiolst);
195 
196 		/* close open files and release open-file table */
197 		fdfree(p);
198 
199 		cancel_all_itimers();
200 
201 		timeout_del(&pr->ps_rucheck_to);
202 #ifdef SYSVSEM
203 		semexit(pr);
204 #endif
205 		killjobc(pr);
206 #ifdef ACCOUNTING
207 		acct_process(p);
208 #endif
209 
210 #ifdef KTRACE
211 		/* release trace file */
212 		if (pr->ps_tracevp)
213 			ktrcleartrace(pr);
214 #endif
215 
216 		unveil_destroy(pr);
217 
218 		/*
219 		 * If parent has the SAS_NOCLDWAIT flag set, we're not
220 		 * going to become a zombie.
221 		 */
222 		if (pr->ps_pptr->ps_sigacts->ps_sigflags & SAS_NOCLDWAIT)
223 			atomic_setbits_int(&pr->ps_flags, PS_NOZOMBIE);
224 	}
225 
226 	p->p_fd = NULL;		/* zap the thread's copy */
227 
228         /*
229 	 * Remove proc from pidhash chain and allproc so looking
230 	 * it up won't work.  We will put the proc on the
231 	 * deadproc list later (using the p_hash member), and
232 	 * wake up the reaper when we do.  If this is the last
233 	 * thread of a process that isn't PS_NOZOMBIE, we'll put
234 	 * the process on the zombprocess list below.
235 	 */
236 	/*
237 	 * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP!
238 	 */
239 	p->p_stat = SDEAD;
240 
241 	LIST_REMOVE(p, p_hash);
242 	LIST_REMOVE(p, p_list);
243 
244 	if ((p->p_flag & P_THREAD) == 0) {
245 		LIST_REMOVE(pr, ps_hash);
246 		LIST_REMOVE(pr, ps_list);
247 
248 		if ((pr->ps_flags & PS_NOZOMBIE) == 0)
249 			LIST_INSERT_HEAD(&zombprocess, pr, ps_list);
250 		else {
251 			/*
252 			 * Not going to be a zombie, so it's now off all
253 			 * the lists scanned by ispidtaken(), so block
254 			 * fast reuse of the pid now.
255 			 */
256 			freepid(pr->ps_pid);
257 		}
258 
259 		/*
260 		 * Reparent children to their original parent, in case
261 		 * they were being traced, or to init(8).
262 		 */
263 		qr = LIST_FIRST(&pr->ps_children);
264 		if (qr)		/* only need this if any child is S_ZOMB */
265 			wakeup(initprocess);
266 		for (; qr != 0; qr = nqr) {
267 			nqr = LIST_NEXT(qr, ps_sibling);
268 			/*
269 			 * Traced processes are killed since their
270 			 * existence means someone is screwing up.
271 			 */
272 			if (qr->ps_flags & PS_TRACED &&
273 			    !(qr->ps_flags & PS_EXITING)) {
274 				process_untrace(qr);
275 
276 				/*
277 				 * If single threading is active,
278 				 * direct the signal to the active
279 				 * thread to avoid deadlock.
280 				 */
281 				if (qr->ps_single)
282 					ptsignal(qr->ps_single, SIGKILL,
283 					    STHREAD);
284 				else
285 					prsignal(qr, SIGKILL);
286 			} else {
287 				process_reparent(qr, initprocess);
288 			}
289 		}
290 
291 		/*
292 		 * Make sure orphans won't remember the exiting process.
293 		 */
294 		while ((qr = LIST_FIRST(&pr->ps_orphans)) != NULL) {
295 			KASSERT(qr->ps_oppid == pr->ps_pid);
296 			qr->ps_oppid = 0;
297 			process_clear_orphan(qr);
298 		}
299 	}
300 
301 	/* add thread's accumulated rusage into the process's total */
302 	ruadd(rup, &p->p_ru);
303 	tuagg(pr, p);
304 
305 	/*
306 	 * clear %cpu usage during swap
307 	 */
308 	p->p_pctcpu = 0;
309 
310 	if ((p->p_flag & P_THREAD) == 0) {
311 		/*
312 		 * Final thread has died, so add on our children's rusage
313 		 * and calculate the total times
314 		 */
315 		calcru(&pr->ps_tu, &rup->ru_utime, &rup->ru_stime, NULL);
316 		ruadd(rup, &pr->ps_cru);
317 
318 		/* notify interested parties of our demise and clean up */
319 		knote_processexit(p);
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 		 * Release the process's signal state.
335 		 */
336 		sigactsfree(pr);
337 	}
338 
339 	/* just a thread? detach it from its process */
340 	if (p->p_flag & P_THREAD) {
341 		/* scheduler_wait_hook(pr->ps_mainproc, p); XXX */
342 		if (--pr->ps_refcnt == 1)
343 			wakeup(&pr->ps_threads);
344 		KASSERT(pr->ps_refcnt > 0);
345 	}
346 
347 	/* Release the thread's read reference of resource limit structure. */
348 	if (p->p_limit != NULL) {
349 		struct plimit *limit;
350 
351 		limit = p->p_limit;
352 		p->p_limit = NULL;
353 		lim_free(limit);
354 	}
355 
356 	/*
357 	 * Other substructures are freed from reaper and wait().
358 	 */
359 
360 	/*
361 	 * Finally, call machine-dependent code to switch to a new
362 	 * context (possibly the idle context).  Once we are no longer
363 	 * using the dead process's vmspace and stack, exit2() will be
364 	 * called to schedule those resources to be released by the
365 	 * reaper thread.
366 	 *
367 	 * Note that cpu_exit() will end with a call equivalent to
368 	 * cpu_switch(), finishing our execution (pun intended).
369 	 */
370 	uvmexp.swtch++;
371 	cpu_exit(p);
372 	panic("cpu_exit returned");
373 }
374 
375 /*
376  * Locking of this proclist is special; it's accessed in a
377  * critical section of process exit, and thus locking it can't
378  * modify interrupt state.  We use a simple spin lock for this
379  * proclist.  We use the p_hash member to linkup to deadproc.
380  */
381 struct mutex deadproc_mutex =
382     MUTEX_INITIALIZER_FLAGS(IPL_NONE, "deadproc", MTX_NOWITNESS);
383 struct proclist deadproc = LIST_HEAD_INITIALIZER(deadproc);
384 
385 /*
386  * We are called from cpu_exit() once it is safe to schedule the
387  * dead process's resources to be freed.
388  *
389  * NOTE: One must be careful with locking in this routine.  It's
390  * called from a critical section in machine-dependent code, so
391  * we should refrain from changing any interrupt state.
392  *
393  * We lock the deadproc list, place the proc on that list (using
394  * the p_hash member), and wake up the reaper.
395  */
396 void
397 exit2(struct proc *p)
398 {
399 	mtx_enter(&deadproc_mutex);
400 	LIST_INSERT_HEAD(&deadproc, p, p_hash);
401 	mtx_leave(&deadproc_mutex);
402 
403 	wakeup(&deadproc);
404 }
405 
406 void
407 proc_free(struct proc *p)
408 {
409 	crfree(p->p_ucred);
410 	pool_put(&proc_pool, p);
411 	nthreads--;
412 }
413 
414 /*
415  * Process reaper.  This is run by a kernel thread to free the resources
416  * of a dead process.  Once the resources are free, the process becomes
417  * a zombie, and the parent is allowed to read the undead's status.
418  */
419 void
420 reaper(void *arg)
421 {
422 	struct proc *p;
423 
424 	KERNEL_UNLOCK();
425 
426 	SCHED_ASSERT_UNLOCKED();
427 
428 	for (;;) {
429 		mtx_enter(&deadproc_mutex);
430 		while ((p = LIST_FIRST(&deadproc)) == NULL)
431 			msleep_nsec(&deadproc, &deadproc_mutex, PVM, "reaper",
432 			    INFSLP);
433 
434 		/* Remove us from the deadproc list. */
435 		LIST_REMOVE(p, p_hash);
436 		mtx_leave(&deadproc_mutex);
437 
438 		WITNESS_THREAD_EXIT(p);
439 
440 		KERNEL_LOCK();
441 
442 		/*
443 		 * Free the VM resources we're still holding on to.
444 		 * We must do this from a valid thread because doing
445 		 * so may block.
446 		 */
447 		uvm_uarea_free(p);
448 		p->p_vmspace = NULL;		/* zap the thread's copy */
449 
450 		if (p->p_flag & P_THREAD) {
451 			/* Just a thread */
452 			proc_free(p);
453 		} else {
454 			struct process *pr = p->p_p;
455 
456 			/* Release the rest of the process's vmspace */
457 			uvm_exit(pr);
458 
459 			if ((pr->ps_flags & PS_NOZOMBIE) == 0) {
460 				/* Process is now a true zombie. */
461 				atomic_setbits_int(&pr->ps_flags, PS_ZOMBIE);
462 				prsignal(pr->ps_pptr, SIGCHLD);
463 
464 				/* Wake up the parent so it can get exit status. */
465 				wakeup(pr->ps_pptr);
466 			} else {
467 				/* No one will wait for us. Just zap the process now */
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 it's child 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 	lim_free(pr->ps_limit);
732 	crfree(pr->ps_ucred);
733 	pool_put(&process_pool, pr);
734 	nprocesses--;
735 
736 	proc_free(p);
737 }
738