Home | History | Annotate | Line # | Download | only in stdlib
atexit.c revision 1.14
      1  1.14  thorpej /*	$NetBSD: atexit.c,v 1.14 2003/03/01 04:19:37 thorpej Exp $	*/
      2   1.6  thorpej 
      3   1.1      cgd /*-
      4  1.14  thorpej  * Copyright (c) 2003 The NetBSD Foundation, Inc.
      5  1.14  thorpej  * All rights reserved.
      6   1.1      cgd  *
      7  1.14  thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.14  thorpej  * by Jason R. Thorpe.
      9   1.1      cgd  *
     10   1.1      cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1      cgd  * modification, are permitted provided that the following conditions
     12   1.1      cgd  * are met:
     13   1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.1      cgd  *    documentation and/or other materials provided with the distribution.
     18   1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     19   1.1      cgd  *    must display the following acknowledgement:
     20  1.14  thorpej  *        This product includes software developed by the NetBSD
     21  1.14  thorpej  *        Foundation, Inc. and its contributors.
     22  1.14  thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.14  thorpej  *    contributors may be used to endorse or promote products derived
     24  1.14  thorpej  *    from this software without specific prior written permission.
     25   1.1      cgd  *
     26  1.14  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.14  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.14  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.14  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.14  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.14  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.14  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.14  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.14  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.14  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.14  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37  1.14  thorpej  */
     38  1.14  thorpej 
     39  1.14  thorpej #include "reentrant.h"
     40   1.1      cgd 
     41  1.11    lukem #include <assert.h>
     42   1.1      cgd #include <stdlib.h>
     43  1.14  thorpej 
     44   1.1      cgd #include "atexit.h"
     45   1.5      jtc 
     46  1.14  thorpej struct atexit_handler {
     47  1.14  thorpej 	struct atexit_handler *ah_next;
     48  1.14  thorpej 	union {
     49  1.14  thorpej 		void (*fun_atexit)(void);
     50  1.14  thorpej 		void (*fun_cxa_atexit)(void *);
     51  1.14  thorpej 	} ah_fun;
     52  1.14  thorpej #define	ah_atexit	ah_fun.fun_atexit
     53  1.14  thorpej #define	ah_cxa_atexit	ah_fun.fun_cxa_atexit
     54  1.14  thorpej 
     55  1.14  thorpej 	void *ah_arg;	/* argument for cxa_atexit handlers */
     56  1.14  thorpej 	void *ah_dso;	/* home DSO for cxa_atexit handlers */
     57  1.14  thorpej };
     58  1.14  thorpej 
     59  1.14  thorpej /*
     60  1.14  thorpej  * There must be at least 32 to guarantee ANSI conformance, plus
     61  1.14  thorpej  * 3 additional ones for the benefit of the startup code, which
     62  1.14  thorpej  * may use them to register the dynamic loader's cleanup routine,
     63  1.14  thorpej  * the profiling cleanup routine, and the global destructor routine.
     64  1.14  thorpej  */
     65  1.14  thorpej #define	NSTATIC_HANDLERS	(32 + 3)
     66  1.14  thorpej static struct atexit_handler atexit_handler0[NSTATIC_HANDLERS];
     67  1.14  thorpej 
     68  1.14  thorpej #define	STATIC_HANDLER_P(ah)						\
     69  1.14  thorpej 	(ah >= &atexit_handler0[0] && ah < &atexit_handler0[NSTATIC_HANDLERS])
     70  1.14  thorpej 
     71  1.14  thorpej /*
     72  1.14  thorpej  * Stack of atexit handlers.  Handlers must be called in the opposite
     73  1.14  thorpej  * order they were registered.
     74  1.14  thorpej  */
     75  1.14  thorpej static struct atexit_handler *atexit_handler_stack;
     76   1.1      cgd 
     77  1.13  thorpej #ifdef _REENTRANT
     78  1.14  thorpej /* ..and a mutex to protect it all. */
     79  1.14  thorpej static mutex_t atexit_mutex = MUTEX_INITIALIZER;
     80  1.14  thorpej #endif /* _REENTRANT */
     81  1.10   kleink 
     82   1.1      cgd /*
     83  1.14  thorpej  * Allocate an atexit handler descriptor.  If "dso" is NULL, it indicates
     84  1.14  thorpej  * a normal atexit handler, which must be allocated from the static pool,
     85  1.14  thorpej  * if possible. cxa_atexit handlers are never allocated from the static
     86  1.14  thorpej  * pool.
     87  1.14  thorpej  *
     88  1.14  thorpej  * atexit_mutex must be held.
     89  1.14  thorpej  */
     90  1.14  thorpej static struct atexit_handler *
     91  1.14  thorpej atexit_handler_alloc(void *dso)
     92  1.14  thorpej {
     93  1.14  thorpej 	struct atexit_handler *ah;
     94  1.14  thorpej 	int i;
     95  1.14  thorpej 
     96  1.14  thorpej 	if (dso == NULL) {
     97  1.14  thorpej 		for (i = 0; i < NSTATIC_HANDLERS; i++) {
     98  1.14  thorpej 			ah = &atexit_handler0[i];
     99  1.14  thorpej 			if (ah->ah_atexit == NULL) {
    100  1.14  thorpej 				/* Slot is free. */
    101  1.14  thorpej 				return (ah);
    102  1.14  thorpej 			}
    103  1.14  thorpej 		}
    104  1.14  thorpej 	}
    105  1.14  thorpej 
    106  1.14  thorpej 	/*
    107  1.14  thorpej 	 * Either no static slot was free, or this is a cxa_atexit
    108  1.14  thorpej 	 * handler.  Allocate a new one.  We keep the atexit_mutex
    109  1.14  thorpej 	 * held to prevent handlers from being run while we (potentially)
    110  1.14  thorpej 	 * block in malloc().
    111  1.14  thorpej 	 */
    112  1.14  thorpej 	ah = malloc(sizeof(*ah));
    113  1.14  thorpej 	return (ah);
    114  1.14  thorpej }
    115  1.14  thorpej 
    116  1.14  thorpej /*
    117  1.14  thorpej  * Register an atexit routine.  This is suitable either for a cxa_atexit
    118  1.14  thorpej  * or normal atexit type handler.  The __cxa_atexit() name and arguments
    119  1.14  thorpej  * are specified by the C++ ABI.  See:
    120  1.14  thorpej  *
    121  1.14  thorpej  *	http://www.codesourcery.com/cxx-abi/abi.html#dso-dtor
    122   1.1      cgd  */
    123   1.1      cgd int
    124  1.14  thorpej __cxa_atexit(void (*func)(void *), void *arg, void *dso)
    125   1.1      cgd {
    126  1.14  thorpej 	struct atexit_handler *ah;
    127  1.11    lukem 
    128  1.14  thorpej 	_DIAGASSERT(func != NULL);
    129   1.1      cgd 
    130  1.14  thorpej 	mutex_lock(&atexit_mutex);
    131  1.14  thorpej 
    132  1.14  thorpej 	ah = atexit_handler_alloc(dso);
    133  1.14  thorpej 	if (ah == NULL) {
    134  1.14  thorpej 		mutex_unlock(&atexit_mutex);
    135  1.14  thorpej 		return (-1);
    136   1.1      cgd 	}
    137  1.14  thorpej 
    138  1.14  thorpej 	ah->ah_cxa_atexit = func;
    139  1.14  thorpej 	ah->ah_arg = arg;
    140  1.14  thorpej 	ah->ah_dso = dso;
    141  1.14  thorpej 
    142  1.14  thorpej 	ah->ah_next = atexit_handler_stack;
    143  1.14  thorpej 	atexit_handler_stack = ah;
    144  1.14  thorpej 
    145  1.14  thorpej 	mutex_unlock(&atexit_mutex);
    146   1.1      cgd 	return (0);
    147  1.14  thorpej }
    148  1.14  thorpej 
    149  1.14  thorpej /*
    150  1.14  thorpej  * Run the list of atexit handlers.  If dso is NULL, run all of them,
    151  1.14  thorpej  * otherwise run only those matching the specified dso.
    152  1.14  thorpej  */
    153  1.14  thorpej void
    154  1.14  thorpej __cxa_finalize(void *dso)
    155  1.14  thorpej {
    156  1.14  thorpej 	struct atexit_handler *ah, *dead_handlers = NULL, **prevp;
    157  1.14  thorpej 
    158  1.14  thorpej 	mutex_lock(&atexit_mutex);
    159  1.14  thorpej 
    160  1.14  thorpej 	for (prevp = &atexit_handler_stack; (ah = (*prevp)) != NULL;) {
    161  1.14  thorpej 		if (dso == NULL || dso == ah->ah_dso) {
    162  1.14  thorpej 			if (ah->ah_dso != NULL)
    163  1.14  thorpej 				(*ah->ah_cxa_atexit)(ah->ah_arg);
    164  1.14  thorpej 			else
    165  1.14  thorpej 				(*ah->ah_atexit)();
    166  1.14  thorpej 
    167  1.14  thorpej 			*prevp = ah->ah_next;
    168  1.14  thorpej 			if (STATIC_HANDLER_P(ah))
    169  1.14  thorpej 				ah->ah_atexit = NULL;	/* mark it free */
    170  1.14  thorpej 			else {
    171  1.14  thorpej 				ah->ah_next = dead_handlers;
    172  1.14  thorpej 				dead_handlers = ah;
    173  1.14  thorpej 			}
    174  1.14  thorpej 		} else
    175  1.14  thorpej 			prevp = &ah->ah_next;
    176  1.14  thorpej 	}
    177  1.14  thorpej 
    178  1.14  thorpej 	mutex_unlock(&atexit_mutex);
    179  1.14  thorpej 
    180  1.14  thorpej 	/*
    181  1.14  thorpej 	 * Now free any dead handlers.  Do this even if we're about to
    182  1.14  thorpej 	 * exit, in case a leak-detecting malloc is being used.
    183  1.14  thorpej 	 */
    184  1.14  thorpej 	while ((ah = dead_handlers) != NULL) {
    185  1.14  thorpej 		dead_handlers = ah->ah_next;
    186  1.14  thorpej 		free(ah);
    187  1.14  thorpej 	}
    188  1.14  thorpej }
    189  1.14  thorpej 
    190  1.14  thorpej /*
    191  1.14  thorpej  * Register a function to be performed at exit.
    192  1.14  thorpej  */
    193  1.14  thorpej int
    194  1.14  thorpej atexit(void (*func)(void))
    195  1.14  thorpej {
    196  1.14  thorpej 
    197  1.14  thorpej 	return (__cxa_atexit((void (*)(void *))func, NULL, NULL));
    198   1.1      cgd }
    199