Fix lots of bitrot for stub configurations.
[glibc.git] / stdlib / exit.c
blob1ad548f7a85bb4f81e43dab3abf9c468ef5306d2
1 /* Copyright (C) 1991,95,96,97,99,2001,2002,2005,2009
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <sysdep.h>
23 #include "exit.h"
25 #include "set-hooks.h"
26 DEFINE_HOOK (__libc_atexit, (void))
29 /* Call all functions registered with `atexit' and `on_exit',
30 in the reverse of the order in which they were registered
31 perform stdio cleanup, and terminate program execution with STATUS. */
32 void
33 attribute_hidden
34 __run_exit_handlers (int status, struct exit_function_list **listp,
35 bool run_list_atexit)
37 /* We do it this way to handle recursive calls to exit () made by
38 the functions registered with `atexit' and `on_exit'. We call
39 everyone on the list and use the status value in the last
40 exit (). */
41 while (*listp != NULL)
43 struct exit_function_list *cur = *listp;
45 while (cur->idx > 0)
47 const struct exit_function *const f =
48 &cur->fns[--cur->idx];
49 switch (f->flavor)
51 void (*atfct) (void);
52 void (*onfct) (int status, void *arg);
53 void (*cxafct) (void *arg, int status);
55 case ef_free:
56 case ef_us:
57 break;
58 case ef_on:
59 onfct = f->func.on.fn;
60 #ifdef PTR_DEMANGLE
61 PTR_DEMANGLE (onfct);
62 #endif
63 onfct (status, f->func.on.arg);
64 break;
65 case ef_at:
66 atfct = f->func.at;
67 #ifdef PTR_DEMANGLE
68 PTR_DEMANGLE (atfct);
69 #endif
70 atfct ();
71 break;
72 case ef_cxa:
73 cxafct = f->func.cxa.fn;
74 #ifdef PTR_DEMANGLE
75 PTR_DEMANGLE (cxafct);
76 #endif
77 cxafct (f->func.cxa.arg, status);
78 break;
82 *listp = cur->next;
83 if (*listp != NULL)
84 /* Don't free the last element in the chain, this is the statically
85 allocate element. */
86 free (cur);
89 if (run_list_atexit)
90 RUN_HOOK (__libc_atexit, ());
92 _exit (status);
96 void
97 exit (int status)
99 __run_exit_handlers (status, &__exit_funcs, true);
101 libc_hidden_def (exit)