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