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