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
|
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 64#ifdef COMPAT_43 65#include <machine/reg.h> 66#include <machine/psl.h> 67#endif 68#include <machine/limits.h> /* for UCHAR_MAX = typeof(p_priority)_MAX */ 69 70#include <vm/vm.h> 71#include <vm/vm_param.h> 72#include <sys/lock.h> 73#include <vm/pmap.h> 74#include <vm/vm_map.h> 75#include <vm/vm_zone.h> 76 77static MALLOC_DEFINE(M_ZOMBIE, "zombie", "zombie proc status"); 78 79static int wait1 __P((struct proc *, struct wait_args *, int)); 80 81/* 82 * callout list for things to do at exit time 83 */ 84typedef struct exit_list_element { 85 struct exit_list_element *next; 86 exitlist_fn function; 87} *ele_p; 88 89static ele_p exit_list; 90 91/* 92 * exit -- 93 * Death of process. 94 */ 95void 96exit(p, uap) 97 struct proc *p; 98 struct rexit_args /* { 99 int rval; 100 } */ *uap; 101{ 102 103 exit1(p, W_EXITCODE(uap->rval, 0)); 104 /* NOTREACHED */ 105} 106 107/* 108 * Exit: deallocate address space and other resources, change proc state 109 * to zombie, and unlink proc from allproc and parent's lists. Save exit 110 * status and rusage for wait(). Check for child processes and orphan them. 111 */ 112void 113exit1(p, rv) 114 register struct proc *p; 115 int rv; 116{ 117 register struct proc *q, *nq; 118 register struct vmspace *vm; 119 ele_p ep = exit_list; 120 121 if (p->p_pid == 1) { 122 printf("init died (signal %d, exit %d)\n", 123 WTERMSIG(rv), WEXITSTATUS(rv)); 124 panic("Going nowhere without my init!"); 125 } 126 127 aio_proc_rundown(p); 128 129 /* are we a task leader? */ 130 if(p == p->p_leader) { 131 struct kill_args killArgs; 132 killArgs.signum = SIGKILL; 133 q = p->p_peers; 134 while(q) { 135 killArgs.pid = q->p_pid; 136 /* 137 * The interface for kill is better 138 * than the internal signal 139 */ 140 kill(p, &killArgs); 141 nq = q; 142 q = q->p_peers; 143 /* 144 * orphan the threads so we don't mess up 145 * when they call exit 146 */ 147 nq->p_peers = 0; 148 nq->p_leader = nq; 149 } 150 151 /* otherwise are we a peer? */ 152 } else if(p->p_peers) { 153 q = p->p_leader; 154 while(q->p_peers != p) 155 q = q->p_peers; 156 q->p_peers = p->p_peers; 157 } 158 159#ifdef PGINPROF 160 vmsizmon(); 161#endif 162 STOPEVENT(p, S_EXIT, rv); 163 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 if (timevalisset(&p->p_realtimer.it_value)) 187 untimeout(realitexpire, (caddr_t)p, p->p_ithandle); 188 189 /*
|
196 * Close open files and release open-file table. 197 * This may block! 198 */ 199 fdfree(p); 200 201 /* 202 * XXX Shutdown SYSV semaphores 203 */ 204 semexit(p); 205 206 /* The next two chunks should probably be moved to vmspace_exit. */ 207 vm = p->p_vmspace; 208 /* 209 * Release user portion of address space. 210 * This releases references to vnodes, 211 * which could cause I/O if the file has been unlinked. 212 * Need to do this early enough that we can still sleep. 213 * Can't free the entire vmspace as the kernel stack 214 * may be mapped within that space also. 215 */ 216 if (vm->vm_refcnt == 1) { 217 if (vm->vm_shm) 218 shmexit(p); 219 pmap_remove_pages(&vm->vm_pmap, VM_MIN_ADDRESS, 220 VM_MAXUSER_ADDRESS); 221 (void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS, 222 VM_MAXUSER_ADDRESS); 223 } 224 225 if (SESS_LEADER(p)) { 226 register struct session *sp = p->p_session; 227 228 if (sp->s_ttyvp) { 229 /* 230 * Controlling process. 231 * Signal foreground pgrp, 232 * drain controlling terminal 233 * and revoke access to controlling terminal. 234 */ 235 if (sp->s_ttyp && (sp->s_ttyp->t_session == sp)) { 236 if (sp->s_ttyp->t_pgrp) 237 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1); 238 (void) ttywait(sp->s_ttyp); 239 /* 240 * The tty could have been revoked 241 * if we blocked. 242 */ 243 if (sp->s_ttyvp) 244 VOP_REVOKE(sp->s_ttyvp, REVOKEALL); 245 } 246 if (sp->s_ttyvp) 247 vrele(sp->s_ttyvp); 248 sp->s_ttyvp = NULL; 249 /* 250 * s_ttyp is not zero'd; we use this to indicate 251 * that the session once had a controlling terminal. 252 * (for logging and informational purposes) 253 */ 254 } 255 sp->s_leader = NULL; 256 } 257 fixjobc(p, p->p_pgrp, 0); 258 (void)acct_process(p); 259#ifdef KTRACE 260 /* 261 * release trace file 262 */ 263 p->p_traceflag = 0; /* don't trace the vrele() */ 264 if (p->p_tracep) 265 vrele(p->p_tracep); 266#endif 267 /* 268 * Remove proc from allproc queue and pidhash chain. 269 * Place onto zombproc. Unlink from parent's child list. 270 */ 271 LIST_REMOVE(p, p_list); 272 LIST_INSERT_HEAD(&zombproc, p, p_list); 273 p->p_stat = SZOMB; 274 275 LIST_REMOVE(p, p_hash); 276 277 q = p->p_children.lh_first; 278 if (q) /* only need this if any child is S_ZOMB */ 279 wakeup((caddr_t) initproc); 280 for (; q != 0; q = nq) { 281 nq = q->p_sibling.le_next; 282 LIST_REMOVE(q, p_sibling); 283 LIST_INSERT_HEAD(&initproc->p_children, q, p_sibling); 284 q->p_pptr = initproc; 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 * Notify parent that we're gone. If parent has the P_NOCLDWAIT 306 * flag set, notify process 1 instead (and hope it will handle 307 * this situation). 308 */ 309 if (p->p_pptr->p_flag & P_NOCLDWAIT) { 310 struct proc *pp = p->p_pptr; 311 proc_reparent(p, initproc); 312 /* 313 * If this was the last child of our parent, notify 314 * parent, so in case he was wait(2)ing, he will 315 * continue. 316 */ 317 if (LIST_EMPTY(&pp->p_children)) 318 wakeup((caddr_t)pp); 319 } 320 321 psignal(p->p_pptr, SIGCHLD); 322 wakeup((caddr_t)p->p_pptr); 323#if defined(tahoe) 324 /* move this to cpu_exit */ 325 p->p_addr->u_pcb.pcb_savacc.faddr = (float *)NULL; 326#endif 327 /* 328 * Clear curproc after we've done all operations 329 * that could block, and before tearing down the rest 330 * of the process state that might be used from clock, etc. 331 * Also, can't clear curproc while we're still runnable, 332 * as we're not on a run queue (we are current, just not 333 * a proper proc any longer!). 334 * 335 * Other substructures are freed from wait(). 336 */ 337 curproc = NULL; 338 if (--p->p_limit->p_refcnt == 0) { 339 FREE(p->p_limit, M_SUBPROC); 340 p->p_limit = NULL; 341 } 342 343 /* 344 * Finally, call machine-dependent code to release the remaining 345 * resources including address space, the kernel stack and pcb. 346 * The address space is released by "vmspace_free(p->p_vmspace)"; 347 * This is machine-dependent, as we may have to change stacks 348 * or ensure that the current one isn't reallocated before we 349 * finish. cpu_exit will end with a call to cpu_switch(), finishing 350 * our execution (pun intended). 351 */ 352 cpu_exit(p); 353} 354 355#ifdef COMPAT_43 356#if defined(hp300) || defined(luna68k) 357#include <machine/frame.h> 358#define GETPS(rp) ((struct frame *)(rp))->f_sr 359#else 360#define GETPS(rp) (rp)[PS] 361#endif 362 363int 364owait(p, uap) 365 struct proc *p; 366 register struct owait_args /* { 367 int dummy; 368 } */ *uap; 369{ 370 struct wait_args w; 371 372#ifdef PSL_ALLCC 373 if ((GETPS(p->p_md.md_regs) & PSL_ALLCC) != PSL_ALLCC) { 374 w.options = 0; 375 w.rusage = NULL; 376 } else { 377 w.options = p->p_md.md_regs[R0]; 378 w.rusage = (struct rusage *)p->p_md.md_regs[R1]; 379 } 380#else 381 w.options = 0; 382 w.rusage = NULL; 383#endif 384 w.pid = WAIT_ANY; 385 w.status = NULL; 386 return (wait1(p, &w, 1)); 387} 388#endif /* COMPAT_43 */ 389 390int 391wait4(p, uap) 392 struct proc *p; 393 struct wait_args *uap; 394{ 395 396 return (wait1(p, uap, 0)); 397} 398 399static int 400wait1(q, uap, compat) 401 register struct proc *q; 402 register struct wait_args /* { 403 int pid; 404 int *status; 405 int options; 406 struct rusage *rusage; 407 } */ *uap; 408 int compat; 409{ 410 register int nfound; 411 register struct proc *p, *t; 412 int status, error; 413 414 if (uap->pid == 0) 415 uap->pid = -q->p_pgid; 416 if (uap->options &~ (WUNTRACED|WNOHANG)) 417 return (EINVAL); 418loop: 419 nfound = 0; 420 for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) { 421 if (uap->pid != WAIT_ANY && 422 p->p_pid != uap->pid && p->p_pgid != -uap->pid) 423 continue; 424 nfound++; 425 if (p->p_stat == SZOMB) { 426 /* charge childs scheduling cpu usage to parent */ 427 if (curproc->p_pid != 1) { 428 curproc->p_estcpu = min(curproc->p_estcpu + 429 p->p_estcpu, UCHAR_MAX); 430 } 431 432 q->p_retval[0] = p->p_pid; 433#ifdef COMPAT_43 434 if (compat) 435 q->p_retval[1] = p->p_xstat; 436 else 437#endif 438 if (uap->status) { 439 status = p->p_xstat; /* convert to int */ 440 if ((error = copyout((caddr_t)&status, 441 (caddr_t)uap->status, sizeof(status)))) 442 return (error); 443 } 444 if (uap->rusage && (error = copyout((caddr_t)p->p_ru, 445 (caddr_t)uap->rusage, sizeof (struct rusage)))) 446 return (error); 447 /* 448 * If we got the child via a ptrace 'attach', 449 * we need to give it back to the old parent. 450 */ 451 if (p->p_oppid && (t = pfind(p->p_oppid))) { 452 p->p_oppid = 0; 453 proc_reparent(p, t); 454 psignal(t, SIGCHLD); 455 wakeup((caddr_t)t); 456 return (0); 457 } 458 p->p_xstat = 0; 459 ruadd(&q->p_stats->p_cru, p->p_ru); 460 FREE(p->p_ru, M_ZOMBIE); 461 p->p_ru = NULL; 462 463 /* 464 * Decrement the count of procs running with this uid. 465 */ 466 (void)chgproccnt(p->p_cred->p_ruid, -1); 467 468 /* 469 * Release reference to text vnode 470 */ 471 if (p->p_textvp) 472 vrele(p->p_textvp); 473 474 /* 475 * Free up credentials. 476 */ 477 if (--p->p_cred->p_refcnt == 0) { 478 crfree(p->p_cred->pc_ucred); 479 FREE(p->p_cred, M_SUBPROC); 480 p->p_cred = NULL; 481 } 482 483 /* 484 * Finally finished with old proc entry. 485 * Unlink it from its process group and free it. 486 */ 487 leavepgrp(p); 488 LIST_REMOVE(p, p_list); /* off zombproc */ 489 LIST_REMOVE(p, p_sibling); 490 491 /* 492 * Give machine-dependent layer a chance 493 * to free anything that cpu_exit couldn't 494 * release while still running in process context. 495 */ 496 cpu_wait(p); 497 zfree(proc_zone, p); 498 nprocs--; 499 return (0); 500 } 501 if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 && 502 (p->p_flag & P_TRACED || uap->options & WUNTRACED)) { 503 p->p_flag |= P_WAITED; 504 q->p_retval[0] = p->p_pid; 505#ifdef COMPAT_43 506 if (compat) { 507 q->p_retval[1] = W_STOPCODE(p->p_xstat); 508 error = 0; 509 } else 510#endif 511 if (uap->status) { 512 status = W_STOPCODE(p->p_xstat); 513 error = copyout((caddr_t)&status, 514 (caddr_t)uap->status, sizeof(status)); 515 } else 516 error = 0; 517 return (error); 518 } 519 } 520 if (nfound == 0) 521 return (ECHILD); 522 if (uap->options & WNOHANG) { 523 q->p_retval[0] = 0; 524 return (0); 525 } 526 if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0))) 527 return (error); 528 goto loop; 529} 530 531/* 532 * make process 'parent' the new parent of process 'child'. 533 */ 534void 535proc_reparent(child, parent) 536 register struct proc *child; 537 register struct proc *parent; 538{ 539 540 if (child->p_pptr == parent) 541 return; 542 543 LIST_REMOVE(child, p_sibling); 544 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 545 child->p_pptr = parent; 546} 547 548/* 549 * The next two functions are to handle adding/deleting items on the 550 * exit callout list 551 * 552 * at_exit(): 553 * Take the arguments given and put them onto the exit callout list, 554 * However first make sure that it's not already there. 555 * returns 0 on success. 556 */ 557int 558at_exit(function) 559 exitlist_fn function; 560{ 561 ele_p ep; 562 563 /* Be noisy if the programmer has lost track of things */ 564 if (rm_at_exit(function)) 565 printf("exit callout entry already present\n"); 566 ep = malloc(sizeof(*ep), M_TEMP, M_NOWAIT); 567 if (ep == NULL) 568 return (ENOMEM); 569 ep->next = exit_list; 570 ep->function = function; 571 exit_list = ep; 572 return (0); 573} 574/* 575 * Scan the exit callout list for the given items and remove them. 576 * Returns the number of items removed. 577 * Logically this can only be 0 or 1. 578 */ 579int 580rm_at_exit(function) 581 exitlist_fn function; 582{ 583 ele_p *epp, ep; 584 int count; 585 586 count = 0; 587 epp = &exit_list; 588 ep = *epp; 589 while (ep) { 590 if (ep->function == function) { 591 *epp = ep->next; 592 free(ep, M_TEMP); 593 count++; 594 } else { 595 epp = &ep->next; 596 } 597 ep = *epp; 598 } 599 return (count); 600}
|