]> git.sesse.net Git - casparcg/blob - common/os/linux/signal_handlers.cpp
457c674b8f5d9ad5dcaccb2c05e6d10166f39092
[casparcg] / common / os / linux / signal_handlers.cpp
1 #include "../general_protection_fault.h"
2
3 #include "../../except.h"
4 #include "../../log.h"
5
6 #include <signal.h>
7
8 namespace caspar {
9
10 struct floating_point_exception : virtual caspar_exception {};
11 struct segmentation_fault_exception : virtual caspar_exception {};
12
13 void catch_fpe(int signum)
14 {
15         try
16         {
17                 CASPAR_THROW_EXCEPTION(floating_point_exception());
18         }
19         catch (...)
20         {
21                 CASPAR_LOG_CURRENT_EXCEPTION();
22                 throw;
23         }
24 }
25
26 void catch_segv(int signum)
27 {
28         try
29         {
30                 CASPAR_THROW_EXCEPTION(segmentation_fault_exception());
31         }
32         catch (...)
33         {
34                 CASPAR_LOG_CURRENT_EXCEPTION();
35                 throw;
36         }
37 }
38
39 void do_install_handlers()
40 {
41         signal(SIGFPE, catch_fpe);
42         signal(SIGSEGV, catch_segv);
43 }
44
45 void install_gpf_handler()
46 {
47         ensure_gpf_handler_installed_for_thread();
48 }
49
50 void ensure_gpf_handler_installed_for_thread(
51                 const char* thread_description)
52 {
53         static auto install = []() { do_install_handlers(); return 0; } ();
54 }
55
56 }