xref: /freebsd/sys/kern/kern_exit.c (revision 0a7139485ccb8192e7c7c6d93653da92bfcdc196)
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  *	@(#)kern_exit.c	8.7 (Berkeley) 2/12/94
37df8bae1dSRodney W. Grimes  */
38df8bae1dSRodney W. Grimes 
39677b542eSDavid E. O'Brien #include <sys/cdefs.h>
4047877889SJohn Baldwin #include "opt_ddb.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>
49daec9284SConrad Meyer #include <sys/ktr.h>
50a1c995b6SPoul-Henning Kamp #include <sys/malloc.h>
51f34fa851SJohn Baldwin #include <sys/lock.h>
5235e0e5b3SJohn Baldwin #include <sys/mutex.h>
53df8bae1dSRodney W. Grimes #include <sys/proc.h>
54cfb5f768SJonathan Anderson #include <sys/procdesc.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>
67fd3ac06fSKonstantin Belousov #include <sys/sysctl.h>
6825f6e35aSPoul-Henning Kamp #include <sys/syslog.h>
69df8bae1dSRodney W. Grimes #include <sys/ptrace.h>
707c409b8aSJeffrey Hsu #include <sys/acct.h>		/* for acct_process() function prototype */
71797f2d22SPoul-Henning Kamp #include <sys/filedesc.h>
725d217f17SJohn Birrell #include <sys/sdt.h>
73780dc5a8SPeter Wemm #include <sys/shm.h>
74780dc5a8SPeter Wemm #include <sys/sem.h>
7587a9b18dSKonstantin Belousov #include <sys/sysent.h>
76e68c6191SKonstantin Belousov #include <sys/timers.h>
77af29f399SDmitry Chagin #include <sys/umtxvar.h>
786c84de02SJohn Baldwin #ifdef KTRACE
796c84de02SJohn Baldwin #include <sys/ktrace.h>
806c84de02SJohn Baldwin #endif
81780dc5a8SPeter Wemm 
82fcf7f27aSRobert Watson #include <security/audit/audit.h>
83aed55708SRobert Watson #include <security/mac/mac_framework.h>
84fcf7f27aSRobert Watson 
85df8bae1dSRodney W. Grimes #include <vm/vm.h>
86eb30c1c0SPeter Wemm #include <vm/vm_extern.h>
877a6b989bSJohn Baldwin #include <vm/vm_param.h>
88efeaf95aSDavid Greenman #include <vm/pmap.h>
89efeaf95aSDavid Greenman #include <vm/vm_map.h>
902d21129dSAlan Cox #include <vm/vm_page.h>
91c897b813SJeff Roberson #include <vm/uma.h>
92df8bae1dSRodney W. Grimes 
935d217f17SJohn Birrell #ifdef KDTRACE_HOOKS
945d217f17SJohn Birrell #include <sys/dtrace_bsd.h>
955d217f17SJohn Birrell dtrace_execexit_func_t	dtrace_fasttrap_exit;
965d217f17SJohn Birrell #endif
975d217f17SJohn Birrell 
985d217f17SJohn Birrell SDT_PROVIDER_DECLARE(proc);
9936160958SMark Johnston SDT_PROBE_DEFINE1(proc, , , exit, "int");
1005d217f17SJohn Birrell 
101fd3ac06fSKonstantin Belousov static int kern_kill_on_dbg_exit = 1;
102fd3ac06fSKonstantin Belousov SYSCTL_INT(_kern, OID_AUTO, kill_on_debugger_exit, CTLFLAG_RWTUN,
103fd3ac06fSKonstantin Belousov     &kern_kill_on_dbg_exit, 0,
104fd3ac06fSKonstantin Belousov     "Kill ptraced processes when debugger exits");
105fd3ac06fSKonstantin Belousov 
106a12e901aSKonstantin Belousov static bool kern_wait_dequeue_sigchld = 1;
107a12e901aSKonstantin Belousov SYSCTL_BOOL(_kern, OID_AUTO, wait_dequeue_sigchld, CTLFLAG_RWTUN,
108a12e901aSKonstantin Belousov     &kern_wait_dequeue_sigchld, 0,
109a12e901aSKonstantin Belousov     "Dequeue SIGCHLD on wait(2) for live process");
110a12e901aSKonstantin Belousov 
111d7359980SKonstantin Belousov struct proc *
112d7359980SKonstantin Belousov proc_realparent(struct proc *child)
113d7359980SKonstantin Belousov {
114d7359980SKonstantin Belousov 	struct proc *p, *parent;
115d7359980SKonstantin Belousov 
116d7359980SKonstantin Belousov 	sx_assert(&proctree_lock, SX_LOCKED);
1172c054ce9SMateusz Guzik 	if ((child->p_treeflag & P_TREE_ORPHANED) == 0)
1182c054ce9SMateusz Guzik 		return (child->p_pptr->p_pid == child->p_oppid ?
119844219f4SKonstantin Belousov 		    child->p_pptr : child->p_reaper);
120d7359980SKonstantin Belousov 	for (p = child; (p->p_treeflag & P_TREE_FIRST_ORPHAN) == 0;) {
121d7359980SKonstantin Belousov 		/* Cannot use LIST_PREV(), since the list head is not known. */
122d7359980SKonstantin Belousov 		p = __containerof(p->p_orphan.le_prev, struct proc,
123d7359980SKonstantin Belousov 		    p_orphan.le_next);
124d7359980SKonstantin Belousov 		KASSERT((p->p_treeflag & P_TREE_ORPHANED) != 0,
125d7359980SKonstantin Belousov 		    ("missing P_ORPHAN %p", p));
126d7359980SKonstantin Belousov 	}
127d7359980SKonstantin Belousov 	parent = __containerof(p->p_orphan.le_prev, struct proc,
128d7359980SKonstantin Belousov 	    p_orphans.lh_first);
129d7359980SKonstantin Belousov 	return (parent);
130d7359980SKonstantin Belousov }
131d7359980SKonstantin Belousov 
132237623b0SKonstantin Belousov void
133237623b0SKonstantin Belousov reaper_abandon_children(struct proc *p, bool exiting)
134237623b0SKonstantin Belousov {
135237623b0SKonstantin Belousov 	struct proc *p1, *p2, *ptmp;
136237623b0SKonstantin Belousov 
137b9294a3eSKonstantin Belousov 	sx_assert(&proctree_lock, SX_XLOCKED);
138237623b0SKonstantin Belousov 	KASSERT(p != initproc, ("reaper_abandon_children for initproc"));
139237623b0SKonstantin Belousov 	if ((p->p_treeflag & P_TREE_REAPER) == 0)
140237623b0SKonstantin Belousov 		return;
141237623b0SKonstantin Belousov 	p1 = p->p_reaper;
142237623b0SKonstantin Belousov 	LIST_FOREACH_SAFE(p2, &p->p_reaplist, p_reapsibling, ptmp) {
143237623b0SKonstantin Belousov 		LIST_REMOVE(p2, p_reapsibling);
144237623b0SKonstantin Belousov 		p2->p_reaper = p1;
145237623b0SKonstantin Belousov 		p2->p_reapsubtree = p->p_reapsubtree;
146237623b0SKonstantin Belousov 		LIST_INSERT_HEAD(&p1->p_reaplist, p2, p_reapsibling);
147237623b0SKonstantin Belousov 		if (exiting && p2->p_pptr == p) {
148237623b0SKonstantin Belousov 			PROC_LOCK(p2);
1492c054ce9SMateusz Guzik 			proc_reparent(p2, p1, true);
150237623b0SKonstantin Belousov 			PROC_UNLOCK(p2);
151237623b0SKonstantin Belousov 		}
152237623b0SKonstantin Belousov 	}
153237623b0SKonstantin Belousov 	KASSERT(LIST_EMPTY(&p->p_reaplist), ("p_reaplist not empty"));
154237623b0SKonstantin Belousov 	p->p_treeflag &= ~P_TREE_REAPER;
155237623b0SKonstantin Belousov }
156237623b0SKonstantin Belousov 
1572e39e24fSKonstantin Belousov static void
15834ebdceaSMateusz Guzik reaper_clear(struct proc *p)
15934ebdceaSMateusz Guzik {
16034ebdceaSMateusz Guzik 	struct proc *p1;
16134ebdceaSMateusz Guzik 	bool clear;
16234ebdceaSMateusz Guzik 
16334ebdceaSMateusz Guzik 	sx_assert(&proctree_lock, SX_LOCKED);
16434ebdceaSMateusz Guzik 	LIST_REMOVE(p, p_reapsibling);
16534ebdceaSMateusz Guzik 	if (p->p_reapsubtree == 1)
16634ebdceaSMateusz Guzik 		return;
16734ebdceaSMateusz Guzik 	clear = true;
16834ebdceaSMateusz Guzik 	LIST_FOREACH(p1, &p->p_reaper->p_reaplist, p_reapsibling) {
16934ebdceaSMateusz Guzik 		if (p1->p_reapsubtree == p->p_reapsubtree) {
17034ebdceaSMateusz Guzik 			clear = false;
17134ebdceaSMateusz Guzik 			break;
17234ebdceaSMateusz Guzik 		}
17334ebdceaSMateusz Guzik 	}
17434ebdceaSMateusz Guzik 	if (clear)
17534ebdceaSMateusz Guzik 		proc_id_clear(PROC_ID_REAP, p->p_reapsubtree);
17634ebdceaSMateusz Guzik }
17734ebdceaSMateusz Guzik 
1789db97ca0SMariusz Zaborski void
1799db97ca0SMariusz Zaborski proc_clear_orphan(struct proc *p)
1802e39e24fSKonstantin Belousov {
181d7359980SKonstantin Belousov 	struct proc *p1;
1822e39e24fSKonstantin Belousov 
183d7359980SKonstantin Belousov 	sx_assert(&proctree_lock, SA_XLOCKED);
184d7359980SKonstantin Belousov 	if ((p->p_treeflag & P_TREE_ORPHANED) == 0)
185d7359980SKonstantin Belousov 		return;
186d7359980SKonstantin Belousov 	if ((p->p_treeflag & P_TREE_FIRST_ORPHAN) != 0) {
187d7359980SKonstantin Belousov 		p1 = LIST_NEXT(p, p_orphan);
188d7359980SKonstantin Belousov 		if (p1 != NULL)
189d7359980SKonstantin Belousov 			p1->p_treeflag |= P_TREE_FIRST_ORPHAN;
190d7359980SKonstantin Belousov 		p->p_treeflag &= ~P_TREE_FIRST_ORPHAN;
1912e39e24fSKonstantin Belousov 	}
192d7359980SKonstantin Belousov 	LIST_REMOVE(p, p_orphan);
193d7359980SKonstantin Belousov 	p->p_treeflag &= ~P_TREE_ORPHANED;
1942e39e24fSKonstantin Belousov }
1952e39e24fSKonstantin Belousov 
19628a66fc3SKonstantin Belousov void
19728a66fc3SKonstantin Belousov exit_onexit(struct proc *p)
19828a66fc3SKonstantin Belousov {
19928a66fc3SKonstantin Belousov 	MPASS(p->p_numthreads == 1);
20028a66fc3SKonstantin Belousov 	umtx_thread_exit(FIRST_THREAD_IN_PROC(p));
20128a66fc3SKonstantin Belousov }
20228a66fc3SKonstantin Belousov 
203df8bae1dSRodney W. Grimes /*
204873fbcd7SRobert Watson  * exit -- death of process.
205df8bae1dSRodney W. Grimes  */
206cffb55f0SBrooks Davis int
207cffb55f0SBrooks Davis sys_exit(struct thread *td, struct exit_args *uap)
208df8bae1dSRodney W. Grimes {
209b40ce416SJulian Elischer 
210b4490c6eSKonstantin Belousov 	exit1(td, uap->rval, 0);
211cffb55f0SBrooks Davis 	__unreachable();
212df8bae1dSRodney W. Grimes }
213df8bae1dSRodney W. Grimes 
2144493a13eSKonstantin Belousov void
2154493a13eSKonstantin Belousov proc_set_p2_wexit(struct proc *p)
2164493a13eSKonstantin Belousov {
2174493a13eSKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
2184493a13eSKonstantin Belousov 	p->p_flag2 |= P2_WEXIT;
2194493a13eSKonstantin Belousov }
2204493a13eSKonstantin Belousov 
221df8bae1dSRodney W. Grimes /*
222873fbcd7SRobert Watson  * Exit: deallocate address space and other resources, change proc state to
223873fbcd7SRobert Watson  * zombie, and unlink proc from allproc and parent's lists.  Save exit status
224873fbcd7SRobert Watson  * and rusage for wait().  Check for child processes and orphan them.
225df8bae1dSRodney W. Grimes  */
226fc0b1dbfSBruce Evans void
227aaa92413SKonstantin Belousov exit1(struct thread *td, int rval, int signo)
228df8bae1dSRodney W. Grimes {
229a661bebeSMateusz Guzik 	struct proc *p, *nq, *q, *t;
230a661bebeSMateusz Guzik 	struct thread *tdt;
2319a2dde80SKonstantin Belousov 	ksiginfo_t *ksi, *ksi1;
232015cd8dcSMateusz Guzik 	int signal_parent;
233df8bae1dSRodney W. Grimes 
234e17660e7SKris Kennaway 	mtx_assert(&Giant, MA_NOTOWNED);
235b4490c6eSKonstantin Belousov 	KASSERT(rval == 0 || signo == 0, ("exit1 rv %d sig %d", rval, signo));
23646dd801aSColin Percival 	TSPROCEXIT(td->td_proc->p_pid);
2370cddd8f0SMatthew Dillon 
238276c5169SJohn Baldwin 	p = td->td_proc;
23928b36ecfSKonstantin Belousov 	/*
24028b36ecfSKonstantin Belousov 	 * In case we're rebooting we just let init die in order to
24128b36ecfSKonstantin Belousov 	 * work around an issues where pid 1 might get a fatal signal.
24228b36ecfSKonstantin Belousov 	 * For instance, if network interface serving NFS root is
24328b36ecfSKonstantin Belousov 	 * going down due to reboot, page-in requests for text are
24428b36ecfSKonstantin Belousov 	 * failing.
24528b36ecfSKonstantin Belousov 	 */
24628b36ecfSKonstantin Belousov 	if (p == initproc && rebooting == 0) {
247b4490c6eSKonstantin Belousov 		printf("init died (signal %d, exit %d)\n", signo, rval);
2485f7bd355SPoul-Henning Kamp 		panic("Going nowhere without my init!");
2495f7bd355SPoul-Henning Kamp 	}
2502c1011f7SJohn Dyson 
2517a6b989bSJohn Baldwin 	/*
252c6d31b83SKonstantin Belousov 	 * Process deferred operations, designated with ASTF_KCLEAR.
253c6d31b83SKonstantin Belousov 	 * For instance, we need to deref SU mp, since the thread does
254c6d31b83SKonstantin Belousov 	 * not return to userspace, and wait for geom to stabilize.
2551bc93bb7SKonstantin Belousov 	 */
256c6d31b83SKonstantin Belousov 	ast_kclear(td);
2571bc93bb7SKonstantin Belousov 
2581bc93bb7SKonstantin Belousov 	/*
2592c10d16aSJeff Roberson 	 * MUST abort all other threads before proceeding past here.
2607a6b989bSJohn Baldwin 	 */
261e602ba25SJulian Elischer 	PROC_LOCK(p);
2624493a13eSKonstantin Belousov 	proc_set_p2_wexit(p);
263d3000939SKonstantin Belousov 
264e602ba25SJulian Elischer 	/*
265aba1ca52SKonstantin Belousov 	 * First check if some other thread or external request got
266aba1ca52SKonstantin Belousov 	 * here before us.  If so, act appropriately: exit or suspend.
267aba1ca52SKonstantin Belousov 	 * We must ensure that stop requests are handled before we set
268aba1ca52SKonstantin Belousov 	 * P_WEXIT.
269e602ba25SJulian Elischer 	 */
270e602ba25SJulian Elischer 	thread_suspend_check(0);
271aba1ca52SKonstantin Belousov 	while (p->p_flag & P_HADTHREADS) {
272e602ba25SJulian Elischer 		/*
273e602ba25SJulian Elischer 		 * Kill off the other threads. This requires
2746111dcd2SJohn Baldwin 		 * some co-operation from other parts of the kernel
2756111dcd2SJohn Baldwin 		 * so it may not be instantaneous.  With this state set
27630b16a6bSKonstantin Belousov 		 * any thread attempting to interruptibly
2776111dcd2SJohn Baldwin 		 * sleep will return immediately with EINTR or EWOULDBLOCK
2786111dcd2SJohn Baldwin 		 * which will hopefully force them to back out to userland
2796111dcd2SJohn Baldwin 		 * freeing resources as they go.  Any thread attempting
28030b16a6bSKonstantin Belousov 		 * to return to userland will thread_exit() from ast().
2816111dcd2SJohn Baldwin 		 * thread_exit() will unsuspend us when the last of the
2826111dcd2SJohn Baldwin 		 * other threads exits.
283b370279eSDavid Xu 		 * If there is already a thread singler after resumption,
28430b16a6bSKonstantin Belousov 		 * calling thread_single() will fail; in that case, we just
285b370279eSDavid Xu 		 * re-check all suspension request, the thread should
286b370279eSDavid Xu 		 * either be suspended there or exit.
287e602ba25SJulian Elischer 		 */
2886ddcc233SKonstantin Belousov 		if (!thread_single(p, SINGLE_EXIT))
289e602ba25SJulian Elischer 			/*
290aba1ca52SKonstantin Belousov 			 * All other activity in this process is now
291aba1ca52SKonstantin Belousov 			 * stopped.  Threading support has been turned
292aba1ca52SKonstantin Belousov 			 * off.
293e602ba25SJulian Elischer 			 */
294aba1ca52SKonstantin Belousov 			break;
295aba1ca52SKonstantin Belousov 		/*
296aba1ca52SKonstantin Belousov 		 * Recheck for new stop or suspend requests which
297aba1ca52SKonstantin Belousov 		 * might appear while process lock was dropped in
298aba1ca52SKonstantin Belousov 		 * thread_single().
299aba1ca52SKonstantin Belousov 		 */
300aba1ca52SKonstantin Belousov 		thread_suspend_check(0);
301e602ba25SJulian Elischer 	}
3021c4bcd05SJeff Roberson 	KASSERT(p->p_numthreads == 1,
3031c4bcd05SJeff Roberson 	    ("exit1: proc %p exiting with %d threads", p, p->p_numthreads));
30458c77a9dSEdward Tomasz Napierala 	racct_sub(p, RACCT_NTHR, 1);
305b4490c6eSKonstantin Belousov 
306b4490c6eSKonstantin Belousov 	/* Let event handler change exit status */
307b4490c6eSKonstantin Belousov 	p->p_xexit = rval;
308b4490c6eSKonstantin Belousov 	p->p_xsig = signo;
309b4490c6eSKonstantin Belousov 
31006ad42b2SJohn Baldwin 	/*
3110f14f15bSJohn Baldwin 	 * Ignore any pending request to stop due to a stop signal.
3120f14f15bSJohn Baldwin 	 * Once P_WEXIT is set, future requests will be ignored as
3130f14f15bSJohn Baldwin 	 * well.
3140f14f15bSJohn Baldwin 	 */
3150f14f15bSJohn Baldwin 	p->p_flag &= ~P_STOPPED_SIG;
3160f14f15bSJohn Baldwin 	KASSERT(!P_SHOULDSTOP(p), ("exiting process is stopped"));
3170f14f15bSJohn Baldwin 
31859838c1aSJohn Baldwin 	/* Note that we are exiting. */
319e602ba25SJulian Elischer 	p->p_flag |= P_WEXIT;
32006ad42b2SJohn Baldwin 
32106ad42b2SJohn Baldwin 	/*
32206ad42b2SJohn Baldwin 	 * Wait for any processes that have a hold on our vmspace to
32306ad42b2SJohn Baldwin 	 * release their reference.
32406ad42b2SJohn Baldwin 	 */
32506ad42b2SJohn Baldwin 	while (p->p_lock > 0)
32606ad42b2SJohn Baldwin 		msleep(&p->p_lock, &p->p_mtx, PWAIT, "exithold", 0);
327ebceaf6dSDavid Xu 
328e602ba25SJulian Elischer 	PROC_UNLOCK(p);
3291c4bcd05SJeff Roberson 	/* Drain the limit callout while we don't have the proc locked */
3301c4bcd05SJeff Roberson 	callout_drain(&p->p_limco);
331b40ce416SJulian Elischer 
33200c28d96SRobert Watson #ifdef AUDIT
33300c28d96SRobert Watson 	/*
33400c28d96SRobert Watson 	 * The Sun BSM exit token contains two components: an exit status as
33500c28d96SRobert Watson 	 * passed to exit(), and a return value to indicate what sort of exit
33600c28d96SRobert Watson 	 * it was.  The exit status is WEXITSTATUS(rv), but it's not clear
33700c28d96SRobert Watson 	 * what the return value is.
33800c28d96SRobert Watson 	 */
339b4490c6eSKonstantin Belousov 	AUDIT_ARG_EXIT(rval, 0);
34000c28d96SRobert Watson 	AUDIT_SYSCALL_EXIT(0, td);
34100c28d96SRobert Watson #endif
34200c28d96SRobert Watson 
343bad2520aSMateusz Guzik 	/* Are we a task leader with peers? */
344bad2520aSMateusz Guzik 	if (p->p_peers != NULL && p == p->p_leader) {
345c6544064SJohn Baldwin 		mtx_lock(&ppeers_lock);
3462c1011f7SJohn Dyson 		q = p->p_peers;
347776e0b36SJohn Baldwin 		while (q != NULL) {
348776e0b36SJohn Baldwin 			PROC_LOCK(q);
3498451d0ddSKip Macy 			kern_psignal(q, SIGKILL);
350776e0b36SJohn Baldwin 			PROC_UNLOCK(q);
3512c1011f7SJohn Dyson 			q = q->p_peers;
3522c1011f7SJohn Dyson 		}
353c6544064SJohn Baldwin 		while (p->p_peers != NULL)
354c6544064SJohn Baldwin 			msleep(p, &ppeers_lock, PWAIT, "exit1", 0);
355c6544064SJohn Baldwin 		mtx_unlock(&ppeers_lock);
3562c1011f7SJohn Dyson 	}
3572c1011f7SJohn Dyson 
358e68c6191SKonstantin Belousov 	itimers_exit(p);
359e68c6191SKonstantin Belousov 
360fed06968SJulian Elischer 	/*
361e9189611SPeter Wemm 	 * Check if any loadable modules need anything done at process exit.
362b4490c6eSKonstantin Belousov 	 * E.g. SYSV IPC stuff.
363b4490c6eSKonstantin Belousov 	 * Event handler could change exit status.
364fed06968SJulian Elischer 	 * XXX what if one of these generates an error?
365fed06968SJulian Elischer 	 */
3662ca45184SMatt Joras 	EVENTHANDLER_DIRECT_INVOKE(process_exit, p);
367a914fb6bSJohn Baldwin 
368e3d3e828SMateusz Guzik 	/*
369e3d3e828SMateusz Guzik 	 * If parent is waiting for us to exit or exec,
370e3d3e828SMateusz Guzik 	 * P_PPWAIT is set; we will wakeup the parent below.
371e3d3e828SMateusz Guzik 	 */
372a914fb6bSJohn Baldwin 	PROC_LOCK(p);
373a282253aSJulian Elischer 	stopprofclock(p);
3748d570f64SJohn Baldwin 	p->p_ptevents = 0;
3755499ea01SJohn Baldwin 
3765499ea01SJohn Baldwin 	/*
3775499ea01SJohn Baldwin 	 * Stop the real interval timer.  If the handler is currently
3785499ea01SJohn Baldwin 	 * executing, prevent it from rearming itself and let it finish.
3795499ea01SJohn Baldwin 	 */
3805499ea01SJohn Baldwin 	if (timevalisset(&p->p_realtimer.it_value) &&
381b3191718SMark Johnston 	    callout_stop(&p->p_itcallout) == 0) {
3825499ea01SJohn Baldwin 		timevalclear(&p->p_realtimer.it_interval);
38396d7f8efSTim J. Robbins 		PROC_UNLOCK(p);
384b3191718SMark Johnston 		callout_drain(&p->p_itcallout);
385b3191718SMark Johnston 	} else {
386b3191718SMark Johnston 		PROC_UNLOCK(p);
387b3191718SMark Johnston 	}
388df8bae1dSRodney W. Grimes 
38955976ce1SKonstantin Belousov 	if (p->p_sysent->sv_onexit != NULL)
39055976ce1SKonstantin Belousov 		p->p_sysent->sv_onexit(p);
39119d3e47dSMateusz Guzik 	seltdfini(td);
3922a339d9eSKonstantin Belousov 
393df8bae1dSRodney W. Grimes 	/*
394831d27a9SDon Lewis 	 * Reset any sigio structures pointing to us as a result of
395f5297909SMark Johnston 	 * F_SETOWN with our pid.  The P_WEXIT flag interlocks with fsetown().
396831d27a9SDon Lewis 	 */
397831d27a9SDon Lewis 	funsetownlst(&p->p_sigiolst);
398831d27a9SDon Lewis 
399831d27a9SDon Lewis 	/*
400df8bae1dSRodney W. Grimes 	 * Close open files and release open-file table.
401df8bae1dSRodney W. Grimes 	 * This may block!
402df8bae1dSRodney W. Grimes 	 */
40385078b85SConrad Meyer 	pdescfree(td);
4042609222aSPawel Jakub Dawidek 	fdescfree(td);
405df8bae1dSRodney W. Grimes 
406a914fb6bSJohn Baldwin 	/*
407a914fb6bSJohn Baldwin 	 * Remove ourself from our leader's peer list and wake our leader.
408a914fb6bSJohn Baldwin 	 */
409bad2520aSMateusz Guzik 	if (p->p_leader->p_peers != NULL) {
410c6544064SJohn Baldwin 		mtx_lock(&ppeers_lock);
411bad2520aSMateusz Guzik 		if (p->p_leader->p_peers != NULL) {
41279fc0bf4SMike Smith 			q = p->p_leader;
41379fc0bf4SMike Smith 			while (q->p_peers != p)
41479fc0bf4SMike Smith 				q = q->p_peers;
41579fc0bf4SMike Smith 			q->p_peers = p->p_peers;
4167f05b035SAlfred Perlstein 			wakeup(p->p_leader);
41779fc0bf4SMike Smith 		}
418c6544064SJohn Baldwin 		mtx_unlock(&ppeers_lock);
419bad2520aSMateusz Guzik 	}
42079fc0bf4SMike Smith 
4211c696903SKonstantin Belousov 	exec_free_abi_mappings(p);
42257051fdcSTor Egge 	vmspace_exit(td);
423b40ce416SJulian Elischer 	(void)acct_process(td);
424bc093719SEd Schouten 
425df8bae1dSRodney W. Grimes #ifdef KTRACE
4262c255e9dSRobert Watson 	ktrprocexit(td);
427df8bae1dSRodney W. Grimes #endif
428df8bae1dSRodney W. Grimes 	/*
429351d5f7fSKonstantin Belousov 	 * Release reference to text vnode etc
430ee42d0a9SDavid Malone 	 */
43188f98985SMateusz Guzik 	if (p->p_textvp != NULL) {
43288f98985SMateusz Guzik 		vrele(p->p_textvp);
433ee42d0a9SDavid Malone 		p->p_textvp = NULL;
434ee42d0a9SDavid Malone 	}
435351d5f7fSKonstantin Belousov 	if (p->p_textdvp != NULL) {
436351d5f7fSKonstantin Belousov 		vrele(p->p_textdvp);
437351d5f7fSKonstantin Belousov 		p->p_textdvp = NULL;
438351d5f7fSKonstantin Belousov 	}
439351d5f7fSKonstantin Belousov 	if (p->p_binname != NULL) {
440351d5f7fSKonstantin Belousov 		free(p->p_binname, M_PARGS);
441351d5f7fSKonstantin Belousov 		p->p_binname = NULL;
442351d5f7fSKonstantin Belousov 	}
443ee42d0a9SDavid Malone 
444ee42d0a9SDavid Malone 	/*
445d7aadbf9SJohn Baldwin 	 * Release our limits structure.
446d7aadbf9SJohn Baldwin 	 */
44788f98985SMateusz Guzik 	lim_free(p->p_limit);
448d7aadbf9SJohn Baldwin 	p->p_limit = NULL;
449d7aadbf9SJohn Baldwin 
450cf7d9a8cSDavid Xu 	tidhash_remove(td);
451cf7d9a8cSDavid Xu 
452d7aadbf9SJohn Baldwin 	/*
45381d68271SMateusz Guzik 	 * Call machine-dependent code to release any
45481d68271SMateusz Guzik 	 * machine-dependent resources other than the address space.
45581d68271SMateusz Guzik 	 * The address space is released by "vmspace_exitfree(p)" in
45681d68271SMateusz Guzik 	 * vm_waitproc().
45781d68271SMateusz Guzik 	 */
45881d68271SMateusz Guzik 	cpu_exit(td);
45981d68271SMateusz Guzik 
46081d68271SMateusz Guzik 	WITNESS_WARN(WARN_PANIC, NULL, "process (pid %d) exiting", p->p_pid);
46181d68271SMateusz Guzik 
46281d68271SMateusz Guzik 	/*
46388cc62e5SMateusz Guzik 	 * Remove from allproc. It still sits in the hash.
464df8bae1dSRodney W. Grimes 	 */
4651005a129SJohn Baldwin 	sx_xlock(&allproc_lock);
466b75356e1SJeffrey Hsu 	LIST_REMOVE(p, p_list);
46747877889SJohn Baldwin 
46847877889SJohn Baldwin #ifdef DDB
46947877889SJohn Baldwin 	/*
47047877889SJohn Baldwin 	 * Used by ddb's 'ps' command to find this process via the
47147877889SJohn Baldwin 	 * pidhash.
47247877889SJohn Baldwin 	 */
47347877889SJohn Baldwin 	p->p_list.le_prev = NULL;
47447877889SJohn Baldwin #endif
4755ecb5444SMateusz Guzik 	prison_proc_unlink(p->p_ucred->cr_prison, p);
4761005a129SJohn Baldwin 	sx_xunlock(&allproc_lock);
477df8bae1dSRodney W. Grimes 
478b1fbffe7SMateusz Guzik 	sx_xlock(&proctree_lock);
47980cf427bSMateusz Guzik 	if ((p->p_flag & (P_TRACED | P_PPWAIT | P_PPTRACE)) != 0) {
48041fadb3fSMariusz Zaborski 		PROC_LOCK(p);
48141fadb3fSMariusz Zaborski 		p->p_flag &= ~(P_TRACED | P_PPWAIT | P_PPTRACE);
48241fadb3fSMariusz Zaborski 		PROC_UNLOCK(p);
48380cf427bSMateusz Guzik 	}
484b1fbffe7SMateusz Guzik 
48537f84a60SJohn Baldwin 	/*
486c5bc28b2SKonstantin Belousov 	 * killjobc() might drop and re-acquire proctree_lock to
487c5bc28b2SKonstantin Belousov 	 * revoke control tty if exiting process was a session leader.
488c5bc28b2SKonstantin Belousov 	 */
489c5bc28b2SKonstantin Belousov 	killjobc();
490c5bc28b2SKonstantin Belousov 
491c5bc28b2SKonstantin Belousov 	/*
492a661bebeSMateusz Guzik 	 * Reparent all children processes:
493a661bebeSMateusz Guzik 	 * - traced ones to the original parent (or init if we are that parent)
494a661bebeSMateusz Guzik 	 * - the rest to init
49537f84a60SJohn Baldwin 	 */
4962e3c8fcbSPoul-Henning Kamp 	q = LIST_FIRST(&p->p_children);
497a914fb6bSJohn Baldwin 	if (q != NULL)		/* only need this if any child is S_ZOMB */
498237623b0SKonstantin Belousov 		wakeup(q->p_reaper);
499a914fb6bSJohn Baldwin 	for (; q != NULL; q = nq) {
5002e3c8fcbSPoul-Henning Kamp 		nq = LIST_NEXT(q, p_sibling);
501cc29f221SKonstantin Belousov 		ksi = ksiginfo_alloc(M_WAITOK);
502a914fb6bSJohn Baldwin 		PROC_LOCK(q);
5034ac9ae70SJulian Elischer 		q->p_sigparent = SIGCHLD;
504904c5ec4SDavid Xu 
505a05cfdf4SMariusz Zaborski 		if ((q->p_flag & P_TRACED) == 0) {
5062c054ce9SMateusz Guzik 			proc_reparent(q, q->p_reaper, true);
507545d3122SKonstantin Belousov 			if (q->p_state == PRS_ZOMBIE) {
5089a2dde80SKonstantin Belousov 				/*
5099a2dde80SKonstantin Belousov 				 * Inform reaper about the reparented
5109a2dde80SKonstantin Belousov 				 * zombie, since wait(2) has something
5119a2dde80SKonstantin Belousov 				 * new to report.  Guarantee queueing
5129a2dde80SKonstantin Belousov 				 * of the SIGCHLD signal, similar to
5139a2dde80SKonstantin Belousov 				 * the _exit() behaviour, by providing
5149a2dde80SKonstantin Belousov 				 * our ksiginfo.  Ksi is freed by the
5159a2dde80SKonstantin Belousov 				 * signal delivery.
5169a2dde80SKonstantin Belousov 				 */
5179a2dde80SKonstantin Belousov 				if (q->p_ksi == NULL) {
5189a2dde80SKonstantin Belousov 					ksi1 = NULL;
5199a2dde80SKonstantin Belousov 				} else {
5209a2dde80SKonstantin Belousov 					ksiginfo_copy(q->p_ksi, ksi);
5219a2dde80SKonstantin Belousov 					ksi->ksi_flags |= KSI_INS;
5229a2dde80SKonstantin Belousov 					ksi1 = ksi;
5239a2dde80SKonstantin Belousov 					ksi = NULL;
5249a2dde80SKonstantin Belousov 				}
525545d3122SKonstantin Belousov 				PROC_LOCK(q->p_reaper);
5269a2dde80SKonstantin Belousov 				pksignal(q->p_reaper, SIGCHLD, ksi1);
527545d3122SKonstantin Belousov 				PROC_UNLOCK(q->p_reaper);
528b9408863SKonstantin Belousov 			} else if (q->p_pdeathsig > 0) {
529b9408863SKonstantin Belousov 				/*
530b9408863SKonstantin Belousov 				 * The child asked to received a signal
531b9408863SKonstantin Belousov 				 * when we exit.
532b9408863SKonstantin Belousov 				 */
533b9408863SKonstantin Belousov 				kern_psignal(q, q->p_pdeathsig);
534545d3122SKonstantin Belousov 			}
535a661bebeSMateusz Guzik 		} else {
536a661bebeSMateusz Guzik 			/*
537fd3ac06fSKonstantin Belousov 			 * Traced processes are killed by default
538fd3ac06fSKonstantin Belousov 			 * since their existence means someone is
539fd3ac06fSKonstantin Belousov 			 * screwing up.
540a661bebeSMateusz Guzik 			 */
541a661bebeSMateusz Guzik 			t = proc_realparent(q);
542a661bebeSMateusz Guzik 			if (t == p) {
5432c054ce9SMateusz Guzik 				proc_reparent(q, q->p_reaper, true);
544a661bebeSMateusz Guzik 			} else {
545a661bebeSMateusz Guzik 				PROC_LOCK(t);
5462c054ce9SMateusz Guzik 				proc_reparent(q, t, true);
547a661bebeSMateusz Guzik 				PROC_UNLOCK(t);
548a661bebeSMateusz Guzik 			}
5495085ecb7SKonstantin Belousov 			/*
5505085ecb7SKonstantin Belousov 			 * Since q was found on our children list, the
5515085ecb7SKonstantin Belousov 			 * proc_reparent() call moved q to the orphan
5525085ecb7SKonstantin Belousov 			 * list due to present P_TRACED flag. Clear
5535085ecb7SKonstantin Belousov 			 * orphan link for q now while q is locked.
5545085ecb7SKonstantin Belousov 			 */
5559db97ca0SMariusz Zaborski 			proc_clear_orphan(q);
556fd3ac06fSKonstantin Belousov 			q->p_flag &= ~P_TRACED;
557b7a25e63SKonstantin Belousov 			q->p_flag2 &= ~P2_PTRACE_FSTP;
5588d570f64SJohn Baldwin 			q->p_ptevents = 0;
559fd3ac06fSKonstantin Belousov 			p->p_xthread = NULL;
560b7a25e63SKonstantin Belousov 			FOREACH_THREAD_IN_PROC(q, tdt) {
561b7a25e63SKonstantin Belousov 				tdt->td_dbgflags &= ~(TDB_SUSPEND | TDB_XSIG |
562b7a25e63SKonstantin Belousov 				    TDB_FSTP);
563fd3ac06fSKonstantin Belousov 				tdt->td_xsig = 0;
564b7a25e63SKonstantin Belousov 			}
565fd3ac06fSKonstantin Belousov 			if (kern_kill_on_dbg_exit) {
566fd3ac06fSKonstantin Belousov 				q->p_flag &= ~P_STOPPED_TRACE;
5678451d0ddSKip Macy 				kern_psignal(q, SIGKILL);
568fd3ac06fSKonstantin Belousov 			} else if ((q->p_flag & (P_STOPPED_TRACE |
569fd3ac06fSKonstantin Belousov 			    P_STOPPED_SIG)) != 0) {
570fd3ac06fSKonstantin Belousov 				sigqueue_delete_proc(q, SIGTRAP);
571fd3ac06fSKonstantin Belousov 				ptrace_unsuspend(q);
572fd3ac06fSKonstantin Belousov 			}
573c65437a3SJohn Baldwin 		}
574a914fb6bSJohn Baldwin 		PROC_UNLOCK(q);
5759a2dde80SKonstantin Belousov 		if (ksi != NULL)
5769a2dde80SKonstantin Belousov 			ksiginfo_free(ksi);
577df8bae1dSRodney W. Grimes 	}
578df8bae1dSRodney W. Grimes 
5795085ecb7SKonstantin Belousov 	/*
5805085ecb7SKonstantin Belousov 	 * Also get rid of our orphans.
5815085ecb7SKonstantin Belousov 	 */
5825085ecb7SKonstantin Belousov 	while ((q = LIST_FIRST(&p->p_orphans)) != NULL) {
5835085ecb7SKonstantin Belousov 		PROC_LOCK(q);
584128c9bc0SMark Johnston 		KASSERT(q->p_oppid == p->p_pid,
585128c9bc0SMark Johnston 		    ("orphan %p of %p has unexpected oppid %d", q, p,
586128c9bc0SMark Johnston 		    q->p_oppid));
587128c9bc0SMark Johnston 		q->p_oppid = q->p_reaper->p_pid;
588128c9bc0SMark Johnston 
589b9408863SKonstantin Belousov 		/*
590b9408863SKonstantin Belousov 		 * If we are the real parent of this process
591b9408863SKonstantin Belousov 		 * but it has been reparented to a debugger, then
592b9408863SKonstantin Belousov 		 * check if it asked for a signal when we exit.
593b9408863SKonstantin Belousov 		 */
594b9408863SKonstantin Belousov 		if (q->p_pdeathsig > 0)
595b9408863SKonstantin Belousov 			kern_psignal(q, q->p_pdeathsig);
596515b7a0bSJohn Baldwin 		CTR2(KTR_PTRACE, "exit: pid %d, clearing orphan %d", p->p_pid,
597515b7a0bSJohn Baldwin 		    q->p_pid);
5989db97ca0SMariusz Zaborski 		proc_clear_orphan(q);
5995085ecb7SKonstantin Belousov 		PROC_UNLOCK(q);
6005085ecb7SKonstantin Belousov 	}
6015085ecb7SKonstantin Belousov 
602eadb1dcbSMateusz Guzik #ifdef KDTRACE_HOOKS
603eadb1dcbSMateusz Guzik 	if (SDT_PROBES_ENABLED()) {
604eadb1dcbSMateusz Guzik 		int reason = CLD_EXITED;
605eadb1dcbSMateusz Guzik 		if (WCOREDUMP(signo))
606eadb1dcbSMateusz Guzik 			reason = CLD_DUMPED;
607eadb1dcbSMateusz Guzik 		else if (WIFSIGNALED(signo))
608eadb1dcbSMateusz Guzik 			reason = CLD_KILLED;
609eadb1dcbSMateusz Guzik 		SDT_PROBE1(proc, , , exit, reason);
610eadb1dcbSMateusz Guzik 	}
611eadb1dcbSMateusz Guzik #endif
612eadb1dcbSMateusz Guzik 
6131c4bcd05SJeff Roberson 	/* Save exit status. */
614d7aadbf9SJohn Baldwin 	PROC_LOCK(p);
615cbf4e354SDavid Xu 	p->p_xthread = td;
6165d217f17SJohn Birrell 
61787a9b18dSKonstantin Belousov 	if (p->p_sysent->sv_ontdexit != NULL)
61887a9b18dSKonstantin Belousov 		p->p_sysent->sv_ontdexit(td);
61987a9b18dSKonstantin Belousov 
6205d217f17SJohn Birrell #ifdef KDTRACE_HOOKS
6215d217f17SJohn Birrell 	/*
6225d217f17SJohn Birrell 	 * Tell the DTrace fasttrap provider about the exit if it
6235d217f17SJohn Birrell 	 * has declared an interest.
6245d217f17SJohn Birrell 	 */
6255d217f17SJohn Birrell 	if (dtrace_fasttrap_exit)
6265d217f17SJohn Birrell 		dtrace_fasttrap_exit(p);
6275d217f17SJohn Birrell #endif
6285d217f17SJohn Birrell 
6291c4bcd05SJeff Roberson 	/*
6307a6b989bSJohn Baldwin 	 * Notify interested parties of our demise.
631cb679c38SJonathan Lemon 	 */
6329e590ff0SKonstantin Belousov 	KNOTE_LOCKED(p->p_klist, NOTE_EXIT);
6337eaec467SJohn Baldwin 
6341a29c806SOlivier Houchard 	/*
635cfb5f768SJonathan Anderson 	 * If this is a process with a descriptor, we may not need to deliver
636cfb5f768SJonathan Anderson 	 * a signal to the parent.  proctree_lock is held over
637cfb5f768SJonathan Anderson 	 * procdesc_exit() to serialize concurrent calls to close() and
638cfb5f768SJonathan Anderson 	 * exit().
639cfb5f768SJonathan Anderson 	 */
640015cd8dcSMateusz Guzik 	signal_parent = 0;
641cfb5f768SJonathan Anderson 	if (p->p_procdesc == NULL || procdesc_exit(p)) {
642cfb5f768SJonathan Anderson 		/*
643cfb5f768SJonathan Anderson 		 * Notify parent that we're gone.  If parent has the
644cfb5f768SJonathan Anderson 		 * PS_NOCLDWAIT flag set, or if the handler is set to SIG_IGN,
645cfb5f768SJonathan Anderson 		 * notify process 1 instead (and hope it will handle this
646cfb5f768SJonathan Anderson 		 * situation).
647df8bae1dSRodney W. Grimes 		 */
648d7aadbf9SJohn Baldwin 		PROC_LOCK(p->p_pptr);
64990af4afaSJohn Baldwin 		mtx_lock(&p->p_pptr->p_sigacts->ps_mtx);
650cfb5f768SJonathan Anderson 		if (p->p_pptr->p_sigacts->ps_flag &
651cfb5f768SJonathan Anderson 		    (PS_NOCLDWAIT | PS_CLDSIGIGN)) {
652276c5169SJohn Baldwin 			struct proc *pp;
653276c5169SJohn Baldwin 
65490af4afaSJohn Baldwin 			mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx);
655276c5169SJohn Baldwin 			pp = p->p_pptr;
656f591779bSSeigo Tanimura 			PROC_UNLOCK(pp);
6572c054ce9SMateusz Guzik 			proc_reparent(p, p->p_reaper, true);
65855b5f2a2SDon Lewis 			p->p_sigparent = SIGCHLD;
659f591779bSSeigo Tanimura 			PROC_LOCK(p->p_pptr);
660007abb3dSKonstantin Belousov 
661245f17d4SJoerg Wunsch 			/*
662007abb3dSKonstantin Belousov 			 * Notify parent, so in case he was wait(2)ing or
6636fae832aSKonstantin Belousov 			 * executing waitpid(2) with our pid, he will
664245f17d4SJoerg Wunsch 			 * continue.
665245f17d4SJoerg Wunsch 			 */
6667f05b035SAlfred Perlstein 			wakeup(pp);
66790af4afaSJohn Baldwin 		} else
66890af4afaSJohn Baldwin 			mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx);
669245f17d4SJoerg Wunsch 
670015cd8dcSMateusz Guzik 		if (p->p_pptr == p->p_reaper || p->p_pptr == initproc) {
671015cd8dcSMateusz Guzik 			signal_parent = 1;
672015cd8dcSMateusz Guzik 		} else if (p->p_sigparent != 0) {
673015cd8dcSMateusz Guzik 			if (p->p_sigparent == SIGCHLD) {
674015cd8dcSMateusz Guzik 				signal_parent = 1;
675015cd8dcSMateusz Guzik 			} else { /* LINUX thread */
676015cd8dcSMateusz Guzik 				signal_parent = 2;
677015cd8dcSMateusz Guzik 			}
678ebceaf6dSDavid Xu 		}
679cfb5f768SJonathan Anderson 	} else
680cfb5f768SJonathan Anderson 		PROC_LOCK(p->p_pptr);
681d7aadbf9SJohn Baldwin 	sx_xunlock(&proctree_lock);
682696058c3SJulian Elischer 
683015cd8dcSMateusz Guzik 	if (signal_parent == 1) {
684015cd8dcSMateusz Guzik 		childproc_exited(p);
685015cd8dcSMateusz Guzik 	} else if (signal_parent == 2) {
686015cd8dcSMateusz Guzik 		kern_psignal(p->p_pptr, p->p_sigparent);
687015cd8dcSMateusz Guzik 	}
688015cd8dcSMateusz Guzik 
6897e588b92SMateusz Guzik 	/* Tell the prison that we are gone. */
6907e588b92SMateusz Guzik 	prison_proc_free(p->p_ucred->cr_prison);
6917e588b92SMateusz Guzik 
692eb30c1c0SPeter Wemm 	/*
693452661c9SIgor Ostapenko 	 * The state PRS_ZOMBIE prevents other processes from sending
694f71e748dSDavid Xu 	 * signal to the process, to avoid memory leak, we free memory
695f71e748dSDavid Xu 	 * for signal queue at the time when the state is set.
696f71e748dSDavid Xu 	 */
697f71e748dSDavid Xu 	sigqueue_flush(&p->p_sigqueue);
698f71e748dSDavid Xu 	sigqueue_flush(&td->td_sigqueue);
699f71e748dSDavid Xu 
700f71e748dSDavid Xu 	/*
701462f31bfSJohn Baldwin 	 * We have to wait until after acquiring all locks before
702a9a64385SJohn Baldwin 	 * changing p_state.  We need to avoid all possible context
703a9a64385SJohn Baldwin 	 * switches (including ones from blocking on a mutex) while
704ddf9c4f7SJohn Baldwin 	 * marked as a zombie.  We also have to set the zombie state
705ddf9c4f7SJohn Baldwin 	 * before we release the parent process' proc lock to avoid
706ddf9c4f7SJohn Baldwin 	 * a lost wakeup.  So, we first call wakeup, then we grab the
707ddf9c4f7SJohn Baldwin 	 * sched lock, update the state, and release the parent process'
708ddf9c4f7SJohn Baldwin 	 * proc lock.
709eb30c1c0SPeter Wemm 	 */
710ddf9c4f7SJohn Baldwin 	wakeup(p->p_pptr);
711e3d3e828SMateusz Guzik 	cv_broadcast(&p->p_pwait);
712982d11f8SJeff Roberson 	sched_exit(p->p_pptr, td);
713982d11f8SJeff Roberson 	PROC_SLOCK(p);
714e602ba25SJulian Elischer 	p->p_state = PRS_ZOMBIE;
715d7aadbf9SJohn Baldwin 	PROC_UNLOCK(p->p_pptr);
716871684b8SBruce Evans 
717f6f230feSJeff Roberson 	/*
718a140976eSAttilio Rao 	 * Save our children's rusage information in our exit rusage.
719a140976eSAttilio Rao 	 */
7205c7bebf9SKonstantin Belousov 	PROC_STATLOCK(p);
721a140976eSAttilio Rao 	ruadd(&p->p_ru, &p->p_rux, &p->p_stats->p_cru, &p->p_crux);
7225c7bebf9SKonstantin Belousov 	PROC_STATUNLOCK(p);
723a140976eSAttilio Rao 
724a140976eSAttilio Rao 	/*
725696058c3SJulian Elischer 	 * Make sure the scheduler takes this thread out of its tables etc.
726e602ba25SJulian Elischer 	 * This will also release this thread's reference to the ucred.
727696058c3SJulian Elischer 	 * Other thread parts to release include pcb bits and such.
728e602ba25SJulian Elischer 	 */
729e602ba25SJulian Elischer 	thread_exit();
730df8bae1dSRodney W. Grimes }
731df8bae1dSRodney W. Grimes 
73225f6e35aSPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_
73325f6e35aSPoul-Henning Kamp struct abort2_args {
73425f6e35aSPoul-Henning Kamp 	char *why;
73525f6e35aSPoul-Henning Kamp 	int nargs;
73625f6e35aSPoul-Henning Kamp 	void **args;
73725f6e35aSPoul-Henning Kamp };
73825f6e35aSPoul-Henning Kamp #endif
73925f6e35aSPoul-Henning Kamp 
74025f6e35aSPoul-Henning Kamp int
7418451d0ddSKip Macy sys_abort2(struct thread *td, struct abort2_args *uap)
74225f6e35aSPoul-Henning Kamp {
743e02f64d9SBrooks Davis 	void *uargs[16];
744e02f64d9SBrooks Davis 	void **uargsp;
745e02f64d9SBrooks Davis 	int error, nargs;
746e02f64d9SBrooks Davis 
747e02f64d9SBrooks Davis 	nargs = uap->nargs;
748e02f64d9SBrooks Davis 	if (nargs < 0 || nargs > nitems(uargs))
749e02f64d9SBrooks Davis 		nargs = -1;
750e02f64d9SBrooks Davis 	uargsp = NULL;
751e02f64d9SBrooks Davis 	if (nargs > 0) {
752e02f64d9SBrooks Davis 		if (uap->args != NULL) {
753e02f64d9SBrooks Davis 			error = copyin(uap->args, uargs,
754e02f64d9SBrooks Davis 			    nargs * sizeof(void *));
755e02f64d9SBrooks Davis 			if (error != 0)
756e02f64d9SBrooks Davis 				nargs = -1;
757e02f64d9SBrooks Davis 			else
758e02f64d9SBrooks Davis 				uargsp = uargs;
759e02f64d9SBrooks Davis 		} else
760e02f64d9SBrooks Davis 			nargs = -1;
761e02f64d9SBrooks Davis 	}
762e02f64d9SBrooks Davis 	return (kern_abort2(td, uap->why, nargs, uargsp));
763e02f64d9SBrooks Davis }
764e02f64d9SBrooks Davis 
765e02f64d9SBrooks Davis /*
766e02f64d9SBrooks Davis  * kern_abort2()
767e02f64d9SBrooks Davis  * Arguments:
768e02f64d9SBrooks Davis  *  why - user pointer to why
76949a033d8SGordon Bergling  *  nargs - number of arguments copied or -1 if an error occurred in copying
770e02f64d9SBrooks Davis  *  args - pointer to an array of pointers in kernel format
771e02f64d9SBrooks Davis  */
772e02f64d9SBrooks Davis int
773e02f64d9SBrooks Davis kern_abort2(struct thread *td, const char *why, int nargs, void **uargs)
774e02f64d9SBrooks Davis {
77525f6e35aSPoul-Henning Kamp 	struct proc *p = td->td_proc;
77625f6e35aSPoul-Henning Kamp 	struct sbuf *sb;
77725f6e35aSPoul-Henning Kamp 	int error, i, sig;
77825f6e35aSPoul-Henning Kamp 
77925f6e35aSPoul-Henning Kamp 	/*
78025f6e35aSPoul-Henning Kamp 	 * Do it right now so we can log either proper call of abort2(), or
78125f6e35aSPoul-Henning Kamp 	 * note, that invalid argument was passed. 512 is big enough to
78225f6e35aSPoul-Henning Kamp 	 * handle 16 arguments' descriptions with additional comments.
78325f6e35aSPoul-Henning Kamp 	 */
78425f6e35aSPoul-Henning Kamp 	sb = sbuf_new(NULL, NULL, 512, SBUF_FIXEDLEN);
78525f6e35aSPoul-Henning Kamp 	sbuf_clear(sb);
78625f6e35aSPoul-Henning Kamp 	sbuf_printf(sb, "%s(pid %d uid %d) aborted: ",
78725f6e35aSPoul-Henning Kamp 	    p->p_comm, p->p_pid, td->td_ucred->cr_uid);
78825f6e35aSPoul-Henning Kamp 	/*
78925f6e35aSPoul-Henning Kamp 	 * Since we can't return from abort2(), send SIGKILL in cases, where
79025f6e35aSPoul-Henning Kamp 	 * abort2() was called improperly
79125f6e35aSPoul-Henning Kamp 	 */
79225f6e35aSPoul-Henning Kamp 	sig = SIGKILL;
79325f6e35aSPoul-Henning Kamp 	/* Prevent from DoSes from user-space. */
794e02f64d9SBrooks Davis 	if (nargs == -1)
79525f6e35aSPoul-Henning Kamp 		goto out;
796e02f64d9SBrooks Davis 	KASSERT(nargs >= 0 && nargs <= 16, ("called with too many args (%d)",
797e02f64d9SBrooks Davis 	    nargs));
79825f6e35aSPoul-Henning Kamp 	/*
79925f6e35aSPoul-Henning Kamp 	 * Limit size of 'reason' string to 128. Will fit even when
80025f6e35aSPoul-Henning Kamp 	 * maximal number of arguments was chosen to be logged.
80125f6e35aSPoul-Henning Kamp 	 */
802e02f64d9SBrooks Davis 	if (why != NULL) {
803e02f64d9SBrooks Davis 		error = sbuf_copyin(sb, why, 128);
80425f6e35aSPoul-Henning Kamp 		if (error < 0)
80525f6e35aSPoul-Henning Kamp 			goto out;
80625f6e35aSPoul-Henning Kamp 	} else {
807*0a713948SAlexander Motin 		sbuf_cat(sb, "(null)");
80825f6e35aSPoul-Henning Kamp 	}
809e02f64d9SBrooks Davis 	if (nargs > 0) {
810*0a713948SAlexander Motin 		sbuf_putc(sb, '(');
811e02f64d9SBrooks Davis 		for (i = 0;i < nargs; i++)
81225f6e35aSPoul-Henning Kamp 			sbuf_printf(sb, "%s%p", i == 0 ? "" : ", ", uargs[i]);
813*0a713948SAlexander Motin 		sbuf_putc(sb, ')');
81425f6e35aSPoul-Henning Kamp 	}
81525f6e35aSPoul-Henning Kamp 	/*
81625f6e35aSPoul-Henning Kamp 	 * Final stage: arguments were proper, string has been
81725f6e35aSPoul-Henning Kamp 	 * successfully copied from userspace, and copying pointers
81825f6e35aSPoul-Henning Kamp 	 * from user-space succeed.
81925f6e35aSPoul-Henning Kamp 	 */
82025f6e35aSPoul-Henning Kamp 	sig = SIGABRT;
82125f6e35aSPoul-Henning Kamp out:
82225f6e35aSPoul-Henning Kamp 	if (sig == SIGKILL) {
82325f6e35aSPoul-Henning Kamp 		sbuf_trim(sb);
824*0a713948SAlexander Motin 		sbuf_cat(sb, " (Reason text inaccessible)");
82525f6e35aSPoul-Henning Kamp 	}
82625f6e35aSPoul-Henning Kamp 	sbuf_cat(sb, "\n");
82725f6e35aSPoul-Henning Kamp 	sbuf_finish(sb);
82825f6e35aSPoul-Henning Kamp 	log(LOG_INFO, "%s", sbuf_data(sb));
82925f6e35aSPoul-Henning Kamp 	sbuf_delete(sb);
8309b57e30cSJohn Baldwin 	PROC_LOCK(p);
8319b57e30cSJohn Baldwin 	sigexit(td, sig);
8329b57e30cSJohn Baldwin 	/* NOTREACHED */
83325f6e35aSPoul-Henning Kamp }
83425f6e35aSPoul-Henning Kamp 
835b2f9e8b1SBruce Evans #ifdef COMPAT_43
836234216efSMatthew Dillon /*
837a9a64385SJohn Baldwin  * The dirty work is handled by kern_wait().
838234216efSMatthew Dillon  */
83926f9a767SRodney W. Grimes int
840830c3153SDag-Erling Smørgrav owait(struct thread *td, struct owait_args *uap __unused)
841df8bae1dSRodney W. Grimes {
842b7e23e82SJohn Baldwin 	int error, status;
843df8bae1dSRodney W. Grimes 
844b7e23e82SJohn Baldwin 	error = kern_wait(td, WAIT_ANY, &status, 0, NULL);
845b7e23e82SJohn Baldwin 	if (error == 0)
846b7e23e82SJohn Baldwin 		td->td_retval[1] = status;
847b7e23e82SJohn Baldwin 	return (error);
848df8bae1dSRodney W. Grimes }
849b2f9e8b1SBruce Evans #endif /* COMPAT_43 */
850df8bae1dSRodney W. Grimes 
851234216efSMatthew Dillon /*
852a9a64385SJohn Baldwin  * The dirty work is handled by kern_wait().
853234216efSMatthew Dillon  */
85426f9a767SRodney W. Grimes int
855f13b5a0fSKonstantin Belousov sys_wait4(struct thread *td, struct wait4_args *uap)
856df8bae1dSRodney W. Grimes {
85778c85e8dSJohn Baldwin 	struct rusage ru, *rup;
858b7e23e82SJohn Baldwin 	int error, status;
859b40ce416SJulian Elischer 
86078c85e8dSJohn Baldwin 	if (uap->rusage != NULL)
86178c85e8dSJohn Baldwin 		rup = &ru;
86278c85e8dSJohn Baldwin 	else
86378c85e8dSJohn Baldwin 		rup = NULL;
86478c85e8dSJohn Baldwin 	error = kern_wait(td, uap->pid, &status, uap->options, rup);
865d30e66e5SJilles Tjoelker 	if (uap->status != NULL && error == 0 && td->td_retval[0] != 0)
866b7e23e82SJohn Baldwin 		error = copyout(&status, uap->status, sizeof(status));
867d30e66e5SJilles Tjoelker 	if (uap->rusage != NULL && error == 0 && td->td_retval[0] != 0)
868b7e23e82SJohn Baldwin 		error = copyout(&ru, uap->rusage, sizeof(struct rusage));
869b7e23e82SJohn Baldwin 	return (error);
870df8bae1dSRodney W. Grimes }
871df8bae1dSRodney W. Grimes 
872f13b5a0fSKonstantin Belousov int
873f13b5a0fSKonstantin Belousov sys_wait6(struct thread *td, struct wait6_args *uap)
874f13b5a0fSKonstantin Belousov {
875f13b5a0fSKonstantin Belousov 	struct __wrusage wru, *wrup;
876f13b5a0fSKonstantin Belousov 	siginfo_t si, *sip;
877f13b5a0fSKonstantin Belousov 	idtype_t idtype;
878f13b5a0fSKonstantin Belousov 	id_t id;
879a2a85596SKonstantin Belousov 	int error, status;
880f13b5a0fSKonstantin Belousov 
881f13b5a0fSKonstantin Belousov 	idtype = uap->idtype;
882f13b5a0fSKonstantin Belousov 	id = uap->id;
883f13b5a0fSKonstantin Belousov 
884f13b5a0fSKonstantin Belousov 	if (uap->wrusage != NULL)
885f13b5a0fSKonstantin Belousov 		wrup = &wru;
886f13b5a0fSKonstantin Belousov 	else
887f13b5a0fSKonstantin Belousov 		wrup = NULL;
888f13b5a0fSKonstantin Belousov 
889f13b5a0fSKonstantin Belousov 	if (uap->info != NULL) {
890f13b5a0fSKonstantin Belousov 		sip = &si;
891f13b5a0fSKonstantin Belousov 		bzero(sip, sizeof(*sip));
892f13b5a0fSKonstantin Belousov 	} else
893f13b5a0fSKonstantin Belousov 		sip = NULL;
894f13b5a0fSKonstantin Belousov 
895f13b5a0fSKonstantin Belousov 	/*
896f13b5a0fSKonstantin Belousov 	 *  We expect all callers of wait6() to know about WEXITED and
897f13b5a0fSKonstantin Belousov 	 *  WTRAPPED.
898f13b5a0fSKonstantin Belousov 	 */
899f13b5a0fSKonstantin Belousov 	error = kern_wait6(td, idtype, id, &status, uap->options, wrup, sip);
900f13b5a0fSKonstantin Belousov 
901d30e66e5SJilles Tjoelker 	if (uap->status != NULL && error == 0 && td->td_retval[0] != 0)
902f13b5a0fSKonstantin Belousov 		error = copyout(&status, uap->status, sizeof(status));
903d30e66e5SJilles Tjoelker 	if (uap->wrusage != NULL && error == 0 && td->td_retval[0] != 0)
904f13b5a0fSKonstantin Belousov 		error = copyout(&wru, uap->wrusage, sizeof(wru));
905f13b5a0fSKonstantin Belousov 	if (uap->info != NULL && error == 0)
906f13b5a0fSKonstantin Belousov 		error = copyout(&si, uap->info, sizeof(si));
907f13b5a0fSKonstantin Belousov 	return (error);
908f13b5a0fSKonstantin Belousov }
909f13b5a0fSKonstantin Belousov 
910324fb6beSRobert Watson /*
911324fb6beSRobert Watson  * Reap the remains of a zombie process and optionally return status and
912324fb6beSRobert Watson  * rusage.  Asserts and will release both the proctree_lock and the process
913324fb6beSRobert Watson  * lock as part of its work.
914324fb6beSRobert Watson  */
915cfb5f768SJonathan Anderson void
916f13b5a0fSKonstantin Belousov proc_reap(struct thread *td, struct proc *p, int *status, int options)
917324fb6beSRobert Watson {
918324fb6beSRobert Watson 	struct proc *q, *t;
919324fb6beSRobert Watson 
920324fb6beSRobert Watson 	sx_assert(&proctree_lock, SA_XLOCKED);
921324fb6beSRobert Watson 	PROC_LOCK_ASSERT(p, MA_OWNED);
922324fb6beSRobert Watson 	KASSERT(p->p_state == PRS_ZOMBIE, ("proc_reap: !PRS_ZOMBIE"));
923324fb6beSRobert Watson 
9242ca66c1eSMateusz Guzik 	mtx_spin_wait_unlocked(&p->p_slock);
9252ca66c1eSMateusz Guzik 
926324fb6beSRobert Watson 	q = td->td_proc;
927f13b5a0fSKonstantin Belousov 
928324fb6beSRobert Watson 	if (status)
929b4490c6eSKonstantin Belousov 		*status = KW_EXITCODE(p->p_xexit, p->p_xsig);
930324fb6beSRobert Watson 	if (options & WNOWAIT) {
931324fb6beSRobert Watson 		/*
932324fb6beSRobert Watson 		 *  Only poll, returning the status.  Caller does not wish to
933324fb6beSRobert Watson 		 * release the proc struct just yet.
934324fb6beSRobert Watson 		 */
935324fb6beSRobert Watson 		PROC_UNLOCK(p);
936324fb6beSRobert Watson 		sx_xunlock(&proctree_lock);
937324fb6beSRobert Watson 		return;
938324fb6beSRobert Watson 	}
939324fb6beSRobert Watson 
940324fb6beSRobert Watson 	PROC_LOCK(q);
941324fb6beSRobert Watson 	sigqueue_take(p->p_ksi);
942324fb6beSRobert Watson 	PROC_UNLOCK(q);
943324fb6beSRobert Watson 
944324fb6beSRobert Watson 	/*
945324fb6beSRobert Watson 	 * If we got the child via a ptrace 'attach', we need to give it back
946324fb6beSRobert Watson 	 * to the old parent.
947324fb6beSRobert Watson 	 */
9482c054ce9SMateusz Guzik 	if (p->p_oppid != p->p_pptr->p_pid) {
949c209e3e2SJohn Baldwin 		PROC_UNLOCK(p);
950d7359980SKonstantin Belousov 		t = proc_realparent(p);
951d7359980SKonstantin Belousov 		PROC_LOCK(t);
952324fb6beSRobert Watson 		PROC_LOCK(p);
953515b7a0bSJohn Baldwin 		CTR2(KTR_PTRACE,
954515b7a0bSJohn Baldwin 		    "wait: traced child %d moved back to parent %d", p->p_pid,
955515b7a0bSJohn Baldwin 		    t->p_pid);
9562c054ce9SMateusz Guzik 		proc_reparent(p, t, false);
957324fb6beSRobert Watson 		PROC_UNLOCK(p);
958ad6eec7bSJohn Baldwin 		pksignal(t, SIGCHLD, p->p_ksi);
959324fb6beSRobert Watson 		wakeup(t);
960324fb6beSRobert Watson 		cv_broadcast(&p->p_pwait);
961324fb6beSRobert Watson 		PROC_UNLOCK(t);
962324fb6beSRobert Watson 		sx_xunlock(&proctree_lock);
963324fb6beSRobert Watson 		return;
964324fb6beSRobert Watson 	}
965c209e3e2SJohn Baldwin 	PROC_UNLOCK(p);
966324fb6beSRobert Watson 
967324fb6beSRobert Watson 	/*
968324fb6beSRobert Watson 	 * Remove other references to this process to ensure we have an
969324fb6beSRobert Watson 	 * exclusive reference.
970324fb6beSRobert Watson 	 */
9713d3e6793SMateusz Guzik 	sx_xlock(PIDHASHLOCK(p->p_pid));
9723d3e6793SMateusz Guzik 	LIST_REMOVE(p, p_hash);
9733d3e6793SMateusz Guzik 	sx_xunlock(PIDHASHLOCK(p->p_pid));
974324fb6beSRobert Watson 	LIST_REMOVE(p, p_sibling);
975237623b0SKonstantin Belousov 	reaper_abandon_children(p, true);
97634ebdceaSMateusz Guzik 	reaper_clear(p);
9777335ed90SKonstantin Belousov 	PROC_LOCK(p);
9789db97ca0SMariusz Zaborski 	proc_clear_orphan(p);
9797335ed90SKonstantin Belousov 	PROC_UNLOCK(p);
980324fb6beSRobert Watson 	leavepgrp(p);
981cfb5f768SJonathan Anderson 	if (p->p_procdesc != NULL)
982cfb5f768SJonathan Anderson 		procdesc_reap(p);
983324fb6beSRobert Watson 	sx_xunlock(&proctree_lock);
984324fb6beSRobert Watson 
9859576ff58SMateusz Guzik 	proc_id_clear(PROC_ID_PID, p->p_pid);
9869576ff58SMateusz Guzik 
9879e590ff0SKonstantin Belousov 	PROC_LOCK(p);
9889e590ff0SKonstantin Belousov 	knlist_detach(p->p_klist);
9899e590ff0SKonstantin Belousov 	p->p_klist = NULL;
9909e590ff0SKonstantin Belousov 	PROC_UNLOCK(p);
9919e590ff0SKonstantin Belousov 
992324fb6beSRobert Watson 	/*
993fbe503d4SMateusz Guzik 	 * Removal from allproc list and process group list paired with
994fbe503d4SMateusz Guzik 	 * PROC_LOCK which was executed during that time should guarantee
995fbe503d4SMateusz Guzik 	 * nothing can reach this process anymore. As such further locking
996fbe503d4SMateusz Guzik 	 * is unnecessary.
997324fb6beSRobert Watson 	 */
998b4490c6eSKonstantin Belousov 	p->p_xexit = p->p_xsig = 0;		/* XXX: why? */
999fbe503d4SMateusz Guzik 
1000324fb6beSRobert Watson 	PROC_LOCK(q);
1001324fb6beSRobert Watson 	ruadd(&q->p_stats->p_cru, &q->p_crux, &p->p_ru, &p->p_rux);
1002324fb6beSRobert Watson 	PROC_UNLOCK(q);
1003324fb6beSRobert Watson 
1004324fb6beSRobert Watson 	/*
1005324fb6beSRobert Watson 	 * Decrement the count of procs running with this uid.
1006324fb6beSRobert Watson 	 */
1007324fb6beSRobert Watson 	(void)chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0);
1008324fb6beSRobert Watson 
1009324fb6beSRobert Watson 	/*
1010097055e2SEdward Tomasz Napierala 	 * Destroy resource accounting information associated with the process.
1011097055e2SEdward Tomasz Napierala 	 */
1012afcc55f3SEdward Tomasz Napierala #ifdef RACCT
10134b5c9cf6SEdward Tomasz Napierala 	if (racct_enable) {
1014b38520f0SEdward Tomasz Napierala 		PROC_LOCK(p);
1015b38520f0SEdward Tomasz Napierala 		racct_sub(p, RACCT_NPROC, 1);
1016b38520f0SEdward Tomasz Napierala 		PROC_UNLOCK(p);
10174b5c9cf6SEdward Tomasz Napierala 	}
1018afcc55f3SEdward Tomasz Napierala #endif
1019b38520f0SEdward Tomasz Napierala 	racct_proc_exit(p);
1020097055e2SEdward Tomasz Napierala 
1021097055e2SEdward Tomasz Napierala 	/*
1022324fb6beSRobert Watson 	 * Free credentials, arguments, and sigacts.
1023324fb6beSRobert Watson 	 */
10245a90435cSMateusz Guzik 	proc_unset_cred(p);
1025324fb6beSRobert Watson 	pargs_drop(p->p_args);
1026324fb6beSRobert Watson 	p->p_args = NULL;
1027324fb6beSRobert Watson 	sigacts_free(p->p_sigacts);
1028324fb6beSRobert Watson 	p->p_sigacts = NULL;
1029324fb6beSRobert Watson 
1030324fb6beSRobert Watson 	/*
1031324fb6beSRobert Watson 	 * Do any thread-system specific cleanups.
1032324fb6beSRobert Watson 	 */
1033324fb6beSRobert Watson 	thread_wait(p);
1034324fb6beSRobert Watson 
1035324fb6beSRobert Watson 	/*
1036324fb6beSRobert Watson 	 * Give vm and machine-dependent layer a chance to free anything that
1037324fb6beSRobert Watson 	 * cpu_exit couldn't release while still running in process context.
1038324fb6beSRobert Watson 	 */
1039324fb6beSRobert Watson 	vm_waitproc(p);
1040324fb6beSRobert Watson #ifdef MAC
1041324fb6beSRobert Watson 	mac_proc_destroy(p);
1042324fb6beSRobert Watson #endif
10436520495aSAdrian Chadd 
1044324fb6beSRobert Watson 	KASSERT(FIRST_THREAD_IN_PROC(p),
1045324fb6beSRobert Watson 	    ("proc_reap: no residual thread!"));
1046324fb6beSRobert Watson 	uma_zfree(proc_zone, p);
10474b48959fSKonstantin Belousov 	atomic_add_int(&nprocs, -1);
1048324fb6beSRobert Watson }
1049324fb6beSRobert Watson 
1050dcd43281SKonstantin Belousov static int
1051f13b5a0fSKonstantin Belousov proc_to_reap(struct thread *td, struct proc *p, idtype_t idtype, id_t id,
105257c74f5bSJohn Baldwin     int *status, int options, struct __wrusage *wrusage, siginfo_t *siginfo,
105357c74f5bSJohn Baldwin     int check_only)
1054dcd43281SKonstantin Belousov {
1055f13b5a0fSKonstantin Belousov 	struct rusage *rup;
1056dcd43281SKonstantin Belousov 
1057db62ced2SJaakko Heinonen 	sx_assert(&proctree_lock, SA_XLOCKED);
1058db62ced2SJaakko Heinonen 
1059dcd43281SKonstantin Belousov 	PROC_LOCK(p);
1060f13b5a0fSKonstantin Belousov 
1061f13b5a0fSKonstantin Belousov 	switch (idtype) {
1062f13b5a0fSKonstantin Belousov 	case P_ALL:
10638e493611SMariusz Zaborski 		if (p->p_procdesc == NULL ||
10648e493611SMariusz Zaborski 		   (p->p_pptr == td->td_proc &&
10658e493611SMariusz Zaborski 		   (p->p_flag & P_TRACED) != 0)) {
10668e493611SMariusz Zaborski 			break;
10678e493611SMariusz Zaborski 		}
10688e493611SMariusz Zaborski 
106953bf545dSMariusz Zaborski 		PROC_UNLOCK(p);
107053bf545dSMariusz Zaborski 		return (0);
1071f13b5a0fSKonstantin Belousov 	case P_PID:
1072f13b5a0fSKonstantin Belousov 		if (p->p_pid != (pid_t)id) {
1073dcd43281SKonstantin Belousov 			PROC_UNLOCK(p);
1074dcd43281SKonstantin Belousov 			return (0);
1075dcd43281SKonstantin Belousov 		}
1076f13b5a0fSKonstantin Belousov 		break;
1077f13b5a0fSKonstantin Belousov 	case P_PGID:
1078f13b5a0fSKonstantin Belousov 		if (p->p_pgid != (pid_t)id) {
1079f13b5a0fSKonstantin Belousov 			PROC_UNLOCK(p);
1080f13b5a0fSKonstantin Belousov 			return (0);
1081f13b5a0fSKonstantin Belousov 		}
1082f13b5a0fSKonstantin Belousov 		break;
1083f13b5a0fSKonstantin Belousov 	case P_SID:
1084f13b5a0fSKonstantin Belousov 		if (p->p_session->s_sid != (pid_t)id) {
1085f13b5a0fSKonstantin Belousov 			PROC_UNLOCK(p);
1086f13b5a0fSKonstantin Belousov 			return (0);
1087f13b5a0fSKonstantin Belousov 		}
1088f13b5a0fSKonstantin Belousov 		break;
1089f13b5a0fSKonstantin Belousov 	case P_UID:
1090f13b5a0fSKonstantin Belousov 		if (p->p_ucred->cr_uid != (uid_t)id) {
1091f13b5a0fSKonstantin Belousov 			PROC_UNLOCK(p);
1092f13b5a0fSKonstantin Belousov 			return (0);
1093f13b5a0fSKonstantin Belousov 		}
1094f13b5a0fSKonstantin Belousov 		break;
1095f13b5a0fSKonstantin Belousov 	case P_GID:
1096f13b5a0fSKonstantin Belousov 		if (p->p_ucred->cr_gid != (gid_t)id) {
1097f13b5a0fSKonstantin Belousov 			PROC_UNLOCK(p);
1098f13b5a0fSKonstantin Belousov 			return (0);
1099f13b5a0fSKonstantin Belousov 		}
1100f13b5a0fSKonstantin Belousov 		break;
1101f13b5a0fSKonstantin Belousov 	case P_JAILID:
1102b1051d92SMateusz Guzik 		if (p->p_ucred->cr_prison->pr_id != (int)id) {
1103f13b5a0fSKonstantin Belousov 			PROC_UNLOCK(p);
1104f13b5a0fSKonstantin Belousov 			return (0);
1105f13b5a0fSKonstantin Belousov 		}
1106f13b5a0fSKonstantin Belousov 		break;
1107f13b5a0fSKonstantin Belousov 	/*
1108f13b5a0fSKonstantin Belousov 	 * It seems that the thread structures get zeroed out
1109f13b5a0fSKonstantin Belousov 	 * at process exit.  This makes it impossible to
1110f13b5a0fSKonstantin Belousov 	 * support P_SETID, P_CID or P_CPUID.
1111f13b5a0fSKonstantin Belousov 	 */
1112f13b5a0fSKonstantin Belousov 	default:
1113f13b5a0fSKonstantin Belousov 		PROC_UNLOCK(p);
1114f13b5a0fSKonstantin Belousov 		return (0);
1115f13b5a0fSKonstantin Belousov 	}
1116f13b5a0fSKonstantin Belousov 
1117dcd43281SKonstantin Belousov 	if (p_canwait(td, p)) {
1118dcd43281SKonstantin Belousov 		PROC_UNLOCK(p);
1119dcd43281SKonstantin Belousov 		return (0);
1120dcd43281SKonstantin Belousov 	}
1121dcd43281SKonstantin Belousov 
1122f13b5a0fSKonstantin Belousov 	if (((options & WEXITED) == 0) && (p->p_state == PRS_ZOMBIE)) {
1123f13b5a0fSKonstantin Belousov 		PROC_UNLOCK(p);
1124f13b5a0fSKonstantin Belousov 		return (0);
1125f13b5a0fSKonstantin Belousov 	}
1126f13b5a0fSKonstantin Belousov 
1127dcd43281SKonstantin Belousov 	/*
1128dcd43281SKonstantin Belousov 	 * This special case handles a kthread spawned by linux_clone
1129dcd43281SKonstantin Belousov 	 * (see linux_misc.c).  The linux_wait4 and linux_waitpid
1130dcd43281SKonstantin Belousov 	 * functions need to be able to distinguish between waiting
1131dcd43281SKonstantin Belousov 	 * on a process and waiting on a thread.  It is a thread if
1132dcd43281SKonstantin Belousov 	 * p_sigparent is not SIGCHLD, and the WLINUXCLONE option
1133dcd43281SKonstantin Belousov 	 * signifies we want to wait for threads and not processes.
1134dcd43281SKonstantin Belousov 	 */
1135dcd43281SKonstantin Belousov 	if ((p->p_sigparent != SIGCHLD) ^
1136dcd43281SKonstantin Belousov 	    ((options & WLINUXCLONE) != 0)) {
1137dcd43281SKonstantin Belousov 		PROC_UNLOCK(p);
1138dcd43281SKonstantin Belousov 		return (0);
1139dcd43281SKonstantin Belousov 	}
1140dcd43281SKonstantin Belousov 
1141f13b5a0fSKonstantin Belousov 	if (siginfo != NULL) {
1142f13b5a0fSKonstantin Belousov 		bzero(siginfo, sizeof(*siginfo));
1143f13b5a0fSKonstantin Belousov 		siginfo->si_errno = 0;
1144f13b5a0fSKonstantin Belousov 
1145f13b5a0fSKonstantin Belousov 		/*
1146f13b5a0fSKonstantin Belousov 		 * SUSv4 requires that the si_signo value is always
1147f13b5a0fSKonstantin Belousov 		 * SIGCHLD. Obey it despite the rfork(2) interface
1148f13b5a0fSKonstantin Belousov 		 * allows to request other signal for child exit
1149f13b5a0fSKonstantin Belousov 		 * notification.
1150f13b5a0fSKonstantin Belousov 		 */
1151f13b5a0fSKonstantin Belousov 		siginfo->si_signo = SIGCHLD;
1152f13b5a0fSKonstantin Belousov 
1153f13b5a0fSKonstantin Belousov 		/*
1154f13b5a0fSKonstantin Belousov 		 *  This is still a rough estimate.  We will fix the
1155f13b5a0fSKonstantin Belousov 		 *  cases TRAPPED, STOPPED, and CONTINUED later.
1156f13b5a0fSKonstantin Belousov 		 */
1157b4490c6eSKonstantin Belousov 		if (WCOREDUMP(p->p_xsig)) {
1158f13b5a0fSKonstantin Belousov 			siginfo->si_code = CLD_DUMPED;
1159b4490c6eSKonstantin Belousov 			siginfo->si_status = WTERMSIG(p->p_xsig);
1160b4490c6eSKonstantin Belousov 		} else if (WIFSIGNALED(p->p_xsig)) {
1161f13b5a0fSKonstantin Belousov 			siginfo->si_code = CLD_KILLED;
1162b4490c6eSKonstantin Belousov 			siginfo->si_status = WTERMSIG(p->p_xsig);
1163b20a9aa9SJilles Tjoelker 		} else {
1164f13b5a0fSKonstantin Belousov 			siginfo->si_code = CLD_EXITED;
1165b4490c6eSKonstantin Belousov 			siginfo->si_status = p->p_xexit;
1166b20a9aa9SJilles Tjoelker 		}
1167f13b5a0fSKonstantin Belousov 
1168f13b5a0fSKonstantin Belousov 		siginfo->si_pid = p->p_pid;
1169f13b5a0fSKonstantin Belousov 		siginfo->si_uid = p->p_ucred->cr_uid;
1170f13b5a0fSKonstantin Belousov 
1171f13b5a0fSKonstantin Belousov 		/*
1172f13b5a0fSKonstantin Belousov 		 * The si_addr field would be useful additional
1173f13b5a0fSKonstantin Belousov 		 * detail, but apparently the PC value may be lost
1174f13b5a0fSKonstantin Belousov 		 * when we reach this point.  bzero() above sets
1175f13b5a0fSKonstantin Belousov 		 * siginfo->si_addr to NULL.
1176f13b5a0fSKonstantin Belousov 		 */
1177f13b5a0fSKonstantin Belousov 	}
1178f13b5a0fSKonstantin Belousov 
1179f13b5a0fSKonstantin Belousov 	/*
1180f13b5a0fSKonstantin Belousov 	 * There should be no reason to limit resources usage info to
1181f13b5a0fSKonstantin Belousov 	 * exited processes only.  A snapshot about any resources used
1182f13b5a0fSKonstantin Belousov 	 * by a stopped process may be exactly what is needed.
1183f13b5a0fSKonstantin Belousov 	 */
1184f13b5a0fSKonstantin Belousov 	if (wrusage != NULL) {
1185f13b5a0fSKonstantin Belousov 		rup = &wrusage->wru_self;
1186f13b5a0fSKonstantin Belousov 		*rup = p->p_ru;
11875c7bebf9SKonstantin Belousov 		PROC_STATLOCK(p);
1188f13b5a0fSKonstantin Belousov 		calcru(p, &rup->ru_utime, &rup->ru_stime);
11895c7bebf9SKonstantin Belousov 		PROC_STATUNLOCK(p);
1190f13b5a0fSKonstantin Belousov 
1191f13b5a0fSKonstantin Belousov 		rup = &wrusage->wru_children;
1192f13b5a0fSKonstantin Belousov 		*rup = p->p_stats->p_cru;
1193f13b5a0fSKonstantin Belousov 		calccru(p, &rup->ru_utime, &rup->ru_stime);
1194f13b5a0fSKonstantin Belousov 	}
1195f13b5a0fSKonstantin Belousov 
119657c74f5bSJohn Baldwin 	if (p->p_state == PRS_ZOMBIE && !check_only) {
1197f13b5a0fSKonstantin Belousov 		proc_reap(td, p, status, options);
1198dcd43281SKonstantin Belousov 		return (-1);
1199dcd43281SKonstantin Belousov 	}
1200dcd43281SKonstantin Belousov 	return (1);
1201dcd43281SKonstantin Belousov }
1202dcd43281SKonstantin Belousov 
1203b7e23e82SJohn Baldwin int
12047eaec467SJohn Baldwin kern_wait(struct thread *td, pid_t pid, int *status, int options,
12057eaec467SJohn Baldwin     struct rusage *rusage)
1206df8bae1dSRodney W. Grimes {
1207f13b5a0fSKonstantin Belousov 	struct __wrusage wru, *wrup;
1208f13b5a0fSKonstantin Belousov 	idtype_t idtype;
1209f13b5a0fSKonstantin Belousov 	id_t id;
1210f13b5a0fSKonstantin Belousov 	int ret;
1211f13b5a0fSKonstantin Belousov 
1212538375d4SKonstantin Belousov 	/*
1213538375d4SKonstantin Belousov 	 * Translate the special pid values into the (idtype, pid)
1214538375d4SKonstantin Belousov 	 * pair for kern_wait6.  The WAIT_MYPGRP case is handled by
1215538375d4SKonstantin Belousov 	 * kern_wait6() on its own.
1216538375d4SKonstantin Belousov 	 */
1217f13b5a0fSKonstantin Belousov 	if (pid == WAIT_ANY) {
1218f13b5a0fSKonstantin Belousov 		idtype = P_ALL;
1219f13b5a0fSKonstantin Belousov 		id = 0;
1220ea293f3fSKonstantin Belousov 	} else if (pid < 0) {
1221f13b5a0fSKonstantin Belousov 		idtype = P_PGID;
1222f13b5a0fSKonstantin Belousov 		id = (id_t)-pid;
1223ea293f3fSKonstantin Belousov 	} else {
1224f13b5a0fSKonstantin Belousov 		idtype = P_PID;
1225f13b5a0fSKonstantin Belousov 		id = (id_t)pid;
1226f13b5a0fSKonstantin Belousov 	}
1227538375d4SKonstantin Belousov 
1228f13b5a0fSKonstantin Belousov 	if (rusage != NULL)
1229f13b5a0fSKonstantin Belousov 		wrup = &wru;
1230f13b5a0fSKonstantin Belousov 	else
1231f13b5a0fSKonstantin Belousov 		wrup = NULL;
1232538375d4SKonstantin Belousov 
1233f13b5a0fSKonstantin Belousov 	/*
1234f13b5a0fSKonstantin Belousov 	 * For backward compatibility we implicitly add flags WEXITED
1235f13b5a0fSKonstantin Belousov 	 * and WTRAPPED here.
1236f13b5a0fSKonstantin Belousov 	 */
1237f13b5a0fSKonstantin Belousov 	options |= WEXITED | WTRAPPED;
1238f13b5a0fSKonstantin Belousov 	ret = kern_wait6(td, idtype, id, status, options, wrup, NULL);
1239f13b5a0fSKonstantin Belousov 	if (rusage != NULL)
1240f13b5a0fSKonstantin Belousov 		*rusage = wru.wru_self;
1241f13b5a0fSKonstantin Belousov 	return (ret);
1242f13b5a0fSKonstantin Belousov }
1243f13b5a0fSKonstantin Belousov 
1244435da985SKonstantin Belousov static void
1245435da985SKonstantin Belousov report_alive_proc(struct thread *td, struct proc *p, siginfo_t *siginfo,
1246435da985SKonstantin Belousov     int *status, int options, int si_code)
1247435da985SKonstantin Belousov {
1248435da985SKonstantin Belousov 	bool cont;
1249435da985SKonstantin Belousov 
1250435da985SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
1251435da985SKonstantin Belousov 	sx_assert(&proctree_lock, SA_XLOCKED);
1252435da985SKonstantin Belousov 	MPASS(si_code == CLD_TRAPPED || si_code == CLD_STOPPED ||
1253435da985SKonstantin Belousov 	    si_code == CLD_CONTINUED);
1254435da985SKonstantin Belousov 
1255435da985SKonstantin Belousov 	cont = si_code == CLD_CONTINUED;
1256435da985SKonstantin Belousov 	if ((options & WNOWAIT) == 0) {
1257435da985SKonstantin Belousov 		if (cont)
1258435da985SKonstantin Belousov 			p->p_flag &= ~P_CONTINUED;
1259435da985SKonstantin Belousov 		else
1260435da985SKonstantin Belousov 			p->p_flag |= P_WAITED;
1261a12e901aSKonstantin Belousov 		if (kern_wait_dequeue_sigchld &&
1262a12e901aSKonstantin Belousov 		    (td->td_proc->p_sysent->sv_flags & SV_SIG_WAITNDQ) == 0) {
1263435da985SKonstantin Belousov 			PROC_LOCK(td->td_proc);
1264435da985SKonstantin Belousov 			sigqueue_take(p->p_ksi);
1265435da985SKonstantin Belousov 			PROC_UNLOCK(td->td_proc);
1266435da985SKonstantin Belousov 		}
1267a12e901aSKonstantin Belousov 	}
1268435da985SKonstantin Belousov 	sx_xunlock(&proctree_lock);
1269435da985SKonstantin Belousov 	if (siginfo != NULL) {
1270435da985SKonstantin Belousov 		siginfo->si_code = si_code;
1271435da985SKonstantin Belousov 		siginfo->si_status = cont ? SIGCONT : p->p_xsig;
1272435da985SKonstantin Belousov 	}
1273435da985SKonstantin Belousov 	if (status != NULL)
1274435da985SKonstantin Belousov 		*status = cont ? SIGCONT : W_STOPCODE(p->p_xsig);
1275435da985SKonstantin Belousov 	td->td_retval[0] = p->p_pid;
127649afea10SMateusz Guzik 	PROC_UNLOCK(p);
1277435da985SKonstantin Belousov }
1278435da985SKonstantin Belousov 
1279f13b5a0fSKonstantin Belousov int
1280f13b5a0fSKonstantin Belousov kern_wait6(struct thread *td, idtype_t idtype, id_t id, int *status,
1281f13b5a0fSKonstantin Belousov     int options, struct __wrusage *wrusage, siginfo_t *siginfo)
1282f13b5a0fSKonstantin Belousov {
1283324fb6beSRobert Watson 	struct proc *p, *q;
12843a41ec6aSEd Schouten 	pid_t pid;
1285dcd43281SKonstantin Belousov 	int error, nfound, ret;
12867beb6082SMateusz Guzik 	bool report;
1287df8bae1dSRodney W. Grimes 
1288f13b5a0fSKonstantin Belousov 	AUDIT_ARG_VALUE((int)idtype);	/* XXX - This is likely wrong! */
1289f13b5a0fSKonstantin Belousov 	AUDIT_ARG_PID((pid_t)id);	/* XXX - This may be wrong! */
12902ef24ddeSRobert Watson 	AUDIT_ARG_VALUE(options);
1291de3007e8SWayne Salamon 
1292b40ce416SJulian Elischer 	q = td->td_proc;
1293f13b5a0fSKonstantin Belousov 
1294a2a85596SKonstantin Belousov 	if ((pid_t)id == WAIT_MYPGRP && (idtype == P_PID || idtype == P_PGID)) {
1295a545089eSKonstantin Belousov 		PROC_LOCK(q);
1296f13b5a0fSKonstantin Belousov 		id = (id_t)q->p_pgid;
1297a545089eSKonstantin Belousov 		PROC_UNLOCK(q);
1298f13b5a0fSKonstantin Belousov 		idtype = P_PGID;
1299f591779bSSeigo Tanimura 	}
1300f13b5a0fSKonstantin Belousov 
1301f528c3fdSDavid E. O'Brien 	/* If we don't know the option, just return. */
1302f13b5a0fSKonstantin Belousov 	if ((options & ~(WUNTRACED | WNOHANG | WCONTINUED | WNOWAIT |
1303f13b5a0fSKonstantin Belousov 	    WEXITED | WTRAPPED | WLINUXCLONE)) != 0)
13046dc958b9SJohn Baldwin 		return (EINVAL);
1305f13b5a0fSKonstantin Belousov 	if ((options & (WEXITED | WUNTRACED | WCONTINUED | WTRAPPED)) == 0) {
1306f13b5a0fSKonstantin Belousov 		/*
1307f13b5a0fSKonstantin Belousov 		 * We will be unable to find any matching processes,
1308f13b5a0fSKonstantin Belousov 		 * because there are no known events to look for.
1309f13b5a0fSKonstantin Belousov 		 * Prefer to return error instead of blocking
1310f13b5a0fSKonstantin Belousov 		 * indefinitely.
1311f13b5a0fSKonstantin Belousov 		 */
1312f13b5a0fSKonstantin Belousov 		return (EINVAL);
1313f13b5a0fSKonstantin Belousov 	}
1314f13b5a0fSKonstantin Belousov 
1315df8bae1dSRodney W. Grimes loop:
1316902c0d82SDavid Xu 	if (q->p_flag & P_STATCHILD) {
1317902c0d82SDavid Xu 		PROC_LOCK(q);
1318902c0d82SDavid Xu 		q->p_flag &= ~P_STATCHILD;
1319902c0d82SDavid Xu 		PROC_UNLOCK(q);
1320902c0d82SDavid Xu 	}
1321d7aadbf9SJohn Baldwin 	sx_xlock(&proctree_lock);
13226776bfebSMateusz Guzik loop_locked:
13236776bfebSMateusz Guzik 	nfound = 0;
13242e3c8fcbSPoul-Henning Kamp 	LIST_FOREACH(p, &q->p_children, p_sibling) {
13253a41ec6aSEd Schouten 		pid = p->p_pid;
1326f13b5a0fSKonstantin Belousov 		ret = proc_to_reap(td, p, idtype, id, status, options,
132757c74f5bSJohn Baldwin 		    wrusage, siginfo, 0);
1328dcd43281SKonstantin Belousov 		if (ret == 0)
1329df8bae1dSRodney W. Grimes 			continue;
13306776bfebSMateusz Guzik 		else if (ret != 1) {
13313a41ec6aSEd Schouten 			td->td_retval[0] = pid;
1332d7aadbf9SJohn Baldwin 			return (0);
13333a41ec6aSEd Schouten 		}
1334dcd43281SKonstantin Belousov 
13356776bfebSMateusz Guzik 		nfound++;
1336746b6e81SMateusz Guzik 		PROC_LOCK_ASSERT(p, MA_OWNED);
1337435da985SKonstantin Belousov 
1338f13b5a0fSKonstantin Belousov 		if ((options & WTRAPPED) != 0 &&
13397beb6082SMateusz Guzik 		    (p->p_flag & P_TRACED) != 0) {
13407beb6082SMateusz Guzik 			PROC_SLOCK(p);
13417beb6082SMateusz Guzik 			report =
13427beb6082SMateusz Guzik 			    ((p->p_flag & (P_STOPPED_TRACE | P_STOPPED_SIG)) &&
1343435da985SKonstantin Belousov 			    p->p_suspcount == p->p_numthreads &&
13447beb6082SMateusz Guzik 			    (p->p_flag & P_WAITED) == 0);
13458bf6ff22SMateusz Guzik 			PROC_SUNLOCK(p);
13467beb6082SMateusz Guzik 			if (report) {
1347515b7a0bSJohn Baldwin 			CTR4(KTR_PTRACE,
1348435da985SKonstantin Belousov 			    "wait: returning trapped pid %d status %#x "
1349435da985SKonstantin Belousov 			    "(xstat %d) xthread %d",
1350b4490c6eSKonstantin Belousov 			    p->p_pid, W_STOPCODE(p->p_xsig), p->p_xsig,
1351435da985SKonstantin Belousov 			    p->p_xthread != NULL ?
1352435da985SKonstantin Belousov 			    p->p_xthread->td_tid : -1);
13537beb6082SMateusz Guzik 				report_alive_proc(td, p, siginfo, status,
13547beb6082SMateusz Guzik 				    options, CLD_TRAPPED);
1355f13b5a0fSKonstantin Belousov 				return (0);
1356f13b5a0fSKonstantin Belousov 			}
13577beb6082SMateusz Guzik 		}
1358f13b5a0fSKonstantin Belousov 		if ((options & WUNTRACED) != 0 &&
13597beb6082SMateusz Guzik 		    (p->p_flag & P_STOPPED_SIG) != 0) {
13607beb6082SMateusz Guzik 			PROC_SLOCK(p);
13617beb6082SMateusz Guzik 			report = (p->p_suspcount == p->p_numthreads &&
13627beb6082SMateusz Guzik 			    ((p->p_flag & P_WAITED) == 0));
13638bf6ff22SMateusz Guzik 			PROC_SUNLOCK(p);
13647beb6082SMateusz Guzik 			if (report) {
13657beb6082SMateusz Guzik 				report_alive_proc(td, p, siginfo, status,
13667beb6082SMateusz Guzik 				    options, CLD_STOPPED);
1367b7e23e82SJohn Baldwin 				return (0);
1368df8bae1dSRodney W. Grimes 			}
13697beb6082SMateusz Guzik 		}
1370f13b5a0fSKonstantin Belousov 		if ((options & WCONTINUED) != 0 &&
1371f13b5a0fSKonstantin Belousov 		    (p->p_flag & P_CONTINUED) != 0) {
1372435da985SKonstantin Belousov 			report_alive_proc(td, p, siginfo, status, options,
1373435da985SKonstantin Belousov 			    CLD_CONTINUED);
1374dcdc6c36SKonstantin Belousov 			return (0);
13756ee093fbSMike Barcroft 		}
1376d7aadbf9SJohn Baldwin 		PROC_UNLOCK(p);
1377d7aadbf9SJohn Baldwin 	}
1378dcd43281SKonstantin Belousov 
1379dcd43281SKonstantin Belousov 	/*
1380dcd43281SKonstantin Belousov 	 * Look in the orphans list too, to allow the parent to
1381dcd43281SKonstantin Belousov 	 * collect it's child exit status even if child is being
1382dcd43281SKonstantin Belousov 	 * debugged.
1383dcd43281SKonstantin Belousov 	 *
1384dcd43281SKonstantin Belousov 	 * Debugger detaches from the parent upon successful
1385dcd43281SKonstantin Belousov 	 * switch-over from parent to child.  At this point due to
1386dcd43281SKonstantin Belousov 	 * re-parenting the parent loses the child to debugger and a
1387dcd43281SKonstantin Belousov 	 * wait4(2) call would report that it has no children to wait
1388dcd43281SKonstantin Belousov 	 * for.  By maintaining a list of orphans we allow the parent
1389dcd43281SKonstantin Belousov 	 * to successfully wait until the child becomes a zombie.
1390dcd43281SKonstantin Belousov 	 */
139157c74f5bSJohn Baldwin 	if (nfound == 0) {
1392dcd43281SKonstantin Belousov 		LIST_FOREACH(p, &q->p_orphans, p_orphan) {
139357c74f5bSJohn Baldwin 			ret = proc_to_reap(td, p, idtype, id, NULL, options,
139457c74f5bSJohn Baldwin 			    NULL, NULL, 1);
139557c74f5bSJohn Baldwin 			if (ret != 0) {
139657c74f5bSJohn Baldwin 				KASSERT(ret != -1, ("reaped an orphan (pid %d)",
139757c74f5bSJohn Baldwin 				    (int)td->td_retval[0]));
1398746b6e81SMateusz Guzik 				PROC_UNLOCK(p);
1399dcd43281SKonstantin Belousov 				nfound++;
140057c74f5bSJohn Baldwin 				break;
140157c74f5bSJohn Baldwin 			}
140257c74f5bSJohn Baldwin 		}
1403dcd43281SKonstantin Belousov 	}
1404d7aadbf9SJohn Baldwin 	if (nfound == 0) {
1405d7aadbf9SJohn Baldwin 		sx_xunlock(&proctree_lock);
1406d7aadbf9SJohn Baldwin 		return (ECHILD);
1407d7aadbf9SJohn Baldwin 	}
1408b7e23e82SJohn Baldwin 	if (options & WNOHANG) {
1409d7aadbf9SJohn Baldwin 		sx_xunlock(&proctree_lock);
1410d7aadbf9SJohn Baldwin 		td->td_retval[0] = 0;
1411d7aadbf9SJohn Baldwin 		return (0);
1412d7aadbf9SJohn Baldwin 	}
1413d7aadbf9SJohn Baldwin 	PROC_LOCK(q);
141495992d56SDavid Xu 	if (q->p_flag & P_STATCHILD) {
141595992d56SDavid Xu 		q->p_flag &= ~P_STATCHILD;
1416d7aadbf9SJohn Baldwin 		PROC_UNLOCK(q);
14176776bfebSMateusz Guzik 		goto loop_locked;
14186776bfebSMateusz Guzik 	}
14196776bfebSMateusz Guzik 	sx_xunlock(&proctree_lock);
14206776bfebSMateusz Guzik 	error = msleep(q, &q->p_mtx, PWAIT | PCATCH | PDROP, "wait", 0);
14214ae89b95SJohn Baldwin 	if (error)
1422d7aadbf9SJohn Baldwin 		return (error);
1423d7aadbf9SJohn Baldwin 	goto loop;
1424d7aadbf9SJohn Baldwin }
1425df8bae1dSRodney W. Grimes 
1426799d92abSMariusz Zaborski void
1427799d92abSMariusz Zaborski proc_add_orphan(struct proc *child, struct proc *parent)
1428799d92abSMariusz Zaborski {
1429799d92abSMariusz Zaborski 
1430799d92abSMariusz Zaborski 	sx_assert(&proctree_lock, SX_XLOCKED);
1431799d92abSMariusz Zaborski 	KASSERT((child->p_flag & P_TRACED) != 0,
1432799d92abSMariusz Zaborski 	    ("proc_add_orphan: not traced"));
1433799d92abSMariusz Zaborski 
1434799d92abSMariusz Zaborski 	if (LIST_EMPTY(&parent->p_orphans)) {
1435799d92abSMariusz Zaborski 		child->p_treeflag |= P_TREE_FIRST_ORPHAN;
1436799d92abSMariusz Zaborski 		LIST_INSERT_HEAD(&parent->p_orphans, child, p_orphan);
1437799d92abSMariusz Zaborski 	} else {
1438799d92abSMariusz Zaborski 		LIST_INSERT_AFTER(LIST_FIRST(&parent->p_orphans),
1439799d92abSMariusz Zaborski 		    child, p_orphan);
1440799d92abSMariusz Zaborski 	}
1441799d92abSMariusz Zaborski 	child->p_treeflag |= P_TREE_ORPHANED;
1442799d92abSMariusz Zaborski }
1443799d92abSMariusz Zaborski 
1444df8bae1dSRodney W. Grimes /*
144598f03f90SJake Burkholder  * Make process 'parent' the new parent of process 'child'.
144698f03f90SJake Burkholder  * Must be called with an exclusive hold of proctree lock.
1447df8bae1dSRodney W. Grimes  */
1448df8bae1dSRodney W. Grimes void
14492c054ce9SMateusz Guzik proc_reparent(struct proc *child, struct proc *parent, bool set_oppid)
1450df8bae1dSRodney W. Grimes {
1451df8bae1dSRodney W. Grimes 
14524e5e677bSJohn Baldwin 	sx_assert(&proctree_lock, SX_XLOCKED);
1453c65437a3SJohn Baldwin 	PROC_LOCK_ASSERT(child, MA_OWNED);
1454df8bae1dSRodney W. Grimes 	if (child->p_pptr == parent)
1455df8bae1dSRodney W. Grimes 		return;
1456df8bae1dSRodney W. Grimes 
1457ff766807SDavid Xu 	PROC_LOCK(child->p_pptr);
1458ff766807SDavid Xu 	sigqueue_take(child->p_ksi);
1459ff766807SDavid Xu 	PROC_UNLOCK(child->p_pptr);
1460b75356e1SJeffrey Hsu 	LIST_REMOVE(child, p_sibling);
1461b75356e1SJeffrey Hsu 	LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
1462dcd43281SKonstantin Belousov 
14639db97ca0SMariusz Zaborski 	proc_clear_orphan(child);
1464799d92abSMariusz Zaborski 	if ((child->p_flag & P_TRACED) != 0) {
1465799d92abSMariusz Zaborski 		proc_add_orphan(child, child->p_pptr);
1466dcd43281SKonstantin Belousov 	}
1467dcd43281SKonstantin Belousov 
1468df8bae1dSRodney W. Grimes 	child->p_pptr = parent;
14692c054ce9SMateusz Guzik 	if (set_oppid)
14702c054ce9SMateusz Guzik 		child->p_oppid = parent->p_pid;
1471df8bae1dSRodney W. Grimes }
1472