Home | History | Annotate | Line # | Download | only in kern
kern_exit.c revision 1.37
      1 /*	$NetBSD: kern_exit.c,v 1.37 1996/02/09 01:19:21 mycroft Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1989, 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  * (c) UNIX System Laboratories, Inc.
      7  * All or some portions of this file are derived from material licensed
      8  * to the University of California by American Telephone and Telegraph
      9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  * the permission of UNIX System Laboratories, Inc.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the University of
     23  *	California, Berkeley and its contributors.
     24  * 4. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  *	@(#)kern_exit.c	8.7 (Berkeley) 2/12/94
     41  */
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/map.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/proc.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/ptrace.h>
     61 #include <sys/acct.h>
     62 #include <sys/filedesc.h>
     63 #include <sys/signalvar.h>
     64 
     65 #include <sys/mount.h>
     66 #include <sys/syscallargs.h>
     67 
     68 #include <machine/cpu.h>
     69 
     70 #include <vm/vm.h>
     71 #include <vm/vm_kern.h>
     72 
     73 #include <kern/kern_extern.h>
     74 
     75 /*
     76  * exit --
     77  *	Death of process.
     78  */
     79 int
     80 sys_exit(p, v, retval)
     81 	struct proc *p;
     82 	void *v;
     83 	register_t *retval;
     84 {
     85 	struct sys_exit_args /* {
     86 		syscallarg(int) rval;
     87 	} */ *uap = v;
     88 
     89 	exit1(p, W_EXITCODE(SCARG(uap, rval), 0));
     90 	/* NOTREACHED */
     91 	return (0);
     92 }
     93 
     94 /*
     95  * Exit: deallocate address space and other resources, change proc state
     96  * to zombie, and unlink proc from allproc and parent's lists.  Save exit
     97  * status and rusage for wait().  Check for child processes and orphan them.
     98  */
     99 void
    100 exit1(p, rv)
    101 	register struct proc *p;
    102 	int rv;
    103 {
    104 	register struct proc *q, *nq;
    105 	register struct vmspace *vm;
    106 
    107 	if (p->p_pid == 1)
    108 		panic("init died (signal %d, exit %d)",
    109 		    WTERMSIG(rv), WEXITSTATUS(rv));
    110 #ifdef PGINPROF
    111 	vmsizmon();
    112 #endif
    113 	if (p->p_flag & P_PROFIL)
    114 		stopprofclock(p);
    115 	MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage),
    116 		M_ZOMBIE, M_WAITOK);
    117 	/*
    118 	 * If parent is waiting for us to exit or exec, P_PPWAIT is set; we
    119 	 * wake up the parent early to avoid deadlock.
    120 	 */
    121 	p->p_flag |= P_WEXIT;
    122 	p->p_flag &= ~P_TRACED;
    123 	if (p->p_flag & P_PPWAIT) {
    124 		p->p_flag &= ~P_PPWAIT;
    125 		wakeup((caddr_t)p->p_pptr);
    126 	}
    127 	p->p_sigignore = ~0;
    128 	p->p_siglist = 0;
    129 	untimeout(realitexpire, (caddr_t)p);
    130 
    131 	/*
    132 	 * Close open files and release open-file table.
    133 	 * This may block!
    134 	 */
    135 	fdfree(p);
    136 
    137 	/* The next three chunks should probably be moved to vmspace_exit. */
    138 	vm = p->p_vmspace;
    139 #ifdef SYSVSHM
    140 	if (vm->vm_shm)
    141 		shmexit(p);
    142 #endif
    143 #ifdef SYSVSEM
    144 	semexit(p);
    145 #endif
    146 	/*
    147 	 * Release user portion of address space.
    148 	 * This releases references to vnodes,
    149 	 * which could cause I/O if the file has been unlinked.
    150 	 * Need to do this early enough that we can still sleep.
    151 	 * Can't free the entire vmspace as the kernel stack
    152 	 * may be mapped within that space also.
    153 	 */
    154 	if (vm->vm_refcnt == 1)
    155 		(void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS,
    156 		    VM_MAXUSER_ADDRESS);
    157 
    158 	if (SESS_LEADER(p)) {
    159 		register struct session *sp = p->p_session;
    160 
    161 		if (sp->s_ttyvp) {
    162 			/*
    163 			 * Controlling process.
    164 			 * Signal foreground pgrp,
    165 			 * drain controlling terminal
    166 			 * and revoke access to controlling terminal.
    167 			 */
    168 			if (sp->s_ttyp->t_session == sp) {
    169 				if (sp->s_ttyp->t_pgrp)
    170 					pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
    171 				(void) ttywait(sp->s_ttyp);
    172 				/*
    173 				 * The tty could have been revoked
    174 				 * if we blocked.
    175 				 */
    176 				if (sp->s_ttyvp)
    177 					vgoneall(sp->s_ttyvp);
    178 			}
    179 			if (sp->s_ttyvp)
    180 				vrele(sp->s_ttyvp);
    181 			sp->s_ttyvp = NULL;
    182 			/*
    183 			 * s_ttyp is not zero'd; we use this to indicate
    184 			 * that the session once had a controlling terminal.
    185 			 * (for logging and informational purposes)
    186 			 */
    187 		}
    188 		sp->s_leader = NULL;
    189 	}
    190 	fixjobc(p, p->p_pgrp, 0);
    191 	p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
    192 	(void)acct_process(p);
    193 #ifdef KTRACE
    194 	/*
    195 	 * release trace file
    196 	 */
    197 	p->p_traceflag = 0;	/* don't trace the vrele() */
    198 	if (p->p_tracep)
    199 		vrele(p->p_tracep);
    200 #endif
    201 	/*
    202 	 * Remove proc from allproc queue and pidhash chain.
    203 	 * Place onto zombproc.  Unlink from parent's child list.
    204 	 */
    205 	LIST_REMOVE(p, p_list);
    206 	LIST_INSERT_HEAD(&zombproc, p, p_list);
    207 	p->p_stat = SZOMB;
    208 
    209 	LIST_REMOVE(p, p_hash);
    210 
    211 	q = p->p_children.lh_first;
    212 	if (q)		/* only need this if any child is S_ZOMB */
    213 		wakeup((caddr_t)initproc);
    214 	for (; q != 0; q = nq) {
    215 		nq = q->p_sibling.le_next;
    216 		proc_reparent(q, initproc);
    217 		/*
    218 		 * Traced processes are killed
    219 		 * since their existence means someone is screwing up.
    220 		 */
    221 		if (q->p_flag & P_TRACED) {
    222 			q->p_flag &= ~P_TRACED;
    223 			psignal(q, SIGKILL);
    224 		}
    225 	}
    226 
    227 	/*
    228 	 * Save exit status and final rusage info, adding in child rusage
    229 	 * info and self times.
    230 	 */
    231 	p->p_xstat = rv;
    232 	*p->p_ru = p->p_stats->p_ru;
    233 	calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
    234 	ruadd(p->p_ru, &p->p_stats->p_cru);
    235 
    236 	/*
    237 	 * Notify parent that we're gone.
    238 	 */
    239 	psignal(p->p_pptr, SIGCHLD);
    240 	wakeup((caddr_t)p->p_pptr);
    241 	/*
    242 	 * Notify procfs debugger
    243 	 */
    244 	if (p->p_flag & P_FSTRACE)
    245 		wakeup((caddr_t)p);
    246 #if defined(tahoe)
    247 	/* move this to cpu_exit */
    248 	p->p_addr->u_pcb.pcb_savacc.faddr = (float *)NULL;
    249 #endif
    250 	/*
    251 	 * Clear curproc after we've done all operations
    252 	 * that could block, and before tearing down the rest
    253 	 * of the process state that might be used from clock, etc.
    254 	 * Also, can't clear curproc while we're still runnable,
    255 	 * as we're not on a run queue (we are current, just not
    256 	 * a proper proc any longer!).
    257 	 *
    258 	 * Other substructures are freed from wait().
    259 	 */
    260 	curproc = NULL;
    261 	if (--p->p_limit->p_refcnt == 0)
    262 		FREE(p->p_limit, M_SUBPROC);
    263 
    264 	/*
    265 	 * Finally, call machine-dependent code to release the remaining
    266 	 * resources including address space, the kernel stack and pcb.
    267 	 * The address space is released by "vmspace_free(p->p_vmspace)";
    268 	 * This is machine-dependent, as we may have to change stacks
    269 	 * or ensure that the current one isn't reallocated before we
    270 	 * finish.  cpu_exit will end with a call to cpu_swtch(), finishing
    271 	 * our execution (pun intended).
    272 	 */
    273 	cpu_exit(p);
    274 }
    275 
    276 int
    277 sys_wait4(q, v, retval)
    278 	register struct proc *q;
    279 	void *v;
    280 	register_t *retval;
    281 {
    282 	register struct sys_wait4_args /* {
    283 		syscallarg(int) pid;
    284 		syscallarg(int *) status;
    285 		syscallarg(int) options;
    286 		syscallarg(struct rusage *) rusage;
    287 	} */ *uap = v;
    288 	register int nfound;
    289 	register struct proc *p, *t;
    290 	int status, error;
    291 
    292 #ifdef COMPAT_09
    293 	SCARG(uap, pid) = (short)SCARG(uap, pid);
    294 #endif
    295 
    296 	if (SCARG(uap, pid) == 0)
    297 		SCARG(uap, pid) = -q->p_pgid;
    298 #ifdef notyet
    299 	if (SCARG(uap, options) &~ (WUNTRACED|WNOHANG))
    300 		return (EINVAL);
    301 #endif
    302 loop:
    303 	nfound = 0;
    304 	for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
    305 		if (SCARG(uap, pid) != WAIT_ANY &&
    306 		    p->p_pid != SCARG(uap, pid) &&
    307 		    p->p_pgid != -SCARG(uap, pid))
    308 			continue;
    309 		nfound++;
    310 		if (p->p_stat == SZOMB) {
    311 			retval[0] = p->p_pid;
    312 
    313 			if (SCARG(uap, status)) {
    314 				status = p->p_xstat;	/* convert to int */
    315 				error = copyout((caddr_t)&status,
    316 						(caddr_t)SCARG(uap, status),
    317 						sizeof(status));
    318 				if (error)
    319 					return (error);
    320 			}
    321 			if (SCARG(uap, rusage) &&
    322 			    (error = copyout((caddr_t)p->p_ru,
    323 			    (caddr_t)SCARG(uap, rusage),
    324 			    sizeof (struct rusage))))
    325 				return (error);
    326 			/*
    327 			 * If we got the child via a ptrace 'attach',
    328 			 * we need to give it back to the old parent.
    329 			 */
    330 			if (p->p_oppid && (t = pfind(p->p_oppid))) {
    331 				p->p_oppid = 0;
    332 				proc_reparent(p, t);
    333 				psignal(t, SIGCHLD);
    334 				wakeup((caddr_t)t);
    335 				return (0);
    336 			}
    337 			p->p_xstat = 0;
    338 			ruadd(&q->p_stats->p_cru, p->p_ru);
    339 			FREE(p->p_ru, M_ZOMBIE);
    340 
    341 			/*
    342 			 * Finally finished with old proc entry.
    343 			 * Unlink it from its process group and free it.
    344 			 */
    345 			leavepgrp(p);
    346 			LIST_REMOVE(p, p_list);	/* off zombproc */
    347 			LIST_REMOVE(p, p_sibling);
    348 
    349 			/*
    350 			 * Decrement the count of procs running with this uid.
    351 			 */
    352 			(void)chgproccnt(p->p_cred->p_ruid, -1);
    353 
    354 			/*
    355 			 * Free up credentials.
    356 			 */
    357 			if (--p->p_cred->p_refcnt == 0) {
    358 				crfree(p->p_cred->pc_ucred);
    359 				FREE(p->p_cred, M_SUBPROC);
    360 			}
    361 
    362 			/*
    363 			 * Release reference to text vnode
    364 			 */
    365 			if (p->p_textvp)
    366 				vrele(p->p_textvp);
    367 
    368 			/*
    369 			 * Give machine-dependent layer a chance
    370 			 * to free anything that cpu_exit couldn't
    371 			 * release while still running in process context.
    372 			 */
    373 			cpu_wait(p);
    374 			FREE(p, M_PROC);
    375 			nprocs--;
    376 			return (0);
    377 		}
    378 		if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
    379 		    (p->p_flag & P_TRACED || SCARG(uap, options) & WUNTRACED)) {
    380 			p->p_flag |= P_WAITED;
    381 			retval[0] = p->p_pid;
    382 
    383 			if (SCARG(uap, status)) {
    384 				status = W_STOPCODE(p->p_xstat);
    385 				error = copyout((caddr_t)&status,
    386 				    (caddr_t)SCARG(uap, status),
    387 				    sizeof(status));
    388 			} else
    389 				error = 0;
    390 			return (error);
    391 		}
    392 	}
    393 	if (nfound == 0)
    394 		return (ECHILD);
    395 	if (SCARG(uap, options) & WNOHANG) {
    396 		retval[0] = 0;
    397 		return (0);
    398 	}
    399 	if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)) != 0)
    400 		return (error);
    401 	goto loop;
    402 }
    403 
    404 /*
    405  * make process 'parent' the new parent of process 'child'.
    406  */
    407 void
    408 proc_reparent(child, parent)
    409 	register struct proc *child;
    410 	register struct proc *parent;
    411 {
    412 
    413 	if (child->p_pptr == parent)
    414 		return;
    415 
    416 	LIST_REMOVE(child, p_sibling);
    417 	LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
    418 	child->p_pptr = parent;
    419 }
    420