1 /* $OpenBSD: kern_exit.c,v 1.35 2001/09/11 20:05:25 miod 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 #include <sys/sched.h> 66 #include <sys/ktrace.h> 67 #include <sys/pool.h> 68 #ifdef SYSVSHM 69 #include <sys/shm.h> 70 #endif 71 #ifdef SYSVSEM 72 #include <sys/sem.h> 73 #endif 74 75 #include <sys/mount.h> 76 #include <sys/syscallargs.h> 77 78 #include <machine/cpu.h> 79 80 #include <vm/vm.h> 81 82 #include <uvm/uvm_extern.h> 83 84 void proc_zap __P((struct proc *)); 85 86 /* 87 * exit -- 88 * Death of process. 89 */ 90 int 91 sys_exit(p, v, retval) 92 struct proc *p; 93 void *v; 94 register_t *retval; 95 { 96 struct sys_exit_args /* { 97 syscallarg(int) rval; 98 } */ *uap = v; 99 100 exit1(p, W_EXITCODE(SCARG(uap, rval), 0)); 101 /* NOTREACHED */ 102 return (0); 103 } 104 105 /* 106 * Exit: deallocate address space and other resources, change proc state 107 * to zombie, and unlink proc from allproc and parent's lists. Save exit 108 * status and rusage for wait(). Check for child processes and orphan them. 109 */ 110 void 111 exit1(p, rv) 112 struct proc *p; 113 int rv; 114 { 115 struct proc *q, *nq; 116 struct vmspace *vm; 117 118 if (p->p_pid == 1) 119 panic("init died (signal %d, exit %d)", 120 WTERMSIG(rv), WEXITSTATUS(rv)); 121 122 if (p->p_flag & P_PROFIL) 123 stopprofclock(p); 124 MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage), 125 M_ZOMBIE, M_WAITOK); 126 /* 127 * If parent is waiting for us to exit or exec, P_PPWAIT is set; we 128 * wake up the parent early to avoid deadlock. 129 */ 130 p->p_flag |= P_WEXIT; 131 p->p_flag &= ~P_TRACED; 132 if (p->p_flag & P_PPWAIT) { 133 p->p_flag &= ~P_PPWAIT; 134 wakeup((caddr_t)p->p_pptr); 135 } 136 p->p_sigignore = ~0; 137 p->p_siglist = 0; 138 timeout_del(&p->p_realit_to); 139 140 /* 141 * Close open files and release open-file table. 142 * This may block! 143 */ 144 fdfree(p); 145 146 /* The next three chunks should probably be moved to vmspace_exit. */ 147 vm = p->p_vmspace; 148 #ifdef SYSVSHM 149 if (vm->vm_shm && vm->vm_refcnt == 1) 150 shmexit(vm); 151 #endif 152 #ifdef SYSVSEM 153 semexit(p); 154 #endif 155 /* 156 * Release user portion of address space. 157 * This releases references to vnodes, 158 * which could cause I/O if the file has been unlinked. 159 * Need to do this early enough that we can still sleep. 160 * Can't free the entire vmspace as the kernel stack 161 * may be mapped within that space also. 162 */ 163 if (vm->vm_refcnt == 1) 164 (void) uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS, 165 VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS); 166 167 if (SESS_LEADER(p)) { 168 register struct session *sp = p->p_session; 169 170 if (sp->s_ttyvp) { 171 /* 172 * Controlling process. 173 * Signal foreground pgrp, 174 * drain controlling terminal 175 * and revoke access to controlling terminal. 176 */ 177 if (sp->s_ttyp->t_session == sp) { 178 if (sp->s_ttyp->t_pgrp) 179 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1); 180 (void) ttywait(sp->s_ttyp); 181 /* 182 * The tty could have been revoked 183 * if we blocked. 184 */ 185 if (sp->s_ttyvp) 186 VOP_REVOKE(sp->s_ttyvp, REVOKEALL); 187 } 188 if (sp->s_ttyvp) 189 vrele(sp->s_ttyvp); 190 sp->s_ttyvp = NULL; 191 /* 192 * s_ttyp is not zero'd; we use this to indicate 193 * that the session once had a controlling terminal. 194 * (for logging and informational purposes) 195 */ 196 } 197 sp->s_leader = NULL; 198 } 199 fixjobc(p, p->p_pgrp, 0); 200 (void)acct_process(p); 201 #ifdef KTRACE 202 /* 203 * release trace file 204 */ 205 p->p_traceflag = 0; /* don't trace the vrele() */ 206 if (p->p_tracep) 207 ktrsettracevnode(p, NULL); 208 #endif 209 /* 210 * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP! 211 */ 212 p->p_stat = SDEAD; 213 214 /* 215 * Remove proc from pidhash chain so looking it up won't 216 * work. Move it from allproc to zombproc, but do not yet 217 * wake up the reaper. We will put the proc on the 218 * deadproc list later (using the p_hash member), and 219 * wake up the reaper when we do. 220 */ 221 LIST_REMOVE(p, p_hash); 222 LIST_REMOVE(p, p_list); 223 LIST_INSERT_HEAD(&zombproc, p, p_list); 224 225 /* 226 * Give orphaned children to init(8). 227 */ 228 q = p->p_children.lh_first; 229 if (q) /* only need this if any child is S_ZOMB */ 230 wakeup((caddr_t)initproc); 231 for (; q != 0; q = nq) { 232 nq = q->p_sibling.le_next; 233 proc_reparent(q, initproc); 234 /* 235 * Traced processes are killed 236 * since their existence means someone is screwing up. 237 */ 238 if (q->p_flag & P_TRACED) { 239 q->p_flag &= ~P_TRACED; 240 psignal(q, SIGKILL); 241 } 242 } 243 244 /* 245 * Save exit status and final rusage info, adding in child rusage 246 * info and self times. 247 */ 248 p->p_xstat = rv; 249 *p->p_ru = p->p_stats->p_ru; 250 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL); 251 ruadd(p->p_ru, &p->p_stats->p_cru); 252 253 /* 254 * clear %cpu usage during swap 255 */ 256 p->p_pctcpu = 0; 257 258 /* 259 * notify interested parties of our demise. 260 */ 261 KNOTE(&p->p_klist, NOTE_EXIT); 262 263 /* 264 * Notify parent that we're gone. If we have P_NOZOMBIE or parent has 265 * the P_NOCLDWAIT flag set, notify process 1 instead (and hope it 266 * will handle this situation). 267 */ 268 if ((p->p_flag & P_NOZOMBIE) || (p->p_pptr->p_flag & P_NOCLDWAIT)) { 269 struct proc *pp = p->p_pptr; 270 proc_reparent(p, initproc); 271 /* 272 * If this was the last child of our parent, notify 273 * parent, so in case he was wait(2)ing, he will 274 * continue. 275 */ 276 if (pp->p_children.lh_first == NULL) 277 wakeup((caddr_t)pp); 278 } 279 280 if ((p->p_flag & P_FSTRACE) == 0 && p->p_exitsig != 0) 281 psignal(p->p_pptr, P_EXITSIG(p)); 282 wakeup((caddr_t)p->p_pptr); 283 284 /* 285 * Notify procfs debugger 286 */ 287 if (p->p_flag & P_FSTRACE) 288 wakeup((caddr_t)p); 289 290 /* 291 * Release the process's signal state. 292 */ 293 sigactsfree(p); 294 295 /* 296 * Clear curproc after we've done all operations 297 * that could block, and before tearing down the rest 298 * of the process state that might be used from clock, etc. 299 * Also, can't clear curproc while we're still runnable, 300 * as we're not on a run queue (we are current, just not 301 * a proper proc any longer!). 302 * 303 * Other substructures are freed from wait(). 304 */ 305 curproc = NULL; 306 limfree(p->p_limit); 307 p->p_limit = NULL; 308 309 /* 310 * Finally, call machine-dependent code to switch to a new 311 * context (possibly the idle context). Once we are no longer 312 * using the dead process's vmspace and stack, exit2() will be 313 * called to schedule those resources to be released by the 314 * reaper thread. 315 * 316 * Note that cpu_exit() will end with a call equivalent to 317 * cpu_switch(), finishing our execution (pun intended). 318 */ 319 cpu_exit(p); 320 } 321 322 /* 323 * We are called from cpu_exit() once it is safe to schedule the 324 * dead process's resources to be freed. 325 * 326 * NOTE: One must be careful with locking in this routine. It's 327 * called from a critical section in machine-dependent code, so 328 * we should refrain from changing any interrupt state. 329 * 330 * We lock the deadproc list (a spin lock), place the proc on that 331 * list (using the p_hash member), and wake up the reaper. 332 */ 333 void 334 exit2(p) 335 struct proc *p; 336 { 337 338 simple_lock(&deadproc_slock); 339 LIST_INSERT_HEAD(&deadproc, p, p_hash); 340 simple_unlock(&deadproc_slock); 341 342 wakeup(&deadproc); 343 } 344 345 /* 346 * Process reaper. This is run by a kernel thread to free the resources 347 * of a dead process. Once the resources are free, the process becomes 348 * a zombie, and the parent is allowed to read the undead's status. 349 */ 350 void 351 reaper() 352 { 353 struct proc *p; 354 355 for (;;) { 356 simple_lock(&deadproc_slock); 357 p = LIST_FIRST(&deadproc); 358 if (p == NULL) { 359 /* No work for us; go to sleep until someone exits. */ 360 simple_unlock(&deadproc_slock); 361 (void) tsleep(&deadproc, PVM, "reaper", 0); 362 continue; 363 } 364 365 /* Remove us from the deadproc list. */ 366 LIST_REMOVE(p, p_hash); 367 simple_unlock(&deadproc_slock); 368 369 /* 370 * Give machine-dependent code a chance to free any 371 * resources it couldn't free while still running on 372 * that process's context. This must be done before 373 * uvm_exit(), in case these resources are in the PCB. 374 */ 375 cpu_wait(p); 376 377 /* 378 * Free the VM resources we're still holding on to. 379 * We must do this from a valid thread because doing 380 * so may block. 381 */ 382 uvm_exit(p); 383 384 /* Process is now a true zombie. */ 385 if ((p->p_flag & P_NOZOMBIE) == 0) { 386 p->p_stat = SZOMB; 387 388 /* Wake up the parent so it can get exit status. */ 389 psignal(p->p_pptr, SIGCHLD); 390 wakeup((caddr_t)p->p_pptr); 391 } else { 392 /* Noone will wait for us. Just zap the process now */ 393 proc_zap(p); 394 } 395 } 396 } 397 398 int 399 sys_wait4(q, v, retval) 400 register struct proc *q; 401 void *v; 402 register_t *retval; 403 { 404 register struct sys_wait4_args /* { 405 syscallarg(int) pid; 406 syscallarg(int *) status; 407 syscallarg(int) options; 408 syscallarg(struct rusage *) rusage; 409 } */ *uap = v; 410 register int nfound; 411 register struct proc *p, *t; 412 int status, error; 413 414 if (SCARG(uap, pid) == 0) 415 SCARG(uap, pid) = -q->p_pgid; 416 if (SCARG(uap, options) &~ (WUNTRACED|WNOHANG|WALTSIG)) 417 return (EINVAL); 418 419 loop: 420 nfound = 0; 421 for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) { 422 if ((p->p_flag & P_NOZOMBIE) || 423 (SCARG(uap, pid) != WAIT_ANY && 424 p->p_pid != SCARG(uap, pid) && 425 p->p_pgid != -SCARG(uap, pid))) 426 continue; 427 428 /* 429 * Wait for processes with p_exitsig != SIGCHLD processes only 430 * if WALTSIG is set; wait for processes with pexitsig == 431 * SIGCHLD only if WALTSIG is clear. 432 */ 433 if ((SCARG(uap, options) & WALTSIG) ? 434 (p->p_exitsig == SIGCHLD) : (P_EXITSIG(p) != SIGCHLD)) 435 continue; 436 437 nfound++; 438 if (p->p_stat == SZOMB) { 439 retval[0] = p->p_pid; 440 441 if (SCARG(uap, status)) { 442 status = p->p_xstat; /* convert to int */ 443 error = copyout((caddr_t)&status, 444 (caddr_t)SCARG(uap, status), 445 sizeof(status)); 446 if (error) 447 return (error); 448 } 449 if (SCARG(uap, rusage) && 450 (error = copyout((caddr_t)p->p_ru, 451 (caddr_t)SCARG(uap, rusage), 452 sizeof(struct rusage)))) 453 return (error); 454 455 /* 456 * If we got the child via a ptrace 'attach', 457 * we need to give it back to the old parent. 458 */ 459 if (p->p_oppid && (t = pfind(p->p_oppid))) { 460 p->p_oppid = 0; 461 proc_reparent(p, t); 462 if (p->p_exitsig != 0) 463 psignal(t, P_EXITSIG(p)); 464 wakeup((caddr_t)t); 465 return (0); 466 } 467 468 scheduler_wait_hook(q, p); 469 p->p_xstat = 0; 470 ruadd(&q->p_stats->p_cru, p->p_ru); 471 472 proc_zap(p); 473 474 return (0); 475 } 476 if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 && 477 (p->p_flag & P_TRACED || SCARG(uap, options) & WUNTRACED)) { 478 p->p_flag |= P_WAITED; 479 retval[0] = p->p_pid; 480 481 if (SCARG(uap, status)) { 482 status = W_STOPCODE(p->p_xstat); 483 error = copyout((caddr_t)&status, 484 (caddr_t)SCARG(uap, status), 485 sizeof(status)); 486 } else 487 error = 0; 488 return (error); 489 } 490 } 491 if (nfound == 0) 492 return (ECHILD); 493 if (SCARG(uap, options) & WNOHANG) { 494 retval[0] = 0; 495 return (0); 496 } 497 if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)) != 0) 498 return (error); 499 goto loop; 500 } 501 502 /* 503 * make process 'parent' the new parent of process 'child'. 504 */ 505 void 506 proc_reparent(child, parent) 507 register struct proc *child; 508 register struct proc *parent; 509 { 510 511 if (child->p_pptr == parent) 512 return; 513 514 if (parent == initproc) 515 child->p_exitsig = SIGCHLD; 516 517 LIST_REMOVE(child, p_sibling); 518 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 519 child->p_pptr = parent; 520 } 521 522 void 523 proc_zap(p) 524 struct proc *p; 525 { 526 527 FREE(p->p_ru, M_ZOMBIE); 528 529 /* 530 * Finally finished with old proc entry. 531 * Unlink it from its process group and free it. 532 */ 533 leavepgrp(p); 534 LIST_REMOVE(p, p_list); /* off zombproc */ 535 LIST_REMOVE(p, p_sibling); 536 537 /* 538 * Decrement the count of procs running with this uid. 539 */ 540 (void)chgproccnt(p->p_cred->p_ruid, -1); 541 542 /* 543 * Free up credentials. 544 */ 545 if (--p->p_cred->p_refcnt == 0) { 546 crfree(p->p_cred->pc_ucred); 547 FREE(p->p_cred, M_SUBPROC); 548 } 549 550 /* 551 * Release reference to text vnode 552 */ 553 if (p->p_textvp) 554 vrele(p->p_textvp); 555 556 pool_put(&proc_pool, p); 557 nprocs--; 558 } 559 560