xref: /freebsd/sys/kern/kern_exit.c (revision 6ddcc23386b9a6241f1e3193e192fd5b8d02266c)
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"
42db6a20e2SGarrett Wollman 
43df8bae1dSRodney W. Grimes #include <sys/param.h>
44df8bae1dSRodney W. Grimes #include <sys/systm.h>
455fdb8324SBruce Evans #include <sys/sysproto.h>
464a144410SRobert Watson #include <sys/capsicum.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>
53cfb5f768SJonathan Anderson #include <sys/procdesc.h>
542a024a2bSSean Eric Fagan #include <sys/pioctl.h>
55413628a7SBjoern A. Zeeb #include <sys/jail.h>
56df8bae1dSRodney W. Grimes #include <sys/tty.h>
57df8bae1dSRodney W. Grimes #include <sys/wait.h>
58eb30c1c0SPeter Wemm #include <sys/vmmeter.h>
597a6b989bSJohn Baldwin #include <sys/vnode.h>
60097055e2SEdward Tomasz Napierala #include <sys/racct.h>
61df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
6225f6e35aSPoul-Henning Kamp #include <sys/sbuf.h>
63797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
64b43179fbSJeff Roberson #include <sys/sched.h>
651005a129SJohn Baldwin #include <sys/sx.h>
66c8837938SJohn Baldwin #include <sys/syscallsubr.h>
6725f6e35aSPoul-Henning Kamp #include <sys/syslog.h>
68df8bae1dSRodney W. Grimes #include <sys/ptrace.h>
697c409b8aSJeffrey Hsu #include <sys/acct.h>		/* for acct_process() function prototype */
70797f2d22SPoul-Henning Kamp #include <sys/filedesc.h>
715d217f17SJohn Birrell #include <sys/sdt.h>
72780dc5a8SPeter Wemm #include <sys/shm.h>
73780dc5a8SPeter Wemm #include <sys/sem.h>
746c84de02SJohn Baldwin #ifdef KTRACE
756c84de02SJohn Baldwin #include <sys/ktrace.h>
766c84de02SJohn Baldwin #endif
77780dc5a8SPeter Wemm 
78fcf7f27aSRobert Watson #include <security/audit/audit.h>
79aed55708SRobert Watson #include <security/mac/mac_framework.h>
80fcf7f27aSRobert Watson 
81df8bae1dSRodney W. Grimes #include <vm/vm.h>
82eb30c1c0SPeter Wemm #include <vm/vm_extern.h>
837a6b989bSJohn Baldwin #include <vm/vm_param.h>
84efeaf95aSDavid Greenman #include <vm/pmap.h>
85efeaf95aSDavid Greenman #include <vm/vm_map.h>
862d21129dSAlan Cox #include <vm/vm_page.h>
87c897b813SJeff Roberson #include <vm/uma.h>
88df8bae1dSRodney W. Grimes 
895d217f17SJohn Birrell #ifdef KDTRACE_HOOKS
905d217f17SJohn Birrell #include <sys/dtrace_bsd.h>
915d217f17SJohn Birrell dtrace_execexit_func_t	dtrace_fasttrap_exit;
925d217f17SJohn Birrell #endif
935d217f17SJohn Birrell 
945d217f17SJohn Birrell SDT_PROVIDER_DECLARE(proc);
95d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(proc, kernel, , exit, "int");
965d217f17SJohn Birrell 
97c0bc2867SGleb Smirnoff /* Hook for NFS teardown procedure. */
98c0bc2867SGleb Smirnoff void (*nlminfo_release_p)(struct proc *p);
99c0bc2867SGleb Smirnoff 
100d7359980SKonstantin Belousov struct proc *
101d7359980SKonstantin Belousov proc_realparent(struct proc *child)
102d7359980SKonstantin Belousov {
103d7359980SKonstantin Belousov 	struct proc *p, *parent;
104d7359980SKonstantin Belousov 
105d7359980SKonstantin Belousov 	sx_assert(&proctree_lock, SX_LOCKED);
106d7359980SKonstantin Belousov 	if ((child->p_treeflag & P_TREE_ORPHANED) == 0) {
1079152087eSMateusz Guzik 		if (child->p_oppid == 0 ||
1089152087eSMateusz Guzik 		    child->p_pptr->p_pid == child->p_oppid)
1099152087eSMateusz Guzik 			parent = child->p_pptr;
1109152087eSMateusz Guzik 		else
1119152087eSMateusz Guzik 			parent = initproc;
1129152087eSMateusz Guzik 		return (parent);
113d7359980SKonstantin Belousov 	}
114d7359980SKonstantin Belousov 	for (p = child; (p->p_treeflag & P_TREE_FIRST_ORPHAN) == 0;) {
115d7359980SKonstantin Belousov 		/* Cannot use LIST_PREV(), since the list head is not known. */
116d7359980SKonstantin Belousov 		p = __containerof(p->p_orphan.le_prev, struct proc,
117d7359980SKonstantin Belousov 		    p_orphan.le_next);
118d7359980SKonstantin Belousov 		KASSERT((p->p_treeflag & P_TREE_ORPHANED) != 0,
119d7359980SKonstantin Belousov 		    ("missing P_ORPHAN %p", p));
120d7359980SKonstantin Belousov 	}
121d7359980SKonstantin Belousov 	parent = __containerof(p->p_orphan.le_prev, struct proc,
122d7359980SKonstantin Belousov 	    p_orphans.lh_first);
123d7359980SKonstantin Belousov 	return (parent);
124d7359980SKonstantin Belousov }
125d7359980SKonstantin Belousov 
1262e39e24fSKonstantin Belousov static void
1272e39e24fSKonstantin Belousov clear_orphan(struct proc *p)
1282e39e24fSKonstantin Belousov {
129d7359980SKonstantin Belousov 	struct proc *p1;
1302e39e24fSKonstantin Belousov 
131d7359980SKonstantin Belousov 	sx_assert(&proctree_lock, SA_XLOCKED);
132d7359980SKonstantin Belousov 	if ((p->p_treeflag & P_TREE_ORPHANED) == 0)
133d7359980SKonstantin Belousov 		return;
134d7359980SKonstantin Belousov 	if ((p->p_treeflag & P_TREE_FIRST_ORPHAN) != 0) {
135d7359980SKonstantin Belousov 		p1 = LIST_NEXT(p, p_orphan);
136d7359980SKonstantin Belousov 		if (p1 != NULL)
137d7359980SKonstantin Belousov 			p1->p_treeflag |= P_TREE_FIRST_ORPHAN;
138d7359980SKonstantin Belousov 		p->p_treeflag &= ~P_TREE_FIRST_ORPHAN;
1392e39e24fSKonstantin Belousov 	}
140d7359980SKonstantin Belousov 	LIST_REMOVE(p, p_orphan);
141d7359980SKonstantin Belousov 	p->p_treeflag &= ~P_TREE_ORPHANED;
1422e39e24fSKonstantin Belousov }
1432e39e24fSKonstantin Belousov 
144df8bae1dSRodney W. Grimes /*
145873fbcd7SRobert Watson  * exit -- death of process.
146df8bae1dSRodney W. Grimes  */
147fc0b1dbfSBruce Evans void
1488451d0ddSKip Macy sys_sys_exit(struct thread *td, struct sys_exit_args *uap)
149df8bae1dSRodney W. Grimes {
150b40ce416SJulian Elischer 
151b40ce416SJulian Elischer 	exit1(td, W_EXITCODE(uap->rval, 0));
152df8bae1dSRodney W. Grimes 	/* NOTREACHED */
153df8bae1dSRodney W. Grimes }
154df8bae1dSRodney W. Grimes 
155df8bae1dSRodney W. Grimes /*
156873fbcd7SRobert Watson  * Exit: deallocate address space and other resources, change proc state to
157873fbcd7SRobert Watson  * zombie, and unlink proc from allproc and parent's lists.  Save exit status
158873fbcd7SRobert Watson  * and rusage for wait().  Check for child processes and orphan them.
159df8bae1dSRodney W. Grimes  */
160fc0b1dbfSBruce Evans void
161830c3153SDag-Erling Smørgrav exit1(struct thread *td, int rv)
162df8bae1dSRodney W. Grimes {
163a661bebeSMateusz Guzik 	struct proc *p, *nq, *q, *t;
164a661bebeSMateusz Guzik 	struct thread *tdt;
165bc093719SEd Schouten 	struct vnode *ttyvp = NULL;
166df8bae1dSRodney W. Grimes 
167e17660e7SKris Kennaway 	mtx_assert(&Giant, MA_NOTOWNED);
1680cddd8f0SMatthew Dillon 
169276c5169SJohn Baldwin 	p = td->td_proc;
1705486ffc8SMarius Strobl 	/*
1715486ffc8SMarius Strobl 	 * XXX in case we're rebooting we just let init die in order to
1725486ffc8SMarius Strobl 	 * work around an unsolved stack overflow seen very late during
1735486ffc8SMarius Strobl 	 * shutdown on sparc64 when the gmirror worker process exists.
1745486ffc8SMarius Strobl 	 */
1755486ffc8SMarius Strobl 	if (p == initproc && rebooting == 0) {
1765f7bd355SPoul-Henning Kamp 		printf("init died (signal %d, exit %d)\n",
177df8bae1dSRodney W. Grimes 		    WTERMSIG(rv), WEXITSTATUS(rv));
1785f7bd355SPoul-Henning Kamp 		panic("Going nowhere without my init!");
1795f7bd355SPoul-Henning Kamp 	}
1802c1011f7SJohn Dyson 
1817a6b989bSJohn Baldwin 	/*
1822c10d16aSJeff Roberson 	 * MUST abort all other threads before proceeding past here.
1837a6b989bSJohn Baldwin 	 */
184e602ba25SJulian Elischer 	PROC_LOCK(p);
185e602ba25SJulian Elischer 	/*
186aba1ca52SKonstantin Belousov 	 * First check if some other thread or external request got
187aba1ca52SKonstantin Belousov 	 * here before us.  If so, act appropriately: exit or suspend.
188aba1ca52SKonstantin Belousov 	 * We must ensure that stop requests are handled before we set
189aba1ca52SKonstantin Belousov 	 * P_WEXIT.
190e602ba25SJulian Elischer 	 */
191e602ba25SJulian Elischer 	thread_suspend_check(0);
192aba1ca52SKonstantin Belousov 	while (p->p_flag & P_HADTHREADS) {
193e602ba25SJulian Elischer 		/*
194e602ba25SJulian Elischer 		 * Kill off the other threads. This requires
1956111dcd2SJohn Baldwin 		 * some co-operation from other parts of the kernel
1966111dcd2SJohn Baldwin 		 * so it may not be instantaneous.  With this state set
1976111dcd2SJohn Baldwin 		 * any thread entering the kernel from userspace will
198e602ba25SJulian Elischer 		 * thread_exit() in trap().  Any thread attempting to
1996111dcd2SJohn Baldwin 		 * sleep will return immediately with EINTR or EWOULDBLOCK
2006111dcd2SJohn Baldwin 		 * which will hopefully force them to back out to userland
2016111dcd2SJohn Baldwin 		 * freeing resources as they go.  Any thread attempting
202b3248998SJulian Elischer 		 * to return to userland will thread_exit() from userret().
2036111dcd2SJohn Baldwin 		 * thread_exit() will unsuspend us when the last of the
2046111dcd2SJohn Baldwin 		 * other threads exits.
205b370279eSDavid Xu 		 * If there is already a thread singler after resumption,
2066111dcd2SJohn Baldwin 		 * calling thread_single will fail; in that case, we just
207b370279eSDavid Xu 		 * re-check all suspension request, the thread should
208b370279eSDavid Xu 		 * either be suspended there or exit.
209e602ba25SJulian Elischer 		 */
210*6ddcc233SKonstantin Belousov 		if (!thread_single(p, SINGLE_EXIT))
211e602ba25SJulian Elischer 			/*
212aba1ca52SKonstantin Belousov 			 * All other activity in this process is now
213aba1ca52SKonstantin Belousov 			 * stopped.  Threading support has been turned
214aba1ca52SKonstantin Belousov 			 * off.
215e602ba25SJulian Elischer 			 */
216aba1ca52SKonstantin Belousov 			break;
217aba1ca52SKonstantin Belousov 		/*
218aba1ca52SKonstantin Belousov 		 * Recheck for new stop or suspend requests which
219aba1ca52SKonstantin Belousov 		 * might appear while process lock was dropped in
220aba1ca52SKonstantin Belousov 		 * thread_single().
221aba1ca52SKonstantin Belousov 		 */
222aba1ca52SKonstantin Belousov 		thread_suspend_check(0);
223e602ba25SJulian Elischer 	}
2241c4bcd05SJeff Roberson 	KASSERT(p->p_numthreads == 1,
2251c4bcd05SJeff Roberson 	    ("exit1: proc %p exiting with %d threads", p, p->p_numthreads));
22658c77a9dSEdward Tomasz Napierala 	racct_sub(p, RACCT_NTHR, 1);
22706ad42b2SJohn Baldwin 	/*
22806ad42b2SJohn Baldwin 	 * Wakeup anyone in procfs' PIOCWAIT.  They should have a hold
22906ad42b2SJohn Baldwin 	 * on our vmspace, so we should block below until they have
23006ad42b2SJohn Baldwin 	 * released their reference to us.  Note that if they have
23106ad42b2SJohn Baldwin 	 * requested S_EXIT stops we will block here until they ack
23206ad42b2SJohn Baldwin 	 * via PIOCCONT.
23306ad42b2SJohn Baldwin 	 */
23406ad42b2SJohn Baldwin 	_STOPEVENT(p, S_EXIT, rv);
23506ad42b2SJohn Baldwin 
23606ad42b2SJohn Baldwin 	/*
2370f14f15bSJohn Baldwin 	 * Ignore any pending request to stop due to a stop signal.
2380f14f15bSJohn Baldwin 	 * Once P_WEXIT is set, future requests will be ignored as
2390f14f15bSJohn Baldwin 	 * well.
2400f14f15bSJohn Baldwin 	 */
2410f14f15bSJohn Baldwin 	p->p_flag &= ~P_STOPPED_SIG;
2420f14f15bSJohn Baldwin 	KASSERT(!P_SHOULDSTOP(p), ("exiting process is stopped"));
2430f14f15bSJohn Baldwin 
2440f14f15bSJohn Baldwin 	/*
24506ad42b2SJohn Baldwin 	 * Note that we are exiting and do another wakeup of anyone in
24606ad42b2SJohn Baldwin 	 * PIOCWAIT in case they aren't listening for S_EXIT stops or
24706ad42b2SJohn Baldwin 	 * decided to wait again after we told them we are exiting.
24806ad42b2SJohn Baldwin 	 */
249e602ba25SJulian Elischer 	p->p_flag |= P_WEXIT;
25006ad42b2SJohn Baldwin 	wakeup(&p->p_stype);
25106ad42b2SJohn Baldwin 
25206ad42b2SJohn Baldwin 	/*
25306ad42b2SJohn Baldwin 	 * Wait for any processes that have a hold on our vmspace to
25406ad42b2SJohn Baldwin 	 * release their reference.
25506ad42b2SJohn Baldwin 	 */
25606ad42b2SJohn Baldwin 	while (p->p_lock > 0)
25706ad42b2SJohn Baldwin 		msleep(&p->p_lock, &p->p_mtx, PWAIT, "exithold", 0);
258ebceaf6dSDavid Xu 
259bb63fddeSAlexander Leidinger 	p->p_xstat = rv;	/* Let event handler change exit status */
260e602ba25SJulian Elischer 	PROC_UNLOCK(p);
2611c4bcd05SJeff Roberson 	/* Drain the limit callout while we don't have the proc locked */
2621c4bcd05SJeff Roberson 	callout_drain(&p->p_limco);
263b40ce416SJulian Elischer 
26400c28d96SRobert Watson #ifdef AUDIT
26500c28d96SRobert Watson 	/*
26600c28d96SRobert Watson 	 * The Sun BSM exit token contains two components: an exit status as
26700c28d96SRobert Watson 	 * passed to exit(), and a return value to indicate what sort of exit
26800c28d96SRobert Watson 	 * it was.  The exit status is WEXITSTATUS(rv), but it's not clear
26900c28d96SRobert Watson 	 * what the return value is.
27000c28d96SRobert Watson 	 */
27114961ba7SRobert Watson 	AUDIT_ARG_EXIT(WEXITSTATUS(rv), 0);
27200c28d96SRobert Watson 	AUDIT_SYSCALL_EXIT(0, td);
27300c28d96SRobert Watson #endif
27400c28d96SRobert Watson 
275bad2520aSMateusz Guzik 	/* Are we a task leader with peers? */
276bad2520aSMateusz Guzik 	if (p->p_peers != NULL && p == p->p_leader) {
277c6544064SJohn Baldwin 		mtx_lock(&ppeers_lock);
2782c1011f7SJohn Dyson 		q = p->p_peers;
279776e0b36SJohn Baldwin 		while (q != NULL) {
280776e0b36SJohn Baldwin 			PROC_LOCK(q);
2818451d0ddSKip Macy 			kern_psignal(q, SIGKILL);
282776e0b36SJohn Baldwin 			PROC_UNLOCK(q);
2832c1011f7SJohn Dyson 			q = q->p_peers;
2842c1011f7SJohn Dyson 		}
285c6544064SJohn Baldwin 		while (p->p_peers != NULL)
286c6544064SJohn Baldwin 			msleep(p, &ppeers_lock, PWAIT, "exit1", 0);
287c6544064SJohn Baldwin 		mtx_unlock(&ppeers_lock);
2882c1011f7SJohn Dyson 	}
2892c1011f7SJohn Dyson 
290fed06968SJulian Elischer 	/*
291e9189611SPeter Wemm 	 * Check if any loadable modules need anything done at process exit.
2926111dcd2SJohn Baldwin 	 * E.g. SYSV IPC stuff
293fed06968SJulian Elischer 	 * XXX what if one of these generates an error?
294fed06968SJulian Elischer 	 */
29575b8b3b2SJohn Baldwin 	EVENTHANDLER_INVOKE(process_exit, p);
296a914fb6bSJohn Baldwin 
297df8bae1dSRodney W. Grimes 	/*
298df8bae1dSRodney W. Grimes 	 * If parent is waiting for us to exit or exec,
299df8bae1dSRodney W. Grimes 	 * P_PPWAIT is set; we will wakeup the parent below.
300df8bae1dSRodney W. Grimes 	 */
301a914fb6bSJohn Baldwin 	PROC_LOCK(p);
302bb63fddeSAlexander Leidinger 	rv = p->p_xstat;	/* Event handler could change exit status */
303a282253aSJulian Elischer 	stopprofclock(p);
304888d4d4fSKonstantin Belousov 	p->p_flag &= ~(P_TRACED | P_PPWAIT | P_PPTRACE);
3055499ea01SJohn Baldwin 
3065499ea01SJohn Baldwin 	/*
3075499ea01SJohn Baldwin 	 * Stop the real interval timer.  If the handler is currently
3085499ea01SJohn Baldwin 	 * executing, prevent it from rearming itself and let it finish.
3095499ea01SJohn Baldwin 	 */
3105499ea01SJohn Baldwin 	if (timevalisset(&p->p_realtimer.it_value) &&
3115499ea01SJohn Baldwin 	    callout_stop(&p->p_itcallout) == 0) {
3125499ea01SJohn Baldwin 		timevalclear(&p->p_realtimer.it_interval);
3135499ea01SJohn Baldwin 		msleep(&p->p_itcallout, &p->p_mtx, PWAIT, "ritwait", 0);
3145499ea01SJohn Baldwin 		KASSERT(!timevalisset(&p->p_realtimer.it_value),
3155499ea01SJohn Baldwin 		    ("realtime timer is still armed"));
3165499ea01SJohn Baldwin 	}
31796d7f8efSTim J. Robbins 	PROC_UNLOCK(p);
318df8bae1dSRodney W. Grimes 
319df8bae1dSRodney W. Grimes 	/*
320831d27a9SDon Lewis 	 * Reset any sigio structures pointing to us as a result of
321831d27a9SDon Lewis 	 * F_SETOWN with our pid.
322831d27a9SDon Lewis 	 */
323831d27a9SDon Lewis 	funsetownlst(&p->p_sigiolst);
324831d27a9SDon Lewis 
325831d27a9SDon Lewis 	/*
326c0bc2867SGleb Smirnoff 	 * If this process has an nlminfo data area (for lockd), release it
327c0bc2867SGleb Smirnoff 	 */
328c0bc2867SGleb Smirnoff 	if (nlminfo_release_p != NULL && p->p_nlminfo != NULL)
329c0bc2867SGleb Smirnoff 		(*nlminfo_release_p)(p);
330c0bc2867SGleb Smirnoff 
331c0bc2867SGleb Smirnoff 	/*
332df8bae1dSRodney W. Grimes 	 * Close open files and release open-file table.
333df8bae1dSRodney W. Grimes 	 * This may block!
334df8bae1dSRodney W. Grimes 	 */
3352609222aSPawel Jakub Dawidek 	fdescfree(td);
336df8bae1dSRodney W. Grimes 
337a914fb6bSJohn Baldwin 	/*
338a2587073SPoul-Henning Kamp 	 * If this thread tickled GEOM, we need to wait for the giggling to
339a2587073SPoul-Henning Kamp 	 * stop before we return to userland
340a2587073SPoul-Henning Kamp 	 */
341a2587073SPoul-Henning Kamp 	if (td->td_pflags & TDP_GEOM)
342a2587073SPoul-Henning Kamp 		g_waitidle();
343a2587073SPoul-Henning Kamp 
344a2587073SPoul-Henning Kamp 	/*
345a914fb6bSJohn Baldwin 	 * Remove ourself from our leader's peer list and wake our leader.
346a914fb6bSJohn Baldwin 	 */
347bad2520aSMateusz Guzik 	if (p->p_leader->p_peers != NULL) {
348c6544064SJohn Baldwin 		mtx_lock(&ppeers_lock);
349bad2520aSMateusz Guzik 		if (p->p_leader->p_peers != NULL) {
35079fc0bf4SMike Smith 			q = p->p_leader;
35179fc0bf4SMike Smith 			while (q->p_peers != p)
35279fc0bf4SMike Smith 				q = q->p_peers;
35379fc0bf4SMike Smith 			q->p_peers = p->p_peers;
3547f05b035SAlfred Perlstein 			wakeup(p->p_leader);
35579fc0bf4SMike Smith 		}
356c6544064SJohn Baldwin 		mtx_unlock(&ppeers_lock);
357bad2520aSMateusz Guzik 	}
35879fc0bf4SMike Smith 
35957051fdcSTor Egge 	vmspace_exit(td);
360df8bae1dSRodney W. Grimes 
361ea97757aSJohn Baldwin 	sx_xlock(&proctree_lock);
362df8bae1dSRodney W. Grimes 	if (SESS_LEADER(p)) {
363eaaaf190SEd Schouten 		struct session *sp = p->p_session;
364eaaaf190SEd Schouten 		struct tty *tp;
365df8bae1dSRodney W. Grimes 
366eaaaf190SEd Schouten 		/*
367eaaaf190SEd Schouten 		 * s_ttyp is not zero'd; we use this to indicate that
368eaaaf190SEd Schouten 		 * the session once had a controlling terminal. (for
369eaaaf190SEd Schouten 		 * logging and informational purposes)
370eaaaf190SEd Schouten 		 */
371bc093719SEd Schouten 		SESS_LOCK(sp);
372bc093719SEd Schouten 		ttyvp = sp->s_ttyvp;
373eaaaf190SEd Schouten 		tp = sp->s_ttyp;
374bc093719SEd Schouten 		sp->s_ttyvp = NULL;
3758dc9b4cfSEd Schouten 		sp->s_ttydp = NULL;
376eaaaf190SEd Schouten 		sp->s_leader = NULL;
377bc093719SEd Schouten 		SESS_UNLOCK(sp);
378bc093719SEd Schouten 
379df8bae1dSRodney W. Grimes 		/*
380bc093719SEd Schouten 		 * Signal foreground pgrp and revoke access to
381eaaaf190SEd Schouten 		 * controlling terminal if it has not been revoked
382eaaaf190SEd Schouten 		 * already.
383bc093719SEd Schouten 		 *
384eaaaf190SEd Schouten 		 * Because the TTY may have been revoked in the mean
385eaaaf190SEd Schouten 		 * time and could already have a new session associated
386eaaaf190SEd Schouten 		 * with it, make sure we don't send a SIGHUP to a
387eaaaf190SEd Schouten 		 * foreground process group that does not belong to this
388eaaaf190SEd Schouten 		 * session.
389df8bae1dSRodney W. Grimes 		 */
390bc093719SEd Schouten 
391eaaaf190SEd Schouten 		if (tp != NULL) {
392bc093719SEd Schouten 			tty_lock(tp);
393eaaaf190SEd Schouten 			if (tp->t_session == sp)
394bc093719SEd Schouten 				tty_signal_pgrp(tp, SIGHUP);
395bc093719SEd Schouten 			tty_unlock(tp);
396eaaaf190SEd Schouten 		}
397bc093719SEd Schouten 
3987c6d401cSKonstantin Belousov 		if (ttyvp != NULL) {
399ea97757aSJohn Baldwin 			sx_xunlock(&proctree_lock);
4006728dc16SKonstantin Belousov 			if (vn_lock(ttyvp, LK_EXCLUSIVE) == 0) {
401f591779bSSeigo Tanimura 				VOP_REVOKE(ttyvp, REVOKEALL);
402bc093719SEd Schouten 				VOP_UNLOCK(ttyvp, 0);
4036728dc16SKonstantin Belousov 			}
404ea97757aSJohn Baldwin 			sx_xlock(&proctree_lock);
405f591779bSSeigo Tanimura 		}
406f591779bSSeigo Tanimura 	}
407df8bae1dSRodney W. Grimes 	fixjobc(p, p->p_pgrp, 0);
408ea97757aSJohn Baldwin 	sx_xunlock(&proctree_lock);
409b40ce416SJulian Elischer 	(void)acct_process(td);
410bc093719SEd Schouten 
411bc093719SEd Schouten 	/* Release the TTY now we've unlocked everything. */
412bc093719SEd Schouten 	if (ttyvp != NULL)
413bc093719SEd Schouten 		vrele(ttyvp);
414df8bae1dSRodney W. Grimes #ifdef KTRACE
4152c255e9dSRobert Watson 	ktrprocexit(td);
416df8bae1dSRodney W. Grimes #endif
417df8bae1dSRodney W. Grimes 	/*
418ee42d0a9SDavid Malone 	 * Release reference to text vnode
419ee42d0a9SDavid Malone 	 */
42088f98985SMateusz Guzik 	if (p->p_textvp != NULL) {
42188f98985SMateusz Guzik 		vrele(p->p_textvp);
422ee42d0a9SDavid Malone 		p->p_textvp = NULL;
423ee42d0a9SDavid Malone 	}
424ee42d0a9SDavid Malone 
425ee42d0a9SDavid Malone 	/*
426d7aadbf9SJohn Baldwin 	 * Release our limits structure.
427d7aadbf9SJohn Baldwin 	 */
42888f98985SMateusz Guzik 	lim_free(p->p_limit);
429d7aadbf9SJohn Baldwin 	p->p_limit = NULL;
430d7aadbf9SJohn Baldwin 
431cf7d9a8cSDavid Xu 	tidhash_remove(td);
432cf7d9a8cSDavid Xu 
433d7aadbf9SJohn Baldwin 	/*
434df8bae1dSRodney W. Grimes 	 * Remove proc from allproc queue and pidhash chain.
435df8bae1dSRodney W. Grimes 	 * Place onto zombproc.  Unlink from parent's child list.
436df8bae1dSRodney W. Grimes 	 */
4371005a129SJohn Baldwin 	sx_xlock(&allproc_lock);
438b75356e1SJeffrey Hsu 	LIST_REMOVE(p, p_list);
439b75356e1SJeffrey Hsu 	LIST_INSERT_HEAD(&zombproc, p, p_list);
440b75356e1SJeffrey Hsu 	LIST_REMOVE(p, p_hash);
4411005a129SJohn Baldwin 	sx_xunlock(&allproc_lock);
442df8bae1dSRodney W. Grimes 
44337f84a60SJohn Baldwin 	/*
4441bba2a94SJohn Baldwin 	 * Call machine-dependent code to release any
4451bba2a94SJohn Baldwin 	 * machine-dependent resources other than the address space.
4461bba2a94SJohn Baldwin 	 * The address space is released by "vmspace_exitfree(p)" in
4471bba2a94SJohn Baldwin 	 * vm_waitproc().
4481bba2a94SJohn Baldwin 	 */
4491bba2a94SJohn Baldwin 	cpu_exit(td);
4501bba2a94SJohn Baldwin 
4511bba2a94SJohn Baldwin 	WITNESS_WARN(WARN_PANIC, NULL, "process (pid %d) exiting", p->p_pid);
4521bba2a94SJohn Baldwin 
4531bba2a94SJohn Baldwin 	/*
454a661bebeSMateusz Guzik 	 * Reparent all children processes:
455a661bebeSMateusz Guzik 	 * - traced ones to the original parent (or init if we are that parent)
456a661bebeSMateusz Guzik 	 * - the rest to init
45737f84a60SJohn Baldwin 	 */
4581005a129SJohn Baldwin 	sx_xlock(&proctree_lock);
4592e3c8fcbSPoul-Henning Kamp 	q = LIST_FIRST(&p->p_children);
460a914fb6bSJohn Baldwin 	if (q != NULL)		/* only need this if any child is S_ZOMB */
4617f05b035SAlfred Perlstein 		wakeup(initproc);
462a914fb6bSJohn Baldwin 	for (; q != NULL; q = nq) {
4632e3c8fcbSPoul-Henning Kamp 		nq = LIST_NEXT(q, p_sibling);
464a914fb6bSJohn Baldwin 		PROC_LOCK(q);
4654ac9ae70SJulian Elischer 		q->p_sigparent = SIGCHLD;
466904c5ec4SDavid Xu 
467a661bebeSMateusz Guzik 		if (!(q->p_flag & P_TRACED)) {
468a661bebeSMateusz Guzik 			proc_reparent(q, initproc);
469a661bebeSMateusz Guzik 		} else {
470a661bebeSMateusz Guzik 			/*
471a661bebeSMateusz Guzik 			 * Traced processes are killed since their existence
472a661bebeSMateusz Guzik 			 * means someone is screwing up.
473a661bebeSMateusz Guzik 			 */
474a661bebeSMateusz Guzik 			t = proc_realparent(q);
475a661bebeSMateusz Guzik 			if (t == p) {
476a661bebeSMateusz Guzik 				proc_reparent(q, initproc);
477a661bebeSMateusz Guzik 			} else {
478a661bebeSMateusz Guzik 				PROC_LOCK(t);
479a661bebeSMateusz Guzik 				proc_reparent(q, t);
480a661bebeSMateusz Guzik 				PROC_UNLOCK(t);
481a661bebeSMateusz Guzik 			}
4825085ecb7SKonstantin Belousov 			/*
4835085ecb7SKonstantin Belousov 			 * Since q was found on our children list, the
4845085ecb7SKonstantin Belousov 			 * proc_reparent() call moved q to the orphan
4855085ecb7SKonstantin Belousov 			 * list due to present P_TRACED flag. Clear
4865085ecb7SKonstantin Belousov 			 * orphan link for q now while q is locked.
4875085ecb7SKonstantin Belousov 			 */
4885085ecb7SKonstantin Belousov 			clear_orphan(q);
489c2836532SDavid Xu 			q->p_flag &= ~(P_TRACED | P_STOPPED_TRACE);
490a661bebeSMateusz Guzik 			FOREACH_THREAD_IN_PROC(q, tdt)
491a661bebeSMateusz Guzik 				tdt->td_dbgflags &= ~TDB_SUSPEND;
4928451d0ddSKip Macy 			kern_psignal(q, SIGKILL);
493c65437a3SJohn Baldwin 		}
494a914fb6bSJohn Baldwin 		PROC_UNLOCK(q);
495df8bae1dSRodney W. Grimes 	}
496df8bae1dSRodney W. Grimes 
4975085ecb7SKonstantin Belousov 	/*
4985085ecb7SKonstantin Belousov 	 * Also get rid of our orphans.
4995085ecb7SKonstantin Belousov 	 */
5005085ecb7SKonstantin Belousov 	while ((q = LIST_FIRST(&p->p_orphans)) != NULL) {
5015085ecb7SKonstantin Belousov 		PROC_LOCK(q);
5025085ecb7SKonstantin Belousov 		clear_orphan(q);
5035085ecb7SKonstantin Belousov 		PROC_UNLOCK(q);
5045085ecb7SKonstantin Belousov 	}
5055085ecb7SKonstantin Belousov 
5061c4bcd05SJeff Roberson 	/* Save exit status. */
507d7aadbf9SJohn Baldwin 	PROC_LOCK(p);
508cbf4e354SDavid Xu 	p->p_xthread = td;
5095d217f17SJohn Birrell 
5100304c731SJamie Gritton 	/* Tell the prison that we are gone. */
511413628a7SBjoern A. Zeeb 	prison_proc_free(p->p_ucred->cr_prison);
512413628a7SBjoern A. Zeeb 
5135d217f17SJohn Birrell #ifdef KDTRACE_HOOKS
5145d217f17SJohn Birrell 	/*
5155d217f17SJohn Birrell 	 * Tell the DTrace fasttrap provider about the exit if it
5165d217f17SJohn Birrell 	 * has declared an interest.
5175d217f17SJohn Birrell 	 */
5185d217f17SJohn Birrell 	if (dtrace_fasttrap_exit)
5195d217f17SJohn Birrell 		dtrace_fasttrap_exit(p);
5205d217f17SJohn Birrell #endif
5215d217f17SJohn Birrell 
5221c4bcd05SJeff Roberson 	/*
5237a6b989bSJohn Baldwin 	 * Notify interested parties of our demise.
524cb679c38SJonathan Lemon 	 */
525ad3b9257SJohn-Mark Gurney 	KNOTE_LOCKED(&p->p_klist, NOTE_EXIT);
5267eaec467SJohn Baldwin 
5275d217f17SJohn Birrell #ifdef KDTRACE_HOOKS
5285d217f17SJohn Birrell 	int reason = CLD_EXITED;
5295d217f17SJohn Birrell 	if (WCOREDUMP(rv))
5305d217f17SJohn Birrell 		reason = CLD_DUMPED;
5315d217f17SJohn Birrell 	else if (WIFSIGNALED(rv))
5325d217f17SJohn Birrell 		reason = CLD_KILLED;
5335d217f17SJohn Birrell 	SDT_PROBE(proc, kernel, , exit, reason, 0, 0, 0, 0);
5345d217f17SJohn Birrell #endif
5355d217f17SJohn Birrell 
5361a29c806SOlivier Houchard 	/*
5371a29c806SOlivier Houchard 	 * Just delete all entries in the p_klist. At this point we won't
5381a29c806SOlivier Houchard 	 * report any more events, and there are nasty race conditions that
5391a29c806SOlivier Houchard 	 * can beat us if we don't.
5401a29c806SOlivier Houchard 	 */
541ad3b9257SJohn-Mark Gurney 	knlist_clear(&p->p_klist, 1);
542cb679c38SJonathan Lemon 
543cb679c38SJonathan Lemon 	/*
544cfb5f768SJonathan Anderson 	 * If this is a process with a descriptor, we may not need to deliver
545cfb5f768SJonathan Anderson 	 * a signal to the parent.  proctree_lock is held over
546cfb5f768SJonathan Anderson 	 * procdesc_exit() to serialize concurrent calls to close() and
547cfb5f768SJonathan Anderson 	 * exit().
548cfb5f768SJonathan Anderson 	 */
549cfb5f768SJonathan Anderson 	if (p->p_procdesc == NULL || procdesc_exit(p)) {
550cfb5f768SJonathan Anderson 		/*
551cfb5f768SJonathan Anderson 		 * Notify parent that we're gone.  If parent has the
552cfb5f768SJonathan Anderson 		 * PS_NOCLDWAIT flag set, or if the handler is set to SIG_IGN,
553cfb5f768SJonathan Anderson 		 * notify process 1 instead (and hope it will handle this
554cfb5f768SJonathan Anderson 		 * situation).
555df8bae1dSRodney W. Grimes 		 */
556d7aadbf9SJohn Baldwin 		PROC_LOCK(p->p_pptr);
55790af4afaSJohn Baldwin 		mtx_lock(&p->p_pptr->p_sigacts->ps_mtx);
558cfb5f768SJonathan Anderson 		if (p->p_pptr->p_sigacts->ps_flag &
559cfb5f768SJonathan Anderson 		    (PS_NOCLDWAIT | PS_CLDSIGIGN)) {
560276c5169SJohn Baldwin 			struct proc *pp;
561276c5169SJohn Baldwin 
56290af4afaSJohn Baldwin 			mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx);
563276c5169SJohn Baldwin 			pp = p->p_pptr;
564f591779bSSeigo Tanimura 			PROC_UNLOCK(pp);
565245f17d4SJoerg Wunsch 			proc_reparent(p, initproc);
56655b5f2a2SDon Lewis 			p->p_sigparent = SIGCHLD;
567f591779bSSeigo Tanimura 			PROC_LOCK(p->p_pptr);
568007abb3dSKonstantin Belousov 
569245f17d4SJoerg Wunsch 			/*
570007abb3dSKonstantin Belousov 			 * Notify parent, so in case he was wait(2)ing or
5716fae832aSKonstantin Belousov 			 * executing waitpid(2) with our pid, he will
572245f17d4SJoerg Wunsch 			 * continue.
573245f17d4SJoerg Wunsch 			 */
5747f05b035SAlfred Perlstein 			wakeup(pp);
57590af4afaSJohn Baldwin 		} else
57690af4afaSJohn Baldwin 			mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx);
577245f17d4SJoerg Wunsch 
5786567eef7SDon Lewis 		if (p->p_pptr == initproc)
5798451d0ddSKip Macy 			kern_psignal(p->p_pptr, SIGCHLD);
580ebceaf6dSDavid Xu 		else if (p->p_sigparent != 0) {
581ebceaf6dSDavid Xu 			if (p->p_sigparent == SIGCHLD)
582ebceaf6dSDavid Xu 				childproc_exited(p);
583ebceaf6dSDavid Xu 			else	/* LINUX thread */
5848451d0ddSKip Macy 				kern_psignal(p->p_pptr, p->p_sigparent);
585ebceaf6dSDavid Xu 		}
586cfb5f768SJonathan Anderson 	} else
587cfb5f768SJonathan Anderson 		PROC_LOCK(p->p_pptr);
588d7aadbf9SJohn Baldwin 	sx_xunlock(&proctree_lock);
589696058c3SJulian Elischer 
590eb30c1c0SPeter Wemm 	/*
591f71e748dSDavid Xu 	 * The state PRS_ZOMBIE prevents other proesses from sending
592f71e748dSDavid Xu 	 * signal to the process, to avoid memory leak, we free memory
593f71e748dSDavid Xu 	 * for signal queue at the time when the state is set.
594f71e748dSDavid Xu 	 */
595f71e748dSDavid Xu 	sigqueue_flush(&p->p_sigqueue);
596f71e748dSDavid Xu 	sigqueue_flush(&td->td_sigqueue);
597f71e748dSDavid Xu 
598f71e748dSDavid Xu 	/*
599462f31bfSJohn Baldwin 	 * We have to wait until after acquiring all locks before
600a9a64385SJohn Baldwin 	 * changing p_state.  We need to avoid all possible context
601a9a64385SJohn Baldwin 	 * switches (including ones from blocking on a mutex) while
602ddf9c4f7SJohn Baldwin 	 * marked as a zombie.  We also have to set the zombie state
603ddf9c4f7SJohn Baldwin 	 * before we release the parent process' proc lock to avoid
604ddf9c4f7SJohn Baldwin 	 * a lost wakeup.  So, we first call wakeup, then we grab the
605ddf9c4f7SJohn Baldwin 	 * sched lock, update the state, and release the parent process'
606ddf9c4f7SJohn Baldwin 	 * proc lock.
607eb30c1c0SPeter Wemm 	 */
608ddf9c4f7SJohn Baldwin 	wakeup(p->p_pptr);
609aeb32571SKonstantin Belousov 	cv_broadcast(&p->p_pwait);
610982d11f8SJeff Roberson 	sched_exit(p->p_pptr, td);
611982d11f8SJeff Roberson 	PROC_SLOCK(p);
612e602ba25SJulian Elischer 	p->p_state = PRS_ZOMBIE;
613d7aadbf9SJohn Baldwin 	PROC_UNLOCK(p->p_pptr);
614871684b8SBruce Evans 
615f6f230feSJeff Roberson 	/*
6167eaec467SJohn Baldwin 	 * Hopefully no one will try to deliver a signal to the process this
617ad3b9257SJohn-Mark Gurney 	 * late in the game.
618ad3b9257SJohn-Mark Gurney 	 */
619ad3b9257SJohn-Mark Gurney 	knlist_destroy(&p->p_klist);
620ad3b9257SJohn-Mark Gurney 
621ad3b9257SJohn-Mark Gurney 	/*
622a140976eSAttilio Rao 	 * Save our children's rusage information in our exit rusage.
623a140976eSAttilio Rao 	 */
6245c7bebf9SKonstantin Belousov 	PROC_STATLOCK(p);
625a140976eSAttilio Rao 	ruadd(&p->p_ru, &p->p_rux, &p->p_stats->p_cru, &p->p_crux);
6265c7bebf9SKonstantin Belousov 	PROC_STATUNLOCK(p);
627a140976eSAttilio Rao 
628a140976eSAttilio Rao 	/*
629696058c3SJulian Elischer 	 * Make sure the scheduler takes this thread out of its tables etc.
630e602ba25SJulian Elischer 	 * This will also release this thread's reference to the ucred.
631696058c3SJulian Elischer 	 * Other thread parts to release include pcb bits and such.
632e602ba25SJulian Elischer 	 */
633e602ba25SJulian Elischer 	thread_exit();
634df8bae1dSRodney W. Grimes }
635df8bae1dSRodney W. Grimes 
63625f6e35aSPoul-Henning Kamp 
63725f6e35aSPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_
63825f6e35aSPoul-Henning Kamp struct abort2_args {
63925f6e35aSPoul-Henning Kamp 	char *why;
64025f6e35aSPoul-Henning Kamp 	int nargs;
64125f6e35aSPoul-Henning Kamp 	void **args;
64225f6e35aSPoul-Henning Kamp };
64325f6e35aSPoul-Henning Kamp #endif
64425f6e35aSPoul-Henning Kamp 
64525f6e35aSPoul-Henning Kamp int
6468451d0ddSKip Macy sys_abort2(struct thread *td, struct abort2_args *uap)
64725f6e35aSPoul-Henning Kamp {
64825f6e35aSPoul-Henning Kamp 	struct proc *p = td->td_proc;
64925f6e35aSPoul-Henning Kamp 	struct sbuf *sb;
65025f6e35aSPoul-Henning Kamp 	void *uargs[16];
65125f6e35aSPoul-Henning Kamp 	int error, i, sig;
65225f6e35aSPoul-Henning Kamp 
65325f6e35aSPoul-Henning Kamp 	/*
65425f6e35aSPoul-Henning Kamp 	 * Do it right now so we can log either proper call of abort2(), or
65525f6e35aSPoul-Henning Kamp 	 * note, that invalid argument was passed. 512 is big enough to
65625f6e35aSPoul-Henning Kamp 	 * handle 16 arguments' descriptions with additional comments.
65725f6e35aSPoul-Henning Kamp 	 */
65825f6e35aSPoul-Henning Kamp 	sb = sbuf_new(NULL, NULL, 512, SBUF_FIXEDLEN);
65925f6e35aSPoul-Henning Kamp 	sbuf_clear(sb);
66025f6e35aSPoul-Henning Kamp 	sbuf_printf(sb, "%s(pid %d uid %d) aborted: ",
66125f6e35aSPoul-Henning Kamp 	    p->p_comm, p->p_pid, td->td_ucred->cr_uid);
66225f6e35aSPoul-Henning Kamp 	/*
66325f6e35aSPoul-Henning Kamp 	 * Since we can't return from abort2(), send SIGKILL in cases, where
66425f6e35aSPoul-Henning Kamp 	 * abort2() was called improperly
66525f6e35aSPoul-Henning Kamp 	 */
66625f6e35aSPoul-Henning Kamp 	sig = SIGKILL;
66725f6e35aSPoul-Henning Kamp 	/* Prevent from DoSes from user-space. */
66825f6e35aSPoul-Henning Kamp 	if (uap->nargs < 0 || uap->nargs > 16)
66925f6e35aSPoul-Henning Kamp 		goto out;
6704218a731SPoul-Henning Kamp 	if (uap->nargs > 0) {
67125f6e35aSPoul-Henning Kamp 		if (uap->args == NULL)
67225f6e35aSPoul-Henning Kamp 			goto out;
67325f6e35aSPoul-Henning Kamp 		error = copyin(uap->args, uargs, uap->nargs * sizeof(void *));
67425f6e35aSPoul-Henning Kamp 		if (error != 0)
67525f6e35aSPoul-Henning Kamp 			goto out;
6764218a731SPoul-Henning Kamp 	}
67725f6e35aSPoul-Henning Kamp 	/*
67825f6e35aSPoul-Henning Kamp 	 * Limit size of 'reason' string to 128. Will fit even when
67925f6e35aSPoul-Henning Kamp 	 * maximal number of arguments was chosen to be logged.
68025f6e35aSPoul-Henning Kamp 	 */
68125f6e35aSPoul-Henning Kamp 	if (uap->why != NULL) {
68225f6e35aSPoul-Henning Kamp 		error = sbuf_copyin(sb, uap->why, 128);
68325f6e35aSPoul-Henning Kamp 		if (error < 0)
68425f6e35aSPoul-Henning Kamp 			goto out;
68525f6e35aSPoul-Henning Kamp 	} else {
68625f6e35aSPoul-Henning Kamp 		sbuf_printf(sb, "(null)");
68725f6e35aSPoul-Henning Kamp 	}
6884218a731SPoul-Henning Kamp 	if (uap->nargs > 0) {
68925f6e35aSPoul-Henning Kamp 		sbuf_printf(sb, "(");
69025f6e35aSPoul-Henning Kamp 		for (i = 0;i < uap->nargs; i++)
69125f6e35aSPoul-Henning Kamp 			sbuf_printf(sb, "%s%p", i == 0 ? "" : ", ", uargs[i]);
69225f6e35aSPoul-Henning Kamp 		sbuf_printf(sb, ")");
69325f6e35aSPoul-Henning Kamp 	}
69425f6e35aSPoul-Henning Kamp 	/*
69525f6e35aSPoul-Henning Kamp 	 * Final stage: arguments were proper, string has been
69625f6e35aSPoul-Henning Kamp 	 * successfully copied from userspace, and copying pointers
69725f6e35aSPoul-Henning Kamp 	 * from user-space succeed.
69825f6e35aSPoul-Henning Kamp 	 */
69925f6e35aSPoul-Henning Kamp 	sig = SIGABRT;
70025f6e35aSPoul-Henning Kamp out:
70125f6e35aSPoul-Henning Kamp 	if (sig == SIGKILL) {
70225f6e35aSPoul-Henning Kamp 		sbuf_trim(sb);
70325f6e35aSPoul-Henning Kamp 		sbuf_printf(sb, " (Reason text inaccessible)");
70425f6e35aSPoul-Henning Kamp 	}
70525f6e35aSPoul-Henning Kamp 	sbuf_cat(sb, "\n");
70625f6e35aSPoul-Henning Kamp 	sbuf_finish(sb);
70725f6e35aSPoul-Henning Kamp 	log(LOG_INFO, "%s", sbuf_data(sb));
70825f6e35aSPoul-Henning Kamp 	sbuf_delete(sb);
70925f6e35aSPoul-Henning Kamp 	exit1(td, W_EXITCODE(0, sig));
71025f6e35aSPoul-Henning Kamp 	return (0);
71125f6e35aSPoul-Henning Kamp }
71225f6e35aSPoul-Henning Kamp 
71325f6e35aSPoul-Henning Kamp 
714b2f9e8b1SBruce Evans #ifdef COMPAT_43
715234216efSMatthew Dillon /*
716a9a64385SJohn Baldwin  * The dirty work is handled by kern_wait().
717234216efSMatthew Dillon  */
71826f9a767SRodney W. Grimes int
719830c3153SDag-Erling Smørgrav owait(struct thread *td, struct owait_args *uap __unused)
720df8bae1dSRodney W. Grimes {
721b7e23e82SJohn Baldwin 	int error, status;
722df8bae1dSRodney W. Grimes 
723b7e23e82SJohn Baldwin 	error = kern_wait(td, WAIT_ANY, &status, 0, NULL);
724b7e23e82SJohn Baldwin 	if (error == 0)
725b7e23e82SJohn Baldwin 		td->td_retval[1] = status;
726b7e23e82SJohn Baldwin 	return (error);
727df8bae1dSRodney W. Grimes }
728b2f9e8b1SBruce Evans #endif /* COMPAT_43 */
729df8bae1dSRodney W. Grimes 
730234216efSMatthew Dillon /*
731a9a64385SJohn Baldwin  * The dirty work is handled by kern_wait().
732234216efSMatthew Dillon  */
73326f9a767SRodney W. Grimes int
734f13b5a0fSKonstantin Belousov sys_wait4(struct thread *td, struct wait4_args *uap)
735df8bae1dSRodney W. Grimes {
73678c85e8dSJohn Baldwin 	struct rusage ru, *rup;
737b7e23e82SJohn Baldwin 	int error, status;
738b40ce416SJulian Elischer 
73978c85e8dSJohn Baldwin 	if (uap->rusage != NULL)
74078c85e8dSJohn Baldwin 		rup = &ru;
74178c85e8dSJohn Baldwin 	else
74278c85e8dSJohn Baldwin 		rup = NULL;
74378c85e8dSJohn Baldwin 	error = kern_wait(td, uap->pid, &status, uap->options, rup);
744b7e23e82SJohn Baldwin 	if (uap->status != NULL && error == 0)
745b7e23e82SJohn Baldwin 		error = copyout(&status, uap->status, sizeof(status));
746b7e23e82SJohn Baldwin 	if (uap->rusage != NULL && error == 0)
747b7e23e82SJohn Baldwin 		error = copyout(&ru, uap->rusage, sizeof(struct rusage));
748b7e23e82SJohn Baldwin 	return (error);
749df8bae1dSRodney W. Grimes }
750df8bae1dSRodney W. Grimes 
751f13b5a0fSKonstantin Belousov int
752f13b5a0fSKonstantin Belousov sys_wait6(struct thread *td, struct wait6_args *uap)
753f13b5a0fSKonstantin Belousov {
754f13b5a0fSKonstantin Belousov 	struct __wrusage wru, *wrup;
755f13b5a0fSKonstantin Belousov 	siginfo_t si, *sip;
756f13b5a0fSKonstantin Belousov 	idtype_t idtype;
757f13b5a0fSKonstantin Belousov 	id_t id;
758a2a85596SKonstantin Belousov 	int error, status;
759f13b5a0fSKonstantin Belousov 
760f13b5a0fSKonstantin Belousov 	idtype = uap->idtype;
761f13b5a0fSKonstantin Belousov 	id = uap->id;
762f13b5a0fSKonstantin Belousov 
763f13b5a0fSKonstantin Belousov 	if (uap->wrusage != NULL)
764f13b5a0fSKonstantin Belousov 		wrup = &wru;
765f13b5a0fSKonstantin Belousov 	else
766f13b5a0fSKonstantin Belousov 		wrup = NULL;
767f13b5a0fSKonstantin Belousov 
768f13b5a0fSKonstantin Belousov 	if (uap->info != NULL) {
769f13b5a0fSKonstantin Belousov 		sip = &si;
770f13b5a0fSKonstantin Belousov 		bzero(sip, sizeof(*sip));
771f13b5a0fSKonstantin Belousov 	} else
772f13b5a0fSKonstantin Belousov 		sip = NULL;
773f13b5a0fSKonstantin Belousov 
774f13b5a0fSKonstantin Belousov 	/*
775f13b5a0fSKonstantin Belousov 	 *  We expect all callers of wait6() to know about WEXITED and
776f13b5a0fSKonstantin Belousov 	 *  WTRAPPED.
777f13b5a0fSKonstantin Belousov 	 */
778f13b5a0fSKonstantin Belousov 	error = kern_wait6(td, idtype, id, &status, uap->options, wrup, sip);
779f13b5a0fSKonstantin Belousov 
780f13b5a0fSKonstantin Belousov 	if (uap->status != NULL && error == 0)
781f13b5a0fSKonstantin Belousov 		error = copyout(&status, uap->status, sizeof(status));
782f13b5a0fSKonstantin Belousov 	if (uap->wrusage != NULL && error == 0)
783f13b5a0fSKonstantin Belousov 		error = copyout(&wru, uap->wrusage, sizeof(wru));
784f13b5a0fSKonstantin Belousov 	if (uap->info != NULL && error == 0)
785f13b5a0fSKonstantin Belousov 		error = copyout(&si, uap->info, sizeof(si));
786f13b5a0fSKonstantin Belousov 	return (error);
787f13b5a0fSKonstantin Belousov }
788f13b5a0fSKonstantin Belousov 
789324fb6beSRobert Watson /*
790324fb6beSRobert Watson  * Reap the remains of a zombie process and optionally return status and
791324fb6beSRobert Watson  * rusage.  Asserts and will release both the proctree_lock and the process
792324fb6beSRobert Watson  * lock as part of its work.
793324fb6beSRobert Watson  */
794cfb5f768SJonathan Anderson void
795f13b5a0fSKonstantin Belousov proc_reap(struct thread *td, struct proc *p, int *status, int options)
796324fb6beSRobert Watson {
797324fb6beSRobert Watson 	struct proc *q, *t;
798324fb6beSRobert Watson 
799324fb6beSRobert Watson 	sx_assert(&proctree_lock, SA_XLOCKED);
800324fb6beSRobert Watson 	PROC_LOCK_ASSERT(p, MA_OWNED);
801324fb6beSRobert Watson 	PROC_SLOCK_ASSERT(p, MA_OWNED);
802324fb6beSRobert Watson 	KASSERT(p->p_state == PRS_ZOMBIE, ("proc_reap: !PRS_ZOMBIE"));
803324fb6beSRobert Watson 
804324fb6beSRobert Watson 	q = td->td_proc;
805f13b5a0fSKonstantin Belousov 
806324fb6beSRobert Watson 	PROC_SUNLOCK(p);
807324fb6beSRobert Watson 	td->td_retval[0] = p->p_pid;
808324fb6beSRobert Watson 	if (status)
809324fb6beSRobert Watson 		*status = p->p_xstat;	/* convert to int */
810324fb6beSRobert Watson 	if (options & WNOWAIT) {
811324fb6beSRobert Watson 		/*
812324fb6beSRobert Watson 		 *  Only poll, returning the status.  Caller does not wish to
813324fb6beSRobert Watson 		 * release the proc struct just yet.
814324fb6beSRobert Watson 		 */
815324fb6beSRobert Watson 		PROC_UNLOCK(p);
816324fb6beSRobert Watson 		sx_xunlock(&proctree_lock);
817324fb6beSRobert Watson 		return;
818324fb6beSRobert Watson 	}
819324fb6beSRobert Watson 
820324fb6beSRobert Watson 	PROC_LOCK(q);
821324fb6beSRobert Watson 	sigqueue_take(p->p_ksi);
822324fb6beSRobert Watson 	PROC_UNLOCK(q);
823324fb6beSRobert Watson 	PROC_UNLOCK(p);
824324fb6beSRobert Watson 
825324fb6beSRobert Watson 	/*
826324fb6beSRobert Watson 	 * If we got the child via a ptrace 'attach', we need to give it back
827324fb6beSRobert Watson 	 * to the old parent.
828324fb6beSRobert Watson 	 */
829d7359980SKonstantin Belousov 	if (p->p_oppid != 0) {
830d7359980SKonstantin Belousov 		t = proc_realparent(p);
831d7359980SKonstantin Belousov 		PROC_LOCK(t);
832324fb6beSRobert Watson 		PROC_LOCK(p);
833324fb6beSRobert Watson 		proc_reparent(p, t);
834f528c3fdSDavid E. O'Brien 		p->p_oppid = 0;
835324fb6beSRobert Watson 		PROC_UNLOCK(p);
836ad6eec7bSJohn Baldwin 		pksignal(t, SIGCHLD, p->p_ksi);
837324fb6beSRobert Watson 		wakeup(t);
838324fb6beSRobert Watson 		cv_broadcast(&p->p_pwait);
839324fb6beSRobert Watson 		PROC_UNLOCK(t);
840324fb6beSRobert Watson 		sx_xunlock(&proctree_lock);
841324fb6beSRobert Watson 		return;
842324fb6beSRobert Watson 	}
843324fb6beSRobert Watson 
844324fb6beSRobert Watson 	/*
845324fb6beSRobert Watson 	 * Remove other references to this process to ensure we have an
846324fb6beSRobert Watson 	 * exclusive reference.
847324fb6beSRobert Watson 	 */
848324fb6beSRobert Watson 	sx_xlock(&allproc_lock);
849324fb6beSRobert Watson 	LIST_REMOVE(p, p_list);	/* off zombproc */
850324fb6beSRobert Watson 	sx_xunlock(&allproc_lock);
851324fb6beSRobert Watson 	LIST_REMOVE(p, p_sibling);
8527335ed90SKonstantin Belousov 	PROC_LOCK(p);
8532e39e24fSKonstantin Belousov 	clear_orphan(p);
8547335ed90SKonstantin Belousov 	PROC_UNLOCK(p);
855324fb6beSRobert Watson 	leavepgrp(p);
856cfb5f768SJonathan Anderson 	if (p->p_procdesc != NULL)
857cfb5f768SJonathan Anderson 		procdesc_reap(p);
858324fb6beSRobert Watson 	sx_xunlock(&proctree_lock);
859324fb6beSRobert Watson 
860324fb6beSRobert Watson 	/*
861324fb6beSRobert Watson 	 * As a side effect of this lock, we know that all other writes to
862324fb6beSRobert Watson 	 * this proc are visible now, so no more locking is needed for p.
863324fb6beSRobert Watson 	 */
864324fb6beSRobert Watson 	PROC_LOCK(p);
865324fb6beSRobert Watson 	p->p_xstat = 0;		/* XXX: why? */
866324fb6beSRobert Watson 	PROC_UNLOCK(p);
867324fb6beSRobert Watson 	PROC_LOCK(q);
868324fb6beSRobert Watson 	ruadd(&q->p_stats->p_cru, &q->p_crux, &p->p_ru, &p->p_rux);
869324fb6beSRobert Watson 	PROC_UNLOCK(q);
870324fb6beSRobert Watson 
871324fb6beSRobert Watson 	/*
872324fb6beSRobert Watson 	 * Decrement the count of procs running with this uid.
873324fb6beSRobert Watson 	 */
874324fb6beSRobert Watson 	(void)chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0);
875324fb6beSRobert Watson 
876324fb6beSRobert Watson 	/*
877097055e2SEdward Tomasz Napierala 	 * Destroy resource accounting information associated with the process.
878097055e2SEdward Tomasz Napierala 	 */
879afcc55f3SEdward Tomasz Napierala #ifdef RACCT
880b38520f0SEdward Tomasz Napierala 	PROC_LOCK(p);
881b38520f0SEdward Tomasz Napierala 	racct_sub(p, RACCT_NPROC, 1);
882b38520f0SEdward Tomasz Napierala 	PROC_UNLOCK(p);
883afcc55f3SEdward Tomasz Napierala #endif
884b38520f0SEdward Tomasz Napierala 	racct_proc_exit(p);
885097055e2SEdward Tomasz Napierala 
886097055e2SEdward Tomasz Napierala 	/*
887324fb6beSRobert Watson 	 * Free credentials, arguments, and sigacts.
888324fb6beSRobert Watson 	 */
889324fb6beSRobert Watson 	crfree(p->p_ucred);
890324fb6beSRobert Watson 	p->p_ucred = NULL;
891324fb6beSRobert Watson 	pargs_drop(p->p_args);
892324fb6beSRobert Watson 	p->p_args = NULL;
893324fb6beSRobert Watson 	sigacts_free(p->p_sigacts);
894324fb6beSRobert Watson 	p->p_sigacts = NULL;
895324fb6beSRobert Watson 
896324fb6beSRobert Watson 	/*
897324fb6beSRobert Watson 	 * Do any thread-system specific cleanups.
898324fb6beSRobert Watson 	 */
899324fb6beSRobert Watson 	thread_wait(p);
900324fb6beSRobert Watson 
901324fb6beSRobert Watson 	/*
902324fb6beSRobert Watson 	 * Give vm and machine-dependent layer a chance to free anything that
903324fb6beSRobert Watson 	 * cpu_exit couldn't release while still running in process context.
904324fb6beSRobert Watson 	 */
905324fb6beSRobert Watson 	vm_waitproc(p);
906324fb6beSRobert Watson #ifdef MAC
907324fb6beSRobert Watson 	mac_proc_destroy(p);
908324fb6beSRobert Watson #endif
909324fb6beSRobert Watson 	KASSERT(FIRST_THREAD_IN_PROC(p),
910324fb6beSRobert Watson 	    ("proc_reap: no residual thread!"));
911324fb6beSRobert Watson 	uma_zfree(proc_zone, p);
912324fb6beSRobert Watson 	sx_xlock(&allproc_lock);
913324fb6beSRobert Watson 	nprocs--;
914324fb6beSRobert Watson 	sx_xunlock(&allproc_lock);
915324fb6beSRobert Watson }
916324fb6beSRobert Watson 
917dcd43281SKonstantin Belousov static int
918f13b5a0fSKonstantin Belousov proc_to_reap(struct thread *td, struct proc *p, idtype_t idtype, id_t id,
919f13b5a0fSKonstantin Belousov     int *status, int options, struct __wrusage *wrusage, siginfo_t *siginfo)
920dcd43281SKonstantin Belousov {
921dcd43281SKonstantin Belousov 	struct proc *q;
922f13b5a0fSKonstantin Belousov 	struct rusage *rup;
923dcd43281SKonstantin Belousov 
924db62ced2SJaakko Heinonen 	sx_assert(&proctree_lock, SA_XLOCKED);
925db62ced2SJaakko Heinonen 
926dcd43281SKonstantin Belousov 	q = td->td_proc;
927dcd43281SKonstantin Belousov 	PROC_LOCK(p);
928f13b5a0fSKonstantin Belousov 
929f13b5a0fSKonstantin Belousov 	switch (idtype) {
930f13b5a0fSKonstantin Belousov 	case P_ALL:
931f13b5a0fSKonstantin Belousov 		break;
932f13b5a0fSKonstantin Belousov 	case P_PID:
933f13b5a0fSKonstantin Belousov 		if (p->p_pid != (pid_t)id) {
934dcd43281SKonstantin Belousov 			PROC_UNLOCK(p);
935dcd43281SKonstantin Belousov 			return (0);
936dcd43281SKonstantin Belousov 		}
937f13b5a0fSKonstantin Belousov 		break;
938f13b5a0fSKonstantin Belousov 	case P_PGID:
939f13b5a0fSKonstantin Belousov 		if (p->p_pgid != (pid_t)id) {
940f13b5a0fSKonstantin Belousov 			PROC_UNLOCK(p);
941f13b5a0fSKonstantin Belousov 			return (0);
942f13b5a0fSKonstantin Belousov 		}
943f13b5a0fSKonstantin Belousov 		break;
944f13b5a0fSKonstantin Belousov 	case P_SID:
945f13b5a0fSKonstantin Belousov 		if (p->p_session->s_sid != (pid_t)id) {
946f13b5a0fSKonstantin Belousov 			PROC_UNLOCK(p);
947f13b5a0fSKonstantin Belousov 			return (0);
948f13b5a0fSKonstantin Belousov 		}
949f13b5a0fSKonstantin Belousov 		break;
950f13b5a0fSKonstantin Belousov 	case P_UID:
951f13b5a0fSKonstantin Belousov 		if (p->p_ucred->cr_uid != (uid_t)id) {
952f13b5a0fSKonstantin Belousov 			PROC_UNLOCK(p);
953f13b5a0fSKonstantin Belousov 			return (0);
954f13b5a0fSKonstantin Belousov 		}
955f13b5a0fSKonstantin Belousov 		break;
956f13b5a0fSKonstantin Belousov 	case P_GID:
957f13b5a0fSKonstantin Belousov 		if (p->p_ucred->cr_gid != (gid_t)id) {
958f13b5a0fSKonstantin Belousov 			PROC_UNLOCK(p);
959f13b5a0fSKonstantin Belousov 			return (0);
960f13b5a0fSKonstantin Belousov 		}
961f13b5a0fSKonstantin Belousov 		break;
962f13b5a0fSKonstantin Belousov 	case P_JAILID:
963b1051d92SMateusz Guzik 		if (p->p_ucred->cr_prison->pr_id != (int)id) {
964f13b5a0fSKonstantin Belousov 			PROC_UNLOCK(p);
965f13b5a0fSKonstantin Belousov 			return (0);
966f13b5a0fSKonstantin Belousov 		}
967f13b5a0fSKonstantin Belousov 		break;
968f13b5a0fSKonstantin Belousov 	/*
969f13b5a0fSKonstantin Belousov 	 * It seems that the thread structures get zeroed out
970f13b5a0fSKonstantin Belousov 	 * at process exit.  This makes it impossible to
971f13b5a0fSKonstantin Belousov 	 * support P_SETID, P_CID or P_CPUID.
972f13b5a0fSKonstantin Belousov 	 */
973f13b5a0fSKonstantin Belousov 	default:
974f13b5a0fSKonstantin Belousov 		PROC_UNLOCK(p);
975f13b5a0fSKonstantin Belousov 		return (0);
976f13b5a0fSKonstantin Belousov 	}
977f13b5a0fSKonstantin Belousov 
978dcd43281SKonstantin Belousov 	if (p_canwait(td, p)) {
979dcd43281SKonstantin Belousov 		PROC_UNLOCK(p);
980dcd43281SKonstantin Belousov 		return (0);
981dcd43281SKonstantin Belousov 	}
982dcd43281SKonstantin Belousov 
983f13b5a0fSKonstantin Belousov 	if (((options & WEXITED) == 0) && (p->p_state == PRS_ZOMBIE)) {
984f13b5a0fSKonstantin Belousov 		PROC_UNLOCK(p);
985f13b5a0fSKonstantin Belousov 		return (0);
986f13b5a0fSKonstantin Belousov 	}
987f13b5a0fSKonstantin Belousov 
988dcd43281SKonstantin Belousov 	/*
989dcd43281SKonstantin Belousov 	 * This special case handles a kthread spawned by linux_clone
990dcd43281SKonstantin Belousov 	 * (see linux_misc.c).  The linux_wait4 and linux_waitpid
991dcd43281SKonstantin Belousov 	 * functions need to be able to distinguish between waiting
992dcd43281SKonstantin Belousov 	 * on a process and waiting on a thread.  It is a thread if
993dcd43281SKonstantin Belousov 	 * p_sigparent is not SIGCHLD, and the WLINUXCLONE option
994dcd43281SKonstantin Belousov 	 * signifies we want to wait for threads and not processes.
995dcd43281SKonstantin Belousov 	 */
996dcd43281SKonstantin Belousov 	if ((p->p_sigparent != SIGCHLD) ^
997dcd43281SKonstantin Belousov 	    ((options & WLINUXCLONE) != 0)) {
998dcd43281SKonstantin Belousov 		PROC_UNLOCK(p);
999dcd43281SKonstantin Belousov 		return (0);
1000dcd43281SKonstantin Belousov 	}
1001dcd43281SKonstantin Belousov 
1002f13b5a0fSKonstantin Belousov 	if (siginfo != NULL) {
1003f13b5a0fSKonstantin Belousov 		bzero(siginfo, sizeof(*siginfo));
1004f13b5a0fSKonstantin Belousov 		siginfo->si_errno = 0;
1005f13b5a0fSKonstantin Belousov 
1006f13b5a0fSKonstantin Belousov 		/*
1007f13b5a0fSKonstantin Belousov 		 * SUSv4 requires that the si_signo value is always
1008f13b5a0fSKonstantin Belousov 		 * SIGCHLD. Obey it despite the rfork(2) interface
1009f13b5a0fSKonstantin Belousov 		 * allows to request other signal for child exit
1010f13b5a0fSKonstantin Belousov 		 * notification.
1011f13b5a0fSKonstantin Belousov 		 */
1012f13b5a0fSKonstantin Belousov 		siginfo->si_signo = SIGCHLD;
1013f13b5a0fSKonstantin Belousov 
1014f13b5a0fSKonstantin Belousov 		/*
1015f13b5a0fSKonstantin Belousov 		 *  This is still a rough estimate.  We will fix the
1016f13b5a0fSKonstantin Belousov 		 *  cases TRAPPED, STOPPED, and CONTINUED later.
1017f13b5a0fSKonstantin Belousov 		 */
1018b20a9aa9SJilles Tjoelker 		if (WCOREDUMP(p->p_xstat)) {
1019f13b5a0fSKonstantin Belousov 			siginfo->si_code = CLD_DUMPED;
1020b20a9aa9SJilles Tjoelker 			siginfo->si_status = WTERMSIG(p->p_xstat);
1021b20a9aa9SJilles Tjoelker 		} else if (WIFSIGNALED(p->p_xstat)) {
1022f13b5a0fSKonstantin Belousov 			siginfo->si_code = CLD_KILLED;
1023b20a9aa9SJilles Tjoelker 			siginfo->si_status = WTERMSIG(p->p_xstat);
1024b20a9aa9SJilles Tjoelker 		} else {
1025f13b5a0fSKonstantin Belousov 			siginfo->si_code = CLD_EXITED;
1026b20a9aa9SJilles Tjoelker 			siginfo->si_status = WEXITSTATUS(p->p_xstat);
1027b20a9aa9SJilles Tjoelker 		}
1028f13b5a0fSKonstantin Belousov 
1029f13b5a0fSKonstantin Belousov 		siginfo->si_pid = p->p_pid;
1030f13b5a0fSKonstantin Belousov 		siginfo->si_uid = p->p_ucred->cr_uid;
1031f13b5a0fSKonstantin Belousov 
1032f13b5a0fSKonstantin Belousov 		/*
1033f13b5a0fSKonstantin Belousov 		 * The si_addr field would be useful additional
1034f13b5a0fSKonstantin Belousov 		 * detail, but apparently the PC value may be lost
1035f13b5a0fSKonstantin Belousov 		 * when we reach this point.  bzero() above sets
1036f13b5a0fSKonstantin Belousov 		 * siginfo->si_addr to NULL.
1037f13b5a0fSKonstantin Belousov 		 */
1038f13b5a0fSKonstantin Belousov 	}
1039f13b5a0fSKonstantin Belousov 
1040f13b5a0fSKonstantin Belousov 	/*
1041f13b5a0fSKonstantin Belousov 	 * There should be no reason to limit resources usage info to
1042f13b5a0fSKonstantin Belousov 	 * exited processes only.  A snapshot about any resources used
1043f13b5a0fSKonstantin Belousov 	 * by a stopped process may be exactly what is needed.
1044f13b5a0fSKonstantin Belousov 	 */
1045f13b5a0fSKonstantin Belousov 	if (wrusage != NULL) {
1046f13b5a0fSKonstantin Belousov 		rup = &wrusage->wru_self;
1047f13b5a0fSKonstantin Belousov 		*rup = p->p_ru;
10485c7bebf9SKonstantin Belousov 		PROC_STATLOCK(p);
1049f13b5a0fSKonstantin Belousov 		calcru(p, &rup->ru_utime, &rup->ru_stime);
10505c7bebf9SKonstantin Belousov 		PROC_STATUNLOCK(p);
1051f13b5a0fSKonstantin Belousov 
1052f13b5a0fSKonstantin Belousov 		rup = &wrusage->wru_children;
1053f13b5a0fSKonstantin Belousov 		*rup = p->p_stats->p_cru;
1054f13b5a0fSKonstantin Belousov 		calccru(p, &rup->ru_utime, &rup->ru_stime);
1055f13b5a0fSKonstantin Belousov 	}
1056f13b5a0fSKonstantin Belousov 
1057dcd43281SKonstantin Belousov 	if (p->p_state == PRS_ZOMBIE) {
10585c7bebf9SKonstantin Belousov 		PROC_SLOCK(p);
1059f13b5a0fSKonstantin Belousov 		proc_reap(td, p, status, options);
1060dcd43281SKonstantin Belousov 		return (-1);
1061dcd43281SKonstantin Belousov 	}
1062dcd43281SKonstantin Belousov 	PROC_UNLOCK(p);
1063dcd43281SKonstantin Belousov 	return (1);
1064dcd43281SKonstantin Belousov }
1065dcd43281SKonstantin Belousov 
1066b7e23e82SJohn Baldwin int
10677eaec467SJohn Baldwin kern_wait(struct thread *td, pid_t pid, int *status, int options,
10687eaec467SJohn Baldwin     struct rusage *rusage)
1069df8bae1dSRodney W. Grimes {
1070f13b5a0fSKonstantin Belousov 	struct __wrusage wru, *wrup;
1071f13b5a0fSKonstantin Belousov 	idtype_t idtype;
1072f13b5a0fSKonstantin Belousov 	id_t id;
1073f13b5a0fSKonstantin Belousov 	int ret;
1074f13b5a0fSKonstantin Belousov 
1075538375d4SKonstantin Belousov 	/*
1076538375d4SKonstantin Belousov 	 * Translate the special pid values into the (idtype, pid)
1077538375d4SKonstantin Belousov 	 * pair for kern_wait6.  The WAIT_MYPGRP case is handled by
1078538375d4SKonstantin Belousov 	 * kern_wait6() on its own.
1079538375d4SKonstantin Belousov 	 */
1080f13b5a0fSKonstantin Belousov 	if (pid == WAIT_ANY) {
1081f13b5a0fSKonstantin Belousov 		idtype = P_ALL;
1082f13b5a0fSKonstantin Belousov 		id = 0;
1083ea293f3fSKonstantin Belousov 	} else if (pid < 0) {
1084f13b5a0fSKonstantin Belousov 		idtype = P_PGID;
1085f13b5a0fSKonstantin Belousov 		id = (id_t)-pid;
1086ea293f3fSKonstantin Belousov 	} else {
1087f13b5a0fSKonstantin Belousov 		idtype = P_PID;
1088f13b5a0fSKonstantin Belousov 		id = (id_t)pid;
1089f13b5a0fSKonstantin Belousov 	}
1090538375d4SKonstantin Belousov 
1091f13b5a0fSKonstantin Belousov 	if (rusage != NULL)
1092f13b5a0fSKonstantin Belousov 		wrup = &wru;
1093f13b5a0fSKonstantin Belousov 	else
1094f13b5a0fSKonstantin Belousov 		wrup = NULL;
1095538375d4SKonstantin Belousov 
1096f13b5a0fSKonstantin Belousov 	/*
1097f13b5a0fSKonstantin Belousov 	 * For backward compatibility we implicitly add flags WEXITED
1098f13b5a0fSKonstantin Belousov 	 * and WTRAPPED here.
1099f13b5a0fSKonstantin Belousov 	 */
1100f13b5a0fSKonstantin Belousov 	options |= WEXITED | WTRAPPED;
1101f13b5a0fSKonstantin Belousov 	ret = kern_wait6(td, idtype, id, status, options, wrup, NULL);
1102f13b5a0fSKonstantin Belousov 	if (rusage != NULL)
1103f13b5a0fSKonstantin Belousov 		*rusage = wru.wru_self;
1104f13b5a0fSKonstantin Belousov 	return (ret);
1105f13b5a0fSKonstantin Belousov }
1106f13b5a0fSKonstantin Belousov 
1107f13b5a0fSKonstantin Belousov int
1108f13b5a0fSKonstantin Belousov kern_wait6(struct thread *td, idtype_t idtype, id_t id, int *status,
1109f13b5a0fSKonstantin Belousov     int options, struct __wrusage *wrusage, siginfo_t *siginfo)
1110f13b5a0fSKonstantin Belousov {
1111324fb6beSRobert Watson 	struct proc *p, *q;
1112dcd43281SKonstantin Belousov 	int error, nfound, ret;
1113df8bae1dSRodney W. Grimes 
1114f13b5a0fSKonstantin Belousov 	AUDIT_ARG_VALUE((int)idtype);	/* XXX - This is likely wrong! */
1115f13b5a0fSKonstantin Belousov 	AUDIT_ARG_PID((pid_t)id);	/* XXX - This may be wrong! */
11162ef24ddeSRobert Watson 	AUDIT_ARG_VALUE(options);
1117de3007e8SWayne Salamon 
1118b40ce416SJulian Elischer 	q = td->td_proc;
1119f13b5a0fSKonstantin Belousov 
1120a2a85596SKonstantin Belousov 	if ((pid_t)id == WAIT_MYPGRP && (idtype == P_PID || idtype == P_PGID)) {
1121a545089eSKonstantin Belousov 		PROC_LOCK(q);
1122f13b5a0fSKonstantin Belousov 		id = (id_t)q->p_pgid;
1123a545089eSKonstantin Belousov 		PROC_UNLOCK(q);
1124f13b5a0fSKonstantin Belousov 		idtype = P_PGID;
1125f591779bSSeigo Tanimura 	}
1126f13b5a0fSKonstantin Belousov 
1127f528c3fdSDavid E. O'Brien 	/* If we don't know the option, just return. */
1128f13b5a0fSKonstantin Belousov 	if ((options & ~(WUNTRACED | WNOHANG | WCONTINUED | WNOWAIT |
1129f13b5a0fSKonstantin Belousov 	    WEXITED | WTRAPPED | WLINUXCLONE)) != 0)
11306dc958b9SJohn Baldwin 		return (EINVAL);
1131f13b5a0fSKonstantin Belousov 	if ((options & (WEXITED | WUNTRACED | WCONTINUED | WTRAPPED)) == 0) {
1132f13b5a0fSKonstantin Belousov 		/*
1133f13b5a0fSKonstantin Belousov 		 * We will be unable to find any matching processes,
1134f13b5a0fSKonstantin Belousov 		 * because there are no known events to look for.
1135f13b5a0fSKonstantin Belousov 		 * Prefer to return error instead of blocking
1136f13b5a0fSKonstantin Belousov 		 * indefinitely.
1137f13b5a0fSKonstantin Belousov 		 */
1138f13b5a0fSKonstantin Belousov 		return (EINVAL);
1139f13b5a0fSKonstantin Belousov 	}
1140f13b5a0fSKonstantin Belousov 
1141df8bae1dSRodney W. Grimes loop:
1142902c0d82SDavid Xu 	if (q->p_flag & P_STATCHILD) {
1143902c0d82SDavid Xu 		PROC_LOCK(q);
1144902c0d82SDavid Xu 		q->p_flag &= ~P_STATCHILD;
1145902c0d82SDavid Xu 		PROC_UNLOCK(q);
1146902c0d82SDavid Xu 	}
1147df8bae1dSRodney W. Grimes 	nfound = 0;
1148d7aadbf9SJohn Baldwin 	sx_xlock(&proctree_lock);
11492e3c8fcbSPoul-Henning Kamp 	LIST_FOREACH(p, &q->p_children, p_sibling) {
1150f13b5a0fSKonstantin Belousov 		ret = proc_to_reap(td, p, idtype, id, status, options,
1151f13b5a0fSKonstantin Belousov 		    wrusage, siginfo);
1152dcd43281SKonstantin Belousov 		if (ret == 0)
1153df8bae1dSRodney W. Grimes 			continue;
1154dcd43281SKonstantin Belousov 		else if (ret == 1)
1155df8bae1dSRodney W. Grimes 			nfound++;
1156dcd43281SKonstantin Belousov 		else
1157d7aadbf9SJohn Baldwin 			return (0);
1158dcd43281SKonstantin Belousov 
1159dcd43281SKonstantin Belousov 		PROC_LOCK(p);
1160dcd43281SKonstantin Belousov 		PROC_SLOCK(p);
1161f13b5a0fSKonstantin Belousov 
1162f13b5a0fSKonstantin Belousov 		if ((options & WTRAPPED) != 0 &&
1163f13b5a0fSKonstantin Belousov 		    (p->p_flag & P_TRACED) != 0 &&
1164f13b5a0fSKonstantin Belousov 		    (p->p_flag & (P_STOPPED_TRACE | P_STOPPED_SIG)) != 0 &&
11655a2f73e6SDavid Xu 		    (p->p_suspcount == p->p_numthreads) &&
1166f13b5a0fSKonstantin Belousov 		    ((p->p_flag & P_WAITED) == 0)) {
1167982d11f8SJeff Roberson 			PROC_SUNLOCK(p);
1168f13b5a0fSKonstantin Belousov 			if ((options & WNOWAIT) == 0)
1169df8bae1dSRodney W. Grimes 				p->p_flag |= P_WAITED;
1170d7aadbf9SJohn Baldwin 			sx_xunlock(&proctree_lock);
1171b40ce416SJulian Elischer 			td->td_retval[0] = p->p_pid;
1172ebceaf6dSDavid Xu 
1173f13b5a0fSKonstantin Belousov 			if (status != NULL)
1174f13b5a0fSKonstantin Belousov 				*status = W_STOPCODE(p->p_xstat);
1175f13b5a0fSKonstantin Belousov 			if (siginfo != NULL) {
1176f13b5a0fSKonstantin Belousov 				siginfo->si_status = p->p_xstat;
1177f13b5a0fSKonstantin Belousov 				siginfo->si_code = CLD_TRAPPED;
1178f13b5a0fSKonstantin Belousov 			}
1179f13b5a0fSKonstantin Belousov 			if ((options & WNOWAIT) == 0) {
1180ebceaf6dSDavid Xu 				PROC_LOCK(q);
1181ebceaf6dSDavid Xu 				sigqueue_take(p->p_ksi);
1182ebceaf6dSDavid Xu 				PROC_UNLOCK(q);
1183f13b5a0fSKonstantin Belousov 			}
1184ebceaf6dSDavid Xu 
1185f13b5a0fSKonstantin Belousov 			PROC_UNLOCK(p);
1186f13b5a0fSKonstantin Belousov 			return (0);
1187f13b5a0fSKonstantin Belousov 		}
1188f13b5a0fSKonstantin Belousov 		if ((options & WUNTRACED) != 0 &&
1189f13b5a0fSKonstantin Belousov 		    (p->p_flag & P_STOPPED_SIG) != 0 &&
1190f13b5a0fSKonstantin Belousov 		    (p->p_suspcount == p->p_numthreads) &&
1191f13b5a0fSKonstantin Belousov 		    ((p->p_flag & P_WAITED) == 0)) {
1192f13b5a0fSKonstantin Belousov 			PROC_SUNLOCK(p);
1193f13b5a0fSKonstantin Belousov 			if ((options & WNOWAIT) == 0)
1194f13b5a0fSKonstantin Belousov 				p->p_flag |= P_WAITED;
1195f13b5a0fSKonstantin Belousov 			sx_xunlock(&proctree_lock);
1196f13b5a0fSKonstantin Belousov 			td->td_retval[0] = p->p_pid;
1197f13b5a0fSKonstantin Belousov 
1198f13b5a0fSKonstantin Belousov 			if (status != NULL)
1199f13b5a0fSKonstantin Belousov 				*status = W_STOPCODE(p->p_xstat);
1200f13b5a0fSKonstantin Belousov 			if (siginfo != NULL) {
1201f13b5a0fSKonstantin Belousov 				siginfo->si_status = p->p_xstat;
1202f13b5a0fSKonstantin Belousov 				siginfo->si_code = CLD_STOPPED;
1203f13b5a0fSKonstantin Belousov 			}
1204f13b5a0fSKonstantin Belousov 			if ((options & WNOWAIT) == 0) {
1205f13b5a0fSKonstantin Belousov 				PROC_LOCK(q);
1206f13b5a0fSKonstantin Belousov 				sigqueue_take(p->p_ksi);
1207f13b5a0fSKonstantin Belousov 				PROC_UNLOCK(q);
1208f13b5a0fSKonstantin Belousov 			}
1209f13b5a0fSKonstantin Belousov 
1210f13b5a0fSKonstantin Belousov 			PROC_UNLOCK(p);
1211b7e23e82SJohn Baldwin 			return (0);
1212df8bae1dSRodney W. Grimes 		}
1213982d11f8SJeff Roberson 		PROC_SUNLOCK(p);
1214f13b5a0fSKonstantin Belousov 		if ((options & WCONTINUED) != 0 &&
1215f13b5a0fSKonstantin Belousov 		    (p->p_flag & P_CONTINUED) != 0) {
12166ee093fbSMike Barcroft 			sx_xunlock(&proctree_lock);
12176ee093fbSMike Barcroft 			td->td_retval[0] = p->p_pid;
1218f13b5a0fSKonstantin Belousov 			if ((options & WNOWAIT) == 0) {
12196ee093fbSMike Barcroft 				p->p_flag &= ~P_CONTINUED;
1220ebceaf6dSDavid Xu 				PROC_LOCK(q);
1221ebceaf6dSDavid Xu 				sigqueue_take(p->p_ksi);
1222ebceaf6dSDavid Xu 				PROC_UNLOCK(q);
1223f13b5a0fSKonstantin Belousov 			}
1224e94cc4acSDavid Xu 			PROC_UNLOCK(p);
1225ebceaf6dSDavid Xu 
1226f13b5a0fSKonstantin Belousov 			if (status != NULL)
1227b7e23e82SJohn Baldwin 				*status = SIGCONT;
1228f13b5a0fSKonstantin Belousov 			if (siginfo != NULL) {
1229f13b5a0fSKonstantin Belousov 				siginfo->si_status = SIGCONT;
1230f13b5a0fSKonstantin Belousov 				siginfo->si_code = CLD_CONTINUED;
1231f13b5a0fSKonstantin Belousov 			}
1232dcdc6c36SKonstantin Belousov 			return (0);
12336ee093fbSMike Barcroft 		}
1234d7aadbf9SJohn Baldwin 		PROC_UNLOCK(p);
1235d7aadbf9SJohn Baldwin 	}
1236dcd43281SKonstantin Belousov 
1237dcd43281SKonstantin Belousov 	/*
1238dcd43281SKonstantin Belousov 	 * Look in the orphans list too, to allow the parent to
1239dcd43281SKonstantin Belousov 	 * collect it's child exit status even if child is being
1240dcd43281SKonstantin Belousov 	 * debugged.
1241dcd43281SKonstantin Belousov 	 *
1242dcd43281SKonstantin Belousov 	 * Debugger detaches from the parent upon successful
1243dcd43281SKonstantin Belousov 	 * switch-over from parent to child.  At this point due to
1244dcd43281SKonstantin Belousov 	 * re-parenting the parent loses the child to debugger and a
1245dcd43281SKonstantin Belousov 	 * wait4(2) call would report that it has no children to wait
1246dcd43281SKonstantin Belousov 	 * for.  By maintaining a list of orphans we allow the parent
1247dcd43281SKonstantin Belousov 	 * to successfully wait until the child becomes a zombie.
1248dcd43281SKonstantin Belousov 	 */
1249dcd43281SKonstantin Belousov 	LIST_FOREACH(p, &q->p_orphans, p_orphan) {
1250f13b5a0fSKonstantin Belousov 		ret = proc_to_reap(td, p, idtype, id, status, options,
1251f13b5a0fSKonstantin Belousov 		    wrusage, siginfo);
1252dcd43281SKonstantin Belousov 		if (ret == 0)
1253dcd43281SKonstantin Belousov 			continue;
1254dcd43281SKonstantin Belousov 		else if (ret == 1)
1255dcd43281SKonstantin Belousov 			nfound++;
1256dcd43281SKonstantin Belousov 		else
1257dcd43281SKonstantin Belousov 			return (0);
1258dcd43281SKonstantin Belousov 	}
1259d7aadbf9SJohn Baldwin 	if (nfound == 0) {
1260d7aadbf9SJohn Baldwin 		sx_xunlock(&proctree_lock);
1261d7aadbf9SJohn Baldwin 		return (ECHILD);
1262d7aadbf9SJohn Baldwin 	}
1263b7e23e82SJohn Baldwin 	if (options & WNOHANG) {
1264d7aadbf9SJohn Baldwin 		sx_xunlock(&proctree_lock);
1265d7aadbf9SJohn Baldwin 		td->td_retval[0] = 0;
1266d7aadbf9SJohn Baldwin 		return (0);
1267d7aadbf9SJohn Baldwin 	}
1268d7aadbf9SJohn Baldwin 	PROC_LOCK(q);
1269d7aadbf9SJohn Baldwin 	sx_xunlock(&proctree_lock);
127095992d56SDavid Xu 	if (q->p_flag & P_STATCHILD) {
127195992d56SDavid Xu 		q->p_flag &= ~P_STATCHILD;
127295992d56SDavid Xu 		error = 0;
127395992d56SDavid Xu 	} else
12747f05b035SAlfred Perlstein 		error = msleep(q, &q->p_mtx, PWAIT | PCATCH, "wait", 0);
1275d7aadbf9SJohn Baldwin 	PROC_UNLOCK(q);
12764ae89b95SJohn Baldwin 	if (error)
1277d7aadbf9SJohn Baldwin 		return (error);
1278d7aadbf9SJohn Baldwin 	goto loop;
1279d7aadbf9SJohn Baldwin }
1280df8bae1dSRodney W. Grimes 
1281df8bae1dSRodney W. Grimes /*
128298f03f90SJake Burkholder  * Make process 'parent' the new parent of process 'child'.
128398f03f90SJake Burkholder  * Must be called with an exclusive hold of proctree lock.
1284df8bae1dSRodney W. Grimes  */
1285df8bae1dSRodney W. Grimes void
1286830c3153SDag-Erling Smørgrav proc_reparent(struct proc *child, struct proc *parent)
1287df8bae1dSRodney W. Grimes {
1288df8bae1dSRodney W. Grimes 
12894e5e677bSJohn Baldwin 	sx_assert(&proctree_lock, SX_XLOCKED);
1290c65437a3SJohn Baldwin 	PROC_LOCK_ASSERT(child, MA_OWNED);
1291df8bae1dSRodney W. Grimes 	if (child->p_pptr == parent)
1292df8bae1dSRodney W. Grimes 		return;
1293df8bae1dSRodney W. Grimes 
1294ff766807SDavid Xu 	PROC_LOCK(child->p_pptr);
1295ff766807SDavid Xu 	sigqueue_take(child->p_ksi);
1296ff766807SDavid Xu 	PROC_UNLOCK(child->p_pptr);
1297b75356e1SJeffrey Hsu 	LIST_REMOVE(child, p_sibling);
1298b75356e1SJeffrey Hsu 	LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
1299dcd43281SKonstantin Belousov 
13002e39e24fSKonstantin Belousov 	clear_orphan(child);
1301dcd43281SKonstantin Belousov 	if (child->p_flag & P_TRACED) {
1302d7359980SKonstantin Belousov 		if (LIST_EMPTY(&child->p_pptr->p_orphans)) {
1303d7359980SKonstantin Belousov 			child->p_treeflag |= P_TREE_FIRST_ORPHAN;
1304d7359980SKonstantin Belousov 			LIST_INSERT_HEAD(&child->p_pptr->p_orphans, child,
1305d7359980SKonstantin Belousov 			    p_orphan);
1306d7359980SKonstantin Belousov 		} else {
1307ba78d6b7SMark Johnston 			LIST_INSERT_AFTER(LIST_FIRST(&child->p_pptr->p_orphans),
1308ba78d6b7SMark Johnston 			    child, p_orphan);
1309d7359980SKonstantin Belousov 		}
1310d7359980SKonstantin Belousov 		child->p_treeflag |= P_TREE_ORPHANED;
1311dcd43281SKonstantin Belousov 	}
1312dcd43281SKonstantin Belousov 
1313df8bae1dSRodney W. Grimes 	child->p_pptr = parent;
1314df8bae1dSRodney W. Grimes }
1315