1 /* $OpenBSD: kern_exit.c,v 1.100 2011/04/18 21:44:56 guenther 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 SYSVSEM 64 #include <sys/sem.h> 65 #endif 66 67 #include "systrace.h" 68 #include <dev/systrace.h> 69 70 #include <sys/mount.h> 71 #include <sys/syscallargs.h> 72 73 #include <machine/cpu.h> 74 75 #include <uvm/uvm_extern.h> 76 77 /* 78 * exit -- 79 * Death of process. 80 */ 81 int 82 sys_exit(struct proc *p, void *v, register_t *retval) 83 { 84 struct sys_exit_args /* { 85 syscallarg(int) rval; 86 } */ *uap = v; 87 88 exit1(p, W_EXITCODE(SCARG(uap, rval), 0), EXIT_NORMAL); 89 /* NOTREACHED */ 90 return (0); 91 } 92 93 int 94 sys_threxit(struct proc *p, void *v, register_t *retval) 95 { 96 struct sys_threxit_args /* { 97 syscallarg(pid_t *) notdead; 98 } */ *uap = v; 99 100 if (!rthreads_enabled) 101 return (EINVAL); 102 103 if (SCARG(uap, notdead) != NULL) { 104 pid_t zero = 0; 105 if (copyout(&zero, SCARG(uap, notdead), sizeof(zero))) { 106 psignal(p, SIGSEGV); 107 } 108 } 109 exit1(p, 0, EXIT_THREAD); 110 111 return (0); 112 } 113 114 /* 115 * Exit: deallocate address space and other resources, change proc state 116 * to zombie, and unlink proc from allproc and parent's lists. Save exit 117 * status and rusage for wait(). Check for child processes and orphan them. 118 */ 119 void 120 exit1(struct proc *p, int rv, int flags) 121 { 122 struct proc *q, *nq; 123 struct process *pr, *qr, *nqr; 124 125 if (p->p_pid == 1) 126 panic("init died (signal %d, exit %d)", 127 WTERMSIG(rv), WEXITSTATUS(rv)); 128 129 atomic_setbits_int(&p->p_flag, P_WEXIT); 130 131 /* unlink ourselves from the active threads */ 132 pr = p->p_p; 133 TAILQ_REMOVE(&pr->ps_threads, p, p_thr_link); 134 if (TAILQ_EMPTY(&pr->ps_threads)) 135 wakeup(&pr->ps_threads); 136 /* 137 * if one thread calls exit, we take down everybody. 138 * we have to be careful not to get recursively caught. 139 * this is kinda sick. 140 */ 141 if (flags == EXIT_NORMAL && (p->p_flag & P_THREAD) && 142 (pr->ps_mainproc->p_flag & P_WEXIT) == 0) { 143 /* 144 * we are one of the threads. we SIGKILL the parent, 145 * it will wake us up again, then we proceed. 146 */ 147 atomic_setbits_int(&pr->ps_mainproc->p_flag, P_IGNEXITRV); 148 pr->ps_mainproc->p_xstat = rv; 149 ptsignal(pr->ps_mainproc, SIGKILL, SPROPAGATED); 150 tsleep(pr, PUSER, "thrdying", 0); 151 } else if ((p->p_flag & P_THREAD) == 0) { 152 if (flags == EXIT_NORMAL) { 153 q = TAILQ_FIRST(&pr->ps_threads); 154 for (; q != NULL; q = nq) { 155 nq = TAILQ_NEXT(q, p_thr_link); 156 atomic_setbits_int(&q->p_flag, P_IGNEXITRV); 157 q->p_xstat = rv; 158 ptsignal(q, SIGKILL, SPROPAGATED); 159 } 160 } 161 wakeup(pr); 162 while (!TAILQ_EMPTY(&pr->ps_threads)) 163 tsleep(&pr->ps_threads, PUSER, "thrdeath", 0); 164 /* 165 * If parent is waiting for us to exit or exec, PS_PPWAIT 166 * is set; we wake up the parent early to avoid deadlock. 167 */ 168 if (pr->ps_flags & PS_PPWAIT) { 169 atomic_clearbits_int(&pr->ps_flags, PS_PPWAIT); 170 atomic_clearbits_int(&pr->ps_pptr->ps_flags, 171 PS_ISPWAIT); 172 wakeup(pr->ps_pptr); 173 } 174 } 175 176 if (p->p_flag & P_PROFIL) 177 stopprofclock(p); 178 p->p_ru = pool_get(&rusage_pool, PR_WAITOK); 179 p->p_sigignore = ~0; 180 p->p_siglist = 0; 181 timeout_del(&p->p_realit_to); 182 timeout_del(&p->p_stats->p_virt_to); 183 timeout_del(&p->p_stats->p_prof_to); 184 185 /* 186 * Close open files and release open-file table. 187 */ 188 fdfree(p); 189 190 if ((p->p_flag & P_THREAD) == 0) { 191 #ifdef SYSVSEM 192 semexit(pr); 193 #endif 194 if (SESS_LEADER(pr)) { 195 struct session *sp = pr->ps_session; 196 197 if (sp->s_ttyvp) { 198 /* 199 * Controlling process. 200 * Signal foreground pgrp, 201 * drain controlling terminal 202 * and revoke access to controlling terminal. 203 */ 204 if (sp->s_ttyp->t_session == sp) { 205 if (sp->s_ttyp->t_pgrp) 206 pgsignal(sp->s_ttyp->t_pgrp, 207 SIGHUP, 1); 208 (void) ttywait(sp->s_ttyp); 209 /* 210 * The tty could have been revoked 211 * if we blocked. 212 */ 213 if (sp->s_ttyvp) 214 VOP_REVOKE(sp->s_ttyvp, 215 REVOKEALL); 216 } 217 if (sp->s_ttyvp) 218 vrele(sp->s_ttyvp); 219 sp->s_ttyvp = NULL; 220 /* 221 * s_ttyp is not zero'd; we use this to 222 * indicate that the session once had a 223 * controlling terminal. (for logging and 224 * informational purposes) 225 */ 226 } 227 sp->s_leader = NULL; 228 } 229 fixjobc(pr, pr->ps_pgrp, 0); 230 231 #ifdef ACCOUNTING 232 (void)acct_process(p); 233 #endif 234 } 235 236 #ifdef KTRACE 237 /* 238 * release trace file 239 */ 240 p->p_traceflag = 0; /* don't trace the vrele() */ 241 if (p->p_tracep) 242 ktrsettracevnode(p, NULL); 243 #endif 244 #if NSYSTRACE > 0 245 if (ISSET(p->p_flag, P_SYSTRACE)) 246 systrace_exit(p); 247 #endif 248 /* 249 * Remove proc from pidhash chain so looking it up won't 250 * work. Move it from allproc to zombproc, but do not yet 251 * wake up the reaper. We will put the proc on the 252 * deadproc list later (using the p_hash member), and 253 * wake up the reaper when we do. 254 */ 255 /* 256 * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP! 257 */ 258 p->p_stat = SDEAD; 259 260 LIST_REMOVE(p, p_hash); 261 LIST_REMOVE(p, p_list); 262 LIST_INSERT_HEAD(&zombproc, p, p_list); 263 264 /* 265 * Give orphaned children to init(8). 266 */ 267 if ((p->p_flag & P_THREAD) == 0) { 268 qr = LIST_FIRST(&pr->ps_children); 269 if (qr) /* only need this if any child is S_ZOMB */ 270 wakeup(initproc->p_p); 271 for (; qr != 0; qr = nqr) { 272 nqr = LIST_NEXT(qr, ps_sibling); 273 proc_reparent(qr, initproc->p_p); 274 /* 275 * Traced processes are killed 276 * since their existence means someone is screwing up. 277 */ 278 if (qr->ps_mainproc->p_flag & P_TRACED) { 279 atomic_clearbits_int(&qr->ps_mainproc->p_flag, 280 P_TRACED); 281 prsignal(qr, SIGKILL); 282 } 283 } 284 } 285 286 287 /* 288 * Save exit status and final rusage info, adding in child rusage 289 * info and self times. 290 */ 291 if (!(p->p_flag & P_IGNEXITRV)) 292 p->p_xstat = rv; 293 *p->p_ru = p->p_stats->p_ru; 294 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL); 295 ruadd(p->p_ru, &p->p_stats->p_cru); 296 297 /* 298 * clear %cpu usage during swap 299 */ 300 p->p_pctcpu = 0; 301 302 if ((p->p_flag & P_THREAD) == 0) { 303 /* notify interested parties of our demise and clean up */ 304 knote_processexit(pr); 305 306 /* 307 * Notify parent that we're gone. If we have P_NOZOMBIE 308 * or parent has the P_NOCLDWAIT flag set, notify process 1 309 * instead (and hope it will handle this situation). 310 */ 311 if ((p->p_flag & P_NOZOMBIE) || 312 (pr->ps_pptr->ps_mainproc->p_flag & P_NOCLDWAIT)) { 313 struct process *ppr = pr->ps_pptr; 314 proc_reparent(pr, initproc->p_p); 315 /* 316 * If this was the last child of our parent, notify 317 * parent, so in case he was wait(2)ing, he will 318 * continue. 319 */ 320 if (LIST_EMPTY(&ppr->ps_children)) 321 wakeup(ppr); 322 } 323 } 324 325 /* 326 * Release the process's signal state. 327 */ 328 sigactsfree(p); 329 330 /* 331 * Other substructures are freed from reaper and wait(). 332 */ 333 334 /* 335 * If emulation has process exit hook, call it now. 336 */ 337 if (p->p_emul->e_proc_exit) 338 (*p->p_emul->e_proc_exit)(p); 339 340 /* 341 * Finally, call machine-dependent code to switch to a new 342 * context (possibly the idle context). Once we are no longer 343 * using the dead process's vmspace and stack, exit2() will be 344 * called to schedule those resources to be released by the 345 * reaper thread. 346 * 347 * Note that cpu_exit() will end with a call equivalent to 348 * cpu_switch(), finishing our execution (pun intended). 349 */ 350 uvmexp.swtch++; 351 cpu_exit(p); 352 panic("cpu_exit returned"); 353 } 354 355 /* 356 * Locking of this proclist is special; it's accessed in a 357 * critical section of process exit, and thus locking it can't 358 * modify interrupt state. We use a simple spin lock for this 359 * proclist. Processes on this proclist are also on zombproc; 360 * we use the p_hash member to linkup to deadproc. 361 */ 362 struct mutex deadproc_mutex = MUTEX_INITIALIZER(IPL_NONE); 363 struct proclist deadproc = LIST_HEAD_INITIALIZER(deadproc); 364 365 /* 366 * We are called from cpu_exit() once it is safe to schedule the 367 * dead process's resources to be freed. 368 * 369 * NOTE: One must be careful with locking in this routine. It's 370 * called from a critical section in machine-dependent code, so 371 * we should refrain from changing any interrupt state. 372 * 373 * We lock the deadproc list, place the proc on that list (using 374 * the p_hash member), and wake up the reaper. 375 */ 376 void 377 exit2(struct proc *p) 378 { 379 mtx_enter(&deadproc_mutex); 380 LIST_INSERT_HEAD(&deadproc, p, p_hash); 381 mtx_leave(&deadproc_mutex); 382 383 wakeup(&deadproc); 384 } 385 386 /* 387 * Process reaper. This is run by a kernel thread to free the resources 388 * of a dead process. Once the resources are free, the process becomes 389 * a zombie, and the parent is allowed to read the undead's status. 390 */ 391 void 392 reaper(void) 393 { 394 struct proc *p; 395 396 KERNEL_PROC_UNLOCK(curproc); 397 398 SCHED_ASSERT_UNLOCKED(); 399 400 for (;;) { 401 mtx_enter(&deadproc_mutex); 402 while ((p = LIST_FIRST(&deadproc)) == NULL) 403 msleep(&deadproc, &deadproc_mutex, PVM, "reaper", 0); 404 405 /* Remove us from the deadproc list. */ 406 LIST_REMOVE(p, p_hash); 407 mtx_leave(&deadproc_mutex); 408 409 KERNEL_PROC_LOCK(curproc); 410 411 /* 412 * Free the VM resources we're still holding on to. 413 * We must do this from a valid thread because doing 414 * so may block. 415 */ 416 uvm_exit(p); 417 418 /* Process is now a true zombie. */ 419 if ((p->p_flag & P_NOZOMBIE) == 0) { 420 p->p_stat = SZOMB; 421 422 if (P_EXITSIG(p) != 0) 423 prsignal(p->p_p->ps_pptr, P_EXITSIG(p)); 424 /* Wake up the parent so it can get exit status. */ 425 wakeup(p->p_p->ps_pptr); 426 } else { 427 /* Noone will wait for us. Just zap the process now */ 428 proc_zap(p); 429 } 430 431 KERNEL_PROC_UNLOCK(curproc); 432 } 433 } 434 435 pid_t 436 sys_wait4(struct proc *q, void *v, register_t *retval) 437 { 438 struct sys_wait4_args /* { 439 syscallarg(pid_t) pid; 440 syscallarg(int *) status; 441 syscallarg(int) options; 442 syscallarg(struct rusage *) rusage; 443 } */ *uap = v; 444 int nfound; 445 struct process *pr; 446 struct proc *p; 447 int status, error; 448 449 if (SCARG(uap, pid) == 0) 450 SCARG(uap, pid) = -q->p_p->ps_pgid; 451 if (SCARG(uap, options) &~ (WUNTRACED|WNOHANG|WALTSIG|WCONTINUED)) 452 return (EINVAL); 453 454 loop: 455 nfound = 0; 456 LIST_FOREACH(pr, &q->p_p->ps_children, ps_sibling) { 457 p = pr->ps_mainproc; 458 if ((p->p_flag & P_NOZOMBIE) || 459 (SCARG(uap, pid) != WAIT_ANY && 460 p->p_pid != SCARG(uap, pid) && 461 pr->ps_pgid != -SCARG(uap, pid))) 462 continue; 463 464 /* 465 * Wait for processes with p_exitsig != SIGCHLD processes only 466 * if WALTSIG is set; wait for processes with pexitsig == 467 * SIGCHLD only if WALTSIG is clear. 468 */ 469 if ((SCARG(uap, options) & WALTSIG) ? 470 (p->p_exitsig == SIGCHLD) : (P_EXITSIG(p) != SIGCHLD)) 471 continue; 472 473 nfound++; 474 if (p->p_stat == SZOMB) { 475 retval[0] = p->p_pid; 476 477 if (SCARG(uap, status)) { 478 status = p->p_xstat; /* convert to int */ 479 error = copyout(&status, 480 SCARG(uap, status), sizeof(status)); 481 if (error) 482 return (error); 483 } 484 if (SCARG(uap, rusage) && 485 (error = copyout(p->p_ru, 486 SCARG(uap, rusage), sizeof(struct rusage)))) 487 return (error); 488 proc_finish_wait(q, p); 489 return (0); 490 } 491 if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 && 492 (p->p_flag & P_TRACED || SCARG(uap, options) & WUNTRACED)) { 493 atomic_setbits_int(&p->p_flag, P_WAITED); 494 retval[0] = p->p_pid; 495 496 if (SCARG(uap, status)) { 497 status = W_STOPCODE(p->p_xstat); 498 error = copyout(&status, SCARG(uap, status), 499 sizeof(status)); 500 } else 501 error = 0; 502 return (error); 503 } 504 if ((SCARG(uap, options) & WCONTINUED) && (p->p_flag & P_CONTINUED)) { 505 atomic_clearbits_int(&p->p_flag, P_CONTINUED); 506 retval[0] = p->p_pid; 507 508 if (SCARG(uap, status)) { 509 status = _WCONTINUED; 510 error = copyout(&status, SCARG(uap, status), 511 sizeof(status)); 512 } else 513 error = 0; 514 return (error); 515 } 516 } 517 if (nfound == 0) 518 return (ECHILD); 519 if (SCARG(uap, options) & WNOHANG) { 520 retval[0] = 0; 521 return (0); 522 } 523 if ((error = tsleep(q->p_p, PWAIT | PCATCH, "wait", 0)) != 0) 524 return (error); 525 goto loop; 526 } 527 528 void 529 proc_finish_wait(struct proc *waiter, struct proc *p) 530 { 531 struct process *tr; 532 533 /* 534 * If we got the child via a ptrace 'attach', 535 * we need to give it back to the old parent. 536 */ 537 if (p->p_oppid && (tr = prfind(p->p_oppid))) { 538 atomic_clearbits_int(&p->p_flag, P_TRACED); 539 p->p_oppid = 0; 540 proc_reparent(p->p_p, tr); 541 if (p->p_exitsig != 0) 542 prsignal(tr, p->p_exitsig); 543 wakeup(tr); 544 } else { 545 scheduler_wait_hook(waiter, p); 546 p->p_xstat = 0; 547 ruadd(&waiter->p_stats->p_cru, p->p_ru); 548 proc_zap(p); 549 } 550 } 551 552 /* 553 * make process 'parent' the new parent of process 'child'. 554 */ 555 void 556 proc_reparent(struct process *child, struct process *parent) 557 { 558 559 if (child->ps_pptr == parent) 560 return; 561 562 if (parent == initproc->p_p) 563 child->ps_mainproc->p_exitsig = SIGCHLD; 564 565 LIST_REMOVE(child, ps_sibling); 566 LIST_INSERT_HEAD(&parent->ps_children, child, ps_sibling); 567 child->ps_pptr = parent; 568 } 569 570 void 571 proc_zap(struct proc *p) 572 { 573 struct process *pr = p->p_p; 574 575 pool_put(&rusage_pool, p->p_ru); 576 if (p->p_ptstat) 577 free(p->p_ptstat, M_SUBPROC); 578 579 /* 580 * Finally finished with old proc entry. 581 * Unlink it from its process group and free it. 582 */ 583 if ((p->p_flag & P_THREAD) == 0) 584 leavepgrp(pr); 585 LIST_REMOVE(p, p_list); /* off zombproc */ 586 if ((p->p_flag & P_THREAD) == 0) 587 LIST_REMOVE(pr, ps_sibling); 588 589 /* 590 * Decrement the count of procs running with this uid. 591 */ 592 (void)chgproccnt(p->p_cred->p_ruid, -1); 593 594 /* 595 * Release reference to text vnode 596 */ 597 if (p->p_textvp) 598 vrele(p->p_textvp); 599 600 /* 601 * Remove us from our process list, possibly killing the process 602 * in the process (pun intended). 603 */ 604 if (--pr->ps_refcnt == 0) { 605 KASSERT(TAILQ_EMPTY(&pr->ps_threads)); 606 limfree(pr->ps_limit); 607 crfree(pr->ps_cred->pc_ucred); 608 pool_put(&pcred_pool, pr->ps_cred); 609 pool_put(&process_pool, pr); 610 } 611 612 pool_put(&proc_pool, p); 613 nprocs--; 614 } 615