1 // Userspace emulation of `raise` and `signal`.
3 // WebAssembly doesn't support asynchronous signal delivery, so we can't
4 // support it in WASI libc. But we can make things like `raise` work.
6 #define _WASI_EMULATED_SIGNAL
16 void __SIG_IGN(int sig
) {
21 void __SIG_ERR(int sig
) {
26 static void core_handler(int sig
) {
27 fprintf(stderr
, "Program received fatal signal: %s\n", strsignal(sig
));
32 static void terminate_handler(int sig
) {
33 fprintf(stderr
, "Program received termination signal: %s\n", strsignal(sig
));
38 static void stop_handler(int sig
) {
39 fprintf(stderr
, "Program received stop signal: %s\n", strsignal(sig
));
43 static void continue_handler(int sig
) {
47 static const sighandler_t default_handlers
[_NSIG
] = {
48 // Default behavior: "core".
49 [SIGABRT
] = core_handler
,
50 [SIGBUS
] = core_handler
,
51 [SIGFPE
] = core_handler
,
52 [SIGILL
] = core_handler
,
54 [SIGIOT
] = core_handler
,
56 [SIGQUIT
] = core_handler
,
57 [SIGSEGV
] = core_handler
,
58 [SIGSYS
] = core_handler
,
59 [SIGTRAP
] = core_handler
,
60 [SIGXCPU
] = core_handler
,
61 [SIGXFSZ
] = core_handler
,
62 #if defined(SIGUNUSED) && SIGUNUSED != SIGSYS
63 [SIGUNUSED
] = core_handler
,
66 // Default behavior: ignore.
68 #if defined(SIGCLD) && SIGCLD != SIGCHLD
74 // Default behavior: "continue".
75 [SIGCONT
] = continue_handler
,
77 // Default behavior: "stop".
78 [SIGSTOP
] = stop_handler
,
79 [SIGTSTP
] = stop_handler
,
80 [SIGTTIN
] = stop_handler
,
81 [SIGTTOU
] = stop_handler
,
83 // Default behavior: "terminate".
84 [SIGHUP
] = terminate_handler
,
85 [SIGINT
] = terminate_handler
,
86 [SIGKILL
] = terminate_handler
,
87 [SIGUSR1
] = terminate_handler
,
88 [SIGUSR2
] = terminate_handler
,
89 [SIGPIPE
] = terminate_handler
,
90 [SIGALRM
] = terminate_handler
,
91 [SIGTERM
] = terminate_handler
,
92 [SIGSTKFLT
] = terminate_handler
,
93 [SIGVTALRM
] = terminate_handler
,
94 [SIGPROF
] = terminate_handler
,
95 [SIGIO
] = terminate_handler
,
97 [SIGPOLL
] = terminate_handler
,
99 [SIGPWR
] = terminate_handler
,
102 static sighandler_t handlers
[_NSIG
];
105 if (sig
< 0 || sig
>= _NSIG
) {
110 sighandler_t func
= handlers
[sig
];
113 default_handlers
[sig
](sig
);
121 void (*signal(int sig
, void (*func
)(int)))(int) {
122 assert(SIG_DFL
== NULL
);
124 if (sig
< 0 || sig
>= _NSIG
) {
129 if (sig
== SIGKILL
|| sig
== SIGSTOP
) {
134 sighandler_t old
= handlers
[sig
];
136 handlers
[sig
] = func
;
141 extern __typeof(signal
) bsd_signal
__attribute__((weak
, alias("signal")));
142 extern __typeof(signal
) __sysv_signal
__attribute__((weak
, alias("signal")));