xref: /openbsd-src/sys/kern/kern_exit.c (revision d2c5a4743fb945f45b034a3a830a96f7e1bc695d)
1 /*	$OpenBSD: kern_exit.c,v 1.190 2020/10/15 16:31:11 cheloha 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) {
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 #if NKCOV > 0
188 	kcov_exit(p);
189 #endif
190 
191 	if ((p->p_flag & P_THREAD) == 0) {
192 		sigio_freelist(&pr->ps_sigiolst);
193 
194 		/* close open files and release open-file table */
195 		fdfree(p);
196 
197 		cancel_all_itimers();
198 
199 		timeout_del(&pr->ps_rucheck_to);
200 #ifdef SYSVSEM
201 		semexit(pr);
202 #endif
203 		killjobc(pr);
204 #ifdef ACCOUNTING
205 		acct_process(p);
206 #endif
207 
208 #ifdef KTRACE
209 		/* release trace file */
210 		if (pr->ps_tracevp)
211 			ktrcleartrace(pr);
212 #endif
213 
214 		unveil_destroy(pr);
215 
216 		/*
217 		 * If parent has the SAS_NOCLDWAIT flag set, we're not
218 		 * going to become a zombie.
219 		 */
220 		if (pr->ps_pptr->ps_sigacts->ps_sigflags & SAS_NOCLDWAIT)
221 			atomic_setbits_int(&pr->ps_flags, PS_NOZOMBIE);
222 	}
223 
224 	p->p_fd = NULL;		/* zap the thread's copy */
225 
226         /*
227 	 * Remove proc from pidhash chain and allproc so looking
228 	 * it up won't work.  We will put the proc on the
229 	 * deadproc list later (using the p_hash member), and
230 	 * wake up the reaper when we do.  If this is the last
231 	 * thread of a process that isn't PS_NOZOMBIE, we'll put
232 	 * the process on the zombprocess list below.
233 	 */
234 	/*
235 	 * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP!
236 	 */
237 	p->p_stat = SDEAD;
238 
239 	LIST_REMOVE(p, p_hash);
240 	LIST_REMOVE(p, p_list);
241 
242 	if ((p->p_flag & P_THREAD) == 0) {
243 		LIST_REMOVE(pr, ps_hash);
244 		LIST_REMOVE(pr, ps_list);
245 
246 		if ((pr->ps_flags & PS_NOZOMBIE) == 0)
247 			LIST_INSERT_HEAD(&zombprocess, pr, ps_list);
248 		else {
249 			/*
250 			 * Not going to be a zombie, so it's now off all
251 			 * the lists scanned by ispidtaken(), so block
252 			 * fast reuse of the pid now.
253 			 */
254 			freepid(pr->ps_pid);
255 		}
256 
257 		/*
258 		 * Reparent children to their original parent, in case
259 		 * they were being traced, or to init(8).
260 		 */
261 		qr = LIST_FIRST(&pr->ps_children);
262 		if (qr)		/* only need this if any child is S_ZOMB */
263 			wakeup(initprocess);
264 		for (; qr != 0; qr = nqr) {
265 			nqr = LIST_NEXT(qr, ps_sibling);
266 			/*
267 			 * Traced processes are killed since their
268 			 * existence means someone is screwing up.
269 			 */
270 			if (qr->ps_flags & PS_TRACED &&
271 			    !(qr->ps_flags & PS_EXITING)) {
272 				process_untrace(qr);
273 
274 				/*
275 				 * If single threading is active,
276 				 * direct the signal to the active
277 				 * thread to avoid deadlock.
278 				 */
279 				if (qr->ps_single)
280 					ptsignal(qr->ps_single, SIGKILL,
281 					    STHREAD);
282 				else
283 					prsignal(qr, SIGKILL);
284 			} else {
285 				process_reparent(qr, initprocess);
286 			}
287 		}
288 
289 		/*
290 		 * Make sure orphans won't remember the exiting process.
291 		 */
292 		while ((qr = LIST_FIRST(&pr->ps_orphans)) != NULL) {
293 			KASSERT(qr->ps_oppid == pr->ps_pid);
294 			qr->ps_oppid = 0;
295 			process_clear_orphan(qr);
296 		}
297 	}
298 
299 	/* add thread's accumulated rusage into the process's total */
300 	ruadd(rup, &p->p_ru);
301 	tuagg(pr, p);
302 
303 	/*
304 	 * clear %cpu usage during swap
305 	 */
306 	p->p_pctcpu = 0;
307 
308 	if ((p->p_flag & P_THREAD) == 0) {
309 		/*
310 		 * Final thread has died, so add on our children's rusage
311 		 * and calculate the total times
312 		 */
313 		calcru(&pr->ps_tu, &rup->ru_utime, &rup->ru_stime, NULL);
314 		ruadd(rup, &pr->ps_cru);
315 
316 		/* notify interested parties of our demise and clean up */
317 		knote_processexit(p);
318 
319 		/*
320 		 * Notify parent that we're gone.  If we're not going to
321 		 * become a zombie, reparent to process 1 (init) so that
322 		 * we can wake our original parent to possibly unblock
323 		 * wait4() to return ECHILD.
324 		 */
325 		if (pr->ps_flags & PS_NOZOMBIE) {
326 			struct process *ppr = pr->ps_pptr;
327 			process_reparent(pr, initprocess);
328 			wakeup(ppr);
329 		}
330 
331 		/*
332 		 * Release the process's signal state.
333 		 */
334 		sigactsfree(pr);
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 it's child 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 }
695 
696 void
697 process_zap(struct process *pr)
698 {
699 	struct vnode *otvp;
700 	struct proc *p = pr->ps_mainproc;
701 
702 	/*
703 	 * Finally finished with old proc entry.
704 	 * Unlink it from its process group and free it.
705 	 */
706 	leavepgrp(pr);
707 	LIST_REMOVE(pr, ps_sibling);
708 	process_clear_orphan(pr);
709 
710 	/*
711 	 * Decrement the count of procs running with this uid.
712 	 */
713 	(void)chgproccnt(pr->ps_ucred->cr_ruid, -1);
714 
715 	/*
716 	 * Release reference to text vnode
717 	 */
718 	otvp = pr->ps_textvp;
719 	pr->ps_textvp = NULL;
720 	if (otvp)
721 		vrele(otvp);
722 
723 	KASSERT(pr->ps_refcnt == 1);
724 	if (pr->ps_ptstat != NULL)
725 		free(pr->ps_ptstat, M_SUBPROC, sizeof(*pr->ps_ptstat));
726 	pool_put(&rusage_pool, pr->ps_ru);
727 	KASSERT(TAILQ_EMPTY(&pr->ps_threads));
728 	lim_free(pr->ps_limit);
729 	crfree(pr->ps_ucred);
730 	pool_put(&process_pool, pr);
731 	nprocesses--;
732 
733 	proc_free(p);
734 }
735