1 /* $NetBSD: kern_exit.c,v 1.106 2002/11/30 09:59:22 jdolecek Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 * NASA Ames Research Center. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 /* 41 * Copyright (c) 1982, 1986, 1989, 1991, 1993 42 * The Regents of the University of California. All rights reserved. 43 * (c) UNIX System Laboratories, Inc. 44 * All or some portions of this file are derived from material licensed 45 * to the University of California by American Telephone and Telegraph 46 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 47 * the permission of UNIX System Laboratories, Inc. 48 * 49 * Redistribution and use in source and binary forms, with or without 50 * modification, are permitted provided that the following conditions 51 * are met: 52 * 1. Redistributions of source code must retain the above copyright 53 * notice, this list of conditions and the following disclaimer. 54 * 2. Redistributions in binary form must reproduce the above copyright 55 * notice, this list of conditions and the following disclaimer in the 56 * documentation and/or other materials provided with the distribution. 57 * 3. All advertising materials mentioning features or use of this software 58 * must display the following acknowledgement: 59 * This product includes software developed by the University of 60 * California, Berkeley and its contributors. 61 * 4. Neither the name of the University nor the names of its contributors 62 * may be used to endorse or promote products derived from this software 63 * without specific prior written permission. 64 * 65 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 66 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 67 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 68 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 69 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 70 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 71 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 72 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 73 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 74 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 75 * SUCH DAMAGE. 76 * 77 * @(#)kern_exit.c 8.10 (Berkeley) 2/23/95 78 */ 79 80 #include <sys/cdefs.h> 81 __KERNEL_RCSID(0, "$NetBSD: kern_exit.c,v 1.106 2002/11/30 09:59:22 jdolecek Exp $"); 82 83 #include "opt_ktrace.h" 84 #include "opt_perfctrs.h" 85 #include "opt_systrace.h" 86 #include "opt_sysv.h" 87 88 #include <sys/param.h> 89 #include <sys/systm.h> 90 #include <sys/ioctl.h> 91 #include <sys/proc.h> 92 #include <sys/tty.h> 93 #include <sys/time.h> 94 #include <sys/resource.h> 95 #include <sys/kernel.h> 96 #include <sys/ktrace.h> 97 #include <sys/proc.h> 98 #include <sys/buf.h> 99 #include <sys/wait.h> 100 #include <sys/file.h> 101 #include <sys/vnode.h> 102 #include <sys/syslog.h> 103 #include <sys/malloc.h> 104 #include <sys/pool.h> 105 #include <sys/resourcevar.h> 106 #if defined(PERFCTRS) 107 #include <sys/pmc.h> 108 #endif 109 #include <sys/ptrace.h> 110 #include <sys/acct.h> 111 #include <sys/filedesc.h> 112 #include <sys/ras.h> 113 #include <sys/signalvar.h> 114 #include <sys/sched.h> 115 #include <sys/mount.h> 116 #include <sys/syscallargs.h> 117 #include <sys/systrace.h> 118 119 #include <machine/cpu.h> 120 121 #include <uvm/uvm_extern.h> 122 123 124 /* 125 * exit -- 126 * Death of process. 127 */ 128 int 129 sys_exit(struct proc *p, void *v, register_t *retval) 130 { 131 struct sys_exit_args /* { 132 syscallarg(int) rval; 133 } */ *uap = v; 134 135 exit1(p, W_EXITCODE(SCARG(uap, rval), 0)); 136 /* NOTREACHED */ 137 return (0); 138 } 139 140 /* 141 * Exit: deallocate address space and other resources, change proc state 142 * to zombie, and unlink proc from allproc and parent's lists. Save exit 143 * status and rusage for wait(). Check for child processes and orphan them. 144 */ 145 void 146 exit1(struct proc *p, int rv) 147 { 148 struct proc *q, *nq; 149 int s; 150 151 if (__predict_false(p == initproc)) 152 panic("init died (signal %d, exit %d)", 153 WTERMSIG(rv), WEXITSTATUS(rv)); 154 155 #ifdef PGINPROF 156 vmsizmon(); 157 #endif 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 p->p_flag |= P_WEXIT; 166 if (p->p_flag & P_PPWAIT) { 167 p->p_flag &= ~P_PPWAIT; 168 wakeup((caddr_t)p->p_pptr); 169 } 170 sigfillset(&p->p_sigctx.ps_sigignore); 171 sigemptyset(&p->p_sigctx.ps_siglist); 172 p->p_sigctx.ps_sigcheck = 0; 173 callout_stop(&p->p_realit_ch); 174 175 #if defined(__HAVE_RAS) 176 ras_purgeall(p); 177 #endif 178 179 /* 180 * Close open files and release open-file table. 181 * This may block! 182 */ 183 fdfree(p); 184 cwdfree(p); 185 186 doexithooks(p); 187 188 if (SESS_LEADER(p)) { 189 struct session *sp = p->p_session; 190 191 if (sp->s_ttyvp) { 192 /* 193 * Controlling process. 194 * Signal foreground pgrp, 195 * drain controlling terminal 196 * and revoke access to controlling terminal. 197 */ 198 if (sp->s_ttyp->t_session == sp) { 199 if (sp->s_ttyp->t_pgrp) 200 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1); 201 (void) 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, REVOKEALL); 208 } 209 if (sp->s_ttyvp) 210 vrele(sp->s_ttyvp); 211 sp->s_ttyvp = NULL; 212 /* 213 * s_ttyp is not zero'd; we use this to indicate 214 * that the session once had a controlling terminal. 215 * (for logging and informational purposes) 216 */ 217 } 218 sp->s_leader = NULL; 219 } 220 fixjobc(p, p->p_pgrp, 0); 221 (void)acct_process(p); 222 #ifdef KTRACE 223 /* 224 * release trace file 225 */ 226 ktrderef(p); 227 #endif 228 #ifdef SYSTRACE 229 systrace_sys_exit(p); 230 #endif 231 /* 232 * If emulation has process exit hook, call it now. 233 */ 234 if (p->p_emul->e_proc_exit) 235 (*p->p_emul->e_proc_exit)(p); 236 237 /* 238 * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP! 239 */ 240 p->p_stat = SDEAD; 241 242 /* 243 * Remove proc from pidhash chain so looking it up won't 244 * work. Move it from allproc to zombproc, but do not yet 245 * wake up the reaper. We will put the proc on the 246 * deadproc list later (using the p_hash member), and 247 * wake up the reaper when we do. 248 */ 249 s = proclist_lock_write(); 250 LIST_REMOVE(p, p_hash); 251 LIST_REMOVE(p, p_list); 252 LIST_INSERT_HEAD(&zombproc, p, p_list); 253 proclist_unlock_write(s); 254 255 /* 256 * Give orphaned children to init(8). 257 */ 258 q = LIST_FIRST(&p->p_children); 259 if (q) /* only need this if any child is S_ZOMB */ 260 wakeup((caddr_t)initproc); 261 for (; q != 0; q = nq) { 262 nq = LIST_NEXT(q, p_sibling); 263 264 /* 265 * Traced processes are killed since their existence 266 * means someone is screwing up. Since we reset the 267 * trace flags, the logic in sys_wait4() would not be 268 * triggered to reparent the process to its 269 * original parent, so we must do this here. 270 */ 271 if (q->p_flag & P_TRACED) { 272 if (q->p_opptr != q->p_pptr) { 273 struct proc *t = q->p_opptr; 274 proc_reparent(q, t ? t : initproc); 275 q->p_opptr = NULL; 276 } else 277 proc_reparent(q, initproc); 278 q->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE); 279 psignal(q, SIGKILL); 280 } else { 281 proc_reparent(q, initproc); 282 } 283 } 284 285 /* 286 * Reset p_opptr pointer of all former children which got 287 * traced by another process and were reparented. We reset 288 * it to NULL here; the trace detach code then reparents 289 * the child to initproc. We only check allproc list, since 290 * eventual former children on zombproc list won't reference 291 * p_opptr anymore. 292 */ 293 if (p->p_flag & P_CHTRACED) { 294 struct proc *t; 295 296 proclist_lock_read(); 297 298 LIST_FOREACH(t, &allproc, p_list) { 299 if (t->p_opptr == p) 300 t->p_opptr = NULL; 301 } 302 303 proclist_unlock_read(); 304 } 305 306 /* 307 * Save exit status and final rusage info, adding in child rusage 308 * info and self times. 309 */ 310 p->p_xstat = rv; 311 *p->p_ru = p->p_stats->p_ru; 312 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL); 313 ruadd(p->p_ru, &p->p_stats->p_cru); 314 315 /* 316 * Notify interested parties of our demise. 317 */ 318 KNOTE(&p->p_klist, NOTE_EXIT); 319 320 #if PERFCTRS 321 /* 322 * Save final PMC information in parent process & clean up. 323 */ 324 if (PMC_ENABLED(p)) { 325 pmc_save_context(p); 326 pmc_accumulate(p->p_pptr, p); 327 pmc_process_exit(p); 328 } 329 #endif 330 331 /* 332 * Notify parent that we're gone. If parent has the P_NOCLDWAIT 333 * flag set, notify init instead (and hope it will handle 334 * this situation). 335 */ 336 if (p->p_pptr->p_flag & P_NOCLDWAIT) { 337 struct proc *pp = p->p_pptr; 338 proc_reparent(p, initproc); 339 /* 340 * If this was the last child of our parent, notify 341 * parent, so in case he was wait(2)ing, he will 342 * continue. 343 */ 344 if (LIST_FIRST(&pp->p_children) == NULL) 345 wakeup((caddr_t)pp); 346 } 347 348 /* 349 * Release the process's signal state. 350 */ 351 sigactsfree(p); 352 353 /* 354 * Clear curproc after we've done all operations 355 * that could block, and before tearing down the rest 356 * of the process state that might be used from clock, etc. 357 * Also, can't clear curproc while we're still runnable, 358 * as we're not on a run queue (we are current, just not 359 * a proper proc any longer!). 360 * 361 * Other substructures are freed from wait(). 362 */ 363 curproc = NULL; 364 limfree(p->p_limit); 365 p->p_limit = NULL; 366 367 /* This process no longer needs to hold the kernel lock. */ 368 KERNEL_PROC_UNLOCK(p); 369 370 /* 371 * Finally, call machine-dependent code to switch to a new 372 * context (possibly the idle context). Once we are no longer 373 * using the dead process's vmspace and stack, exit2() will be 374 * called to schedule those resources to be released by the 375 * reaper thread. 376 * 377 * Note that cpu_exit() will end with a call equivalent to 378 * cpu_switch(), finishing our execution (pun intended). 379 */ 380 cpu_exit(p); 381 } 382 383 /* 384 * We are called from cpu_exit() once it is safe to schedule the 385 * dead process's resources to be freed (i.e., once we've switched to 386 * the idle PCB for the current CPU). 387 * 388 * NOTE: One must be careful with locking in this routine. It's 389 * called from a critical section in machine-dependent code, so 390 * we should refrain from changing any interrupt state. 391 * 392 * We lock the deadproc list (a spin lock), place the proc on that 393 * list (using the p_hash member), and wake up the reaper. 394 */ 395 void 396 exit2(struct proc *p) 397 { 398 399 simple_lock(&deadproc_slock); 400 LIST_INSERT_HEAD(&deadproc, p, p_hash); 401 simple_unlock(&deadproc_slock); 402 403 wakeup(&deadproc); 404 } 405 406 /* 407 * Process reaper. This is run by a kernel thread to free the resources 408 * of a dead process. Once the resources are free, the process becomes 409 * a zombie, and the parent is allowed to read the undead's status. 410 */ 411 void 412 reaper(void *arg) 413 { 414 struct proc *p; 415 416 KERNEL_PROC_UNLOCK(curproc); 417 418 for (;;) { 419 simple_lock(&deadproc_slock); 420 p = LIST_FIRST(&deadproc); 421 if (p == NULL) { 422 /* No work for us; go to sleep until someone exits. */ 423 (void) ltsleep(&deadproc, PVM|PNORELOCK, 424 "reaper", 0, &deadproc_slock); 425 continue; 426 } 427 428 /* Remove us from the deadproc list. */ 429 LIST_REMOVE(p, p_hash); 430 simple_unlock(&deadproc_slock); 431 KERNEL_PROC_LOCK(curproc); 432 433 /* 434 * Give machine-dependent code a chance to free any 435 * resources it couldn't free while still running on 436 * that process's context. This must be done before 437 * uvm_exit(), in case these resources are in the PCB. 438 */ 439 cpu_wait(p); 440 441 /* 442 * Free the VM resources we're still holding on to. 443 * We must do this from a valid thread because doing 444 * so may block. 445 */ 446 uvm_exit(p); 447 448 /* Process is now a true zombie. */ 449 p->p_stat = SZOMB; 450 451 /* Wake up the parent so it can get exit status. */ 452 if ((p->p_flag & P_FSTRACE) == 0 && p->p_exitsig != 0) 453 psignal(p->p_pptr, P_EXITSIG(p)); 454 KERNEL_PROC_UNLOCK(curproc); 455 wakeup((caddr_t)p->p_pptr); 456 } 457 } 458 459 int 460 sys_wait4(struct proc *q, void *v, register_t *retval) 461 { 462 struct sys_wait4_args /* { 463 syscallarg(int) pid; 464 syscallarg(int *) status; 465 syscallarg(int) options; 466 syscallarg(struct rusage *) rusage; 467 } */ *uap = v; 468 struct proc *p, *t; 469 int nfound, status, error, s; 470 471 if (SCARG(uap, pid) == 0) 472 SCARG(uap, pid) = -q->p_pgid; 473 if (SCARG(uap, options) &~ (WUNTRACED|WNOHANG|WALTSIG)) 474 return (EINVAL); 475 476 loop: 477 nfound = 0; 478 LIST_FOREACH(p, &q->p_children, p_sibling) { 479 if (SCARG(uap, pid) != WAIT_ANY && 480 p->p_pid != SCARG(uap, pid) && 481 p->p_pgid != -SCARG(uap, pid)) 482 continue; 483 /* 484 * Wait for processes with p_exitsig != SIGCHLD processes only 485 * if WALTSIG is set; wait for processes with p_exitsig == 486 * SIGCHLD only if WALTSIG is clear. 487 */ 488 if (((SCARG(uap, options) & WALLSIG) == 0) && 489 ((SCARG(uap, options) & WALTSIG) ? 490 (p->p_exitsig == SIGCHLD) : (P_EXITSIG(p) != SIGCHLD))) 491 continue; 492 493 nfound++; 494 if (p->p_stat == SZOMB) { 495 retval[0] = p->p_pid; 496 497 if (SCARG(uap, status)) { 498 status = p->p_xstat; /* convert to int */ 499 error = copyout((caddr_t)&status, 500 (caddr_t)SCARG(uap, status), 501 sizeof(status)); 502 if (error) 503 return (error); 504 } 505 if (SCARG(uap, rusage) && 506 (error = copyout((caddr_t)p->p_ru, 507 (caddr_t)SCARG(uap, rusage), 508 sizeof(struct rusage)))) 509 return (error); 510 /* 511 * If we got the child via ptrace(2) or procfs, and 512 * the parent is different (meaning the process was 513 * attached, rather than run as a child), then we need 514 * to give it back to the old parent, and send the 515 * parent the exit signal. The rest of the cleanup 516 * will be done when the old parent waits on the child. 517 */ 518 if ((p->p_flag & P_TRACED) && p->p_opptr != p->p_pptr){ 519 t = p->p_opptr; 520 proc_reparent(p, t ? t : initproc); 521 p->p_opptr = NULL; 522 p->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE); 523 if (p->p_exitsig != 0) 524 psignal(p->p_pptr, P_EXITSIG(p)); 525 wakeup((caddr_t)p->p_pptr); 526 return (0); 527 } 528 scheduler_wait_hook(q, p); 529 p->p_xstat = 0; 530 ruadd(&q->p_stats->p_cru, p->p_ru); 531 pool_put(&rusage_pool, p->p_ru); 532 533 /* 534 * Finally finished with old proc entry. 535 * Unlink it from its process group and free it. 536 */ 537 leavepgrp(p); 538 539 s = proclist_lock_write(); 540 LIST_REMOVE(p, p_list); /* off zombproc */ 541 proclist_unlock_write(s); 542 543 LIST_REMOVE(p, p_sibling); 544 545 /* 546 * Decrement the count of procs running with this uid. 547 */ 548 (void)chgproccnt(p->p_cred->p_ruid, -1); 549 550 /* 551 * Free up credentials. 552 */ 553 if (--p->p_cred->p_refcnt == 0) { 554 crfree(p->p_cred->pc_ucred); 555 pool_put(&pcred_pool, p->p_cred); 556 } 557 558 /* 559 * Release reference to text vnode 560 */ 561 if (p->p_textvp) 562 vrele(p->p_textvp); 563 564 pool_put(&proc_pool, p); 565 nprocs--; 566 return (0); 567 } 568 if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 && 569 (p->p_flag & P_TRACED || SCARG(uap, options) & WUNTRACED)) { 570 p->p_flag |= P_WAITED; 571 retval[0] = p->p_pid; 572 573 if (SCARG(uap, status)) { 574 status = W_STOPCODE(p->p_xstat); 575 error = copyout((caddr_t)&status, 576 (caddr_t)SCARG(uap, status), 577 sizeof(status)); 578 } else 579 error = 0; 580 return (error); 581 } 582 } 583 if (nfound == 0) 584 return (ECHILD); 585 if (SCARG(uap, options) & WNOHANG) { 586 retval[0] = 0; 587 return (0); 588 } 589 if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)) != 0) 590 return (error); 591 goto loop; 592 } 593 594 /* 595 * make process 'parent' the new parent of process 'child'. 596 */ 597 void 598 proc_reparent(struct proc *child, struct proc *parent) 599 { 600 601 if (child->p_pptr == parent) 602 return; 603 604 if (parent == initproc) 605 child->p_exitsig = SIGCHLD; 606 607 LIST_REMOVE(child, p_sibling); 608 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 609 child->p_pptr = parent; 610 } 611