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