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$ 40 */ 41 42 #include "opt_compat.h" 43 #include "opt_ktrace.h" 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/sysproto.h> 48 #include <sys/kernel.h> 49 #include <sys/malloc.h> 50 #include <sys/lock.h> 51 #include <sys/mutex.h> 52 #include <sys/proc.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/sx.h> 60 #include <sys/ptrace.h> 61 #include <sys/acct.h> /* for acct_process() function prototype */ 62 #include <sys/filedesc.h> 63 #include <sys/shm.h> 64 #include <sys/sem.h> 65 #include <sys/aio.h> 66 #include <sys/jail.h> 67 68 #include <vm/vm.h> 69 #include <vm/vm_param.h> 70 #include <vm/pmap.h> 71 #include <vm/vm_map.h> 72 #include <vm/vm_zone.h> 73 #include <sys/user.h> 74 75 /* Required to be non-static for SysVR4 emulator */ 76 MALLOC_DEFINE(M_ZOMBIE, "zombie", "zombie proc status"); 77 78 static MALLOC_DEFINE(M_ATEXIT, "atexit", "atexit callback"); 79 80 static int wait1 __P((struct proc *, struct wait_args *, int)); 81 82 /* 83 * callout list for things to do at exit time 84 */ 85 struct exitlist { 86 exitlist_fn function; 87 TAILQ_ENTRY(exitlist) next; 88 }; 89 90 TAILQ_HEAD(exit_list_head, exitlist); 91 static struct exit_list_head exit_list = TAILQ_HEAD_INITIALIZER(exit_list); 92 93 /* 94 * exit -- 95 * Death of process. 96 */ 97 void 98 sys_exit(p, uap) 99 struct proc *p; 100 struct sys_exit_args /* { 101 int rval; 102 } */ *uap; 103 { 104 105 exit1(p, W_EXITCODE(uap->rval, 0)); 106 /* NOTREACHED */ 107 } 108 109 /* 110 * Exit: deallocate address space and other resources, change proc state 111 * to zombie, and unlink proc from allproc and parent's lists. Save exit 112 * status and rusage for wait(). Check for child processes and orphan them. 113 */ 114 void 115 exit1(p, rv) 116 register struct proc *p; 117 int rv; 118 { 119 register struct proc *q, *nq; 120 register struct vmspace *vm; 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 aio_proc_rundown(p); 130 131 /* are we a task leader? */ 132 PROC_LOCK(p); 133 if(p == p->p_leader) { 134 struct kill_args killArgs; 135 136 killArgs.signum = SIGKILL; 137 q = p->p_peers; 138 while(q) { 139 killArgs.pid = q->p_pid; 140 /* 141 * The interface for kill is better 142 * than the internal signal 143 */ 144 PROC_UNLOCK(p); 145 kill(p, &killArgs); 146 PROC_LOCK(p); 147 nq = q; 148 q = q->p_peers; 149 } 150 while (p->p_peers) 151 msleep((caddr_t)p, &p->p_mtx, PWAIT, "exit1", 0); 152 } 153 PROC_UNLOCK(p); 154 155 #ifdef PGINPROF 156 vmsizmon(); 157 #endif 158 STOPEVENT(p, S_EXIT, rv); 159 wakeup(&p->p_stype); /* Wakeup anyone in procfs' PIOCWAIT */ 160 161 /* 162 * Check if any loadable modules need anything done at process exit. 163 * e.g. SYSV IPC stuff 164 * XXX what if one of these generates an error? 165 */ 166 TAILQ_FOREACH(ep, &exit_list, next) 167 (*ep->function)(p); 168 169 stopprofclock(p); 170 171 MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage), 172 M_ZOMBIE, M_WAITOK); 173 /* 174 * If parent is waiting for us to exit or exec, 175 * P_PPWAIT is set; we will wakeup the parent below. 176 */ 177 PROC_LOCK(p); 178 p->p_flag &= ~(P_TRACED | P_PPWAIT); 179 p->p_flag |= P_WEXIT; 180 SIGEMPTYSET(p->p_siglist); 181 PROC_UNLOCK(p); 182 if (timevalisset(&p->p_realtimer.it_value)) 183 callout_stop(&p->p_itcallout); 184 185 /* 186 * Reset any sigio structures pointing to us as a result of 187 * F_SETOWN with our pid. 188 */ 189 funsetownlst(&p->p_sigiolst); 190 191 /* 192 * Close open files and release open-file table. 193 * This may block! 194 */ 195 fdfree(p); 196 197 /* 198 * Remove ourself from our leader's peer list and wake our leader. 199 */ 200 PROC_LOCK(p); 201 if(p->p_leader->p_peers) { 202 q = p->p_leader; 203 while(q->p_peers != p) 204 q = q->p_peers; 205 q->p_peers = p->p_peers; 206 wakeup((caddr_t)p->p_leader); 207 } 208 PROC_UNLOCK(p); 209 210 /* 211 * XXX Shutdown SYSV semaphores 212 */ 213 semexit(p); 214 215 /* The next two chunks should probably be moved to vmspace_exit. */ 216 vm = p->p_vmspace; 217 /* 218 * Release user portion of address space. 219 * This releases references to vnodes, 220 * which could cause I/O if the file has been unlinked. 221 * Need to do this early enough that we can still sleep. 222 * Can't free the entire vmspace as the kernel stack 223 * may be mapped within that space also. 224 */ 225 mtx_lock(&vm_mtx); 226 if (vm->vm_refcnt == 1) { 227 if (vm->vm_shm) 228 shmexit(p); 229 pmap_remove_pages(vmspace_pmap(vm), VM_MIN_ADDRESS, 230 VM_MAXUSER_ADDRESS); 231 (void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS, 232 VM_MAXUSER_ADDRESS); 233 } 234 mtx_unlock(&vm_mtx); 235 236 PROC_LOCK(p); 237 if (SESS_LEADER(p)) { 238 register struct session *sp = p->p_session; 239 240 PROC_UNLOCK(p); 241 if (sp->s_ttyvp) { 242 /* 243 * Controlling process. 244 * Signal foreground pgrp, 245 * drain controlling terminal 246 * and revoke access to controlling terminal. 247 */ 248 if (sp->s_ttyp && (sp->s_ttyp->t_session == sp)) { 249 if (sp->s_ttyp->t_pgrp) 250 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1); 251 (void) ttywait(sp->s_ttyp); 252 /* 253 * The tty could have been revoked 254 * if we blocked. 255 */ 256 if (sp->s_ttyvp) 257 VOP_REVOKE(sp->s_ttyvp, REVOKEALL); 258 } 259 if (sp->s_ttyvp) 260 vrele(sp->s_ttyvp); 261 sp->s_ttyvp = NULL; 262 /* 263 * s_ttyp is not zero'd; we use this to indicate 264 * that the session once had a controlling terminal. 265 * (for logging and informational purposes) 266 */ 267 } 268 sp->s_leader = NULL; 269 } else 270 PROC_UNLOCK(p); 271 fixjobc(p, p->p_pgrp, 0); 272 (void)acct_process(p); 273 #ifdef KTRACE 274 /* 275 * release trace file 276 */ 277 p->p_traceflag = 0; /* don't trace the vrele() */ 278 if (p->p_tracep) 279 vrele(p->p_tracep); 280 #endif 281 /* 282 * Remove proc from allproc queue and pidhash chain. 283 * Place onto zombproc. Unlink from parent's child list. 284 */ 285 sx_xlock(&allproc_lock); 286 LIST_REMOVE(p, p_list); 287 LIST_INSERT_HEAD(&zombproc, p, p_list); 288 LIST_REMOVE(p, p_hash); 289 sx_xunlock(&allproc_lock); 290 291 sx_xlock(&proctree_lock); 292 q = LIST_FIRST(&p->p_children); 293 if (q != NULL) /* only need this if any child is S_ZOMB */ 294 wakeup((caddr_t) initproc); 295 for (; q != NULL; q = nq) { 296 nq = LIST_NEXT(q, p_sibling); 297 PROC_LOCK(q); 298 proc_reparent(q, initproc); 299 q->p_sigparent = SIGCHLD; 300 /* 301 * Traced processes are killed 302 * since their existence means someone is screwing up. 303 */ 304 if (q->p_flag & P_TRACED) { 305 q->p_flag &= ~P_TRACED; 306 psignal(q, SIGKILL); 307 } 308 PROC_UNLOCK(q); 309 } 310 311 /* 312 * Save exit status and final rusage info, adding in child rusage 313 * info and self times. 314 */ 315 p->p_xstat = rv; 316 *p->p_ru = p->p_stats->p_ru; 317 mtx_lock_spin(&sched_lock); 318 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL); 319 mtx_unlock_spin(&sched_lock); 320 ruadd(p->p_ru, &p->p_stats->p_cru); 321 322 /* 323 * Pretend that an mi_switch() to the next process occurs now. We 324 * must set `switchtime' directly since we will call cpu_switch() 325 * directly. Set it now so that the rest of the exit time gets 326 * counted somewhere if possible. 327 */ 328 mtx_lock_spin(&sched_lock); 329 microuptime(PCPU_PTR(switchtime)); 330 PCPU_SET(switchticks, ticks); 331 mtx_unlock_spin(&sched_lock); 332 333 /* 334 * notify interested parties of our demise. 335 */ 336 PROC_LOCK(p); 337 KNOTE(&p->p_klist, NOTE_EXIT); 338 339 /* 340 * Notify parent that we're gone. If parent has the PS_NOCLDWAIT 341 * flag set, notify process 1 instead (and hope it will handle 342 * this situation). 343 */ 344 if (p->p_pptr->p_procsig->ps_flag & PS_NOCLDWAIT) { 345 struct proc *pp = p->p_pptr; 346 proc_reparent(p, initproc); 347 /* 348 * If this was the last child of our parent, notify 349 * parent, so in case he was wait(2)ing, he will 350 * continue. 351 */ 352 if (LIST_EMPTY(&pp->p_children)) 353 wakeup((caddr_t)pp); 354 } 355 356 PROC_LOCK(p->p_pptr); 357 if (p->p_sigparent && p->p_pptr != initproc) 358 psignal(p->p_pptr, p->p_sigparent); 359 else 360 psignal(p->p_pptr, SIGCHLD); 361 PROC_UNLOCK(p->p_pptr); 362 PROC_UNLOCK(p); 363 sx_xunlock(&proctree_lock); 364 365 /* 366 * Clear curproc after we've done all operations 367 * that could block, and before tearing down the rest 368 * of the process state that might be used from clock, etc. 369 * Also, can't clear curproc while we're still runnable, 370 * as we're not on a run queue (we are current, just not 371 * a proper proc any longer!). 372 * 373 * Other substructures are freed from wait(). 374 */ 375 mtx_assert(&Giant, MA_OWNED); 376 if (--p->p_limit->p_refcnt == 0) { 377 FREE(p->p_limit, M_SUBPROC); 378 p->p_limit = NULL; 379 } 380 381 /* 382 * Finally, call machine-dependent code to release the remaining 383 * resources including address space, the kernel stack and pcb. 384 * The address space is released by "vmspace_free(p->p_vmspace)"; 385 * This is machine-dependent, as we may have to change stacks 386 * or ensure that the current one isn't reallocated before we 387 * finish. cpu_exit will end with a call to cpu_switch(), finishing 388 * our execution (pun intended). 389 */ 390 cpu_exit(p); 391 } 392 393 #ifdef COMPAT_43 394 int 395 owait(p, uap) 396 struct proc *p; 397 register struct owait_args /* { 398 int dummy; 399 } */ *uap; 400 { 401 struct wait_args w; 402 403 w.options = 0; 404 w.rusage = NULL; 405 w.pid = WAIT_ANY; 406 w.status = NULL; 407 return (wait1(p, &w, 1)); 408 } 409 #endif /* COMPAT_43 */ 410 411 int 412 wait4(p, uap) 413 struct proc *p; 414 struct wait_args *uap; 415 { 416 417 return (wait1(p, uap, 0)); 418 } 419 420 static int 421 wait1(q, uap, compat) 422 register struct proc *q; 423 register struct wait_args /* { 424 int pid; 425 int *status; 426 int options; 427 struct rusage *rusage; 428 } */ *uap; 429 int compat; 430 { 431 register int nfound; 432 register struct proc *p, *t; 433 int status, error; 434 435 if (uap->pid == 0) 436 uap->pid = -q->p_pgid; 437 if (uap->options &~ (WUNTRACED|WNOHANG|WLINUXCLONE)) 438 return (EINVAL); 439 loop: 440 nfound = 0; 441 sx_slock(&proctree_lock); 442 LIST_FOREACH(p, &q->p_children, p_sibling) { 443 if (uap->pid != WAIT_ANY && 444 p->p_pid != uap->pid && p->p_pgid != -uap->pid) 445 continue; 446 447 /* 448 * This special case handles a kthread spawned by linux_clone 449 * (see linux_misc.c). The linux_wait4 and linux_waitpid 450 * functions need to be able to distinguish between waiting 451 * on a process and waiting on a thread. It is a thread if 452 * p_sigparent is not SIGCHLD, and the WLINUXCLONE option 453 * signifies we want to wait for threads and not processes. 454 */ 455 PROC_LOCK(p); 456 if ((p->p_sigparent != SIGCHLD) ^ 457 ((uap->options & WLINUXCLONE) != 0)) { 458 PROC_UNLOCK(p); 459 continue; 460 } 461 462 nfound++; 463 mtx_lock_spin(&sched_lock); 464 if (p->p_stat == SZOMB) { 465 /* charge childs scheduling cpu usage to parent */ 466 if (curproc->p_pid != 1) { 467 curproc->p_estcpu = 468 ESTCPULIM(curproc->p_estcpu + p->p_estcpu); 469 } 470 471 mtx_unlock_spin(&sched_lock); 472 PROC_UNLOCK(p); 473 sx_sunlock(&proctree_lock); 474 475 q->p_retval[0] = p->p_pid; 476 #ifdef COMPAT_43 477 if (compat) 478 q->p_retval[1] = p->p_xstat; 479 else 480 #endif 481 if (uap->status) { 482 status = p->p_xstat; /* convert to int */ 483 if ((error = copyout((caddr_t)&status, 484 (caddr_t)uap->status, sizeof(status)))) 485 return (error); 486 } 487 if (uap->rusage && (error = copyout((caddr_t)p->p_ru, 488 (caddr_t)uap->rusage, sizeof (struct rusage)))) 489 return (error); 490 /* 491 * If we got the child via a ptrace 'attach', 492 * we need to give it back to the old parent. 493 */ 494 sx_xlock(&proctree_lock); 495 if (p->p_oppid) { 496 if ((t = pfind(p->p_oppid)) != NULL) { 497 PROC_LOCK(p); 498 p->p_oppid = 0; 499 proc_reparent(p, t); 500 PROC_UNLOCK(p); 501 psignal(t, SIGCHLD); 502 wakeup((caddr_t)t); 503 PROC_UNLOCK(t); 504 sx_xunlock(&proctree_lock); 505 return (0); 506 } 507 } 508 sx_xunlock(&proctree_lock); 509 PROC_LOCK(p); 510 p->p_xstat = 0; 511 PROC_UNLOCK(p); 512 ruadd(&q->p_stats->p_cru, p->p_ru); 513 FREE(p->p_ru, M_ZOMBIE); 514 p->p_ru = NULL; 515 516 /* 517 * Decrement the count of procs running with this uid. 518 */ 519 (void)chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0); 520 521 /* 522 * Release reference to text vnode 523 */ 524 if (p->p_textvp) 525 vrele(p->p_textvp); 526 527 /* 528 * Finally finished with old proc entry. 529 * Unlink it from its process group and free it. 530 */ 531 leavepgrp(p); 532 533 sx_xlock(&allproc_lock); 534 LIST_REMOVE(p, p_list); /* off zombproc */ 535 sx_xunlock(&allproc_lock); 536 537 sx_xlock(&proctree_lock); 538 LIST_REMOVE(p, p_sibling); 539 sx_xunlock(&proctree_lock); 540 541 /* 542 * Free up credentials. 543 */ 544 crfree(p->p_ucred); 545 p->p_ucred = NULL; 546 547 /* 548 * Remove unused arguments 549 */ 550 if (p->p_args && --p->p_args->ar_ref == 0) 551 FREE(p->p_args, M_PARGS); 552 553 if (--p->p_procsig->ps_refcnt == 0) { 554 if (p->p_sigacts != &p->p_addr->u_sigacts) 555 FREE(p->p_sigacts, M_SUBPROC); 556 FREE(p->p_procsig, M_SUBPROC); 557 p->p_procsig = NULL; 558 } 559 560 /* 561 * Give machine-dependent layer a chance 562 * to free anything that cpu_exit couldn't 563 * release while still running in process context. 564 */ 565 cpu_wait(p); 566 mtx_destroy(&p->p_mtx); 567 zfree(proc_zone, p); 568 nprocs--; 569 return (0); 570 } 571 if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 && 572 (p->p_flag & P_TRACED || uap->options & WUNTRACED)) { 573 mtx_unlock_spin(&sched_lock); 574 p->p_flag |= P_WAITED; 575 PROC_UNLOCK(p); 576 sx_sunlock(&proctree_lock); 577 q->p_retval[0] = p->p_pid; 578 #ifdef COMPAT_43 579 if (compat) { 580 q->p_retval[1] = W_STOPCODE(p->p_xstat); 581 error = 0; 582 } else 583 #endif 584 if (uap->status) { 585 status = W_STOPCODE(p->p_xstat); 586 error = copyout((caddr_t)&status, 587 (caddr_t)uap->status, sizeof(status)); 588 } else 589 error = 0; 590 return (error); 591 } 592 mtx_unlock_spin(&sched_lock); 593 PROC_UNLOCK(p); 594 } 595 sx_sunlock(&proctree_lock); 596 if (nfound == 0) 597 return (ECHILD); 598 if (uap->options & WNOHANG) { 599 q->p_retval[0] = 0; 600 return (0); 601 } 602 if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0))) 603 return (error); 604 goto loop; 605 } 606 607 /* 608 * Make process 'parent' the new parent of process 'child'. 609 * Must be called with an exclusive hold of proctree lock. 610 */ 611 void 612 proc_reparent(child, parent) 613 register struct proc *child; 614 register struct proc *parent; 615 { 616 617 SX_ASSERT_XLOCKED(&proctree_lock); 618 PROC_LOCK_ASSERT(child, MA_OWNED); 619 if (child->p_pptr == parent) 620 return; 621 622 LIST_REMOVE(child, p_sibling); 623 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 624 child->p_pptr = parent; 625 } 626 627 /* 628 * The next two functions are to handle adding/deleting items on the 629 * exit callout list 630 * 631 * at_exit(): 632 * Take the arguments given and put them onto the exit callout list, 633 * However first make sure that it's not already there. 634 * returns 0 on success. 635 */ 636 637 int 638 at_exit(function) 639 exitlist_fn function; 640 { 641 struct exitlist *ep; 642 643 #ifdef INVARIANTS 644 /* Be noisy if the programmer has lost track of things */ 645 if (rm_at_exit(function)) 646 printf("WARNING: exit callout entry (%p) already present\n", 647 function); 648 #endif 649 ep = malloc(sizeof(*ep), M_ATEXIT, M_NOWAIT); 650 if (ep == NULL) 651 return (ENOMEM); 652 ep->function = function; 653 TAILQ_INSERT_TAIL(&exit_list, ep, next); 654 return (0); 655 } 656 657 /* 658 * Scan the exit callout list for the given item and remove it. 659 * Returns the number of items removed (0 or 1) 660 */ 661 int 662 rm_at_exit(function) 663 exitlist_fn function; 664 { 665 struct exitlist *ep; 666 667 TAILQ_FOREACH(ep, &exit_list, next) { 668 if (ep->function == function) { 669 TAILQ_REMOVE(&exit_list, ep, next); 670 free(ep, M_ATEXIT); 671 return(1); 672 } 673 } 674 return (0); 675 } 676 677 void check_sigacts (void) 678 { 679 struct proc *p = curproc; 680 struct sigacts *pss; 681 int s; 682 683 PROC_LOCK(p); 684 if (p->p_procsig->ps_refcnt == 1 && 685 p->p_sigacts != &p->p_addr->u_sigacts) { 686 pss = p->p_sigacts; 687 s = splhigh(); 688 p->p_addr->u_sigacts = *pss; 689 p->p_sigacts = &p->p_addr->u_sigacts; 690 splx(s); 691 FREE(pss, M_SUBPROC); 692 } 693 PROC_UNLOCK(p); 694 } 695 696