xref: /csrg-svn/sys/kern/kern_exit.c (revision 54816)
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.47 (Berkeley) 07/08/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, adding in child rusage
195 	 * info and self times.
196 	 */
197 	p->p_xstat = rv;
198 	*p->p_ru = p->p_stats->p_ru;
199 	calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
200 	ruadd(p->p_ru, &p->p_stats->p_cru);
201 
202 	/*
203 	 * Notify parent that we're gone.
204 	 */
205 	psignal(p->p_pptr, SIGCHLD);
206 	wakeup((caddr_t)p->p_pptr);
207 #if defined(tahoe)
208 	/* move this to cpu_exit */
209 	p->p_addr->u_pcb.pcb_savacc.faddr = (float *)NULL;
210 #endif
211 	/*
212 	 * Clear curproc after we've done all operations
213 	 * that could block, and before tearing down the rest
214 	 * of the process state that might be used from clock, etc.
215 	 * Also, can't clear curproc while we're still runnable,
216 	 * as we're not on a run queue (we are current, just not
217 	 * a proper proc any longer!).
218 	 *
219 	 * Other substructures are freed from wait().
220 	 */
221 	curproc = NULL;
222 	if (--p->p_limit->p_refcnt == 0)
223 		FREE(p->p_limit, M_SUBPROC);
224 
225 	/*
226 	 * Finally, call machine-dependent code to release the remaining
227 	 * resources including address space, the kernel stack and pcb.
228 	 * The address space is released by "vmspace_free(p->p_vmspace)";
229 	 * This is machine-dependent, as we may have to change stacks
230 	 * or ensure that the current one isn't reallocated before we
231 	 * finish.  cpu_exit will end with a call to cpu_swtch(), finishing
232 	 * our execution (pun intended).
233 	 */
234 	cpu_exit(p);
235 	/* NOTREACHED */
236 }
237 
238 #ifdef COMPAT_43
239 owait(p, uap, retval)
240 	struct proc *p;
241 	register struct args {
242 		int	pid;
243 		int	*status;
244 		int	options;
245 		struct	rusage *rusage;
246 		int	compat;
247 	} *uap;
248 	int *retval;
249 {
250 
251 #ifdef PSL_ALLCC
252 	if ((p->p_md.md_regs[PS] & PSL_ALLCC) != PSL_ALLCC) {
253 		uap->options = 0;
254 		uap->rusage = 0;
255 	} else {
256 		uap->options = p->p_md.md_regs[R0];
257 		uap->rusage = (struct rusage *)p->p_md.md_regs[R1];
258 	}
259 #else
260 	uap->options = 0;
261 	uap->rusage = 0;
262 #endif
263 	uap->pid = WAIT_ANY;
264 	uap->status = 0;
265 	uap->compat = 1;
266 	return (wait1(p, uap, retval));
267 }
268 
269 wait4(p, uap, retval)
270 	struct proc *p;
271 	struct args {
272 		int	pid;
273 		int	*status;
274 		int	options;
275 		struct	rusage *rusage;
276 		int	compat;
277 	} *uap;
278 	int *retval;
279 {
280 
281 	uap->compat = 0;
282 	return (wait1(p, uap, retval));
283 }
284 #else
285 #define	wait1	wait4
286 #endif
287 
288 /*
289  * Wait: check child processes to see if any have exited,
290  * stopped under trace, or (optionally) stopped by a signal.
291  * Pass back status and deallocate exited child's proc structure.
292  */
293 wait1(q, uap, retval)
294 	register struct proc *q;
295 	register struct args {
296 		int	pid;
297 		int	*status;
298 		int	options;
299 		struct	rusage *rusage;
300 #ifdef COMPAT_43
301 		int compat;
302 #endif
303 	} *uap;
304 	int retval[];
305 {
306 	register int nfound;
307 	register struct proc *p, *t;
308 	int status, error;
309 
310 	if (uap->pid == 0)
311 		uap->pid = -q->p_pgid;
312 #ifdef notyet
313 	if (uap->options &~ (WUNTRACED|WNOHANG))
314 		return (EINVAL);
315 #endif
316 loop:
317 	nfound = 0;
318 	for (p = q->p_cptr; p; p = p->p_osptr) {
319 		if (uap->pid != WAIT_ANY &&
320 		    p->p_pid != uap->pid && p->p_pgid != -uap->pid)
321 			continue;
322 		nfound++;
323 		if (p->p_stat == SZOMB) {
324 			retval[0] = p->p_pid;
325 #ifdef COMPAT_43
326 			if (uap->compat)
327 				retval[1] = p->p_xstat;
328 			else
329 #endif
330 			if (uap->status) {
331 				status = p->p_xstat;	/* convert to int */
332 				if (error = copyout((caddr_t)&status,
333 				    (caddr_t)uap->status, sizeof(status)))
334 					return (error);
335 			}
336 			if (uap->rusage && (error = copyout((caddr_t)p->p_ru,
337 			    (caddr_t)uap->rusage, sizeof (struct rusage))))
338 				return (error);
339 			/*
340 			 * If we got the child via a ptrace 'attach',
341 			 * we need to give it back to the old parent.
342 			 */
343 			if (p->p_oppid && (t = pfind(p->p_oppid))) {
344 				p->p_oppid = 0;
345 				proc_reparent(p, t);
346 				psignal(t, SIGCHLD);
347 				wakeup((caddr_t)t);
348 				return (0);
349 			}
350 			p->p_xstat = 0;
351 			ruadd(&q->p_stats->p_cru, p->p_ru);
352 			FREE(p->p_ru, M_ZOMBIE);
353 			if (--p->p_cred->p_refcnt == 0) {
354 				crfree(p->p_cred->pc_ucred);
355 				FREE(p->p_cred, M_SUBPROC);
356 			}
357 
358 			/*
359 			 * Finally finished with old proc entry.
360 			 * Unlink it from its process group and free it.
361 			 */
362 			leavepgrp(p);
363 			if (*p->p_prev = p->p_nxt)	/* off zombproc */
364 				p->p_nxt->p_prev = p->p_prev;
365 			if (q = p->p_ysptr)
366 				q->p_osptr = p->p_osptr;
367 			if (q = p->p_osptr)
368 				q->p_ysptr = p->p_ysptr;
369 			if ((q = p->p_pptr)->p_cptr == p)
370 				q->p_cptr = p->p_osptr;
371 
372 			/*
373 			 * Give machine-dependent layer a chance
374 			 * to free anything that cpu_exit couldn't
375 			 * release while still running in process context.
376 			 */
377 			cpu_wait(p);
378 			FREE(p, M_PROC);
379 			nprocs--;
380 			return (0);
381 		}
382 		if (p->p_stat == SSTOP && (p->p_flag & SWTED) == 0 &&
383 		    (p->p_flag & STRC || uap->options & WUNTRACED)) {
384 			p->p_flag |= SWTED;
385 			retval[0] = p->p_pid;
386 #ifdef COMPAT_43
387 			if (uap->compat) {
388 				retval[1] = W_STOPCODE(p->p_xstat);
389 				error = 0;
390 			} else
391 #endif
392 			if (uap->status) {
393 				status = W_STOPCODE(p->p_xstat);
394 				error = copyout((caddr_t)&status,
395 					(caddr_t)uap->status, sizeof(status));
396 			} else
397 				error = 0;
398 			return (error);
399 		}
400 	}
401 	if (nfound == 0)
402 		return (ECHILD);
403 	if (uap->options & WNOHANG) {
404 		retval[0] = 0;
405 		return (0);
406 	}
407 	if (error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0))
408 		return (error);
409 	goto loop;
410 }
411 
412 /*
413  * make process 'parent' the new parent of process 'child'.
414  */
415 void
416 proc_reparent(child, parent)
417 	register struct proc *child;
418 	register struct proc *parent;
419 {
420 	register struct proc *o;
421 	register struct proc *y;
422 
423 	if (child->p_pptr == parent)
424 		return;
425 
426 	/* fix up the child linkage for the old parent */
427 	o = child->p_osptr;
428 	y = child->p_ysptr;
429 	if (y)
430 		y->p_osptr = o;
431 	if (o)
432 		o->p_ysptr = y;
433 	if (child->p_pptr->p_cptr == child)
434 		child->p_pptr->p_cptr = o;
435 
436 	/* fix up child linkage for new parent */
437 	o = parent->p_cptr;
438 	if (o)
439 		o->p_ysptr = child;
440 	child->p_osptr = o;
441 	child->p_ysptr = NULL;
442 	parent->p_cptr = child;
443 	child->p_pptr = parent;
444 }
445