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
|
92
|
168 ngx_event_flags = NGX_USE_RTSIG_EVENT
|
|
169 |NGX_USE_GREEDY_EVENT
|
|
170 |NGX_USE_FD_EVENT;
|
0
|
171
|
|
172 return NGX_OK;
|
|
173 }
|
|
174
|
|
175
|
48
|
176 static void
|
|
177 ngx_rtsig_done(ngx_cycle_t *cycle)
|
0
|
178 {
|
2
|
179 ngx_free(overflow_list);
|
|
180
|
|
181 overflow_list = NULL;
|
0
|
182 }
|
|
183
|
|
184
|
48
|
185 static ngx_int_t
|
|
186 ngx_rtsig_add_connection(ngx_connection_t *c)
|
0
|
187 {
|
|
188 int signo;
|
|
189 ngx_rtsig_conf_t *rtscf;
|
|
190
|
|
191 if (c->read->accept && c->read->disabled) {
|
|
192
|
|
193 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
194 "rtsig enable connection: fd:%d", c->fd);
|
|
195
|
|
196 if (fcntl(c->fd, F_SETOWN, ngx_pid) == -1) {
|
|
197 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
|
|
198 "fcntl(F_SETOWN) failed");
|
|
199 return NGX_ERROR;
|
|
200 }
|
|
201
|
|
202 c->read->active = 1;
|
|
203 c->read->disabled = 0;
|
|
204 }
|
|
205
|
|
206 rtscf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_rtsig_module);
|
|
207
|
|
208 signo = rtscf->signo + c->read->instance;
|
|
209
|
|
210 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
211 "rtsig add connection: fd:%d signo:%d", c->fd, signo);
|
|
212
|
|
213 if (fcntl(c->fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC) == -1) {
|
|
214 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
|
|
215 "fcntl(O_RDWR|O_NONBLOCK|O_ASYNC) failed");
|
|
216 return NGX_ERROR;
|
|
217 }
|
|
218
|
|
219 if (fcntl(c->fd, F_SETSIG, signo) == -1) {
|
|
220 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
|
|
221 "fcntl(F_SETSIG) failed");
|
|
222 return NGX_ERROR;
|
|
223 }
|
|
224
|
|
225 if (fcntl(c->fd, F_SETOWN, ngx_pid) == -1) {
|
|
226 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
|
|
227 "fcntl(F_SETOWN) failed");
|
|
228 return NGX_ERROR;
|
|
229 }
|
|
230
|
18
|
231 #if (NGX_HAVE_ONESIGFD)
|
0
|
232 if (fcntl(c->fd, F_SETAUXFL, O_ONESIGFD) == -1) {
|
|
233 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
|
|
234 "fcntl(F_SETAUXFL) failed");
|
|
235 return NGX_ERROR;
|
|
236 }
|
|
237 #endif
|
|
238
|
|
239 c->read->active = 1;
|
|
240 c->write->active = 1;
|
|
241
|
|
242 return NGX_OK;
|
|
243 }
|
|
244
|
|
245
|
48
|
246 static ngx_int_t
|
|
247 ngx_rtsig_del_connection(ngx_connection_t *c, u_int flags)
|
0
|
248 {
|
|
249 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
250 "rtsig del connection: fd:%d", c->fd);
|
|
251
|
|
252 if ((flags & NGX_DISABLE_EVENT) && c->read->accept) {
|
|
253
|
|
254 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
255 "rtsig disable connection: fd:%d", c->fd);
|
|
256
|
|
257 c->read->active = 0;
|
|
258 c->read->disabled = 1;
|
|
259 return NGX_OK;
|
|
260 }
|
|
261
|
|
262 if (flags & NGX_CLOSE_EVENT) {
|
|
263 c->read->active = 0;
|
|
264 c->write->active = 0;
|
|
265 return NGX_OK;
|
|
266 }
|
|
267
|
|
268 if (fcntl(c->fd, F_SETFL, O_RDWR|O_NONBLOCK) == -1) {
|
|
269 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
|
|
270 "fcntl(O_RDWR|O_NONBLOCK) failed");
|
|
271 return NGX_ERROR;
|
|
272 }
|
|
273
|
|
274 c->read->active = 0;
|
|
275 c->write->active = 0;
|
|
276
|
|
277 return NGX_OK;
|
|
278 }
|
|
279
|
|
280
|
48
|
281 static ngx_int_t
|
|
282 ngx_rtsig_process_events(ngx_cycle_t *cycle)
|
0
|
283 {
|
|
284 int signo;
|
50
|
285 ngx_int_t instance;
|
106
|
286 ngx_msec_t timer, delta;
|
0
|
287 ngx_err_t err;
|
|
288 siginfo_t si;
|
|
289 ngx_event_t *rev, *wev;
|
|
290 struct timeval tv;
|
|
291 struct timespec ts, *tp;
|
|
292 struct sigaction sa;
|
|
293 ngx_connection_t *c;
|
|
294 ngx_rtsig_conf_t *rtscf;
|
|
295
|
|
296 if (overflow) {
|
|
297 timer = 0;
|
|
298
|
|
299 } else {
|
106
|
300 timer = ngx_event_find_timer();
|
0
|
301
|
|
302 #if (NGX_THREADS)
|
|
303
|
106
|
304 if (timer == NGX_TIMER_ERROR) {
|
|
305 return NGX_ERROR;
|
|
306 }
|
0
|
307
|
106
|
308 if (timer == NGX_TIMER_INFINITE || timer > 500) {
|
|
309 timer = 500;
|
|
310 }
|
0
|
311
|
|
312 #endif
|
|
313
|
|
314 if (ngx_accept_mutex) {
|
|
315 if (ngx_accept_disabled > 0) {
|
|
316 ngx_accept_disabled--;
|
|
317
|
|
318 } else {
|
|
319 ngx_accept_mutex_held = 0;
|
|
320
|
|
321 if (ngx_trylock_accept_mutex(cycle) == NGX_ERROR) {
|
|
322 return NGX_ERROR;
|
|
323 }
|
|
324
|
|
325 if (ngx_accept_mutex_held == 0
|
|
326 && (timer == NGX_TIMER_INFINITE
|
|
327 || timer > ngx_accept_mutex_delay))
|
|
328 {
|
|
329 timer = ngx_accept_mutex_delay;
|
|
330 }
|
|
331 }
|
|
332 }
|
|
333 }
|
|
334
|
|
335 if (timer == NGX_TIMER_INFINITE) {
|
|
336 tp = NULL;
|
|
337
|
|
338 } else {
|
|
339 ts.tv_sec = timer / 1000;
|
|
340 ts.tv_nsec = (timer % 1000) * 1000000;
|
|
341 tp = &ts;
|
|
342 }
|
|
343
|
|
344 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
106
|
345 "rtsig timer: %M", timer);
|
0
|
346
|
|
347 /* Linux's sigwaitinfo() is sigtimedwait() with the NULL timeout pointer */
|
|
348
|
|
349 signo = sigtimedwait(&set, &si, tp);
|
|
350
|
|
351 if (signo == -1) {
|
|
352 err = ngx_errno;
|
|
353
|
|
354 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, err,
|
|
355 "rtsig signo:%d", signo);
|
|
356
|
|
357 if (err == NGX_EAGAIN) {
|
|
358
|
|
359 if (timer == NGX_TIMER_INFINITE) {
|
|
360 ngx_accept_mutex_unlock();
|
|
361 ngx_log_error(NGX_LOG_ALERT, cycle->log, err,
|
|
362 "sigtimedwait() returned EAGAIN without timeout");
|
|
363 return NGX_ERROR;
|
|
364 }
|
|
365
|
|
366 err = 0;
|
|
367 }
|
|
368
|
|
369 } else {
|
|
370 err = 0;
|
|
371 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
10
|
372 "rtsig signo:%d fd:%d band:%04Xd",
|
0
|
373 signo, si.si_fd, si.si_band);
|
|
374 }
|
|
375
|
|
376 ngx_gettimeofday(&tv);
|
|
377 ngx_time_update(tv.tv_sec);
|
|
378
|
106
|
379 delta = ngx_current_time;
|
|
380 ngx_current_time = (ngx_msec_t) tv.tv_sec * 1000 + tv.tv_usec / 1000;
|
0
|
381
|
|
382 if (err) {
|
|
383 ngx_accept_mutex_unlock();
|
|
384 ngx_log_error((err == NGX_EINTR) ? NGX_LOG_INFO : NGX_LOG_ALERT,
|
|
385 cycle->log, err, "sigtimedwait() failed");
|
|
386 return NGX_ERROR;
|
|
387 }
|
|
388
|
|
389 if (timer != NGX_TIMER_INFINITE) {
|
106
|
390 delta = ngx_current_time - delta;
|
0
|
391
|
|
392 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
106
|
393 "rtsig timer: %M, delta: %M", timer, delta);
|
0
|
394 }
|
|
395
|
|
396 rtscf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_rtsig_module);
|
|
397
|
|
398 if (signo == rtscf->signo || signo == rtscf->signo + 1) {
|
|
399
|
|
400 if (overflow && (ngx_uint_t) si.si_fd > overflow_current) {
|
|
401 return NGX_OK;
|
|
402 }
|
|
403
|
100
|
404 c = ngx_cycle->files[si.si_fd];
|
|
405
|
|
406 if (c == NULL) {
|
|
407 /* the stale event */
|
0
|
408
|
100
|
409 ngx_accept_mutex_unlock();
|
|
410
|
|
411 return NGX_OK;
|
|
412 }
|
0
|
413
|
|
414 instance = signo - rtscf->signo;
|
|
415
|
|
416 rev = c->read;
|
|
417
|
|
418 if (c->read->instance != instance) {
|
|
419
|
|
420 /*
|
|
421 * the stale event from a file descriptor
|
|
422 * that was just closed in this iteration
|
|
423 */
|
|
424
|
|
425 ngx_accept_mutex_unlock();
|
|
426
|
|
427 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
10
|
428 "rtsig: stale event %p", c);
|
0
|
429
|
|
430 return NGX_OK;
|
|
431 }
|
|
432
|
|
433 if (si.si_band & (POLLIN|POLLHUP|POLLERR)) {
|
|
434 if (rev->active) {
|
|
435
|
|
436 if (ngx_threaded && !rev->accept) {
|
|
437 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
|
|
438 ngx_accept_mutex_unlock();
|
|
439 return NGX_ERROR;
|
|
440 }
|
|
441
|
|
442 rev->posted_ready = 1;
|
|
443 ngx_post_event(rev);
|
|
444
|
|
445 ngx_mutex_unlock(ngx_posted_events_mutex);
|
|
446
|
|
447 } else {
|
|
448 rev->ready = 1;
|
|
449
|
|
450 if (!ngx_threaded && !ngx_accept_mutex_held) {
|
58
|
451 rev->handler(rev);
|
0
|
452
|
|
453 } else if (rev->accept) {
|
|
454 if (ngx_accept_disabled <= 0) {
|
58
|
455 rev->handler(rev);
|
0
|
456 }
|
|
457
|
|
458 } else {
|
|
459 ngx_post_event(rev);
|
|
460 }
|
|
461 }
|
|
462 }
|
|
463 }
|
|
464
|
|
465 wev = c->write;
|
|
466
|
|
467 if (si.si_band & (POLLOUT|POLLHUP|POLLERR)) {
|
|
468 if (wev->active) {
|
|
469
|
|
470 if (ngx_threaded) {
|
|
471 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
|
|
472 ngx_accept_mutex_unlock();
|
|
473 return NGX_ERROR;
|
|
474 }
|
|
475
|
|
476 wev->posted_ready = 1;
|
|
477 ngx_post_event(wev);
|
|
478
|
|
479 ngx_mutex_unlock(ngx_posted_events_mutex);
|
|
480
|
|
481 } else {
|
|
482 wev->ready = 1;
|
|
483
|
|
484 if (!ngx_threaded && !ngx_accept_mutex_held) {
|
58
|
485 wev->handler(wev);
|
0
|
486
|
|
487 } else {
|
|
488 ngx_post_event(wev);
|
|
489 }
|
|
490 }
|
|
491 }
|
|
492 }
|
|
493
|
|
494 } else if (signo == SIGIO) {
|
|
495 ngx_accept_mutex_unlock();
|
|
496
|
|
497 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
|
|
498 "rt signal queue overflowed");
|
|
499
|
|
500 /* flush the RT signal queue */
|
|
501
|
|
502 ngx_memzero(&sa, sizeof(struct sigaction));
|
|
503 sa.sa_handler = SIG_DFL;
|
|
504 sigemptyset(&sa.sa_mask);
|
|
505
|
|
506 if (sigaction(rtscf->signo, &sa, NULL) == -1) {
|
|
507 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
508 "sigaction(%d, SIG_DFL) failed", rtscf->signo);
|
|
509 }
|
|
510
|
|
511 if (sigaction(rtscf->signo + 1, &sa, NULL) == -1) {
|
|
512 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
513 "sigaction(%d, SIG_DFL) failed", rtscf->signo + 1);
|
|
514 }
|
|
515
|
|
516 overflow = 1;
|
|
517 overflow_current = 0;
|
|
518 ngx_event_actions.process_events = ngx_rtsig_process_overflow;
|
|
519
|
|
520 return NGX_ERROR;
|
|
521
|
|
522 } else if (signo != -1) {
|
|
523 ngx_accept_mutex_unlock();
|
|
524
|
|
525 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
|
|
526 "sigtimedwait() returned unexpected signal: %d", signo);
|
|
527
|
|
528 return NGX_ERROR;
|
|
529 }
|
|
530
|
|
531 ngx_accept_mutex_unlock();
|
|
532
|
106
|
533 ngx_event_expire_timers();
|
0
|
534
|
|
535 if (ngx_posted_events) {
|
|
536 if (ngx_threaded) {
|
|
537 ngx_wakeup_worker_thread(cycle);
|
|
538
|
|
539 } else {
|
|
540 ngx_event_process_posted(cycle);
|
|
541 }
|
|
542 }
|
|
543
|
|
544 if (signo == -1) {
|
|
545 return NGX_AGAIN;
|
|
546 } else {
|
|
547 return NGX_OK;
|
|
548 }
|
|
549 }
|
|
550
|
|
551
|
|
552 /* TODO: old cylces */
|
|
553
|
48
|
554 static ngx_int_t
|
|
555 ngx_rtsig_process_overflow(ngx_cycle_t *cycle)
|
0
|
556 {
|
|
557 int name[2], rtsig_max, rtsig_nr, events, ready;
|
|
558 size_t len;
|
|
559 ngx_int_t tested, n, i;
|
|
560 ngx_err_t err;
|
|
561 ngx_event_t *rev, *wev;
|
|
562 ngx_connection_t *c;
|
|
563 ngx_rtsig_conf_t *rtscf;
|
|
564
|
|
565 rtscf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_rtsig_module);
|
|
566
|
|
567 tested = 0;
|
|
568
|
|
569 for ( ;; ) {
|
|
570
|
|
571 n = 0;
|
|
572 while (n < rtscf->overflow_events) {
|
|
573
|
|
574 if (overflow_current == cycle->connection_n) {
|
|
575 break;
|
|
576 }
|
|
577
|
92
|
578 c = cycle->files[overflow_current++];
|
0
|
579
|
108
|
580 if (c == NULL || c->fd == -1) {
|
0
|
581 continue;
|
|
582 }
|
|
583
|
|
584 events = 0;
|
|
585
|
58
|
586 if (c->read->active && c->read->handler) {
|
0
|
587 events |= POLLIN;
|
|
588 }
|
|
589
|
58
|
590 if (c->write->active && c->write->handler) {
|
0
|
591 events |= POLLOUT;
|
|
592 }
|
|
593
|
|
594 if (events == 0) {
|
|
595 continue;
|
|
596 }
|
|
597
|
|
598 overflow_list[n].fd = c->fd;
|
|
599 overflow_list[n].events = events;
|
|
600 overflow_list[n].revents = 0;
|
|
601 n++;
|
|
602 }
|
|
603
|
|
604 if (n == 0) {
|
|
605 break;
|
|
606 }
|
|
607
|
|
608 for ( ;; ) {
|
|
609 ready = poll(overflow_list, n, 0);
|
|
610
|
|
611 if (ready == -1) {
|
|
612 err = ngx_errno;
|
|
613 ngx_log_error((err == NGX_EINTR) ? NGX_LOG_INFO : NGX_LOG_ALERT,
|
|
614 cycle->log, 0,
|
|
615 "poll() failed while the overflow recover");
|
|
616
|
|
617 if (err == NGX_EINTR) {
|
|
618 continue;
|
|
619 }
|
|
620 }
|
|
621
|
|
622 break;
|
|
623 }
|
|
624
|
|
625 if (ready <= 0) {
|
|
626 continue;
|
|
627 }
|
|
628
|
|
629 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
|
|
630 return NGX_ERROR;
|
|
631 }
|
|
632
|
|
633 for (i = 0; i < n; i++) {
|
92
|
634 c = cycle->files[overflow_list[i].fd];
|
0
|
635
|
108
|
636 if (c == NULL) {
|
|
637 continue;
|
|
638 }
|
|
639
|
0
|
640 rev = c->read;
|
|
641
|
|
642 if (rev->active
|
|
643 && !rev->closed
|
58
|
644 && rev->handler
|
0
|
645 && (overflow_list[i].revents
|
|
646 & (POLLIN|POLLERR|POLLHUP|POLLNVAL)))
|
|
647 {
|
|
648 tested++;
|
|
649
|
|
650 if (ngx_threaded) {
|
|
651 rev->posted_ready = 1;
|
|
652 ngx_post_event(rev);
|
|
653
|
|
654 } else {
|
|
655 rev->ready = 1;
|
58
|
656 rev->handler(rev);
|
0
|
657 }
|
|
658 }
|
|
659
|
|
660 wev = c->write;
|
|
661
|
|
662 if (wev->active
|
|
663 && !wev->closed
|
58
|
664 && wev->handler
|
0
|
665 && (overflow_list[i].revents
|
|
666 & (POLLOUT|POLLERR|POLLHUP|POLLNVAL)))
|
|
667 {
|
|
668 tested++;
|
|
669
|
|
670 if (ngx_threaded) {
|
|
671 wev->posted_ready = 1;
|
|
672 ngx_post_event(wev);
|
|
673
|
|
674 } else {
|
|
675 wev->ready = 1;
|
58
|
676 wev->handler(wev);
|
0
|
677 }
|
|
678 }
|
|
679 }
|
|
680
|
|
681 ngx_mutex_unlock(ngx_posted_events_mutex);
|
|
682
|
|
683 if (tested >= rtscf->overflow_test) {
|
|
684
|
|
685 if (ngx_linux_rtsig_max) {
|
|
686
|
|
687 /*
|
|
688 * Check the current rt queue length to prevent
|
|
689 * the new overflow.
|
|
690 *
|
|
691 * Learn the /proc/sys/kernel/rtsig-max value because
|
2
|
692 * it can be changed since the last checking.
|
0
|
693 */
|
|
694
|
|
695 name[0] = CTL_KERN;
|
|
696 name[1] = KERN_RTSIGMAX;
|
|
697 len = sizeof(rtsig_max);
|
42
|
698
|
|
699 if (sysctl(name, 2, &rtsig_max, &len, NULL, 0) == -1) {
|
0
|
700 ngx_log_error(NGX_LOG_ALERT, cycle->log, errno,
|
|
701 "sysctl(KERN_RTSIGMAX) failed");
|
|
702 return NGX_ERROR;
|
|
703 }
|
|
704
|
42
|
705 /* name[0] = CTL_KERN; */
|
0
|
706 name[1] = KERN_RTSIGNR;
|
|
707 len = sizeof(rtsig_nr);
|
42
|
708
|
|
709 if (sysctl(name, 2, &rtsig_nr, &len, NULL, 0) == -1) {
|
0
|
710 ngx_log_error(NGX_LOG_ALERT, cycle->log, errno,
|
|
711 "sysctl(KERN_RTSIGNR) failed");
|
|
712 return NGX_ERROR;
|
|
713 }
|
|
714
|
|
715 /*
|
|
716 * drain the rt signal queue if the /proc/sys/kernel/rtsig-nr
|
|
717 * is bigger than
|
|
718 * /proc/sys/kernel/rtsig-max / rtsig_overflow_threshold
|
|
719 */
|
|
720
|
|
721 if (rtsig_max / rtscf->overflow_threshold < rtsig_nr) {
|
|
722 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
|
723 "rtsig queue state: %d/%d",
|
|
724 rtsig_nr, rtsig_max);
|
|
725 while (ngx_rtsig_process_events(cycle) == NGX_OK) {
|
|
726 /* void */
|
|
727 }
|
|
728 }
|
|
729
|
|
730 } else {
|
|
731
|
|
732 /*
|
|
733 * Linux has not KERN_RTSIGMAX since 2.6.6-mm2
|
|
734 * so drain the rt signal queue unconditionally
|
|
735 */
|
|
736
|
|
737 while (ngx_rtsig_process_events(cycle) == NGX_OK) { /* void */ }
|
|
738 }
|
|
739
|
|
740 tested = 0;
|
|
741 }
|
|
742 }
|
|
743
|
|
744 if (ngx_posted_events) {
|
|
745 if (ngx_threaded) {
|
|
746 ngx_wakeup_worker_thread(cycle);
|
|
747
|
|
748 } else {
|
|
749 ngx_event_process_posted(cycle);
|
|
750 }
|
|
751 }
|
|
752
|
76
|
753 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
|
0
|
754 "rt signal queue overflow recovered");
|
|
755
|
|
756 overflow = 0;
|
|
757 ngx_event_actions.process_events = ngx_rtsig_process_events;
|
|
758
|
|
759 return NGX_OK;
|
|
760 }
|
|
761
|
|
762
|
48
|
763 static void *
|
|
764 ngx_rtsig_create_conf(ngx_cycle_t *cycle)
|
0
|
765 {
|
|
766 ngx_rtsig_conf_t *rtscf;
|
|
767
|
50
|
768 rtscf = ngx_palloc(cycle->pool, sizeof(ngx_rtsig_conf_t));
|
|
769 if (rtscf == NULL) {
|
|
770 return NGX_CONF_ERROR;
|
|
771 }
|
0
|
772
|
|
773 rtscf->signo = NGX_CONF_UNSET;
|
|
774 rtscf->overflow_events = NGX_CONF_UNSET;
|
|
775 rtscf->overflow_test = NGX_CONF_UNSET;
|
|
776 rtscf->overflow_threshold = NGX_CONF_UNSET;
|
|
777
|
|
778 return rtscf;
|
|
779 }
|
|
780
|
|
781
|
48
|
782 static char *
|
|
783 ngx_rtsig_init_conf(ngx_cycle_t *cycle, void *conf)
|
0
|
784 {
|
|
785 ngx_rtsig_conf_t *rtscf = conf;
|
|
786
|
|
787 /* LinuxThreads use the first 3 RT signals */
|
|
788 ngx_conf_init_value(rtscf->signo, SIGRTMIN + 10);
|
|
789
|
|
790 ngx_conf_init_value(rtscf->overflow_events, 16);
|
|
791 ngx_conf_init_value(rtscf->overflow_test, 32);
|
|
792 ngx_conf_init_value(rtscf->overflow_threshold, 10);
|
|
793
|
|
794 return NGX_CONF_OK;
|
|
795 }
|
|
796
|
|
797
|
48
|
798 static char *
|
|
799 ngx_check_ngx_overflow_threshold_bounds(ngx_conf_t *cf,
|
0
|
800 void *post, void *data)
|
|
801 {
|
|
802 if (ngx_linux_rtsig_max) {
|
|
803 return ngx_conf_check_num_bounds(cf, post, data);
|
|
804 }
|
|
805
|
|
806 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
807 "\"rtsig_overflow_threshold\" is not supported "
|
|
808 "since Linux 2.6.6-mm2, ignored");
|
|
809
|
|
810 return NGX_CONF_OK;
|
|
811 }
|