0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_event.h>
|
|
10
|
|
11
|
18
|
12 #if (NGX_TEST_BUILD_RTSIG)
|
0
|
13
|
|
14 #define F_SETSIG 10
|
|
15 #define SIGRTMIN 33
|
|
16 #define si_fd __spare__[0]
|
|
17 #define KERN_RTSIGNR 30
|
|
18 #define KERN_RTSIGMAX 31
|
|
19
|
|
20 int sigtimedwait(const sigset_t *set, siginfo_t *info,
|
|
21 const struct timespec *timeout)
|
|
22 {
|
|
23 return -1;
|
|
24 }
|
|
25
|
|
26 int ngx_linux_rtsig_max;
|
|
27
|
|
28 #endif
|
|
29
|
|
30
|
|
31 typedef struct {
|
324
|
32 ngx_uint_t signo;
|
|
33 ngx_uint_t overflow_events;
|
|
34 ngx_uint_t overflow_test;
|
|
35 ngx_uint_t overflow_threshold;
|
0
|
36 } ngx_rtsig_conf_t;
|
|
37
|
|
38
|
|
39 extern ngx_event_module_t ngx_poll_module_ctx;
|
|
40
|
112
|
41 static ngx_int_t ngx_rtsig_init(ngx_cycle_t *cycle, ngx_msec_t timer);
|
0
|
42 static void ngx_rtsig_done(ngx_cycle_t *cycle);
|
|
43 static ngx_int_t ngx_rtsig_add_connection(ngx_connection_t *c);
|
324
|
44 static ngx_int_t ngx_rtsig_del_connection(ngx_connection_t *c,
|
|
45 ngx_uint_t flags);
|
112
|
46 static ngx_int_t ngx_rtsig_process_events(ngx_cycle_t *cycle,
|
|
47 ngx_msec_t timer, ngx_uint_t flags);
|
|
48 static ngx_int_t ngx_rtsig_process_overflow(ngx_cycle_t *cycle,
|
|
49 ngx_msec_t timer, ngx_uint_t flags);
|
0
|
50
|
|
51 static void *ngx_rtsig_create_conf(ngx_cycle_t *cycle);
|
|
52 static char *ngx_rtsig_init_conf(ngx_cycle_t *cycle, void *conf);
|
|
53 static char *ngx_check_ngx_overflow_threshold_bounds(ngx_conf_t *cf,
|
48
|
54 void *post, void *data);
|
0
|
55
|
|
56
|
|
57 static sigset_t set;
|
|
58 static ngx_uint_t overflow, overflow_current;
|
|
59 static struct pollfd *overflow_list;
|
|
60
|
|
61
|
|
62 static ngx_str_t rtsig_name = ngx_string("rtsig");
|
|
63
|
|
64 static ngx_conf_num_bounds_t ngx_overflow_threshold_bounds = {
|
|
65 ngx_check_ngx_overflow_threshold_bounds, 2, 10
|
|
66 };
|
|
67
|
|
68
|
|
69 static ngx_command_t ngx_rtsig_commands[] = {
|
|
70
|
48
|
71 { ngx_string("rtsig_signo"),
|
|
72 NGX_EVENT_CONF|NGX_CONF_TAKE1,
|
|
73 ngx_conf_set_num_slot,
|
|
74 0,
|
|
75 offsetof(ngx_rtsig_conf_t, signo),
|
|
76 NULL },
|
0
|
77
|
48
|
78 { ngx_string("rtsig_overflow_events"),
|
|
79 NGX_EVENT_CONF|NGX_CONF_TAKE1,
|
|
80 ngx_conf_set_num_slot,
|
|
81 0,
|
|
82 offsetof(ngx_rtsig_conf_t, overflow_events),
|
|
83 NULL },
|
0
|
84
|
48
|
85 { ngx_string("rtsig_overflow_test"),
|
|
86 NGX_EVENT_CONF|NGX_CONF_TAKE1,
|
|
87 ngx_conf_set_num_slot,
|
|
88 0,
|
|
89 offsetof(ngx_rtsig_conf_t, overflow_test),
|
|
90 NULL },
|
0
|
91
|
48
|
92 { ngx_string("rtsig_overflow_threshold"),
|
|
93 NGX_EVENT_CONF|NGX_CONF_TAKE1,
|
|
94 ngx_conf_set_num_slot,
|
|
95 0,
|
|
96 offsetof(ngx_rtsig_conf_t, overflow_threshold),
|
|
97 &ngx_overflow_threshold_bounds },
|
0
|
98
|
48
|
99 ngx_null_command
|
0
|
100 };
|
|
101
|
|
102
|
|
103 ngx_event_module_t ngx_rtsig_module_ctx = {
|
|
104 &rtsig_name,
|
|
105 ngx_rtsig_create_conf, /* create configuration */
|
|
106 ngx_rtsig_init_conf, /* init configuration */
|
|
107
|
|
108 {
|
|
109 NULL, /* add an event */
|
|
110 NULL, /* delete an event */
|
|
111 NULL, /* enable an event */
|
|
112 NULL, /* disable an event */
|
|
113 ngx_rtsig_add_connection, /* add an connection */
|
|
114 ngx_rtsig_del_connection, /* delete an connection */
|
|
115 NULL, /* process the changes */
|
|
116 ngx_rtsig_process_events, /* process the events */
|
|
117 ngx_rtsig_init, /* init the events */
|
|
118 ngx_rtsig_done, /* done the events */
|
|
119 }
|
|
120
|
|
121 };
|
|
122
|
|
123 ngx_module_t ngx_rtsig_module = {
|
58
|
124 NGX_MODULE_V1,
|
0
|
125 &ngx_rtsig_module_ctx, /* module context */
|
|
126 ngx_rtsig_commands, /* module directives */
|
48
|
127 NGX_EVENT_MODULE, /* module type */
|
90
|
128 NULL, /* init master */
|
48
|
129 NULL, /* init module */
|
90
|
130 NULL, /* init process */
|
|
131 NULL, /* init thread */
|
|
132 NULL, /* exit thread */
|
|
133 NULL, /* exit process */
|
|
134 NULL, /* exit master */
|
|
135 NGX_MODULE_V1_PADDING
|
0
|
136 };
|
|
137
|
|
138
|
48
|
139 static ngx_int_t
|
112
|
140 ngx_rtsig_init(ngx_cycle_t *cycle, ngx_msec_t timer)
|
0
|
141 {
|
|
142 ngx_rtsig_conf_t *rtscf;
|
|
143
|
|
144 rtscf = ngx_event_get_conf(cycle->conf_ctx, ngx_rtsig_module);
|
|
145
|
|
146 sigemptyset(&set);
|
324
|
147 sigaddset(&set, (int) rtscf->signo);
|
|
148 sigaddset(&set, (int) rtscf->signo + 1);
|
0
|
149 sigaddset(&set, SIGIO);
|
112
|
150 sigaddset(&set, SIGALRM);
|
0
|
151
|
|
152 if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) {
|
|
153 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
|
154 "sigprocmask() failed");
|
|
155 return NGX_ERROR;
|
|
156 }
|
|
157
|
|
158 if (overflow_list) {
|
|
159 ngx_free(overflow_list);
|
|
160 }
|
|
161
|
|
162 overflow_list = ngx_alloc(sizeof(struct pollfd) * rtscf->overflow_events,
|
|
163 cycle->log);
|
|
164 if (overflow_list == NULL) {
|
|
165 return NGX_ERROR;
|
|
166 }
|
|
167
|
|
168 ngx_io = ngx_os_io;
|
|
169
|
|
170 ngx_event_actions = ngx_rtsig_module_ctx.actions;
|
|
171
|
92
|
172 ngx_event_flags = NGX_USE_RTSIG_EVENT
|
|
173 |NGX_USE_GREEDY_EVENT
|
|
174 |NGX_USE_FD_EVENT;
|
0
|
175
|
|
176 return NGX_OK;
|
|
177 }
|
|
178
|
|
179
|
48
|
180 static void
|
|
181 ngx_rtsig_done(ngx_cycle_t *cycle)
|
0
|
182 {
|
2
|
183 ngx_free(overflow_list);
|
|
184
|
|
185 overflow_list = NULL;
|
0
|
186 }
|
|
187
|
|
188
|
48
|
189 static ngx_int_t
|
|
190 ngx_rtsig_add_connection(ngx_connection_t *c)
|
0
|
191 {
|
324
|
192 ngx_uint_t signo;
|
0
|
193 ngx_rtsig_conf_t *rtscf;
|
|
194
|
|
195 if (c->read->accept && c->read->disabled) {
|
|
196
|
|
197 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
198 "rtsig enable connection: fd:%d", c->fd);
|
|
199
|
|
200 if (fcntl(c->fd, F_SETOWN, ngx_pid) == -1) {
|
|
201 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
|
|
202 "fcntl(F_SETOWN) failed");
|
|
203 return NGX_ERROR;
|
|
204 }
|
|
205
|
|
206 c->read->active = 1;
|
|
207 c->read->disabled = 0;
|
|
208 }
|
|
209
|
|
210 rtscf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_rtsig_module);
|
|
211
|
|
212 signo = rtscf->signo + c->read->instance;
|
|
213
|
|
214 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
324
|
215 "rtsig add connection: fd:%d signo:%ui", c->fd, signo);
|
0
|
216
|
|
217 if (fcntl(c->fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC) == -1) {
|
|
218 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
|
|
219 "fcntl(O_RDWR|O_NONBLOCK|O_ASYNC) failed");
|
|
220 return NGX_ERROR;
|
|
221 }
|
|
222
|
324
|
223 if (fcntl(c->fd, F_SETSIG, (int) signo) == -1) {
|
0
|
224 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
|
|
225 "fcntl(F_SETSIG) failed");
|
|
226 return NGX_ERROR;
|
|
227 }
|
|
228
|
|
229 if (fcntl(c->fd, F_SETOWN, ngx_pid) == -1) {
|
|
230 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
|
|
231 "fcntl(F_SETOWN) failed");
|
|
232 return NGX_ERROR;
|
|
233 }
|
|
234
|
18
|
235 #if (NGX_HAVE_ONESIGFD)
|
0
|
236 if (fcntl(c->fd, F_SETAUXFL, O_ONESIGFD) == -1) {
|
|
237 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
|
|
238 "fcntl(F_SETAUXFL) failed");
|
|
239 return NGX_ERROR;
|
|
240 }
|
|
241 #endif
|
|
242
|
|
243 c->read->active = 1;
|
|
244 c->write->active = 1;
|
|
245
|
|
246 return NGX_OK;
|
|
247 }
|
|
248
|
|
249
|
48
|
250 static ngx_int_t
|
324
|
251 ngx_rtsig_del_connection(ngx_connection_t *c, ngx_uint_t flags)
|
0
|
252 {
|
|
253 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
254 "rtsig del connection: fd:%d", c->fd);
|
|
255
|
|
256 if ((flags & NGX_DISABLE_EVENT) && c->read->accept) {
|
|
257
|
|
258 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
259 "rtsig disable connection: fd:%d", c->fd);
|
|
260
|
|
261 c->read->active = 0;
|
|
262 c->read->disabled = 1;
|
|
263 return NGX_OK;
|
|
264 }
|
|
265
|
|
266 if (flags & NGX_CLOSE_EVENT) {
|
|
267 c->read->active = 0;
|
|
268 c->write->active = 0;
|
|
269 return NGX_OK;
|
|
270 }
|
|
271
|
|
272 if (fcntl(c->fd, F_SETFL, O_RDWR|O_NONBLOCK) == -1) {
|
|
273 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
|
|
274 "fcntl(O_RDWR|O_NONBLOCK) failed");
|
|
275 return NGX_ERROR;
|
|
276 }
|
|
277
|
|
278 c->read->active = 0;
|
|
279 c->write->active = 0;
|
|
280
|
|
281 return NGX_OK;
|
|
282 }
|
|
283
|
|
284
|
48
|
285 static ngx_int_t
|
112
|
286 ngx_rtsig_process_events(ngx_cycle_t *cycle, ngx_msec_t timer, ngx_uint_t flags)
|
0
|
287 {
|
|
288 int signo;
|
50
|
289 ngx_int_t instance;
|
0
|
290 ngx_err_t err;
|
|
291 siginfo_t si;
|
122
|
292 ngx_event_t *rev, *wev, **queue;
|
0
|
293 struct timespec ts, *tp;
|
|
294 struct sigaction sa;
|
|
295 ngx_connection_t *c;
|
|
296 ngx_rtsig_conf_t *rtscf;
|
|
297
|
|
298 if (timer == NGX_TIMER_INFINITE) {
|
|
299 tp = NULL;
|
|
300
|
|
301 } else {
|
|
302 ts.tv_sec = timer / 1000;
|
|
303 ts.tv_nsec = (timer % 1000) * 1000000;
|
|
304 tp = &ts;
|
|
305 }
|
|
306
|
|
307 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
106
|
308 "rtsig timer: %M", timer);
|
0
|
309
|
|
310 /* Linux's sigwaitinfo() is sigtimedwait() with the NULL timeout pointer */
|
|
311
|
|
312 signo = sigtimedwait(&set, &si, tp);
|
|
313
|
|
314 if (signo == -1) {
|
|
315 err = ngx_errno;
|
|
316
|
|
317 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, err,
|
|
318 "rtsig signo:%d", signo);
|
|
319
|
116
|
320 if (flags & NGX_UPDATE_TIME) {
|
|
321 ngx_time_update(0, 0);
|
0
|
322 }
|
|
323
|
116
|
324 if (err == NGX_EAGAIN) {
|
|
325
|
|
326 /* timeout */
|
0
|
327
|
116
|
328 if (timer != NGX_TIMER_INFINITE) {
|
|
329 return NGX_AGAIN;
|
|
330 }
|
126
|
331
|
116
|
332 ngx_log_error(NGX_LOG_ALERT, cycle->log, err,
|
|
333 "sigtimedwait() returned EAGAIN without timeout");
|
|
334 return NGX_ERROR;
|
|
335 }
|
0
|
336
|
|
337 ngx_log_error((err == NGX_EINTR) ? NGX_LOG_INFO : NGX_LOG_ALERT,
|
|
338 cycle->log, err, "sigtimedwait() failed");
|
|
339 return NGX_ERROR;
|
|
340 }
|
|
341
|
116
|
342 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
|
343 "rtsig signo:%d fd:%d band:%04Xd",
|
|
344 signo, si.si_fd, si.si_band);
|
0
|
345
|
116
|
346 if (flags & NGX_UPDATE_TIME) {
|
|
347 ngx_time_update(0, 0);
|
0
|
348 }
|
|
349
|
|
350 rtscf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_rtsig_module);
|
|
351
|
324
|
352 if (signo == (int) rtscf->signo || signo == (int) rtscf->signo + 1) {
|
0
|
353
|
|
354 if (overflow && (ngx_uint_t) si.si_fd > overflow_current) {
|
|
355 return NGX_OK;
|
|
356 }
|
|
357
|
100
|
358 c = ngx_cycle->files[si.si_fd];
|
|
359
|
|
360 if (c == NULL) {
|
112
|
361
|
100
|
362 /* the stale event */
|
0
|
363
|
100
|
364 return NGX_OK;
|
|
365 }
|
0
|
366
|
324
|
367 instance = signo - (int) rtscf->signo;
|
0
|
368
|
|
369 rev = c->read;
|
|
370
|
112
|
371 if (rev->instance != instance) {
|
0
|
372
|
|
373 /*
|
|
374 * the stale event from a file descriptor
|
|
375 * that was just closed in this iteration
|
|
376 */
|
|
377
|
|
378 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
10
|
379 "rtsig: stale event %p", c);
|
0
|
380
|
|
381 return NGX_OK;
|
|
382 }
|
|
383
|
112
|
384 if ((si.si_band & (POLLIN|POLLHUP|POLLERR)) && rev->active) {
|
122
|
385
|
112
|
386 rev->ready = 1;
|
122
|
387
|
|
388 if (flags & NGX_POST_EVENTS) {
|
|
389 queue = (ngx_event_t **) (rev->accept ?
|
|
390 &ngx_posted_accept_events : &ngx_posted_events);
|
|
391
|
|
392 ngx_locked_post_event(rev, queue);
|
|
393
|
126
|
394 } else {
|
122
|
395 rev->handler(rev);
|
|
396 }
|
0
|
397 }
|
|
398
|
|
399 wev = c->write;
|
|
400
|
112
|
401 if ((si.si_band & (POLLOUT|POLLHUP|POLLERR)) && wev->active) {
|
122
|
402
|
112
|
403 wev->ready = 1;
|
122
|
404
|
|
405 if (flags & NGX_POST_EVENTS) {
|
|
406 ngx_locked_post_event(wev, &ngx_posted_events);
|
|
407
|
|
408 } else {
|
|
409 wev->handler(wev);
|
|
410 }
|
0
|
411 }
|
|
412
|
116
|
413 return NGX_OK;
|
|
414
|
112
|
415 } else if (signo == SIGALRM) {
|
|
416
|
116
|
417 ngx_time_update(0, 0);
|
|
418
|
112
|
419 return NGX_OK;
|
|
420
|
0
|
421 } else if (signo == SIGIO) {
|
|
422
|
|
423 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
|
|
424 "rt signal queue overflowed");
|
|
425
|
|
426 /* flush the RT signal queue */
|
|
427
|
|
428 ngx_memzero(&sa, sizeof(struct sigaction));
|
|
429 sa.sa_handler = SIG_DFL;
|
|
430 sigemptyset(&sa.sa_mask);
|
|
431
|
|
432 if (sigaction(rtscf->signo, &sa, NULL) == -1) {
|
|
433 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
434 "sigaction(%d, SIG_DFL) failed", rtscf->signo);
|
|
435 }
|
|
436
|
|
437 if (sigaction(rtscf->signo + 1, &sa, NULL) == -1) {
|
|
438 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
439 "sigaction(%d, SIG_DFL) failed", rtscf->signo + 1);
|
|
440 }
|
|
441
|
|
442 overflow = 1;
|
|
443 overflow_current = 0;
|
|
444 ngx_event_actions.process_events = ngx_rtsig_process_overflow;
|
|
445
|
|
446 return NGX_ERROR;
|
|
447
|
|
448 }
|
|
449
|
116
|
450 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
|
|
451 "sigtimedwait() returned unexpected signal: %d", signo);
|
0
|
452
|
116
|
453 return NGX_ERROR;
|
0
|
454 }
|
|
455
|
|
456
|
48
|
457 static ngx_int_t
|
112
|
458 ngx_rtsig_process_overflow(ngx_cycle_t *cycle, ngx_msec_t timer,
|
|
459 ngx_uint_t flags)
|
0
|
460 {
|
|
461 int name[2], rtsig_max, rtsig_nr, events, ready;
|
|
462 size_t len;
|
|
463 ngx_err_t err;
|
324
|
464 ngx_uint_t tested, n, i;
|
112
|
465 ngx_event_t *rev, *wev, **queue;
|
0
|
466 ngx_connection_t *c;
|
|
467 ngx_rtsig_conf_t *rtscf;
|
|
468
|
112
|
469 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
|
470 "rtsig process overflow");
|
|
471
|
0
|
472 rtscf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_rtsig_module);
|
|
473
|
|
474 tested = 0;
|
|
475
|
|
476 for ( ;; ) {
|
|
477
|
|
478 n = 0;
|
|
479 while (n < rtscf->overflow_events) {
|
|
480
|
|
481 if (overflow_current == cycle->connection_n) {
|
|
482 break;
|
|
483 }
|
|
484
|
92
|
485 c = cycle->files[overflow_current++];
|
0
|
486
|
108
|
487 if (c == NULL || c->fd == -1) {
|
0
|
488 continue;
|
|
489 }
|
|
490
|
|
491 events = 0;
|
|
492
|
58
|
493 if (c->read->active && c->read->handler) {
|
0
|
494 events |= POLLIN;
|
|
495 }
|
|
496
|
58
|
497 if (c->write->active && c->write->handler) {
|
0
|
498 events |= POLLOUT;
|
|
499 }
|
|
500
|
|
501 if (events == 0) {
|
|
502 continue;
|
|
503 }
|
|
504
|
|
505 overflow_list[n].fd = c->fd;
|
|
506 overflow_list[n].events = events;
|
|
507 overflow_list[n].revents = 0;
|
|
508 n++;
|
|
509 }
|
|
510
|
|
511 if (n == 0) {
|
|
512 break;
|
|
513 }
|
|
514
|
|
515 for ( ;; ) {
|
|
516 ready = poll(overflow_list, n, 0);
|
|
517
|
112
|
518 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
|
519 "rtsig overflow poll:%d", ready);
|
|
520
|
0
|
521 if (ready == -1) {
|
|
522 err = ngx_errno;
|
|
523 ngx_log_error((err == NGX_EINTR) ? NGX_LOG_INFO : NGX_LOG_ALERT,
|
|
524 cycle->log, 0,
|
|
525 "poll() failed while the overflow recover");
|
|
526
|
|
527 if (err == NGX_EINTR) {
|
|
528 continue;
|
|
529 }
|
|
530 }
|
|
531
|
|
532 break;
|
|
533 }
|
|
534
|
|
535 if (ready <= 0) {
|
|
536 continue;
|
|
537 }
|
|
538
|
112
|
539 ngx_mutex_lock(ngx_posted_events_mutex);
|
0
|
540
|
|
541 for (i = 0; i < n; i++) {
|
92
|
542 c = cycle->files[overflow_list[i].fd];
|
0
|
543
|
108
|
544 if (c == NULL) {
|
|
545 continue;
|
|
546 }
|
|
547
|
0
|
548 rev = c->read;
|
|
549
|
|
550 if (rev->active
|
|
551 && !rev->closed
|
58
|
552 && rev->handler
|
0
|
553 && (overflow_list[i].revents
|
|
554 & (POLLIN|POLLERR|POLLHUP|POLLNVAL)))
|
|
555 {
|
|
556 tested++;
|
|
557
|
112
|
558 if ((flags & NGX_POST_THREAD_EVENTS) && !rev->accept) {
|
0
|
559 rev->posted_ready = 1;
|
|
560
|
|
561 } else {
|
|
562 rev->ready = 1;
|
112
|
563 }
|
|
564
|
|
565 if (flags & NGX_POST_EVENTS) {
|
|
566 queue = (ngx_event_t **) (rev->accept ?
|
|
567 &ngx_posted_accept_events : &ngx_posted_events);
|
|
568
|
|
569 ngx_locked_post_event(rev, queue);
|
|
570
|
|
571 } else {
|
|
572 rev->handler(rev);
|
0
|
573 }
|
|
574 }
|
|
575
|
|
576 wev = c->write;
|
|
577
|
|
578 if (wev->active
|
|
579 && !wev->closed
|
58
|
580 && wev->handler
|
0
|
581 && (overflow_list[i].revents
|
|
582 & (POLLOUT|POLLERR|POLLHUP|POLLNVAL)))
|
|
583 {
|
|
584 tested++;
|
|
585
|
112
|
586 if (flags & NGX_POST_THREAD_EVENTS) {
|
0
|
587 wev->posted_ready = 1;
|
|
588
|
|
589 } else {
|
|
590 wev->ready = 1;
|
112
|
591 }
|
|
592
|
|
593 if (flags & NGX_POST_EVENTS) {
|
|
594 ngx_locked_post_event(wev, &ngx_posted_events);
|
|
595
|
|
596 } else {
|
|
597 wev->handler(wev);
|
0
|
598 }
|
|
599 }
|
|
600 }
|
|
601
|
|
602 ngx_mutex_unlock(ngx_posted_events_mutex);
|
|
603
|
|
604 if (tested >= rtscf->overflow_test) {
|
|
605
|
|
606 if (ngx_linux_rtsig_max) {
|
|
607
|
|
608 /*
|
|
609 * Check the current rt queue length to prevent
|
|
610 * the new overflow.
|
|
611 *
|
112
|
612 * learn the "/proc/sys/kernel/rtsig-max" value because
|
|
613 * it can be changed since the last checking
|
0
|
614 */
|
|
615
|
|
616 name[0] = CTL_KERN;
|
|
617 name[1] = KERN_RTSIGMAX;
|
|
618 len = sizeof(rtsig_max);
|
42
|
619
|
|
620 if (sysctl(name, 2, &rtsig_max, &len, NULL, 0) == -1) {
|
0
|
621 ngx_log_error(NGX_LOG_ALERT, cycle->log, errno,
|
|
622 "sysctl(KERN_RTSIGMAX) failed");
|
|
623 return NGX_ERROR;
|
|
624 }
|
|
625
|
42
|
626 /* name[0] = CTL_KERN; */
|
0
|
627 name[1] = KERN_RTSIGNR;
|
|
628 len = sizeof(rtsig_nr);
|
42
|
629
|
|
630 if (sysctl(name, 2, &rtsig_nr, &len, NULL, 0) == -1) {
|
0
|
631 ngx_log_error(NGX_LOG_ALERT, cycle->log, errno,
|
|
632 "sysctl(KERN_RTSIGNR) failed");
|
|
633 return NGX_ERROR;
|
|
634 }
|
|
635
|
|
636 /*
|
112
|
637 * drain the rt signal queue if the /"proc/sys/kernel/rtsig-nr"
|
0
|
638 * is bigger than
|
112
|
639 * "/proc/sys/kernel/rtsig-max" / "rtsig_overflow_threshold"
|
0
|
640 */
|
|
641
|
324
|
642 if (rtsig_max / (int) rtscf->overflow_threshold < rtsig_nr) {
|
0
|
643 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
|
644 "rtsig queue state: %d/%d",
|
|
645 rtsig_nr, rtsig_max);
|
112
|
646 while (ngx_rtsig_process_events(cycle, 0, flags) == NGX_OK)
|
|
647 {
|
0
|
648 /* void */
|
|
649 }
|
|
650 }
|
|
651
|
|
652 } else {
|
|
653
|
|
654 /*
|
|
655 * Linux has not KERN_RTSIGMAX since 2.6.6-mm2
|
|
656 * so drain the rt signal queue unconditionally
|
|
657 */
|
|
658
|
112
|
659 while (ngx_rtsig_process_events(cycle, 0, flags) == NGX_OK) {
|
|
660 /* void */
|
|
661 }
|
0
|
662 }
|
|
663
|
|
664 tested = 0;
|
|
665 }
|
|
666 }
|
|
667
|
112
|
668 if (flags & NGX_UPDATE_TIME) {
|
|
669 ngx_time_update(0, 0);
|
0
|
670 }
|
|
671
|
76
|
672 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
|
0
|
673 "rt signal queue overflow recovered");
|
|
674
|
|
675 overflow = 0;
|
|
676 ngx_event_actions.process_events = ngx_rtsig_process_events;
|
|
677
|
|
678 return NGX_OK;
|
|
679 }
|
|
680
|
|
681
|
48
|
682 static void *
|
|
683 ngx_rtsig_create_conf(ngx_cycle_t *cycle)
|
0
|
684 {
|
|
685 ngx_rtsig_conf_t *rtscf;
|
|
686
|
50
|
687 rtscf = ngx_palloc(cycle->pool, sizeof(ngx_rtsig_conf_t));
|
|
688 if (rtscf == NULL) {
|
|
689 return NGX_CONF_ERROR;
|
|
690 }
|
0
|
691
|
|
692 rtscf->signo = NGX_CONF_UNSET;
|
|
693 rtscf->overflow_events = NGX_CONF_UNSET;
|
|
694 rtscf->overflow_test = NGX_CONF_UNSET;
|
|
695 rtscf->overflow_threshold = NGX_CONF_UNSET;
|
|
696
|
|
697 return rtscf;
|
|
698 }
|
|
699
|
|
700
|
48
|
701 static char *
|
|
702 ngx_rtsig_init_conf(ngx_cycle_t *cycle, void *conf)
|
0
|
703 {
|
|
704 ngx_rtsig_conf_t *rtscf = conf;
|
|
705
|
|
706 /* LinuxThreads use the first 3 RT signals */
|
324
|
707 ngx_conf_init_uint_value(rtscf->signo, SIGRTMIN + 10);
|
0
|
708
|
324
|
709 ngx_conf_init_uint_value(rtscf->overflow_events, 16);
|
|
710 ngx_conf_init_uint_value(rtscf->overflow_test, 32);
|
|
711 ngx_conf_init_uint_value(rtscf->overflow_threshold, 10);
|
0
|
712
|
|
713 return NGX_CONF_OK;
|
|
714 }
|
|
715
|
|
716
|
48
|
717 static char *
|
324
|
718 ngx_check_ngx_overflow_threshold_bounds(ngx_conf_t *cf, void *post, void *data)
|
0
|
719 {
|
|
720 if (ngx_linux_rtsig_max) {
|
|
721 return ngx_conf_check_num_bounds(cf, post, data);
|
|
722 }
|
|
723
|
|
724 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
725 "\"rtsig_overflow_threshold\" is not supported "
|
|
726 "since Linux 2.6.6-mm2, ignored");
|
|
727
|
|
728 return NGX_CONF_OK;
|
|
729 }
|