xref: /freebsd/sys/kern/kern_exit.c (revision ee42d0a9657793cac6b18c55d2c1af4f26fbd857)
1996c772fSJohn Dyson /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
5df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
6df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
7df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
19df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
20df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
21df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
22df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
23df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
24df8bae1dSRodney W. Grimes  *    without specific prior written permission.
25df8bae1dSRodney W. Grimes  *
26df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  *	@(#)kern_exit.c	8.7 (Berkeley) 2/12/94
39c3aac50fSPeter Wemm  * $FreeBSD$
40df8bae1dSRodney W. Grimes  */
41df8bae1dSRodney W. Grimes 
425591b823SEivind Eklund #include "opt_compat.h"
43db6a20e2SGarrett Wollman #include "opt_ktrace.h"
44db6a20e2SGarrett Wollman 
45df8bae1dSRodney W. Grimes #include <sys/param.h>
46df8bae1dSRodney W. Grimes #include <sys/systm.h>
475fdb8324SBruce Evans #include <sys/sysproto.h>
481c5bb3eaSPeter Wemm #include <sys/kernel.h>
49a1c995b6SPoul-Henning Kamp #include <sys/malloc.h>
50f34fa851SJohn Baldwin #include <sys/lock.h>
5135e0e5b3SJohn Baldwin #include <sys/mutex.h>
52df8bae1dSRodney W. Grimes #include <sys/proc.h>
532a024a2bSSean Eric Fagan #include <sys/pioctl.h>
54df8bae1dSRodney W. Grimes #include <sys/tty.h>
55df8bae1dSRodney W. Grimes #include <sys/wait.h>
56df8bae1dSRodney W. Grimes #include <sys/vnode.h>
57eb30c1c0SPeter Wemm #include <sys/vmmeter.h>
58df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
59797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
601005a129SJohn Baldwin #include <sys/sx.h>
61df8bae1dSRodney W. Grimes #include <sys/ptrace.h>
627c409b8aSJeffrey Hsu #include <sys/acct.h>		/* for acct_process() function prototype */
63797f2d22SPoul-Henning Kamp #include <sys/filedesc.h>
64780dc5a8SPeter Wemm #include <sys/shm.h>
65780dc5a8SPeter Wemm #include <sys/sem.h>
6675c13541SPoul-Henning Kamp #include <sys/jail.h>
67780dc5a8SPeter Wemm 
68df8bae1dSRodney W. Grimes #include <vm/vm.h>
69efeaf95aSDavid Greenman #include <vm/vm_param.h>
70eb30c1c0SPeter Wemm #include <vm/vm_extern.h>
71efeaf95aSDavid Greenman #include <vm/pmap.h>
72efeaf95aSDavid Greenman #include <vm/vm_map.h>
732d8acc0fSJohn Dyson #include <vm/vm_zone.h>
74dc9c271aSJulian Elischer #include <sys/user.h>
75df8bae1dSRodney W. Grimes 
76ba198b1cSMark Newton /* Required to be non-static for SysVR4 emulator */
7769a6f20bSMark Newton MALLOC_DEFINE(M_ZOMBIE, "zombie", "zombie proc status");
7855166637SPoul-Henning Kamp 
7993efcae8SPoul-Henning Kamp static MALLOC_DEFINE(M_ATEXIT, "atexit", "atexit callback");
8093efcae8SPoul-Henning Kamp 
81b40ce416SJulian Elischer static int wait1 __P((struct thread *, struct wait_args *, int));
825fdb8324SBruce Evans 
83df8bae1dSRodney W. Grimes /*
84fed06968SJulian Elischer  * callout list for things to do at exit time
85fed06968SJulian Elischer  */
8693efcae8SPoul-Henning Kamp struct exitlist {
87fed06968SJulian Elischer 	exitlist_fn function;
88e3975643SJake Burkholder 	TAILQ_ENTRY(exitlist) next;
8993efcae8SPoul-Henning Kamp };
90fed06968SJulian Elischer 
91e3975643SJake Burkholder TAILQ_HEAD(exit_list_head, exitlist);
9293efcae8SPoul-Henning Kamp static struct exit_list_head exit_list = TAILQ_HEAD_INITIALIZER(exit_list);
93fed06968SJulian Elischer 
94fed06968SJulian Elischer /*
95df8bae1dSRodney W. Grimes  * exit --
96df8bae1dSRodney W. Grimes  *	Death of process.
97234216efSMatthew Dillon  *
98234216efSMatthew Dillon  * MPSAFE
99df8bae1dSRodney W. Grimes  */
100fc0b1dbfSBruce Evans void
101b40ce416SJulian Elischer sys_exit(td, uap)
102b40ce416SJulian Elischer 	struct thread *td;
103ac2b067bSPeter Wemm 	struct sys_exit_args /* {
1045fdb8324SBruce Evans 		int	rval;
1055fdb8324SBruce Evans 	} */ *uap;
106df8bae1dSRodney W. Grimes {
107b40ce416SJulian Elischer 
108234216efSMatthew Dillon 	mtx_lock(&Giant);
109b40ce416SJulian Elischer 	exit1(td, W_EXITCODE(uap->rval, 0));
110df8bae1dSRodney W. Grimes 	/* NOTREACHED */
111df8bae1dSRodney W. Grimes }
112df8bae1dSRodney W. Grimes 
113df8bae1dSRodney W. Grimes /*
114df8bae1dSRodney W. Grimes  * Exit: deallocate address space and other resources, change proc state
115df8bae1dSRodney W. Grimes  * to zombie, and unlink proc from allproc and parent's lists.  Save exit
116df8bae1dSRodney W. Grimes  * status and rusage for wait().  Check for child processes and orphan them.
117df8bae1dSRodney W. Grimes  */
118fc0b1dbfSBruce Evans void
119b40ce416SJulian Elischer exit1(td, rv)
120b40ce416SJulian Elischer 	register struct thread *td;
121df8bae1dSRodney W. Grimes 	int rv;
122df8bae1dSRodney W. Grimes {
123b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
124df8bae1dSRodney W. Grimes 	register struct proc *q, *nq;
125df8bae1dSRodney W. Grimes 	register struct vmspace *vm;
12679deba82SMatthew Dillon 	struct vnode *vtmp;
12793efcae8SPoul-Henning Kamp 	struct exitlist *ep;
128df8bae1dSRodney W. Grimes 
1290cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
1300cddd8f0SMatthew Dillon 
1315f7bd355SPoul-Henning Kamp 	if (p->p_pid == 1) {
1325f7bd355SPoul-Henning Kamp 		printf("init died (signal %d, exit %d)\n",
133df8bae1dSRodney W. Grimes 		    WTERMSIG(rv), WEXITSTATUS(rv));
1345f7bd355SPoul-Henning Kamp 		panic("Going nowhere without my init!");
1355f7bd355SPoul-Henning Kamp 	}
1362c1011f7SJohn Dyson 
137b40ce416SJulian Elischer /* XXXXKSE */
138b40ce416SJulian Elischer /* MUST abort all other threads before proceeding past this point */
139b40ce416SJulian Elischer 
1402c1011f7SJohn Dyson 	/* are we a task leader? */
141a914fb6bSJohn Baldwin 	PROC_LOCK(p);
1422c1011f7SJohn Dyson 	if(p == p->p_leader) {
1432c1011f7SJohn Dyson 		q = p->p_peers;
144776e0b36SJohn Baldwin 		while (q != NULL) {
145776e0b36SJohn Baldwin 			PROC_LOCK(q);
146776e0b36SJohn Baldwin 			psignal(q, SIGKILL);
147776e0b36SJohn Baldwin 			PROC_UNLOCK(q);
1482c1011f7SJohn Dyson 			q = q->p_peers;
1492c1011f7SJohn Dyson 		}
15079fc0bf4SMike Smith 		while (p->p_peers)
151a914fb6bSJohn Baldwin 			msleep((caddr_t)p, &p->p_mtx, PWAIT, "exit1", 0);
1522c1011f7SJohn Dyson 	}
153a914fb6bSJohn Baldwin 	PROC_UNLOCK(p);
1542c1011f7SJohn Dyson 
155df8bae1dSRodney W. Grimes #ifdef PGINPROF
156df8bae1dSRodney W. Grimes 	vmsizmon();
157df8bae1dSRodney W. Grimes #endif
1582a024a2bSSean Eric Fagan 	STOPEVENT(p, S_EXIT, rv);
15989361835SSean Eric Fagan 	wakeup(&p->p_stype);	/* Wakeup anyone in procfs' PIOCWAIT */
1602a024a2bSSean Eric Fagan 
161fed06968SJulian Elischer 	/*
162e9189611SPeter Wemm 	 * Check if any loadable modules need anything done at process exit.
163fed06968SJulian Elischer 	 * e.g. SYSV IPC stuff
164fed06968SJulian Elischer 	 * XXX what if one of these generates an error?
165fed06968SJulian Elischer 	 */
16693efcae8SPoul-Henning Kamp 	TAILQ_FOREACH(ep, &exit_list, next)
167fed06968SJulian Elischer 		(*ep->function)(p);
168fed06968SJulian Elischer 
169df8bae1dSRodney W. Grimes 	stopprofclock(p);
170a914fb6bSJohn Baldwin 
171df8bae1dSRodney W. Grimes 	MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage),
172df8bae1dSRodney W. Grimes 		M_ZOMBIE, M_WAITOK);
173df8bae1dSRodney W. Grimes 	/*
174df8bae1dSRodney W. Grimes 	 * If parent is waiting for us to exit or exec,
175df8bae1dSRodney W. Grimes 	 * P_PPWAIT is set; we will wakeup the parent below.
176df8bae1dSRodney W. Grimes 	 */
177a914fb6bSJohn Baldwin 	PROC_LOCK(p);
178df8bae1dSRodney W. Grimes 	p->p_flag &= ~(P_TRACED | P_PPWAIT);
179df8bae1dSRodney W. Grimes 	p->p_flag |= P_WEXIT;
1802c42a146SMarcel Moolenaar 	SIGEMPTYSET(p->p_siglist);
181a914fb6bSJohn Baldwin 	PROC_UNLOCK(p);
1824cf41af3SPoul-Henning Kamp 	if (timevalisset(&p->p_realtimer.it_value))
1834f559836SJake Burkholder 		callout_stop(&p->p_itcallout);
184df8bae1dSRodney W. Grimes 
185df8bae1dSRodney W. Grimes 	/*
186831d27a9SDon Lewis 	 * Reset any sigio structures pointing to us as a result of
187831d27a9SDon Lewis 	 * F_SETOWN with our pid.
188831d27a9SDon Lewis 	 */
189831d27a9SDon Lewis 	funsetownlst(&p->p_sigiolst);
190831d27a9SDon Lewis 
191831d27a9SDon Lewis 	/*
192df8bae1dSRodney W. Grimes 	 * Close open files and release open-file table.
193df8bae1dSRodney W. Grimes 	 * This may block!
194df8bae1dSRodney W. Grimes 	 */
19548810023SJulian Elischer 	fdfree(td); /* XXXKSE *//* may not be the one in proc */
196df8bae1dSRodney W. Grimes 
197a914fb6bSJohn Baldwin 	/*
198a914fb6bSJohn Baldwin 	 * Remove ourself from our leader's peer list and wake our leader.
199a914fb6bSJohn Baldwin 	 */
200776e0b36SJohn Baldwin 	PROC_LOCK(p->p_leader);
20179fc0bf4SMike Smith 	if(p->p_leader->p_peers) {
20279fc0bf4SMike Smith 		q = p->p_leader;
20379fc0bf4SMike Smith 		while(q->p_peers != p)
20479fc0bf4SMike Smith 			q = q->p_peers;
20579fc0bf4SMike Smith 		q->p_peers = p->p_peers;
20679fc0bf4SMike Smith 		wakeup((caddr_t)p->p_leader);
20779fc0bf4SMike Smith 	}
208776e0b36SJohn Baldwin 	PROC_UNLOCK(p->p_leader);
20979fc0bf4SMike Smith 
210df8bae1dSRodney W. Grimes 	/* The next two chunks should probably be moved to vmspace_exit. */
211df8bae1dSRodney W. Grimes 	vm = p->p_vmspace;
212df8bae1dSRodney W. Grimes 	/*
213df8bae1dSRodney W. Grimes 	 * Release user portion of address space.
214df8bae1dSRodney W. Grimes 	 * This releases references to vnodes,
215df8bae1dSRodney W. Grimes 	 * which could cause I/O if the file has been unlinked.
216df8bae1dSRodney W. Grimes 	 * Need to do this early enough that we can still sleep.
217df8bae1dSRodney W. Grimes 	 * Can't free the entire vmspace as the kernel stack
218df8bae1dSRodney W. Grimes 	 * may be mapped within that space also.
219df8bae1dSRodney W. Grimes 	 */
2209d3fbbb5SJohn Dyson 	if (vm->vm_refcnt == 1) {
221a2a1c95cSPeter Wemm 		if (vm->vm_shm)
222a2a1c95cSPeter Wemm 			shmexit(p);
223b1028ad1SLuoqi Chen 		pmap_remove_pages(vmspace_pmap(vm), VM_MIN_ADDRESS,
2249d3fbbb5SJohn Dyson 		    VM_MAXUSER_ADDRESS);
22567bf6868SJohn Dyson 		(void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS,
22667bf6868SJohn Dyson 		    VM_MAXUSER_ADDRESS);
2279d3fbbb5SJohn Dyson 	}
228df8bae1dSRodney W. Grimes 
229a914fb6bSJohn Baldwin 	PROC_LOCK(p);
230df8bae1dSRodney W. Grimes 	if (SESS_LEADER(p)) {
231df8bae1dSRodney W. Grimes 		register struct session *sp = p->p_session;
232df8bae1dSRodney W. Grimes 
233a914fb6bSJohn Baldwin 		PROC_UNLOCK(p);
234df8bae1dSRodney W. Grimes 		if (sp->s_ttyvp) {
235df8bae1dSRodney W. Grimes 			/*
236df8bae1dSRodney W. Grimes 			 * Controlling process.
237df8bae1dSRodney W. Grimes 			 * Signal foreground pgrp,
238df8bae1dSRodney W. Grimes 			 * drain controlling terminal
239df8bae1dSRodney W. Grimes 			 * and revoke access to controlling terminal.
240df8bae1dSRodney W. Grimes 			 */
241dd45d8adSJulian Elischer 			if (sp->s_ttyp && (sp->s_ttyp->t_session == sp)) {
242df8bae1dSRodney W. Grimes 				if (sp->s_ttyp->t_pgrp)
243df8bae1dSRodney W. Grimes 					pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
244df8bae1dSRodney W. Grimes 				(void) ttywait(sp->s_ttyp);
245df8bae1dSRodney W. Grimes 				/*
246df8bae1dSRodney W. Grimes 				 * The tty could have been revoked
247df8bae1dSRodney W. Grimes 				 * if we blocked.
248df8bae1dSRodney W. Grimes 				 */
249df8bae1dSRodney W. Grimes 				if (sp->s_ttyvp)
250996c772fSJohn Dyson 					VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
251df8bae1dSRodney W. Grimes 			}
252df8bae1dSRodney W. Grimes 			if (sp->s_ttyvp)
253df8bae1dSRodney W. Grimes 				vrele(sp->s_ttyvp);
254df8bae1dSRodney W. Grimes 			sp->s_ttyvp = NULL;
255df8bae1dSRodney W. Grimes 			/*
256df8bae1dSRodney W. Grimes 			 * s_ttyp is not zero'd; we use this to indicate
257df8bae1dSRodney W. Grimes 			 * that the session once had a controlling terminal.
258df8bae1dSRodney W. Grimes 			 * (for logging and informational purposes)
259df8bae1dSRodney W. Grimes 			 */
260df8bae1dSRodney W. Grimes 		}
261df8bae1dSRodney W. Grimes 		sp->s_leader = NULL;
262a914fb6bSJohn Baldwin 	} else
263a914fb6bSJohn Baldwin 		PROC_UNLOCK(p);
264df8bae1dSRodney W. Grimes 	fixjobc(p, p->p_pgrp, 0);
265b40ce416SJulian Elischer 	(void)acct_process(td);
266df8bae1dSRodney W. Grimes #ifdef KTRACE
267df8bae1dSRodney W. Grimes 	/*
268df8bae1dSRodney W. Grimes 	 * release trace file
269df8bae1dSRodney W. Grimes 	 */
270df8bae1dSRodney W. Grimes 	p->p_traceflag = 0;	/* don't trace the vrele() */
27179deba82SMatthew Dillon 	if ((vtmp = p->p_tracep) != NULL) {
27279deba82SMatthew Dillon 		p->p_tracep = NULL;
27379deba82SMatthew Dillon 		vrele(vtmp);
27479deba82SMatthew Dillon 	}
275df8bae1dSRodney W. Grimes #endif
276df8bae1dSRodney W. Grimes 	/*
277ee42d0a9SDavid Malone 	 * Release reference to text vnode
278ee42d0a9SDavid Malone 	 */
279ee42d0a9SDavid Malone 	if ((vtmp = p->p_textvp) != NULL) {
280ee42d0a9SDavid Malone 		p->p_textvp = NULL;
281ee42d0a9SDavid Malone 		vrele(vtmp);
282ee42d0a9SDavid Malone 	}
283ee42d0a9SDavid Malone 
284ee42d0a9SDavid Malone 	/*
285df8bae1dSRodney W. Grimes 	 * Remove proc from allproc queue and pidhash chain.
286df8bae1dSRodney W. Grimes 	 * Place onto zombproc.  Unlink from parent's child list.
287df8bae1dSRodney W. Grimes 	 */
2881005a129SJohn Baldwin 	sx_xlock(&allproc_lock);
289b75356e1SJeffrey Hsu 	LIST_REMOVE(p, p_list);
290b75356e1SJeffrey Hsu 	LIST_INSERT_HEAD(&zombproc, p, p_list);
291b75356e1SJeffrey Hsu 	LIST_REMOVE(p, p_hash);
2921005a129SJohn Baldwin 	sx_xunlock(&allproc_lock);
293df8bae1dSRodney W. Grimes 
2941005a129SJohn Baldwin 	sx_xlock(&proctree_lock);
2952e3c8fcbSPoul-Henning Kamp 	q = LIST_FIRST(&p->p_children);
296a914fb6bSJohn Baldwin 	if (q != NULL)		/* only need this if any child is S_ZOMB */
297df8bae1dSRodney W. Grimes 		wakeup((caddr_t) initproc);
298a914fb6bSJohn Baldwin 	for (; q != NULL; q = nq) {
2992e3c8fcbSPoul-Henning Kamp 		nq = LIST_NEXT(q, p_sibling);
300a914fb6bSJohn Baldwin 		PROC_LOCK(q);
301c65437a3SJohn Baldwin 		proc_reparent(q, initproc);
3024ac9ae70SJulian Elischer 		q->p_sigparent = SIGCHLD;
303df8bae1dSRodney W. Grimes 		/*
304df8bae1dSRodney W. Grimes 		 * Traced processes are killed
305df8bae1dSRodney W. Grimes 		 * since their existence means someone is screwing up.
306df8bae1dSRodney W. Grimes 		 */
307df8bae1dSRodney W. Grimes 		if (q->p_flag & P_TRACED) {
308df8bae1dSRodney W. Grimes 			q->p_flag &= ~P_TRACED;
309df8bae1dSRodney W. Grimes 			psignal(q, SIGKILL);
310c65437a3SJohn Baldwin 		}
311a914fb6bSJohn Baldwin 		PROC_UNLOCK(q);
312df8bae1dSRodney W. Grimes 	}
313df8bae1dSRodney W. Grimes 
314df8bae1dSRodney W. Grimes 	/*
315df8bae1dSRodney W. Grimes 	 * Save exit status and final rusage info, adding in child rusage
316df8bae1dSRodney W. Grimes 	 * info and self times.
317df8bae1dSRodney W. Grimes 	 */
318df8bae1dSRodney W. Grimes 	p->p_xstat = rv;
319df8bae1dSRodney W. Grimes 	*p->p_ru = p->p_stats->p_ru;
3209ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
321df8bae1dSRodney W. Grimes 	calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
3229ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
323df8bae1dSRodney W. Grimes 	ruadd(p->p_ru, &p->p_stats->p_cru);
324df8bae1dSRodney W. Grimes 
325df8bae1dSRodney W. Grimes 	/*
32656ce1a8dSBruce Evans 	 * Pretend that an mi_switch() to the next process occurs now.  We
32756ce1a8dSBruce Evans 	 * must set `switchtime' directly since we will call cpu_switch()
32856ce1a8dSBruce Evans 	 * directly.  Set it now so that the rest of the exit time gets
32956ce1a8dSBruce Evans 	 * counted somewhere if possible.
33056ce1a8dSBruce Evans 	 */
3310d139b37STor Egge 	mtx_lock_spin(&sched_lock);
3329d0ddf18STor Egge 	microuptime(PCPU_PTR(switchtime));
333ef73ae4bSJake Burkholder 	PCPU_SET(switchticks, ticks);
3349d0ddf18STor Egge 	mtx_unlock_spin(&sched_lock);
33556ce1a8dSBruce Evans 
33656ce1a8dSBruce Evans 	/*
337a274d19bSBrian Feldman 	 * notify interested parties of our demise.
338cb679c38SJonathan Lemon 	 */
339a914fb6bSJohn Baldwin 	PROC_LOCK(p);
340a274d19bSBrian Feldman 	KNOTE(&p->p_klist, NOTE_EXIT);
341cb679c38SJonathan Lemon 
342cb679c38SJonathan Lemon 	/*
343645682fdSLuoqi Chen 	 * Notify parent that we're gone.  If parent has the PS_NOCLDWAIT
344aa7a4daeSPeter Wemm 	 * flag set, notify process 1 instead (and hope it will handle
345aa7a4daeSPeter Wemm 	 * this situation).
346df8bae1dSRodney W. Grimes 	 */
347aa7a4daeSPeter Wemm 	if (p->p_pptr->p_procsig->ps_flag & PS_NOCLDWAIT) {
348245f17d4SJoerg Wunsch 		struct proc *pp = p->p_pptr;
349245f17d4SJoerg Wunsch 		proc_reparent(p, initproc);
350245f17d4SJoerg Wunsch 		/*
351245f17d4SJoerg Wunsch 		 * If this was the last child of our parent, notify
352245f17d4SJoerg Wunsch 		 * parent, so in case he was wait(2)ing, he will
353245f17d4SJoerg Wunsch 		 * continue.
354245f17d4SJoerg Wunsch 		 */
355245f17d4SJoerg Wunsch 		if (LIST_EMPTY(&pp->p_children))
356245f17d4SJoerg Wunsch 			wakeup((caddr_t)pp);
357245f17d4SJoerg Wunsch 	}
358245f17d4SJoerg Wunsch 
359c65437a3SJohn Baldwin 	PROC_LOCK(p->p_pptr);
360c65437a3SJohn Baldwin 	if (p->p_sigparent && p->p_pptr != initproc)
3616626c604SJulian Elischer 	        psignal(p->p_pptr, p->p_sigparent);
362c65437a3SJohn Baldwin 	else
3636626c604SJulian Elischer 	        psignal(p->p_pptr, SIGCHLD);
364c65437a3SJohn Baldwin 	PROC_UNLOCK(p->p_pptr);
365776e0b36SJohn Baldwin 
366776e0b36SJohn Baldwin 	/*
367776e0b36SJohn Baldwin 	 * If this is a kthread, then wakeup anyone waiting for it to exit.
368776e0b36SJohn Baldwin 	 */
369776e0b36SJohn Baldwin 	if (p->p_flag & P_KTHREAD)
370776e0b36SJohn Baldwin 		wakeup((caddr_t)p);
371c65437a3SJohn Baldwin 	PROC_UNLOCK(p);
3721005a129SJohn Baldwin 	sx_xunlock(&proctree_lock);
373c65437a3SJohn Baldwin 
374df8bae1dSRodney W. Grimes 	/*
375df8bae1dSRodney W. Grimes 	 * Clear curproc after we've done all operations
376df8bae1dSRodney W. Grimes 	 * that could block, and before tearing down the rest
377df8bae1dSRodney W. Grimes 	 * of the process state that might be used from clock, etc.
378df8bae1dSRodney W. Grimes 	 * Also, can't clear curproc while we're still runnable,
379df8bae1dSRodney W. Grimes 	 * as we're not on a run queue (we are current, just not
380df8bae1dSRodney W. Grimes 	 * a proper proc any longer!).
381df8bae1dSRodney W. Grimes 	 *
382df8bae1dSRodney W. Grimes 	 * Other substructures are freed from wait().
383df8bae1dSRodney W. Grimes 	 */
384a914fb6bSJohn Baldwin 	mtx_assert(&Giant, MA_OWNED);
385be6a1d14SDavid Greenman 	if (--p->p_limit->p_refcnt == 0) {
386df8bae1dSRodney W. Grimes 		FREE(p->p_limit, M_SUBPROC);
387be6a1d14SDavid Greenman 		p->p_limit = NULL;
388be6a1d14SDavid Greenman 	}
389df8bae1dSRodney W. Grimes 
390df8bae1dSRodney W. Grimes 	/*
3918e2e767bSJohn Baldwin 	 * Release this thread's reference to the ucred.  The actual proc
3928e2e767bSJohn Baldwin 	 * reference will stay around until the proc is harvested by
3938e2e767bSJohn Baldwin 	 * wait().  At this point the ucred is immutable (no other threads
3948e2e767bSJohn Baldwin 	 * from this proc are around that can change it) so we leave the
3958e2e767bSJohn Baldwin 	 * per-thread ucred pointer intact in case it is needed although
3968e2e767bSJohn Baldwin 	 * in theory nothing should be using it at this point.
3978e2e767bSJohn Baldwin 	 */
3988e2e767bSJohn Baldwin 	crfree(td->td_ucred);
3998e2e767bSJohn Baldwin 
4008e2e767bSJohn Baldwin 	/*
401df8bae1dSRodney W. Grimes 	 * Finally, call machine-dependent code to release the remaining
402df8bae1dSRodney W. Grimes 	 * resources including address space, the kernel stack and pcb.
403eb30c1c0SPeter Wemm 	 * The address space is released by "vmspace_free(p->p_vmspace)"
404eb30c1c0SPeter Wemm 	 * in vm_waitproc();
405df8bae1dSRodney W. Grimes 	 */
406b40ce416SJulian Elischer 	cpu_exit(td);
407eb30c1c0SPeter Wemm 
408eb30c1c0SPeter Wemm 	PROC_LOCK(p);
409eb30c1c0SPeter Wemm 	mtx_lock_spin(&sched_lock);
410eb30c1c0SPeter Wemm 	while (mtx_owned(&Giant))
411c86b6ff5SJohn Baldwin 		mtx_unlock(&Giant);
412eb30c1c0SPeter Wemm 
413eb30c1c0SPeter Wemm 	/*
414eb30c1c0SPeter Wemm 	 * We have to wait until after releasing all locks before
415eb30c1c0SPeter Wemm 	 * changing p_stat.  If we block on a mutex then we will be
416eb30c1c0SPeter Wemm 	 * back at SRUN when we resume and our parent will never
417eb30c1c0SPeter Wemm 	 * harvest us.
418eb30c1c0SPeter Wemm 	 */
419eb30c1c0SPeter Wemm 	p->p_stat = SZOMB;
420eb30c1c0SPeter Wemm 
421eb30c1c0SPeter Wemm 	wakeup(p->p_pptr);
422c86b6ff5SJohn Baldwin 	PROC_UNLOCK(p);
423eb30c1c0SPeter Wemm 
424eb30c1c0SPeter Wemm 	cnt.v_swtch++;
425eb30c1c0SPeter Wemm 	cpu_throw();
426eb30c1c0SPeter Wemm 	panic("exit1");
427df8bae1dSRodney W. Grimes }
428df8bae1dSRodney W. Grimes 
429b2f9e8b1SBruce Evans #ifdef COMPAT_43
430234216efSMatthew Dillon /*
431234216efSMatthew Dillon  * MPSAFE, the dirty work is handled by wait1().
432234216efSMatthew Dillon  */
43326f9a767SRodney W. Grimes int
434b40ce416SJulian Elischer owait(td, uap)
435b40ce416SJulian Elischer 	struct thread *td;
4365fdb8324SBruce Evans 	register struct owait_args /* {
4375fdb8324SBruce Evans 		int     dummy;
4385fdb8324SBruce Evans 	} */ *uap;
439df8bae1dSRodney W. Grimes {
44093c9414eSSteven Wallace 	struct wait_args w;
441df8bae1dSRodney W. Grimes 
44293c9414eSSteven Wallace 	w.options = 0;
44393c9414eSSteven Wallace 	w.rusage = NULL;
44493c9414eSSteven Wallace 	w.pid = WAIT_ANY;
44593c9414eSSteven Wallace 	w.status = NULL;
446b40ce416SJulian Elischer 	return (wait1(td, &w, 1));
447df8bae1dSRodney W. Grimes }
448b2f9e8b1SBruce Evans #endif /* COMPAT_43 */
449df8bae1dSRodney W. Grimes 
450234216efSMatthew Dillon /*
451234216efSMatthew Dillon  * MPSAFE, the dirty work is handled by wait1().
452234216efSMatthew Dillon  */
45326f9a767SRodney W. Grimes int
454b40ce416SJulian Elischer wait4(td, uap)
455b40ce416SJulian Elischer 	struct thread *td;
456df8bae1dSRodney W. Grimes 	struct wait_args *uap;
457df8bae1dSRodney W. Grimes {
458b40ce416SJulian Elischer 
459b40ce416SJulian Elischer 	return (wait1(td, uap, 0));
460df8bae1dSRodney W. Grimes }
461df8bae1dSRodney W. Grimes 
462234216efSMatthew Dillon /*
463234216efSMatthew Dillon  * MPSAFE
464234216efSMatthew Dillon  */
46593c9414eSSteven Wallace static int
466b40ce416SJulian Elischer wait1(td, uap, compat)
467b40ce416SJulian Elischer 	register struct thread *td;
4685fdb8324SBruce Evans 	register struct wait_args /* {
4695fdb8324SBruce Evans 		int pid;
4705fdb8324SBruce Evans 		int *status;
4715fdb8324SBruce Evans 		int options;
4725fdb8324SBruce Evans 		struct rusage *rusage;
4735fdb8324SBruce Evans 	} */ *uap;
47493c9414eSSteven Wallace 	int compat;
475df8bae1dSRodney W. Grimes {
476df8bae1dSRodney W. Grimes 	register int nfound;
477b40ce416SJulian Elischer 	register struct proc *q, *p, *t;
478f23c6d68SSteven Wallace 	int status, error;
479df8bae1dSRodney W. Grimes 
480234216efSMatthew Dillon 	mtx_lock(&Giant);
481b40ce416SJulian Elischer 	q = td->td_proc;
482df8bae1dSRodney W. Grimes 	if (uap->pid == 0)
483df8bae1dSRodney W. Grimes 		uap->pid = -q->p_pgid;
484234216efSMatthew Dillon 	if (uap->options &~ (WUNTRACED|WNOHANG|WLINUXCLONE)) {
485234216efSMatthew Dillon 		error = EINVAL;
486234216efSMatthew Dillon 		goto done2;
487234216efSMatthew Dillon 	}
488df8bae1dSRodney W. Grimes loop:
489df8bae1dSRodney W. Grimes 	nfound = 0;
4901005a129SJohn Baldwin 	sx_slock(&proctree_lock);
4912e3c8fcbSPoul-Henning Kamp 	LIST_FOREACH(p, &q->p_children, p_sibling) {
492df8bae1dSRodney W. Grimes 		if (uap->pid != WAIT_ANY &&
493df8bae1dSRodney W. Grimes 		    p->p_pid != uap->pid && p->p_pgid != -uap->pid)
494df8bae1dSRodney W. Grimes 			continue;
4954ac9ae70SJulian Elischer 
4961156bc4dSJake Burkholder 		/*
4971156bc4dSJake Burkholder 		 * This special case handles a kthread spawned by linux_clone
4981156bc4dSJake Burkholder 		 * (see linux_misc.c).  The linux_wait4 and linux_waitpid
4991156bc4dSJake Burkholder 		 * functions need to be able to distinguish between waiting
5001156bc4dSJake Burkholder 		 * on a process and waiting on a thread.  It is a thread if
5011156bc4dSJake Burkholder 		 * p_sigparent is not SIGCHLD, and the WLINUXCLONE option
5021156bc4dSJake Burkholder 		 * signifies we want to wait for threads and not processes.
5034ac9ae70SJulian Elischer 		 */
504a914fb6bSJohn Baldwin 		PROC_LOCK(p);
5051156bc4dSJake Burkholder 		if ((p->p_sigparent != SIGCHLD) ^
506a914fb6bSJohn Baldwin 		    ((uap->options & WLINUXCLONE) != 0)) {
507a914fb6bSJohn Baldwin 			PROC_UNLOCK(p);
5084ac9ae70SJulian Elischer 			continue;
509a914fb6bSJohn Baldwin 		}
5104ac9ae70SJulian Elischer 
511df8bae1dSRodney W. Grimes 		nfound++;
5129ed346baSBosko Milekic 		mtx_lock_spin(&sched_lock);
513df8bae1dSRodney W. Grimes 		if (p->p_stat == SZOMB) {
514b40ce416SJulian Elischer 			/*
515b40ce416SJulian Elischer 			 * charge childs scheduling cpu usage to parent
516b40ce416SJulian Elischer 			 * XXXKSE assume only one thread & kse & ksegrp
517b40ce416SJulian Elischer 			 * keep estcpu in each ksegrp
518b40ce416SJulian Elischer 			 * so charge it to the ksegrp that did the wait
519b40ce416SJulian Elischer 			 * since process estcpu is sum of all ksegrps,
520b40ce416SJulian Elischer 			 * this is strictly as expected.
521b40ce416SJulian Elischer 			 * Assume that the child process aggregated all
522b40ce416SJulian Elischer 			 * tke estcpu into the 'build-in' ksegrp.
523b40ce416SJulian Elischer 			 * XXXKSE
524b40ce416SJulian Elischer 			 */
525b40ce416SJulian Elischer 			if (curthread->td_proc->p_pid != 1) {
526b40ce416SJulian Elischer 				curthread->td_ksegrp->kg_estcpu =
527b40ce416SJulian Elischer 				    ESTCPULIM(curthread->td_ksegrp->kg_estcpu +
528b40ce416SJulian Elischer 				    p->p_ksegrp.kg_estcpu);
529d5c4431eSDavid Greenman 			}
5300d2afceeSDavid Greenman 
531c65437a3SJohn Baldwin 			mtx_unlock_spin(&sched_lock);
532c65437a3SJohn Baldwin 			PROC_UNLOCK(p);
5331005a129SJohn Baldwin 			sx_sunlock(&proctree_lock);
534c65437a3SJohn Baldwin 
535b40ce416SJulian Elischer 			td->td_retval[0] = p->p_pid;
536b2f9e8b1SBruce Evans #ifdef COMPAT_43
53793c9414eSSteven Wallace 			if (compat)
538b40ce416SJulian Elischer 				td->td_retval[1] = p->p_xstat;
539df8bae1dSRodney W. Grimes 			else
540df8bae1dSRodney W. Grimes #endif
541df8bae1dSRodney W. Grimes 			if (uap->status) {
542df8bae1dSRodney W. Grimes 				status = p->p_xstat;	/* convert to int */
543bb56ec4aSPoul-Henning Kamp 				if ((error = copyout((caddr_t)&status,
544234216efSMatthew Dillon 				    (caddr_t)uap->status, sizeof(status)))) {
545234216efSMatthew Dillon 					goto done2;
546234216efSMatthew Dillon 				}
547df8bae1dSRodney W. Grimes 			}
548df8bae1dSRodney W. Grimes 			if (uap->rusage && (error = copyout((caddr_t)p->p_ru,
549234216efSMatthew Dillon 			    (caddr_t)uap->rusage, sizeof (struct rusage)))) {
550234216efSMatthew Dillon 				goto done2;
551234216efSMatthew Dillon 			}
552df8bae1dSRodney W. Grimes 			/*
553df8bae1dSRodney W. Grimes 			 * If we got the child via a ptrace 'attach',
554df8bae1dSRodney W. Grimes 			 * we need to give it back to the old parent.
555df8bae1dSRodney W. Grimes 			 */
5561005a129SJohn Baldwin 			sx_xlock(&proctree_lock);
557c65437a3SJohn Baldwin 			if (p->p_oppid) {
55898f03f90SJake Burkholder 				if ((t = pfind(p->p_oppid)) != NULL) {
559c65437a3SJohn Baldwin 					PROC_LOCK(p);
560df8bae1dSRodney W. Grimes 					p->p_oppid = 0;
561df8bae1dSRodney W. Grimes 					proc_reparent(p, t);
562c65437a3SJohn Baldwin 					PROC_UNLOCK(p);
563df8bae1dSRodney W. Grimes 					psignal(t, SIGCHLD);
56433a9ed9dSJohn Baldwin 					wakeup((caddr_t)t);
565c65437a3SJohn Baldwin 					PROC_UNLOCK(t);
5661005a129SJohn Baldwin 					sx_xunlock(&proctree_lock);
567234216efSMatthew Dillon 					error = 0;
568234216efSMatthew Dillon 					goto done2;
569df8bae1dSRodney W. Grimes 				}
57098f03f90SJake Burkholder 			}
5711005a129SJohn Baldwin 			sx_xunlock(&proctree_lock);
572c65437a3SJohn Baldwin 			PROC_LOCK(p);
573df8bae1dSRodney W. Grimes 			p->p_xstat = 0;
574c65437a3SJohn Baldwin 			PROC_UNLOCK(p);
575df8bae1dSRodney W. Grimes 			ruadd(&q->p_stats->p_cru, p->p_ru);
576df8bae1dSRodney W. Grimes 			FREE(p->p_ru, M_ZOMBIE);
577be6a1d14SDavid Greenman 			p->p_ru = NULL;
578df8bae1dSRodney W. Grimes 
579df8bae1dSRodney W. Grimes 			/*
580df8bae1dSRodney W. Grimes 			 * Decrement the count of procs running with this uid.
581df8bae1dSRodney W. Grimes 			 */
582b1fc0ec1SRobert Watson 			(void)chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0);
583df8bae1dSRodney W. Grimes 
584df8bae1dSRodney W. Grimes 			/*
585ebdc3f1dSSeigo Tanimura 			 * Finally finished with old proc entry.
586ebdc3f1dSSeigo Tanimura 			 * Unlink it from its process group and free it.
587ebdc3f1dSSeigo Tanimura 			 */
588ebdc3f1dSSeigo Tanimura 			leavepgrp(p);
589ebdc3f1dSSeigo Tanimura 
590ebdc3f1dSSeigo Tanimura 			sx_xlock(&allproc_lock);
591ebdc3f1dSSeigo Tanimura 			LIST_REMOVE(p, p_list);	/* off zombproc */
592ebdc3f1dSSeigo Tanimura 			sx_xunlock(&allproc_lock);
593ebdc3f1dSSeigo Tanimura 
594ebdc3f1dSSeigo Tanimura 			sx_xlock(&proctree_lock);
595ebdc3f1dSSeigo Tanimura 			LIST_REMOVE(p, p_sibling);
596ebdc3f1dSSeigo Tanimura 			sx_xunlock(&proctree_lock);
597ebdc3f1dSSeigo Tanimura 
598ebdc3f1dSSeigo Tanimura 			/*
599df8bae1dSRodney W. Grimes 			 * Free up credentials.
600df8bae1dSRodney W. Grimes 			 */
601da654d90SPoul-Henning Kamp 			crfree(p->p_ucred);
602b1fc0ec1SRobert Watson 			p->p_ucred = NULL;
603df8bae1dSRodney W. Grimes 
604df8bae1dSRodney W. Grimes 			/*
605b9df5231SPoul-Henning Kamp 			 * Remove unused arguments
606b9df5231SPoul-Henning Kamp 			 */
607b9df5231SPoul-Henning Kamp 			if (p->p_args && --p->p_args->ar_ref == 0)
608b9df5231SPoul-Henning Kamp 				FREE(p->p_args, M_PARGS);
609b9df5231SPoul-Henning Kamp 
6106626c604SJulian Elischer 			if (--p->p_procsig->ps_refcnt == 0) {
611b40ce416SJulian Elischer 				if (p->p_sigacts != &p->p_uarea->u_sigacts)
612dc9c271aSJulian Elischer 					FREE(p->p_sigacts, M_SUBPROC);
613dc9c271aSJulian Elischer 			        FREE(p->p_procsig, M_SUBPROC);
6146626c604SJulian Elischer 				p->p_procsig = NULL;
6156626c604SJulian Elischer 			}
61688c5ea45SJulian Elischer 
617df8bae1dSRodney W. Grimes 			/*
618eb30c1c0SPeter Wemm 			 * Give vm and machine-dependent layer a chance
619df8bae1dSRodney W. Grimes 			 * to free anything that cpu_exit couldn't
620df8bae1dSRodney W. Grimes 			 * release while still running in process context.
621df8bae1dSRodney W. Grimes 			 */
622eb30c1c0SPeter Wemm 			vm_waitproc(p);
6234971f62aSJohn Baldwin 			mtx_destroy(&p->p_mtx);
6242d8acc0fSJohn Dyson 			zfree(proc_zone, p);
625df8bae1dSRodney W. Grimes 			nprocs--;
626234216efSMatthew Dillon 			error = 0;
627234216efSMatthew Dillon 			goto done2;
628df8bae1dSRodney W. Grimes 		}
629df8bae1dSRodney W. Grimes 		if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
630df8bae1dSRodney W. Grimes 		    (p->p_flag & P_TRACED || uap->options & WUNTRACED)) {
6319ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
632df8bae1dSRodney W. Grimes 			p->p_flag |= P_WAITED;
633a914fb6bSJohn Baldwin 			PROC_UNLOCK(p);
6341005a129SJohn Baldwin 			sx_sunlock(&proctree_lock);
635b40ce416SJulian Elischer 			td->td_retval[0] = p->p_pid;
636b2f9e8b1SBruce Evans #ifdef COMPAT_43
63793c9414eSSteven Wallace 			if (compat) {
638b40ce416SJulian Elischer 				td->td_retval[1] = W_STOPCODE(p->p_xstat);
639df8bae1dSRodney W. Grimes 				error = 0;
640df8bae1dSRodney W. Grimes 			} else
641df8bae1dSRodney W. Grimes #endif
642df8bae1dSRodney W. Grimes 			if (uap->status) {
643df8bae1dSRodney W. Grimes 				status = W_STOPCODE(p->p_xstat);
644df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&status,
645df8bae1dSRodney W. Grimes 					(caddr_t)uap->status, sizeof(status));
646df8bae1dSRodney W. Grimes 			} else
647df8bae1dSRodney W. Grimes 				error = 0;
648234216efSMatthew Dillon 			goto done2;
649df8bae1dSRodney W. Grimes 		}
6509ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
651a914fb6bSJohn Baldwin 		PROC_UNLOCK(p);
652df8bae1dSRodney W. Grimes 	}
6531005a129SJohn Baldwin 	sx_sunlock(&proctree_lock);
654234216efSMatthew Dillon 	if (nfound == 0) {
655234216efSMatthew Dillon 		error = ECHILD;
656234216efSMatthew Dillon 		goto done2;
657234216efSMatthew Dillon 	}
658df8bae1dSRodney W. Grimes 	if (uap->options & WNOHANG) {
659b40ce416SJulian Elischer 		td->td_retval[0] = 0;
660234216efSMatthew Dillon 		error = 0;
661234216efSMatthew Dillon 		goto done2;
662df8bae1dSRodney W. Grimes 	}
663234216efSMatthew Dillon 	if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)) != 0)
664234216efSMatthew Dillon 		goto done2;
665df8bae1dSRodney W. Grimes 	goto loop;
666234216efSMatthew Dillon done2:
667234216efSMatthew Dillon 	mtx_unlock(&Giant);
668234216efSMatthew Dillon 	return(error);
669df8bae1dSRodney W. Grimes }
670df8bae1dSRodney W. Grimes 
671df8bae1dSRodney W. Grimes /*
67298f03f90SJake Burkholder  * Make process 'parent' the new parent of process 'child'.
67398f03f90SJake Burkholder  * Must be called with an exclusive hold of proctree lock.
674df8bae1dSRodney W. Grimes  */
675df8bae1dSRodney W. Grimes void
676df8bae1dSRodney W. Grimes proc_reparent(child, parent)
677df8bae1dSRodney W. Grimes 	register struct proc *child;
678df8bae1dSRodney W. Grimes 	register struct proc *parent;
679df8bae1dSRodney W. Grimes {
680df8bae1dSRodney W. Grimes 
6814e5e677bSJohn Baldwin 	sx_assert(&proctree_lock, SX_XLOCKED);
682c65437a3SJohn Baldwin 	PROC_LOCK_ASSERT(child, MA_OWNED);
683df8bae1dSRodney W. Grimes 	if (child->p_pptr == parent)
684df8bae1dSRodney W. Grimes 		return;
685df8bae1dSRodney W. Grimes 
686b75356e1SJeffrey Hsu 	LIST_REMOVE(child, p_sibling);
687b75356e1SJeffrey Hsu 	LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
688df8bae1dSRodney W. Grimes 	child->p_pptr = parent;
689df8bae1dSRodney W. Grimes }
690fed06968SJulian Elischer 
691e0d898b4SJulian Elischer /*
692e0d898b4SJulian Elischer  * The next two functions are to handle adding/deleting items on the
693fed06968SJulian Elischer  * exit callout list
694e0d898b4SJulian Elischer  *
695e0d898b4SJulian Elischer  * at_exit():
696e0d898b4SJulian Elischer  * Take the arguments given and put them onto the exit callout list,
697fed06968SJulian Elischer  * However first make sure that it's not already there.
698fed06968SJulian Elischer  * returns 0 on success.
699fed06968SJulian Elischer  */
70093efcae8SPoul-Henning Kamp 
701fed06968SJulian Elischer int
702eb776aeaSBruce Evans at_exit(function)
703eb776aeaSBruce Evans 	exitlist_fn function;
704fed06968SJulian Elischer {
70593efcae8SPoul-Henning Kamp 	struct exitlist *ep;
706e0d898b4SJulian Elischer 
70793efcae8SPoul-Henning Kamp #ifdef INVARIANTS
708e0d898b4SJulian Elischer 	/* Be noisy if the programmer has lost track of things */
709e0d898b4SJulian Elischer 	if (rm_at_exit(function))
71093efcae8SPoul-Henning Kamp 		printf("WARNING: exit callout entry (%p) already present\n",
71193efcae8SPoul-Henning Kamp 		    function);
71293efcae8SPoul-Henning Kamp #endif
71393efcae8SPoul-Henning Kamp 	ep = malloc(sizeof(*ep), M_ATEXIT, M_NOWAIT);
714e0d898b4SJulian Elischer 	if (ep == NULL)
715e0d898b4SJulian Elischer 		return (ENOMEM);
716fed06968SJulian Elischer 	ep->function = function;
71793efcae8SPoul-Henning Kamp 	TAILQ_INSERT_TAIL(&exit_list, ep, next);
718e0d898b4SJulian Elischer 	return (0);
719fed06968SJulian Elischer }
72093efcae8SPoul-Henning Kamp 
721fed06968SJulian Elischer /*
72293efcae8SPoul-Henning Kamp  * Scan the exit callout list for the given item and remove it.
72393efcae8SPoul-Henning Kamp  * Returns the number of items removed (0 or 1)
724fed06968SJulian Elischer  */
725fed06968SJulian Elischer int
726eb776aeaSBruce Evans rm_at_exit(function)
727eb776aeaSBruce Evans 	exitlist_fn function;
728fed06968SJulian Elischer {
72993efcae8SPoul-Henning Kamp 	struct exitlist *ep;
730fed06968SJulian Elischer 
73193efcae8SPoul-Henning Kamp 	TAILQ_FOREACH(ep, &exit_list, next) {
732fed06968SJulian Elischer 		if (ep->function == function) {
73393efcae8SPoul-Henning Kamp 			TAILQ_REMOVE(&exit_list, ep, next);
73493efcae8SPoul-Henning Kamp 			free(ep, M_ATEXIT);
73593efcae8SPoul-Henning Kamp 			return(1);
736fed06968SJulian Elischer 		}
737fed06968SJulian Elischer 	}
73893efcae8SPoul-Henning Kamp 	return (0);
739fed06968SJulian Elischer }
740