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