atexit.c revision 209117
11573Srgrimes/*-
21573Srgrimes * Copyright (c) 1990, 1993
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * This code is derived from software contributed to Berkeley by
61573Srgrimes * Chris Torek.
71573Srgrimes *
81573Srgrimes * Redistribution and use in source and binary forms, with or without
91573Srgrimes * modification, are permitted provided that the following conditions
101573Srgrimes * are met:
111573Srgrimes * 1. Redistributions of source code must retain the above copyright
121573Srgrimes *    notice, this list of conditions and the following disclaimer.
131573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141573Srgrimes *    notice, this list of conditions and the following disclaimer in the
151573Srgrimes *    documentation and/or other materials provided with the distribution.
161573Srgrimes * 4. Neither the name of the University nor the names of its contributors
171573Srgrimes *    may be used to endorse or promote products derived from this software
181573Srgrimes *    without specific prior written permission.
191573Srgrimes *
201573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301573Srgrimes * SUCH DAMAGE.
311573Srgrimes */
321573Srgrimes
331573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
3423662Speterstatic char sccsid[] = "@(#)atexit.c	8.2 (Berkeley) 7/3/94";
351573Srgrimes#endif /* LIBC_SCCS and not lint */
3692986Sobrien#include <sys/cdefs.h>
3792986Sobrien__FBSDID("$FreeBSD: head/lib/libc/stdlib/atexit.c 209117 2010-06-13 01:13:36Z cperciva $");
381573Srgrimes
3991697Stegge#include "namespace.h"
401573Srgrimes#include <stddef.h>
411573Srgrimes#include <stdlib.h>
4218516Sphk#include <unistd.h>
4391697Stegge#include <pthread.h>
441573Srgrimes#include "atexit.h"
4591697Stegge#include "un-namespace.h"
461573Srgrimes
4791697Stegge#include "libc_private.h"
4891697Stegge
49123673Skan#define	ATEXIT_FN_EMPTY	0
50123673Skan#define	ATEXIT_FN_STD	1
51123673Skan#define	ATEXIT_FN_CXA	2
52123673Skan
5391697Steggestatic pthread_mutex_t atexit_mutex = PTHREAD_MUTEX_INITIALIZER;
5491697Stegge
5591697Stegge#define _MUTEX_LOCK(x)		if (__isthreaded) _pthread_mutex_lock(x)
5691697Stegge#define _MUTEX_UNLOCK(x)	if (__isthreaded) _pthread_mutex_unlock(x)
57209117Scperciva#define _MUTEX_DESTROY(x)	if (__isthreaded) _pthread_mutex_destroy(x)
5891697Stegge
59123673Skanstruct atexit {
60123673Skan	struct atexit *next;			/* next in list */
61123673Skan	int ind;				/* next index in this table */
62123673Skan	struct atexit_fn {
63123673Skan		int fn_type;			/* ATEXIT_? from above */
64123673Skan		union {
65123673Skan			void (*std_func)(void);
66123673Skan			void (*cxa_func)(void *);
67123673Skan		} fn_ptr;			/* function pointer */
68123673Skan		void *fn_arg;			/* argument for CXA callback */
69123673Skan		void *fn_dso;			/* shared module handle */
70123673Skan	} fns[ATEXIT_SIZE];			/* the table itself */
71123673Skan};
7223662Speter
73123673Skanstatic struct atexit *__atexit;		/* points to head of LIFO stack */
74123673Skan
751573Srgrimes/*
76123673Skan * Register the function described by 'fptr' to be called at application
77123673Skan * exit or owning shared object unload time. This is a helper function
78123673Skan * for atexit and __cxa_atexit.
791573Srgrimes */
80123673Skanstatic int
81123673Skanatexit_register(struct atexit_fn *fptr)
821573Srgrimes{
831573Srgrimes	static struct atexit __atexit0;	/* one guaranteed table */
8492889Sobrien	struct atexit *p;
851573Srgrimes
8691697Stegge	_MUTEX_LOCK(&atexit_mutex);
871573Srgrimes	if ((p = __atexit) == NULL)
881573Srgrimes		__atexit = p = &__atexit0;
8991697Stegge	else while (p->ind >= ATEXIT_SIZE) {
9091697Stegge		struct atexit *old__atexit;
9191697Stegge		old__atexit = __atexit;
9291697Stegge	        _MUTEX_UNLOCK(&atexit_mutex);
9391697Stegge		if ((p = (struct atexit *)malloc(sizeof(*p))) == NULL)
941573Srgrimes			return (-1);
9591697Stegge		_MUTEX_LOCK(&atexit_mutex);
9691697Stegge		if (old__atexit != __atexit) {
9791697Stegge			/* Lost race, retry operation */
9891697Stegge			_MUTEX_UNLOCK(&atexit_mutex);
9991697Stegge			free(p);
10091697Stegge			_MUTEX_LOCK(&atexit_mutex);
10191697Stegge			p = __atexit;
10291697Stegge			continue;
10391697Stegge		}
1041573Srgrimes		p->ind = 0;
1051573Srgrimes		p->next = __atexit;
1061573Srgrimes		__atexit = p;
1071573Srgrimes	}
108123673Skan	p->fns[p->ind++] = *fptr;
10991697Stegge	_MUTEX_UNLOCK(&atexit_mutex);
110123673Skan	return 0;
1111573Srgrimes}
112123673Skan
113123673Skan/*
114123673Skan * Register a function to be performed at exit.
115123673Skan */
116123673Skanint
117123673Skanatexit(void (*func)(void))
118123673Skan{
119123673Skan	struct atexit_fn fn;
120123673Skan	int error;
121123673Skan
122123673Skan	fn.fn_type = ATEXIT_FN_STD;
123123673Skan	fn.fn_ptr.std_func = func;;
124123673Skan	fn.fn_arg = NULL;
125123673Skan	fn.fn_dso = NULL;
126123673Skan
127123673Skan 	error = atexit_register(&fn);
128123673Skan	return (error);
129123673Skan}
130123673Skan
131123673Skan/*
132123673Skan * Register a function to be performed at exit or when an shared object
133123673Skan * with given dso handle is unloaded dynamically.
134123673Skan */
135123673Skanint
136123673Skan__cxa_atexit(void (*func)(void *), void *arg, void *dso)
137123673Skan{
138123673Skan	struct atexit_fn fn;
139123673Skan	int error;
140123673Skan
141123673Skan	fn.fn_type = ATEXIT_FN_CXA;
142123673Skan	fn.fn_ptr.cxa_func = func;;
143123673Skan	fn.fn_arg = arg;
144123673Skan	fn.fn_dso = dso;
145123673Skan
146123673Skan 	error = atexit_register(&fn);
147123673Skan	return (error);
148123673Skan}
149123673Skan
150123673Skan/*
151123673Skan * Call all handlers registered with __cxa_atexit for the shared
152123673Skan * object owning 'dso'.  Note: if 'dso' is NULL, then all remaining
153123673Skan * handlers are called.
154123673Skan */
155123673Skanvoid
156123673Skan__cxa_finalize(void *dso)
157123673Skan{
158123673Skan	struct atexit *p;
159123673Skan	struct atexit_fn fn;
160123673Skan	int n;
161123673Skan
162123673Skan	_MUTEX_LOCK(&atexit_mutex);
163123673Skan	for (p = __atexit; p; p = p->next) {
164123673Skan		for (n = p->ind; --n >= 0;) {
165123673Skan			if (p->fns[n].fn_type == ATEXIT_FN_EMPTY)
166123673Skan				continue; /* already been called */
167123673Skan			if (dso != NULL && dso != p->fns[n].fn_dso)
168123673Skan				continue; /* wrong DSO */
169123673Skan			fn = p->fns[n];
170123673Skan			/*
171123673Skan			  Mark entry to indicate that this particular handler
172123673Skan			  has already been called.
173123673Skan			*/
174123673Skan			p->fns[n].fn_type = ATEXIT_FN_EMPTY;
175123673Skan		        _MUTEX_UNLOCK(&atexit_mutex);
176123673Skan
177123673Skan			/* Call the function of correct type. */
178123673Skan			if (fn.fn_type == ATEXIT_FN_CXA)
179123673Skan				fn.fn_ptr.cxa_func(fn.fn_arg);
180123673Skan			else if (fn.fn_type == ATEXIT_FN_STD)
181123673Skan				fn.fn_ptr.std_func();
182123673Skan			_MUTEX_LOCK(&atexit_mutex);
183123673Skan		}
184123673Skan	}
185123673Skan	_MUTEX_UNLOCK(&atexit_mutex);
186209117Scperciva	if (dso == NULL)
187209117Scperciva		_MUTEX_DESTROY(&atexit_mutex);
188123673Skan}
189