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