0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_event.h>
|
|
10
|
|
11
|
18
|
12 #if (NGX_TEST_BUILD_RTSIG)
|
0
|
13
|
|
14 #define F_SETSIG 10
|
|
15 #define SIGRTMIN 33
|
|
16 #define si_fd __spare__[0]
|
|
17 #define KERN_RTSIGNR 30
|
|
18 #define KERN_RTSIGMAX 31
|
|
19
|
|
20 int sigtimedwait(const sigset_t *set, siginfo_t *info,
|
|
21 const struct timespec *timeout)
|
|
22 {
|
|
23 return -1;
|
|
24 }
|
|
25
|
|
26 int ngx_linux_rtsig_max;
|
|
27
|
|
28 #endif
|
|
29
|
|
30
|
|
31 typedef struct {
|
|
32 int signo;
|
|
33 ngx_int_t overflow_events;
|
|
34 ngx_int_t overflow_test;
|
|
35 ngx_int_t overflow_threshold;
|
|
36 } ngx_rtsig_conf_t;
|
|
37
|
|
38
|
|
39 extern ngx_event_module_t ngx_poll_module_ctx;
|
|
40
|
|
41 static ngx_int_t ngx_rtsig_init(ngx_cycle_t *cycle);
|
|
42 static void ngx_rtsig_done(ngx_cycle_t *cycle);
|
|
43 static ngx_int_t ngx_rtsig_add_connection(ngx_connection_t *c);
|
|
44 static ngx_int_t ngx_rtsig_del_connection(ngx_connection_t *c, u_int flags);
|
|
45 static ngx_int_t ngx_rtsig_process_events(ngx_cycle_t *cycle);
|
|
46 static ngx_int_t ngx_rtsig_process_overflow(ngx_cycle_t *cycle);
|
|
47
|
|
48 static void *ngx_rtsig_create_conf(ngx_cycle_t *cycle);
|
|
49 static char *ngx_rtsig_init_conf(ngx_cycle_t *cycle, void *conf);
|
|
50 static char *ngx_check_ngx_overflow_threshold_bounds(ngx_conf_t *cf,
|
48
|
51 void *post, void *data);
|
0
|
52
|
|
53
|
|
54 static sigset_t set;
|
|
55 static ngx_uint_t overflow, overflow_current;
|
|
56 static struct pollfd *overflow_list;
|
|
57
|
|
58
|
|
59 static ngx_str_t rtsig_name = ngx_string("rtsig");
|
|
60
|
|
61 static ngx_conf_num_bounds_t ngx_overflow_threshold_bounds = {
|
|
62 ngx_check_ngx_overflow_threshold_bounds, 2, 10
|
|
63 };
|
|
64
|
|
65
|
|
66 static ngx_command_t ngx_rtsig_commands[] = {
|
|
67
|
48
|
68 { ngx_string("rtsig_signo"),
|
|
69 NGX_EVENT_CONF|NGX_CONF_TAKE1,
|
|
70 ngx_conf_set_num_slot,
|
|
71 0,
|
|
72 offsetof(ngx_rtsig_conf_t, signo),
|
|
73 NULL },
|
0
|
74
|
48
|
75 { ngx_string("rtsig_overflow_events"),
|
|
76 NGX_EVENT_CONF|NGX_CONF_TAKE1,
|
|
77 ngx_conf_set_num_slot,
|
|
78 0,
|
|
79 offsetof(ngx_rtsig_conf_t, overflow_events),
|
|
80 NULL },
|
0
|
81
|
48
|
82 { ngx_string("rtsig_overflow_test"),
|
|
83 NGX_EVENT_CONF|NGX_CONF_TAKE1,
|
|
84 ngx_conf_set_num_slot,
|
|
85 0,
|
|
86 offsetof(ngx_rtsig_conf_t, overflow_test),
|
|
87 NULL },
|
0
|
88
|
48
|
89 { ngx_string("rtsig_overflow_threshold"),
|
|
90 NGX_EVENT_CONF|NGX_CONF_TAKE1,
|
|
91 ngx_conf_set_num_slot,
|
|
92 0,
|
|
93 offsetof(ngx_rtsig_conf_t, overflow_threshold),
|
|
94 &ngx_overflow_threshold_bounds },
|
0
|
95
|
48
|
96 ngx_null_command
|
0
|
97 };
|
|
98
|
|
99
|
|
100 ngx_event_module_t ngx_rtsig_module_ctx = {
|
|
101 &rtsig_name,
|
|
102 ngx_rtsig_create_conf, /* create configuration */
|
|
103 ngx_rtsig_init_conf, /* init configuration */
|
|
104
|
|
105 {
|
|
106 NULL, /* add an event */
|
|
107 NULL, /* delete an event */
|
|
108 NULL, /* enable an event */
|
|
109 NULL, /* disable an event */
|
|
110 ngx_rtsig_add_connection, /* add an connection */
|
|
111 ngx_rtsig_del_connection, /* delete an connection */
|
|
112 NULL, /* process the changes */
|
|
113 ngx_rtsig_process_events, /* process the events */
|
|
114 ngx_rtsig_init, /* init the events */
|
|
115 ngx_rtsig_done, /* done the events */
|
|
116 }
|
|
117
|
|
118 };
|
|
119
|
|
120 ngx_module_t ngx_rtsig_module = {
|
58
|
121 NGX_MODULE_V1,
|
0
|
122 &ngx_rtsig_module_ctx, /* module context */
|
|
123 ngx_rtsig_commands, /* module directives */
|
48
|
124 NGX_EVENT_MODULE, /* module type */
|
90
|
125 NULL, /* init master */
|
48
|
126 NULL, /* init module */
|
90
|
127 NULL, /* init process */
|
|
128 NULL, /* init thread */
|
|
129 NULL, /* exit thread */
|
|
130 NULL, /* exit process */
|
|
131 NULL, /* exit master */
|
|
132 NGX_MODULE_V1_PADDING
|
0
|
133 };
|
|
134
|
|
135
|
48
|
136 static ngx_int_t
|
|
137 ngx_rtsig_init(ngx_cycle_t *cycle)
|
0
|
138 {
|
|
139 ngx_rtsig_conf_t *rtscf;
|
|
140
|
|
141 rtscf = ngx_event_get_conf(cycle->conf_ctx, ngx_rtsig_module);
|
|
142
|
|
143 sigemptyset(&set);
|
|
144 sigaddset(&set, rtscf->signo);
|
|
145 sigaddset(&set, rtscf->signo + 1);
|
|
146 sigaddset(&set, SIGIO);
|
|
147
|
|
148 if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) {
|
|
149 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
|
150 "sigprocmask() failed");
|
|
151 return NGX_ERROR;
|
|
152 }
|
|
153
|
|
154 if (overflow_list) {
|
|
155 ngx_free(overflow_list);
|
|
156 }
|
|
157
|
|
158 overflow_list = ngx_alloc(sizeof(struct pollfd) * rtscf->overflow_events,
|
|
159 cycle->log);
|
|
160 if (overflow_list == NULL) {
|
|
161 return NGX_ERROR;
|
|
162 }
|
|
163
|
|
164 ngx_io = ngx_os_io;
|
|
165
|
|
166 ngx_event_actions = ngx_rtsig_module_ctx.actions;
|
|
167
|
4
|
168 ngx_event_flags = NGX_USE_RTSIG_EVENT|NGX_USE_GREEDY_EVENT;
|
0
|
169
|
|
170 return NGX_OK;
|
|
171 }
|
|
172
|
|
173
|
48
|
174 static void
|
|
175 ngx_rtsig_done(ngx_cycle_t *cycle)
|
0
|
176 {
|
2
|
177 ngx_free(overflow_list);
|
|
178
|
|
179 overflow_list = NULL;
|
0
|
180 }
|
|
181
|
|
182
|
48
|
183 static ngx_int_t
|
|
184 ngx_rtsig_add_connection(ngx_connection_t *c)
|
0
|
185 {
|
|
186 int signo;
|
|
187 ngx_rtsig_conf_t *rtscf;
|
|
188
|
|
189 if (c->read->accept && c->read->disabled) {
|
|
190
|
|
191 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
192 "rtsig enable connection: fd:%d", c->fd);
|
|
193
|
|
194 if (fcntl(c->fd, F_SETOWN, ngx_pid) == -1) {
|
|
195 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
|
|
196 "fcntl(F_SETOWN) failed");
|
|
197 return NGX_ERROR;
|
|
198 }
|
|
199
|
|
200 c->read->active = 1;
|
|
201 c->read->disabled = 0;
|
|
202 }
|
|
203
|
|
204 rtscf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_rtsig_module);
|
|
205
|
|
206 signo = rtscf->signo + c->read->instance;
|
|
207
|
|
208 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
209 "rtsig add connection: fd:%d signo:%d", c->fd, signo);
|
|
210
|
|
211 if (fcntl(c->fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC) == -1) {
|
|
212 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
|
|
213 "fcntl(O_RDWR|O_NONBLOCK|O_ASYNC) failed");
|
|
214 return NGX_ERROR;
|
|
215 }
|
|
216
|
|
217 if (fcntl(c->fd, F_SETSIG, signo) == -1) {
|
|
218 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
|
|
219 "fcntl(F_SETSIG) failed");
|
|
220 return NGX_ERROR;
|
|
221 }
|
|
222
|
|
223 if (fcntl(c->fd, F_SETOWN, ngx_pid) == -1) {
|
|
224 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
|
|
225 "fcntl(F_SETOWN) failed");
|
|
226 return NGX_ERROR;
|
|
227 }
|
|
228
|
18
|
229 #if (NGX_HAVE_ONESIGFD)
|
0
|
230 if (fcntl(c->fd, F_SETAUXFL, O_ONESIGFD) == -1) {
|
|
231 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
|
|
232 "fcntl(F_SETAUXFL) failed");
|
|
233 return NGX_ERROR;
|
|
234 }
|
|
235 #endif
|
|
236
|
|
237 c->read->active = 1;
|
|
238 c->write->active = 1;
|
|
239
|
|
240 return NGX_OK;
|
|
241 }
|
|
242
|
|
243
|
48
|
244 static ngx_int_t
|
|
245 ngx_rtsig_del_connection(ngx_connection_t *c, u_int flags)
|
0
|
246 {
|
|
247 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
248 "rtsig del connection: fd:%d", c->fd);
|
|
249
|
|
250 if ((flags & NGX_DISABLE_EVENT) && c->read->accept) {
|
|
251
|
|
252 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
253 "rtsig disable connection: fd:%d", c->fd);
|
|
254
|
|
255 c->read->active = 0;
|
|
256 c->read->disabled = 1;
|
|
257 return NGX_OK;
|
|
258 }
|
|
259
|
|
260 if (flags & NGX_CLOSE_EVENT) {
|
|
261 c->read->active = 0;
|
|
262 c->write->active = 0;
|
|
263 return NGX_OK;
|
|
264 }
|
|
265
|
|
266 if (fcntl(c->fd, F_SETFL, O_RDWR|O_NONBLOCK) == -1) {
|
|
267 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
|
|
268 "fcntl(O_RDWR|O_NONBLOCK) failed");
|
|
269 return NGX_ERROR;
|
|
270 }
|
|
271
|
|
272 c->read->active = 0;
|
|
273 c->write->active = 0;
|
|
274
|
|
275 return NGX_OK;
|
|
276 }
|
|
277
|
|
278
|
48
|
279 static ngx_int_t
|
|
280 ngx_rtsig_process_events(ngx_cycle_t *cycle)
|
0
|
281 {
|
|
282 int signo;
|
50
|
283 ngx_int_t instance;
|
0
|
284 ngx_uint_t expire;
|
|
285 ngx_msec_t timer;
|
|
286 ngx_err_t err;
|
|
287 siginfo_t si;
|
|
288 ngx_event_t *rev, *wev;
|
|
289 struct timeval tv;
|
|
290 struct timespec ts, *tp;
|
|
291 struct sigaction sa;
|
|
292 ngx_epoch_msec_t delta;
|
|
293 ngx_connection_t *c;
|
|
294 ngx_rtsig_conf_t *rtscf;
|
|
295
|
|
296 if (overflow) {
|
|
297 timer = 0;
|
|
298 expire = 0;
|
|
299
|
|
300 } else {
|
|
301 for ( ;; ) {
|
|
302 timer = ngx_event_find_timer();
|
|
303
|
|
304 #if (NGX_THREADS)
|
|
305
|
|
306 if (timer == NGX_TIMER_ERROR) {
|
|
307 return NGX_ERROR;
|
|
308 }
|
|
309
|
|
310 if (timer == NGX_TIMER_INFINITE || timer > 500) {
|
|
311 timer = 500;
|
|
312 break;
|
|
313 }
|
|
314
|
|
315 #endif
|
|
316
|
|
317 if (timer != 0) {
|
|
318 break;
|
|
319 }
|
|
320
|
|
321 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
|
322 "rtsig expired timer");
|
|
323
|
|
324 ngx_event_expire_timers((ngx_msec_t)
|
|
325 (ngx_elapsed_msec - ngx_old_elapsed_msec));
|
|
326
|
|
327 if (ngx_posted_events && ngx_threaded) {
|
|
328 ngx_wakeup_worker_thread(cycle);
|
|
329 }
|
|
330 }
|
|
331
|
|
332 expire = 1;
|
|
333
|
|
334 if (ngx_accept_mutex) {
|
|
335 if (ngx_accept_disabled > 0) {
|
|
336 ngx_accept_disabled--;
|
|
337
|
|
338 } else {
|
|
339 ngx_accept_mutex_held = 0;
|
|
340
|
|
341 if (ngx_trylock_accept_mutex(cycle) == NGX_ERROR) {
|
|
342 return NGX_ERROR;
|
|
343 }
|
|
344
|
|
345 if (ngx_accept_mutex_held == 0
|
|
346 && (timer == NGX_TIMER_INFINITE
|
|
347 || timer > ngx_accept_mutex_delay))
|
|
348 {
|
|
349 timer = ngx_accept_mutex_delay;
|
|
350 expire = 0;
|
|
351 }
|
|
352 }
|
|
353 }
|
|
354 }
|
|
355
|
|
356 if (timer == NGX_TIMER_INFINITE) {
|
|
357 tp = NULL;
|
|
358 expire = 0;
|
|
359
|
|
360 } else {
|
|
361 ts.tv_sec = timer / 1000;
|
|
362 ts.tv_nsec = (timer % 1000) * 1000000;
|
|
363 tp = &ts;
|
|
364 }
|
|
365
|
|
366 ngx_old_elapsed_msec = ngx_elapsed_msec;
|
|
367
|
|
368 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
|
369 "rtsig timer: %d", timer);
|
|
370
|
|
371 /* Linux's sigwaitinfo() is sigtimedwait() with the NULL timeout pointer */
|
|
372
|
|
373 signo = sigtimedwait(&set, &si, tp);
|
|
374
|
|
375 if (signo == -1) {
|
|
376 err = ngx_errno;
|
|
377
|
|
378 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, err,
|
|
379 "rtsig signo:%d", signo);
|
|
380
|
|
381 if (err == NGX_EAGAIN) {
|
|
382
|
|
383 if (timer == NGX_TIMER_INFINITE) {
|
|
384 ngx_accept_mutex_unlock();
|
|
385 ngx_log_error(NGX_LOG_ALERT, cycle->log, err,
|
|
386 "sigtimedwait() returned EAGAIN without timeout");
|
|
387 return NGX_ERROR;
|
|
388 }
|
|
389
|
|
390 err = 0;
|
|
391 }
|
|
392
|
|
393 } else {
|
|
394 err = 0;
|
|
395 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
10
|
396 "rtsig signo:%d fd:%d band:%04Xd",
|
0
|
397 signo, si.si_fd, si.si_band);
|
|
398 }
|
|
399
|
|
400 ngx_gettimeofday(&tv);
|
|
401 ngx_time_update(tv.tv_sec);
|
|
402
|
|
403 delta = ngx_elapsed_msec;
|
|
404 ngx_elapsed_msec = (ngx_epoch_msec_t) tv.tv_sec * 1000
|
|
405 + tv.tv_usec / 1000 - ngx_start_msec;
|
|
406
|
|
407 if (err) {
|
|
408 ngx_accept_mutex_unlock();
|
|
409 ngx_log_error((err == NGX_EINTR) ? NGX_LOG_INFO : NGX_LOG_ALERT,
|
|
410 cycle->log, err, "sigtimedwait() failed");
|
|
411 return NGX_ERROR;
|
|
412 }
|
|
413
|
|
414 if (timer != NGX_TIMER_INFINITE) {
|
|
415 delta = ngx_elapsed_msec - delta;
|
|
416
|
|
417 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
|
418 "rtsig timer: %d, delta: %d", timer, (int) delta);
|
|
419 }
|
|
420
|
|
421 rtscf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_rtsig_module);
|
|
422
|
|
423 if (signo == rtscf->signo || signo == rtscf->signo + 1) {
|
|
424
|
|
425 if (overflow && (ngx_uint_t) si.si_fd > overflow_current) {
|
|
426 return NGX_OK;
|
|
427 }
|
|
428
|
|
429 /* TODO: old_cycles */
|
|
430
|
|
431 c = &ngx_cycle->connections[si.si_fd];
|
|
432
|
|
433 instance = signo - rtscf->signo;
|
|
434
|
|
435 rev = c->read;
|
|
436
|
|
437 if (c->read->instance != instance) {
|
|
438
|
|
439 /*
|
|
440 * the stale event from a file descriptor
|
|
441 * that was just closed in this iteration
|
|
442 */
|
|
443
|
|
444 ngx_accept_mutex_unlock();
|
|
445
|
|
446 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
10
|
447 "rtsig: stale event %p", c);
|
0
|
448
|
|
449 return NGX_OK;
|
|
450 }
|
|
451
|
|
452 if (si.si_band & (POLLIN|POLLHUP|POLLERR)) {
|
|
453 if (rev->active) {
|
|
454
|
|
455 if (ngx_threaded && !rev->accept) {
|
|
456 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
|
|
457 ngx_accept_mutex_unlock();
|
|
458 return NGX_ERROR;
|
|
459 }
|
|
460
|
|
461 rev->posted_ready = 1;
|
|
462 ngx_post_event(rev);
|
|
463
|
|
464 ngx_mutex_unlock(ngx_posted_events_mutex);
|
|
465
|
|
466 } else {
|
|
467 rev->ready = 1;
|
|
468
|
|
469 if (!ngx_threaded && !ngx_accept_mutex_held) {
|
58
|
470 rev->handler(rev);
|
0
|
471
|
|
472 } else if (rev->accept) {
|
|
473 if (ngx_accept_disabled <= 0) {
|
58
|
474 rev->handler(rev);
|
0
|
475 }
|
|
476
|
|
477 } else {
|
|
478 ngx_post_event(rev);
|
|
479 }
|
|
480 }
|
|
481 }
|
|
482 }
|
|
483
|
|
484 wev = c->write;
|
|
485
|
|
486 if (si.si_band & (POLLOUT|POLLHUP|POLLERR)) {
|
|
487 if (wev->active) {
|
|
488
|
|
489 if (ngx_threaded) {
|
|
490 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
|
|
491 ngx_accept_mutex_unlock();
|
|
492 return NGX_ERROR;
|
|
493 }
|
|
494
|
|
495 wev->posted_ready = 1;
|
|
496 ngx_post_event(wev);
|
|
497
|
|
498 ngx_mutex_unlock(ngx_posted_events_mutex);
|
|
499
|
|
500 } else {
|
|
501 wev->ready = 1;
|
|
502
|
|
503 if (!ngx_threaded && !ngx_accept_mutex_held) {
|
58
|
504 wev->handler(wev);
|
0
|
505
|
|
506 } else {
|
|
507 ngx_post_event(wev);
|
|
508 }
|
|
509 }
|
|
510 }
|
|
511 }
|
|
512
|
|
513 } else if (signo == SIGIO) {
|
|
514 ngx_accept_mutex_unlock();
|
|
515
|
|
516 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
|
|
517 "rt signal queue overflowed");
|
|
518
|
|
519 /* flush the RT signal queue */
|
|
520
|
|
521 ngx_memzero(&sa, sizeof(struct sigaction));
|
|
522 sa.sa_handler = SIG_DFL;
|
|
523 sigemptyset(&sa.sa_mask);
|
|
524
|
|
525 if (sigaction(rtscf->signo, &sa, NULL) == -1) {
|
|
526 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
527 "sigaction(%d, SIG_DFL) failed", rtscf->signo);
|
|
528 }
|
|
529
|
|
530 if (sigaction(rtscf->signo + 1, &sa, NULL) == -1) {
|
|
531 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
532 "sigaction(%d, SIG_DFL) failed", rtscf->signo + 1);
|
|
533 }
|
|
534
|
|
535 overflow = 1;
|
|
536 overflow_current = 0;
|
|
537 ngx_event_actions.process_events = ngx_rtsig_process_overflow;
|
|
538
|
|
539 return NGX_ERROR;
|
|
540
|
|
541 } else if (signo != -1) {
|
|
542 ngx_accept_mutex_unlock();
|
|
543
|
|
544 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
|
|
545 "sigtimedwait() returned unexpected signal: %d", signo);
|
|
546
|
|
547 return NGX_ERROR;
|
|
548 }
|
|
549
|
|
550 ngx_accept_mutex_unlock();
|
|
551
|
|
552 if (expire && delta) {
|
|
553 ngx_event_expire_timers((ngx_msec_t) delta);
|
|
554 }
|
|
555
|
|
556 if (ngx_posted_events) {
|
|
557 if (ngx_threaded) {
|
|
558 ngx_wakeup_worker_thread(cycle);
|
|
559
|
|
560 } else {
|
|
561 ngx_event_process_posted(cycle);
|
|
562 }
|
|
563 }
|
|
564
|
|
565 if (signo == -1) {
|
|
566 return NGX_AGAIN;
|
|
567 } else {
|
|
568 return NGX_OK;
|
|
569 }
|
|
570 }
|
|
571
|
|
572
|
|
573 /* TODO: old cylces */
|
|
574
|
48
|
575 static ngx_int_t
|
|
576 ngx_rtsig_process_overflow(ngx_cycle_t *cycle)
|
0
|
577 {
|
|
578 int name[2], rtsig_max, rtsig_nr, events, ready;
|
|
579 size_t len;
|
|
580 ngx_int_t tested, n, i;
|
|
581 ngx_err_t err;
|
|
582 ngx_event_t *rev, *wev;
|
|
583 ngx_connection_t *c;
|
|
584 ngx_rtsig_conf_t *rtscf;
|
|
585
|
|
586 rtscf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_rtsig_module);
|
|
587
|
|
588 tested = 0;
|
|
589
|
|
590 for ( ;; ) {
|
|
591
|
|
592 n = 0;
|
|
593 while (n < rtscf->overflow_events) {
|
|
594
|
|
595 if (overflow_current == cycle->connection_n) {
|
|
596 break;
|
|
597 }
|
|
598
|
|
599 c = &cycle->connections[overflow_current++];
|
|
600
|
|
601 if (c->fd == -1) {
|
|
602 continue;
|
|
603 }
|
|
604
|
|
605 events = 0;
|
|
606
|
58
|
607 if (c->read->active && c->read->handler) {
|
0
|
608 events |= POLLIN;
|
|
609 }
|
|
610
|
58
|
611 if (c->write->active && c->write->handler) {
|
0
|
612 events |= POLLOUT;
|
|
613 }
|
|
614
|
|
615 if (events == 0) {
|
|
616 continue;
|
|
617 }
|
|
618
|
|
619 overflow_list[n].fd = c->fd;
|
|
620 overflow_list[n].events = events;
|
|
621 overflow_list[n].revents = 0;
|
|
622 n++;
|
|
623 }
|
|
624
|
|
625 if (n == 0) {
|
|
626 break;
|
|
627 }
|
|
628
|
|
629 for ( ;; ) {
|
|
630 ready = poll(overflow_list, n, 0);
|
|
631
|
|
632 if (ready == -1) {
|
|
633 err = ngx_errno;
|
|
634 ngx_log_error((err == NGX_EINTR) ? NGX_LOG_INFO : NGX_LOG_ALERT,
|
|
635 cycle->log, 0,
|
|
636 "poll() failed while the overflow recover");
|
|
637
|
|
638 if (err == NGX_EINTR) {
|
|
639 continue;
|
|
640 }
|
|
641 }
|
|
642
|
|
643 break;
|
|
644 }
|
|
645
|
|
646 if (ready <= 0) {
|
|
647 continue;
|
|
648 }
|
|
649
|
|
650 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
|
|
651 return NGX_ERROR;
|
|
652 }
|
|
653
|
|
654 for (i = 0; i < n; i++) {
|
|
655 c = &cycle->connections[overflow_list[i].fd];
|
|
656
|
|
657 rev = c->read;
|
|
658
|
|
659 if (rev->active
|
|
660 && !rev->closed
|
58
|
661 && rev->handler
|
0
|
662 && (overflow_list[i].revents
|
|
663 & (POLLIN|POLLERR|POLLHUP|POLLNVAL)))
|
|
664 {
|
|
665 tested++;
|
|
666
|
|
667 if (ngx_threaded) {
|
|
668 rev->posted_ready = 1;
|
|
669 ngx_post_event(rev);
|
|
670
|
|
671 } else {
|
|
672 rev->ready = 1;
|
58
|
673 rev->handler(rev);
|
0
|
674 }
|
|
675 }
|
|
676
|
|
677 wev = c->write;
|
|
678
|
|
679 if (wev->active
|
|
680 && !wev->closed
|
58
|
681 && wev->handler
|
0
|
682 && (overflow_list[i].revents
|
|
683 & (POLLOUT|POLLERR|POLLHUP|POLLNVAL)))
|
|
684 {
|
|
685 tested++;
|
|
686
|
|
687 if (ngx_threaded) {
|
|
688 wev->posted_ready = 1;
|
|
689 ngx_post_event(wev);
|
|
690
|
|
691 } else {
|
|
692 wev->ready = 1;
|
58
|
693 wev->handler(wev);
|
0
|
694 }
|
|
695 }
|
|
696 }
|
|
697
|
|
698 ngx_mutex_unlock(ngx_posted_events_mutex);
|
|
699
|
|
700 if (tested >= rtscf->overflow_test) {
|
|
701
|
|
702 if (ngx_linux_rtsig_max) {
|
|
703
|
|
704 /*
|
|
705 * Check the current rt queue length to prevent
|
|
706 * the new overflow.
|
|
707 *
|
|
708 * Learn the /proc/sys/kernel/rtsig-max value because
|
2
|
709 * it can be changed since the last checking.
|
0
|
710 */
|
|
711
|
|
712 name[0] = CTL_KERN;
|
|
713 name[1] = KERN_RTSIGMAX;
|
|
714 len = sizeof(rtsig_max);
|
42
|
715
|
|
716 if (sysctl(name, 2, &rtsig_max, &len, NULL, 0) == -1) {
|
0
|
717 ngx_log_error(NGX_LOG_ALERT, cycle->log, errno,
|
|
718 "sysctl(KERN_RTSIGMAX) failed");
|
|
719 return NGX_ERROR;
|
|
720 }
|
|
721
|
42
|
722 /* name[0] = CTL_KERN; */
|
0
|
723 name[1] = KERN_RTSIGNR;
|
|
724 len = sizeof(rtsig_nr);
|
42
|
725
|
|
726 if (sysctl(name, 2, &rtsig_nr, &len, NULL, 0) == -1) {
|
0
|
727 ngx_log_error(NGX_LOG_ALERT, cycle->log, errno,
|
|
728 "sysctl(KERN_RTSIGNR) failed");
|
|
729 return NGX_ERROR;
|
|
730 }
|
|
731
|
|
732 /*
|
|
733 * drain the rt signal queue if the /proc/sys/kernel/rtsig-nr
|
|
734 * is bigger than
|
|
735 * /proc/sys/kernel/rtsig-max / rtsig_overflow_threshold
|
|
736 */
|
|
737
|
|
738 if (rtsig_max / rtscf->overflow_threshold < rtsig_nr) {
|
|
739 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
|
740 "rtsig queue state: %d/%d",
|
|
741 rtsig_nr, rtsig_max);
|
|
742 while (ngx_rtsig_process_events(cycle) == NGX_OK) {
|
|
743 /* void */
|
|
744 }
|
|
745 }
|
|
746
|
|
747 } else {
|
|
748
|
|
749 /*
|
|
750 * Linux has not KERN_RTSIGMAX since 2.6.6-mm2
|
|
751 * so drain the rt signal queue unconditionally
|
|
752 */
|
|
753
|
|
754 while (ngx_rtsig_process_events(cycle) == NGX_OK) { /* void */ }
|
|
755 }
|
|
756
|
|
757 tested = 0;
|
|
758 }
|
|
759 }
|
|
760
|
|
761 if (ngx_posted_events) {
|
|
762 if (ngx_threaded) {
|
|
763 ngx_wakeup_worker_thread(cycle);
|
|
764
|
|
765 } else {
|
|
766 ngx_event_process_posted(cycle);
|
|
767 }
|
|
768 }
|
|
769
|
76
|
770 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
|
0
|
771 "rt signal queue overflow recovered");
|
|
772
|
|
773 overflow = 0;
|
|
774 ngx_event_actions.process_events = ngx_rtsig_process_events;
|
|
775
|
|
776 return NGX_OK;
|
|
777 }
|
|
778
|
|
779
|
48
|
780 static void *
|
|
781 ngx_rtsig_create_conf(ngx_cycle_t *cycle)
|
0
|
782 {
|
|
783 ngx_rtsig_conf_t *rtscf;
|
|
784
|
50
|
785 rtscf = ngx_palloc(cycle->pool, sizeof(ngx_rtsig_conf_t));
|
|
786 if (rtscf == NULL) {
|
|
787 return NGX_CONF_ERROR;
|
|
788 }
|
0
|
789
|
|
790 rtscf->signo = NGX_CONF_UNSET;
|
|
791 rtscf->overflow_events = NGX_CONF_UNSET;
|
|
792 rtscf->overflow_test = NGX_CONF_UNSET;
|
|
793 rtscf->overflow_threshold = NGX_CONF_UNSET;
|
|
794
|
|
795 return rtscf;
|
|
796 }
|
|
797
|
|
798
|
48
|
799 static char *
|
|
800 ngx_rtsig_init_conf(ngx_cycle_t *cycle, void *conf)
|
0
|
801 {
|
|
802 ngx_rtsig_conf_t *rtscf = conf;
|
|
803
|
|
804 /* LinuxThreads use the first 3 RT signals */
|
|
805 ngx_conf_init_value(rtscf->signo, SIGRTMIN + 10);
|
|
806
|
|
807 ngx_conf_init_value(rtscf->overflow_events, 16);
|
|
808 ngx_conf_init_value(rtscf->overflow_test, 32);
|
|
809 ngx_conf_init_value(rtscf->overflow_threshold, 10);
|
|
810
|
|
811 return NGX_CONF_OK;
|
|
812 }
|
|
813
|
|
814
|
48
|
815 static char *
|
|
816 ngx_check_ngx_overflow_threshold_bounds(ngx_conf_t *cf,
|
0
|
817 void *post, void *data)
|
|
818 {
|
|
819 if (ngx_linux_rtsig_max) {
|
|
820 return ngx_conf_check_num_bounds(cf, post, data);
|
|
821 }
|
|
822
|
|
823 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
824 "\"rtsig_overflow_threshold\" is not supported "
|
|
825 "since Linux 2.6.6-mm2, ignored");
|
|
826
|
|
827 return NGX_CONF_OK;
|
|
828 }
|