blob: 9d1e1b9c32627acd9f6edd1cefc82bc7db03e43d [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 */
Igor Sysoevc78c41c2004-07-07 06:15:04 +0000108 NULL, /* process the changes */
Igor Sysoev9139cd22004-02-17 17:53:12 +0000109 ngx_rtsig_process_events, /* process the events */
110 ngx_rtsig_init, /* init the events */
111 ngx_rtsig_done, /* done the events */
112 }
113
114};
115
116ngx_module_t ngx_rtsig_module = {
117 NGX_MODULE,
118 &ngx_rtsig_module_ctx, /* module context */
119 ngx_rtsig_commands, /* module directives */
120 NGX_EVENT_MODULE, /* module type */
121 NULL, /* init module */
Igor Sysoevc78c41c2004-07-07 06:15:04 +0000122 NULL /* init process */
Igor Sysoev9139cd22004-02-17 17:53:12 +0000123};
124
125
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000126static ngx_int_t ngx_rtsig_init(ngx_cycle_t *cycle)
Igor Sysoev9139cd22004-02-17 17:53:12 +0000127{
128 ngx_rtsig_conf_t *rtscf;
129
Igor Sysoev22a7c502004-02-17 21:11:27 +0000130 if (ngx_poll_module_ctx.actions.init(cycle) == NGX_ERROR) {
131 return NGX_ERROR;
132 }
133
Igor Sysoev9139cd22004-02-17 17:53:12 +0000134 rtscf = ngx_event_get_conf(cycle->conf_ctx, ngx_rtsig_module);
135
136 sigemptyset(&set);
137 sigaddset(&set, rtscf->signo);
Igor Sysoev67f450d2004-06-01 06:04:46 +0000138 sigaddset(&set, rtscf->signo + 1);
Igor Sysoev9139cd22004-02-17 17:53:12 +0000139 sigaddset(&set, SIGIO);
140
141 if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) {
142 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
143 "sigprocmask() failed");
144 return NGX_ERROR;
145 }
146
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000147 if (overflow_list) {
148 ngx_free(overflow_list);
149 }
150
151 overflow_list = ngx_alloc(sizeof(struct pollfd) * rtscf->overflow_events,
152 cycle->log);
153 if (overflow_list == NULL) {
154 return NGX_ERROR;
155 }
156
Igor Sysoev9139cd22004-02-17 17:53:12 +0000157 ngx_io = ngx_os_io;
158
159 ngx_event_actions = ngx_rtsig_module_ctx.actions;
160
Igor Sysoev2b979932004-07-07 15:01:00 +0000161 ngx_event_flags = NGX_USE_RTSIG_EVENT|NGX_HAVE_GREEDY_EVENT;
Igor Sysoev9139cd22004-02-17 17:53:12 +0000162
163 return NGX_OK;
164}
165
166
167static void ngx_rtsig_done(ngx_cycle_t *cycle)
168{
Igor Sysoev22a7c502004-02-17 21:11:27 +0000169 ngx_poll_module_ctx.actions.done(cycle);
Igor Sysoev9139cd22004-02-17 17:53:12 +0000170}
171
172
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000173static ngx_int_t ngx_rtsig_add_connection(ngx_connection_t *c)
Igor Sysoev9139cd22004-02-17 17:53:12 +0000174{
Igor Sysoev67f450d2004-06-01 06:04:46 +0000175 int signo;
Igor Sysoev9139cd22004-02-17 17:53:12 +0000176 ngx_rtsig_conf_t *rtscf;
177
Igor Sysoev956ae652004-06-09 16:36:55 +0000178 if (c->read->accept && c->read->disabled) {
Igor Sysoev87350f22004-06-15 07:55:11 +0000179
180 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
181 "rtsig enable connection: fd:%d", c->fd);
182
Igor Sysoev956ae652004-06-09 16:36:55 +0000183 if (fcntl(c->fd, F_SETOWN, ngx_pid) == -1) {
184 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
185 "fcntl(F_SETOWN) failed");
186 return NGX_ERROR;
187 }
188
189 c->read->active = 1;
190 c->read->disabled = 0;
191 }
192
Igor Sysoev9139cd22004-02-17 17:53:12 +0000193 rtscf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_rtsig_module);
194
Igor Sysoev67f450d2004-06-01 06:04:46 +0000195 signo = rtscf->signo + c->read->instance;
196
Igor Sysoev9139cd22004-02-17 17:53:12 +0000197 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
Igor Sysoev67f450d2004-06-01 06:04:46 +0000198 "rtsig add connection: fd:%d signo:%d", c->fd, signo);
Igor Sysoev9139cd22004-02-17 17:53:12 +0000199
200 if (fcntl(c->fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC) == -1) {
201 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
202 "fcntl(O_RDWR|O_NONBLOCK|O_ASYNC) failed");
203 return NGX_ERROR;
204 }
205
Igor Sysoev67f450d2004-06-01 06:04:46 +0000206 if (fcntl(c->fd, F_SETSIG, signo) == -1) {
Igor Sysoev9139cd22004-02-17 17:53:12 +0000207 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
208 "fcntl(F_SETSIG) failed");
209 return NGX_ERROR;
210 }
211
Igor Sysoev67f450d2004-06-01 06:04:46 +0000212 if (fcntl(c->fd, F_SETOWN, ngx_pid) == -1) {
Igor Sysoev9139cd22004-02-17 17:53:12 +0000213 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
214 "fcntl(F_SETOWN) failed");
215 return NGX_ERROR;
216 }
217
218#if (HAVE_ONESIGFD)
219 if (fcntl(c->fd, F_SETAUXFL, O_ONESIGFD) == -1) {
220 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
221 "fcntl(F_SETAUXFL) failed");
222 return NGX_ERROR;
223 }
224#endif
225
226 c->read->active = 1;
227 c->write->active = 1;
228
229 return NGX_OK;
230}
231
232
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000233static ngx_int_t ngx_rtsig_del_connection(ngx_connection_t *c, u_int flags)
Igor Sysoev9139cd22004-02-17 17:53:12 +0000234{
Igor Sysoev67f450d2004-06-01 06:04:46 +0000235 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
236 "rtsig del connection: fd:%d", c->fd);
237
Igor Sysoev956ae652004-06-09 16:36:55 +0000238 if ((flags & NGX_DISABLE_EVENT) && c->read->accept) {
Igor Sysoev87350f22004-06-15 07:55:11 +0000239
240 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
241 "rtsig disable connection: fd:%d", c->fd);
242
Igor Sysoev956ae652004-06-09 16:36:55 +0000243 c->read->active = 0;
Igor Sysoev87350f22004-06-15 07:55:11 +0000244 c->read->disabled = 1;
Igor Sysoev956ae652004-06-09 16:36:55 +0000245 return NGX_OK;
246 }
247
248 if (flags & NGX_CLOSE_EVENT) {
249 c->read->active = 0;
250 c->write->active = 0;
251 return NGX_OK;
252 }
253
254 if (fcntl(c->fd, F_SETFL, O_RDWR|O_NONBLOCK) == -1) {
255 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
256 "fcntl(O_RDWR|O_NONBLOCK) failed");
257 return NGX_ERROR;
Igor Sysoev22a7c502004-02-17 21:11:27 +0000258 }
259
Igor Sysoev9139cd22004-02-17 17:53:12 +0000260 c->read->active = 0;
261 c->write->active = 0;
262
263 return NGX_OK;
264}
265
266
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000267ngx_int_t ngx_rtsig_process_events(ngx_cycle_t *cycle)
Igor Sysoev9139cd22004-02-17 17:53:12 +0000268{
269 int signo;
270 ngx_int_t instance, i;
Igor Sysoevcccc5522004-04-14 20:34:05 +0000271 ngx_uint_t expire;
Igor Sysoev9139cd22004-02-17 17:53:12 +0000272 size_t n;
273 ngx_msec_t timer;
274 ngx_err_t err;
Igor Sysoev9139cd22004-02-17 17:53:12 +0000275 siginfo_t si;
Igor Sysoev2b979932004-07-07 15:01:00 +0000276 ngx_event_t *rev, *wev;
Igor Sysoev9139cd22004-02-17 17:53:12 +0000277 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 Sysoev2b979932004-07-07 15:01:00 +0000292#if (NGX_THREADS)
293
294 if (timer == NGX_TIMER_ERROR) {
295 return NGX_ERROR;
296 }
297
298 if (timer == NGX_TIMER_INFINITE || timer > 500) {
299 timer = 500;
300 break;
301 }
302
303#endif
304
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000305 if (timer != 0) {
306 break;
Igor Sysoev732a2712004-04-21 18:54:33 +0000307 }
308
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000309 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
310 "rtsig expired timer");
311
312 ngx_event_expire_timers((ngx_msec_t)
313 (ngx_elapsed_msec - ngx_old_elapsed_msec));
Igor Sysoev2b979932004-07-07 15:01:00 +0000314
315 if (ngx_posted_events && ngx_threaded) {
316 ngx_wakeup_worker_thread(cycle);
317 }
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000318 }
319
320 expire = 1;
321
322 if (ngx_accept_mutex) {
323 if (ngx_accept_disabled > 0) {
324 ngx_accept_disabled--;
325
326 } else {
Igor Sysoev87350f22004-06-15 07:55:11 +0000327 ngx_accept_mutex_held = 0;
328
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000329 if (ngx_trylock_accept_mutex(cycle) == NGX_ERROR) {
330 return NGX_ERROR;
331 }
332
333 if (ngx_accept_mutex_held == 0
334 && (timer == NGX_TIMER_INFINITE
335 || timer > ngx_accept_mutex_delay))
336 {
337 timer = ngx_accept_mutex_delay;
338 expire = 0;
339 }
340 }
Igor Sysoevcccc5522004-04-14 20:34:05 +0000341 }
342 }
343
344 if (timer == NGX_TIMER_INFINITE) {
345 tp = NULL;
346 expire = 0;
347
348 } else {
Igor Sysoev9139cd22004-02-17 17:53:12 +0000349 ts.tv_sec = timer / 1000;
350 ts.tv_nsec = (timer % 1000) * 1000000;
Igor Sysoev22a7c502004-02-17 21:11:27 +0000351 tp = &ts;
Igor Sysoev9139cd22004-02-17 17:53:12 +0000352 }
353
Igor Sysoevcccc5522004-04-14 20:34:05 +0000354 ngx_old_elapsed_msec = ngx_elapsed_msec;
355
Igor Sysoevc972a3f2004-04-02 15:13:20 +0000356 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
357 "rtsig timer: %d", timer);
Igor Sysoev9139cd22004-02-17 17:53:12 +0000358
Igor Sysoev2b979932004-07-07 15:01:00 +0000359 /* Linux's sigwaitinfo() is sigtimedwait() with the NULL timeout pointer */
Igor Sysoev22a7c502004-02-17 21:11:27 +0000360
361 signo = sigtimedwait(&set, &si, tp);
Igor Sysoev9139cd22004-02-17 17:53:12 +0000362
363 if (signo == -1) {
364 err = ngx_errno;
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000365
366 if (err == NGX_EAGAIN) {
367
368 if (timer == NGX_TIMER_INFINITE) {
369 ngx_accept_mutex_unlock();
370 ngx_log_error(NGX_LOG_ALERT, cycle->log, err,
371 "sigtimedwait() returned EAGAIN without timeout");
372 return NGX_ERROR;
373 }
374
375 err = 0;
376 }
377
Igor Sysoev9139cd22004-02-17 17:53:12 +0000378 } else {
379 err = 0;
380 }
381
382 ngx_gettimeofday(&tv);
383 ngx_time_update(tv.tv_sec);
384
385 delta = ngx_elapsed_msec;
386 ngx_elapsed_msec = tv.tv_sec * 1000 + tv.tv_usec / 1000 - ngx_start_msec;
387
Igor Sysoev22a7c502004-02-17 21:11:27 +0000388 if (err) {
Igor Sysoev67f450d2004-06-01 06:04:46 +0000389 ngx_accept_mutex_unlock();
Igor Sysoev9139cd22004-02-17 17:53:12 +0000390 ngx_log_error((err == NGX_EINTR) ? NGX_LOG_INFO : NGX_LOG_ALERT,
Igor Sysoevc972a3f2004-04-02 15:13:20 +0000391 cycle->log, err, "sigtimedwait() failed");
Igor Sysoev9139cd22004-02-17 17:53:12 +0000392 return NGX_ERROR;
393 }
394
Igor Sysoevcccc5522004-04-14 20:34:05 +0000395 if (timer != NGX_TIMER_INFINITE) {
Igor Sysoev9139cd22004-02-17 17:53:12 +0000396 delta = ngx_elapsed_msec - delta;
397
Igor Sysoevc972a3f2004-04-02 15:13:20 +0000398 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
Igor Sysoev9139cd22004-02-17 17:53:12 +0000399 "rtsig timer: %d, delta: %d", timer, (int) delta);
400 }
401
Igor Sysoevc972a3f2004-04-02 15:13:20 +0000402 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
Igor Sysoev67f450d2004-06-01 06:04:46 +0000403 "rtsig signo:%d fd:%d band:%X", signo, si.si_fd, si.si_band);
Igor Sysoev9139cd22004-02-17 17:53:12 +0000404
405 rtscf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_rtsig_module);
406
Igor Sysoev67f450d2004-06-01 06:04:46 +0000407 if (signo == rtscf->signo || signo == rtscf->signo + 1) {
Igor Sysoev9139cd22004-02-17 17:53:12 +0000408
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000409 if (overflow && (ngx_uint_t) si.si_fd > overflow_current) {
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000410 return NGX_OK;
411 }
412
Igor Sysoev9139cd22004-02-17 17:53:12 +0000413 /* TODO: old_cycles */
Igor Sysoevc972a3f2004-04-02 15:13:20 +0000414
Igor Sysoev9139cd22004-02-17 17:53:12 +0000415 c = &ngx_cycle->connections[si.si_fd];
416
Igor Sysoev67f450d2004-06-01 06:04:46 +0000417 instance = signo - rtscf->signo;
418
Igor Sysoev2b979932004-07-07 15:01:00 +0000419 rev = c->read;
Igor Sysoev67f450d2004-06-01 06:04:46 +0000420
Igor Sysoev67f450d2004-06-01 06:04:46 +0000421 if (c->read->instance != instance) {
422
423 /*
424 * the stale event from a file descriptor
425 * that was just closed in this iteration
426 */
427
428 ngx_accept_mutex_unlock();
429
430 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
431 "rtsig: stale event " PTR_FMT, c);
432
433 return NGX_OK;
434 }
Igor Sysoev9139cd22004-02-17 17:53:12 +0000435
436 if (si.si_band & (POLLIN|POLLHUP|POLLERR)) {
Igor Sysoev2b979932004-07-07 15:01:00 +0000437 if (rev->active) {
Igor Sysoevcccc5522004-04-14 20:34:05 +0000438
Igor Sysoev2b979932004-07-07 15:01:00 +0000439 if (ngx_threaded && !rev->accept) {
Igor Sysoevcccc5522004-04-14 20:34:05 +0000440 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
441 ngx_accept_mutex_unlock();
442 return NGX_ERROR;
443 }
444
Igor Sysoev2b979932004-07-07 15:01:00 +0000445 rev->posted_ready = 1;
446 ngx_post_event(rev);
Igor Sysoevcccc5522004-04-14 20:34:05 +0000447
448 ngx_mutex_unlock(ngx_posted_events_mutex);
Igor Sysoev2b979932004-07-07 15:01:00 +0000449
450 } else {
451 rev->ready = 1;
452
453 if (!ngx_threaded && !ngx_accept_mutex_held) {
454 rev->event_handler(rev);
455
456 } else if (rev->accept) {
457 if (ngx_accept_disabled <= 0) {
458 rev->event_handler(rev);
459 }
460
461 } else {
462 ngx_post_event(rev);
463 }
Igor Sysoevcccc5522004-04-14 20:34:05 +0000464 }
Igor Sysoev9139cd22004-02-17 17:53:12 +0000465 }
466 }
467
Igor Sysoev2b979932004-07-07 15:01:00 +0000468 wev = c->write;
469
Igor Sysoev9139cd22004-02-17 17:53:12 +0000470 if (si.si_band & (POLLOUT|POLLHUP|POLLERR)) {
Igor Sysoev2b979932004-07-07 15:01:00 +0000471 if (wev->active) {
Igor Sysoevcccc5522004-04-14 20:34:05 +0000472
Igor Sysoev2b979932004-07-07 15:01:00 +0000473 if (ngx_threaded) {
Igor Sysoevcccc5522004-04-14 20:34:05 +0000474 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
475 ngx_accept_mutex_unlock();
476 return NGX_ERROR;
477 }
478
Igor Sysoev2b979932004-07-07 15:01:00 +0000479 wev->posted_ready = 1;
480 ngx_post_event(wev);
Igor Sysoevcccc5522004-04-14 20:34:05 +0000481
482 ngx_mutex_unlock(ngx_posted_events_mutex);
Igor Sysoev2b979932004-07-07 15:01:00 +0000483
484 } else {
485 wev->ready = 1;
486
487 if (!ngx_threaded && !ngx_accept_mutex_held) {
488 wev->event_handler(wev);
489
490 } else {
491 ngx_post_event(wev);
492 }
Igor Sysoevcccc5522004-04-14 20:34:05 +0000493 }
Igor Sysoev9139cd22004-02-17 17:53:12 +0000494 }
495 }
496
497 } else if (signo == SIGIO) {
Igor Sysoevcccc5522004-04-14 20:34:05 +0000498 ngx_accept_mutex_unlock();
499
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000500 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
501 "rt signal queue overflowed");
Igor Sysoev9139cd22004-02-17 17:53:12 +0000502
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000503 /* flush the RT signal queue */
Igor Sysoev22a7c502004-02-17 21:11:27 +0000504
Igor Sysoev9139cd22004-02-17 17:53:12 +0000505 ngx_memzero(&sa, sizeof(struct sigaction));
506 sa.sa_handler = SIG_DFL;
507 sigemptyset(&sa.sa_mask);
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000508
Igor Sysoev9139cd22004-02-17 17:53:12 +0000509 if (sigaction(rtscf->signo, &sa, NULL) == -1) {
Igor Sysoevc972a3f2004-04-02 15:13:20 +0000510 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
Igor Sysoev9139cd22004-02-17 17:53:12 +0000511 "sigaction(%d, SIG_DFL) failed", rtscf->signo);
512 }
513
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000514 if (sigaction(rtscf->signo + 1, &sa, NULL) == -1) {
515 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
516 "sigaction(%d, SIG_DFL) failed", rtscf->signo + 1);
517 }
518
519 overflow = 1;
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000520 overflow_current = 0;
Igor Sysoevc78c41c2004-07-07 06:15:04 +0000521 ngx_event_actions.process_events = ngx_rtsig_process_overflow;
Igor Sysoev22a7c502004-02-17 21:11:27 +0000522
Igor Sysoevd962b612004-06-11 06:15:08 +0000523 return NGX_ERROR;
Igor Sysoev22a7c502004-02-17 21:11:27 +0000524
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000525 } else if (signo != -1) {
Igor Sysoev67f450d2004-06-01 06:04:46 +0000526 ngx_accept_mutex_unlock();
527
Igor Sysoevc972a3f2004-04-02 15:13:20 +0000528 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
Igor Sysoev22a7c502004-02-17 21:11:27 +0000529 "sigtimedwait() returned unexpected signal: %d", signo);
Igor Sysoev67f450d2004-06-01 06:04:46 +0000530
Igor Sysoev9139cd22004-02-17 17:53:12 +0000531 return NGX_ERROR;
532 }
533
Igor Sysoevcccc5522004-04-14 20:34:05 +0000534 ngx_accept_mutex_unlock();
535
536 if (expire && delta) {
Igor Sysoev9139cd22004-02-17 17:53:12 +0000537 ngx_event_expire_timers((ngx_msec_t) delta);
538 }
539
Igor Sysoev2b979932004-07-07 15:01:00 +0000540 if (ngx_posted_events) {
541 if (ngx_threaded) {
542 ngx_wakeup_worker_thread(cycle);
543
544 } else {
545 ngx_event_process_posted(cycle);
546 }
Igor Sysoevcccc5522004-04-14 20:34:05 +0000547 }
548
Igor Sysoev956ae652004-06-09 16:36:55 +0000549 if (signo == -1) {
550 return NGX_AGAIN;
551 } else {
552 return NGX_OK;
553 }
Igor Sysoev9139cd22004-02-17 17:53:12 +0000554}
555
556
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000557/* TODO: old cylces */
558
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000559static ngx_int_t ngx_rtsig_process_overflow(ngx_cycle_t *cycle)
Igor Sysoev22a7c502004-02-17 21:11:27 +0000560{
Igor Sysoev87350f22004-06-15 07:55:11 +0000561 int name[2], rtsig_max, rtsig_nr, events, ready;
562 size_t len;
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000563 ngx_int_t tested, n, i;
564 ngx_err_t err;
Igor Sysoev2b979932004-07-07 15:01:00 +0000565 ngx_event_t *rev, *wev;
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000566 ngx_connection_t *c;
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000567 ngx_rtsig_conf_t *rtscf;
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000568
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000569 rtscf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_rtsig_module);
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000570
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000571 tested = 0;
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000572
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000573 for ( ;; ) {
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000574
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000575 n = 0;
576 while (n < rtscf->overflow_events) {
577
578 if (overflow_current == cycle->connection_n) {
579 break;
580 }
581
582 c = &cycle->connections[overflow_current++];
583
584 if (c->fd == -1) {
585 continue;
586 }
587
588 events = 0;
589
590 if (c->read->active && c->read->event_handler) {
591 events |= POLLIN;
592 }
593
594 if (c->write->active && c->write->event_handler) {
595 events |= POLLOUT;
596 }
597
598 if (events == 0) {
599 continue;
600 }
601
602 overflow_list[n].fd = c->fd;
603 overflow_list[n].events = events;
604 overflow_list[n].revents = 0;
605 n++;
606 }
607
608 if (n == 0) {
609 break;
610 }
611
612 for ( ;; ) {
613 ready = poll(overflow_list, n, 0);
614
615 if (ready == -1) {
616 err = ngx_errno;
617 ngx_log_error((err == NGX_EINTR) ? NGX_LOG_INFO : NGX_LOG_ALERT,
618 cycle->log, 0,
619 "poll() failed while the overflow recover");
620
Igor Sysoev2b979932004-07-07 15:01:00 +0000621 if (err != NGX_EINTR) {
622 break;
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000623 }
624 }
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000625 }
626
627 if (ready <= 0) {
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000628 continue;
629 }
630
Igor Sysoev2b979932004-07-07 15:01:00 +0000631 if (n) {
632 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
633 return NGX_ERROR;
634 }
635 }
636
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000637 for (i = 0; i < n; i++) {
638 c = &cycle->connections[overflow_list[i].fd];
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000639
Igor Sysoev2b979932004-07-07 15:01:00 +0000640 rev = c->read;
641
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000642 if (overflow_list[i].revents & (POLLIN|POLLERR|POLLHUP|POLLNVAL)) {
643 tested++;
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000644
Igor Sysoev2b979932004-07-07 15:01:00 +0000645 if (ngx_threaded) {
646 rev->posted_ready = 1;
647 ngx_post_event(rev);
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000648
649 } else {
Igor Sysoev2b979932004-07-07 15:01:00 +0000650 rev->ready = 1;
651 rev->event_handler(rev);
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000652 }
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000653 }
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000654
Igor Sysoev2b979932004-07-07 15:01:00 +0000655 wev = c->write;
656
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000657 if (overflow_list[i].revents & (POLLOUT|POLLERR|POLLHUP|POLLNVAL)) {
658 tested++;
Igor Sysoev2b979932004-07-07 15:01:00 +0000659
660 if (ngx_threaded) {
661 wev->posted_ready = 1;
662 ngx_post_event(wev);
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000663
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000664 } else {
Igor Sysoev2b979932004-07-07 15:01:00 +0000665 wev->ready = 1;
666 wev->event_handler(wev);
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000667 }
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000668 }
669 }
670
Igor Sysoev2b979932004-07-07 15:01:00 +0000671 if (n) {
672 ngx_mutex_unlock(ngx_posted_events_mutex);
673 }
674
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000675 if (tested >= rtscf->overflow_test) {
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000676
677 /*
678 * Check the current rt queue length to prevent the new overflow.
679 *
680 * Learn the /proc/sys/kernel/rtsig-max value because
Igor Sysoev5428ae72004-06-09 20:03:54 +0000681 * it can be changed sisnce the last checking.
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000682 */
683
684 name[0] = CTL_KERN;
685 name[1] = KERN_RTSIGMAX;
686 len = sizeof(rtsig_max);
687 if (sysctl(name, sizeof(name), &rtsig_max, &len, NULL, 0) == -1) {
688 ngx_log_error(NGX_LOG_ALERT, cycle->log, errno,
689 "sysctl(KERN_RTSIGMAX) failed");
690 return NGX_ERROR;
691 }
692
693 name[0] = CTL_KERN;
694 name[1] = KERN_RTSIGNR;
695 len = sizeof(rtsig_nr);
696 if (sysctl(name, sizeof(name), &rtsig_nr, &len, NULL, 0) == -1) {
697 ngx_log_error(NGX_LOG_ALERT, cycle->log, errno,
698 "sysctl(KERN_RTSIGNR) failed");
699 return NGX_ERROR;
700 }
701
702 /*
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000703 * drain rt signal queue if the /proc/sys/kernel/rtsig-nr is bigger
704 * than "/proc/sys/kernel/rtsig-max / rtsig_overflow_threshold"
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000705 */
706
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000707 if (rtsig_max / rtscf->overflow_threshold < rtsig_nr) {
Igor Sysoev5428ae72004-06-09 20:03:54 +0000708 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
709 "rtsig queue state: %d/%d", rtsig_nr, rtsig_max);
Igor Sysoev956ae652004-06-09 16:36:55 +0000710 while (ngx_rtsig_process_events(cycle) == NGX_OK) { /* void */ }
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000711 }
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000712
713 tested = 0;
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000714 }
Igor Sysoev22a7c502004-02-17 21:11:27 +0000715 }
716
Igor Sysoev2b979932004-07-07 15:01:00 +0000717 if (ngx_posted_events) {
718 if (ngx_threaded) {
719 ngx_wakeup_worker_thread(cycle);
720
721 } else {
722 ngx_event_process_posted(cycle);
723 }
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000724 }
725
726 ngx_log_error(NGX_LOG_INFO, cycle->log, 0,
727 "rt signal queue overflow recovered");
728
729 overflow = 0;
Igor Sysoevc78c41c2004-07-07 06:15:04 +0000730 ngx_event_actions.process_events = ngx_rtsig_process_events;
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000731
Igor Sysoev22a7c502004-02-17 21:11:27 +0000732 return NGX_OK;
733}
734
735
Igor Sysoev9139cd22004-02-17 17:53:12 +0000736static void *ngx_rtsig_create_conf(ngx_cycle_t *cycle)
737{
738 ngx_rtsig_conf_t *rtscf;
739
740 ngx_test_null(rtscf, ngx_palloc(cycle->pool, sizeof(ngx_rtsig_conf_t)),
741 NGX_CONF_ERROR);
742
743 rtscf->signo = NGX_CONF_UNSET;
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000744 rtscf->overflow_events = NGX_CONF_UNSET;
745 rtscf->overflow_test = NGX_CONF_UNSET;
746 rtscf->overflow_threshold = NGX_CONF_UNSET;
Igor Sysoev9139cd22004-02-17 17:53:12 +0000747
748 return rtscf;
749}
750
751
752static char *ngx_rtsig_init_conf(ngx_cycle_t *cycle, void *conf)
753{
754 ngx_rtsig_conf_t *rtscf = conf;
755
756 /* LinuxThreads use the first 3 RT signals */
757 ngx_conf_init_value(rtscf->signo, SIGRTMIN + 10);
758
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000759 ngx_conf_init_value(rtscf->overflow_events, 16);
Igor Sysoev87350f22004-06-15 07:55:11 +0000760 ngx_conf_init_value(rtscf->overflow_test, 32);
761 ngx_conf_init_value(rtscf->overflow_threshold, 10);
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000762
Igor Sysoev9139cd22004-02-17 17:53:12 +0000763 return NGX_CONF_OK;
764}