xref: /freebsd/sys/kern/kern_exit.c (revision 4ac9ae70836f53e6d351857aedc400151dc62f86)
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
394ac9ae70SJulian Elischer  * $Id: kern_exit.c,v 1.75 1999/02/19 14:25:34 luoqi Exp $
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>
63780dc5a8SPeter Wemm 
64df8bae1dSRodney W. Grimes #ifdef COMPAT_43
65df8bae1dSRodney W. Grimes #include <machine/reg.h>
66df8bae1dSRodney W. Grimes #include <machine/psl.h>
67df8bae1dSRodney W. Grimes #endif
68b1037dcdSBruce Evans #include <machine/limits.h>	/* for UCHAR_MAX = typeof(p_priority)_MAX */
69df8bae1dSRodney W. Grimes 
70df8bae1dSRodney W. Grimes #include <vm/vm.h>
71efeaf95aSDavid Greenman #include <vm/vm_param.h>
72996c772fSJohn Dyson #include <sys/lock.h>
73efeaf95aSDavid Greenman #include <vm/pmap.h>
74efeaf95aSDavid Greenman #include <vm/vm_map.h>
752d8acc0fSJohn Dyson #include <vm/vm_zone.h>
76dc9c271aSJulian Elischer #include <sys/user.h>
77df8bae1dSRodney W. Grimes 
78ba198b1cSMark Newton /* Required to be non-static for SysVR4 emulator */
7969a6f20bSMark Newton MALLOC_DEFINE(M_ZOMBIE, "zombie", "zombie proc status");
8055166637SPoul-Henning Kamp 
8101166e92SBruce Evans static int wait1 __P((struct proc *, struct wait_args *, int));
825fdb8324SBruce Evans 
83df8bae1dSRodney W. Grimes /*
84fed06968SJulian Elischer  * callout list for things to do at exit time
85fed06968SJulian Elischer  */
86fed06968SJulian Elischer typedef struct exit_list_element {
87fed06968SJulian Elischer 	struct exit_list_element *next;
88fed06968SJulian Elischer 	exitlist_fn function;
89fed06968SJulian Elischer } *ele_p;
90fed06968SJulian Elischer 
91fed06968SJulian Elischer static ele_p exit_list;
92fed06968SJulian Elischer 
93fed06968SJulian Elischer /*
94df8bae1dSRodney W. Grimes  * exit --
95df8bae1dSRodney W. Grimes  *	Death of process.
96df8bae1dSRodney W. Grimes  */
97fc0b1dbfSBruce Evans void
98cb226aaaSPoul-Henning Kamp exit(p, uap)
99df8bae1dSRodney W. Grimes 	struct proc *p;
1005fdb8324SBruce Evans 	struct rexit_args /* {
1015fdb8324SBruce Evans 		int	rval;
1025fdb8324SBruce Evans 	} */ *uap;
103df8bae1dSRodney W. Grimes {
104df8bae1dSRodney W. Grimes 
105df8bae1dSRodney W. Grimes 	exit1(p, W_EXITCODE(uap->rval, 0));
106df8bae1dSRodney W. Grimes 	/* NOTREACHED */
107df8bae1dSRodney W. Grimes }
108df8bae1dSRodney W. Grimes 
109df8bae1dSRodney W. Grimes /*
110df8bae1dSRodney W. Grimes  * Exit: deallocate address space and other resources, change proc state
111df8bae1dSRodney W. Grimes  * to zombie, and unlink proc from allproc and parent's lists.  Save exit
112df8bae1dSRodney W. Grimes  * status and rusage for wait().  Check for child processes and orphan them.
113df8bae1dSRodney W. Grimes  */
114fc0b1dbfSBruce Evans void
115df8bae1dSRodney W. Grimes exit1(p, rv)
116df8bae1dSRodney W. Grimes 	register struct proc *p;
117df8bae1dSRodney W. Grimes 	int rv;
118df8bae1dSRodney W. Grimes {
119df8bae1dSRodney W. Grimes 	register struct proc *q, *nq;
120df8bae1dSRodney W. Grimes 	register struct vmspace *vm;
121fed06968SJulian Elischer 	ele_p ep = exit_list;
122df8bae1dSRodney W. Grimes 
1235f7bd355SPoul-Henning Kamp 	if (p->p_pid == 1) {
1245f7bd355SPoul-Henning Kamp 		printf("init died (signal %d, exit %d)\n",
125df8bae1dSRodney W. Grimes 		    WTERMSIG(rv), WEXITSTATUS(rv));
1265f7bd355SPoul-Henning Kamp 		panic("Going nowhere without my init!");
1275f7bd355SPoul-Henning Kamp 	}
1282c1011f7SJohn Dyson 
1292244ea07SJohn Dyson 	aio_proc_rundown(p);
1302244ea07SJohn Dyson 
1312c1011f7SJohn Dyson 	/* are we a task leader? */
1322c1011f7SJohn Dyson 	if(p == p->p_leader) {
1332c1011f7SJohn Dyson         	struct kill_args killArgs;
1342c1011f7SJohn Dyson 		killArgs.signum = SIGKILL;
1352c1011f7SJohn Dyson 		q = p->p_peers;
1362c1011f7SJohn Dyson 		while(q) {
1372c1011f7SJohn Dyson 			killArgs.pid = q->p_pid;
1382c1011f7SJohn Dyson 			/*
1392c1011f7SJohn Dyson 		         * The interface for kill is better
1402c1011f7SJohn Dyson 			 * than the internal signal
1412c1011f7SJohn Dyson 			 */
142cb226aaaSPoul-Henning Kamp 			kill(p, &killArgs);
1432c1011f7SJohn Dyson 			nq = q;
1442c1011f7SJohn Dyson 			q = q->p_peers;
1452c1011f7SJohn Dyson 			/*
1462c1011f7SJohn Dyson 			 * orphan the threads so we don't mess up
1472c1011f7SJohn Dyson 			 * when they call exit
1482c1011f7SJohn Dyson 			 */
1492c1011f7SJohn Dyson 			nq->p_peers = 0;
1502c1011f7SJohn Dyson 			nq->p_leader = nq;
1512c1011f7SJohn Dyson 		}
1522c1011f7SJohn Dyson 
1532c1011f7SJohn Dyson 	/* otherwise are we a peer? */
1542c1011f7SJohn Dyson 	} else if(p->p_peers) {
1552c1011f7SJohn Dyson 		q = p->p_leader;
1562c1011f7SJohn Dyson 		while(q->p_peers != p)
1572c1011f7SJohn Dyson 			q = q->p_peers;
1582c1011f7SJohn Dyson 		q->p_peers = p->p_peers;
1592c1011f7SJohn Dyson 	}
1602c1011f7SJohn Dyson 
161df8bae1dSRodney W. Grimes #ifdef PGINPROF
162df8bae1dSRodney W. Grimes 	vmsizmon();
163df8bae1dSRodney W. Grimes #endif
1642a024a2bSSean Eric Fagan 	STOPEVENT(p, S_EXIT, rv);
1652a024a2bSSean Eric Fagan 
166fed06968SJulian Elischer 	/*
167e0d898b4SJulian Elischer 	 * Check if any LKMs need anything done at process exit.
168fed06968SJulian Elischer 	 * e.g. SYSV IPC stuff
169fed06968SJulian Elischer 	 * XXX what if one of these generates an error?
170fed06968SJulian Elischer 	 */
171fed06968SJulian Elischer 	while (ep) {
172fed06968SJulian Elischer 		(*ep->function)(p);
173fed06968SJulian Elischer 		ep = ep->next;
174fed06968SJulian Elischer 	}
175fed06968SJulian Elischer 
176df8bae1dSRodney W. Grimes 	if (p->p_flag & P_PROFIL)
177df8bae1dSRodney W. Grimes 		stopprofclock(p);
178df8bae1dSRodney W. Grimes 	MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage),
179df8bae1dSRodney W. Grimes 		M_ZOMBIE, M_WAITOK);
180df8bae1dSRodney W. Grimes 	/*
181df8bae1dSRodney W. Grimes 	 * If parent is waiting for us to exit or exec,
182df8bae1dSRodney W. Grimes 	 * P_PPWAIT is set; we will wakeup the parent below.
183df8bae1dSRodney W. Grimes 	 */
184df8bae1dSRodney W. Grimes 	p->p_flag &= ~(P_TRACED | P_PPWAIT);
185df8bae1dSRodney W. Grimes 	p->p_flag |= P_WEXIT;
186df8bae1dSRodney W. Grimes 	p->p_siglist = 0;
1874cf41af3SPoul-Henning Kamp 	if (timevalisset(&p->p_realtimer.it_value))
188ab36c067SJustin T. Gibbs 		untimeout(realitexpire, (caddr_t)p, p->p_ithandle);
189df8bae1dSRodney W. Grimes 
190df8bae1dSRodney W. Grimes 	/*
191831d27a9SDon Lewis 	 * Reset any sigio structures pointing to us as a result of
192831d27a9SDon Lewis 	 * F_SETOWN with our pid.
193831d27a9SDon Lewis 	 */
194831d27a9SDon Lewis 	funsetownlst(&p->p_sigiolst);
195831d27a9SDon Lewis 
196831d27a9SDon Lewis 	/*
197df8bae1dSRodney W. Grimes 	 * Close open files and release open-file table.
198df8bae1dSRodney W. Grimes 	 * This may block!
199df8bae1dSRodney W. Grimes 	 */
200df8bae1dSRodney W. Grimes 	fdfree(p);
201df8bae1dSRodney W. Grimes 
20281090119SPeter Wemm 	/*
20381090119SPeter Wemm 	 * XXX Shutdown SYSV semaphores
20481090119SPeter Wemm 	 */
205a353d785SJoerg Wunsch 	semexit(p);
206a353d785SJoerg Wunsch 
207df8bae1dSRodney W. Grimes 	/* The next two chunks should probably be moved to vmspace_exit. */
208df8bae1dSRodney W. Grimes 	vm = p->p_vmspace;
209df8bae1dSRodney W. Grimes 	/*
210df8bae1dSRodney W. Grimes 	 * Release user portion of address space.
211df8bae1dSRodney W. Grimes 	 * This releases references to vnodes,
212df8bae1dSRodney W. Grimes 	 * which could cause I/O if the file has been unlinked.
213df8bae1dSRodney W. Grimes 	 * Need to do this early enough that we can still sleep.
214df8bae1dSRodney W. Grimes 	 * Can't free the entire vmspace as the kernel stack
215df8bae1dSRodney W. Grimes 	 * may be mapped within that space also.
216df8bae1dSRodney W. Grimes 	 */
2179d3fbbb5SJohn Dyson 	if (vm->vm_refcnt == 1) {
218a2a1c95cSPeter Wemm 		if (vm->vm_shm)
219a2a1c95cSPeter Wemm 			shmexit(p);
220b1028ad1SLuoqi Chen 		pmap_remove_pages(vmspace_pmap(vm), VM_MIN_ADDRESS,
2219d3fbbb5SJohn Dyson 		    VM_MAXUSER_ADDRESS);
22267bf6868SJohn Dyson 		(void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS,
22367bf6868SJohn Dyson 		    VM_MAXUSER_ADDRESS);
2249d3fbbb5SJohn Dyson 	}
225df8bae1dSRodney W. Grimes 
226df8bae1dSRodney W. Grimes 	if (SESS_LEADER(p)) {
227df8bae1dSRodney W. Grimes 		register struct session *sp = p->p_session;
228df8bae1dSRodney W. Grimes 
229df8bae1dSRodney W. Grimes 		if (sp->s_ttyvp) {
230df8bae1dSRodney W. Grimes 			/*
231df8bae1dSRodney W. Grimes 			 * Controlling process.
232df8bae1dSRodney W. Grimes 			 * Signal foreground pgrp,
233df8bae1dSRodney W. Grimes 			 * drain controlling terminal
234df8bae1dSRodney W. Grimes 			 * and revoke access to controlling terminal.
235df8bae1dSRodney W. Grimes 			 */
236dd45d8adSJulian Elischer 			if (sp->s_ttyp && (sp->s_ttyp->t_session == sp)) {
237df8bae1dSRodney W. Grimes 				if (sp->s_ttyp->t_pgrp)
238df8bae1dSRodney W. Grimes 					pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
239df8bae1dSRodney W. Grimes 				(void) ttywait(sp->s_ttyp);
240df8bae1dSRodney W. Grimes 				/*
241df8bae1dSRodney W. Grimes 				 * The tty could have been revoked
242df8bae1dSRodney W. Grimes 				 * if we blocked.
243df8bae1dSRodney W. Grimes 				 */
244df8bae1dSRodney W. Grimes 				if (sp->s_ttyvp)
245996c772fSJohn Dyson 					VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
246df8bae1dSRodney W. Grimes 			}
247df8bae1dSRodney W. Grimes 			if (sp->s_ttyvp)
248df8bae1dSRodney W. Grimes 				vrele(sp->s_ttyvp);
249df8bae1dSRodney W. Grimes 			sp->s_ttyvp = NULL;
250df8bae1dSRodney W. Grimes 			/*
251df8bae1dSRodney W. Grimes 			 * s_ttyp is not zero'd; we use this to indicate
252df8bae1dSRodney W. Grimes 			 * that the session once had a controlling terminal.
253df8bae1dSRodney W. Grimes 			 * (for logging and informational purposes)
254df8bae1dSRodney W. Grimes 			 */
255df8bae1dSRodney W. Grimes 		}
256df8bae1dSRodney W. Grimes 		sp->s_leader = NULL;
257df8bae1dSRodney W. Grimes 	}
258df8bae1dSRodney W. Grimes 	fixjobc(p, p->p_pgrp, 0);
259df8bae1dSRodney W. Grimes 	(void)acct_process(p);
260df8bae1dSRodney W. Grimes #ifdef KTRACE
261df8bae1dSRodney W. Grimes 	/*
262df8bae1dSRodney W. Grimes 	 * release trace file
263df8bae1dSRodney W. Grimes 	 */
264df8bae1dSRodney W. Grimes 	p->p_traceflag = 0;	/* don't trace the vrele() */
265df8bae1dSRodney W. Grimes 	if (p->p_tracep)
266df8bae1dSRodney W. Grimes 		vrele(p->p_tracep);
267df8bae1dSRodney W. Grimes #endif
268df8bae1dSRodney W. Grimes 	/*
269df8bae1dSRodney W. Grimes 	 * Remove proc from allproc queue and pidhash chain.
270df8bae1dSRodney W. Grimes 	 * Place onto zombproc.  Unlink from parent's child list.
271df8bae1dSRodney W. Grimes 	 */
272b75356e1SJeffrey Hsu 	LIST_REMOVE(p, p_list);
273b75356e1SJeffrey Hsu 	LIST_INSERT_HEAD(&zombproc, p, p_list);
274df8bae1dSRodney W. Grimes 	p->p_stat = SZOMB;
275df8bae1dSRodney W. Grimes 
276b75356e1SJeffrey Hsu 	LIST_REMOVE(p, p_hash);
277df8bae1dSRodney W. Grimes 
278b75356e1SJeffrey Hsu 	q = p->p_children.lh_first;
279b75356e1SJeffrey Hsu 	if (q)		/* only need this if any child is S_ZOMB */
280df8bae1dSRodney W. Grimes 		wakeup((caddr_t) initproc);
281b75356e1SJeffrey Hsu 	for (; q != 0; q = nq) {
282b75356e1SJeffrey Hsu 		nq = q->p_sibling.le_next;
283b75356e1SJeffrey Hsu 		LIST_REMOVE(q, p_sibling);
284b75356e1SJeffrey Hsu 		LIST_INSERT_HEAD(&initproc->p_children, q, p_sibling);
285df8bae1dSRodney W. Grimes 		q->p_pptr = initproc;
2864ac9ae70SJulian Elischer 		q->p_sigparent = SIGCHLD;
287df8bae1dSRodney W. Grimes 		/*
288df8bae1dSRodney W. Grimes 		 * Traced processes are killed
289df8bae1dSRodney W. Grimes 		 * since their existence means someone is screwing up.
290df8bae1dSRodney W. Grimes 		 */
291df8bae1dSRodney W. Grimes 		if (q->p_flag & P_TRACED) {
292df8bae1dSRodney W. Grimes 			q->p_flag &= ~P_TRACED;
293df8bae1dSRodney W. Grimes 			psignal(q, SIGKILL);
294df8bae1dSRodney W. Grimes 		}
295df8bae1dSRodney W. Grimes 	}
296df8bae1dSRodney W. Grimes 
297df8bae1dSRodney W. Grimes 	/*
298df8bae1dSRodney W. Grimes 	 * Save exit status and final rusage info, adding in child rusage
299df8bae1dSRodney W. Grimes 	 * info and self times.
300df8bae1dSRodney W. Grimes 	 */
301df8bae1dSRodney W. Grimes 	p->p_xstat = rv;
302df8bae1dSRodney W. Grimes 	*p->p_ru = p->p_stats->p_ru;
303df8bae1dSRodney W. Grimes 	calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
304df8bae1dSRodney W. Grimes 	ruadd(p->p_ru, &p->p_stats->p_cru);
305df8bae1dSRodney W. Grimes 
306df8bae1dSRodney W. Grimes 	/*
307245f17d4SJoerg Wunsch 	 * Notify parent that we're gone.  If parent has the P_NOCLDWAIT
308245f17d4SJoerg Wunsch 	 * flag set, notify process 1 instead (and hope it will handle
309245f17d4SJoerg Wunsch 	 * this situation).
310df8bae1dSRodney W. Grimes 	 */
3116626c604SJulian Elischer 	if (p->p_pptr->p_procsig->ps_flag & P_NOCLDWAIT) {
312245f17d4SJoerg Wunsch 		struct proc *pp = p->p_pptr;
313245f17d4SJoerg Wunsch 		proc_reparent(p, initproc);
314245f17d4SJoerg Wunsch 		/*
315245f17d4SJoerg Wunsch 		 * If this was the last child of our parent, notify
316245f17d4SJoerg Wunsch 		 * parent, so in case he was wait(2)ing, he will
317245f17d4SJoerg Wunsch 		 * continue.
318245f17d4SJoerg Wunsch 		 */
319245f17d4SJoerg Wunsch 		if (LIST_EMPTY(&pp->p_children))
320245f17d4SJoerg Wunsch 			wakeup((caddr_t)pp);
321245f17d4SJoerg Wunsch 	}
322245f17d4SJoerg Wunsch 
3236626c604SJulian Elischer 	if (p->p_sigparent && p->p_pptr != initproc) {
3246626c604SJulian Elischer 	        psignal(p->p_pptr, p->p_sigparent);
3256626c604SJulian Elischer 	} else {
3266626c604SJulian Elischer 	        psignal(p->p_pptr, SIGCHLD);
3276626c604SJulian Elischer 	}
32888c5ea45SJulian Elischer 
329df8bae1dSRodney W. Grimes 	wakeup((caddr_t)p->p_pptr);
330df8bae1dSRodney W. Grimes #if defined(tahoe)
331df8bae1dSRodney W. Grimes 	/* move this to cpu_exit */
332df8bae1dSRodney W. Grimes 	p->p_addr->u_pcb.pcb_savacc.faddr = (float *)NULL;
333df8bae1dSRodney W. Grimes #endif
334df8bae1dSRodney W. Grimes 	/*
335df8bae1dSRodney W. Grimes 	 * Clear curproc after we've done all operations
336df8bae1dSRodney W. Grimes 	 * that could block, and before tearing down the rest
337df8bae1dSRodney W. Grimes 	 * of the process state that might be used from clock, etc.
338df8bae1dSRodney W. Grimes 	 * Also, can't clear curproc while we're still runnable,
339df8bae1dSRodney W. Grimes 	 * as we're not on a run queue (we are current, just not
340df8bae1dSRodney W. Grimes 	 * a proper proc any longer!).
341df8bae1dSRodney W. Grimes 	 *
342df8bae1dSRodney W. Grimes 	 * Other substructures are freed from wait().
343df8bae1dSRodney W. Grimes 	 */
344df8bae1dSRodney W. Grimes 	curproc = NULL;
345be6a1d14SDavid Greenman 	if (--p->p_limit->p_refcnt == 0) {
346df8bae1dSRodney W. Grimes 		FREE(p->p_limit, M_SUBPROC);
347be6a1d14SDavid Greenman 		p->p_limit = NULL;
348be6a1d14SDavid Greenman 	}
349df8bae1dSRodney W. Grimes 
350df8bae1dSRodney W. Grimes 	/*
351df8bae1dSRodney W. Grimes 	 * Finally, call machine-dependent code to release the remaining
352df8bae1dSRodney W. Grimes 	 * resources including address space, the kernel stack and pcb.
353df8bae1dSRodney W. Grimes 	 * The address space is released by "vmspace_free(p->p_vmspace)";
354df8bae1dSRodney W. Grimes 	 * This is machine-dependent, as we may have to change stacks
355df8bae1dSRodney W. Grimes 	 * or ensure that the current one isn't reallocated before we
35678d7e629SBruce Evans 	 * finish.  cpu_exit will end with a call to cpu_switch(), finishing
357df8bae1dSRodney W. Grimes 	 * our execution (pun intended).
358df8bae1dSRodney W. Grimes 	 */
359df8bae1dSRodney W. Grimes 	cpu_exit(p);
360df8bae1dSRodney W. Grimes }
361df8bae1dSRodney W. Grimes 
362b2f9e8b1SBruce Evans #ifdef COMPAT_43
363df8bae1dSRodney W. Grimes #if defined(hp300) || defined(luna68k)
364df8bae1dSRodney W. Grimes #include <machine/frame.h>
365df8bae1dSRodney W. Grimes #define GETPS(rp)	((struct frame *)(rp))->f_sr
366df8bae1dSRodney W. Grimes #else
367df8bae1dSRodney W. Grimes #define GETPS(rp)	(rp)[PS]
368df8bae1dSRodney W. Grimes #endif
369df8bae1dSRodney W. Grimes 
37026f9a767SRodney W. Grimes int
371cb226aaaSPoul-Henning Kamp owait(p, uap)
372df8bae1dSRodney W. Grimes 	struct proc *p;
3735fdb8324SBruce Evans 	register struct owait_args /* {
3745fdb8324SBruce Evans 		int     dummy;
3755fdb8324SBruce Evans 	} */ *uap;
376df8bae1dSRodney W. Grimes {
37793c9414eSSteven Wallace 	struct wait_args w;
378df8bae1dSRodney W. Grimes 
379df8bae1dSRodney W. Grimes #ifdef PSL_ALLCC
380df8bae1dSRodney W. Grimes 	if ((GETPS(p->p_md.md_regs) & PSL_ALLCC) != PSL_ALLCC) {
38193c9414eSSteven Wallace 		w.options = 0;
38293c9414eSSteven Wallace 		w.rusage = NULL;
383df8bae1dSRodney W. Grimes 	} else {
38493c9414eSSteven Wallace 		w.options = p->p_md.md_regs[R0];
38593c9414eSSteven Wallace 		w.rusage = (struct rusage *)p->p_md.md_regs[R1];
386df8bae1dSRodney W. Grimes 	}
387df8bae1dSRodney W. Grimes #else
38893c9414eSSteven Wallace 	w.options = 0;
38993c9414eSSteven Wallace 	w.rusage = NULL;
390df8bae1dSRodney W. Grimes #endif
39193c9414eSSteven Wallace 	w.pid = WAIT_ANY;
39293c9414eSSteven Wallace 	w.status = NULL;
39301166e92SBruce Evans 	return (wait1(p, &w, 1));
394df8bae1dSRodney W. Grimes }
395b2f9e8b1SBruce Evans #endif /* COMPAT_43 */
396df8bae1dSRodney W. Grimes 
39726f9a767SRodney W. Grimes int
398cb226aaaSPoul-Henning Kamp wait4(p, uap)
399df8bae1dSRodney W. Grimes 	struct proc *p;
400df8bae1dSRodney W. Grimes 	struct wait_args *uap;
401df8bae1dSRodney W. Grimes {
4025fdb8324SBruce Evans 
40301166e92SBruce Evans 	return (wait1(p, uap, 0));
404df8bae1dSRodney W. Grimes }
405df8bae1dSRodney W. Grimes 
40693c9414eSSteven Wallace static int
40701166e92SBruce Evans wait1(q, uap, compat)
408df8bae1dSRodney W. Grimes 	register struct proc *q;
4095fdb8324SBruce Evans 	register struct wait_args /* {
4105fdb8324SBruce Evans 		int pid;
4115fdb8324SBruce Evans 		int *status;
4125fdb8324SBruce Evans 		int options;
4135fdb8324SBruce Evans 		struct rusage *rusage;
4145fdb8324SBruce Evans 	} */ *uap;
41593c9414eSSteven Wallace 	int compat;
416df8bae1dSRodney W. Grimes {
417df8bae1dSRodney W. Grimes 	register int nfound;
418df8bae1dSRodney W. Grimes 	register struct proc *p, *t;
419f23c6d68SSteven Wallace 	int status, error;
420df8bae1dSRodney W. Grimes 
421df8bae1dSRodney W. Grimes 	if (uap->pid == 0)
422df8bae1dSRodney W. Grimes 		uap->pid = -q->p_pgid;
4234ac9ae70SJulian Elischer 	if (uap->options &~ (WUNTRACED|WNOHANG|WLINUXCLONE))
424df8bae1dSRodney W. Grimes 		return (EINVAL);
425df8bae1dSRodney W. Grimes loop:
426df8bae1dSRodney W. Grimes 	nfound = 0;
427b75356e1SJeffrey Hsu 	for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
428df8bae1dSRodney W. Grimes 		if (uap->pid != WAIT_ANY &&
429df8bae1dSRodney W. Grimes 		    p->p_pid != uap->pid && p->p_pgid != -uap->pid)
430df8bae1dSRodney W. Grimes 			continue;
4314ac9ae70SJulian Elischer 
4324ac9ae70SJulian Elischer 		/* This special case handles a kthread spawned by linux_clone
4334ac9ae70SJulian Elischer 		 * (see linux_misc.c).  The linux_wait4 and linux_waitpid functions
4344ac9ae70SJulian Elischer 		 * need to be able to distinguish between waiting on a process and
4354ac9ae70SJulian Elischer 		 * waiting on a thread.  It is a thread if p_sigparent is not SIGCHLD,
4364ac9ae70SJulian Elischer 		 * and the WLINUXCLONE option signifies we want to wait for threads
4374ac9ae70SJulian Elischer 		 * and not processes.
4384ac9ae70SJulian Elischer 		 */
4394ac9ae70SJulian Elischer 		if ((p->p_sigparent != SIGCHLD) ^ ((uap->options & WLINUXCLONE) != 0))
4404ac9ae70SJulian Elischer 			continue;
4414ac9ae70SJulian Elischer 
442df8bae1dSRodney W. Grimes 		nfound++;
443df8bae1dSRodney W. Grimes 		if (p->p_stat == SZOMB) {
4440d2afceeSDavid Greenman 			/* charge childs scheduling cpu usage to parent */
445d5c4431eSDavid Greenman 			if (curproc->p_pid != 1) {
446d5c4431eSDavid Greenman 				curproc->p_estcpu = min(curproc->p_estcpu +
447d5c4431eSDavid Greenman 				    p->p_estcpu, UCHAR_MAX);
448d5c4431eSDavid Greenman 			}
4490d2afceeSDavid Greenman 
45001166e92SBruce Evans 			q->p_retval[0] = p->p_pid;
451b2f9e8b1SBruce Evans #ifdef COMPAT_43
45293c9414eSSteven Wallace 			if (compat)
45301166e92SBruce Evans 				q->p_retval[1] = p->p_xstat;
454df8bae1dSRodney W. Grimes 			else
455df8bae1dSRodney W. Grimes #endif
456df8bae1dSRodney W. Grimes 			if (uap->status) {
457df8bae1dSRodney W. Grimes 				status = p->p_xstat;	/* convert to int */
458bb56ec4aSPoul-Henning Kamp 				if ((error = copyout((caddr_t)&status,
459bb56ec4aSPoul-Henning Kamp 				    (caddr_t)uap->status, sizeof(status))))
460df8bae1dSRodney W. Grimes 					return (error);
461df8bae1dSRodney W. Grimes 			}
462df8bae1dSRodney W. Grimes 			if (uap->rusage && (error = copyout((caddr_t)p->p_ru,
463df8bae1dSRodney W. Grimes 			    (caddr_t)uap->rusage, sizeof (struct rusage))))
464df8bae1dSRodney W. Grimes 				return (error);
465df8bae1dSRodney W. Grimes 			/*
466df8bae1dSRodney W. Grimes 			 * If we got the child via a ptrace 'attach',
467df8bae1dSRodney W. Grimes 			 * we need to give it back to the old parent.
468df8bae1dSRodney W. Grimes 			 */
469df8bae1dSRodney W. Grimes 			if (p->p_oppid && (t = pfind(p->p_oppid))) {
470df8bae1dSRodney W. Grimes 				p->p_oppid = 0;
471df8bae1dSRodney W. Grimes 				proc_reparent(p, t);
472df8bae1dSRodney W. Grimes 				psignal(t, SIGCHLD);
473df8bae1dSRodney W. Grimes 				wakeup((caddr_t)t);
474df8bae1dSRodney W. Grimes 				return (0);
475df8bae1dSRodney W. Grimes 			}
476df8bae1dSRodney W. Grimes 			p->p_xstat = 0;
477df8bae1dSRodney W. Grimes 			ruadd(&q->p_stats->p_cru, p->p_ru);
478df8bae1dSRodney W. Grimes 			FREE(p->p_ru, M_ZOMBIE);
479be6a1d14SDavid Greenman 			p->p_ru = NULL;
480df8bae1dSRodney W. Grimes 
481df8bae1dSRodney W. Grimes 			/*
482df8bae1dSRodney W. Grimes 			 * Decrement the count of procs running with this uid.
483df8bae1dSRodney W. Grimes 			 */
484df8bae1dSRodney W. Grimes 			(void)chgproccnt(p->p_cred->p_ruid, -1);
485df8bae1dSRodney W. Grimes 
486df8bae1dSRodney W. Grimes 			/*
487bd7e5f99SJohn Dyson 			 * Release reference to text vnode
488bd7e5f99SJohn Dyson 			 */
489bd7e5f99SJohn Dyson 			if (p->p_textvp)
490bd7e5f99SJohn Dyson 				vrele(p->p_textvp);
491bd7e5f99SJohn Dyson 
492bd7e5f99SJohn Dyson 			/*
493df8bae1dSRodney W. Grimes 			 * Free up credentials.
494df8bae1dSRodney W. Grimes 			 */
495df8bae1dSRodney W. Grimes 			if (--p->p_cred->p_refcnt == 0) {
496df8bae1dSRodney W. Grimes 				crfree(p->p_cred->pc_ucred);
497df8bae1dSRodney W. Grimes 				FREE(p->p_cred, M_SUBPROC);
498be6a1d14SDavid Greenman 				p->p_cred = NULL;
499df8bae1dSRodney W. Grimes 			}
500df8bae1dSRodney W. Grimes 
501df8bae1dSRodney W. Grimes 			/*
502df8bae1dSRodney W. Grimes 			 * Finally finished with old proc entry.
503df8bae1dSRodney W. Grimes 			 * Unlink it from its process group and free it.
504df8bae1dSRodney W. Grimes 			 */
505df8bae1dSRodney W. Grimes 			leavepgrp(p);
506b75356e1SJeffrey Hsu 			LIST_REMOVE(p, p_list);	/* off zombproc */
507b75356e1SJeffrey Hsu 			LIST_REMOVE(p, p_sibling);
508df8bae1dSRodney W. Grimes 
5096626c604SJulian Elischer 			if (--p->p_procsig->ps_refcnt == 0) {
510dc9c271aSJulian Elischer 				if (p->p_sigacts != &p->p_addr->u_sigacts)
511dc9c271aSJulian Elischer 					FREE(p->p_sigacts, M_SUBPROC);
512dc9c271aSJulian Elischer 			        FREE(p->p_procsig, M_SUBPROC);
5136626c604SJulian Elischer 				p->p_procsig = NULL;
5146626c604SJulian Elischer 			}
51588c5ea45SJulian Elischer 
516df8bae1dSRodney W. Grimes 			/*
517df8bae1dSRodney W. Grimes 			 * Give machine-dependent layer a chance
518df8bae1dSRodney W. Grimes 			 * to free anything that cpu_exit couldn't
519df8bae1dSRodney W. Grimes 			 * release while still running in process context.
520df8bae1dSRodney W. Grimes 			 */
521df8bae1dSRodney W. Grimes 			cpu_wait(p);
5222d8acc0fSJohn Dyson 			zfree(proc_zone, p);
523df8bae1dSRodney W. Grimes 			nprocs--;
524df8bae1dSRodney W. Grimes 			return (0);
525df8bae1dSRodney W. Grimes 		}
526df8bae1dSRodney W. Grimes 		if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
527df8bae1dSRodney W. Grimes 		    (p->p_flag & P_TRACED || uap->options & WUNTRACED)) {
528df8bae1dSRodney W. Grimes 			p->p_flag |= P_WAITED;
52901166e92SBruce Evans 			q->p_retval[0] = p->p_pid;
530b2f9e8b1SBruce Evans #ifdef COMPAT_43
53193c9414eSSteven Wallace 			if (compat) {
53201166e92SBruce Evans 				q->p_retval[1] = W_STOPCODE(p->p_xstat);
533df8bae1dSRodney W. Grimes 				error = 0;
534df8bae1dSRodney W. Grimes 			} else
535df8bae1dSRodney W. Grimes #endif
536df8bae1dSRodney W. Grimes 			if (uap->status) {
537df8bae1dSRodney W. Grimes 				status = W_STOPCODE(p->p_xstat);
538df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&status,
539df8bae1dSRodney W. Grimes 					(caddr_t)uap->status, sizeof(status));
540df8bae1dSRodney W. Grimes 			} else
541df8bae1dSRodney W. Grimes 				error = 0;
542df8bae1dSRodney W. Grimes 			return (error);
543df8bae1dSRodney W. Grimes 		}
544df8bae1dSRodney W. Grimes 	}
545df8bae1dSRodney W. Grimes 	if (nfound == 0)
546df8bae1dSRodney W. Grimes 		return (ECHILD);
547df8bae1dSRodney W. Grimes 	if (uap->options & WNOHANG) {
54801166e92SBruce Evans 		q->p_retval[0] = 0;
549df8bae1dSRodney W. Grimes 		return (0);
550df8bae1dSRodney W. Grimes 	}
551bb56ec4aSPoul-Henning Kamp 	if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)))
552df8bae1dSRodney W. Grimes 		return (error);
553df8bae1dSRodney W. Grimes 	goto loop;
554df8bae1dSRodney W. Grimes }
555df8bae1dSRodney W. Grimes 
556df8bae1dSRodney W. Grimes /*
557df8bae1dSRodney W. Grimes  * make process 'parent' the new parent of process 'child'.
558df8bae1dSRodney W. Grimes  */
559df8bae1dSRodney W. Grimes void
560df8bae1dSRodney W. Grimes proc_reparent(child, parent)
561df8bae1dSRodney W. Grimes 	register struct proc *child;
562df8bae1dSRodney W. Grimes 	register struct proc *parent;
563df8bae1dSRodney W. Grimes {
564df8bae1dSRodney W. Grimes 
565df8bae1dSRodney W. Grimes 	if (child->p_pptr == parent)
566df8bae1dSRodney W. Grimes 		return;
567df8bae1dSRodney W. Grimes 
568b75356e1SJeffrey Hsu 	LIST_REMOVE(child, p_sibling);
569b75356e1SJeffrey Hsu 	LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
570df8bae1dSRodney W. Grimes 	child->p_pptr = parent;
571df8bae1dSRodney W. Grimes }
572fed06968SJulian Elischer 
573e0d898b4SJulian Elischer /*
574e0d898b4SJulian Elischer  * The next two functions are to handle adding/deleting items on the
575fed06968SJulian Elischer  * exit callout list
576e0d898b4SJulian Elischer  *
577e0d898b4SJulian Elischer  * at_exit():
578e0d898b4SJulian Elischer  * Take the arguments given and put them onto the exit callout list,
579fed06968SJulian Elischer  * However first make sure that it's not already there.
580fed06968SJulian Elischer  * returns 0 on success.
581fed06968SJulian Elischer  */
582fed06968SJulian Elischer int
583eb776aeaSBruce Evans at_exit(function)
584eb776aeaSBruce Evans 	exitlist_fn function;
585fed06968SJulian Elischer {
586fed06968SJulian Elischer 	ele_p ep;
587e0d898b4SJulian Elischer 
588e0d898b4SJulian Elischer 	/* Be noisy if the programmer has lost track of things */
589e0d898b4SJulian Elischer 	if (rm_at_exit(function))
590fed06968SJulian Elischer 		printf("exit callout entry already present\n");
591fed06968SJulian Elischer 	ep = malloc(sizeof(*ep), M_TEMP, M_NOWAIT);
592e0d898b4SJulian Elischer 	if (ep == NULL)
593e0d898b4SJulian Elischer 		return (ENOMEM);
594fed06968SJulian Elischer 	ep->next = exit_list;
595fed06968SJulian Elischer 	ep->function = function;
596fed06968SJulian Elischer 	exit_list = ep;
597e0d898b4SJulian Elischer 	return (0);
598fed06968SJulian Elischer }
599fed06968SJulian Elischer /*
600fed06968SJulian Elischer  * Scan the exit callout list for the given items and remove them.
601fed06968SJulian Elischer  * Returns the number of items removed.
602e0d898b4SJulian Elischer  * Logically this can only be 0 or 1.
603fed06968SJulian Elischer  */
604fed06968SJulian Elischer int
605eb776aeaSBruce Evans rm_at_exit(function)
606eb776aeaSBruce Evans 	exitlist_fn function;
607fed06968SJulian Elischer {
608fed06968SJulian Elischer 	ele_p *epp, ep;
609e0d898b4SJulian Elischer 	int count;
610fed06968SJulian Elischer 
611e0d898b4SJulian Elischer 	count = 0;
612fed06968SJulian Elischer 	epp = &exit_list;
613fed06968SJulian Elischer 	ep = *epp;
614fed06968SJulian Elischer 	while (ep) {
615fed06968SJulian Elischer 		if (ep->function == function) {
616fed06968SJulian Elischer 			*epp = ep->next;
617fed06968SJulian Elischer 			free(ep, M_TEMP);
618fed06968SJulian Elischer 			count++;
619fed06968SJulian Elischer 		} else {
620fed06968SJulian Elischer 			epp = &ep->next;
621fed06968SJulian Elischer 		}
622fed06968SJulian Elischer 		ep = *epp;
623fed06968SJulian Elischer 	}
624e0d898b4SJulian Elischer 	return (count);
625fed06968SJulian Elischer }
626dc9c271aSJulian Elischer 
627dc9c271aSJulian Elischer void check_sigacts (void)
628dc9c271aSJulian Elischer {
629dc9c271aSJulian Elischer 	struct proc *p = curproc;
630dc9c271aSJulian Elischer 	struct sigacts *pss;
631dc9c271aSJulian Elischer 	int s;
632dc9c271aSJulian Elischer 
633dc9c271aSJulian Elischer 	if (p->p_procsig->ps_refcnt == 1 &&
634dc9c271aSJulian Elischer 	    p->p_sigacts != &p->p_addr->u_sigacts) {
635dc9c271aSJulian Elischer 		pss = p->p_sigacts;
636dc9c271aSJulian Elischer 		s = splhigh();
637dc9c271aSJulian Elischer 		p->p_addr->u_sigacts = *pss;
638dc9c271aSJulian Elischer 		p->p_sigacts = &p->p_addr->u_sigacts;
639dc9c271aSJulian Elischer 		splx(s);
640dc9c271aSJulian Elischer 		FREE(pss, M_SUBPROC);
641dc9c271aSJulian Elischer 	}
642dc9c271aSJulian Elischer }
64388c5ea45SJulian Elischer 
644