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