xref: /openbsd-src/lib/libc/stdlib/thread_atexit.c (revision c90a81c56dcebd6a1b73fe4aff9b03385b8e63b3)
1 /*	$OpenBSD: thread_atexit.c,v 1.1 2017/12/16 20:06:56 guenther Exp $ */
2 /*
3  * Copyright (c) 2017 Mark Kettenis <kettenis@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <dlfcn.h>
19 #include <elf.h>
20 #pragma weak _DYNAMIC
21 #include <stdlib.h>
22 #include <tib.h>
23 
24 #include "atexit.h"
25 
26 typeof(dlctl) dlctl asm("_dlctl") __attribute__((weak));
27 
28 __weak_alias(__cxa_thread_atexit, __cxa_thread_atexit_impl);
29 
30 int
31 __cxa_thread_atexit_impl(void (*func)(void *), void *arg, void *dso)
32 {
33 	struct thread_atexit_fn *fnp;
34 	struct tib *tib = TIB_GET();
35 
36 	fnp = calloc(1, sizeof(struct thread_atexit_fn));
37 	if (fnp == NULL)
38 		return -1;
39 
40 	if (_DYNAMIC)
41 		dlctl(NULL, DL_REFERENCE, dso);
42 
43 	fnp->func = func;
44 	fnp->arg = arg;
45 	fnp->next = tib->tib_atexit;
46 	tib->tib_atexit = fnp;
47 
48 	return 0;
49 }
50