Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/system/lib/libc/raise.c
6162 views
1
/*
2
* Copyright 2021 The Emscripten Authors. All rights reserved.
3
* Emscripten is available under two separate licenses, the MIT license and the
4
* University of Illinois/NCSA Open Source License. Both these licenses can be
5
* found in the LICENSE file.
6
*/
7
8
#define _GNU_SOURCE // for sighandler_t
9
#include <stdbool.h>
10
#include <stddef.h>
11
#include <signal.h>
12
13
#include "emscripten_internal.h"
14
15
extern struct sigaction __sig_actions[_NSIG];
16
extern sigset_t __sig_pending;
17
18
bool __sig_is_blocked(int sig);
19
20
// Default handler actions ~auto-generated from https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html.
21
// Note that stop and continue actions are not supported and treated as ignored.
22
23
void action_abort(int sig) {
24
abort();
25
}
26
27
void action_terminate(int sig) {
28
// Prepare to forcibly shut down runtime even if it has async work in flight.
29
_emscripten_runtime_keepalive_clear();
30
// Intentionally exiting via a function that doesn't call atexit handlers.
31
_Exit(128 + sig);
32
}
33
34
static sighandler_t default_actions[_NSIG] = {
35
[SIGABRT] = action_abort,
36
[SIGALRM] = action_terminate,
37
[SIGBUS] = action_abort,
38
[SIGFPE] = action_abort,
39
[SIGHUP] = action_terminate,
40
[SIGILL] = action_abort,
41
[SIGINT] = action_terminate,
42
[SIGKILL] = action_terminate,
43
[SIGPIPE] = action_terminate,
44
[SIGQUIT] = action_abort,
45
[SIGSEGV] = action_abort,
46
[SIGTERM] = action_terminate,
47
[SIGUSR1] = action_terminate,
48
[SIGUSR2] = action_terminate,
49
[SIGPOLL] = action_terminate,
50
[SIGPROF] = action_terminate,
51
[SIGSYS] = action_abort,
52
[SIGTRAP] = action_abort,
53
[SIGVTALRM] = action_terminate,
54
[SIGXCPU] = action_abort,
55
[SIGXFSZ] = action_abort,
56
};
57
58
int raise(int sig) {
59
if (__sig_is_blocked(sig)) {
60
sigaddset(&__sig_pending, sig);
61
return 0;
62
}
63
if (__sig_actions[sig].sa_flags & SA_SIGINFO) {
64
siginfo_t t = {0};
65
__sig_actions[sig].sa_sigaction(sig, &t, NULL);
66
} else {
67
sighandler_t handler = __sig_actions[sig].sa_handler;
68
if (handler == SIG_DFL) {
69
handler = default_actions[sig];
70
if (handler) {
71
handler(sig);
72
}
73
} else if (handler != SIG_IGN) {
74
// Avoid a direct call to the handler, and instead call via JS so we can
75
// avoid strict signature checking.
76
// https://github.com/emscripten-core/posixtestsuite/issues/6
77
__call_sighandler(handler, sig);
78
}
79
}
80
return 0;
81
}
82
83