xref: /freebsd/sys/kern/kern_exit.c (revision 5ebc7e6281887681c3a348a5a4c902e262ccd656)
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.13 1994/12/28 06:15:07 davidg 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 #if defined(COMPAT_43) || defined(COMPAT_IBCS2)
285 #if defined(hp300) || defined(luna68k)
286 #include <machine/frame.h>
287 #define GETPS(rp)	((struct frame *)(rp))->f_sr
288 #else
289 #define GETPS(rp)	(rp)[PS]
290 #endif
291 
292 int
293 owait(p, uap, retval)
294 	struct proc *p;
295 	register struct wait_args *uap;
296 	int *retval;
297 {
298 
299 #ifdef PSL_ALLCC
300 	if ((GETPS(p->p_md.md_regs) & PSL_ALLCC) != PSL_ALLCC) {
301 		uap->options = 0;
302 		uap->rusage = NULL;
303 	} else {
304 		uap->options = p->p_md.md_regs[R0];
305 		uap->rusage = (struct rusage *)p->p_md.md_regs[R1];
306 	}
307 #else
308 	uap->options = 0;
309 	uap->rusage = NULL;
310 #endif
311 	uap->pid = WAIT_ANY;
312 	uap->status = NULL;
313 	uap->compat = 1;
314 	return (wait1(p, uap, retval));
315 }
316 
317 int
318 wait4(p, uap, retval)
319 	struct proc *p;
320 	struct wait_args *uap;
321 	int *retval;
322 {
323 
324 	uap->compat = 0;
325 	return (wait1(p, uap, retval));
326 }
327 #else
328 #define	wait1	wait4
329 #endif
330 
331 int
332 wait1(q, uap, retval)
333 	register struct proc *q;
334 	register struct wait_args *uap;
335 	int retval[];
336 {
337 	register int nfound;
338 	register struct proc *p, *t;
339 	int status, error, sig;
340 
341 	if (uap->pid == 0)
342 		uap->pid = -q->p_pgid;
343 #ifdef notyet
344 	if (uap->options &~ (WUNTRACED|WNOHANG))
345 		return (EINVAL);
346 #endif
347 loop:
348 	nfound = 0;
349 	for (p = q->p_cptr; p; p = p->p_osptr) {
350 		if (uap->pid != WAIT_ANY &&
351 		    p->p_pid != uap->pid && p->p_pgid != -uap->pid)
352 			continue;
353 		nfound++;
354 #if defined(COMPAT_43) || defined(COMPAT_IBCS2)
355 		if (q->p_sysent->sv_sigtbl) {
356 			if ((p->p_xstat & 0xff) < q->p_sysent->sv_sigsize)
357 				sig = q->p_sysent->sv_sigtbl[p->p_xstat & 0xff];
358 			else
359 				sig = q->p_sysent->sv_sigsize + 1;
360 		} else
361 			sig = p->p_xstat;
362 #endif
363 		if (p->p_stat == SZOMB) {
364 			/* charge childs scheduling cpu usage to parent */
365 			if (curproc->p_pid != 1) {
366 				curproc->p_estcpu = min(curproc->p_estcpu +
367 				    p->p_estcpu, UCHAR_MAX);
368 			}
369 
370 			retval[0] = p->p_pid;
371 #if defined(COMPAT_43) || defined(COMPAT_IBCS2)
372 			if (uap->compat)
373 				retval[1] = sig;
374 			else
375 #endif
376 			if (uap->status) {
377 				status = p->p_xstat;	/* convert to int */
378 				if ((error = copyout((caddr_t)&status,
379 				    (caddr_t)uap->status, sizeof(status))))
380 					return (error);
381 			}
382 			if (uap->rusage && (error = copyout((caddr_t)p->p_ru,
383 			    (caddr_t)uap->rusage, sizeof (struct rusage))))
384 				return (error);
385 			/*
386 			 * If we got the child via a ptrace 'attach',
387 			 * we need to give it back to the old parent.
388 			 */
389 			if (p->p_oppid && (t = pfind(p->p_oppid))) {
390 				p->p_oppid = 0;
391 				proc_reparent(p, t);
392 				psignal(t, SIGCHLD);
393 				wakeup((caddr_t)t);
394 				return (0);
395 			}
396 			p->p_xstat = 0;
397 			ruadd(&q->p_stats->p_cru, p->p_ru);
398 			FREE(p->p_ru, M_ZOMBIE);
399 			p->p_ru = NULL;
400 
401 			/*
402 			 * Decrement the count of procs running with this uid.
403 			 */
404 			(void)chgproccnt(p->p_cred->p_ruid, -1);
405 
406 			/*
407 			 * Free up credentials.
408 			 */
409 			if (--p->p_cred->p_refcnt == 0) {
410 				crfree(p->p_cred->pc_ucred);
411 				FREE(p->p_cred, M_SUBPROC);
412 				p->p_cred = NULL;
413 			}
414 
415 			/*
416 			 * Release reference to text vnode
417 			 */
418 			if (p->p_textvp)
419 				vrele(p->p_textvp);
420 
421 			/*
422 			 * Finally finished with old proc entry.
423 			 * Unlink it from its process group and free it.
424 			 */
425 			leavepgrp(p);
426 			if ((*p->p_prev = p->p_next))	/* off zombproc */
427 				p->p_next->p_prev = p->p_prev;
428 			if ((q = p->p_ysptr))
429 				q->p_osptr = p->p_osptr;
430 			if ((q = p->p_osptr))
431 				q->p_ysptr = p->p_ysptr;
432 			if ((q = p->p_pptr)->p_cptr == p)
433 				q->p_cptr = p->p_osptr;
434 
435 			/*
436 			 * Give machine-dependent layer a chance
437 			 * to free anything that cpu_exit couldn't
438 			 * release while still running in process context.
439 			 */
440 			cpu_wait(p);
441 			FREE(p, M_PROC);
442 			nprocs--;
443 			return (0);
444 		}
445 		if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
446 		    (p->p_flag & P_TRACED || uap->options & WUNTRACED)) {
447 			p->p_flag |= P_WAITED;
448 			retval[0] = p->p_pid;
449 #if defined(COMPAT_43) || defined(COMPAT_IBCS2)
450 			if (uap->compat) {
451 				retval[1] = W_STOPCODE(sig);
452 				error = 0;
453 			} else
454 #endif
455 			if (uap->status) {
456 				status = W_STOPCODE(p->p_xstat);
457 				error = copyout((caddr_t)&status,
458 					(caddr_t)uap->status, sizeof(status));
459 			} else
460 				error = 0;
461 			return (error);
462 		}
463 	}
464 	if (nfound == 0)
465 		return (ECHILD);
466 	if (uap->options & WNOHANG) {
467 		retval[0] = 0;
468 		return (0);
469 	}
470 	if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)))
471 		return (error);
472 	goto loop;
473 }
474 
475 /*
476  * make process 'parent' the new parent of process 'child'.
477  */
478 void
479 proc_reparent(child, parent)
480 	register struct proc *child;
481 	register struct proc *parent;
482 {
483 	register struct proc *o;
484 	register struct proc *y;
485 
486 	if (child->p_pptr == parent)
487 		return;
488 
489 	/* fix up the child linkage for the old parent */
490 	o = child->p_osptr;
491 	y = child->p_ysptr;
492 	if (y)
493 		y->p_osptr = o;
494 	if (o)
495 		o->p_ysptr = y;
496 	if (child->p_pptr->p_cptr == child)
497 		child->p_pptr->p_cptr = o;
498 
499 	/* fix up child linkage for new parent */
500 	o = parent->p_cptr;
501 	if (o)
502 		o->p_ysptr = child;
503 	child->p_osptr = o;
504 	child->p_ysptr = NULL;
505 	parent->p_cptr = child;
506 	child->p_pptr = parent;
507 }
508