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