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