1 /* $OpenBSD: kern_exit.c,v 1.57 2005/12/03 18:09:08 tedu 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. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * @(#)kern_exit.c 8.7 (Berkeley) 2/12/94 38 */ 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/ioctl.h> 43 #include <sys/proc.h> 44 #include <sys/tty.h> 45 #include <sys/time.h> 46 #include <sys/resource.h> 47 #include <sys/kernel.h> 48 #include <sys/buf.h> 49 #include <sys/wait.h> 50 #include <sys/file.h> 51 #include <sys/vnode.h> 52 #include <sys/syslog.h> 53 #include <sys/malloc.h> 54 #include <sys/resourcevar.h> 55 #include <sys/ptrace.h> 56 #include <sys/acct.h> 57 #include <sys/filedesc.h> 58 #include <sys/signalvar.h> 59 #include <sys/sched.h> 60 #include <sys/ktrace.h> 61 #include <sys/pool.h> 62 #include <sys/mutex.h> 63 #ifdef SYSVSHM 64 #include <sys/shm.h> 65 #endif 66 #ifdef SYSVSEM 67 #include <sys/sem.h> 68 #endif 69 70 #include "systrace.h" 71 #include <dev/systrace.h> 72 73 #include <sys/mount.h> 74 #include <sys/syscallargs.h> 75 76 #include <machine/cpu.h> 77 78 #include <uvm/uvm_extern.h> 79 80 /* 81 * exit -- 82 * Death of process. 83 */ 84 int 85 sys_exit(struct proc *p, void *v, register_t *retval) 86 { 87 struct sys_exit_args /* { 88 syscallarg(int) rval; 89 } */ *uap = v; 90 91 exit1(p, W_EXITCODE(SCARG(uap, rval), 0), EXIT_NORMAL); 92 /* NOTREACHED */ 93 return (0); 94 } 95 96 #ifdef RTHREADS 97 int 98 sys_threxit(struct proc *p, void *v, register_t *retval) 99 { 100 struct sys_threxit_args *uap = v; 101 102 exit1(p, W_EXITCODE(SCARG(uap, rval), 0), EXIT_THREAD); 103 104 return (0); 105 } 106 #endif 107 108 /* 109 * Exit: deallocate address space and other resources, change proc state 110 * to zombie, and unlink proc from allproc and parent's lists. Save exit 111 * status and rusage for wait(). Check for child processes and orphan them. 112 */ 113 void 114 exit1(struct proc *p, int rv, int flags) 115 { 116 struct proc *q, *nq; 117 118 if (p->p_pid == 1) 119 panic("init died (signal %d, exit %d)", 120 WTERMSIG(rv), WEXITSTATUS(rv)); 121 122 /* 123 * if one thread calls exit, we take down everybody. 124 * we have to be careful not to get recursively caught. 125 * this is kinda sick. 126 */ 127 if (flags == EXIT_NORMAL && p != p->p_thrparent && 128 (p->p_thrparent->p_flag & P_WEXIT) == 0) { 129 printf("thread exiting normally %d\n", p->p_pid); 130 /* 131 * we are one of the threads. we SIGKILL the parent, 132 * then wait for it to kill us back. as soon as we return, 133 * we'll exit again. 134 */ 135 p->p_thrparent->p_flag |= P_IGNEXITRV; 136 p->p_thrparent->p_xstat = rv; 137 psignal(p->p_thrparent, SIGKILL); 138 tsleep(&p->p_thrparent->p_thrchildren, PUSER | PCATCH, "dying", 139 0); 140 printf("thread got sig %d\n", p->p_pid); 141 return; 142 } else if (p == p->p_thrparent) { 143 p->p_flag |= P_WEXIT; 144 if (flags == EXIT_NORMAL) { 145 q = LIST_FIRST(&p->p_thrchildren); 146 for (; q != 0; q = nq) { 147 nq = LIST_NEXT(q, p_thrsib); 148 q->p_flag |= P_IGNEXITRV; 149 q->p_xstat = rv; 150 printf("parent killing child %d\n", q->p_pid); 151 psignal(q, SIGKILL); 152 } 153 } 154 while (!LIST_EMPTY(&p->p_thrchildren)) 155 tsleep(&p->p_thrchildren, PUSER, "thrdeath", 0); 156 } 157 158 159 if (p->p_flag & P_PROFIL) 160 stopprofclock(p); 161 p->p_ru = pool_get(&rusage_pool, PR_WAITOK); 162 /* 163 * If parent is waiting for us to exit or exec, P_PPWAIT is set; we 164 * wake up the parent early to avoid deadlock. 165 */ 166 p->p_flag |= P_WEXIT; 167 p->p_flag &= ~P_TRACED; 168 if (p->p_flag & P_PPWAIT) { 169 p->p_flag &= ~P_PPWAIT; 170 wakeup(p->p_pptr); 171 } 172 p->p_sigignore = ~0; 173 p->p_siglist = 0; 174 timeout_del(&p->p_realit_to); 175 timeout_del(&p->p_stats->p_virt_to); 176 timeout_del(&p->p_stats->p_prof_to); 177 178 /* 179 * Close open files and release open-file table. 180 * This may block! 181 */ 182 fdfree(p); 183 184 #ifdef SYSVSEM 185 semexit(p); 186 #endif 187 if (SESS_LEADER(p)) { 188 struct session *sp = p->p_session; 189 190 if (sp->s_ttyvp) { 191 /* 192 * Controlling process. 193 * Signal foreground pgrp, 194 * drain controlling terminal 195 * and revoke access to controlling terminal. 196 */ 197 if (sp->s_ttyp->t_session == sp) { 198 if (sp->s_ttyp->t_pgrp) 199 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1); 200 (void) ttywait(sp->s_ttyp); 201 /* 202 * The tty could have been revoked 203 * if we blocked. 204 */ 205 if (sp->s_ttyvp) 206 VOP_REVOKE(sp->s_ttyvp, REVOKEALL); 207 } 208 if (sp->s_ttyvp) 209 vrele(sp->s_ttyvp); 210 sp->s_ttyvp = NULL; 211 /* 212 * s_ttyp is not zero'd; we use this to indicate 213 * that the session once had a controlling terminal. 214 * (for logging and informational purposes) 215 */ 216 } 217 sp->s_leader = NULL; 218 } 219 fixjobc(p, p->p_pgrp, 0); 220 #ifdef ACCOUNTING 221 (void)acct_process(p); 222 #endif 223 #ifdef KTRACE 224 /* 225 * release trace file 226 */ 227 p->p_traceflag = 0; /* don't trace the vrele() */ 228 if (p->p_tracep) 229 ktrsettracevnode(p, NULL); 230 #endif 231 #if NSYSTRACE > 0 232 if (ISSET(p->p_flag, P_SYSTRACE)) 233 systrace_exit(p); 234 #endif 235 /* 236 * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP! 237 */ 238 p->p_stat = SDEAD; 239 240 /* 241 * Remove proc from pidhash chain so looking it up won't 242 * work. Move it from allproc to zombproc, but do not yet 243 * wake up the reaper. We will put the proc on the 244 * deadproc list later (using the p_hash member), and 245 * wake up the reaper when we do. 246 */ 247 LIST_REMOVE(p, p_hash); 248 LIST_REMOVE(p, p_list); 249 LIST_INSERT_HEAD(&zombproc, p, p_list); 250 251 /* 252 * Give orphaned children to init(8). 253 */ 254 q = LIST_FIRST(&p->p_children); 255 if (q) /* only need this if any child is S_ZOMB */ 256 wakeup(initproc); 257 for (; q != 0; q = nq) { 258 nq = LIST_NEXT(q, p_sibling); 259 proc_reparent(q, initproc); 260 /* 261 * Traced processes are killed 262 * since their existence means someone is screwing up. 263 */ 264 if (q->p_flag & P_TRACED) { 265 q->p_flag &= ~P_TRACED; 266 psignal(q, SIGKILL); 267 } 268 } 269 270 /* unlink oursleves from the active threads */ 271 if (p != p->p_thrparent) { 272 LIST_REMOVE(p, p_thrsib); 273 if (LIST_EMPTY(&p->p_thrparent->p_thrchildren)) 274 wakeup(&p->p_thrparent->p_thrchildren); 275 } 276 277 278 /* 279 * Save exit status and final rusage info, adding in child rusage 280 * info and self times. 281 */ 282 if (!(p->p_flag & P_IGNEXITRV)) 283 p->p_xstat = rv; 284 *p->p_ru = p->p_stats->p_ru; 285 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL); 286 ruadd(p->p_ru, &p->p_stats->p_cru); 287 288 /* 289 * clear %cpu usage during swap 290 */ 291 p->p_pctcpu = 0; 292 293 /* 294 * notify interested parties of our demise. 295 */ 296 KNOTE(&p->p_klist, NOTE_EXIT); 297 298 /* 299 * Notify parent that we're gone. If we have P_NOZOMBIE or parent has 300 * the P_NOCLDWAIT flag set, notify process 1 instead (and hope it 301 * will handle this situation). 302 */ 303 if ((p->p_flag & P_NOZOMBIE) || (p->p_pptr->p_flag & P_NOCLDWAIT)) { 304 struct proc *pp = p->p_pptr; 305 proc_reparent(p, initproc); 306 /* 307 * If this was the last child of our parent, notify 308 * parent, so in case he was wait(2)ing, he will 309 * continue. 310 */ 311 if (LIST_EMPTY(&pp->p_children)) 312 wakeup(pp); 313 } 314 315 if ((p->p_flag & P_FSTRACE) == 0 && p->p_exitsig != 0) 316 psignal(p->p_pptr, P_EXITSIG(p)); 317 wakeup(p->p_pptr); 318 319 /* 320 * Notify procfs debugger 321 */ 322 if (p->p_flag & P_FSTRACE) 323 wakeup(p); 324 325 /* 326 * Release the process's signal state. 327 */ 328 sigactsfree(p); 329 330 /* 331 * Clear curproc after we've done all operations 332 * that could block, and before tearing down the rest 333 * of the process state that might be used from clock, etc. 334 * Also, can't clear curproc while we're still runnable, 335 * as we're not on a run queue (we are current, just not 336 * a proper proc any longer!). 337 * 338 * Other substructures are freed from wait(). 339 */ 340 curproc = NULL; 341 limfree(p->p_limit); 342 p->p_limit = NULL; 343 344 /* This process no longer needs to hold the kernel lock. */ 345 KERNEL_PROC_UNLOCK(p); 346 347 /* 348 * If emulation has process exit hook, call it now. 349 */ 350 if (p->p_emul->e_proc_exit) 351 (*p->p_emul->e_proc_exit)(p); 352 353 /* 354 * Finally, call machine-dependent code to switch to a new 355 * context (possibly the idle context). Once we are no longer 356 * using the dead process's vmspace and stack, exit2() will be 357 * called to schedule those resources to be released by the 358 * reaper thread. 359 * 360 * Note that cpu_exit() will end with a call equivalent to 361 * cpu_switch(), finishing our execution (pun intended). 362 */ 363 cpu_exit(p); 364 } 365 366 /* 367 * Locking of this proclist is special; it's accessed in a 368 * critical section of process exit, and thus locking it can't 369 * modify interrupt state. We use a simple spin lock for this 370 * proclist. Processes on this proclist are also on zombproc; 371 * we use the p_hash member to linkup to deadproc. 372 */ 373 struct mutex deadproc_mutex = MUTEX_INITIALIZER(IPL_NONE); 374 struct proclist deadproc = LIST_HEAD_INITIALIZER(deadproc); 375 376 /* 377 * We are called from cpu_exit() once it is safe to schedule the 378 * dead process's resources to be freed. 379 * 380 * NOTE: One must be careful with locking in this routine. It's 381 * called from a critical section in machine-dependent code, so 382 * we should refrain from changing any interrupt state. 383 * 384 * We lock the deadproc list, place the proc on that list (using 385 * the p_hash member), and wake up the reaper. 386 */ 387 void 388 exit2(struct proc *p) 389 { 390 int s; 391 392 mtx_enter(&deadproc_mutex); 393 LIST_INSERT_HEAD(&deadproc, p, p_hash); 394 mtx_leave(&deadproc_mutex); 395 396 wakeup(&deadproc); 397 398 SCHED_LOCK(s); 399 } 400 401 /* 402 * Process reaper. This is run by a kernel thread to free the resources 403 * of a dead process. Once the resources are free, the process becomes 404 * a zombie, and the parent is allowed to read the undead's status. 405 */ 406 void 407 reaper(void) 408 { 409 struct proc *p; 410 411 KERNEL_PROC_UNLOCK(curproc); 412 413 for (;;) { 414 mtx_enter(&deadproc_mutex); 415 p = LIST_FIRST(&deadproc); 416 if (p == NULL) { 417 /* No work for us; go to sleep until someone exits. */ 418 mtx_leave(&deadproc_mutex); 419 (void) tsleep(&deadproc, PVM, "reaper", 0); 420 continue; 421 } 422 423 /* Remove us from the deadproc list. */ 424 LIST_REMOVE(p, p_hash); 425 mtx_leave(&deadproc_mutex); 426 KERNEL_PROC_LOCK(curproc); 427 428 /* 429 * Give machine-dependent code a chance to free any 430 * resources it couldn't free while still running on 431 * that process's context. This must be done before 432 * uvm_exit(), in case these resources are in the PCB. 433 */ 434 cpu_wait(p); 435 436 /* 437 * Free the VM resources we're still holding on to. 438 * We must do this from a valid thread because doing 439 * so may block. 440 */ 441 uvm_exit(p); 442 443 /* Process is now a true zombie. */ 444 if ((p->p_flag & P_NOZOMBIE) == 0) { 445 p->p_stat = SZOMB; 446 447 /* Wake up the parent so it can get exit status. */ 448 psignal(p->p_pptr, SIGCHLD); 449 wakeup(p->p_pptr); 450 } else { 451 /* Noone will wait for us. Just zap the process now */ 452 proc_zap(p); 453 } 454 455 KERNEL_PROC_UNLOCK(curproc); 456 } 457 } 458 459 pid_t 460 sys_wait4(struct proc *q, void *v, register_t *retval) 461 { 462 struct sys_wait4_args /* { 463 syscallarg(pid_t) pid; 464 syscallarg(int *) status; 465 syscallarg(int) options; 466 syscallarg(struct rusage *) rusage; 467 } */ *uap = v; 468 int nfound; 469 struct proc *p, *t; 470 int status, error; 471 472 if (SCARG(uap, pid) == 0) 473 SCARG(uap, pid) = -q->p_pgid; 474 if (SCARG(uap, options) &~ (WUNTRACED|WNOHANG|WALTSIG|WCONTINUED)) 475 return (EINVAL); 476 477 loop: 478 nfound = 0; 479 LIST_FOREACH(p, &q->p_children, p_sibling) { 480 if ((p->p_flag & P_NOZOMBIE) || 481 (SCARG(uap, pid) != WAIT_ANY && 482 p->p_pid != SCARG(uap, pid) && 483 p->p_pgid != -SCARG(uap, pid))) 484 continue; 485 486 /* 487 * Wait for processes with p_exitsig != SIGCHLD processes only 488 * if WALTSIG is set; wait for processes with pexitsig == 489 * SIGCHLD only if WALTSIG is clear. 490 */ 491 if ((SCARG(uap, options) & WALTSIG) ? 492 (p->p_exitsig == SIGCHLD) : (P_EXITSIG(p) != SIGCHLD)) 493 continue; 494 495 nfound++; 496 if (p->p_stat == SZOMB) { 497 retval[0] = p->p_pid; 498 499 if (SCARG(uap, status)) { 500 status = p->p_xstat; /* convert to int */ 501 error = copyout(&status, 502 SCARG(uap, status), sizeof(status)); 503 if (error) 504 return (error); 505 } 506 if (SCARG(uap, rusage) && 507 (error = copyout(p->p_ru, 508 SCARG(uap, rusage), sizeof(struct rusage)))) 509 return (error); 510 511 /* 512 * If we got the child via a ptrace 'attach', 513 * we need to give it back to the old parent. 514 */ 515 if (p->p_oppid && (t = pfind(p->p_oppid))) { 516 p->p_oppid = 0; 517 proc_reparent(p, t); 518 if (p->p_exitsig != 0) 519 psignal(t, P_EXITSIG(p)); 520 wakeup(t); 521 return (0); 522 } 523 524 scheduler_wait_hook(q, p); 525 p->p_xstat = 0; 526 ruadd(&q->p_stats->p_cru, p->p_ru); 527 528 proc_zap(p); 529 530 return (0); 531 } 532 if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 && 533 (p->p_flag & P_TRACED || SCARG(uap, options) & WUNTRACED)) { 534 p->p_flag |= P_WAITED; 535 retval[0] = p->p_pid; 536 537 if (SCARG(uap, status)) { 538 status = W_STOPCODE(p->p_xstat); 539 error = copyout(&status, SCARG(uap, status), 540 sizeof(status)); 541 } else 542 error = 0; 543 return (error); 544 } 545 if ((SCARG(uap, options) & WCONTINUED) && (p->p_flag & P_CONTINUED)) { 546 p->p_flag &= ~P_CONTINUED; 547 retval[0] = p->p_pid; 548 549 if (SCARG(uap, status)) { 550 status = _WCONTINUED; 551 error = copyout(&status, SCARG(uap, status), 552 sizeof(status)); 553 } else 554 error = 0; 555 return (error); 556 } 557 } 558 if (nfound == 0) 559 return (ECHILD); 560 if (SCARG(uap, options) & WNOHANG) { 561 retval[0] = 0; 562 return (0); 563 } 564 if ((error = tsleep(q, PWAIT | PCATCH, "wait", 0)) != 0) 565 return (error); 566 goto loop; 567 } 568 569 /* 570 * make process 'parent' the new parent of process 'child'. 571 */ 572 void 573 proc_reparent(struct proc *child, struct proc *parent) 574 { 575 576 if (child->p_pptr == parent) 577 return; 578 579 if (parent == initproc) 580 child->p_exitsig = SIGCHLD; 581 582 LIST_REMOVE(child, p_sibling); 583 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 584 child->p_pptr = parent; 585 } 586 587 void 588 proc_zap(struct proc *p) 589 { 590 pool_put(&rusage_pool, p->p_ru); 591 if (p->p_ptstat) 592 free(p->p_ptstat, M_SUBPROC); 593 594 /* 595 * Finally finished with old proc entry. 596 * Unlink it from its process group and free it. 597 */ 598 leavepgrp(p); 599 LIST_REMOVE(p, p_list); /* off zombproc */ 600 LIST_REMOVE(p, p_sibling); 601 602 /* 603 * Decrement the count of procs running with this uid. 604 */ 605 (void)chgproccnt(p->p_cred->p_ruid, -1); 606 607 /* 608 * Free up credentials. 609 */ 610 if (--p->p_cred->p_refcnt == 0) { 611 crfree(p->p_cred->pc_ucred); 612 pool_put(&pcred_pool, p->p_cred); 613 } 614 615 /* 616 * Release reference to text vnode 617 */ 618 if (p->p_textvp) 619 vrele(p->p_textvp); 620 621 pool_put(&proc_pool, p); 622 nprocs--; 623 } 624 625