xref: /freebsd/sys/kern/kern_exit.c (revision ef5d438ed4bc17ad7ece3e40fe4d1f9baf3aadf7)
1 /*-
2  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)kern_exit.c	8.7 (Berkeley) 2/12/94
39  * $Id: kern_exit.c,v 1.27 1996/01/19 03:58:03 dyson Exp $
40  */
41 
42 #include "opt_ktrace.h"
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/sysproto.h>
47 #include <sys/sysent.h>
48 #include <sys/ioctl.h>
49 #include <sys/proc.h>
50 #include <sys/tty.h>
51 #include <sys/time.h>
52 #include <sys/resource.h>
53 #include <sys/kernel.h>
54 #include <sys/buf.h>
55 #include <sys/wait.h>
56 #include <sys/file.h>
57 #include <sys/vnode.h>
58 #include <sys/syslog.h>
59 #include <sys/malloc.h>
60 #include <sys/resourcevar.h>
61 #include <sys/signalvar.h>
62 #include <sys/ptrace.h>
63 #include <sys/filedesc.h>
64 #include <sys/shm.h>
65 #include <sys/sem.h>
66 
67 #include <machine/cpu.h>
68 #ifdef COMPAT_43
69 #include <machine/reg.h>
70 #include <machine/psl.h>
71 #endif
72 
73 #include <vm/vm.h>
74 #include <vm/vm_param.h>
75 #include <vm/vm_prot.h>
76 #include <vm/lock.h>
77 #include <vm/pmap.h>
78 #include <vm/vm_map.h>
79 #include <vm/vm_kern.h>
80 
81 static int wait1 __P((struct proc *, struct wait_args *, int [], int));
82 
83 /*
84  * exit --
85  *	Death of process.
86  */
87 __dead void
88 exit(p, uap, retval)
89 	struct proc *p;
90 	struct rexit_args /* {
91 		int	rval;
92 	} */ *uap;
93 	int *retval;
94 {
95 
96 	exit1(p, W_EXITCODE(uap->rval, 0));
97 	/* NOTREACHED */
98 }
99 
100 /*
101  * Exit: deallocate address space and other resources, change proc state
102  * to zombie, and unlink proc from allproc and parent's lists.  Save exit
103  * status and rusage for wait().  Check for child processes and orphan them.
104  */
105 __dead void
106 exit1(p, rv)
107 	register struct proc *p;
108 	int rv;
109 {
110 	register struct proc *q, *nq;
111 	register struct proc **pp;
112 	register struct vmspace *vm;
113 
114 	if (p->p_pid == 1) {
115 		printf("init died (signal %d, exit %d)\n",
116 		    WTERMSIG(rv), WEXITSTATUS(rv));
117 		panic("Going nowhere without my init!");
118 	}
119 #ifdef PGINPROF
120 	vmsizmon();
121 #endif
122 	if (p->p_flag & P_PROFIL)
123 		stopprofclock(p);
124 	MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage),
125 		M_ZOMBIE, M_WAITOK);
126 	/*
127 	 * If parent is waiting for us to exit or exec,
128 	 * P_PPWAIT is set; we will wakeup the parent below.
129 	 */
130 	p->p_flag &= ~(P_TRACED | P_PPWAIT);
131 	p->p_flag |= P_WEXIT;
132 	p->p_sigignore = ~0;
133 	p->p_siglist = 0;
134 	untimeout(realitexpire, (caddr_t)p);
135 
136 	/*
137 	 * Close open files and release open-file table.
138 	 * This may block!
139 	 */
140 	fdfree(p);
141 
142 	/*
143 	 * XXX Shutdown SYSV semaphores
144 	 */
145 	semexit(p);
146 
147 	/* The next two chunks should probably be moved to vmspace_exit. */
148 	vm = p->p_vmspace;
149 	if (vm->vm_shm)
150 		shmexit(p);
151 	/*
152 	 * Release user portion of address space.
153 	 * This releases references to vnodes,
154 	 * which could cause I/O if the file has been unlinked.
155 	 * Need to do this early enough that we can still sleep.
156 	 * Can't free the entire vmspace as the kernel stack
157 	 * may be mapped within that space also.
158 	 */
159 	if (vm->vm_refcnt == 1)
160 		(void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS,
161 		    VM_MAXUSER_ADDRESS);
162 
163 	if (SESS_LEADER(p)) {
164 		register struct session *sp = p->p_session;
165 
166 		if (sp->s_ttyvp) {
167 			/*
168 			 * Controlling process.
169 			 * Signal foreground pgrp,
170 			 * drain controlling terminal
171 			 * and revoke access to controlling terminal.
172 			 */
173 			if (sp->s_ttyp->t_session == sp) {
174 				if (sp->s_ttyp->t_pgrp)
175 					pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
176 				(void) ttywait(sp->s_ttyp);
177 				/*
178 				 * The tty could have been revoked
179 				 * if we blocked.
180 				 */
181 				if (sp->s_ttyvp)
182 					vgoneall(sp->s_ttyvp);
183 			}
184 			if (sp->s_ttyvp)
185 				vrele(sp->s_ttyvp);
186 			sp->s_ttyvp = NULL;
187 			/*
188 			 * s_ttyp is not zero'd; we use this to indicate
189 			 * that the session once had a controlling terminal.
190 			 * (for logging and informational purposes)
191 			 */
192 		}
193 		sp->s_leader = NULL;
194 	}
195 	fixjobc(p, p->p_pgrp, 0);
196 	p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
197 	(void)acct_process(p);
198 #ifdef KTRACE
199 	/*
200 	 * release trace file
201 	 */
202 	p->p_traceflag = 0;	/* don't trace the vrele() */
203 	if (p->p_tracep)
204 		vrele(p->p_tracep);
205 #endif
206 	/*
207 	 * Remove proc from allproc queue and pidhash chain.
208 	 * Place onto zombproc.  Unlink from parent's child list.
209 	 */
210 	if ((*p->p_prev = p->p_next))
211 		p->p_next->p_prev = p->p_prev;
212 	if ((p->p_next = zombproc))
213 		p->p_next->p_prev = &p->p_next;
214 	p->p_prev = &zombproc;
215 	zombproc = p;
216 	p->p_stat = SZOMB;
217 
218 	for (pp = &pidhash[PIDHASH(p->p_pid)]; *pp; pp = &(*pp)->p_hash)
219 		if (*pp == p) {
220 			*pp = p->p_hash;
221 			goto done;
222 		}
223 	panic("exit");
224 done:
225 
226 	if (p->p_cptr)		/* only need this if any child is S_ZOMB */
227 		wakeup((caddr_t) initproc);
228 	for (q = p->p_cptr; q != NULL; q = nq) {
229 		nq = q->p_osptr;
230 		if (nq != NULL)
231 			nq->p_ysptr = NULL;
232 		if (initproc->p_cptr)
233 			initproc->p_cptr->p_ysptr = q;
234 		q->p_osptr = initproc->p_cptr;
235 		q->p_ysptr = NULL;
236 		initproc->p_cptr = q;
237 
238 		q->p_pptr = initproc;
239 		/*
240 		 * Traced processes are killed
241 		 * since their existence means someone is screwing up.
242 		 */
243 		if (q->p_flag & P_TRACED) {
244 			q->p_flag &= ~P_TRACED;
245 			psignal(q, SIGKILL);
246 		}
247 	}
248 	p->p_cptr = NULL;
249 
250 	/*
251 	 * Save exit status and final rusage info, adding in child rusage
252 	 * info and self times.
253 	 */
254 	p->p_xstat = rv;
255 	*p->p_ru = p->p_stats->p_ru;
256 	calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
257 	ruadd(p->p_ru, &p->p_stats->p_cru);
258 
259 	/*
260 	 * Notify parent that we're gone.
261 	 */
262 	psignal(p->p_pptr, SIGCHLD);
263 	wakeup((caddr_t)p->p_pptr);
264 #if defined(tahoe)
265 	/* move this to cpu_exit */
266 	p->p_addr->u_pcb.pcb_savacc.faddr = (float *)NULL;
267 #endif
268 	/*
269 	 * Clear curproc after we've done all operations
270 	 * that could block, and before tearing down the rest
271 	 * of the process state that might be used from clock, etc.
272 	 * Also, can't clear curproc while we're still runnable,
273 	 * as we're not on a run queue (we are current, just not
274 	 * a proper proc any longer!).
275 	 *
276 	 * Other substructures are freed from wait().
277 	 */
278 	curproc = NULL;
279 	if (--p->p_limit->p_refcnt == 0) {
280 		FREE(p->p_limit, M_SUBPROC);
281 		p->p_limit = NULL;
282 	}
283 
284 	/*
285 	 * Finally, call machine-dependent code to release the remaining
286 	 * resources including address space, the kernel stack and pcb.
287 	 * The address space is released by "vmspace_free(p->p_vmspace)";
288 	 * This is machine-dependent, as we may have to change stacks
289 	 * or ensure that the current one isn't reallocated before we
290 	 * finish.  cpu_exit will end with a call to cpu_swtch(), finishing
291 	 * our execution (pun intended).
292 	 */
293 	cpu_exit(p);
294 }
295 
296 #ifdef COMPAT_43
297 #if defined(hp300) || defined(luna68k)
298 #include <machine/frame.h>
299 #define GETPS(rp)	((struct frame *)(rp))->f_sr
300 #else
301 #define GETPS(rp)	(rp)[PS]
302 #endif
303 
304 int
305 owait(p, uap, retval)
306 	struct proc *p;
307 	register struct owait_args /* {
308 		int     dummy;
309 	} */ *uap;
310 	int *retval;
311 {
312 	struct wait_args w;
313 
314 #ifdef PSL_ALLCC
315 	if ((GETPS(p->p_md.md_regs) & PSL_ALLCC) != PSL_ALLCC) {
316 		w.options = 0;
317 		w.rusage = NULL;
318 	} else {
319 		w.options = p->p_md.md_regs[R0];
320 		w.rusage = (struct rusage *)p->p_md.md_regs[R1];
321 	}
322 #else
323 	w.options = 0;
324 	w.rusage = NULL;
325 #endif
326 	w.pid = WAIT_ANY;
327 	w.status = NULL;
328 	return (wait1(p, &w, retval, 1));
329 }
330 #endif /* COMPAT_43 */
331 
332 int
333 wait4(p, uap, retval)
334 	struct proc *p;
335 	struct wait_args *uap;
336 	int *retval;
337 {
338 
339 	return (wait1(p, uap, retval, 0));
340 }
341 
342 static int
343 wait1(q, uap, retval, compat)
344 	register struct proc *q;
345 	register struct wait_args /* {
346 		int pid;
347 		int *status;
348 		int options;
349 		struct rusage *rusage;
350 	} */ *uap;
351 	int retval[];
352 	int compat;
353 {
354 	register int nfound;
355 	register struct proc *p, *t;
356 	int status, error;
357 
358 	if (uap->pid == 0)
359 		uap->pid = -q->p_pgid;
360 #ifdef notyet
361 	if (uap->options &~ (WUNTRACED|WNOHANG))
362 		return (EINVAL);
363 #endif
364 loop:
365 	nfound = 0;
366 	for (p = q->p_cptr; p; p = p->p_osptr) {
367 		if (uap->pid != WAIT_ANY &&
368 		    p->p_pid != uap->pid && p->p_pgid != -uap->pid)
369 			continue;
370 		nfound++;
371 		if (p->p_stat == SZOMB) {
372 			/* charge childs scheduling cpu usage to parent */
373 			if (curproc->p_pid != 1) {
374 				curproc->p_estcpu = min(curproc->p_estcpu +
375 				    p->p_estcpu, UCHAR_MAX);
376 			}
377 
378 			retval[0] = p->p_pid;
379 #ifdef COMPAT_43
380 			if (compat)
381 				retval[1] = p->p_xstat;
382 			else
383 #endif
384 			if (uap->status) {
385 				status = p->p_xstat;	/* convert to int */
386 				if ((error = copyout((caddr_t)&status,
387 				    (caddr_t)uap->status, sizeof(status))))
388 					return (error);
389 			}
390 			if (uap->rusage && (error = copyout((caddr_t)p->p_ru,
391 			    (caddr_t)uap->rusage, sizeof (struct rusage))))
392 				return (error);
393 			/*
394 			 * If we got the child via a ptrace 'attach',
395 			 * we need to give it back to the old parent.
396 			 */
397 			if (p->p_oppid && (t = pfind(p->p_oppid))) {
398 				p->p_oppid = 0;
399 				proc_reparent(p, t);
400 				psignal(t, SIGCHLD);
401 				wakeup((caddr_t)t);
402 				return (0);
403 			}
404 			p->p_xstat = 0;
405 			ruadd(&q->p_stats->p_cru, p->p_ru);
406 			FREE(p->p_ru, M_ZOMBIE);
407 			p->p_ru = NULL;
408 
409 			/*
410 			 * Decrement the count of procs running with this uid.
411 			 */
412 			(void)chgproccnt(p->p_cred->p_ruid, -1);
413 
414 			/*
415 			 * Release reference to text vnode
416 			 */
417 			if (p->p_textvp)
418 				vrele(p->p_textvp);
419 
420 			/*
421 			 * Free up credentials.
422 			 */
423 			if (--p->p_cred->p_refcnt == 0) {
424 				crfree(p->p_cred->pc_ucred);
425 				FREE(p->p_cred, M_SUBPROC);
426 				p->p_cred = NULL;
427 			}
428 
429 			/*
430 			 * Finally finished with old proc entry.
431 			 * Unlink it from its process group and free it.
432 			 */
433 			leavepgrp(p);
434 			if ((*p->p_prev = p->p_next))	/* off zombproc */
435 				p->p_next->p_prev = p->p_prev;
436 			if ((q = p->p_ysptr))
437 				q->p_osptr = p->p_osptr;
438 			if ((q = p->p_osptr))
439 				q->p_ysptr = p->p_ysptr;
440 			if ((q = p->p_pptr)->p_cptr == p)
441 				q->p_cptr = p->p_osptr;
442 
443 			/*
444 			 * Give machine-dependent layer a chance
445 			 * to free anything that cpu_exit couldn't
446 			 * release while still running in process context.
447 			 */
448 			cpu_wait(p);
449 			FREE(p, M_PROC);
450 			nprocs--;
451 			return (0);
452 		}
453 		if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
454 		    (p->p_flag & P_TRACED || uap->options & WUNTRACED)) {
455 			p->p_flag |= P_WAITED;
456 			retval[0] = p->p_pid;
457 #ifdef COMPAT_43
458 			if (compat) {
459 				retval[1] = W_STOPCODE(p->p_xstat);
460 				error = 0;
461 			} else
462 #endif
463 			if (uap->status) {
464 				status = W_STOPCODE(p->p_xstat);
465 				error = copyout((caddr_t)&status,
466 					(caddr_t)uap->status, sizeof(status));
467 			} else
468 				error = 0;
469 			return (error);
470 		}
471 	}
472 	if (nfound == 0)
473 		return (ECHILD);
474 	if (uap->options & WNOHANG) {
475 		retval[0] = 0;
476 		return (0);
477 	}
478 	if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)))
479 		return (error);
480 	goto loop;
481 }
482 
483 /*
484  * make process 'parent' the new parent of process 'child'.
485  */
486 void
487 proc_reparent(child, parent)
488 	register struct proc *child;
489 	register struct proc *parent;
490 {
491 	register struct proc *o;
492 	register struct proc *y;
493 
494 	if (child->p_pptr == parent)
495 		return;
496 
497 	/* fix up the child linkage for the old parent */
498 	o = child->p_osptr;
499 	y = child->p_ysptr;
500 	if (y)
501 		y->p_osptr = o;
502 	if (o)
503 		o->p_ysptr = y;
504 	if (child->p_pptr->p_cptr == child)
505 		child->p_pptr->p_cptr = o;
506 
507 	/* fix up child linkage for new parent */
508 	o = parent->p_cptr;
509 	if (o)
510 		o->p_ysptr = child;
511 	child->p_osptr = o;
512 	child->p_ysptr = NULL;
513 	parent->p_cptr = child;
514 	child->p_pptr = parent;
515 }
516