1*284b4eb2Szrj /*
2*284b4eb2Szrj * Copyright (c) 2019 The DragonFly Project. All rights reserved.
3*284b4eb2Szrj *
4*284b4eb2Szrj * Redistribution and use in source and binary forms, with or without
5*284b4eb2Szrj * modification, are permitted provided that the following conditions
6*284b4eb2Szrj * are met:
7*284b4eb2Szrj * 1. Redistributions of source code must retain the above copyright
8*284b4eb2Szrj * notice, this list of conditions and the following disclaimer.
9*284b4eb2Szrj * 2. Redistributions in binary form must reproduce the above copyright
10*284b4eb2Szrj * notice, this list of conditions and the following disclaimer in the
11*284b4eb2Szrj * documentation and/or other materials provided with the distribution.
12*284b4eb2Szrj *
13*284b4eb2Szrj * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
14*284b4eb2Szrj * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
15*284b4eb2Szrj * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
16*284b4eb2Szrj * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
17*284b4eb2Szrj * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
18*284b4eb2Szrj * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
19*284b4eb2Szrj * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20*284b4eb2Szrj * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21*284b4eb2Szrj * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22*284b4eb2Szrj * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
23*284b4eb2Szrj * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*284b4eb2Szrj * SUCH DAMAGE.
25*284b4eb2Szrj */
26*284b4eb2Szrj
27*284b4eb2Szrj #include <sys/queue.h>
28*284b4eb2Szrj #include "namespace.h"
29*284b4eb2Szrj #include <link.h>
30*284b4eb2Szrj #include <stddef.h>
31*284b4eb2Szrj #include <stdlib.h>
32*284b4eb2Szrj #include <unistd.h>
33*284b4eb2Szrj #include "un-namespace.h"
34*284b4eb2Szrj
35*284b4eb2Szrj #include "libc_private.h"
36*284b4eb2Szrj
37*284b4eb2Szrj #define DTORS_ITERATIONS 5
38*284b4eb2Szrj
39*284b4eb2Szrj struct thread_dtorfn {
40*284b4eb2Szrj void (*func)(void *);
41*284b4eb2Szrj void *arg;
42*284b4eb2Szrj void *dso;
43*284b4eb2Szrj LIST_ENTRY(thread_dtorfn) entry;
44*284b4eb2Szrj };
45*284b4eb2Szrj static __thread LIST_HEAD(dtor_list, thread_dtorfn) dtors =
46*284b4eb2Szrj LIST_HEAD_INITIALIZER(dtors);
47*284b4eb2Szrj
48*284b4eb2Szrj int __cxa_thread_atexit_impl(void (*func)(void *), void *arg, void *dso);
49*284b4eb2Szrj
50*284b4eb2Szrj void
_thread_finalize(void)51*284b4eb2Szrj _thread_finalize(void)
52*284b4eb2Szrj {
53*284b4eb2Szrj struct dl_phdr_info phdr_info;
54*284b4eb2Szrj struct thread_dtorfn *fnp, *tdtor;
55*284b4eb2Szrj int i;
56*284b4eb2Szrj
57*284b4eb2Szrj /*
58*284b4eb2Szrj * It is possible to get more destructors registered while
59*284b4eb2Szrj * unregistering them on thread exit. Use maximum DTORS_ITERATIONS
60*284b4eb2Szrj * loops. If dso is no longer available (dlclose()), skip it.
61*284b4eb2Szrj */
62*284b4eb2Szrj for (i = 0; i < DTORS_ITERATIONS && !LIST_EMPTY(&dtors); i++) {
63*284b4eb2Szrj LIST_FOREACH_MUTABLE(fnp, &dtors, entry, tdtor) {
64*284b4eb2Szrj LIST_REMOVE(fnp, entry);
65*284b4eb2Szrj if (_rtld_addr_phdr(fnp->dso, &phdr_info) &&
66*284b4eb2Szrj __elf_phdr_match_addr(&phdr_info, fnp->func))
67*284b4eb2Szrj fnp->func(fnp->arg);
68*284b4eb2Szrj free(fnp);
69*284b4eb2Szrj }
70*284b4eb2Szrj }
71*284b4eb2Szrj }
72*284b4eb2Szrj
73*284b4eb2Szrj int
__cxa_thread_atexit_impl(void (* func)(void *),void * arg,void * dso)74*284b4eb2Szrj __cxa_thread_atexit_impl(void (*func)(void *), void *arg, void *dso)
75*284b4eb2Szrj {
76*284b4eb2Szrj struct thread_dtorfn *fnp;
77*284b4eb2Szrj
78*284b4eb2Szrj fnp = calloc(1, sizeof(*fnp));
79*284b4eb2Szrj if (fnp == NULL)
80*284b4eb2Szrj return -1;
81*284b4eb2Szrj
82*284b4eb2Szrj fnp->func = func;
83*284b4eb2Szrj fnp->arg = arg;
84*284b4eb2Szrj fnp->dso = dso;
85*284b4eb2Szrj LIST_INSERT_HEAD(&dtors, fnp, entry);
86*284b4eb2Szrj
87*284b4eb2Szrj return 0;
88*284b4eb2Szrj }
89