Line data Source code
1 : /*
2 : * (C) Copyright 2010 Marek Dopiera
3 : *
4 : * This file is part of CoherentDB.
5 : *
6 : * CoherentDB is free software: you can redistribute it and/or modify it
7 : * under the terms of the GNU General Public License as published by
8 : * the Free Software Foundation, either version 3 of the License, or
9 : * (at your option) any later version.
10 : *
11 : * CoherentDB is distributed in the hope that it will be useful, but
12 : * WITHOUT ANY WARRANTY; without even the implied warranty of
13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : * General Public License for more details.
15 : *
16 : * You should have received a copy of the GNU General Public
17 : * License along with CoherentDB. If not, see
18 : * http://www.gnu.org/licenses/.
19 : */
20 :
21 : #include <errno.h>
22 : #include <signal.h>
23 :
24 : #include <exception>
25 : #include <iostream>
26 : #include <cstring>
27 :
28 : #include <debug/common.h>
29 : #include <log/log.h>
30 :
31 : using namespace std;
32 :
33 : namespace coherent {
34 : namespace debug {
35 :
36 : static int const affected_signals[] = { SIGHUP, SIGILL, SIGINT, SIGQUIT,
37 : SIGFPE, SIGSEGV, SIGPIPE, SIGALRM, SIGTERM, SIGUSR1, SIGUSR2, SIGBUS,
38 : SIGPROF, SIGSYS, SIGTRAP, SIGVTALRM, SIGXCPU, SIGXFSZ };
39 : static unsigned const num_signals = sizeof(affected_signals) / sizeof(int);
40 :
41 0 : static void unexpected_hndlr()
42 : {
43 : r_assert(false, "An unexpected excpetion has been thrown.");
44 : }
45 :
46 0 : static void terminate_hndlr()
47 : {
48 : r_assert(false, "Terminate called.");
49 : }
50 :
51 0 : static void signal_hndlr(int sig)
52 : {
53 : r_assert(false, "Unexpected signal " << sig << " has been received");
54 : }
55 :
56 7 : void set_terminate_handler()
57 : {
58 7 : std::set_terminate(terminate_hndlr);
59 7 : std::set_unexpected(unexpected_hndlr);
60 7 : if (!(&(*log::log_FATAL))) {
61 : cerr <<
62 0 : "Log system should be initialized before setting terminate handlers."
63 0 : << endl;
64 0 : ::abort();
65 : }
66 133 : for (unsigned i = 0; i < num_signals; ++i) {
67 : struct sigaction sa;
68 126 : memset(&sa, 0, sizeof(struct sigaction));
69 126 : sa.sa_handler = signal_hndlr;
70 126 : sa.sa_flags = SA_RESETHAND | SA_NODEFER;
71 126 : int err = sigaction(affected_signals[i], &sa, NULL);
72 126 : if (err != 0)
73 : LOG(WARN, "Failed to set signal handler for sig="
74 : <<affected_signals[i] << ", errno=" << errno);
75 : }
76 7 : }
77 :
78 : } // namespace debug
79 21 : } // namespace coherent
|