1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
/*
* Copyright (C) Igor Sysoev
* Copyright (C) NGINX, Inc.
*/
#include <nxt_main.h>
/*
* Signals are handled only via a main thread event engine work queue.
* There are three ways to route signals to the work queue:
*
* 1) Using signal event notifications if an event facility supports it:
* kqueue and epoll/signalfd. This method is used regardless of thread mode.
*
* 2) Multi-threaded mode: a dedicated signal thread which waits in sigwait()
* and post a signal number to the main thread event engine.
*
* 3) Single-threaded mode: a signal handler which posts a signal number
* to the event engine.
*/
static nxt_int_t nxt_signal_action(int signo, void (*handler)(int));
nxt_event_signals_t *
nxt_event_engine_signals(const nxt_sig_event_t *sigev)
{
nxt_event_signals_t *signals;
signals = nxt_zalloc(sizeof(nxt_event_signals_t));
if (signals == NULL) {
return NULL;
}
signals->sigev = sigev;
if (nxt_signal_action(SIGSYS, SIG_IGN) != NXT_OK) {
goto fail;
}
if (nxt_signal_action(SIGPIPE, SIG_IGN) != NXT_OK) {
goto fail;
}
sigemptyset(&signals->sigmask);
while (sigev->signo != 0) {
sigaddset(&signals->sigmask, sigev->signo);
sigev++;
}
if (sigprocmask(SIG_BLOCK, &signals->sigmask, NULL) != 0) {
nxt_main_log_alert("sigprocmask(SIG_BLOCK) failed %E", nxt_errno);
goto fail;
}
return signals;
fail:
nxt_free(signals);
return NULL;
}
static nxt_int_t
nxt_signal_action(int signo, void (*handler)(int))
{
struct sigaction sa;
nxt_memzero(&sa, sizeof(struct sigaction));
sigemptyset(&sa.sa_mask);
sa.sa_handler = handler;
if (sigaction(signo, &sa, NULL) == 0) {
return NXT_OK;
}
nxt_main_log_alert("sigaction(%d) failed %E", signo, nxt_errno);
return NXT_ERROR;
}
static void
nxt_signal_handler(int signo)
{
nxt_thread_t *thr;
thr = nxt_thread();
/* Thread is running in a single context now. */
thr->time.signal++;
nxt_thread_time_update(thr);
nxt_main_log_error(NXT_LOG_INFO, "signal handler: %d", signo);
nxt_event_engine_signal(thr->engine, signo);
thr->time.signal--;
}
#if (NXT_THREADS)
static void nxt_signal_thread(void *data);
nxt_int_t
nxt_signal_thread_start(nxt_event_engine_t *engine)
{
nxt_thread_link_t *link;
const nxt_sig_event_t *sigev;
if (engine->signals->process == nxt_pid) {
return NXT_OK;
}
if (sigprocmask(SIG_BLOCK, &engine->signals->sigmask, NULL) != 0) {
nxt_main_log_alert("sigprocmask(SIG_BLOCK) failed %E", nxt_errno);
return NXT_ERROR;
}
/*
* kqueue sets signal handlers to SIG_IGN and sigwait() ignores
* them after the switch of event facility from "kqueue" to "select".
*/
for (sigev = engine->signals->sigev; sigev->signo != 0; sigev++) {
if (nxt_signal_action(sigev->signo, nxt_signal_handler) != NXT_OK) {
return NXT_ERROR;
}
}
link = nxt_zalloc(sizeof(nxt_thread_link_t));
if (nxt_fast_path(link != NULL)) {
link->start = nxt_signal_thread;
link->data = engine;
if (nxt_thread_create(&engine->signals->thread, link) == NXT_OK) {
engine->signals->process = nxt_pid;
return NXT_OK;
}
}
return NXT_ERROR;
}
static void
nxt_signal_thread(void *data)
{
int signo;
nxt_err_t err;
nxt_thread_t *thr;
nxt_event_engine_t *engine;
engine = data;
thr = nxt_thread();
nxt_main_log_debug("signal thread");
for ( ;; ) {
err = sigwait(&engine->signals->sigmask, &signo);
nxt_thread_time_update(thr);
if (nxt_fast_path(err == 0)) {
nxt_main_log_error(NXT_LOG_INFO, "signo: %d", signo);
nxt_event_engine_signal(engine, signo);
} else {
nxt_main_log_alert("sigwait() failed %E", err);
}
}
}
void
nxt_signal_thread_stop(nxt_event_engine_t *engine)
{
nxt_thread_handle_t thread;
thread = engine->signals->thread;
nxt_thread_cancel(thread);
nxt_thread_wait(thread);
}
#else /* !(NXT_THREADS) */
nxt_int_t
nxt_signal_handlers_start(nxt_event_engine_t *engine)
{
const nxt_sig_event_t *sigev;
for (sigev = engine->signals->sigev; sigev->signo != 0; sigev++) {
if (nxt_signal_action(sigev->signo, nxt_signal_handler) != NXT_OK) {
return NXT_ERROR;
}
}
if (sigprocmask(SIG_UNBLOCK, &engine->signals->sigmask, NULL) != 0) {
nxt_main_log_alert("sigprocmask(SIG_UNBLOCK) failed %E", nxt_errno);
return NXT_ERROR;
}
return NXT_OK;
}
void
nxt_signal_handlers_stop(nxt_event_engine_t *engine)
{
if (sigprocmask(SIG_BLOCK, &engine->signals->sigmask, NULL) != 0) {
nxt_main_log_alert("sigprocmask(SIG_BLOCK) failed %E", nxt_errno);
}
}
#endif
|