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 if (vm->vm_refcnt == 1) { 226 if (vm->vm_shm) 227 shmexit(p); 228 pmap_remove_pages(vmspace_pmap(vm), VM_MIN_ADDRESS, 229 VM_MAXUSER_ADDRESS); 230 (void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS, 231 VM_MAXUSER_ADDRESS); 232 } 233 234 PROC_LOCK(p); 235 if (SESS_LEADER(p)) { 236 register struct session *sp = p->p_session; 237 238 PROC_UNLOCK(p); 239 if (sp->s_ttyvp) { 240 /* 241 * Controlling process. 242 * Signal foreground pgrp, 243 * drain controlling terminal 244 * and revoke access to controlling terminal. 245 */ 246 if (sp->s_ttyp && (sp->s_ttyp->t_session == sp)) { 247 if (sp->s_ttyp->t_pgrp) 248 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1); 249 (void) ttywait(sp->s_ttyp); 250 /* 251 * The tty could have been revoked 252 * if we blocked. 253 */ 254 if (sp->s_ttyvp) 255 VOP_REVOKE(sp->s_ttyvp, REVOKEALL); 256 } 257 if (sp->s_ttyvp) 258 vrele(sp->s_ttyvp); 259 sp->s_ttyvp = NULL; 260 /* 261 * s_ttyp is not zero'd; we use this to indicate 262 * that the session once had a controlling terminal. 263 * (for logging and informational purposes) 264 */ 265 } 266 sp->s_leader = NULL; 267 } else 268 PROC_UNLOCK(p); 269 fixjobc(p, p->p_pgrp, 0); 270 (void)acct_process(p); 271 #ifdef KTRACE 272 /* 273 * release trace file 274 */ 275 p->p_traceflag = 0; /* don't trace the vrele() */ 276 if (p->p_tracep) 277 vrele(p->p_tracep); 278 #endif 279 /* 280 * Remove proc from allproc queue and pidhash chain. 281 * Place onto zombproc. Unlink from parent's child list. 282 */ 283 sx_xlock(&allproc_lock); 284 LIST_REMOVE(p, p_list); 285 LIST_INSERT_HEAD(&zombproc, p, p_list); 286 LIST_REMOVE(p, p_hash); 287 sx_xunlock(&allproc_lock); 288 289 sx_xlock(&proctree_lock); 290 q = LIST_FIRST(&p->p_children); 291 if (q != NULL) /* only need this if any child is S_ZOMB */ 292 wakeup((caddr_t) initproc); 293 for (; q != NULL; q = nq) { 294 nq = LIST_NEXT(q, p_sibling); 295 PROC_LOCK(q); 296 proc_reparent(q, initproc); 297 q->p_sigparent = SIGCHLD; 298 /* 299 * Traced processes are killed 300 * since their existence means someone is screwing up. 301 */ 302 if (q->p_flag & P_TRACED) { 303 q->p_flag &= ~P_TRACED; 304 psignal(q, SIGKILL); 305 } 306 PROC_UNLOCK(q); 307 } 308 309 /* 310 * Save exit status and final rusage info, adding in child rusage 311 * info and self times. 312 */ 313 p->p_xstat = rv; 314 *p->p_ru = p->p_stats->p_ru; 315 mtx_lock_spin(&sched_lock); 316 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL); 317 mtx_unlock_spin(&sched_lock); 318 ruadd(p->p_ru, &p->p_stats->p_cru); 319 320 /* 321 * Pretend that an mi_switch() to the next process occurs now. We 322 * must set `switchtime' directly since we will call cpu_switch() 323 * directly. Set it now so that the rest of the exit time gets 324 * counted somewhere if possible. 325 */ 326 mtx_lock_spin(&sched_lock); 327 microuptime(PCPU_PTR(switchtime)); 328 PCPU_SET(switchticks, ticks); 329 mtx_unlock_spin(&sched_lock); 330 331 /* 332 * notify interested parties of our demise. 333 */ 334 PROC_LOCK(p); 335 KNOTE(&p->p_klist, NOTE_EXIT); 336 337 /* 338 * Notify parent that we're gone. If parent has the PS_NOCLDWAIT 339 * flag set, notify process 1 instead (and hope it will handle 340 * this situation). 341 */ 342 if (p->p_pptr->p_procsig->ps_flag & PS_NOCLDWAIT) { 343 struct proc *pp = p->p_pptr; 344 proc_reparent(p, initproc); 345 /* 346 * If this was the last child of our parent, notify 347 * parent, so in case he was wait(2)ing, he will 348 * continue. 349 */ 350 if (LIST_EMPTY(&pp->p_children)) 351 wakeup((caddr_t)pp); 352 } 353 354 PROC_LOCK(p->p_pptr); 355 if (p->p_sigparent && p->p_pptr != initproc) 356 psignal(p->p_pptr, p->p_sigparent); 357 else 358 psignal(p->p_pptr, SIGCHLD); 359 PROC_UNLOCK(p->p_pptr); 360 PROC_UNLOCK(p); 361 sx_xunlock(&proctree_lock); 362 363 /* 364 * Clear curproc after we've done all operations 365 * that could block, and before tearing down the rest 366 * of the process state that might be used from clock, etc. 367 * Also, can't clear curproc while we're still runnable, 368 * as we're not on a run queue (we are current, just not 369 * a proper proc any longer!). 370 * 371 * Other substructures are freed from wait(). 372 */ 373 mtx_assert(&Giant, MA_OWNED); 374 if (--p->p_limit->p_refcnt == 0) { 375 FREE(p->p_limit, M_SUBPROC); 376 p->p_limit = NULL; 377 } 378 379 /* 380 * Finally, call machine-dependent code to release the remaining 381 * resources including address space, the kernel stack and pcb. 382 * The address space is released by "vmspace_free(p->p_vmspace)"; 383 * This is machine-dependent, as we may have to change stacks 384 * or ensure that the current one isn't reallocated before we 385 * finish. cpu_exit will end with a call to cpu_switch(), finishing 386 * our execution (pun intended). 387 */ 388 cpu_exit(p); 389 } 390 391 #ifdef COMPAT_43 392 int 393 owait(p, uap) 394 struct proc *p; 395 register struct owait_args /* { 396 int dummy; 397 } */ *uap; 398 { 399 struct wait_args w; 400 401 w.options = 0; 402 w.rusage = NULL; 403 w.pid = WAIT_ANY; 404 w.status = NULL; 405 return (wait1(p, &w, 1)); 406 } 407 #endif /* COMPAT_43 */ 408 409 int 410 wait4(p, uap) 411 struct proc *p; 412 struct wait_args *uap; 413 { 414 415 return (wait1(p, uap, 0)); 416 } 417 418 static int 419 wait1(q, uap, compat) 420 register struct proc *q; 421 register struct wait_args /* { 422 int pid; 423 int *status; 424 int options; 425 struct rusage *rusage; 426 } */ *uap; 427 int compat; 428 { 429 register int nfound; 430 register struct proc *p, *t; 431 int status, error; 432 433 if (uap->pid == 0) 434 uap->pid = -q->p_pgid; 435 if (uap->options &~ (WUNTRACED|WNOHANG|WLINUXCLONE)) 436 return (EINVAL); 437 loop: 438 nfound = 0; 439 sx_slock(&proctree_lock); 440 LIST_FOREACH(p, &q->p_children, p_sibling) { 441 if (uap->pid != WAIT_ANY && 442 p->p_pid != uap->pid && p->p_pgid != -uap->pid) 443 continue; 444 445 /* 446 * This special case handles a kthread spawned by linux_clone 447 * (see linux_misc.c). The linux_wait4 and linux_waitpid 448 * functions need to be able to distinguish between waiting 449 * on a process and waiting on a thread. It is a thread if 450 * p_sigparent is not SIGCHLD, and the WLINUXCLONE option 451 * signifies we want to wait for threads and not processes. 452 */ 453 PROC_LOCK(p); 454 if ((p->p_sigparent != SIGCHLD) ^ 455 ((uap->options & WLINUXCLONE) != 0)) { 456 PROC_UNLOCK(p); 457 continue; 458 } 459 460 nfound++; 461 mtx_lock_spin(&sched_lock); 462 if (p->p_stat == SZOMB) { 463 /* charge childs scheduling cpu usage to parent */ 464 if (curproc->p_pid != 1) { 465 curproc->p_estcpu = 466 ESTCPULIM(curproc->p_estcpu + p->p_estcpu); 467 } 468 469 mtx_unlock_spin(&sched_lock); 470 PROC_UNLOCK(p); 471 sx_sunlock(&proctree_lock); 472 473 q->p_retval[0] = p->p_pid; 474 #ifdef COMPAT_43 475 if (compat) 476 q->p_retval[1] = p->p_xstat; 477 else 478 #endif 479 if (uap->status) { 480 status = p->p_xstat; /* convert to int */ 481 if ((error = copyout((caddr_t)&status, 482 (caddr_t)uap->status, sizeof(status)))) 483 return (error); 484 } 485 if (uap->rusage && (error = copyout((caddr_t)p->p_ru, 486 (caddr_t)uap->rusage, sizeof (struct rusage)))) 487 return (error); 488 /* 489 * If we got the child via a ptrace 'attach', 490 * we need to give it back to the old parent. 491 */ 492 sx_xlock(&proctree_lock); 493 if (p->p_oppid) { 494 if ((t = pfind(p->p_oppid)) != NULL) { 495 PROC_LOCK(p); 496 p->p_oppid = 0; 497 proc_reparent(p, t); 498 PROC_UNLOCK(p); 499 psignal(t, SIGCHLD); 500 wakeup((caddr_t)t); 501 PROC_UNLOCK(t); 502 sx_xunlock(&proctree_lock); 503 return (0); 504 } 505 } 506 sx_xunlock(&proctree_lock); 507 PROC_LOCK(p); 508 p->p_xstat = 0; 509 PROC_UNLOCK(p); 510 ruadd(&q->p_stats->p_cru, p->p_ru); 511 FREE(p->p_ru, M_ZOMBIE); 512 p->p_ru = NULL; 513 514 /* 515 * Decrement the count of procs running with this uid. 516 */ 517 (void)chgproccnt(p->p_cred->p_uidinfo, -1, 0); 518 519 /* 520 * Release reference to text vnode 521 */ 522 if (p->p_textvp) 523 vrele(p->p_textvp); 524 525 /* 526 * Finally finished with old proc entry. 527 * Unlink it from its process group and free it. 528 */ 529 leavepgrp(p); 530 531 sx_xlock(&allproc_lock); 532 LIST_REMOVE(p, p_list); /* off zombproc */ 533 sx_xunlock(&allproc_lock); 534 535 sx_xlock(&proctree_lock); 536 LIST_REMOVE(p, p_sibling); 537 sx_xunlock(&proctree_lock); 538 539 /* 540 * Free up credentials. 541 */ 542 if (--p->p_cred->p_refcnt == 0) { 543 crfree(p->p_ucred); 544 uifree(p->p_cred->p_uidinfo); 545 FREE(p->p_cred, M_SUBPROC); 546 p->p_cred = NULL; 547 } 548 549 /* 550 * Remove unused arguments 551 */ 552 if (p->p_args && --p->p_args->ar_ref == 0) 553 FREE(p->p_args, M_PARGS); 554 555 if (--p->p_procsig->ps_refcnt == 0) { 556 if (p->p_sigacts != &p->p_addr->u_sigacts) 557 FREE(p->p_sigacts, M_SUBPROC); 558 FREE(p->p_procsig, M_SUBPROC); 559 p->p_procsig = NULL; 560 } 561 562 /* 563 * Give machine-dependent layer a chance 564 * to free anything that cpu_exit couldn't 565 * release while still running in process context. 566 */ 567 cpu_wait(p); 568 mtx_destroy(&p->p_mtx); 569 zfree(proc_zone, p); 570 nprocs--; 571 return (0); 572 } 573 if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 && 574 (p->p_flag & P_TRACED || uap->options & WUNTRACED)) { 575 mtx_unlock_spin(&sched_lock); 576 p->p_flag |= P_WAITED; 577 PROC_UNLOCK(p); 578 sx_sunlock(&proctree_lock); 579 q->p_retval[0] = p->p_pid; 580 #ifdef COMPAT_43 581 if (compat) { 582 q->p_retval[1] = W_STOPCODE(p->p_xstat); 583 error = 0; 584 } else 585 #endif 586 if (uap->status) { 587 status = W_STOPCODE(p->p_xstat); 588 error = copyout((caddr_t)&status, 589 (caddr_t)uap->status, sizeof(status)); 590 } else 591 error = 0; 592 return (error); 593 } 594 mtx_unlock_spin(&sched_lock); 595 PROC_UNLOCK(p); 596 } 597 sx_sunlock(&proctree_lock); 598 if (nfound == 0) 599 return (ECHILD); 600 if (uap->options & WNOHANG) { 601 q->p_retval[0] = 0; 602 return (0); 603 } 604 if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0))) 605 return (error); 606 goto loop; 607 } 608 609 /* 610 * Make process 'parent' the new parent of process 'child'. 611 * Must be called with an exclusive hold of proctree lock. 612 */ 613 void 614 proc_reparent(child, parent) 615 register struct proc *child; 616 register struct proc *parent; 617 { 618 619 SX_ASSERT_XLOCKED(&proctree_lock); 620 PROC_LOCK_ASSERT(child, MA_OWNED); 621 if (child->p_pptr == parent) 622 return; 623 624 LIST_REMOVE(child, p_sibling); 625 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 626 child->p_pptr = parent; 627 } 628 629 /* 630 * The next two functions are to handle adding/deleting items on the 631 * exit callout list 632 * 633 * at_exit(): 634 * Take the arguments given and put them onto the exit callout list, 635 * However first make sure that it's not already there. 636 * returns 0 on success. 637 */ 638 639 int 640 at_exit(function) 641 exitlist_fn function; 642 { 643 struct exitlist *ep; 644 645 #ifdef INVARIANTS 646 /* Be noisy if the programmer has lost track of things */ 647 if (rm_at_exit(function)) 648 printf("WARNING: exit callout entry (%p) already present\n", 649 function); 650 #endif 651 ep = malloc(sizeof(*ep), M_ATEXIT, M_NOWAIT); 652 if (ep == NULL) 653 return (ENOMEM); 654 ep->function = function; 655 TAILQ_INSERT_TAIL(&exit_list, ep, next); 656 return (0); 657 } 658 659 /* 660 * Scan the exit callout list for the given item and remove it. 661 * Returns the number of items removed (0 or 1) 662 */ 663 int 664 rm_at_exit(function) 665 exitlist_fn function; 666 { 667 struct exitlist *ep; 668 669 TAILQ_FOREACH(ep, &exit_list, next) { 670 if (ep->function == function) { 671 TAILQ_REMOVE(&exit_list, ep, next); 672 free(ep, M_ATEXIT); 673 return(1); 674 } 675 } 676 return (0); 677 } 678 679 void check_sigacts (void) 680 { 681 struct proc *p = curproc; 682 struct sigacts *pss; 683 int s; 684 685 PROC_LOCK(p); 686 if (p->p_procsig->ps_refcnt == 1 && 687 p->p_sigacts != &p->p_addr->u_sigacts) { 688 pss = p->p_sigacts; 689 s = splhigh(); 690 p->p_addr->u_sigacts = *pss; 691 p->p_sigacts = &p->p_addr->u_sigacts; 692 splx(s); 693 FREE(pss, M_SUBPROC); 694 } 695 PROC_UNLOCK(p); 696 } 697 698