1 /* 2 * Copyright (c) 1982, 1986, 1989, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)kern_exit.c 8.7 (Berkeley) 2/12/94 39 * $FreeBSD: src/sys/kern/kern_exit.c,v 1.92.2.11 2003/01/13 22:51:16 dillon Exp $ 40 * $DragonFly: src/sys/kern/kern_exit.c,v 1.63 2006/09/19 11:47:35 corecode Exp $ 41 */ 42 43 #include "opt_compat.h" 44 #include "opt_ktrace.h" 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/sysproto.h> 49 #include <sys/kernel.h> 50 #include <sys/malloc.h> 51 #include <sys/proc.h> 52 #include <sys/ktrace.h> 53 #include <sys/pioctl.h> 54 #include <sys/tty.h> 55 #include <sys/wait.h> 56 #include <sys/vnode.h> 57 #include <sys/resourcevar.h> 58 #include <sys/signalvar.h> 59 #include <sys/ptrace.h> 60 #include <sys/acct.h> /* for acct_process() function prototype */ 61 #include <sys/filedesc.h> 62 #include <sys/shm.h> 63 #include <sys/sem.h> 64 #include <sys/aio.h> 65 #include <sys/jail.h> 66 #include <sys/kern_syscall.h> 67 #include <sys/upcall.h> 68 #include <sys/caps.h> 69 70 #include <vm/vm.h> 71 #include <vm/vm_param.h> 72 #include <sys/lock.h> 73 #include <vm/pmap.h> 74 #include <vm/vm_map.h> 75 #include <vm/vm_zone.h> 76 #include <vm/vm_extern.h> 77 #include <sys/user.h> 78 79 #include <sys/thread2.h> 80 81 static MALLOC_DEFINE(M_ATEXIT, "atexit", "atexit callback"); 82 static MALLOC_DEFINE(M_ZOMBIE, "zombie", "zombie proc status"); 83 84 /* 85 * callout list for things to do at exit time 86 */ 87 struct exitlist { 88 exitlist_fn function; 89 TAILQ_ENTRY(exitlist) next; 90 }; 91 92 TAILQ_HEAD(exit_list_head, exitlist); 93 static struct exit_list_head exit_list = TAILQ_HEAD_INITIALIZER(exit_list); 94 95 /* 96 * exit -- 97 * Death of process. 98 * 99 * SYS_EXIT_ARGS(int rval) 100 */ 101 int 102 sys_exit(struct exit_args *uap) 103 { 104 exit1(W_EXITCODE(uap->rval, 0)); 105 /* NOTREACHED */ 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(int rv) 115 { 116 struct thread *td = curthread; 117 struct proc *p = td->td_proc; 118 struct lwp *lp = td->td_lwp; 119 struct proc *q, *nq; 120 struct vmspace *vm; 121 struct vnode *vtmp; 122 struct exitlist *ep; 123 124 if (p->p_pid == 1) { 125 printf("init died (signal %d, exit %d)\n", 126 WTERMSIG(rv), WEXITSTATUS(rv)); 127 panic("Going nowhere without my init!"); 128 } 129 130 /* XXX lwp kill other threads */ 131 132 caps_exit(lp->lwp_thread); 133 aio_proc_rundown(p); 134 135 /* are we a task leader? */ 136 if(p == p->p_leader) { 137 struct kill_args killArgs; 138 killArgs.signum = SIGKILL; 139 q = p->p_peers; 140 while(q) { 141 killArgs.pid = q->p_pid; 142 /* 143 * The interface for kill is better 144 * than the internal signal 145 */ 146 sys_kill(&killArgs); 147 nq = q; 148 q = q->p_peers; 149 } 150 while (p->p_peers) 151 tsleep((caddr_t)p, 0, "exit1", 0); 152 } 153 154 #ifdef PGINPROF 155 vmsizmon(); 156 #endif 157 STOPEVENT(p, S_EXIT, rv); 158 wakeup(&p->p_stype); /* Wakeup anyone in procfs' PIOCWAIT */ 159 160 /* 161 * Check if any loadable modules need anything done at process exit. 162 * e.g. SYSV IPC stuff 163 * XXX what if one of these generates an error? 164 */ 165 TAILQ_FOREACH(ep, &exit_list, next) 166 (*ep->function)(td); 167 168 if (p->p_flag & P_PROFIL) 169 stopprofclock(p); 170 MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage), 171 M_ZOMBIE, M_WAITOK); 172 /* 173 * If parent is waiting for us to exit or exec, 174 * P_PPWAIT is set; we will wakeup the parent below. 175 */ 176 p->p_flag &= ~(P_TRACED | P_PPWAIT); 177 p->p_flag |= P_WEXIT; 178 SIGEMPTYSET(p->p_siglist); 179 if (timevalisset(&p->p_realtimer.it_value)) 180 callout_stop(&p->p_ithandle); 181 182 /* 183 * Reset any sigio structures pointing to us as a result of 184 * F_SETOWN with our pid. 185 */ 186 funsetownlst(&p->p_sigiolst); 187 188 /* 189 * Close open files and release open-file table. 190 * This may block! 191 */ 192 fdfree(p); 193 p->p_fd = NULL; 194 195 if(p->p_leader->p_peers) { 196 q = p->p_leader; 197 while(q->p_peers != p) 198 q = q->p_peers; 199 q->p_peers = p->p_peers; 200 wakeup((caddr_t)p->p_leader); 201 } 202 203 /* 204 * XXX Shutdown SYSV semaphores 205 */ 206 semexit(p); 207 208 KKASSERT(p->p_numposixlocks == 0); 209 210 /* The next two chunks should probably be moved to vmspace_exit. */ 211 vm = p->p_vmspace; 212 213 /* 214 * Release upcalls associated with this process 215 */ 216 if (vm->vm_upcalls) 217 upc_release(vm, &p->p_lwp); 218 219 /* clean up data related to virtual kernel operation */ 220 if (p->p_vkernel) { 221 vkernel_drop(p->p_vkernel); 222 p->p_vkernel = NULL; 223 } 224 225 /* 226 * Release user portion of address space. 227 * This releases references to vnodes, 228 * which could cause I/O if the file has been unlinked. 229 * Need to do this early enough that we can still sleep. 230 * Can't free the entire vmspace as the kernel stack 231 * may be mapped within that space also. 232 * 233 * Processes sharing the same vmspace may exit in one order, and 234 * get cleaned up by vmspace_exit() in a different order. The 235 * last exiting process to reach this point releases as much of 236 * the environment as it can, and the last process cleaned up 237 * by vmspace_exit() (which decrements exitingcnt) cleans up the 238 * remainder. 239 */ 240 ++vm->vm_exitingcnt; 241 if (--vm->vm_refcnt == 0) { 242 shmexit(vm); 243 pmap_remove_pages(vmspace_pmap(vm), VM_MIN_ADDRESS, 244 VM_MAXUSER_ADDRESS); 245 (void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS, 246 VM_MAXUSER_ADDRESS); 247 } 248 249 if (SESS_LEADER(p)) { 250 struct session *sp = p->p_session; 251 struct vnode *vp; 252 253 if (sp->s_ttyvp) { 254 /* 255 * We are the controlling process. Signal the 256 * foreground process group, drain the controlling 257 * terminal, and revoke access to the controlling 258 * terminal. 259 * 260 * NOTE: while waiting for the process group to exit 261 * it is possible that one of the processes in the 262 * group will revoke the tty, so we have to recheck. 263 */ 264 if (sp->s_ttyp && (sp->s_ttyp->t_session == sp)) { 265 if (sp->s_ttyp->t_pgrp) 266 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1); 267 (void) ttywait(sp->s_ttyp); 268 /* 269 * The tty could have been revoked 270 * if we blocked. 271 */ 272 if ((vp = sp->s_ttyvp) != NULL) { 273 ttyclosesession(sp, 0); 274 vx_lock(vp); 275 VOP_REVOKE(vp, REVOKEALL); 276 vx_unlock(vp); 277 vrele(vp); /* s_ttyvp ref */ 278 } 279 } 280 /* 281 * Release the tty. If someone has it open via 282 * /dev/tty then close it (since they no longer can 283 * once we've NULL'd it out). 284 */ 285 if (sp->s_ttyvp) 286 ttyclosesession(sp, 1); 287 /* 288 * s_ttyp is not zero'd; we use this to indicate 289 * that the session once had a controlling terminal. 290 * (for logging and informational purposes) 291 */ 292 } 293 sp->s_leader = NULL; 294 } 295 fixjobc(p, p->p_pgrp, 0); 296 (void)acct_process(p); 297 #ifdef KTRACE 298 /* 299 * release trace file 300 */ 301 if (p->p_tracenode) 302 ktrdestroy(&p->p_tracenode); 303 p->p_traceflag = 0; 304 #endif 305 /* 306 * Release reference to text vnode 307 */ 308 if ((vtmp = p->p_textvp) != NULL) { 309 p->p_textvp = NULL; 310 vrele(vtmp); 311 } 312 313 /* 314 * Move the process to the zombie list. This will block 315 * until the process p_lock count reaches 0. The process will 316 * not be reaped until TDF_EXITING is set by cpu_thread_exit(), 317 * which is called from cpu_proc_exit(). 318 */ 319 proc_move_allproc_zombie(p); 320 321 q = LIST_FIRST(&p->p_children); 322 if (q) /* only need this if any child is S_ZOMB */ 323 wakeup((caddr_t) initproc); 324 for (; q != 0; q = nq) { 325 nq = LIST_NEXT(q, p_sibling); 326 LIST_REMOVE(q, p_sibling); 327 LIST_INSERT_HEAD(&initproc->p_children, q, p_sibling); 328 q->p_pptr = initproc; 329 q->p_sigparent = SIGCHLD; 330 /* 331 * Traced processes are killed 332 * since their existence means someone is screwing up. 333 */ 334 if (q->p_flag & P_TRACED) { 335 q->p_flag &= ~P_TRACED; 336 ksignal(q, SIGKILL); 337 } 338 } 339 340 /* 341 * Save exit status and final rusage info, adding in child rusage 342 * info and self times. 343 */ 344 p->p_xstat = rv; 345 *p->p_ru = p->p_stats->p_ru; 346 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL); 347 ruadd(p->p_ru, &p->p_stats->p_cru); 348 349 /* 350 * notify interested parties of our demise. 351 */ 352 KNOTE(&p->p_klist, NOTE_EXIT); 353 354 /* 355 * Notify parent that we're gone. If parent has the PS_NOCLDWAIT 356 * flag set, notify process 1 instead (and hope it will handle 357 * this situation). 358 */ 359 if (p->p_pptr->p_procsig->ps_flag & PS_NOCLDWAIT) { 360 struct proc *pp = p->p_pptr; 361 proc_reparent(p, initproc); 362 /* 363 * If this was the last child of our parent, notify 364 * parent, so in case he was wait(2)ing, he will 365 * continue. 366 */ 367 if (LIST_EMPTY(&pp->p_children)) 368 wakeup((caddr_t)pp); 369 } 370 371 if (p->p_sigparent && p->p_pptr != initproc) { 372 ksignal(p->p_pptr, p->p_sigparent); 373 } else { 374 ksignal(p->p_pptr, SIGCHLD); 375 } 376 377 wakeup((caddr_t)p->p_pptr); 378 /* 379 * cpu_exit is responsible for clearing curproc, since 380 * it is heavily integrated with the thread/switching sequence. 381 * 382 * Other substructures are freed from wait(). 383 */ 384 plimit_free(&p->p_limit); 385 386 /* 387 * Release the current user process designation on the process so 388 * the userland scheduler can work in someone else. 389 */ 390 p->p_usched->release_curproc(lp); 391 392 /* 393 * Finally, call machine-dependent code to release the remaining 394 * resources including address space, the kernel stack and pcb. 395 * The address space is released by "vmspace_free(p->p_vmspace)"; 396 * This is machine-dependent, as we may have to change stacks 397 * or ensure that the current one isn't reallocated before we 398 * finish. cpu_exit will end with a call to cpu_switch(), finishing 399 * our execution (pun intended). 400 */ 401 cpu_proc_exit(); 402 } 403 404 int 405 sys_wait4(struct wait_args *uap) 406 { 407 struct rusage rusage; 408 int error, status; 409 410 error = kern_wait(uap->pid, uap->status ? &status : NULL, 411 uap->options, uap->rusage ? &rusage : NULL, &uap->sysmsg_fds[0]); 412 413 if (error == 0 && uap->status) 414 error = copyout(&status, uap->status, sizeof(*uap->status)); 415 if (error == 0 && uap->rusage) 416 error = copyout(&rusage, uap->rusage, sizeof(*uap->rusage)); 417 return (error); 418 } 419 420 /* 421 * wait1() 422 * 423 * wait_args(int pid, int *status, int options, struct rusage *rusage) 424 */ 425 int 426 kern_wait(pid_t pid, int *status, int options, struct rusage *rusage, int *res) 427 { 428 struct thread *td = curthread; 429 struct thread *deadtd; 430 struct proc *q = td->td_proc; 431 struct proc *p, *t; 432 int nfound, error; 433 434 if (pid == 0) 435 pid = -q->p_pgid; 436 if (options &~ (WUNTRACED|WNOHANG|WLINUXCLONE)) 437 return (EINVAL); 438 loop: 439 /* 440 * Hack for backwards compatibility with badly written user code. 441 * Or perhaps we have to do this anyway, it is unclear. XXX 442 * 443 * The problem is that if a process group is stopped and the parent 444 * is doing a wait*(..., WUNTRACED, ...), it will see the STOP 445 * of the child and then stop itself when it tries to return from the 446 * system call. When the process group is resumed the parent will 447 * then get the STOP status even though the child has now resumed 448 * (a followup wait*() will get the CONT status). 449 * 450 * Previously the CONT would overwrite the STOP because the tstop 451 * was handled within tsleep(), and the parent would only see 452 * the CONT when both are stopped and continued together. This litte 453 * two-line hack restores this effect. 454 */ 455 while (q->p_flag & P_STOPPED) 456 tstop(q); 457 458 nfound = 0; 459 LIST_FOREACH(p, &q->p_children, p_sibling) { 460 if (pid != WAIT_ANY && 461 p->p_pid != pid && p->p_pgid != -pid) 462 continue; 463 464 /* This special case handles a kthread spawned by linux_clone 465 * (see linux_misc.c). The linux_wait4 and linux_waitpid 466 * functions need to be able to distinguish between waiting 467 * on a process and waiting on a thread. It is a thread if 468 * p_sigparent is not SIGCHLD, and the WLINUXCLONE option 469 * signifies we want to wait for threads and not processes. 470 */ 471 if ((p->p_sigparent != SIGCHLD) ^ 472 ((options & WLINUXCLONE) != 0)) { 473 continue; 474 } 475 476 nfound++; 477 if (p->p_flag & P_ZOMBIE) { 478 KKASSERT((p->p_nthreads == 1)); 479 deadtd = LIST_FIRST(&p->p_lwps)->lwp_thread; 480 481 /* 482 * Other kernel threads may be in the middle of 483 * accessing the proc. For example, kern/kern_proc.c 484 * could be blocked writing proc data to a sysctl. 485 * At the moment, if this occurs, we are not woken 486 * up and rely on a one-second retry. 487 */ 488 if (p->p_lock) { 489 while (p->p_lock) 490 tsleep(p, 0, "reap3", hz); 491 } 492 lwkt_wait_free(deadtd); 493 494 /* 495 * The process's thread may still be in the middle 496 * of switching away, we can't rip its stack out from 497 * under it until TDF_EXITING is set and both 498 * TDF_RUNNING and TDF_PREEMPT_LOCK are clear. 499 * TDF_PREEMPT_LOCK must be checked because TDF_RUNNING 500 * will be cleared temporarily if a thread gets 501 * preempted. 502 * 503 * YYY no wakeup occurs so we depend on the timeout. 504 */ 505 if ((deadtd->td_flags & (TDF_RUNNING|TDF_PREEMPT_LOCK|TDF_EXITING)) != TDF_EXITING) { 506 tsleep(deadtd, 0, "reap2", 1); 507 goto loop; 508 } 509 510 /* scheduling hook for heuristic */ 511 p->p_usched->heuristic_exiting(td->td_lwp, deadtd->td_lwp); 512 513 /* Take care of our return values. */ 514 *res = p->p_pid; 515 if (status) 516 *status = p->p_xstat; 517 if (rusage) 518 *rusage = *p->p_ru; 519 /* 520 * If we got the child via a ptrace 'attach', 521 * we need to give it back to the old parent. 522 */ 523 if (p->p_oppid && (t = pfind(p->p_oppid))) { 524 p->p_oppid = 0; 525 proc_reparent(p, t); 526 ksignal(t, SIGCHLD); 527 wakeup((caddr_t)t); 528 return (0); 529 } 530 p->p_xstat = 0; 531 ruadd(&q->p_stats->p_cru, p->p_ru); 532 FREE(p->p_ru, M_ZOMBIE); 533 p->p_ru = NULL; 534 535 /* 536 * Decrement the count of procs running with this uid. 537 */ 538 chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0); 539 540 /* 541 * Free up credentials. 542 */ 543 crfree(p->p_ucred); 544 p->p_ucred = NULL; 545 546 /* 547 * Remove unused arguments 548 */ 549 if (p->p_args && --p->p_args->ar_ref == 0) 550 FREE(p->p_args, M_PARGS); 551 552 /* 553 * Finally finished with old proc entry. 554 * Unlink it from its process group and free it. 555 */ 556 leavepgrp(p); 557 proc_remove_zombie(p); 558 559 if (--p->p_procsig->ps_refcnt == 0) { 560 if (p->p_sigacts != &p->p_addr->u_sigacts) 561 FREE(p->p_sigacts, M_SUBPROC); 562 FREE(p->p_procsig, M_SUBPROC); 563 p->p_procsig = NULL; 564 } 565 566 vm_waitproc(p); 567 zfree(proc_zone, p); 568 nprocs--; 569 return (0); 570 } 571 if ((p->p_flag & P_STOPPED) && (p->p_flag & P_WAITED) == 0 && 572 (p->p_flag & P_TRACED || options & WUNTRACED)) { 573 p->p_flag |= P_WAITED; 574 575 *res = p->p_pid; 576 if (status) 577 *status = W_STOPCODE(p->p_xstat); 578 /* Zero rusage so we get something consistent. */ 579 if (rusage) 580 bzero(rusage, sizeof(rusage)); 581 return (0); 582 } 583 } 584 if (nfound == 0) 585 return (ECHILD); 586 if (options & WNOHANG) { 587 *res = 0; 588 return (0); 589 } 590 error = tsleep((caddr_t)q, PCATCH, "wait", 0); 591 if (error) 592 return (error); 593 goto loop; 594 } 595 596 /* 597 * make process 'parent' the new parent of process 'child'. 598 */ 599 void 600 proc_reparent(struct proc *child, struct proc *parent) 601 { 602 603 if (child->p_pptr == parent) 604 return; 605 606 LIST_REMOVE(child, p_sibling); 607 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 608 child->p_pptr = parent; 609 } 610 611 /* 612 * The next two functions are to handle adding/deleting items on the 613 * exit callout list 614 * 615 * at_exit(): 616 * Take the arguments given and put them onto the exit callout list, 617 * However first make sure that it's not already there. 618 * returns 0 on success. 619 */ 620 621 int 622 at_exit(exitlist_fn function) 623 { 624 struct exitlist *ep; 625 626 #ifdef INVARIANTS 627 /* Be noisy if the programmer has lost track of things */ 628 if (rm_at_exit(function)) 629 printf("WARNING: exit callout entry (%p) already present\n", 630 function); 631 #endif 632 ep = kmalloc(sizeof(*ep), M_ATEXIT, M_NOWAIT); 633 if (ep == NULL) 634 return (ENOMEM); 635 ep->function = function; 636 TAILQ_INSERT_TAIL(&exit_list, ep, next); 637 return (0); 638 } 639 640 /* 641 * Scan the exit callout list for the given item and remove it. 642 * Returns the number of items removed (0 or 1) 643 */ 644 int 645 rm_at_exit(exitlist_fn function) 646 { 647 struct exitlist *ep; 648 649 TAILQ_FOREACH(ep, &exit_list, next) { 650 if (ep->function == function) { 651 TAILQ_REMOVE(&exit_list, ep, next); 652 kfree(ep, M_ATEXIT); 653 return(1); 654 } 655 } 656 return (0); 657 } 658 659 void 660 check_sigacts(void) 661 { 662 struct proc *p = curproc; 663 struct sigacts *pss; 664 665 if (p->p_procsig->ps_refcnt == 1 && 666 p->p_sigacts != &p->p_addr->u_sigacts) { 667 pss = p->p_sigacts; 668 crit_enter(); 669 p->p_addr->u_sigacts = *pss; 670 p->p_sigacts = &p->p_addr->u_sigacts; 671 crit_exit(); 672 FREE(pss, M_SUBPROC); 673 } 674 } 675 676