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