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.
16251069Semaste * 3. 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$");
381573Srgrimes
3991697Stegge#include "namespace.h"
40211706Skib#include <link.h>
411573Srgrimes#include <stddef.h>
421573Srgrimes#include <stdlib.h>
4318516Sphk#include <unistd.h>
4491697Stegge#include <pthread.h>
451573Srgrimes#include "atexit.h"
4691697Stegge#include "un-namespace.h"
471573Srgrimes
4891697Stegge#include "libc_private.h"
4991697Stegge
50123673Skan#define	ATEXIT_FN_EMPTY	0
51123673Skan#define	ATEXIT_FN_STD	1
52123673Skan#define	ATEXIT_FN_CXA	2
53123673Skan
5491697Steggestatic pthread_mutex_t atexit_mutex = PTHREAD_MUTEX_INITIALIZER;
5591697Stegge
5691697Stegge#define _MUTEX_LOCK(x)		if (__isthreaded) _pthread_mutex_lock(x)
5791697Stegge#define _MUTEX_UNLOCK(x)	if (__isthreaded) _pthread_mutex_unlock(x)
58209117Scperciva#define _MUTEX_DESTROY(x)	if (__isthreaded) _pthread_mutex_destroy(x)
5991697Stegge
60123673Skanstruct atexit {
61123673Skan	struct atexit *next;			/* next in list */
62123673Skan	int ind;				/* next index in this table */
63123673Skan	struct atexit_fn {
64123673Skan		int fn_type;			/* ATEXIT_? from above */
65123673Skan		union {
66123673Skan			void (*std_func)(void);
67123673Skan			void (*cxa_func)(void *);
68123673Skan		} fn_ptr;			/* function pointer */
69123673Skan		void *fn_arg;			/* argument for CXA callback */
70123673Skan		void *fn_dso;			/* shared module handle */
71123673Skan	} fns[ATEXIT_SIZE];			/* the table itself */
72123673Skan};
7323662Speter
74123673Skanstatic struct atexit *__atexit;		/* points to head of LIFO stack */
75123673Skan
761573Srgrimes/*
77123673Skan * Register the function described by 'fptr' to be called at application
78123673Skan * exit or owning shared object unload time. This is a helper function
79123673Skan * for atexit and __cxa_atexit.
801573Srgrimes */
81123673Skanstatic int
82123673Skanatexit_register(struct atexit_fn *fptr)
831573Srgrimes{
841573Srgrimes	static struct atexit __atexit0;	/* one guaranteed table */
8592889Sobrien	struct atexit *p;
861573Srgrimes
8791697Stegge	_MUTEX_LOCK(&atexit_mutex);
881573Srgrimes	if ((p = __atexit) == NULL)
891573Srgrimes		__atexit = p = &__atexit0;
9091697Stegge	else while (p->ind >= ATEXIT_SIZE) {
9191697Stegge		struct atexit *old__atexit;
9291697Stegge		old__atexit = __atexit;
9391697Stegge	        _MUTEX_UNLOCK(&atexit_mutex);
9491697Stegge		if ((p = (struct atexit *)malloc(sizeof(*p))) == NULL)
951573Srgrimes			return (-1);
9691697Stegge		_MUTEX_LOCK(&atexit_mutex);
9791697Stegge		if (old__atexit != __atexit) {
9891697Stegge			/* Lost race, retry operation */
9991697Stegge			_MUTEX_UNLOCK(&atexit_mutex);
10091697Stegge			free(p);
10191697Stegge			_MUTEX_LOCK(&atexit_mutex);
10291697Stegge			p = __atexit;
10391697Stegge			continue;
10491697Stegge		}
1051573Srgrimes		p->ind = 0;
1061573Srgrimes		p->next = __atexit;
1071573Srgrimes		__atexit = p;
1081573Srgrimes	}
109123673Skan	p->fns[p->ind++] = *fptr;
11091697Stegge	_MUTEX_UNLOCK(&atexit_mutex);
111123673Skan	return 0;
1121573Srgrimes}
113123673Skan
114123673Skan/*
115123673Skan * Register a function to be performed at exit.
116123673Skan */
117123673Skanint
118123673Skanatexit(void (*func)(void))
119123673Skan{
120123673Skan	struct atexit_fn fn;
121123673Skan	int error;
122123673Skan
123123673Skan	fn.fn_type = ATEXIT_FN_STD;
124211704Skib	fn.fn_ptr.std_func = func;
125123673Skan	fn.fn_arg = NULL;
126123673Skan	fn.fn_dso = NULL;
127123673Skan
128123673Skan 	error = atexit_register(&fn);
129123673Skan	return (error);
130123673Skan}
131123673Skan
132123673Skan/*
133123673Skan * Register a function to be performed at exit or when an shared object
134123673Skan * with given dso handle is unloaded dynamically.
135123673Skan */
136123673Skanint
137123673Skan__cxa_atexit(void (*func)(void *), void *arg, void *dso)
138123673Skan{
139123673Skan	struct atexit_fn fn;
140123673Skan	int error;
141123673Skan
142123673Skan	fn.fn_type = ATEXIT_FN_CXA;
143211704Skib	fn.fn_ptr.cxa_func = func;
144123673Skan	fn.fn_arg = arg;
145123673Skan	fn.fn_dso = dso;
146123673Skan
147123673Skan 	error = atexit_register(&fn);
148123673Skan	return (error);
149123673Skan}
150123673Skan
151211706Skib#pragma weak __pthread_cxa_finalize
152211706Skibvoid __pthread_cxa_finalize(const struct dl_phdr_info *);
153211706Skib
154123673Skan/*
155123673Skan * Call all handlers registered with __cxa_atexit for the shared
156123673Skan * object owning 'dso'.  Note: if 'dso' is NULL, then all remaining
157123673Skan * handlers are called.
158123673Skan */
159123673Skanvoid
160123673Skan__cxa_finalize(void *dso)
161123673Skan{
162211706Skib	struct dl_phdr_info phdr_info;
163123673Skan	struct atexit *p;
164123673Skan	struct atexit_fn fn;
165211706Skib	int n, has_phdr;
166123673Skan
167211706Skib	if (dso != NULL)
168211706Skib		has_phdr = _rtld_addr_phdr(dso, &phdr_info);
169211706Skib	else
170211706Skib		has_phdr = 0;
171211706Skib
172123673Skan	_MUTEX_LOCK(&atexit_mutex);
173123673Skan	for (p = __atexit; p; p = p->next) {
174123673Skan		for (n = p->ind; --n >= 0;) {
175123673Skan			if (p->fns[n].fn_type == ATEXIT_FN_EMPTY)
176123673Skan				continue; /* already been called */
177123673Skan			fn = p->fns[n];
178211706Skib			if (dso != NULL && dso != fn.fn_dso) {
179211706Skib				/* wrong DSO ? */
180211706Skib				if (!has_phdr || !__elf_phdr_match_addr(
181211706Skib				    &phdr_info, fn.fn_ptr.cxa_func))
182211706Skib					continue;
183211706Skib			}
184123673Skan			/*
185123673Skan			  Mark entry to indicate that this particular handler
186123673Skan			  has already been called.
187123673Skan			*/
188123673Skan			p->fns[n].fn_type = ATEXIT_FN_EMPTY;
189123673Skan		        _MUTEX_UNLOCK(&atexit_mutex);
190123673Skan
191123673Skan			/* Call the function of correct type. */
192123673Skan			if (fn.fn_type == ATEXIT_FN_CXA)
193123673Skan				fn.fn_ptr.cxa_func(fn.fn_arg);
194123673Skan			else if (fn.fn_type == ATEXIT_FN_STD)
195123673Skan				fn.fn_ptr.std_func();
196123673Skan			_MUTEX_LOCK(&atexit_mutex);
197123673Skan		}
198123673Skan	}
199123673Skan	_MUTEX_UNLOCK(&atexit_mutex);
200209117Scperciva	if (dso == NULL)
201209117Scperciva		_MUTEX_DESTROY(&atexit_mutex);
202211706Skib
203211894Skib	if (has_phdr && &__pthread_cxa_finalize != NULL)
204211706Skib		__pthread_cxa_finalize(&phdr_info);
205123673Skan}
206