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