]> git.ipfire.org Git - thirdparty/glibc.git/blame - stdlib/exit.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / stdlib / exit.c
CommitLineData
04277e02 1/* Copyright (C) 1991-2019 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
PE
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
28f540f4 17
28f540f4
RM
18#include <stdio.h>
19#include <stdlib.h>
20#include <unistd.h>
a3c88553 21#include <sysdep.h>
26e70aec 22#include <libc-lock.h>
28f540f4
RM
23#include "exit.h"
24
28f540f4
RM
25#include "set-hooks.h"
26DEFINE_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. */
31bool __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. */
36void
610e67ed
UD
37attribute_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
cc3fa755
UD
48 /* We do it this way to handle recursive calls to exit () made by
49 the functions registered with `atexit' and `on_exit'. We call
50 everyone on the list and use the status value in the last
51 exit (). */
26e70aec 52 while (true)
28f540f4 53 {
26e70aec
PP
54 struct exit_function_list *cur;
55
56 __libc_lock_lock (__exit_funcs_lock);
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 __libc_lock_unlock (__exit_funcs_lock);
67 break;
68 }
c4563d2d 69
610e67ed 70 while (cur->idx > 0)
28f540f4 71 {
e5e4d7cc 72 struct exit_function *const f = &cur->fns[--cur->idx];
26e70aec
PP
73 const uint64_t new_exitfn_called = __new_exitfn_called;
74
75 /* Unlock the list while we call a foreign function. */
76 __libc_lock_unlock (__exit_funcs_lock);
28f540f4
RM
77 switch (f->flavor)
78 {
a3c88553
UD
79 void (*atfct) (void);
80 void (*onfct) (int status, void *arg);
81 void (*cxafct) (void *arg, int status);
82
28f540f4 83 case ef_free:
ba1ffaa1 84 case ef_us:
28f540f4
RM
85 break;
86 case ef_on:
a3c88553
UD
87 onfct = f->func.on.fn;
88#ifdef PTR_DEMANGLE
89 PTR_DEMANGLE (onfct);
90#endif
91 onfct (status, f->func.on.arg);
28f540f4
RM
92 break;
93 case ef_at:
a3c88553
UD
94 atfct = f->func.at;
95#ifdef PTR_DEMANGLE
96 PTR_DEMANGLE (atfct);
97#endif
98 atfct ();
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
UD
104 cxafct = f->func.cxa.fn;
105#ifdef PTR_DEMANGLE
106 PTR_DEMANGLE (cxafct);
107#endif
108 cxafct (f->func.cxa.arg, status);
3bbddbe4 109 break;
28f540f4 110 }
26e70aec
PP
111 /* Re-lock again before looking at global state. */
112 __libc_lock_lock (__exit_funcs_lock);
113
114 if (__glibc_unlikely (new_exitfn_called != __new_exitfn_called))
115 /* The last exit function, or another thread, has registered
116 more exit functions. Start the loop over. */
117 goto restart;
28f540f4 118 }
c4563d2d 119
610e67ed
UD
120 *listp = cur->next;
121 if (*listp != NULL)
bca9f4ab
UD
122 /* Don't free the last element in the chain, this is the statically
123 allocate element. */
610e67ed 124 free (cur);
26e70aec
PP
125
126 __libc_lock_unlock (__exit_funcs_lock);
28f540f4
RM
127 }
128
610e67ed
UD
129 if (run_list_atexit)
130 RUN_HOOK (__libc_atexit, ());
28f540f4 131
ba1ffaa1 132 _exit (status);
28f540f4 133}
610e67ed
UD
134
135
136void
137exit (int status)
138{
47dd3543 139 __run_exit_handlers (status, &__exit_funcs, true, true);
610e67ed 140}
a757607d 141libc_hidden_def (exit)