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