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