1/* Copyright (C) 1991-2022 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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.
8
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.
13
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/>. */
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <unistd.h>
21#include <sysdep.h>
22#include <libc-lock.h>
23#include "exit.h"
24
25#include "set-hooks.h"
26DEFINE_HOOK (__libc_atexit, (void))
27
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. */
31bool __exit_funcs_done = false;
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. */
36void
37attribute_hidden
38__run_exit_handlers (int status, struct exit_function_list **listp,
39 bool run_list_atexit, bool run_dtors)
40{
41 /* First, call the TLS destructors. */
42#ifndef SHARED
43 if (&__call_tls_dtors != NULL)
44#endif
45 if (run_dtors)
46 __call_tls_dtors ();
47
48 __libc_lock_lock (__exit_funcs_lock);
49
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 (). */
54 while (true)
55 {
56 struct exit_function_list *cur;
57
58 restart:
59 cur = *listp;
60
61 if (cur == NULL)
62 {
63 /* Exit processing complete. We will not allow any more
64 atexit/on_exit registrations. */
65 __exit_funcs_done = true;
66 break;
67 }
68
69 while (cur->idx > 0)
70 {
71 struct exit_function *const f = &cur->fns[--cur->idx];
72 const uint64_t new_exitfn_called = __new_exitfn_called;
73
74 switch (f->flavor)
75 {
76 void (*atfct) (void);
77 void (*onfct) (int status, void *arg);
78 void (*cxafct) (void *arg, int status);
79 void *arg;
80
81 case ef_free:
82 case ef_us:
83 break;
84 case ef_on:
85 onfct = f->func.on.fn;
86 arg = f->func.on.arg;
87#ifdef PTR_DEMANGLE
88 PTR_DEMANGLE (onfct);
89#endif
90 /* Unlock the list while we call a foreign function. */
91 __libc_lock_unlock (__exit_funcs_lock);
92 onfct (status, arg);
93 __libc_lock_lock (__exit_funcs_lock);
94 break;
95 case ef_at:
96 atfct = f->func.at;
97#ifdef PTR_DEMANGLE
98 PTR_DEMANGLE (atfct);
99#endif
100 /* Unlock the list while we call a foreign function. */
101 __libc_lock_unlock (__exit_funcs_lock);
102 atfct ();
103 __libc_lock_lock (__exit_funcs_lock);
104 break;
105 case ef_cxa:
106 /* To avoid dlclose/exit race calling cxafct twice (BZ 22180),
107 we must mark this function as ef_free. */
108 f->flavor = ef_free;
109 cxafct = f->func.cxa.fn;
110 arg = f->func.cxa.arg;
111#ifdef PTR_DEMANGLE
112 PTR_DEMANGLE (cxafct);
113#endif
114 /* Unlock the list while we call a foreign function. */
115 __libc_lock_unlock (__exit_funcs_lock);
116 cxafct (arg, status);
117 __libc_lock_lock (__exit_funcs_lock);
118 break;
119 }
120
121 if (__glibc_unlikely (new_exitfn_called != __new_exitfn_called))
122 /* The last exit function, or another thread, has registered
123 more exit functions. Start the loop over. */
124 goto restart;
125 }
126
127 *listp = cur->next;
128 if (*listp != NULL)
129 /* Don't free the last element in the chain, this is the statically
130 allocate element. */
131 free (ptr: cur);
132 }
133
134 __libc_lock_unlock (__exit_funcs_lock);
135
136 if (run_list_atexit)
137 RUN_HOOK (__libc_atexit, ());
138
139 _exit (status);
140}
141
142
143void
144exit (int status)
145{
146 __run_exit_handlers (status, listp: &__exit_funcs, true, true);
147}
148libc_hidden_def (exit)
149

source code of glibc/stdlib/exit.c