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 * $FreeBSD$ 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/lock.h> 51 #include <sys/mutex.h> 52 #include <sys/proc.h> 53 #include <sys/pioctl.h> 54 #include <sys/tty.h> 55 #include <sys/wait.h> 56 #include <sys/vnode.h> 57 #include <sys/vmmeter.h> 58 #include <sys/resourcevar.h> 59 #include <sys/signalvar.h> 60 #include <sys/sx.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 #include <sys/jail.h> 67 68 #include <vm/vm.h> 69 #include <vm/vm_param.h> 70 #include <vm/vm_extern.h> 71 #include <vm/pmap.h> 72 #include <vm/vm_map.h> 73 #include <vm/vm_zone.h> 74 #include <sys/user.h> 75 76 /* Required to be non-static for SysVR4 emulator */ 77 MALLOC_DEFINE(M_ZOMBIE, "zombie", "zombie proc status"); 78 79 static MALLOC_DEFINE(M_ATEXIT, "atexit", "atexit callback"); 80 81 static int wait1 __P((struct thread *, struct wait_args *, int)); 82 83 /* 84 * callout list for things to do at exit time 85 */ 86 struct exitlist { 87 exitlist_fn function; 88 TAILQ_ENTRY(exitlist) next; 89 }; 90 91 TAILQ_HEAD(exit_list_head, exitlist); 92 static struct exit_list_head exit_list = TAILQ_HEAD_INITIALIZER(exit_list); 93 94 /* 95 * exit -- 96 * Death of process. 97 * 98 * MPSAFE 99 */ 100 void 101 sys_exit(td, uap) 102 struct thread *td; 103 struct sys_exit_args /* { 104 int rval; 105 } */ *uap; 106 { 107 108 mtx_lock(&Giant); 109 exit1(td, W_EXITCODE(uap->rval, 0)); 110 /* NOTREACHED */ 111 } 112 113 /* 114 * Exit: deallocate address space and other resources, change proc state 115 * to zombie, and unlink proc from allproc and parent's lists. Save exit 116 * status and rusage for wait(). Check for child processes and orphan them. 117 */ 118 void 119 exit1(td, rv) 120 register struct thread *td; 121 int rv; 122 { 123 struct proc *p = td->td_proc; 124 register struct proc *q, *nq; 125 register struct vmspace *vm; 126 struct vnode *vtmp; 127 struct exitlist *ep; 128 129 GIANT_REQUIRED; 130 131 if (p->p_pid == 1) { 132 printf("init died (signal %d, exit %d)\n", 133 WTERMSIG(rv), WEXITSTATUS(rv)); 134 panic("Going nowhere without my init!"); 135 } 136 137 /* XXXXKSE */ 138 /* MUST abort all other threads before proceeding past this point */ 139 140 /* are we a task leader? */ 141 PROC_LOCK(p); 142 if(p == p->p_leader) { 143 q = p->p_peers; 144 while (q != NULL) { 145 PROC_LOCK(q); 146 psignal(q, SIGKILL); 147 PROC_UNLOCK(q); 148 q = q->p_peers; 149 } 150 while (p->p_peers) 151 msleep((caddr_t)p, &p->p_mtx, PWAIT, "exit1", 0); 152 } 153 PROC_UNLOCK(p); 154 155 #ifdef PGINPROF 156 vmsizmon(); 157 #endif 158 STOPEVENT(p, S_EXIT, rv); 159 wakeup(&p->p_stype); /* Wakeup anyone in procfs' PIOCWAIT */ 160 161 /* 162 * Check if any loadable modules need anything done at process exit. 163 * e.g. SYSV IPC stuff 164 * XXX what if one of these generates an error? 165 */ 166 TAILQ_FOREACH(ep, &exit_list, next) 167 (*ep->function)(p); 168 169 stopprofclock(p); 170 171 MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage), 172 M_ZOMBIE, M_WAITOK); 173 /* 174 * If parent is waiting for us to exit or exec, 175 * P_PPWAIT is set; we will wakeup the parent below. 176 */ 177 PROC_LOCK(p); 178 p->p_flag &= ~(P_TRACED | P_PPWAIT); 179 p->p_flag |= P_WEXIT; 180 SIGEMPTYSET(p->p_siglist); 181 PROC_UNLOCK(p); 182 if (timevalisset(&p->p_realtimer.it_value)) 183 callout_stop(&p->p_itcallout); 184 185 /* 186 * Reset any sigio structures pointing to us as a result of 187 * F_SETOWN with our pid. 188 */ 189 funsetownlst(&p->p_sigiolst); 190 191 /* 192 * Close open files and release open-file table. 193 * This may block! 194 */ 195 fdfree(td); /* XXXKSE *//* may not be the one in proc */ 196 197 /* 198 * Remove ourself from our leader's peer list and wake our leader. 199 */ 200 PROC_LOCK(p->p_leader); 201 if(p->p_leader->p_peers) { 202 q = p->p_leader; 203 while(q->p_peers != p) 204 q = q->p_peers; 205 q->p_peers = p->p_peers; 206 wakeup((caddr_t)p->p_leader); 207 } 208 PROC_UNLOCK(p->p_leader); 209 210 /* The next two chunks should probably be moved to vmspace_exit. */ 211 vm = p->p_vmspace; 212 /* 213 * Release user portion of address space. 214 * This releases references to vnodes, 215 * which could cause I/O if the file has been unlinked. 216 * Need to do this early enough that we can still sleep. 217 * Can't free the entire vmspace as the kernel stack 218 * may be mapped within that space also. 219 */ 220 if (vm->vm_refcnt == 1) { 221 if (vm->vm_shm) 222 shmexit(p); 223 pmap_remove_pages(vmspace_pmap(vm), VM_MIN_ADDRESS, 224 VM_MAXUSER_ADDRESS); 225 (void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS, 226 VM_MAXUSER_ADDRESS); 227 } 228 229 PROC_LOCK(p); 230 if (SESS_LEADER(p)) { 231 register struct session *sp = p->p_session; 232 233 PROC_UNLOCK(p); 234 if (sp->s_ttyvp) { 235 /* 236 * Controlling process. 237 * Signal foreground pgrp, 238 * drain controlling terminal 239 * and revoke access to controlling terminal. 240 */ 241 if (sp->s_ttyp && (sp->s_ttyp->t_session == sp)) { 242 if (sp->s_ttyp->t_pgrp) 243 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1); 244 (void) ttywait(sp->s_ttyp); 245 /* 246 * The tty could have been revoked 247 * if we blocked. 248 */ 249 if (sp->s_ttyvp) 250 VOP_REVOKE(sp->s_ttyvp, REVOKEALL); 251 } 252 if (sp->s_ttyvp) 253 vrele(sp->s_ttyvp); 254 sp->s_ttyvp = NULL; 255 /* 256 * s_ttyp is not zero'd; we use this to indicate 257 * that the session once had a controlling terminal. 258 * (for logging and informational purposes) 259 */ 260 } 261 sp->s_leader = NULL; 262 } else 263 PROC_UNLOCK(p); 264 fixjobc(p, p->p_pgrp, 0); 265 (void)acct_process(td); 266 #ifdef KTRACE 267 /* 268 * release trace file 269 */ 270 p->p_traceflag = 0; /* don't trace the vrele() */ 271 if ((vtmp = p->p_tracep) != NULL) { 272 p->p_tracep = NULL; 273 vrele(vtmp); 274 } 275 #endif 276 /* 277 * Release reference to text vnode 278 */ 279 if ((vtmp = p->p_textvp) != NULL) { 280 p->p_textvp = NULL; 281 vrele(vtmp); 282 } 283 284 /* 285 * Remove proc from allproc queue and pidhash chain. 286 * Place onto zombproc. Unlink from parent's child list. 287 */ 288 sx_xlock(&allproc_lock); 289 LIST_REMOVE(p, p_list); 290 LIST_INSERT_HEAD(&zombproc, p, p_list); 291 LIST_REMOVE(p, p_hash); 292 sx_xunlock(&allproc_lock); 293 294 sx_xlock(&proctree_lock); 295 q = LIST_FIRST(&p->p_children); 296 if (q != NULL) /* only need this if any child is S_ZOMB */ 297 wakeup((caddr_t) initproc); 298 for (; q != NULL; q = nq) { 299 nq = LIST_NEXT(q, p_sibling); 300 PROC_LOCK(q); 301 proc_reparent(q, initproc); 302 q->p_sigparent = SIGCHLD; 303 /* 304 * Traced processes are killed 305 * since their existence means someone is screwing up. 306 */ 307 if (q->p_flag & P_TRACED) { 308 q->p_flag &= ~P_TRACED; 309 psignal(q, SIGKILL); 310 } 311 PROC_UNLOCK(q); 312 } 313 314 /* 315 * Save exit status and final rusage info, adding in child rusage 316 * info and self times. 317 */ 318 p->p_xstat = rv; 319 *p->p_ru = p->p_stats->p_ru; 320 mtx_lock_spin(&sched_lock); 321 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL); 322 mtx_unlock_spin(&sched_lock); 323 ruadd(p->p_ru, &p->p_stats->p_cru); 324 325 /* 326 * Pretend that an mi_switch() to the next process occurs now. We 327 * must set `switchtime' directly since we will call cpu_switch() 328 * directly. Set it now so that the rest of the exit time gets 329 * counted somewhere if possible. 330 */ 331 mtx_lock_spin(&sched_lock); 332 microuptime(PCPU_PTR(switchtime)); 333 PCPU_SET(switchticks, ticks); 334 mtx_unlock_spin(&sched_lock); 335 336 /* 337 * notify interested parties of our demise. 338 */ 339 PROC_LOCK(p); 340 KNOTE(&p->p_klist, NOTE_EXIT); 341 342 /* 343 * Notify parent that we're gone. If parent has the PS_NOCLDWAIT 344 * flag set, notify process 1 instead (and hope it will handle 345 * this situation). 346 */ 347 if (p->p_pptr->p_procsig->ps_flag & PS_NOCLDWAIT) { 348 struct proc *pp = p->p_pptr; 349 proc_reparent(p, initproc); 350 /* 351 * If this was the last child of our parent, notify 352 * parent, so in case he was wait(2)ing, he will 353 * continue. 354 */ 355 if (LIST_EMPTY(&pp->p_children)) 356 wakeup((caddr_t)pp); 357 } 358 359 PROC_LOCK(p->p_pptr); 360 if (p->p_sigparent && p->p_pptr != initproc) 361 psignal(p->p_pptr, p->p_sigparent); 362 else 363 psignal(p->p_pptr, SIGCHLD); 364 PROC_UNLOCK(p->p_pptr); 365 366 /* 367 * If this is a kthread, then wakeup anyone waiting for it to exit. 368 */ 369 if (p->p_flag & P_KTHREAD) 370 wakeup((caddr_t)p); 371 PROC_UNLOCK(p); 372 sx_xunlock(&proctree_lock); 373 374 /* 375 * Clear curproc after we've done all operations 376 * that could block, and before tearing down the rest 377 * of the process state that might be used from clock, etc. 378 * Also, can't clear curproc while we're still runnable, 379 * as we're not on a run queue (we are current, just not 380 * a proper proc any longer!). 381 * 382 * Other substructures are freed from wait(). 383 */ 384 mtx_assert(&Giant, MA_OWNED); 385 if (--p->p_limit->p_refcnt == 0) { 386 FREE(p->p_limit, M_SUBPROC); 387 p->p_limit = NULL; 388 } 389 390 /* 391 * Release this thread's reference to the ucred. The actual proc 392 * reference will stay around until the proc is harvested by 393 * wait(). At this point the ucred is immutable (no other threads 394 * from this proc are around that can change it) so we leave the 395 * per-thread ucred pointer intact in case it is needed although 396 * in theory nothing should be using it at this point. 397 */ 398 crfree(td->td_ucred); 399 400 /* 401 * Finally, call machine-dependent code to release the remaining 402 * resources including address space, the kernel stack and pcb. 403 * The address space is released by "vmspace_free(p->p_vmspace)" 404 * in vm_waitproc(); 405 */ 406 cpu_exit(td); 407 408 PROC_LOCK(p); 409 mtx_lock_spin(&sched_lock); 410 while (mtx_owned(&Giant)) 411 mtx_unlock(&Giant); 412 413 /* 414 * We have to wait until after releasing all locks before 415 * changing p_stat. If we block on a mutex then we will be 416 * back at SRUN when we resume and our parent will never 417 * harvest us. 418 */ 419 p->p_stat = SZOMB; 420 421 wakeup(p->p_pptr); 422 PROC_UNLOCK(p); 423 424 cnt.v_swtch++; 425 cpu_throw(); 426 panic("exit1"); 427 } 428 429 #ifdef COMPAT_43 430 /* 431 * MPSAFE, the dirty work is handled by wait1(). 432 */ 433 int 434 owait(td, uap) 435 struct thread *td; 436 register struct owait_args /* { 437 int dummy; 438 } */ *uap; 439 { 440 struct wait_args w; 441 442 w.options = 0; 443 w.rusage = NULL; 444 w.pid = WAIT_ANY; 445 w.status = NULL; 446 return (wait1(td, &w, 1)); 447 } 448 #endif /* COMPAT_43 */ 449 450 /* 451 * MPSAFE, the dirty work is handled by wait1(). 452 */ 453 int 454 wait4(td, uap) 455 struct thread *td; 456 struct wait_args *uap; 457 { 458 459 return (wait1(td, uap, 0)); 460 } 461 462 /* 463 * MPSAFE 464 */ 465 static int 466 wait1(td, uap, compat) 467 register struct thread *td; 468 register struct wait_args /* { 469 int pid; 470 int *status; 471 int options; 472 struct rusage *rusage; 473 } */ *uap; 474 int compat; 475 { 476 register int nfound; 477 register struct proc *q, *p, *t; 478 int status, error; 479 480 mtx_lock(&Giant); 481 q = td->td_proc; 482 if (uap->pid == 0) 483 uap->pid = -q->p_pgid; 484 if (uap->options &~ (WUNTRACED|WNOHANG|WLINUXCLONE)) { 485 error = EINVAL; 486 goto done2; 487 } 488 loop: 489 nfound = 0; 490 sx_slock(&proctree_lock); 491 LIST_FOREACH(p, &q->p_children, p_sibling) { 492 if (uap->pid != WAIT_ANY && 493 p->p_pid != uap->pid && p->p_pgid != -uap->pid) 494 continue; 495 496 /* 497 * This special case handles a kthread spawned by linux_clone 498 * (see linux_misc.c). The linux_wait4 and linux_waitpid 499 * functions need to be able to distinguish between waiting 500 * on a process and waiting on a thread. It is a thread if 501 * p_sigparent is not SIGCHLD, and the WLINUXCLONE option 502 * signifies we want to wait for threads and not processes. 503 */ 504 PROC_LOCK(p); 505 if ((p->p_sigparent != SIGCHLD) ^ 506 ((uap->options & WLINUXCLONE) != 0)) { 507 PROC_UNLOCK(p); 508 continue; 509 } 510 511 nfound++; 512 mtx_lock_spin(&sched_lock); 513 if (p->p_stat == SZOMB) { 514 /* 515 * charge childs scheduling cpu usage to parent 516 * XXXKSE assume only one thread & kse & ksegrp 517 * keep estcpu in each ksegrp 518 * so charge it to the ksegrp that did the wait 519 * since process estcpu is sum of all ksegrps, 520 * this is strictly as expected. 521 * Assume that the child process aggregated all 522 * tke estcpu into the 'build-in' ksegrp. 523 * XXXKSE 524 */ 525 if (curthread->td_proc->p_pid != 1) { 526 curthread->td_ksegrp->kg_estcpu = 527 ESTCPULIM(curthread->td_ksegrp->kg_estcpu + 528 p->p_ksegrp.kg_estcpu); 529 } 530 531 mtx_unlock_spin(&sched_lock); 532 PROC_UNLOCK(p); 533 sx_sunlock(&proctree_lock); 534 535 td->td_retval[0] = p->p_pid; 536 #ifdef COMPAT_43 537 if (compat) 538 td->td_retval[1] = p->p_xstat; 539 else 540 #endif 541 if (uap->status) { 542 status = p->p_xstat; /* convert to int */ 543 if ((error = copyout((caddr_t)&status, 544 (caddr_t)uap->status, sizeof(status)))) { 545 goto done2; 546 } 547 } 548 if (uap->rusage && (error = copyout((caddr_t)p->p_ru, 549 (caddr_t)uap->rusage, sizeof (struct rusage)))) { 550 goto done2; 551 } 552 /* 553 * If we got the child via a ptrace 'attach', 554 * we need to give it back to the old parent. 555 */ 556 sx_xlock(&proctree_lock); 557 if (p->p_oppid) { 558 if ((t = pfind(p->p_oppid)) != NULL) { 559 PROC_LOCK(p); 560 p->p_oppid = 0; 561 proc_reparent(p, t); 562 PROC_UNLOCK(p); 563 psignal(t, SIGCHLD); 564 wakeup((caddr_t)t); 565 PROC_UNLOCK(t); 566 sx_xunlock(&proctree_lock); 567 error = 0; 568 goto done2; 569 } 570 } 571 sx_xunlock(&proctree_lock); 572 PROC_LOCK(p); 573 p->p_xstat = 0; 574 PROC_UNLOCK(p); 575 ruadd(&q->p_stats->p_cru, p->p_ru); 576 FREE(p->p_ru, M_ZOMBIE); 577 p->p_ru = NULL; 578 579 /* 580 * Decrement the count of procs running with this uid. 581 */ 582 (void)chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0); 583 584 /* 585 * Finally finished with old proc entry. 586 * Unlink it from its process group and free it. 587 */ 588 leavepgrp(p); 589 590 sx_xlock(&allproc_lock); 591 LIST_REMOVE(p, p_list); /* off zombproc */ 592 sx_xunlock(&allproc_lock); 593 594 sx_xlock(&proctree_lock); 595 LIST_REMOVE(p, p_sibling); 596 sx_xunlock(&proctree_lock); 597 598 /* 599 * Free up credentials. 600 */ 601 crfree(p->p_ucred); 602 p->p_ucred = NULL; 603 604 /* 605 * Remove unused arguments 606 */ 607 if (p->p_args && --p->p_args->ar_ref == 0) 608 FREE(p->p_args, M_PARGS); 609 610 if (--p->p_procsig->ps_refcnt == 0) { 611 if (p->p_sigacts != &p->p_uarea->u_sigacts) 612 FREE(p->p_sigacts, M_SUBPROC); 613 FREE(p->p_procsig, M_SUBPROC); 614 p->p_procsig = NULL; 615 } 616 617 /* 618 * Give vm and machine-dependent layer a chance 619 * to free anything that cpu_exit couldn't 620 * release while still running in process context. 621 */ 622 vm_waitproc(p); 623 mtx_destroy(&p->p_mtx); 624 zfree(proc_zone, p); 625 nprocs--; 626 error = 0; 627 goto done2; 628 } 629 if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 && 630 (p->p_flag & P_TRACED || uap->options & WUNTRACED)) { 631 mtx_unlock_spin(&sched_lock); 632 p->p_flag |= P_WAITED; 633 PROC_UNLOCK(p); 634 sx_sunlock(&proctree_lock); 635 td->td_retval[0] = p->p_pid; 636 #ifdef COMPAT_43 637 if (compat) { 638 td->td_retval[1] = W_STOPCODE(p->p_xstat); 639 error = 0; 640 } else 641 #endif 642 if (uap->status) { 643 status = W_STOPCODE(p->p_xstat); 644 error = copyout((caddr_t)&status, 645 (caddr_t)uap->status, sizeof(status)); 646 } else 647 error = 0; 648 goto done2; 649 } 650 mtx_unlock_spin(&sched_lock); 651 PROC_UNLOCK(p); 652 } 653 sx_sunlock(&proctree_lock); 654 if (nfound == 0) { 655 error = ECHILD; 656 goto done2; 657 } 658 if (uap->options & WNOHANG) { 659 td->td_retval[0] = 0; 660 error = 0; 661 goto done2; 662 } 663 if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)) != 0) 664 goto done2; 665 goto loop; 666 done2: 667 mtx_unlock(&Giant); 668 return(error); 669 } 670 671 /* 672 * Make process 'parent' the new parent of process 'child'. 673 * Must be called with an exclusive hold of proctree lock. 674 */ 675 void 676 proc_reparent(child, parent) 677 register struct proc *child; 678 register struct proc *parent; 679 { 680 681 sx_assert(&proctree_lock, SX_XLOCKED); 682 PROC_LOCK_ASSERT(child, MA_OWNED); 683 if (child->p_pptr == parent) 684 return; 685 686 LIST_REMOVE(child, p_sibling); 687 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 688 child->p_pptr = parent; 689 } 690 691 /* 692 * The next two functions are to handle adding/deleting items on the 693 * exit callout list 694 * 695 * at_exit(): 696 * Take the arguments given and put them onto the exit callout list, 697 * However first make sure that it's not already there. 698 * returns 0 on success. 699 */ 700 701 int 702 at_exit(function) 703 exitlist_fn function; 704 { 705 struct exitlist *ep; 706 707 #ifdef INVARIANTS 708 /* Be noisy if the programmer has lost track of things */ 709 if (rm_at_exit(function)) 710 printf("WARNING: exit callout entry (%p) already present\n", 711 function); 712 #endif 713 ep = malloc(sizeof(*ep), M_ATEXIT, M_NOWAIT); 714 if (ep == NULL) 715 return (ENOMEM); 716 ep->function = function; 717 TAILQ_INSERT_TAIL(&exit_list, ep, next); 718 return (0); 719 } 720 721 /* 722 * Scan the exit callout list for the given item and remove it. 723 * Returns the number of items removed (0 or 1) 724 */ 725 int 726 rm_at_exit(function) 727 exitlist_fn function; 728 { 729 struct exitlist *ep; 730 731 TAILQ_FOREACH(ep, &exit_list, next) { 732 if (ep->function == function) { 733 TAILQ_REMOVE(&exit_list, ep, next); 734 free(ep, M_ATEXIT); 735 return(1); 736 } 737 } 738 return (0); 739 } 740