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