1 /* $OpenBSD: kern_exit.c,v 1.8 1996/10/26 07:30:24 tholo Exp $ */ 2 /* $NetBSD: kern_exit.c,v 1.39 1996/04/22 01:38:25 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1989, 1991, 1993 6 * The Regents of the University of California. All rights reserved. 7 * (c) UNIX System Laboratories, Inc. 8 * All or some portions of this file are derived from material licensed 9 * to the University of California by American Telephone and Telegraph 10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 11 * the permission of UNIX System Laboratories, Inc. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by the University of 24 * California, Berkeley and its contributors. 25 * 4. Neither the name of the University nor the names of its contributors 26 * may be used to endorse or promote products derived from this software 27 * without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 * 41 * @(#)kern_exit.c 8.7 (Berkeley) 2/12/94 42 */ 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/map.h> 47 #include <sys/ioctl.h> 48 #include <sys/proc.h> 49 #include <sys/tty.h> 50 #include <sys/time.h> 51 #include <sys/resource.h> 52 #include <sys/kernel.h> 53 #include <sys/proc.h> 54 #include <sys/buf.h> 55 #include <sys/wait.h> 56 #include <sys/file.h> 57 #include <sys/vnode.h> 58 #include <sys/syslog.h> 59 #include <sys/malloc.h> 60 #include <sys/resourcevar.h> 61 #include <sys/ptrace.h> 62 #include <sys/acct.h> 63 #include <sys/filedesc.h> 64 #include <sys/signalvar.h> 65 #ifdef SYSVSHM 66 #include <sys/shm.h> 67 #endif 68 #ifdef SYSVSEM 69 #include <sys/sem.h> 70 #endif 71 72 #include <sys/mount.h> 73 #include <sys/syscallargs.h> 74 75 #include <machine/cpu.h> 76 77 #include <vm/vm.h> 78 #include <vm/vm_kern.h> 79 80 /* 81 * exit -- 82 * Death of process. 83 */ 84 int 85 sys_exit(p, v, retval) 86 struct proc *p; 87 void *v; 88 register_t *retval; 89 { 90 struct sys_exit_args /* { 91 syscallarg(int) rval; 92 } */ *uap = v; 93 94 exit1(p, W_EXITCODE(SCARG(uap, rval), 0)); 95 /* NOTREACHED */ 96 return (0); 97 } 98 99 /* 100 * Exit: deallocate address space and other resources, change proc state 101 * to zombie, and unlink proc from allproc and parent's lists. Save exit 102 * status and rusage for wait(). Check for child processes and orphan them. 103 */ 104 void 105 exit1(p, rv) 106 register struct proc *p; 107 int rv; 108 { 109 register struct proc *q, *nq; 110 register struct vmspace *vm; 111 112 if (p->p_pid == 1) 113 panic("init died (signal %d, exit %d)", 114 WTERMSIG(rv), WEXITSTATUS(rv)); 115 #ifdef PGINPROF 116 vmsizmon(); 117 #endif 118 if (p->p_flag & P_PROFIL) 119 stopprofclock(p); 120 MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage), 121 M_ZOMBIE, M_WAITOK); 122 /* 123 * If parent is waiting for us to exit or exec, P_PPWAIT is set; we 124 * wake up the parent early to avoid deadlock. 125 */ 126 p->p_flag |= P_WEXIT; 127 p->p_flag &= ~P_TRACED; 128 if (p->p_flag & P_PPWAIT) { 129 p->p_flag &= ~P_PPWAIT; 130 wakeup((caddr_t)p->p_pptr); 131 } 132 p->p_sigignore = ~0; 133 p->p_siglist = 0; 134 untimeout(realitexpire, (caddr_t)p); 135 136 /* 137 * Close open files and release open-file table. 138 * This may block! 139 */ 140 fdfree(p); 141 142 /* The next three chunks should probably be moved to vmspace_exit. */ 143 vm = p->p_vmspace; 144 #ifdef SYSVSHM 145 if (vm->vm_shm) 146 shmexit(p); 147 #endif 148 #ifdef SYSVSEM 149 semexit(p); 150 #endif 151 /* 152 * Release user portion of address space. 153 * This releases references to vnodes, 154 * which could cause I/O if the file has been unlinked. 155 * Need to do this early enough that we can still sleep. 156 * Can't free the entire vmspace as the kernel stack 157 * may be mapped within that space also. 158 */ 159 if (vm->vm_refcnt == 1) 160 (void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS, 161 VM_MAXUSER_ADDRESS); 162 163 if (SESS_LEADER(p)) { 164 register struct session *sp = p->p_session; 165 166 if (sp->s_ttyvp) { 167 /* 168 * Controlling process. 169 * Signal foreground pgrp, 170 * drain controlling terminal 171 * and revoke access to controlling terminal. 172 */ 173 if (sp->s_ttyp->t_session == sp) { 174 if (sp->s_ttyp->t_pgrp) 175 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1); 176 (void) ttywait(sp->s_ttyp); 177 /* 178 * The tty could have been revoked 179 * if we blocked. 180 */ 181 if (sp->s_ttyvp) 182 vgoneall(sp->s_ttyvp); 183 } 184 if (sp->s_ttyvp) 185 vrele(sp->s_ttyvp); 186 sp->s_ttyvp = NULL; 187 /* 188 * s_ttyp is not zero'd; we use this to indicate 189 * that the session once had a controlling terminal. 190 * (for logging and informational purposes) 191 */ 192 } 193 sp->s_leader = NULL; 194 } 195 fixjobc(p, p->p_pgrp, 0); 196 p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; 197 (void)acct_process(p); 198 #ifdef KTRACE 199 /* 200 * release trace file 201 */ 202 p->p_traceflag = 0; /* don't trace the vrele() */ 203 if (p->p_tracep) 204 vrele(p->p_tracep); 205 #endif 206 /* 207 * Remove proc from allproc queue and pidhash chain. 208 * Place onto zombproc. Unlink from parent's child list. 209 */ 210 LIST_REMOVE(p, p_list); 211 LIST_INSERT_HEAD(&zombproc, p, p_list); 212 p->p_stat = SZOMB; 213 214 LIST_REMOVE(p, p_hash); 215 216 q = p->p_children.lh_first; 217 if (q) /* only need this if any child is S_ZOMB */ 218 wakeup((caddr_t)initproc); 219 for (; q != 0; q = nq) { 220 nq = q->p_sibling.le_next; 221 proc_reparent(q, initproc); 222 /* 223 * Traced processes are killed 224 * since their existence means someone is screwing up. 225 */ 226 if (q->p_flag & P_TRACED) { 227 q->p_flag &= ~P_TRACED; 228 psignal(q, SIGKILL); 229 } 230 } 231 232 /* 233 * Save exit status and final rusage info, adding in child rusage 234 * info and self times. 235 */ 236 p->p_xstat = rv; 237 *p->p_ru = p->p_stats->p_ru; 238 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL); 239 ruadd(p->p_ru, &p->p_stats->p_cru); 240 241 /* 242 * clear %cpu usage during swap 243 */ 244 p->p_pctcpu = 0; 245 246 /* 247 * Notify parent that we're gone. 248 */ 249 psignal(p->p_pptr, SIGCHLD); 250 wakeup((caddr_t)p->p_pptr); 251 /* 252 * Notify procfs debugger 253 */ 254 if (p->p_flag & P_FSTRACE) 255 wakeup((caddr_t)p); 256 #if defined(tahoe) 257 /* move this to cpu_exit */ 258 p->p_addr->u_pcb.pcb_savacc.faddr = (float *)NULL; 259 #endif 260 /* 261 * Clear curproc after we've done all operations 262 * that could block, and before tearing down the rest 263 * of the process state that might be used from clock, etc. 264 * Also, can't clear curproc while we're still runnable, 265 * as we're not on a run queue (we are current, just not 266 * a proper proc any longer!). 267 * 268 * Other substructures are freed from wait(). 269 */ 270 curproc = NULL; 271 if (--p->p_limit->p_refcnt == 0) 272 FREE(p->p_limit, M_SUBPROC); 273 274 /* 275 * Finally, call machine-dependent code to release the remaining 276 * resources including address space, the kernel stack and pcb. 277 * The address space is released by "vmspace_free(p->p_vmspace)"; 278 * This is machine-dependent, as we may have to change stacks 279 * or ensure that the current one isn't reallocated before we 280 * finish. cpu_exit will end with a call to cpu_swtch(), finishing 281 * our execution (pun intended). 282 */ 283 cpu_exit(p); 284 } 285 286 int 287 sys_wait4(q, v, retval) 288 register struct proc *q; 289 void *v; 290 register_t *retval; 291 { 292 register struct sys_wait4_args /* { 293 syscallarg(int) pid; 294 syscallarg(int *) status; 295 syscallarg(int) options; 296 syscallarg(struct rusage *) rusage; 297 } */ *uap = v; 298 register int nfound; 299 register struct proc *p, *t; 300 int status, error; 301 302 #ifdef COMPAT_09 303 SCARG(uap, pid) = (short)SCARG(uap, pid); 304 #endif 305 306 if (SCARG(uap, pid) == 0) 307 SCARG(uap, pid) = -q->p_pgid; 308 if (SCARG(uap, options) &~ (WUNTRACED|WNOHANG)) 309 return (EINVAL); 310 311 loop: 312 nfound = 0; 313 for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) { 314 if (SCARG(uap, pid) != WAIT_ANY && 315 p->p_pid != SCARG(uap, pid) && 316 p->p_pgid != -SCARG(uap, pid)) 317 continue; 318 nfound++; 319 if (p->p_stat == SZOMB) { 320 retval[0] = p->p_pid; 321 322 if (SCARG(uap, status)) { 323 status = p->p_xstat; /* convert to int */ 324 error = copyout((caddr_t)&status, 325 (caddr_t)SCARG(uap, status), 326 sizeof(status)); 327 if (error) 328 return (error); 329 } 330 if (SCARG(uap, rusage) && 331 (error = copyout((caddr_t)p->p_ru, 332 (caddr_t)SCARG(uap, rusage), 333 sizeof (struct rusage)))) 334 return (error); 335 /* 336 * If we got the child via a ptrace 'attach', 337 * we need to give it back to the old parent. 338 */ 339 if (p->p_oppid && (t = pfind(p->p_oppid))) { 340 p->p_oppid = 0; 341 proc_reparent(p, t); 342 psignal(t, SIGCHLD); 343 wakeup((caddr_t)t); 344 return (0); 345 } 346 p->p_xstat = 0; 347 ruadd(&q->p_stats->p_cru, p->p_ru); 348 FREE(p->p_ru, M_ZOMBIE); 349 350 /* 351 * Finally finished with old proc entry. 352 * Unlink it from its process group and free it. 353 */ 354 leavepgrp(p); 355 LIST_REMOVE(p, p_list); /* off zombproc */ 356 LIST_REMOVE(p, p_sibling); 357 358 /* 359 * Decrement the count of procs running with this uid. 360 */ 361 (void)chgproccnt(p->p_cred->p_ruid, -1); 362 363 /* 364 * Free up credentials. 365 */ 366 if (--p->p_cred->p_refcnt == 0) { 367 crfree(p->p_cred->pc_ucred); 368 FREE(p->p_cred, M_SUBPROC); 369 } 370 371 /* 372 * Release reference to text vnode 373 */ 374 if (p->p_textvp) 375 vrele(p->p_textvp); 376 377 /* 378 * Give machine-dependent layer a chance 379 * to free anything that cpu_exit couldn't 380 * release while still running in process context. 381 */ 382 cpu_wait(p); 383 FREE(p, M_PROC); 384 nprocs--; 385 return (0); 386 } 387 if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 && 388 (p->p_flag & P_TRACED || SCARG(uap, options) & WUNTRACED)) { 389 p->p_flag |= P_WAITED; 390 retval[0] = p->p_pid; 391 392 if (SCARG(uap, status)) { 393 status = W_STOPCODE(p->p_xstat); 394 error = copyout((caddr_t)&status, 395 (caddr_t)SCARG(uap, status), 396 sizeof(status)); 397 } else 398 error = 0; 399 return (error); 400 } 401 } 402 if (nfound == 0) 403 return (ECHILD); 404 if (SCARG(uap, options) & WNOHANG) { 405 retval[0] = 0; 406 return (0); 407 } 408 if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)) != 0) 409 return (error); 410 goto loop; 411 } 412 413 /* 414 * make process 'parent' the new parent of process 'child'. 415 */ 416 void 417 proc_reparent(child, parent) 418 register struct proc *child; 419 register struct proc *parent; 420 { 421 422 if (child->p_pptr == parent) 423 return; 424 425 LIST_REMOVE(child, p_sibling); 426 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 427 child->p_pptr = parent; 428 } 429