xref: /freebsd/sys/kern/kern_exit.c (revision d0675399d09f02d347912e23d004329710338450)
19454b2d8SWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1991, 1993
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
7df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
8df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
9df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
11df8bae1dSRodney W. Grimes  *
12df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
13df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
14df8bae1dSRodney W. Grimes  * are met:
15df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
17df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
18df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
19df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
2069a28758SEd Maste  * 3. Neither the name of the University nor the names of its contributors
21df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
22df8bae1dSRodney W. Grimes  *    without specific prior written permission.
23df8bae1dSRodney W. Grimes  *
24df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37677b542eSDavid E. O'Brien #include <sys/cdefs.h>
3847877889SJohn Baldwin #include "opt_ddb.h"
39db6a20e2SGarrett Wollman #include "opt_ktrace.h"
40db6a20e2SGarrett Wollman 
41df8bae1dSRodney W. Grimes #include <sys/param.h>
42df8bae1dSRodney W. Grimes #include <sys/systm.h>
435fdb8324SBruce Evans #include <sys/sysproto.h>
444a144410SRobert Watson #include <sys/capsicum.h>
4575b8b3b2SJohn Baldwin #include <sys/eventhandler.h>
461c5bb3eaSPeter Wemm #include <sys/kernel.h>
47daec9284SConrad Meyer #include <sys/ktr.h>
48a1c995b6SPoul-Henning Kamp #include <sys/malloc.h>
49f34fa851SJohn Baldwin #include <sys/lock.h>
5035e0e5b3SJohn Baldwin #include <sys/mutex.h>
51df8bae1dSRodney W. Grimes #include <sys/proc.h>
52cfb5f768SJonathan Anderson #include <sys/procdesc.h>
53413628a7SBjoern A. Zeeb #include <sys/jail.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>
58097055e2SEdward Tomasz Napierala #include <sys/racct.h>
59df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
6025f6e35aSPoul-Henning Kamp #include <sys/sbuf.h>
61797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
62b43179fbSJeff Roberson #include <sys/sched.h>
631005a129SJohn Baldwin #include <sys/sx.h>
64c8837938SJohn Baldwin #include <sys/syscallsubr.h>
65fd3ac06fSKonstantin Belousov #include <sys/sysctl.h>
6625f6e35aSPoul-Henning Kamp #include <sys/syslog.h>
67df8bae1dSRodney W. Grimes #include <sys/ptrace.h>
687c409b8aSJeffrey Hsu #include <sys/acct.h>		/* for acct_process() function prototype */
69797f2d22SPoul-Henning Kamp #include <sys/filedesc.h>
705d217f17SJohn Birrell #include <sys/sdt.h>
71780dc5a8SPeter Wemm #include <sys/shm.h>
72780dc5a8SPeter Wemm #include <sys/sem.h>
7387a9b18dSKonstantin Belousov #include <sys/sysent.h>
74e68c6191SKonstantin Belousov #include <sys/timers.h>
75af29f399SDmitry Chagin #include <sys/umtxvar.h>
766c84de02SJohn Baldwin #ifdef KTRACE
776c84de02SJohn Baldwin #include <sys/ktrace.h>
786c84de02SJohn Baldwin #endif
79780dc5a8SPeter Wemm 
80fcf7f27aSRobert Watson #include <security/audit/audit.h>
81aed55708SRobert Watson #include <security/mac/mac_framework.h>
82fcf7f27aSRobert Watson 
83df8bae1dSRodney W. Grimes #include <vm/vm.h>
84eb30c1c0SPeter Wemm #include <vm/vm_extern.h>
857a6b989bSJohn Baldwin #include <vm/vm_param.h>
86efeaf95aSDavid Greenman #include <vm/pmap.h>
87efeaf95aSDavid Greenman #include <vm/vm_map.h>
882d21129dSAlan Cox #include <vm/vm_page.h>
89c897b813SJeff Roberson #include <vm/uma.h>
90df8bae1dSRodney W. Grimes 
915d217f17SJohn Birrell #ifdef KDTRACE_HOOKS
925d217f17SJohn Birrell #include <sys/dtrace_bsd.h>
935d217f17SJohn Birrell dtrace_execexit_func_t	dtrace_fasttrap_exit;
945d217f17SJohn Birrell #endif
955d217f17SJohn Birrell 
965d217f17SJohn Birrell SDT_PROVIDER_DECLARE(proc);
9736160958SMark Johnston SDT_PROBE_DEFINE1(proc, , , exit, "int");
985d217f17SJohn Birrell 
99fd3ac06fSKonstantin Belousov static int kern_kill_on_dbg_exit = 1;
100fd3ac06fSKonstantin Belousov SYSCTL_INT(_kern, OID_AUTO, kill_on_debugger_exit, CTLFLAG_RWTUN,
101fd3ac06fSKonstantin Belousov     &kern_kill_on_dbg_exit, 0,
102fd3ac06fSKonstantin Belousov     "Kill ptraced processes when debugger exits");
103fd3ac06fSKonstantin Belousov 
104a12e901aSKonstantin Belousov static bool kern_wait_dequeue_sigchld = 1;
105a12e901aSKonstantin Belousov SYSCTL_BOOL(_kern, OID_AUTO, wait_dequeue_sigchld, CTLFLAG_RWTUN,
106a12e901aSKonstantin Belousov     &kern_wait_dequeue_sigchld, 0,
107a12e901aSKonstantin Belousov     "Dequeue SIGCHLD on wait(2) for live process");
108a12e901aSKonstantin Belousov 
109d7359980SKonstantin Belousov struct proc *
proc_realparent(struct proc * child)110d7359980SKonstantin Belousov proc_realparent(struct proc *child)
111d7359980SKonstantin Belousov {
112d7359980SKonstantin Belousov 	struct proc *p, *parent;
113d7359980SKonstantin Belousov 
114d7359980SKonstantin Belousov 	sx_assert(&proctree_lock, SX_LOCKED);
1152c054ce9SMateusz Guzik 	if ((child->p_treeflag & P_TREE_ORPHANED) == 0)
1162c054ce9SMateusz Guzik 		return (child->p_pptr->p_pid == child->p_oppid ?
117844219f4SKonstantin Belousov 		    child->p_pptr : child->p_reaper);
118d7359980SKonstantin Belousov 	for (p = child; (p->p_treeflag & P_TREE_FIRST_ORPHAN) == 0;) {
119d7359980SKonstantin Belousov 		/* Cannot use LIST_PREV(), since the list head is not known. */
120d7359980SKonstantin Belousov 		p = __containerof(p->p_orphan.le_prev, struct proc,
121d7359980SKonstantin Belousov 		    p_orphan.le_next);
122d7359980SKonstantin Belousov 		KASSERT((p->p_treeflag & P_TREE_ORPHANED) != 0,
123d7359980SKonstantin Belousov 		    ("missing P_ORPHAN %p", p));
124d7359980SKonstantin Belousov 	}
125d7359980SKonstantin Belousov 	parent = __containerof(p->p_orphan.le_prev, struct proc,
126d7359980SKonstantin Belousov 	    p_orphans.lh_first);
127d7359980SKonstantin Belousov 	return (parent);
128d7359980SKonstantin Belousov }
129d7359980SKonstantin Belousov 
130237623b0SKonstantin Belousov void
reaper_abandon_children(struct proc * p,bool exiting)131237623b0SKonstantin Belousov reaper_abandon_children(struct proc *p, bool exiting)
132237623b0SKonstantin Belousov {
133237623b0SKonstantin Belousov 	struct proc *p1, *p2, *ptmp;
134237623b0SKonstantin Belousov 
135b9294a3eSKonstantin Belousov 	sx_assert(&proctree_lock, SX_XLOCKED);
136237623b0SKonstantin Belousov 	KASSERT(p != initproc, ("reaper_abandon_children for initproc"));
137237623b0SKonstantin Belousov 	if ((p->p_treeflag & P_TREE_REAPER) == 0)
138237623b0SKonstantin Belousov 		return;
139237623b0SKonstantin Belousov 	p1 = p->p_reaper;
140237623b0SKonstantin Belousov 	LIST_FOREACH_SAFE(p2, &p->p_reaplist, p_reapsibling, ptmp) {
141237623b0SKonstantin Belousov 		LIST_REMOVE(p2, p_reapsibling);
142237623b0SKonstantin Belousov 		p2->p_reaper = p1;
143237623b0SKonstantin Belousov 		p2->p_reapsubtree = p->p_reapsubtree;
144237623b0SKonstantin Belousov 		LIST_INSERT_HEAD(&p1->p_reaplist, p2, p_reapsibling);
145237623b0SKonstantin Belousov 		if (exiting && p2->p_pptr == p) {
146237623b0SKonstantin Belousov 			PROC_LOCK(p2);
1472c054ce9SMateusz Guzik 			proc_reparent(p2, p1, true);
148237623b0SKonstantin Belousov 			PROC_UNLOCK(p2);
149237623b0SKonstantin Belousov 		}
150237623b0SKonstantin Belousov 	}
151237623b0SKonstantin Belousov 	KASSERT(LIST_EMPTY(&p->p_reaplist), ("p_reaplist not empty"));
152237623b0SKonstantin Belousov 	p->p_treeflag &= ~P_TREE_REAPER;
153237623b0SKonstantin Belousov }
154237623b0SKonstantin Belousov 
1552e39e24fSKonstantin Belousov static void
reaper_clear(struct proc * p)15634ebdceaSMateusz Guzik reaper_clear(struct proc *p)
15734ebdceaSMateusz Guzik {
15834ebdceaSMateusz Guzik 	struct proc *p1;
15934ebdceaSMateusz Guzik 	bool clear;
16034ebdceaSMateusz Guzik 
16134ebdceaSMateusz Guzik 	sx_assert(&proctree_lock, SX_LOCKED);
16234ebdceaSMateusz Guzik 	LIST_REMOVE(p, p_reapsibling);
16334ebdceaSMateusz Guzik 	if (p->p_reapsubtree == 1)
16434ebdceaSMateusz Guzik 		return;
16534ebdceaSMateusz Guzik 	clear = true;
16634ebdceaSMateusz Guzik 	LIST_FOREACH(p1, &p->p_reaper->p_reaplist, p_reapsibling) {
16734ebdceaSMateusz Guzik 		if (p1->p_reapsubtree == p->p_reapsubtree) {
16834ebdceaSMateusz Guzik 			clear = false;
16934ebdceaSMateusz Guzik 			break;
17034ebdceaSMateusz Guzik 		}
17134ebdceaSMateusz Guzik 	}
17234ebdceaSMateusz Guzik 	if (clear)
17334ebdceaSMateusz Guzik 		proc_id_clear(PROC_ID_REAP, p->p_reapsubtree);
17434ebdceaSMateusz Guzik }
17534ebdceaSMateusz Guzik 
1769db97ca0SMariusz Zaborski void
proc_clear_orphan(struct proc * p)1779db97ca0SMariusz Zaborski proc_clear_orphan(struct proc *p)
1782e39e24fSKonstantin Belousov {
179d7359980SKonstantin Belousov 	struct proc *p1;
1802e39e24fSKonstantin Belousov 
181d7359980SKonstantin Belousov 	sx_assert(&proctree_lock, SA_XLOCKED);
182d7359980SKonstantin Belousov 	if ((p->p_treeflag & P_TREE_ORPHANED) == 0)
183d7359980SKonstantin Belousov 		return;
184d7359980SKonstantin Belousov 	if ((p->p_treeflag & P_TREE_FIRST_ORPHAN) != 0) {
185d7359980SKonstantin Belousov 		p1 = LIST_NEXT(p, p_orphan);
186d7359980SKonstantin Belousov 		if (p1 != NULL)
187d7359980SKonstantin Belousov 			p1->p_treeflag |= P_TREE_FIRST_ORPHAN;
188d7359980SKonstantin Belousov 		p->p_treeflag &= ~P_TREE_FIRST_ORPHAN;
1892e39e24fSKonstantin Belousov 	}
190d7359980SKonstantin Belousov 	LIST_REMOVE(p, p_orphan);
191d7359980SKonstantin Belousov 	p->p_treeflag &= ~P_TREE_ORPHANED;
1922e39e24fSKonstantin Belousov }
1932e39e24fSKonstantin Belousov 
19428a66fc3SKonstantin Belousov void
exit_onexit(struct proc * p)19528a66fc3SKonstantin Belousov exit_onexit(struct proc *p)
19628a66fc3SKonstantin Belousov {
19728a66fc3SKonstantin Belousov 	MPASS(p->p_numthreads == 1);
19828a66fc3SKonstantin Belousov 	umtx_thread_exit(FIRST_THREAD_IN_PROC(p));
19928a66fc3SKonstantin Belousov }
20028a66fc3SKonstantin Belousov 
201df8bae1dSRodney W. Grimes /*
202873fbcd7SRobert Watson  * exit -- death of process.
203df8bae1dSRodney W. Grimes  */
204cffb55f0SBrooks Davis int
sys_exit(struct thread * td,struct exit_args * uap)205cffb55f0SBrooks Davis sys_exit(struct thread *td, struct exit_args *uap)
206df8bae1dSRodney W. Grimes {
207b40ce416SJulian Elischer 
208b4490c6eSKonstantin Belousov 	exit1(td, uap->rval, 0);
209cffb55f0SBrooks Davis 	__unreachable();
210df8bae1dSRodney W. Grimes }
211df8bae1dSRodney W. Grimes 
2124493a13eSKonstantin Belousov void
proc_set_p2_wexit(struct proc * p)2134493a13eSKonstantin Belousov proc_set_p2_wexit(struct proc *p)
2144493a13eSKonstantin Belousov {
2154493a13eSKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
2164493a13eSKonstantin Belousov 	p->p_flag2 |= P2_WEXIT;
2174493a13eSKonstantin Belousov }
2184493a13eSKonstantin Belousov 
219df8bae1dSRodney W. Grimes /*
220873fbcd7SRobert Watson  * Exit: deallocate address space and other resources, change proc state to
221873fbcd7SRobert Watson  * zombie, and unlink proc from allproc and parent's lists.  Save exit status
222873fbcd7SRobert Watson  * and rusage for wait().  Check for child processes and orphan them.
223df8bae1dSRodney W. Grimes  */
224fc0b1dbfSBruce Evans void
exit1(struct thread * td,int rval,int signo)225aaa92413SKonstantin Belousov exit1(struct thread *td, int rval, int signo)
226df8bae1dSRodney W. Grimes {
227a661bebeSMateusz Guzik 	struct proc *p, *nq, *q, *t;
228a661bebeSMateusz Guzik 	struct thread *tdt;
2299a2dde80SKonstantin Belousov 	ksiginfo_t *ksi, *ksi1;
230015cd8dcSMateusz Guzik 	int signal_parent;
231df8bae1dSRodney W. Grimes 
232e17660e7SKris Kennaway 	mtx_assert(&Giant, MA_NOTOWNED);
233b4490c6eSKonstantin Belousov 	KASSERT(rval == 0 || signo == 0, ("exit1 rv %d sig %d", rval, signo));
23446dd801aSColin Percival 	TSPROCEXIT(td->td_proc->p_pid);
2350cddd8f0SMatthew Dillon 
236276c5169SJohn Baldwin 	p = td->td_proc;
23728b36ecfSKonstantin Belousov 	/*
23828b36ecfSKonstantin Belousov 	 * In case we're rebooting we just let init die in order to
23928b36ecfSKonstantin Belousov 	 * work around an issues where pid 1 might get a fatal signal.
24028b36ecfSKonstantin Belousov 	 * For instance, if network interface serving NFS root is
24128b36ecfSKonstantin Belousov 	 * going down due to reboot, page-in requests for text are
24228b36ecfSKonstantin Belousov 	 * failing.
24328b36ecfSKonstantin Belousov 	 */
24428b36ecfSKonstantin Belousov 	if (p == initproc && rebooting == 0) {
245b4490c6eSKonstantin Belousov 		printf("init died (signal %d, exit %d)\n", signo, rval);
2465f7bd355SPoul-Henning Kamp 		panic("Going nowhere without my init!");
2475f7bd355SPoul-Henning Kamp 	}
2482c1011f7SJohn Dyson 
2497a6b989bSJohn Baldwin 	/*
250c6d31b83SKonstantin Belousov 	 * Process deferred operations, designated with ASTF_KCLEAR.
251c6d31b83SKonstantin Belousov 	 * For instance, we need to deref SU mp, since the thread does
252c6d31b83SKonstantin Belousov 	 * not return to userspace, and wait for geom to stabilize.
2531bc93bb7SKonstantin Belousov 	 */
254c6d31b83SKonstantin Belousov 	ast_kclear(td);
2551bc93bb7SKonstantin Belousov 
2561bc93bb7SKonstantin Belousov 	/*
2572c10d16aSJeff Roberson 	 * MUST abort all other threads before proceeding past here.
2587a6b989bSJohn Baldwin 	 */
259e602ba25SJulian Elischer 	PROC_LOCK(p);
2604493a13eSKonstantin Belousov 	proc_set_p2_wexit(p);
261d3000939SKonstantin Belousov 
262e602ba25SJulian Elischer 	/*
263aba1ca52SKonstantin Belousov 	 * First check if some other thread or external request got
264aba1ca52SKonstantin Belousov 	 * here before us.  If so, act appropriately: exit or suspend.
265aba1ca52SKonstantin Belousov 	 * We must ensure that stop requests are handled before we set
266aba1ca52SKonstantin Belousov 	 * P_WEXIT.
267e602ba25SJulian Elischer 	 */
268e602ba25SJulian Elischer 	thread_suspend_check(0);
269aba1ca52SKonstantin Belousov 	while (p->p_flag & P_HADTHREADS) {
270e602ba25SJulian Elischer 		/*
271e602ba25SJulian Elischer 		 * Kill off the other threads. This requires
2726111dcd2SJohn Baldwin 		 * some co-operation from other parts of the kernel
2736111dcd2SJohn Baldwin 		 * so it may not be instantaneous.  With this state set
27430b16a6bSKonstantin Belousov 		 * any thread attempting to interruptibly
2756111dcd2SJohn Baldwin 		 * sleep will return immediately with EINTR or EWOULDBLOCK
2766111dcd2SJohn Baldwin 		 * which will hopefully force them to back out to userland
2776111dcd2SJohn Baldwin 		 * freeing resources as they go.  Any thread attempting
27830b16a6bSKonstantin Belousov 		 * to return to userland will thread_exit() from ast().
2796111dcd2SJohn Baldwin 		 * thread_exit() will unsuspend us when the last of the
2806111dcd2SJohn Baldwin 		 * other threads exits.
281b370279eSDavid Xu 		 * If there is already a thread singler after resumption,
28230b16a6bSKonstantin Belousov 		 * calling thread_single() will fail; in that case, we just
283b370279eSDavid Xu 		 * re-check all suspension request, the thread should
284b370279eSDavid Xu 		 * either be suspended there or exit.
285e602ba25SJulian Elischer 		 */
2866ddcc233SKonstantin Belousov 		if (!thread_single(p, SINGLE_EXIT))
287e602ba25SJulian Elischer 			/*
288aba1ca52SKonstantin Belousov 			 * All other activity in this process is now
289aba1ca52SKonstantin Belousov 			 * stopped.  Threading support has been turned
290aba1ca52SKonstantin Belousov 			 * off.
291e602ba25SJulian Elischer 			 */
292aba1ca52SKonstantin Belousov 			break;
293aba1ca52SKonstantin Belousov 		/*
294aba1ca52SKonstantin Belousov 		 * Recheck for new stop or suspend requests which
295aba1ca52SKonstantin Belousov 		 * might appear while process lock was dropped in
296aba1ca52SKonstantin Belousov 		 * thread_single().
297aba1ca52SKonstantin Belousov 		 */
298aba1ca52SKonstantin Belousov 		thread_suspend_check(0);
299e602ba25SJulian Elischer 	}
3001c4bcd05SJeff Roberson 	KASSERT(p->p_numthreads == 1,
3011c4bcd05SJeff Roberson 	    ("exit1: proc %p exiting with %d threads", p, p->p_numthreads));
30258c77a9dSEdward Tomasz Napierala 	racct_sub(p, RACCT_NTHR, 1);
303b4490c6eSKonstantin Belousov 
304b4490c6eSKonstantin Belousov 	/* Let event handler change exit status */
305b4490c6eSKonstantin Belousov 	p->p_xexit = rval;
306b4490c6eSKonstantin Belousov 	p->p_xsig = signo;
307b4490c6eSKonstantin Belousov 
30806ad42b2SJohn Baldwin 	/*
3090f14f15bSJohn Baldwin 	 * Ignore any pending request to stop due to a stop signal.
3100f14f15bSJohn Baldwin 	 * Once P_WEXIT is set, future requests will be ignored as
3110f14f15bSJohn Baldwin 	 * well.
3120f14f15bSJohn Baldwin 	 */
3130f14f15bSJohn Baldwin 	p->p_flag &= ~P_STOPPED_SIG;
3140f14f15bSJohn Baldwin 	KASSERT(!P_SHOULDSTOP(p), ("exiting process is stopped"));
3150f14f15bSJohn Baldwin 
31659838c1aSJohn Baldwin 	/* Note that we are exiting. */
317e602ba25SJulian Elischer 	p->p_flag |= P_WEXIT;
31806ad42b2SJohn Baldwin 
31906ad42b2SJohn Baldwin 	/*
32006ad42b2SJohn Baldwin 	 * Wait for any processes that have a hold on our vmspace to
32106ad42b2SJohn Baldwin 	 * release their reference.
32206ad42b2SJohn Baldwin 	 */
32306ad42b2SJohn Baldwin 	while (p->p_lock > 0)
32406ad42b2SJohn Baldwin 		msleep(&p->p_lock, &p->p_mtx, PWAIT, "exithold", 0);
325ebceaf6dSDavid Xu 
326e602ba25SJulian Elischer 	PROC_UNLOCK(p);
3271c4bcd05SJeff Roberson 	/* Drain the limit callout while we don't have the proc locked */
3281c4bcd05SJeff Roberson 	callout_drain(&p->p_limco);
329b40ce416SJulian Elischer 
33000c28d96SRobert Watson #ifdef AUDIT
33100c28d96SRobert Watson 	/*
33200c28d96SRobert Watson 	 * The Sun BSM exit token contains two components: an exit status as
33300c28d96SRobert Watson 	 * passed to exit(), and a return value to indicate what sort of exit
33400c28d96SRobert Watson 	 * it was.  The exit status is WEXITSTATUS(rv), but it's not clear
33500c28d96SRobert Watson 	 * what the return value is.
33600c28d96SRobert Watson 	 */
337b4490c6eSKonstantin Belousov 	AUDIT_ARG_EXIT(rval, 0);
33800c28d96SRobert Watson 	AUDIT_SYSCALL_EXIT(0, td);
33900c28d96SRobert Watson #endif
34000c28d96SRobert Watson 
341bad2520aSMateusz Guzik 	/* Are we a task leader with peers? */
342bad2520aSMateusz Guzik 	if (p->p_peers != NULL && p == p->p_leader) {
343c6544064SJohn Baldwin 		mtx_lock(&ppeers_lock);
3442c1011f7SJohn Dyson 		q = p->p_peers;
345776e0b36SJohn Baldwin 		while (q != NULL) {
346776e0b36SJohn Baldwin 			PROC_LOCK(q);
3478451d0ddSKip Macy 			kern_psignal(q, SIGKILL);
348776e0b36SJohn Baldwin 			PROC_UNLOCK(q);
3492c1011f7SJohn Dyson 			q = q->p_peers;
3502c1011f7SJohn Dyson 		}
351c6544064SJohn Baldwin 		while (p->p_peers != NULL)
352c6544064SJohn Baldwin 			msleep(p, &ppeers_lock, PWAIT, "exit1", 0);
353c6544064SJohn Baldwin 		mtx_unlock(&ppeers_lock);
3542c1011f7SJohn Dyson 	}
3552c1011f7SJohn Dyson 
356e68c6191SKonstantin Belousov 	itimers_exit(p);
357e68c6191SKonstantin Belousov 
358fed06968SJulian Elischer 	/*
359e9189611SPeter Wemm 	 * Check if any loadable modules need anything done at process exit.
360b4490c6eSKonstantin Belousov 	 * E.g. SYSV IPC stuff.
361b4490c6eSKonstantin Belousov 	 * Event handler could change exit status.
362fed06968SJulian Elischer 	 * XXX what if one of these generates an error?
363fed06968SJulian Elischer 	 */
3642ca45184SMatt Joras 	EVENTHANDLER_DIRECT_INVOKE(process_exit, p);
365a914fb6bSJohn Baldwin 
366e3d3e828SMateusz Guzik 	/*
367e3d3e828SMateusz Guzik 	 * If parent is waiting for us to exit or exec,
368e3d3e828SMateusz Guzik 	 * P_PPWAIT is set; we will wakeup the parent below.
369e3d3e828SMateusz Guzik 	 */
370a914fb6bSJohn Baldwin 	PROC_LOCK(p);
371a282253aSJulian Elischer 	stopprofclock(p);
3728d570f64SJohn Baldwin 	p->p_ptevents = 0;
3735499ea01SJohn Baldwin 
3745499ea01SJohn Baldwin 	/*
3755499ea01SJohn Baldwin 	 * Stop the real interval timer.  If the handler is currently
3765499ea01SJohn Baldwin 	 * executing, prevent it from rearming itself and let it finish.
3775499ea01SJohn Baldwin 	 */
3785499ea01SJohn Baldwin 	if (timevalisset(&p->p_realtimer.it_value) &&
379b3191718SMark Johnston 	    callout_stop(&p->p_itcallout) == 0) {
3805499ea01SJohn Baldwin 		timevalclear(&p->p_realtimer.it_interval);
38196d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
382b3191718SMark Johnston 		callout_drain(&p->p_itcallout);
383b3191718SMark Johnston 	} else {
384b3191718SMark Johnston 		PROC_UNLOCK(p);
385b3191718SMark Johnston 	}
386df8bae1dSRodney W. Grimes 
38755976ce1SKonstantin Belousov 	if (p->p_sysent->sv_onexit != NULL)
38855976ce1SKonstantin Belousov 		p->p_sysent->sv_onexit(p);
38919d3e47dSMateusz Guzik 	seltdfini(td);
3902a339d9eSKonstantin Belousov 
391df8bae1dSRodney W. Grimes 	/*
392831d27a9SDon Lewis 	 * Reset any sigio structures pointing to us as a result of
393f5297909SMark Johnston 	 * F_SETOWN with our pid.  The P_WEXIT flag interlocks with fsetown().
394831d27a9SDon Lewis 	 */
395831d27a9SDon Lewis 	funsetownlst(&p->p_sigiolst);
396831d27a9SDon Lewis 
397831d27a9SDon Lewis 	/*
398df8bae1dSRodney W. Grimes 	 * Close open files and release open-file table.
399df8bae1dSRodney W. Grimes 	 * This may block!
400df8bae1dSRodney W. Grimes 	 */
40185078b85SConrad Meyer 	pdescfree(td);
4022609222aSPawel Jakub Dawidek 	fdescfree(td);
403df8bae1dSRodney W. Grimes 
404a914fb6bSJohn Baldwin 	/*
405a914fb6bSJohn Baldwin 	 * Remove ourself from our leader's peer list and wake our leader.
406a914fb6bSJohn Baldwin 	 */
407bad2520aSMateusz Guzik 	if (p->p_leader->p_peers != NULL) {
408c6544064SJohn Baldwin 		mtx_lock(&ppeers_lock);
409bad2520aSMateusz Guzik 		if (p->p_leader->p_peers != NULL) {
41079fc0bf4SMike Smith 			q = p->p_leader;
41179fc0bf4SMike Smith 			while (q->p_peers != p)
41279fc0bf4SMike Smith 				q = q->p_peers;
41379fc0bf4SMike Smith 			q->p_peers = p->p_peers;
4147f05b035SAlfred Perlstein 			wakeup(p->p_leader);
41579fc0bf4SMike Smith 		}
416c6544064SJohn Baldwin 		mtx_unlock(&ppeers_lock);
417bad2520aSMateusz Guzik 	}
41879fc0bf4SMike Smith 
4191c696903SKonstantin Belousov 	exec_free_abi_mappings(p);
42057051fdcSTor Egge 	vmspace_exit(td);
421b40ce416SJulian Elischer 	(void)acct_process(td);
422bc093719SEd Schouten 
423df8bae1dSRodney W. Grimes #ifdef KTRACE
4242c255e9dSRobert Watson 	ktrprocexit(td);
425df8bae1dSRodney W. Grimes #endif
426df8bae1dSRodney W. Grimes 	/*
427351d5f7fSKonstantin Belousov 	 * Release reference to text vnode etc
428ee42d0a9SDavid Malone 	 */
42988f98985SMateusz Guzik 	if (p->p_textvp != NULL) {
43088f98985SMateusz Guzik 		vrele(p->p_textvp);
431ee42d0a9SDavid Malone 		p->p_textvp = NULL;
432ee42d0a9SDavid Malone 	}
433351d5f7fSKonstantin Belousov 	if (p->p_textdvp != NULL) {
434351d5f7fSKonstantin Belousov 		vrele(p->p_textdvp);
435351d5f7fSKonstantin Belousov 		p->p_textdvp = NULL;
436351d5f7fSKonstantin Belousov 	}
437351d5f7fSKonstantin Belousov 	if (p->p_binname != NULL) {
438351d5f7fSKonstantin Belousov 		free(p->p_binname, M_PARGS);
439351d5f7fSKonstantin Belousov 		p->p_binname = NULL;
440351d5f7fSKonstantin Belousov 	}
441ee42d0a9SDavid Malone 
442ee42d0a9SDavid Malone 	/*
443d7aadbf9SJohn Baldwin 	 * Release our limits structure.
444d7aadbf9SJohn Baldwin 	 */
44588f98985SMateusz Guzik 	lim_free(p->p_limit);
446d7aadbf9SJohn Baldwin 	p->p_limit = NULL;
447d7aadbf9SJohn Baldwin 
448cf7d9a8cSDavid Xu 	tidhash_remove(td);
449cf7d9a8cSDavid Xu 
450d7aadbf9SJohn Baldwin 	/*
45181d68271SMateusz Guzik 	 * Call machine-dependent code to release any
45281d68271SMateusz Guzik 	 * machine-dependent resources other than the address space.
45381d68271SMateusz Guzik 	 * The address space is released by "vmspace_exitfree(p)" in
45481d68271SMateusz Guzik 	 * vm_waitproc().
45581d68271SMateusz Guzik 	 */
45681d68271SMateusz Guzik 	cpu_exit(td);
45781d68271SMateusz Guzik 
45881d68271SMateusz Guzik 	WITNESS_WARN(WARN_PANIC, NULL, "process (pid %d) exiting", p->p_pid);
45981d68271SMateusz Guzik 
46081d68271SMateusz Guzik 	/*
46188cc62e5SMateusz Guzik 	 * Remove from allproc. It still sits in the hash.
462df8bae1dSRodney W. Grimes 	 */
4631005a129SJohn Baldwin 	sx_xlock(&allproc_lock);
464b75356e1SJeffrey Hsu 	LIST_REMOVE(p, p_list);
46547877889SJohn Baldwin 
46647877889SJohn Baldwin #ifdef DDB
46747877889SJohn Baldwin 	/*
46847877889SJohn Baldwin 	 * Used by ddb's 'ps' command to find this process via the
46947877889SJohn Baldwin 	 * pidhash.
47047877889SJohn Baldwin 	 */
47147877889SJohn Baldwin 	p->p_list.le_prev = NULL;
47247877889SJohn Baldwin #endif
4735ecb5444SMateusz Guzik 	prison_proc_unlink(p->p_ucred->cr_prison, p);
4741005a129SJohn Baldwin 	sx_xunlock(&allproc_lock);
475df8bae1dSRodney W. Grimes 
476b1fbffe7SMateusz Guzik 	sx_xlock(&proctree_lock);
47780cf427bSMateusz Guzik 	if ((p->p_flag & (P_TRACED | P_PPWAIT | P_PPTRACE)) != 0) {
47841fadb3fSMariusz Zaborski 		PROC_LOCK(p);
47941fadb3fSMariusz Zaborski 		p->p_flag &= ~(P_TRACED | P_PPWAIT | P_PPTRACE);
48041fadb3fSMariusz Zaborski 		PROC_UNLOCK(p);
48180cf427bSMateusz Guzik 	}
482b1fbffe7SMateusz Guzik 
48337f84a60SJohn Baldwin 	/*
484c5bc28b2SKonstantin Belousov 	 * killjobc() might drop and re-acquire proctree_lock to
485c5bc28b2SKonstantin Belousov 	 * revoke control tty if exiting process was a session leader.
486c5bc28b2SKonstantin Belousov 	 */
487c5bc28b2SKonstantin Belousov 	killjobc();
488c5bc28b2SKonstantin Belousov 
489c5bc28b2SKonstantin Belousov 	/*
490a661bebeSMateusz Guzik 	 * Reparent all children processes:
491a661bebeSMateusz Guzik 	 * - traced ones to the original parent (or init if we are that parent)
492a661bebeSMateusz Guzik 	 * - the rest to init
49337f84a60SJohn Baldwin 	 */
4942e3c8fcbSPoul-Henning Kamp 	q = LIST_FIRST(&p->p_children);
495a914fb6bSJohn Baldwin 	if (q != NULL)		/* only need this if any child is S_ZOMB */
496237623b0SKonstantin Belousov 		wakeup(q->p_reaper);
497a914fb6bSJohn Baldwin 	for (; q != NULL; q = nq) {
4982e3c8fcbSPoul-Henning Kamp 		nq = LIST_NEXT(q, p_sibling);
499cc29f221SKonstantin Belousov 		ksi = ksiginfo_alloc(M_WAITOK);
500a914fb6bSJohn Baldwin 		PROC_LOCK(q);
5014ac9ae70SJulian Elischer 		q->p_sigparent = SIGCHLD;
502904c5ec4SDavid Xu 
503a05cfdf4SMariusz Zaborski 		if ((q->p_flag & P_TRACED) == 0) {
5042c054ce9SMateusz Guzik 			proc_reparent(q, q->p_reaper, true);
505545d3122SKonstantin Belousov 			if (q->p_state == PRS_ZOMBIE) {
5069a2dde80SKonstantin Belousov 				/*
5079a2dde80SKonstantin Belousov 				 * Inform reaper about the reparented
5089a2dde80SKonstantin Belousov 				 * zombie, since wait(2) has something
5099a2dde80SKonstantin Belousov 				 * new to report.  Guarantee queueing
5109a2dde80SKonstantin Belousov 				 * of the SIGCHLD signal, similar to
5119a2dde80SKonstantin Belousov 				 * the _exit() behaviour, by providing
5129a2dde80SKonstantin Belousov 				 * our ksiginfo.  Ksi is freed by the
5139a2dde80SKonstantin Belousov 				 * signal delivery.
5149a2dde80SKonstantin Belousov 				 */
5159a2dde80SKonstantin Belousov 				if (q->p_ksi == NULL) {
5169a2dde80SKonstantin Belousov 					ksi1 = NULL;
5179a2dde80SKonstantin Belousov 				} else {
5189a2dde80SKonstantin Belousov 					ksiginfo_copy(q->p_ksi, ksi);
5199a2dde80SKonstantin Belousov 					ksi->ksi_flags |= KSI_INS;
5209a2dde80SKonstantin Belousov 					ksi1 = ksi;
5219a2dde80SKonstantin Belousov 					ksi = NULL;
5229a2dde80SKonstantin Belousov 				}
523545d3122SKonstantin Belousov 				PROC_LOCK(q->p_reaper);
5249a2dde80SKonstantin Belousov 				pksignal(q->p_reaper, SIGCHLD, ksi1);
525545d3122SKonstantin Belousov 				PROC_UNLOCK(q->p_reaper);
526b9408863SKonstantin Belousov 			} else if (q->p_pdeathsig > 0) {
527b9408863SKonstantin Belousov 				/*
528b9408863SKonstantin Belousov 				 * The child asked to received a signal
529b9408863SKonstantin Belousov 				 * when we exit.
530b9408863SKonstantin Belousov 				 */
531b9408863SKonstantin Belousov 				kern_psignal(q, q->p_pdeathsig);
532545d3122SKonstantin Belousov 			}
533a661bebeSMateusz Guzik 		} else {
534a661bebeSMateusz Guzik 			/*
535fd3ac06fSKonstantin Belousov 			 * Traced processes are killed by default
536fd3ac06fSKonstantin Belousov 			 * since their existence means someone is
537fd3ac06fSKonstantin Belousov 			 * screwing up.
538a661bebeSMateusz Guzik 			 */
539a661bebeSMateusz Guzik 			t = proc_realparent(q);
540a661bebeSMateusz Guzik 			if (t == p) {
5412c054ce9SMateusz Guzik 				proc_reparent(q, q->p_reaper, true);
542a661bebeSMateusz Guzik 			} else {
543a661bebeSMateusz Guzik 				PROC_LOCK(t);
5442c054ce9SMateusz Guzik 				proc_reparent(q, t, true);
545a661bebeSMateusz Guzik 				PROC_UNLOCK(t);
546a661bebeSMateusz Guzik 			}
5475085ecb7SKonstantin Belousov 			/*
5485085ecb7SKonstantin Belousov 			 * Since q was found on our children list, the
5495085ecb7SKonstantin Belousov 			 * proc_reparent() call moved q to the orphan
5505085ecb7SKonstantin Belousov 			 * list due to present P_TRACED flag. Clear
5515085ecb7SKonstantin Belousov 			 * orphan link for q now while q is locked.
5525085ecb7SKonstantin Belousov 			 */
5539db97ca0SMariusz Zaborski 			proc_clear_orphan(q);
554fd3ac06fSKonstantin Belousov 			q->p_flag &= ~P_TRACED;
555b7a25e63SKonstantin Belousov 			q->p_flag2 &= ~P2_PTRACE_FSTP;
5568d570f64SJohn Baldwin 			q->p_ptevents = 0;
557fd3ac06fSKonstantin Belousov 			p->p_xthread = NULL;
558b7a25e63SKonstantin Belousov 			FOREACH_THREAD_IN_PROC(q, tdt) {
559b7a25e63SKonstantin Belousov 				tdt->td_dbgflags &= ~(TDB_SUSPEND | TDB_XSIG |
560b7a25e63SKonstantin Belousov 				    TDB_FSTP);
561fd3ac06fSKonstantin Belousov 				tdt->td_xsig = 0;
562b7a25e63SKonstantin Belousov 			}
563fd3ac06fSKonstantin Belousov 			if (kern_kill_on_dbg_exit) {
564fd3ac06fSKonstantin Belousov 				q->p_flag &= ~P_STOPPED_TRACE;
5658451d0ddSKip Macy 				kern_psignal(q, SIGKILL);
566fd3ac06fSKonstantin Belousov 			} else if ((q->p_flag & (P_STOPPED_TRACE |
567fd3ac06fSKonstantin Belousov 			    P_STOPPED_SIG)) != 0) {
568fd3ac06fSKonstantin Belousov 				sigqueue_delete_proc(q, SIGTRAP);
569fd3ac06fSKonstantin Belousov 				ptrace_unsuspend(q);
570fd3ac06fSKonstantin Belousov 			}
571c65437a3SJohn Baldwin 		}
572a914fb6bSJohn Baldwin 		PROC_UNLOCK(q);
5739a2dde80SKonstantin Belousov 		if (ksi != NULL)
5749a2dde80SKonstantin Belousov 			ksiginfo_free(ksi);
575df8bae1dSRodney W. Grimes 	}
576df8bae1dSRodney W. Grimes 
5775085ecb7SKonstantin Belousov 	/*
5785085ecb7SKonstantin Belousov 	 * Also get rid of our orphans.
5795085ecb7SKonstantin Belousov 	 */
5805085ecb7SKonstantin Belousov 	while ((q = LIST_FIRST(&p->p_orphans)) != NULL) {
5815085ecb7SKonstantin Belousov 		PROC_LOCK(q);
582128c9bc0SMark Johnston 		KASSERT(q->p_oppid == p->p_pid,
583128c9bc0SMark Johnston 		    ("orphan %p of %p has unexpected oppid %d", q, p,
584128c9bc0SMark Johnston 		    q->p_oppid));
585128c9bc0SMark Johnston 		q->p_oppid = q->p_reaper->p_pid;
586128c9bc0SMark Johnston 
587b9408863SKonstantin Belousov 		/*
588b9408863SKonstantin Belousov 		 * If we are the real parent of this process
589b9408863SKonstantin Belousov 		 * but it has been reparented to a debugger, then
590b9408863SKonstantin Belousov 		 * check if it asked for a signal when we exit.
591b9408863SKonstantin Belousov 		 */
592b9408863SKonstantin Belousov 		if (q->p_pdeathsig > 0)
593b9408863SKonstantin Belousov 			kern_psignal(q, q->p_pdeathsig);
594515b7a0bSJohn Baldwin 		CTR2(KTR_PTRACE, "exit: pid %d, clearing orphan %d", p->p_pid,
595515b7a0bSJohn Baldwin 		    q->p_pid);
5969db97ca0SMariusz Zaborski 		proc_clear_orphan(q);
5975085ecb7SKonstantin Belousov 		PROC_UNLOCK(q);
5985085ecb7SKonstantin Belousov 	}
5995085ecb7SKonstantin Belousov 
600eadb1dcbSMateusz Guzik #ifdef KDTRACE_HOOKS
601eadb1dcbSMateusz Guzik 	if (SDT_PROBES_ENABLED()) {
602eadb1dcbSMateusz Guzik 		int reason = CLD_EXITED;
603eadb1dcbSMateusz Guzik 		if (WCOREDUMP(signo))
604eadb1dcbSMateusz Guzik 			reason = CLD_DUMPED;
605eadb1dcbSMateusz Guzik 		else if (WIFSIGNALED(signo))
606eadb1dcbSMateusz Guzik 			reason = CLD_KILLED;
607eadb1dcbSMateusz Guzik 		SDT_PROBE1(proc, , , exit, reason);
608eadb1dcbSMateusz Guzik 	}
609eadb1dcbSMateusz Guzik #endif
610eadb1dcbSMateusz Guzik 
6111c4bcd05SJeff Roberson 	/* Save exit status. */
612d7aadbf9SJohn Baldwin 	PROC_LOCK(p);
613cbf4e354SDavid Xu 	p->p_xthread = td;
6145d217f17SJohn Birrell 
61587a9b18dSKonstantin Belousov 	if (p->p_sysent->sv_ontdexit != NULL)
61687a9b18dSKonstantin Belousov 		p->p_sysent->sv_ontdexit(td);
61787a9b18dSKonstantin Belousov 
6185d217f17SJohn Birrell #ifdef KDTRACE_HOOKS
6195d217f17SJohn Birrell 	/*
6205d217f17SJohn Birrell 	 * Tell the DTrace fasttrap provider about the exit if it
6215d217f17SJohn Birrell 	 * has declared an interest.
6225d217f17SJohn Birrell 	 */
6235d217f17SJohn Birrell 	if (dtrace_fasttrap_exit)
6245d217f17SJohn Birrell 		dtrace_fasttrap_exit(p);
6255d217f17SJohn Birrell #endif
6265d217f17SJohn Birrell 
6271c4bcd05SJeff Roberson 	/*
6287a6b989bSJohn Baldwin 	 * Notify interested parties of our demise.
629cb679c38SJonathan Lemon 	 */
6309e590ff0SKonstantin Belousov 	KNOTE_LOCKED(p->p_klist, NOTE_EXIT);
6317eaec467SJohn Baldwin 
6321a29c806SOlivier Houchard 	/*
633cfb5f768SJonathan Anderson 	 * If this is a process with a descriptor, we may not need to deliver
634cfb5f768SJonathan Anderson 	 * a signal to the parent.  proctree_lock is held over
635cfb5f768SJonathan Anderson 	 * procdesc_exit() to serialize concurrent calls to close() and
636cfb5f768SJonathan Anderson 	 * exit().
637cfb5f768SJonathan Anderson 	 */
638015cd8dcSMateusz Guzik 	signal_parent = 0;
639cfb5f768SJonathan Anderson 	if (p->p_procdesc == NULL || procdesc_exit(p)) {
640cfb5f768SJonathan Anderson 		/*
641cfb5f768SJonathan Anderson 		 * Notify parent that we're gone.  If parent has the
642cfb5f768SJonathan Anderson 		 * PS_NOCLDWAIT flag set, or if the handler is set to SIG_IGN,
643cfb5f768SJonathan Anderson 		 * notify process 1 instead (and hope it will handle this
644cfb5f768SJonathan Anderson 		 * situation).
645df8bae1dSRodney W. Grimes 		 */
646d7aadbf9SJohn Baldwin 		PROC_LOCK(p->p_pptr);
64790af4afaSJohn Baldwin 		mtx_lock(&p->p_pptr->p_sigacts->ps_mtx);
648cfb5f768SJonathan Anderson 		if (p->p_pptr->p_sigacts->ps_flag &
649cfb5f768SJonathan Anderson 		    (PS_NOCLDWAIT | PS_CLDSIGIGN)) {
650276c5169SJohn Baldwin 			struct proc *pp;
651276c5169SJohn Baldwin 
65290af4afaSJohn Baldwin 			mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx);
653276c5169SJohn Baldwin 			pp = p->p_pptr;
654f591779bSSeigo Tanimura 			PROC_UNLOCK(pp);
6552c054ce9SMateusz Guzik 			proc_reparent(p, p->p_reaper, true);
65655b5f2a2SDon Lewis 			p->p_sigparent = SIGCHLD;
657f591779bSSeigo Tanimura 			PROC_LOCK(p->p_pptr);
658007abb3dSKonstantin Belousov 
659245f17d4SJoerg Wunsch 			/*
660007abb3dSKonstantin Belousov 			 * Notify parent, so in case he was wait(2)ing or
6616fae832aSKonstantin Belousov 			 * executing waitpid(2) with our pid, he will
662245f17d4SJoerg Wunsch 			 * continue.
663245f17d4SJoerg Wunsch 			 */
6647f05b035SAlfred Perlstein 			wakeup(pp);
66590af4afaSJohn Baldwin 		} else
66690af4afaSJohn Baldwin 			mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx);
667245f17d4SJoerg Wunsch 
668015cd8dcSMateusz Guzik 		if (p->p_pptr == p->p_reaper || p->p_pptr == initproc) {
669015cd8dcSMateusz Guzik 			signal_parent = 1;
670015cd8dcSMateusz Guzik 		} else if (p->p_sigparent != 0) {
671015cd8dcSMateusz Guzik 			if (p->p_sigparent == SIGCHLD) {
672015cd8dcSMateusz Guzik 				signal_parent = 1;
673015cd8dcSMateusz Guzik 			} else { /* LINUX thread */
674015cd8dcSMateusz Guzik 				signal_parent = 2;
675015cd8dcSMateusz Guzik 			}
676ebceaf6dSDavid Xu 		}
677cfb5f768SJonathan Anderson 	} else
678cfb5f768SJonathan Anderson 		PROC_LOCK(p->p_pptr);
679d7aadbf9SJohn Baldwin 	sx_xunlock(&proctree_lock);
680696058c3SJulian Elischer 
681015cd8dcSMateusz Guzik 	if (signal_parent == 1) {
682015cd8dcSMateusz Guzik 		childproc_exited(p);
683015cd8dcSMateusz Guzik 	} else if (signal_parent == 2) {
684015cd8dcSMateusz Guzik 		kern_psignal(p->p_pptr, p->p_sigparent);
685015cd8dcSMateusz Guzik 	}
686015cd8dcSMateusz Guzik 
6877e588b92SMateusz Guzik 	/* Tell the prison that we are gone. */
6887e588b92SMateusz Guzik 	prison_proc_free(p->p_ucred->cr_prison);
6897e588b92SMateusz Guzik 
690eb30c1c0SPeter Wemm 	/*
691452661c9SIgor Ostapenko 	 * The state PRS_ZOMBIE prevents other processes from sending
692f71e748dSDavid Xu 	 * signal to the process, to avoid memory leak, we free memory
693f71e748dSDavid Xu 	 * for signal queue at the time when the state is set.
694f71e748dSDavid Xu 	 */
695f71e748dSDavid Xu 	sigqueue_flush(&p->p_sigqueue);
696f71e748dSDavid Xu 	sigqueue_flush(&td->td_sigqueue);
697f71e748dSDavid Xu 
698f71e748dSDavid Xu 	/*
699462f31bfSJohn Baldwin 	 * We have to wait until after acquiring all locks before
700a9a64385SJohn Baldwin 	 * changing p_state.  We need to avoid all possible context
701a9a64385SJohn Baldwin 	 * switches (including ones from blocking on a mutex) while
702ddf9c4f7SJohn Baldwin 	 * marked as a zombie.  We also have to set the zombie state
703ddf9c4f7SJohn Baldwin 	 * before we release the parent process' proc lock to avoid
704ddf9c4f7SJohn Baldwin 	 * a lost wakeup.  So, we first call wakeup, then we grab the
705ddf9c4f7SJohn Baldwin 	 * sched lock, update the state, and release the parent process'
706ddf9c4f7SJohn Baldwin 	 * proc lock.
707eb30c1c0SPeter Wemm 	 */
708ddf9c4f7SJohn Baldwin 	wakeup(p->p_pptr);
709e3d3e828SMateusz Guzik 	cv_broadcast(&p->p_pwait);
710982d11f8SJeff Roberson 	sched_exit(p->p_pptr, td);
711982d11f8SJeff Roberson 	PROC_SLOCK(p);
712e602ba25SJulian Elischer 	p->p_state = PRS_ZOMBIE;
713d7aadbf9SJohn Baldwin 	PROC_UNLOCK(p->p_pptr);
714871684b8SBruce Evans 
715f6f230feSJeff Roberson 	/*
716a140976eSAttilio Rao 	 * Save our children's rusage information in our exit rusage.
717a140976eSAttilio Rao 	 */
7185c7bebf9SKonstantin Belousov 	PROC_STATLOCK(p);
719a140976eSAttilio Rao 	ruadd(&p->p_ru, &p->p_rux, &p->p_stats->p_cru, &p->p_crux);
7205c7bebf9SKonstantin Belousov 	PROC_STATUNLOCK(p);
721a140976eSAttilio Rao 
722a140976eSAttilio Rao 	/*
723696058c3SJulian Elischer 	 * Make sure the scheduler takes this thread out of its tables etc.
724e602ba25SJulian Elischer 	 * This will also release this thread's reference to the ucred.
725696058c3SJulian Elischer 	 * Other thread parts to release include pcb bits and such.
726e602ba25SJulian Elischer 	 */
727e602ba25SJulian Elischer 	thread_exit();
728df8bae1dSRodney W. Grimes }
729df8bae1dSRodney W. Grimes 
73025f6e35aSPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_
73125f6e35aSPoul-Henning Kamp struct abort2_args {
73225f6e35aSPoul-Henning Kamp 	char *why;
73325f6e35aSPoul-Henning Kamp 	int nargs;
73425f6e35aSPoul-Henning Kamp 	void **args;
73525f6e35aSPoul-Henning Kamp };
73625f6e35aSPoul-Henning Kamp #endif
73725f6e35aSPoul-Henning Kamp 
73825f6e35aSPoul-Henning Kamp int
sys_abort2(struct thread * td,struct abort2_args * uap)7398451d0ddSKip Macy sys_abort2(struct thread *td, struct abort2_args *uap)
74025f6e35aSPoul-Henning Kamp {
741e02f64d9SBrooks Davis 	void *uargs[16];
742e02f64d9SBrooks Davis 	void **uargsp;
743e02f64d9SBrooks Davis 	int error, nargs;
744e02f64d9SBrooks Davis 
745e02f64d9SBrooks Davis 	nargs = uap->nargs;
746e02f64d9SBrooks Davis 	if (nargs < 0 || nargs > nitems(uargs))
747e02f64d9SBrooks Davis 		nargs = -1;
748e02f64d9SBrooks Davis 	uargsp = NULL;
749e02f64d9SBrooks Davis 	if (nargs > 0) {
750e02f64d9SBrooks Davis 		if (uap->args != NULL) {
751e02f64d9SBrooks Davis 			error = copyin(uap->args, uargs,
752e02f64d9SBrooks Davis 			    nargs * sizeof(void *));
753e02f64d9SBrooks Davis 			if (error != 0)
754e02f64d9SBrooks Davis 				nargs = -1;
755e02f64d9SBrooks Davis 			else
756e02f64d9SBrooks Davis 				uargsp = uargs;
757e02f64d9SBrooks Davis 		} else
758e02f64d9SBrooks Davis 			nargs = -1;
759e02f64d9SBrooks Davis 	}
760e02f64d9SBrooks Davis 	return (kern_abort2(td, uap->why, nargs, uargsp));
761e02f64d9SBrooks Davis }
762e02f64d9SBrooks Davis 
763e02f64d9SBrooks Davis /*
764e02f64d9SBrooks Davis  * kern_abort2()
765e02f64d9SBrooks Davis  * Arguments:
766e02f64d9SBrooks Davis  *  why - user pointer to why
76749a033d8SGordon Bergling  *  nargs - number of arguments copied or -1 if an error occurred in copying
768e02f64d9SBrooks Davis  *  args - pointer to an array of pointers in kernel format
769e02f64d9SBrooks Davis  */
770e02f64d9SBrooks Davis int
kern_abort2(struct thread * td,const char * why,int nargs,void ** uargs)771e02f64d9SBrooks Davis kern_abort2(struct thread *td, const char *why, int nargs, void **uargs)
772e02f64d9SBrooks Davis {
77325f6e35aSPoul-Henning Kamp 	struct proc *p = td->td_proc;
77425f6e35aSPoul-Henning Kamp 	struct sbuf *sb;
77525f6e35aSPoul-Henning Kamp 	int error, i, sig;
77625f6e35aSPoul-Henning Kamp 
77725f6e35aSPoul-Henning Kamp 	/*
77825f6e35aSPoul-Henning Kamp 	 * Do it right now so we can log either proper call of abort2(), or
77925f6e35aSPoul-Henning Kamp 	 * note, that invalid argument was passed. 512 is big enough to
78025f6e35aSPoul-Henning Kamp 	 * handle 16 arguments' descriptions with additional comments.
78125f6e35aSPoul-Henning Kamp 	 */
78225f6e35aSPoul-Henning Kamp 	sb = sbuf_new(NULL, NULL, 512, SBUF_FIXEDLEN);
78325f6e35aSPoul-Henning Kamp 	sbuf_clear(sb);
78425f6e35aSPoul-Henning Kamp 	sbuf_printf(sb, "%s(pid %d uid %d) aborted: ",
78525f6e35aSPoul-Henning Kamp 	    p->p_comm, p->p_pid, td->td_ucred->cr_uid);
78625f6e35aSPoul-Henning Kamp 	/*
78725f6e35aSPoul-Henning Kamp 	 * Since we can't return from abort2(), send SIGKILL in cases, where
78825f6e35aSPoul-Henning Kamp 	 * abort2() was called improperly
78925f6e35aSPoul-Henning Kamp 	 */
79025f6e35aSPoul-Henning Kamp 	sig = SIGKILL;
79125f6e35aSPoul-Henning Kamp 	/* Prevent from DoSes from user-space. */
792e02f64d9SBrooks Davis 	if (nargs == -1)
79325f6e35aSPoul-Henning Kamp 		goto out;
794e02f64d9SBrooks Davis 	KASSERT(nargs >= 0 && nargs <= 16, ("called with too many args (%d)",
795e02f64d9SBrooks Davis 	    nargs));
79625f6e35aSPoul-Henning Kamp 	/*
79725f6e35aSPoul-Henning Kamp 	 * Limit size of 'reason' string to 128. Will fit even when
79825f6e35aSPoul-Henning Kamp 	 * maximal number of arguments was chosen to be logged.
79925f6e35aSPoul-Henning Kamp 	 */
800e02f64d9SBrooks Davis 	if (why != NULL) {
801e02f64d9SBrooks Davis 		error = sbuf_copyin(sb, why, 128);
80225f6e35aSPoul-Henning Kamp 		if (error < 0)
80325f6e35aSPoul-Henning Kamp 			goto out;
80425f6e35aSPoul-Henning Kamp 	} else {
8050a713948SAlexander Motin 		sbuf_cat(sb, "(null)");
80625f6e35aSPoul-Henning Kamp 	}
807e02f64d9SBrooks Davis 	if (nargs > 0) {
8080a713948SAlexander Motin 		sbuf_putc(sb, '(');
809e02f64d9SBrooks Davis 		for (i = 0;i < nargs; i++)
81025f6e35aSPoul-Henning Kamp 			sbuf_printf(sb, "%s%p", i == 0 ? "" : ", ", uargs[i]);
8110a713948SAlexander Motin 		sbuf_putc(sb, ')');
81225f6e35aSPoul-Henning Kamp 	}
81325f6e35aSPoul-Henning Kamp 	/*
81425f6e35aSPoul-Henning Kamp 	 * Final stage: arguments were proper, string has been
81525f6e35aSPoul-Henning Kamp 	 * successfully copied from userspace, and copying pointers
81625f6e35aSPoul-Henning Kamp 	 * from user-space succeed.
81725f6e35aSPoul-Henning Kamp 	 */
81825f6e35aSPoul-Henning Kamp 	sig = SIGABRT;
81925f6e35aSPoul-Henning Kamp out:
82025f6e35aSPoul-Henning Kamp 	if (sig == SIGKILL) {
82125f6e35aSPoul-Henning Kamp 		sbuf_trim(sb);
8220a713948SAlexander Motin 		sbuf_cat(sb, " (Reason text inaccessible)");
82325f6e35aSPoul-Henning Kamp 	}
82425f6e35aSPoul-Henning Kamp 	sbuf_cat(sb, "\n");
82525f6e35aSPoul-Henning Kamp 	sbuf_finish(sb);
82625f6e35aSPoul-Henning Kamp 	log(LOG_INFO, "%s", sbuf_data(sb));
82725f6e35aSPoul-Henning Kamp 	sbuf_delete(sb);
8289b57e30cSJohn Baldwin 	PROC_LOCK(p);
8299b57e30cSJohn Baldwin 	sigexit(td, sig);
8309b57e30cSJohn Baldwin 	/* NOTREACHED */
83125f6e35aSPoul-Henning Kamp }
83225f6e35aSPoul-Henning Kamp 
833b2f9e8b1SBruce Evans #ifdef COMPAT_43
834234216efSMatthew Dillon /*
835a9a64385SJohn Baldwin  * The dirty work is handled by kern_wait().
836234216efSMatthew Dillon  */
83726f9a767SRodney W. Grimes int
owait(struct thread * td,struct owait_args * uap __unused)838830c3153SDag-Erling Smørgrav owait(struct thread *td, struct owait_args *uap __unused)
839df8bae1dSRodney W. Grimes {
840b7e23e82SJohn Baldwin 	int error, status;
841df8bae1dSRodney W. Grimes 
842b7e23e82SJohn Baldwin 	error = kern_wait(td, WAIT_ANY, &status, 0, NULL);
843b7e23e82SJohn Baldwin 	if (error == 0)
844b7e23e82SJohn Baldwin 		td->td_retval[1] = status;
845b7e23e82SJohn Baldwin 	return (error);
846df8bae1dSRodney W. Grimes }
847b2f9e8b1SBruce Evans #endif /* COMPAT_43 */
848df8bae1dSRodney W. Grimes 
849234216efSMatthew Dillon /*
850a9a64385SJohn Baldwin  * The dirty work is handled by kern_wait().
851234216efSMatthew Dillon  */
85226f9a767SRodney W. Grimes int
sys_wait4(struct thread * td,struct wait4_args * uap)853f13b5a0fSKonstantin Belousov sys_wait4(struct thread *td, struct wait4_args *uap)
854df8bae1dSRodney W. Grimes {
85578c85e8dSJohn Baldwin 	struct rusage ru, *rup;
856b7e23e82SJohn Baldwin 	int error, status;
857b40ce416SJulian Elischer 
85878c85e8dSJohn Baldwin 	if (uap->rusage != NULL)
85978c85e8dSJohn Baldwin 		rup = &ru;
86078c85e8dSJohn Baldwin 	else
86178c85e8dSJohn Baldwin 		rup = NULL;
86278c85e8dSJohn Baldwin 	error = kern_wait(td, uap->pid, &status, uap->options, rup);
863d30e66e5SJilles Tjoelker 	if (uap->status != NULL && error == 0 && td->td_retval[0] != 0)
864b7e23e82SJohn Baldwin 		error = copyout(&status, uap->status, sizeof(status));
865d30e66e5SJilles Tjoelker 	if (uap->rusage != NULL && error == 0 && td->td_retval[0] != 0)
866b7e23e82SJohn Baldwin 		error = copyout(&ru, uap->rusage, sizeof(struct rusage));
867b7e23e82SJohn Baldwin 	return (error);
868df8bae1dSRodney W. Grimes }
869df8bae1dSRodney W. Grimes 
870f13b5a0fSKonstantin Belousov int
sys_wait6(struct thread * td,struct wait6_args * uap)871f13b5a0fSKonstantin Belousov sys_wait6(struct thread *td, struct wait6_args *uap)
872f13b5a0fSKonstantin Belousov {
873f13b5a0fSKonstantin Belousov 	struct __wrusage wru, *wrup;
874f13b5a0fSKonstantin Belousov 	siginfo_t si, *sip;
875f13b5a0fSKonstantin Belousov 	idtype_t idtype;
876f13b5a0fSKonstantin Belousov 	id_t id;
877a2a85596SKonstantin Belousov 	int error, status;
878f13b5a0fSKonstantin Belousov 
879f13b5a0fSKonstantin Belousov 	idtype = uap->idtype;
880f13b5a0fSKonstantin Belousov 	id = uap->id;
881f13b5a0fSKonstantin Belousov 
882f13b5a0fSKonstantin Belousov 	if (uap->wrusage != NULL)
883f13b5a0fSKonstantin Belousov 		wrup = &wru;
884f13b5a0fSKonstantin Belousov 	else
885f13b5a0fSKonstantin Belousov 		wrup = NULL;
886f13b5a0fSKonstantin Belousov 
887f13b5a0fSKonstantin Belousov 	if (uap->info != NULL) {
888f13b5a0fSKonstantin Belousov 		sip = &si;
889f13b5a0fSKonstantin Belousov 		bzero(sip, sizeof(*sip));
890f13b5a0fSKonstantin Belousov 	} else
891f13b5a0fSKonstantin Belousov 		sip = NULL;
892f13b5a0fSKonstantin Belousov 
893f13b5a0fSKonstantin Belousov 	/*
894f13b5a0fSKonstantin Belousov 	 *  We expect all callers of wait6() to know about WEXITED and
895f13b5a0fSKonstantin Belousov 	 *  WTRAPPED.
896f13b5a0fSKonstantin Belousov 	 */
897f13b5a0fSKonstantin Belousov 	error = kern_wait6(td, idtype, id, &status, uap->options, wrup, sip);
898f13b5a0fSKonstantin Belousov 
899d30e66e5SJilles Tjoelker 	if (uap->status != NULL && error == 0 && td->td_retval[0] != 0)
900f13b5a0fSKonstantin Belousov 		error = copyout(&status, uap->status, sizeof(status));
901d30e66e5SJilles Tjoelker 	if (uap->wrusage != NULL && error == 0 && td->td_retval[0] != 0)
902f13b5a0fSKonstantin Belousov 		error = copyout(&wru, uap->wrusage, sizeof(wru));
903f13b5a0fSKonstantin Belousov 	if (uap->info != NULL && error == 0)
904f13b5a0fSKonstantin Belousov 		error = copyout(&si, uap->info, sizeof(si));
905f13b5a0fSKonstantin Belousov 	return (error);
906f13b5a0fSKonstantin Belousov }
907f13b5a0fSKonstantin Belousov 
908324fb6beSRobert Watson /*
909324fb6beSRobert Watson  * Reap the remains of a zombie process and optionally return status and
910324fb6beSRobert Watson  * rusage.  Asserts and will release both the proctree_lock and the process
911324fb6beSRobert Watson  * lock as part of its work.
912324fb6beSRobert Watson  */
913cfb5f768SJonathan Anderson void
proc_reap(struct thread * td,struct proc * p,int * status,int options)914f13b5a0fSKonstantin Belousov proc_reap(struct thread *td, struct proc *p, int *status, int options)
915324fb6beSRobert Watson {
916324fb6beSRobert Watson 	struct proc *q, *t;
917324fb6beSRobert Watson 
918324fb6beSRobert Watson 	sx_assert(&proctree_lock, SA_XLOCKED);
919324fb6beSRobert Watson 	PROC_LOCK_ASSERT(p, MA_OWNED);
920324fb6beSRobert Watson 	KASSERT(p->p_state == PRS_ZOMBIE, ("proc_reap: !PRS_ZOMBIE"));
921324fb6beSRobert Watson 
9222ca66c1eSMateusz Guzik 	mtx_spin_wait_unlocked(&p->p_slock);
9232ca66c1eSMateusz Guzik 
924324fb6beSRobert Watson 	q = td->td_proc;
925f13b5a0fSKonstantin Belousov 
926324fb6beSRobert Watson 	if (status)
927b4490c6eSKonstantin Belousov 		*status = KW_EXITCODE(p->p_xexit, p->p_xsig);
928324fb6beSRobert Watson 	if (options & WNOWAIT) {
929324fb6beSRobert Watson 		/*
930324fb6beSRobert Watson 		 *  Only poll, returning the status.  Caller does not wish to
931324fb6beSRobert Watson 		 * release the proc struct just yet.
932324fb6beSRobert Watson 		 */
933324fb6beSRobert Watson 		PROC_UNLOCK(p);
934324fb6beSRobert Watson 		sx_xunlock(&proctree_lock);
935324fb6beSRobert Watson 		return;
936324fb6beSRobert Watson 	}
937324fb6beSRobert Watson 
938324fb6beSRobert Watson 	PROC_LOCK(q);
939324fb6beSRobert Watson 	sigqueue_take(p->p_ksi);
940324fb6beSRobert Watson 	PROC_UNLOCK(q);
941324fb6beSRobert Watson 
942324fb6beSRobert Watson 	/*
943324fb6beSRobert Watson 	 * If we got the child via a ptrace 'attach', we need to give it back
944324fb6beSRobert Watson 	 * to the old parent.
945324fb6beSRobert Watson 	 */
9462c054ce9SMateusz Guzik 	if (p->p_oppid != p->p_pptr->p_pid) {
947c209e3e2SJohn Baldwin 		PROC_UNLOCK(p);
948d7359980SKonstantin Belousov 		t = proc_realparent(p);
949d7359980SKonstantin Belousov 		PROC_LOCK(t);
950324fb6beSRobert Watson 		PROC_LOCK(p);
951515b7a0bSJohn Baldwin 		CTR2(KTR_PTRACE,
952515b7a0bSJohn Baldwin 		    "wait: traced child %d moved back to parent %d", p->p_pid,
953515b7a0bSJohn Baldwin 		    t->p_pid);
9542c054ce9SMateusz Guzik 		proc_reparent(p, t, false);
955324fb6beSRobert Watson 		PROC_UNLOCK(p);
956ad6eec7bSJohn Baldwin 		pksignal(t, SIGCHLD, p->p_ksi);
957324fb6beSRobert Watson 		wakeup(t);
958324fb6beSRobert Watson 		cv_broadcast(&p->p_pwait);
959324fb6beSRobert Watson 		PROC_UNLOCK(t);
960324fb6beSRobert Watson 		sx_xunlock(&proctree_lock);
961324fb6beSRobert Watson 		return;
962324fb6beSRobert Watson 	}
963c209e3e2SJohn Baldwin 	PROC_UNLOCK(p);
964324fb6beSRobert Watson 
965324fb6beSRobert Watson 	/*
966324fb6beSRobert Watson 	 * Remove other references to this process to ensure we have an
967324fb6beSRobert Watson 	 * exclusive reference.
968324fb6beSRobert Watson 	 */
9693d3e6793SMateusz Guzik 	sx_xlock(PIDHASHLOCK(p->p_pid));
9703d3e6793SMateusz Guzik 	LIST_REMOVE(p, p_hash);
9713d3e6793SMateusz Guzik 	sx_xunlock(PIDHASHLOCK(p->p_pid));
972324fb6beSRobert Watson 	LIST_REMOVE(p, p_sibling);
973237623b0SKonstantin Belousov 	reaper_abandon_children(p, true);
97434ebdceaSMateusz Guzik 	reaper_clear(p);
9757335ed90SKonstantin Belousov 	PROC_LOCK(p);
9769db97ca0SMariusz Zaborski 	proc_clear_orphan(p);
9777335ed90SKonstantin Belousov 	PROC_UNLOCK(p);
978324fb6beSRobert Watson 	leavepgrp(p);
979cfb5f768SJonathan Anderson 	if (p->p_procdesc != NULL)
980cfb5f768SJonathan Anderson 		procdesc_reap(p);
981324fb6beSRobert Watson 	sx_xunlock(&proctree_lock);
982324fb6beSRobert Watson 
9839576ff58SMateusz Guzik 	proc_id_clear(PROC_ID_PID, p->p_pid);
9849576ff58SMateusz Guzik 
9859e590ff0SKonstantin Belousov 	PROC_LOCK(p);
9869e590ff0SKonstantin Belousov 	knlist_detach(p->p_klist);
9879e590ff0SKonstantin Belousov 	p->p_klist = NULL;
9889e590ff0SKonstantin Belousov 	PROC_UNLOCK(p);
9899e590ff0SKonstantin Belousov 
990324fb6beSRobert Watson 	/*
991fbe503d4SMateusz Guzik 	 * Removal from allproc list and process group list paired with
992fbe503d4SMateusz Guzik 	 * PROC_LOCK which was executed during that time should guarantee
993fbe503d4SMateusz Guzik 	 * nothing can reach this process anymore. As such further locking
994fbe503d4SMateusz Guzik 	 * is unnecessary.
995324fb6beSRobert Watson 	 */
996b4490c6eSKonstantin Belousov 	p->p_xexit = p->p_xsig = 0;		/* XXX: why? */
997fbe503d4SMateusz Guzik 
998324fb6beSRobert Watson 	PROC_LOCK(q);
999324fb6beSRobert Watson 	ruadd(&q->p_stats->p_cru, &q->p_crux, &p->p_ru, &p->p_rux);
1000324fb6beSRobert Watson 	PROC_UNLOCK(q);
1001324fb6beSRobert Watson 
1002324fb6beSRobert Watson 	/*
1003324fb6beSRobert Watson 	 * Decrement the count of procs running with this uid.
1004324fb6beSRobert Watson 	 */
1005324fb6beSRobert Watson 	(void)chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0);
1006324fb6beSRobert Watson 
1007324fb6beSRobert Watson 	/*
1008097055e2SEdward Tomasz Napierala 	 * Destroy resource accounting information associated with the process.
1009097055e2SEdward Tomasz Napierala 	 */
1010afcc55f3SEdward Tomasz Napierala #ifdef RACCT
10114b5c9cf6SEdward Tomasz Napierala 	if (racct_enable) {
1012b38520f0SEdward Tomasz Napierala 		PROC_LOCK(p);
1013b38520f0SEdward Tomasz Napierala 		racct_sub(p, RACCT_NPROC, 1);
1014b38520f0SEdward Tomasz Napierala 		PROC_UNLOCK(p);
10154b5c9cf6SEdward Tomasz Napierala 	}
1016afcc55f3SEdward Tomasz Napierala #endif
1017b38520f0SEdward Tomasz Napierala 	racct_proc_exit(p);
1018097055e2SEdward Tomasz Napierala 
1019097055e2SEdward Tomasz Napierala 	/*
1020324fb6beSRobert Watson 	 * Free credentials, arguments, and sigacts.
1021324fb6beSRobert Watson 	 */
10225a90435cSMateusz Guzik 	proc_unset_cred(p);
1023324fb6beSRobert Watson 	pargs_drop(p->p_args);
1024324fb6beSRobert Watson 	p->p_args = NULL;
1025324fb6beSRobert Watson 	sigacts_free(p->p_sigacts);
1026324fb6beSRobert Watson 	p->p_sigacts = NULL;
1027324fb6beSRobert Watson 
1028324fb6beSRobert Watson 	/*
1029324fb6beSRobert Watson 	 * Do any thread-system specific cleanups.
1030324fb6beSRobert Watson 	 */
1031324fb6beSRobert Watson 	thread_wait(p);
1032324fb6beSRobert Watson 
1033324fb6beSRobert Watson 	/*
1034324fb6beSRobert Watson 	 * Give vm and machine-dependent layer a chance to free anything that
1035324fb6beSRobert Watson 	 * cpu_exit couldn't release while still running in process context.
1036324fb6beSRobert Watson 	 */
1037324fb6beSRobert Watson 	vm_waitproc(p);
1038324fb6beSRobert Watson #ifdef MAC
1039324fb6beSRobert Watson 	mac_proc_destroy(p);
1040324fb6beSRobert Watson #endif
10416520495aSAdrian Chadd 
1042324fb6beSRobert Watson 	KASSERT(FIRST_THREAD_IN_PROC(p),
1043324fb6beSRobert Watson 	    ("proc_reap: no residual thread!"));
1044324fb6beSRobert Watson 	uma_zfree(proc_zone, p);
10454b48959fSKonstantin Belousov 	atomic_add_int(&nprocs, -1);
1046324fb6beSRobert Watson }
1047324fb6beSRobert Watson 
1048dcd43281SKonstantin Belousov static int
proc_to_reap(struct thread * td,struct proc * p,idtype_t idtype,id_t id,int * status,int options,struct __wrusage * wrusage,siginfo_t * siginfo,int check_only)1049f13b5a0fSKonstantin Belousov proc_to_reap(struct thread *td, struct proc *p, idtype_t idtype, id_t id,
105057c74f5bSJohn Baldwin     int *status, int options, struct __wrusage *wrusage, siginfo_t *siginfo,
105157c74f5bSJohn Baldwin     int check_only)
1052dcd43281SKonstantin Belousov {
1053f13b5a0fSKonstantin Belousov 	struct rusage *rup;
1054dcd43281SKonstantin Belousov 
1055db62ced2SJaakko Heinonen 	sx_assert(&proctree_lock, SA_XLOCKED);
1056db62ced2SJaakko Heinonen 
1057dcd43281SKonstantin Belousov 	PROC_LOCK(p);
1058f13b5a0fSKonstantin Belousov 
1059f13b5a0fSKonstantin Belousov 	switch (idtype) {
1060f13b5a0fSKonstantin Belousov 	case P_ALL:
10618e493611SMariusz Zaborski 		if (p->p_procdesc == NULL ||
10628e493611SMariusz Zaborski 		   (p->p_pptr == td->td_proc &&
10638e493611SMariusz Zaborski 		   (p->p_flag & P_TRACED) != 0)) {
10648e493611SMariusz Zaborski 			break;
10658e493611SMariusz Zaborski 		}
10668e493611SMariusz Zaborski 
106753bf545dSMariusz Zaborski 		PROC_UNLOCK(p);
106853bf545dSMariusz Zaborski 		return (0);
1069f13b5a0fSKonstantin Belousov 	case P_PID:
1070f13b5a0fSKonstantin Belousov 		if (p->p_pid != (pid_t)id) {
1071dcd43281SKonstantin Belousov 			PROC_UNLOCK(p);
1072dcd43281SKonstantin Belousov 			return (0);
1073dcd43281SKonstantin Belousov 		}
1074f13b5a0fSKonstantin Belousov 		break;
1075f13b5a0fSKonstantin Belousov 	case P_PGID:
1076f13b5a0fSKonstantin Belousov 		if (p->p_pgid != (pid_t)id) {
1077f13b5a0fSKonstantin Belousov 			PROC_UNLOCK(p);
1078f13b5a0fSKonstantin Belousov 			return (0);
1079f13b5a0fSKonstantin Belousov 		}
1080f13b5a0fSKonstantin Belousov 		break;
1081f13b5a0fSKonstantin Belousov 	case P_SID:
1082f13b5a0fSKonstantin Belousov 		if (p->p_session->s_sid != (pid_t)id) {
1083f13b5a0fSKonstantin Belousov 			PROC_UNLOCK(p);
1084f13b5a0fSKonstantin Belousov 			return (0);
1085f13b5a0fSKonstantin Belousov 		}
1086f13b5a0fSKonstantin Belousov 		break;
1087f13b5a0fSKonstantin Belousov 	case P_UID:
1088f13b5a0fSKonstantin Belousov 		if (p->p_ucred->cr_uid != (uid_t)id) {
1089f13b5a0fSKonstantin Belousov 			PROC_UNLOCK(p);
1090f13b5a0fSKonstantin Belousov 			return (0);
1091f13b5a0fSKonstantin Belousov 		}
1092f13b5a0fSKonstantin Belousov 		break;
1093f13b5a0fSKonstantin Belousov 	case P_GID:
1094f13b5a0fSKonstantin Belousov 		if (p->p_ucred->cr_gid != (gid_t)id) {
1095f13b5a0fSKonstantin Belousov 			PROC_UNLOCK(p);
1096f13b5a0fSKonstantin Belousov 			return (0);
1097f13b5a0fSKonstantin Belousov 		}
1098f13b5a0fSKonstantin Belousov 		break;
1099f13b5a0fSKonstantin Belousov 	case P_JAILID:
1100b1051d92SMateusz Guzik 		if (p->p_ucred->cr_prison->pr_id != (int)id) {
1101f13b5a0fSKonstantin Belousov 			PROC_UNLOCK(p);
1102f13b5a0fSKonstantin Belousov 			return (0);
1103f13b5a0fSKonstantin Belousov 		}
1104f13b5a0fSKonstantin Belousov 		break;
1105f13b5a0fSKonstantin Belousov 	/*
1106f13b5a0fSKonstantin Belousov 	 * It seems that the thread structures get zeroed out
1107f13b5a0fSKonstantin Belousov 	 * at process exit.  This makes it impossible to
1108f13b5a0fSKonstantin Belousov 	 * support P_SETID, P_CID or P_CPUID.
1109f13b5a0fSKonstantin Belousov 	 */
1110f13b5a0fSKonstantin Belousov 	default:
1111f13b5a0fSKonstantin Belousov 		PROC_UNLOCK(p);
1112f13b5a0fSKonstantin Belousov 		return (0);
1113f13b5a0fSKonstantin Belousov 	}
1114f13b5a0fSKonstantin Belousov 
1115dcd43281SKonstantin Belousov 	if (p_canwait(td, p)) {
1116dcd43281SKonstantin Belousov 		PROC_UNLOCK(p);
1117dcd43281SKonstantin Belousov 		return (0);
1118dcd43281SKonstantin Belousov 	}
1119dcd43281SKonstantin Belousov 
1120f13b5a0fSKonstantin Belousov 	if (((options & WEXITED) == 0) && (p->p_state == PRS_ZOMBIE)) {
1121f13b5a0fSKonstantin Belousov 		PROC_UNLOCK(p);
1122f13b5a0fSKonstantin Belousov 		return (0);
1123f13b5a0fSKonstantin Belousov 	}
1124f13b5a0fSKonstantin Belousov 
1125dcd43281SKonstantin Belousov 	/*
1126dcd43281SKonstantin Belousov 	 * This special case handles a kthread spawned by linux_clone
1127dcd43281SKonstantin Belousov 	 * (see linux_misc.c).  The linux_wait4 and linux_waitpid
1128dcd43281SKonstantin Belousov 	 * functions need to be able to distinguish between waiting
1129dcd43281SKonstantin Belousov 	 * on a process and waiting on a thread.  It is a thread if
1130dcd43281SKonstantin Belousov 	 * p_sigparent is not SIGCHLD, and the WLINUXCLONE option
1131dcd43281SKonstantin Belousov 	 * signifies we want to wait for threads and not processes.
1132dcd43281SKonstantin Belousov 	 */
1133dcd43281SKonstantin Belousov 	if ((p->p_sigparent != SIGCHLD) ^
1134dcd43281SKonstantin Belousov 	    ((options & WLINUXCLONE) != 0)) {
1135dcd43281SKonstantin Belousov 		PROC_UNLOCK(p);
1136dcd43281SKonstantin Belousov 		return (0);
1137dcd43281SKonstantin Belousov 	}
1138dcd43281SKonstantin Belousov 
1139f13b5a0fSKonstantin Belousov 	if (siginfo != NULL) {
1140f13b5a0fSKonstantin Belousov 		bzero(siginfo, sizeof(*siginfo));
1141f13b5a0fSKonstantin Belousov 		siginfo->si_errno = 0;
1142f13b5a0fSKonstantin Belousov 
1143f13b5a0fSKonstantin Belousov 		/*
1144f13b5a0fSKonstantin Belousov 		 * SUSv4 requires that the si_signo value is always
1145f13b5a0fSKonstantin Belousov 		 * SIGCHLD. Obey it despite the rfork(2) interface
1146f13b5a0fSKonstantin Belousov 		 * allows to request other signal for child exit
1147f13b5a0fSKonstantin Belousov 		 * notification.
1148f13b5a0fSKonstantin Belousov 		 */
1149f13b5a0fSKonstantin Belousov 		siginfo->si_signo = SIGCHLD;
1150f13b5a0fSKonstantin Belousov 
1151f13b5a0fSKonstantin Belousov 		/*
1152f13b5a0fSKonstantin Belousov 		 *  This is still a rough estimate.  We will fix the
1153f13b5a0fSKonstantin Belousov 		 *  cases TRAPPED, STOPPED, and CONTINUED later.
1154f13b5a0fSKonstantin Belousov 		 */
1155b4490c6eSKonstantin Belousov 		if (WCOREDUMP(p->p_xsig)) {
1156f13b5a0fSKonstantin Belousov 			siginfo->si_code = CLD_DUMPED;
1157b4490c6eSKonstantin Belousov 			siginfo->si_status = WTERMSIG(p->p_xsig);
1158b4490c6eSKonstantin Belousov 		} else if (WIFSIGNALED(p->p_xsig)) {
1159f13b5a0fSKonstantin Belousov 			siginfo->si_code = CLD_KILLED;
1160b4490c6eSKonstantin Belousov 			siginfo->si_status = WTERMSIG(p->p_xsig);
1161b20a9aa9SJilles Tjoelker 		} else {
1162f13b5a0fSKonstantin Belousov 			siginfo->si_code = CLD_EXITED;
1163b4490c6eSKonstantin Belousov 			siginfo->si_status = p->p_xexit;
1164b20a9aa9SJilles Tjoelker 		}
1165f13b5a0fSKonstantin Belousov 
1166f13b5a0fSKonstantin Belousov 		siginfo->si_pid = p->p_pid;
1167f13b5a0fSKonstantin Belousov 		siginfo->si_uid = p->p_ucred->cr_uid;
1168f13b5a0fSKonstantin Belousov 
1169f13b5a0fSKonstantin Belousov 		/*
1170f13b5a0fSKonstantin Belousov 		 * The si_addr field would be useful additional
1171f13b5a0fSKonstantin Belousov 		 * detail, but apparently the PC value may be lost
1172f13b5a0fSKonstantin Belousov 		 * when we reach this point.  bzero() above sets
1173f13b5a0fSKonstantin Belousov 		 * siginfo->si_addr to NULL.
1174f13b5a0fSKonstantin Belousov 		 */
1175f13b5a0fSKonstantin Belousov 	}
1176f13b5a0fSKonstantin Belousov 
1177f13b5a0fSKonstantin Belousov 	/*
1178f13b5a0fSKonstantin Belousov 	 * There should be no reason to limit resources usage info to
1179f13b5a0fSKonstantin Belousov 	 * exited processes only.  A snapshot about any resources used
1180f13b5a0fSKonstantin Belousov 	 * by a stopped process may be exactly what is needed.
1181f13b5a0fSKonstantin Belousov 	 */
1182f13b5a0fSKonstantin Belousov 	if (wrusage != NULL) {
1183f13b5a0fSKonstantin Belousov 		rup = &wrusage->wru_self;
1184f13b5a0fSKonstantin Belousov 		*rup = p->p_ru;
11855c7bebf9SKonstantin Belousov 		PROC_STATLOCK(p);
1186f13b5a0fSKonstantin Belousov 		calcru(p, &rup->ru_utime, &rup->ru_stime);
11875c7bebf9SKonstantin Belousov 		PROC_STATUNLOCK(p);
1188f13b5a0fSKonstantin Belousov 
1189f13b5a0fSKonstantin Belousov 		rup = &wrusage->wru_children;
1190f13b5a0fSKonstantin Belousov 		*rup = p->p_stats->p_cru;
1191f13b5a0fSKonstantin Belousov 		calccru(p, &rup->ru_utime, &rup->ru_stime);
1192f13b5a0fSKonstantin Belousov 	}
1193f13b5a0fSKonstantin Belousov 
119457c74f5bSJohn Baldwin 	if (p->p_state == PRS_ZOMBIE && !check_only) {
1195f13b5a0fSKonstantin Belousov 		proc_reap(td, p, status, options);
1196dcd43281SKonstantin Belousov 		return (-1);
1197dcd43281SKonstantin Belousov 	}
1198dcd43281SKonstantin Belousov 	return (1);
1199dcd43281SKonstantin Belousov }
1200dcd43281SKonstantin Belousov 
1201b7e23e82SJohn Baldwin int
kern_wait(struct thread * td,pid_t pid,int * status,int options,struct rusage * rusage)12027eaec467SJohn Baldwin kern_wait(struct thread *td, pid_t pid, int *status, int options,
12037eaec467SJohn Baldwin     struct rusage *rusage)
1204df8bae1dSRodney W. Grimes {
1205f13b5a0fSKonstantin Belousov 	struct __wrusage wru, *wrup;
1206f13b5a0fSKonstantin Belousov 	idtype_t idtype;
1207f13b5a0fSKonstantin Belousov 	id_t id;
1208f13b5a0fSKonstantin Belousov 	int ret;
1209f13b5a0fSKonstantin Belousov 
1210538375d4SKonstantin Belousov 	/*
1211538375d4SKonstantin Belousov 	 * Translate the special pid values into the (idtype, pid)
1212538375d4SKonstantin Belousov 	 * pair for kern_wait6.  The WAIT_MYPGRP case is handled by
1213538375d4SKonstantin Belousov 	 * kern_wait6() on its own.
1214538375d4SKonstantin Belousov 	 */
1215f13b5a0fSKonstantin Belousov 	if (pid == WAIT_ANY) {
1216f13b5a0fSKonstantin Belousov 		idtype = P_ALL;
1217f13b5a0fSKonstantin Belousov 		id = 0;
1218ea293f3fSKonstantin Belousov 	} else if (pid < 0) {
1219f13b5a0fSKonstantin Belousov 		idtype = P_PGID;
1220f13b5a0fSKonstantin Belousov 		id = (id_t)-pid;
1221ea293f3fSKonstantin Belousov 	} else {
1222f13b5a0fSKonstantin Belousov 		idtype = P_PID;
1223f13b5a0fSKonstantin Belousov 		id = (id_t)pid;
1224f13b5a0fSKonstantin Belousov 	}
1225538375d4SKonstantin Belousov 
1226f13b5a0fSKonstantin Belousov 	if (rusage != NULL)
1227f13b5a0fSKonstantin Belousov 		wrup = &wru;
1228f13b5a0fSKonstantin Belousov 	else
1229f13b5a0fSKonstantin Belousov 		wrup = NULL;
1230538375d4SKonstantin Belousov 
1231f13b5a0fSKonstantin Belousov 	/*
1232f13b5a0fSKonstantin Belousov 	 * For backward compatibility we implicitly add flags WEXITED
1233f13b5a0fSKonstantin Belousov 	 * and WTRAPPED here.
1234f13b5a0fSKonstantin Belousov 	 */
1235f13b5a0fSKonstantin Belousov 	options |= WEXITED | WTRAPPED;
1236f13b5a0fSKonstantin Belousov 	ret = kern_wait6(td, idtype, id, status, options, wrup, NULL);
1237f13b5a0fSKonstantin Belousov 	if (rusage != NULL)
1238f13b5a0fSKonstantin Belousov 		*rusage = wru.wru_self;
1239f13b5a0fSKonstantin Belousov 	return (ret);
1240f13b5a0fSKonstantin Belousov }
1241f13b5a0fSKonstantin Belousov 
1242435da985SKonstantin Belousov static void
report_alive_proc(struct thread * td,struct proc * p,siginfo_t * siginfo,int * status,int options,int si_code)1243435da985SKonstantin Belousov report_alive_proc(struct thread *td, struct proc *p, siginfo_t *siginfo,
1244435da985SKonstantin Belousov     int *status, int options, int si_code)
1245435da985SKonstantin Belousov {
1246435da985SKonstantin Belousov 	bool cont;
1247435da985SKonstantin Belousov 
1248435da985SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
1249435da985SKonstantin Belousov 	sx_assert(&proctree_lock, SA_XLOCKED);
1250435da985SKonstantin Belousov 	MPASS(si_code == CLD_TRAPPED || si_code == CLD_STOPPED ||
1251435da985SKonstantin Belousov 	    si_code == CLD_CONTINUED);
1252435da985SKonstantin Belousov 
1253435da985SKonstantin Belousov 	cont = si_code == CLD_CONTINUED;
1254435da985SKonstantin Belousov 	if ((options & WNOWAIT) == 0) {
1255435da985SKonstantin Belousov 		if (cont)
1256435da985SKonstantin Belousov 			p->p_flag &= ~P_CONTINUED;
1257435da985SKonstantin Belousov 		else
1258435da985SKonstantin Belousov 			p->p_flag |= P_WAITED;
1259a12e901aSKonstantin Belousov 		if (kern_wait_dequeue_sigchld &&
1260a12e901aSKonstantin Belousov 		    (td->td_proc->p_sysent->sv_flags & SV_SIG_WAITNDQ) == 0) {
1261435da985SKonstantin Belousov 			PROC_LOCK(td->td_proc);
1262435da985SKonstantin Belousov 			sigqueue_take(p->p_ksi);
1263435da985SKonstantin Belousov 			PROC_UNLOCK(td->td_proc);
1264435da985SKonstantin Belousov 		}
1265a12e901aSKonstantin Belousov 	}
1266435da985SKonstantin Belousov 	sx_xunlock(&proctree_lock);
1267435da985SKonstantin Belousov 	if (siginfo != NULL) {
1268435da985SKonstantin Belousov 		siginfo->si_code = si_code;
1269435da985SKonstantin Belousov 		siginfo->si_status = cont ? SIGCONT : p->p_xsig;
1270435da985SKonstantin Belousov 	}
1271435da985SKonstantin Belousov 	if (status != NULL)
1272435da985SKonstantin Belousov 		*status = cont ? SIGCONT : W_STOPCODE(p->p_xsig);
1273435da985SKonstantin Belousov 	td->td_retval[0] = p->p_pid;
127449afea10SMateusz Guzik 	PROC_UNLOCK(p);
1275435da985SKonstantin Belousov }
1276435da985SKonstantin Belousov 
1277f13b5a0fSKonstantin Belousov int
kern_wait6(struct thread * td,idtype_t idtype,id_t id,int * status,int options,struct __wrusage * wrusage,siginfo_t * siginfo)1278f13b5a0fSKonstantin Belousov kern_wait6(struct thread *td, idtype_t idtype, id_t id, int *status,
1279f13b5a0fSKonstantin Belousov     int options, struct __wrusage *wrusage, siginfo_t *siginfo)
1280f13b5a0fSKonstantin Belousov {
1281324fb6beSRobert Watson 	struct proc *p, *q;
12823a41ec6aSEd Schouten 	pid_t pid;
1283dcd43281SKonstantin Belousov 	int error, nfound, ret;
12847beb6082SMateusz Guzik 	bool report;
1285df8bae1dSRodney W. Grimes 
1286f13b5a0fSKonstantin Belousov 	AUDIT_ARG_VALUE((int)idtype);	/* XXX - This is likely wrong! */
1287f13b5a0fSKonstantin Belousov 	AUDIT_ARG_PID((pid_t)id);	/* XXX - This may be wrong! */
12882ef24ddeSRobert Watson 	AUDIT_ARG_VALUE(options);
1289de3007e8SWayne Salamon 
1290b40ce416SJulian Elischer 	q = td->td_proc;
1291f13b5a0fSKonstantin Belousov 
1292a2a85596SKonstantin Belousov 	if ((pid_t)id == WAIT_MYPGRP && (idtype == P_PID || idtype == P_PGID)) {
1293a545089eSKonstantin Belousov 		PROC_LOCK(q);
1294f13b5a0fSKonstantin Belousov 		id = (id_t)q->p_pgid;
1295a545089eSKonstantin Belousov 		PROC_UNLOCK(q);
1296f13b5a0fSKonstantin Belousov 		idtype = P_PGID;
1297f591779bSSeigo Tanimura 	}
1298f13b5a0fSKonstantin Belousov 
1299f528c3fdSDavid E. O'Brien 	/* If we don't know the option, just return. */
1300f13b5a0fSKonstantin Belousov 	if ((options & ~(WUNTRACED | WNOHANG | WCONTINUED | WNOWAIT |
1301f13b5a0fSKonstantin Belousov 	    WEXITED | WTRAPPED | WLINUXCLONE)) != 0)
13026dc958b9SJohn Baldwin 		return (EINVAL);
1303f13b5a0fSKonstantin Belousov 	if ((options & (WEXITED | WUNTRACED | WCONTINUED | WTRAPPED)) == 0) {
1304f13b5a0fSKonstantin Belousov 		/*
1305f13b5a0fSKonstantin Belousov 		 * We will be unable to find any matching processes,
1306f13b5a0fSKonstantin Belousov 		 * because there are no known events to look for.
1307f13b5a0fSKonstantin Belousov 		 * Prefer to return error instead of blocking
1308f13b5a0fSKonstantin Belousov 		 * indefinitely.
1309f13b5a0fSKonstantin Belousov 		 */
1310f13b5a0fSKonstantin Belousov 		return (EINVAL);
1311f13b5a0fSKonstantin Belousov 	}
1312f13b5a0fSKonstantin Belousov 
1313df8bae1dSRodney W. Grimes loop:
1314902c0d82SDavid Xu 	if (q->p_flag & P_STATCHILD) {
1315902c0d82SDavid Xu 		PROC_LOCK(q);
1316902c0d82SDavid Xu 		q->p_flag &= ~P_STATCHILD;
1317902c0d82SDavid Xu 		PROC_UNLOCK(q);
1318902c0d82SDavid Xu 	}
1319d7aadbf9SJohn Baldwin 	sx_xlock(&proctree_lock);
13206776bfebSMateusz Guzik loop_locked:
13216776bfebSMateusz Guzik 	nfound = 0;
13222e3c8fcbSPoul-Henning Kamp 	LIST_FOREACH(p, &q->p_children, p_sibling) {
13233a41ec6aSEd Schouten 		pid = p->p_pid;
1324f13b5a0fSKonstantin Belousov 		ret = proc_to_reap(td, p, idtype, id, status, options,
132557c74f5bSJohn Baldwin 		    wrusage, siginfo, 0);
1326dcd43281SKonstantin Belousov 		if (ret == 0)
1327df8bae1dSRodney W. Grimes 			continue;
13286776bfebSMateusz Guzik 		else if (ret != 1) {
13293a41ec6aSEd Schouten 			td->td_retval[0] = pid;
1330d7aadbf9SJohn Baldwin 			return (0);
13313a41ec6aSEd Schouten 		}
1332dcd43281SKonstantin Belousov 
1333*d0675399SEdward Tomasz Napierala 		/*
1334*d0675399SEdward Tomasz Napierala 		 * When running in capsicum(4) mode, make wait(2) ignore
1335*d0675399SEdward Tomasz Napierala 		 * processes created with pdfork(2).  This is because one can
1336*d0675399SEdward Tomasz Napierala 		 * disown them - by passing their process descriptor to another
1337*d0675399SEdward Tomasz Napierala 		 * process - which means it needs to be prevented from touching
1338*d0675399SEdward Tomasz Napierala 		 * them afterwards.
1339*d0675399SEdward Tomasz Napierala 		 */
1340*d0675399SEdward Tomasz Napierala 		if (IN_CAPABILITY_MODE(td) && p->p_procdesc != NULL) {
1341*d0675399SEdward Tomasz Napierala 			PROC_UNLOCK(p);
1342*d0675399SEdward Tomasz Napierala 			continue;
1343*d0675399SEdward Tomasz Napierala 		}
1344*d0675399SEdward Tomasz Napierala 
13456776bfebSMateusz Guzik 		nfound++;
1346746b6e81SMateusz Guzik 		PROC_LOCK_ASSERT(p, MA_OWNED);
1347435da985SKonstantin Belousov 
1348f13b5a0fSKonstantin Belousov 		if ((options & WTRAPPED) != 0 &&
13497beb6082SMateusz Guzik 		    (p->p_flag & P_TRACED) != 0) {
13507beb6082SMateusz Guzik 			PROC_SLOCK(p);
13517beb6082SMateusz Guzik 			report =
13527beb6082SMateusz Guzik 			    ((p->p_flag & (P_STOPPED_TRACE | P_STOPPED_SIG)) &&
1353435da985SKonstantin Belousov 			    p->p_suspcount == p->p_numthreads &&
13547beb6082SMateusz Guzik 			    (p->p_flag & P_WAITED) == 0);
13558bf6ff22SMateusz Guzik 			PROC_SUNLOCK(p);
13567beb6082SMateusz Guzik 			if (report) {
1357515b7a0bSJohn Baldwin 			CTR4(KTR_PTRACE,
1358435da985SKonstantin Belousov 			    "wait: returning trapped pid %d status %#x "
1359435da985SKonstantin Belousov 			    "(xstat %d) xthread %d",
1360b4490c6eSKonstantin Belousov 			    p->p_pid, W_STOPCODE(p->p_xsig), p->p_xsig,
1361435da985SKonstantin Belousov 			    p->p_xthread != NULL ?
1362435da985SKonstantin Belousov 			    p->p_xthread->td_tid : -1);
13637beb6082SMateusz Guzik 				report_alive_proc(td, p, siginfo, status,
13647beb6082SMateusz Guzik 				    options, CLD_TRAPPED);
1365f13b5a0fSKonstantin Belousov 				return (0);
1366f13b5a0fSKonstantin Belousov 			}
13677beb6082SMateusz Guzik 		}
1368f13b5a0fSKonstantin Belousov 		if ((options & WUNTRACED) != 0 &&
13697beb6082SMateusz Guzik 		    (p->p_flag & P_STOPPED_SIG) != 0) {
13707beb6082SMateusz Guzik 			PROC_SLOCK(p);
13717beb6082SMateusz Guzik 			report = (p->p_suspcount == p->p_numthreads &&
13727beb6082SMateusz Guzik 			    ((p->p_flag & P_WAITED) == 0));
13738bf6ff22SMateusz Guzik 			PROC_SUNLOCK(p);
13747beb6082SMateusz Guzik 			if (report) {
13757beb6082SMateusz Guzik 				report_alive_proc(td, p, siginfo, status,
13767beb6082SMateusz Guzik 				    options, CLD_STOPPED);
1377b7e23e82SJohn Baldwin 				return (0);
1378df8bae1dSRodney W. Grimes 			}
13797beb6082SMateusz Guzik 		}
1380f13b5a0fSKonstantin Belousov 		if ((options & WCONTINUED) != 0 &&
1381f13b5a0fSKonstantin Belousov 		    (p->p_flag & P_CONTINUED) != 0) {
1382435da985SKonstantin Belousov 			report_alive_proc(td, p, siginfo, status, options,
1383435da985SKonstantin Belousov 			    CLD_CONTINUED);
1384dcdc6c36SKonstantin Belousov 			return (0);
13856ee093fbSMike Barcroft 		}
1386d7aadbf9SJohn Baldwin 		PROC_UNLOCK(p);
1387d7aadbf9SJohn Baldwin 	}
1388dcd43281SKonstantin Belousov 
1389dcd43281SKonstantin Belousov 	/*
1390dcd43281SKonstantin Belousov 	 * Look in the orphans list too, to allow the parent to
1391dcd43281SKonstantin Belousov 	 * collect it's child exit status even if child is being
1392dcd43281SKonstantin Belousov 	 * debugged.
1393dcd43281SKonstantin Belousov 	 *
1394dcd43281SKonstantin Belousov 	 * Debugger detaches from the parent upon successful
1395dcd43281SKonstantin Belousov 	 * switch-over from parent to child.  At this point due to
1396dcd43281SKonstantin Belousov 	 * re-parenting the parent loses the child to debugger and a
1397dcd43281SKonstantin Belousov 	 * wait4(2) call would report that it has no children to wait
1398dcd43281SKonstantin Belousov 	 * for.  By maintaining a list of orphans we allow the parent
1399dcd43281SKonstantin Belousov 	 * to successfully wait until the child becomes a zombie.
1400dcd43281SKonstantin Belousov 	 */
140157c74f5bSJohn Baldwin 	if (nfound == 0) {
1402dcd43281SKonstantin Belousov 		LIST_FOREACH(p, &q->p_orphans, p_orphan) {
140357c74f5bSJohn Baldwin 			ret = proc_to_reap(td, p, idtype, id, NULL, options,
140457c74f5bSJohn Baldwin 			    NULL, NULL, 1);
140557c74f5bSJohn Baldwin 			if (ret != 0) {
140657c74f5bSJohn Baldwin 				KASSERT(ret != -1, ("reaped an orphan (pid %d)",
140757c74f5bSJohn Baldwin 				    (int)td->td_retval[0]));
1408746b6e81SMateusz Guzik 				PROC_UNLOCK(p);
1409dcd43281SKonstantin Belousov 				nfound++;
141057c74f5bSJohn Baldwin 				break;
141157c74f5bSJohn Baldwin 			}
141257c74f5bSJohn Baldwin 		}
1413dcd43281SKonstantin Belousov 	}
1414d7aadbf9SJohn Baldwin 	if (nfound == 0) {
1415d7aadbf9SJohn Baldwin 		sx_xunlock(&proctree_lock);
1416d7aadbf9SJohn Baldwin 		return (ECHILD);
1417d7aadbf9SJohn Baldwin 	}
1418b7e23e82SJohn Baldwin 	if (options & WNOHANG) {
1419d7aadbf9SJohn Baldwin 		sx_xunlock(&proctree_lock);
1420d7aadbf9SJohn Baldwin 		td->td_retval[0] = 0;
1421d7aadbf9SJohn Baldwin 		return (0);
1422d7aadbf9SJohn Baldwin 	}
1423d7aadbf9SJohn Baldwin 	PROC_LOCK(q);
142495992d56SDavid Xu 	if (q->p_flag & P_STATCHILD) {
142595992d56SDavid Xu 		q->p_flag &= ~P_STATCHILD;
1426d7aadbf9SJohn Baldwin 		PROC_UNLOCK(q);
14276776bfebSMateusz Guzik 		goto loop_locked;
14286776bfebSMateusz Guzik 	}
14296776bfebSMateusz Guzik 	sx_xunlock(&proctree_lock);
14306776bfebSMateusz Guzik 	error = msleep(q, &q->p_mtx, PWAIT | PCATCH | PDROP, "wait", 0);
14314ae89b95SJohn Baldwin 	if (error)
1432d7aadbf9SJohn Baldwin 		return (error);
1433d7aadbf9SJohn Baldwin 	goto loop;
1434d7aadbf9SJohn Baldwin }
1435df8bae1dSRodney W. Grimes 
1436799d92abSMariusz Zaborski void
proc_add_orphan(struct proc * child,struct proc * parent)1437799d92abSMariusz Zaborski proc_add_orphan(struct proc *child, struct proc *parent)
1438799d92abSMariusz Zaborski {
1439799d92abSMariusz Zaborski 
1440799d92abSMariusz Zaborski 	sx_assert(&proctree_lock, SX_XLOCKED);
1441799d92abSMariusz Zaborski 	KASSERT((child->p_flag & P_TRACED) != 0,
1442799d92abSMariusz Zaborski 	    ("proc_add_orphan: not traced"));
1443799d92abSMariusz Zaborski 
1444799d92abSMariusz Zaborski 	if (LIST_EMPTY(&parent->p_orphans)) {
1445799d92abSMariusz Zaborski 		child->p_treeflag |= P_TREE_FIRST_ORPHAN;
1446799d92abSMariusz Zaborski 		LIST_INSERT_HEAD(&parent->p_orphans, child, p_orphan);
1447799d92abSMariusz Zaborski 	} else {
1448799d92abSMariusz Zaborski 		LIST_INSERT_AFTER(LIST_FIRST(&parent->p_orphans),
1449799d92abSMariusz Zaborski 		    child, p_orphan);
1450799d92abSMariusz Zaborski 	}
1451799d92abSMariusz Zaborski 	child->p_treeflag |= P_TREE_ORPHANED;
1452799d92abSMariusz Zaborski }
1453799d92abSMariusz Zaborski 
1454df8bae1dSRodney W. Grimes /*
145598f03f90SJake Burkholder  * Make process 'parent' the new parent of process 'child'.
145698f03f90SJake Burkholder  * Must be called with an exclusive hold of proctree lock.
1457df8bae1dSRodney W. Grimes  */
1458df8bae1dSRodney W. Grimes void
proc_reparent(struct proc * child,struct proc * parent,bool set_oppid)14592c054ce9SMateusz Guzik proc_reparent(struct proc *child, struct proc *parent, bool set_oppid)
1460df8bae1dSRodney W. Grimes {
1461df8bae1dSRodney W. Grimes 
14624e5e677bSJohn Baldwin 	sx_assert(&proctree_lock, SX_XLOCKED);
1463c65437a3SJohn Baldwin 	PROC_LOCK_ASSERT(child, MA_OWNED);
1464df8bae1dSRodney W. Grimes 	if (child->p_pptr == parent)
1465df8bae1dSRodney W. Grimes 		return;
1466df8bae1dSRodney W. Grimes 
1467ff766807SDavid Xu 	PROC_LOCK(child->p_pptr);
1468ff766807SDavid Xu 	sigqueue_take(child->p_ksi);
1469ff766807SDavid Xu 	PROC_UNLOCK(child->p_pptr);
1470b75356e1SJeffrey Hsu 	LIST_REMOVE(child, p_sibling);
1471b75356e1SJeffrey Hsu 	LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
1472dcd43281SKonstantin Belousov 
14739db97ca0SMariusz Zaborski 	proc_clear_orphan(child);
1474799d92abSMariusz Zaborski 	if ((child->p_flag & P_TRACED) != 0) {
1475799d92abSMariusz Zaborski 		proc_add_orphan(child, child->p_pptr);
1476dcd43281SKonstantin Belousov 	}
1477dcd43281SKonstantin Belousov 
1478df8bae1dSRodney W. Grimes 	child->p_pptr = parent;
14792c054ce9SMateusz Guzik 	if (set_oppid)
14802c054ce9SMateusz Guzik 		child->p_oppid = parent->p_pid;
1481df8bae1dSRodney W. Grimes }
1482