1 /*
2 * nghttp2 - HTTP/2 C Library
3 *
4 * Copyright (c) 2015 Tatsuhiro Tsujikawa
5 *
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:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
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.
24 */
25 #include "shrpx_signal.h"
26
27 #include <cerrno>
28
29 #include "shrpx_log.h"
30 #include "template.h"
31
32 using namespace nghttp2;
33
34 namespace shrpx {
35
shrpx_signal_block_all(sigset_t * oldset)36 int shrpx_signal_block_all(sigset_t *oldset) {
37 sigset_t newset;
38
39 sigfillset(&newset);
40
41 #ifndef NOTHREADS
42 int rv;
43
44 rv = pthread_sigmask(SIG_SETMASK, &newset, oldset);
45
46 if (rv != 0) {
47 errno = rv;
48 return -1;
49 }
50
51 return 0;
52 #else // NOTHREADS
53 return sigprocmask(SIG_SETMASK, &newset, oldset);
54 #endif // NOTHREADS
55 }
56
shrpx_signal_unblock_all()57 int shrpx_signal_unblock_all() {
58 sigset_t newset;
59
60 sigemptyset(&newset);
61
62 #ifndef NOTHREADS
63 int rv;
64
65 rv = pthread_sigmask(SIG_SETMASK, &newset, nullptr);
66
67 if (rv != 0) {
68 errno = rv;
69 return -1;
70 }
71
72 return 0;
73 #else // NOTHREADS
74 return sigprocmask(SIG_SETMASK, &newset, nullptr);
75 #endif // NOTHREADS
76 }
77
shrpx_signal_set(sigset_t * set)78 int shrpx_signal_set(sigset_t *set) {
79 #ifndef NOTHREADS
80 int rv;
81
82 rv = pthread_sigmask(SIG_SETMASK, set, nullptr);
83
84 if (rv != 0) {
85 errno = rv;
86 return -1;
87 }
88
89 return 0;
90 #else // NOTHREADS
91 return sigprocmask(SIG_SETMASK, set, nullptr);
92 #endif // NOTHREADS
93 }
94
95 namespace {
96 template <typename Signals>
signal_set_handler(void (* handler)(int),Signals && sigs)97 int signal_set_handler(void (*handler)(int), Signals &&sigs) {
98 struct sigaction act {};
99 act.sa_handler = handler;
100 sigemptyset(&act.sa_mask);
101 int rv;
102 for (auto sig : sigs) {
103 rv = sigaction(sig, &act, nullptr);
104 if (rv != 0) {
105 return -1;
106 }
107 }
108 return 0;
109 }
110 } // namespace
111
112 namespace {
113 constexpr auto main_proc_ign_signals = std::array<int, 1>{SIGPIPE};
114 } // namespace
115
116 namespace {
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};
120 } // namespace
121
shrpx_signal_set_main_proc_ign_handler()122 int shrpx_signal_set_main_proc_ign_handler() {
123 return signal_set_handler(SIG_IGN, main_proc_ign_signals);
124 }
125
shrpx_signal_unset_main_proc_ign_handler()126 int shrpx_signal_unset_main_proc_ign_handler() {
127 return signal_set_handler(SIG_DFL, main_proc_ign_signals);
128 }
129
shrpx_signal_set_worker_proc_ign_handler()130 int shrpx_signal_set_worker_proc_ign_handler() {
131 return signal_set_handler(SIG_IGN, worker_proc_ign_signals);
132 }
133
shrpx_signal_unset_worker_proc_ign_handler()134 int shrpx_signal_unset_worker_proc_ign_handler() {
135 return signal_set_handler(SIG_DFL, worker_proc_ign_signals);
136 }
137
138 } // namespace shrpx
139