blob: 66b2454c4aeaaeff00594551374e9d1f1c37d5dc [file] [log] [blame]
Igor Sysoev9139cd22004-02-17 17:53:12 +00001
2/*
Igor Sysoevff8da912004-09-29 16:00:49 +00003 * Copyright (C) Igor Sysoev
Maxim Konovalovf8d59e32012-01-18 15:07:43 +00004 * Copyright (C) Nginx, Inc.
Igor Sysoev9139cd22004-02-17 17:53:12 +00005 */
6
7
8#include <ngx_config.h>
9#include <ngx_core.h>
10#include <ngx_event.h>
11
12
Igor Sysoevf6906042004-11-25 16:17:31 +000013#if (NGX_TEST_BUILD_RTSIG)
Igor Sysoev9139cd22004-02-17 17:53:12 +000014
Ruslan Ermilov1efcca32012-07-24 15:09:54 +000015#if (NGX_DARWIN)
16
17#define SIGRTMIN 33
18#define si_fd __pad[0]
19
20#else
21
Igor Sysoevb9186ad2008-03-13 15:47:14 +000022#ifdef SIGRTMIN
Igor Sysoev6e8bc2b2008-01-28 16:24:01 +000023#define si_fd _reason.__spare__.__spare2__[0]
Igor Sysoevb9186ad2008-03-13 15:47:14 +000024#else
25#define SIGRTMIN 33
26#define si_fd __spare__[0]
27#endif
28
Ruslan Ermilov1efcca32012-07-24 15:09:54 +000029#endif
30
Igor Sysoevb9186ad2008-03-13 15:47:14 +000031#define F_SETSIG 10
Igor Sysoevebfd6c82004-06-09 07:45:27 +000032#define KERN_RTSIGNR 30
33#define KERN_RTSIGMAX 31
Igor Sysoev9139cd22004-02-17 17:53:12 +000034
Igor Sysoev9139cd22004-02-17 17:53:12 +000035int sigtimedwait(const sigset_t *set, siginfo_t *info,
Ruslan Ermilov1efcca32012-07-24 15:09:54 +000036 const struct timespec *timeout);
37
38int sigtimedwait(const sigset_t *set, siginfo_t *info,
Igor Sysoev9139cd22004-02-17 17:53:12 +000039 const struct timespec *timeout)
40{
41 return -1;
42}
43
Igor Sysoev7823cc32004-07-14 16:01:42 +000044int ngx_linux_rtsig_max;
45
Igor Sysoev9139cd22004-02-17 17:53:12 +000046#endif
47
48
49typedef struct {
Igor Sysoev83a68512007-07-29 18:24:53 +000050 ngx_uint_t signo;
51 ngx_uint_t overflow_events;
52 ngx_uint_t overflow_test;
53 ngx_uint_t overflow_threshold;
Igor Sysoev9139cd22004-02-17 17:53:12 +000054} ngx_rtsig_conf_t;
55
56
Igor Sysoev22a7c502004-02-17 21:11:27 +000057extern ngx_event_module_t ngx_poll_module_ctx;
58
Igor Sysoevc2068d02005-10-19 12:33:58 +000059static ngx_int_t ngx_rtsig_init(ngx_cycle_t *cycle, ngx_msec_t timer);
Igor Sysoev9139cd22004-02-17 17:53:12 +000060static void ngx_rtsig_done(ngx_cycle_t *cycle);
Igor Sysoevebfd6c82004-06-09 07:45:27 +000061static ngx_int_t ngx_rtsig_add_connection(ngx_connection_t *c);
Igor Sysoev83a68512007-07-29 18:24:53 +000062static ngx_int_t ngx_rtsig_del_connection(ngx_connection_t *c,
63 ngx_uint_t flags);
Igor Sysoevc2068d02005-10-19 12:33:58 +000064static ngx_int_t ngx_rtsig_process_events(ngx_cycle_t *cycle,
65 ngx_msec_t timer, ngx_uint_t flags);
66static ngx_int_t ngx_rtsig_process_overflow(ngx_cycle_t *cycle,
67 ngx_msec_t timer, ngx_uint_t flags);
Igor Sysoev9139cd22004-02-17 17:53:12 +000068
69static void *ngx_rtsig_create_conf(ngx_cycle_t *cycle);
70static char *ngx_rtsig_init_conf(ngx_cycle_t *cycle, void *conf);
Igor Sysoev7823cc32004-07-14 16:01:42 +000071static char *ngx_check_ngx_overflow_threshold_bounds(ngx_conf_t *cf,
Igor Sysoev8184d1b2005-03-04 14:06:57 +000072 void *post, void *data);
Igor Sysoev9139cd22004-02-17 17:53:12 +000073
74
Igor Sysoev0ed19cc2004-06-10 18:36:57 +000075static sigset_t set;
76static ngx_uint_t overflow, overflow_current;
77static struct pollfd *overflow_list;
Igor Sysoev9139cd22004-02-17 17:53:12 +000078
79
80static ngx_str_t rtsig_name = ngx_string("rtsig");
81
Igor Sysoev0ed19cc2004-06-10 18:36:57 +000082static ngx_conf_num_bounds_t ngx_overflow_threshold_bounds = {
Igor Sysoev7823cc32004-07-14 16:01:42 +000083 ngx_check_ngx_overflow_threshold_bounds, 2, 10
Igor Sysoev0ed19cc2004-06-10 18:36:57 +000084};
85
86
Igor Sysoev9139cd22004-02-17 17:53:12 +000087static ngx_command_t ngx_rtsig_commands[] = {
88
Igor Sysoev8184d1b2005-03-04 14:06:57 +000089 { ngx_string("rtsig_signo"),
90 NGX_EVENT_CONF|NGX_CONF_TAKE1,
91 ngx_conf_set_num_slot,
92 0,
93 offsetof(ngx_rtsig_conf_t, signo),
94 NULL },
Igor Sysoev9139cd22004-02-17 17:53:12 +000095
Igor Sysoev8184d1b2005-03-04 14:06:57 +000096 { ngx_string("rtsig_overflow_events"),
97 NGX_EVENT_CONF|NGX_CONF_TAKE1,
98 ngx_conf_set_num_slot,
99 0,
100 offsetof(ngx_rtsig_conf_t, overflow_events),
101 NULL },
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000102
Igor Sysoev8184d1b2005-03-04 14:06:57 +0000103 { ngx_string("rtsig_overflow_test"),
104 NGX_EVENT_CONF|NGX_CONF_TAKE1,
105 ngx_conf_set_num_slot,
106 0,
107 offsetof(ngx_rtsig_conf_t, overflow_test),
108 NULL },
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000109
Igor Sysoev8184d1b2005-03-04 14:06:57 +0000110 { ngx_string("rtsig_overflow_threshold"),
111 NGX_EVENT_CONF|NGX_CONF_TAKE1,
112 ngx_conf_set_num_slot,
113 0,
114 offsetof(ngx_rtsig_conf_t, overflow_threshold),
115 &ngx_overflow_threshold_bounds },
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000116
Igor Sysoev8184d1b2005-03-04 14:06:57 +0000117 ngx_null_command
Igor Sysoev9139cd22004-02-17 17:53:12 +0000118};
119
120
121ngx_event_module_t ngx_rtsig_module_ctx = {
122 &rtsig_name,
123 ngx_rtsig_create_conf, /* create configuration */
124 ngx_rtsig_init_conf, /* init configuration */
125
126 {
127 NULL, /* add an event */
128 NULL, /* delete an event */
129 NULL, /* enable an event */
130 NULL, /* disable an event */
131 ngx_rtsig_add_connection, /* add an connection */
132 ngx_rtsig_del_connection, /* delete an connection */
Igor Sysoevc78c41c2004-07-07 06:15:04 +0000133 NULL, /* process the changes */
Igor Sysoev9139cd22004-02-17 17:53:12 +0000134 ngx_rtsig_process_events, /* process the events */
135 ngx_rtsig_init, /* init the events */
136 ngx_rtsig_done, /* done the events */
137 }
138
139};
140
141ngx_module_t ngx_rtsig_module = {
Igor Sysoev899b44e2005-05-12 14:58:06 +0000142 NGX_MODULE_V1,
Igor Sysoev9139cd22004-02-17 17:53:12 +0000143 &ngx_rtsig_module_ctx, /* module context */
144 ngx_rtsig_commands, /* module directives */
Igor Sysoev8184d1b2005-03-04 14:06:57 +0000145 NGX_EVENT_MODULE, /* module type */
Igor Sysoeve5733802005-09-08 14:36:09 +0000146 NULL, /* init master */
Igor Sysoev8184d1b2005-03-04 14:06:57 +0000147 NULL, /* init module */
Igor Sysoeve5733802005-09-08 14:36:09 +0000148 NULL, /* init process */
149 NULL, /* init thread */
150 NULL, /* exit thread */
151 NULL, /* exit process */
152 NULL, /* exit master */
153 NGX_MODULE_V1_PADDING
Igor Sysoev9139cd22004-02-17 17:53:12 +0000154};
155
156
Igor Sysoev8184d1b2005-03-04 14:06:57 +0000157static ngx_int_t
Igor Sysoevc2068d02005-10-19 12:33:58 +0000158ngx_rtsig_init(ngx_cycle_t *cycle, ngx_msec_t timer)
Igor Sysoev9139cd22004-02-17 17:53:12 +0000159{
160 ngx_rtsig_conf_t *rtscf;
161
162 rtscf = ngx_event_get_conf(cycle->conf_ctx, ngx_rtsig_module);
163
164 sigemptyset(&set);
Igor Sysoev83a68512007-07-29 18:24:53 +0000165 sigaddset(&set, (int) rtscf->signo);
166 sigaddset(&set, (int) rtscf->signo + 1);
Igor Sysoev9139cd22004-02-17 17:53:12 +0000167 sigaddset(&set, SIGIO);
Igor Sysoevc2068d02005-10-19 12:33:58 +0000168 sigaddset(&set, SIGALRM);
Igor Sysoev9139cd22004-02-17 17:53:12 +0000169
170 if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) {
171 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
172 "sigprocmask() failed");
173 return NGX_ERROR;
174 }
175
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000176 if (overflow_list) {
177 ngx_free(overflow_list);
178 }
179
180 overflow_list = ngx_alloc(sizeof(struct pollfd) * rtscf->overflow_events,
181 cycle->log);
182 if (overflow_list == NULL) {
183 return NGX_ERROR;
184 }
185
Igor Sysoev9139cd22004-02-17 17:53:12 +0000186 ngx_io = ngx_os_io;
187
188 ngx_event_actions = ngx_rtsig_module_ctx.actions;
189
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000190 ngx_event_flags = NGX_USE_RTSIG_EVENT
191 |NGX_USE_GREEDY_EVENT
192 |NGX_USE_FD_EVENT;
Igor Sysoev9139cd22004-02-17 17:53:12 +0000193
194 return NGX_OK;
195}
196
197
Igor Sysoev8184d1b2005-03-04 14:06:57 +0000198static void
199ngx_rtsig_done(ngx_cycle_t *cycle)
Igor Sysoev9139cd22004-02-17 17:53:12 +0000200{
Igor Sysoev924bd792004-10-11 15:07:03 +0000201 ngx_free(overflow_list);
202
203 overflow_list = NULL;
Igor Sysoev9139cd22004-02-17 17:53:12 +0000204}
205
206
Igor Sysoev8184d1b2005-03-04 14:06:57 +0000207static ngx_int_t
208ngx_rtsig_add_connection(ngx_connection_t *c)
Igor Sysoev9139cd22004-02-17 17:53:12 +0000209{
Igor Sysoev83a68512007-07-29 18:24:53 +0000210 ngx_uint_t signo;
Igor Sysoev9139cd22004-02-17 17:53:12 +0000211 ngx_rtsig_conf_t *rtscf;
212
Igor Sysoev956ae652004-06-09 16:36:55 +0000213 if (c->read->accept && c->read->disabled) {
Igor Sysoev87350f22004-06-15 07:55:11 +0000214
215 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
216 "rtsig enable connection: fd:%d", c->fd);
217
Igor Sysoev956ae652004-06-09 16:36:55 +0000218 if (fcntl(c->fd, F_SETOWN, ngx_pid) == -1) {
219 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
220 "fcntl(F_SETOWN) failed");
221 return NGX_ERROR;
222 }
223
224 c->read->active = 1;
225 c->read->disabled = 0;
226 }
227
Igor Sysoev9139cd22004-02-17 17:53:12 +0000228 rtscf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_rtsig_module);
229
Igor Sysoev67f450d2004-06-01 06:04:46 +0000230 signo = rtscf->signo + c->read->instance;
231
Igor Sysoev9139cd22004-02-17 17:53:12 +0000232 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
Igor Sysoev83a68512007-07-29 18:24:53 +0000233 "rtsig add connection: fd:%d signo:%ui", c->fd, signo);
Igor Sysoev9139cd22004-02-17 17:53:12 +0000234
235 if (fcntl(c->fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC) == -1) {
236 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
237 "fcntl(O_RDWR|O_NONBLOCK|O_ASYNC) failed");
238 return NGX_ERROR;
239 }
240
Igor Sysoev83a68512007-07-29 18:24:53 +0000241 if (fcntl(c->fd, F_SETSIG, (int) signo) == -1) {
Igor Sysoev9139cd22004-02-17 17:53:12 +0000242 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
243 "fcntl(F_SETSIG) failed");
244 return NGX_ERROR;
245 }
246
Igor Sysoev67f450d2004-06-01 06:04:46 +0000247 if (fcntl(c->fd, F_SETOWN, ngx_pid) == -1) {
Igor Sysoev9139cd22004-02-17 17:53:12 +0000248 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
249 "fcntl(F_SETOWN) failed");
250 return NGX_ERROR;
251 }
252
Igor Sysoevf6906042004-11-25 16:17:31 +0000253#if (NGX_HAVE_ONESIGFD)
Igor Sysoev9139cd22004-02-17 17:53:12 +0000254 if (fcntl(c->fd, F_SETAUXFL, O_ONESIGFD) == -1) {
255 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
256 "fcntl(F_SETAUXFL) failed");
257 return NGX_ERROR;
258 }
259#endif
260
261 c->read->active = 1;
262 c->write->active = 1;
263
264 return NGX_OK;
265}
266
267
Igor Sysoev8184d1b2005-03-04 14:06:57 +0000268static ngx_int_t
Igor Sysoev83a68512007-07-29 18:24:53 +0000269ngx_rtsig_del_connection(ngx_connection_t *c, ngx_uint_t flags)
Igor Sysoev9139cd22004-02-17 17:53:12 +0000270{
Igor Sysoev67f450d2004-06-01 06:04:46 +0000271 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
272 "rtsig del connection: fd:%d", c->fd);
273
Igor Sysoev956ae652004-06-09 16:36:55 +0000274 if ((flags & NGX_DISABLE_EVENT) && c->read->accept) {
Igor Sysoev87350f22004-06-15 07:55:11 +0000275
276 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
277 "rtsig disable connection: fd:%d", c->fd);
278
Igor Sysoev956ae652004-06-09 16:36:55 +0000279 c->read->active = 0;
Igor Sysoev87350f22004-06-15 07:55:11 +0000280 c->read->disabled = 1;
Igor Sysoev956ae652004-06-09 16:36:55 +0000281 return NGX_OK;
282 }
283
284 if (flags & NGX_CLOSE_EVENT) {
285 c->read->active = 0;
286 c->write->active = 0;
287 return NGX_OK;
288 }
289
290 if (fcntl(c->fd, F_SETFL, O_RDWR|O_NONBLOCK) == -1) {
291 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
292 "fcntl(O_RDWR|O_NONBLOCK) failed");
293 return NGX_ERROR;
Igor Sysoev22a7c502004-02-17 21:11:27 +0000294 }
295
Igor Sysoev9139cd22004-02-17 17:53:12 +0000296 c->read->active = 0;
297 c->write->active = 0;
298
299 return NGX_OK;
300}
301
302
Igor Sysoev8184d1b2005-03-04 14:06:57 +0000303static ngx_int_t
Igor Sysoevc2068d02005-10-19 12:33:58 +0000304ngx_rtsig_process_events(ngx_cycle_t *cycle, ngx_msec_t timer, ngx_uint_t flags)
Igor Sysoev9139cd22004-02-17 17:53:12 +0000305{
306 int signo;
Igor Sysoevc1571722005-03-19 12:38:37 +0000307 ngx_int_t instance;
Igor Sysoev9139cd22004-02-17 17:53:12 +0000308 ngx_err_t err;
Igor Sysoev9139cd22004-02-17 17:53:12 +0000309 siginfo_t si;
Igor Sysoev09c684b2005-11-09 17:25:55 +0000310 ngx_event_t *rev, *wev, **queue;
Igor Sysoev22a7c502004-02-17 21:11:27 +0000311 struct timespec ts, *tp;
Igor Sysoev9139cd22004-02-17 17:53:12 +0000312 struct sigaction sa;
Igor Sysoevcccc5522004-04-14 20:34:05 +0000313 ngx_connection_t *c;
Igor Sysoev9139cd22004-02-17 17:53:12 +0000314 ngx_rtsig_conf_t *rtscf;
315
Igor Sysoevcccc5522004-04-14 20:34:05 +0000316 if (timer == NGX_TIMER_INFINITE) {
317 tp = NULL;
Igor Sysoevcccc5522004-04-14 20:34:05 +0000318
319 } else {
Igor Sysoev9139cd22004-02-17 17:53:12 +0000320 ts.tv_sec = timer / 1000;
321 ts.tv_nsec = (timer % 1000) * 1000000;
Igor Sysoev22a7c502004-02-17 21:11:27 +0000322 tp = &ts;
Igor Sysoev9139cd22004-02-17 17:53:12 +0000323 }
324
Igor Sysoevc972a3f2004-04-02 15:13:20 +0000325 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
Igor Sysoev208eed22005-10-07 13:30:52 +0000326 "rtsig timer: %M", timer);
Igor Sysoev9139cd22004-02-17 17:53:12 +0000327
Igor Sysoev2b979932004-07-07 15:01:00 +0000328 /* Linux's sigwaitinfo() is sigtimedwait() with the NULL timeout pointer */
Igor Sysoev22a7c502004-02-17 21:11:27 +0000329
330 signo = sigtimedwait(&set, &si, tp);
Igor Sysoev9139cd22004-02-17 17:53:12 +0000331
332 if (signo == -1) {
333 err = ngx_errno;
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000334
Igor Sysoev8e811c12004-07-07 19:48:31 +0000335 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, err,
336 "rtsig signo:%d", signo);
337
Igor Sysoev055951d2005-10-21 19:12:18 +0000338 if (flags & NGX_UPDATE_TIME) {
Igor Sysoev6d45d8a2010-03-25 09:10:10 +0000339 ngx_time_update();
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000340 }
341
Igor Sysoev055951d2005-10-21 19:12:18 +0000342 if (err == NGX_EAGAIN) {
Igor Sysoev9139cd22004-02-17 17:53:12 +0000343
Igor Sysoev055951d2005-10-21 19:12:18 +0000344 /* timeout */
Igor Sysoev9139cd22004-02-17 17:53:12 +0000345
Igor Sysoev055951d2005-10-21 19:12:18 +0000346 if (timer != NGX_TIMER_INFINITE) {
347 return NGX_AGAIN;
348 }
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000349
Igor Sysoev055951d2005-10-21 19:12:18 +0000350 ngx_log_error(NGX_LOG_ALERT, cycle->log, err,
351 "sigtimedwait() returned EAGAIN without timeout");
352 return NGX_ERROR;
353 }
354
Igor Sysoev9139cd22004-02-17 17:53:12 +0000355 ngx_log_error((err == NGX_EINTR) ? NGX_LOG_INFO : NGX_LOG_ALERT,
Igor Sysoevc972a3f2004-04-02 15:13:20 +0000356 cycle->log, err, "sigtimedwait() failed");
Igor Sysoev9139cd22004-02-17 17:53:12 +0000357 return NGX_ERROR;
358 }
359
Igor Sysoev055951d2005-10-21 19:12:18 +0000360 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
361 "rtsig signo:%d fd:%d band:%04Xd",
362 signo, si.si_fd, si.si_band);
Igor Sysoev9139cd22004-02-17 17:53:12 +0000363
Igor Sysoev055951d2005-10-21 19:12:18 +0000364 if (flags & NGX_UPDATE_TIME) {
Igor Sysoev6d45d8a2010-03-25 09:10:10 +0000365 ngx_time_update();
Igor Sysoev9139cd22004-02-17 17:53:12 +0000366 }
367
Igor Sysoev9139cd22004-02-17 17:53:12 +0000368 rtscf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_rtsig_module);
369
Igor Sysoev83a68512007-07-29 18:24:53 +0000370 if (signo == (int) rtscf->signo || signo == (int) rtscf->signo + 1) {
Igor Sysoev9139cd22004-02-17 17:53:12 +0000371
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000372 if (overflow && (ngx_uint_t) si.si_fd > overflow_current) {
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000373 return NGX_OK;
374 }
375
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000376 c = ngx_cycle->files[si.si_fd];
Igor Sysoev9139cd22004-02-17 17:53:12 +0000377
Igor Sysoev82dc95e2005-10-03 12:53:14 +0000378 if (c == NULL) {
Igor Sysoev82dc95e2005-10-03 12:53:14 +0000379
Igor Sysoevc2068d02005-10-19 12:33:58 +0000380 /* the stale event */
Igor Sysoev82dc95e2005-10-03 12:53:14 +0000381
382 return NGX_OK;
383 }
384
Igor Sysoev83a68512007-07-29 18:24:53 +0000385 instance = signo - (int) rtscf->signo;
Igor Sysoev67f450d2004-06-01 06:04:46 +0000386
Igor Sysoev2b979932004-07-07 15:01:00 +0000387 rev = c->read;
Igor Sysoev67f450d2004-06-01 06:04:46 +0000388
Igor Sysoevc2068d02005-10-19 12:33:58 +0000389 if (rev->instance != instance) {
Igor Sysoev67f450d2004-06-01 06:04:46 +0000390
391 /*
392 * the stale event from a file descriptor
393 * that was just closed in this iteration
394 */
395
Igor Sysoev67f450d2004-06-01 06:04:46 +0000396 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
Igor Sysoev1b735832004-11-11 14:07:14 +0000397 "rtsig: stale event %p", c);
Igor Sysoev67f450d2004-06-01 06:04:46 +0000398
399 return NGX_OK;
400 }
Igor Sysoev9139cd22004-02-17 17:53:12 +0000401
Igor Sysoevc2068d02005-10-19 12:33:58 +0000402 if ((si.si_band & (POLLIN|POLLHUP|POLLERR)) && rev->active) {
Igor Sysoev09c684b2005-11-09 17:25:55 +0000403
Igor Sysoevc2068d02005-10-19 12:33:58 +0000404 rev->ready = 1;
Igor Sysoev09c684b2005-11-09 17:25:55 +0000405
406 if (flags & NGX_POST_EVENTS) {
Valentin Bartenev2a81e052014-09-01 18:20:03 +0400407 queue = rev->accept ? &ngx_posted_accept_events
408 : &ngx_posted_events;
Igor Sysoev09c684b2005-11-09 17:25:55 +0000409
Valentin Bartenev2a81e052014-09-01 18:20:03 +0400410 ngx_post_event(rev, queue);
Igor Sysoev09c684b2005-11-09 17:25:55 +0000411
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000412 } else {
Igor Sysoev09c684b2005-11-09 17:25:55 +0000413 rev->handler(rev);
414 }
Igor Sysoev9139cd22004-02-17 17:53:12 +0000415 }
416
Igor Sysoev2b979932004-07-07 15:01:00 +0000417 wev = c->write;
418
Igor Sysoevc2068d02005-10-19 12:33:58 +0000419 if ((si.si_band & (POLLOUT|POLLHUP|POLLERR)) && wev->active) {
Igor Sysoev09c684b2005-11-09 17:25:55 +0000420
Igor Sysoevc2068d02005-10-19 12:33:58 +0000421 wev->ready = 1;
Igor Sysoev09c684b2005-11-09 17:25:55 +0000422
423 if (flags & NGX_POST_EVENTS) {
Valentin Bartenev2a81e052014-09-01 18:20:03 +0400424 ngx_post_event(wev, &ngx_posted_events);
Igor Sysoev09c684b2005-11-09 17:25:55 +0000425
426 } else {
427 wev->handler(wev);
428 }
Igor Sysoev9139cd22004-02-17 17:53:12 +0000429 }
430
Igor Sysoev055951d2005-10-21 19:12:18 +0000431 return NGX_OK;
432
Igor Sysoevc2068d02005-10-19 12:33:58 +0000433 } else if (signo == SIGALRM) {
434
Igor Sysoev6d45d8a2010-03-25 09:10:10 +0000435 ngx_time_update();
Igor Sysoev055951d2005-10-21 19:12:18 +0000436
Igor Sysoevc2068d02005-10-19 12:33:58 +0000437 return NGX_OK;
438
Igor Sysoev9139cd22004-02-17 17:53:12 +0000439 } else if (signo == SIGIO) {
Igor Sysoevcccc5522004-04-14 20:34:05 +0000440
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000441 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
442 "rt signal queue overflowed");
Igor Sysoev9139cd22004-02-17 17:53:12 +0000443
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000444 /* flush the RT signal queue */
Igor Sysoev22a7c502004-02-17 21:11:27 +0000445
Igor Sysoev9139cd22004-02-17 17:53:12 +0000446 ngx_memzero(&sa, sizeof(struct sigaction));
447 sa.sa_handler = SIG_DFL;
448 sigemptyset(&sa.sa_mask);
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000449
Igor Sysoev9139cd22004-02-17 17:53:12 +0000450 if (sigaction(rtscf->signo, &sa, NULL) == -1) {
Igor Sysoevc972a3f2004-04-02 15:13:20 +0000451 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
Igor Sysoev9139cd22004-02-17 17:53:12 +0000452 "sigaction(%d, SIG_DFL) failed", rtscf->signo);
453 }
454
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000455 if (sigaction(rtscf->signo + 1, &sa, NULL) == -1) {
456 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
457 "sigaction(%d, SIG_DFL) failed", rtscf->signo + 1);
458 }
459
460 overflow = 1;
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000461 overflow_current = 0;
Igor Sysoevc78c41c2004-07-07 06:15:04 +0000462 ngx_event_actions.process_events = ngx_rtsig_process_overflow;
Igor Sysoev22a7c502004-02-17 21:11:27 +0000463
Igor Sysoevd962b612004-06-11 06:15:08 +0000464 return NGX_ERROR;
Igor Sysoev22a7c502004-02-17 21:11:27 +0000465
Igor Sysoev9139cd22004-02-17 17:53:12 +0000466 }
467
Igor Sysoev055951d2005-10-21 19:12:18 +0000468 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
469 "sigtimedwait() returned unexpected signal: %d", signo);
Igor Sysoevc2068d02005-10-19 12:33:58 +0000470
Igor Sysoev055951d2005-10-21 19:12:18 +0000471 return NGX_ERROR;
Igor Sysoev9139cd22004-02-17 17:53:12 +0000472}
473
474
Igor Sysoev8184d1b2005-03-04 14:06:57 +0000475static ngx_int_t
Igor Sysoevc2068d02005-10-19 12:33:58 +0000476ngx_rtsig_process_overflow(ngx_cycle_t *cycle, ngx_msec_t timer,
477 ngx_uint_t flags)
Igor Sysoev22a7c502004-02-17 21:11:27 +0000478{
Igor Sysoev87350f22004-06-15 07:55:11 +0000479 int name[2], rtsig_max, rtsig_nr, events, ready;
480 size_t len;
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000481 ngx_err_t err;
Igor Sysoev83a68512007-07-29 18:24:53 +0000482 ngx_uint_t tested, n, i;
Igor Sysoevc2068d02005-10-19 12:33:58 +0000483 ngx_event_t *rev, *wev, **queue;
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000484 ngx_connection_t *c;
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000485 ngx_rtsig_conf_t *rtscf;
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000486
Igor Sysoevc2068d02005-10-19 12:33:58 +0000487 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
488 "rtsig process overflow");
489
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000490 rtscf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_rtsig_module);
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000491
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000492 tested = 0;
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000493
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000494 for ( ;; ) {
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000495
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000496 n = 0;
497 while (n < rtscf->overflow_events) {
498
499 if (overflow_current == cycle->connection_n) {
500 break;
501 }
502
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000503 c = cycle->files[overflow_current++];
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000504
Igor Sysoev1bfa7bc2005-10-10 12:59:41 +0000505 if (c == NULL || c->fd == -1) {
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000506 continue;
507 }
508
509 events = 0;
510
Igor Sysoev899b44e2005-05-12 14:58:06 +0000511 if (c->read->active && c->read->handler) {
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000512 events |= POLLIN;
513 }
514
Igor Sysoev899b44e2005-05-12 14:58:06 +0000515 if (c->write->active && c->write->handler) {
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000516 events |= POLLOUT;
517 }
518
519 if (events == 0) {
520 continue;
521 }
522
523 overflow_list[n].fd = c->fd;
524 overflow_list[n].events = events;
525 overflow_list[n].revents = 0;
526 n++;
527 }
528
529 if (n == 0) {
530 break;
531 }
532
533 for ( ;; ) {
534 ready = poll(overflow_list, n, 0);
535
Igor Sysoevc2068d02005-10-19 12:33:58 +0000536 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
537 "rtsig overflow poll:%d", ready);
538
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000539 if (ready == -1) {
540 err = ngx_errno;
541 ngx_log_error((err == NGX_EINTR) ? NGX_LOG_INFO : NGX_LOG_ALERT,
542 cycle->log, 0,
543 "poll() failed while the overflow recover");
544
Igor Sysoev5ec68f62004-07-28 16:16:50 +0000545 if (err == NGX_EINTR) {
546 continue;
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000547 }
548 }
Igor Sysoev5ec68f62004-07-28 16:16:50 +0000549
550 break;
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000551 }
552
553 if (ready <= 0) {
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000554 continue;
555 }
556
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000557 for (i = 0; i < n; i++) {
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000558 c = cycle->files[overflow_list[i].fd];
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000559
Igor Sysoev1bfa7bc2005-10-10 12:59:41 +0000560 if (c == NULL) {
561 continue;
562 }
563
Igor Sysoev2b979932004-07-07 15:01:00 +0000564 rev = c->read;
565
Igor Sysoev846c27b2004-07-13 20:24:56 +0000566 if (rev->active
Igor Sysoev7823cc32004-07-14 16:01:42 +0000567 && !rev->closed
Igor Sysoev899b44e2005-05-12 14:58:06 +0000568 && rev->handler
Igor Sysoev846c27b2004-07-13 20:24:56 +0000569 && (overflow_list[i].revents
570 & (POLLIN|POLLERR|POLLHUP|POLLNVAL)))
571 {
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000572 tested++;
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000573
Valentin Bartenev2a81e052014-09-01 18:20:03 +0400574 rev->ready = 1;
Igor Sysoevc2068d02005-10-19 12:33:58 +0000575
576 if (flags & NGX_POST_EVENTS) {
Valentin Bartenev2a81e052014-09-01 18:20:03 +0400577 queue = rev->accept ? &ngx_posted_accept_events
578 : &ngx_posted_events;
Igor Sysoevc2068d02005-10-19 12:33:58 +0000579
Valentin Bartenev2a81e052014-09-01 18:20:03 +0400580 ngx_post_event(rev, queue);
Igor Sysoevc2068d02005-10-19 12:33:58 +0000581
582 } else {
583 rev->handler(rev);
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000584 }
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000585 }
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000586
Igor Sysoev2b979932004-07-07 15:01:00 +0000587 wev = c->write;
588
Igor Sysoev846c27b2004-07-13 20:24:56 +0000589 if (wev->active
Igor Sysoev7823cc32004-07-14 16:01:42 +0000590 && !wev->closed
Igor Sysoev899b44e2005-05-12 14:58:06 +0000591 && wev->handler
Igor Sysoev846c27b2004-07-13 20:24:56 +0000592 && (overflow_list[i].revents
593 & (POLLOUT|POLLERR|POLLHUP|POLLNVAL)))
594 {
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000595 tested++;
Igor Sysoev2b979932004-07-07 15:01:00 +0000596
Valentin Bartenev2a81e052014-09-01 18:20:03 +0400597 wev->ready = 1;
Igor Sysoevc2068d02005-10-19 12:33:58 +0000598
599 if (flags & NGX_POST_EVENTS) {
Valentin Bartenev2a81e052014-09-01 18:20:03 +0400600 ngx_post_event(wev, &ngx_posted_events);
Igor Sysoevc2068d02005-10-19 12:33:58 +0000601
602 } else {
603 wev->handler(wev);
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000604 }
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000605 }
606 }
607
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000608 if (tested >= rtscf->overflow_test) {
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000609
Igor Sysoev7823cc32004-07-14 16:01:42 +0000610 if (ngx_linux_rtsig_max) {
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000611
Igor Sysoev7823cc32004-07-14 16:01:42 +0000612 /*
613 * Check the current rt queue length to prevent
614 * the new overflow.
615 *
Igor Sysoevc2068d02005-10-19 12:33:58 +0000616 * learn the "/proc/sys/kernel/rtsig-max" value because
617 * it can be changed since the last checking
Igor Sysoev7823cc32004-07-14 16:01:42 +0000618 */
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000619
Igor Sysoev7823cc32004-07-14 16:01:42 +0000620 name[0] = CTL_KERN;
621 name[1] = KERN_RTSIGMAX;
622 len = sizeof(rtsig_max);
Igor Sysoevd039a2e2005-02-22 14:40:13 +0000623
624 if (sysctl(name, 2, &rtsig_max, &len, NULL, 0) == -1) {
Igor Sysoev7823cc32004-07-14 16:01:42 +0000625 ngx_log_error(NGX_LOG_ALERT, cycle->log, errno,
626 "sysctl(KERN_RTSIGMAX) failed");
627 return NGX_ERROR;
628 }
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000629
Igor Sysoevd039a2e2005-02-22 14:40:13 +0000630 /* name[0] = CTL_KERN; */
Igor Sysoev7823cc32004-07-14 16:01:42 +0000631 name[1] = KERN_RTSIGNR;
632 len = sizeof(rtsig_nr);
Igor Sysoevd039a2e2005-02-22 14:40:13 +0000633
634 if (sysctl(name, 2, &rtsig_nr, &len, NULL, 0) == -1) {
Igor Sysoev7823cc32004-07-14 16:01:42 +0000635 ngx_log_error(NGX_LOG_ALERT, cycle->log, errno,
636 "sysctl(KERN_RTSIGNR) failed");
637 return NGX_ERROR;
638 }
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000639
Igor Sysoev7823cc32004-07-14 16:01:42 +0000640 /*
Igor Sysoevc2068d02005-10-19 12:33:58 +0000641 * drain the rt signal queue if the /"proc/sys/kernel/rtsig-nr"
Igor Sysoev7823cc32004-07-14 16:01:42 +0000642 * is bigger than
Igor Sysoevc2068d02005-10-19 12:33:58 +0000643 * "/proc/sys/kernel/rtsig-max" / "rtsig_overflow_threshold"
Igor Sysoev7823cc32004-07-14 16:01:42 +0000644 */
645
Igor Sysoev83a68512007-07-29 18:24:53 +0000646 if (rtsig_max / (int) rtscf->overflow_threshold < rtsig_nr) {
Igor Sysoev7823cc32004-07-14 16:01:42 +0000647 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
648 "rtsig queue state: %d/%d",
649 rtsig_nr, rtsig_max);
Igor Sysoevc2068d02005-10-19 12:33:58 +0000650 while (ngx_rtsig_process_events(cycle, 0, flags) == NGX_OK)
651 {
Igor Sysoev7823cc32004-07-14 16:01:42 +0000652 /* void */
653 }
654 }
655
656 } else {
657
658 /*
659 * Linux has not KERN_RTSIGMAX since 2.6.6-mm2
Igor Sysoev5ec68f62004-07-28 16:16:50 +0000660 * so drain the rt signal queue unconditionally
Igor Sysoev7823cc32004-07-14 16:01:42 +0000661 */
662
Igor Sysoevc2068d02005-10-19 12:33:58 +0000663 while (ngx_rtsig_process_events(cycle, 0, flags) == NGX_OK) {
664 /* void */
665 }
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000666 }
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000667
668 tested = 0;
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000669 }
Igor Sysoev22a7c502004-02-17 21:11:27 +0000670 }
671
Igor Sysoevc2068d02005-10-19 12:33:58 +0000672 if (flags & NGX_UPDATE_TIME) {
Igor Sysoev6d45d8a2010-03-25 09:10:10 +0000673 ngx_time_update();
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000674 }
675
Igor Sysoev5192b362005-07-08 14:34:20 +0000676 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000677 "rt signal queue overflow recovered");
678
679 overflow = 0;
Igor Sysoevc78c41c2004-07-07 06:15:04 +0000680 ngx_event_actions.process_events = ngx_rtsig_process_events;
Igor Sysoevebfd6c82004-06-09 07:45:27 +0000681
Igor Sysoev22a7c502004-02-17 21:11:27 +0000682 return NGX_OK;
683}
684
685
Igor Sysoev8184d1b2005-03-04 14:06:57 +0000686static void *
687ngx_rtsig_create_conf(ngx_cycle_t *cycle)
Igor Sysoev9139cd22004-02-17 17:53:12 +0000688{
689 ngx_rtsig_conf_t *rtscf;
690
Igor Sysoevc1571722005-03-19 12:38:37 +0000691 rtscf = ngx_palloc(cycle->pool, sizeof(ngx_rtsig_conf_t));
692 if (rtscf == NULL) {
Igor Sysoev260c4322009-06-02 16:09:44 +0000693 return NULL;
Igor Sysoevc1571722005-03-19 12:38:37 +0000694 }
Igor Sysoev9139cd22004-02-17 17:53:12 +0000695
696 rtscf->signo = NGX_CONF_UNSET;
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000697 rtscf->overflow_events = NGX_CONF_UNSET;
698 rtscf->overflow_test = NGX_CONF_UNSET;
699 rtscf->overflow_threshold = NGX_CONF_UNSET;
Igor Sysoev9139cd22004-02-17 17:53:12 +0000700
701 return rtscf;
702}
703
704
Igor Sysoev8184d1b2005-03-04 14:06:57 +0000705static char *
706ngx_rtsig_init_conf(ngx_cycle_t *cycle, void *conf)
Igor Sysoev9139cd22004-02-17 17:53:12 +0000707{
708 ngx_rtsig_conf_t *rtscf = conf;
709
710 /* LinuxThreads use the first 3 RT signals */
Igor Sysoev83a68512007-07-29 18:24:53 +0000711 ngx_conf_init_uint_value(rtscf->signo, SIGRTMIN + 10);
Igor Sysoev9139cd22004-02-17 17:53:12 +0000712
Igor Sysoev83a68512007-07-29 18:24:53 +0000713 ngx_conf_init_uint_value(rtscf->overflow_events, 16);
714 ngx_conf_init_uint_value(rtscf->overflow_test, 32);
715 ngx_conf_init_uint_value(rtscf->overflow_threshold, 10);
Igor Sysoev0ed19cc2004-06-10 18:36:57 +0000716
Igor Sysoev9139cd22004-02-17 17:53:12 +0000717 return NGX_CONF_OK;
718}
Igor Sysoev7823cc32004-07-14 16:01:42 +0000719
720
Igor Sysoev8184d1b2005-03-04 14:06:57 +0000721static char *
Igor Sysoev83a68512007-07-29 18:24:53 +0000722ngx_check_ngx_overflow_threshold_bounds(ngx_conf_t *cf, void *post, void *data)
Igor Sysoev7823cc32004-07-14 16:01:42 +0000723{
724 if (ngx_linux_rtsig_max) {
725 return ngx_conf_check_num_bounds(cf, post, data);
726 }
727
728 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
729 "\"rtsig_overflow_threshold\" is not supported "
730 "since Linux 2.6.6-mm2, ignored");
731
732 return NGX_CONF_OK;
733}