xref: /freebsd/sys/kern/kern_exit.c (revision 7ca6b7823d714b6f1ef9473d4f2296387d77c0eb)
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"
41db6a20e2SGarrett Wollman #include "opt_ktrace.h"
422555374cSRobert Watson #include "opt_mac.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>
54df8bae1dSRodney W. Grimes #include <sys/tty.h>
55df8bae1dSRodney W. Grimes #include <sys/wait.h>
56eb30c1c0SPeter Wemm #include <sys/vmmeter.h>
577a6b989bSJohn Baldwin #include <sys/vnode.h>
58df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
5925f6e35aSPoul-Henning Kamp #include <sys/sbuf.h>
60797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
61b43179fbSJeff Roberson #include <sys/sched.h>
621005a129SJohn Baldwin #include <sys/sx.h>
63c8837938SJohn Baldwin #include <sys/syscallsubr.h>
6425f6e35aSPoul-Henning Kamp #include <sys/syslog.h>
65df8bae1dSRodney W. Grimes #include <sys/ptrace.h>
667c409b8aSJeffrey Hsu #include <sys/acct.h>		/* for acct_process() function prototype */
67797f2d22SPoul-Henning Kamp #include <sys/filedesc.h>
682555374cSRobert Watson #include <sys/mac.h>
69780dc5a8SPeter Wemm #include <sys/shm.h>
70780dc5a8SPeter Wemm #include <sys/sem.h>
716c84de02SJohn Baldwin #ifdef KTRACE
726c84de02SJohn Baldwin #include <sys/ktrace.h>
736c84de02SJohn Baldwin #endif
74780dc5a8SPeter Wemm 
75fcf7f27aSRobert Watson #include <security/audit/audit.h>
76fcf7f27aSRobert Watson 
77df8bae1dSRodney W. Grimes #include <vm/vm.h>
78eb30c1c0SPeter Wemm #include <vm/vm_extern.h>
797a6b989bSJohn Baldwin #include <vm/vm_param.h>
80efeaf95aSDavid Greenman #include <vm/pmap.h>
81efeaf95aSDavid Greenman #include <vm/vm_map.h>
822d21129dSAlan Cox #include <vm/vm_page.h>
83c897b813SJeff Roberson #include <vm/uma.h>
84df8bae1dSRodney W. Grimes 
85ba198b1cSMark Newton /* Required to be non-static for SysVR4 emulator */
8669a6f20bSMark Newton MALLOC_DEFINE(M_ZOMBIE, "zombie", "zombie proc status");
8755166637SPoul-Henning Kamp 
88c0bc2867SGleb Smirnoff /* Hook for NFS teardown procedure. */
89c0bc2867SGleb Smirnoff void (*nlminfo_release_p)(struct proc *p);
90c0bc2867SGleb Smirnoff 
91df8bae1dSRodney W. Grimes /*
92df8bae1dSRodney W. Grimes  * exit --
93df8bae1dSRodney W. Grimes  *	Death of process.
94234216efSMatthew Dillon  *
95234216efSMatthew Dillon  * MPSAFE
96df8bae1dSRodney W. Grimes  */
97fc0b1dbfSBruce Evans void
98830c3153SDag-Erling Smørgrav sys_exit(struct thread *td, struct sys_exit_args *uap)
99df8bae1dSRodney W. Grimes {
100b40ce416SJulian Elischer 
101b40ce416SJulian Elischer 	exit1(td, W_EXITCODE(uap->rval, 0));
102df8bae1dSRodney W. Grimes 	/* NOTREACHED */
103df8bae1dSRodney W. Grimes }
104df8bae1dSRodney W. Grimes 
105df8bae1dSRodney W. Grimes /*
106df8bae1dSRodney W. Grimes  * Exit: deallocate address space and other resources, change proc state
107df8bae1dSRodney W. Grimes  * to zombie, and unlink proc from allproc and parent's lists.  Save exit
108df8bae1dSRodney W. Grimes  * status and rusage for wait().  Check for child processes and orphan them.
109df8bae1dSRodney W. Grimes  */
110fc0b1dbfSBruce Evans void
111830c3153SDag-Erling Smørgrav exit1(struct thread *td, int rv)
112df8bae1dSRodney W. Grimes {
113276c5169SJohn Baldwin 	struct proc *p, *nq, *q;
114276c5169SJohn Baldwin 	struct tty *tp;
115276c5169SJohn Baldwin 	struct vnode *ttyvp;
11679deba82SMatthew Dillon 	struct vnode *vtmp;
117d7aadbf9SJohn Baldwin #ifdef KTRACE
118d7aadbf9SJohn Baldwin 	struct vnode *tracevp;
119a5881ea5SJohn Baldwin 	struct ucred *tracecred;
120d7aadbf9SJohn Baldwin #endif
12191d5354aSJohn Baldwin 	struct plimit *plim;
12257051fdcSTor Egge 	int locked;
123df8bae1dSRodney W. Grimes 
1244ae89b95SJohn Baldwin 	/*
1254ae89b95SJohn Baldwin 	 * Drop Giant if caller has it.  Eventually we should warn about
1264ae89b95SJohn Baldwin 	 * being called with Giant held.
1274ae89b95SJohn Baldwin 	 */
1284ae89b95SJohn Baldwin 	while (mtx_owned(&Giant))
1294ae89b95SJohn Baldwin 		mtx_unlock(&Giant);
1300cddd8f0SMatthew Dillon 
131276c5169SJohn Baldwin 	p = td->td_proc;
132e746d950SJohn Baldwin 	if (p == initproc) {
1335f7bd355SPoul-Henning Kamp 		printf("init died (signal %d, exit %d)\n",
134df8bae1dSRodney W. Grimes 		    WTERMSIG(rv), WEXITSTATUS(rv));
1355f7bd355SPoul-Henning Kamp 		panic("Going nowhere without my init!");
1365f7bd355SPoul-Henning Kamp 	}
1372c1011f7SJohn Dyson 
1387a6b989bSJohn Baldwin 	/*
1392c10d16aSJeff Roberson 	 * MUST abort all other threads before proceeding past here.
1407a6b989bSJohn Baldwin 	 */
141e602ba25SJulian Elischer 	PROC_LOCK(p);
142ed062c8dSJulian Elischer 	if (p->p_flag & P_HADTHREADS) {
1430aabef65SDavid Xu retry:
144e602ba25SJulian Elischer 		/*
145e602ba25SJulian Elischer 		 * First check if some other thread got here before us..
146e602ba25SJulian Elischer 		 * if so, act apropriatly, (exit or suspend);
147e602ba25SJulian Elischer 		 */
148e602ba25SJulian Elischer 		thread_suspend_check(0);
149e602ba25SJulian Elischer 
150e602ba25SJulian Elischer 		/*
151e602ba25SJulian Elischer 		 * Kill off the other threads. This requires
1526111dcd2SJohn Baldwin 		 * some co-operation from other parts of the kernel
1536111dcd2SJohn Baldwin 		 * so it may not be instantaneous.  With this state set
1546111dcd2SJohn Baldwin 		 * any thread entering the kernel from userspace will
155e602ba25SJulian Elischer 		 * thread_exit() in trap().  Any thread attempting to
1566111dcd2SJohn Baldwin 		 * sleep will return immediately with EINTR or EWOULDBLOCK
1576111dcd2SJohn Baldwin 		 * which will hopefully force them to back out to userland
1586111dcd2SJohn Baldwin 		 * freeing resources as they go.  Any thread attempting
159b3248998SJulian Elischer 		 * to return to userland will thread_exit() from userret().
1606111dcd2SJohn Baldwin 		 * thread_exit() will unsuspend us when the last of the
1616111dcd2SJohn Baldwin 		 * other threads exits.
162b370279eSDavid Xu 		 * If there is already a thread singler after resumption,
1636111dcd2SJohn Baldwin 		 * calling thread_single will fail; in that case, we just
164b370279eSDavid Xu 		 * re-check all suspension request, the thread should
165b370279eSDavid Xu 		 * either be suspended there or exit.
166e602ba25SJulian Elischer 		 */
16737814395SPeter Wemm 		if (thread_single(SINGLE_EXIT))
1680aabef65SDavid Xu 			goto retry;
1696111dcd2SJohn Baldwin 
170e602ba25SJulian Elischer 		/*
171e602ba25SJulian Elischer 		 * All other activity in this process is now stopped.
172ed062c8dSJulian Elischer 		 * Threading support has been turned off.
173e602ba25SJulian Elischer 		 */
174e602ba25SJulian Elischer 	}
175e602ba25SJulian Elischer 
17606ad42b2SJohn Baldwin 	/*
17706ad42b2SJohn Baldwin 	 * Wakeup anyone in procfs' PIOCWAIT.  They should have a hold
17806ad42b2SJohn Baldwin 	 * on our vmspace, so we should block below until they have
17906ad42b2SJohn Baldwin 	 * released their reference to us.  Note that if they have
18006ad42b2SJohn Baldwin 	 * requested S_EXIT stops we will block here until they ack
18106ad42b2SJohn Baldwin 	 * via PIOCCONT.
18206ad42b2SJohn Baldwin 	 */
18306ad42b2SJohn Baldwin 	_STOPEVENT(p, S_EXIT, rv);
18406ad42b2SJohn Baldwin 
18506ad42b2SJohn Baldwin 	/*
18606ad42b2SJohn Baldwin 	 * Note that we are exiting and do another wakeup of anyone in
18706ad42b2SJohn Baldwin 	 * PIOCWAIT in case they aren't listening for S_EXIT stops or
18806ad42b2SJohn Baldwin 	 * decided to wait again after we told them we are exiting.
18906ad42b2SJohn Baldwin 	 */
190e602ba25SJulian Elischer 	p->p_flag |= P_WEXIT;
19106ad42b2SJohn Baldwin 	wakeup(&p->p_stype);
19206ad42b2SJohn Baldwin 
19306ad42b2SJohn Baldwin 	/*
19406ad42b2SJohn Baldwin 	 * Wait for any processes that have a hold on our vmspace to
19506ad42b2SJohn Baldwin 	 * release their reference.
19606ad42b2SJohn Baldwin 	 */
19706ad42b2SJohn Baldwin 	while (p->p_lock > 0)
19806ad42b2SJohn Baldwin 		msleep(&p->p_lock, &p->p_mtx, PWAIT, "exithold", 0);
199ebceaf6dSDavid Xu 
200ebceaf6dSDavid Xu 	PROC_LOCK(p->p_pptr);
201ebceaf6dSDavid Xu 	sigqueue_take(p->p_ksi);
202ebceaf6dSDavid Xu 	PROC_UNLOCK(p->p_pptr);
203ebceaf6dSDavid Xu 
204e602ba25SJulian Elischer 	PROC_UNLOCK(p);
205b40ce416SJulian Elischer 
20600c28d96SRobert Watson #ifdef AUDIT
20700c28d96SRobert Watson 	/*
20800c28d96SRobert Watson 	 * The Sun BSM exit token contains two components: an exit status as
20900c28d96SRobert Watson 	 * passed to exit(), and a return value to indicate what sort of exit
21000c28d96SRobert Watson 	 * it was.  The exit status is WEXITSTATUS(rv), but it's not clear
21100c28d96SRobert Watson 	 * what the return value is.
21200c28d96SRobert Watson 	 */
21300c28d96SRobert Watson 	AUDIT_ARG(exit, WEXITSTATUS(rv), 0);
21400c28d96SRobert Watson 	AUDIT_SYSCALL_EXIT(0, td);
21500c28d96SRobert Watson #endif
21600c28d96SRobert Watson 
2177a6b989bSJohn Baldwin 	/* Are we a task leader? */
2182c1011f7SJohn Dyson 	if (p == p->p_leader) {
219c6544064SJohn Baldwin 		mtx_lock(&ppeers_lock);
2202c1011f7SJohn Dyson 		q = p->p_peers;
221776e0b36SJohn Baldwin 		while (q != NULL) {
222776e0b36SJohn Baldwin 			PROC_LOCK(q);
223776e0b36SJohn Baldwin 			psignal(q, SIGKILL);
224776e0b36SJohn Baldwin 			PROC_UNLOCK(q);
2252c1011f7SJohn Dyson 			q = q->p_peers;
2262c1011f7SJohn Dyson 		}
227c6544064SJohn Baldwin 		while (p->p_peers != NULL)
228c6544064SJohn Baldwin 			msleep(p, &ppeers_lock, PWAIT, "exit1", 0);
229c6544064SJohn Baldwin 		mtx_unlock(&ppeers_lock);
2302c1011f7SJohn Dyson 	}
2312c1011f7SJohn Dyson 
232fed06968SJulian Elischer 	/*
233e9189611SPeter Wemm 	 * Check if any loadable modules need anything done at process exit.
2346111dcd2SJohn Baldwin 	 * E.g. SYSV IPC stuff
235fed06968SJulian Elischer 	 * XXX what if one of these generates an error?
236fed06968SJulian Elischer 	 */
23775b8b3b2SJohn Baldwin 	EVENTHANDLER_INVOKE(process_exit, p);
238a914fb6bSJohn Baldwin 
239df8bae1dSRodney W. Grimes 	MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage),
240a163d034SWarner Losh 		M_ZOMBIE, M_WAITOK);
241df8bae1dSRodney W. Grimes 	/*
242df8bae1dSRodney W. Grimes 	 * If parent is waiting for us to exit or exec,
243df8bae1dSRodney W. Grimes 	 * P_PPWAIT is set; we will wakeup the parent below.
244df8bae1dSRodney W. Grimes 	 */
245a914fb6bSJohn Baldwin 	PROC_LOCK(p);
246a282253aSJulian Elischer 	stopprofclock(p);
247df8bae1dSRodney W. Grimes 	p->p_flag &= ~(P_TRACED | P_PPWAIT);
2485499ea01SJohn Baldwin 
2495499ea01SJohn Baldwin 	/*
2505499ea01SJohn Baldwin 	 * Stop the real interval timer.  If the handler is currently
2515499ea01SJohn Baldwin 	 * executing, prevent it from rearming itself and let it finish.
2525499ea01SJohn Baldwin 	 */
2535499ea01SJohn Baldwin 	if (timevalisset(&p->p_realtimer.it_value) &&
2545499ea01SJohn Baldwin 	    callout_stop(&p->p_itcallout) == 0) {
2555499ea01SJohn Baldwin 		timevalclear(&p->p_realtimer.it_interval);
2565499ea01SJohn Baldwin 		msleep(&p->p_itcallout, &p->p_mtx, PWAIT, "ritwait", 0);
2575499ea01SJohn Baldwin 		KASSERT(!timevalisset(&p->p_realtimer.it_value),
2585499ea01SJohn Baldwin 		    ("realtime timer is still armed"));
2595499ea01SJohn Baldwin 	}
26086857b36SDavid Xu 	sigqueue_flush(&p->p_sigqueue);
26186857b36SDavid Xu 	sigqueue_flush(&td->td_sigqueue);
26296d7f8efSTim J. Robbins 	PROC_UNLOCK(p);
263df8bae1dSRodney W. Grimes 
264df8bae1dSRodney W. Grimes 	/*
265831d27a9SDon Lewis 	 * Reset any sigio structures pointing to us as a result of
266831d27a9SDon Lewis 	 * F_SETOWN with our pid.
267831d27a9SDon Lewis 	 */
268831d27a9SDon Lewis 	funsetownlst(&p->p_sigiolst);
269831d27a9SDon Lewis 
270831d27a9SDon Lewis 	/*
271c0bc2867SGleb Smirnoff 	 * If this process has an nlminfo data area (for lockd), release it
272c0bc2867SGleb Smirnoff 	 */
273c0bc2867SGleb Smirnoff 	if (nlminfo_release_p != NULL && p->p_nlminfo != NULL)
274c0bc2867SGleb Smirnoff 		(*nlminfo_release_p)(p);
275c0bc2867SGleb Smirnoff 
276c0bc2867SGleb Smirnoff 	/*
277df8bae1dSRodney W. Grimes 	 * Close open files and release open-file table.
278df8bae1dSRodney W. Grimes 	 * This may block!
279df8bae1dSRodney W. Grimes 	 */
280edf6699aSAlfred Perlstein 	fdfree(td);
281df8bae1dSRodney W. Grimes 
282a914fb6bSJohn Baldwin 	/*
283a2587073SPoul-Henning Kamp 	 * If this thread tickled GEOM, we need to wait for the giggling to
284a2587073SPoul-Henning Kamp 	 * stop before we return to userland
285a2587073SPoul-Henning Kamp 	 */
286a2587073SPoul-Henning Kamp 	if (td->td_pflags & TDP_GEOM)
287a2587073SPoul-Henning Kamp 		g_waitidle();
288a2587073SPoul-Henning Kamp 
289a2587073SPoul-Henning Kamp 	/*
290a914fb6bSJohn Baldwin 	 * Remove ourself from our leader's peer list and wake our leader.
291a914fb6bSJohn Baldwin 	 */
292c6544064SJohn Baldwin 	mtx_lock(&ppeers_lock);
29379fc0bf4SMike Smith 	if (p->p_leader->p_peers) {
29479fc0bf4SMike Smith 		q = p->p_leader;
29579fc0bf4SMike Smith 		while (q->p_peers != p)
29679fc0bf4SMike Smith 			q = q->p_peers;
29779fc0bf4SMike Smith 		q->p_peers = p->p_peers;
2987f05b035SAlfred Perlstein 		wakeup(p->p_leader);
29979fc0bf4SMike Smith 	}
300c6544064SJohn Baldwin 	mtx_unlock(&ppeers_lock);
30179fc0bf4SMike Smith 
30257051fdcSTor Egge 	vmspace_exit(td);
303df8bae1dSRodney W. Grimes 
3047ca6b782SChristian S.J. Peron 	mtx_lock(&Giant);	/* XXX TTY */
305ea97757aSJohn Baldwin 	sx_xlock(&proctree_lock);
306df8bae1dSRodney W. Grimes 	if (SESS_LEADER(p)) {
307830c3153SDag-Erling Smørgrav 		struct session *sp;
308df8bae1dSRodney W. Grimes 
309f591779bSSeigo Tanimura 		sp = p->p_session;
310df8bae1dSRodney W. Grimes 		if (sp->s_ttyvp) {
311df8bae1dSRodney W. Grimes 			/*
312df8bae1dSRodney W. Grimes 			 * Controlling process.
313df8bae1dSRodney W. Grimes 			 * Signal foreground pgrp,
314df8bae1dSRodney W. Grimes 			 * drain controlling terminal
315df8bae1dSRodney W. Grimes 			 * and revoke access to controlling terminal.
316df8bae1dSRodney W. Grimes 			 */
317dd45d8adSJulian Elischer 			if (sp->s_ttyp && (sp->s_ttyp->t_session == sp)) {
318f591779bSSeigo Tanimura 				tp = sp->s_ttyp;
319f591779bSSeigo Tanimura 				if (sp->s_ttyp->t_pgrp) {
320f591779bSSeigo Tanimura 					PGRP_LOCK(sp->s_ttyp->t_pgrp);
321df8bae1dSRodney W. Grimes 					pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
322f591779bSSeigo Tanimura 					PGRP_UNLOCK(sp->s_ttyp->t_pgrp);
323f591779bSSeigo Tanimura 				}
324f591779bSSeigo Tanimura 				/* XXX tp should be locked. */
325ea97757aSJohn Baldwin 				sx_xunlock(&proctree_lock);
326f591779bSSeigo Tanimura 				(void) ttywait(tp);
327ea97757aSJohn Baldwin 				sx_xlock(&proctree_lock);
328df8bae1dSRodney W. Grimes 				/*
329df8bae1dSRodney W. Grimes 				 * The tty could have been revoked
330df8bae1dSRodney W. Grimes 				 * if we blocked.
331df8bae1dSRodney W. Grimes 				 */
332f591779bSSeigo Tanimura 				if (sp->s_ttyvp) {
333f591779bSSeigo Tanimura 					ttyvp = sp->s_ttyvp;
334f591779bSSeigo Tanimura 					SESS_LOCK(p->p_session);
335df8bae1dSRodney W. Grimes 					sp->s_ttyvp = NULL;
336f591779bSSeigo Tanimura 					SESS_UNLOCK(p->p_session);
337ea97757aSJohn Baldwin 					sx_xunlock(&proctree_lock);
3382b3183a8SJeff Roberson 					VOP_LOCK(ttyvp, LK_EXCLUSIVE, td);
339f591779bSSeigo Tanimura 					VOP_REVOKE(ttyvp, REVOKEALL);
3402b3183a8SJeff Roberson 					vput(ttyvp);
341ea97757aSJohn Baldwin 					sx_xlock(&proctree_lock);
342f591779bSSeigo Tanimura 				}
343f591779bSSeigo Tanimura 			}
344f591779bSSeigo Tanimura 			if (sp->s_ttyvp) {
345f591779bSSeigo Tanimura 				ttyvp = sp->s_ttyvp;
346f591779bSSeigo Tanimura 				SESS_LOCK(p->p_session);
347f591779bSSeigo Tanimura 				sp->s_ttyvp = NULL;
348f591779bSSeigo Tanimura 				SESS_UNLOCK(p->p_session);
349f591779bSSeigo Tanimura 				vrele(ttyvp);
350f591779bSSeigo Tanimura 			}
351df8bae1dSRodney W. Grimes 			/*
352df8bae1dSRodney W. Grimes 			 * s_ttyp is not zero'd; we use this to indicate
353df8bae1dSRodney W. Grimes 			 * that the session once had a controlling terminal.
354df8bae1dSRodney W. Grimes 			 * (for logging and informational purposes)
355df8bae1dSRodney W. Grimes 			 */
356df8bae1dSRodney W. Grimes 		}
357f591779bSSeigo Tanimura 		SESS_LOCK(p->p_session);
358df8bae1dSRodney W. Grimes 		sp->s_leader = NULL;
359f591779bSSeigo Tanimura 		SESS_UNLOCK(p->p_session);
360f591779bSSeigo Tanimura 	}
361df8bae1dSRodney W. Grimes 	fixjobc(p, p->p_pgrp, 0);
362ea97757aSJohn Baldwin 	sx_xunlock(&proctree_lock);
363b40ce416SJulian Elischer 	(void)acct_process(td);
3647ca6b782SChristian S.J. Peron 	mtx_unlock(&Giant);
365df8bae1dSRodney W. Grimes #ifdef KTRACE
366df8bae1dSRodney W. Grimes 	/*
3672c255e9dSRobert Watson 	 * Drain any pending records on the thread and release the trace
3682c255e9dSRobert Watson 	 * file.  It might be better if drain-and-clear were atomic.
369df8bae1dSRodney W. Grimes 	 */
3702c255e9dSRobert Watson 	ktrprocexit(td);
371d7aadbf9SJohn Baldwin 	PROC_LOCK(p);
3726c84de02SJohn Baldwin 	mtx_lock(&ktrace_mtx);
373df8bae1dSRodney W. Grimes 	p->p_traceflag = 0;	/* don't trace the vrele() */
374a5881ea5SJohn Baldwin 	tracevp = p->p_tracevp;
375a5881ea5SJohn Baldwin 	p->p_tracevp = NULL;
376a5881ea5SJohn Baldwin 	tracecred = p->p_tracecred;
377a5881ea5SJohn Baldwin 	p->p_tracecred = NULL;
3786c84de02SJohn Baldwin 	mtx_unlock(&ktrace_mtx);
379d7aadbf9SJohn Baldwin 	PROC_UNLOCK(p);
38015088317SBrian Feldman 	if (tracevp != NULL) {
38157606880SChristian S.J. Peron 		locked = VFS_LOCK_GIANT(tracevp->v_mount);
382d7aadbf9SJohn Baldwin 		vrele(tracevp);
38357606880SChristian S.J. Peron 		VFS_UNLOCK_GIANT(locked);
38415088317SBrian Feldman 	}
385a5881ea5SJohn Baldwin 	if (tracecred != NULL)
386a5881ea5SJohn Baldwin 		crfree(tracecred);
387df8bae1dSRodney W. Grimes #endif
388df8bae1dSRodney W. Grimes 	/*
389ee42d0a9SDavid Malone 	 * Release reference to text vnode
390ee42d0a9SDavid Malone 	 */
391ee42d0a9SDavid Malone 	if ((vtmp = p->p_textvp) != NULL) {
392ee42d0a9SDavid Malone 		p->p_textvp = NULL;
39357606880SChristian S.J. Peron 		locked = VFS_LOCK_GIANT(vtmp->v_mount);
394ee42d0a9SDavid Malone 		vrele(vtmp);
39557606880SChristian S.J. Peron 		VFS_UNLOCK_GIANT(locked);
396ee42d0a9SDavid Malone 	}
397ee42d0a9SDavid Malone 
398ee42d0a9SDavid Malone 	/*
399d7aadbf9SJohn Baldwin 	 * Release our limits structure.
400d7aadbf9SJohn Baldwin 	 */
40191d5354aSJohn Baldwin 	PROC_LOCK(p);
40291d5354aSJohn Baldwin 	plim = p->p_limit;
403d7aadbf9SJohn Baldwin 	p->p_limit = NULL;
40491d5354aSJohn Baldwin 	PROC_UNLOCK(p);
40591d5354aSJohn Baldwin 	lim_free(plim);
406d7aadbf9SJohn Baldwin 
407d7aadbf9SJohn Baldwin 	/*
408df8bae1dSRodney W. Grimes 	 * Remove proc from allproc queue and pidhash chain.
409df8bae1dSRodney W. Grimes 	 * Place onto zombproc.  Unlink from parent's child list.
410df8bae1dSRodney W. Grimes 	 */
4111005a129SJohn Baldwin 	sx_xlock(&allproc_lock);
412b75356e1SJeffrey Hsu 	LIST_REMOVE(p, p_list);
413b75356e1SJeffrey Hsu 	LIST_INSERT_HEAD(&zombproc, p, p_list);
414b75356e1SJeffrey Hsu 	LIST_REMOVE(p, p_hash);
4151005a129SJohn Baldwin 	sx_xunlock(&allproc_lock);
416df8bae1dSRodney W. Grimes 
41737f84a60SJohn Baldwin 	/*
41837f84a60SJohn Baldwin 	 * Reparent all of our children to init.
41937f84a60SJohn Baldwin 	 */
4201005a129SJohn Baldwin 	sx_xlock(&proctree_lock);
4212e3c8fcbSPoul-Henning Kamp 	q = LIST_FIRST(&p->p_children);
422a914fb6bSJohn Baldwin 	if (q != NULL)		/* only need this if any child is S_ZOMB */
4237f05b035SAlfred Perlstein 		wakeup(initproc);
424a914fb6bSJohn Baldwin 	for (; q != NULL; q = nq) {
4252e3c8fcbSPoul-Henning Kamp 		nq = LIST_NEXT(q, p_sibling);
426a914fb6bSJohn Baldwin 		PROC_LOCK(q);
427c65437a3SJohn Baldwin 		proc_reparent(q, initproc);
4284ac9ae70SJulian Elischer 		q->p_sigparent = SIGCHLD;
429df8bae1dSRodney W. Grimes 		/*
430df8bae1dSRodney W. Grimes 		 * Traced processes are killed
431df8bae1dSRodney W. Grimes 		 * since their existence means someone is screwing up.
432df8bae1dSRodney W. Grimes 		 */
433df8bae1dSRodney W. Grimes 		if (q->p_flag & P_TRACED) {
434c2836532SDavid Xu 			q->p_flag &= ~(P_TRACED | P_STOPPED_TRACE);
435df8bae1dSRodney W. Grimes 			psignal(q, SIGKILL);
436c65437a3SJohn Baldwin 		}
437a914fb6bSJohn Baldwin 		PROC_UNLOCK(q);
438df8bae1dSRodney W. Grimes 	}
439df8bae1dSRodney W. Grimes 
440df8bae1dSRodney W. Grimes 	/*
44178c85e8dSJohn Baldwin 	 * Save exit status and finalize rusage info except for times,
44203001f59SStephan Uphoff 	 * adding in child rusage info later when our time is locked.
443df8bae1dSRodney W. Grimes 	 */
444d7aadbf9SJohn Baldwin 	PROC_LOCK(p);
445df8bae1dSRodney W. Grimes 	p->p_xstat = rv;
446cbf4e354SDavid Xu 	p->p_xthread = td;
44778c85e8dSJohn Baldwin 	p->p_stats->p_ru.ru_nvcsw++;
448df8bae1dSRodney W. Grimes 	*p->p_ru = p->p_stats->p_ru;
449df8bae1dSRodney W. Grimes 
450df8bae1dSRodney W. Grimes 	/*
4517a6b989bSJohn Baldwin 	 * Notify interested parties of our demise.
452cb679c38SJonathan Lemon 	 */
453ad3b9257SJohn-Mark Gurney 	KNOTE_LOCKED(&p->p_klist, NOTE_EXIT);
4547eaec467SJohn Baldwin 
4551a29c806SOlivier Houchard 	/*
4561a29c806SOlivier Houchard 	 * Just delete all entries in the p_klist. At this point we won't
4571a29c806SOlivier Houchard 	 * report any more events, and there are nasty race conditions that
4581a29c806SOlivier Houchard 	 * can beat us if we don't.
4591a29c806SOlivier Houchard 	 */
460ad3b9257SJohn-Mark Gurney 	knlist_clear(&p->p_klist, 1);
461cb679c38SJonathan Lemon 
462cb679c38SJonathan Lemon 	/*
463645682fdSLuoqi Chen 	 * Notify parent that we're gone.  If parent has the PS_NOCLDWAIT
464ba1551caSIan Dowse 	 * flag set, or if the handler is set to SIG_IGN, notify process
465ba1551caSIan Dowse 	 * 1 instead (and hope it will handle this situation).
466df8bae1dSRodney W. Grimes 	 */
467d7aadbf9SJohn Baldwin 	PROC_LOCK(p->p_pptr);
46890af4afaSJohn Baldwin 	mtx_lock(&p->p_pptr->p_sigacts->ps_mtx);
46990af4afaSJohn Baldwin 	if (p->p_pptr->p_sigacts->ps_flag & (PS_NOCLDWAIT | PS_CLDSIGIGN)) {
470276c5169SJohn Baldwin 		struct proc *pp;
471276c5169SJohn Baldwin 
47290af4afaSJohn Baldwin 		mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx);
473276c5169SJohn Baldwin 		pp = p->p_pptr;
474f591779bSSeigo Tanimura 		PROC_UNLOCK(pp);
475245f17d4SJoerg Wunsch 		proc_reparent(p, initproc);
47655b5f2a2SDon Lewis 		p->p_sigparent = SIGCHLD;
477f591779bSSeigo Tanimura 		PROC_LOCK(p->p_pptr);
478245f17d4SJoerg Wunsch 		/*
479245f17d4SJoerg Wunsch 		 * If this was the last child of our parent, notify
480245f17d4SJoerg Wunsch 		 * parent, so in case he was wait(2)ing, he will
481245f17d4SJoerg Wunsch 		 * continue.
482245f17d4SJoerg Wunsch 		 */
483245f17d4SJoerg Wunsch 		if (LIST_EMPTY(&pp->p_children))
4847f05b035SAlfred Perlstein 			wakeup(pp);
48590af4afaSJohn Baldwin 	} else
48690af4afaSJohn Baldwin 		mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx);
487245f17d4SJoerg Wunsch 
4886567eef7SDon Lewis 	if (p->p_pptr == initproc)
4896626c604SJulian Elischer 		psignal(p->p_pptr, SIGCHLD);
490ebceaf6dSDavid Xu 	else if (p->p_sigparent != 0) {
491ebceaf6dSDavid Xu 		if (p->p_sigparent == SIGCHLD)
492ebceaf6dSDavid Xu 			childproc_exited(p);
493ebceaf6dSDavid Xu 		else	/* LINUX thread */
4946567eef7SDon Lewis 			psignal(p->p_pptr, p->p_sigparent);
495ebceaf6dSDavid Xu 	}
496c65437a3SJohn Baldwin 	PROC_UNLOCK(p->p_pptr);
497c65437a3SJohn Baldwin 	PROC_UNLOCK(p);
4988e2e767bSJohn Baldwin 
4998e2e767bSJohn Baldwin 	/*
500df8bae1dSRodney W. Grimes 	 * Finally, call machine-dependent code to release the remaining
501696058c3SJulian Elischer 	 * resources including address space.
502582ec34cSAlfred Perlstein 	 * The address space is released by "vmspace_exitfree(p)" in
503582ec34cSAlfred Perlstein 	 * vm_waitproc().
504df8bae1dSRodney W. Grimes 	 */
505b40ce416SJulian Elischer 	cpu_exit(td);
506eb30c1c0SPeter Wemm 
50783de502dSJohn Baldwin 	WITNESS_WARN(WARN_PANIC, &proctree_lock.sx_object,
50883de502dSJohn Baldwin 	    "process (pid %d) exiting", p->p_pid);
50983de502dSJohn Baldwin 
510eb30c1c0SPeter Wemm 	PROC_LOCK(p);
511d7aadbf9SJohn Baldwin 	PROC_LOCK(p->p_pptr);
512d7aadbf9SJohn Baldwin 	sx_xunlock(&proctree_lock);
513696058c3SJulian Elischer 
514eb30c1c0SPeter Wemm 	/*
515462f31bfSJohn Baldwin 	 * We have to wait until after acquiring all locks before
516a9a64385SJohn Baldwin 	 * changing p_state.  We need to avoid all possible context
517a9a64385SJohn Baldwin 	 * switches (including ones from blocking on a mutex) while
518ddf9c4f7SJohn Baldwin 	 * marked as a zombie.  We also have to set the zombie state
519ddf9c4f7SJohn Baldwin 	 * before we release the parent process' proc lock to avoid
520ddf9c4f7SJohn Baldwin 	 * a lost wakeup.  So, we first call wakeup, then we grab the
521ddf9c4f7SJohn Baldwin 	 * sched lock, update the state, and release the parent process'
522ddf9c4f7SJohn Baldwin 	 * proc lock.
523eb30c1c0SPeter Wemm 	 */
524ddf9c4f7SJohn Baldwin 	wakeup(p->p_pptr);
525e5bb601dSJohn Baldwin 	mtx_lock_spin(&sched_lock);
526e602ba25SJulian Elischer 	p->p_state = PRS_ZOMBIE;
527d7aadbf9SJohn Baldwin 	PROC_UNLOCK(p->p_pptr);
528871684b8SBruce Evans 
52955d44f79SJulian Elischer 	sched_exit(p->p_pptr, td);
530f6f230feSJeff Roberson 
531f6f230feSJeff Roberson 	/*
5327eaec467SJohn Baldwin 	 * Hopefully no one will try to deliver a signal to the process this
533ad3b9257SJohn-Mark Gurney 	 * late in the game.
534ad3b9257SJohn-Mark Gurney 	 */
535ad3b9257SJohn-Mark Gurney 	knlist_destroy(&p->p_klist);
536ad3b9257SJohn-Mark Gurney 
537ad3b9257SJohn-Mark Gurney 	/*
538696058c3SJulian Elischer 	 * Make sure the scheduler takes this thread out of its tables etc.
539e602ba25SJulian Elischer 	 * This will also release this thread's reference to the ucred.
540696058c3SJulian Elischer 	 * Other thread parts to release include pcb bits and such.
541e602ba25SJulian Elischer 	 */
542e602ba25SJulian Elischer 	thread_exit();
543df8bae1dSRodney W. Grimes }
544df8bae1dSRodney W. Grimes 
54525f6e35aSPoul-Henning Kamp 
54625f6e35aSPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_
54725f6e35aSPoul-Henning Kamp struct abort2_args {
54825f6e35aSPoul-Henning Kamp 	char *why;
54925f6e35aSPoul-Henning Kamp 	int nargs;
55025f6e35aSPoul-Henning Kamp 	void **args;
55125f6e35aSPoul-Henning Kamp };
55225f6e35aSPoul-Henning Kamp #endif
55325f6e35aSPoul-Henning Kamp 
55425f6e35aSPoul-Henning Kamp /*
55525f6e35aSPoul-Henning Kamp  * MPSAFE.
55625f6e35aSPoul-Henning Kamp  */
55725f6e35aSPoul-Henning Kamp int
55825f6e35aSPoul-Henning Kamp abort2(struct thread *td, struct abort2_args *uap)
55925f6e35aSPoul-Henning Kamp {
56025f6e35aSPoul-Henning Kamp 	struct proc *p = td->td_proc;
56125f6e35aSPoul-Henning Kamp 	struct sbuf *sb;
56225f6e35aSPoul-Henning Kamp 	void *uargs[16];
56325f6e35aSPoul-Henning Kamp 	int error, i, sig;
56425f6e35aSPoul-Henning Kamp 
56525f6e35aSPoul-Henning Kamp 	error = 0;	/* satisfy compiler */
56625f6e35aSPoul-Henning Kamp 
56725f6e35aSPoul-Henning Kamp 	/*
56825f6e35aSPoul-Henning Kamp 	 * Do it right now so we can log either proper call of abort2(), or
56925f6e35aSPoul-Henning Kamp 	 * note, that invalid argument was passed. 512 is big enough to
57025f6e35aSPoul-Henning Kamp 	 * handle 16 arguments' descriptions with additional comments.
57125f6e35aSPoul-Henning Kamp 	 */
57225f6e35aSPoul-Henning Kamp 	sb = sbuf_new(NULL, NULL, 512, SBUF_FIXEDLEN);
57325f6e35aSPoul-Henning Kamp 	sbuf_clear(sb);
57425f6e35aSPoul-Henning Kamp 	sbuf_printf(sb, "%s(pid %d uid %d) aborted: ",
57525f6e35aSPoul-Henning Kamp 	    p->p_comm, p->p_pid, td->td_ucred->cr_uid);
57625f6e35aSPoul-Henning Kamp 	/*
57725f6e35aSPoul-Henning Kamp 	 * Since we can't return from abort2(), send SIGKILL in cases, where
57825f6e35aSPoul-Henning Kamp 	 * abort2() was called improperly
57925f6e35aSPoul-Henning Kamp 	 */
58025f6e35aSPoul-Henning Kamp 	sig = SIGKILL;
58125f6e35aSPoul-Henning Kamp 	/* Prevent from DoSes from user-space. */
58225f6e35aSPoul-Henning Kamp 	if (uap->nargs < 0 || uap->nargs > 16)
58325f6e35aSPoul-Henning Kamp 		goto out;
58425f6e35aSPoul-Henning Kamp 	if (uap->args == NULL)
58525f6e35aSPoul-Henning Kamp 		goto out;
58625f6e35aSPoul-Henning Kamp 	error = copyin(uap->args, uargs, uap->nargs * sizeof(void *));
58725f6e35aSPoul-Henning Kamp 	if (error != 0)
58825f6e35aSPoul-Henning Kamp 		goto out;
58925f6e35aSPoul-Henning Kamp 	/*
59025f6e35aSPoul-Henning Kamp 	 * Limit size of 'reason' string to 128. Will fit even when
59125f6e35aSPoul-Henning Kamp 	 * maximal number of arguments was chosen to be logged.
59225f6e35aSPoul-Henning Kamp 	 */
59325f6e35aSPoul-Henning Kamp 	if (uap->why != NULL) {
59425f6e35aSPoul-Henning Kamp 		error = sbuf_copyin(sb, uap->why, 128);
59525f6e35aSPoul-Henning Kamp 		if (error < 0)
59625f6e35aSPoul-Henning Kamp 			goto out;
59725f6e35aSPoul-Henning Kamp 	} else {
59825f6e35aSPoul-Henning Kamp 		sbuf_printf(sb, "(null)");
59925f6e35aSPoul-Henning Kamp 	}
60025f6e35aSPoul-Henning Kamp 	if (uap->nargs) {
60125f6e35aSPoul-Henning Kamp 		sbuf_printf(sb, "(");
60225f6e35aSPoul-Henning Kamp 		for (i = 0;i < uap->nargs; i++)
60325f6e35aSPoul-Henning Kamp 			sbuf_printf(sb, "%s%p", i == 0 ? "" : ", ", uargs[i]);
60425f6e35aSPoul-Henning Kamp 		sbuf_printf(sb, ")");
60525f6e35aSPoul-Henning Kamp 	}
60625f6e35aSPoul-Henning Kamp 	/*
60725f6e35aSPoul-Henning Kamp 	 * Final stage: arguments were proper, string has been
60825f6e35aSPoul-Henning Kamp 	 * successfully copied from userspace, and copying pointers
60925f6e35aSPoul-Henning Kamp 	 * from user-space succeed.
61025f6e35aSPoul-Henning Kamp 	 */
61125f6e35aSPoul-Henning Kamp 	sig = SIGABRT;
61225f6e35aSPoul-Henning Kamp out:
61325f6e35aSPoul-Henning Kamp 	if (sig == SIGKILL) {
61425f6e35aSPoul-Henning Kamp 		sbuf_trim(sb);
61525f6e35aSPoul-Henning Kamp 		sbuf_printf(sb, " (Reason text inaccessible)");
61625f6e35aSPoul-Henning Kamp 	}
61725f6e35aSPoul-Henning Kamp 	sbuf_cat(sb, "\n");
61825f6e35aSPoul-Henning Kamp 	sbuf_finish(sb);
61925f6e35aSPoul-Henning Kamp 	log(LOG_INFO, "%s", sbuf_data(sb));
62025f6e35aSPoul-Henning Kamp 	sbuf_delete(sb);
62125f6e35aSPoul-Henning Kamp 	exit1(td, W_EXITCODE(0, sig));
62225f6e35aSPoul-Henning Kamp 	return (0);
62325f6e35aSPoul-Henning Kamp }
62425f6e35aSPoul-Henning Kamp 
62525f6e35aSPoul-Henning Kamp 
626b2f9e8b1SBruce Evans #ifdef COMPAT_43
627234216efSMatthew Dillon /*
628a9a64385SJohn Baldwin  * The dirty work is handled by kern_wait().
629a9a64385SJohn Baldwin  *
630a9a64385SJohn Baldwin  * MPSAFE.
631234216efSMatthew Dillon  */
63226f9a767SRodney W. Grimes int
633830c3153SDag-Erling Smørgrav owait(struct thread *td, struct owait_args *uap __unused)
634df8bae1dSRodney W. Grimes {
635b7e23e82SJohn Baldwin 	int error, status;
636df8bae1dSRodney W. Grimes 
637b7e23e82SJohn Baldwin 	error = kern_wait(td, WAIT_ANY, &status, 0, NULL);
638b7e23e82SJohn Baldwin 	if (error == 0)
639b7e23e82SJohn Baldwin 		td->td_retval[1] = status;
640b7e23e82SJohn Baldwin 	return (error);
641df8bae1dSRodney W. Grimes }
642b2f9e8b1SBruce Evans #endif /* COMPAT_43 */
643df8bae1dSRodney W. Grimes 
644234216efSMatthew Dillon /*
645a9a64385SJohn Baldwin  * The dirty work is handled by kern_wait().
646a9a64385SJohn Baldwin  *
647a9a64385SJohn Baldwin  * MPSAFE.
648234216efSMatthew Dillon  */
64926f9a767SRodney W. Grimes int
650830c3153SDag-Erling Smørgrav wait4(struct thread *td, struct wait_args *uap)
651df8bae1dSRodney W. Grimes {
65278c85e8dSJohn Baldwin 	struct rusage ru, *rup;
653b7e23e82SJohn Baldwin 	int error, status;
654b40ce416SJulian Elischer 
65578c85e8dSJohn Baldwin 	if (uap->rusage != NULL)
65678c85e8dSJohn Baldwin 		rup = &ru;
65778c85e8dSJohn Baldwin 	else
65878c85e8dSJohn Baldwin 		rup = NULL;
65978c85e8dSJohn Baldwin 	error = kern_wait(td, uap->pid, &status, uap->options, rup);
660b7e23e82SJohn Baldwin 	if (uap->status != NULL && error == 0)
661b7e23e82SJohn Baldwin 		error = copyout(&status, uap->status, sizeof(status));
662b7e23e82SJohn Baldwin 	if (uap->rusage != NULL && error == 0)
663b7e23e82SJohn Baldwin 		error = copyout(&ru, uap->rusage, sizeof(struct rusage));
664b7e23e82SJohn Baldwin 	return (error);
665df8bae1dSRodney W. Grimes }
666df8bae1dSRodney W. Grimes 
667b7e23e82SJohn Baldwin int
6687eaec467SJohn Baldwin kern_wait(struct thread *td, pid_t pid, int *status, int options,
6697eaec467SJohn Baldwin     struct rusage *rusage)
670df8bae1dSRodney W. Grimes {
67148bfcdddSJulian Elischer 	struct proc *p, *q, *t;
672a9a64385SJohn Baldwin 	int error, nfound;
673df8bae1dSRodney W. Grimes 
674de3007e8SWayne Salamon 	AUDIT_ARG(pid, pid);
675de3007e8SWayne Salamon 
676b40ce416SJulian Elischer 	q = td->td_proc;
677b7e23e82SJohn Baldwin 	if (pid == 0) {
678f591779bSSeigo Tanimura 		PROC_LOCK(q);
679b7e23e82SJohn Baldwin 		pid = -q->p_pgid;
680f591779bSSeigo Tanimura 		PROC_UNLOCK(q);
681f591779bSSeigo Tanimura 	}
682b7e23e82SJohn Baldwin 	if (options &~ (WUNTRACED|WNOHANG|WCONTINUED|WLINUXCLONE))
6836dc958b9SJohn Baldwin 		return (EINVAL);
684df8bae1dSRodney W. Grimes loop:
685902c0d82SDavid Xu 	if (q->p_flag & P_STATCHILD) {
686902c0d82SDavid Xu 		PROC_LOCK(q);
687902c0d82SDavid Xu 		q->p_flag &= ~P_STATCHILD;
688902c0d82SDavid Xu 		PROC_UNLOCK(q);
689902c0d82SDavid Xu 	}
690df8bae1dSRodney W. Grimes 	nfound = 0;
691d7aadbf9SJohn Baldwin 	sx_xlock(&proctree_lock);
6922e3c8fcbSPoul-Henning Kamp 	LIST_FOREACH(p, &q->p_children, p_sibling) {
693f591779bSSeigo Tanimura 		PROC_LOCK(p);
694b7e23e82SJohn Baldwin 		if (pid != WAIT_ANY &&
695b7e23e82SJohn Baldwin 		    p->p_pid != pid && p->p_pgid != -pid) {
696f591779bSSeigo Tanimura 			PROC_UNLOCK(p);
697df8bae1dSRodney W. Grimes 			continue;
698f591779bSSeigo Tanimura 		}
699babe9a2bSRobert Watson 		if (p_canwait(td, p)) {
700babe9a2bSRobert Watson 			PROC_UNLOCK(p);
701babe9a2bSRobert Watson 			continue;
702babe9a2bSRobert Watson 		}
7034ac9ae70SJulian Elischer 
7041156bc4dSJake Burkholder 		/*
7051156bc4dSJake Burkholder 		 * This special case handles a kthread spawned by linux_clone
7061156bc4dSJake Burkholder 		 * (see linux_misc.c).  The linux_wait4 and linux_waitpid
7071156bc4dSJake Burkholder 		 * functions need to be able to distinguish between waiting
7081156bc4dSJake Burkholder 		 * on a process and waiting on a thread.  It is a thread if
7091156bc4dSJake Burkholder 		 * p_sigparent is not SIGCHLD, and the WLINUXCLONE option
7101156bc4dSJake Burkholder 		 * signifies we want to wait for threads and not processes.
7114ac9ae70SJulian Elischer 		 */
7121156bc4dSJake Burkholder 		if ((p->p_sigparent != SIGCHLD) ^
713b7e23e82SJohn Baldwin 		    ((options & WLINUXCLONE) != 0)) {
714a914fb6bSJohn Baldwin 			PROC_UNLOCK(p);
7154ac9ae70SJulian Elischer 			continue;
716a914fb6bSJohn Baldwin 		}
7174ac9ae70SJulian Elischer 
718df8bae1dSRodney W. Grimes 		nfound++;
719e602ba25SJulian Elischer 		if (p->p_state == PRS_ZOMBIE) {
720ddf9c4f7SJohn Baldwin 
721ddf9c4f7SJohn Baldwin 			/*
722ddf9c4f7SJohn Baldwin 			 * It is possible that the last thread of this
723ddf9c4f7SJohn Baldwin 			 * process is still running on another CPU
724ddf9c4f7SJohn Baldwin 			 * in thread_exit() after having dropped the process
725ddf9c4f7SJohn Baldwin 			 * lock via PROC_UNLOCK() but before it has completed
726ddf9c4f7SJohn Baldwin 			 * cpu_throw().  In that case, the other thread must
727ddf9c4f7SJohn Baldwin 			 * still hold sched_lock, so simply by acquiring
728ddf9c4f7SJohn Baldwin 			 * sched_lock once we will wait long enough for the
729ddf9c4f7SJohn Baldwin 			 * thread to exit in that case.
730ddf9c4f7SJohn Baldwin 			 */
731ddf9c4f7SJohn Baldwin 			mtx_lock_spin(&sched_lock);
732ddf9c4f7SJohn Baldwin 			mtx_unlock_spin(&sched_lock);
733ddf9c4f7SJohn Baldwin 
734b40ce416SJulian Elischer 			td->td_retval[0] = p->p_pid;
735b7e23e82SJohn Baldwin 			if (status)
736b7e23e82SJohn Baldwin 				*status = p->p_xstat;	/* convert to int */
73778c85e8dSJohn Baldwin 			if (rusage) {
738fd544ee8SRobert Watson 				*rusage = *p->p_ru;
73978c85e8dSJohn Baldwin 				calcru(p, &rusage->ru_utime, &rusage->ru_stime);
74078c85e8dSJohn Baldwin 			}
741b7e23e82SJohn Baldwin 
742ebceaf6dSDavid Xu 			PROC_LOCK(q);
743ebceaf6dSDavid Xu 			sigqueue_take(p->p_ksi);
744ebceaf6dSDavid Xu 			PROC_UNLOCK(q);
745ebceaf6dSDavid Xu 
746df8bae1dSRodney W. Grimes 			/*
747df8bae1dSRodney W. Grimes 			 * If we got the child via a ptrace 'attach',
748df8bae1dSRodney W. Grimes 			 * we need to give it back to the old parent.
749df8bae1dSRodney W. Grimes 			 */
750b7e23e82SJohn Baldwin 			PROC_UNLOCK(p);
751d7aadbf9SJohn Baldwin 			if (p->p_oppid && (t = pfind(p->p_oppid)) != NULL) {
752c65437a3SJohn Baldwin 				PROC_LOCK(p);
753df8bae1dSRodney W. Grimes 				p->p_oppid = 0;
754df8bae1dSRodney W. Grimes 				proc_reparent(p, t);
755c65437a3SJohn Baldwin 				PROC_UNLOCK(p);
756ebceaf6dSDavid Xu 				tdsignal(t, NULL, SIGCHLD, p->p_ksi);
7577f05b035SAlfred Perlstein 				wakeup(t);
758c65437a3SJohn Baldwin 				PROC_UNLOCK(t);
7591005a129SJohn Baldwin 				sx_xunlock(&proctree_lock);
760d7aadbf9SJohn Baldwin 				return (0);
761df8bae1dSRodney W. Grimes 			}
762ebdc3f1dSSeigo Tanimura 
7633890793eSTim J. Robbins 			/*
7643890793eSTim J. Robbins 			 * Remove other references to this process to ensure
7653890793eSTim J. Robbins 			 * we have an exclusive reference.
7663890793eSTim J. Robbins 			 */
7676ec62361STim J. Robbins 			sx_xlock(&allproc_lock);
7686ec62361STim J. Robbins 			LIST_REMOVE(p, p_list);	/* off zombproc */
7696ec62361STim J. Robbins 			sx_xunlock(&allproc_lock);
7706ec62361STim J. Robbins 			LIST_REMOVE(p, p_sibling);
7713890793eSTim J. Robbins 			leavepgrp(p);
772ebdc3f1dSSeigo Tanimura 			sx_xunlock(&proctree_lock);
773ebdc3f1dSSeigo Tanimura 
774ebdc3f1dSSeigo Tanimura 			/*
775d7aadbf9SJohn Baldwin 			 * As a side effect of this lock, we know that
776d7aadbf9SJohn Baldwin 			 * all other writes to this proc are visible now, so
777d7aadbf9SJohn Baldwin 			 * no more locking is needed for p.
778d7aadbf9SJohn Baldwin 			 */
779d7aadbf9SJohn Baldwin 			PROC_LOCK(p);
780d7aadbf9SJohn Baldwin 			p->p_xstat = 0;		/* XXX: why? */
781d7aadbf9SJohn Baldwin 			PROC_UNLOCK(p);
782d7aadbf9SJohn Baldwin 			PROC_LOCK(q);
78378c85e8dSJohn Baldwin 			ruadd(&q->p_stats->p_cru, &q->p_crux, p->p_ru,
78478c85e8dSJohn Baldwin 			    &p->p_rux);
785d7aadbf9SJohn Baldwin 			PROC_UNLOCK(q);
786d7aadbf9SJohn Baldwin 			FREE(p->p_ru, M_ZOMBIE);
787d7aadbf9SJohn Baldwin 			p->p_ru = NULL;
788d7aadbf9SJohn Baldwin 
789d7aadbf9SJohn Baldwin 			/*
790d7aadbf9SJohn Baldwin 			 * Decrement the count of procs running with this uid.
791d7aadbf9SJohn Baldwin 			 */
792d7aadbf9SJohn Baldwin 			(void)chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0);
793d7aadbf9SJohn Baldwin 
794d7aadbf9SJohn Baldwin 			/*
795a9a64385SJohn Baldwin 			 * Free credentials, arguments, and sigacts.
796df8bae1dSRodney W. Grimes 			 */
797da654d90SPoul-Henning Kamp 			crfree(p->p_ucred);
79890af4afaSJohn Baldwin 			p->p_ucred = NULL;
799d7aadbf9SJohn Baldwin 			pargs_drop(p->p_args);
800d7aadbf9SJohn Baldwin 			p->p_args = NULL;
80190af4afaSJohn Baldwin 			sigacts_free(p->p_sigacts);
80290af4afaSJohn Baldwin 			p->p_sigacts = NULL;
80388c5ea45SJulian Elischer 
804df8bae1dSRodney W. Grimes 			/*
805a9a64385SJohn Baldwin 			 * Do any thread-system specific cleanups.
806e602ba25SJulian Elischer 			 */
807696058c3SJulian Elischer 			thread_wait(p);
808e602ba25SJulian Elischer 
809e602ba25SJulian Elischer 			/*
810eb30c1c0SPeter Wemm 			 * Give vm and machine-dependent layer a chance
811df8bae1dSRodney W. Grimes 			 * to free anything that cpu_exit couldn't
812df8bae1dSRodney W. Grimes 			 * release while still running in process context.
813df8bae1dSRodney W. Grimes 			 */
814eb30c1c0SPeter Wemm 			vm_waitproc(p);
8152555374cSRobert Watson #ifdef MAC
8162555374cSRobert Watson 			mac_destroy_proc(p);
8172555374cSRobert Watson #endif
818fcf7f27aSRobert Watson #ifdef AUDIT
819fcf7f27aSRobert Watson 			audit_proc_free(p);
820fcf7f27aSRobert Watson #endif
8211faf202eSJulian Elischer 			KASSERT(FIRST_THREAD_IN_PROC(p),
822b7e23e82SJohn Baldwin 			    ("kern_wait: no residual thread!"));
823c897b813SJeff Roberson 			uma_zfree(proc_zone, p);
824d7aadbf9SJohn Baldwin 			sx_xlock(&allproc_lock);
825df8bae1dSRodney W. Grimes 			nprocs--;
826d7aadbf9SJohn Baldwin 			sx_xunlock(&allproc_lock);
827d7aadbf9SJohn Baldwin 			return (0);
828df8bae1dSRodney W. Grimes 		}
829112afcb2SJohn Baldwin 		mtx_lock_spin(&sched_lock);
8305a2f73e6SDavid Xu 		if ((p->p_flag & P_STOPPED_SIG) &&
8315a2f73e6SDavid Xu 		    (p->p_suspcount == p->p_numthreads) &&
832a9a64385SJohn Baldwin 		    (p->p_flag & P_WAITED) == 0 &&
833b7e23e82SJohn Baldwin 		    (p->p_flag & P_TRACED || options & WUNTRACED)) {
834112afcb2SJohn Baldwin 			mtx_unlock_spin(&sched_lock);
835df8bae1dSRodney W. Grimes 			p->p_flag |= P_WAITED;
836d7aadbf9SJohn Baldwin 			sx_xunlock(&proctree_lock);
837b40ce416SJulian Elischer 			td->td_retval[0] = p->p_pid;
838b7e23e82SJohn Baldwin 			if (status)
839b7e23e82SJohn Baldwin 				*status = W_STOPCODE(p->p_xstat);
840d7aadbf9SJohn Baldwin 			PROC_UNLOCK(p);
841ebceaf6dSDavid Xu 
842ebceaf6dSDavid Xu 			PROC_LOCK(q);
843ebceaf6dSDavid Xu 			sigqueue_take(p->p_ksi);
844ebceaf6dSDavid Xu 			PROC_UNLOCK(q);
845ebceaf6dSDavid Xu 
846b7e23e82SJohn Baldwin 			return (0);
847df8bae1dSRodney W. Grimes 		}
848112afcb2SJohn Baldwin 		mtx_unlock_spin(&sched_lock);
849b7e23e82SJohn Baldwin 		if (options & WCONTINUED && (p->p_flag & P_CONTINUED)) {
8506ee093fbSMike Barcroft 			sx_xunlock(&proctree_lock);
8516ee093fbSMike Barcroft 			td->td_retval[0] = p->p_pid;
8526ee093fbSMike Barcroft 			p->p_flag &= ~P_CONTINUED;
8536ee093fbSMike Barcroft 			PROC_UNLOCK(p);
8546ee093fbSMike Barcroft 
855ebceaf6dSDavid Xu 			PROC_LOCK(q);
856ebceaf6dSDavid Xu 			sigqueue_take(p->p_ksi);
857ebceaf6dSDavid Xu 			PROC_UNLOCK(q);
858ebceaf6dSDavid Xu 
859b7e23e82SJohn Baldwin 			if (status)
860b7e23e82SJohn Baldwin 				*status = SIGCONT;
861b7e23e82SJohn Baldwin 			return (0);
8626ee093fbSMike Barcroft 		}
863d7aadbf9SJohn Baldwin 		PROC_UNLOCK(p);
864d7aadbf9SJohn Baldwin 	}
865d7aadbf9SJohn Baldwin 	if (nfound == 0) {
866d7aadbf9SJohn Baldwin 		sx_xunlock(&proctree_lock);
867d7aadbf9SJohn Baldwin 		return (ECHILD);
868d7aadbf9SJohn Baldwin 	}
869b7e23e82SJohn Baldwin 	if (options & WNOHANG) {
870d7aadbf9SJohn Baldwin 		sx_xunlock(&proctree_lock);
871d7aadbf9SJohn Baldwin 		td->td_retval[0] = 0;
872d7aadbf9SJohn Baldwin 		return (0);
873d7aadbf9SJohn Baldwin 	}
874d7aadbf9SJohn Baldwin 	PROC_LOCK(q);
875d7aadbf9SJohn Baldwin 	sx_xunlock(&proctree_lock);
87695992d56SDavid Xu 	if (q->p_flag & P_STATCHILD) {
87795992d56SDavid Xu 		q->p_flag &= ~P_STATCHILD;
87895992d56SDavid Xu 		error = 0;
87995992d56SDavid Xu 	} else
8807f05b035SAlfred Perlstein 		error = msleep(q, &q->p_mtx, PWAIT | PCATCH, "wait", 0);
881d7aadbf9SJohn Baldwin 	PROC_UNLOCK(q);
8824ae89b95SJohn Baldwin 	if (error)
883d7aadbf9SJohn Baldwin 		return (error);
884d7aadbf9SJohn Baldwin 	goto loop;
885d7aadbf9SJohn Baldwin }
886df8bae1dSRodney W. Grimes 
887df8bae1dSRodney W. Grimes /*
88898f03f90SJake Burkholder  * Make process 'parent' the new parent of process 'child'.
88998f03f90SJake Burkholder  * Must be called with an exclusive hold of proctree lock.
890df8bae1dSRodney W. Grimes  */
891df8bae1dSRodney W. Grimes void
892830c3153SDag-Erling Smørgrav proc_reparent(struct proc *child, struct proc *parent)
893df8bae1dSRodney W. Grimes {
894df8bae1dSRodney W. Grimes 
8954e5e677bSJohn Baldwin 	sx_assert(&proctree_lock, SX_XLOCKED);
896c65437a3SJohn Baldwin 	PROC_LOCK_ASSERT(child, MA_OWNED);
897df8bae1dSRodney W. Grimes 	if (child->p_pptr == parent)
898df8bae1dSRodney W. Grimes 		return;
899df8bae1dSRodney W. Grimes 
900b75356e1SJeffrey Hsu 	LIST_REMOVE(child, p_sibling);
901b75356e1SJeffrey Hsu 	LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
902df8bae1dSRodney W. Grimes 	child->p_pptr = parent;
903df8bae1dSRodney W. Grimes }
904