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