blob: 5ab17ce156101bf179dd59e8a024d2f98658accf [file] [log] [blame]
Igor Sysoev9139cd22004-02-17 17:53:12 +00001
2/*
3 * Copyright (C) 2002-2004 Igor Sysoev, http://sysoev.ru/en/
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
Igor Sysoevebfd6c82004-06-09 07:45:27 +000014#define F_SETSIG 10
15#define SIGRTMIN 33
16#define si_fd __spare__[0]
17#define KERN_RTSIGNR 30
18#define KERN_RTSIGMAX 31
Igor Sysoev9139cd22004-02-17 17:53:12 +000019
Igor Sysoev9139cd22004-02-17 17:53:12 +000020int sigtimedwait(const sigset_t *set, siginfo_t *info,
Igor Sysoev9139cd22004-02-17 17:53:12 +000021 const struct timespec *timeout)
22{
23 return -1;
24}
25
26#endif
27
28
29typedef struct {
Igor Sysoev0ed19cc2004-06-10 18:36:57 +000030 int signo;
31 ngx_int_t overflow_events;
32 ngx_int_t overflow_test;
33 ngx_int_t overflow_threshold;
Igor Sysoev9139cd22004-02-17 17:53:12 +000034} ngx_rtsig_conf_t;
35
36
Igor Sysoev22a7c502004-02-17 21:11:27 +000037extern ngx_event_module_t ngx_poll_module_ctx;
38
Igor Sysoevebfd6c82004-06-09 07:45:27 +000039static ngx_int_t ngx_rtsig_init(ngx_cycle_t *cycle);
Igor Sysoev9139cd22004-02-17 17:53:12 +000040static void ngx_rtsig_done(ngx_cycle_t *cycle);
Igor Sysoevebfd6c82004-06-09 07:45:27 +000041static ngx_int_t ngx_rtsig_add_connection(ngx_connection_t *c);
42static ngx_int_t ngx_rtsig_del_connection(ngx_connection_t *c, u_int flags);
43static ngx_int_t ngx_rtsig_process_events(ngx_cycle_t *cycle);
44static ngx_int_t ngx_rtsig_process_overflow(ngx_cycle_t *cycle);
Igor Sysoev9139cd22004-02-17 17:53:12 +000045
46static void *ngx_rtsig_create_conf(ngx_cycle_t *cycle);
47static char *ngx_rtsig_init_conf(ngx_cycle_t *cycle, void *conf);
48
49
Igor Sysoev0ed19cc2004-06-10 18:36:57 +000050static sigset_t set;
51static ngx_uint_t overflow, overflow_current;
52static struct pollfd *overflow_list;
Igor Sysoev9139cd22004-02-17 17:53:12 +000053
54
55static ngx_str_t rtsig_name = ngx_string("rtsig");
56
Igor Sysoev0ed19cc2004-06-10 18:36:57 +000057static ngx_conf_num_bounds_t ngx_overflow_threshold_bounds = {
58 ngx_conf_check_num_bounds, 2, 10
59};
60
61
Igor Sysoev9139cd22004-02-17 17:53:12 +000062static ngx_command_t ngx_rtsig_commands[] = {
63
64 {ngx_string("rtsig_signo"),
65 NGX_EVENT_CONF|NGX_CONF_TAKE1,
66 ngx_conf_set_num_slot,
67 0,
68 offsetof(ngx_rtsig_conf_t, signo),
69 NULL},
70
Igor Sysoev0ed19cc2004-06-10 18:36:57 +000071 {ngx_string("rtsig_overflow_events"),
72 NGX_EVENT_CONF|NGX_CONF_TAKE1,
73 ngx_conf_set_num_slot,
74 0,
75 offsetof(ngx_rtsig_conf_t, overflow_events),
76 NULL},
77
78 {ngx_string("rtsig_overflow_test"),
79 NGX_EVENT_CONF|NGX_CONF_TAKE1,
80 ngx_conf_set_num_slot,
81 0,
82 offsetof(ngx_rtsig_conf_t, overflow_test),
83 NULL},
84
85 {ngx_string("rtsig_overflow_threshold"),
86 NGX_EVENT_CONF|NGX_CONF_TAKE1,
87 ngx_conf_set_num_slot,
88 0,
89 offsetof(ngx_rtsig_conf_t, overflow_threshold),
90 &ngx_overflow_threshold_bounds},
91
Igor Sysoev9139cd22004-02-17 17:53:12 +000092 ngx_null_command
93};
94
95
96ngx_event_module_t ngx_rtsig_module_ctx = {
97 &rtsig_name,
98 ngx_rtsig_create_conf, /* create configuration */
99 ngx_rtsig_init_conf, /* init configuration */
100
101 {
102 NULL, /* add an event */
103 NULL, /* delete an event */
104 NULL, /* enable an event */
105 NULL, /* disable an event */
106 ngx_rtsig_add_connection, /* add an connection */
107 ngx_rtsig_del_connection, /* delete an connection */
108 ngx_rtsig_process_events, /* process the events */
109 ngx_rtsig_init, /* init the events */
110 ngx_rtsig_done, /* done the events */
111 }
112
113};
114
115ngx_module_t ngx_rtsig_module = {
116 NGX_MODULE,
117 &ngx_rtsig_module_ctx, /* module context */
118 ngx_rtsig_commands, /* module directives */
119 NGX_EVENT_MODULE, /* module type */
120 NULL, /* init module */
121 NULL /* init child */
122};
123
124
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000125static ngx_int_t ngx_rtsig_init(ngx_cycle_t *cycle)
Igor Sysoev9139cd22004-02-17 17:53:12 +0000126{
127 ngx_rtsig_conf_t *rtscf;
128
Igor Sysoev22a7c502004-02-17 21:11:27 +0000129 if (ngx_poll_module_ctx.actions.init(cycle) == NGX_ERROR) {
130 return NGX_ERROR;
131 }
132
Igor Sysoev9139cd22004-02-17 17:53:12 +0000133 rtscf = ngx_event_get_conf(cycle->conf_ctx, ngx_rtsig_module);
134
135 sigemptyset(&set);
136 sigaddset(&set, rtscf->signo);
Igor Sysoev67f450d2004-06-01 06:04:46 +0000137 sigaddset(&set, rtscf->signo + 1);
Igor Sysoev9139cd22004-02-17 17:53:12 +0000138 sigaddset(&set, SIGIO);
139
140 if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) {
141 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
142 "sigprocmask() failed");
143 return NGX_ERROR;
144 }
145
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000146 if (overflow_list) {
147 ngx_free(overflow_list);
148 }
149
150 overflow_list = ngx_alloc(sizeof(struct pollfd) * rtscf->overflow_events,
151 cycle->log);
152 if (overflow_list == NULL) {
153 return NGX_ERROR;
154 }
155
Igor Sysoev9139cd22004-02-17 17:53:12 +0000156 ngx_io = ngx_os_io;
157
158 ngx_event_actions = ngx_rtsig_module_ctx.actions;
159
Igor Sysoev0ab91b92004-06-06 19:49:18 +0000160 ngx_event_flags = NGX_USE_RTSIG_EVENT
Igor Sysoev4cec79f2004-04-28 06:14:50 +0000161 |NGX_HAVE_GREEDY_EVENT
162 |NGX_HAVE_INSTANCE_EVENT;
Igor Sysoev9139cd22004-02-17 17:53:12 +0000163
164 return NGX_OK;
165}
166
167
168static void ngx_rtsig_done(ngx_cycle_t *cycle)
169{
Igor Sysoev22a7c502004-02-17 21:11:27 +0000170 ngx_poll_module_ctx.actions.done(cycle);
Igor Sysoev9139cd22004-02-17 17:53:12 +0000171}
172
173
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000174static ngx_int_t ngx_rtsig_add_connection(ngx_connection_t *c)
Igor Sysoev9139cd22004-02-17 17:53:12 +0000175{
Igor Sysoev67f450d2004-06-01 06:04:46 +0000176 int signo;
Igor Sysoev9139cd22004-02-17 17:53:12 +0000177 ngx_rtsig_conf_t *rtscf;
178
Igor Sysoev956ae652004-06-09 16:36:55 +0000179 if (c->read->accept && c->read->disabled) {
Igor Sysoev87350f22004-06-15 07:55:11 +0000180
181 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
182 "rtsig enable connection: fd:%d", c->fd);
183
Igor Sysoev956ae652004-06-09 16:36:55 +0000184 if (fcntl(c->fd, F_SETOWN, ngx_pid) == -1) {
185 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
186 "fcntl(F_SETOWN) failed");
187 return NGX_ERROR;
188 }
189
190 c->read->active = 1;
191 c->read->disabled = 0;
192 }
193
Igor Sysoev9139cd22004-02-17 17:53:12 +0000194 rtscf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_rtsig_module);
195
Igor Sysoev67f450d2004-06-01 06:04:46 +0000196 signo = rtscf->signo + c->read->instance;
197
Igor Sysoev9139cd22004-02-17 17:53:12 +0000198 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
Igor Sysoev67f450d2004-06-01 06:04:46 +0000199 "rtsig add connection: fd:%d signo:%d", c->fd, signo);
Igor Sysoev9139cd22004-02-17 17:53:12 +0000200
201 if (fcntl(c->fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC) == -1) {
202 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
203 "fcntl(O_RDWR|O_NONBLOCK|O_ASYNC) failed");
204 return NGX_ERROR;
205 }
206
Igor Sysoev67f450d2004-06-01 06:04:46 +0000207 if (fcntl(c->fd, F_SETSIG, signo) == -1) {
Igor Sysoev9139cd22004-02-17 17:53:12 +0000208 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
209 "fcntl(F_SETSIG) failed");
210 return NGX_ERROR;
211 }
212
Igor Sysoev67f450d2004-06-01 06:04:46 +0000213 if (fcntl(c->fd, F_SETOWN, ngx_pid) == -1) {
Igor Sysoev9139cd22004-02-17 17:53:12 +0000214 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
215 "fcntl(F_SETOWN) failed");
216 return NGX_ERROR;
217 }
218
219#if (HAVE_ONESIGFD)
220 if (fcntl(c->fd, F_SETAUXFL, O_ONESIGFD) == -1) {
221 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
222 "fcntl(F_SETAUXFL) failed");
223 return NGX_ERROR;
224 }
225#endif
226
227 c->read->active = 1;
228 c->write->active = 1;
229
230 return NGX_OK;
231}
232
233
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000234static ngx_int_t ngx_rtsig_del_connection(ngx_connection_t *c, u_int flags)
Igor Sysoev9139cd22004-02-17 17:53:12 +0000235{
Igor Sysoev67f450d2004-06-01 06:04:46 +0000236 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
237 "rtsig del connection: fd:%d", c->fd);
238
Igor Sysoev956ae652004-06-09 16:36:55 +0000239 if ((flags & NGX_DISABLE_EVENT) && c->read->accept) {
Igor Sysoev87350f22004-06-15 07:55:11 +0000240
241 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
242 "rtsig disable connection: fd:%d", c->fd);
243
Igor Sysoev956ae652004-06-09 16:36:55 +0000244 c->read->active = 0;
Igor Sysoev87350f22004-06-15 07:55:11 +0000245 c->read->disabled = 1;
Igor Sysoev956ae652004-06-09 16:36:55 +0000246 return NGX_OK;
247 }
248
249 if (flags & NGX_CLOSE_EVENT) {
250 c->read->active = 0;
251 c->write->active = 0;
252 return NGX_OK;
253 }
254
255 if (fcntl(c->fd, F_SETFL, O_RDWR|O_NONBLOCK) == -1) {
256 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
257 "fcntl(O_RDWR|O_NONBLOCK) failed");
258 return NGX_ERROR;
Igor Sysoev22a7c502004-02-17 21:11:27 +0000259 }
260
Igor Sysoev9139cd22004-02-17 17:53:12 +0000261 c->read->active = 0;
262 c->write->active = 0;
263
264 return NGX_OK;
265}
266
267
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000268ngx_int_t ngx_rtsig_process_events(ngx_cycle_t *cycle)
Igor Sysoev9139cd22004-02-17 17:53:12 +0000269{
270 int signo;
271 ngx_int_t instance, i;
Igor Sysoevcccc5522004-04-14 20:34:05 +0000272 ngx_uint_t expire;
Igor Sysoev9139cd22004-02-17 17:53:12 +0000273 size_t n;
274 ngx_msec_t timer;
275 ngx_err_t err;
Igor Sysoev9139cd22004-02-17 17:53:12 +0000276 siginfo_t si;
277 struct timeval tv;
Igor Sysoev22a7c502004-02-17 21:11:27 +0000278 struct timespec ts, *tp;
Igor Sysoev9139cd22004-02-17 17:53:12 +0000279 struct sigaction sa;
Igor Sysoev9139cd22004-02-17 17:53:12 +0000280 ngx_epoch_msec_t delta;
Igor Sysoevcccc5522004-04-14 20:34:05 +0000281 ngx_connection_t *c;
Igor Sysoev9139cd22004-02-17 17:53:12 +0000282 ngx_rtsig_conf_t *rtscf;
283
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000284 if (overflow) {
285 timer = 0;
286 expire = 0;
Igor Sysoev9139cd22004-02-17 17:53:12 +0000287
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000288 } else {
289 for ( ;; ) {
290 timer = ngx_event_find_timer();
Igor Sysoevcccc5522004-04-14 20:34:05 +0000291
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000292 if (timer != 0) {
293 break;
Igor Sysoev732a2712004-04-21 18:54:33 +0000294 }
295
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000296 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
297 "rtsig expired timer");
298
299 ngx_event_expire_timers((ngx_msec_t)
300 (ngx_elapsed_msec - ngx_old_elapsed_msec));
301 }
302
303 expire = 1;
304
305 if (ngx_accept_mutex) {
306 if (ngx_accept_disabled > 0) {
307 ngx_accept_disabled--;
308
309 } else {
Igor Sysoev87350f22004-06-15 07:55:11 +0000310 ngx_accept_mutex_held = 0;
311
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000312 if (ngx_trylock_accept_mutex(cycle) == NGX_ERROR) {
313 return NGX_ERROR;
314 }
315
316 if (ngx_accept_mutex_held == 0
317 && (timer == NGX_TIMER_INFINITE
318 || timer > ngx_accept_mutex_delay))
319 {
320 timer = ngx_accept_mutex_delay;
321 expire = 0;
322 }
323 }
Igor Sysoevcccc5522004-04-14 20:34:05 +0000324 }
325 }
326
327 if (timer == NGX_TIMER_INFINITE) {
328 tp = NULL;
329 expire = 0;
330
331 } else {
Igor Sysoev9139cd22004-02-17 17:53:12 +0000332 ts.tv_sec = timer / 1000;
333 ts.tv_nsec = (timer % 1000) * 1000000;
Igor Sysoev22a7c502004-02-17 21:11:27 +0000334 tp = &ts;
Igor Sysoev9139cd22004-02-17 17:53:12 +0000335 }
336
Igor Sysoevcccc5522004-04-14 20:34:05 +0000337 ngx_old_elapsed_msec = ngx_elapsed_msec;
338
Igor Sysoevc972a3f2004-04-02 15:13:20 +0000339 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
340 "rtsig timer: %d", timer);
Igor Sysoev9139cd22004-02-17 17:53:12 +0000341
Igor Sysoev22a7c502004-02-17 21:11:27 +0000342 /* Linux sigwaitinfo() is sigtimedwait() with the NULL timeout pointer */
343
344 signo = sigtimedwait(&set, &si, tp);
Igor Sysoev9139cd22004-02-17 17:53:12 +0000345
346 if (signo == -1) {
347 err = ngx_errno;
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000348
349 if (err == NGX_EAGAIN) {
350
351 if (timer == NGX_TIMER_INFINITE) {
352 ngx_accept_mutex_unlock();
353 ngx_log_error(NGX_LOG_ALERT, cycle->log, err,
354 "sigtimedwait() returned EAGAIN without timeout");
355 return NGX_ERROR;
356 }
357
358 err = 0;
359 }
360
Igor Sysoev9139cd22004-02-17 17:53:12 +0000361 } else {
362 err = 0;
363 }
364
365 ngx_gettimeofday(&tv);
366 ngx_time_update(tv.tv_sec);
367
368 delta = ngx_elapsed_msec;
369 ngx_elapsed_msec = tv.tv_sec * 1000 + tv.tv_usec / 1000 - ngx_start_msec;
370
Igor Sysoev22a7c502004-02-17 21:11:27 +0000371 if (err) {
Igor Sysoev67f450d2004-06-01 06:04:46 +0000372 ngx_accept_mutex_unlock();
Igor Sysoev9139cd22004-02-17 17:53:12 +0000373 ngx_log_error((err == NGX_EINTR) ? NGX_LOG_INFO : NGX_LOG_ALERT,
Igor Sysoevc972a3f2004-04-02 15:13:20 +0000374 cycle->log, err, "sigtimedwait() failed");
Igor Sysoev9139cd22004-02-17 17:53:12 +0000375 return NGX_ERROR;
376 }
377
Igor Sysoevcccc5522004-04-14 20:34:05 +0000378 if (timer != NGX_TIMER_INFINITE) {
Igor Sysoev9139cd22004-02-17 17:53:12 +0000379 delta = ngx_elapsed_msec - delta;
380
Igor Sysoevc972a3f2004-04-02 15:13:20 +0000381 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
Igor Sysoev9139cd22004-02-17 17:53:12 +0000382 "rtsig timer: %d, delta: %d", timer, (int) delta);
383 }
384
Igor Sysoevc972a3f2004-04-02 15:13:20 +0000385 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
Igor Sysoev67f450d2004-06-01 06:04:46 +0000386 "rtsig signo:%d fd:%d band:%X", signo, si.si_fd, si.si_band);
Igor Sysoev9139cd22004-02-17 17:53:12 +0000387
388 rtscf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_rtsig_module);
389
Igor Sysoev67f450d2004-06-01 06:04:46 +0000390 if (signo == rtscf->signo || signo == rtscf->signo + 1) {
Igor Sysoev9139cd22004-02-17 17:53:12 +0000391
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000392 if (overflow && (ngx_uint_t) si.si_fd > overflow_current) {
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000393 return NGX_OK;
394 }
395
Igor Sysoev9139cd22004-02-17 17:53:12 +0000396 /* TODO: old_cycles */
Igor Sysoevc972a3f2004-04-02 15:13:20 +0000397
Igor Sysoev9139cd22004-02-17 17:53:12 +0000398 c = &ngx_cycle->connections[si.si_fd];
399
Igor Sysoev67f450d2004-06-01 06:04:46 +0000400 instance = signo - rtscf->signo;
401
402 if (si.si_band & POLLIN) {
403 c->read->returned_instance = instance;
404 }
405
406 if (si.si_band & POLLOUT) {
407 c->write->returned_instance = instance;
408 }
409
410 if (c->read->instance != instance) {
411
412 /*
413 * the stale event from a file descriptor
414 * that was just closed in this iteration
415 */
416
417 ngx_accept_mutex_unlock();
418
419 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
420 "rtsig: stale event " PTR_FMT, c);
421
422 return NGX_OK;
423 }
Igor Sysoev9139cd22004-02-17 17:53:12 +0000424
425 if (si.si_band & (POLLIN|POLLHUP|POLLERR)) {
426 if (c->read->active) {
427 c->read->ready = 1;
Igor Sysoevcccc5522004-04-14 20:34:05 +0000428
429 if (!ngx_threaded && !ngx_accept_mutex_held) {
430 c->read->event_handler(c->read);
431
432 } else if (c->read->accept) {
Igor Sysoev67f450d2004-06-01 06:04:46 +0000433 if (ngx_accept_disabled <= 0) {
Igor Sysoev732a2712004-04-21 18:54:33 +0000434 c->read->event_handler(c->read);
435 }
Igor Sysoevcccc5522004-04-14 20:34:05 +0000436
437 } else {
438 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
439 ngx_accept_mutex_unlock();
440 return NGX_ERROR;
441 }
442
443 ngx_post_event(c->read);
444
445 ngx_mutex_unlock(ngx_posted_events_mutex);
446 }
Igor Sysoev9139cd22004-02-17 17:53:12 +0000447 }
448 }
449
450 if (si.si_band & (POLLOUT|POLLHUP|POLLERR)) {
451 if (c->write->active) {
452 c->write->ready = 1;
Igor Sysoevcccc5522004-04-14 20:34:05 +0000453
454 if (!ngx_threaded && !ngx_accept_mutex_held) {
455 c->write->event_handler(c->write);
456
457 } else {
458
459 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
460 ngx_accept_mutex_unlock();
461 return NGX_ERROR;
462 }
463
464 ngx_post_event(c->write);
465
466 ngx_mutex_unlock(ngx_posted_events_mutex);
467 }
Igor Sysoev9139cd22004-02-17 17:53:12 +0000468 }
469 }
470
471 } else if (signo == SIGIO) {
Igor Sysoevcccc5522004-04-14 20:34:05 +0000472 ngx_accept_mutex_unlock();
473
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000474 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
475 "rt signal queue overflowed");
Igor Sysoev9139cd22004-02-17 17:53:12 +0000476
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000477 /* flush the RT signal queue */
Igor Sysoev22a7c502004-02-17 21:11:27 +0000478
Igor Sysoev9139cd22004-02-17 17:53:12 +0000479 ngx_memzero(&sa, sizeof(struct sigaction));
480 sa.sa_handler = SIG_DFL;
481 sigemptyset(&sa.sa_mask);
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000482
Igor Sysoev9139cd22004-02-17 17:53:12 +0000483 if (sigaction(rtscf->signo, &sa, NULL) == -1) {
Igor Sysoevc972a3f2004-04-02 15:13:20 +0000484 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
Igor Sysoev9139cd22004-02-17 17:53:12 +0000485 "sigaction(%d, SIG_DFL) failed", rtscf->signo);
486 }
487
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000488 if (sigaction(rtscf->signo + 1, &sa, NULL) == -1) {
489 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
490 "sigaction(%d, SIG_DFL) failed", rtscf->signo + 1);
491 }
492
493 overflow = 1;
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000494 overflow_current = 0;
Igor Sysoev22a7c502004-02-17 21:11:27 +0000495 ngx_event_actions.process = ngx_rtsig_process_overflow;
Igor Sysoev22a7c502004-02-17 21:11:27 +0000496
Igor Sysoevd962b612004-06-11 06:15:08 +0000497 return NGX_ERROR;
Igor Sysoev22a7c502004-02-17 21:11:27 +0000498
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000499 } else if (signo != -1) {
Igor Sysoev67f450d2004-06-01 06:04:46 +0000500 ngx_accept_mutex_unlock();
501
Igor Sysoevc972a3f2004-04-02 15:13:20 +0000502 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
Igor Sysoev22a7c502004-02-17 21:11:27 +0000503 "sigtimedwait() returned unexpected signal: %d", signo);
Igor Sysoev67f450d2004-06-01 06:04:46 +0000504
Igor Sysoev9139cd22004-02-17 17:53:12 +0000505 return NGX_ERROR;
506 }
507
Igor Sysoevcccc5522004-04-14 20:34:05 +0000508 ngx_accept_mutex_unlock();
509
510 if (expire && delta) {
Igor Sysoev9139cd22004-02-17 17:53:12 +0000511 ngx_event_expire_timers((ngx_msec_t) delta);
512 }
513
Igor Sysoevcccc5522004-04-14 20:34:05 +0000514 if (!ngx_threaded) {
515 ngx_event_process_posted(cycle);
516 }
517
Igor Sysoev956ae652004-06-09 16:36:55 +0000518 if (signo == -1) {
519 return NGX_AGAIN;
520 } else {
521 return NGX_OK;
522 }
Igor Sysoev9139cd22004-02-17 17:53:12 +0000523}
524
525
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000526/* TODO: old cylces */
527
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000528static ngx_int_t ngx_rtsig_process_overflow(ngx_cycle_t *cycle)
Igor Sysoev22a7c502004-02-17 21:11:27 +0000529{
Igor Sysoev87350f22004-06-15 07:55:11 +0000530 int name[2], rtsig_max, rtsig_nr, events, ready;
531 size_t len;
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000532 ngx_int_t tested, n, i;
533 ngx_err_t err;
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000534 ngx_connection_t *c;
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000535 ngx_rtsig_conf_t *rtscf;
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000536
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000537 rtscf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_rtsig_module);
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000538
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000539 tested = 0;
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000540
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000541 for ( ;; ) {
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000542
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000543 n = 0;
544 while (n < rtscf->overflow_events) {
545
546 if (overflow_current == cycle->connection_n) {
547 break;
548 }
549
550 c = &cycle->connections[overflow_current++];
551
552 if (c->fd == -1) {
553 continue;
554 }
555
556 events = 0;
557
558 if (c->read->active && c->read->event_handler) {
559 events |= POLLIN;
560 }
561
562 if (c->write->active && c->write->event_handler) {
563 events |= POLLOUT;
564 }
565
566 if (events == 0) {
567 continue;
568 }
569
570 overflow_list[n].fd = c->fd;
571 overflow_list[n].events = events;
572 overflow_list[n].revents = 0;
573 n++;
574 }
575
576 if (n == 0) {
577 break;
578 }
579
580 for ( ;; ) {
581 ready = poll(overflow_list, n, 0);
582
583 if (ready == -1) {
584 err = ngx_errno;
585 ngx_log_error((err == NGX_EINTR) ? NGX_LOG_INFO : NGX_LOG_ALERT,
586 cycle->log, 0,
587 "poll() failed while the overflow recover");
588
589 if (err == NGX_EINTR) {
590 continue;
591 }
592 }
593
594 break;
595 }
596
597 if (ready <= 0) {
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000598 continue;
599 }
600
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000601 for (i = 0; i < n; i++) {
602 c = &cycle->connections[overflow_list[i].fd];
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000603
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000604 if (overflow_list[i].revents & (POLLIN|POLLERR|POLLHUP|POLLNVAL)) {
605 tested++;
606 c->read->ready = 1;
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000607
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000608 if (!ngx_threaded) {
609 c->read->event_handler(c->read);
610
611 } else {
612 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
613 return NGX_ERROR;
614 }
615
616 ngx_post_event(c->read);
617 c->read->returned_instance = c->read->instance;
618
619 ngx_mutex_unlock(ngx_posted_events_mutex);
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000620 }
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000621 }
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000622
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000623 if (overflow_list[i].revents & (POLLOUT|POLLERR|POLLHUP|POLLNVAL)) {
624 tested++;
625 c->write->ready = 1;
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000626
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000627 if (!ngx_threaded) {
628 c->write->event_handler(c->write);
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000629
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000630 } else {
631 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
632 return NGX_ERROR;
633 }
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000634
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000635 ngx_post_event(c->write);
636 c->write->returned_instance = c->write->instance;
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000637
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000638 ngx_mutex_unlock(ngx_posted_events_mutex);
639 }
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000640 }
641 }
642
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000643 if (tested >= rtscf->overflow_test) {
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000644
645 /*
646 * Check the current rt queue length to prevent the new overflow.
647 *
648 * Learn the /proc/sys/kernel/rtsig-max value because
Igor Sysoev5428ae72004-06-09 20:03:54 +0000649 * it can be changed sisnce the last checking.
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000650 */
651
652 name[0] = CTL_KERN;
653 name[1] = KERN_RTSIGMAX;
654 len = sizeof(rtsig_max);
655 if (sysctl(name, sizeof(name), &rtsig_max, &len, NULL, 0) == -1) {
656 ngx_log_error(NGX_LOG_ALERT, cycle->log, errno,
657 "sysctl(KERN_RTSIGMAX) failed");
658 return NGX_ERROR;
659 }
660
661 name[0] = CTL_KERN;
662 name[1] = KERN_RTSIGNR;
663 len = sizeof(rtsig_nr);
664 if (sysctl(name, sizeof(name), &rtsig_nr, &len, NULL, 0) == -1) {
665 ngx_log_error(NGX_LOG_ALERT, cycle->log, errno,
666 "sysctl(KERN_RTSIGNR) failed");
667 return NGX_ERROR;
668 }
669
670 /*
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000671 * drain rt signal queue if the /proc/sys/kernel/rtsig-nr is bigger
672 * than "/proc/sys/kernel/rtsig-max / rtsig_overflow_threshold"
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000673 */
674
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000675 if (rtsig_max / rtscf->overflow_threshold < rtsig_nr) {
Igor Sysoev5428ae72004-06-09 20:03:54 +0000676 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
677 "rtsig queue state: %d/%d", rtsig_nr, rtsig_max);
Igor Sysoev956ae652004-06-09 16:36:55 +0000678 while (ngx_rtsig_process_events(cycle) == NGX_OK) { /* void */ }
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000679 }
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000680
681 tested = 0;
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000682 }
Igor Sysoev22a7c502004-02-17 21:11:27 +0000683 }
684
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000685 if (!ngx_threaded) {
686 ngx_event_process_posted(cycle);
687 }
688
689 ngx_log_error(NGX_LOG_INFO, cycle->log, 0,
690 "rt signal queue overflow recovered");
691
692 overflow = 0;
693 ngx_event_actions.process = ngx_rtsig_process_events;
694
Igor Sysoev22a7c502004-02-17 21:11:27 +0000695 return NGX_OK;
696}
697
698
Igor Sysoev9139cd22004-02-17 17:53:12 +0000699static void *ngx_rtsig_create_conf(ngx_cycle_t *cycle)
700{
701 ngx_rtsig_conf_t *rtscf;
702
703 ngx_test_null(rtscf, ngx_palloc(cycle->pool, sizeof(ngx_rtsig_conf_t)),
704 NGX_CONF_ERROR);
705
706 rtscf->signo = NGX_CONF_UNSET;
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000707 rtscf->overflow_events = NGX_CONF_UNSET;
708 rtscf->overflow_test = NGX_CONF_UNSET;
709 rtscf->overflow_threshold = NGX_CONF_UNSET;
Igor Sysoev9139cd22004-02-17 17:53:12 +0000710
711 return rtscf;
712}
713
714
715static char *ngx_rtsig_init_conf(ngx_cycle_t *cycle, void *conf)
716{
717 ngx_rtsig_conf_t *rtscf = conf;
718
719 /* LinuxThreads use the first 3 RT signals */
720 ngx_conf_init_value(rtscf->signo, SIGRTMIN + 10);
721
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000722 ngx_conf_init_value(rtscf->overflow_events, 16);
Igor Sysoev87350f22004-06-15 07:55:11 +0000723 ngx_conf_init_value(rtscf->overflow_test, 32);
724 ngx_conf_init_value(rtscf->overflow_threshold, 10);
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000725
Igor Sysoev9139cd22004-02-17 17:53:12 +0000726 return NGX_CONF_OK;
727}