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