xref: /freebsd/sys/kern/kern_exit.c (revision 59838c1a1979924f3904536f2e6d5a771e185c14)
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>
40677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
41677b542eSDavid E. O'Brien 
42db6a20e2SGarrett Wollman #include "opt_ktrace.h"
43db6a20e2SGarrett Wollman 
44df8bae1dSRodney W. Grimes #include <sys/param.h>
45df8bae1dSRodney W. Grimes #include <sys/systm.h>
465fdb8324SBruce Evans #include <sys/sysproto.h>
474a144410SRobert Watson #include <sys/capsicum.h>
4875b8b3b2SJohn Baldwin #include <sys/eventhandler.h>
491c5bb3eaSPeter Wemm #include <sys/kernel.h>
50daec9284SConrad Meyer #include <sys/ktr.h>
51a1c995b6SPoul-Henning Kamp #include <sys/malloc.h>
52f34fa851SJohn Baldwin #include <sys/lock.h>
5335e0e5b3SJohn Baldwin #include <sys/mutex.h>
54df8bae1dSRodney W. Grimes #include <sys/proc.h>
55cfb5f768SJonathan Anderson #include <sys/procdesc.h>
56413628a7SBjoern A. Zeeb #include <sys/jail.h>
57df8bae1dSRodney W. Grimes #include <sys/tty.h>
58df8bae1dSRodney W. Grimes #include <sys/wait.h>
59eb30c1c0SPeter Wemm #include <sys/vmmeter.h>
607a6b989bSJohn Baldwin #include <sys/vnode.h>
61097055e2SEdward Tomasz Napierala #include <sys/racct.h>
62df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
6325f6e35aSPoul-Henning Kamp #include <sys/sbuf.h>
64797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
65b43179fbSJeff Roberson #include <sys/sched.h>
661005a129SJohn Baldwin #include <sys/sx.h>
67c8837938SJohn Baldwin #include <sys/syscallsubr.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>
7513dad108SKonstantin Belousov #include <sys/umtx.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 
99c0bc2867SGleb Smirnoff /* Hook for NFS teardown procedure. */
100c0bc2867SGleb Smirnoff void (*nlminfo_release_p)(struct proc *p);
101c0bc2867SGleb Smirnoff 
102d7359980SKonstantin Belousov struct proc *
103d7359980SKonstantin Belousov proc_realparent(struct proc *child)
104d7359980SKonstantin Belousov {
105d7359980SKonstantin Belousov 	struct proc *p, *parent;
106d7359980SKonstantin Belousov 
107d7359980SKonstantin Belousov 	sx_assert(&proctree_lock, SX_LOCKED);
1082c054ce9SMateusz Guzik 	if ((child->p_treeflag & P_TREE_ORPHANED) == 0)
1092c054ce9SMateusz Guzik 		return (child->p_pptr->p_pid == child->p_oppid ?
1102c054ce9SMateusz Guzik 			    child->p_pptr : initproc);
111d7359980SKonstantin Belousov 	for (p = child; (p->p_treeflag & P_TREE_FIRST_ORPHAN) == 0;) {
112d7359980SKonstantin Belousov 		/* Cannot use LIST_PREV(), since the list head is not known. */
113d7359980SKonstantin Belousov 		p = __containerof(p->p_orphan.le_prev, struct proc,
114d7359980SKonstantin Belousov 		    p_orphan.le_next);
115d7359980SKonstantin Belousov 		KASSERT((p->p_treeflag & P_TREE_ORPHANED) != 0,
116d7359980SKonstantin Belousov 		    ("missing P_ORPHAN %p", p));
117d7359980SKonstantin Belousov 	}
118d7359980SKonstantin Belousov 	parent = __containerof(p->p_orphan.le_prev, struct proc,
119d7359980SKonstantin Belousov 	    p_orphans.lh_first);
120d7359980SKonstantin Belousov 	return (parent);
121d7359980SKonstantin Belousov }
122d7359980SKonstantin Belousov 
123237623b0SKonstantin Belousov void
124237623b0SKonstantin Belousov reaper_abandon_children(struct proc *p, bool exiting)
125237623b0SKonstantin Belousov {
126237623b0SKonstantin Belousov 	struct proc *p1, *p2, *ptmp;
127237623b0SKonstantin Belousov 
128237623b0SKonstantin Belousov 	sx_assert(&proctree_lock, SX_LOCKED);
129237623b0SKonstantin Belousov 	KASSERT(p != initproc, ("reaper_abandon_children for initproc"));
130237623b0SKonstantin Belousov 	if ((p->p_treeflag & P_TREE_REAPER) == 0)
131237623b0SKonstantin Belousov 		return;
132237623b0SKonstantin Belousov 	p1 = p->p_reaper;
133237623b0SKonstantin Belousov 	LIST_FOREACH_SAFE(p2, &p->p_reaplist, p_reapsibling, ptmp) {
134237623b0SKonstantin Belousov 		LIST_REMOVE(p2, p_reapsibling);
135237623b0SKonstantin Belousov 		p2->p_reaper = p1;
136237623b0SKonstantin Belousov 		p2->p_reapsubtree = p->p_reapsubtree;
137237623b0SKonstantin Belousov 		LIST_INSERT_HEAD(&p1->p_reaplist, p2, p_reapsibling);
138237623b0SKonstantin Belousov 		if (exiting && p2->p_pptr == p) {
139237623b0SKonstantin Belousov 			PROC_LOCK(p2);
1402c054ce9SMateusz Guzik 			proc_reparent(p2, p1, true);
141237623b0SKonstantin Belousov 			PROC_UNLOCK(p2);
142237623b0SKonstantin Belousov 		}
143237623b0SKonstantin Belousov 	}
144237623b0SKonstantin Belousov 	KASSERT(LIST_EMPTY(&p->p_reaplist), ("p_reaplist not empty"));
145237623b0SKonstantin Belousov 	p->p_treeflag &= ~P_TREE_REAPER;
146237623b0SKonstantin Belousov }
147237623b0SKonstantin Belousov 
1482e39e24fSKonstantin Belousov static void
14934ebdceaSMateusz Guzik reaper_clear(struct proc *p)
15034ebdceaSMateusz Guzik {
15134ebdceaSMateusz Guzik 	struct proc *p1;
15234ebdceaSMateusz Guzik 	bool clear;
15334ebdceaSMateusz Guzik 
15434ebdceaSMateusz Guzik 	sx_assert(&proctree_lock, SX_LOCKED);
15534ebdceaSMateusz Guzik 	LIST_REMOVE(p, p_reapsibling);
15634ebdceaSMateusz Guzik 	if (p->p_reapsubtree == 1)
15734ebdceaSMateusz Guzik 		return;
15834ebdceaSMateusz Guzik 	clear = true;
15934ebdceaSMateusz Guzik 	LIST_FOREACH(p1, &p->p_reaper->p_reaplist, p_reapsibling) {
16034ebdceaSMateusz Guzik 		if (p1->p_reapsubtree == p->p_reapsubtree) {
16134ebdceaSMateusz Guzik 			clear = false;
16234ebdceaSMateusz Guzik 			break;
16334ebdceaSMateusz Guzik 		}
16434ebdceaSMateusz Guzik 	}
16534ebdceaSMateusz Guzik 	if (clear)
16634ebdceaSMateusz Guzik 		proc_id_clear(PROC_ID_REAP, p->p_reapsubtree);
16734ebdceaSMateusz Guzik }
16834ebdceaSMateusz Guzik 
1699db97ca0SMariusz Zaborski void
1709db97ca0SMariusz Zaborski proc_clear_orphan(struct proc *p)
1712e39e24fSKonstantin Belousov {
172d7359980SKonstantin Belousov 	struct proc *p1;
1732e39e24fSKonstantin Belousov 
174d7359980SKonstantin Belousov 	sx_assert(&proctree_lock, SA_XLOCKED);
175d7359980SKonstantin Belousov 	if ((p->p_treeflag & P_TREE_ORPHANED) == 0)
176d7359980SKonstantin Belousov 		return;
177d7359980SKonstantin Belousov 	if ((p->p_treeflag & P_TREE_FIRST_ORPHAN) != 0) {
178d7359980SKonstantin Belousov 		p1 = LIST_NEXT(p, p_orphan);
179d7359980SKonstantin Belousov 		if (p1 != NULL)
180d7359980SKonstantin Belousov 			p1->p_treeflag |= P_TREE_FIRST_ORPHAN;
181d7359980SKonstantin Belousov 		p->p_treeflag &= ~P_TREE_FIRST_ORPHAN;
1822e39e24fSKonstantin Belousov 	}
183d7359980SKonstantin Belousov 	LIST_REMOVE(p, p_orphan);
184d7359980SKonstantin Belousov 	p->p_treeflag &= ~P_TREE_ORPHANED;
1852e39e24fSKonstantin Belousov }
1862e39e24fSKonstantin Belousov 
187df8bae1dSRodney W. Grimes /*
188873fbcd7SRobert Watson  * exit -- death of process.
189df8bae1dSRodney W. Grimes  */
190fc0b1dbfSBruce Evans void
1918451d0ddSKip Macy sys_sys_exit(struct thread *td, struct sys_exit_args *uap)
192df8bae1dSRodney W. Grimes {
193b40ce416SJulian Elischer 
194b4490c6eSKonstantin Belousov 	exit1(td, uap->rval, 0);
195df8bae1dSRodney W. Grimes 	/* NOTREACHED */
196df8bae1dSRodney W. Grimes }
197df8bae1dSRodney W. Grimes 
198df8bae1dSRodney W. Grimes /*
199873fbcd7SRobert Watson  * Exit: deallocate address space and other resources, change proc state to
200873fbcd7SRobert Watson  * zombie, and unlink proc from allproc and parent's lists.  Save exit status
201873fbcd7SRobert Watson  * and rusage for wait().  Check for child processes and orphan them.
202df8bae1dSRodney W. Grimes  */
203fc0b1dbfSBruce Evans void
204b4490c6eSKonstantin Belousov exit1(struct thread *td, int rval, int signo)
205df8bae1dSRodney W. Grimes {
206a661bebeSMateusz Guzik 	struct proc *p, *nq, *q, *t;
207a661bebeSMateusz Guzik 	struct thread *tdt;
2089a2dde80SKonstantin Belousov 	ksiginfo_t *ksi, *ksi1;
209015cd8dcSMateusz Guzik 	int signal_parent;
210df8bae1dSRodney W. Grimes 
211e17660e7SKris Kennaway 	mtx_assert(&Giant, MA_NOTOWNED);
212b4490c6eSKonstantin Belousov 	KASSERT(rval == 0 || signo == 0, ("exit1 rv %d sig %d", rval, signo));
2130cddd8f0SMatthew Dillon 
214276c5169SJohn Baldwin 	p = td->td_proc;
2155486ffc8SMarius Strobl 	/*
2165486ffc8SMarius Strobl 	 * XXX in case we're rebooting we just let init die in order to
2175486ffc8SMarius Strobl 	 * work around an unsolved stack overflow seen very late during
2185486ffc8SMarius Strobl 	 * shutdown on sparc64 when the gmirror worker process exists.
21958aa35d4SWarner Losh 	 * XXX what to do now that sparc64 is gone... remove if?
2205486ffc8SMarius Strobl 	 */
2215486ffc8SMarius Strobl 	if (p == initproc && rebooting == 0) {
222b4490c6eSKonstantin Belousov 		printf("init died (signal %d, exit %d)\n", signo, rval);
2235f7bd355SPoul-Henning Kamp 		panic("Going nowhere without my init!");
2245f7bd355SPoul-Henning Kamp 	}
2252c1011f7SJohn Dyson 
2267a6b989bSJohn Baldwin 	/*
2271bc93bb7SKonstantin Belousov 	 * Deref SU mp, since the thread does not return to userspace.
2281bc93bb7SKonstantin Belousov 	 */
229aca4bb91SKonstantin Belousov 	td_softdep_cleanup(td);
2301bc93bb7SKonstantin Belousov 
2311bc93bb7SKonstantin Belousov 	/*
2322c10d16aSJeff Roberson 	 * MUST abort all other threads before proceeding past here.
2337a6b989bSJohn Baldwin 	 */
234e602ba25SJulian Elischer 	PROC_LOCK(p);
235e602ba25SJulian Elischer 	/*
236aba1ca52SKonstantin Belousov 	 * First check if some other thread or external request got
237aba1ca52SKonstantin Belousov 	 * here before us.  If so, act appropriately: exit or suspend.
238aba1ca52SKonstantin Belousov 	 * We must ensure that stop requests are handled before we set
239aba1ca52SKonstantin Belousov 	 * P_WEXIT.
240e602ba25SJulian Elischer 	 */
241e602ba25SJulian Elischer 	thread_suspend_check(0);
242aba1ca52SKonstantin Belousov 	while (p->p_flag & P_HADTHREADS) {
243e602ba25SJulian Elischer 		/*
244e602ba25SJulian Elischer 		 * Kill off the other threads. This requires
2456111dcd2SJohn Baldwin 		 * some co-operation from other parts of the kernel
2466111dcd2SJohn Baldwin 		 * so it may not be instantaneous.  With this state set
2476111dcd2SJohn Baldwin 		 * any thread entering the kernel from userspace will
248e602ba25SJulian Elischer 		 * thread_exit() in trap().  Any thread attempting to
2496111dcd2SJohn Baldwin 		 * sleep will return immediately with EINTR or EWOULDBLOCK
2506111dcd2SJohn Baldwin 		 * which will hopefully force them to back out to userland
2516111dcd2SJohn Baldwin 		 * freeing resources as they go.  Any thread attempting
252b3248998SJulian Elischer 		 * to return to userland will thread_exit() from userret().
2536111dcd2SJohn Baldwin 		 * thread_exit() will unsuspend us when the last of the
2546111dcd2SJohn Baldwin 		 * other threads exits.
255b370279eSDavid Xu 		 * If there is already a thread singler after resumption,
2566111dcd2SJohn Baldwin 		 * calling thread_single will fail; in that case, we just
257b370279eSDavid Xu 		 * re-check all suspension request, the thread should
258b370279eSDavid Xu 		 * either be suspended there or exit.
259e602ba25SJulian Elischer 		 */
2606ddcc233SKonstantin Belousov 		if (!thread_single(p, SINGLE_EXIT))
261e602ba25SJulian Elischer 			/*
262aba1ca52SKonstantin Belousov 			 * All other activity in this process is now
263aba1ca52SKonstantin Belousov 			 * stopped.  Threading support has been turned
264aba1ca52SKonstantin Belousov 			 * off.
265e602ba25SJulian Elischer 			 */
266aba1ca52SKonstantin Belousov 			break;
267aba1ca52SKonstantin Belousov 		/*
268aba1ca52SKonstantin Belousov 		 * Recheck for new stop or suspend requests which
269aba1ca52SKonstantin Belousov 		 * might appear while process lock was dropped in
270aba1ca52SKonstantin Belousov 		 * thread_single().
271aba1ca52SKonstantin Belousov 		 */
272aba1ca52SKonstantin Belousov 		thread_suspend_check(0);
273e602ba25SJulian Elischer 	}
2741c4bcd05SJeff Roberson 	KASSERT(p->p_numthreads == 1,
2751c4bcd05SJeff Roberson 	    ("exit1: proc %p exiting with %d threads", p, p->p_numthreads));
27658c77a9dSEdward Tomasz Napierala 	racct_sub(p, RACCT_NTHR, 1);
277b4490c6eSKonstantin Belousov 
278b4490c6eSKonstantin Belousov 	/* Let event handler change exit status */
279b4490c6eSKonstantin Belousov 	p->p_xexit = rval;
280b4490c6eSKonstantin Belousov 	p->p_xsig = signo;
281b4490c6eSKonstantin Belousov 
28206ad42b2SJohn Baldwin 	/*
2830f14f15bSJohn Baldwin 	 * Ignore any pending request to stop due to a stop signal.
2840f14f15bSJohn Baldwin 	 * Once P_WEXIT is set, future requests will be ignored as
2850f14f15bSJohn Baldwin 	 * well.
2860f14f15bSJohn Baldwin 	 */
2870f14f15bSJohn Baldwin 	p->p_flag &= ~P_STOPPED_SIG;
2880f14f15bSJohn Baldwin 	KASSERT(!P_SHOULDSTOP(p), ("exiting process is stopped"));
2890f14f15bSJohn Baldwin 
290*59838c1aSJohn Baldwin 	/* Note that we are exiting. */
291e602ba25SJulian Elischer 	p->p_flag |= P_WEXIT;
29206ad42b2SJohn Baldwin 
29306ad42b2SJohn Baldwin 	/*
29406ad42b2SJohn Baldwin 	 * Wait for any processes that have a hold on our vmspace to
29506ad42b2SJohn Baldwin 	 * release their reference.
29606ad42b2SJohn Baldwin 	 */
29706ad42b2SJohn Baldwin 	while (p->p_lock > 0)
29806ad42b2SJohn Baldwin 		msleep(&p->p_lock, &p->p_mtx, PWAIT, "exithold", 0);
299ebceaf6dSDavid Xu 
300e602ba25SJulian Elischer 	PROC_UNLOCK(p);
3011c4bcd05SJeff Roberson 	/* Drain the limit callout while we don't have the proc locked */
3021c4bcd05SJeff Roberson 	callout_drain(&p->p_limco);
303b40ce416SJulian Elischer 
30400c28d96SRobert Watson #ifdef AUDIT
30500c28d96SRobert Watson 	/*
30600c28d96SRobert Watson 	 * The Sun BSM exit token contains two components: an exit status as
30700c28d96SRobert Watson 	 * passed to exit(), and a return value to indicate what sort of exit
30800c28d96SRobert Watson 	 * it was.  The exit status is WEXITSTATUS(rv), but it's not clear
30900c28d96SRobert Watson 	 * what the return value is.
31000c28d96SRobert Watson 	 */
311b4490c6eSKonstantin Belousov 	AUDIT_ARG_EXIT(rval, 0);
31200c28d96SRobert Watson 	AUDIT_SYSCALL_EXIT(0, td);
31300c28d96SRobert Watson #endif
31400c28d96SRobert Watson 
315bad2520aSMateusz Guzik 	/* Are we a task leader with peers? */
316bad2520aSMateusz Guzik 	if (p->p_peers != NULL && p == p->p_leader) {
317c6544064SJohn Baldwin 		mtx_lock(&ppeers_lock);
3182c1011f7SJohn Dyson 		q = p->p_peers;
319776e0b36SJohn Baldwin 		while (q != NULL) {
320776e0b36SJohn Baldwin 			PROC_LOCK(q);
3218451d0ddSKip Macy 			kern_psignal(q, SIGKILL);
322776e0b36SJohn Baldwin 			PROC_UNLOCK(q);
3232c1011f7SJohn Dyson 			q = q->p_peers;
3242c1011f7SJohn Dyson 		}
325c6544064SJohn Baldwin 		while (p->p_peers != NULL)
326c6544064SJohn Baldwin 			msleep(p, &ppeers_lock, PWAIT, "exit1", 0);
327c6544064SJohn Baldwin 		mtx_unlock(&ppeers_lock);
3282c1011f7SJohn Dyson 	}
3292c1011f7SJohn Dyson 
330fed06968SJulian Elischer 	/*
331e9189611SPeter Wemm 	 * Check if any loadable modules need anything done at process exit.
332b4490c6eSKonstantin Belousov 	 * E.g. SYSV IPC stuff.
333b4490c6eSKonstantin Belousov 	 * Event handler could change exit status.
334fed06968SJulian Elischer 	 * XXX what if one of these generates an error?
335fed06968SJulian Elischer 	 */
3362ca45184SMatt Joras 	EVENTHANDLER_DIRECT_INVOKE(process_exit, p);
337a914fb6bSJohn Baldwin 
338e3d3e828SMateusz Guzik 	/*
339e3d3e828SMateusz Guzik 	 * If parent is waiting for us to exit or exec,
340e3d3e828SMateusz Guzik 	 * P_PPWAIT is set; we will wakeup the parent below.
341e3d3e828SMateusz Guzik 	 */
342a914fb6bSJohn Baldwin 	PROC_LOCK(p);
343a282253aSJulian Elischer 	stopprofclock(p);
3448d570f64SJohn Baldwin 	p->p_ptevents = 0;
3455499ea01SJohn Baldwin 
3465499ea01SJohn Baldwin 	/*
3475499ea01SJohn Baldwin 	 * Stop the real interval timer.  If the handler is currently
3485499ea01SJohn Baldwin 	 * executing, prevent it from rearming itself and let it finish.
3495499ea01SJohn Baldwin 	 */
3505499ea01SJohn Baldwin 	if (timevalisset(&p->p_realtimer.it_value) &&
35156d48d6dSKonstantin Belousov 	    _callout_stop_safe(&p->p_itcallout, CS_EXECUTING, NULL) == 0) {
3525499ea01SJohn Baldwin 		timevalclear(&p->p_realtimer.it_interval);
3535499ea01SJohn Baldwin 		msleep(&p->p_itcallout, &p->p_mtx, PWAIT, "ritwait", 0);
3545499ea01SJohn Baldwin 		KASSERT(!timevalisset(&p->p_realtimer.it_value),
3555499ea01SJohn Baldwin 		    ("realtime timer is still armed"));
3565499ea01SJohn Baldwin 	}
3572a339d9eSKonstantin Belousov 
35896d7f8efSTim J. Robbins 	PROC_UNLOCK(p);
359df8bae1dSRodney W. Grimes 
3602a339d9eSKonstantin Belousov 	umtx_thread_exit(td);
3612a339d9eSKonstantin Belousov 
362df8bae1dSRodney W. Grimes 	/*
363831d27a9SDon Lewis 	 * Reset any sigio structures pointing to us as a result of
364831d27a9SDon Lewis 	 * F_SETOWN with our pid.
365831d27a9SDon Lewis 	 */
366831d27a9SDon Lewis 	funsetownlst(&p->p_sigiolst);
367831d27a9SDon Lewis 
368831d27a9SDon Lewis 	/*
369c0bc2867SGleb Smirnoff 	 * If this process has an nlminfo data area (for lockd), release it
370c0bc2867SGleb Smirnoff 	 */
371c0bc2867SGleb Smirnoff 	if (nlminfo_release_p != NULL && p->p_nlminfo != NULL)
372c0bc2867SGleb Smirnoff 		(*nlminfo_release_p)(p);
373c0bc2867SGleb Smirnoff 
374c0bc2867SGleb Smirnoff 	/*
375df8bae1dSRodney W. Grimes 	 * Close open files and release open-file table.
376df8bae1dSRodney W. Grimes 	 * This may block!
377df8bae1dSRodney W. Grimes 	 */
3782609222aSPawel Jakub Dawidek 	fdescfree(td);
379df8bae1dSRodney W. Grimes 
380a914fb6bSJohn Baldwin 	/*
381a2587073SPoul-Henning Kamp 	 * If this thread tickled GEOM, we need to wait for the giggling to
382a2587073SPoul-Henning Kamp 	 * stop before we return to userland
383a2587073SPoul-Henning Kamp 	 */
384a2587073SPoul-Henning Kamp 	if (td->td_pflags & TDP_GEOM)
385a2587073SPoul-Henning Kamp 		g_waitidle();
386a2587073SPoul-Henning Kamp 
387a2587073SPoul-Henning Kamp 	/*
388a914fb6bSJohn Baldwin 	 * Remove ourself from our leader's peer list and wake our leader.
389a914fb6bSJohn Baldwin 	 */
390bad2520aSMateusz Guzik 	if (p->p_leader->p_peers != NULL) {
391c6544064SJohn Baldwin 		mtx_lock(&ppeers_lock);
392bad2520aSMateusz Guzik 		if (p->p_leader->p_peers != NULL) {
39379fc0bf4SMike Smith 			q = p->p_leader;
39479fc0bf4SMike Smith 			while (q->p_peers != p)
39579fc0bf4SMike Smith 				q = q->p_peers;
39679fc0bf4SMike Smith 			q->p_peers = p->p_peers;
3977f05b035SAlfred Perlstein 			wakeup(p->p_leader);
39879fc0bf4SMike Smith 		}
399c6544064SJohn Baldwin 		mtx_unlock(&ppeers_lock);
400bad2520aSMateusz Guzik 	}
40179fc0bf4SMike Smith 
40257051fdcSTor Egge 	vmspace_exit(td);
40317182b13SMateusz Guzik 	killjobc();
404b40ce416SJulian Elischer 	(void)acct_process(td);
405bc093719SEd Schouten 
406df8bae1dSRodney W. Grimes #ifdef KTRACE
4072c255e9dSRobert Watson 	ktrprocexit(td);
408df8bae1dSRodney W. Grimes #endif
409df8bae1dSRodney W. Grimes 	/*
410ee42d0a9SDavid Malone 	 * Release reference to text vnode
411ee42d0a9SDavid Malone 	 */
41288f98985SMateusz Guzik 	if (p->p_textvp != NULL) {
41388f98985SMateusz Guzik 		vrele(p->p_textvp);
414ee42d0a9SDavid Malone 		p->p_textvp = NULL;
415ee42d0a9SDavid Malone 	}
416ee42d0a9SDavid Malone 
417ee42d0a9SDavid Malone 	/*
418d7aadbf9SJohn Baldwin 	 * Release our limits structure.
419d7aadbf9SJohn Baldwin 	 */
42088f98985SMateusz Guzik 	lim_free(p->p_limit);
421d7aadbf9SJohn Baldwin 	p->p_limit = NULL;
422d7aadbf9SJohn Baldwin 
423cf7d9a8cSDavid Xu 	tidhash_remove(td);
424cf7d9a8cSDavid Xu 
425d7aadbf9SJohn Baldwin 	/*
42681d68271SMateusz Guzik 	 * Call machine-dependent code to release any
42781d68271SMateusz Guzik 	 * machine-dependent resources other than the address space.
42881d68271SMateusz Guzik 	 * The address space is released by "vmspace_exitfree(p)" in
42981d68271SMateusz Guzik 	 * vm_waitproc().
43081d68271SMateusz Guzik 	 */
43181d68271SMateusz Guzik 	cpu_exit(td);
43281d68271SMateusz Guzik 
43381d68271SMateusz Guzik 	WITNESS_WARN(WARN_PANIC, NULL, "process (pid %d) exiting", p->p_pid);
43481d68271SMateusz Guzik 
43581d68271SMateusz Guzik 	/*
43688cc62e5SMateusz Guzik 	 * Remove from allproc. It still sits in the hash.
437df8bae1dSRodney W. Grimes 	 */
4381005a129SJohn Baldwin 	sx_xlock(&allproc_lock);
439b75356e1SJeffrey Hsu 	LIST_REMOVE(p, p_list);
4401005a129SJohn Baldwin 	sx_xunlock(&allproc_lock);
441df8bae1dSRodney W. Grimes 
442b1fbffe7SMateusz Guzik 	sx_xlock(&proctree_lock);
44341fadb3fSMariusz Zaborski 	PROC_LOCK(p);
44441fadb3fSMariusz Zaborski 	p->p_flag &= ~(P_TRACED | P_PPWAIT | P_PPTRACE);
44541fadb3fSMariusz Zaborski 	PROC_UNLOCK(p);
446b1fbffe7SMateusz Guzik 
44737f84a60SJohn Baldwin 	/*
448a661bebeSMateusz Guzik 	 * Reparent all children processes:
449a661bebeSMateusz Guzik 	 * - traced ones to the original parent (or init if we are that parent)
450a661bebeSMateusz Guzik 	 * - the rest to init
45137f84a60SJohn Baldwin 	 */
4522e3c8fcbSPoul-Henning Kamp 	q = LIST_FIRST(&p->p_children);
453a914fb6bSJohn Baldwin 	if (q != NULL)		/* only need this if any child is S_ZOMB */
454237623b0SKonstantin Belousov 		wakeup(q->p_reaper);
455a914fb6bSJohn Baldwin 	for (; q != NULL; q = nq) {
4562e3c8fcbSPoul-Henning Kamp 		nq = LIST_NEXT(q, p_sibling);
4579a2dde80SKonstantin Belousov 		ksi = ksiginfo_alloc(TRUE);
458a914fb6bSJohn Baldwin 		PROC_LOCK(q);
4594ac9ae70SJulian Elischer 		q->p_sigparent = SIGCHLD;
460904c5ec4SDavid Xu 
461a05cfdf4SMariusz Zaborski 		if ((q->p_flag & P_TRACED) == 0) {
4622c054ce9SMateusz Guzik 			proc_reparent(q, q->p_reaper, true);
463545d3122SKonstantin Belousov 			if (q->p_state == PRS_ZOMBIE) {
4649a2dde80SKonstantin Belousov 				/*
4659a2dde80SKonstantin Belousov 				 * Inform reaper about the reparented
4669a2dde80SKonstantin Belousov 				 * zombie, since wait(2) has something
4679a2dde80SKonstantin Belousov 				 * new to report.  Guarantee queueing
4689a2dde80SKonstantin Belousov 				 * of the SIGCHLD signal, similar to
4699a2dde80SKonstantin Belousov 				 * the _exit() behaviour, by providing
4709a2dde80SKonstantin Belousov 				 * our ksiginfo.  Ksi is freed by the
4719a2dde80SKonstantin Belousov 				 * signal delivery.
4729a2dde80SKonstantin Belousov 				 */
4739a2dde80SKonstantin Belousov 				if (q->p_ksi == NULL) {
4749a2dde80SKonstantin Belousov 					ksi1 = NULL;
4759a2dde80SKonstantin Belousov 				} else {
4769a2dde80SKonstantin Belousov 					ksiginfo_copy(q->p_ksi, ksi);
4779a2dde80SKonstantin Belousov 					ksi->ksi_flags |= KSI_INS;
4789a2dde80SKonstantin Belousov 					ksi1 = ksi;
4799a2dde80SKonstantin Belousov 					ksi = NULL;
4809a2dde80SKonstantin Belousov 				}
481545d3122SKonstantin Belousov 				PROC_LOCK(q->p_reaper);
4829a2dde80SKonstantin Belousov 				pksignal(q->p_reaper, SIGCHLD, ksi1);
483545d3122SKonstantin Belousov 				PROC_UNLOCK(q->p_reaper);
484b9408863SKonstantin Belousov 			} else if (q->p_pdeathsig > 0) {
485b9408863SKonstantin Belousov 				/*
486b9408863SKonstantin Belousov 				 * The child asked to received a signal
487b9408863SKonstantin Belousov 				 * when we exit.
488b9408863SKonstantin Belousov 				 */
489b9408863SKonstantin Belousov 				kern_psignal(q, q->p_pdeathsig);
490545d3122SKonstantin Belousov 			}
491a661bebeSMateusz Guzik 		} else {
492a661bebeSMateusz Guzik 			/*
493a661bebeSMateusz Guzik 			 * Traced processes are killed since their existence
494a661bebeSMateusz Guzik 			 * means someone is screwing up.
495a661bebeSMateusz Guzik 			 */
496a661bebeSMateusz Guzik 			t = proc_realparent(q);
497a661bebeSMateusz Guzik 			if (t == p) {
4982c054ce9SMateusz Guzik 				proc_reparent(q, q->p_reaper, true);
499a661bebeSMateusz Guzik 			} else {
500a661bebeSMateusz Guzik 				PROC_LOCK(t);
5012c054ce9SMateusz Guzik 				proc_reparent(q, t, true);
502a661bebeSMateusz Guzik 				PROC_UNLOCK(t);
503a661bebeSMateusz Guzik 			}
5045085ecb7SKonstantin Belousov 			/*
5055085ecb7SKonstantin Belousov 			 * Since q was found on our children list, the
5065085ecb7SKonstantin Belousov 			 * proc_reparent() call moved q to the orphan
5075085ecb7SKonstantin Belousov 			 * list due to present P_TRACED flag. Clear
5085085ecb7SKonstantin Belousov 			 * orphan link for q now while q is locked.
5095085ecb7SKonstantin Belousov 			 */
5109db97ca0SMariusz Zaborski 			proc_clear_orphan(q);
511c2836532SDavid Xu 			q->p_flag &= ~(P_TRACED | P_STOPPED_TRACE);
512b7a25e63SKonstantin Belousov 			q->p_flag2 &= ~P2_PTRACE_FSTP;
5138d570f64SJohn Baldwin 			q->p_ptevents = 0;
514b7a25e63SKonstantin Belousov 			FOREACH_THREAD_IN_PROC(q, tdt) {
515b7a25e63SKonstantin Belousov 				tdt->td_dbgflags &= ~(TDB_SUSPEND | TDB_XSIG |
516b7a25e63SKonstantin Belousov 				    TDB_FSTP);
517b7a25e63SKonstantin Belousov 			}
5188451d0ddSKip Macy 			kern_psignal(q, SIGKILL);
519c65437a3SJohn Baldwin 		}
520a914fb6bSJohn Baldwin 		PROC_UNLOCK(q);
5219a2dde80SKonstantin Belousov 		if (ksi != NULL)
5229a2dde80SKonstantin Belousov 			ksiginfo_free(ksi);
523df8bae1dSRodney W. Grimes 	}
524df8bae1dSRodney W. Grimes 
5255085ecb7SKonstantin Belousov 	/*
5265085ecb7SKonstantin Belousov 	 * Also get rid of our orphans.
5275085ecb7SKonstantin Belousov 	 */
5285085ecb7SKonstantin Belousov 	while ((q = LIST_FIRST(&p->p_orphans)) != NULL) {
5295085ecb7SKonstantin Belousov 		PROC_LOCK(q);
530128c9bc0SMark Johnston 		KASSERT(q->p_oppid == p->p_pid,
531128c9bc0SMark Johnston 		    ("orphan %p of %p has unexpected oppid %d", q, p,
532128c9bc0SMark Johnston 		    q->p_oppid));
533128c9bc0SMark Johnston 		q->p_oppid = q->p_reaper->p_pid;
534128c9bc0SMark Johnston 
535b9408863SKonstantin Belousov 		/*
536b9408863SKonstantin Belousov 		 * If we are the real parent of this process
537b9408863SKonstantin Belousov 		 * but it has been reparented to a debugger, then
538b9408863SKonstantin Belousov 		 * check if it asked for a signal when we exit.
539b9408863SKonstantin Belousov 		 */
540b9408863SKonstantin Belousov 		if (q->p_pdeathsig > 0)
541b9408863SKonstantin Belousov 			kern_psignal(q, q->p_pdeathsig);
542515b7a0bSJohn Baldwin 		CTR2(KTR_PTRACE, "exit: pid %d, clearing orphan %d", p->p_pid,
543515b7a0bSJohn Baldwin 		    q->p_pid);
5449db97ca0SMariusz Zaborski 		proc_clear_orphan(q);
5455085ecb7SKonstantin Belousov 		PROC_UNLOCK(q);
5465085ecb7SKonstantin Belousov 	}
5475085ecb7SKonstantin Belousov 
548eadb1dcbSMateusz Guzik #ifdef KDTRACE_HOOKS
549eadb1dcbSMateusz Guzik 	if (SDT_PROBES_ENABLED()) {
550eadb1dcbSMateusz Guzik 		int reason = CLD_EXITED;
551eadb1dcbSMateusz Guzik 		if (WCOREDUMP(signo))
552eadb1dcbSMateusz Guzik 			reason = CLD_DUMPED;
553eadb1dcbSMateusz Guzik 		else if (WIFSIGNALED(signo))
554eadb1dcbSMateusz Guzik 			reason = CLD_KILLED;
555eadb1dcbSMateusz Guzik 		SDT_PROBE1(proc, , , exit, reason);
556eadb1dcbSMateusz Guzik 	}
557eadb1dcbSMateusz Guzik #endif
558eadb1dcbSMateusz Guzik 
5591c4bcd05SJeff Roberson 	/* Save exit status. */
560d7aadbf9SJohn Baldwin 	PROC_LOCK(p);
561cbf4e354SDavid Xu 	p->p_xthread = td;
5625d217f17SJohn Birrell 
5635d217f17SJohn Birrell #ifdef KDTRACE_HOOKS
5645d217f17SJohn Birrell 	/*
5655d217f17SJohn Birrell 	 * Tell the DTrace fasttrap provider about the exit if it
5665d217f17SJohn Birrell 	 * has declared an interest.
5675d217f17SJohn Birrell 	 */
5685d217f17SJohn Birrell 	if (dtrace_fasttrap_exit)
5695d217f17SJohn Birrell 		dtrace_fasttrap_exit(p);
5705d217f17SJohn Birrell #endif
5715d217f17SJohn Birrell 
5721c4bcd05SJeff Roberson 	/*
5737a6b989bSJohn Baldwin 	 * Notify interested parties of our demise.
574cb679c38SJonathan Lemon 	 */
5759e590ff0SKonstantin Belousov 	KNOTE_LOCKED(p->p_klist, NOTE_EXIT);
5767eaec467SJohn Baldwin 
5771a29c806SOlivier Houchard 	/*
578cfb5f768SJonathan Anderson 	 * If this is a process with a descriptor, we may not need to deliver
579cfb5f768SJonathan Anderson 	 * a signal to the parent.  proctree_lock is held over
580cfb5f768SJonathan Anderson 	 * procdesc_exit() to serialize concurrent calls to close() and
581cfb5f768SJonathan Anderson 	 * exit().
582cfb5f768SJonathan Anderson 	 */
583015cd8dcSMateusz Guzik 	signal_parent = 0;
584cfb5f768SJonathan Anderson 	if (p->p_procdesc == NULL || procdesc_exit(p)) {
585cfb5f768SJonathan Anderson 		/*
586cfb5f768SJonathan Anderson 		 * Notify parent that we're gone.  If parent has the
587cfb5f768SJonathan Anderson 		 * PS_NOCLDWAIT flag set, or if the handler is set to SIG_IGN,
588cfb5f768SJonathan Anderson 		 * notify process 1 instead (and hope it will handle this
589cfb5f768SJonathan Anderson 		 * situation).
590df8bae1dSRodney W. Grimes 		 */
591d7aadbf9SJohn Baldwin 		PROC_LOCK(p->p_pptr);
59290af4afaSJohn Baldwin 		mtx_lock(&p->p_pptr->p_sigacts->ps_mtx);
593cfb5f768SJonathan Anderson 		if (p->p_pptr->p_sigacts->ps_flag &
594cfb5f768SJonathan Anderson 		    (PS_NOCLDWAIT | PS_CLDSIGIGN)) {
595276c5169SJohn Baldwin 			struct proc *pp;
596276c5169SJohn Baldwin 
59790af4afaSJohn Baldwin 			mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx);
598276c5169SJohn Baldwin 			pp = p->p_pptr;
599f591779bSSeigo Tanimura 			PROC_UNLOCK(pp);
6002c054ce9SMateusz Guzik 			proc_reparent(p, p->p_reaper, true);
60155b5f2a2SDon Lewis 			p->p_sigparent = SIGCHLD;
602f591779bSSeigo Tanimura 			PROC_LOCK(p->p_pptr);
603007abb3dSKonstantin Belousov 
604245f17d4SJoerg Wunsch 			/*
605007abb3dSKonstantin Belousov 			 * Notify parent, so in case he was wait(2)ing or
6066fae832aSKonstantin Belousov 			 * executing waitpid(2) with our pid, he will
607245f17d4SJoerg Wunsch 			 * continue.
608245f17d4SJoerg Wunsch 			 */
6097f05b035SAlfred Perlstein 			wakeup(pp);
61090af4afaSJohn Baldwin 		} else
61190af4afaSJohn Baldwin 			mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx);
612245f17d4SJoerg Wunsch 
613015cd8dcSMateusz Guzik 		if (p->p_pptr == p->p_reaper || p->p_pptr == initproc) {
614015cd8dcSMateusz Guzik 			signal_parent = 1;
615015cd8dcSMateusz Guzik 		} else if (p->p_sigparent != 0) {
616015cd8dcSMateusz Guzik 			if (p->p_sigparent == SIGCHLD) {
617015cd8dcSMateusz Guzik 				signal_parent = 1;
618015cd8dcSMateusz Guzik 			} else { /* LINUX thread */
619015cd8dcSMateusz Guzik 				signal_parent = 2;
620015cd8dcSMateusz Guzik 			}
621ebceaf6dSDavid Xu 		}
622cfb5f768SJonathan Anderson 	} else
623cfb5f768SJonathan Anderson 		PROC_LOCK(p->p_pptr);
624d7aadbf9SJohn Baldwin 	sx_xunlock(&proctree_lock);
625696058c3SJulian Elischer 
626015cd8dcSMateusz Guzik 	if (signal_parent == 1) {
627015cd8dcSMateusz Guzik 		childproc_exited(p);
628015cd8dcSMateusz Guzik 	} else if (signal_parent == 2) {
629015cd8dcSMateusz Guzik 		kern_psignal(p->p_pptr, p->p_sigparent);
630015cd8dcSMateusz Guzik 	}
631015cd8dcSMateusz Guzik 
6327e588b92SMateusz Guzik 	/* Tell the prison that we are gone. */
6337e588b92SMateusz Guzik 	prison_proc_free(p->p_ucred->cr_prison);
6347e588b92SMateusz Guzik 
635eb30c1c0SPeter Wemm 	/*
636f71e748dSDavid Xu 	 * The state PRS_ZOMBIE prevents other proesses from sending
637f71e748dSDavid Xu 	 * signal to the process, to avoid memory leak, we free memory
638f71e748dSDavid Xu 	 * for signal queue at the time when the state is set.
639f71e748dSDavid Xu 	 */
640f71e748dSDavid Xu 	sigqueue_flush(&p->p_sigqueue);
641f71e748dSDavid Xu 	sigqueue_flush(&td->td_sigqueue);
642f71e748dSDavid Xu 
643f71e748dSDavid Xu 	/*
644462f31bfSJohn Baldwin 	 * We have to wait until after acquiring all locks before
645a9a64385SJohn Baldwin 	 * changing p_state.  We need to avoid all possible context
646a9a64385SJohn Baldwin 	 * switches (including ones from blocking on a mutex) while
647ddf9c4f7SJohn Baldwin 	 * marked as a zombie.  We also have to set the zombie state
648ddf9c4f7SJohn Baldwin 	 * before we release the parent process' proc lock to avoid
649ddf9c4f7SJohn Baldwin 	 * a lost wakeup.  So, we first call wakeup, then we grab the
650ddf9c4f7SJohn Baldwin 	 * sched lock, update the state, and release the parent process'
651ddf9c4f7SJohn Baldwin 	 * proc lock.
652eb30c1c0SPeter Wemm 	 */
653ddf9c4f7SJohn Baldwin 	wakeup(p->p_pptr);
654e3d3e828SMateusz Guzik 	cv_broadcast(&p->p_pwait);
655982d11f8SJeff Roberson 	sched_exit(p->p_pptr, td);
656982d11f8SJeff Roberson 	PROC_SLOCK(p);
657e602ba25SJulian Elischer 	p->p_state = PRS_ZOMBIE;
658d7aadbf9SJohn Baldwin 	PROC_UNLOCK(p->p_pptr);
659871684b8SBruce Evans 
660f6f230feSJeff Roberson 	/*
661a140976eSAttilio Rao 	 * Save our children's rusage information in our exit rusage.
662a140976eSAttilio Rao 	 */
6635c7bebf9SKonstantin Belousov 	PROC_STATLOCK(p);
664a140976eSAttilio Rao 	ruadd(&p->p_ru, &p->p_rux, &p->p_stats->p_cru, &p->p_crux);
6655c7bebf9SKonstantin Belousov 	PROC_STATUNLOCK(p);
666a140976eSAttilio Rao 
667a140976eSAttilio Rao 	/*
668696058c3SJulian Elischer 	 * Make sure the scheduler takes this thread out of its tables etc.
669e602ba25SJulian Elischer 	 * This will also release this thread's reference to the ucred.
670696058c3SJulian Elischer 	 * Other thread parts to release include pcb bits and such.
671e602ba25SJulian Elischer 	 */
672e602ba25SJulian Elischer 	thread_exit();
673df8bae1dSRodney W. Grimes }
674df8bae1dSRodney W. Grimes 
67525f6e35aSPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_
67625f6e35aSPoul-Henning Kamp struct abort2_args {
67725f6e35aSPoul-Henning Kamp 	char *why;
67825f6e35aSPoul-Henning Kamp 	int nargs;
67925f6e35aSPoul-Henning Kamp 	void **args;
68025f6e35aSPoul-Henning Kamp };
68125f6e35aSPoul-Henning Kamp #endif
68225f6e35aSPoul-Henning Kamp 
68325f6e35aSPoul-Henning Kamp int
6848451d0ddSKip Macy sys_abort2(struct thread *td, struct abort2_args *uap)
68525f6e35aSPoul-Henning Kamp {
68625f6e35aSPoul-Henning Kamp 	struct proc *p = td->td_proc;
68725f6e35aSPoul-Henning Kamp 	struct sbuf *sb;
68825f6e35aSPoul-Henning Kamp 	void *uargs[16];
68925f6e35aSPoul-Henning Kamp 	int error, i, sig;
69025f6e35aSPoul-Henning Kamp 
69125f6e35aSPoul-Henning Kamp 	/*
69225f6e35aSPoul-Henning Kamp 	 * Do it right now so we can log either proper call of abort2(), or
69325f6e35aSPoul-Henning Kamp 	 * note, that invalid argument was passed. 512 is big enough to
69425f6e35aSPoul-Henning Kamp 	 * handle 16 arguments' descriptions with additional comments.
69525f6e35aSPoul-Henning Kamp 	 */
69625f6e35aSPoul-Henning Kamp 	sb = sbuf_new(NULL, NULL, 512, SBUF_FIXEDLEN);
69725f6e35aSPoul-Henning Kamp 	sbuf_clear(sb);
69825f6e35aSPoul-Henning Kamp 	sbuf_printf(sb, "%s(pid %d uid %d) aborted: ",
69925f6e35aSPoul-Henning Kamp 	    p->p_comm, p->p_pid, td->td_ucred->cr_uid);
70025f6e35aSPoul-Henning Kamp 	/*
70125f6e35aSPoul-Henning Kamp 	 * Since we can't return from abort2(), send SIGKILL in cases, where
70225f6e35aSPoul-Henning Kamp 	 * abort2() was called improperly
70325f6e35aSPoul-Henning Kamp 	 */
70425f6e35aSPoul-Henning Kamp 	sig = SIGKILL;
70525f6e35aSPoul-Henning Kamp 	/* Prevent from DoSes from user-space. */
70625f6e35aSPoul-Henning Kamp 	if (uap->nargs < 0 || uap->nargs > 16)
70725f6e35aSPoul-Henning Kamp 		goto out;
7084218a731SPoul-Henning Kamp 	if (uap->nargs > 0) {
70925f6e35aSPoul-Henning Kamp 		if (uap->args == NULL)
71025f6e35aSPoul-Henning Kamp 			goto out;
71125f6e35aSPoul-Henning Kamp 		error = copyin(uap->args, uargs, uap->nargs * sizeof(void *));
71225f6e35aSPoul-Henning Kamp 		if (error != 0)
71325f6e35aSPoul-Henning Kamp 			goto out;
7144218a731SPoul-Henning Kamp 	}
71525f6e35aSPoul-Henning Kamp 	/*
71625f6e35aSPoul-Henning Kamp 	 * Limit size of 'reason' string to 128. Will fit even when
71725f6e35aSPoul-Henning Kamp 	 * maximal number of arguments was chosen to be logged.
71825f6e35aSPoul-Henning Kamp 	 */
71925f6e35aSPoul-Henning Kamp 	if (uap->why != NULL) {
72025f6e35aSPoul-Henning Kamp 		error = sbuf_copyin(sb, uap->why, 128);
72125f6e35aSPoul-Henning Kamp 		if (error < 0)
72225f6e35aSPoul-Henning Kamp 			goto out;
72325f6e35aSPoul-Henning Kamp 	} else {
72425f6e35aSPoul-Henning Kamp 		sbuf_printf(sb, "(null)");
72525f6e35aSPoul-Henning Kamp 	}
7264218a731SPoul-Henning Kamp 	if (uap->nargs > 0) {
72725f6e35aSPoul-Henning Kamp 		sbuf_printf(sb, "(");
72825f6e35aSPoul-Henning Kamp 		for (i = 0;i < uap->nargs; i++)
72925f6e35aSPoul-Henning Kamp 			sbuf_printf(sb, "%s%p", i == 0 ? "" : ", ", uargs[i]);
73025f6e35aSPoul-Henning Kamp 		sbuf_printf(sb, ")");
73125f6e35aSPoul-Henning Kamp 	}
73225f6e35aSPoul-Henning Kamp 	/*
73325f6e35aSPoul-Henning Kamp 	 * Final stage: arguments were proper, string has been
73425f6e35aSPoul-Henning Kamp 	 * successfully copied from userspace, and copying pointers
73525f6e35aSPoul-Henning Kamp 	 * from user-space succeed.
73625f6e35aSPoul-Henning Kamp 	 */
73725f6e35aSPoul-Henning Kamp 	sig = SIGABRT;
73825f6e35aSPoul-Henning Kamp out:
73925f6e35aSPoul-Henning Kamp 	if (sig == SIGKILL) {
74025f6e35aSPoul-Henning Kamp 		sbuf_trim(sb);
74125f6e35aSPoul-Henning Kamp 		sbuf_printf(sb, " (Reason text inaccessible)");
74225f6e35aSPoul-Henning Kamp 	}
74325f6e35aSPoul-Henning Kamp 	sbuf_cat(sb, "\n");
74425f6e35aSPoul-Henning Kamp 	sbuf_finish(sb);
74525f6e35aSPoul-Henning Kamp 	log(LOG_INFO, "%s", sbuf_data(sb));
74625f6e35aSPoul-Henning Kamp 	sbuf_delete(sb);
747b4490c6eSKonstantin Belousov 	exit1(td, 0, sig);
74825f6e35aSPoul-Henning Kamp 	return (0);
74925f6e35aSPoul-Henning Kamp }
75025f6e35aSPoul-Henning Kamp 
751b2f9e8b1SBruce Evans #ifdef COMPAT_43
752234216efSMatthew Dillon /*
753a9a64385SJohn Baldwin  * The dirty work is handled by kern_wait().
754234216efSMatthew Dillon  */
75526f9a767SRodney W. Grimes int
756830c3153SDag-Erling Smørgrav owait(struct thread *td, struct owait_args *uap __unused)
757df8bae1dSRodney W. Grimes {
758b7e23e82SJohn Baldwin 	int error, status;
759df8bae1dSRodney W. Grimes 
760b7e23e82SJohn Baldwin 	error = kern_wait(td, WAIT_ANY, &status, 0, NULL);
761b7e23e82SJohn Baldwin 	if (error == 0)
762b7e23e82SJohn Baldwin 		td->td_retval[1] = status;
763b7e23e82SJohn Baldwin 	return (error);
764df8bae1dSRodney W. Grimes }
765b2f9e8b1SBruce Evans #endif /* COMPAT_43 */
766df8bae1dSRodney W. Grimes 
767234216efSMatthew Dillon /*
768a9a64385SJohn Baldwin  * The dirty work is handled by kern_wait().
769234216efSMatthew Dillon  */
77026f9a767SRodney W. Grimes int
771f13b5a0fSKonstantin Belousov sys_wait4(struct thread *td, struct wait4_args *uap)
772df8bae1dSRodney W. Grimes {
77378c85e8dSJohn Baldwin 	struct rusage ru, *rup;
774b7e23e82SJohn Baldwin 	int error, status;
775b40ce416SJulian Elischer 
77678c85e8dSJohn Baldwin 	if (uap->rusage != NULL)
77778c85e8dSJohn Baldwin 		rup = &ru;
77878c85e8dSJohn Baldwin 	else
77978c85e8dSJohn Baldwin 		rup = NULL;
78078c85e8dSJohn Baldwin 	error = kern_wait(td, uap->pid, &status, uap->options, rup);
781d30e66e5SJilles Tjoelker 	if (uap->status != NULL && error == 0 && td->td_retval[0] != 0)
782b7e23e82SJohn Baldwin 		error = copyout(&status, uap->status, sizeof(status));
783d30e66e5SJilles Tjoelker 	if (uap->rusage != NULL && error == 0 && td->td_retval[0] != 0)
784b7e23e82SJohn Baldwin 		error = copyout(&ru, uap->rusage, sizeof(struct rusage));
785b7e23e82SJohn Baldwin 	return (error);
786df8bae1dSRodney W. Grimes }
787df8bae1dSRodney W. Grimes 
788f13b5a0fSKonstantin Belousov int
789f13b5a0fSKonstantin Belousov sys_wait6(struct thread *td, struct wait6_args *uap)
790f13b5a0fSKonstantin Belousov {
791f13b5a0fSKonstantin Belousov 	struct __wrusage wru, *wrup;
792f13b5a0fSKonstantin Belousov 	siginfo_t si, *sip;
793f13b5a0fSKonstantin Belousov 	idtype_t idtype;
794f13b5a0fSKonstantin Belousov 	id_t id;
795a2a85596SKonstantin Belousov 	int error, status;
796f13b5a0fSKonstantin Belousov 
797f13b5a0fSKonstantin Belousov 	idtype = uap->idtype;
798f13b5a0fSKonstantin Belousov 	id = uap->id;
799f13b5a0fSKonstantin Belousov 
800f13b5a0fSKonstantin Belousov 	if (uap->wrusage != NULL)
801f13b5a0fSKonstantin Belousov 		wrup = &wru;
802f13b5a0fSKonstantin Belousov 	else
803f13b5a0fSKonstantin Belousov 		wrup = NULL;
804f13b5a0fSKonstantin Belousov 
805f13b5a0fSKonstantin Belousov 	if (uap->info != NULL) {
806f13b5a0fSKonstantin Belousov 		sip = &si;
807f13b5a0fSKonstantin Belousov 		bzero(sip, sizeof(*sip));
808f13b5a0fSKonstantin Belousov 	} else
809f13b5a0fSKonstantin Belousov 		sip = NULL;
810f13b5a0fSKonstantin Belousov 
811f13b5a0fSKonstantin Belousov 	/*
812f13b5a0fSKonstantin Belousov 	 *  We expect all callers of wait6() to know about WEXITED and
813f13b5a0fSKonstantin Belousov 	 *  WTRAPPED.
814f13b5a0fSKonstantin Belousov 	 */
815f13b5a0fSKonstantin Belousov 	error = kern_wait6(td, idtype, id, &status, uap->options, wrup, sip);
816f13b5a0fSKonstantin Belousov 
817d30e66e5SJilles Tjoelker 	if (uap->status != NULL && error == 0 && td->td_retval[0] != 0)
818f13b5a0fSKonstantin Belousov 		error = copyout(&status, uap->status, sizeof(status));
819d30e66e5SJilles Tjoelker 	if (uap->wrusage != NULL && error == 0 && td->td_retval[0] != 0)
820f13b5a0fSKonstantin Belousov 		error = copyout(&wru, uap->wrusage, sizeof(wru));
821f13b5a0fSKonstantin Belousov 	if (uap->info != NULL && error == 0)
822f13b5a0fSKonstantin Belousov 		error = copyout(&si, uap->info, sizeof(si));
823f13b5a0fSKonstantin Belousov 	return (error);
824f13b5a0fSKonstantin Belousov }
825f13b5a0fSKonstantin Belousov 
826324fb6beSRobert Watson /*
827324fb6beSRobert Watson  * Reap the remains of a zombie process and optionally return status and
828324fb6beSRobert Watson  * rusage.  Asserts and will release both the proctree_lock and the process
829324fb6beSRobert Watson  * lock as part of its work.
830324fb6beSRobert Watson  */
831cfb5f768SJonathan Anderson void
832f13b5a0fSKonstantin Belousov proc_reap(struct thread *td, struct proc *p, int *status, int options)
833324fb6beSRobert Watson {
834324fb6beSRobert Watson 	struct proc *q, *t;
835324fb6beSRobert Watson 
836324fb6beSRobert Watson 	sx_assert(&proctree_lock, SA_XLOCKED);
837324fb6beSRobert Watson 	PROC_LOCK_ASSERT(p, MA_OWNED);
838324fb6beSRobert Watson 	KASSERT(p->p_state == PRS_ZOMBIE, ("proc_reap: !PRS_ZOMBIE"));
839324fb6beSRobert Watson 
8402ca66c1eSMateusz Guzik 	mtx_spin_wait_unlocked(&p->p_slock);
8412ca66c1eSMateusz Guzik 
842324fb6beSRobert Watson 	q = td->td_proc;
843f13b5a0fSKonstantin Belousov 
844324fb6beSRobert Watson 	if (status)
845b4490c6eSKonstantin Belousov 		*status = KW_EXITCODE(p->p_xexit, p->p_xsig);
846324fb6beSRobert Watson 	if (options & WNOWAIT) {
847324fb6beSRobert Watson 		/*
848324fb6beSRobert Watson 		 *  Only poll, returning the status.  Caller does not wish to
849324fb6beSRobert Watson 		 * release the proc struct just yet.
850324fb6beSRobert Watson 		 */
851324fb6beSRobert Watson 		PROC_UNLOCK(p);
852324fb6beSRobert Watson 		sx_xunlock(&proctree_lock);
853324fb6beSRobert Watson 		return;
854324fb6beSRobert Watson 	}
855324fb6beSRobert Watson 
856324fb6beSRobert Watson 	PROC_LOCK(q);
857324fb6beSRobert Watson 	sigqueue_take(p->p_ksi);
858324fb6beSRobert Watson 	PROC_UNLOCK(q);
859324fb6beSRobert Watson 
860324fb6beSRobert Watson 	/*
861324fb6beSRobert Watson 	 * If we got the child via a ptrace 'attach', we need to give it back
862324fb6beSRobert Watson 	 * to the old parent.
863324fb6beSRobert Watson 	 */
8642c054ce9SMateusz Guzik 	if (p->p_oppid != p->p_pptr->p_pid) {
865c209e3e2SJohn Baldwin 		PROC_UNLOCK(p);
866d7359980SKonstantin Belousov 		t = proc_realparent(p);
867d7359980SKonstantin Belousov 		PROC_LOCK(t);
868324fb6beSRobert Watson 		PROC_LOCK(p);
869515b7a0bSJohn Baldwin 		CTR2(KTR_PTRACE,
870515b7a0bSJohn Baldwin 		    "wait: traced child %d moved back to parent %d", p->p_pid,
871515b7a0bSJohn Baldwin 		    t->p_pid);
8722c054ce9SMateusz Guzik 		proc_reparent(p, t, false);
873324fb6beSRobert Watson 		PROC_UNLOCK(p);
874ad6eec7bSJohn Baldwin 		pksignal(t, SIGCHLD, p->p_ksi);
875324fb6beSRobert Watson 		wakeup(t);
876324fb6beSRobert Watson 		cv_broadcast(&p->p_pwait);
877324fb6beSRobert Watson 		PROC_UNLOCK(t);
878324fb6beSRobert Watson 		sx_xunlock(&proctree_lock);
879324fb6beSRobert Watson 		return;
880324fb6beSRobert Watson 	}
881c209e3e2SJohn Baldwin 	PROC_UNLOCK(p);
882324fb6beSRobert Watson 
883324fb6beSRobert Watson 	/*
884324fb6beSRobert Watson 	 * Remove other references to this process to ensure we have an
885324fb6beSRobert Watson 	 * exclusive reference.
886324fb6beSRobert Watson 	 */
8873d3e6793SMateusz Guzik 	sx_xlock(PIDHASHLOCK(p->p_pid));
8883d3e6793SMateusz Guzik 	LIST_REMOVE(p, p_hash);
8893d3e6793SMateusz Guzik 	sx_xunlock(PIDHASHLOCK(p->p_pid));
890324fb6beSRobert Watson 	LIST_REMOVE(p, p_sibling);
891237623b0SKonstantin Belousov 	reaper_abandon_children(p, true);
89234ebdceaSMateusz Guzik 	reaper_clear(p);
8937335ed90SKonstantin Belousov 	PROC_LOCK(p);
8949db97ca0SMariusz Zaborski 	proc_clear_orphan(p);
8957335ed90SKonstantin Belousov 	PROC_UNLOCK(p);
896324fb6beSRobert Watson 	leavepgrp(p);
897cfb5f768SJonathan Anderson 	if (p->p_procdesc != NULL)
898cfb5f768SJonathan Anderson 		procdesc_reap(p);
899324fb6beSRobert Watson 	sx_xunlock(&proctree_lock);
900324fb6beSRobert Watson 
9019576ff58SMateusz Guzik 	proc_id_clear(PROC_ID_PID, p->p_pid);
9029576ff58SMateusz Guzik 
9039e590ff0SKonstantin Belousov 	PROC_LOCK(p);
9049e590ff0SKonstantin Belousov 	knlist_detach(p->p_klist);
9059e590ff0SKonstantin Belousov 	p->p_klist = NULL;
9069e590ff0SKonstantin Belousov 	PROC_UNLOCK(p);
9079e590ff0SKonstantin Belousov 
908324fb6beSRobert Watson 	/*
909fbe503d4SMateusz Guzik 	 * Removal from allproc list and process group list paired with
910fbe503d4SMateusz Guzik 	 * PROC_LOCK which was executed during that time should guarantee
911fbe503d4SMateusz Guzik 	 * nothing can reach this process anymore. As such further locking
912fbe503d4SMateusz Guzik 	 * is unnecessary.
913324fb6beSRobert Watson 	 */
914b4490c6eSKonstantin Belousov 	p->p_xexit = p->p_xsig = 0;		/* XXX: why? */
915fbe503d4SMateusz Guzik 
916324fb6beSRobert Watson 	PROC_LOCK(q);
917324fb6beSRobert Watson 	ruadd(&q->p_stats->p_cru, &q->p_crux, &p->p_ru, &p->p_rux);
918324fb6beSRobert Watson 	PROC_UNLOCK(q);
919324fb6beSRobert Watson 
920324fb6beSRobert Watson 	/*
921324fb6beSRobert Watson 	 * Decrement the count of procs running with this uid.
922324fb6beSRobert Watson 	 */
923324fb6beSRobert Watson 	(void)chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0);
924324fb6beSRobert Watson 
925324fb6beSRobert Watson 	/*
926097055e2SEdward Tomasz Napierala 	 * Destroy resource accounting information associated with the process.
927097055e2SEdward Tomasz Napierala 	 */
928afcc55f3SEdward Tomasz Napierala #ifdef RACCT
9294b5c9cf6SEdward Tomasz Napierala 	if (racct_enable) {
930b38520f0SEdward Tomasz Napierala 		PROC_LOCK(p);
931b38520f0SEdward Tomasz Napierala 		racct_sub(p, RACCT_NPROC, 1);
932b38520f0SEdward Tomasz Napierala 		PROC_UNLOCK(p);
9334b5c9cf6SEdward Tomasz Napierala 	}
934afcc55f3SEdward Tomasz Napierala #endif
935b38520f0SEdward Tomasz Napierala 	racct_proc_exit(p);
936097055e2SEdward Tomasz Napierala 
937097055e2SEdward Tomasz Napierala 	/*
938324fb6beSRobert Watson 	 * Free credentials, arguments, and sigacts.
939324fb6beSRobert Watson 	 */
940324fb6beSRobert Watson 	crfree(p->p_ucred);
941daf63fd2SMateusz Guzik 	proc_set_cred(p, NULL);
942324fb6beSRobert Watson 	pargs_drop(p->p_args);
943324fb6beSRobert Watson 	p->p_args = NULL;
944324fb6beSRobert Watson 	sigacts_free(p->p_sigacts);
945324fb6beSRobert Watson 	p->p_sigacts = NULL;
946324fb6beSRobert Watson 
947324fb6beSRobert Watson 	/*
948324fb6beSRobert Watson 	 * Do any thread-system specific cleanups.
949324fb6beSRobert Watson 	 */
950324fb6beSRobert Watson 	thread_wait(p);
951324fb6beSRobert Watson 
952324fb6beSRobert Watson 	/*
953324fb6beSRobert Watson 	 * Give vm and machine-dependent layer a chance to free anything that
954324fb6beSRobert Watson 	 * cpu_exit couldn't release while still running in process context.
955324fb6beSRobert Watson 	 */
956324fb6beSRobert Watson 	vm_waitproc(p);
957324fb6beSRobert Watson #ifdef MAC
958324fb6beSRobert Watson 	mac_proc_destroy(p);
959324fb6beSRobert Watson #endif
9606520495aSAdrian Chadd 
961324fb6beSRobert Watson 	KASSERT(FIRST_THREAD_IN_PROC(p),
962324fb6beSRobert Watson 	    ("proc_reap: no residual thread!"));
963324fb6beSRobert Watson 	uma_zfree(proc_zone, p);
9644b48959fSKonstantin Belousov 	atomic_add_int(&nprocs, -1);
965324fb6beSRobert Watson }
966324fb6beSRobert Watson 
967dcd43281SKonstantin Belousov static int
968f13b5a0fSKonstantin Belousov proc_to_reap(struct thread *td, struct proc *p, idtype_t idtype, id_t id,
96957c74f5bSJohn Baldwin     int *status, int options, struct __wrusage *wrusage, siginfo_t *siginfo,
97057c74f5bSJohn Baldwin     int check_only)
971dcd43281SKonstantin Belousov {
972f13b5a0fSKonstantin Belousov 	struct rusage *rup;
973dcd43281SKonstantin Belousov 
974db62ced2SJaakko Heinonen 	sx_assert(&proctree_lock, SA_XLOCKED);
975db62ced2SJaakko Heinonen 
976dcd43281SKonstantin Belousov 	PROC_LOCK(p);
977f13b5a0fSKonstantin Belousov 
978f13b5a0fSKonstantin Belousov 	switch (idtype) {
979f13b5a0fSKonstantin Belousov 	case P_ALL:
9808e493611SMariusz Zaborski 		if (p->p_procdesc == NULL ||
9818e493611SMariusz Zaborski 		   (p->p_pptr == td->td_proc &&
9828e493611SMariusz Zaborski 		   (p->p_flag & P_TRACED) != 0)) {
9838e493611SMariusz Zaborski 			break;
9848e493611SMariusz Zaborski 		}
9858e493611SMariusz Zaborski 
98653bf545dSMariusz Zaborski 		PROC_UNLOCK(p);
98753bf545dSMariusz Zaborski 		return (0);
988f13b5a0fSKonstantin Belousov 	case P_PID:
989f13b5a0fSKonstantin Belousov 		if (p->p_pid != (pid_t)id) {
990dcd43281SKonstantin Belousov 			PROC_UNLOCK(p);
991dcd43281SKonstantin Belousov 			return (0);
992dcd43281SKonstantin Belousov 		}
993f13b5a0fSKonstantin Belousov 		break;
994f13b5a0fSKonstantin Belousov 	case P_PGID:
995f13b5a0fSKonstantin Belousov 		if (p->p_pgid != (pid_t)id) {
996f13b5a0fSKonstantin Belousov 			PROC_UNLOCK(p);
997f13b5a0fSKonstantin Belousov 			return (0);
998f13b5a0fSKonstantin Belousov 		}
999f13b5a0fSKonstantin Belousov 		break;
1000f13b5a0fSKonstantin Belousov 	case P_SID:
1001f13b5a0fSKonstantin Belousov 		if (p->p_session->s_sid != (pid_t)id) {
1002f13b5a0fSKonstantin Belousov 			PROC_UNLOCK(p);
1003f13b5a0fSKonstantin Belousov 			return (0);
1004f13b5a0fSKonstantin Belousov 		}
1005f13b5a0fSKonstantin Belousov 		break;
1006f13b5a0fSKonstantin Belousov 	case P_UID:
1007f13b5a0fSKonstantin Belousov 		if (p->p_ucred->cr_uid != (uid_t)id) {
1008f13b5a0fSKonstantin Belousov 			PROC_UNLOCK(p);
1009f13b5a0fSKonstantin Belousov 			return (0);
1010f13b5a0fSKonstantin Belousov 		}
1011f13b5a0fSKonstantin Belousov 		break;
1012f13b5a0fSKonstantin Belousov 	case P_GID:
1013f13b5a0fSKonstantin Belousov 		if (p->p_ucred->cr_gid != (gid_t)id) {
1014f13b5a0fSKonstantin Belousov 			PROC_UNLOCK(p);
1015f13b5a0fSKonstantin Belousov 			return (0);
1016f13b5a0fSKonstantin Belousov 		}
1017f13b5a0fSKonstantin Belousov 		break;
1018f13b5a0fSKonstantin Belousov 	case P_JAILID:
1019b1051d92SMateusz Guzik 		if (p->p_ucred->cr_prison->pr_id != (int)id) {
1020f13b5a0fSKonstantin Belousov 			PROC_UNLOCK(p);
1021f13b5a0fSKonstantin Belousov 			return (0);
1022f13b5a0fSKonstantin Belousov 		}
1023f13b5a0fSKonstantin Belousov 		break;
1024f13b5a0fSKonstantin Belousov 	/*
1025f13b5a0fSKonstantin Belousov 	 * It seems that the thread structures get zeroed out
1026f13b5a0fSKonstantin Belousov 	 * at process exit.  This makes it impossible to
1027f13b5a0fSKonstantin Belousov 	 * support P_SETID, P_CID or P_CPUID.
1028f13b5a0fSKonstantin Belousov 	 */
1029f13b5a0fSKonstantin Belousov 	default:
1030f13b5a0fSKonstantin Belousov 		PROC_UNLOCK(p);
1031f13b5a0fSKonstantin Belousov 		return (0);
1032f13b5a0fSKonstantin Belousov 	}
1033f13b5a0fSKonstantin Belousov 
1034dcd43281SKonstantin Belousov 	if (p_canwait(td, p)) {
1035dcd43281SKonstantin Belousov 		PROC_UNLOCK(p);
1036dcd43281SKonstantin Belousov 		return (0);
1037dcd43281SKonstantin Belousov 	}
1038dcd43281SKonstantin Belousov 
1039f13b5a0fSKonstantin Belousov 	if (((options & WEXITED) == 0) && (p->p_state == PRS_ZOMBIE)) {
1040f13b5a0fSKonstantin Belousov 		PROC_UNLOCK(p);
1041f13b5a0fSKonstantin Belousov 		return (0);
1042f13b5a0fSKonstantin Belousov 	}
1043f13b5a0fSKonstantin Belousov 
1044dcd43281SKonstantin Belousov 	/*
1045dcd43281SKonstantin Belousov 	 * This special case handles a kthread spawned by linux_clone
1046dcd43281SKonstantin Belousov 	 * (see linux_misc.c).  The linux_wait4 and linux_waitpid
1047dcd43281SKonstantin Belousov 	 * functions need to be able to distinguish between waiting
1048dcd43281SKonstantin Belousov 	 * on a process and waiting on a thread.  It is a thread if
1049dcd43281SKonstantin Belousov 	 * p_sigparent is not SIGCHLD, and the WLINUXCLONE option
1050dcd43281SKonstantin Belousov 	 * signifies we want to wait for threads and not processes.
1051dcd43281SKonstantin Belousov 	 */
1052dcd43281SKonstantin Belousov 	if ((p->p_sigparent != SIGCHLD) ^
1053dcd43281SKonstantin Belousov 	    ((options & WLINUXCLONE) != 0)) {
1054dcd43281SKonstantin Belousov 		PROC_UNLOCK(p);
1055dcd43281SKonstantin Belousov 		return (0);
1056dcd43281SKonstantin Belousov 	}
1057dcd43281SKonstantin Belousov 
1058f13b5a0fSKonstantin Belousov 	if (siginfo != NULL) {
1059f13b5a0fSKonstantin Belousov 		bzero(siginfo, sizeof(*siginfo));
1060f13b5a0fSKonstantin Belousov 		siginfo->si_errno = 0;
1061f13b5a0fSKonstantin Belousov 
1062f13b5a0fSKonstantin Belousov 		/*
1063f13b5a0fSKonstantin Belousov 		 * SUSv4 requires that the si_signo value is always
1064f13b5a0fSKonstantin Belousov 		 * SIGCHLD. Obey it despite the rfork(2) interface
1065f13b5a0fSKonstantin Belousov 		 * allows to request other signal for child exit
1066f13b5a0fSKonstantin Belousov 		 * notification.
1067f13b5a0fSKonstantin Belousov 		 */
1068f13b5a0fSKonstantin Belousov 		siginfo->si_signo = SIGCHLD;
1069f13b5a0fSKonstantin Belousov 
1070f13b5a0fSKonstantin Belousov 		/*
1071f13b5a0fSKonstantin Belousov 		 *  This is still a rough estimate.  We will fix the
1072f13b5a0fSKonstantin Belousov 		 *  cases TRAPPED, STOPPED, and CONTINUED later.
1073f13b5a0fSKonstantin Belousov 		 */
1074b4490c6eSKonstantin Belousov 		if (WCOREDUMP(p->p_xsig)) {
1075f13b5a0fSKonstantin Belousov 			siginfo->si_code = CLD_DUMPED;
1076b4490c6eSKonstantin Belousov 			siginfo->si_status = WTERMSIG(p->p_xsig);
1077b4490c6eSKonstantin Belousov 		} else if (WIFSIGNALED(p->p_xsig)) {
1078f13b5a0fSKonstantin Belousov 			siginfo->si_code = CLD_KILLED;
1079b4490c6eSKonstantin Belousov 			siginfo->si_status = WTERMSIG(p->p_xsig);
1080b20a9aa9SJilles Tjoelker 		} else {
1081f13b5a0fSKonstantin Belousov 			siginfo->si_code = CLD_EXITED;
1082b4490c6eSKonstantin Belousov 			siginfo->si_status = p->p_xexit;
1083b20a9aa9SJilles Tjoelker 		}
1084f13b5a0fSKonstantin Belousov 
1085f13b5a0fSKonstantin Belousov 		siginfo->si_pid = p->p_pid;
1086f13b5a0fSKonstantin Belousov 		siginfo->si_uid = p->p_ucred->cr_uid;
1087f13b5a0fSKonstantin Belousov 
1088f13b5a0fSKonstantin Belousov 		/*
1089f13b5a0fSKonstantin Belousov 		 * The si_addr field would be useful additional
1090f13b5a0fSKonstantin Belousov 		 * detail, but apparently the PC value may be lost
1091f13b5a0fSKonstantin Belousov 		 * when we reach this point.  bzero() above sets
1092f13b5a0fSKonstantin Belousov 		 * siginfo->si_addr to NULL.
1093f13b5a0fSKonstantin Belousov 		 */
1094f13b5a0fSKonstantin Belousov 	}
1095f13b5a0fSKonstantin Belousov 
1096f13b5a0fSKonstantin Belousov 	/*
1097f13b5a0fSKonstantin Belousov 	 * There should be no reason to limit resources usage info to
1098f13b5a0fSKonstantin Belousov 	 * exited processes only.  A snapshot about any resources used
1099f13b5a0fSKonstantin Belousov 	 * by a stopped process may be exactly what is needed.
1100f13b5a0fSKonstantin Belousov 	 */
1101f13b5a0fSKonstantin Belousov 	if (wrusage != NULL) {
1102f13b5a0fSKonstantin Belousov 		rup = &wrusage->wru_self;
1103f13b5a0fSKonstantin Belousov 		*rup = p->p_ru;
11045c7bebf9SKonstantin Belousov 		PROC_STATLOCK(p);
1105f13b5a0fSKonstantin Belousov 		calcru(p, &rup->ru_utime, &rup->ru_stime);
11065c7bebf9SKonstantin Belousov 		PROC_STATUNLOCK(p);
1107f13b5a0fSKonstantin Belousov 
1108f13b5a0fSKonstantin Belousov 		rup = &wrusage->wru_children;
1109f13b5a0fSKonstantin Belousov 		*rup = p->p_stats->p_cru;
1110f13b5a0fSKonstantin Belousov 		calccru(p, &rup->ru_utime, &rup->ru_stime);
1111f13b5a0fSKonstantin Belousov 	}
1112f13b5a0fSKonstantin Belousov 
111357c74f5bSJohn Baldwin 	if (p->p_state == PRS_ZOMBIE && !check_only) {
1114f13b5a0fSKonstantin Belousov 		proc_reap(td, p, status, options);
1115dcd43281SKonstantin Belousov 		return (-1);
1116dcd43281SKonstantin Belousov 	}
1117dcd43281SKonstantin Belousov 	return (1);
1118dcd43281SKonstantin Belousov }
1119dcd43281SKonstantin Belousov 
1120b7e23e82SJohn Baldwin int
11217eaec467SJohn Baldwin kern_wait(struct thread *td, pid_t pid, int *status, int options,
11227eaec467SJohn Baldwin     struct rusage *rusage)
1123df8bae1dSRodney W. Grimes {
1124f13b5a0fSKonstantin Belousov 	struct __wrusage wru, *wrup;
1125f13b5a0fSKonstantin Belousov 	idtype_t idtype;
1126f13b5a0fSKonstantin Belousov 	id_t id;
1127f13b5a0fSKonstantin Belousov 	int ret;
1128f13b5a0fSKonstantin Belousov 
1129538375d4SKonstantin Belousov 	/*
1130538375d4SKonstantin Belousov 	 * Translate the special pid values into the (idtype, pid)
1131538375d4SKonstantin Belousov 	 * pair for kern_wait6.  The WAIT_MYPGRP case is handled by
1132538375d4SKonstantin Belousov 	 * kern_wait6() on its own.
1133538375d4SKonstantin Belousov 	 */
1134f13b5a0fSKonstantin Belousov 	if (pid == WAIT_ANY) {
1135f13b5a0fSKonstantin Belousov 		idtype = P_ALL;
1136f13b5a0fSKonstantin Belousov 		id = 0;
1137ea293f3fSKonstantin Belousov 	} else if (pid < 0) {
1138f13b5a0fSKonstantin Belousov 		idtype = P_PGID;
1139f13b5a0fSKonstantin Belousov 		id = (id_t)-pid;
1140ea293f3fSKonstantin Belousov 	} else {
1141f13b5a0fSKonstantin Belousov 		idtype = P_PID;
1142f13b5a0fSKonstantin Belousov 		id = (id_t)pid;
1143f13b5a0fSKonstantin Belousov 	}
1144538375d4SKonstantin Belousov 
1145f13b5a0fSKonstantin Belousov 	if (rusage != NULL)
1146f13b5a0fSKonstantin Belousov 		wrup = &wru;
1147f13b5a0fSKonstantin Belousov 	else
1148f13b5a0fSKonstantin Belousov 		wrup = NULL;
1149538375d4SKonstantin Belousov 
1150f13b5a0fSKonstantin Belousov 	/*
1151f13b5a0fSKonstantin Belousov 	 * For backward compatibility we implicitly add flags WEXITED
1152f13b5a0fSKonstantin Belousov 	 * and WTRAPPED here.
1153f13b5a0fSKonstantin Belousov 	 */
1154f13b5a0fSKonstantin Belousov 	options |= WEXITED | WTRAPPED;
1155f13b5a0fSKonstantin Belousov 	ret = kern_wait6(td, idtype, id, status, options, wrup, NULL);
1156f13b5a0fSKonstantin Belousov 	if (rusage != NULL)
1157f13b5a0fSKonstantin Belousov 		*rusage = wru.wru_self;
1158f13b5a0fSKonstantin Belousov 	return (ret);
1159f13b5a0fSKonstantin Belousov }
1160f13b5a0fSKonstantin Belousov 
1161435da985SKonstantin Belousov static void
1162435da985SKonstantin Belousov report_alive_proc(struct thread *td, struct proc *p, siginfo_t *siginfo,
1163435da985SKonstantin Belousov     int *status, int options, int si_code)
1164435da985SKonstantin Belousov {
1165435da985SKonstantin Belousov 	bool cont;
1166435da985SKonstantin Belousov 
1167435da985SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
1168435da985SKonstantin Belousov 	sx_assert(&proctree_lock, SA_XLOCKED);
1169435da985SKonstantin Belousov 	MPASS(si_code == CLD_TRAPPED || si_code == CLD_STOPPED ||
1170435da985SKonstantin Belousov 	    si_code == CLD_CONTINUED);
1171435da985SKonstantin Belousov 
1172435da985SKonstantin Belousov 	cont = si_code == CLD_CONTINUED;
1173435da985SKonstantin Belousov 	if ((options & WNOWAIT) == 0) {
1174435da985SKonstantin Belousov 		if (cont)
1175435da985SKonstantin Belousov 			p->p_flag &= ~P_CONTINUED;
1176435da985SKonstantin Belousov 		else
1177435da985SKonstantin Belousov 			p->p_flag |= P_WAITED;
1178435da985SKonstantin Belousov 		PROC_LOCK(td->td_proc);
1179435da985SKonstantin Belousov 		sigqueue_take(p->p_ksi);
1180435da985SKonstantin Belousov 		PROC_UNLOCK(td->td_proc);
1181435da985SKonstantin Belousov 	}
1182435da985SKonstantin Belousov 	sx_xunlock(&proctree_lock);
1183435da985SKonstantin Belousov 	if (siginfo != NULL) {
1184435da985SKonstantin Belousov 		siginfo->si_code = si_code;
1185435da985SKonstantin Belousov 		siginfo->si_status = cont ? SIGCONT : p->p_xsig;
1186435da985SKonstantin Belousov 	}
1187435da985SKonstantin Belousov 	if (status != NULL)
1188435da985SKonstantin Belousov 		*status = cont ? SIGCONT : W_STOPCODE(p->p_xsig);
1189435da985SKonstantin Belousov 	PROC_UNLOCK(p);
1190435da985SKonstantin Belousov 	td->td_retval[0] = p->p_pid;
1191435da985SKonstantin Belousov }
1192435da985SKonstantin Belousov 
1193f13b5a0fSKonstantin Belousov int
1194f13b5a0fSKonstantin Belousov kern_wait6(struct thread *td, idtype_t idtype, id_t id, int *status,
1195f13b5a0fSKonstantin Belousov     int options, struct __wrusage *wrusage, siginfo_t *siginfo)
1196f13b5a0fSKonstantin Belousov {
1197324fb6beSRobert Watson 	struct proc *p, *q;
11983a41ec6aSEd Schouten 	pid_t pid;
1199dcd43281SKonstantin Belousov 	int error, nfound, ret;
12007beb6082SMateusz Guzik 	bool report;
1201df8bae1dSRodney W. Grimes 
1202f13b5a0fSKonstantin Belousov 	AUDIT_ARG_VALUE((int)idtype);	/* XXX - This is likely wrong! */
1203f13b5a0fSKonstantin Belousov 	AUDIT_ARG_PID((pid_t)id);	/* XXX - This may be wrong! */
12042ef24ddeSRobert Watson 	AUDIT_ARG_VALUE(options);
1205de3007e8SWayne Salamon 
1206b40ce416SJulian Elischer 	q = td->td_proc;
1207f13b5a0fSKonstantin Belousov 
1208a2a85596SKonstantin Belousov 	if ((pid_t)id == WAIT_MYPGRP && (idtype == P_PID || idtype == P_PGID)) {
1209a545089eSKonstantin Belousov 		PROC_LOCK(q);
1210f13b5a0fSKonstantin Belousov 		id = (id_t)q->p_pgid;
1211a545089eSKonstantin Belousov 		PROC_UNLOCK(q);
1212f13b5a0fSKonstantin Belousov 		idtype = P_PGID;
1213f591779bSSeigo Tanimura 	}
1214f13b5a0fSKonstantin Belousov 
1215f528c3fdSDavid E. O'Brien 	/* If we don't know the option, just return. */
1216f13b5a0fSKonstantin Belousov 	if ((options & ~(WUNTRACED | WNOHANG | WCONTINUED | WNOWAIT |
1217f13b5a0fSKonstantin Belousov 	    WEXITED | WTRAPPED | WLINUXCLONE)) != 0)
12186dc958b9SJohn Baldwin 		return (EINVAL);
1219f13b5a0fSKonstantin Belousov 	if ((options & (WEXITED | WUNTRACED | WCONTINUED | WTRAPPED)) == 0) {
1220f13b5a0fSKonstantin Belousov 		/*
1221f13b5a0fSKonstantin Belousov 		 * We will be unable to find any matching processes,
1222f13b5a0fSKonstantin Belousov 		 * because there are no known events to look for.
1223f13b5a0fSKonstantin Belousov 		 * Prefer to return error instead of blocking
1224f13b5a0fSKonstantin Belousov 		 * indefinitely.
1225f13b5a0fSKonstantin Belousov 		 */
1226f13b5a0fSKonstantin Belousov 		return (EINVAL);
1227f13b5a0fSKonstantin Belousov 	}
1228f13b5a0fSKonstantin Belousov 
1229df8bae1dSRodney W. Grimes loop:
1230902c0d82SDavid Xu 	if (q->p_flag & P_STATCHILD) {
1231902c0d82SDavid Xu 		PROC_LOCK(q);
1232902c0d82SDavid Xu 		q->p_flag &= ~P_STATCHILD;
1233902c0d82SDavid Xu 		PROC_UNLOCK(q);
1234902c0d82SDavid Xu 	}
1235d7aadbf9SJohn Baldwin 	sx_xlock(&proctree_lock);
12366776bfebSMateusz Guzik loop_locked:
12376776bfebSMateusz Guzik 	nfound = 0;
12382e3c8fcbSPoul-Henning Kamp 	LIST_FOREACH(p, &q->p_children, p_sibling) {
12393a41ec6aSEd Schouten 		pid = p->p_pid;
1240f13b5a0fSKonstantin Belousov 		ret = proc_to_reap(td, p, idtype, id, status, options,
124157c74f5bSJohn Baldwin 		    wrusage, siginfo, 0);
1242dcd43281SKonstantin Belousov 		if (ret == 0)
1243df8bae1dSRodney W. Grimes 			continue;
12446776bfebSMateusz Guzik 		else if (ret != 1) {
12453a41ec6aSEd Schouten 			td->td_retval[0] = pid;
1246d7aadbf9SJohn Baldwin 			return (0);
12473a41ec6aSEd Schouten 		}
1248dcd43281SKonstantin Belousov 
12496776bfebSMateusz Guzik 		nfound++;
1250746b6e81SMateusz Guzik 		PROC_LOCK_ASSERT(p, MA_OWNED);
1251435da985SKonstantin Belousov 
1252f13b5a0fSKonstantin Belousov 		if ((options & WTRAPPED) != 0 &&
12537beb6082SMateusz Guzik 		    (p->p_flag & P_TRACED) != 0) {
12547beb6082SMateusz Guzik 			PROC_SLOCK(p);
12557beb6082SMateusz Guzik 			report =
12567beb6082SMateusz Guzik 			    ((p->p_flag & (P_STOPPED_TRACE | P_STOPPED_SIG)) &&
1257435da985SKonstantin Belousov 			    p->p_suspcount == p->p_numthreads &&
12587beb6082SMateusz Guzik 			    (p->p_flag & P_WAITED) == 0);
12598bf6ff22SMateusz Guzik 			PROC_SUNLOCK(p);
12607beb6082SMateusz Guzik 			if (report) {
1261515b7a0bSJohn Baldwin 			CTR4(KTR_PTRACE,
1262435da985SKonstantin Belousov 			    "wait: returning trapped pid %d status %#x "
1263435da985SKonstantin Belousov 			    "(xstat %d) xthread %d",
1264b4490c6eSKonstantin Belousov 			    p->p_pid, W_STOPCODE(p->p_xsig), p->p_xsig,
1265435da985SKonstantin Belousov 			    p->p_xthread != NULL ?
1266435da985SKonstantin Belousov 			    p->p_xthread->td_tid : -1);
12677beb6082SMateusz Guzik 				report_alive_proc(td, p, siginfo, status,
12687beb6082SMateusz Guzik 				    options, CLD_TRAPPED);
1269f13b5a0fSKonstantin Belousov 				return (0);
1270f13b5a0fSKonstantin Belousov 			}
12717beb6082SMateusz Guzik 		}
1272f13b5a0fSKonstantin Belousov 		if ((options & WUNTRACED) != 0 &&
12737beb6082SMateusz Guzik 		    (p->p_flag & P_STOPPED_SIG) != 0) {
12747beb6082SMateusz Guzik 			PROC_SLOCK(p);
12757beb6082SMateusz Guzik 			report = (p->p_suspcount == p->p_numthreads &&
12767beb6082SMateusz Guzik 			    ((p->p_flag & P_WAITED) == 0));
12778bf6ff22SMateusz Guzik 			PROC_SUNLOCK(p);
12787beb6082SMateusz Guzik 			if (report) {
12797beb6082SMateusz Guzik 				report_alive_proc(td, p, siginfo, status,
12807beb6082SMateusz Guzik 				    options, CLD_STOPPED);
1281b7e23e82SJohn Baldwin 				return (0);
1282df8bae1dSRodney W. Grimes 			}
12837beb6082SMateusz Guzik 		}
1284f13b5a0fSKonstantin Belousov 		if ((options & WCONTINUED) != 0 &&
1285f13b5a0fSKonstantin Belousov 		    (p->p_flag & P_CONTINUED) != 0) {
1286435da985SKonstantin Belousov 			report_alive_proc(td, p, siginfo, status, options,
1287435da985SKonstantin Belousov 			    CLD_CONTINUED);
1288dcdc6c36SKonstantin Belousov 			return (0);
12896ee093fbSMike Barcroft 		}
1290d7aadbf9SJohn Baldwin 		PROC_UNLOCK(p);
1291d7aadbf9SJohn Baldwin 	}
1292dcd43281SKonstantin Belousov 
1293dcd43281SKonstantin Belousov 	/*
1294dcd43281SKonstantin Belousov 	 * Look in the orphans list too, to allow the parent to
1295dcd43281SKonstantin Belousov 	 * collect it's child exit status even if child is being
1296dcd43281SKonstantin Belousov 	 * debugged.
1297dcd43281SKonstantin Belousov 	 *
1298dcd43281SKonstantin Belousov 	 * Debugger detaches from the parent upon successful
1299dcd43281SKonstantin Belousov 	 * switch-over from parent to child.  At this point due to
1300dcd43281SKonstantin Belousov 	 * re-parenting the parent loses the child to debugger and a
1301dcd43281SKonstantin Belousov 	 * wait4(2) call would report that it has no children to wait
1302dcd43281SKonstantin Belousov 	 * for.  By maintaining a list of orphans we allow the parent
1303dcd43281SKonstantin Belousov 	 * to successfully wait until the child becomes a zombie.
1304dcd43281SKonstantin Belousov 	 */
130557c74f5bSJohn Baldwin 	if (nfound == 0) {
1306dcd43281SKonstantin Belousov 		LIST_FOREACH(p, &q->p_orphans, p_orphan) {
130757c74f5bSJohn Baldwin 			ret = proc_to_reap(td, p, idtype, id, NULL, options,
130857c74f5bSJohn Baldwin 			    NULL, NULL, 1);
130957c74f5bSJohn Baldwin 			if (ret != 0) {
131057c74f5bSJohn Baldwin 				KASSERT(ret != -1, ("reaped an orphan (pid %d)",
131157c74f5bSJohn Baldwin 				    (int)td->td_retval[0]));
1312746b6e81SMateusz Guzik 				PROC_UNLOCK(p);
1313dcd43281SKonstantin Belousov 				nfound++;
131457c74f5bSJohn Baldwin 				break;
131557c74f5bSJohn Baldwin 			}
131657c74f5bSJohn Baldwin 		}
1317dcd43281SKonstantin Belousov 	}
1318d7aadbf9SJohn Baldwin 	if (nfound == 0) {
1319d7aadbf9SJohn Baldwin 		sx_xunlock(&proctree_lock);
1320d7aadbf9SJohn Baldwin 		return (ECHILD);
1321d7aadbf9SJohn Baldwin 	}
1322b7e23e82SJohn Baldwin 	if (options & WNOHANG) {
1323d7aadbf9SJohn Baldwin 		sx_xunlock(&proctree_lock);
1324d7aadbf9SJohn Baldwin 		td->td_retval[0] = 0;
1325d7aadbf9SJohn Baldwin 		return (0);
1326d7aadbf9SJohn Baldwin 	}
1327d7aadbf9SJohn Baldwin 	PROC_LOCK(q);
132895992d56SDavid Xu 	if (q->p_flag & P_STATCHILD) {
132995992d56SDavid Xu 		q->p_flag &= ~P_STATCHILD;
1330d7aadbf9SJohn Baldwin 		PROC_UNLOCK(q);
13316776bfebSMateusz Guzik 		goto loop_locked;
13326776bfebSMateusz Guzik 	}
13336776bfebSMateusz Guzik 	sx_xunlock(&proctree_lock);
13346776bfebSMateusz Guzik 	error = msleep(q, &q->p_mtx, PWAIT | PCATCH | PDROP, "wait", 0);
13354ae89b95SJohn Baldwin 	if (error)
1336d7aadbf9SJohn Baldwin 		return (error);
1337d7aadbf9SJohn Baldwin 	goto loop;
1338d7aadbf9SJohn Baldwin }
1339df8bae1dSRodney W. Grimes 
1340799d92abSMariusz Zaborski void
1341799d92abSMariusz Zaborski proc_add_orphan(struct proc *child, struct proc *parent)
1342799d92abSMariusz Zaborski {
1343799d92abSMariusz Zaborski 
1344799d92abSMariusz Zaborski 	sx_assert(&proctree_lock, SX_XLOCKED);
1345799d92abSMariusz Zaborski 	KASSERT((child->p_flag & P_TRACED) != 0,
1346799d92abSMariusz Zaborski 	    ("proc_add_orphan: not traced"));
1347799d92abSMariusz Zaborski 
1348799d92abSMariusz Zaborski 	if (LIST_EMPTY(&parent->p_orphans)) {
1349799d92abSMariusz Zaborski 		child->p_treeflag |= P_TREE_FIRST_ORPHAN;
1350799d92abSMariusz Zaborski 		LIST_INSERT_HEAD(&parent->p_orphans, child, p_orphan);
1351799d92abSMariusz Zaborski 	} else {
1352799d92abSMariusz Zaborski 		LIST_INSERT_AFTER(LIST_FIRST(&parent->p_orphans),
1353799d92abSMariusz Zaborski 		    child, p_orphan);
1354799d92abSMariusz Zaborski 	}
1355799d92abSMariusz Zaborski 	child->p_treeflag |= P_TREE_ORPHANED;
1356799d92abSMariusz Zaborski }
1357799d92abSMariusz Zaborski 
1358df8bae1dSRodney W. Grimes /*
135998f03f90SJake Burkholder  * Make process 'parent' the new parent of process 'child'.
136098f03f90SJake Burkholder  * Must be called with an exclusive hold of proctree lock.
1361df8bae1dSRodney W. Grimes  */
1362df8bae1dSRodney W. Grimes void
13632c054ce9SMateusz Guzik proc_reparent(struct proc *child, struct proc *parent, bool set_oppid)
1364df8bae1dSRodney W. Grimes {
1365df8bae1dSRodney W. Grimes 
13664e5e677bSJohn Baldwin 	sx_assert(&proctree_lock, SX_XLOCKED);
1367c65437a3SJohn Baldwin 	PROC_LOCK_ASSERT(child, MA_OWNED);
1368df8bae1dSRodney W. Grimes 	if (child->p_pptr == parent)
1369df8bae1dSRodney W. Grimes 		return;
1370df8bae1dSRodney W. Grimes 
1371ff766807SDavid Xu 	PROC_LOCK(child->p_pptr);
1372ff766807SDavid Xu 	sigqueue_take(child->p_ksi);
1373ff766807SDavid Xu 	PROC_UNLOCK(child->p_pptr);
1374b75356e1SJeffrey Hsu 	LIST_REMOVE(child, p_sibling);
1375b75356e1SJeffrey Hsu 	LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
1376dcd43281SKonstantin Belousov 
13779db97ca0SMariusz Zaborski 	proc_clear_orphan(child);
1378799d92abSMariusz Zaborski 	if ((child->p_flag & P_TRACED) != 0) {
1379799d92abSMariusz Zaborski 		proc_add_orphan(child, child->p_pptr);
1380dcd43281SKonstantin Belousov 	}
1381dcd43281SKonstantin Belousov 
1382df8bae1dSRodney W. Grimes 	child->p_pptr = parent;
13832c054ce9SMateusz Guzik 	if (set_oppid)
13842c054ce9SMateusz Guzik 		child->p_oppid = parent->p_pid;
1385df8bae1dSRodney W. Grimes }
1386