xref: /freebsd/sys/kern/kern_exit.c (revision e39756439c7255bc16ba14b7b991cb01ba1c93bd)
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>
50df8bae1dSRodney W. Grimes #include <sys/proc.h>
512a024a2bSSean Eric Fagan #include <sys/pioctl.h>
52df8bae1dSRodney W. Grimes #include <sys/tty.h>
53df8bae1dSRodney W. Grimes #include <sys/wait.h>
54df8bae1dSRodney W. Grimes #include <sys/vnode.h>
55df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
56797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
57df8bae1dSRodney W. Grimes #include <sys/ptrace.h>
587c409b8aSJeffrey Hsu #include <sys/acct.h>		/* for acct_process() function prototype */
59797f2d22SPoul-Henning Kamp #include <sys/filedesc.h>
60780dc5a8SPeter Wemm #include <sys/shm.h>
61780dc5a8SPeter Wemm #include <sys/sem.h>
625aaef07cSJohn Dyson #include <sys/aio.h>
6375c13541SPoul-Henning Kamp #include <sys/jail.h>
64780dc5a8SPeter Wemm 
65df8bae1dSRodney W. Grimes #include <vm/vm.h>
66efeaf95aSDavid Greenman #include <vm/vm_param.h>
67996c772fSJohn Dyson #include <sys/lock.h>
68efeaf95aSDavid Greenman #include <vm/pmap.h>
69efeaf95aSDavid Greenman #include <vm/vm_map.h>
702d8acc0fSJohn Dyson #include <vm/vm_zone.h>
71dc9c271aSJulian Elischer #include <sys/user.h>
72df8bae1dSRodney W. Grimes 
73ba198b1cSMark Newton /* Required to be non-static for SysVR4 emulator */
7469a6f20bSMark Newton MALLOC_DEFINE(M_ZOMBIE, "zombie", "zombie proc status");
7555166637SPoul-Henning Kamp 
7693efcae8SPoul-Henning Kamp static MALLOC_DEFINE(M_ATEXIT, "atexit", "atexit callback");
7793efcae8SPoul-Henning Kamp 
7801166e92SBruce Evans static int wait1 __P((struct proc *, struct wait_args *, int));
795fdb8324SBruce Evans 
80df8bae1dSRodney W. Grimes /*
81fed06968SJulian Elischer  * callout list for things to do at exit time
82fed06968SJulian Elischer  */
8393efcae8SPoul-Henning Kamp struct exitlist {
84fed06968SJulian Elischer 	exitlist_fn function;
85e3975643SJake Burkholder 	TAILQ_ENTRY(exitlist) next;
8693efcae8SPoul-Henning Kamp };
87fed06968SJulian Elischer 
88e3975643SJake Burkholder TAILQ_HEAD(exit_list_head, exitlist);
8993efcae8SPoul-Henning Kamp static struct exit_list_head exit_list = TAILQ_HEAD_INITIALIZER(exit_list);
90fed06968SJulian Elischer 
91fed06968SJulian Elischer /*
92df8bae1dSRodney W. Grimes  * exit --
93df8bae1dSRodney W. Grimes  *	Death of process.
94df8bae1dSRodney W. Grimes  */
95fc0b1dbfSBruce Evans void
96cb226aaaSPoul-Henning Kamp exit(p, uap)
97df8bae1dSRodney W. Grimes 	struct proc *p;
985fdb8324SBruce Evans 	struct rexit_args /* {
995fdb8324SBruce Evans 		int	rval;
1005fdb8324SBruce Evans 	} */ *uap;
101df8bae1dSRodney W. Grimes {
102df8bae1dSRodney W. Grimes 
103df8bae1dSRodney W. Grimes 	exit1(p, W_EXITCODE(uap->rval, 0));
104df8bae1dSRodney W. Grimes 	/* NOTREACHED */
105df8bae1dSRodney W. Grimes }
106df8bae1dSRodney W. Grimes 
107df8bae1dSRodney W. Grimes /*
108df8bae1dSRodney W. Grimes  * Exit: deallocate address space and other resources, change proc state
109df8bae1dSRodney W. Grimes  * to zombie, and unlink proc from allproc and parent's lists.  Save exit
110df8bae1dSRodney W. Grimes  * status and rusage for wait().  Check for child processes and orphan them.
111df8bae1dSRodney W. Grimes  */
112fc0b1dbfSBruce Evans void
113df8bae1dSRodney W. Grimes exit1(p, rv)
114df8bae1dSRodney W. Grimes 	register struct proc *p;
115df8bae1dSRodney W. Grimes 	int rv;
116df8bae1dSRodney W. Grimes {
117df8bae1dSRodney W. Grimes 	register struct proc *q, *nq;
118df8bae1dSRodney W. Grimes 	register struct vmspace *vm;
11993efcae8SPoul-Henning Kamp 	struct exitlist *ep;
120df8bae1dSRodney W. Grimes 
1215f7bd355SPoul-Henning Kamp 	if (p->p_pid == 1) {
1225f7bd355SPoul-Henning Kamp 		printf("init died (signal %d, exit %d)\n",
123df8bae1dSRodney W. Grimes 		    WTERMSIG(rv), WEXITSTATUS(rv));
1245f7bd355SPoul-Henning Kamp 		panic("Going nowhere without my init!");
1255f7bd355SPoul-Henning Kamp 	}
1262c1011f7SJohn Dyson 
1272244ea07SJohn Dyson 	aio_proc_rundown(p);
1282244ea07SJohn Dyson 
1292c1011f7SJohn Dyson 	/* are we a task leader? */
1302c1011f7SJohn Dyson 	if(p == p->p_leader) {
1312c1011f7SJohn Dyson         	struct kill_args killArgs;
1322c1011f7SJohn Dyson 		killArgs.signum = SIGKILL;
1332c1011f7SJohn Dyson 		q = p->p_peers;
1342c1011f7SJohn Dyson 		while(q) {
1352c1011f7SJohn Dyson 			killArgs.pid = q->p_pid;
1362c1011f7SJohn Dyson 			/*
1372c1011f7SJohn Dyson 		         * The interface for kill is better
1382c1011f7SJohn Dyson 			 * than the internal signal
1392c1011f7SJohn Dyson 			 */
140cb226aaaSPoul-Henning Kamp 			kill(p, &killArgs);
1412c1011f7SJohn Dyson 			nq = q;
1422c1011f7SJohn Dyson 			q = q->p_peers;
1432c1011f7SJohn Dyson 		}
14479fc0bf4SMike Smith 		while (p->p_peers)
14579fc0bf4SMike Smith 		  tsleep((caddr_t)p, PWAIT, "exit1", 0);
1462c1011f7SJohn Dyson 	}
1472c1011f7SJohn Dyson 
148df8bae1dSRodney W. Grimes #ifdef PGINPROF
149df8bae1dSRodney W. Grimes 	vmsizmon();
150df8bae1dSRodney W. Grimes #endif
1512a024a2bSSean Eric Fagan 	STOPEVENT(p, S_EXIT, rv);
15289361835SSean Eric Fagan 	wakeup(&p->p_stype);	/* Wakeup anyone in procfs' PIOCWAIT */
1532a024a2bSSean Eric Fagan 
154fed06968SJulian Elischer 	/*
155e9189611SPeter Wemm 	 * Check if any loadable modules need anything done at process exit.
156fed06968SJulian Elischer 	 * e.g. SYSV IPC stuff
157fed06968SJulian Elischer 	 * XXX what if one of these generates an error?
158fed06968SJulian Elischer 	 */
15993efcae8SPoul-Henning Kamp 	TAILQ_FOREACH(ep, &exit_list, next)
160fed06968SJulian Elischer 		(*ep->function)(p);
161fed06968SJulian Elischer 
162df8bae1dSRodney W. Grimes 	if (p->p_flag & P_PROFIL)
163df8bae1dSRodney W. Grimes 		stopprofclock(p);
164df8bae1dSRodney W. Grimes 	MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage),
165df8bae1dSRodney W. Grimes 		M_ZOMBIE, M_WAITOK);
166df8bae1dSRodney W. Grimes 	/*
167df8bae1dSRodney W. Grimes 	 * If parent is waiting for us to exit or exec,
168df8bae1dSRodney W. Grimes 	 * P_PPWAIT is set; we will wakeup the parent below.
169df8bae1dSRodney W. Grimes 	 */
170df8bae1dSRodney W. Grimes 	p->p_flag &= ~(P_TRACED | P_PPWAIT);
171df8bae1dSRodney W. Grimes 	p->p_flag |= P_WEXIT;
1722c42a146SMarcel Moolenaar 	SIGEMPTYSET(p->p_siglist);
1734cf41af3SPoul-Henning Kamp 	if (timevalisset(&p->p_realtimer.it_value))
174ab36c067SJustin T. Gibbs 		untimeout(realitexpire, (caddr_t)p, p->p_ithandle);
175df8bae1dSRodney W. Grimes 
176df8bae1dSRodney W. Grimes 	/*
177831d27a9SDon Lewis 	 * Reset any sigio structures pointing to us as a result of
178831d27a9SDon Lewis 	 * F_SETOWN with our pid.
179831d27a9SDon Lewis 	 */
180831d27a9SDon Lewis 	funsetownlst(&p->p_sigiolst);
181831d27a9SDon Lewis 
182831d27a9SDon Lewis 	/*
183df8bae1dSRodney W. Grimes 	 * Close open files and release open-file table.
184df8bae1dSRodney W. Grimes 	 * This may block!
185df8bae1dSRodney W. Grimes 	 */
186df8bae1dSRodney W. Grimes 	fdfree(p);
187df8bae1dSRodney W. Grimes 
18879fc0bf4SMike Smith 	if(p->p_leader->p_peers) {
18979fc0bf4SMike Smith 		q = p->p_leader;
19079fc0bf4SMike Smith 		while(q->p_peers != p)
19179fc0bf4SMike Smith 			q = q->p_peers;
19279fc0bf4SMike Smith 		q->p_peers = p->p_peers;
19379fc0bf4SMike Smith 		wakeup((caddr_t)p->p_leader);
19479fc0bf4SMike Smith 	}
19579fc0bf4SMike Smith 
19681090119SPeter Wemm 	/*
19781090119SPeter Wemm 	 * XXX Shutdown SYSV semaphores
19881090119SPeter Wemm 	 */
199a353d785SJoerg Wunsch 	semexit(p);
200a353d785SJoerg Wunsch 
201df8bae1dSRodney W. Grimes 	/* The next two chunks should probably be moved to vmspace_exit. */
202df8bae1dSRodney W. Grimes 	vm = p->p_vmspace;
203df8bae1dSRodney W. Grimes 	/*
204df8bae1dSRodney W. Grimes 	 * Release user portion of address space.
205df8bae1dSRodney W. Grimes 	 * This releases references to vnodes,
206df8bae1dSRodney W. Grimes 	 * which could cause I/O if the file has been unlinked.
207df8bae1dSRodney W. Grimes 	 * Need to do this early enough that we can still sleep.
208df8bae1dSRodney W. Grimes 	 * Can't free the entire vmspace as the kernel stack
209df8bae1dSRodney W. Grimes 	 * may be mapped within that space also.
210df8bae1dSRodney W. Grimes 	 */
2119d3fbbb5SJohn Dyson 	if (vm->vm_refcnt == 1) {
212a2a1c95cSPeter Wemm 		if (vm->vm_shm)
213a2a1c95cSPeter Wemm 			shmexit(p);
214b1028ad1SLuoqi Chen 		pmap_remove_pages(vmspace_pmap(vm), VM_MIN_ADDRESS,
2159d3fbbb5SJohn Dyson 		    VM_MAXUSER_ADDRESS);
21667bf6868SJohn Dyson 		(void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS,
21767bf6868SJohn Dyson 		    VM_MAXUSER_ADDRESS);
2189d3fbbb5SJohn Dyson 	}
219df8bae1dSRodney W. Grimes 
220df8bae1dSRodney W. Grimes 	if (SESS_LEADER(p)) {
221df8bae1dSRodney W. Grimes 		register struct session *sp = p->p_session;
222df8bae1dSRodney W. Grimes 
223df8bae1dSRodney W. Grimes 		if (sp->s_ttyvp) {
224df8bae1dSRodney W. Grimes 			/*
225df8bae1dSRodney W. Grimes 			 * Controlling process.
226df8bae1dSRodney W. Grimes 			 * Signal foreground pgrp,
227df8bae1dSRodney W. Grimes 			 * drain controlling terminal
228df8bae1dSRodney W. Grimes 			 * and revoke access to controlling terminal.
229df8bae1dSRodney W. Grimes 			 */
230dd45d8adSJulian Elischer 			if (sp->s_ttyp && (sp->s_ttyp->t_session == sp)) {
231df8bae1dSRodney W. Grimes 				if (sp->s_ttyp->t_pgrp)
232df8bae1dSRodney W. Grimes 					pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
233df8bae1dSRodney W. Grimes 				(void) ttywait(sp->s_ttyp);
234df8bae1dSRodney W. Grimes 				/*
235df8bae1dSRodney W. Grimes 				 * The tty could have been revoked
236df8bae1dSRodney W. Grimes 				 * if we blocked.
237df8bae1dSRodney W. Grimes 				 */
238df8bae1dSRodney W. Grimes 				if (sp->s_ttyvp)
239996c772fSJohn Dyson 					VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
240df8bae1dSRodney W. Grimes 			}
241df8bae1dSRodney W. Grimes 			if (sp->s_ttyvp)
242df8bae1dSRodney W. Grimes 				vrele(sp->s_ttyvp);
243df8bae1dSRodney W. Grimes 			sp->s_ttyvp = NULL;
244df8bae1dSRodney W. Grimes 			/*
245df8bae1dSRodney W. Grimes 			 * s_ttyp is not zero'd; we use this to indicate
246df8bae1dSRodney W. Grimes 			 * that the session once had a controlling terminal.
247df8bae1dSRodney W. Grimes 			 * (for logging and informational purposes)
248df8bae1dSRodney W. Grimes 			 */
249df8bae1dSRodney W. Grimes 		}
250df8bae1dSRodney W. Grimes 		sp->s_leader = NULL;
251df8bae1dSRodney W. Grimes 	}
252df8bae1dSRodney W. Grimes 	fixjobc(p, p->p_pgrp, 0);
253df8bae1dSRodney W. Grimes 	(void)acct_process(p);
254df8bae1dSRodney W. Grimes #ifdef KTRACE
255df8bae1dSRodney W. Grimes 	/*
256df8bae1dSRodney W. Grimes 	 * release trace file
257df8bae1dSRodney W. Grimes 	 */
258df8bae1dSRodney W. Grimes 	p->p_traceflag = 0;	/* don't trace the vrele() */
259df8bae1dSRodney W. Grimes 	if (p->p_tracep)
260df8bae1dSRodney W. Grimes 		vrele(p->p_tracep);
261df8bae1dSRodney W. Grimes #endif
262df8bae1dSRodney W. Grimes 	/*
263df8bae1dSRodney W. Grimes 	 * Remove proc from allproc queue and pidhash chain.
264df8bae1dSRodney W. Grimes 	 * Place onto zombproc.  Unlink from parent's child list.
265df8bae1dSRodney W. Grimes 	 */
266b75356e1SJeffrey Hsu 	LIST_REMOVE(p, p_list);
267b75356e1SJeffrey Hsu 	LIST_INSERT_HEAD(&zombproc, p, p_list);
268df8bae1dSRodney W. Grimes 	p->p_stat = SZOMB;
269df8bae1dSRodney W. Grimes 
270b75356e1SJeffrey Hsu 	LIST_REMOVE(p, p_hash);
271df8bae1dSRodney W. Grimes 
2722e3c8fcbSPoul-Henning Kamp 	q = LIST_FIRST(&p->p_children);
273b75356e1SJeffrey Hsu 	if (q)		/* only need this if any child is S_ZOMB */
274df8bae1dSRodney W. Grimes 		wakeup((caddr_t) initproc);
275b75356e1SJeffrey Hsu 	for (; q != 0; q = nq) {
2762e3c8fcbSPoul-Henning Kamp 		nq = LIST_NEXT(q, p_sibling);
277b75356e1SJeffrey Hsu 		LIST_REMOVE(q, p_sibling);
278b75356e1SJeffrey Hsu 		LIST_INSERT_HEAD(&initproc->p_children, q, p_sibling);
279df8bae1dSRodney W. Grimes 		q->p_pptr = initproc;
2804ac9ae70SJulian Elischer 		q->p_sigparent = SIGCHLD;
281df8bae1dSRodney W. Grimes 		/*
282df8bae1dSRodney W. Grimes 		 * Traced processes are killed
283df8bae1dSRodney W. Grimes 		 * since their existence means someone is screwing up.
284df8bae1dSRodney W. Grimes 		 */
285df8bae1dSRodney W. Grimes 		if (q->p_flag & P_TRACED) {
286df8bae1dSRodney W. Grimes 			q->p_flag &= ~P_TRACED;
287df8bae1dSRodney W. Grimes 			psignal(q, SIGKILL);
288df8bae1dSRodney W. Grimes 		}
289df8bae1dSRodney W. Grimes 	}
290df8bae1dSRodney W. Grimes 
291df8bae1dSRodney W. Grimes 	/*
292df8bae1dSRodney W. Grimes 	 * Save exit status and final rusage info, adding in child rusage
293df8bae1dSRodney W. Grimes 	 * info and self times.
294df8bae1dSRodney W. Grimes 	 */
295df8bae1dSRodney W. Grimes 	p->p_xstat = rv;
296df8bae1dSRodney W. Grimes 	*p->p_ru = p->p_stats->p_ru;
297df8bae1dSRodney W. Grimes 	calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
298df8bae1dSRodney W. Grimes 	ruadd(p->p_ru, &p->p_stats->p_cru);
299df8bae1dSRodney W. Grimes 
300df8bae1dSRodney W. Grimes 	/*
30156ce1a8dSBruce Evans 	 * Pretend that an mi_switch() to the next process occurs now.  We
30256ce1a8dSBruce Evans 	 * must set `switchtime' directly since we will call cpu_switch()
30356ce1a8dSBruce Evans 	 * directly.  Set it now so that the rest of the exit time gets
30456ce1a8dSBruce Evans 	 * counted somewhere if possible.
30556ce1a8dSBruce Evans 	 */
30656ce1a8dSBruce Evans 	microuptime(&switchtime);
30756ce1a8dSBruce Evans 	switchticks = ticks;
30856ce1a8dSBruce Evans 
30956ce1a8dSBruce Evans 	/*
310a274d19bSBrian Feldman 	 * notify interested parties of our demise.
311cb679c38SJonathan Lemon 	 */
312a274d19bSBrian Feldman 	KNOTE(&p->p_klist, NOTE_EXIT);
313cb679c38SJonathan Lemon 
314cb679c38SJonathan Lemon 	/*
315645682fdSLuoqi Chen 	 * Notify parent that we're gone.  If parent has the PS_NOCLDWAIT
316245f17d4SJoerg Wunsch 	 * flag set, notify process 1 instead (and hope it will handle
317245f17d4SJoerg Wunsch 	 * this situation).
318df8bae1dSRodney W. Grimes 	 */
319645682fdSLuoqi Chen 	if (p->p_pptr->p_procsig->ps_flag & PS_NOCLDWAIT) {
320245f17d4SJoerg Wunsch 		struct proc *pp = p->p_pptr;
321245f17d4SJoerg Wunsch 		proc_reparent(p, initproc);
322245f17d4SJoerg Wunsch 		/*
323245f17d4SJoerg Wunsch 		 * If this was the last child of our parent, notify
324245f17d4SJoerg Wunsch 		 * parent, so in case he was wait(2)ing, he will
325245f17d4SJoerg Wunsch 		 * continue.
326245f17d4SJoerg Wunsch 		 */
327245f17d4SJoerg Wunsch 		if (LIST_EMPTY(&pp->p_children))
328245f17d4SJoerg Wunsch 			wakeup((caddr_t)pp);
329245f17d4SJoerg Wunsch 	}
330245f17d4SJoerg Wunsch 
3316626c604SJulian Elischer 	if (p->p_sigparent && p->p_pptr != initproc) {
3326626c604SJulian Elischer 	        psignal(p->p_pptr, p->p_sigparent);
3336626c604SJulian Elischer 	} else {
3346626c604SJulian Elischer 	        psignal(p->p_pptr, SIGCHLD);
3356626c604SJulian Elischer 	}
33688c5ea45SJulian Elischer 
337df8bae1dSRodney W. Grimes 	wakeup((caddr_t)p->p_pptr);
338df8bae1dSRodney W. Grimes #if defined(tahoe)
339df8bae1dSRodney W. Grimes 	/* move this to cpu_exit */
340df8bae1dSRodney W. Grimes 	p->p_addr->u_pcb.pcb_savacc.faddr = (float *)NULL;
341df8bae1dSRodney W. Grimes #endif
342df8bae1dSRodney W. Grimes 	/*
343df8bae1dSRodney W. Grimes 	 * Clear curproc after we've done all operations
344df8bae1dSRodney W. Grimes 	 * that could block, and before tearing down the rest
345df8bae1dSRodney W. Grimes 	 * of the process state that might be used from clock, etc.
346df8bae1dSRodney W. Grimes 	 * Also, can't clear curproc while we're still runnable,
347df8bae1dSRodney W. Grimes 	 * as we're not on a run queue (we are current, just not
348df8bae1dSRodney W. Grimes 	 * a proper proc any longer!).
349df8bae1dSRodney W. Grimes 	 *
350df8bae1dSRodney W. Grimes 	 * Other substructures are freed from wait().
351df8bae1dSRodney W. Grimes 	 */
3525206bca1SLuoqi Chen 	SET_CURPROC(NULL);
353be6a1d14SDavid Greenman 	if (--p->p_limit->p_refcnt == 0) {
354df8bae1dSRodney W. Grimes 		FREE(p->p_limit, M_SUBPROC);
355be6a1d14SDavid Greenman 		p->p_limit = NULL;
356be6a1d14SDavid Greenman 	}
357df8bae1dSRodney W. Grimes 
358df8bae1dSRodney W. Grimes 	/*
359df8bae1dSRodney W. Grimes 	 * Finally, call machine-dependent code to release the remaining
360df8bae1dSRodney W. Grimes 	 * resources including address space, the kernel stack and pcb.
361df8bae1dSRodney W. Grimes 	 * The address space is released by "vmspace_free(p->p_vmspace)";
362df8bae1dSRodney W. Grimes 	 * This is machine-dependent, as we may have to change stacks
363df8bae1dSRodney W. Grimes 	 * or ensure that the current one isn't reallocated before we
36478d7e629SBruce Evans 	 * finish.  cpu_exit will end with a call to cpu_switch(), finishing
365df8bae1dSRodney W. Grimes 	 * our execution (pun intended).
366df8bae1dSRodney W. Grimes 	 */
367df8bae1dSRodney W. Grimes 	cpu_exit(p);
368df8bae1dSRodney W. Grimes }
369df8bae1dSRodney W. Grimes 
370b2f9e8b1SBruce Evans #ifdef COMPAT_43
37126f9a767SRodney W. Grimes int
372cb226aaaSPoul-Henning Kamp owait(p, uap)
373df8bae1dSRodney W. Grimes 	struct proc *p;
3745fdb8324SBruce Evans 	register struct owait_args /* {
3755fdb8324SBruce Evans 		int     dummy;
3765fdb8324SBruce Evans 	} */ *uap;
377df8bae1dSRodney W. Grimes {
37893c9414eSSteven Wallace 	struct wait_args w;
379df8bae1dSRodney W. Grimes 
38093c9414eSSteven Wallace 	w.options = 0;
38193c9414eSSteven Wallace 	w.rusage = NULL;
38293c9414eSSteven Wallace 	w.pid = WAIT_ANY;
38393c9414eSSteven Wallace 	w.status = NULL;
38401166e92SBruce Evans 	return (wait1(p, &w, 1));
385df8bae1dSRodney W. Grimes }
386b2f9e8b1SBruce Evans #endif /* COMPAT_43 */
387df8bae1dSRodney W. Grimes 
38826f9a767SRodney W. Grimes int
389cb226aaaSPoul-Henning Kamp wait4(p, uap)
390df8bae1dSRodney W. Grimes 	struct proc *p;
391df8bae1dSRodney W. Grimes 	struct wait_args *uap;
392df8bae1dSRodney W. Grimes {
3935fdb8324SBruce Evans 
39401166e92SBruce Evans 	return (wait1(p, uap, 0));
395df8bae1dSRodney W. Grimes }
396df8bae1dSRodney W. Grimes 
39793c9414eSSteven Wallace static int
39801166e92SBruce Evans wait1(q, uap, compat)
399df8bae1dSRodney W. Grimes 	register struct proc *q;
4005fdb8324SBruce Evans 	register struct wait_args /* {
4015fdb8324SBruce Evans 		int pid;
4025fdb8324SBruce Evans 		int *status;
4035fdb8324SBruce Evans 		int options;
4045fdb8324SBruce Evans 		struct rusage *rusage;
4055fdb8324SBruce Evans 	} */ *uap;
40693c9414eSSteven Wallace 	int compat;
407df8bae1dSRodney W. Grimes {
408df8bae1dSRodney W. Grimes 	register int nfound;
409df8bae1dSRodney W. Grimes 	register struct proc *p, *t;
410f23c6d68SSteven Wallace 	int status, error;
411df8bae1dSRodney W. Grimes 
412df8bae1dSRodney W. Grimes 	if (uap->pid == 0)
413df8bae1dSRodney W. Grimes 		uap->pid = -q->p_pgid;
4144ac9ae70SJulian Elischer 	if (uap->options &~ (WUNTRACED|WNOHANG|WLINUXCLONE))
415df8bae1dSRodney W. Grimes 		return (EINVAL);
416df8bae1dSRodney W. Grimes loop:
417df8bae1dSRodney W. Grimes 	nfound = 0;
4182e3c8fcbSPoul-Henning Kamp 	LIST_FOREACH(p, &q->p_children, p_sibling) {
419df8bae1dSRodney W. Grimes 		if (uap->pid != WAIT_ANY &&
420df8bae1dSRodney W. Grimes 		    p->p_pid != uap->pid && p->p_pgid != -uap->pid)
421df8bae1dSRodney W. Grimes 			continue;
4224ac9ae70SJulian Elischer 
4234ac9ae70SJulian Elischer 		/* This special case handles a kthread spawned by linux_clone
4244ac9ae70SJulian Elischer 		 * (see linux_misc.c).  The linux_wait4 and linux_waitpid functions
4254ac9ae70SJulian Elischer 		 * need to be able to distinguish between waiting on a process and
4264ac9ae70SJulian Elischer 		 * waiting on a thread.  It is a thread if p_sigparent is not SIGCHLD,
4274ac9ae70SJulian Elischer 		 * and the WLINUXCLONE option signifies we want to wait for threads
4284ac9ae70SJulian Elischer 		 * and not processes.
4294ac9ae70SJulian Elischer 		 */
4304ac9ae70SJulian Elischer 		if ((p->p_sigparent != SIGCHLD) ^ ((uap->options & WLINUXCLONE) != 0))
4314ac9ae70SJulian Elischer 			continue;
4324ac9ae70SJulian Elischer 
433df8bae1dSRodney W. Grimes 		nfound++;
434df8bae1dSRodney W. Grimes 		if (p->p_stat == SZOMB) {
4350d2afceeSDavid Greenman 			/* charge childs scheduling cpu usage to parent */
436d5c4431eSDavid Greenman 			if (curproc->p_pid != 1) {
437bdf42357SBruce Evans 				curproc->p_estcpu =
438bdf42357SBruce Evans 				    ESTCPULIM(curproc->p_estcpu + p->p_estcpu);
439d5c4431eSDavid Greenman 			}
4400d2afceeSDavid Greenman 
44101166e92SBruce Evans 			q->p_retval[0] = p->p_pid;
442b2f9e8b1SBruce Evans #ifdef COMPAT_43
44393c9414eSSteven Wallace 			if (compat)
44401166e92SBruce Evans 				q->p_retval[1] = p->p_xstat;
445df8bae1dSRodney W. Grimes 			else
446df8bae1dSRodney W. Grimes #endif
447df8bae1dSRodney W. Grimes 			if (uap->status) {
448df8bae1dSRodney W. Grimes 				status = p->p_xstat;	/* convert to int */
449bb56ec4aSPoul-Henning Kamp 				if ((error = copyout((caddr_t)&status,
450bb56ec4aSPoul-Henning Kamp 				    (caddr_t)uap->status, sizeof(status))))
451df8bae1dSRodney W. Grimes 					return (error);
452df8bae1dSRodney W. Grimes 			}
453df8bae1dSRodney W. Grimes 			if (uap->rusage && (error = copyout((caddr_t)p->p_ru,
454df8bae1dSRodney W. Grimes 			    (caddr_t)uap->rusage, sizeof (struct rusage))))
455df8bae1dSRodney W. Grimes 				return (error);
456df8bae1dSRodney W. Grimes 			/*
457df8bae1dSRodney W. Grimes 			 * If we got the child via a ptrace 'attach',
458df8bae1dSRodney W. Grimes 			 * we need to give it back to the old parent.
459df8bae1dSRodney W. Grimes 			 */
460df8bae1dSRodney W. Grimes 			if (p->p_oppid && (t = pfind(p->p_oppid))) {
461df8bae1dSRodney W. Grimes 				p->p_oppid = 0;
462df8bae1dSRodney W. Grimes 				proc_reparent(p, t);
463df8bae1dSRodney W. Grimes 				psignal(t, SIGCHLD);
464df8bae1dSRodney W. Grimes 				wakeup((caddr_t)t);
465df8bae1dSRodney W. Grimes 				return (0);
466df8bae1dSRodney W. Grimes 			}
467df8bae1dSRodney W. Grimes 			p->p_xstat = 0;
468df8bae1dSRodney W. Grimes 			ruadd(&q->p_stats->p_cru, p->p_ru);
469df8bae1dSRodney W. Grimes 			FREE(p->p_ru, M_ZOMBIE);
470be6a1d14SDavid Greenman 			p->p_ru = NULL;
471df8bae1dSRodney W. Grimes 
472df8bae1dSRodney W. Grimes 			/*
473df8bae1dSRodney W. Grimes 			 * Decrement the count of procs running with this uid.
474df8bae1dSRodney W. Grimes 			 */
475df8bae1dSRodney W. Grimes 			(void)chgproccnt(p->p_cred->p_ruid, -1);
476df8bae1dSRodney W. Grimes 
477df8bae1dSRodney W. Grimes 			/*
478bd7e5f99SJohn Dyson 			 * Release reference to text vnode
479bd7e5f99SJohn Dyson 			 */
480bd7e5f99SJohn Dyson 			if (p->p_textvp)
481bd7e5f99SJohn Dyson 				vrele(p->p_textvp);
482bd7e5f99SJohn Dyson 
483bd7e5f99SJohn Dyson 			/*
484df8bae1dSRodney W. Grimes 			 * Free up credentials.
485df8bae1dSRodney W. Grimes 			 */
486df8bae1dSRodney W. Grimes 			if (--p->p_cred->p_refcnt == 0) {
487da654d90SPoul-Henning Kamp 				crfree(p->p_ucred);
488df8bae1dSRodney W. Grimes 				FREE(p->p_cred, M_SUBPROC);
489be6a1d14SDavid Greenman 				p->p_cred = NULL;
490df8bae1dSRodney W. Grimes 			}
491df8bae1dSRodney W. Grimes 
492df8bae1dSRodney W. Grimes 			/*
49375c13541SPoul-Henning Kamp 			 * Destroy empty prisons
49475c13541SPoul-Henning Kamp 			 */
495c6dfea0eSMarcel Moolenaar 			if (p->p_prison && !--p->p_prison->pr_ref) {
496c6dfea0eSMarcel Moolenaar 				if (p->p_prison->pr_linux != NULL)
497c6dfea0eSMarcel Moolenaar 					FREE(p->p_prison->pr_linux, M_PRISON);
49875c13541SPoul-Henning Kamp 				FREE(p->p_prison, M_PRISON);
499c6dfea0eSMarcel Moolenaar 			}
50075c13541SPoul-Henning Kamp 
50175c13541SPoul-Henning Kamp 			/*
502b9df5231SPoul-Henning Kamp 			 * Remove unused arguments
503b9df5231SPoul-Henning Kamp 			 */
504b9df5231SPoul-Henning Kamp 			if (p->p_args && --p->p_args->ar_ref == 0)
505b9df5231SPoul-Henning Kamp 				FREE(p->p_args, M_PARGS);
506b9df5231SPoul-Henning Kamp 
507b9df5231SPoul-Henning Kamp 			/*
508df8bae1dSRodney W. Grimes 			 * Finally finished with old proc entry.
509df8bae1dSRodney W. Grimes 			 * Unlink it from its process group and free it.
510df8bae1dSRodney W. Grimes 			 */
511df8bae1dSRodney W. Grimes 			leavepgrp(p);
512b75356e1SJeffrey Hsu 			LIST_REMOVE(p, p_list);	/* off zombproc */
513b75356e1SJeffrey Hsu 			LIST_REMOVE(p, p_sibling);
514df8bae1dSRodney W. Grimes 
5156626c604SJulian Elischer 			if (--p->p_procsig->ps_refcnt == 0) {
516dc9c271aSJulian Elischer 				if (p->p_sigacts != &p->p_addr->u_sigacts)
517dc9c271aSJulian Elischer 					FREE(p->p_sigacts, M_SUBPROC);
518dc9c271aSJulian Elischer 			        FREE(p->p_procsig, M_SUBPROC);
5196626c604SJulian Elischer 				p->p_procsig = NULL;
5206626c604SJulian Elischer 			}
52188c5ea45SJulian Elischer 
522df8bae1dSRodney W. Grimes 			/*
523df8bae1dSRodney W. Grimes 			 * Give machine-dependent layer a chance
524df8bae1dSRodney W. Grimes 			 * to free anything that cpu_exit couldn't
525df8bae1dSRodney W. Grimes 			 * release while still running in process context.
526df8bae1dSRodney W. Grimes 			 */
527df8bae1dSRodney W. Grimes 			cpu_wait(p);
5282d8acc0fSJohn Dyson 			zfree(proc_zone, p);
529df8bae1dSRodney W. Grimes 			nprocs--;
530df8bae1dSRodney W. Grimes 			return (0);
531df8bae1dSRodney W. Grimes 		}
532df8bae1dSRodney W. Grimes 		if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
533df8bae1dSRodney W. Grimes 		    (p->p_flag & P_TRACED || uap->options & WUNTRACED)) {
534df8bae1dSRodney W. Grimes 			p->p_flag |= P_WAITED;
53501166e92SBruce Evans 			q->p_retval[0] = p->p_pid;
536b2f9e8b1SBruce Evans #ifdef COMPAT_43
53793c9414eSSteven Wallace 			if (compat) {
53801166e92SBruce Evans 				q->p_retval[1] = W_STOPCODE(p->p_xstat);
539df8bae1dSRodney W. Grimes 				error = 0;
540df8bae1dSRodney W. Grimes 			} else
541df8bae1dSRodney W. Grimes #endif
542df8bae1dSRodney W. Grimes 			if (uap->status) {
543df8bae1dSRodney W. Grimes 				status = W_STOPCODE(p->p_xstat);
544df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&status,
545df8bae1dSRodney W. Grimes 					(caddr_t)uap->status, sizeof(status));
546df8bae1dSRodney W. Grimes 			} else
547df8bae1dSRodney W. Grimes 				error = 0;
548df8bae1dSRodney W. Grimes 			return (error);
549df8bae1dSRodney W. Grimes 		}
550df8bae1dSRodney W. Grimes 	}
551df8bae1dSRodney W. Grimes 	if (nfound == 0)
552df8bae1dSRodney W. Grimes 		return (ECHILD);
553df8bae1dSRodney W. Grimes 	if (uap->options & WNOHANG) {
55401166e92SBruce Evans 		q->p_retval[0] = 0;
555df8bae1dSRodney W. Grimes 		return (0);
556df8bae1dSRodney W. Grimes 	}
557bb56ec4aSPoul-Henning Kamp 	if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)))
558df8bae1dSRodney W. Grimes 		return (error);
559df8bae1dSRodney W. Grimes 	goto loop;
560df8bae1dSRodney W. Grimes }
561df8bae1dSRodney W. Grimes 
562df8bae1dSRodney W. Grimes /*
563df8bae1dSRodney W. Grimes  * make process 'parent' the new parent of process 'child'.
564df8bae1dSRodney W. Grimes  */
565df8bae1dSRodney W. Grimes void
566df8bae1dSRodney W. Grimes proc_reparent(child, parent)
567df8bae1dSRodney W. Grimes 	register struct proc *child;
568df8bae1dSRodney W. Grimes 	register struct proc *parent;
569df8bae1dSRodney W. Grimes {
570df8bae1dSRodney W. Grimes 
571df8bae1dSRodney W. Grimes 	if (child->p_pptr == parent)
572df8bae1dSRodney W. Grimes 		return;
573df8bae1dSRodney W. Grimes 
574b75356e1SJeffrey Hsu 	LIST_REMOVE(child, p_sibling);
575b75356e1SJeffrey Hsu 	LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
576df8bae1dSRodney W. Grimes 	child->p_pptr = parent;
577df8bae1dSRodney W. Grimes }
578fed06968SJulian Elischer 
579e0d898b4SJulian Elischer /*
580e0d898b4SJulian Elischer  * The next two functions are to handle adding/deleting items on the
581fed06968SJulian Elischer  * exit callout list
582e0d898b4SJulian Elischer  *
583e0d898b4SJulian Elischer  * at_exit():
584e0d898b4SJulian Elischer  * Take the arguments given and put them onto the exit callout list,
585fed06968SJulian Elischer  * However first make sure that it's not already there.
586fed06968SJulian Elischer  * returns 0 on success.
587fed06968SJulian Elischer  */
58893efcae8SPoul-Henning Kamp 
589fed06968SJulian Elischer int
590eb776aeaSBruce Evans at_exit(function)
591eb776aeaSBruce Evans 	exitlist_fn function;
592fed06968SJulian Elischer {
59393efcae8SPoul-Henning Kamp 	struct exitlist *ep;
594e0d898b4SJulian Elischer 
59593efcae8SPoul-Henning Kamp #ifdef INVARIANTS
596e0d898b4SJulian Elischer 	/* Be noisy if the programmer has lost track of things */
597e0d898b4SJulian Elischer 	if (rm_at_exit(function))
59893efcae8SPoul-Henning Kamp 		printf("WARNING: exit callout entry (%p) already present\n",
59993efcae8SPoul-Henning Kamp 		    function);
60093efcae8SPoul-Henning Kamp #endif
60193efcae8SPoul-Henning Kamp 	ep = malloc(sizeof(*ep), M_ATEXIT, M_NOWAIT);
602e0d898b4SJulian Elischer 	if (ep == NULL)
603e0d898b4SJulian Elischer 		return (ENOMEM);
604fed06968SJulian Elischer 	ep->function = function;
60593efcae8SPoul-Henning Kamp 	TAILQ_INSERT_TAIL(&exit_list, ep, next);
606e0d898b4SJulian Elischer 	return (0);
607fed06968SJulian Elischer }
60893efcae8SPoul-Henning Kamp 
609fed06968SJulian Elischer /*
61093efcae8SPoul-Henning Kamp  * Scan the exit callout list for the given item and remove it.
61193efcae8SPoul-Henning Kamp  * Returns the number of items removed (0 or 1)
612fed06968SJulian Elischer  */
613fed06968SJulian Elischer int
614eb776aeaSBruce Evans rm_at_exit(function)
615eb776aeaSBruce Evans 	exitlist_fn function;
616fed06968SJulian Elischer {
61793efcae8SPoul-Henning Kamp 	struct exitlist *ep;
618fed06968SJulian Elischer 
61993efcae8SPoul-Henning Kamp 	TAILQ_FOREACH(ep, &exit_list, next) {
620fed06968SJulian Elischer 		if (ep->function == function) {
62193efcae8SPoul-Henning Kamp 			TAILQ_REMOVE(&exit_list, ep, next);
62293efcae8SPoul-Henning Kamp 			free(ep, M_ATEXIT);
62393efcae8SPoul-Henning Kamp 			return(1);
624fed06968SJulian Elischer 		}
625fed06968SJulian Elischer 	}
62693efcae8SPoul-Henning Kamp 	return (0);
627fed06968SJulian Elischer }
628dc9c271aSJulian Elischer 
629dc9c271aSJulian Elischer void check_sigacts (void)
630dc9c271aSJulian Elischer {
631dc9c271aSJulian Elischer 	struct proc *p = curproc;
632dc9c271aSJulian Elischer 	struct sigacts *pss;
633dc9c271aSJulian Elischer 	int s;
634dc9c271aSJulian Elischer 
635dc9c271aSJulian Elischer 	if (p->p_procsig->ps_refcnt == 1 &&
636dc9c271aSJulian Elischer 	    p->p_sigacts != &p->p_addr->u_sigacts) {
637dc9c271aSJulian Elischer 		pss = p->p_sigacts;
638dc9c271aSJulian Elischer 		s = splhigh();
639dc9c271aSJulian Elischer 		p->p_addr->u_sigacts = *pss;
640dc9c271aSJulian Elischer 		p->p_sigacts = &p->p_addr->u_sigacts;
641dc9c271aSJulian Elischer 		splx(s);
642dc9c271aSJulian Elischer 		FREE(pss, M_SUBPROC);
643dc9c271aSJulian Elischer 	}
644dc9c271aSJulian Elischer }
64588c5ea45SJulian Elischer 
646