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