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.58 2006/06/07 03:02:10 dillon 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 proc *p = curproc; 117 struct lwp *lp; 118 struct proc *q, *nq; 119 struct vmspace *vm; 120 struct vnode *vtmp; 121 struct exitlist *ep; 122 123 if (p->p_pid == 1) { 124 printf("init died (signal %d, exit %d)\n", 125 WTERMSIG(rv), WEXITSTATUS(rv)); 126 panic("Going nowhere without my init!"); 127 } 128 129 lp = &p->p_lwp; /* XXX lwp kill other threads */ 130 131 caps_exit(lp->lwp_thread); 132 aio_proc_rundown(p); 133 134 /* are we a task leader? */ 135 if(p == p->p_leader) { 136 struct kill_args killArgs; 137 killArgs.signum = SIGKILL; 138 q = p->p_peers; 139 while(q) { 140 killArgs.pid = q->p_pid; 141 /* 142 * The interface for kill is better 143 * than the internal signal 144 */ 145 sys_kill(&killArgs); 146 nq = q; 147 q = q->p_peers; 148 } 149 while (p->p_peers) 150 tsleep((caddr_t)p, 0, "exit1", 0); 151 } 152 153 #ifdef PGINPROF 154 vmsizmon(); 155 #endif 156 STOPEVENT(p, S_EXIT, rv); 157 wakeup(&p->p_stype); /* Wakeup anyone in procfs' PIOCWAIT */ 158 159 /* 160 * Check if any loadable modules need anything done at process exit. 161 * e.g. SYSV IPC stuff 162 * XXX what if one of these generates an error? 163 */ 164 TAILQ_FOREACH(ep, &exit_list, next) 165 (*ep->function)(p->p_thread); 166 167 if (p->p_flag & P_PROFIL) 168 stopprofclock(p); 169 MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage), 170 M_ZOMBIE, M_WAITOK); 171 /* 172 * If parent is waiting for us to exit or exec, 173 * P_PPWAIT is set; we will wakeup the parent below. 174 */ 175 p->p_flag &= ~(P_TRACED | P_PPWAIT); 176 p->p_flag |= P_WEXIT; 177 SIGEMPTYSET(p->p_siglist); 178 if (timevalisset(&p->p_realtimer.it_value)) 179 callout_stop(&p->p_ithandle); 180 181 /* 182 * Reset any sigio structures pointing to us as a result of 183 * F_SETOWN with our pid. 184 */ 185 funsetownlst(&p->p_sigiolst); 186 187 /* 188 * Close open files and release open-file table. 189 * This may block! 190 */ 191 fdfree(p); 192 p->p_fd = NULL; 193 194 if(p->p_leader->p_peers) { 195 q = p->p_leader; 196 while(q->p_peers != p) 197 q = q->p_peers; 198 q->p_peers = p->p_peers; 199 wakeup((caddr_t)p->p_leader); 200 } 201 202 /* 203 * XXX Shutdown SYSV semaphores 204 */ 205 semexit(p); 206 207 KKASSERT(p->p_numposixlocks == 0); 208 209 /* The next two chunks should probably be moved to vmspace_exit. */ 210 vm = p->p_vmspace; 211 212 /* 213 * Release upcalls associated with this process 214 */ 215 if (vm->vm_upcalls) 216 upc_release(vm, &p->p_lwp); 217 218 /* 219 * Release user portion of address space. 220 * This releases references to vnodes, 221 * which could cause I/O if the file has been unlinked. 222 * Need to do this early enough that we can still sleep. 223 * Can't free the entire vmspace as the kernel stack 224 * may be mapped within that space also. 225 * 226 * Processes sharing the same vmspace may exit in one order, and 227 * get cleaned up by vmspace_exit() in a different order. The 228 * last exiting process to reach this point releases as much of 229 * the environment as it can, and the last process cleaned up 230 * by vmspace_exit() (which decrements exitingcnt) cleans up the 231 * remainder. 232 */ 233 ++vm->vm_exitingcnt; 234 if (--vm->vm_refcnt == 0) { 235 shmexit(vm); 236 pmap_remove_pages(vmspace_pmap(vm), VM_MIN_ADDRESS, 237 VM_MAXUSER_ADDRESS); 238 (void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS, 239 VM_MAXUSER_ADDRESS); 240 } 241 242 if (SESS_LEADER(p)) { 243 struct session *sp = p->p_session; 244 struct vnode *vp; 245 246 if (sp->s_ttyvp) { 247 /* 248 * We are the controlling process. Signal the 249 * foreground process group, drain the controlling 250 * terminal, and revoke access to the controlling 251 * terminal. 252 * 253 * NOTE: while waiting for the process group to exit 254 * it is possible that one of the processes in the 255 * group will revoke the tty, so we have to recheck. 256 */ 257 if (sp->s_ttyp && (sp->s_ttyp->t_session == sp)) { 258 if (sp->s_ttyp->t_pgrp) 259 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1); 260 (void) ttywait(sp->s_ttyp); 261 /* 262 * The tty could have been revoked 263 * if we blocked. 264 */ 265 if ((vp = sp->s_ttyvp) != NULL) { 266 ttyclosesession(sp, 0); 267 if (vx_lock(vp) == 0) { 268 VOP_REVOKE(vp, REVOKEALL); 269 vx_unlock(vp); 270 } 271 vrele(vp); /* s_ttyvp ref */ 272 } 273 } 274 /* 275 * Release the tty. If someone has it open via 276 * /dev/tty then close it (since they no longer can 277 * once we've NULL'd it out). 278 */ 279 if (sp->s_ttyvp) 280 ttyclosesession(sp, 1); 281 /* 282 * s_ttyp is not zero'd; we use this to indicate 283 * that the session once had a controlling terminal. 284 * (for logging and informational purposes) 285 */ 286 } 287 sp->s_leader = NULL; 288 } 289 fixjobc(p, p->p_pgrp, 0); 290 (void)acct_process(p); 291 #ifdef KTRACE 292 /* 293 * release trace file 294 */ 295 if (p->p_tracenode) 296 ktrdestroy(&p->p_tracenode); 297 p->p_traceflag = 0; 298 #endif 299 /* 300 * Release reference to text vnode 301 */ 302 if ((vtmp = p->p_textvp) != NULL) { 303 p->p_textvp = NULL; 304 vrele(vtmp); 305 } 306 307 /* 308 * Move the process to the zombie list. This will block 309 * until the process p_lock count reaches 0. The process will 310 * not be reaped until TDF_EXITING is set by cpu_thread_exit(), 311 * which is called from cpu_proc_exit(). 312 */ 313 proc_move_allproc_zombie(p); 314 315 q = LIST_FIRST(&p->p_children); 316 if (q) /* only need this if any child is S_ZOMB */ 317 wakeup((caddr_t) initproc); 318 for (; q != 0; q = nq) { 319 nq = LIST_NEXT(q, p_sibling); 320 LIST_REMOVE(q, p_sibling); 321 LIST_INSERT_HEAD(&initproc->p_children, q, p_sibling); 322 q->p_pptr = initproc; 323 q->p_sigparent = SIGCHLD; 324 /* 325 * Traced processes are killed 326 * since their existence means someone is screwing up. 327 */ 328 if (q->p_flag & P_TRACED) { 329 q->p_flag &= ~P_TRACED; 330 psignal(q, SIGKILL); 331 } 332 } 333 334 /* 335 * Save exit status and final rusage info, adding in child rusage 336 * info and self times. 337 */ 338 p->p_xstat = rv; 339 *p->p_ru = p->p_stats->p_ru; 340 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL); 341 ruadd(p->p_ru, &p->p_stats->p_cru); 342 343 /* 344 * notify interested parties of our demise. 345 */ 346 KNOTE(&p->p_klist, NOTE_EXIT); 347 348 /* 349 * Notify parent that we're gone. If parent has the PS_NOCLDWAIT 350 * flag set, notify process 1 instead (and hope it will handle 351 * this situation). 352 */ 353 if (p->p_pptr->p_procsig->ps_flag & PS_NOCLDWAIT) { 354 struct proc *pp = p->p_pptr; 355 proc_reparent(p, initproc); 356 /* 357 * If this was the last child of our parent, notify 358 * parent, so in case he was wait(2)ing, he will 359 * continue. 360 */ 361 if (LIST_EMPTY(&pp->p_children)) 362 wakeup((caddr_t)pp); 363 } 364 365 if (p->p_sigparent && p->p_pptr != initproc) { 366 psignal(p->p_pptr, p->p_sigparent); 367 } else { 368 psignal(p->p_pptr, SIGCHLD); 369 } 370 371 wakeup((caddr_t)p->p_pptr); 372 /* 373 * cpu_exit is responsible for clearing curproc, since 374 * it is heavily integrated with the thread/switching sequence. 375 * 376 * Other substructures are freed from wait(). 377 */ 378 plimit_free(&p->p_limit); 379 380 /* 381 * Release the current user process designation on the process so 382 * the userland scheduler can work in someone else. 383 */ 384 p->p_usched->release_curproc(lp); 385 386 /* 387 * Finally, call machine-dependent code to release the remaining 388 * resources including address space, the kernel stack and pcb. 389 * The address space is released by "vmspace_free(p->p_vmspace)"; 390 * This is machine-dependent, as we may have to change stacks 391 * or ensure that the current one isn't reallocated before we 392 * finish. cpu_exit will end with a call to cpu_switch(), finishing 393 * our execution (pun intended). 394 */ 395 cpu_proc_exit(); 396 } 397 398 int 399 sys_wait4(struct wait_args *uap) 400 { 401 struct rusage rusage; 402 int error, status; 403 404 error = kern_wait(uap->pid, uap->status ? &status : NULL, 405 uap->options, uap->rusage ? &rusage : NULL, &uap->sysmsg_fds[0]); 406 407 if (error == 0 && uap->status) 408 error = copyout(&status, uap->status, sizeof(*uap->status)); 409 if (error == 0 && uap->rusage) 410 error = copyout(&rusage, uap->rusage, sizeof(*uap->rusage)); 411 return (error); 412 } 413 414 /* 415 * wait1() 416 * 417 * wait_args(int pid, int *status, int options, struct rusage *rusage) 418 */ 419 int 420 kern_wait(pid_t pid, int *status, int options, struct rusage *rusage, int *res) 421 { 422 struct thread *td = curthread; 423 struct proc *q = td->td_proc; 424 struct proc *p, *t; 425 int nfound, error; 426 427 if (pid == 0) 428 pid = -q->p_pgid; 429 if (options &~ (WUNTRACED|WNOHANG|WLINUXCLONE)) 430 return (EINVAL); 431 loop: 432 /* 433 * Hack for backwards compatibility with badly written user code. 434 * Or perhaps we have to do this anyway, it is unclear. XXX 435 * 436 * The problem is that if a process group is stopped and the parent 437 * is doing a wait*(..., WUNTRACED, ...), it will see the STOP 438 * of the child and then stop itself when it tries to return from the 439 * system call. When the process group is resumed the parent will 440 * then get the STOP status even though the child has now resumed 441 * (a followup wait*() will get the CONT status). 442 * 443 * Previously the CONT would overwrite the STOP because the tstop 444 * was handled within tsleep(), and the parent would only see 445 * the CONT when both are stopped and continued together. This litte 446 * two-line hack restores this effect. 447 */ 448 while (q->p_flag & P_STOPPED) 449 tstop(q); 450 451 nfound = 0; 452 LIST_FOREACH(p, &q->p_children, p_sibling) { 453 if (pid != WAIT_ANY && 454 p->p_pid != pid && p->p_pgid != -pid) 455 continue; 456 457 /* This special case handles a kthread spawned by linux_clone 458 * (see linux_misc.c). The linux_wait4 and linux_waitpid 459 * functions need to be able to distinguish between waiting 460 * on a process and waiting on a thread. It is a thread if 461 * p_sigparent is not SIGCHLD, and the WLINUXCLONE option 462 * signifies we want to wait for threads and not processes. 463 */ 464 if ((p->p_sigparent != SIGCHLD) ^ 465 ((options & WLINUXCLONE) != 0)) { 466 continue; 467 } 468 469 nfound++; 470 if (p->p_flag & P_ZOMBIE) { 471 /* 472 * Other kernel threads may be in the middle of 473 * accessing the proc. For example, kern/kern_proc.c 474 * could be blocked writing proc data to a sysctl. 475 * At the moment, if this occurs, we are not woken 476 * up and rely on a one-second retry. 477 */ 478 if (p->p_lock) { 479 while (p->p_lock) 480 tsleep(p, 0, "reap3", hz); 481 } 482 lwkt_wait_free(p->p_thread); 483 484 /* 485 * The process's thread may still be in the middle 486 * of switching away, we can't rip its stack out from 487 * under it until TDF_EXITING is set and both 488 * TDF_RUNNING and TDF_PREEMPT_LOCK are clear. 489 * TDF_PREEMPT_LOCK must be checked because TDF_RUNNING 490 * will be cleared temporarily if a thread gets 491 * preempted. 492 * 493 * YYY no wakeup occurs so we depend on the timeout. 494 */ 495 if ((p->p_thread->td_flags & (TDF_RUNNING|TDF_PREEMPT_LOCK|TDF_EXITING)) != TDF_EXITING) { 496 tsleep(p->p_thread, 0, "reap2", 1); 497 goto loop; 498 } 499 500 /* scheduling hook for heuristic */ 501 p->p_usched->heuristic_exiting(td->td_lwp, &p->p_lwp); 502 503 /* Take care of our return values. */ 504 *res = p->p_pid; 505 if (status) 506 *status = p->p_xstat; 507 if (rusage) 508 *rusage = *p->p_ru; 509 /* 510 * If we got the child via a ptrace 'attach', 511 * we need to give it back to the old parent. 512 */ 513 if (p->p_oppid && (t = pfind(p->p_oppid))) { 514 p->p_oppid = 0; 515 proc_reparent(p, t); 516 psignal(t, SIGCHLD); 517 wakeup((caddr_t)t); 518 return (0); 519 } 520 p->p_xstat = 0; 521 ruadd(&q->p_stats->p_cru, p->p_ru); 522 FREE(p->p_ru, M_ZOMBIE); 523 p->p_ru = NULL; 524 525 /* 526 * Decrement the count of procs running with this uid. 527 */ 528 chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0); 529 530 /* 531 * Free up credentials. 532 */ 533 crfree(p->p_ucred); 534 p->p_ucred = NULL; 535 536 /* 537 * Remove unused arguments 538 */ 539 if (p->p_args && --p->p_args->ar_ref == 0) 540 FREE(p->p_args, M_PARGS); 541 542 /* 543 * Finally finished with old proc entry. 544 * Unlink it from its process group and free it. 545 */ 546 leavepgrp(p); 547 proc_remove_zombie(p); 548 549 if (--p->p_procsig->ps_refcnt == 0) { 550 if (p->p_sigacts != &p->p_addr->u_sigacts) 551 FREE(p->p_sigacts, M_SUBPROC); 552 FREE(p->p_procsig, M_SUBPROC); 553 p->p_procsig = NULL; 554 } 555 556 vm_waitproc(p); 557 zfree(proc_zone, p); 558 nprocs--; 559 return (0); 560 } 561 if ((p->p_flag & P_STOPPED) && (p->p_flag & P_WAITED) == 0 && 562 (p->p_flag & P_TRACED || options & WUNTRACED)) { 563 p->p_flag |= P_WAITED; 564 565 *res = p->p_pid; 566 if (status) 567 *status = W_STOPCODE(p->p_xstat); 568 /* Zero rusage so we get something consistent. */ 569 if (rusage) 570 bzero(rusage, sizeof(rusage)); 571 return (0); 572 } 573 } 574 if (nfound == 0) 575 return (ECHILD); 576 if (options & WNOHANG) { 577 *res = 0; 578 return (0); 579 } 580 error = tsleep((caddr_t)q, PCATCH, "wait", 0); 581 if (error) 582 return (error); 583 goto loop; 584 } 585 586 /* 587 * make process 'parent' the new parent of process 'child'. 588 */ 589 void 590 proc_reparent(struct proc *child, struct proc *parent) 591 { 592 593 if (child->p_pptr == parent) 594 return; 595 596 LIST_REMOVE(child, p_sibling); 597 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 598 child->p_pptr = parent; 599 } 600 601 /* 602 * The next two functions are to handle adding/deleting items on the 603 * exit callout list 604 * 605 * at_exit(): 606 * Take the arguments given and put them onto the exit callout list, 607 * However first make sure that it's not already there. 608 * returns 0 on success. 609 */ 610 611 int 612 at_exit(exitlist_fn function) 613 { 614 struct exitlist *ep; 615 616 #ifdef INVARIANTS 617 /* Be noisy if the programmer has lost track of things */ 618 if (rm_at_exit(function)) 619 printf("WARNING: exit callout entry (%p) already present\n", 620 function); 621 #endif 622 ep = malloc(sizeof(*ep), M_ATEXIT, M_NOWAIT); 623 if (ep == NULL) 624 return (ENOMEM); 625 ep->function = function; 626 TAILQ_INSERT_TAIL(&exit_list, ep, next); 627 return (0); 628 } 629 630 /* 631 * Scan the exit callout list for the given item and remove it. 632 * Returns the number of items removed (0 or 1) 633 */ 634 int 635 rm_at_exit(exitlist_fn function) 636 { 637 struct exitlist *ep; 638 639 TAILQ_FOREACH(ep, &exit_list, next) { 640 if (ep->function == function) { 641 TAILQ_REMOVE(&exit_list, ep, next); 642 free(ep, M_ATEXIT); 643 return(1); 644 } 645 } 646 return (0); 647 } 648 649 void 650 check_sigacts(void) 651 { 652 struct proc *p = curproc; 653 struct sigacts *pss; 654 655 if (p->p_procsig->ps_refcnt == 1 && 656 p->p_sigacts != &p->p_addr->u_sigacts) { 657 pss = p->p_sigacts; 658 crit_enter(); 659 p->p_addr->u_sigacts = *pss; 660 p->p_sigacts = &p->p_addr->u_sigacts; 661 crit_exit(); 662 FREE(pss, M_SUBPROC); 663 } 664 } 665 666