Home | History | Annotate | Line # | Download | only in kern
kern_exit.c revision 1.54
      1 /*	$NetBSD: kern_exit.c,v 1.54 1998/08/31 23:20:16 thorpej 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.10 (Berkeley) 2/23/95
     41  */
     42 
     43 #include "opt_ktrace.h"
     44 #include "opt_uvm.h"
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/map.h>
     49 #include <sys/ioctl.h>
     50 #include <sys/proc.h>
     51 #include <sys/tty.h>
     52 #include <sys/time.h>
     53 #include <sys/resource.h>
     54 #include <sys/kernel.h>
     55 #include <sys/ktrace.h>
     56 #include <sys/proc.h>
     57 #include <sys/buf.h>
     58 #include <sys/wait.h>
     59 #include <sys/file.h>
     60 #include <sys/vnode.h>
     61 #include <sys/syslog.h>
     62 #include <sys/malloc.h>
     63 #include <sys/pool.h>
     64 #include <sys/resourcevar.h>
     65 #include <sys/ptrace.h>
     66 #include <sys/acct.h>
     67 #include <sys/filedesc.h>
     68 #include <sys/signalvar.h>
     69 #ifdef SYSVSHM
     70 #include <sys/shm.h>
     71 #endif
     72 #ifdef SYSVSEM
     73 #include <sys/sem.h>
     74 #endif
     75 
     76 #include <sys/mount.h>
     77 #include <sys/syscallargs.h>
     78 
     79 #include <machine/cpu.h>
     80 
     81 #include <vm/vm.h>
     82 #include <vm/vm_kern.h>
     83 
     84 #if defined(UVM)
     85 #include <uvm/uvm_extern.h>
     86 #endif
     87 
     88 /*
     89  * exit --
     90  *	Death of process.
     91  */
     92 int
     93 sys_exit(p, v, retval)
     94 	struct proc *p;
     95 	void *v;
     96 	register_t *retval;
     97 {
     98 	struct sys_exit_args /* {
     99 		syscallarg(int) rval;
    100 	} */ *uap = v;
    101 
    102 	exit1(p, W_EXITCODE(SCARG(uap, rval), 0));
    103 	/* NOTREACHED */
    104 	return (0);
    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  */
    112 void
    113 exit1(p, rv)
    114 	register struct proc *p;
    115 	int rv;
    116 {
    117 	register struct proc *q, *nq;
    118 	register struct vmspace *vm;
    119 
    120 	if (p->p_pid == 1)
    121 		panic("init died (signal %d, exit %d)",
    122 		    WTERMSIG(rv), WEXITSTATUS(rv));
    123 #ifdef PGINPROF
    124 	vmsizmon();
    125 #endif
    126 	if (p->p_flag & P_PROFIL)
    127 		stopprofclock(p);
    128 	MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage),
    129 		M_ZOMBIE, M_WAITOK);
    130 	/*
    131 	 * If parent is waiting for us to exit or exec, P_PPWAIT is set; we
    132 	 * wake up the parent early to avoid deadlock.
    133 	 */
    134 	p->p_flag |= P_WEXIT;
    135 	if (p->p_flag & P_PPWAIT) {
    136 		p->p_flag &= ~P_PPWAIT;
    137 		wakeup((caddr_t)p->p_pptr);
    138 	}
    139 	p->p_sigignore = ~0;
    140 	p->p_siglist = 0;
    141 	untimeout(realitexpire, (caddr_t)p);
    142 
    143 	/*
    144 	 * Close open files and release open-file table.
    145 	 * This may block!
    146 	 */
    147 	fdfree(p);
    148 
    149 	/* The next three chunks should probably be moved to vmspace_exit. */
    150 	vm = p->p_vmspace;
    151 #ifdef SYSVSHM
    152 	if (vm->vm_shm && vm->vm_refcnt == 1)
    153 		shmexit(vm);
    154 #endif
    155 #ifdef SYSVSEM
    156 	semexit(p);
    157 #endif
    158 	/*
    159 	 * Release user portion of address space.
    160 	 * This releases references to vnodes,
    161 	 * which could cause I/O if the file has been unlinked.
    162 	 * Need to do this early enough that we can still sleep.
    163 	 * Can't free the entire vmspace as the kernel stack
    164 	 * may be mapped within that space also.
    165 	 */
    166 #if defined(UVM)
    167 	if (vm->vm_refcnt == 1)
    168 		(void) uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
    169 		    VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
    170 #else
    171 	if (vm->vm_refcnt == 1)
    172 		(void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS,
    173 		    VM_MAXUSER_ADDRESS);
    174 #endif
    175 
    176 	if (SESS_LEADER(p)) {
    177 		register struct session *sp = p->p_session;
    178 
    179 		if (sp->s_ttyvp) {
    180 			/*
    181 			 * Controlling process.
    182 			 * Signal foreground pgrp,
    183 			 * drain controlling terminal
    184 			 * and revoke access to controlling terminal.
    185 			 */
    186 			if (sp->s_ttyp->t_session == sp) {
    187 				if (sp->s_ttyp->t_pgrp)
    188 					pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
    189 				(void) ttywait(sp->s_ttyp);
    190 				/*
    191 				 * The tty could have been revoked
    192 				 * if we blocked.
    193 				 */
    194 				if (sp->s_ttyvp)
    195 					VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
    196 			}
    197 			if (sp->s_ttyvp)
    198 				vrele(sp->s_ttyvp);
    199 			sp->s_ttyvp = NULL;
    200 			/*
    201 			 * s_ttyp is not zero'd; we use this to indicate
    202 			 * that the session once had a controlling terminal.
    203 			 * (for logging and informational purposes)
    204 			 */
    205 		}
    206 		sp->s_leader = NULL;
    207 	}
    208 	fixjobc(p, p->p_pgrp, 0);
    209 	p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
    210 	(void)acct_process(p);
    211 #ifdef KTRACE
    212 	/*
    213 	 * release trace file
    214 	 */
    215 	ktrderef(p);
    216 #endif
    217 	/*
    218 	 * Remove proc from allproc queue and pidhash chain.
    219 	 * Place onto zombproc.  Unlink from parent's child list.
    220 	 */
    221 	LIST_REMOVE(p, p_list);
    222 	LIST_INSERT_HEAD(&zombproc, p, p_list);
    223 	p->p_stat = SZOMB;
    224 
    225 	LIST_REMOVE(p, p_hash);
    226 
    227 	q = p->p_children.lh_first;
    228 	if (q)		/* only need this if any child is S_ZOMB */
    229 		wakeup((caddr_t)initproc);
    230 	for (; q != 0; q = nq) {
    231 		nq = q->p_sibling.le_next;
    232 		proc_reparent(q, 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|P_WAITED|P_FSTRACE);
    239 			psignal(q, SIGKILL);
    240 		}
    241 	}
    242 
    243 	/*
    244 	 * Save exit status and final rusage info, adding in child rusage
    245 	 * info and self times.
    246 	 */
    247 	p->p_xstat = rv;
    248 	*p->p_ru = p->p_stats->p_ru;
    249 	calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL);
    250 	ruadd(p->p_ru, &p->p_stats->p_cru);
    251 
    252 	/*
    253 	 * Notify parent that we're gone.
    254 	 */
    255 	if ((p->p_flag & P_FSTRACE) == 0)
    256 		psignal(p->p_pptr, SIGCHLD);
    257 	wakeup((caddr_t)p->p_pptr);
    258 
    259 	/*
    260 	 * Clear curproc after we've done all operations
    261 	 * that could block, and before tearing down the rest
    262 	 * of the process state that might be used from clock, etc.
    263 	 * Also, can't clear curproc while we're still runnable,
    264 	 * as we're not on a run queue (we are current, just not
    265 	 * a proper proc any longer!).
    266 	 *
    267 	 * Other substructures are freed from wait().
    268 	 */
    269 	curproc = NULL;
    270 	if (--p->p_limit->p_refcnt == 0)
    271 		pool_put(&plimit_pool, p->p_limit);
    272 
    273 	/*
    274 	 * Finally, call machine-dependent code to release the remaining
    275 	 * resources including address space, the kernel stack and pcb.
    276 	 * The address space is released by "vmspace_free(p->p_vmspace)";
    277 	 * This is machine-dependent, as we may have to change stacks
    278 	 * or ensure that the current one isn't reallocated before we
    279 	 * finish.  cpu_exit will end with a call to cpu_swtch(), finishing
    280 	 * our execution (pun intended).
    281 	 */
    282 	cpu_exit(p);
    283 }
    284 
    285 int
    286 sys_wait4(q, v, retval)
    287 	register struct proc *q;
    288 	void *v;
    289 	register_t *retval;
    290 {
    291 	register struct sys_wait4_args /* {
    292 		syscallarg(int) pid;
    293 		syscallarg(int *) status;
    294 		syscallarg(int) options;
    295 		syscallarg(struct rusage *) rusage;
    296 	} */ *uap = v;
    297 	register int nfound;
    298 	register struct proc *p, *t;
    299 	int status, error;
    300 
    301 	if (SCARG(uap, pid) == 0)
    302 		SCARG(uap, pid) = -q->p_pgid;
    303 	if (SCARG(uap, options) &~ (WUNTRACED|WNOHANG))
    304 		return (EINVAL);
    305 
    306 loop:
    307 	nfound = 0;
    308 	for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
    309 		if (SCARG(uap, pid) != WAIT_ANY &&
    310 		    p->p_pid != SCARG(uap, pid) &&
    311 		    p->p_pgid != -SCARG(uap, pid))
    312 			continue;
    313 		nfound++;
    314 		if (p->p_stat == SZOMB) {
    315 			retval[0] = p->p_pid;
    316 
    317 			if (SCARG(uap, status)) {
    318 				status = p->p_xstat;	/* convert to int */
    319 				error = copyout((caddr_t)&status,
    320 						(caddr_t)SCARG(uap, status),
    321 						sizeof(status));
    322 				if (error)
    323 					return (error);
    324 			}
    325 			if (SCARG(uap, rusage) &&
    326 			    (error = copyout((caddr_t)p->p_ru,
    327 			    (caddr_t)SCARG(uap, rusage),
    328 			    sizeof(struct rusage))))
    329 				return (error);
    330 			/*
    331 			 * If we got the child via ptrace(2) or procfs, and
    332 			 * the parent is different (meaning the process was
    333 			 * attached, rather than run as a child), then we need
    334 			 * to give it back to the old parent, and send the
    335 			 * parent a SIGCHLD.  The rest of the cleanup will be
    336 			 * done when the old parent waits on the child.
    337 			 */
    338 			if ((p->p_flag & P_TRACED) &&
    339 			    p->p_oppid != p->p_pptr->p_pid) {
    340 				t = pfind(p->p_oppid);
    341 				proc_reparent(p, t ? t : initproc);
    342 				p->p_oppid = 0;
    343 				p->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE);
    344 				psignal(p->p_pptr, SIGCHLD);
    345 				wakeup((caddr_t)p->p_pptr);
    346 				return (0);
    347 			}
    348 			p->p_xstat = 0;
    349 			ruadd(&q->p_stats->p_cru, p->p_ru);
    350 			FREE(p->p_ru, M_ZOMBIE);
    351 
    352 			/*
    353 			 * Finally finished with old proc entry.
    354 			 * Unlink it from its process group and free it.
    355 			 */
    356 			leavepgrp(p);
    357 			LIST_REMOVE(p, p_list);	/* off zombproc */
    358 			LIST_REMOVE(p, p_sibling);
    359 
    360 			/*
    361 			 * Decrement the count of procs running with this uid.
    362 			 */
    363 			(void)chgproccnt(p->p_cred->p_ruid, -1);
    364 
    365 			/*
    366 			 * Free up credentials.
    367 			 */
    368 			if (--p->p_cred->p_refcnt == 0) {
    369 				crfree(p->p_cred->pc_ucred);
    370 				pool_put(&pcred_pool, p->p_cred);
    371 			}
    372 
    373 			/*
    374 			 * Release reference to text vnode
    375 			 */
    376 			if (p->p_textvp)
    377 				vrele(p->p_textvp);
    378 
    379 			/*
    380 			 * Give machine-dependent layer a chance
    381 			 * to free anything that cpu_exit couldn't
    382 			 * release while still running in process context.
    383 			 */
    384 			cpu_wait(p);
    385 			pool_put(&proc_pool, p);
    386 			nprocs--;
    387 			return (0);
    388 		}
    389 		if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
    390 		    (p->p_flag & P_TRACED || SCARG(uap, options) & WUNTRACED)) {
    391 			p->p_flag |= P_WAITED;
    392 			retval[0] = p->p_pid;
    393 
    394 			if (SCARG(uap, status)) {
    395 				status = W_STOPCODE(p->p_xstat);
    396 				error = copyout((caddr_t)&status,
    397 				    (caddr_t)SCARG(uap, status),
    398 				    sizeof(status));
    399 			} else
    400 				error = 0;
    401 			return (error);
    402 		}
    403 	}
    404 	if (nfound == 0)
    405 		return (ECHILD);
    406 	if (SCARG(uap, options) & WNOHANG) {
    407 		retval[0] = 0;
    408 		return (0);
    409 	}
    410 	if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)) != 0)
    411 		return (error);
    412 	goto loop;
    413 }
    414 
    415 /*
    416  * make process 'parent' the new parent of process 'child'.
    417  */
    418 void
    419 proc_reparent(child, parent)
    420 	register struct proc *child;
    421 	register struct proc *parent;
    422 {
    423 
    424 	if (child->p_pptr == parent)
    425 		return;
    426 
    427 	LIST_REMOVE(child, p_sibling);
    428 	LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
    429 	child->p_pptr = parent;
    430 }
    431