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