1 /* $OpenBSD: kern_exit.c,v 1.149 2015/03/14 03:38:50 jsg 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/sysctl.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 <uvm/uvm_extern.h> 74 75 /* 76 * exit -- 77 * Death of process. 78 */ 79 int 80 sys_exit(struct proc *p, void *v, register_t *retval) 81 { 82 struct sys_exit_args /* { 83 syscallarg(int) rval; 84 } */ *uap = v; 85 86 exit1(p, W_EXITCODE(SCARG(uap, rval), 0), EXIT_NORMAL); 87 /* NOTREACHED */ 88 return (0); 89 } 90 91 int 92 sys___threxit(struct proc *p, void *v, register_t *retval) 93 { 94 struct sys___threxit_args /* { 95 syscallarg(pid_t *) notdead; 96 } */ *uap = v; 97 98 if (SCARG(uap, notdead) != NULL) { 99 pid_t zero = 0; 100 if (copyout(&zero, SCARG(uap, notdead), sizeof(zero))) 101 psignal(p, SIGSEGV); 102 } 103 exit1(p, 0, EXIT_THREAD); 104 105 return (0); 106 } 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 process *pr, *qr, *nqr; 117 struct rusage *rup; 118 struct vnode *ovp; 119 120 atomic_setbits_int(&p->p_flag, P_WEXIT); 121 122 pr = p->p_p; 123 124 /* single-threaded? */ 125 if (TAILQ_FIRST(&pr->ps_threads) == p && 126 TAILQ_NEXT(p, p_thr_link) == NULL) { 127 flags = EXIT_NORMAL; 128 } else { 129 /* nope, multi-threaded */ 130 if (flags == EXIT_NORMAL) 131 single_thread_set(p, SINGLE_EXIT, 0); 132 else if (flags == EXIT_THREAD) 133 single_thread_check(p, 0); 134 } 135 136 if (flags == EXIT_NORMAL) { 137 if (pr->ps_pid == 1) 138 panic("init died (signal %d, exit %d)", 139 WTERMSIG(rv), WEXITSTATUS(rv)); 140 141 atomic_setbits_int(&pr->ps_flags, PS_EXITING); 142 pr->ps_mainproc->p_xstat = rv; 143 144 /* 145 * If parent is waiting for us to exit or exec, PS_PPWAIT 146 * is set; we wake up the parent early to avoid deadlock. 147 */ 148 if (pr->ps_flags & PS_PPWAIT) { 149 atomic_clearbits_int(&pr->ps_flags, PS_PPWAIT); 150 atomic_clearbits_int(&pr->ps_pptr->ps_flags, 151 PS_ISPWAIT); 152 wakeup(pr->ps_pptr); 153 } 154 } 155 156 /* unlink ourselves from the active threads */ 157 TAILQ_REMOVE(&pr->ps_threads, p, p_thr_link); 158 if ((p->p_flag & P_THREAD) == 0) { 159 /* main thread gotta wait because it has the pid, et al */ 160 while (pr->ps_refcnt > 1) 161 tsleep(&pr->ps_threads, PUSER, "thrdeath", 0); 162 if (pr->ps_flags & PS_PROFIL) 163 stopprofclock(pr); 164 } 165 166 rup = pr->ps_ru; 167 if (rup == NULL) { 168 rup = pool_get(&rusage_pool, PR_WAITOK | PR_ZERO); 169 if (pr->ps_ru == NULL) { 170 pr->ps_ru = rup; 171 } else { 172 pool_put(&rusage_pool, rup); 173 rup = pr->ps_ru; 174 } 175 } 176 p->p_siglist = 0; 177 178 if ((p->p_flag & P_THREAD) == 0) { 179 /* close open files and release open-file table */ 180 fdfree(p); 181 182 timeout_del(&pr->ps_realit_to); 183 #ifdef SYSVSEM 184 semexit(pr); 185 #endif 186 if (SESS_LEADER(pr)) { 187 struct session *sp = pr->ps_session; 188 189 if (sp->s_ttyvp) { 190 /* 191 * Controlling process. 192 * Signal foreground pgrp, 193 * drain controlling terminal 194 * and revoke access to controlling terminal. 195 */ 196 if (sp->s_ttyp->t_session == sp) { 197 if (sp->s_ttyp->t_pgrp) 198 pgsignal(sp->s_ttyp->t_pgrp, 199 SIGHUP, 1); 200 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, 207 REVOKEALL); 208 } 209 ovp = sp->s_ttyvp; 210 sp->s_ttyvp = NULL; 211 if (ovp) 212 vrele(ovp); 213 /* 214 * s_ttyp is not zero'd; we use this to 215 * indicate that the session once had a 216 * controlling terminal. (for logging and 217 * informational purposes) 218 */ 219 } 220 sp->s_leader = NULL; 221 } 222 fixjobc(pr, pr->ps_pgrp, 0); 223 224 #ifdef ACCOUNTING 225 acct_process(p); 226 #endif 227 228 #ifdef KTRACE 229 /* release trace file */ 230 if (pr->ps_tracevp) 231 ktrcleartrace(pr); 232 #endif 233 234 /* 235 * If parent has the SAS_NOCLDWAIT flag set, we're not 236 * going to become a zombie. 237 */ 238 if (pr->ps_pptr->ps_sigacts->ps_flags & SAS_NOCLDWAIT) 239 atomic_setbits_int(&pr->ps_flags, PS_NOZOMBIE); 240 } 241 242 p->p_fd = NULL; /* zap the thread's copy */ 243 244 #if NSYSTRACE > 0 245 if (ISSET(p->p_flag, P_SYSTRACE)) 246 systrace_exit(p); 247 #endif 248 249 /* 250 * If emulation has thread exit hook, call it now. 251 */ 252 if (pr->ps_emul->e_proc_exit) 253 (*pr->ps_emul->e_proc_exit)(p); 254 255 /* 256 * Remove proc from pidhash chain and allproc so looking 257 * it up won't work. We will put the proc on the 258 * deadproc list later (using the p_hash member), and 259 * wake up the reaper when we do. If this is the last 260 * thread of a process that isn't PS_NOZOMBIE, we'll put 261 * the process on the zombprocess list below. 262 */ 263 /* 264 * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP! 265 */ 266 p->p_stat = SDEAD; 267 268 LIST_REMOVE(p, p_hash); 269 LIST_REMOVE(p, p_list); 270 271 if ((p->p_flag & P_THREAD) == 0) { 272 LIST_REMOVE(pr, ps_list); 273 274 if ((pr->ps_flags & PS_NOZOMBIE) == 0) 275 LIST_INSERT_HEAD(&zombprocess, pr, ps_list); 276 else { 277 /* 278 * Not going to be a zombie, so it's now off all 279 * the lists scanned by ispidtaken(), so block 280 * fast reuse of the pid now. 281 */ 282 freepid(p->p_pid); 283 } 284 285 /* 286 * Give orphaned children to init(8). 287 */ 288 qr = LIST_FIRST(&pr->ps_children); 289 if (qr) /* only need this if any child is S_ZOMB */ 290 wakeup(initprocess); 291 for (; qr != 0; qr = nqr) { 292 nqr = LIST_NEXT(qr, ps_sibling); 293 proc_reparent(qr, initprocess); 294 /* 295 * Traced processes are killed since their 296 * existence means someone is screwing up. 297 */ 298 if (qr->ps_flags & PS_TRACED && 299 !(qr->ps_flags & PS_EXITING)) { 300 atomic_clearbits_int(&qr->ps_flags, PS_TRACED); 301 /* 302 * If single threading is active, 303 * direct the signal to the active 304 * thread to avoid deadlock. 305 */ 306 if (qr->ps_single) 307 ptsignal(qr->ps_single, SIGKILL, 308 STHREAD); 309 else 310 prsignal(qr, SIGKILL); 311 } 312 } 313 } 314 315 /* add thread's accumulated rusage into the process's total */ 316 ruadd(rup, &p->p_ru); 317 tuagg(pr, p); 318 319 /* 320 * clear %cpu usage during swap 321 */ 322 p->p_pctcpu = 0; 323 324 if ((p->p_flag & P_THREAD) == 0) { 325 /* 326 * Final thread has died, so add on our children's rusage 327 * and calculate the total times 328 */ 329 calcru(&pr->ps_tu, &rup->ru_utime, &rup->ru_stime, NULL); 330 ruadd(rup, &pr->ps_cru); 331 332 /* notify interested parties of our demise and clean up */ 333 knote_processexit(p); 334 335 /* 336 * Notify parent that we're gone. If we're not going to 337 * become a zombie, reparent to process 1 (init) so that 338 * we can wake our original parent to possibly unblock 339 * wait4() to return ECHILD. 340 */ 341 if (pr->ps_flags & PS_NOZOMBIE) { 342 struct process *ppr = pr->ps_pptr; 343 proc_reparent(pr, initprocess); 344 wakeup(ppr); 345 } 346 347 /* 348 * Release the process's signal state. 349 */ 350 sigactsfree(pr); 351 } 352 353 /* just a thread? detach it from its process */ 354 if (p->p_flag & P_THREAD) { 355 /* scheduler_wait_hook(pr->ps_mainproc, p); XXX */ 356 if (--pr->ps_refcnt == 1) 357 wakeup(&pr->ps_threads); 358 KASSERT(pr->ps_refcnt > 0); 359 } 360 361 /* 362 * Other substructures are freed from reaper and wait(). 363 */ 364 365 /* 366 * Finally, call machine-dependent code to switch to a new 367 * context (possibly the idle context). Once we are no longer 368 * using the dead process's vmspace and stack, exit2() will be 369 * called to schedule those resources to be released by the 370 * reaper thread. 371 * 372 * Note that cpu_exit() will end with a call equivalent to 373 * cpu_switch(), finishing our execution (pun intended). 374 */ 375 uvmexp.swtch++; 376 cpu_exit(p); 377 panic("cpu_exit returned"); 378 } 379 380 /* 381 * Locking of this proclist is special; it's accessed in a 382 * critical section of process exit, and thus locking it can't 383 * modify interrupt state. We use a simple spin lock for this 384 * proclist. We use the p_hash member to linkup to deadproc. 385 */ 386 struct mutex deadproc_mutex = MUTEX_INITIALIZER(IPL_NONE); 387 struct proclist deadproc = LIST_HEAD_INITIALIZER(deadproc); 388 389 /* 390 * We are called from cpu_exit() once it is safe to schedule the 391 * dead process's resources to be freed. 392 * 393 * NOTE: One must be careful with locking in this routine. It's 394 * called from a critical section in machine-dependent code, so 395 * we should refrain from changing any interrupt state. 396 * 397 * We lock the deadproc list, place the proc on that list (using 398 * the p_hash member), and wake up the reaper. 399 */ 400 void 401 exit2(struct proc *p) 402 { 403 mtx_enter(&deadproc_mutex); 404 LIST_INSERT_HEAD(&deadproc, p, p_hash); 405 mtx_leave(&deadproc_mutex); 406 407 wakeup(&deadproc); 408 } 409 410 void 411 proc_free(struct proc *p) 412 { 413 crfree(p->p_ucred); 414 pool_put(&proc_pool, p); 415 nthreads--; 416 } 417 418 /* 419 * Process reaper. This is run by a kernel thread to free the resources 420 * of a dead process. Once the resources are free, the process becomes 421 * a zombie, and the parent is allowed to read the undead's status. 422 */ 423 void 424 reaper(void) 425 { 426 struct proc *p; 427 428 KERNEL_UNLOCK(); 429 430 SCHED_ASSERT_UNLOCKED(); 431 432 for (;;) { 433 mtx_enter(&deadproc_mutex); 434 while ((p = LIST_FIRST(&deadproc)) == NULL) 435 msleep(&deadproc, &deadproc_mutex, PVM, "reaper", 0); 436 437 /* Remove us from the deadproc list. */ 438 LIST_REMOVE(p, p_hash); 439 mtx_leave(&deadproc_mutex); 440 441 KERNEL_LOCK(); 442 443 /* 444 * Free the VM resources we're still holding on to. 445 * We must do this from a valid thread because doing 446 * so may block. 447 */ 448 uvm_uarea_free(p); 449 p->p_vmspace = NULL; /* zap the thread's copy */ 450 451 if (p->p_flag & P_THREAD) { 452 /* Just a thread */ 453 proc_free(p); 454 } else { 455 struct process *pr = p->p_p; 456 457 /* Release the rest of the process's vmspace */ 458 uvm_exit(pr); 459 460 if ((pr->ps_flags & PS_NOZOMBIE) == 0) { 461 /* Process is now a true zombie. */ 462 atomic_setbits_int(&pr->ps_flags, PS_ZOMBIE); 463 prsignal(pr->ps_pptr, SIGCHLD); 464 465 /* Wake up the parent so it can get exit status. */ 466 wakeup(pr->ps_pptr); 467 } else { 468 /* No one will wait for us. Just zap the process now */ 469 process_zap(pr); 470 } 471 } 472 473 KERNEL_UNLOCK(); 474 } 475 } 476 477 int 478 sys_wait4(struct proc *q, void *v, register_t *retval) 479 { 480 struct sys_wait4_args /* { 481 syscallarg(pid_t) pid; 482 syscallarg(int *) status; 483 syscallarg(int) options; 484 syscallarg(struct rusage *) rusage; 485 } */ *uap = v; 486 struct rusage ru; 487 int status, error; 488 489 error = dowait4(q, SCARG(uap, pid), 490 SCARG(uap, status) ? &status : NULL, 491 SCARG(uap, options), SCARG(uap, rusage) ? &ru : NULL, retval); 492 if (error == 0 && retval[0] > 0 && SCARG(uap, status)) { 493 error = copyout(&status, SCARG(uap, status), sizeof(status)); 494 } 495 if (error == 0 && retval[0] > 0 && SCARG(uap, rusage)) { 496 error = copyout(&ru, SCARG(uap, rusage), sizeof(ru)); 497 #ifdef KTRACE 498 if (error == 0 && KTRPOINT(q, KTR_STRUCT)) 499 ktrrusage(q, &ru); 500 #endif 501 } 502 return (error); 503 } 504 505 int 506 dowait4(struct proc *q, pid_t pid, int *statusp, int options, 507 struct rusage *rusage, register_t *retval) 508 { 509 int nfound; 510 struct process *pr; 511 struct proc *p; 512 int error; 513 514 if (pid == 0) 515 pid = -q->p_p->ps_pgid; 516 if (options &~ (WUNTRACED|WNOHANG|WCONTINUED)) 517 return (EINVAL); 518 519 loop: 520 nfound = 0; 521 LIST_FOREACH(pr, &q->p_p->ps_children, ps_sibling) { 522 p = pr->ps_mainproc; 523 if ((pr->ps_flags & PS_NOZOMBIE) || 524 (pid != WAIT_ANY && 525 p->p_pid != pid && 526 pr->ps_pgid != -pid)) 527 continue; 528 529 nfound++; 530 if (pr->ps_flags & PS_ZOMBIE) { 531 retval[0] = p->p_pid; 532 533 if (statusp != NULL) 534 *statusp = p->p_xstat; /* convert to int */ 535 if (rusage != NULL) 536 memcpy(rusage, pr->ps_ru, sizeof(*rusage)); 537 proc_finish_wait(q, p); 538 return (0); 539 } 540 if (pr->ps_flags & PS_TRACED && 541 (pr->ps_flags & PS_WAITED) == 0 && pr->ps_single && 542 pr->ps_single->p_stat == SSTOP && 543 (pr->ps_single->p_flag & P_SUSPSINGLE) == 0) { 544 single_thread_wait(pr); 545 546 atomic_setbits_int(&pr->ps_flags, PS_WAITED); 547 retval[0] = p->p_pid; 548 549 if (statusp != NULL) 550 *statusp = W_STOPCODE(pr->ps_single->p_xstat); 551 if (rusage != NULL) 552 memset(rusage, 0, sizeof(*rusage)); 553 return (0); 554 } 555 if (p->p_stat == SSTOP && 556 (pr->ps_flags & PS_WAITED) == 0 && 557 (p->p_flag & P_SUSPSINGLE) == 0 && 558 (pr->ps_flags & PS_TRACED || 559 options & WUNTRACED)) { 560 atomic_setbits_int(&pr->ps_flags, PS_WAITED); 561 retval[0] = p->p_pid; 562 563 if (statusp != NULL) 564 *statusp = W_STOPCODE(p->p_xstat); 565 if (rusage != NULL) 566 memset(rusage, 0, sizeof(*rusage)); 567 return (0); 568 } 569 if ((options & WCONTINUED) && (p->p_flag & P_CONTINUED)) { 570 atomic_clearbits_int(&p->p_flag, P_CONTINUED); 571 retval[0] = p->p_pid; 572 573 if (statusp != NULL) 574 *statusp = _WCONTINUED; 575 if (rusage != NULL) 576 memset(rusage, 0, sizeof(*rusage)); 577 return (0); 578 } 579 } 580 if (nfound == 0) 581 return (ECHILD); 582 if (options & WNOHANG) { 583 retval[0] = 0; 584 return (0); 585 } 586 if ((error = tsleep(q->p_p, PWAIT | PCATCH, "wait", 0)) != 0) 587 return (error); 588 goto loop; 589 } 590 591 void 592 proc_finish_wait(struct proc *waiter, struct proc *p) 593 { 594 struct process *pr, *tr; 595 struct rusage *rup; 596 597 /* 598 * If we got the child via a ptrace 'attach', 599 * we need to give it back to the old parent. 600 */ 601 pr = p->p_p; 602 if (pr->ps_oppid && (tr = prfind(pr->ps_oppid))) { 603 atomic_clearbits_int(&pr->ps_flags, PS_TRACED); 604 pr->ps_oppid = 0; 605 proc_reparent(pr, tr); 606 prsignal(tr, SIGCHLD); 607 wakeup(tr); 608 } else { 609 scheduler_wait_hook(waiter, p); 610 p->p_xstat = 0; 611 rup = &waiter->p_p->ps_cru; 612 ruadd(rup, pr->ps_ru); 613 LIST_REMOVE(pr, ps_list); /* off zombprocess */ 614 freepid(p->p_pid); 615 process_zap(pr); 616 } 617 } 618 619 /* 620 * make process 'parent' the new parent of process 'child'. 621 */ 622 void 623 proc_reparent(struct process *child, struct process *parent) 624 { 625 626 if (child->ps_pptr == parent) 627 return; 628 629 LIST_REMOVE(child, ps_sibling); 630 LIST_INSERT_HEAD(&parent->ps_children, child, ps_sibling); 631 child->ps_pptr = parent; 632 } 633 634 void 635 process_zap(struct process *pr) 636 { 637 struct vnode *otvp; 638 struct proc *p = pr->ps_mainproc; 639 640 /* 641 * Finally finished with old proc entry. 642 * Unlink it from its process group and free it. 643 */ 644 leavepgrp(pr); 645 LIST_REMOVE(pr, ps_sibling); 646 647 /* 648 * Decrement the count of procs running with this uid. 649 */ 650 (void)chgproccnt(pr->ps_ucred->cr_ruid, -1); 651 652 /* 653 * Release reference to text vnode 654 */ 655 otvp = pr->ps_textvp; 656 pr->ps_textvp = NULL; 657 if (otvp) 658 vrele(otvp); 659 660 KASSERT(pr->ps_refcnt == 1); 661 if (pr->ps_ptstat != NULL) 662 free(pr->ps_ptstat, M_SUBPROC, 0); 663 pool_put(&rusage_pool, pr->ps_ru); 664 KASSERT(TAILQ_EMPTY(&pr->ps_threads)); 665 limfree(pr->ps_limit); 666 crfree(pr->ps_ucred); 667 pool_put(&process_pool, pr); 668 nprocesses--; 669 670 proc_free(p); 671 } 672