xref: /csrg-svn/sys/kern/kern_exit.c (revision 51014)
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1991 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)kern_exit.c	7.36 (Berkeley) 09/06/91
8  */
9 
10 #include "param.h"
11 #include "systm.h"
12 #include "map.h"
13 #include "ioctl.h"
14 #include "tty.h"
15 #include "time.h"
16 #include "resource.h"
17 #include "kernel.h"
18 #include "proc.h"
19 #include "buf.h"
20 #include "wait.h"
21 #include "file.h"
22 #include "vnode.h"
23 #include "syslog.h"
24 #include "malloc.h"
25 #include "resourcevar.h"
26 
27 #include "machine/cpu.h"
28 #ifdef COMPAT_43
29 #include "machine/reg.h"
30 #include "machine/psl.h"
31 #endif
32 
33 #include "vm/vm.h"
34 #include "vm/vm_kern.h"
35 
36 /*
37  * Exit system call: pass back caller's arg
38  */
39 /* ARGSUSED */
40 rexit(p, uap, retval)
41 	struct proc *p;
42 	struct args {
43 		int	rval;
44 	} *uap;
45 	int *retval;
46 {
47 
48 	exit(p, W_EXITCODE(uap->rval, 0));
49 	/* NOTREACHED */
50 }
51 
52 /*
53  * Exit: deallocate address space and other resources,
54  * change proc state to zombie, and unlink proc from allproc
55  * and parent's lists.  Save exit status and rusage for wait().
56  * Check for child processes and orphan them.
57  */
58 exit(p, rv)
59 	register struct proc *p;
60 	int rv;
61 {
62 	register struct proc *q, *nq;
63 	register struct proc **pp;
64 	int s;
65 
66 #ifdef PGINPROF
67 	vmsizmon();
68 #endif
69 	MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage),
70 		M_ZOMBIE, M_WAITOK);
71 	/*
72 	 * If parent is waiting for us to exit or exec,
73 	 * SPPWAIT is set; we will wakeup the parent below.
74 	 */
75 	p->p_flag &= ~(STRC|SPPWAIT);
76 	p->p_flag |= SWEXIT;
77 	p->p_sigignore = ~0;
78 	p->p_sig = 0;
79 	untimeout(realitexpire, (caddr_t)p);
80 
81 	/*
82 	 * Close open files and release open-file table.
83 	 * This may block!
84 	 */
85 	fdfree(p);
86 
87 	/* The next two chunks should probably be moved to vmspace_exit. */
88 #ifdef SYSVSHM
89 	if (p->p_vmspace->vm_shm)
90 		shmexit(p);
91 #endif
92 	/*
93 	 * Release user portion of address space.
94 	 * This releases references to vnodes,
95 	 * which could cause I/O if the file has been unlinked.
96 	 * Need to do this early enough that we can still sleep.
97 	 * Can't free the entire vmspace as the kernel stack
98 	 * may be mapped within that space also.
99 	 */
100 	if (p->p_vmspace->vm_refcnt == 1)
101 		(void) vm_map_remove(&p->p_vmspace->vm_map, VM_MIN_ADDRESS,
102 		    VM_MAXUSER_ADDRESS);
103 
104 	if (p->p_pid == 1)
105 		panic("init died");
106 	if (SESS_LEADER(p)) {
107 		register struct session *sp = p->p_session;
108 
109 		if (sp->s_ttyvp) {
110 			/*
111 			 * Controlling process.
112 			 * Signal foreground pgrp,
113 			 * drain controlling terminal
114 			 * and revoke access to controlling terminal.
115 			 */
116 			if (sp->s_ttyp->t_session == sp) {
117 				if (sp->s_ttyp->t_pgrp)
118 					pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
119 				(void) ttywait(sp->s_ttyp);
120 				vgoneall(sp->s_ttyvp);
121 			}
122 			vrele(sp->s_ttyvp);
123 			sp->s_ttyvp = NULL;
124 			/*
125 			 * s_ttyp is not zero'd; we use this to indicate
126 			 * that the session once had a controlling terminal.
127 			 * (for logging and informational purposes)
128 			 */
129 		}
130 		sp->s_leader = NULL;
131 	}
132 	fixjobc(p, p->p_pgrp, 0);
133 	p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
134 	(void) acct(p);
135 #ifdef KTRACE
136 	/*
137 	 * release trace file
138 	 */
139 	if (p->p_tracep)
140 		vrele(p->p_tracep);
141 #endif
142 	/*
143 	 * Clear curproc after we've done all operations
144 	 * that could block, and before tearing down
145 	 * the rest of the process state.
146 	 */
147 	curproc = NULL;
148 	if (--p->p_limit->p_refcnt == 0)
149 		FREE(p->p_limit, M_SUBPROC);
150 
151 	/*
152 	 * Remove proc from allproc queue and pidhash chain.
153 	 * Place onto zombproc.  Unlink from parent's child list.
154 	 */
155 	if (*p->p_prev = p->p_nxt)
156 		p->p_nxt->p_prev = p->p_prev;
157 	if (p->p_nxt = zombproc)
158 		p->p_nxt->p_prev = &p->p_nxt;
159 	p->p_prev = &zombproc;
160 	zombproc = p;
161 	p->p_stat = SZOMB;
162 	for (pp = &pidhash[PIDHASH(p->p_pid)]; *pp; pp = &(*pp)->p_hash)
163 		if (*pp == p) {
164 			*pp = p->p_hash;
165 			goto done;
166 		}
167 	panic("exit");
168 done:
169 
170 	if (p->p_cptr)		/* only need this if any child is S_ZOMB */
171 		wakeup((caddr_t) initproc);
172 	for (q = p->p_cptr; q != NULL; q = nq) {
173 		nq = q->p_osptr;
174 		if (nq != NULL)
175 			nq->p_ysptr = NULL;
176 		if (initproc->p_cptr)
177 			initproc->p_cptr->p_ysptr = q;
178 		q->p_osptr = initproc->p_cptr;
179 		q->p_ysptr = NULL;
180 		initproc->p_cptr = q;
181 
182 		q->p_pptr = initproc;
183 		/*
184 		 * Traced processes are killed
185 		 * since their existence means someone is screwing up.
186 		 */
187 		if (q->p_flag&STRC) {
188 			q->p_flag &= ~STRC;
189 			psignal(q, SIGKILL);
190 		}
191 	}
192 	p->p_cptr = NULL;
193 
194 	/*
195 	 * Save exit status and final rusage info,
196 	 * adding in child rusage info and self times.
197 	 */
198 	p->p_xstat = rv;
199 	*p->p_ru = p->p_stats->p_ru;
200 	p->p_ru->ru_stime = p->p_stime;
201 	p->p_ru->ru_utime = p->p_utime;
202 	ruadd(p->p_ru, &p->p_stats->p_cru);
203 
204 	/*
205 	 * Notify parent that we're gone.
206 	 */
207 	psignal(p->p_pptr, SIGCHLD);
208 	wakeup((caddr_t)p->p_pptr);
209 #if defined(tahoe)
210 	/* move this to cpu_exit */
211 	p->p_addr->u_pcb.pcb_savacc.faddr = (float *)NULL;
212 #endif
213 	/*
214 	 * Finally, call machine-dependent code to release the remaining
215 	 * resources including address space, the kernel stack and pcb.
216 	 * The address space is released by "vmspace_free(p->p_vmspace)";
217 	 * This is machine-dependent, as we may have to change stacks
218 	 * or ensure that the current one isn't reallocated before we
219 	 * finish.  cpu_exit will end with a call to swtch(), finishing
220 	 * our execution (pun intended).
221 	 */
222 	cpu_exit(p);
223 	/* NOTREACHED */
224 }
225 
226 #ifdef COMPAT_43
227 owait(p, uap, retval)
228 	struct proc *p;
229 	register struct args {
230 		int	pid;
231 		int	*status;
232 		int	options;
233 		struct	rusage *rusage;
234 		int	compat;
235 	} *uap;
236 	int *retval;
237 {
238 
239 	if ((p->p_regs[PS] & PSL_ALLCC) != PSL_ALLCC) {
240 		uap->options = 0;
241 		uap->rusage = 0;
242 	} else {
243 		uap->options = p->p_regs[R0];
244 		uap->rusage = (struct rusage *)p->p_regs[R1];
245 	}
246 	uap->pid = WAIT_ANY;
247 	uap->status = 0;
248 	uap->compat = 1;
249 	return (wait1(p, uap, retval));
250 }
251 
252 wait4(p, uap, retval)
253 	struct proc *p;
254 	struct args {
255 		int	pid;
256 		int	*status;
257 		int	options;
258 		struct	rusage *rusage;
259 		int	compat;
260 	} *uap;
261 	int *retval;
262 {
263 
264 	uap->compat = 0;
265 	return (wait1(p, uap, retval));
266 }
267 #else
268 #define	wait1	wait4
269 #endif
270 
271 /*
272  * Wait: check child processes to see if any have exited,
273  * stopped under trace, or (optionally) stopped by a signal.
274  * Pass back status and deallocate exited child's proc structure.
275  */
276 wait1(q, uap, retval)
277 	register struct proc *q;
278 	register struct args {
279 		int	pid;
280 		int	*status;
281 		int	options;
282 		struct	rusage *rusage;
283 #ifdef COMPAT_43
284 		int compat;
285 #endif
286 	} *uap;
287 	int retval[];
288 {
289 	register int nfound;
290 	register struct proc *p;
291 	int status, error;
292 
293 	if (uap->pid == 0)
294 		uap->pid = -q->p_pgid;
295 #ifdef notyet
296 	if (uap->options &~ (WUNTRACED|WNOHANG))
297 		return (EINVAL);
298 #endif
299 loop:
300 	nfound = 0;
301 	for (p = q->p_cptr; p; p = p->p_osptr) {
302 		if (uap->pid != WAIT_ANY &&
303 		    p->p_pid != uap->pid && p->p_pgid != -uap->pid)
304 			continue;
305 		nfound++;
306 		if (p->p_stat == SZOMB) {
307 			retval[0] = p->p_pid;
308 #ifdef COMPAT_43
309 			if (uap->compat)
310 				retval[1] = p->p_xstat;
311 			else
312 #endif
313 			if (uap->status) {
314 				status = p->p_xstat;	/* convert to int */
315 				if (error = copyout((caddr_t)&status,
316 				    (caddr_t)uap->status, sizeof(status)))
317 					return (error);
318 			}
319 			if (uap->rusage && (error = copyout((caddr_t)p->p_ru,
320 			    (caddr_t)uap->rusage, sizeof (struct rusage))))
321 				return (error);
322 			p->p_xstat = 0;
323 			ruadd(&q->p_stats->p_cru, p->p_ru);
324 			FREE(p->p_ru, M_ZOMBIE);
325 			if (--p->p_cred->p_refcnt == 0) {
326 				crfree(p->p_cred->pc_ucred);
327 				FREE(p->p_cred, M_SUBPROC);
328 			}
329 
330 			/*
331 			 * Finally finished with old proc entry.
332 			 * Unlink it from its process group and free it.
333 			 */
334 			leavepgrp(p);
335 			if (*p->p_prev = p->p_nxt)	/* off zombproc */
336 				p->p_nxt->p_prev = p->p_prev;
337 			if (q = p->p_ysptr)
338 				q->p_osptr = p->p_osptr;
339 			if (q = p->p_osptr)
340 				q->p_ysptr = p->p_ysptr;
341 			if ((q = p->p_pptr)->p_cptr == p)
342 				q->p_cptr = p->p_osptr;
343 
344 			/*
345 			 * Give machine-dependent layer a chance
346 			 * to free anything that cpu_exit couldn't
347 			 * release while still running in process context.
348 			 */
349 			cpu_wait(p);
350 			FREE(p, M_PROC);
351 			nprocs--;
352 			return (0);
353 		}
354 		if (p->p_stat == SSTOP && (p->p_flag & SWTED) == 0 &&
355 		    (p->p_flag & STRC || uap->options & WUNTRACED)) {
356 			p->p_flag |= SWTED;
357 			retval[0] = p->p_pid;
358 #ifdef COMPAT_43
359 			if (uap->compat) {
360 				retval[1] = W_STOPCODE(p->p_xstat);
361 				error = 0;
362 			} else
363 #endif
364 			if (uap->status) {
365 				status = W_STOPCODE(p->p_xstat);
366 				error = copyout((caddr_t)&status,
367 				    (caddr_t)uap->status, sizeof(status));
368 			} else
369 				error = 0;
370 			return (error);
371 		}
372 	}
373 	if (nfound == 0)
374 		return (ECHILD);
375 	if (uap->options & WNOHANG) {
376 		retval[0] = 0;
377 		return (0);
378 	}
379 	if (error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0))
380 		return (error);
381 	goto loop;
382 }
383