xref: /freebsd/sys/kern/kern_exit.c (revision 799d92ab784c346a539844a882ce2e5d05e3d6b0)
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>
562a024a2bSSean Eric Fagan #include <sys/pioctl.h>
57413628a7SBjoern A. Zeeb #include <sys/jail.h>
58df8bae1dSRodney W. Grimes #include <sys/tty.h>
59df8bae1dSRodney W. Grimes #include <sys/wait.h>
60eb30c1c0SPeter Wemm #include <sys/vmmeter.h>
617a6b989bSJohn Baldwin #include <sys/vnode.h>
62097055e2SEdward Tomasz Napierala #include <sys/racct.h>
63df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
6425f6e35aSPoul-Henning Kamp #include <sys/sbuf.h>
65797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
66b43179fbSJeff Roberson #include <sys/sched.h>
671005a129SJohn Baldwin #include <sys/sx.h>
68c8837938SJohn Baldwin #include <sys/syscallsubr.h>
6925f6e35aSPoul-Henning Kamp #include <sys/syslog.h>
70df8bae1dSRodney W. Grimes #include <sys/ptrace.h>
717c409b8aSJeffrey Hsu #include <sys/acct.h>		/* for acct_process() function prototype */
72797f2d22SPoul-Henning Kamp #include <sys/filedesc.h>
735d217f17SJohn Birrell #include <sys/sdt.h>
74780dc5a8SPeter Wemm #include <sys/shm.h>
75780dc5a8SPeter Wemm #include <sys/sem.h>
7613dad108SKonstantin Belousov #include <sys/umtx.h>
776c84de02SJohn Baldwin #ifdef KTRACE
786c84de02SJohn Baldwin #include <sys/ktrace.h>
796c84de02SJohn Baldwin #endif
80780dc5a8SPeter Wemm 
81fcf7f27aSRobert Watson #include <security/audit/audit.h>
82aed55708SRobert Watson #include <security/mac/mac_framework.h>
83fcf7f27aSRobert Watson 
84df8bae1dSRodney W. Grimes #include <vm/vm.h>
85eb30c1c0SPeter Wemm #include <vm/vm_extern.h>
867a6b989bSJohn Baldwin #include <vm/vm_param.h>
87efeaf95aSDavid Greenman #include <vm/pmap.h>
88efeaf95aSDavid Greenman #include <vm/vm_map.h>
892d21129dSAlan Cox #include <vm/vm_page.h>
90c897b813SJeff Roberson #include <vm/uma.h>
91df8bae1dSRodney W. Grimes 
925d217f17SJohn Birrell #ifdef KDTRACE_HOOKS
935d217f17SJohn Birrell #include <sys/dtrace_bsd.h>
945d217f17SJohn Birrell dtrace_execexit_func_t	dtrace_fasttrap_exit;
955d217f17SJohn Birrell #endif
965d217f17SJohn Birrell 
975d217f17SJohn Birrell SDT_PROVIDER_DECLARE(proc);
9836160958SMark Johnston SDT_PROBE_DEFINE1(proc, , , exit, "int");
995d217f17SJohn Birrell 
100c0bc2867SGleb Smirnoff /* Hook for NFS teardown procedure. */
101c0bc2867SGleb Smirnoff void (*nlminfo_release_p)(struct proc *p);
102c0bc2867SGleb Smirnoff 
103d7359980SKonstantin Belousov struct proc *
104d7359980SKonstantin Belousov proc_realparent(struct proc *child)
105d7359980SKonstantin Belousov {
106d7359980SKonstantin Belousov 	struct proc *p, *parent;
107d7359980SKonstantin Belousov 
108d7359980SKonstantin Belousov 	sx_assert(&proctree_lock, SX_LOCKED);
1092c054ce9SMateusz Guzik 	if ((child->p_treeflag & P_TREE_ORPHANED) == 0)
1102c054ce9SMateusz Guzik 		return (child->p_pptr->p_pid == child->p_oppid ?
1112c054ce9SMateusz Guzik 			    child->p_pptr : initproc);
112d7359980SKonstantin Belousov 	for (p = child; (p->p_treeflag & P_TREE_FIRST_ORPHAN) == 0;) {
113d7359980SKonstantin Belousov 		/* Cannot use LIST_PREV(), since the list head is not known. */
114d7359980SKonstantin Belousov 		p = __containerof(p->p_orphan.le_prev, struct proc,
115d7359980SKonstantin Belousov 		    p_orphan.le_next);
116d7359980SKonstantin Belousov 		KASSERT((p->p_treeflag & P_TREE_ORPHANED) != 0,
117d7359980SKonstantin Belousov 		    ("missing P_ORPHAN %p", p));
118d7359980SKonstantin Belousov 	}
119d7359980SKonstantin Belousov 	parent = __containerof(p->p_orphan.le_prev, struct proc,
120d7359980SKonstantin Belousov 	    p_orphans.lh_first);
121d7359980SKonstantin Belousov 	return (parent);
122d7359980SKonstantin Belousov }
123d7359980SKonstantin Belousov 
124237623b0SKonstantin Belousov void
125237623b0SKonstantin Belousov reaper_abandon_children(struct proc *p, bool exiting)
126237623b0SKonstantin Belousov {
127237623b0SKonstantin Belousov 	struct proc *p1, *p2, *ptmp;
128237623b0SKonstantin Belousov 
129237623b0SKonstantin Belousov 	sx_assert(&proctree_lock, SX_LOCKED);
130237623b0SKonstantin Belousov 	KASSERT(p != initproc, ("reaper_abandon_children for initproc"));
131237623b0SKonstantin Belousov 	if ((p->p_treeflag & P_TREE_REAPER) == 0)
132237623b0SKonstantin Belousov 		return;
133237623b0SKonstantin Belousov 	p1 = p->p_reaper;
134237623b0SKonstantin Belousov 	LIST_FOREACH_SAFE(p2, &p->p_reaplist, p_reapsibling, ptmp) {
135237623b0SKonstantin Belousov 		LIST_REMOVE(p2, p_reapsibling);
136237623b0SKonstantin Belousov 		p2->p_reaper = p1;
137237623b0SKonstantin Belousov 		p2->p_reapsubtree = p->p_reapsubtree;
138237623b0SKonstantin Belousov 		LIST_INSERT_HEAD(&p1->p_reaplist, p2, p_reapsibling);
139237623b0SKonstantin Belousov 		if (exiting && p2->p_pptr == p) {
140237623b0SKonstantin Belousov 			PROC_LOCK(p2);
1412c054ce9SMateusz Guzik 			proc_reparent(p2, p1, true);
142237623b0SKonstantin Belousov 			PROC_UNLOCK(p2);
143237623b0SKonstantin Belousov 		}
144237623b0SKonstantin Belousov 	}
145237623b0SKonstantin Belousov 	KASSERT(LIST_EMPTY(&p->p_reaplist), ("p_reaplist not empty"));
146237623b0SKonstantin Belousov 	p->p_treeflag &= ~P_TREE_REAPER;
147237623b0SKonstantin Belousov }
148237623b0SKonstantin Belousov 
1492e39e24fSKonstantin Belousov static void
15034ebdceaSMateusz Guzik reaper_clear(struct proc *p)
15134ebdceaSMateusz Guzik {
15234ebdceaSMateusz Guzik 	struct proc *p1;
15334ebdceaSMateusz Guzik 	bool clear;
15434ebdceaSMateusz Guzik 
15534ebdceaSMateusz Guzik 	sx_assert(&proctree_lock, SX_LOCKED);
15634ebdceaSMateusz Guzik 	LIST_REMOVE(p, p_reapsibling);
15734ebdceaSMateusz Guzik 	if (p->p_reapsubtree == 1)
15834ebdceaSMateusz Guzik 		return;
15934ebdceaSMateusz Guzik 	clear = true;
16034ebdceaSMateusz Guzik 	LIST_FOREACH(p1, &p->p_reaper->p_reaplist, p_reapsibling) {
16134ebdceaSMateusz Guzik 		if (p1->p_reapsubtree == p->p_reapsubtree) {
16234ebdceaSMateusz Guzik 			clear = false;
16334ebdceaSMateusz Guzik 			break;
16434ebdceaSMateusz Guzik 		}
16534ebdceaSMateusz Guzik 	}
16634ebdceaSMateusz Guzik 	if (clear)
16734ebdceaSMateusz Guzik 		proc_id_clear(PROC_ID_REAP, p->p_reapsubtree);
16834ebdceaSMateusz Guzik }
16934ebdceaSMateusz Guzik 
1709db97ca0SMariusz Zaborski void
1719db97ca0SMariusz Zaborski proc_clear_orphan(struct proc *p)
1722e39e24fSKonstantin Belousov {
173d7359980SKonstantin Belousov 	struct proc *p1;
1742e39e24fSKonstantin Belousov 
175d7359980SKonstantin Belousov 	sx_assert(&proctree_lock, SA_XLOCKED);
176d7359980SKonstantin Belousov 	if ((p->p_treeflag & P_TREE_ORPHANED) == 0)
177d7359980SKonstantin Belousov 		return;
178d7359980SKonstantin Belousov 	if ((p->p_treeflag & P_TREE_FIRST_ORPHAN) != 0) {
179d7359980SKonstantin Belousov 		p1 = LIST_NEXT(p, p_orphan);
180d7359980SKonstantin Belousov 		if (p1 != NULL)
181d7359980SKonstantin Belousov 			p1->p_treeflag |= P_TREE_FIRST_ORPHAN;
182d7359980SKonstantin Belousov 		p->p_treeflag &= ~P_TREE_FIRST_ORPHAN;
1832e39e24fSKonstantin Belousov 	}
184d7359980SKonstantin Belousov 	LIST_REMOVE(p, p_orphan);
185d7359980SKonstantin Belousov 	p->p_treeflag &= ~P_TREE_ORPHANED;
1862e39e24fSKonstantin Belousov }
1872e39e24fSKonstantin Belousov 
188df8bae1dSRodney W. Grimes /*
189873fbcd7SRobert Watson  * exit -- death of process.
190df8bae1dSRodney W. Grimes  */
191fc0b1dbfSBruce Evans void
1928451d0ddSKip Macy sys_sys_exit(struct thread *td, struct sys_exit_args *uap)
193df8bae1dSRodney W. Grimes {
194b40ce416SJulian Elischer 
195b4490c6eSKonstantin Belousov 	exit1(td, uap->rval, 0);
196df8bae1dSRodney W. Grimes 	/* NOTREACHED */
197df8bae1dSRodney W. Grimes }
198df8bae1dSRodney W. Grimes 
199df8bae1dSRodney W. Grimes /*
200873fbcd7SRobert Watson  * Exit: deallocate address space and other resources, change proc state to
201873fbcd7SRobert Watson  * zombie, and unlink proc from allproc and parent's lists.  Save exit status
202873fbcd7SRobert Watson  * and rusage for wait().  Check for child processes and orphan them.
203df8bae1dSRodney W. Grimes  */
204fc0b1dbfSBruce Evans void
205b4490c6eSKonstantin Belousov exit1(struct thread *td, int rval, int signo)
206df8bae1dSRodney W. Grimes {
207a661bebeSMateusz Guzik 	struct proc *p, *nq, *q, *t;
208a661bebeSMateusz Guzik 	struct thread *tdt;
2099a2dde80SKonstantin Belousov 	ksiginfo_t *ksi, *ksi1;
210015cd8dcSMateusz Guzik 	int signal_parent;
211df8bae1dSRodney W. Grimes 
212e17660e7SKris Kennaway 	mtx_assert(&Giant, MA_NOTOWNED);
213b4490c6eSKonstantin Belousov 	KASSERT(rval == 0 || signo == 0, ("exit1 rv %d sig %d", rval, signo));
2140cddd8f0SMatthew Dillon 
215276c5169SJohn Baldwin 	p = td->td_proc;
2165486ffc8SMarius Strobl 	/*
2175486ffc8SMarius Strobl 	 * XXX in case we're rebooting we just let init die in order to
2185486ffc8SMarius Strobl 	 * work around an unsolved stack overflow seen very late during
2195486ffc8SMarius Strobl 	 * shutdown on sparc64 when the gmirror worker process exists.
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 	/*
28306ad42b2SJohn Baldwin 	 * Wakeup anyone in procfs' PIOCWAIT.  They should have a hold
28406ad42b2SJohn Baldwin 	 * on our vmspace, so we should block below until they have
28506ad42b2SJohn Baldwin 	 * released their reference to us.  Note that if they have
28606ad42b2SJohn Baldwin 	 * requested S_EXIT stops we will block here until they ack
28706ad42b2SJohn Baldwin 	 * via PIOCCONT.
28806ad42b2SJohn Baldwin 	 */
289b4490c6eSKonstantin Belousov 	_STOPEVENT(p, S_EXIT, 0);
29006ad42b2SJohn Baldwin 
29106ad42b2SJohn Baldwin 	/*
2920f14f15bSJohn Baldwin 	 * Ignore any pending request to stop due to a stop signal.
2930f14f15bSJohn Baldwin 	 * Once P_WEXIT is set, future requests will be ignored as
2940f14f15bSJohn Baldwin 	 * well.
2950f14f15bSJohn Baldwin 	 */
2960f14f15bSJohn Baldwin 	p->p_flag &= ~P_STOPPED_SIG;
2970f14f15bSJohn Baldwin 	KASSERT(!P_SHOULDSTOP(p), ("exiting process is stopped"));
2980f14f15bSJohn Baldwin 
2990f14f15bSJohn Baldwin 	/*
30006ad42b2SJohn Baldwin 	 * Note that we are exiting and do another wakeup of anyone in
30106ad42b2SJohn Baldwin 	 * PIOCWAIT in case they aren't listening for S_EXIT stops or
30206ad42b2SJohn Baldwin 	 * decided to wait again after we told them we are exiting.
30306ad42b2SJohn Baldwin 	 */
304e602ba25SJulian Elischer 	p->p_flag |= P_WEXIT;
30506ad42b2SJohn Baldwin 	wakeup(&p->p_stype);
30606ad42b2SJohn Baldwin 
30706ad42b2SJohn Baldwin 	/*
30806ad42b2SJohn Baldwin 	 * Wait for any processes that have a hold on our vmspace to
30906ad42b2SJohn Baldwin 	 * release their reference.
31006ad42b2SJohn Baldwin 	 */
31106ad42b2SJohn Baldwin 	while (p->p_lock > 0)
31206ad42b2SJohn Baldwin 		msleep(&p->p_lock, &p->p_mtx, PWAIT, "exithold", 0);
313ebceaf6dSDavid Xu 
314e602ba25SJulian Elischer 	PROC_UNLOCK(p);
3151c4bcd05SJeff Roberson 	/* Drain the limit callout while we don't have the proc locked */
3161c4bcd05SJeff Roberson 	callout_drain(&p->p_limco);
317b40ce416SJulian Elischer 
31800c28d96SRobert Watson #ifdef AUDIT
31900c28d96SRobert Watson 	/*
32000c28d96SRobert Watson 	 * The Sun BSM exit token contains two components: an exit status as
32100c28d96SRobert Watson 	 * passed to exit(), and a return value to indicate what sort of exit
32200c28d96SRobert Watson 	 * it was.  The exit status is WEXITSTATUS(rv), but it's not clear
32300c28d96SRobert Watson 	 * what the return value is.
32400c28d96SRobert Watson 	 */
325b4490c6eSKonstantin Belousov 	AUDIT_ARG_EXIT(rval, 0);
32600c28d96SRobert Watson 	AUDIT_SYSCALL_EXIT(0, td);
32700c28d96SRobert Watson #endif
32800c28d96SRobert Watson 
329bad2520aSMateusz Guzik 	/* Are we a task leader with peers? */
330bad2520aSMateusz Guzik 	if (p->p_peers != NULL && p == p->p_leader) {
331c6544064SJohn Baldwin 		mtx_lock(&ppeers_lock);
3322c1011f7SJohn Dyson 		q = p->p_peers;
333776e0b36SJohn Baldwin 		while (q != NULL) {
334776e0b36SJohn Baldwin 			PROC_LOCK(q);
3358451d0ddSKip Macy 			kern_psignal(q, SIGKILL);
336776e0b36SJohn Baldwin 			PROC_UNLOCK(q);
3372c1011f7SJohn Dyson 			q = q->p_peers;
3382c1011f7SJohn Dyson 		}
339c6544064SJohn Baldwin 		while (p->p_peers != NULL)
340c6544064SJohn Baldwin 			msleep(p, &ppeers_lock, PWAIT, "exit1", 0);
341c6544064SJohn Baldwin 		mtx_unlock(&ppeers_lock);
3422c1011f7SJohn Dyson 	}
3432c1011f7SJohn Dyson 
344fed06968SJulian Elischer 	/*
345e9189611SPeter Wemm 	 * Check if any loadable modules need anything done at process exit.
346b4490c6eSKonstantin Belousov 	 * E.g. SYSV IPC stuff.
347b4490c6eSKonstantin Belousov 	 * Event handler could change exit status.
348fed06968SJulian Elischer 	 * XXX what if one of these generates an error?
349fed06968SJulian Elischer 	 */
3502ca45184SMatt Joras 	EVENTHANDLER_DIRECT_INVOKE(process_exit, p);
351a914fb6bSJohn Baldwin 
352e3d3e828SMateusz Guzik 	/*
353e3d3e828SMateusz Guzik 	 * If parent is waiting for us to exit or exec,
354e3d3e828SMateusz Guzik 	 * P_PPWAIT is set; we will wakeup the parent below.
355e3d3e828SMateusz Guzik 	 */
356a914fb6bSJohn Baldwin 	PROC_LOCK(p);
357a282253aSJulian Elischer 	stopprofclock(p);
3588d570f64SJohn Baldwin 	p->p_ptevents = 0;
3595499ea01SJohn Baldwin 
3605499ea01SJohn Baldwin 	/*
3615499ea01SJohn Baldwin 	 * Stop the real interval timer.  If the handler is currently
3625499ea01SJohn Baldwin 	 * executing, prevent it from rearming itself and let it finish.
3635499ea01SJohn Baldwin 	 */
3645499ea01SJohn Baldwin 	if (timevalisset(&p->p_realtimer.it_value) &&
36556d48d6dSKonstantin Belousov 	    _callout_stop_safe(&p->p_itcallout, CS_EXECUTING, NULL) == 0) {
3665499ea01SJohn Baldwin 		timevalclear(&p->p_realtimer.it_interval);
3675499ea01SJohn Baldwin 		msleep(&p->p_itcallout, &p->p_mtx, PWAIT, "ritwait", 0);
3685499ea01SJohn Baldwin 		KASSERT(!timevalisset(&p->p_realtimer.it_value),
3695499ea01SJohn Baldwin 		    ("realtime timer is still armed"));
3705499ea01SJohn Baldwin 	}
3712a339d9eSKonstantin Belousov 
37296d7f8efSTim J. Robbins 	PROC_UNLOCK(p);
373df8bae1dSRodney W. Grimes 
3742a339d9eSKonstantin Belousov 	umtx_thread_exit(td);
3752a339d9eSKonstantin Belousov 
376df8bae1dSRodney W. Grimes 	/*
377831d27a9SDon Lewis 	 * Reset any sigio structures pointing to us as a result of
378831d27a9SDon Lewis 	 * F_SETOWN with our pid.
379831d27a9SDon Lewis 	 */
380831d27a9SDon Lewis 	funsetownlst(&p->p_sigiolst);
381831d27a9SDon Lewis 
382831d27a9SDon Lewis 	/*
383c0bc2867SGleb Smirnoff 	 * If this process has an nlminfo data area (for lockd), release it
384c0bc2867SGleb Smirnoff 	 */
385c0bc2867SGleb Smirnoff 	if (nlminfo_release_p != NULL && p->p_nlminfo != NULL)
386c0bc2867SGleb Smirnoff 		(*nlminfo_release_p)(p);
387c0bc2867SGleb Smirnoff 
388c0bc2867SGleb Smirnoff 	/*
389df8bae1dSRodney W. Grimes 	 * Close open files and release open-file table.
390df8bae1dSRodney W. Grimes 	 * This may block!
391df8bae1dSRodney W. Grimes 	 */
3922609222aSPawel Jakub Dawidek 	fdescfree(td);
393df8bae1dSRodney W. Grimes 
394a914fb6bSJohn Baldwin 	/*
395a2587073SPoul-Henning Kamp 	 * If this thread tickled GEOM, we need to wait for the giggling to
396a2587073SPoul-Henning Kamp 	 * stop before we return to userland
397a2587073SPoul-Henning Kamp 	 */
398a2587073SPoul-Henning Kamp 	if (td->td_pflags & TDP_GEOM)
399a2587073SPoul-Henning Kamp 		g_waitidle();
400a2587073SPoul-Henning Kamp 
401a2587073SPoul-Henning Kamp 	/*
402a914fb6bSJohn Baldwin 	 * Remove ourself from our leader's peer list and wake our leader.
403a914fb6bSJohn Baldwin 	 */
404bad2520aSMateusz Guzik 	if (p->p_leader->p_peers != NULL) {
405c6544064SJohn Baldwin 		mtx_lock(&ppeers_lock);
406bad2520aSMateusz Guzik 		if (p->p_leader->p_peers != NULL) {
40779fc0bf4SMike Smith 			q = p->p_leader;
40879fc0bf4SMike Smith 			while (q->p_peers != p)
40979fc0bf4SMike Smith 				q = q->p_peers;
41079fc0bf4SMike Smith 			q->p_peers = p->p_peers;
4117f05b035SAlfred Perlstein 			wakeup(p->p_leader);
41279fc0bf4SMike Smith 		}
413c6544064SJohn Baldwin 		mtx_unlock(&ppeers_lock);
414bad2520aSMateusz Guzik 	}
41579fc0bf4SMike Smith 
41657051fdcSTor Egge 	vmspace_exit(td);
41717182b13SMateusz Guzik 	killjobc();
418b40ce416SJulian Elischer 	(void)acct_process(td);
419bc093719SEd Schouten 
420df8bae1dSRodney W. Grimes #ifdef KTRACE
4212c255e9dSRobert Watson 	ktrprocexit(td);
422df8bae1dSRodney W. Grimes #endif
423df8bae1dSRodney W. Grimes 	/*
424ee42d0a9SDavid Malone 	 * Release reference to text vnode
425ee42d0a9SDavid Malone 	 */
42688f98985SMateusz Guzik 	if (p->p_textvp != NULL) {
42788f98985SMateusz Guzik 		vrele(p->p_textvp);
428ee42d0a9SDavid Malone 		p->p_textvp = NULL;
429ee42d0a9SDavid Malone 	}
430ee42d0a9SDavid Malone 
431ee42d0a9SDavid Malone 	/*
432d7aadbf9SJohn Baldwin 	 * Release our limits structure.
433d7aadbf9SJohn Baldwin 	 */
43488f98985SMateusz Guzik 	lim_free(p->p_limit);
435d7aadbf9SJohn Baldwin 	p->p_limit = NULL;
436d7aadbf9SJohn Baldwin 
437cf7d9a8cSDavid Xu 	tidhash_remove(td);
438cf7d9a8cSDavid Xu 
439d7aadbf9SJohn Baldwin 	/*
44081d68271SMateusz Guzik 	 * Call machine-dependent code to release any
44181d68271SMateusz Guzik 	 * machine-dependent resources other than the address space.
44281d68271SMateusz Guzik 	 * The address space is released by "vmspace_exitfree(p)" in
44381d68271SMateusz Guzik 	 * vm_waitproc().
44481d68271SMateusz Guzik 	 */
44581d68271SMateusz Guzik 	cpu_exit(td);
44681d68271SMateusz Guzik 
44781d68271SMateusz Guzik 	WITNESS_WARN(WARN_PANIC, NULL, "process (pid %d) exiting", p->p_pid);
44881d68271SMateusz Guzik 
44981d68271SMateusz Guzik 	/*
450a627b462SMateusz Guzik 	 * Move proc from allproc queue to zombproc.
451df8bae1dSRodney W. Grimes 	 */
4521005a129SJohn Baldwin 	sx_xlock(&allproc_lock);
4531e9a1bf5SMateusz Guzik 	sx_xlock(&zombproc_lock);
454b75356e1SJeffrey Hsu 	LIST_REMOVE(p, p_list);
455b75356e1SJeffrey Hsu 	LIST_INSERT_HEAD(&zombproc, p, p_list);
4561e9a1bf5SMateusz Guzik 	sx_xunlock(&zombproc_lock);
4571005a129SJohn Baldwin 	sx_xunlock(&allproc_lock);
458df8bae1dSRodney W. Grimes 
459b1fbffe7SMateusz Guzik 	sx_xlock(&proctree_lock);
46041fadb3fSMariusz Zaborski 	PROC_LOCK(p);
46141fadb3fSMariusz Zaborski 	p->p_flag &= ~(P_TRACED | P_PPWAIT | P_PPTRACE);
46241fadb3fSMariusz Zaborski 	PROC_UNLOCK(p);
463b1fbffe7SMateusz Guzik 
46437f84a60SJohn Baldwin 	/*
465a661bebeSMateusz Guzik 	 * Reparent all children processes:
466a661bebeSMateusz Guzik 	 * - traced ones to the original parent (or init if we are that parent)
467a661bebeSMateusz Guzik 	 * - the rest to init
46837f84a60SJohn Baldwin 	 */
4692e3c8fcbSPoul-Henning Kamp 	q = LIST_FIRST(&p->p_children);
470a914fb6bSJohn Baldwin 	if (q != NULL)		/* only need this if any child is S_ZOMB */
471237623b0SKonstantin Belousov 		wakeup(q->p_reaper);
472a914fb6bSJohn Baldwin 	for (; q != NULL; q = nq) {
4732e3c8fcbSPoul-Henning Kamp 		nq = LIST_NEXT(q, p_sibling);
4749a2dde80SKonstantin Belousov 		ksi = ksiginfo_alloc(TRUE);
475a914fb6bSJohn Baldwin 		PROC_LOCK(q);
4764ac9ae70SJulian Elischer 		q->p_sigparent = SIGCHLD;
477904c5ec4SDavid Xu 
478a661bebeSMateusz Guzik 		if (!(q->p_flag & P_TRACED)) {
4792c054ce9SMateusz Guzik 			proc_reparent(q, q->p_reaper, true);
480545d3122SKonstantin Belousov 			if (q->p_state == PRS_ZOMBIE) {
4819a2dde80SKonstantin Belousov 				/*
4829a2dde80SKonstantin Belousov 				 * Inform reaper about the reparented
4839a2dde80SKonstantin Belousov 				 * zombie, since wait(2) has something
4849a2dde80SKonstantin Belousov 				 * new to report.  Guarantee queueing
4859a2dde80SKonstantin Belousov 				 * of the SIGCHLD signal, similar to
4869a2dde80SKonstantin Belousov 				 * the _exit() behaviour, by providing
4879a2dde80SKonstantin Belousov 				 * our ksiginfo.  Ksi is freed by the
4889a2dde80SKonstantin Belousov 				 * signal delivery.
4899a2dde80SKonstantin Belousov 				 */
4909a2dde80SKonstantin Belousov 				if (q->p_ksi == NULL) {
4919a2dde80SKonstantin Belousov 					ksi1 = NULL;
4929a2dde80SKonstantin Belousov 				} else {
4939a2dde80SKonstantin Belousov 					ksiginfo_copy(q->p_ksi, ksi);
4949a2dde80SKonstantin Belousov 					ksi->ksi_flags |= KSI_INS;
4959a2dde80SKonstantin Belousov 					ksi1 = ksi;
4969a2dde80SKonstantin Belousov 					ksi = NULL;
4979a2dde80SKonstantin Belousov 				}
498545d3122SKonstantin Belousov 				PROC_LOCK(q->p_reaper);
4999a2dde80SKonstantin Belousov 				pksignal(q->p_reaper, SIGCHLD, ksi1);
500545d3122SKonstantin Belousov 				PROC_UNLOCK(q->p_reaper);
501b9408863SKonstantin Belousov 			} else if (q->p_pdeathsig > 0) {
502b9408863SKonstantin Belousov 				/*
503b9408863SKonstantin Belousov 				 * The child asked to received a signal
504b9408863SKonstantin Belousov 				 * when we exit.
505b9408863SKonstantin Belousov 				 */
506b9408863SKonstantin Belousov 				kern_psignal(q, q->p_pdeathsig);
507545d3122SKonstantin Belousov 			}
508a661bebeSMateusz Guzik 		} else {
509a661bebeSMateusz Guzik 			/*
510a661bebeSMateusz Guzik 			 * Traced processes are killed since their existence
511a661bebeSMateusz Guzik 			 * means someone is screwing up.
512a661bebeSMateusz Guzik 			 */
513a661bebeSMateusz Guzik 			t = proc_realparent(q);
514a661bebeSMateusz Guzik 			if (t == p) {
5152c054ce9SMateusz Guzik 				proc_reparent(q, q->p_reaper, true);
516a661bebeSMateusz Guzik 			} else {
517a661bebeSMateusz Guzik 				PROC_LOCK(t);
5182c054ce9SMateusz Guzik 				proc_reparent(q, t, true);
519a661bebeSMateusz Guzik 				PROC_UNLOCK(t);
520a661bebeSMateusz Guzik 			}
5215085ecb7SKonstantin Belousov 			/*
5225085ecb7SKonstantin Belousov 			 * Since q was found on our children list, the
5235085ecb7SKonstantin Belousov 			 * proc_reparent() call moved q to the orphan
5245085ecb7SKonstantin Belousov 			 * list due to present P_TRACED flag. Clear
5255085ecb7SKonstantin Belousov 			 * orphan link for q now while q is locked.
5265085ecb7SKonstantin Belousov 			 */
5279db97ca0SMariusz Zaborski 			proc_clear_orphan(q);
528c2836532SDavid Xu 			q->p_flag &= ~(P_TRACED | P_STOPPED_TRACE);
529b7a25e63SKonstantin Belousov 			q->p_flag2 &= ~P2_PTRACE_FSTP;
5308d570f64SJohn Baldwin 			q->p_ptevents = 0;
531b7a25e63SKonstantin Belousov 			FOREACH_THREAD_IN_PROC(q, tdt) {
532b7a25e63SKonstantin Belousov 				tdt->td_dbgflags &= ~(TDB_SUSPEND | TDB_XSIG |
533b7a25e63SKonstantin Belousov 				    TDB_FSTP);
534b7a25e63SKonstantin Belousov 			}
5358451d0ddSKip Macy 			kern_psignal(q, SIGKILL);
536c65437a3SJohn Baldwin 		}
537a914fb6bSJohn Baldwin 		PROC_UNLOCK(q);
5389a2dde80SKonstantin Belousov 		if (ksi != NULL)
5399a2dde80SKonstantin Belousov 			ksiginfo_free(ksi);
540df8bae1dSRodney W. Grimes 	}
541df8bae1dSRodney W. Grimes 
5425085ecb7SKonstantin Belousov 	/*
5435085ecb7SKonstantin Belousov 	 * Also get rid of our orphans.
5445085ecb7SKonstantin Belousov 	 */
5455085ecb7SKonstantin Belousov 	while ((q = LIST_FIRST(&p->p_orphans)) != NULL) {
5465085ecb7SKonstantin Belousov 		PROC_LOCK(q);
547128c9bc0SMark Johnston 		KASSERT(q->p_oppid == p->p_pid,
548128c9bc0SMark Johnston 		    ("orphan %p of %p has unexpected oppid %d", q, p,
549128c9bc0SMark Johnston 		    q->p_oppid));
550128c9bc0SMark Johnston 		q->p_oppid = q->p_reaper->p_pid;
551128c9bc0SMark Johnston 
552b9408863SKonstantin Belousov 		/*
553b9408863SKonstantin Belousov 		 * If we are the real parent of this process
554b9408863SKonstantin Belousov 		 * but it has been reparented to a debugger, then
555b9408863SKonstantin Belousov 		 * check if it asked for a signal when we exit.
556b9408863SKonstantin Belousov 		 */
557b9408863SKonstantin Belousov 		if (q->p_pdeathsig > 0)
558b9408863SKonstantin Belousov 			kern_psignal(q, q->p_pdeathsig);
559515b7a0bSJohn Baldwin 		CTR2(KTR_PTRACE, "exit: pid %d, clearing orphan %d", p->p_pid,
560515b7a0bSJohn Baldwin 		    q->p_pid);
5619db97ca0SMariusz Zaborski 		proc_clear_orphan(q);
5625085ecb7SKonstantin Belousov 		PROC_UNLOCK(q);
5635085ecb7SKonstantin Belousov 	}
5645085ecb7SKonstantin Belousov 
565eadb1dcbSMateusz Guzik #ifdef KDTRACE_HOOKS
566eadb1dcbSMateusz Guzik 	if (SDT_PROBES_ENABLED()) {
567eadb1dcbSMateusz Guzik 		int reason = CLD_EXITED;
568eadb1dcbSMateusz Guzik 		if (WCOREDUMP(signo))
569eadb1dcbSMateusz Guzik 			reason = CLD_DUMPED;
570eadb1dcbSMateusz Guzik 		else if (WIFSIGNALED(signo))
571eadb1dcbSMateusz Guzik 			reason = CLD_KILLED;
572eadb1dcbSMateusz Guzik 		SDT_PROBE1(proc, , , exit, reason);
573eadb1dcbSMateusz Guzik 	}
574eadb1dcbSMateusz Guzik #endif
575eadb1dcbSMateusz Guzik 
5761c4bcd05SJeff Roberson 	/* Save exit status. */
577d7aadbf9SJohn Baldwin 	PROC_LOCK(p);
578cbf4e354SDavid Xu 	p->p_xthread = td;
5795d217f17SJohn Birrell 
5805d217f17SJohn Birrell #ifdef KDTRACE_HOOKS
5815d217f17SJohn Birrell 	/*
5825d217f17SJohn Birrell 	 * Tell the DTrace fasttrap provider about the exit if it
5835d217f17SJohn Birrell 	 * has declared an interest.
5845d217f17SJohn Birrell 	 */
5855d217f17SJohn Birrell 	if (dtrace_fasttrap_exit)
5865d217f17SJohn Birrell 		dtrace_fasttrap_exit(p);
5875d217f17SJohn Birrell #endif
5885d217f17SJohn Birrell 
5891c4bcd05SJeff Roberson 	/*
5907a6b989bSJohn Baldwin 	 * Notify interested parties of our demise.
591cb679c38SJonathan Lemon 	 */
5929e590ff0SKonstantin Belousov 	KNOTE_LOCKED(p->p_klist, NOTE_EXIT);
5937eaec467SJohn Baldwin 
5941a29c806SOlivier Houchard 	/*
595cfb5f768SJonathan Anderson 	 * If this is a process with a descriptor, we may not need to deliver
596cfb5f768SJonathan Anderson 	 * a signal to the parent.  proctree_lock is held over
597cfb5f768SJonathan Anderson 	 * procdesc_exit() to serialize concurrent calls to close() and
598cfb5f768SJonathan Anderson 	 * exit().
599cfb5f768SJonathan Anderson 	 */
600015cd8dcSMateusz Guzik 	signal_parent = 0;
601cfb5f768SJonathan Anderson 	if (p->p_procdesc == NULL || procdesc_exit(p)) {
602cfb5f768SJonathan Anderson 		/*
603cfb5f768SJonathan Anderson 		 * Notify parent that we're gone.  If parent has the
604cfb5f768SJonathan Anderson 		 * PS_NOCLDWAIT flag set, or if the handler is set to SIG_IGN,
605cfb5f768SJonathan Anderson 		 * notify process 1 instead (and hope it will handle this
606cfb5f768SJonathan Anderson 		 * situation).
607df8bae1dSRodney W. Grimes 		 */
608d7aadbf9SJohn Baldwin 		PROC_LOCK(p->p_pptr);
60990af4afaSJohn Baldwin 		mtx_lock(&p->p_pptr->p_sigacts->ps_mtx);
610cfb5f768SJonathan Anderson 		if (p->p_pptr->p_sigacts->ps_flag &
611cfb5f768SJonathan Anderson 		    (PS_NOCLDWAIT | PS_CLDSIGIGN)) {
612276c5169SJohn Baldwin 			struct proc *pp;
613276c5169SJohn Baldwin 
61490af4afaSJohn Baldwin 			mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx);
615276c5169SJohn Baldwin 			pp = p->p_pptr;
616f591779bSSeigo Tanimura 			PROC_UNLOCK(pp);
6172c054ce9SMateusz Guzik 			proc_reparent(p, p->p_reaper, true);
61855b5f2a2SDon Lewis 			p->p_sigparent = SIGCHLD;
619f591779bSSeigo Tanimura 			PROC_LOCK(p->p_pptr);
620007abb3dSKonstantin Belousov 
621245f17d4SJoerg Wunsch 			/*
622007abb3dSKonstantin Belousov 			 * Notify parent, so in case he was wait(2)ing or
6236fae832aSKonstantin Belousov 			 * executing waitpid(2) with our pid, he will
624245f17d4SJoerg Wunsch 			 * continue.
625245f17d4SJoerg Wunsch 			 */
6267f05b035SAlfred Perlstein 			wakeup(pp);
62790af4afaSJohn Baldwin 		} else
62890af4afaSJohn Baldwin 			mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx);
629245f17d4SJoerg Wunsch 
630015cd8dcSMateusz Guzik 		if (p->p_pptr == p->p_reaper || p->p_pptr == initproc) {
631015cd8dcSMateusz Guzik 			signal_parent = 1;
632015cd8dcSMateusz Guzik 		} else if (p->p_sigparent != 0) {
633015cd8dcSMateusz Guzik 			if (p->p_sigparent == SIGCHLD) {
634015cd8dcSMateusz Guzik 				signal_parent = 1;
635015cd8dcSMateusz Guzik 			} else { /* LINUX thread */
636015cd8dcSMateusz Guzik 				signal_parent = 2;
637015cd8dcSMateusz Guzik 			}
638ebceaf6dSDavid Xu 		}
639cfb5f768SJonathan Anderson 	} else
640cfb5f768SJonathan Anderson 		PROC_LOCK(p->p_pptr);
641d7aadbf9SJohn Baldwin 	sx_xunlock(&proctree_lock);
642696058c3SJulian Elischer 
643015cd8dcSMateusz Guzik 	if (signal_parent == 1) {
644015cd8dcSMateusz Guzik 		childproc_exited(p);
645015cd8dcSMateusz Guzik 	} else if (signal_parent == 2) {
646015cd8dcSMateusz Guzik 		kern_psignal(p->p_pptr, p->p_sigparent);
647015cd8dcSMateusz Guzik 	}
648015cd8dcSMateusz Guzik 
6497e588b92SMateusz Guzik 	/* Tell the prison that we are gone. */
6507e588b92SMateusz Guzik 	prison_proc_free(p->p_ucred->cr_prison);
6517e588b92SMateusz Guzik 
652eb30c1c0SPeter Wemm 	/*
653f71e748dSDavid Xu 	 * The state PRS_ZOMBIE prevents other proesses from sending
654f71e748dSDavid Xu 	 * signal to the process, to avoid memory leak, we free memory
655f71e748dSDavid Xu 	 * for signal queue at the time when the state is set.
656f71e748dSDavid Xu 	 */
657f71e748dSDavid Xu 	sigqueue_flush(&p->p_sigqueue);
658f71e748dSDavid Xu 	sigqueue_flush(&td->td_sigqueue);
659f71e748dSDavid Xu 
660f71e748dSDavid Xu 	/*
661462f31bfSJohn Baldwin 	 * We have to wait until after acquiring all locks before
662a9a64385SJohn Baldwin 	 * changing p_state.  We need to avoid all possible context
663a9a64385SJohn Baldwin 	 * switches (including ones from blocking on a mutex) while
664ddf9c4f7SJohn Baldwin 	 * marked as a zombie.  We also have to set the zombie state
665ddf9c4f7SJohn Baldwin 	 * before we release the parent process' proc lock to avoid
666ddf9c4f7SJohn Baldwin 	 * a lost wakeup.  So, we first call wakeup, then we grab the
667ddf9c4f7SJohn Baldwin 	 * sched lock, update the state, and release the parent process'
668ddf9c4f7SJohn Baldwin 	 * proc lock.
669eb30c1c0SPeter Wemm 	 */
670ddf9c4f7SJohn Baldwin 	wakeup(p->p_pptr);
671e3d3e828SMateusz Guzik 	cv_broadcast(&p->p_pwait);
672982d11f8SJeff Roberson 	sched_exit(p->p_pptr, td);
673982d11f8SJeff Roberson 	PROC_SLOCK(p);
674e602ba25SJulian Elischer 	p->p_state = PRS_ZOMBIE;
675d7aadbf9SJohn Baldwin 	PROC_UNLOCK(p->p_pptr);
676871684b8SBruce Evans 
677f6f230feSJeff Roberson 	/*
678a140976eSAttilio Rao 	 * Save our children's rusage information in our exit rusage.
679a140976eSAttilio Rao 	 */
6805c7bebf9SKonstantin Belousov 	PROC_STATLOCK(p);
681a140976eSAttilio Rao 	ruadd(&p->p_ru, &p->p_rux, &p->p_stats->p_cru, &p->p_crux);
6825c7bebf9SKonstantin Belousov 	PROC_STATUNLOCK(p);
683a140976eSAttilio Rao 
684a140976eSAttilio Rao 	/*
685696058c3SJulian Elischer 	 * Make sure the scheduler takes this thread out of its tables etc.
686e602ba25SJulian Elischer 	 * This will also release this thread's reference to the ucred.
687696058c3SJulian Elischer 	 * Other thread parts to release include pcb bits and such.
688e602ba25SJulian Elischer 	 */
689e602ba25SJulian Elischer 	thread_exit();
690df8bae1dSRodney W. Grimes }
691df8bae1dSRodney W. Grimes 
69225f6e35aSPoul-Henning Kamp 
69325f6e35aSPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_
69425f6e35aSPoul-Henning Kamp struct abort2_args {
69525f6e35aSPoul-Henning Kamp 	char *why;
69625f6e35aSPoul-Henning Kamp 	int nargs;
69725f6e35aSPoul-Henning Kamp 	void **args;
69825f6e35aSPoul-Henning Kamp };
69925f6e35aSPoul-Henning Kamp #endif
70025f6e35aSPoul-Henning Kamp 
70125f6e35aSPoul-Henning Kamp int
7028451d0ddSKip Macy sys_abort2(struct thread *td, struct abort2_args *uap)
70325f6e35aSPoul-Henning Kamp {
70425f6e35aSPoul-Henning Kamp 	struct proc *p = td->td_proc;
70525f6e35aSPoul-Henning Kamp 	struct sbuf *sb;
70625f6e35aSPoul-Henning Kamp 	void *uargs[16];
70725f6e35aSPoul-Henning Kamp 	int error, i, sig;
70825f6e35aSPoul-Henning Kamp 
70925f6e35aSPoul-Henning Kamp 	/*
71025f6e35aSPoul-Henning Kamp 	 * Do it right now so we can log either proper call of abort2(), or
71125f6e35aSPoul-Henning Kamp 	 * note, that invalid argument was passed. 512 is big enough to
71225f6e35aSPoul-Henning Kamp 	 * handle 16 arguments' descriptions with additional comments.
71325f6e35aSPoul-Henning Kamp 	 */
71425f6e35aSPoul-Henning Kamp 	sb = sbuf_new(NULL, NULL, 512, SBUF_FIXEDLEN);
71525f6e35aSPoul-Henning Kamp 	sbuf_clear(sb);
71625f6e35aSPoul-Henning Kamp 	sbuf_printf(sb, "%s(pid %d uid %d) aborted: ",
71725f6e35aSPoul-Henning Kamp 	    p->p_comm, p->p_pid, td->td_ucred->cr_uid);
71825f6e35aSPoul-Henning Kamp 	/*
71925f6e35aSPoul-Henning Kamp 	 * Since we can't return from abort2(), send SIGKILL in cases, where
72025f6e35aSPoul-Henning Kamp 	 * abort2() was called improperly
72125f6e35aSPoul-Henning Kamp 	 */
72225f6e35aSPoul-Henning Kamp 	sig = SIGKILL;
72325f6e35aSPoul-Henning Kamp 	/* Prevent from DoSes from user-space. */
72425f6e35aSPoul-Henning Kamp 	if (uap->nargs < 0 || uap->nargs > 16)
72525f6e35aSPoul-Henning Kamp 		goto out;
7264218a731SPoul-Henning Kamp 	if (uap->nargs > 0) {
72725f6e35aSPoul-Henning Kamp 		if (uap->args == NULL)
72825f6e35aSPoul-Henning Kamp 			goto out;
72925f6e35aSPoul-Henning Kamp 		error = copyin(uap->args, uargs, uap->nargs * sizeof(void *));
73025f6e35aSPoul-Henning Kamp 		if (error != 0)
73125f6e35aSPoul-Henning Kamp 			goto out;
7324218a731SPoul-Henning Kamp 	}
73325f6e35aSPoul-Henning Kamp 	/*
73425f6e35aSPoul-Henning Kamp 	 * Limit size of 'reason' string to 128. Will fit even when
73525f6e35aSPoul-Henning Kamp 	 * maximal number of arguments was chosen to be logged.
73625f6e35aSPoul-Henning Kamp 	 */
73725f6e35aSPoul-Henning Kamp 	if (uap->why != NULL) {
73825f6e35aSPoul-Henning Kamp 		error = sbuf_copyin(sb, uap->why, 128);
73925f6e35aSPoul-Henning Kamp 		if (error < 0)
74025f6e35aSPoul-Henning Kamp 			goto out;
74125f6e35aSPoul-Henning Kamp 	} else {
74225f6e35aSPoul-Henning Kamp 		sbuf_printf(sb, "(null)");
74325f6e35aSPoul-Henning Kamp 	}
7444218a731SPoul-Henning Kamp 	if (uap->nargs > 0) {
74525f6e35aSPoul-Henning Kamp 		sbuf_printf(sb, "(");
74625f6e35aSPoul-Henning Kamp 		for (i = 0;i < uap->nargs; i++)
74725f6e35aSPoul-Henning Kamp 			sbuf_printf(sb, "%s%p", i == 0 ? "" : ", ", uargs[i]);
74825f6e35aSPoul-Henning Kamp 		sbuf_printf(sb, ")");
74925f6e35aSPoul-Henning Kamp 	}
75025f6e35aSPoul-Henning Kamp 	/*
75125f6e35aSPoul-Henning Kamp 	 * Final stage: arguments were proper, string has been
75225f6e35aSPoul-Henning Kamp 	 * successfully copied from userspace, and copying pointers
75325f6e35aSPoul-Henning Kamp 	 * from user-space succeed.
75425f6e35aSPoul-Henning Kamp 	 */
75525f6e35aSPoul-Henning Kamp 	sig = SIGABRT;
75625f6e35aSPoul-Henning Kamp out:
75725f6e35aSPoul-Henning Kamp 	if (sig == SIGKILL) {
75825f6e35aSPoul-Henning Kamp 		sbuf_trim(sb);
75925f6e35aSPoul-Henning Kamp 		sbuf_printf(sb, " (Reason text inaccessible)");
76025f6e35aSPoul-Henning Kamp 	}
76125f6e35aSPoul-Henning Kamp 	sbuf_cat(sb, "\n");
76225f6e35aSPoul-Henning Kamp 	sbuf_finish(sb);
76325f6e35aSPoul-Henning Kamp 	log(LOG_INFO, "%s", sbuf_data(sb));
76425f6e35aSPoul-Henning Kamp 	sbuf_delete(sb);
765b4490c6eSKonstantin Belousov 	exit1(td, 0, sig);
76625f6e35aSPoul-Henning Kamp 	return (0);
76725f6e35aSPoul-Henning Kamp }
76825f6e35aSPoul-Henning Kamp 
76925f6e35aSPoul-Henning Kamp 
770b2f9e8b1SBruce Evans #ifdef COMPAT_43
771234216efSMatthew Dillon /*
772a9a64385SJohn Baldwin  * The dirty work is handled by kern_wait().
773234216efSMatthew Dillon  */
77426f9a767SRodney W. Grimes int
775830c3153SDag-Erling Smørgrav owait(struct thread *td, struct owait_args *uap __unused)
776df8bae1dSRodney W. Grimes {
777b7e23e82SJohn Baldwin 	int error, status;
778df8bae1dSRodney W. Grimes 
779b7e23e82SJohn Baldwin 	error = kern_wait(td, WAIT_ANY, &status, 0, NULL);
780b7e23e82SJohn Baldwin 	if (error == 0)
781b7e23e82SJohn Baldwin 		td->td_retval[1] = status;
782b7e23e82SJohn Baldwin 	return (error);
783df8bae1dSRodney W. Grimes }
784b2f9e8b1SBruce Evans #endif /* COMPAT_43 */
785df8bae1dSRodney W. Grimes 
786234216efSMatthew Dillon /*
787a9a64385SJohn Baldwin  * The dirty work is handled by kern_wait().
788234216efSMatthew Dillon  */
78926f9a767SRodney W. Grimes int
790f13b5a0fSKonstantin Belousov sys_wait4(struct thread *td, struct wait4_args *uap)
791df8bae1dSRodney W. Grimes {
79278c85e8dSJohn Baldwin 	struct rusage ru, *rup;
793b7e23e82SJohn Baldwin 	int error, status;
794b40ce416SJulian Elischer 
79578c85e8dSJohn Baldwin 	if (uap->rusage != NULL)
79678c85e8dSJohn Baldwin 		rup = &ru;
79778c85e8dSJohn Baldwin 	else
79878c85e8dSJohn Baldwin 		rup = NULL;
79978c85e8dSJohn Baldwin 	error = kern_wait(td, uap->pid, &status, uap->options, rup);
800d30e66e5SJilles Tjoelker 	if (uap->status != NULL && error == 0 && td->td_retval[0] != 0)
801b7e23e82SJohn Baldwin 		error = copyout(&status, uap->status, sizeof(status));
802d30e66e5SJilles Tjoelker 	if (uap->rusage != NULL && error == 0 && td->td_retval[0] != 0)
803b7e23e82SJohn Baldwin 		error = copyout(&ru, uap->rusage, sizeof(struct rusage));
804b7e23e82SJohn Baldwin 	return (error);
805df8bae1dSRodney W. Grimes }
806df8bae1dSRodney W. Grimes 
807f13b5a0fSKonstantin Belousov int
808f13b5a0fSKonstantin Belousov sys_wait6(struct thread *td, struct wait6_args *uap)
809f13b5a0fSKonstantin Belousov {
810f13b5a0fSKonstantin Belousov 	struct __wrusage wru, *wrup;
811f13b5a0fSKonstantin Belousov 	siginfo_t si, *sip;
812f13b5a0fSKonstantin Belousov 	idtype_t idtype;
813f13b5a0fSKonstantin Belousov 	id_t id;
814a2a85596SKonstantin Belousov 	int error, status;
815f13b5a0fSKonstantin Belousov 
816f13b5a0fSKonstantin Belousov 	idtype = uap->idtype;
817f13b5a0fSKonstantin Belousov 	id = uap->id;
818f13b5a0fSKonstantin Belousov 
819f13b5a0fSKonstantin Belousov 	if (uap->wrusage != NULL)
820f13b5a0fSKonstantin Belousov 		wrup = &wru;
821f13b5a0fSKonstantin Belousov 	else
822f13b5a0fSKonstantin Belousov 		wrup = NULL;
823f13b5a0fSKonstantin Belousov 
824f13b5a0fSKonstantin Belousov 	if (uap->info != NULL) {
825f13b5a0fSKonstantin Belousov 		sip = &si;
826f13b5a0fSKonstantin Belousov 		bzero(sip, sizeof(*sip));
827f13b5a0fSKonstantin Belousov 	} else
828f13b5a0fSKonstantin Belousov 		sip = NULL;
829f13b5a0fSKonstantin Belousov 
830f13b5a0fSKonstantin Belousov 	/*
831f13b5a0fSKonstantin Belousov 	 *  We expect all callers of wait6() to know about WEXITED and
832f13b5a0fSKonstantin Belousov 	 *  WTRAPPED.
833f13b5a0fSKonstantin Belousov 	 */
834f13b5a0fSKonstantin Belousov 	error = kern_wait6(td, idtype, id, &status, uap->options, wrup, sip);
835f13b5a0fSKonstantin Belousov 
836d30e66e5SJilles Tjoelker 	if (uap->status != NULL && error == 0 && td->td_retval[0] != 0)
837f13b5a0fSKonstantin Belousov 		error = copyout(&status, uap->status, sizeof(status));
838d30e66e5SJilles Tjoelker 	if (uap->wrusage != NULL && error == 0 && td->td_retval[0] != 0)
839f13b5a0fSKonstantin Belousov 		error = copyout(&wru, uap->wrusage, sizeof(wru));
840f13b5a0fSKonstantin Belousov 	if (uap->info != NULL && error == 0)
841f13b5a0fSKonstantin Belousov 		error = copyout(&si, uap->info, sizeof(si));
842f13b5a0fSKonstantin Belousov 	return (error);
843f13b5a0fSKonstantin Belousov }
844f13b5a0fSKonstantin Belousov 
845324fb6beSRobert Watson /*
846324fb6beSRobert Watson  * Reap the remains of a zombie process and optionally return status and
847324fb6beSRobert Watson  * rusage.  Asserts and will release both the proctree_lock and the process
848324fb6beSRobert Watson  * lock as part of its work.
849324fb6beSRobert Watson  */
850cfb5f768SJonathan Anderson void
851f13b5a0fSKonstantin Belousov proc_reap(struct thread *td, struct proc *p, int *status, int options)
852324fb6beSRobert Watson {
853324fb6beSRobert Watson 	struct proc *q, *t;
854324fb6beSRobert Watson 
855324fb6beSRobert Watson 	sx_assert(&proctree_lock, SA_XLOCKED);
856324fb6beSRobert Watson 	PROC_LOCK_ASSERT(p, MA_OWNED);
857324fb6beSRobert Watson 	KASSERT(p->p_state == PRS_ZOMBIE, ("proc_reap: !PRS_ZOMBIE"));
858324fb6beSRobert Watson 
8592ca66c1eSMateusz Guzik 	mtx_spin_wait_unlocked(&p->p_slock);
8602ca66c1eSMateusz Guzik 
861324fb6beSRobert Watson 	q = td->td_proc;
862f13b5a0fSKonstantin Belousov 
863324fb6beSRobert Watson 	if (status)
864b4490c6eSKonstantin Belousov 		*status = KW_EXITCODE(p->p_xexit, p->p_xsig);
865324fb6beSRobert Watson 	if (options & WNOWAIT) {
866324fb6beSRobert Watson 		/*
867324fb6beSRobert Watson 		 *  Only poll, returning the status.  Caller does not wish to
868324fb6beSRobert Watson 		 * release the proc struct just yet.
869324fb6beSRobert Watson 		 */
870324fb6beSRobert Watson 		PROC_UNLOCK(p);
871324fb6beSRobert Watson 		sx_xunlock(&proctree_lock);
872324fb6beSRobert Watson 		return;
873324fb6beSRobert Watson 	}
874324fb6beSRobert Watson 
875324fb6beSRobert Watson 	PROC_LOCK(q);
876324fb6beSRobert Watson 	sigqueue_take(p->p_ksi);
877324fb6beSRobert Watson 	PROC_UNLOCK(q);
878324fb6beSRobert Watson 
879324fb6beSRobert Watson 	/*
880324fb6beSRobert Watson 	 * If we got the child via a ptrace 'attach', we need to give it back
881324fb6beSRobert Watson 	 * to the old parent.
882324fb6beSRobert Watson 	 */
8832c054ce9SMateusz Guzik 	if (p->p_oppid != p->p_pptr->p_pid) {
884c209e3e2SJohn Baldwin 		PROC_UNLOCK(p);
885d7359980SKonstantin Belousov 		t = proc_realparent(p);
886d7359980SKonstantin Belousov 		PROC_LOCK(t);
887324fb6beSRobert Watson 		PROC_LOCK(p);
888515b7a0bSJohn Baldwin 		CTR2(KTR_PTRACE,
889515b7a0bSJohn Baldwin 		    "wait: traced child %d moved back to parent %d", p->p_pid,
890515b7a0bSJohn Baldwin 		    t->p_pid);
8912c054ce9SMateusz Guzik 		proc_reparent(p, t, false);
892324fb6beSRobert Watson 		PROC_UNLOCK(p);
893ad6eec7bSJohn Baldwin 		pksignal(t, SIGCHLD, p->p_ksi);
894324fb6beSRobert Watson 		wakeup(t);
895324fb6beSRobert Watson 		cv_broadcast(&p->p_pwait);
896324fb6beSRobert Watson 		PROC_UNLOCK(t);
897324fb6beSRobert Watson 		sx_xunlock(&proctree_lock);
898324fb6beSRobert Watson 		return;
899324fb6beSRobert Watson 	}
900c209e3e2SJohn Baldwin 	PROC_UNLOCK(p);
901324fb6beSRobert Watson 
902324fb6beSRobert Watson 	/*
903324fb6beSRobert Watson 	 * Remove other references to this process to ensure we have an
904324fb6beSRobert Watson 	 * exclusive reference.
905324fb6beSRobert Watson 	 */
9061e9a1bf5SMateusz Guzik 	sx_xlock(&zombproc_lock);
907324fb6beSRobert Watson 	LIST_REMOVE(p, p_list);	/* off zombproc */
9081e9a1bf5SMateusz Guzik 	sx_xunlock(&zombproc_lock);
9093d3e6793SMateusz Guzik 	sx_xlock(PIDHASHLOCK(p->p_pid));
9103d3e6793SMateusz Guzik 	LIST_REMOVE(p, p_hash);
9113d3e6793SMateusz Guzik 	sx_xunlock(PIDHASHLOCK(p->p_pid));
912324fb6beSRobert Watson 	LIST_REMOVE(p, p_sibling);
913237623b0SKonstantin Belousov 	reaper_abandon_children(p, true);
91434ebdceaSMateusz Guzik 	reaper_clear(p);
91534ebdceaSMateusz Guzik 	proc_id_clear(PROC_ID_PID, p->p_pid);
9167335ed90SKonstantin Belousov 	PROC_LOCK(p);
9179db97ca0SMariusz Zaborski 	proc_clear_orphan(p);
9187335ed90SKonstantin Belousov 	PROC_UNLOCK(p);
919324fb6beSRobert Watson 	leavepgrp(p);
920cfb5f768SJonathan Anderson 	if (p->p_procdesc != NULL)
921cfb5f768SJonathan Anderson 		procdesc_reap(p);
922324fb6beSRobert Watson 	sx_xunlock(&proctree_lock);
923324fb6beSRobert Watson 
9249e590ff0SKonstantin Belousov 	PROC_LOCK(p);
9259e590ff0SKonstantin Belousov 	knlist_detach(p->p_klist);
9269e590ff0SKonstantin Belousov 	p->p_klist = NULL;
9279e590ff0SKonstantin Belousov 	PROC_UNLOCK(p);
9289e590ff0SKonstantin Belousov 
929324fb6beSRobert Watson 	/*
930fbe503d4SMateusz Guzik 	 * Removal from allproc list and process group list paired with
931fbe503d4SMateusz Guzik 	 * PROC_LOCK which was executed during that time should guarantee
932fbe503d4SMateusz Guzik 	 * nothing can reach this process anymore. As such further locking
933fbe503d4SMateusz Guzik 	 * is unnecessary.
934324fb6beSRobert Watson 	 */
935b4490c6eSKonstantin Belousov 	p->p_xexit = p->p_xsig = 0;		/* XXX: why? */
936fbe503d4SMateusz Guzik 
937324fb6beSRobert Watson 	PROC_LOCK(q);
938324fb6beSRobert Watson 	ruadd(&q->p_stats->p_cru, &q->p_crux, &p->p_ru, &p->p_rux);
939324fb6beSRobert Watson 	PROC_UNLOCK(q);
940324fb6beSRobert Watson 
941324fb6beSRobert Watson 	/*
942324fb6beSRobert Watson 	 * Decrement the count of procs running with this uid.
943324fb6beSRobert Watson 	 */
944324fb6beSRobert Watson 	(void)chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0);
945324fb6beSRobert Watson 
946324fb6beSRobert Watson 	/*
947097055e2SEdward Tomasz Napierala 	 * Destroy resource accounting information associated with the process.
948097055e2SEdward Tomasz Napierala 	 */
949afcc55f3SEdward Tomasz Napierala #ifdef RACCT
9504b5c9cf6SEdward Tomasz Napierala 	if (racct_enable) {
951b38520f0SEdward Tomasz Napierala 		PROC_LOCK(p);
952b38520f0SEdward Tomasz Napierala 		racct_sub(p, RACCT_NPROC, 1);
953b38520f0SEdward Tomasz Napierala 		PROC_UNLOCK(p);
9544b5c9cf6SEdward Tomasz Napierala 	}
955afcc55f3SEdward Tomasz Napierala #endif
956b38520f0SEdward Tomasz Napierala 	racct_proc_exit(p);
957097055e2SEdward Tomasz Napierala 
958097055e2SEdward Tomasz Napierala 	/*
959324fb6beSRobert Watson 	 * Free credentials, arguments, and sigacts.
960324fb6beSRobert Watson 	 */
961324fb6beSRobert Watson 	crfree(p->p_ucred);
962daf63fd2SMateusz Guzik 	proc_set_cred(p, NULL);
963324fb6beSRobert Watson 	pargs_drop(p->p_args);
964324fb6beSRobert Watson 	p->p_args = NULL;
965324fb6beSRobert Watson 	sigacts_free(p->p_sigacts);
966324fb6beSRobert Watson 	p->p_sigacts = NULL;
967324fb6beSRobert Watson 
968324fb6beSRobert Watson 	/*
969324fb6beSRobert Watson 	 * Do any thread-system specific cleanups.
970324fb6beSRobert Watson 	 */
971324fb6beSRobert Watson 	thread_wait(p);
972324fb6beSRobert Watson 
973324fb6beSRobert Watson 	/*
974324fb6beSRobert Watson 	 * Give vm and machine-dependent layer a chance to free anything that
975324fb6beSRobert Watson 	 * cpu_exit couldn't release while still running in process context.
976324fb6beSRobert Watson 	 */
977324fb6beSRobert Watson 	vm_waitproc(p);
978324fb6beSRobert Watson #ifdef MAC
979324fb6beSRobert Watson 	mac_proc_destroy(p);
980324fb6beSRobert Watson #endif
9816520495aSAdrian Chadd 
982324fb6beSRobert Watson 	KASSERT(FIRST_THREAD_IN_PROC(p),
983324fb6beSRobert Watson 	    ("proc_reap: no residual thread!"));
984324fb6beSRobert Watson 	uma_zfree(proc_zone, p);
9854b48959fSKonstantin Belousov 	atomic_add_int(&nprocs, -1);
986324fb6beSRobert Watson }
987324fb6beSRobert Watson 
988dcd43281SKonstantin Belousov static int
989f13b5a0fSKonstantin Belousov proc_to_reap(struct thread *td, struct proc *p, idtype_t idtype, id_t id,
99057c74f5bSJohn Baldwin     int *status, int options, struct __wrusage *wrusage, siginfo_t *siginfo,
99157c74f5bSJohn Baldwin     int check_only)
992dcd43281SKonstantin Belousov {
993f13b5a0fSKonstantin Belousov 	struct rusage *rup;
994dcd43281SKonstantin Belousov 
995db62ced2SJaakko Heinonen 	sx_assert(&proctree_lock, SA_XLOCKED);
996db62ced2SJaakko Heinonen 
997dcd43281SKonstantin Belousov 	PROC_LOCK(p);
998f13b5a0fSKonstantin Belousov 
999f13b5a0fSKonstantin Belousov 	switch (idtype) {
1000f13b5a0fSKonstantin Belousov 	case P_ALL:
100153bf545dSMariusz Zaborski 		if (p->p_procdesc != NULL) {
100253bf545dSMariusz Zaborski 			PROC_UNLOCK(p);
100353bf545dSMariusz Zaborski 			return (0);
100453bf545dSMariusz Zaborski 		}
1005f13b5a0fSKonstantin Belousov 		break;
1006f13b5a0fSKonstantin Belousov 	case P_PID:
1007f13b5a0fSKonstantin Belousov 		if (p->p_pid != (pid_t)id) {
1008dcd43281SKonstantin Belousov 			PROC_UNLOCK(p);
1009dcd43281SKonstantin Belousov 			return (0);
1010dcd43281SKonstantin Belousov 		}
1011f13b5a0fSKonstantin Belousov 		break;
1012f13b5a0fSKonstantin Belousov 	case P_PGID:
1013f13b5a0fSKonstantin Belousov 		if (p->p_pgid != (pid_t)id) {
1014f13b5a0fSKonstantin Belousov 			PROC_UNLOCK(p);
1015f13b5a0fSKonstantin Belousov 			return (0);
1016f13b5a0fSKonstantin Belousov 		}
1017f13b5a0fSKonstantin Belousov 		break;
1018f13b5a0fSKonstantin Belousov 	case P_SID:
1019f13b5a0fSKonstantin Belousov 		if (p->p_session->s_sid != (pid_t)id) {
1020f13b5a0fSKonstantin Belousov 			PROC_UNLOCK(p);
1021f13b5a0fSKonstantin Belousov 			return (0);
1022f13b5a0fSKonstantin Belousov 		}
1023f13b5a0fSKonstantin Belousov 		break;
1024f13b5a0fSKonstantin Belousov 	case P_UID:
1025f13b5a0fSKonstantin Belousov 		if (p->p_ucred->cr_uid != (uid_t)id) {
1026f13b5a0fSKonstantin Belousov 			PROC_UNLOCK(p);
1027f13b5a0fSKonstantin Belousov 			return (0);
1028f13b5a0fSKonstantin Belousov 		}
1029f13b5a0fSKonstantin Belousov 		break;
1030f13b5a0fSKonstantin Belousov 	case P_GID:
1031f13b5a0fSKonstantin Belousov 		if (p->p_ucred->cr_gid != (gid_t)id) {
1032f13b5a0fSKonstantin Belousov 			PROC_UNLOCK(p);
1033f13b5a0fSKonstantin Belousov 			return (0);
1034f13b5a0fSKonstantin Belousov 		}
1035f13b5a0fSKonstantin Belousov 		break;
1036f13b5a0fSKonstantin Belousov 	case P_JAILID:
1037b1051d92SMateusz Guzik 		if (p->p_ucred->cr_prison->pr_id != (int)id) {
1038f13b5a0fSKonstantin Belousov 			PROC_UNLOCK(p);
1039f13b5a0fSKonstantin Belousov 			return (0);
1040f13b5a0fSKonstantin Belousov 		}
1041f13b5a0fSKonstantin Belousov 		break;
1042f13b5a0fSKonstantin Belousov 	/*
1043f13b5a0fSKonstantin Belousov 	 * It seems that the thread structures get zeroed out
1044f13b5a0fSKonstantin Belousov 	 * at process exit.  This makes it impossible to
1045f13b5a0fSKonstantin Belousov 	 * support P_SETID, P_CID or P_CPUID.
1046f13b5a0fSKonstantin Belousov 	 */
1047f13b5a0fSKonstantin Belousov 	default:
1048f13b5a0fSKonstantin Belousov 		PROC_UNLOCK(p);
1049f13b5a0fSKonstantin Belousov 		return (0);
1050f13b5a0fSKonstantin Belousov 	}
1051f13b5a0fSKonstantin Belousov 
1052dcd43281SKonstantin Belousov 	if (p_canwait(td, p)) {
1053dcd43281SKonstantin Belousov 		PROC_UNLOCK(p);
1054dcd43281SKonstantin Belousov 		return (0);
1055dcd43281SKonstantin Belousov 	}
1056dcd43281SKonstantin Belousov 
1057f13b5a0fSKonstantin Belousov 	if (((options & WEXITED) == 0) && (p->p_state == PRS_ZOMBIE)) {
1058f13b5a0fSKonstantin Belousov 		PROC_UNLOCK(p);
1059f13b5a0fSKonstantin Belousov 		return (0);
1060f13b5a0fSKonstantin Belousov 	}
1061f13b5a0fSKonstantin Belousov 
1062dcd43281SKonstantin Belousov 	/*
1063dcd43281SKonstantin Belousov 	 * This special case handles a kthread spawned by linux_clone
1064dcd43281SKonstantin Belousov 	 * (see linux_misc.c).  The linux_wait4 and linux_waitpid
1065dcd43281SKonstantin Belousov 	 * functions need to be able to distinguish between waiting
1066dcd43281SKonstantin Belousov 	 * on a process and waiting on a thread.  It is a thread if
1067dcd43281SKonstantin Belousov 	 * p_sigparent is not SIGCHLD, and the WLINUXCLONE option
1068dcd43281SKonstantin Belousov 	 * signifies we want to wait for threads and not processes.
1069dcd43281SKonstantin Belousov 	 */
1070dcd43281SKonstantin Belousov 	if ((p->p_sigparent != SIGCHLD) ^
1071dcd43281SKonstantin Belousov 	    ((options & WLINUXCLONE) != 0)) {
1072dcd43281SKonstantin Belousov 		PROC_UNLOCK(p);
1073dcd43281SKonstantin Belousov 		return (0);
1074dcd43281SKonstantin Belousov 	}
1075dcd43281SKonstantin Belousov 
1076f13b5a0fSKonstantin Belousov 	if (siginfo != NULL) {
1077f13b5a0fSKonstantin Belousov 		bzero(siginfo, sizeof(*siginfo));
1078f13b5a0fSKonstantin Belousov 		siginfo->si_errno = 0;
1079f13b5a0fSKonstantin Belousov 
1080f13b5a0fSKonstantin Belousov 		/*
1081f13b5a0fSKonstantin Belousov 		 * SUSv4 requires that the si_signo value is always
1082f13b5a0fSKonstantin Belousov 		 * SIGCHLD. Obey it despite the rfork(2) interface
1083f13b5a0fSKonstantin Belousov 		 * allows to request other signal for child exit
1084f13b5a0fSKonstantin Belousov 		 * notification.
1085f13b5a0fSKonstantin Belousov 		 */
1086f13b5a0fSKonstantin Belousov 		siginfo->si_signo = SIGCHLD;
1087f13b5a0fSKonstantin Belousov 
1088f13b5a0fSKonstantin Belousov 		/*
1089f13b5a0fSKonstantin Belousov 		 *  This is still a rough estimate.  We will fix the
1090f13b5a0fSKonstantin Belousov 		 *  cases TRAPPED, STOPPED, and CONTINUED later.
1091f13b5a0fSKonstantin Belousov 		 */
1092b4490c6eSKonstantin Belousov 		if (WCOREDUMP(p->p_xsig)) {
1093f13b5a0fSKonstantin Belousov 			siginfo->si_code = CLD_DUMPED;
1094b4490c6eSKonstantin Belousov 			siginfo->si_status = WTERMSIG(p->p_xsig);
1095b4490c6eSKonstantin Belousov 		} else if (WIFSIGNALED(p->p_xsig)) {
1096f13b5a0fSKonstantin Belousov 			siginfo->si_code = CLD_KILLED;
1097b4490c6eSKonstantin Belousov 			siginfo->si_status = WTERMSIG(p->p_xsig);
1098b20a9aa9SJilles Tjoelker 		} else {
1099f13b5a0fSKonstantin Belousov 			siginfo->si_code = CLD_EXITED;
1100b4490c6eSKonstantin Belousov 			siginfo->si_status = p->p_xexit;
1101b20a9aa9SJilles Tjoelker 		}
1102f13b5a0fSKonstantin Belousov 
1103f13b5a0fSKonstantin Belousov 		siginfo->si_pid = p->p_pid;
1104f13b5a0fSKonstantin Belousov 		siginfo->si_uid = p->p_ucred->cr_uid;
1105f13b5a0fSKonstantin Belousov 
1106f13b5a0fSKonstantin Belousov 		/*
1107f13b5a0fSKonstantin Belousov 		 * The si_addr field would be useful additional
1108f13b5a0fSKonstantin Belousov 		 * detail, but apparently the PC value may be lost
1109f13b5a0fSKonstantin Belousov 		 * when we reach this point.  bzero() above sets
1110f13b5a0fSKonstantin Belousov 		 * siginfo->si_addr to NULL.
1111f13b5a0fSKonstantin Belousov 		 */
1112f13b5a0fSKonstantin Belousov 	}
1113f13b5a0fSKonstantin Belousov 
1114f13b5a0fSKonstantin Belousov 	/*
1115f13b5a0fSKonstantin Belousov 	 * There should be no reason to limit resources usage info to
1116f13b5a0fSKonstantin Belousov 	 * exited processes only.  A snapshot about any resources used
1117f13b5a0fSKonstantin Belousov 	 * by a stopped process may be exactly what is needed.
1118f13b5a0fSKonstantin Belousov 	 */
1119f13b5a0fSKonstantin Belousov 	if (wrusage != NULL) {
1120f13b5a0fSKonstantin Belousov 		rup = &wrusage->wru_self;
1121f13b5a0fSKonstantin Belousov 		*rup = p->p_ru;
11225c7bebf9SKonstantin Belousov 		PROC_STATLOCK(p);
1123f13b5a0fSKonstantin Belousov 		calcru(p, &rup->ru_utime, &rup->ru_stime);
11245c7bebf9SKonstantin Belousov 		PROC_STATUNLOCK(p);
1125f13b5a0fSKonstantin Belousov 
1126f13b5a0fSKonstantin Belousov 		rup = &wrusage->wru_children;
1127f13b5a0fSKonstantin Belousov 		*rup = p->p_stats->p_cru;
1128f13b5a0fSKonstantin Belousov 		calccru(p, &rup->ru_utime, &rup->ru_stime);
1129f13b5a0fSKonstantin Belousov 	}
1130f13b5a0fSKonstantin Belousov 
113157c74f5bSJohn Baldwin 	if (p->p_state == PRS_ZOMBIE && !check_only) {
1132f13b5a0fSKonstantin Belousov 		proc_reap(td, p, status, options);
1133dcd43281SKonstantin Belousov 		return (-1);
1134dcd43281SKonstantin Belousov 	}
1135dcd43281SKonstantin Belousov 	return (1);
1136dcd43281SKonstantin Belousov }
1137dcd43281SKonstantin Belousov 
1138b7e23e82SJohn Baldwin int
11397eaec467SJohn Baldwin kern_wait(struct thread *td, pid_t pid, int *status, int options,
11407eaec467SJohn Baldwin     struct rusage *rusage)
1141df8bae1dSRodney W. Grimes {
1142f13b5a0fSKonstantin Belousov 	struct __wrusage wru, *wrup;
1143f13b5a0fSKonstantin Belousov 	idtype_t idtype;
1144f13b5a0fSKonstantin Belousov 	id_t id;
1145f13b5a0fSKonstantin Belousov 	int ret;
1146f13b5a0fSKonstantin Belousov 
1147538375d4SKonstantin Belousov 	/*
1148538375d4SKonstantin Belousov 	 * Translate the special pid values into the (idtype, pid)
1149538375d4SKonstantin Belousov 	 * pair for kern_wait6.  The WAIT_MYPGRP case is handled by
1150538375d4SKonstantin Belousov 	 * kern_wait6() on its own.
1151538375d4SKonstantin Belousov 	 */
1152f13b5a0fSKonstantin Belousov 	if (pid == WAIT_ANY) {
1153f13b5a0fSKonstantin Belousov 		idtype = P_ALL;
1154f13b5a0fSKonstantin Belousov 		id = 0;
1155ea293f3fSKonstantin Belousov 	} else if (pid < 0) {
1156f13b5a0fSKonstantin Belousov 		idtype = P_PGID;
1157f13b5a0fSKonstantin Belousov 		id = (id_t)-pid;
1158ea293f3fSKonstantin Belousov 	} else {
1159f13b5a0fSKonstantin Belousov 		idtype = P_PID;
1160f13b5a0fSKonstantin Belousov 		id = (id_t)pid;
1161f13b5a0fSKonstantin Belousov 	}
1162538375d4SKonstantin Belousov 
1163f13b5a0fSKonstantin Belousov 	if (rusage != NULL)
1164f13b5a0fSKonstantin Belousov 		wrup = &wru;
1165f13b5a0fSKonstantin Belousov 	else
1166f13b5a0fSKonstantin Belousov 		wrup = NULL;
1167538375d4SKonstantin Belousov 
1168f13b5a0fSKonstantin Belousov 	/*
1169f13b5a0fSKonstantin Belousov 	 * For backward compatibility we implicitly add flags WEXITED
1170f13b5a0fSKonstantin Belousov 	 * and WTRAPPED here.
1171f13b5a0fSKonstantin Belousov 	 */
1172f13b5a0fSKonstantin Belousov 	options |= WEXITED | WTRAPPED;
1173f13b5a0fSKonstantin Belousov 	ret = kern_wait6(td, idtype, id, status, options, wrup, NULL);
1174f13b5a0fSKonstantin Belousov 	if (rusage != NULL)
1175f13b5a0fSKonstantin Belousov 		*rusage = wru.wru_self;
1176f13b5a0fSKonstantin Belousov 	return (ret);
1177f13b5a0fSKonstantin Belousov }
1178f13b5a0fSKonstantin Belousov 
1179435da985SKonstantin Belousov static void
1180435da985SKonstantin Belousov report_alive_proc(struct thread *td, struct proc *p, siginfo_t *siginfo,
1181435da985SKonstantin Belousov     int *status, int options, int si_code)
1182435da985SKonstantin Belousov {
1183435da985SKonstantin Belousov 	bool cont;
1184435da985SKonstantin Belousov 
1185435da985SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
1186435da985SKonstantin Belousov 	sx_assert(&proctree_lock, SA_XLOCKED);
1187435da985SKonstantin Belousov 	MPASS(si_code == CLD_TRAPPED || si_code == CLD_STOPPED ||
1188435da985SKonstantin Belousov 	    si_code == CLD_CONTINUED);
1189435da985SKonstantin Belousov 
1190435da985SKonstantin Belousov 	cont = si_code == CLD_CONTINUED;
1191435da985SKonstantin Belousov 	if ((options & WNOWAIT) == 0) {
1192435da985SKonstantin Belousov 		if (cont)
1193435da985SKonstantin Belousov 			p->p_flag &= ~P_CONTINUED;
1194435da985SKonstantin Belousov 		else
1195435da985SKonstantin Belousov 			p->p_flag |= P_WAITED;
1196435da985SKonstantin Belousov 		PROC_LOCK(td->td_proc);
1197435da985SKonstantin Belousov 		sigqueue_take(p->p_ksi);
1198435da985SKonstantin Belousov 		PROC_UNLOCK(td->td_proc);
1199435da985SKonstantin Belousov 	}
1200435da985SKonstantin Belousov 	sx_xunlock(&proctree_lock);
1201435da985SKonstantin Belousov 	if (siginfo != NULL) {
1202435da985SKonstantin Belousov 		siginfo->si_code = si_code;
1203435da985SKonstantin Belousov 		siginfo->si_status = cont ? SIGCONT : p->p_xsig;
1204435da985SKonstantin Belousov 	}
1205435da985SKonstantin Belousov 	if (status != NULL)
1206435da985SKonstantin Belousov 		*status = cont ? SIGCONT : W_STOPCODE(p->p_xsig);
1207435da985SKonstantin Belousov 	PROC_UNLOCK(p);
1208435da985SKonstantin Belousov 	td->td_retval[0] = p->p_pid;
1209435da985SKonstantin Belousov }
1210435da985SKonstantin Belousov 
1211f13b5a0fSKonstantin Belousov int
1212f13b5a0fSKonstantin Belousov kern_wait6(struct thread *td, idtype_t idtype, id_t id, int *status,
1213f13b5a0fSKonstantin Belousov     int options, struct __wrusage *wrusage, siginfo_t *siginfo)
1214f13b5a0fSKonstantin Belousov {
1215324fb6beSRobert Watson 	struct proc *p, *q;
12163a41ec6aSEd Schouten 	pid_t pid;
1217dcd43281SKonstantin Belousov 	int error, nfound, ret;
12187beb6082SMateusz Guzik 	bool report;
1219df8bae1dSRodney W. Grimes 
1220f13b5a0fSKonstantin Belousov 	AUDIT_ARG_VALUE((int)idtype);	/* XXX - This is likely wrong! */
1221f13b5a0fSKonstantin Belousov 	AUDIT_ARG_PID((pid_t)id);	/* XXX - This may be wrong! */
12222ef24ddeSRobert Watson 	AUDIT_ARG_VALUE(options);
1223de3007e8SWayne Salamon 
1224b40ce416SJulian Elischer 	q = td->td_proc;
1225f13b5a0fSKonstantin Belousov 
1226a2a85596SKonstantin Belousov 	if ((pid_t)id == WAIT_MYPGRP && (idtype == P_PID || idtype == P_PGID)) {
1227a545089eSKonstantin Belousov 		PROC_LOCK(q);
1228f13b5a0fSKonstantin Belousov 		id = (id_t)q->p_pgid;
1229a545089eSKonstantin Belousov 		PROC_UNLOCK(q);
1230f13b5a0fSKonstantin Belousov 		idtype = P_PGID;
1231f591779bSSeigo Tanimura 	}
1232f13b5a0fSKonstantin Belousov 
1233f528c3fdSDavid E. O'Brien 	/* If we don't know the option, just return. */
1234f13b5a0fSKonstantin Belousov 	if ((options & ~(WUNTRACED | WNOHANG | WCONTINUED | WNOWAIT |
1235f13b5a0fSKonstantin Belousov 	    WEXITED | WTRAPPED | WLINUXCLONE)) != 0)
12366dc958b9SJohn Baldwin 		return (EINVAL);
1237f13b5a0fSKonstantin Belousov 	if ((options & (WEXITED | WUNTRACED | WCONTINUED | WTRAPPED)) == 0) {
1238f13b5a0fSKonstantin Belousov 		/*
1239f13b5a0fSKonstantin Belousov 		 * We will be unable to find any matching processes,
1240f13b5a0fSKonstantin Belousov 		 * because there are no known events to look for.
1241f13b5a0fSKonstantin Belousov 		 * Prefer to return error instead of blocking
1242f13b5a0fSKonstantin Belousov 		 * indefinitely.
1243f13b5a0fSKonstantin Belousov 		 */
1244f13b5a0fSKonstantin Belousov 		return (EINVAL);
1245f13b5a0fSKonstantin Belousov 	}
1246f13b5a0fSKonstantin Belousov 
1247df8bae1dSRodney W. Grimes loop:
1248902c0d82SDavid Xu 	if (q->p_flag & P_STATCHILD) {
1249902c0d82SDavid Xu 		PROC_LOCK(q);
1250902c0d82SDavid Xu 		q->p_flag &= ~P_STATCHILD;
1251902c0d82SDavid Xu 		PROC_UNLOCK(q);
1252902c0d82SDavid Xu 	}
1253d7aadbf9SJohn Baldwin 	sx_xlock(&proctree_lock);
12546776bfebSMateusz Guzik loop_locked:
12556776bfebSMateusz Guzik 	nfound = 0;
12562e3c8fcbSPoul-Henning Kamp 	LIST_FOREACH(p, &q->p_children, p_sibling) {
12573a41ec6aSEd Schouten 		pid = p->p_pid;
1258f13b5a0fSKonstantin Belousov 		ret = proc_to_reap(td, p, idtype, id, status, options,
125957c74f5bSJohn Baldwin 		    wrusage, siginfo, 0);
1260dcd43281SKonstantin Belousov 		if (ret == 0)
1261df8bae1dSRodney W. Grimes 			continue;
12626776bfebSMateusz Guzik 		else if (ret != 1) {
12633a41ec6aSEd Schouten 			td->td_retval[0] = pid;
1264d7aadbf9SJohn Baldwin 			return (0);
12653a41ec6aSEd Schouten 		}
1266dcd43281SKonstantin Belousov 
12676776bfebSMateusz Guzik 		nfound++;
1268746b6e81SMateusz Guzik 		PROC_LOCK_ASSERT(p, MA_OWNED);
1269435da985SKonstantin Belousov 
1270f13b5a0fSKonstantin Belousov 		if ((options & WTRAPPED) != 0 &&
12717beb6082SMateusz Guzik 		    (p->p_flag & P_TRACED) != 0) {
12727beb6082SMateusz Guzik 			PROC_SLOCK(p);
12737beb6082SMateusz Guzik 			report =
12747beb6082SMateusz Guzik 			    ((p->p_flag & (P_STOPPED_TRACE | P_STOPPED_SIG)) &&
1275435da985SKonstantin Belousov 			    p->p_suspcount == p->p_numthreads &&
12767beb6082SMateusz Guzik 			    (p->p_flag & P_WAITED) == 0);
12778bf6ff22SMateusz Guzik 			PROC_SUNLOCK(p);
12787beb6082SMateusz Guzik 			if (report) {
1279515b7a0bSJohn Baldwin 			CTR4(KTR_PTRACE,
1280435da985SKonstantin Belousov 			    "wait: returning trapped pid %d status %#x "
1281435da985SKonstantin Belousov 			    "(xstat %d) xthread %d",
1282b4490c6eSKonstantin Belousov 			    p->p_pid, W_STOPCODE(p->p_xsig), p->p_xsig,
1283435da985SKonstantin Belousov 			    p->p_xthread != NULL ?
1284435da985SKonstantin Belousov 			    p->p_xthread->td_tid : -1);
12857beb6082SMateusz Guzik 				report_alive_proc(td, p, siginfo, status,
12867beb6082SMateusz Guzik 				    options, CLD_TRAPPED);
1287f13b5a0fSKonstantin Belousov 				return (0);
1288f13b5a0fSKonstantin Belousov 			}
12897beb6082SMateusz Guzik 		}
1290f13b5a0fSKonstantin Belousov 		if ((options & WUNTRACED) != 0 &&
12917beb6082SMateusz Guzik 		    (p->p_flag & P_STOPPED_SIG) != 0) {
12927beb6082SMateusz Guzik 			PROC_SLOCK(p);
12937beb6082SMateusz Guzik 			report = (p->p_suspcount == p->p_numthreads &&
12947beb6082SMateusz Guzik 			    ((p->p_flag & P_WAITED) == 0));
12958bf6ff22SMateusz Guzik 			PROC_SUNLOCK(p);
12967beb6082SMateusz Guzik 			if (report) {
12977beb6082SMateusz Guzik 				report_alive_proc(td, p, siginfo, status,
12987beb6082SMateusz Guzik 				    options, CLD_STOPPED);
1299b7e23e82SJohn Baldwin 				return (0);
1300df8bae1dSRodney W. Grimes 			}
13017beb6082SMateusz Guzik 		}
1302f13b5a0fSKonstantin Belousov 		if ((options & WCONTINUED) != 0 &&
1303f13b5a0fSKonstantin Belousov 		    (p->p_flag & P_CONTINUED) != 0) {
1304435da985SKonstantin Belousov 			report_alive_proc(td, p, siginfo, status, options,
1305435da985SKonstantin Belousov 			    CLD_CONTINUED);
1306dcdc6c36SKonstantin Belousov 			return (0);
13076ee093fbSMike Barcroft 		}
1308d7aadbf9SJohn Baldwin 		PROC_UNLOCK(p);
1309d7aadbf9SJohn Baldwin 	}
1310dcd43281SKonstantin Belousov 
1311dcd43281SKonstantin Belousov 	/*
1312dcd43281SKonstantin Belousov 	 * Look in the orphans list too, to allow the parent to
1313dcd43281SKonstantin Belousov 	 * collect it's child exit status even if child is being
1314dcd43281SKonstantin Belousov 	 * debugged.
1315dcd43281SKonstantin Belousov 	 *
1316dcd43281SKonstantin Belousov 	 * Debugger detaches from the parent upon successful
1317dcd43281SKonstantin Belousov 	 * switch-over from parent to child.  At this point due to
1318dcd43281SKonstantin Belousov 	 * re-parenting the parent loses the child to debugger and a
1319dcd43281SKonstantin Belousov 	 * wait4(2) call would report that it has no children to wait
1320dcd43281SKonstantin Belousov 	 * for.  By maintaining a list of orphans we allow the parent
1321dcd43281SKonstantin Belousov 	 * to successfully wait until the child becomes a zombie.
1322dcd43281SKonstantin Belousov 	 */
132357c74f5bSJohn Baldwin 	if (nfound == 0) {
1324dcd43281SKonstantin Belousov 		LIST_FOREACH(p, &q->p_orphans, p_orphan) {
132557c74f5bSJohn Baldwin 			ret = proc_to_reap(td, p, idtype, id, NULL, options,
132657c74f5bSJohn Baldwin 			    NULL, NULL, 1);
132757c74f5bSJohn Baldwin 			if (ret != 0) {
132857c74f5bSJohn Baldwin 				KASSERT(ret != -1, ("reaped an orphan (pid %d)",
132957c74f5bSJohn Baldwin 				    (int)td->td_retval[0]));
1330746b6e81SMateusz Guzik 				PROC_UNLOCK(p);
1331dcd43281SKonstantin Belousov 				nfound++;
133257c74f5bSJohn Baldwin 				break;
133357c74f5bSJohn Baldwin 			}
133457c74f5bSJohn Baldwin 		}
1335dcd43281SKonstantin Belousov 	}
1336d7aadbf9SJohn Baldwin 	if (nfound == 0) {
1337d7aadbf9SJohn Baldwin 		sx_xunlock(&proctree_lock);
1338d7aadbf9SJohn Baldwin 		return (ECHILD);
1339d7aadbf9SJohn Baldwin 	}
1340b7e23e82SJohn Baldwin 	if (options & WNOHANG) {
1341d7aadbf9SJohn Baldwin 		sx_xunlock(&proctree_lock);
1342d7aadbf9SJohn Baldwin 		td->td_retval[0] = 0;
1343d7aadbf9SJohn Baldwin 		return (0);
1344d7aadbf9SJohn Baldwin 	}
1345d7aadbf9SJohn Baldwin 	PROC_LOCK(q);
134695992d56SDavid Xu 	if (q->p_flag & P_STATCHILD) {
134795992d56SDavid Xu 		q->p_flag &= ~P_STATCHILD;
1348d7aadbf9SJohn Baldwin 		PROC_UNLOCK(q);
13496776bfebSMateusz Guzik 		goto loop_locked;
13506776bfebSMateusz Guzik 	}
13516776bfebSMateusz Guzik 	sx_xunlock(&proctree_lock);
13526776bfebSMateusz Guzik 	error = msleep(q, &q->p_mtx, PWAIT | PCATCH | PDROP, "wait", 0);
13534ae89b95SJohn Baldwin 	if (error)
1354d7aadbf9SJohn Baldwin 		return (error);
1355d7aadbf9SJohn Baldwin 	goto loop;
1356d7aadbf9SJohn Baldwin }
1357df8bae1dSRodney W. Grimes 
1358*799d92abSMariusz Zaborski void
1359*799d92abSMariusz Zaborski proc_add_orphan(struct proc *child, struct proc *parent)
1360*799d92abSMariusz Zaborski {
1361*799d92abSMariusz Zaborski 
1362*799d92abSMariusz Zaborski 	sx_assert(&proctree_lock, SX_XLOCKED);
1363*799d92abSMariusz Zaborski 	KASSERT((child->p_flag & P_TRACED) != 0,
1364*799d92abSMariusz Zaborski 	    ("proc_add_orphan: not traced"));
1365*799d92abSMariusz Zaborski 
1366*799d92abSMariusz Zaborski 	if (LIST_EMPTY(&parent->p_orphans)) {
1367*799d92abSMariusz Zaborski 		child->p_treeflag |= P_TREE_FIRST_ORPHAN;
1368*799d92abSMariusz Zaborski 		LIST_INSERT_HEAD(&parent->p_orphans, child, p_orphan);
1369*799d92abSMariusz Zaborski 	} else {
1370*799d92abSMariusz Zaborski 		LIST_INSERT_AFTER(LIST_FIRST(&parent->p_orphans),
1371*799d92abSMariusz Zaborski 		    child, p_orphan);
1372*799d92abSMariusz Zaborski 	}
1373*799d92abSMariusz Zaborski 	child->p_treeflag |= P_TREE_ORPHANED;
1374*799d92abSMariusz Zaborski }
1375*799d92abSMariusz Zaborski 
1376df8bae1dSRodney W. Grimes /*
137798f03f90SJake Burkholder  * Make process 'parent' the new parent of process 'child'.
137898f03f90SJake Burkholder  * Must be called with an exclusive hold of proctree lock.
1379df8bae1dSRodney W. Grimes  */
1380df8bae1dSRodney W. Grimes void
13812c054ce9SMateusz Guzik proc_reparent(struct proc *child, struct proc *parent, bool set_oppid)
1382df8bae1dSRodney W. Grimes {
1383df8bae1dSRodney W. Grimes 
13844e5e677bSJohn Baldwin 	sx_assert(&proctree_lock, SX_XLOCKED);
1385c65437a3SJohn Baldwin 	PROC_LOCK_ASSERT(child, MA_OWNED);
1386df8bae1dSRodney W. Grimes 	if (child->p_pptr == parent)
1387df8bae1dSRodney W. Grimes 		return;
1388df8bae1dSRodney W. Grimes 
1389ff766807SDavid Xu 	PROC_LOCK(child->p_pptr);
1390ff766807SDavid Xu 	sigqueue_take(child->p_ksi);
1391ff766807SDavid Xu 	PROC_UNLOCK(child->p_pptr);
1392b75356e1SJeffrey Hsu 	LIST_REMOVE(child, p_sibling);
1393b75356e1SJeffrey Hsu 	LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
1394dcd43281SKonstantin Belousov 
13959db97ca0SMariusz Zaborski 	proc_clear_orphan(child);
1396*799d92abSMariusz Zaborski 	if ((child->p_flag & P_TRACED) != 0) {
1397*799d92abSMariusz Zaborski 		proc_add_orphan(child, child->p_pptr);
1398dcd43281SKonstantin Belousov 	}
1399dcd43281SKonstantin Belousov 
1400df8bae1dSRodney W. Grimes 	child->p_pptr = parent;
14012c054ce9SMateusz Guzik 	if (set_oppid)
14022c054ce9SMateusz Guzik 		child->p_oppid = parent->p_pid;
1403df8bae1dSRodney W. Grimes }
1404