MySQL 8.1.0
Source Code Documentation
signal_handler.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2022, 2023, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is also distributed with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have included with MySQL.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23*/
24
25#ifndef MYSQL_HARNESS_SIGNAL_HANDLER_INCLUDED
26#define MYSQL_HARNESS_SIGNAL_HANDLER_INCLUDED
27
28#include "harness_export.h"
29
30#include <array>
31#include <csignal>
32#include <functional>
33#include <map>
34#include <thread>
35
37
38namespace mysql_harness {
39
40class HARNESS_EXPORT SignalHandler {
41 public:
42 // mimick the MYSQLD_RESTART_EXIT values from sql/sql_const.h
43 static constexpr const int HARNESS_SUCCESS_EXIT{0};
44 static constexpr const int HARNESS_ABORT_EXIT{1};
45 static constexpr const int HARNESS_FAILURE_EXIT{2};
46 static constexpr const int HARNESS_RESTART_EXIT{16};
47
48#ifndef _WIN32
49 static constexpr const std::array kFatalSignals{SIGSEGV, SIGABRT, SIGBUS,
50 SIGILL, SIGFPE, SIGTRAP};
51
52 static constexpr const std::array kIgnoredSignals{SIGPIPE};
53#endif
54 SignalHandler() = default;
55
57
58 void register_ignored_signals_handler();
59
60 void block_all_nonfatal_signals();
61
62 /**
63 * register a handler for fatal signals.
64 *
65 * @param dump_core dump core of fatal signal
66 */
67 void register_fatal_signal_handler(bool dump_core);
68
69#ifdef _WIN32
70 // register Ctrl-C handler.
71 void register_ctrl_c_handler();
72
73 // unregister Ctrl-C handler.
74 void unregister_ctrl_c_handler();
75#endif
76
77 /**
78 * add signal handler for a signal
79 *
80 * @param signum signal number
81 * @param f signal handler
82 */
83 void add_sig_handler(int signum, std::function<void(int)> f) {
84 sig_handlers_([signum, f](auto &handlers) {
85 handlers.emplace(signum, std::move(f));
86 });
87 }
88
89 void remove_sig_handler(int signum) {
90 sig_handlers_([signum](auto &handlers) { handlers.erase(signum); });
91 }
92
93 void spawn_signal_handler_thread();
94
95 private:
96 // signal handlers per signal number.
97 Monitor<std::map<int, std::function<void(int)>>> sig_handlers_{{}};
98
99 WaitableMonitor<bool> signal_thread_ready_{false};
100
101 // sigwait thread
102 std::thread signal_thread_;
103};
104
105} // namespace mysql_harness
106#endif
Monitor pattern.
Definition: monitor.h:38
Monitor can be waited for.
Definition: monitor.h:61
Definition: signal_handler.h:40
void remove_sig_handler(int signum)
Definition: signal_handler.h:89
std::thread signal_thread_
Definition: signal_handler.h:102
void add_sig_handler(int signum, std::function< void(int)> f)
add signal handler for a signal
Definition: signal_handler.h:83
Definition: common.h:41
std::map< Key, Value, Compare, ut::allocator< std::pair< const Key, Value > > > map
Specialization of map which uses ut_allocator.
Definition: ut0new.h:2891