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