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