• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdlib.h>
2 #include <stdint.h>
3 #include <stdbool.h>
4 #include <debug.h>
5 #include <signal.h>
6 #include <atomic.h>
7 #include "libc.h"
8 
9 extern bool g_enable_check;
10 extern void mem_check_deinit(void);
11 extern void clean_recycle_list(bool clean_all);
12 
dummy()13 static void dummy()
14 {
15 }
16 
17 /* atexit.c and __stdio_exit.c override these. the latter is linked
18  * as a consequence of linking either __toread.c or __towrite.c. */
19 weak_alias(dummy, __funcs_on_exit);
20 weak_alias(dummy, __stdio_exit);
21 weak_alias(dummy, _fini);
22 
23 extern weak hidden void (*const __fini_array_start)(void), (*const __fini_array_end)(void);
24 
libc_exit_fini(void)25 static void libc_exit_fini(void)
26 {
27 	uintptr_t a = (uintptr_t)&__fini_array_end;
28 	for (; a>(uintptr_t)&__fini_array_start; a-=sizeof(void(*)()))
29 		(*(void (**)())(a-sizeof(void(*)())))();
30 	_fini();
31 }
32 
33 weak_alias(libc_exit_fini, __libc_exit_fini);
34 
exit(int code)35 _Noreturn void exit(int code)
36 {
37 	sigset_t set;
38 	if (a_cas(&libc.exit, 0, 1) != 0) {
39 		return;
40 	}
41 	__block_app_sigs(&set);
42 	if (g_enable_check) {
43 		check_leak();
44 		check_heap_integrity();
45 		mem_check_deinit();
46 		clean_recycle_list(true);
47 	}
48 	__funcs_on_exit();
49 	__libc_exit_fini();
50 	__stdio_exit();
51 	_Exit(code);
52 }
53