1996c772fSJohn Dyson /* 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1991, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * (c) UNIX System Laboratories, Inc. 5df8bae1dSRodney W. Grimes * All or some portions of this file are derived from material licensed 6df8bae1dSRodney W. Grimes * to the University of California by American Telephone and Telegraph 7df8bae1dSRodney W. Grimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8df8bae1dSRodney W. Grimes * the permission of UNIX System Laboratories, Inc. 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 11df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 12df8bae1dSRodney W. Grimes * are met: 13df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 15df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 17df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 18df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 19df8bae1dSRodney W. Grimes * must display the following acknowledgement: 20df8bae1dSRodney W. Grimes * This product includes software developed by the University of 21df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 22df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 23df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 24df8bae1dSRodney W. Grimes * without specific prior written permission. 25df8bae1dSRodney W. Grimes * 26df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36df8bae1dSRodney W. Grimes * SUCH DAMAGE. 37df8bae1dSRodney W. Grimes * 38df8bae1dSRodney W. Grimes * @(#)kern_exit.c 8.7 (Berkeley) 2/12/94 3901166e92SBruce Evans * $Id: kern_exit.c,v 1.59 1997/11/06 19:29:08 phk Exp $ 40df8bae1dSRodney W. Grimes */ 41df8bae1dSRodney W. Grimes 42db6a20e2SGarrett Wollman #include "opt_ktrace.h" 43db6a20e2SGarrett Wollman 44df8bae1dSRodney W. Grimes #include <sys/param.h> 45df8bae1dSRodney W. Grimes #include <sys/systm.h> 465fdb8324SBruce Evans #include <sys/sysproto.h> 47a1c995b6SPoul-Henning Kamp #include <sys/malloc.h> 48df8bae1dSRodney W. Grimes #include <sys/proc.h> 49df8bae1dSRodney W. Grimes #include <sys/tty.h> 50df8bae1dSRodney W. Grimes #include <sys/wait.h> 51df8bae1dSRodney W. Grimes #include <sys/vnode.h> 52df8bae1dSRodney W. Grimes #include <sys/resourcevar.h> 53797f2d22SPoul-Henning Kamp #include <sys/signalvar.h> 54df8bae1dSRodney W. Grimes #include <sys/ptrace.h> 557c409b8aSJeffrey Hsu #include <sys/acct.h> /* for acct_process() function prototype */ 56797f2d22SPoul-Henning Kamp #include <sys/filedesc.h> 57780dc5a8SPeter Wemm #include <sys/shm.h> 58780dc5a8SPeter Wemm #include <sys/sem.h> 595aaef07cSJohn Dyson #include <sys/aio.h> 60780dc5a8SPeter Wemm 61df8bae1dSRodney W. Grimes #ifdef COMPAT_43 62df8bae1dSRodney W. Grimes #include <machine/reg.h> 63df8bae1dSRodney W. Grimes #include <machine/psl.h> 64df8bae1dSRodney W. Grimes #endif 65b1037dcdSBruce Evans #include <machine/limits.h> /* for UCHAR_MAX = typeof(p_priority)_MAX */ 66df8bae1dSRodney W. Grimes 67df8bae1dSRodney W. Grimes #include <vm/vm.h> 68efeaf95aSDavid Greenman #include <vm/vm_param.h> 69996c772fSJohn Dyson #include <sys/lock.h> 70efeaf95aSDavid Greenman #include <vm/pmap.h> 71efeaf95aSDavid Greenman #include <vm/vm_map.h> 72df8bae1dSRodney W. Grimes 73a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_ZOMBIE, "zombie", "zombie proc status"); 7455166637SPoul-Henning Kamp 7501166e92SBruce Evans static int wait1 __P((struct proc *, struct wait_args *, int)); 765fdb8324SBruce Evans 77df8bae1dSRodney W. Grimes /* 78fed06968SJulian Elischer * callout list for things to do at exit time 79fed06968SJulian Elischer */ 80fed06968SJulian Elischer typedef struct exit_list_element { 81fed06968SJulian Elischer struct exit_list_element *next; 82fed06968SJulian Elischer exitlist_fn function; 83fed06968SJulian Elischer } *ele_p; 84fed06968SJulian Elischer 85fed06968SJulian Elischer static ele_p exit_list; 86fed06968SJulian Elischer 87fed06968SJulian Elischer /* 88df8bae1dSRodney W. Grimes * exit -- 89df8bae1dSRodney W. Grimes * Death of process. 90df8bae1dSRodney W. Grimes */ 91fc0b1dbfSBruce Evans void 92cb226aaaSPoul-Henning Kamp exit(p, uap) 93df8bae1dSRodney W. Grimes struct proc *p; 945fdb8324SBruce Evans struct rexit_args /* { 955fdb8324SBruce Evans int rval; 965fdb8324SBruce Evans } */ *uap; 97df8bae1dSRodney W. Grimes { 98df8bae1dSRodney W. Grimes 99df8bae1dSRodney W. Grimes exit1(p, W_EXITCODE(uap->rval, 0)); 100df8bae1dSRodney W. Grimes /* NOTREACHED */ 101df8bae1dSRodney W. Grimes } 102df8bae1dSRodney W. Grimes 103df8bae1dSRodney W. Grimes /* 104df8bae1dSRodney W. Grimes * Exit: deallocate address space and other resources, change proc state 105df8bae1dSRodney W. Grimes * to zombie, and unlink proc from allproc and parent's lists. Save exit 106df8bae1dSRodney W. Grimes * status and rusage for wait(). Check for child processes and orphan them. 107df8bae1dSRodney W. Grimes */ 108fc0b1dbfSBruce Evans void 109df8bae1dSRodney W. Grimes exit1(p, rv) 110df8bae1dSRodney W. Grimes register struct proc *p; 111df8bae1dSRodney W. Grimes int rv; 112df8bae1dSRodney W. Grimes { 113df8bae1dSRodney W. Grimes register struct proc *q, *nq; 114df8bae1dSRodney W. Grimes register struct vmspace *vm; 115fed06968SJulian Elischer ele_p ep = exit_list; 116df8bae1dSRodney W. Grimes 1175f7bd355SPoul-Henning Kamp if (p->p_pid == 1) { 1185f7bd355SPoul-Henning Kamp printf("init died (signal %d, exit %d)\n", 119df8bae1dSRodney W. Grimes WTERMSIG(rv), WEXITSTATUS(rv)); 1205f7bd355SPoul-Henning Kamp panic("Going nowhere without my init!"); 1215f7bd355SPoul-Henning Kamp } 1222c1011f7SJohn Dyson 1232244ea07SJohn Dyson aio_proc_rundown(p); 1242244ea07SJohn Dyson 1252c1011f7SJohn Dyson /* are we a task leader? */ 1262c1011f7SJohn Dyson if(p == p->p_leader) { 1272c1011f7SJohn Dyson struct kill_args killArgs; 1282c1011f7SJohn Dyson killArgs.signum = SIGKILL; 1292c1011f7SJohn Dyson q = p->p_peers; 1302c1011f7SJohn Dyson while(q) { 1312c1011f7SJohn Dyson killArgs.pid = q->p_pid; 1322c1011f7SJohn Dyson /* 1332c1011f7SJohn Dyson * The interface for kill is better 1342c1011f7SJohn Dyson * than the internal signal 1352c1011f7SJohn Dyson */ 136cb226aaaSPoul-Henning Kamp kill(p, &killArgs); 1372c1011f7SJohn Dyson nq = q; 1382c1011f7SJohn Dyson q = q->p_peers; 1392c1011f7SJohn Dyson /* 1402c1011f7SJohn Dyson * orphan the threads so we don't mess up 1412c1011f7SJohn Dyson * when they call exit 1422c1011f7SJohn Dyson */ 1432c1011f7SJohn Dyson nq->p_peers = 0; 1442c1011f7SJohn Dyson nq->p_leader = nq; 1452c1011f7SJohn Dyson } 1462c1011f7SJohn Dyson 1472c1011f7SJohn Dyson /* otherwise are we a peer? */ 1482c1011f7SJohn Dyson } else if(p->p_peers) { 1492c1011f7SJohn Dyson q = p->p_leader; 1502c1011f7SJohn Dyson while(q->p_peers != p) 1512c1011f7SJohn Dyson q = q->p_peers; 1522c1011f7SJohn Dyson q->p_peers = p->p_peers; 1532c1011f7SJohn Dyson } 1542c1011f7SJohn Dyson 155df8bae1dSRodney W. Grimes #ifdef PGINPROF 156df8bae1dSRodney W. Grimes vmsizmon(); 157df8bae1dSRodney W. Grimes #endif 158fed06968SJulian Elischer /* 159e0d898b4SJulian Elischer * Check if any LKMs need anything done at process exit. 160fed06968SJulian Elischer * e.g. SYSV IPC stuff 161fed06968SJulian Elischer * XXX what if one of these generates an error? 162fed06968SJulian Elischer */ 163fed06968SJulian Elischer while (ep) { 164fed06968SJulian Elischer (*ep->function)(p); 165fed06968SJulian Elischer ep = ep->next; 166fed06968SJulian Elischer } 167fed06968SJulian Elischer 168df8bae1dSRodney W. Grimes if (p->p_flag & P_PROFIL) 169df8bae1dSRodney W. Grimes stopprofclock(p); 170df8bae1dSRodney W. Grimes MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage), 171df8bae1dSRodney W. Grimes M_ZOMBIE, M_WAITOK); 172df8bae1dSRodney W. Grimes /* 173df8bae1dSRodney W. Grimes * If parent is waiting for us to exit or exec, 174df8bae1dSRodney W. Grimes * P_PPWAIT is set; we will wakeup the parent below. 175df8bae1dSRodney W. Grimes */ 176df8bae1dSRodney W. Grimes p->p_flag &= ~(P_TRACED | P_PPWAIT); 177df8bae1dSRodney W. Grimes p->p_flag |= P_WEXIT; 178df8bae1dSRodney W. Grimes p->p_sigignore = ~0; 179df8bae1dSRodney W. Grimes p->p_siglist = 0; 180ab36c067SJustin T. Gibbs if (timerisset(&p->p_realtimer.it_value)) 181ab36c067SJustin T. Gibbs untimeout(realitexpire, (caddr_t)p, p->p_ithandle); 182df8bae1dSRodney W. Grimes 183df8bae1dSRodney W. Grimes /* 184df8bae1dSRodney W. Grimes * Close open files and release open-file table. 185df8bae1dSRodney W. Grimes * This may block! 186df8bae1dSRodney W. Grimes */ 187df8bae1dSRodney W. Grimes fdfree(p); 188df8bae1dSRodney W. Grimes 18981090119SPeter Wemm /* 19081090119SPeter Wemm * XXX Shutdown SYSV semaphores 19181090119SPeter Wemm */ 192a353d785SJoerg Wunsch semexit(p); 193a353d785SJoerg Wunsch 194df8bae1dSRodney W. Grimes /* The next two chunks should probably be moved to vmspace_exit. */ 195df8bae1dSRodney W. Grimes vm = p->p_vmspace; 196df8bae1dSRodney W. Grimes /* 197df8bae1dSRodney W. Grimes * Release user portion of address space. 198df8bae1dSRodney W. Grimes * This releases references to vnodes, 199df8bae1dSRodney W. Grimes * which could cause I/O if the file has been unlinked. 200df8bae1dSRodney W. Grimes * Need to do this early enough that we can still sleep. 201df8bae1dSRodney W. Grimes * Can't free the entire vmspace as the kernel stack 202df8bae1dSRodney W. Grimes * may be mapped within that space also. 203df8bae1dSRodney W. Grimes */ 2049d3fbbb5SJohn Dyson if (vm->vm_refcnt == 1) { 205a2a1c95cSPeter Wemm if (vm->vm_shm) 206a2a1c95cSPeter Wemm shmexit(p); 2079d3fbbb5SJohn Dyson pmap_remove_pages(&vm->vm_pmap, VM_MIN_ADDRESS, 2089d3fbbb5SJohn Dyson VM_MAXUSER_ADDRESS); 20967bf6868SJohn Dyson (void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS, 21067bf6868SJohn Dyson VM_MAXUSER_ADDRESS); 2119d3fbbb5SJohn Dyson } 212df8bae1dSRodney W. Grimes 213df8bae1dSRodney W. Grimes if (SESS_LEADER(p)) { 214df8bae1dSRodney W. Grimes register struct session *sp = p->p_session; 215df8bae1dSRodney W. Grimes 216df8bae1dSRodney W. Grimes if (sp->s_ttyvp) { 217df8bae1dSRodney W. Grimes /* 218df8bae1dSRodney W. Grimes * Controlling process. 219df8bae1dSRodney W. Grimes * Signal foreground pgrp, 220df8bae1dSRodney W. Grimes * drain controlling terminal 221df8bae1dSRodney W. Grimes * and revoke access to controlling terminal. 222df8bae1dSRodney W. Grimes */ 223dd45d8adSJulian Elischer if (sp->s_ttyp && (sp->s_ttyp->t_session == sp)) { 224df8bae1dSRodney W. Grimes if (sp->s_ttyp->t_pgrp) 225df8bae1dSRodney W. Grimes pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1); 226df8bae1dSRodney W. Grimes (void) ttywait(sp->s_ttyp); 227df8bae1dSRodney W. Grimes /* 228df8bae1dSRodney W. Grimes * The tty could have been revoked 229df8bae1dSRodney W. Grimes * if we blocked. 230df8bae1dSRodney W. Grimes */ 231df8bae1dSRodney W. Grimes if (sp->s_ttyvp) 232996c772fSJohn Dyson VOP_REVOKE(sp->s_ttyvp, REVOKEALL); 233df8bae1dSRodney W. Grimes } 234df8bae1dSRodney W. Grimes if (sp->s_ttyvp) 235df8bae1dSRodney W. Grimes vrele(sp->s_ttyvp); 236df8bae1dSRodney W. Grimes sp->s_ttyvp = NULL; 237df8bae1dSRodney W. Grimes /* 238df8bae1dSRodney W. Grimes * s_ttyp is not zero'd; we use this to indicate 239df8bae1dSRodney W. Grimes * that the session once had a controlling terminal. 240df8bae1dSRodney W. Grimes * (for logging and informational purposes) 241df8bae1dSRodney W. Grimes */ 242df8bae1dSRodney W. Grimes } 243df8bae1dSRodney W. Grimes sp->s_leader = NULL; 244df8bae1dSRodney W. Grimes } 245df8bae1dSRodney W. Grimes fixjobc(p, p->p_pgrp, 0); 2461273ebf5SDavid Nugent if (p->p_limit->p_refcnt > 1 && 2471273ebf5SDavid Nugent (p->p_limit->p_lflags & PL_SHAREMOD) == 0) { 2481273ebf5SDavid Nugent p->p_limit->p_refcnt--; 2491273ebf5SDavid Nugent p->p_limit = limcopy(p->p_limit); 2501273ebf5SDavid Nugent } 251df8bae1dSRodney W. Grimes p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; 252df8bae1dSRodney W. Grimes (void)acct_process(p); 253df8bae1dSRodney W. Grimes #ifdef KTRACE 254df8bae1dSRodney W. Grimes /* 255df8bae1dSRodney W. Grimes * release trace file 256df8bae1dSRodney W. Grimes */ 257df8bae1dSRodney W. Grimes p->p_traceflag = 0; /* don't trace the vrele() */ 258df8bae1dSRodney W. Grimes if (p->p_tracep) 259df8bae1dSRodney W. Grimes vrele(p->p_tracep); 260df8bae1dSRodney W. Grimes #endif 261df8bae1dSRodney W. Grimes /* 262df8bae1dSRodney W. Grimes * Remove proc from allproc queue and pidhash chain. 263df8bae1dSRodney W. Grimes * Place onto zombproc. Unlink from parent's child list. 264df8bae1dSRodney W. Grimes */ 265b75356e1SJeffrey Hsu LIST_REMOVE(p, p_list); 266b75356e1SJeffrey Hsu LIST_INSERT_HEAD(&zombproc, p, p_list); 267df8bae1dSRodney W. Grimes p->p_stat = SZOMB; 268df8bae1dSRodney W. Grimes 269b75356e1SJeffrey Hsu LIST_REMOVE(p, p_hash); 270df8bae1dSRodney W. Grimes 271b75356e1SJeffrey Hsu q = p->p_children.lh_first; 272b75356e1SJeffrey Hsu if (q) /* only need this if any child is S_ZOMB */ 273df8bae1dSRodney W. Grimes wakeup((caddr_t) initproc); 274b75356e1SJeffrey Hsu for (; q != 0; q = nq) { 275b75356e1SJeffrey Hsu nq = q->p_sibling.le_next; 276b75356e1SJeffrey Hsu LIST_REMOVE(q, p_sibling); 277b75356e1SJeffrey Hsu LIST_INSERT_HEAD(&initproc->p_children, q, p_sibling); 278df8bae1dSRodney W. Grimes q->p_pptr = initproc; 279df8bae1dSRodney W. Grimes /* 280df8bae1dSRodney W. Grimes * Traced processes are killed 281df8bae1dSRodney W. Grimes * since their existence means someone is screwing up. 282df8bae1dSRodney W. Grimes */ 283df8bae1dSRodney W. Grimes if (q->p_flag & P_TRACED) { 284df8bae1dSRodney W. Grimes q->p_flag &= ~P_TRACED; 285df8bae1dSRodney W. Grimes psignal(q, SIGKILL); 286df8bae1dSRodney W. Grimes } 287df8bae1dSRodney W. Grimes } 288df8bae1dSRodney W. Grimes 289df8bae1dSRodney W. Grimes /* 290df8bae1dSRodney W. Grimes * Save exit status and final rusage info, adding in child rusage 291df8bae1dSRodney W. Grimes * info and self times. 292df8bae1dSRodney W. Grimes */ 293df8bae1dSRodney W. Grimes p->p_xstat = rv; 294df8bae1dSRodney W. Grimes *p->p_ru = p->p_stats->p_ru; 295df8bae1dSRodney W. Grimes calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL); 296df8bae1dSRodney W. Grimes ruadd(p->p_ru, &p->p_stats->p_cru); 297df8bae1dSRodney W. Grimes 298df8bae1dSRodney W. Grimes /* 299245f17d4SJoerg Wunsch * Notify parent that we're gone. If parent has the P_NOCLDWAIT 300245f17d4SJoerg Wunsch * flag set, notify process 1 instead (and hope it will handle 301245f17d4SJoerg Wunsch * this situation). 302df8bae1dSRodney W. Grimes */ 303245f17d4SJoerg Wunsch if (p->p_pptr->p_flag & P_NOCLDWAIT) { 304245f17d4SJoerg Wunsch struct proc *pp = p->p_pptr; 305245f17d4SJoerg Wunsch proc_reparent(p, initproc); 306245f17d4SJoerg Wunsch /* 307245f17d4SJoerg Wunsch * If this was the last child of our parent, notify 308245f17d4SJoerg Wunsch * parent, so in case he was wait(2)ing, he will 309245f17d4SJoerg Wunsch * continue. 310245f17d4SJoerg Wunsch */ 311245f17d4SJoerg Wunsch if (LIST_EMPTY(&pp->p_children)) 312245f17d4SJoerg Wunsch wakeup((caddr_t)pp); 313245f17d4SJoerg Wunsch } 314245f17d4SJoerg Wunsch 315df8bae1dSRodney W. Grimes psignal(p->p_pptr, SIGCHLD); 316df8bae1dSRodney W. Grimes wakeup((caddr_t)p->p_pptr); 317df8bae1dSRodney W. Grimes #if defined(tahoe) 318df8bae1dSRodney W. Grimes /* move this to cpu_exit */ 319df8bae1dSRodney W. Grimes p->p_addr->u_pcb.pcb_savacc.faddr = (float *)NULL; 320df8bae1dSRodney W. Grimes #endif 321df8bae1dSRodney W. Grimes /* 322df8bae1dSRodney W. Grimes * Clear curproc after we've done all operations 323df8bae1dSRodney W. Grimes * that could block, and before tearing down the rest 324df8bae1dSRodney W. Grimes * of the process state that might be used from clock, etc. 325df8bae1dSRodney W. Grimes * Also, can't clear curproc while we're still runnable, 326df8bae1dSRodney W. Grimes * as we're not on a run queue (we are current, just not 327df8bae1dSRodney W. Grimes * a proper proc any longer!). 328df8bae1dSRodney W. Grimes * 329df8bae1dSRodney W. Grimes * Other substructures are freed from wait(). 330df8bae1dSRodney W. Grimes */ 331df8bae1dSRodney W. Grimes curproc = NULL; 332be6a1d14SDavid Greenman if (--p->p_limit->p_refcnt == 0) { 333df8bae1dSRodney W. Grimes FREE(p->p_limit, M_SUBPROC); 334be6a1d14SDavid Greenman p->p_limit = NULL; 335be6a1d14SDavid Greenman } 336df8bae1dSRodney W. Grimes 337df8bae1dSRodney W. Grimes /* 338df8bae1dSRodney W. Grimes * Finally, call machine-dependent code to release the remaining 339df8bae1dSRodney W. Grimes * resources including address space, the kernel stack and pcb. 340df8bae1dSRodney W. Grimes * The address space is released by "vmspace_free(p->p_vmspace)"; 341df8bae1dSRodney W. Grimes * This is machine-dependent, as we may have to change stacks 342df8bae1dSRodney W. Grimes * or ensure that the current one isn't reallocated before we 34378d7e629SBruce Evans * finish. cpu_exit will end with a call to cpu_switch(), finishing 344df8bae1dSRodney W. Grimes * our execution (pun intended). 345df8bae1dSRodney W. Grimes */ 346df8bae1dSRodney W. Grimes cpu_exit(p); 347df8bae1dSRodney W. Grimes } 348df8bae1dSRodney W. Grimes 349b2f9e8b1SBruce Evans #ifdef COMPAT_43 350df8bae1dSRodney W. Grimes #if defined(hp300) || defined(luna68k) 351df8bae1dSRodney W. Grimes #include <machine/frame.h> 352df8bae1dSRodney W. Grimes #define GETPS(rp) ((struct frame *)(rp))->f_sr 353df8bae1dSRodney W. Grimes #else 354df8bae1dSRodney W. Grimes #define GETPS(rp) (rp)[PS] 355df8bae1dSRodney W. Grimes #endif 356df8bae1dSRodney W. Grimes 35726f9a767SRodney W. Grimes int 358cb226aaaSPoul-Henning Kamp owait(p, uap) 359df8bae1dSRodney W. Grimes struct proc *p; 3605fdb8324SBruce Evans register struct owait_args /* { 3615fdb8324SBruce Evans int dummy; 3625fdb8324SBruce Evans } */ *uap; 363df8bae1dSRodney W. Grimes { 36493c9414eSSteven Wallace struct wait_args w; 365df8bae1dSRodney W. Grimes 366df8bae1dSRodney W. Grimes #ifdef PSL_ALLCC 367df8bae1dSRodney W. Grimes if ((GETPS(p->p_md.md_regs) & PSL_ALLCC) != PSL_ALLCC) { 36893c9414eSSteven Wallace w.options = 0; 36993c9414eSSteven Wallace w.rusage = NULL; 370df8bae1dSRodney W. Grimes } else { 37193c9414eSSteven Wallace w.options = p->p_md.md_regs[R0]; 37293c9414eSSteven Wallace w.rusage = (struct rusage *)p->p_md.md_regs[R1]; 373df8bae1dSRodney W. Grimes } 374df8bae1dSRodney W. Grimes #else 37593c9414eSSteven Wallace w.options = 0; 37693c9414eSSteven Wallace w.rusage = NULL; 377df8bae1dSRodney W. Grimes #endif 37893c9414eSSteven Wallace w.pid = WAIT_ANY; 37993c9414eSSteven Wallace w.status = NULL; 38001166e92SBruce Evans return (wait1(p, &w, 1)); 381df8bae1dSRodney W. Grimes } 382b2f9e8b1SBruce Evans #endif /* COMPAT_43 */ 383df8bae1dSRodney W. Grimes 38426f9a767SRodney W. Grimes int 385cb226aaaSPoul-Henning Kamp wait4(p, uap) 386df8bae1dSRodney W. Grimes struct proc *p; 387df8bae1dSRodney W. Grimes struct wait_args *uap; 388df8bae1dSRodney W. Grimes { 3895fdb8324SBruce Evans 39001166e92SBruce Evans return (wait1(p, uap, 0)); 391df8bae1dSRodney W. Grimes } 392df8bae1dSRodney W. Grimes 39393c9414eSSteven Wallace static int 39401166e92SBruce Evans wait1(q, uap, compat) 395df8bae1dSRodney W. Grimes register struct proc *q; 3965fdb8324SBruce Evans register struct wait_args /* { 3975fdb8324SBruce Evans int pid; 3985fdb8324SBruce Evans int *status; 3995fdb8324SBruce Evans int options; 4005fdb8324SBruce Evans struct rusage *rusage; 4015fdb8324SBruce Evans } */ *uap; 40293c9414eSSteven Wallace int compat; 403df8bae1dSRodney W. Grimes { 404df8bae1dSRodney W. Grimes register int nfound; 405df8bae1dSRodney W. Grimes register struct proc *p, *t; 406f23c6d68SSteven Wallace int status, error; 407df8bae1dSRodney W. Grimes 408df8bae1dSRodney W. Grimes if (uap->pid == 0) 409df8bae1dSRodney W. Grimes uap->pid = -q->p_pgid; 410df8bae1dSRodney W. Grimes if (uap->options &~ (WUNTRACED|WNOHANG)) 411df8bae1dSRodney W. Grimes return (EINVAL); 412df8bae1dSRodney W. Grimes loop: 413df8bae1dSRodney W. Grimes nfound = 0; 414b75356e1SJeffrey Hsu for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) { 415df8bae1dSRodney W. Grimes if (uap->pid != WAIT_ANY && 416df8bae1dSRodney W. Grimes p->p_pid != uap->pid && p->p_pgid != -uap->pid) 417df8bae1dSRodney W. Grimes continue; 418df8bae1dSRodney W. Grimes nfound++; 419df8bae1dSRodney W. Grimes if (p->p_stat == SZOMB) { 4200d2afceeSDavid Greenman /* charge childs scheduling cpu usage to parent */ 421d5c4431eSDavid Greenman if (curproc->p_pid != 1) { 422d5c4431eSDavid Greenman curproc->p_estcpu = min(curproc->p_estcpu + 423d5c4431eSDavid Greenman p->p_estcpu, UCHAR_MAX); 424d5c4431eSDavid Greenman } 4250d2afceeSDavid Greenman 42601166e92SBruce Evans q->p_retval[0] = p->p_pid; 427b2f9e8b1SBruce Evans #ifdef COMPAT_43 42893c9414eSSteven Wallace if (compat) 42901166e92SBruce Evans q->p_retval[1] = p->p_xstat; 430df8bae1dSRodney W. Grimes else 431df8bae1dSRodney W. Grimes #endif 432df8bae1dSRodney W. Grimes if (uap->status) { 433df8bae1dSRodney W. Grimes status = p->p_xstat; /* convert to int */ 434bb56ec4aSPoul-Henning Kamp if ((error = copyout((caddr_t)&status, 435bb56ec4aSPoul-Henning Kamp (caddr_t)uap->status, sizeof(status)))) 436df8bae1dSRodney W. Grimes return (error); 437df8bae1dSRodney W. Grimes } 438df8bae1dSRodney W. Grimes if (uap->rusage && (error = copyout((caddr_t)p->p_ru, 439df8bae1dSRodney W. Grimes (caddr_t)uap->rusage, sizeof (struct rusage)))) 440df8bae1dSRodney W. Grimes return (error); 441df8bae1dSRodney W. Grimes /* 442df8bae1dSRodney W. Grimes * If we got the child via a ptrace 'attach', 443df8bae1dSRodney W. Grimes * we need to give it back to the old parent. 444df8bae1dSRodney W. Grimes */ 445df8bae1dSRodney W. Grimes if (p->p_oppid && (t = pfind(p->p_oppid))) { 446df8bae1dSRodney W. Grimes p->p_oppid = 0; 447df8bae1dSRodney W. Grimes proc_reparent(p, t); 448df8bae1dSRodney W. Grimes psignal(t, SIGCHLD); 449df8bae1dSRodney W. Grimes wakeup((caddr_t)t); 450df8bae1dSRodney W. Grimes return (0); 451df8bae1dSRodney W. Grimes } 452df8bae1dSRodney W. Grimes p->p_xstat = 0; 453df8bae1dSRodney W. Grimes ruadd(&q->p_stats->p_cru, p->p_ru); 454df8bae1dSRodney W. Grimes FREE(p->p_ru, M_ZOMBIE); 455be6a1d14SDavid Greenman p->p_ru = NULL; 456df8bae1dSRodney W. Grimes 457df8bae1dSRodney W. Grimes /* 458df8bae1dSRodney W. Grimes * Decrement the count of procs running with this uid. 459df8bae1dSRodney W. Grimes */ 460df8bae1dSRodney W. Grimes (void)chgproccnt(p->p_cred->p_ruid, -1); 461df8bae1dSRodney W. Grimes 462df8bae1dSRodney W. Grimes /* 463bd7e5f99SJohn Dyson * Release reference to text vnode 464bd7e5f99SJohn Dyson */ 465bd7e5f99SJohn Dyson if (p->p_textvp) 466bd7e5f99SJohn Dyson vrele(p->p_textvp); 467bd7e5f99SJohn Dyson 468bd7e5f99SJohn Dyson /* 469df8bae1dSRodney W. Grimes * Free up credentials. 470df8bae1dSRodney W. Grimes */ 471df8bae1dSRodney W. Grimes if (--p->p_cred->p_refcnt == 0) { 472df8bae1dSRodney W. Grimes crfree(p->p_cred->pc_ucred); 473df8bae1dSRodney W. Grimes FREE(p->p_cred, M_SUBPROC); 474be6a1d14SDavid Greenman p->p_cred = NULL; 475df8bae1dSRodney W. Grimes } 476df8bae1dSRodney W. Grimes 477df8bae1dSRodney W. Grimes /* 478df8bae1dSRodney W. Grimes * Finally finished with old proc entry. 479df8bae1dSRodney W. Grimes * Unlink it from its process group and free it. 480df8bae1dSRodney W. Grimes */ 481df8bae1dSRodney W. Grimes leavepgrp(p); 482b75356e1SJeffrey Hsu LIST_REMOVE(p, p_list); /* off zombproc */ 483b75356e1SJeffrey Hsu LIST_REMOVE(p, p_sibling); 484df8bae1dSRodney W. Grimes 485df8bae1dSRodney W. Grimes /* 486df8bae1dSRodney W. Grimes * Give machine-dependent layer a chance 487df8bae1dSRodney W. Grimes * to free anything that cpu_exit couldn't 488df8bae1dSRodney W. Grimes * release while still running in process context. 489df8bae1dSRodney W. Grimes */ 490df8bae1dSRodney W. Grimes cpu_wait(p); 491df8bae1dSRodney W. Grimes FREE(p, M_PROC); 492df8bae1dSRodney W. Grimes nprocs--; 493df8bae1dSRodney W. Grimes return (0); 494df8bae1dSRodney W. Grimes } 495df8bae1dSRodney W. Grimes if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 && 496df8bae1dSRodney W. Grimes (p->p_flag & P_TRACED || uap->options & WUNTRACED)) { 497df8bae1dSRodney W. Grimes p->p_flag |= P_WAITED; 49801166e92SBruce Evans q->p_retval[0] = p->p_pid; 499b2f9e8b1SBruce Evans #ifdef COMPAT_43 50093c9414eSSteven Wallace if (compat) { 50101166e92SBruce Evans q->p_retval[1] = W_STOPCODE(p->p_xstat); 502df8bae1dSRodney W. Grimes error = 0; 503df8bae1dSRodney W. Grimes } else 504df8bae1dSRodney W. Grimes #endif 505df8bae1dSRodney W. Grimes if (uap->status) { 506df8bae1dSRodney W. Grimes status = W_STOPCODE(p->p_xstat); 507df8bae1dSRodney W. Grimes error = copyout((caddr_t)&status, 508df8bae1dSRodney W. Grimes (caddr_t)uap->status, sizeof(status)); 509df8bae1dSRodney W. Grimes } else 510df8bae1dSRodney W. Grimes error = 0; 511df8bae1dSRodney W. Grimes return (error); 512df8bae1dSRodney W. Grimes } 513df8bae1dSRodney W. Grimes } 514df8bae1dSRodney W. Grimes if (nfound == 0) 515df8bae1dSRodney W. Grimes return (ECHILD); 516df8bae1dSRodney W. Grimes if (uap->options & WNOHANG) { 51701166e92SBruce Evans q->p_retval[0] = 0; 518df8bae1dSRodney W. Grimes return (0); 519df8bae1dSRodney W. Grimes } 520bb56ec4aSPoul-Henning Kamp if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0))) 521df8bae1dSRodney W. Grimes return (error); 522df8bae1dSRodney W. Grimes goto loop; 523df8bae1dSRodney W. Grimes } 524df8bae1dSRodney W. Grimes 525df8bae1dSRodney W. Grimes /* 526df8bae1dSRodney W. Grimes * make process 'parent' the new parent of process 'child'. 527df8bae1dSRodney W. Grimes */ 528df8bae1dSRodney W. Grimes void 529df8bae1dSRodney W. Grimes proc_reparent(child, parent) 530df8bae1dSRodney W. Grimes register struct proc *child; 531df8bae1dSRodney W. Grimes register struct proc *parent; 532df8bae1dSRodney W. Grimes { 533df8bae1dSRodney W. Grimes 534df8bae1dSRodney W. Grimes if (child->p_pptr == parent) 535df8bae1dSRodney W. Grimes return; 536df8bae1dSRodney W. Grimes 537b75356e1SJeffrey Hsu LIST_REMOVE(child, p_sibling); 538b75356e1SJeffrey Hsu LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 539df8bae1dSRodney W. Grimes child->p_pptr = parent; 540df8bae1dSRodney W. Grimes } 541fed06968SJulian Elischer 542e0d898b4SJulian Elischer /* 543e0d898b4SJulian Elischer * The next two functions are to handle adding/deleting items on the 544fed06968SJulian Elischer * exit callout list 545e0d898b4SJulian Elischer * 546e0d898b4SJulian Elischer * at_exit(): 547e0d898b4SJulian Elischer * Take the arguments given and put them onto the exit callout list, 548fed06968SJulian Elischer * However first make sure that it's not already there. 549fed06968SJulian Elischer * returns 0 on success. 550fed06968SJulian Elischer */ 551fed06968SJulian Elischer int 552eb776aeaSBruce Evans at_exit(function) 553eb776aeaSBruce Evans exitlist_fn function; 554fed06968SJulian Elischer { 555fed06968SJulian Elischer ele_p ep; 556e0d898b4SJulian Elischer 557e0d898b4SJulian Elischer /* Be noisy if the programmer has lost track of things */ 558e0d898b4SJulian Elischer if (rm_at_exit(function)) 559fed06968SJulian Elischer printf("exit callout entry already present\n"); 560fed06968SJulian Elischer ep = malloc(sizeof(*ep), M_TEMP, M_NOWAIT); 561e0d898b4SJulian Elischer if (ep == NULL) 562e0d898b4SJulian Elischer return (ENOMEM); 563fed06968SJulian Elischer ep->next = exit_list; 564fed06968SJulian Elischer ep->function = function; 565fed06968SJulian Elischer exit_list = ep; 566e0d898b4SJulian Elischer return (0); 567fed06968SJulian Elischer } 568fed06968SJulian Elischer /* 569fed06968SJulian Elischer * Scan the exit callout list for the given items and remove them. 570fed06968SJulian Elischer * Returns the number of items removed. 571e0d898b4SJulian Elischer * Logically this can only be 0 or 1. 572fed06968SJulian Elischer */ 573fed06968SJulian Elischer int 574eb776aeaSBruce Evans rm_at_exit(function) 575eb776aeaSBruce Evans exitlist_fn function; 576fed06968SJulian Elischer { 577fed06968SJulian Elischer ele_p *epp, ep; 578e0d898b4SJulian Elischer int count; 579fed06968SJulian Elischer 580e0d898b4SJulian Elischer count = 0; 581fed06968SJulian Elischer epp = &exit_list; 582fed06968SJulian Elischer ep = *epp; 583fed06968SJulian Elischer while (ep) { 584fed06968SJulian Elischer if (ep->function == function) { 585fed06968SJulian Elischer *epp = ep->next; 586fed06968SJulian Elischer free(ep, M_TEMP); 587fed06968SJulian Elischer count++; 588fed06968SJulian Elischer } else { 589fed06968SJulian Elischer epp = &ep->next; 590fed06968SJulian Elischer } 591fed06968SJulian Elischer ep = *epp; 592fed06968SJulian Elischer } 593e0d898b4SJulian Elischer return (count); 594fed06968SJulian Elischer } 595