1 /* Copyright (C) 1991-2021 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
22 #include <libc-lock.h>
25 #include "set-hooks.h"
26 DEFINE_HOOK (__libc_atexit
, (void))
28 /* Initialize the flag that indicates exit function processing
29 is complete. See concurrency notes in stdlib/exit.h where
30 __exit_funcs_lock is declared. */
31 bool __exit_funcs_done
= false;
33 /* Call all functions registered with `atexit' and `on_exit',
34 in the reverse of the order in which they were registered
35 perform stdio cleanup, and terminate program execution with STATUS. */
38 __run_exit_handlers (int status
, struct exit_function_list
**listp
,
39 bool run_list_atexit
, bool run_dtors
)
41 /* First, call the TLS destructors. */
43 if (&__call_tls_dtors
!= NULL
)
48 __libc_lock_lock (__exit_funcs_lock
);
50 /* We do it this way to handle recursive calls to exit () made by
51 the functions registered with `atexit' and `on_exit'. We call
52 everyone on the list and use the status value in the last
56 struct exit_function_list
*cur
= *listp
;
60 /* Exit processing complete. We will not allow any more
61 atexit/on_exit registrations. */
62 __exit_funcs_done
= true;
68 struct exit_function
*const f
= &cur
->fns
[--cur
->idx
];
69 const uint64_t new_exitfn_called
= __new_exitfn_called
;
74 void (*onfct
) (int status
, void *arg
);
75 void (*cxafct
) (void *arg
, int status
);
82 onfct
= f
->func
.on
.fn
;
87 /* Unlock the list while we call a foreign function. */
88 __libc_lock_unlock (__exit_funcs_lock
);
90 __libc_lock_lock (__exit_funcs_lock
);
97 /* Unlock the list while we call a foreign function. */
98 __libc_lock_unlock (__exit_funcs_lock
);
100 __libc_lock_lock (__exit_funcs_lock
);
103 /* To avoid dlclose/exit race calling cxafct twice (BZ 22180),
104 we must mark this function as ef_free. */
106 cxafct
= f
->func
.cxa
.fn
;
107 arg
= f
->func
.cxa
.arg
;
109 PTR_DEMANGLE (cxafct
);
111 /* Unlock the list while we call a foreign function. */
112 __libc_lock_unlock (__exit_funcs_lock
);
113 cxafct (arg
, status
);
114 __libc_lock_lock (__exit_funcs_lock
);
118 if (__glibc_unlikely (new_exitfn_called
!= __new_exitfn_called
))
119 /* The last exit function, or another thread, has registered
120 more exit functions. Start the loop over. */
126 /* Don't free the last element in the chain, this is the statically
131 __libc_lock_unlock (__exit_funcs_lock
);
134 RUN_HOOK (__libc_atexit
, ());
143 __run_exit_handlers (status
, &__exit_funcs
, true, true);
145 libc_hidden_def (exit
)