xref: /freebsd/sys/kern/kern_exit.c (revision 58c77a9d530350a60ae5ec0a0160066364742c6e)
19454b2d8SWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
5df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
6df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
7df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
19df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
20df8bae1dSRodney W. Grimes  *    without specific prior written permission.
21df8bae1dSRodney W. Grimes  *
22df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
33df8bae1dSRodney W. Grimes  *
34df8bae1dSRodney W. Grimes  *	@(#)kern_exit.c	8.7 (Berkeley) 2/12/94
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37677b542eSDavid E. O'Brien #include <sys/cdefs.h>
38677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
39677b542eSDavid E. O'Brien 
405591b823SEivind Eklund #include "opt_compat.h"
415d217f17SJohn Birrell #include "opt_kdtrace.h"
42db6a20e2SGarrett Wollman #include "opt_ktrace.h"
43db6a20e2SGarrett Wollman 
44df8bae1dSRodney W. Grimes #include <sys/param.h>
45df8bae1dSRodney W. Grimes #include <sys/systm.h>
465fdb8324SBruce Evans #include <sys/sysproto.h>
4775b8b3b2SJohn Baldwin #include <sys/eventhandler.h>
481c5bb3eaSPeter Wemm #include <sys/kernel.h>
49a1c995b6SPoul-Henning Kamp #include <sys/malloc.h>
50f34fa851SJohn Baldwin #include <sys/lock.h>
5135e0e5b3SJohn Baldwin #include <sys/mutex.h>
52df8bae1dSRodney W. Grimes #include <sys/proc.h>
532a024a2bSSean Eric Fagan #include <sys/pioctl.h>
54413628a7SBjoern A. Zeeb #include <sys/jail.h>
55df8bae1dSRodney W. Grimes #include <sys/tty.h>
56df8bae1dSRodney W. Grimes #include <sys/wait.h>
57eb30c1c0SPeter Wemm #include <sys/vmmeter.h>
587a6b989bSJohn Baldwin #include <sys/vnode.h>
59097055e2SEdward Tomasz Napierala #include <sys/racct.h>
60df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
6125f6e35aSPoul-Henning Kamp #include <sys/sbuf.h>
62797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
63b43179fbSJeff Roberson #include <sys/sched.h>
641005a129SJohn Baldwin #include <sys/sx.h>
65c8837938SJohn Baldwin #include <sys/syscallsubr.h>
6625f6e35aSPoul-Henning Kamp #include <sys/syslog.h>
67df8bae1dSRodney W. Grimes #include <sys/ptrace.h>
687c409b8aSJeffrey Hsu #include <sys/acct.h>		/* for acct_process() function prototype */
69797f2d22SPoul-Henning Kamp #include <sys/filedesc.h>
705d217f17SJohn Birrell #include <sys/sdt.h>
71780dc5a8SPeter Wemm #include <sys/shm.h>
72780dc5a8SPeter Wemm #include <sys/sem.h>
736c84de02SJohn Baldwin #ifdef KTRACE
746c84de02SJohn Baldwin #include <sys/ktrace.h>
756c84de02SJohn Baldwin #endif
76780dc5a8SPeter Wemm 
77fcf7f27aSRobert Watson #include <security/audit/audit.h>
78aed55708SRobert Watson #include <security/mac/mac_framework.h>
79fcf7f27aSRobert Watson 
80df8bae1dSRodney W. Grimes #include <vm/vm.h>
81eb30c1c0SPeter Wemm #include <vm/vm_extern.h>
827a6b989bSJohn Baldwin #include <vm/vm_param.h>
83efeaf95aSDavid Greenman #include <vm/pmap.h>
84efeaf95aSDavid Greenman #include <vm/vm_map.h>
852d21129dSAlan Cox #include <vm/vm_page.h>
86c897b813SJeff Roberson #include <vm/uma.h>
87df8bae1dSRodney W. Grimes 
885d217f17SJohn Birrell #ifdef KDTRACE_HOOKS
895d217f17SJohn Birrell #include <sys/dtrace_bsd.h>
905d217f17SJohn Birrell dtrace_execexit_func_t	dtrace_fasttrap_exit;
915d217f17SJohn Birrell #endif
925d217f17SJohn Birrell 
935d217f17SJohn Birrell SDT_PROVIDER_DECLARE(proc);
9479856499SRui Paulo SDT_PROBE_DEFINE(proc, kernel, , exit, exit);
955d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, , exit, 0, "int");
965d217f17SJohn Birrell 
97ba198b1cSMark Newton /* Required to be non-static for SysVR4 emulator */
9869a6f20bSMark Newton MALLOC_DEFINE(M_ZOMBIE, "zombie", "zombie proc status");
9955166637SPoul-Henning Kamp 
100c0bc2867SGleb Smirnoff /* Hook for NFS teardown procedure. */
101c0bc2867SGleb Smirnoff void (*nlminfo_release_p)(struct proc *p);
102c0bc2867SGleb Smirnoff 
103df8bae1dSRodney W. Grimes /*
104873fbcd7SRobert Watson  * exit -- death of process.
105df8bae1dSRodney W. Grimes  */
106fc0b1dbfSBruce Evans void
107830c3153SDag-Erling Smørgrav sys_exit(struct thread *td, struct sys_exit_args *uap)
108df8bae1dSRodney W. Grimes {
109b40ce416SJulian Elischer 
110b40ce416SJulian Elischer 	exit1(td, W_EXITCODE(uap->rval, 0));
111df8bae1dSRodney W. Grimes 	/* NOTREACHED */
112df8bae1dSRodney W. Grimes }
113df8bae1dSRodney W. Grimes 
114df8bae1dSRodney W. Grimes /*
115873fbcd7SRobert Watson  * Exit: deallocate address space and other resources, change proc state to
116873fbcd7SRobert Watson  * zombie, and unlink proc from allproc and parent's lists.  Save exit status
117873fbcd7SRobert Watson  * and rusage for wait().  Check for child processes and orphan them.
118df8bae1dSRodney W. Grimes  */
119fc0b1dbfSBruce Evans void
120830c3153SDag-Erling Smørgrav exit1(struct thread *td, int rv)
121df8bae1dSRodney W. Grimes {
122276c5169SJohn Baldwin 	struct proc *p, *nq, *q;
12379deba82SMatthew Dillon 	struct vnode *vtmp;
124bc093719SEd Schouten 	struct vnode *ttyvp = NULL;
12591d5354aSJohn Baldwin 	struct plimit *plim;
12657051fdcSTor Egge 	int locked;
127df8bae1dSRodney W. Grimes 
128e17660e7SKris Kennaway 	mtx_assert(&Giant, MA_NOTOWNED);
1290cddd8f0SMatthew Dillon 
130276c5169SJohn Baldwin 	p = td->td_proc;
1315486ffc8SMarius Strobl 	/*
1325486ffc8SMarius Strobl 	 * XXX in case we're rebooting we just let init die in order to
1335486ffc8SMarius Strobl 	 * work around an unsolved stack overflow seen very late during
1345486ffc8SMarius Strobl 	 * shutdown on sparc64 when the gmirror worker process exists.
1355486ffc8SMarius Strobl 	 */
1365486ffc8SMarius Strobl 	if (p == initproc && rebooting == 0) {
1375f7bd355SPoul-Henning Kamp 		printf("init died (signal %d, exit %d)\n",
138df8bae1dSRodney W. Grimes 		    WTERMSIG(rv), WEXITSTATUS(rv));
1395f7bd355SPoul-Henning Kamp 		panic("Going nowhere without my init!");
1405f7bd355SPoul-Henning Kamp 	}
1412c1011f7SJohn Dyson 
1427a6b989bSJohn Baldwin 	/*
1432c10d16aSJeff Roberson 	 * MUST abort all other threads before proceeding past here.
1447a6b989bSJohn Baldwin 	 */
145e602ba25SJulian Elischer 	PROC_LOCK(p);
1467ab24ea3SJulian Elischer 	while (p->p_flag & P_HADTHREADS) {
147e602ba25SJulian Elischer 		/*
148e602ba25SJulian Elischer 		 * First check if some other thread got here before us..
149e602ba25SJulian Elischer 		 * if so, act apropriatly, (exit or suspend);
150e602ba25SJulian Elischer 		 */
151e602ba25SJulian Elischer 		thread_suspend_check(0);
152e602ba25SJulian Elischer 
153e602ba25SJulian Elischer 		/*
154e602ba25SJulian Elischer 		 * Kill off the other threads. This requires
1556111dcd2SJohn Baldwin 		 * some co-operation from other parts of the kernel
1566111dcd2SJohn Baldwin 		 * so it may not be instantaneous.  With this state set
1576111dcd2SJohn Baldwin 		 * any thread entering the kernel from userspace will
158e602ba25SJulian Elischer 		 * thread_exit() in trap().  Any thread attempting to
1596111dcd2SJohn Baldwin 		 * sleep will return immediately with EINTR or EWOULDBLOCK
1606111dcd2SJohn Baldwin 		 * which will hopefully force them to back out to userland
1616111dcd2SJohn Baldwin 		 * freeing resources as they go.  Any thread attempting
162b3248998SJulian Elischer 		 * to return to userland will thread_exit() from userret().
1636111dcd2SJohn Baldwin 		 * thread_exit() will unsuspend us when the last of the
1646111dcd2SJohn Baldwin 		 * other threads exits.
165b370279eSDavid Xu 		 * If there is already a thread singler after resumption,
1666111dcd2SJohn Baldwin 		 * calling thread_single will fail; in that case, we just
167b370279eSDavid Xu 		 * re-check all suspension request, the thread should
168b370279eSDavid Xu 		 * either be suspended there or exit.
169e602ba25SJulian Elischer 		 */
1707ab24ea3SJulian Elischer 		if (! thread_single(SINGLE_EXIT))
1717ab24ea3SJulian Elischer 			break;
1726111dcd2SJohn Baldwin 
173e602ba25SJulian Elischer 		/*
174e602ba25SJulian Elischer 		 * All other activity in this process is now stopped.
175ed062c8dSJulian Elischer 		 * Threading support has been turned off.
176e602ba25SJulian Elischer 		 */
177e602ba25SJulian Elischer 	}
1781c4bcd05SJeff Roberson 	KASSERT(p->p_numthreads == 1,
1791c4bcd05SJeff Roberson 	    ("exit1: proc %p exiting with %d threads", p, p->p_numthreads));
180*58c77a9dSEdward Tomasz Napierala 	racct_sub(p, RACCT_NTHR, 1);
18106ad42b2SJohn Baldwin 	/*
18206ad42b2SJohn Baldwin 	 * Wakeup anyone in procfs' PIOCWAIT.  They should have a hold
18306ad42b2SJohn Baldwin 	 * on our vmspace, so we should block below until they have
18406ad42b2SJohn Baldwin 	 * released their reference to us.  Note that if they have
18506ad42b2SJohn Baldwin 	 * requested S_EXIT stops we will block here until they ack
18606ad42b2SJohn Baldwin 	 * via PIOCCONT.
18706ad42b2SJohn Baldwin 	 */
18806ad42b2SJohn Baldwin 	_STOPEVENT(p, S_EXIT, rv);
18906ad42b2SJohn Baldwin 
19006ad42b2SJohn Baldwin 	/*
19106ad42b2SJohn Baldwin 	 * Note that we are exiting and do another wakeup of anyone in
19206ad42b2SJohn Baldwin 	 * PIOCWAIT in case they aren't listening for S_EXIT stops or
19306ad42b2SJohn Baldwin 	 * decided to wait again after we told them we are exiting.
19406ad42b2SJohn Baldwin 	 */
195e602ba25SJulian Elischer 	p->p_flag |= P_WEXIT;
19606ad42b2SJohn Baldwin 	wakeup(&p->p_stype);
19706ad42b2SJohn Baldwin 
19806ad42b2SJohn Baldwin 	/*
19906ad42b2SJohn Baldwin 	 * Wait for any processes that have a hold on our vmspace to
20006ad42b2SJohn Baldwin 	 * release their reference.
20106ad42b2SJohn Baldwin 	 */
20206ad42b2SJohn Baldwin 	while (p->p_lock > 0)
20306ad42b2SJohn Baldwin 		msleep(&p->p_lock, &p->p_mtx, PWAIT, "exithold", 0);
204ebceaf6dSDavid Xu 
205bb63fddeSAlexander Leidinger 	p->p_xstat = rv;	/* Let event handler change exit status */
206e602ba25SJulian Elischer 	PROC_UNLOCK(p);
2071c4bcd05SJeff Roberson 	/* Drain the limit callout while we don't have the proc locked */
2081c4bcd05SJeff Roberson 	callout_drain(&p->p_limco);
209b40ce416SJulian Elischer 
21000c28d96SRobert Watson #ifdef AUDIT
21100c28d96SRobert Watson 	/*
21200c28d96SRobert Watson 	 * The Sun BSM exit token contains two components: an exit status as
21300c28d96SRobert Watson 	 * passed to exit(), and a return value to indicate what sort of exit
21400c28d96SRobert Watson 	 * it was.  The exit status is WEXITSTATUS(rv), but it's not clear
21500c28d96SRobert Watson 	 * what the return value is.
21600c28d96SRobert Watson 	 */
21714961ba7SRobert Watson 	AUDIT_ARG_EXIT(WEXITSTATUS(rv), 0);
21800c28d96SRobert Watson 	AUDIT_SYSCALL_EXIT(0, td);
21900c28d96SRobert Watson #endif
22000c28d96SRobert Watson 
2217a6b989bSJohn Baldwin 	/* Are we a task leader? */
2222c1011f7SJohn Dyson 	if (p == p->p_leader) {
223c6544064SJohn Baldwin 		mtx_lock(&ppeers_lock);
2242c1011f7SJohn Dyson 		q = p->p_peers;
225776e0b36SJohn Baldwin 		while (q != NULL) {
226776e0b36SJohn Baldwin 			PROC_LOCK(q);
227776e0b36SJohn Baldwin 			psignal(q, SIGKILL);
228776e0b36SJohn Baldwin 			PROC_UNLOCK(q);
2292c1011f7SJohn Dyson 			q = q->p_peers;
2302c1011f7SJohn Dyson 		}
231c6544064SJohn Baldwin 		while (p->p_peers != NULL)
232c6544064SJohn Baldwin 			msleep(p, &ppeers_lock, PWAIT, "exit1", 0);
233c6544064SJohn Baldwin 		mtx_unlock(&ppeers_lock);
2342c1011f7SJohn Dyson 	}
2352c1011f7SJohn Dyson 
236fed06968SJulian Elischer 	/*
237e9189611SPeter Wemm 	 * Check if any loadable modules need anything done at process exit.
2386111dcd2SJohn Baldwin 	 * E.g. SYSV IPC stuff
239fed06968SJulian Elischer 	 * XXX what if one of these generates an error?
240fed06968SJulian Elischer 	 */
24175b8b3b2SJohn Baldwin 	EVENTHANDLER_INVOKE(process_exit, p);
242a914fb6bSJohn Baldwin 
243df8bae1dSRodney W. Grimes 	/*
244df8bae1dSRodney W. Grimes 	 * If parent is waiting for us to exit or exec,
245df8bae1dSRodney W. Grimes 	 * P_PPWAIT is set; we will wakeup the parent below.
246df8bae1dSRodney W. Grimes 	 */
247a914fb6bSJohn Baldwin 	PROC_LOCK(p);
248bb63fddeSAlexander Leidinger 	rv = p->p_xstat;	/* Event handler could change exit status */
249a282253aSJulian Elischer 	stopprofclock(p);
250df8bae1dSRodney W. Grimes 	p->p_flag &= ~(P_TRACED | P_PPWAIT);
2515499ea01SJohn Baldwin 
2525499ea01SJohn Baldwin 	/*
2535499ea01SJohn Baldwin 	 * Stop the real interval timer.  If the handler is currently
2545499ea01SJohn Baldwin 	 * executing, prevent it from rearming itself and let it finish.
2555499ea01SJohn Baldwin 	 */
2565499ea01SJohn Baldwin 	if (timevalisset(&p->p_realtimer.it_value) &&
2575499ea01SJohn Baldwin 	    callout_stop(&p->p_itcallout) == 0) {
2585499ea01SJohn Baldwin 		timevalclear(&p->p_realtimer.it_interval);
2595499ea01SJohn Baldwin 		msleep(&p->p_itcallout, &p->p_mtx, PWAIT, "ritwait", 0);
2605499ea01SJohn Baldwin 		KASSERT(!timevalisset(&p->p_realtimer.it_value),
2615499ea01SJohn Baldwin 		    ("realtime timer is still armed"));
2625499ea01SJohn Baldwin 	}
26396d7f8efSTim J. Robbins 	PROC_UNLOCK(p);
264df8bae1dSRodney W. Grimes 
265df8bae1dSRodney W. Grimes 	/*
266831d27a9SDon Lewis 	 * Reset any sigio structures pointing to us as a result of
267831d27a9SDon Lewis 	 * F_SETOWN with our pid.
268831d27a9SDon Lewis 	 */
269831d27a9SDon Lewis 	funsetownlst(&p->p_sigiolst);
270831d27a9SDon Lewis 
271831d27a9SDon Lewis 	/*
272c0bc2867SGleb Smirnoff 	 * If this process has an nlminfo data area (for lockd), release it
273c0bc2867SGleb Smirnoff 	 */
274c0bc2867SGleb Smirnoff 	if (nlminfo_release_p != NULL && p->p_nlminfo != NULL)
275c0bc2867SGleb Smirnoff 		(*nlminfo_release_p)(p);
276c0bc2867SGleb Smirnoff 
277c0bc2867SGleb Smirnoff 	/*
278df8bae1dSRodney W. Grimes 	 * Close open files and release open-file table.
279df8bae1dSRodney W. Grimes 	 * This may block!
280df8bae1dSRodney W. Grimes 	 */
281edf6699aSAlfred Perlstein 	fdfree(td);
282df8bae1dSRodney W. Grimes 
283a914fb6bSJohn Baldwin 	/*
284a2587073SPoul-Henning Kamp 	 * If this thread tickled GEOM, we need to wait for the giggling to
285a2587073SPoul-Henning Kamp 	 * stop before we return to userland
286a2587073SPoul-Henning Kamp 	 */
287a2587073SPoul-Henning Kamp 	if (td->td_pflags & TDP_GEOM)
288a2587073SPoul-Henning Kamp 		g_waitidle();
289a2587073SPoul-Henning Kamp 
290a2587073SPoul-Henning Kamp 	/*
291a914fb6bSJohn Baldwin 	 * Remove ourself from our leader's peer list and wake our leader.
292a914fb6bSJohn Baldwin 	 */
293c6544064SJohn Baldwin 	mtx_lock(&ppeers_lock);
29479fc0bf4SMike Smith 	if (p->p_leader->p_peers) {
29579fc0bf4SMike Smith 		q = p->p_leader;
29679fc0bf4SMike Smith 		while (q->p_peers != p)
29779fc0bf4SMike Smith 			q = q->p_peers;
29879fc0bf4SMike Smith 		q->p_peers = p->p_peers;
2997f05b035SAlfred Perlstein 		wakeup(p->p_leader);
30079fc0bf4SMike Smith 	}
301c6544064SJohn Baldwin 	mtx_unlock(&ppeers_lock);
30279fc0bf4SMike Smith 
30357051fdcSTor Egge 	vmspace_exit(td);
304df8bae1dSRodney W. Grimes 
305ea97757aSJohn Baldwin 	sx_xlock(&proctree_lock);
306df8bae1dSRodney W. Grimes 	if (SESS_LEADER(p)) {
307eaaaf190SEd Schouten 		struct session *sp = p->p_session;
308eaaaf190SEd Schouten 		struct tty *tp;
309df8bae1dSRodney W. Grimes 
310eaaaf190SEd Schouten 		/*
311eaaaf190SEd Schouten 		 * s_ttyp is not zero'd; we use this to indicate that
312eaaaf190SEd Schouten 		 * the session once had a controlling terminal. (for
313eaaaf190SEd Schouten 		 * logging and informational purposes)
314eaaaf190SEd Schouten 		 */
315bc093719SEd Schouten 		SESS_LOCK(sp);
316bc093719SEd Schouten 		ttyvp = sp->s_ttyvp;
317eaaaf190SEd Schouten 		tp = sp->s_ttyp;
318bc093719SEd Schouten 		sp->s_ttyvp = NULL;
3198dc9b4cfSEd Schouten 		sp->s_ttydp = NULL;
320eaaaf190SEd Schouten 		sp->s_leader = NULL;
321bc093719SEd Schouten 		SESS_UNLOCK(sp);
322bc093719SEd Schouten 
323df8bae1dSRodney W. Grimes 		/*
324bc093719SEd Schouten 		 * Signal foreground pgrp and revoke access to
325eaaaf190SEd Schouten 		 * controlling terminal if it has not been revoked
326eaaaf190SEd Schouten 		 * already.
327bc093719SEd Schouten 		 *
328eaaaf190SEd Schouten 		 * Because the TTY may have been revoked in the mean
329eaaaf190SEd Schouten 		 * time and could already have a new session associated
330eaaaf190SEd Schouten 		 * with it, make sure we don't send a SIGHUP to a
331eaaaf190SEd Schouten 		 * foreground process group that does not belong to this
332eaaaf190SEd Schouten 		 * session.
333df8bae1dSRodney W. Grimes 		 */
334bc093719SEd Schouten 
335eaaaf190SEd Schouten 		if (tp != NULL) {
336bc093719SEd Schouten 			tty_lock(tp);
337eaaaf190SEd Schouten 			if (tp->t_session == sp)
338bc093719SEd Schouten 				tty_signal_pgrp(tp, SIGHUP);
339bc093719SEd Schouten 			tty_unlock(tp);
340eaaaf190SEd Schouten 		}
341bc093719SEd Schouten 
3427c6d401cSKonstantin Belousov 		if (ttyvp != NULL) {
343ea97757aSJohn Baldwin 			sx_xunlock(&proctree_lock);
3446728dc16SKonstantin Belousov 			if (vn_lock(ttyvp, LK_EXCLUSIVE) == 0) {
345f591779bSSeigo Tanimura 				VOP_REVOKE(ttyvp, REVOKEALL);
346bc093719SEd Schouten 				VOP_UNLOCK(ttyvp, 0);
3476728dc16SKonstantin Belousov 			}
348ea97757aSJohn Baldwin 			sx_xlock(&proctree_lock);
349f591779bSSeigo Tanimura 		}
350f591779bSSeigo Tanimura 	}
351df8bae1dSRodney W. Grimes 	fixjobc(p, p->p_pgrp, 0);
352ea97757aSJohn Baldwin 	sx_xunlock(&proctree_lock);
353b40ce416SJulian Elischer 	(void)acct_process(td);
354bc093719SEd Schouten 
355bc093719SEd Schouten 	/* Release the TTY now we've unlocked everything. */
356bc093719SEd Schouten 	if (ttyvp != NULL)
357bc093719SEd Schouten 		vrele(ttyvp);
358df8bae1dSRodney W. Grimes #ifdef KTRACE
3592c255e9dSRobert Watson 	ktrprocexit(td);
360df8bae1dSRodney W. Grimes #endif
361df8bae1dSRodney W. Grimes 	/*
362ee42d0a9SDavid Malone 	 * Release reference to text vnode
363ee42d0a9SDavid Malone 	 */
364ee42d0a9SDavid Malone 	if ((vtmp = p->p_textvp) != NULL) {
365ee42d0a9SDavid Malone 		p->p_textvp = NULL;
36657606880SChristian S.J. Peron 		locked = VFS_LOCK_GIANT(vtmp->v_mount);
367ee42d0a9SDavid Malone 		vrele(vtmp);
36857606880SChristian S.J. Peron 		VFS_UNLOCK_GIANT(locked);
369ee42d0a9SDavid Malone 	}
370ee42d0a9SDavid Malone 
371ee42d0a9SDavid Malone 	/*
372d7aadbf9SJohn Baldwin 	 * Release our limits structure.
373d7aadbf9SJohn Baldwin 	 */
37491d5354aSJohn Baldwin 	PROC_LOCK(p);
37591d5354aSJohn Baldwin 	plim = p->p_limit;
376d7aadbf9SJohn Baldwin 	p->p_limit = NULL;
37791d5354aSJohn Baldwin 	PROC_UNLOCK(p);
37891d5354aSJohn Baldwin 	lim_free(plim);
379d7aadbf9SJohn Baldwin 
380cf7d9a8cSDavid Xu 	tidhash_remove(td);
381cf7d9a8cSDavid Xu 
382d7aadbf9SJohn Baldwin 	/*
383df8bae1dSRodney W. Grimes 	 * Remove proc from allproc queue and pidhash chain.
384df8bae1dSRodney W. Grimes 	 * Place onto zombproc.  Unlink from parent's child list.
385df8bae1dSRodney W. Grimes 	 */
3861005a129SJohn Baldwin 	sx_xlock(&allproc_lock);
387b75356e1SJeffrey Hsu 	LIST_REMOVE(p, p_list);
388b75356e1SJeffrey Hsu 	LIST_INSERT_HEAD(&zombproc, p, p_list);
389b75356e1SJeffrey Hsu 	LIST_REMOVE(p, p_hash);
3901005a129SJohn Baldwin 	sx_xunlock(&allproc_lock);
391df8bae1dSRodney W. Grimes 
39237f84a60SJohn Baldwin 	/*
3931bba2a94SJohn Baldwin 	 * Call machine-dependent code to release any
3941bba2a94SJohn Baldwin 	 * machine-dependent resources other than the address space.
3951bba2a94SJohn Baldwin 	 * The address space is released by "vmspace_exitfree(p)" in
3961bba2a94SJohn Baldwin 	 * vm_waitproc().
3971bba2a94SJohn Baldwin 	 */
3981bba2a94SJohn Baldwin 	cpu_exit(td);
3991bba2a94SJohn Baldwin 
4001bba2a94SJohn Baldwin 	WITNESS_WARN(WARN_PANIC, NULL, "process (pid %d) exiting", p->p_pid);
4011bba2a94SJohn Baldwin 
4021bba2a94SJohn Baldwin 	/*
40337f84a60SJohn Baldwin 	 * Reparent all of our children to init.
40437f84a60SJohn Baldwin 	 */
4051005a129SJohn Baldwin 	sx_xlock(&proctree_lock);
4062e3c8fcbSPoul-Henning Kamp 	q = LIST_FIRST(&p->p_children);
407a914fb6bSJohn Baldwin 	if (q != NULL)		/* only need this if any child is S_ZOMB */
4087f05b035SAlfred Perlstein 		wakeup(initproc);
409a914fb6bSJohn Baldwin 	for (; q != NULL; q = nq) {
4102e3c8fcbSPoul-Henning Kamp 		nq = LIST_NEXT(q, p_sibling);
411a914fb6bSJohn Baldwin 		PROC_LOCK(q);
412c65437a3SJohn Baldwin 		proc_reparent(q, initproc);
4134ac9ae70SJulian Elischer 		q->p_sigparent = SIGCHLD;
414df8bae1dSRodney W. Grimes 		/*
415df8bae1dSRodney W. Grimes 		 * Traced processes are killed
416df8bae1dSRodney W. Grimes 		 * since their existence means someone is screwing up.
417df8bae1dSRodney W. Grimes 		 */
418df8bae1dSRodney W. Grimes 		if (q->p_flag & P_TRACED) {
419904c5ec4SDavid Xu 			struct thread *temp;
420904c5ec4SDavid Xu 
421c2836532SDavid Xu 			q->p_flag &= ~(P_TRACED | P_STOPPED_TRACE);
422904c5ec4SDavid Xu 			FOREACH_THREAD_IN_PROC(q, temp)
423904c5ec4SDavid Xu 				temp->td_dbgflags &= ~TDB_SUSPEND;
424df8bae1dSRodney W. Grimes 			psignal(q, SIGKILL);
425c65437a3SJohn Baldwin 		}
426a914fb6bSJohn Baldwin 		PROC_UNLOCK(q);
427df8bae1dSRodney W. Grimes 	}
428df8bae1dSRodney W. Grimes 
4291c4bcd05SJeff Roberson 	/* Save exit status. */
430d7aadbf9SJohn Baldwin 	PROC_LOCK(p);
431cbf4e354SDavid Xu 	p->p_xthread = td;
4325d217f17SJohn Birrell 
4330304c731SJamie Gritton 	/* Tell the prison that we are gone. */
434413628a7SBjoern A. Zeeb 	prison_proc_free(p->p_ucred->cr_prison);
435413628a7SBjoern A. Zeeb 
4365d217f17SJohn Birrell #ifdef KDTRACE_HOOKS
4375d217f17SJohn Birrell 	/*
4385d217f17SJohn Birrell 	 * Tell the DTrace fasttrap provider about the exit if it
4395d217f17SJohn Birrell 	 * has declared an interest.
4405d217f17SJohn Birrell 	 */
4415d217f17SJohn Birrell 	if (dtrace_fasttrap_exit)
4425d217f17SJohn Birrell 		dtrace_fasttrap_exit(p);
4435d217f17SJohn Birrell #endif
4445d217f17SJohn Birrell 
4451c4bcd05SJeff Roberson 	/*
4467a6b989bSJohn Baldwin 	 * Notify interested parties of our demise.
447cb679c38SJonathan Lemon 	 */
448ad3b9257SJohn-Mark Gurney 	KNOTE_LOCKED(&p->p_klist, NOTE_EXIT);
4497eaec467SJohn Baldwin 
4505d217f17SJohn Birrell #ifdef KDTRACE_HOOKS
4515d217f17SJohn Birrell 	int reason = CLD_EXITED;
4525d217f17SJohn Birrell 	if (WCOREDUMP(rv))
4535d217f17SJohn Birrell 		reason = CLD_DUMPED;
4545d217f17SJohn Birrell 	else if (WIFSIGNALED(rv))
4555d217f17SJohn Birrell 		reason = CLD_KILLED;
4565d217f17SJohn Birrell 	SDT_PROBE(proc, kernel, , exit, reason, 0, 0, 0, 0);
4575d217f17SJohn Birrell #endif
4585d217f17SJohn Birrell 
4591a29c806SOlivier Houchard 	/*
4601a29c806SOlivier Houchard 	 * Just delete all entries in the p_klist. At this point we won't
4611a29c806SOlivier Houchard 	 * report any more events, and there are nasty race conditions that
4621a29c806SOlivier Houchard 	 * can beat us if we don't.
4631a29c806SOlivier Houchard 	 */
464ad3b9257SJohn-Mark Gurney 	knlist_clear(&p->p_klist, 1);
465cb679c38SJonathan Lemon 
466cb679c38SJonathan Lemon 	/*
467645682fdSLuoqi Chen 	 * Notify parent that we're gone.  If parent has the PS_NOCLDWAIT
468ba1551caSIan Dowse 	 * flag set, or if the handler is set to SIG_IGN, notify process
469ba1551caSIan Dowse 	 * 1 instead (and hope it will handle this situation).
470df8bae1dSRodney W. Grimes 	 */
471d7aadbf9SJohn Baldwin 	PROC_LOCK(p->p_pptr);
47290af4afaSJohn Baldwin 	mtx_lock(&p->p_pptr->p_sigacts->ps_mtx);
47390af4afaSJohn Baldwin 	if (p->p_pptr->p_sigacts->ps_flag & (PS_NOCLDWAIT | PS_CLDSIGIGN)) {
474276c5169SJohn Baldwin 		struct proc *pp;
475276c5169SJohn Baldwin 
47690af4afaSJohn Baldwin 		mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx);
477276c5169SJohn Baldwin 		pp = p->p_pptr;
478f591779bSSeigo Tanimura 		PROC_UNLOCK(pp);
479245f17d4SJoerg Wunsch 		proc_reparent(p, initproc);
48055b5f2a2SDon Lewis 		p->p_sigparent = SIGCHLD;
481f591779bSSeigo Tanimura 		PROC_LOCK(p->p_pptr);
482007abb3dSKonstantin Belousov 
483245f17d4SJoerg Wunsch 		/*
484007abb3dSKonstantin Belousov 		 * Notify parent, so in case he was wait(2)ing or
4856fae832aSKonstantin Belousov 		 * executing waitpid(2) with our pid, he will
486245f17d4SJoerg Wunsch 		 * continue.
487245f17d4SJoerg Wunsch 		 */
4887f05b035SAlfred Perlstein 		wakeup(pp);
48990af4afaSJohn Baldwin 	} else
49090af4afaSJohn Baldwin 		mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx);
491245f17d4SJoerg Wunsch 
4926567eef7SDon Lewis 	if (p->p_pptr == initproc)
4936626c604SJulian Elischer 		psignal(p->p_pptr, SIGCHLD);
494ebceaf6dSDavid Xu 	else if (p->p_sigparent != 0) {
495ebceaf6dSDavid Xu 		if (p->p_sigparent == SIGCHLD)
496ebceaf6dSDavid Xu 			childproc_exited(p);
497ebceaf6dSDavid Xu 		else	/* LINUX thread */
4986567eef7SDon Lewis 			psignal(p->p_pptr, p->p_sigparent);
499ebceaf6dSDavid Xu 	}
500d7aadbf9SJohn Baldwin 	sx_xunlock(&proctree_lock);
501696058c3SJulian Elischer 
502eb30c1c0SPeter Wemm 	/*
503f71e748dSDavid Xu 	 * The state PRS_ZOMBIE prevents other proesses from sending
504f71e748dSDavid Xu 	 * signal to the process, to avoid memory leak, we free memory
505f71e748dSDavid Xu 	 * for signal queue at the time when the state is set.
506f71e748dSDavid Xu 	 */
507f71e748dSDavid Xu 	sigqueue_flush(&p->p_sigqueue);
508f71e748dSDavid Xu 	sigqueue_flush(&td->td_sigqueue);
509f71e748dSDavid Xu 
510f71e748dSDavid Xu 	/*
511462f31bfSJohn Baldwin 	 * We have to wait until after acquiring all locks before
512a9a64385SJohn Baldwin 	 * changing p_state.  We need to avoid all possible context
513a9a64385SJohn Baldwin 	 * switches (including ones from blocking on a mutex) while
514ddf9c4f7SJohn Baldwin 	 * marked as a zombie.  We also have to set the zombie state
515ddf9c4f7SJohn Baldwin 	 * before we release the parent process' proc lock to avoid
516ddf9c4f7SJohn Baldwin 	 * a lost wakeup.  So, we first call wakeup, then we grab the
517ddf9c4f7SJohn Baldwin 	 * sched lock, update the state, and release the parent process'
518ddf9c4f7SJohn Baldwin 	 * proc lock.
519eb30c1c0SPeter Wemm 	 */
520ddf9c4f7SJohn Baldwin 	wakeup(p->p_pptr);
521aeb32571SKonstantin Belousov 	cv_broadcast(&p->p_pwait);
522982d11f8SJeff Roberson 	sched_exit(p->p_pptr, td);
523982d11f8SJeff Roberson 	PROC_SLOCK(p);
524e602ba25SJulian Elischer 	p->p_state = PRS_ZOMBIE;
525d7aadbf9SJohn Baldwin 	PROC_UNLOCK(p->p_pptr);
526871684b8SBruce Evans 
527f6f230feSJeff Roberson 	/*
5287eaec467SJohn Baldwin 	 * Hopefully no one will try to deliver a signal to the process this
529ad3b9257SJohn-Mark Gurney 	 * late in the game.
530ad3b9257SJohn-Mark Gurney 	 */
531ad3b9257SJohn-Mark Gurney 	knlist_destroy(&p->p_klist);
532ad3b9257SJohn-Mark Gurney 
533ad3b9257SJohn-Mark Gurney 	/*
534a140976eSAttilio Rao 	 * Save our children's rusage information in our exit rusage.
535a140976eSAttilio Rao 	 */
536a140976eSAttilio Rao 	ruadd(&p->p_ru, &p->p_rux, &p->p_stats->p_cru, &p->p_crux);
537a140976eSAttilio Rao 
538a140976eSAttilio Rao 	/*
539696058c3SJulian Elischer 	 * Make sure the scheduler takes this thread out of its tables etc.
540e602ba25SJulian Elischer 	 * This will also release this thread's reference to the ucred.
541696058c3SJulian Elischer 	 * Other thread parts to release include pcb bits and such.
542e602ba25SJulian Elischer 	 */
543e602ba25SJulian Elischer 	thread_exit();
544df8bae1dSRodney W. Grimes }
545df8bae1dSRodney W. Grimes 
54625f6e35aSPoul-Henning Kamp 
54725f6e35aSPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_
54825f6e35aSPoul-Henning Kamp struct abort2_args {
54925f6e35aSPoul-Henning Kamp 	char *why;
55025f6e35aSPoul-Henning Kamp 	int nargs;
55125f6e35aSPoul-Henning Kamp 	void **args;
55225f6e35aSPoul-Henning Kamp };
55325f6e35aSPoul-Henning Kamp #endif
55425f6e35aSPoul-Henning Kamp 
55525f6e35aSPoul-Henning Kamp int
55625f6e35aSPoul-Henning Kamp abort2(struct thread *td, struct abort2_args *uap)
55725f6e35aSPoul-Henning Kamp {
55825f6e35aSPoul-Henning Kamp 	struct proc *p = td->td_proc;
55925f6e35aSPoul-Henning Kamp 	struct sbuf *sb;
56025f6e35aSPoul-Henning Kamp 	void *uargs[16];
56125f6e35aSPoul-Henning Kamp 	int error, i, sig;
56225f6e35aSPoul-Henning Kamp 
56325f6e35aSPoul-Henning Kamp 	/*
56425f6e35aSPoul-Henning Kamp 	 * Do it right now so we can log either proper call of abort2(), or
56525f6e35aSPoul-Henning Kamp 	 * note, that invalid argument was passed. 512 is big enough to
56625f6e35aSPoul-Henning Kamp 	 * handle 16 arguments' descriptions with additional comments.
56725f6e35aSPoul-Henning Kamp 	 */
56825f6e35aSPoul-Henning Kamp 	sb = sbuf_new(NULL, NULL, 512, SBUF_FIXEDLEN);
56925f6e35aSPoul-Henning Kamp 	sbuf_clear(sb);
57025f6e35aSPoul-Henning Kamp 	sbuf_printf(sb, "%s(pid %d uid %d) aborted: ",
57125f6e35aSPoul-Henning Kamp 	    p->p_comm, p->p_pid, td->td_ucred->cr_uid);
57225f6e35aSPoul-Henning Kamp 	/*
57325f6e35aSPoul-Henning Kamp 	 * Since we can't return from abort2(), send SIGKILL in cases, where
57425f6e35aSPoul-Henning Kamp 	 * abort2() was called improperly
57525f6e35aSPoul-Henning Kamp 	 */
57625f6e35aSPoul-Henning Kamp 	sig = SIGKILL;
57725f6e35aSPoul-Henning Kamp 	/* Prevent from DoSes from user-space. */
57825f6e35aSPoul-Henning Kamp 	if (uap->nargs < 0 || uap->nargs > 16)
57925f6e35aSPoul-Henning Kamp 		goto out;
5804218a731SPoul-Henning Kamp 	if (uap->nargs > 0) {
58125f6e35aSPoul-Henning Kamp 		if (uap->args == NULL)
58225f6e35aSPoul-Henning Kamp 			goto out;
58325f6e35aSPoul-Henning Kamp 		error = copyin(uap->args, uargs, uap->nargs * sizeof(void *));
58425f6e35aSPoul-Henning Kamp 		if (error != 0)
58525f6e35aSPoul-Henning Kamp 			goto out;
5864218a731SPoul-Henning Kamp 	}
58725f6e35aSPoul-Henning Kamp 	/*
58825f6e35aSPoul-Henning Kamp 	 * Limit size of 'reason' string to 128. Will fit even when
58925f6e35aSPoul-Henning Kamp 	 * maximal number of arguments was chosen to be logged.
59025f6e35aSPoul-Henning Kamp 	 */
59125f6e35aSPoul-Henning Kamp 	if (uap->why != NULL) {
59225f6e35aSPoul-Henning Kamp 		error = sbuf_copyin(sb, uap->why, 128);
59325f6e35aSPoul-Henning Kamp 		if (error < 0)
59425f6e35aSPoul-Henning Kamp 			goto out;
59525f6e35aSPoul-Henning Kamp 	} else {
59625f6e35aSPoul-Henning Kamp 		sbuf_printf(sb, "(null)");
59725f6e35aSPoul-Henning Kamp 	}
5984218a731SPoul-Henning Kamp 	if (uap->nargs > 0) {
59925f6e35aSPoul-Henning Kamp 		sbuf_printf(sb, "(");
60025f6e35aSPoul-Henning Kamp 		for (i = 0;i < uap->nargs; i++)
60125f6e35aSPoul-Henning Kamp 			sbuf_printf(sb, "%s%p", i == 0 ? "" : ", ", uargs[i]);
60225f6e35aSPoul-Henning Kamp 		sbuf_printf(sb, ")");
60325f6e35aSPoul-Henning Kamp 	}
60425f6e35aSPoul-Henning Kamp 	/*
60525f6e35aSPoul-Henning Kamp 	 * Final stage: arguments were proper, string has been
60625f6e35aSPoul-Henning Kamp 	 * successfully copied from userspace, and copying pointers
60725f6e35aSPoul-Henning Kamp 	 * from user-space succeed.
60825f6e35aSPoul-Henning Kamp 	 */
60925f6e35aSPoul-Henning Kamp 	sig = SIGABRT;
61025f6e35aSPoul-Henning Kamp out:
61125f6e35aSPoul-Henning Kamp 	if (sig == SIGKILL) {
61225f6e35aSPoul-Henning Kamp 		sbuf_trim(sb);
61325f6e35aSPoul-Henning Kamp 		sbuf_printf(sb, " (Reason text inaccessible)");
61425f6e35aSPoul-Henning Kamp 	}
61525f6e35aSPoul-Henning Kamp 	sbuf_cat(sb, "\n");
61625f6e35aSPoul-Henning Kamp 	sbuf_finish(sb);
61725f6e35aSPoul-Henning Kamp 	log(LOG_INFO, "%s", sbuf_data(sb));
61825f6e35aSPoul-Henning Kamp 	sbuf_delete(sb);
61925f6e35aSPoul-Henning Kamp 	exit1(td, W_EXITCODE(0, sig));
62025f6e35aSPoul-Henning Kamp 	return (0);
62125f6e35aSPoul-Henning Kamp }
62225f6e35aSPoul-Henning Kamp 
62325f6e35aSPoul-Henning Kamp 
624b2f9e8b1SBruce Evans #ifdef COMPAT_43
625234216efSMatthew Dillon /*
626a9a64385SJohn Baldwin  * The dirty work is handled by kern_wait().
627234216efSMatthew Dillon  */
62826f9a767SRodney W. Grimes int
629830c3153SDag-Erling Smørgrav owait(struct thread *td, struct owait_args *uap __unused)
630df8bae1dSRodney W. Grimes {
631b7e23e82SJohn Baldwin 	int error, status;
632df8bae1dSRodney W. Grimes 
633b7e23e82SJohn Baldwin 	error = kern_wait(td, WAIT_ANY, &status, 0, NULL);
634b7e23e82SJohn Baldwin 	if (error == 0)
635b7e23e82SJohn Baldwin 		td->td_retval[1] = status;
636b7e23e82SJohn Baldwin 	return (error);
637df8bae1dSRodney W. Grimes }
638b2f9e8b1SBruce Evans #endif /* COMPAT_43 */
639df8bae1dSRodney W. Grimes 
640234216efSMatthew Dillon /*
641a9a64385SJohn Baldwin  * The dirty work is handled by kern_wait().
642234216efSMatthew Dillon  */
64326f9a767SRodney W. Grimes int
644830c3153SDag-Erling Smørgrav wait4(struct thread *td, struct wait_args *uap)
645df8bae1dSRodney W. Grimes {
64678c85e8dSJohn Baldwin 	struct rusage ru, *rup;
647b7e23e82SJohn Baldwin 	int error, status;
648b40ce416SJulian Elischer 
64978c85e8dSJohn Baldwin 	if (uap->rusage != NULL)
65078c85e8dSJohn Baldwin 		rup = &ru;
65178c85e8dSJohn Baldwin 	else
65278c85e8dSJohn Baldwin 		rup = NULL;
65378c85e8dSJohn Baldwin 	error = kern_wait(td, uap->pid, &status, uap->options, rup);
654b7e23e82SJohn Baldwin 	if (uap->status != NULL && error == 0)
655b7e23e82SJohn Baldwin 		error = copyout(&status, uap->status, sizeof(status));
656b7e23e82SJohn Baldwin 	if (uap->rusage != NULL && error == 0)
657b7e23e82SJohn Baldwin 		error = copyout(&ru, uap->rusage, sizeof(struct rusage));
658b7e23e82SJohn Baldwin 	return (error);
659df8bae1dSRodney W. Grimes }
660df8bae1dSRodney W. Grimes 
661324fb6beSRobert Watson /*
662324fb6beSRobert Watson  * Reap the remains of a zombie process and optionally return status and
663324fb6beSRobert Watson  * rusage.  Asserts and will release both the proctree_lock and the process
664324fb6beSRobert Watson  * lock as part of its work.
665324fb6beSRobert Watson  */
666324fb6beSRobert Watson static void
667324fb6beSRobert Watson proc_reap(struct thread *td, struct proc *p, int *status, int options,
668324fb6beSRobert Watson     struct rusage *rusage)
669324fb6beSRobert Watson {
670324fb6beSRobert Watson 	struct proc *q, *t;
671324fb6beSRobert Watson 
672324fb6beSRobert Watson 	sx_assert(&proctree_lock, SA_XLOCKED);
673324fb6beSRobert Watson 	PROC_LOCK_ASSERT(p, MA_OWNED);
674324fb6beSRobert Watson 	PROC_SLOCK_ASSERT(p, MA_OWNED);
675324fb6beSRobert Watson 	KASSERT(p->p_state == PRS_ZOMBIE, ("proc_reap: !PRS_ZOMBIE"));
676324fb6beSRobert Watson 
677324fb6beSRobert Watson 	q = td->td_proc;
678324fb6beSRobert Watson 	if (rusage) {
679324fb6beSRobert Watson 		*rusage = p->p_ru;
680324fb6beSRobert Watson 		calcru(p, &rusage->ru_utime, &rusage->ru_stime);
681324fb6beSRobert Watson 	}
682324fb6beSRobert Watson 	PROC_SUNLOCK(p);
683324fb6beSRobert Watson 	td->td_retval[0] = p->p_pid;
684324fb6beSRobert Watson 	if (status)
685324fb6beSRobert Watson 		*status = p->p_xstat;	/* convert to int */
686324fb6beSRobert Watson 	if (options & WNOWAIT) {
687324fb6beSRobert Watson 		/*
688324fb6beSRobert Watson 		 *  Only poll, returning the status.  Caller does not wish to
689324fb6beSRobert Watson 		 * release the proc struct just yet.
690324fb6beSRobert Watson 		 */
691324fb6beSRobert Watson 		PROC_UNLOCK(p);
692324fb6beSRobert Watson 		sx_xunlock(&proctree_lock);
693324fb6beSRobert Watson 		return;
694324fb6beSRobert Watson 	}
695324fb6beSRobert Watson 
696324fb6beSRobert Watson 	PROC_LOCK(q);
697324fb6beSRobert Watson 	sigqueue_take(p->p_ksi);
698324fb6beSRobert Watson 	PROC_UNLOCK(q);
699324fb6beSRobert Watson 	PROC_UNLOCK(p);
700324fb6beSRobert Watson 
701324fb6beSRobert Watson 	/*
702324fb6beSRobert Watson 	 * If we got the child via a ptrace 'attach', we need to give it back
703324fb6beSRobert Watson 	 * to the old parent.
704324fb6beSRobert Watson 	 */
705324fb6beSRobert Watson 	if (p->p_oppid && (t = pfind(p->p_oppid)) != NULL) {
706324fb6beSRobert Watson 		PROC_LOCK(p);
707324fb6beSRobert Watson 		p->p_oppid = 0;
708324fb6beSRobert Watson 		proc_reparent(p, t);
709324fb6beSRobert Watson 		PROC_UNLOCK(p);
710ad6eec7bSJohn Baldwin 		pksignal(t, SIGCHLD, p->p_ksi);
711324fb6beSRobert Watson 		wakeup(t);
712324fb6beSRobert Watson 		cv_broadcast(&p->p_pwait);
713324fb6beSRobert Watson 		PROC_UNLOCK(t);
714324fb6beSRobert Watson 		sx_xunlock(&proctree_lock);
715324fb6beSRobert Watson 		return;
716324fb6beSRobert Watson 	}
717324fb6beSRobert Watson 
718324fb6beSRobert Watson 	/*
719324fb6beSRobert Watson 	 * Remove other references to this process to ensure we have an
720324fb6beSRobert Watson 	 * exclusive reference.
721324fb6beSRobert Watson 	 */
722324fb6beSRobert Watson 	sx_xlock(&allproc_lock);
723324fb6beSRobert Watson 	LIST_REMOVE(p, p_list);	/* off zombproc */
724324fb6beSRobert Watson 	sx_xunlock(&allproc_lock);
725324fb6beSRobert Watson 	LIST_REMOVE(p, p_sibling);
726324fb6beSRobert Watson 	leavepgrp(p);
727324fb6beSRobert Watson 	sx_xunlock(&proctree_lock);
728324fb6beSRobert Watson 
729324fb6beSRobert Watson 	/*
730324fb6beSRobert Watson 	 * As a side effect of this lock, we know that all other writes to
731324fb6beSRobert Watson 	 * this proc are visible now, so no more locking is needed for p.
732324fb6beSRobert Watson 	 */
733324fb6beSRobert Watson 	PROC_LOCK(p);
734324fb6beSRobert Watson 	p->p_xstat = 0;		/* XXX: why? */
735324fb6beSRobert Watson 	PROC_UNLOCK(p);
736324fb6beSRobert Watson 	PROC_LOCK(q);
737324fb6beSRobert Watson 	ruadd(&q->p_stats->p_cru, &q->p_crux, &p->p_ru, &p->p_rux);
738324fb6beSRobert Watson 	PROC_UNLOCK(q);
739324fb6beSRobert Watson 
740324fb6beSRobert Watson 	/*
741324fb6beSRobert Watson 	 * Decrement the count of procs running with this uid.
742324fb6beSRobert Watson 	 */
743324fb6beSRobert Watson 	(void)chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0);
744324fb6beSRobert Watson 
745324fb6beSRobert Watson 	/*
746097055e2SEdward Tomasz Napierala 	 * Destroy resource accounting information associated with the process.
747097055e2SEdward Tomasz Napierala 	 */
748097055e2SEdward Tomasz Napierala 	racct_proc_exit(p);
749*58c77a9dSEdward Tomasz Napierala 	PROC_LOCK(p->p_pptr);
750*58c77a9dSEdward Tomasz Napierala 	racct_sub(p->p_pptr, RACCT_NPROC, 1);
751*58c77a9dSEdward Tomasz Napierala 	PROC_UNLOCK(p->p_pptr);
752097055e2SEdward Tomasz Napierala 
753097055e2SEdward Tomasz Napierala 	/*
754324fb6beSRobert Watson 	 * Free credentials, arguments, and sigacts.
755324fb6beSRobert Watson 	 */
756324fb6beSRobert Watson 	crfree(p->p_ucred);
757324fb6beSRobert Watson 	p->p_ucred = NULL;
758324fb6beSRobert Watson 	pargs_drop(p->p_args);
759324fb6beSRobert Watson 	p->p_args = NULL;
760324fb6beSRobert Watson 	sigacts_free(p->p_sigacts);
761324fb6beSRobert Watson 	p->p_sigacts = NULL;
762324fb6beSRobert Watson 
763324fb6beSRobert Watson 	/*
764324fb6beSRobert Watson 	 * Do any thread-system specific cleanups.
765324fb6beSRobert Watson 	 */
766324fb6beSRobert Watson 	thread_wait(p);
767324fb6beSRobert Watson 
768324fb6beSRobert Watson 	/*
769324fb6beSRobert Watson 	 * Give vm and machine-dependent layer a chance to free anything that
770324fb6beSRobert Watson 	 * cpu_exit couldn't release while still running in process context.
771324fb6beSRobert Watson 	 */
772324fb6beSRobert Watson 	vm_waitproc(p);
773324fb6beSRobert Watson #ifdef MAC
774324fb6beSRobert Watson 	mac_proc_destroy(p);
775324fb6beSRobert Watson #endif
776324fb6beSRobert Watson 	KASSERT(FIRST_THREAD_IN_PROC(p),
777324fb6beSRobert Watson 	    ("proc_reap: no residual thread!"));
778324fb6beSRobert Watson 	uma_zfree(proc_zone, p);
779324fb6beSRobert Watson 	sx_xlock(&allproc_lock);
780324fb6beSRobert Watson 	nprocs--;
781324fb6beSRobert Watson 	sx_xunlock(&allproc_lock);
782324fb6beSRobert Watson }
783324fb6beSRobert Watson 
784b7e23e82SJohn Baldwin int
7857eaec467SJohn Baldwin kern_wait(struct thread *td, pid_t pid, int *status, int options,
7867eaec467SJohn Baldwin     struct rusage *rusage)
787df8bae1dSRodney W. Grimes {
788324fb6beSRobert Watson 	struct proc *p, *q;
789a9a64385SJohn Baldwin 	int error, nfound;
790df8bae1dSRodney W. Grimes 
79114961ba7SRobert Watson 	AUDIT_ARG_PID(pid);
7922ef24ddeSRobert Watson 	AUDIT_ARG_VALUE(options);
793de3007e8SWayne Salamon 
794b40ce416SJulian Elischer 	q = td->td_proc;
795b7e23e82SJohn Baldwin 	if (pid == 0) {
796f591779bSSeigo Tanimura 		PROC_LOCK(q);
797b7e23e82SJohn Baldwin 		pid = -q->p_pgid;
798f591779bSSeigo Tanimura 		PROC_UNLOCK(q);
799f591779bSSeigo Tanimura 	}
800cbc15844SKonstantin Belousov 	if (options &~ (WUNTRACED|WNOHANG|WCONTINUED|WNOWAIT|WLINUXCLONE))
8016dc958b9SJohn Baldwin 		return (EINVAL);
802df8bae1dSRodney W. Grimes loop:
803902c0d82SDavid Xu 	if (q->p_flag & P_STATCHILD) {
804902c0d82SDavid Xu 		PROC_LOCK(q);
805902c0d82SDavid Xu 		q->p_flag &= ~P_STATCHILD;
806902c0d82SDavid Xu 		PROC_UNLOCK(q);
807902c0d82SDavid Xu 	}
808df8bae1dSRodney W. Grimes 	nfound = 0;
809d7aadbf9SJohn Baldwin 	sx_xlock(&proctree_lock);
8102e3c8fcbSPoul-Henning Kamp 	LIST_FOREACH(p, &q->p_children, p_sibling) {
811f591779bSSeigo Tanimura 		PROC_LOCK(p);
812b7e23e82SJohn Baldwin 		if (pid != WAIT_ANY &&
813b7e23e82SJohn Baldwin 		    p->p_pid != pid && p->p_pgid != -pid) {
814f591779bSSeigo Tanimura 			PROC_UNLOCK(p);
815df8bae1dSRodney W. Grimes 			continue;
816f591779bSSeigo Tanimura 		}
817babe9a2bSRobert Watson 		if (p_canwait(td, p)) {
818babe9a2bSRobert Watson 			PROC_UNLOCK(p);
819babe9a2bSRobert Watson 			continue;
820babe9a2bSRobert Watson 		}
8214ac9ae70SJulian Elischer 
8221156bc4dSJake Burkholder 		/*
8231156bc4dSJake Burkholder 		 * This special case handles a kthread spawned by linux_clone
8241156bc4dSJake Burkholder 		 * (see linux_misc.c).  The linux_wait4 and linux_waitpid
8251156bc4dSJake Burkholder 		 * functions need to be able to distinguish between waiting
8261156bc4dSJake Burkholder 		 * on a process and waiting on a thread.  It is a thread if
8271156bc4dSJake Burkholder 		 * p_sigparent is not SIGCHLD, and the WLINUXCLONE option
8281156bc4dSJake Burkholder 		 * signifies we want to wait for threads and not processes.
8294ac9ae70SJulian Elischer 		 */
8301156bc4dSJake Burkholder 		if ((p->p_sigparent != SIGCHLD) ^
831b7e23e82SJohn Baldwin 		    ((options & WLINUXCLONE) != 0)) {
832a914fb6bSJohn Baldwin 			PROC_UNLOCK(p);
8334ac9ae70SJulian Elischer 			continue;
834a914fb6bSJohn Baldwin 		}
8354ac9ae70SJulian Elischer 
836df8bae1dSRodney W. Grimes 		nfound++;
837982d11f8SJeff Roberson 		PROC_SLOCK(p);
838a140976eSAttilio Rao 		if (p->p_state == PRS_ZOMBIE) {
839324fb6beSRobert Watson 			proc_reap(td, p, status, options, rusage);
840d7aadbf9SJohn Baldwin 			return (0);
841df8bae1dSRodney W. Grimes 		}
8425a2f73e6SDavid Xu 		if ((p->p_flag & P_STOPPED_SIG) &&
8435a2f73e6SDavid Xu 		    (p->p_suspcount == p->p_numthreads) &&
844a9a64385SJohn Baldwin 		    (p->p_flag & P_WAITED) == 0 &&
845b7e23e82SJohn Baldwin 		    (p->p_flag & P_TRACED || options & WUNTRACED)) {
846982d11f8SJeff Roberson 			PROC_SUNLOCK(p);
847df8bae1dSRodney W. Grimes 			p->p_flag |= P_WAITED;
848d7aadbf9SJohn Baldwin 			sx_xunlock(&proctree_lock);
849b40ce416SJulian Elischer 			td->td_retval[0] = p->p_pid;
850b7e23e82SJohn Baldwin 			if (status)
851b7e23e82SJohn Baldwin 				*status = W_STOPCODE(p->p_xstat);
852ebceaf6dSDavid Xu 
853ebceaf6dSDavid Xu 			PROC_LOCK(q);
854ebceaf6dSDavid Xu 			sigqueue_take(p->p_ksi);
855ebceaf6dSDavid Xu 			PROC_UNLOCK(q);
856e94cc4acSDavid Xu 			PROC_UNLOCK(p);
857ebceaf6dSDavid Xu 
858b7e23e82SJohn Baldwin 			return (0);
859df8bae1dSRodney W. Grimes 		}
860982d11f8SJeff Roberson 		PROC_SUNLOCK(p);
861b7e23e82SJohn Baldwin 		if (options & WCONTINUED && (p->p_flag & P_CONTINUED)) {
8626ee093fbSMike Barcroft 			sx_xunlock(&proctree_lock);
8636ee093fbSMike Barcroft 			td->td_retval[0] = p->p_pid;
8646ee093fbSMike Barcroft 			p->p_flag &= ~P_CONTINUED;
8656ee093fbSMike Barcroft 
866ebceaf6dSDavid Xu 			PROC_LOCK(q);
867ebceaf6dSDavid Xu 			sigqueue_take(p->p_ksi);
868ebceaf6dSDavid Xu 			PROC_UNLOCK(q);
869e94cc4acSDavid Xu 			PROC_UNLOCK(p);
870ebceaf6dSDavid Xu 
871b7e23e82SJohn Baldwin 			if (status)
872b7e23e82SJohn Baldwin 				*status = SIGCONT;
873b7e23e82SJohn Baldwin 			return (0);
8746ee093fbSMike Barcroft 		}
875d7aadbf9SJohn Baldwin 		PROC_UNLOCK(p);
876d7aadbf9SJohn Baldwin 	}
877d7aadbf9SJohn Baldwin 	if (nfound == 0) {
878d7aadbf9SJohn Baldwin 		sx_xunlock(&proctree_lock);
879d7aadbf9SJohn Baldwin 		return (ECHILD);
880d7aadbf9SJohn Baldwin 	}
881b7e23e82SJohn Baldwin 	if (options & WNOHANG) {
882d7aadbf9SJohn Baldwin 		sx_xunlock(&proctree_lock);
883d7aadbf9SJohn Baldwin 		td->td_retval[0] = 0;
884d7aadbf9SJohn Baldwin 		return (0);
885d7aadbf9SJohn Baldwin 	}
886d7aadbf9SJohn Baldwin 	PROC_LOCK(q);
887d7aadbf9SJohn Baldwin 	sx_xunlock(&proctree_lock);
88895992d56SDavid Xu 	if (q->p_flag & P_STATCHILD) {
88995992d56SDavid Xu 		q->p_flag &= ~P_STATCHILD;
89095992d56SDavid Xu 		error = 0;
89195992d56SDavid Xu 	} else
8927f05b035SAlfred Perlstein 		error = msleep(q, &q->p_mtx, PWAIT | PCATCH, "wait", 0);
893d7aadbf9SJohn Baldwin 	PROC_UNLOCK(q);
8944ae89b95SJohn Baldwin 	if (error)
895d7aadbf9SJohn Baldwin 		return (error);
896d7aadbf9SJohn Baldwin 	goto loop;
897d7aadbf9SJohn Baldwin }
898df8bae1dSRodney W. Grimes 
899df8bae1dSRodney W. Grimes /*
90098f03f90SJake Burkholder  * Make process 'parent' the new parent of process 'child'.
90198f03f90SJake Burkholder  * Must be called with an exclusive hold of proctree lock.
902df8bae1dSRodney W. Grimes  */
903df8bae1dSRodney W. Grimes void
904830c3153SDag-Erling Smørgrav proc_reparent(struct proc *child, struct proc *parent)
905df8bae1dSRodney W. Grimes {
906df8bae1dSRodney W. Grimes 
9074e5e677bSJohn Baldwin 	sx_assert(&proctree_lock, SX_XLOCKED);
908c65437a3SJohn Baldwin 	PROC_LOCK_ASSERT(child, MA_OWNED);
909df8bae1dSRodney W. Grimes 	if (child->p_pptr == parent)
910df8bae1dSRodney W. Grimes 		return;
911df8bae1dSRodney W. Grimes 
912*58c77a9dSEdward Tomasz Napierala 	PROC_LOCK(parent);
913*58c77a9dSEdward Tomasz Napierala 	racct_add_force(parent, RACCT_NPROC, 1);
914*58c77a9dSEdward Tomasz Napierala 	PROC_UNLOCK(parent);
915ff766807SDavid Xu 	PROC_LOCK(child->p_pptr);
916*58c77a9dSEdward Tomasz Napierala 	racct_sub(child->p_pptr, RACCT_NPROC, 1);
917ff766807SDavid Xu 	sigqueue_take(child->p_ksi);
918ff766807SDavid Xu 	PROC_UNLOCK(child->p_pptr);
919b75356e1SJeffrey Hsu 	LIST_REMOVE(child, p_sibling);
920b75356e1SJeffrey Hsu 	LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
921df8bae1dSRodney W. Grimes 	child->p_pptr = parent;
922df8bae1dSRodney W. Grimes }
923