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