xref: /original-bsd/sys/kern/kern_exit.c (revision bd226a66)
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.35 (Berkeley) 06/27/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 	if (--p->p_limit->p_refcnt == 0)
136 		FREE(p->p_limit, M_SUBPROC);
137 #ifdef KTRACE
138 	/*
139 	 * release trace file
140 	 */
141 	if (p->p_tracep)
142 		vrele(p->p_tracep);
143 #endif
144 
145 	/*
146 	 * Remove proc from allproc queue and pidhash chain.
147 	 * Place onto zombproc.  Unlink from parent's child list.
148 	 */
149 	if (*p->p_prev = p->p_nxt)
150 		p->p_nxt->p_prev = p->p_prev;
151 	if (p->p_nxt = zombproc)
152 		p->p_nxt->p_prev = &p->p_nxt;
153 	p->p_prev = &zombproc;
154 	zombproc = p;
155 	p->p_stat = SZOMB;
156 	curproc = NULL;
157 	for (pp = &pidhash[PIDHASH(p->p_pid)]; *pp; pp = &(*pp)->p_hash)
158 		if (*pp == p) {
159 			*pp = p->p_hash;
160 			goto done;
161 		}
162 	panic("exit");
163 done:
164 
165 	if (p->p_cptr)		/* only need this if any child is S_ZOMB */
166 		wakeup((caddr_t) initproc);
167 	for (q = p->p_cptr; q != NULL; q = nq) {
168 		nq = q->p_osptr;
169 		if (nq != NULL)
170 			nq->p_ysptr = NULL;
171 		if (initproc->p_cptr)
172 			initproc->p_cptr->p_ysptr = q;
173 		q->p_osptr = initproc->p_cptr;
174 		q->p_ysptr = NULL;
175 		initproc->p_cptr = q;
176 
177 		q->p_pptr = initproc;
178 		/*
179 		 * Traced processes are killed
180 		 * since their existence means someone is screwing up.
181 		 */
182 		if (q->p_flag&STRC) {
183 			q->p_flag &= ~STRC;
184 			psignal(q, SIGKILL);
185 		}
186 	}
187 	p->p_cptr = NULL;
188 
189 	/*
190 	 * Save exit status and final rusage info,
191 	 * adding in child rusage info and self times.
192 	 */
193 	p->p_xstat = rv;
194 	*p->p_ru = p->p_stats->p_ru;
195 	p->p_ru->ru_stime = p->p_stime;
196 	p->p_ru->ru_utime = p->p_utime;
197 	ruadd(p->p_ru, &p->p_stats->p_cru);
198 
199 	/*
200 	 * Notify parent that we're gone.
201 	 */
202 	psignal(p->p_pptr, SIGCHLD);
203 	wakeup((caddr_t)p->p_pptr);
204 #if defined(tahoe)
205 	/* move this to cpu_exit */
206 	p->p_addr->u_pcb.pcb_savacc.faddr = (float *)NULL;
207 #endif
208 	/*
209 	 * Finally, call machine-dependent code to release the remaining
210 	 * resources including address space, the kernel stack and pcb.
211 	 * The address space is released by "vmspace_free(p->p_vmspace)";
212 	 * This is machine-dependent, as we may have to change stacks
213 	 * or ensure that the current one isn't reallocated before we
214 	 * finish.  cpu_exit will end with a call to swtch(), finishing
215 	 * our execution (pun intended).
216 	 */
217 	cpu_exit(p);
218 	/* NOTREACHED */
219 }
220 
221 #ifdef COMPAT_43
222 owait(p, uap, retval)
223 	struct proc *p;
224 	register struct args {
225 		int	pid;
226 		int	*status;
227 		int	options;
228 		struct	rusage *rusage;
229 		int	compat;
230 	} *uap;
231 	int *retval;
232 {
233 
234 	if ((p->p_regs[PS] & PSL_ALLCC) != PSL_ALLCC) {
235 		uap->options = 0;
236 		uap->rusage = 0;
237 	} else {
238 		uap->options = p->p_regs[R0];
239 		uap->rusage = (struct rusage *)p->p_regs[R1];
240 	}
241 	uap->pid = WAIT_ANY;
242 	uap->status = 0;
243 	uap->compat = 1;
244 	return (wait1(p, uap, retval));
245 }
246 
247 wait4(p, uap, retval)
248 	struct proc *p;
249 	struct args {
250 		int	pid;
251 		int	*status;
252 		int	options;
253 		struct	rusage *rusage;
254 		int	compat;
255 	} *uap;
256 	int *retval;
257 {
258 
259 	uap->compat = 0;
260 	return (wait1(p, uap, retval));
261 }
262 #else
263 #define	wait1	wait4
264 #endif
265 
266 /*
267  * Wait: check child processes to see if any have exited,
268  * stopped under trace, or (optionally) stopped by a signal.
269  * Pass back status and deallocate exited child's proc structure.
270  */
271 wait1(q, uap, retval)
272 	register struct proc *q;
273 	register struct args {
274 		int	pid;
275 		int	*status;
276 		int	options;
277 		struct	rusage *rusage;
278 #ifdef COMPAT_43
279 		int compat;
280 #endif
281 	} *uap;
282 	int retval[];
283 {
284 	register int nfound;
285 	register struct proc *p;
286 	int status, error;
287 
288 	if (uap->pid == 0)
289 		uap->pid = -q->p_pgid;
290 #ifdef notyet
291 	if (uap->options &~ (WUNTRACED|WNOHANG))
292 		return (EINVAL);
293 #endif
294 loop:
295 	nfound = 0;
296 	for (p = q->p_cptr; p; p = p->p_osptr) {
297 		if (uap->pid != WAIT_ANY &&
298 		    p->p_pid != uap->pid && p->p_pgid != -uap->pid)
299 			continue;
300 		nfound++;
301 		if (p->p_stat == SZOMB) {
302 			retval[0] = p->p_pid;
303 #ifdef COMPAT_43
304 			if (uap->compat)
305 				retval[1] = p->p_xstat;
306 			else
307 #endif
308 			if (uap->status) {
309 				status = p->p_xstat;	/* convert to int */
310 				if (error = copyout((caddr_t)&status,
311 				    (caddr_t)uap->status, sizeof(status)))
312 					return (error);
313 			}
314 			if (uap->rusage && (error = copyout((caddr_t)p->p_ru,
315 			    (caddr_t)uap->rusage, sizeof (struct rusage))))
316 				return (error);
317 			p->p_xstat = 0;
318 			ruadd(&q->p_stats->p_cru, p->p_ru);
319 			FREE(p->p_ru, M_ZOMBIE);
320 			if (--p->p_cred->p_refcnt == 0) {
321 				crfree(p->p_cred->pc_ucred);
322 				FREE(p->p_cred, M_SUBPROC);
323 			}
324 
325 			/*
326 			 * Finally finished with old proc entry.
327 			 * Unlink it from its process group and free it.
328 			 */
329 			leavepgrp(p);
330 			if (*p->p_prev = p->p_nxt)	/* off zombproc */
331 				p->p_nxt->p_prev = p->p_prev;
332 			if (q = p->p_ysptr)
333 				q->p_osptr = p->p_osptr;
334 			if (q = p->p_osptr)
335 				q->p_ysptr = p->p_ysptr;
336 			if ((q = p->p_pptr)->p_cptr == p)
337 				q->p_cptr = p->p_osptr;
338 
339 			/*
340 			 * Give machine-dependent layer a chance
341 			 * to free anything that cpu_exit couldn't
342 			 * release while still running in process context.
343 			 */
344 			cpu_wait(p);
345 			FREE(p, M_PROC);
346 			nprocs--;
347 			return (0);
348 		}
349 		if (p->p_stat == SSTOP && (p->p_flag & SWTED) == 0 &&
350 		    (p->p_flag & STRC || uap->options & WUNTRACED)) {
351 			p->p_flag |= SWTED;
352 			retval[0] = p->p_pid;
353 #ifdef COMPAT_43
354 			if (uap->compat) {
355 				retval[1] = W_STOPCODE(p->p_xstat);
356 				error = 0;
357 			} else
358 #endif
359 			if (uap->status) {
360 				status = W_STOPCODE(p->p_xstat);
361 				error = copyout((caddr_t)&status,
362 				    (caddr_t)uap->status, sizeof(status));
363 			} else
364 				error = 0;
365 			return (error);
366 		}
367 	}
368 	if (nfound == 0)
369 		return (ECHILD);
370 	if (uap->options & WNOHANG) {
371 		retval[0] = 0;
372 		return (0);
373 	}
374 	if (error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0))
375 		return (error);
376 	goto loop;
377 }
378