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.49 1997/06/16 00:29:30 dyson Exp $ 40 */ 41 42 #include "opt_ktrace.h" 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/sysproto.h> 47 #include <sys/sysent.h> 48 #include <sys/proc.h> 49 #include <sys/tty.h> 50 #include <sys/time.h> 51 #include <sys/resource.h> 52 #include <sys/kernel.h> 53 #include <sys/buf.h> 54 #include <sys/wait.h> 55 #include <sys/file.h> 56 #include <sys/vnode.h> 57 #include <sys/syslog.h> 58 #include <sys/malloc.h> 59 #include <sys/resourcevar.h> 60 #include <sys/signalvar.h> 61 #include <sys/ptrace.h> 62 #include <sys/acct.h> /* for acct_process() function prototype */ 63 #include <sys/filedesc.h> 64 #include <sys/shm.h> 65 #include <sys/sem.h> 66 67 #ifdef COMPAT_43 68 #include <machine/reg.h> 69 #include <machine/psl.h> 70 #endif 71 72 #include <vm/vm.h> 73 #include <vm/vm_param.h> 74 #include <vm/vm_prot.h> 75 #include <sys/lock.h> 76 #include <vm/pmap.h> 77 #include <vm/vm_map.h> 78 #include <vm/vm_kern.h> 79 80 static int wait1 __P((struct proc *, struct wait_args *, int [], int)); 81 82 /* 83 * callout list for things to do at exit time 84 */ 85 typedef struct exit_list_element { 86 struct exit_list_element *next; 87 exitlist_fn function; 88 } *ele_p; 89 90 static ele_p exit_list; 91 92 /* 93 * exit -- 94 * Death of process. 95 */ 96 void 97 exit(p, uap, retval) 98 struct proc *p; 99 struct rexit_args /* { 100 int rval; 101 } */ *uap; 102 int *retval; 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 ele_p ep = exit_list; 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 if(p == p->p_leader) { 133 struct kill_args killArgs; 134 killArgs.signum = SIGKILL; 135 q = p->p_peers; 136 while(q) { 137 killArgs.pid = q->p_pid; 138 /* 139 * The interface for kill is better 140 * than the internal signal 141 */ 142 kill(p, &killArgs, &rv); 143 nq = q; 144 q = q->p_peers; 145 /* 146 * orphan the threads so we don't mess up 147 * when they call exit 148 */ 149 nq->p_peers = 0; 150 nq->p_leader = nq; 151 } 152 153 /* otherwise are we a peer? */ 154 } else if(p->p_peers) { 155 q = p->p_leader; 156 while(q->p_peers != p) 157 q = q->p_peers; 158 q->p_peers = p->p_peers; 159 } 160 161 #ifdef PGINPROF 162 vmsizmon(); 163 #endif 164 /* 165 * Check if any LKMs need anything done at process exit. 166 * e.g. SYSV IPC stuff 167 * XXX what if one of these generates an error? 168 */ 169 while (ep) { 170 (*ep->function)(p); 171 ep = ep->next; 172 } 173 174 if (p->p_flag & P_PROFIL) 175 stopprofclock(p); 176 MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage), 177 M_ZOMBIE, M_WAITOK); 178 /* 179 * If parent is waiting for us to exit or exec, 180 * P_PPWAIT is set; we will wakeup the parent below. 181 */ 182 p->p_flag &= ~(P_TRACED | P_PPWAIT); 183 p->p_flag |= P_WEXIT; 184 p->p_sigignore = ~0; 185 p->p_siglist = 0; 186 untimeout(realitexpire, (caddr_t)p); 187 188 /* 189 * Close open files and release open-file table. 190 * This may block! 191 */ 192 fdfree(p); 193 194 /* 195 * XXX Shutdown SYSV semaphores 196 */ 197 semexit(p); 198 199 /* The next two chunks should probably be moved to vmspace_exit. */ 200 vm = p->p_vmspace; 201 /* 202 * Release user portion of address space. 203 * This releases references to vnodes, 204 * which could cause I/O if the file has been unlinked. 205 * Need to do this early enough that we can still sleep. 206 * Can't free the entire vmspace as the kernel stack 207 * may be mapped within that space also. 208 */ 209 if (vm->vm_refcnt == 1) { 210 if (vm->vm_shm) 211 shmexit(p); 212 pmap_remove_pages(&vm->vm_pmap, VM_MIN_ADDRESS, 213 VM_MAXUSER_ADDRESS); 214 (void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS, 215 VM_MAXUSER_ADDRESS); 216 } 217 218 if (SESS_LEADER(p)) { 219 register struct session *sp = p->p_session; 220 221 if (sp->s_ttyvp) { 222 /* 223 * Controlling process. 224 * Signal foreground pgrp, 225 * drain controlling terminal 226 * and revoke access to controlling terminal. 227 */ 228 if (sp->s_ttyp && (sp->s_ttyp->t_session == sp)) { 229 if (sp->s_ttyp->t_pgrp) 230 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1); 231 (void) ttywait(sp->s_ttyp); 232 /* 233 * The tty could have been revoked 234 * if we blocked. 235 */ 236 if (sp->s_ttyvp) 237 VOP_REVOKE(sp->s_ttyvp, REVOKEALL); 238 } 239 if (sp->s_ttyvp) 240 vrele(sp->s_ttyvp); 241 sp->s_ttyvp = NULL; 242 /* 243 * s_ttyp is not zero'd; we use this to indicate 244 * that the session once had a controlling terminal. 245 * (for logging and informational purposes) 246 */ 247 } 248 sp->s_leader = NULL; 249 } 250 fixjobc(p, p->p_pgrp, 0); 251 if (p->p_limit->p_refcnt > 1 && 252 (p->p_limit->p_lflags & PL_SHAREMOD) == 0) { 253 p->p_limit->p_refcnt--; 254 p->p_limit = limcopy(p->p_limit); 255 } 256 p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; 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 /* 285 * Traced processes are killed 286 * since their existence means someone is screwing up. 287 */ 288 if (q->p_flag & P_TRACED) { 289 q->p_flag &= ~P_TRACED; 290 psignal(q, SIGKILL); 291 } 292 } 293 294 /* 295 * Save exit status and final rusage info, adding in child rusage 296 * info and self times. 297 */ 298 p->p_xstat = rv; 299 *p->p_ru = p->p_stats->p_ru; 300 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL); 301 ruadd(p->p_ru, &p->p_stats->p_cru); 302 303 /* 304 * Notify parent that we're gone. 305 */ 306 psignal(p->p_pptr, SIGCHLD); 307 wakeup((caddr_t)p->p_pptr); 308 #if defined(tahoe) 309 /* move this to cpu_exit */ 310 p->p_addr->u_pcb.pcb_savacc.faddr = (float *)NULL; 311 #endif 312 /* 313 * Clear curproc after we've done all operations 314 * that could block, and before tearing down the rest 315 * of the process state that might be used from clock, etc. 316 * Also, can't clear curproc while we're still runnable, 317 * as we're not on a run queue (we are current, just not 318 * a proper proc any longer!). 319 * 320 * Other substructures are freed from wait(). 321 */ 322 curproc = NULL; 323 if (--p->p_limit->p_refcnt == 0) { 324 FREE(p->p_limit, M_SUBPROC); 325 p->p_limit = NULL; 326 } 327 328 /* 329 * Finally, call machine-dependent code to release the remaining 330 * resources including address space, the kernel stack and pcb. 331 * The address space is released by "vmspace_free(p->p_vmspace)"; 332 * This is machine-dependent, as we may have to change stacks 333 * or ensure that the current one isn't reallocated before we 334 * finish. cpu_exit will end with a call to cpu_switch(), finishing 335 * our execution (pun intended). 336 */ 337 cpu_exit(p); 338 } 339 340 #ifdef COMPAT_43 341 #if defined(hp300) || defined(luna68k) 342 #include <machine/frame.h> 343 #define GETPS(rp) ((struct frame *)(rp))->f_sr 344 #else 345 #define GETPS(rp) (rp)[PS] 346 #endif 347 348 int 349 owait(p, uap, retval) 350 struct proc *p; 351 register struct owait_args /* { 352 int dummy; 353 } */ *uap; 354 int *retval; 355 { 356 struct wait_args w; 357 358 #ifdef PSL_ALLCC 359 if ((GETPS(p->p_md.md_regs) & PSL_ALLCC) != PSL_ALLCC) { 360 w.options = 0; 361 w.rusage = NULL; 362 } else { 363 w.options = p->p_md.md_regs[R0]; 364 w.rusage = (struct rusage *)p->p_md.md_regs[R1]; 365 } 366 #else 367 w.options = 0; 368 w.rusage = NULL; 369 #endif 370 w.pid = WAIT_ANY; 371 w.status = NULL; 372 return (wait1(p, &w, retval, 1)); 373 } 374 #endif /* COMPAT_43 */ 375 376 int 377 wait4(p, uap, retval) 378 struct proc *p; 379 struct wait_args *uap; 380 int *retval; 381 { 382 383 return (wait1(p, uap, retval, 0)); 384 } 385 386 static int 387 wait1(q, uap, retval, compat) 388 register struct proc *q; 389 register struct wait_args /* { 390 int pid; 391 int *status; 392 int options; 393 struct rusage *rusage; 394 } */ *uap; 395 int retval[]; 396 int compat; 397 { 398 register int nfound; 399 register struct proc *p, *t; 400 int status, error; 401 402 if (uap->pid == 0) 403 uap->pid = -q->p_pgid; 404 #ifdef notyet 405 if (uap->options &~ (WUNTRACED|WNOHANG)) 406 return (EINVAL); 407 #endif 408 loop: 409 nfound = 0; 410 for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) { 411 if (uap->pid != WAIT_ANY && 412 p->p_pid != uap->pid && p->p_pgid != -uap->pid) 413 continue; 414 nfound++; 415 if (p->p_stat == SZOMB) { 416 /* charge childs scheduling cpu usage to parent */ 417 if (curproc->p_pid != 1) { 418 curproc->p_estcpu = min(curproc->p_estcpu + 419 p->p_estcpu, UCHAR_MAX); 420 } 421 422 retval[0] = p->p_pid; 423 #ifdef COMPAT_43 424 if (compat) 425 retval[1] = p->p_xstat; 426 else 427 #endif 428 if (uap->status) { 429 status = p->p_xstat; /* convert to int */ 430 if ((error = copyout((caddr_t)&status, 431 (caddr_t)uap->status, sizeof(status)))) 432 return (error); 433 } 434 if (uap->rusage && (error = copyout((caddr_t)p->p_ru, 435 (caddr_t)uap->rusage, sizeof (struct rusage)))) 436 return (error); 437 /* 438 * If we got the child via a ptrace 'attach', 439 * we need to give it back to the old parent. 440 */ 441 if (p->p_oppid && (t = pfind(p->p_oppid))) { 442 p->p_oppid = 0; 443 proc_reparent(p, t); 444 psignal(t, SIGCHLD); 445 wakeup((caddr_t)t); 446 return (0); 447 } 448 p->p_xstat = 0; 449 ruadd(&q->p_stats->p_cru, p->p_ru); 450 FREE(p->p_ru, M_ZOMBIE); 451 p->p_ru = NULL; 452 453 /* 454 * Decrement the count of procs running with this uid. 455 */ 456 (void)chgproccnt(p->p_cred->p_ruid, -1); 457 458 /* 459 * Release reference to text vnode 460 */ 461 if (p->p_textvp) 462 vrele(p->p_textvp); 463 464 /* 465 * Free up credentials. 466 */ 467 if (--p->p_cred->p_refcnt == 0) { 468 crfree(p->p_cred->pc_ucred); 469 FREE(p->p_cred, M_SUBPROC); 470 p->p_cred = NULL; 471 } 472 473 /* 474 * Finally finished with old proc entry. 475 * Unlink it from its process group and free it. 476 */ 477 leavepgrp(p); 478 LIST_REMOVE(p, p_list); /* off zombproc */ 479 LIST_REMOVE(p, p_sibling); 480 481 /* 482 * Give machine-dependent layer a chance 483 * to free anything that cpu_exit couldn't 484 * release while still running in process context. 485 */ 486 cpu_wait(p); 487 FREE(p, M_PROC); 488 nprocs--; 489 return (0); 490 } 491 if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 && 492 (p->p_flag & P_TRACED || uap->options & WUNTRACED)) { 493 p->p_flag |= P_WAITED; 494 retval[0] = p->p_pid; 495 #ifdef COMPAT_43 496 if (compat) { 497 retval[1] = W_STOPCODE(p->p_xstat); 498 error = 0; 499 } else 500 #endif 501 if (uap->status) { 502 status = W_STOPCODE(p->p_xstat); 503 error = copyout((caddr_t)&status, 504 (caddr_t)uap->status, sizeof(status)); 505 } else 506 error = 0; 507 return (error); 508 } 509 } 510 if (nfound == 0) 511 return (ECHILD); 512 if (uap->options & WNOHANG) { 513 retval[0] = 0; 514 return (0); 515 } 516 if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0))) 517 return (error); 518 goto loop; 519 } 520 521 /* 522 * make process 'parent' the new parent of process 'child'. 523 */ 524 void 525 proc_reparent(child, parent) 526 register struct proc *child; 527 register struct proc *parent; 528 { 529 530 if (child->p_pptr == parent) 531 return; 532 533 LIST_REMOVE(child, p_sibling); 534 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 535 child->p_pptr = parent; 536 } 537 538 /* 539 * The next two functions are to handle adding/deleting items on the 540 * exit callout list 541 * 542 * at_exit(): 543 * Take the arguments given and put them onto the exit callout list, 544 * However first make sure that it's not already there. 545 * returns 0 on success. 546 */ 547 int 548 at_exit(exitlist_fn function) 549 { 550 ele_p ep; 551 552 /* Be noisy if the programmer has lost track of things */ 553 if (rm_at_exit(function)) 554 printf("exit callout entry already present\n"); 555 ep = malloc(sizeof(*ep), M_TEMP, M_NOWAIT); 556 if (ep == NULL) 557 return (ENOMEM); 558 ep->next = exit_list; 559 ep->function = function; 560 exit_list = ep; 561 return (0); 562 } 563 /* 564 * Scan the exit callout list for the given items and remove them. 565 * Returns the number of items removed. 566 * Logically this can only be 0 or 1. 567 */ 568 int 569 rm_at_exit(exitlist_fn function) 570 { 571 ele_p *epp, ep; 572 int count; 573 574 count = 0; 575 epp = &exit_list; 576 ep = *epp; 577 while (ep) { 578 if (ep->function == function) { 579 *epp = ep->next; 580 free(ep, M_TEMP); 581 count++; 582 } else { 583 epp = &ep->next; 584 } 585 ep = *epp; 586 } 587 return (count); 588 } 589 590 591