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