xref: /freebsd/sys/kern/kern_exit.c (revision f34fa851e0b97ea3637d73827346927014e1b137)
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>
57df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
58797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
59df8bae1dSRodney W. Grimes #include <sys/ptrace.h>
607c409b8aSJeffrey Hsu #include <sys/acct.h>		/* for acct_process() function prototype */
61797f2d22SPoul-Henning Kamp #include <sys/filedesc.h>
62780dc5a8SPeter Wemm #include <sys/shm.h>
63780dc5a8SPeter Wemm #include <sys/sem.h>
645aaef07cSJohn Dyson #include <sys/aio.h>
6575c13541SPoul-Henning Kamp #include <sys/jail.h>
66780dc5a8SPeter Wemm 
67df8bae1dSRodney W. Grimes #include <vm/vm.h>
68efeaf95aSDavid Greenman #include <vm/vm_param.h>
69efeaf95aSDavid Greenman #include <vm/pmap.h>
70efeaf95aSDavid Greenman #include <vm/vm_map.h>
712d8acc0fSJohn Dyson #include <vm/vm_zone.h>
72dc9c271aSJulian Elischer #include <sys/user.h>
73df8bae1dSRodney W. Grimes 
74ba198b1cSMark Newton /* Required to be non-static for SysVR4 emulator */
7569a6f20bSMark Newton MALLOC_DEFINE(M_ZOMBIE, "zombie", "zombie proc status");
7655166637SPoul-Henning Kamp 
7793efcae8SPoul-Henning Kamp static MALLOC_DEFINE(M_ATEXIT, "atexit", "atexit callback");
7893efcae8SPoul-Henning Kamp 
7901166e92SBruce Evans static int wait1 __P((struct proc *, struct wait_args *, int));
805fdb8324SBruce Evans 
81df8bae1dSRodney W. Grimes /*
82fed06968SJulian Elischer  * callout list for things to do at exit time
83fed06968SJulian Elischer  */
8493efcae8SPoul-Henning Kamp struct exitlist {
85fed06968SJulian Elischer 	exitlist_fn function;
86e3975643SJake Burkholder 	TAILQ_ENTRY(exitlist) next;
8793efcae8SPoul-Henning Kamp };
88fed06968SJulian Elischer 
89e3975643SJake Burkholder TAILQ_HEAD(exit_list_head, exitlist);
9093efcae8SPoul-Henning Kamp static struct exit_list_head exit_list = TAILQ_HEAD_INITIALIZER(exit_list);
91fed06968SJulian Elischer 
92fed06968SJulian Elischer /*
93df8bae1dSRodney W. Grimes  * exit --
94df8bae1dSRodney W. Grimes  *	Death of process.
95df8bae1dSRodney W. Grimes  */
96fc0b1dbfSBruce Evans void
97ac2b067bSPeter Wemm sys_exit(p, uap)
98df8bae1dSRodney W. Grimes 	struct proc *p;
99ac2b067bSPeter Wemm 	struct sys_exit_args /* {
1005fdb8324SBruce Evans 		int	rval;
1015fdb8324SBruce Evans 	} */ *uap;
102df8bae1dSRodney W. Grimes {
103df8bae1dSRodney W. Grimes 
104df8bae1dSRodney W. Grimes 	exit1(p, W_EXITCODE(uap->rval, 0));
105df8bae1dSRodney W. Grimes 	/* NOTREACHED */
106df8bae1dSRodney W. Grimes }
107df8bae1dSRodney W. Grimes 
108df8bae1dSRodney W. Grimes /*
109df8bae1dSRodney W. Grimes  * Exit: deallocate address space and other resources, change proc state
110df8bae1dSRodney W. Grimes  * to zombie, and unlink proc from allproc and parent's lists.  Save exit
111df8bae1dSRodney W. Grimes  * status and rusage for wait().  Check for child processes and orphan them.
112df8bae1dSRodney W. Grimes  */
113fc0b1dbfSBruce Evans void
114df8bae1dSRodney W. Grimes exit1(p, rv)
115df8bae1dSRodney W. Grimes 	register struct proc *p;
116df8bae1dSRodney W. Grimes 	int rv;
117df8bae1dSRodney W. Grimes {
118df8bae1dSRodney W. Grimes 	register struct proc *q, *nq;
119df8bae1dSRodney W. Grimes 	register struct vmspace *vm;
12093efcae8SPoul-Henning Kamp 	struct exitlist *ep;
121df8bae1dSRodney W. Grimes 
1225f7bd355SPoul-Henning Kamp 	if (p->p_pid == 1) {
1235f7bd355SPoul-Henning Kamp 		printf("init died (signal %d, exit %d)\n",
124df8bae1dSRodney W. Grimes 		    WTERMSIG(rv), WEXITSTATUS(rv));
1255f7bd355SPoul-Henning Kamp 		panic("Going nowhere without my init!");
1265f7bd355SPoul-Henning Kamp 	}
1272c1011f7SJohn Dyson 
1282244ea07SJohn Dyson 	aio_proc_rundown(p);
1292244ea07SJohn Dyson 
1302c1011f7SJohn Dyson 	/* are we a task leader? */
131a914fb6bSJohn Baldwin 	PROC_LOCK(p);
1322c1011f7SJohn Dyson 	if(p == p->p_leader) {
1332c1011f7SJohn Dyson         	struct kill_args killArgs;
134a914fb6bSJohn Baldwin 
1352c1011f7SJohn Dyson 		killArgs.signum = SIGKILL;
1362c1011f7SJohn Dyson 		q = p->p_peers;
1372c1011f7SJohn Dyson 		while(q) {
1382c1011f7SJohn Dyson 			killArgs.pid = q->p_pid;
1392c1011f7SJohn Dyson 			/*
1402c1011f7SJohn Dyson 		         * The interface for kill is better
1412c1011f7SJohn Dyson 			 * than the internal signal
1422c1011f7SJohn Dyson 			 */
143a914fb6bSJohn Baldwin 			PROC_UNLOCK(p);
144cb226aaaSPoul-Henning Kamp 			kill(p, &killArgs);
145a914fb6bSJohn Baldwin 			PROC_LOCK(p);
1462c1011f7SJohn Dyson 			nq = q;
1472c1011f7SJohn Dyson 			q = q->p_peers;
1482c1011f7SJohn Dyson 		}
14979fc0bf4SMike Smith 		while (p->p_peers)
150a914fb6bSJohn Baldwin 			msleep((caddr_t)p, &p->p_mtx, PWAIT, "exit1", 0);
1512c1011f7SJohn Dyson 	}
152a914fb6bSJohn Baldwin 	PROC_UNLOCK(p);
1532c1011f7SJohn Dyson 
154df8bae1dSRodney W. Grimes #ifdef PGINPROF
155df8bae1dSRodney W. Grimes 	vmsizmon();
156df8bae1dSRodney W. Grimes #endif
1572a024a2bSSean Eric Fagan 	STOPEVENT(p, S_EXIT, rv);
15889361835SSean Eric Fagan 	wakeup(&p->p_stype);	/* Wakeup anyone in procfs' PIOCWAIT */
1592a024a2bSSean Eric Fagan 
160fed06968SJulian Elischer 	/*
161e9189611SPeter Wemm 	 * Check if any loadable modules need anything done at process exit.
162fed06968SJulian Elischer 	 * e.g. SYSV IPC stuff
163fed06968SJulian Elischer 	 * XXX what if one of these generates an error?
164fed06968SJulian Elischer 	 */
16593efcae8SPoul-Henning Kamp 	TAILQ_FOREACH(ep, &exit_list, next)
166fed06968SJulian Elischer 		(*ep->function)(p);
167fed06968SJulian Elischer 
168df8bae1dSRodney W. Grimes 	stopprofclock(p);
169a914fb6bSJohn Baldwin 
170df8bae1dSRodney W. Grimes 	MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage),
171df8bae1dSRodney W. Grimes 		M_ZOMBIE, M_WAITOK);
172df8bae1dSRodney W. Grimes 	/*
173df8bae1dSRodney W. Grimes 	 * If parent is waiting for us to exit or exec,
174df8bae1dSRodney W. Grimes 	 * P_PPWAIT is set; we will wakeup the parent below.
175df8bae1dSRodney W. Grimes 	 */
176a914fb6bSJohn Baldwin 	PROC_LOCK(p);
177df8bae1dSRodney W. Grimes 	p->p_flag &= ~(P_TRACED | P_PPWAIT);
178df8bae1dSRodney W. Grimes 	p->p_flag |= P_WEXIT;
1792c42a146SMarcel Moolenaar 	SIGEMPTYSET(p->p_siglist);
180a914fb6bSJohn Baldwin 	PROC_UNLOCK(p);
1814cf41af3SPoul-Henning Kamp 	if (timevalisset(&p->p_realtimer.it_value))
1824f559836SJake Burkholder 		callout_stop(&p->p_itcallout);
183df8bae1dSRodney W. Grimes 
184df8bae1dSRodney W. Grimes 	/*
185831d27a9SDon Lewis 	 * Reset any sigio structures pointing to us as a result of
186831d27a9SDon Lewis 	 * F_SETOWN with our pid.
187831d27a9SDon Lewis 	 */
188831d27a9SDon Lewis 	funsetownlst(&p->p_sigiolst);
189831d27a9SDon Lewis 
190831d27a9SDon Lewis 	/*
191df8bae1dSRodney W. Grimes 	 * Close open files and release open-file table.
192df8bae1dSRodney W. Grimes 	 * This may block!
193df8bae1dSRodney W. Grimes 	 */
194df8bae1dSRodney W. Grimes 	fdfree(p);
195df8bae1dSRodney W. Grimes 
196a914fb6bSJohn Baldwin 	/*
197a914fb6bSJohn Baldwin 	 * Remove ourself from our leader's peer list and wake our leader.
198a914fb6bSJohn Baldwin 	 */
199a914fb6bSJohn Baldwin 	PROC_LOCK(p);
20079fc0bf4SMike Smith 	if(p->p_leader->p_peers) {
20179fc0bf4SMike Smith 		q = p->p_leader;
20279fc0bf4SMike Smith 		while(q->p_peers != p)
20379fc0bf4SMike Smith 			q = q->p_peers;
20479fc0bf4SMike Smith 		q->p_peers = p->p_peers;
20579fc0bf4SMike Smith 		wakeup((caddr_t)p->p_leader);
20679fc0bf4SMike Smith 	}
207a914fb6bSJohn Baldwin 	PROC_UNLOCK(p);
20879fc0bf4SMike Smith 
20981090119SPeter Wemm 	/*
21081090119SPeter Wemm 	 * XXX Shutdown SYSV semaphores
21181090119SPeter Wemm 	 */
212a353d785SJoerg Wunsch 	semexit(p);
213a353d785SJoerg Wunsch 
214df8bae1dSRodney W. Grimes 	/* The next two chunks should probably be moved to vmspace_exit. */
215df8bae1dSRodney W. Grimes 	vm = p->p_vmspace;
216df8bae1dSRodney W. Grimes 	/*
217df8bae1dSRodney W. Grimes 	 * Release user portion of address space.
218df8bae1dSRodney W. Grimes 	 * This releases references to vnodes,
219df8bae1dSRodney W. Grimes 	 * which could cause I/O if the file has been unlinked.
220df8bae1dSRodney W. Grimes 	 * Need to do this early enough that we can still sleep.
221df8bae1dSRodney W. Grimes 	 * Can't free the entire vmspace as the kernel stack
222df8bae1dSRodney W. Grimes 	 * may be mapped within that space also.
223df8bae1dSRodney W. Grimes 	 */
2249d3fbbb5SJohn Dyson 	if (vm->vm_refcnt == 1) {
225a2a1c95cSPeter Wemm 		if (vm->vm_shm)
226a2a1c95cSPeter Wemm 			shmexit(p);
227b1028ad1SLuoqi Chen 		pmap_remove_pages(vmspace_pmap(vm), VM_MIN_ADDRESS,
2289d3fbbb5SJohn Dyson 		    VM_MAXUSER_ADDRESS);
22967bf6868SJohn Dyson 		(void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS,
23067bf6868SJohn Dyson 		    VM_MAXUSER_ADDRESS);
2319d3fbbb5SJohn Dyson 	}
232df8bae1dSRodney W. Grimes 
233a914fb6bSJohn Baldwin 	PROC_LOCK(p);
234df8bae1dSRodney W. Grimes 	if (SESS_LEADER(p)) {
235df8bae1dSRodney W. Grimes 		register struct session *sp = p->p_session;
236df8bae1dSRodney W. Grimes 
237a914fb6bSJohn Baldwin 		PROC_UNLOCK(p);
238df8bae1dSRodney W. Grimes 		if (sp->s_ttyvp) {
239df8bae1dSRodney W. Grimes 			/*
240df8bae1dSRodney W. Grimes 			 * Controlling process.
241df8bae1dSRodney W. Grimes 			 * Signal foreground pgrp,
242df8bae1dSRodney W. Grimes 			 * drain controlling terminal
243df8bae1dSRodney W. Grimes 			 * and revoke access to controlling terminal.
244df8bae1dSRodney W. Grimes 			 */
245dd45d8adSJulian Elischer 			if (sp->s_ttyp && (sp->s_ttyp->t_session == sp)) {
246df8bae1dSRodney W. Grimes 				if (sp->s_ttyp->t_pgrp)
247df8bae1dSRodney W. Grimes 					pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
248df8bae1dSRodney W. Grimes 				(void) ttywait(sp->s_ttyp);
249df8bae1dSRodney W. Grimes 				/*
250df8bae1dSRodney W. Grimes 				 * The tty could have been revoked
251df8bae1dSRodney W. Grimes 				 * if we blocked.
252df8bae1dSRodney W. Grimes 				 */
253df8bae1dSRodney W. Grimes 				if (sp->s_ttyvp)
254996c772fSJohn Dyson 					VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
255df8bae1dSRodney W. Grimes 			}
256df8bae1dSRodney W. Grimes 			if (sp->s_ttyvp)
257df8bae1dSRodney W. Grimes 				vrele(sp->s_ttyvp);
258df8bae1dSRodney W. Grimes 			sp->s_ttyvp = NULL;
259df8bae1dSRodney W. Grimes 			/*
260df8bae1dSRodney W. Grimes 			 * s_ttyp is not zero'd; we use this to indicate
261df8bae1dSRodney W. Grimes 			 * that the session once had a controlling terminal.
262df8bae1dSRodney W. Grimes 			 * (for logging and informational purposes)
263df8bae1dSRodney W. Grimes 			 */
264df8bae1dSRodney W. Grimes 		}
265df8bae1dSRodney W. Grimes 		sp->s_leader = NULL;
266a914fb6bSJohn Baldwin 	} else
267a914fb6bSJohn Baldwin 		PROC_UNLOCK(p);
268df8bae1dSRodney W. Grimes 	fixjobc(p, p->p_pgrp, 0);
269df8bae1dSRodney W. Grimes 	(void)acct_process(p);
270df8bae1dSRodney W. Grimes #ifdef KTRACE
271df8bae1dSRodney W. Grimes 	/*
272df8bae1dSRodney W. Grimes 	 * release trace file
273df8bae1dSRodney W. Grimes 	 */
274df8bae1dSRodney W. Grimes 	p->p_traceflag = 0;	/* don't trace the vrele() */
275df8bae1dSRodney W. Grimes 	if (p->p_tracep)
276df8bae1dSRodney W. Grimes 		vrele(p->p_tracep);
277df8bae1dSRodney W. Grimes #endif
278df8bae1dSRodney W. Grimes 	/*
279df8bae1dSRodney W. Grimes 	 * Remove proc from allproc queue and pidhash chain.
280df8bae1dSRodney W. Grimes 	 * Place onto zombproc.  Unlink from parent's child list.
281df8bae1dSRodney W. Grimes 	 */
282c0c25570SJake Burkholder 	ALLPROC_LOCK(AP_EXCLUSIVE);
283b75356e1SJeffrey Hsu 	LIST_REMOVE(p, p_list);
284b75356e1SJeffrey Hsu 	LIST_INSERT_HEAD(&zombproc, p, p_list);
285b75356e1SJeffrey Hsu 	LIST_REMOVE(p, p_hash);
286c0c25570SJake Burkholder 	ALLPROC_LOCK(AP_RELEASE);
287df8bae1dSRodney W. Grimes 
28898f03f90SJake Burkholder 	PROCTREE_LOCK(PT_EXCLUSIVE);
2892e3c8fcbSPoul-Henning Kamp 	q = LIST_FIRST(&p->p_children);
290a914fb6bSJohn Baldwin 	if (q != NULL)		/* only need this if any child is S_ZOMB */
291df8bae1dSRodney W. Grimes 		wakeup((caddr_t) initproc);
292a914fb6bSJohn Baldwin 	for (; q != NULL; q = nq) {
2932e3c8fcbSPoul-Henning Kamp 		nq = LIST_NEXT(q, p_sibling);
294a914fb6bSJohn Baldwin 		PROC_LOCK(q);
295c65437a3SJohn Baldwin 		proc_reparent(q, initproc);
2964ac9ae70SJulian Elischer 		q->p_sigparent = SIGCHLD;
297df8bae1dSRodney W. Grimes 		/*
298df8bae1dSRodney W. Grimes 		 * Traced processes are killed
299df8bae1dSRodney W. Grimes 		 * since their existence means someone is screwing up.
300df8bae1dSRodney W. Grimes 		 */
301df8bae1dSRodney W. Grimes 		if (q->p_flag & P_TRACED) {
302df8bae1dSRodney W. Grimes 			q->p_flag &= ~P_TRACED;
303df8bae1dSRodney W. Grimes 			psignal(q, SIGKILL);
304c65437a3SJohn Baldwin 		}
305a914fb6bSJohn Baldwin 		PROC_UNLOCK(q);
306df8bae1dSRodney W. Grimes 	}
307df8bae1dSRodney W. Grimes 
308df8bae1dSRodney W. Grimes 	/*
309df8bae1dSRodney W. Grimes 	 * Save exit status and final rusage info, adding in child rusage
310df8bae1dSRodney W. Grimes 	 * info and self times.
311df8bae1dSRodney W. Grimes 	 */
312df8bae1dSRodney W. Grimes 	p->p_xstat = rv;
313df8bae1dSRodney W. Grimes 	*p->p_ru = p->p_stats->p_ru;
3149ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
315df8bae1dSRodney W. Grimes 	calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
3169ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
317df8bae1dSRodney W. Grimes 	ruadd(p->p_ru, &p->p_stats->p_cru);
318df8bae1dSRodney W. Grimes 
319df8bae1dSRodney W. Grimes 	/*
32056ce1a8dSBruce Evans 	 * Pretend that an mi_switch() to the next process occurs now.  We
32156ce1a8dSBruce Evans 	 * must set `switchtime' directly since we will call cpu_switch()
32256ce1a8dSBruce Evans 	 * directly.  Set it now so that the rest of the exit time gets
32356ce1a8dSBruce Evans 	 * counted somewhere if possible.
32456ce1a8dSBruce Evans 	 */
3250d139b37STor Egge 	mtx_lock_spin(&sched_lock);
3269d0ddf18STor Egge 	microuptime(PCPU_PTR(switchtime));
327ef73ae4bSJake Burkholder 	PCPU_SET(switchticks, ticks);
3289d0ddf18STor Egge 	mtx_unlock_spin(&sched_lock);
32956ce1a8dSBruce Evans 
33056ce1a8dSBruce Evans 	/*
331a274d19bSBrian Feldman 	 * notify interested parties of our demise.
332cb679c38SJonathan Lemon 	 */
333a914fb6bSJohn Baldwin 	PROC_LOCK(p);
334a274d19bSBrian Feldman 	KNOTE(&p->p_klist, NOTE_EXIT);
335cb679c38SJonathan Lemon 
336cb679c38SJonathan Lemon 	/*
337645682fdSLuoqi Chen 	 * Notify parent that we're gone.  If parent has the PS_NOCLDWAIT
338245f17d4SJoerg Wunsch 	 * flag set, notify process 1 instead (and hope it will handle
339245f17d4SJoerg Wunsch 	 * this situation).
340df8bae1dSRodney W. Grimes 	 */
341645682fdSLuoqi Chen 	if (p->p_pptr->p_procsig->ps_flag & PS_NOCLDWAIT) {
342245f17d4SJoerg Wunsch 		struct proc *pp = p->p_pptr;
343245f17d4SJoerg Wunsch 		proc_reparent(p, initproc);
344245f17d4SJoerg Wunsch 		/*
345245f17d4SJoerg Wunsch 		 * If this was the last child of our parent, notify
346245f17d4SJoerg Wunsch 		 * parent, so in case he was wait(2)ing, he will
347245f17d4SJoerg Wunsch 		 * continue.
348245f17d4SJoerg Wunsch 		 */
349245f17d4SJoerg Wunsch 		if (LIST_EMPTY(&pp->p_children))
350245f17d4SJoerg Wunsch 			wakeup((caddr_t)pp);
351245f17d4SJoerg Wunsch 	}
352245f17d4SJoerg Wunsch 
353c65437a3SJohn Baldwin 	PROC_LOCK(p->p_pptr);
354c65437a3SJohn Baldwin 	if (p->p_sigparent && p->p_pptr != initproc)
3556626c604SJulian Elischer 	        psignal(p->p_pptr, p->p_sigparent);
356c65437a3SJohn Baldwin 	else
3576626c604SJulian Elischer 	        psignal(p->p_pptr, SIGCHLD);
358c65437a3SJohn Baldwin 	PROC_UNLOCK(p->p_pptr);
359c65437a3SJohn Baldwin 	PROC_UNLOCK(p);
36098f03f90SJake Burkholder 	PROCTREE_LOCK(PT_RELEASE);
361c65437a3SJohn Baldwin 
362df8bae1dSRodney W. Grimes 	/*
363df8bae1dSRodney W. Grimes 	 * Clear curproc after we've done all operations
364df8bae1dSRodney W. Grimes 	 * that could block, and before tearing down the rest
365df8bae1dSRodney W. Grimes 	 * of the process state that might be used from clock, etc.
366df8bae1dSRodney W. Grimes 	 * Also, can't clear curproc while we're still runnable,
367df8bae1dSRodney W. Grimes 	 * as we're not on a run queue (we are current, just not
368df8bae1dSRodney W. Grimes 	 * a proper proc any longer!).
369df8bae1dSRodney W. Grimes 	 *
370df8bae1dSRodney W. Grimes 	 * Other substructures are freed from wait().
371df8bae1dSRodney W. Grimes 	 */
372a914fb6bSJohn Baldwin 	mtx_assert(&Giant, MA_OWNED);
373be6a1d14SDavid Greenman 	if (--p->p_limit->p_refcnt == 0) {
374df8bae1dSRodney W. Grimes 		FREE(p->p_limit, M_SUBPROC);
375be6a1d14SDavid Greenman 		p->p_limit = NULL;
376be6a1d14SDavid Greenman 	}
377df8bae1dSRodney W. Grimes 
378df8bae1dSRodney W. Grimes 	/*
379df8bae1dSRodney W. Grimes 	 * Finally, call machine-dependent code to release the remaining
380df8bae1dSRodney W. Grimes 	 * resources including address space, the kernel stack and pcb.
381df8bae1dSRodney W. Grimes 	 * The address space is released by "vmspace_free(p->p_vmspace)";
382df8bae1dSRodney W. Grimes 	 * This is machine-dependent, as we may have to change stacks
383df8bae1dSRodney W. Grimes 	 * or ensure that the current one isn't reallocated before we
38478d7e629SBruce Evans 	 * finish.  cpu_exit will end with a call to cpu_switch(), finishing
385df8bae1dSRodney W. Grimes 	 * our execution (pun intended).
386df8bae1dSRodney W. Grimes 	 */
387df8bae1dSRodney W. Grimes 	cpu_exit(p);
388df8bae1dSRodney W. Grimes }
389df8bae1dSRodney W. Grimes 
390b2f9e8b1SBruce Evans #ifdef COMPAT_43
39126f9a767SRodney W. Grimes int
392cb226aaaSPoul-Henning Kamp owait(p, uap)
393df8bae1dSRodney W. Grimes 	struct proc *p;
3945fdb8324SBruce Evans 	register struct owait_args /* {
3955fdb8324SBruce Evans 		int     dummy;
3965fdb8324SBruce Evans 	} */ *uap;
397df8bae1dSRodney W. Grimes {
39893c9414eSSteven Wallace 	struct wait_args w;
399df8bae1dSRodney W. Grimes 
40093c9414eSSteven Wallace 	w.options = 0;
40193c9414eSSteven Wallace 	w.rusage = NULL;
40293c9414eSSteven Wallace 	w.pid = WAIT_ANY;
40393c9414eSSteven Wallace 	w.status = NULL;
40401166e92SBruce Evans 	return (wait1(p, &w, 1));
405df8bae1dSRodney W. Grimes }
406b2f9e8b1SBruce Evans #endif /* COMPAT_43 */
407df8bae1dSRodney W. Grimes 
40826f9a767SRodney W. Grimes int
409cb226aaaSPoul-Henning Kamp wait4(p, uap)
410df8bae1dSRodney W. Grimes 	struct proc *p;
411df8bae1dSRodney W. Grimes 	struct wait_args *uap;
412df8bae1dSRodney W. Grimes {
4135fdb8324SBruce Evans 
41401166e92SBruce Evans 	return (wait1(p, uap, 0));
415df8bae1dSRodney W. Grimes }
416df8bae1dSRodney W. Grimes 
41793c9414eSSteven Wallace static int
41801166e92SBruce Evans wait1(q, uap, compat)
419df8bae1dSRodney W. Grimes 	register struct proc *q;
4205fdb8324SBruce Evans 	register struct wait_args /* {
4215fdb8324SBruce Evans 		int pid;
4225fdb8324SBruce Evans 		int *status;
4235fdb8324SBruce Evans 		int options;
4245fdb8324SBruce Evans 		struct rusage *rusage;
4255fdb8324SBruce Evans 	} */ *uap;
42693c9414eSSteven Wallace 	int compat;
427df8bae1dSRodney W. Grimes {
428df8bae1dSRodney W. Grimes 	register int nfound;
429df8bae1dSRodney W. Grimes 	register struct proc *p, *t;
430f23c6d68SSteven Wallace 	int status, error;
431df8bae1dSRodney W. Grimes 
432df8bae1dSRodney W. Grimes 	if (uap->pid == 0)
433df8bae1dSRodney W. Grimes 		uap->pid = -q->p_pgid;
4344ac9ae70SJulian Elischer 	if (uap->options &~ (WUNTRACED|WNOHANG|WLINUXCLONE))
435df8bae1dSRodney W. Grimes 		return (EINVAL);
436df8bae1dSRodney W. Grimes loop:
437df8bae1dSRodney W. Grimes 	nfound = 0;
43898f03f90SJake Burkholder 	PROCTREE_LOCK(PT_SHARED);
4392e3c8fcbSPoul-Henning Kamp 	LIST_FOREACH(p, &q->p_children, p_sibling) {
440df8bae1dSRodney W. Grimes 		if (uap->pid != WAIT_ANY &&
441df8bae1dSRodney W. Grimes 		    p->p_pid != uap->pid && p->p_pgid != -uap->pid)
442df8bae1dSRodney W. Grimes 			continue;
4434ac9ae70SJulian Elischer 
4441156bc4dSJake Burkholder 		/*
4451156bc4dSJake Burkholder 		 * This special case handles a kthread spawned by linux_clone
4461156bc4dSJake Burkholder 		 * (see linux_misc.c).  The linux_wait4 and linux_waitpid
4471156bc4dSJake Burkholder 		 * functions need to be able to distinguish between waiting
4481156bc4dSJake Burkholder 		 * on a process and waiting on a thread.  It is a thread if
4491156bc4dSJake Burkholder 		 * p_sigparent is not SIGCHLD, and the WLINUXCLONE option
4501156bc4dSJake Burkholder 		 * signifies we want to wait for threads and not processes.
4514ac9ae70SJulian Elischer 		 */
452a914fb6bSJohn Baldwin 		PROC_LOCK(p);
4531156bc4dSJake Burkholder 		if ((p->p_sigparent != SIGCHLD) ^
454a914fb6bSJohn Baldwin 		    ((uap->options & WLINUXCLONE) != 0)) {
455a914fb6bSJohn Baldwin 			PROC_UNLOCK(p);
4564ac9ae70SJulian Elischer 			continue;
457a914fb6bSJohn Baldwin 		}
4584ac9ae70SJulian Elischer 
459df8bae1dSRodney W. Grimes 		nfound++;
4609ed346baSBosko Milekic 		mtx_lock_spin(&sched_lock);
461df8bae1dSRodney W. Grimes 		if (p->p_stat == SZOMB) {
4620d2afceeSDavid Greenman 			/* charge childs scheduling cpu usage to parent */
463d5c4431eSDavid Greenman 			if (curproc->p_pid != 1) {
464bdf42357SBruce Evans 				curproc->p_estcpu =
465bdf42357SBruce Evans 				    ESTCPULIM(curproc->p_estcpu + p->p_estcpu);
466d5c4431eSDavid Greenman 			}
4670d2afceeSDavid Greenman 
468c65437a3SJohn Baldwin 			mtx_unlock_spin(&sched_lock);
469c65437a3SJohn Baldwin 			PROC_UNLOCK(p);
470c65437a3SJohn Baldwin 			PROCTREE_LOCK(PT_RELEASE);
471c65437a3SJohn Baldwin 
47201166e92SBruce Evans 			q->p_retval[0] = p->p_pid;
473b2f9e8b1SBruce Evans #ifdef COMPAT_43
47493c9414eSSteven Wallace 			if (compat)
47501166e92SBruce Evans 				q->p_retval[1] = p->p_xstat;
476df8bae1dSRodney W. Grimes 			else
477df8bae1dSRodney W. Grimes #endif
478df8bae1dSRodney W. Grimes 			if (uap->status) {
479df8bae1dSRodney W. Grimes 				status = p->p_xstat;	/* convert to int */
480bb56ec4aSPoul-Henning Kamp 				if ((error = copyout((caddr_t)&status,
481bb56ec4aSPoul-Henning Kamp 				    (caddr_t)uap->status, sizeof(status))))
482df8bae1dSRodney W. Grimes 					return (error);
483df8bae1dSRodney W. Grimes 			}
484df8bae1dSRodney W. Grimes 			if (uap->rusage && (error = copyout((caddr_t)p->p_ru,
485df8bae1dSRodney W. Grimes 			    (caddr_t)uap->rusage, sizeof (struct rusage))))
486df8bae1dSRodney W. Grimes 				return (error);
487df8bae1dSRodney W. Grimes 			/*
488df8bae1dSRodney W. Grimes 			 * If we got the child via a ptrace 'attach',
489df8bae1dSRodney W. Grimes 			 * we need to give it back to the old parent.
490df8bae1dSRodney W. Grimes 			 */
49198f03f90SJake Burkholder 			PROCTREE_LOCK(PT_EXCLUSIVE);
492c65437a3SJohn Baldwin 			if (p->p_oppid) {
49398f03f90SJake Burkholder 				if ((t = pfind(p->p_oppid)) != NULL) {
494c65437a3SJohn Baldwin 					PROC_LOCK(p);
495df8bae1dSRodney W. Grimes 					p->p_oppid = 0;
496df8bae1dSRodney W. Grimes 					proc_reparent(p, t);
497c65437a3SJohn Baldwin 					PROC_UNLOCK(p);
498c65437a3SJohn Baldwin 					PROC_LOCK(t);
499df8bae1dSRodney W. Grimes 					psignal(t, SIGCHLD);
500c65437a3SJohn Baldwin 					PROC_UNLOCK(t);
50198f03f90SJake Burkholder 					PROCTREE_LOCK(PT_RELEASE);
502c65437a3SJohn Baldwin 					wakeup((caddr_t)t);
503df8bae1dSRodney W. Grimes 					return (0);
504df8bae1dSRodney W. Grimes 				}
50598f03f90SJake Burkholder 			}
506c65437a3SJohn Baldwin 			PROCTREE_LOCK(PT_RELEASE);
507c65437a3SJohn Baldwin 			PROC_LOCK(p);
508df8bae1dSRodney W. Grimes 			p->p_xstat = 0;
509c65437a3SJohn Baldwin 			PROC_UNLOCK(p);
510df8bae1dSRodney W. Grimes 			ruadd(&q->p_stats->p_cru, p->p_ru);
511df8bae1dSRodney W. Grimes 			FREE(p->p_ru, M_ZOMBIE);
512be6a1d14SDavid Greenman 			p->p_ru = NULL;
513df8bae1dSRodney W. Grimes 
514df8bae1dSRodney W. Grimes 			/*
515df8bae1dSRodney W. Grimes 			 * Decrement the count of procs running with this uid.
516df8bae1dSRodney W. Grimes 			 */
517f535380cSDon Lewis 			(void)chgproccnt(p->p_cred->p_uidinfo, -1, 0);
518df8bae1dSRodney W. Grimes 
519df8bae1dSRodney W. Grimes 			/*
520bd7e5f99SJohn Dyson 			 * Release reference to text vnode
521bd7e5f99SJohn Dyson 			 */
522bd7e5f99SJohn Dyson 			if (p->p_textvp)
523bd7e5f99SJohn Dyson 				vrele(p->p_textvp);
524bd7e5f99SJohn Dyson 
525bd7e5f99SJohn Dyson 			/*
526df8bae1dSRodney W. Grimes 			 * Free up credentials.
527df8bae1dSRodney W. Grimes 			 */
528a914fb6bSJohn Baldwin 			PROC_LOCK(p);
529df8bae1dSRodney W. Grimes 			if (--p->p_cred->p_refcnt == 0) {
530da654d90SPoul-Henning Kamp 				crfree(p->p_ucred);
531f535380cSDon Lewis 				uifree(p->p_cred->p_uidinfo);
532df8bae1dSRodney W. Grimes 				FREE(p->p_cred, M_SUBPROC);
533be6a1d14SDavid Greenman 				p->p_cred = NULL;
534df8bae1dSRodney W. Grimes 			}
535df8bae1dSRodney W. Grimes 
536df8bae1dSRodney W. Grimes 			/*
537b9df5231SPoul-Henning Kamp 			 * Remove unused arguments
538b9df5231SPoul-Henning Kamp 			 */
539b9df5231SPoul-Henning Kamp 			if (p->p_args && --p->p_args->ar_ref == 0)
540b9df5231SPoul-Henning Kamp 				FREE(p->p_args, M_PARGS);
541a914fb6bSJohn Baldwin 			PROC_UNLOCK(p);
542b9df5231SPoul-Henning Kamp 
543b9df5231SPoul-Henning Kamp 			/*
544df8bae1dSRodney W. Grimes 			 * Finally finished with old proc entry.
545df8bae1dSRodney W. Grimes 			 * Unlink it from its process group and free it.
546df8bae1dSRodney W. Grimes 			 */
547df8bae1dSRodney W. Grimes 			leavepgrp(p);
54898f03f90SJake Burkholder 
549c0c25570SJake Burkholder 			ALLPROC_LOCK(AP_EXCLUSIVE);
550b75356e1SJeffrey Hsu 			LIST_REMOVE(p, p_list);	/* off zombproc */
551c0c25570SJake Burkholder 			ALLPROC_LOCK(AP_RELEASE);
55298f03f90SJake Burkholder 
55398f03f90SJake Burkholder 			PROCTREE_LOCK(PT_EXCLUSIVE);
554b75356e1SJeffrey Hsu 			LIST_REMOVE(p, p_sibling);
55598f03f90SJake Burkholder 			PROCTREE_LOCK(PT_RELEASE);
556df8bae1dSRodney W. Grimes 
557a914fb6bSJohn Baldwin 			PROC_LOCK(p);
5586626c604SJulian Elischer 			if (--p->p_procsig->ps_refcnt == 0) {
559dc9c271aSJulian Elischer 				if (p->p_sigacts != &p->p_addr->u_sigacts)
560dc9c271aSJulian Elischer 					FREE(p->p_sigacts, M_SUBPROC);
561dc9c271aSJulian Elischer 			        FREE(p->p_procsig, M_SUBPROC);
5626626c604SJulian Elischer 				p->p_procsig = NULL;
5636626c604SJulian Elischer 			}
564a914fb6bSJohn Baldwin 			PROC_UNLOCK(p);
56588c5ea45SJulian Elischer 
566df8bae1dSRodney W. Grimes 			/*
567df8bae1dSRodney W. Grimes 			 * Give machine-dependent layer a chance
568df8bae1dSRodney W. Grimes 			 * to free anything that cpu_exit couldn't
569df8bae1dSRodney W. Grimes 			 * release while still running in process context.
570df8bae1dSRodney W. Grimes 			 */
571df8bae1dSRodney W. Grimes 			cpu_wait(p);
5724971f62aSJohn Baldwin 			mtx_destroy(&p->p_mtx);
5732d8acc0fSJohn Dyson 			zfree(proc_zone, p);
574df8bae1dSRodney W. Grimes 			nprocs--;
575df8bae1dSRodney W. Grimes 			return (0);
576df8bae1dSRodney W. Grimes 		}
577df8bae1dSRodney W. Grimes 		if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
578df8bae1dSRodney W. Grimes 		    (p->p_flag & P_TRACED || uap->options & WUNTRACED)) {
5799ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
580df8bae1dSRodney W. Grimes 			p->p_flag |= P_WAITED;
581a914fb6bSJohn Baldwin 			PROC_UNLOCK(p);
582a914fb6bSJohn Baldwin 			PROCTREE_LOCK(PT_RELEASE);
58301166e92SBruce Evans 			q->p_retval[0] = p->p_pid;
584b2f9e8b1SBruce Evans #ifdef COMPAT_43
58593c9414eSSteven Wallace 			if (compat) {
58601166e92SBruce Evans 				q->p_retval[1] = W_STOPCODE(p->p_xstat);
587df8bae1dSRodney W. Grimes 				error = 0;
588df8bae1dSRodney W. Grimes 			} else
589df8bae1dSRodney W. Grimes #endif
590df8bae1dSRodney W. Grimes 			if (uap->status) {
591df8bae1dSRodney W. Grimes 				status = W_STOPCODE(p->p_xstat);
592df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&status,
593df8bae1dSRodney W. Grimes 					(caddr_t)uap->status, sizeof(status));
594df8bae1dSRodney W. Grimes 			} else
595df8bae1dSRodney W. Grimes 				error = 0;
596df8bae1dSRodney W. Grimes 			return (error);
597df8bae1dSRodney W. Grimes 		}
5989ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
599a914fb6bSJohn Baldwin 		PROC_UNLOCK(p);
600df8bae1dSRodney W. Grimes 	}
60198f03f90SJake Burkholder 	PROCTREE_LOCK(PT_RELEASE);
602df8bae1dSRodney W. Grimes 	if (nfound == 0)
603df8bae1dSRodney W. Grimes 		return (ECHILD);
604df8bae1dSRodney W. Grimes 	if (uap->options & WNOHANG) {
60501166e92SBruce Evans 		q->p_retval[0] = 0;
606df8bae1dSRodney W. Grimes 		return (0);
607df8bae1dSRodney W. Grimes 	}
608bb56ec4aSPoul-Henning Kamp 	if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)))
609df8bae1dSRodney W. Grimes 		return (error);
610df8bae1dSRodney W. Grimes 	goto loop;
611df8bae1dSRodney W. Grimes }
612df8bae1dSRodney W. Grimes 
613df8bae1dSRodney W. Grimes /*
61498f03f90SJake Burkholder  * Make process 'parent' the new parent of process 'child'.
61598f03f90SJake Burkholder  * Must be called with an exclusive hold of proctree lock.
616df8bae1dSRodney W. Grimes  */
617df8bae1dSRodney W. Grimes void
618df8bae1dSRodney W. Grimes proc_reparent(child, parent)
619df8bae1dSRodney W. Grimes 	register struct proc *child;
620df8bae1dSRodney W. Grimes 	register struct proc *parent;
621df8bae1dSRodney W. Grimes {
622df8bae1dSRodney W. Grimes 
62398f03f90SJake Burkholder 	PROCTREE_ASSERT(PT_EXCLUSIVE);
624c65437a3SJohn Baldwin 	PROC_LOCK_ASSERT(child, MA_OWNED);
625df8bae1dSRodney W. Grimes 	if (child->p_pptr == parent)
626df8bae1dSRodney W. Grimes 		return;
627df8bae1dSRodney W. Grimes 
628b75356e1SJeffrey Hsu 	LIST_REMOVE(child, p_sibling);
629b75356e1SJeffrey Hsu 	LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
630df8bae1dSRodney W. Grimes 	child->p_pptr = parent;
631df8bae1dSRodney W. Grimes }
632fed06968SJulian Elischer 
633e0d898b4SJulian Elischer /*
634e0d898b4SJulian Elischer  * The next two functions are to handle adding/deleting items on the
635fed06968SJulian Elischer  * exit callout list
636e0d898b4SJulian Elischer  *
637e0d898b4SJulian Elischer  * at_exit():
638e0d898b4SJulian Elischer  * Take the arguments given and put them onto the exit callout list,
639fed06968SJulian Elischer  * However first make sure that it's not already there.
640fed06968SJulian Elischer  * returns 0 on success.
641fed06968SJulian Elischer  */
64293efcae8SPoul-Henning Kamp 
643fed06968SJulian Elischer int
644eb776aeaSBruce Evans at_exit(function)
645eb776aeaSBruce Evans 	exitlist_fn function;
646fed06968SJulian Elischer {
64793efcae8SPoul-Henning Kamp 	struct exitlist *ep;
648e0d898b4SJulian Elischer 
64993efcae8SPoul-Henning Kamp #ifdef INVARIANTS
650e0d898b4SJulian Elischer 	/* Be noisy if the programmer has lost track of things */
651e0d898b4SJulian Elischer 	if (rm_at_exit(function))
65293efcae8SPoul-Henning Kamp 		printf("WARNING: exit callout entry (%p) already present\n",
65393efcae8SPoul-Henning Kamp 		    function);
65493efcae8SPoul-Henning Kamp #endif
65593efcae8SPoul-Henning Kamp 	ep = malloc(sizeof(*ep), M_ATEXIT, M_NOWAIT);
656e0d898b4SJulian Elischer 	if (ep == NULL)
657e0d898b4SJulian Elischer 		return (ENOMEM);
658fed06968SJulian Elischer 	ep->function = function;
65993efcae8SPoul-Henning Kamp 	TAILQ_INSERT_TAIL(&exit_list, ep, next);
660e0d898b4SJulian Elischer 	return (0);
661fed06968SJulian Elischer }
66293efcae8SPoul-Henning Kamp 
663fed06968SJulian Elischer /*
66493efcae8SPoul-Henning Kamp  * Scan the exit callout list for the given item and remove it.
66593efcae8SPoul-Henning Kamp  * Returns the number of items removed (0 or 1)
666fed06968SJulian Elischer  */
667fed06968SJulian Elischer int
668eb776aeaSBruce Evans rm_at_exit(function)
669eb776aeaSBruce Evans 	exitlist_fn function;
670fed06968SJulian Elischer {
67193efcae8SPoul-Henning Kamp 	struct exitlist *ep;
672fed06968SJulian Elischer 
67393efcae8SPoul-Henning Kamp 	TAILQ_FOREACH(ep, &exit_list, next) {
674fed06968SJulian Elischer 		if (ep->function == function) {
67593efcae8SPoul-Henning Kamp 			TAILQ_REMOVE(&exit_list, ep, next);
67693efcae8SPoul-Henning Kamp 			free(ep, M_ATEXIT);
67793efcae8SPoul-Henning Kamp 			return(1);
678fed06968SJulian Elischer 		}
679fed06968SJulian Elischer 	}
68093efcae8SPoul-Henning Kamp 	return (0);
681fed06968SJulian Elischer }
682dc9c271aSJulian Elischer 
683dc9c271aSJulian Elischer void check_sigacts (void)
684dc9c271aSJulian Elischer {
685dc9c271aSJulian Elischer 	struct proc *p = curproc;
686dc9c271aSJulian Elischer 	struct sigacts *pss;
687dc9c271aSJulian Elischer 	int s;
688dc9c271aSJulian Elischer 
689a914fb6bSJohn Baldwin 	PROC_LOCK(p);
690dc9c271aSJulian Elischer 	if (p->p_procsig->ps_refcnt == 1 &&
691dc9c271aSJulian Elischer 	    p->p_sigacts != &p->p_addr->u_sigacts) {
692dc9c271aSJulian Elischer 		pss = p->p_sigacts;
693dc9c271aSJulian Elischer 		s = splhigh();
694dc9c271aSJulian Elischer 		p->p_addr->u_sigacts = *pss;
695dc9c271aSJulian Elischer 		p->p_sigacts = &p->p_addr->u_sigacts;
696dc9c271aSJulian Elischer 		splx(s);
697dc9c271aSJulian Elischer 		FREE(pss, M_SUBPROC);
698dc9c271aSJulian Elischer 	}
699a914fb6bSJohn Baldwin 	PROC_UNLOCK(p);
700dc9c271aSJulian Elischer }
70188c5ea45SJulian Elischer 
702