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