-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy patherror_handling.cpp
More file actions
179 lines (150 loc) · 3.65 KB
/
Copy patherror_handling.cpp
File metadata and controls
179 lines (150 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "error_handling.h"
ErrorState *ErrorStateRegistry::custom_error_state_ = nullptr;
int &get_status_code()
{
return ErrorStateRegistry::get_error_state().status_code;
}
std::string &get_status_message()
{
return ErrorStateRegistry::get_error_state().status_message;
}
sigjmp_buf &get_sigjmp_buf_point()
{
static sigjmp_buf sigjmp_buf_point;
return sigjmp_buf_point;
}
std::mutex &get_sigint_hook_mutex()
{
static std::mutex sigint_hook_mutex;
return sigint_hook_mutex;
}
std::vector<Hook> &get_sigint_hooks()
{
static std::vector<Hook> sigint_hooks;
return sigint_hooks;
}
void fail(std::string message, int code)
{
ErrorState &error_state = ErrorStateRegistry::get_error_state();
error_state.status_code = code;
error_state.status_message = message;
}
void handle_exception(int code)
{
try
{
throw;
}
catch (const std::exception &ex)
{
fail(ex.what(), code);
}
catch (...)
{
fail("Caught unknown exception", code);
}
}
sigjmp_buf &get_jump_point()
{
sigjmp_buf &sigjmp_buf_point = get_sigjmp_buf_point();
fail("", 0);
return sigjmp_buf_point;
}
namespace {
thread_local sigjmp_buf* current_jump_point = nullptr;
}
sigjmp_buf* get_current_jump_point_ptr()
{
return current_jump_point;
}
void set_current_jump_point(sigjmp_buf* jump_point)
{
current_jump_point = jump_point;
}
static void handle_terminate()
{
crash_signal_handler(1);
}
#ifdef _WIN32
BOOL WINAPI console_ctrl_handler(DWORD ctrl_type)
{
if (ctrl_type == CTRL_C_EVENT || ctrl_type == CTRL_CLOSE_EVENT)
{
sigint_signal_handler(SIGINT);
return TRUE;
}
return FALSE;
}
void set_error_handlers(bool crash_handlers, bool sigint_handlers)
{
ErrorStateRegistry::get_error_state();
if (crash_handlers)
{
signal(SIGSEGV, crash_signal_handler);
signal(SIGFPE, crash_signal_handler);
std::set_terminate(handle_terminate);
}
if (sigint_handlers)
{
signal(SIGINT, sigint_signal_handler);
signal(SIGTERM, sigint_signal_handler);
SetConsoleCtrlHandler(console_ctrl_handler, TRUE);
}
}
#else
void handle_signal(int sig, siginfo_t *, void *)
{
crash_signal_handler(sig);
}
void set_error_handlers(bool crash_handlers, bool sigint_handlers)
{
ErrorStateRegistry::get_error_state();
if (crash_handlers)
{
struct sigaction sa{};
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_NODEFER;
sa.sa_sigaction = handle_signal;
sigaction(SIGSEGV, &sa, nullptr);
sigaction(SIGFPE, &sa, nullptr);
std::set_terminate(handle_terminate);
}
if (sigint_handlers)
{
struct sigaction shutdown{};
shutdown.sa_handler = sigint_signal_handler;
sigemptyset(&shutdown.sa_mask);
shutdown.sa_flags = 0;
sigaction(SIGINT, &shutdown, nullptr);
sigaction(SIGTERM, &shutdown, nullptr);
}
}
#endif
void crash_signal_handler(int sig)
{
fail("Severe error occurred", sig);
sigjmp_buf* jump_point = get_current_jump_point_ptr();
if (jump_point) siglongjmp(*jump_point, 1);
std::abort();
}
void sigint_signal_handler(int sig)
{
std::vector<Hook> &sigint_hooks = get_sigint_hooks();
std::lock_guard<std::mutex> lock(get_sigint_hook_mutex());
for (auto &hook : sigint_hooks)
{
try
{
hook(sig);
}
catch (...)
{
}
}
}
void register_sigint_hook(Hook hook)
{
std::vector<Hook> &sigint_hooks = get_sigint_hooks();
std::lock_guard<std::mutex> lock(get_sigint_hook_mutex());
sigint_hooks.push_back(std::move(hook));
}