xref: /openbsd-src/sys/kern/kern_exit.c (revision fb3dec7c192d2cc4cfb0f4a1d031bcfa9af2b078)
1 /*	$OpenBSD: kern_exit.c,v 1.162 2017/11/28 06:09:44 guenther 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/file.h>
51 #include <sys/vnode.h>
52 #include <sys/syslog.h>
53 #include <sys/malloc.h>
54 #include <sys/resourcevar.h>
55 #include <sys/ptrace.h>
56 #include <sys/acct.h>
57 #include <sys/filedesc.h>
58 #include <sys/signalvar.h>
59 #include <sys/sched.h>
60 #include <sys/ktrace.h>
61 #include <sys/pool.h>
62 #include <sys/mutex.h>
63 #include <sys/pledge.h>
64 #ifdef SYSVSEM
65 #include <sys/sem.h>
66 #endif
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 void	proc_finish_wait(struct proc *, struct proc *);
75 void	process_zap(struct process *);
76 void	proc_free(struct proc *);
77 
78 /*
79  * exit --
80  *	Death of process.
81  */
82 int
83 sys_exit(struct proc *p, void *v, register_t *retval)
84 {
85 	struct sys_exit_args /* {
86 		syscallarg(int) rval;
87 	} */ *uap = v;
88 
89 	exit1(p, W_EXITCODE(SCARG(uap, rval), 0), EXIT_NORMAL);
90 	/* NOTREACHED */
91 	return (0);
92 }
93 
94 int
95 sys___threxit(struct proc *p, void *v, register_t *retval)
96 {
97 	struct sys___threxit_args /* {
98 		syscallarg(pid_t *) notdead;
99 	} */ *uap = v;
100 
101 	if (SCARG(uap, notdead) != NULL) {
102 		pid_t zero = 0;
103 		if (copyout(&zero, SCARG(uap, notdead), sizeof(zero)))
104 			psignal(p, SIGSEGV);
105 	}
106 	exit1(p, 0, EXIT_THREAD);
107 
108 	return (0);
109 }
110 
111 /*
112  * Exit: deallocate address space and other resources, change proc state
113  * to zombie, and unlink proc from allproc and parent's lists.  Save exit
114  * status and rusage for wait().  Check for child processes and orphan them.
115  */
116 void
117 exit1(struct proc *p, int rv, int flags)
118 {
119 	struct process *pr, *qr, *nqr;
120 	struct rusage *rup;
121 	struct vnode *ovp;
122 
123 	atomic_setbits_int(&p->p_flag, P_WEXIT);
124 
125 	pr = p->p_p;
126 
127 	/* single-threaded? */
128 	if (!P_HASSIBLING(p)) {
129 		flags = EXIT_NORMAL;
130 	} else {
131 		/* nope, multi-threaded */
132 		if (flags == EXIT_NORMAL)
133 			single_thread_set(p, SINGLE_EXIT, 0);
134 		else if (flags == EXIT_THREAD)
135 			single_thread_check(p, 0);
136 	}
137 
138 	if (flags == EXIT_NORMAL) {
139 		if (pr->ps_pid == 1)
140 			panic("init died (signal %d, exit %d)",
141 			    WTERMSIG(rv), WEXITSTATUS(rv));
142 
143 		atomic_setbits_int(&pr->ps_flags, PS_EXITING);
144 		pr->ps_mainproc->p_xstat = rv;
145 
146 		/*
147 		 * If parent is waiting for us to exit or exec, PS_PPWAIT
148 		 * is set; we wake up the parent early to avoid deadlock.
149 		 */
150 		if (pr->ps_flags & PS_PPWAIT) {
151 			atomic_clearbits_int(&pr->ps_flags, PS_PPWAIT);
152 			atomic_clearbits_int(&pr->ps_pptr->ps_flags,
153 			    PS_ISPWAIT);
154 			wakeup(pr->ps_pptr);
155 		}
156 	}
157 
158 	/* unlink ourselves from the active threads */
159 	TAILQ_REMOVE(&pr->ps_threads, p, p_thr_link);
160 	if ((p->p_flag & P_THREAD) == 0) {
161 		/* main thread gotta wait because it has the pid, et al */
162 		while (pr->ps_refcnt > 1)
163 			tsleep(&pr->ps_threads, PUSER, "thrdeath", 0);
164 		if (pr->ps_flags & PS_PROFIL)
165 			stopprofclock(pr);
166 	}
167 
168 	rup = pr->ps_ru;
169 	if (rup == NULL) {
170 		rup = pool_get(&rusage_pool, PR_WAITOK | PR_ZERO);
171 		if (pr->ps_ru == NULL) {
172 			pr->ps_ru = rup;
173 		} else {
174 			pool_put(&rusage_pool, rup);
175 			rup = pr->ps_ru;
176 		}
177 	}
178 	p->p_siglist = 0;
179 
180 	if ((p->p_flag & P_THREAD) == 0) {
181 		/* close open files and release open-file table */
182 		fdfree(p);
183 
184 		timeout_del(&pr->ps_realit_to);
185 #ifdef SYSVSEM
186 		semexit(pr);
187 #endif
188 		if (SESS_LEADER(pr)) {
189 			struct session *sp = pr->ps_session;
190 
191 			if (sp->s_ttyvp) {
192 				/*
193 				 * Controlling process.
194 				 * Signal foreground pgrp,
195 				 * drain controlling terminal
196 				 * and revoke access to controlling terminal.
197 				 */
198 				if (sp->s_ttyp->t_session == sp) {
199 					if (sp->s_ttyp->t_pgrp)
200 						pgsignal(sp->s_ttyp->t_pgrp,
201 						    SIGHUP, 1);
202 					ttywait(sp->s_ttyp);
203 					/*
204 					 * The tty could have been revoked
205 					 * if we blocked.
206 					 */
207 					if (sp->s_ttyvp)
208 						VOP_REVOKE(sp->s_ttyvp,
209 						    REVOKEALL);
210 				}
211 				ovp = sp->s_ttyvp;
212 				sp->s_ttyvp = NULL;
213 				if (ovp)
214 					vrele(ovp);
215 				/*
216 				 * s_ttyp is not zero'd; we use this to
217 				 * indicate that the session once had a
218 				 * controlling terminal.  (for logging and
219 				 * informational purposes)
220 				 */
221 			}
222 			sp->s_leader = NULL;
223 		}
224 		fixjobc(pr, pr->ps_pgrp, 0);
225 
226 #ifdef ACCOUNTING
227 		acct_process(p);
228 #endif
229 
230 #ifdef KTRACE
231 		/* release trace file */
232 		if (pr->ps_tracevp)
233 			ktrcleartrace(pr);
234 #endif
235 
236 		/*
237 		 * If parent has the SAS_NOCLDWAIT flag set, we're not
238 		 * going to become a zombie.
239 		 */
240 		if (pr->ps_pptr->ps_sigacts->ps_flags & SAS_NOCLDWAIT)
241 			atomic_setbits_int(&pr->ps_flags, PS_NOZOMBIE);
242 	}
243 
244 	p->p_fd = NULL;		/* zap the thread's copy */
245 
246         /*
247 	 * Remove proc from pidhash chain and allproc so looking
248 	 * it up won't work.  We will put the proc on the
249 	 * deadproc list later (using the p_hash member), and
250 	 * wake up the reaper when we do.  If this is the last
251 	 * thread of a process that isn't PS_NOZOMBIE, we'll put
252 	 * the process on the zombprocess list below.
253 	 */
254 	/*
255 	 * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP!
256 	 */
257 	p->p_stat = SDEAD;
258 
259 	LIST_REMOVE(p, p_hash);
260 	LIST_REMOVE(p, p_list);
261 
262 	if ((p->p_flag & P_THREAD) == 0) {
263 		LIST_REMOVE(pr, ps_hash);
264 		LIST_REMOVE(pr, ps_list);
265 
266 		if ((pr->ps_flags & PS_NOZOMBIE) == 0)
267 			LIST_INSERT_HEAD(&zombprocess, pr, ps_list);
268 		else {
269 			/*
270 			 * Not going to be a zombie, so it's now off all
271 			 * the lists scanned by ispidtaken(), so block
272 			 * fast reuse of the pid now.
273 			 */
274 			freepid(pr->ps_pid);
275 		}
276 
277 		/*
278 		 * Give orphaned children to init(8).
279 		 */
280 		qr = LIST_FIRST(&pr->ps_children);
281 		if (qr)		/* only need this if any child is S_ZOMB */
282 			wakeup(initprocess);
283 		for (; qr != 0; qr = nqr) {
284 			nqr = LIST_NEXT(qr, ps_sibling);
285 			proc_reparent(qr, initprocess);
286 			/*
287 			 * Traced processes are killed since their
288 			 * existence means someone is screwing up.
289 			 */
290 			if (qr->ps_flags & PS_TRACED &&
291 			    !(qr->ps_flags & PS_EXITING)) {
292 				atomic_clearbits_int(&qr->ps_flags, PS_TRACED);
293 				/*
294 				 * If single threading is active,
295 				 * direct the signal to the active
296 				 * thread to avoid deadlock.
297 				 */
298 				if (qr->ps_single)
299 					ptsignal(qr->ps_single, SIGKILL,
300 					    STHREAD);
301 				else
302 					prsignal(qr, SIGKILL);
303 			}
304 		}
305 	}
306 
307 	/* add thread's accumulated rusage into the process's total */
308 	ruadd(rup, &p->p_ru);
309 	tuagg(pr, p);
310 
311 	/*
312 	 * clear %cpu usage during swap
313 	 */
314 	p->p_pctcpu = 0;
315 
316 	if ((p->p_flag & P_THREAD) == 0) {
317 		/*
318 		 * Final thread has died, so add on our children's rusage
319 		 * and calculate the total times
320 		 */
321 		calcru(&pr->ps_tu, &rup->ru_utime, &rup->ru_stime, NULL);
322 		ruadd(rup, &pr->ps_cru);
323 
324 		/* notify interested parties of our demise and clean up */
325 		knote_processexit(p);
326 
327 		/*
328 		 * Notify parent that we're gone.  If we're not going to
329 		 * become a zombie, reparent to process 1 (init) so that
330 		 * we can wake our original parent to possibly unblock
331 		 * wait4() to return ECHILD.
332 		 */
333 		if (pr->ps_flags & PS_NOZOMBIE) {
334 			struct process *ppr = pr->ps_pptr;
335 			proc_reparent(pr, initprocess);
336 			wakeup(ppr);
337 		}
338 
339 		/*
340 		 * Release the process's signal state.
341 		 */
342 		sigactsfree(pr);
343 	}
344 
345 	/* just a thread? detach it from its process */
346 	if (p->p_flag & P_THREAD) {
347 		/* scheduler_wait_hook(pr->ps_mainproc, p); XXX */
348 		if (--pr->ps_refcnt == 1)
349 			wakeup(&pr->ps_threads);
350 		KASSERT(pr->ps_refcnt > 0);
351 	}
352 
353 	/*
354 	 * Other substructures are freed from reaper and wait().
355 	 */
356 
357 	/*
358 	 * Finally, call machine-dependent code to switch to a new
359 	 * context (possibly the idle context).  Once we are no longer
360 	 * using the dead process's vmspace and stack, exit2() will be
361 	 * called to schedule those resources to be released by the
362 	 * reaper thread.
363 	 *
364 	 * Note that cpu_exit() will end with a call equivalent to
365 	 * cpu_switch(), finishing our execution (pun intended).
366 	 */
367 	uvmexp.swtch++;
368 	cpu_exit(p);
369 	panic("cpu_exit returned");
370 }
371 
372 /*
373  * Locking of this proclist is special; it's accessed in a
374  * critical section of process exit, and thus locking it can't
375  * modify interrupt state.  We use a simple spin lock for this
376  * proclist.  We use the p_hash member to linkup to deadproc.
377  */
378 struct mutex deadproc_mutex =
379     MUTEX_INITIALIZER_FLAGS(IPL_NONE, NULL, MTX_NOWITNESS);
380 struct proclist deadproc = LIST_HEAD_INITIALIZER(deadproc);
381 
382 /*
383  * We are called from cpu_exit() once it is safe to schedule the
384  * dead process's resources to be freed.
385  *
386  * NOTE: One must be careful with locking in this routine.  It's
387  * called from a critical section in machine-dependent code, so
388  * we should refrain from changing any interrupt state.
389  *
390  * We lock the deadproc list, place the proc on that list (using
391  * the p_hash member), and wake up the reaper.
392  */
393 void
394 exit2(struct proc *p)
395 {
396 	mtx_enter(&deadproc_mutex);
397 	LIST_INSERT_HEAD(&deadproc, p, p_hash);
398 	mtx_leave(&deadproc_mutex);
399 
400 	wakeup(&deadproc);
401 }
402 
403 void
404 proc_free(struct proc *p)
405 {
406 	crfree(p->p_ucred);
407 	pool_put(&proc_pool, p);
408 	nthreads--;
409 }
410 
411 /*
412  * Process reaper.  This is run by a kernel thread to free the resources
413  * of a dead process.  Once the resources are free, the process becomes
414  * a zombie, and the parent is allowed to read the undead's status.
415  */
416 void
417 reaper(void)
418 {
419 	struct proc *p;
420 
421 	KERNEL_UNLOCK();
422 
423 	SCHED_ASSERT_UNLOCKED();
424 
425 	for (;;) {
426 		mtx_enter(&deadproc_mutex);
427 		while ((p = LIST_FIRST(&deadproc)) == NULL)
428 			msleep(&deadproc, &deadproc_mutex, PVM, "reaper", 0);
429 
430 		/* Remove us from the deadproc list. */
431 		LIST_REMOVE(p, p_hash);
432 		mtx_leave(&deadproc_mutex);
433 
434 		WITNESS_THREAD_EXIT(p);
435 
436 		KERNEL_LOCK();
437 
438 		/*
439 		 * Free the VM resources we're still holding on to.
440 		 * We must do this from a valid thread because doing
441 		 * so may block.
442 		 */
443 		uvm_uarea_free(p);
444 		p->p_vmspace = NULL;		/* zap the thread's copy */
445 
446 		if (p->p_flag & P_THREAD) {
447 			/* Just a thread */
448 			proc_free(p);
449 		} else {
450 			struct process *pr = p->p_p;
451 
452 			/* Release the rest of the process's vmspace */
453 			uvm_exit(pr);
454 
455 			if ((pr->ps_flags & PS_NOZOMBIE) == 0) {
456 				/* Process is now a true zombie. */
457 				atomic_setbits_int(&pr->ps_flags, PS_ZOMBIE);
458 				prsignal(pr->ps_pptr, SIGCHLD);
459 
460 				/* Wake up the parent so it can get exit status. */
461 				wakeup(pr->ps_pptr);
462 			} else {
463 				/* No one will wait for us. Just zap the process now */
464 				process_zap(pr);
465 			}
466 		}
467 
468 		KERNEL_UNLOCK();
469 	}
470 }
471 
472 int
473 sys_wait4(struct proc *q, void *v, register_t *retval)
474 {
475 	struct sys_wait4_args /* {
476 		syscallarg(pid_t) pid;
477 		syscallarg(int *) status;
478 		syscallarg(int) options;
479 		syscallarg(struct rusage *) rusage;
480 	} */ *uap = v;
481 	struct rusage ru;
482 	int status, error;
483 
484 	error = dowait4(q, SCARG(uap, pid),
485 	    SCARG(uap, status) ? &status : NULL,
486 	    SCARG(uap, options), SCARG(uap, rusage) ? &ru : NULL, retval);
487 	if (error == 0 && retval[0] > 0 && SCARG(uap, status)) {
488 		error = copyout(&status, SCARG(uap, status), sizeof(status));
489 	}
490 	if (error == 0 && retval[0] > 0 && SCARG(uap, rusage)) {
491 		error = copyout(&ru, SCARG(uap, rusage), sizeof(ru));
492 #ifdef KTRACE
493 		if (error == 0 && KTRPOINT(q, KTR_STRUCT))
494 			ktrrusage(q, &ru);
495 #endif
496 	}
497 	return (error);
498 }
499 
500 int
501 dowait4(struct proc *q, pid_t pid, int *statusp, int options,
502     struct rusage *rusage, register_t *retval)
503 {
504 	int nfound;
505 	struct process *pr;
506 	struct proc *p;
507 	int error;
508 
509 	if (pid == 0)
510 		pid = -q->p_p->ps_pgid;
511 	if (options &~ (WUNTRACED|WNOHANG|WCONTINUED))
512 		return (EINVAL);
513 
514 loop:
515 	nfound = 0;
516 	LIST_FOREACH(pr, &q->p_p->ps_children, ps_sibling) {
517 		if ((pr->ps_flags & PS_NOZOMBIE) ||
518 		    (pid != WAIT_ANY &&
519 		    pr->ps_pid != pid &&
520 		    pr->ps_pgid != -pid))
521 			continue;
522 
523 		p = pr->ps_mainproc;
524 
525 		nfound++;
526 		if (pr->ps_flags & PS_ZOMBIE) {
527 			retval[0] = pr->ps_pid;
528 
529 			if (statusp != NULL)
530 				*statusp = p->p_xstat;	/* convert to int */
531 			if (rusage != NULL)
532 				memcpy(rusage, pr->ps_ru, sizeof(*rusage));
533 			proc_finish_wait(q, p);
534 			return (0);
535 		}
536 		if (pr->ps_flags & PS_TRACED &&
537 		    (pr->ps_flags & PS_WAITED) == 0 && pr->ps_single &&
538 		    pr->ps_single->p_stat == SSTOP &&
539 		    (pr->ps_single->p_flag & P_SUSPSINGLE) == 0) {
540 			single_thread_wait(pr);
541 
542 			atomic_setbits_int(&pr->ps_flags, PS_WAITED);
543 			retval[0] = pr->ps_pid;
544 
545 			if (statusp != NULL)
546 				*statusp = W_STOPCODE(pr->ps_single->p_xstat);
547 			if (rusage != NULL)
548 				memset(rusage, 0, sizeof(*rusage));
549 			return (0);
550 		}
551 		if (p->p_stat == SSTOP &&
552 		    (pr->ps_flags & PS_WAITED) == 0 &&
553 		    (p->p_flag & P_SUSPSINGLE) == 0 &&
554 		    (pr->ps_flags & PS_TRACED ||
555 		    options & WUNTRACED)) {
556 			atomic_setbits_int(&pr->ps_flags, PS_WAITED);
557 			retval[0] = pr->ps_pid;
558 
559 			if (statusp != NULL)
560 				*statusp = W_STOPCODE(p->p_xstat);
561 			if (rusage != NULL)
562 				memset(rusage, 0, sizeof(*rusage));
563 			return (0);
564 		}
565 		if ((options & WCONTINUED) && (p->p_flag & P_CONTINUED)) {
566 			atomic_clearbits_int(&p->p_flag, P_CONTINUED);
567 			retval[0] = pr->ps_pid;
568 
569 			if (statusp != NULL)
570 				*statusp = _WCONTINUED;
571 			if (rusage != NULL)
572 				memset(rusage, 0, sizeof(*rusage));
573 			return (0);
574 		}
575 	}
576 	if (nfound == 0)
577 		return (ECHILD);
578 	if (options & WNOHANG) {
579 		retval[0] = 0;
580 		return (0);
581 	}
582 	if ((error = tsleep(q->p_p, PWAIT | PCATCH, "wait", 0)) != 0)
583 		return (error);
584 	goto loop;
585 }
586 
587 void
588 proc_finish_wait(struct proc *waiter, struct proc *p)
589 {
590 	struct process *pr, *tr;
591 	struct rusage *rup;
592 
593 	/*
594 	 * If we got the child via a ptrace 'attach',
595 	 * we need to give it back to the old parent.
596 	 */
597 	pr = p->p_p;
598 	if (pr->ps_oppid && (tr = prfind(pr->ps_oppid))) {
599 		atomic_clearbits_int(&pr->ps_flags, PS_TRACED);
600 		pr->ps_oppid = 0;
601 		proc_reparent(pr, tr);
602 		prsignal(tr, SIGCHLD);
603 		wakeup(tr);
604 	} else {
605 		scheduler_wait_hook(waiter, p);
606 		p->p_xstat = 0;
607 		rup = &waiter->p_p->ps_cru;
608 		ruadd(rup, pr->ps_ru);
609 		LIST_REMOVE(pr, ps_list);	/* off zombprocess */
610 		freepid(pr->ps_pid);
611 		process_zap(pr);
612 	}
613 }
614 
615 /*
616  * make process 'parent' the new parent of process 'child'.
617  */
618 void
619 proc_reparent(struct process *child, struct process *parent)
620 {
621 
622 	if (child->ps_pptr == parent)
623 		return;
624 
625 	LIST_REMOVE(child, ps_sibling);
626 	LIST_INSERT_HEAD(&parent->ps_children, child, ps_sibling);
627 	child->ps_pptr = parent;
628 }
629 
630 void
631 process_zap(struct process *pr)
632 {
633 	struct vnode *otvp;
634 	struct proc *p = pr->ps_mainproc;
635 
636 	/*
637 	 * Finally finished with old proc entry.
638 	 * Unlink it from its process group and free it.
639 	 */
640 	leavepgrp(pr);
641 	LIST_REMOVE(pr, ps_sibling);
642 
643 	/*
644 	 * Decrement the count of procs running with this uid.
645 	 */
646 	(void)chgproccnt(pr->ps_ucred->cr_ruid, -1);
647 
648 	/*
649 	 * Release reference to text vnode
650 	 */
651 	otvp = pr->ps_textvp;
652 	pr->ps_textvp = NULL;
653 	if (otvp)
654 		vrele(otvp);
655 
656 	KASSERT(pr->ps_refcnt == 1);
657 	if (pr->ps_ptstat != NULL)
658 		free(pr->ps_ptstat, M_SUBPROC, sizeof(*pr->ps_ptstat));
659 	pool_put(&rusage_pool, pr->ps_ru);
660 	KASSERT(TAILQ_EMPTY(&pr->ps_threads));
661 	limfree(pr->ps_limit);
662 	crfree(pr->ps_ucred);
663 	pool_put(&process_pool, pr);
664 	nprocesses--;
665 
666 	proc_free(p);
667 }
668