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