lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Copyright (C) 1995,1996 Robert de Bath <rdebath@cix.compulink.co.uk> |
| 2 | * This file is part of the Linux-8086 C library and is distributed |
| 3 | * under the GNU Library General Public License. |
| 4 | */ |
| 5 | |
| 6 | /* |
| 7 | * Dec 2000 Manuel Novoa III |
| 8 | * |
| 9 | * Made atexit handling conform to standards... i.e. no args. |
| 10 | * Removed on_exit since it did not match gnu libc definition. |
| 11 | * Combined atexit and __do_exit into one object file. |
| 12 | * |
| 13 | * Feb 2001 Manuel Novoa III |
| 14 | * |
| 15 | * Reworked file after addition of __uClibc_main. |
| 16 | * Changed name of __do_exit to atexit_handler. |
| 17 | * Changed name of __cleanup to __uClibc_cleanup. |
| 18 | * Moved declaration of __uClibc_cleanup to __uClibc_main |
| 19 | * where it is initialized with (possibly weak alias) |
| 20 | * _stdio_term. |
| 21 | * |
| 22 | * Jul 2001 Steve Thayer |
| 23 | * |
| 24 | * Added an on_exit implementation (that now matches gnu libc definition.) |
| 25 | * Pulled atexit_handler out of the atexit object since it is now required by |
| 26 | * on_exit as well. Renamed it to __exit_handler. |
| 27 | * Fixed a problem where exit functions stop getting called if one of |
| 28 | * them calls exit(). |
| 29 | * As a side effect of these changes, abort() no longer calls the exit |
| 30 | * functions (it now matches the gnu libc definition). |
| 31 | * |
| 32 | * August 2002 Erik Andersen |
| 33 | * Added locking so atexit and friends can be thread safe |
| 34 | * |
| 35 | * August 2005 Stephen Warren |
| 36 | * Added __cxa_atexit and __cxa_finalize support |
| 37 | * |
| 38 | */ |
| 39 | |
| 40 | #include <features.h> |
| 41 | #include <unistd.h> |
| 42 | #include <stdlib.h> |
| 43 | #include <errno.h> |
| 44 | #include <atomic.h> |
| 45 | |
| 46 | #include <bits/uClibc_mutex.h> |
| 47 | __UCLIBC_MUTEX_EXTERN(__atexit_lock); |
| 48 | |
| 49 | |
| 50 | |
| 51 | typedef void (*aefuncp)(void); /* atexit function pointer */ |
| 52 | typedef void (*oefuncp)(int, void *); /* on_exit function pointer */ |
| 53 | typedef void (*cxaefuncp)(void *); /* __cxa_atexit function pointer */ |
| 54 | typedef enum { |
| 55 | ef_free, |
| 56 | ef_in_use, |
| 57 | ef_on_exit, |
| 58 | ef_cxa_atexit |
| 59 | } ef_type; /* exit function types */ |
| 60 | |
| 61 | /* this is in the L_exit object */ |
| 62 | extern void (*__exit_cleanup)(int) attribute_hidden; |
| 63 | |
| 64 | /* these are in the L___do_exit object */ |
| 65 | extern int __exit_slots attribute_hidden; |
| 66 | extern int __exit_count attribute_hidden; |
| 67 | extern void __exit_handler(int) attribute_hidden; |
| 68 | struct exit_function { |
| 69 | /* |
| 70 | * 'type' should be of type of the 'enum ef_type' above but since we |
| 71 | * need this element in an atomic operation we have to use 'long int'. |
| 72 | */ |
| 73 | long int type; /* enum ef_type */ |
| 74 | union { |
| 75 | struct { |
| 76 | oefuncp func; |
| 77 | void *arg; |
| 78 | } on_exit; |
| 79 | struct { |
| 80 | cxaefuncp func; |
| 81 | void *arg; |
| 82 | void* dso_handle; |
| 83 | } cxa_atexit; |
| 84 | } funcs; |
| 85 | }; |
| 86 | #ifdef __UCLIBC_DYNAMIC_ATEXIT__ |
| 87 | extern struct exit_function *__exit_function_table attribute_hidden; |
| 88 | #else |
| 89 | extern struct exit_function __exit_function_table[__UCLIBC_MAX_ATEXIT] attribute_hidden; |
| 90 | #endif |
| 91 | extern struct exit_function *__new_exitfn(void) attribute_hidden; |
| 92 | |
| 93 | /* this is in the L___cxa_atexit object */ |
| 94 | extern int __cxa_atexit(cxaefuncp, void *arg, void *dso_handle); |
| 95 | |
| 96 | |
| 97 | /* remove old_atexit after 0.9.29 */ |
| 98 | #if defined(L_atexit) || defined(L_old_atexit) |
| 99 | extern void *__dso_handle __attribute__ ((__weak__)); |
| 100 | |
| 101 | /* |
| 102 | * register a function to be called at normal program termination |
| 103 | * (the registered function takes no arguments) |
| 104 | */ |
| 105 | #ifdef L_atexit |
| 106 | int attribute_hidden atexit(aefuncp func) |
| 107 | #else |
| 108 | int old_atexit(aefuncp func); |
| 109 | int old_atexit(aefuncp func) |
| 110 | #endif |
| 111 | { |
| 112 | /* |
| 113 | * glibc casts aefuncp to cxaefuncp. |
| 114 | * This seems dodgy, but I guess calling a function with more |
| 115 | * parameters than it needs will work everywhere? |
| 116 | */ |
| 117 | return __cxa_atexit((cxaefuncp)func, NULL, |
| 118 | &__dso_handle == NULL ? NULL : __dso_handle); |
| 119 | } |
| 120 | #ifndef L_atexit |
| 121 | weak_alias(old_atexit,atexit) |
| 122 | #endif |
| 123 | #endif |
| 124 | |
| 125 | #ifdef L_on_exit |
| 126 | /* |
| 127 | * register a function to be called at normal program termination |
| 128 | * the registered function takes two arguments: |
| 129 | * status - the exit status that was passed to the exit() function |
| 130 | * arg - generic argument |
| 131 | */ |
| 132 | int on_exit(oefuncp func, void *arg) |
| 133 | { |
| 134 | struct exit_function *efp; |
| 135 | |
| 136 | if (func == NULL) { |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | efp = __new_exitfn(); |
| 141 | if (efp == NULL) { |
| 142 | return -1; |
| 143 | } |
| 144 | |
| 145 | efp->funcs.on_exit.func = func; |
| 146 | efp->funcs.on_exit.arg = arg; |
| 147 | /* assign last for thread safety, since we're now unlocked */ |
| 148 | efp->type = ef_on_exit; |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | #endif |
| 153 | |
| 154 | #ifdef L___cxa_atexit |
| 155 | libc_hidden_proto(__cxa_atexit) |
| 156 | int __cxa_atexit(cxaefuncp func, void *arg, void *dso_handle) |
| 157 | { |
| 158 | struct exit_function *efp; |
| 159 | |
| 160 | if (func == NULL) { |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | efp = __new_exitfn(); |
| 165 | if (efp == NULL) { |
| 166 | return -1; |
| 167 | } |
| 168 | |
| 169 | efp->funcs.cxa_atexit.func = func; |
| 170 | efp->funcs.cxa_atexit.arg = arg; |
| 171 | efp->funcs.cxa_atexit.dso_handle = dso_handle; |
| 172 | /* assign last for thread safety, since we're now unlocked */ |
| 173 | efp->type = ef_cxa_atexit; |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | libc_hidden_def(__cxa_atexit) |
| 178 | #endif |
| 179 | |
| 180 | #ifdef L___cxa_finalize |
| 181 | /* |
| 182 | * If D is non-NULL, call all functions registered with `__cxa_atexit' |
| 183 | * with the same dso handle. Otherwise, if D is NULL, call all of the |
| 184 | * registered handlers. |
| 185 | */ |
| 186 | void __cxa_finalize(void *dso_handle); |
| 187 | void __cxa_finalize(void *dso_handle) |
| 188 | { |
| 189 | struct exit_function *efp; |
| 190 | int exit_count_snapshot = __exit_count; |
| 191 | |
| 192 | /* In reverse order */ |
| 193 | while (exit_count_snapshot) { |
| 194 | efp = &__exit_function_table[--exit_count_snapshot]; |
| 195 | |
| 196 | /* |
| 197 | * We check dso_handle match before we verify the type of the union entry. |
| 198 | * However, the atomic_exchange will validate that we were really "allowed" |
| 199 | * to read dso_handle... |
| 200 | */ |
| 201 | if ((dso_handle == NULL || dso_handle == efp->funcs.cxa_atexit.dso_handle) |
| 202 | /* We don't want to run this cleanup more than once. */ |
| 203 | && !atomic_compare_and_exchange_bool_acq(&efp->type, ef_free, ef_cxa_atexit) |
| 204 | ) { |
| 205 | /* glibc passes status (0) too, but that's not in the prototype */ |
| 206 | (*efp->funcs.cxa_atexit.func)(efp->funcs.cxa_atexit.arg); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | #if 0 /* haven't looked into this yet... */ |
| 211 | /* |
| 212 | * Remove the registered fork handlers. We do not have to |
| 213 | * unregister anything if the program is going to terminate anyway. |
| 214 | */ |
| 215 | #ifdef UNREGISTER_ATFORK |
| 216 | if (d != NULL) { |
| 217 | UNREGISTER_ATFORK(d); |
| 218 | } |
| 219 | #endif |
| 220 | #endif |
| 221 | } |
| 222 | #endif |
| 223 | |
| 224 | #ifdef L___exit_handler |
| 225 | int __exit_count = 0; /* Number of registered exit functions */ |
| 226 | #ifdef __UCLIBC_DYNAMIC_ATEXIT__ |
| 227 | struct exit_function *__exit_function_table = NULL; |
| 228 | int __exit_slots = 0; /* Size of __exit_function_table */ |
| 229 | #else |
| 230 | struct exit_function __exit_function_table[__UCLIBC_MAX_ATEXIT]; |
| 231 | #endif |
| 232 | |
| 233 | /* |
| 234 | * Find and return a new exit_function pointer, for atexit, |
| 235 | * onexit and __cxa_atexit to initialize |
| 236 | */ |
| 237 | struct exit_function attribute_hidden *__new_exitfn(void) |
| 238 | { |
| 239 | struct exit_function *efp; |
| 240 | |
| 241 | __UCLIBC_MUTEX_LOCK(__atexit_lock); |
| 242 | |
| 243 | #ifdef __UCLIBC_DYNAMIC_ATEXIT__ |
| 244 | /* If we are out of function table slots, make some more */ |
| 245 | if (__exit_slots < __exit_count+1) { |
| 246 | efp = realloc(__exit_function_table, |
| 247 | (__exit_slots+20)*sizeof(struct exit_function)); |
| 248 | if (efp == NULL) { |
| 249 | __set_errno(ENOMEM); |
| 250 | goto DONE; |
| 251 | } |
| 252 | __exit_function_table = efp; |
| 253 | __exit_slots += 20; |
| 254 | } |
| 255 | #else |
| 256 | if (__exit_count >= __UCLIBC_MAX_ATEXIT) { |
| 257 | __set_errno(ENOMEM); |
| 258 | efp = NULL; |
| 259 | goto DONE; |
| 260 | } |
| 261 | #endif |
| 262 | |
| 263 | __exit_cleanup = __exit_handler; /* enable cleanup */ |
| 264 | efp = &__exit_function_table[__exit_count++]; |
| 265 | efp->type = ef_in_use; |
| 266 | |
| 267 | DONE: |
| 268 | __UCLIBC_MUTEX_UNLOCK(__atexit_lock); |
| 269 | return efp; |
| 270 | } |
| 271 | |
| 272 | /* |
| 273 | * Handle the work of executing the registered exit functions |
| 274 | * This is called while we are locked, so no additional locking |
| 275 | * is needed... |
| 276 | */ |
| 277 | void __exit_handler(int status) |
| 278 | { |
| 279 | struct exit_function *efp; |
| 280 | |
| 281 | /* In reverse order */ |
| 282 | while (__exit_count) { |
| 283 | efp = &__exit_function_table[--__exit_count]; |
| 284 | switch (efp->type) { |
| 285 | case ef_on_exit: |
| 286 | if (efp->funcs.on_exit.func) { |
| 287 | (efp->funcs.on_exit.func)(status, efp->funcs.on_exit.arg); |
| 288 | } |
| 289 | break; |
| 290 | case ef_cxa_atexit: |
| 291 | if (efp->funcs.cxa_atexit.func) { |
| 292 | /* glibc passes status too, but that's not in the prototype */ |
| 293 | (efp->funcs.cxa_atexit.func)(efp->funcs.cxa_atexit.arg); |
| 294 | } |
| 295 | break; |
| 296 | } |
| 297 | } |
| 298 | #ifdef __UCLIBC_DYNAMIC_ATEXIT__ |
| 299 | /* Free up memory used by the __exit_function_table structure */ |
| 300 | free(__exit_function_table); |
| 301 | #endif |
| 302 | } |
| 303 | #endif |
| 304 | |
| 305 | #ifdef L_exit |
| 306 | /* Defeat compiler optimization which assumes function addresses are never NULL */ |
| 307 | static __always_inline int not_null_ptr(const void *p) |
| 308 | { |
| 309 | const void *q; |
| 310 | __asm__ ("" |
| 311 | : "=r" (q) /* output */ |
| 312 | : "0" (p) /* input */ |
| 313 | ); |
| 314 | return q != 0; |
| 315 | } |
| 316 | |
| 317 | extern void weak_function _stdio_term(void) attribute_hidden; |
| 318 | attribute_hidden void (*__exit_cleanup)(int) = 0; |
| 319 | __UCLIBC_MUTEX_INIT(__atexit_lock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP); |
| 320 | |
| 321 | extern void __uClibc_fini(void); |
| 322 | libc_hidden_proto(__uClibc_fini) |
| 323 | |
| 324 | /* |
| 325 | * Normal program termination |
| 326 | */ |
| 327 | void exit(int rv) |
| 328 | { |
| 329 | /* Perform exit-specific cleanup (atexit and on_exit) */ |
| 330 | __UCLIBC_MUTEX_LOCK(__atexit_lock); |
| 331 | if (not_null_ptr(__exit_cleanup)) { |
| 332 | __exit_cleanup(rv); |
| 333 | } |
| 334 | __UCLIBC_MUTEX_UNLOCK(__atexit_lock); |
| 335 | |
| 336 | __uClibc_fini(); |
| 337 | |
| 338 | /* If we are using stdio, try to shut it down. At the very least, |
| 339 | * this will attempt to commit all buffered writes. It may also |
| 340 | * unbuffer all writable files, or close them outright. |
| 341 | * Check the stdio routines for details. */ |
| 342 | if (not_null_ptr(_stdio_term)) |
| 343 | _stdio_term(); |
| 344 | |
| 345 | _exit(rv); |
| 346 | } |
| 347 | libc_hidden_def(exit) |
| 348 | #endif |