1*5c7d06e5Sguenther /* $OpenBSD: thread_atexit.c,v 1.2 2019/06/02 01:03:01 guenther Exp $ */
2720afc0eSguenther /*
3720afc0eSguenther * Copyright (c) 2017 Mark Kettenis <kettenis@openbsd.org>
4720afc0eSguenther *
5720afc0eSguenther * Permission to use, copy, modify, and distribute this software for any
6720afc0eSguenther * purpose with or without fee is hereby granted, provided that the above
7720afc0eSguenther * copyright notice and this permission notice appear in all copies.
8720afc0eSguenther *
9720afc0eSguenther * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10720afc0eSguenther * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11720afc0eSguenther * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12720afc0eSguenther * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13720afc0eSguenther * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14720afc0eSguenther * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15720afc0eSguenther * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16720afc0eSguenther */
17720afc0eSguenther
18720afc0eSguenther #include <dlfcn.h>
19720afc0eSguenther #include <stdlib.h>
20720afc0eSguenther #include <tib.h>
21720afc0eSguenther
22720afc0eSguenther #include "atexit.h"
23720afc0eSguenther
24720afc0eSguenther __weak_alias(__cxa_thread_atexit, __cxa_thread_atexit_impl);
25720afc0eSguenther
26720afc0eSguenther int
__cxa_thread_atexit_impl(void (* func)(void *),void * arg,void * dso)27720afc0eSguenther __cxa_thread_atexit_impl(void (*func)(void *), void *arg, void *dso)
28720afc0eSguenther {
29720afc0eSguenther struct thread_atexit_fn *fnp;
30720afc0eSguenther struct tib *tib = TIB_GET();
31720afc0eSguenther
32720afc0eSguenther fnp = calloc(1, sizeof(struct thread_atexit_fn));
33720afc0eSguenther if (fnp == NULL)
34720afc0eSguenther return -1;
35720afc0eSguenther
36720afc0eSguenther dlctl(NULL, DL_REFERENCE, dso);
37720afc0eSguenther
38720afc0eSguenther fnp->func = func;
39720afc0eSguenther fnp->arg = arg;
40720afc0eSguenther fnp->next = tib->tib_atexit;
41720afc0eSguenther tib->tib_atexit = fnp;
42720afc0eSguenther
43720afc0eSguenther return 0;
44720afc0eSguenther }
45