]>
Commit | Line | Data |
---|---|---|
581c785b | 1 | /* Copyright (C) 1991-2022 Free Software Foundation, Inc. |
7cc27f44 | 2 | This file is part of the GNU C Library. |
28f540f4 | 3 | |
7cc27f44 | 4 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
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. | |
28f540f4 | 8 | |
7cc27f44 UD |
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 | |
41bdb6e2 | 12 | Lesser General Public License for more details. |
28f540f4 | 13 | |
41bdb6e2 | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 15 | License along with the GNU C Library; if not, see |
5a82c748 | 16 | <https://www.gnu.org/licenses/>. */ |
28f540f4 | 17 | |
28f540f4 RM |
18 | #include <stdio.h> |
19 | #include <stdlib.h> | |
20 | #include <unistd.h> | |
88f4b692 | 21 | #include <pointer_guard.h> |
26e70aec | 22 | #include <libc-lock.h> |
28f540f4 RM |
23 | #include "exit.h" |
24 | ||
28f540f4 RM |
25 | #include "set-hooks.h" |
26 | DEFINE_HOOK (__libc_atexit, (void)) | |
28f540f4 | 27 | |
26e70aec PP |
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; | |
28f540f4 RM |
32 | |
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. */ | |
36 | void | |
610e67ed UD |
37 | attribute_hidden |
38 | __run_exit_handlers (int status, struct exit_function_list **listp, | |
47dd3543 | 39 | bool run_list_atexit, bool run_dtors) |
28f540f4 | 40 | { |
ba384f6e | 41 | /* First, call the TLS destructors. */ |
e57b0c61 RM |
42 | #ifndef SHARED |
43 | if (&__call_tls_dtors != NULL) | |
44 | #endif | |
47dd3543 CD |
45 | if (run_dtors) |
46 | __call_tls_dtors (); | |
ba384f6e | 47 | |
16adc58e VB |
48 | __libc_lock_lock (__exit_funcs_lock); |
49 | ||
cc3fa755 UD |
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 | |
53 | exit (). */ | |
26e70aec | 54 | while (true) |
28f540f4 | 55 | { |
16adc58e | 56 | struct exit_function_list *cur = *listp; |
26e70aec PP |
57 | |
58 | if (cur == NULL) | |
59 | { | |
60 | /* Exit processing complete. We will not allow any more | |
61 | atexit/on_exit registrations. */ | |
62 | __exit_funcs_done = true; | |
26e70aec PP |
63 | break; |
64 | } | |
c4563d2d | 65 | |
610e67ed | 66 | while (cur->idx > 0) |
28f540f4 | 67 | { |
e5e4d7cc | 68 | struct exit_function *const f = &cur->fns[--cur->idx]; |
26e70aec PP |
69 | const uint64_t new_exitfn_called = __new_exitfn_called; |
70 | ||
28f540f4 RM |
71 | switch (f->flavor) |
72 | { | |
a3c88553 UD |
73 | void (*atfct) (void); |
74 | void (*onfct) (int status, void *arg); | |
75 | void (*cxafct) (void *arg, int status); | |
16adc58e | 76 | void *arg; |
a3c88553 | 77 | |
28f540f4 | 78 | case ef_free: |
ba1ffaa1 | 79 | case ef_us: |
28f540f4 RM |
80 | break; |
81 | case ef_on: | |
a3c88553 | 82 | onfct = f->func.on.fn; |
16adc58e | 83 | arg = f->func.on.arg; |
a3c88553 | 84 | PTR_DEMANGLE (onfct); |
58548b9d | 85 | |
16adc58e VB |
86 | /* Unlock the list while we call a foreign function. */ |
87 | __libc_lock_unlock (__exit_funcs_lock); | |
88 | onfct (status, arg); | |
89 | __libc_lock_lock (__exit_funcs_lock); | |
28f540f4 RM |
90 | break; |
91 | case ef_at: | |
a3c88553 | 92 | atfct = f->func.at; |
a3c88553 | 93 | PTR_DEMANGLE (atfct); |
58548b9d | 94 | |
16adc58e VB |
95 | /* Unlock the list while we call a foreign function. */ |
96 | __libc_lock_unlock (__exit_funcs_lock); | |
a3c88553 | 97 | atfct (); |
16adc58e | 98 | __libc_lock_lock (__exit_funcs_lock); |
28f540f4 | 99 | break; |
c41041bc | 100 | case ef_cxa: |
e5e4d7cc PP |
101 | /* To avoid dlclose/exit race calling cxafct twice (BZ 22180), |
102 | we must mark this function as ef_free. */ | |
103 | f->flavor = ef_free; | |
a3c88553 | 104 | cxafct = f->func.cxa.fn; |
16adc58e | 105 | arg = f->func.cxa.arg; |
a3c88553 | 106 | PTR_DEMANGLE (cxafct); |
58548b9d | 107 | |
16adc58e VB |
108 | /* Unlock the list while we call a foreign function. */ |
109 | __libc_lock_unlock (__exit_funcs_lock); | |
110 | cxafct (arg, status); | |
111 | __libc_lock_lock (__exit_funcs_lock); | |
3bbddbe4 | 112 | break; |
28f540f4 | 113 | } |
26e70aec PP |
114 | |
115 | if (__glibc_unlikely (new_exitfn_called != __new_exitfn_called)) | |
116 | /* The last exit function, or another thread, has registered | |
117 | more exit functions. Start the loop over. */ | |
16adc58e | 118 | continue; |
28f540f4 | 119 | } |
c4563d2d | 120 | |
610e67ed UD |
121 | *listp = cur->next; |
122 | if (*listp != NULL) | |
bca9f4ab UD |
123 | /* Don't free the last element in the chain, this is the statically |
124 | allocate element. */ | |
610e67ed | 125 | free (cur); |
28f540f4 RM |
126 | } |
127 | ||
16adc58e VB |
128 | __libc_lock_unlock (__exit_funcs_lock); |
129 | ||
610e67ed UD |
130 | if (run_list_atexit) |
131 | RUN_HOOK (__libc_atexit, ()); | |
28f540f4 | 132 | |
ba1ffaa1 | 133 | _exit (status); |
28f540f4 | 134 | } |
610e67ed UD |
135 | |
136 | ||
137 | void | |
138 | exit (int status) | |
139 | { | |
47dd3543 | 140 | __run_exit_handlers (status, &__exit_funcs, true, true); |
610e67ed | 141 | } |
a757607d | 142 | libc_hidden_def (exit) |