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