2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2015 Tatsuhiro Tsujikawa
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #include "shrpx_signal.h"
29 #include "shrpx_log.h"
32 using namespace nghttp2;
36 int shrpx_signal_block_all(sigset_t *oldset) {
44 rv = pthread_sigmask(SIG_SETMASK, &newset, oldset);
53 return sigprocmask(SIG_SETMASK, &newset, oldset);
57 int shrpx_signal_unblock_all() {
65 rv = pthread_sigmask(SIG_SETMASK, &newset, nullptr);
74 return sigprocmask(SIG_SETMASK, &newset, nullptr);
78 int shrpx_signal_set(sigset_t *set) {
82 rv = pthread_sigmask(SIG_SETMASK, set, nullptr);
91 return sigprocmask(SIG_SETMASK, set, nullptr);
96 template <typename Signals>
97 int signal_set_handler(void (*handler)(int), Signals &&sigs) {
98 struct sigaction act {};
99 act.sa_handler = handler;
100 sigemptyset(&act.sa_mask);
102 for (auto sig : sigs) {
103 rv = sigaction(sig, &act, nullptr);
113 constexpr auto main_proc_ign_signals = std::array<int, 1>{SIGPIPE};
117 constexpr auto worker_proc_ign_signals =
118 std::array<int, 5>{REOPEN_LOG_SIGNAL, EXEC_BINARY_SIGNAL,
119 GRACEFUL_SHUTDOWN_SIGNAL, RELOAD_SIGNAL, SIGPIPE};
122 int shrpx_signal_set_main_proc_ign_handler() {
123 return signal_set_handler(SIG_IGN, main_proc_ign_signals);
126 int shrpx_signal_unset_main_proc_ign_handler() {
127 return signal_set_handler(SIG_DFL, main_proc_ign_signals);
130 int shrpx_signal_set_worker_proc_ign_handler() {
131 return signal_set_handler(SIG_IGN, worker_proc_ign_signals);
134 int shrpx_signal_unset_worker_proc_ign_handler() {
135 return signal_set_handler(SIG_DFL, worker_proc_ign_signals);