3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
12 #if (NGX_TEST_BUILD_RTSIG)
15 #define si_fd _reason.__spare__.__spare2__[0]
18 #define si_fd __spare__[0]
22 #define KERN_RTSIGNR 30
23 #define KERN_RTSIGMAX 31
25 int sigtimedwait(const sigset_t *set, siginfo_t *info,
26 const struct timespec *timeout)
31 int ngx_linux_rtsig_max;
38 ngx_uint_t overflow_events;
39 ngx_uint_t overflow_test;
40 ngx_uint_t overflow_threshold;
44 extern ngx_event_module_t ngx_poll_module_ctx;
46 static ngx_int_t ngx_rtsig_init(ngx_cycle_t *cycle, ngx_msec_t timer);
47 static void ngx_rtsig_done(ngx_cycle_t *cycle);
48 static ngx_int_t ngx_rtsig_add_connection(ngx_connection_t *c);
49 static ngx_int_t ngx_rtsig_del_connection(ngx_connection_t *c,
51 static ngx_int_t ngx_rtsig_process_events(ngx_cycle_t *cycle,
52 ngx_msec_t timer, ngx_uint_t flags);
53 static ngx_int_t ngx_rtsig_process_overflow(ngx_cycle_t *cycle,
54 ngx_msec_t timer, ngx_uint_t flags);
56 static void *ngx_rtsig_create_conf(ngx_cycle_t *cycle);
57 static char *ngx_rtsig_init_conf(ngx_cycle_t *cycle, void *conf);
58 static char *ngx_check_ngx_overflow_threshold_bounds(ngx_conf_t *cf,
59 void *post, void *data);
63 static ngx_uint_t overflow, overflow_current;
64 static struct pollfd *overflow_list;
67 static ngx_str_t rtsig_name = ngx_string("rtsig");
69 static ngx_conf_num_bounds_t ngx_overflow_threshold_bounds = {
70 ngx_check_ngx_overflow_threshold_bounds, 2, 10
74 static ngx_command_t ngx_rtsig_commands[] = {
76 { ngx_string("rtsig_signo"),
77 NGX_EVENT_CONF|NGX_CONF_TAKE1,
78 ngx_conf_set_num_slot,
80 offsetof(ngx_rtsig_conf_t, signo),
83 { ngx_string("rtsig_overflow_events"),
84 NGX_EVENT_CONF|NGX_CONF_TAKE1,
85 ngx_conf_set_num_slot,
87 offsetof(ngx_rtsig_conf_t, overflow_events),
90 { ngx_string("rtsig_overflow_test"),
91 NGX_EVENT_CONF|NGX_CONF_TAKE1,
92 ngx_conf_set_num_slot,
94 offsetof(ngx_rtsig_conf_t, overflow_test),
97 { ngx_string("rtsig_overflow_threshold"),
98 NGX_EVENT_CONF|NGX_CONF_TAKE1,
99 ngx_conf_set_num_slot,
101 offsetof(ngx_rtsig_conf_t, overflow_threshold),
102 &ngx_overflow_threshold_bounds },
108 ngx_event_module_t ngx_rtsig_module_ctx = {
110 ngx_rtsig_create_conf, /* create configuration */
111 ngx_rtsig_init_conf, /* init configuration */
114 NULL, /* add an event */
115 NULL, /* delete an event */
116 NULL, /* enable an event */
117 NULL, /* disable an event */
118 ngx_rtsig_add_connection, /* add an connection */
119 ngx_rtsig_del_connection, /* delete an connection */
120 NULL, /* process the changes */
121 ngx_rtsig_process_events, /* process the events */
122 ngx_rtsig_init, /* init the events */
123 ngx_rtsig_done, /* done the events */
128 ngx_module_t ngx_rtsig_module = {
130 &ngx_rtsig_module_ctx, /* module context */
131 ngx_rtsig_commands, /* module directives */
132 NGX_EVENT_MODULE, /* module type */
133 NULL, /* init master */
134 NULL, /* init module */
135 NULL, /* init process */
136 NULL, /* init thread */
137 NULL, /* exit thread */
138 NULL, /* exit process */
139 NULL, /* exit master */
140 NGX_MODULE_V1_PADDING
145 ngx_rtsig_init(ngx_cycle_t *cycle, ngx_msec_t timer)
147 ngx_rtsig_conf_t *rtscf;
149 rtscf = ngx_event_get_conf(cycle->conf_ctx, ngx_rtsig_module);
152 sigaddset(&set, (int) rtscf->signo);
153 sigaddset(&set, (int) rtscf->signo + 1);
154 sigaddset(&set, SIGIO);
155 sigaddset(&set, SIGALRM);
157 if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) {
158 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
159 "sigprocmask() failed");
164 ngx_free(overflow_list);
167 overflow_list = ngx_alloc(sizeof(struct pollfd) * rtscf->overflow_events,
169 if (overflow_list == NULL) {
175 ngx_event_actions = ngx_rtsig_module_ctx.actions;
177 ngx_event_flags = NGX_USE_RTSIG_EVENT
178 |NGX_USE_GREEDY_EVENT
186 ngx_rtsig_done(ngx_cycle_t *cycle)
188 ngx_free(overflow_list);
190 overflow_list = NULL;
195 ngx_rtsig_add_connection(ngx_connection_t *c)
198 ngx_rtsig_conf_t *rtscf;
200 if (c->read->accept && c->read->disabled) {
202 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
203 "rtsig enable connection: fd:%d", c->fd);
205 if (fcntl(c->fd, F_SETOWN, ngx_pid) == -1) {
206 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
207 "fcntl(F_SETOWN) failed");
212 c->read->disabled = 0;
215 rtscf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_rtsig_module);
217 signo = rtscf->signo + c->read->instance;
219 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
220 "rtsig add connection: fd:%d signo:%ui", c->fd, signo);
222 if (fcntl(c->fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC) == -1) {
223 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
224 "fcntl(O_RDWR|O_NONBLOCK|O_ASYNC) failed");
228 if (fcntl(c->fd, F_SETSIG, (int) signo) == -1) {
229 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
230 "fcntl(F_SETSIG) failed");
234 if (fcntl(c->fd, F_SETOWN, ngx_pid) == -1) {
235 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
236 "fcntl(F_SETOWN) failed");
240 #if (NGX_HAVE_ONESIGFD)
241 if (fcntl(c->fd, F_SETAUXFL, O_ONESIGFD) == -1) {
242 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
243 "fcntl(F_SETAUXFL) failed");
249 c->write->active = 1;
256 ngx_rtsig_del_connection(ngx_connection_t *c, ngx_uint_t flags)
258 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
259 "rtsig del connection: fd:%d", c->fd);
261 if ((flags & NGX_DISABLE_EVENT) && c->read->accept) {
263 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
264 "rtsig disable connection: fd:%d", c->fd);
267 c->read->disabled = 1;
271 if (flags & NGX_CLOSE_EVENT) {
273 c->write->active = 0;
277 if (fcntl(c->fd, F_SETFL, O_RDWR|O_NONBLOCK) == -1) {
278 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
279 "fcntl(O_RDWR|O_NONBLOCK) failed");
284 c->write->active = 0;
291 ngx_rtsig_process_events(ngx_cycle_t *cycle, ngx_msec_t timer, ngx_uint_t flags)
297 ngx_event_t *rev, *wev, **queue;
298 struct timespec ts, *tp;
301 ngx_rtsig_conf_t *rtscf;
303 if (timer == NGX_TIMER_INFINITE) {
307 ts.tv_sec = timer / 1000;
308 ts.tv_nsec = (timer % 1000) * 1000000;
312 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
313 "rtsig timer: %M", timer);
315 /* Linux's sigwaitinfo() is sigtimedwait() with the NULL timeout pointer */
317 signo = sigtimedwait(&set, &si, tp);
322 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, err,
323 "rtsig signo:%d", signo);
325 if (flags & NGX_UPDATE_TIME) {
326 ngx_time_update(0, 0);
329 if (err == NGX_EAGAIN) {
333 if (timer != NGX_TIMER_INFINITE) {
337 ngx_log_error(NGX_LOG_ALERT, cycle->log, err,
338 "sigtimedwait() returned EAGAIN without timeout");
342 ngx_log_error((err == NGX_EINTR) ? NGX_LOG_INFO : NGX_LOG_ALERT,
343 cycle->log, err, "sigtimedwait() failed");
347 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
348 "rtsig signo:%d fd:%d band:%04Xd",
349 signo, si.si_fd, si.si_band);
351 if (flags & NGX_UPDATE_TIME) {
352 ngx_time_update(0, 0);
355 rtscf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_rtsig_module);
357 if (signo == (int) rtscf->signo || signo == (int) rtscf->signo + 1) {
359 if (overflow && (ngx_uint_t) si.si_fd > overflow_current) {
363 c = ngx_cycle->files[si.si_fd];
367 /* the stale event */
372 instance = signo - (int) rtscf->signo;
376 if (rev->instance != instance) {
379 * the stale event from a file descriptor
380 * that was just closed in this iteration
383 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
384 "rtsig: stale event %p", c);
389 if ((si.si_band & (POLLIN|POLLHUP|POLLERR)) && rev->active) {
393 if (flags & NGX_POST_EVENTS) {
394 queue = (ngx_event_t **) (rev->accept ?
395 &ngx_posted_accept_events : &ngx_posted_events);
397 ngx_locked_post_event(rev, queue);
406 if ((si.si_band & (POLLOUT|POLLHUP|POLLERR)) && wev->active) {
410 if (flags & NGX_POST_EVENTS) {
411 ngx_locked_post_event(wev, &ngx_posted_events);
420 } else if (signo == SIGALRM) {
422 ngx_time_update(0, 0);
426 } else if (signo == SIGIO) {
428 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
429 "rt signal queue overflowed");
431 /* flush the RT signal queue */
433 ngx_memzero(&sa, sizeof(struct sigaction));
434 sa.sa_handler = SIG_DFL;
435 sigemptyset(&sa.sa_mask);
437 if (sigaction(rtscf->signo, &sa, NULL) == -1) {
438 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
439 "sigaction(%d, SIG_DFL) failed", rtscf->signo);
442 if (sigaction(rtscf->signo + 1, &sa, NULL) == -1) {
443 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
444 "sigaction(%d, SIG_DFL) failed", rtscf->signo + 1);
448 overflow_current = 0;
449 ngx_event_actions.process_events = ngx_rtsig_process_overflow;
455 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
456 "sigtimedwait() returned unexpected signal: %d", signo);
463 ngx_rtsig_process_overflow(ngx_cycle_t *cycle, ngx_msec_t timer,
466 int name[2], rtsig_max, rtsig_nr, events, ready;
469 ngx_uint_t tested, n, i;
470 ngx_event_t *rev, *wev, **queue;
472 ngx_rtsig_conf_t *rtscf;
474 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
475 "rtsig process overflow");
477 rtscf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_rtsig_module);
484 while (n < rtscf->overflow_events) {
486 if (overflow_current == cycle->connection_n) {
490 c = cycle->files[overflow_current++];
492 if (c == NULL || c->fd == -1) {
498 if (c->read->active && c->read->handler) {
502 if (c->write->active && c->write->handler) {
510 overflow_list[n].fd = c->fd;
511 overflow_list[n].events = events;
512 overflow_list[n].revents = 0;
521 ready = poll(overflow_list, n, 0);
523 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
524 "rtsig overflow poll:%d", ready);
528 ngx_log_error((err == NGX_EINTR) ? NGX_LOG_INFO : NGX_LOG_ALERT,
530 "poll() failed while the overflow recover");
532 if (err == NGX_EINTR) {
544 ngx_mutex_lock(ngx_posted_events_mutex);
546 for (i = 0; i < n; i++) {
547 c = cycle->files[overflow_list[i].fd];
558 && (overflow_list[i].revents
559 & (POLLIN|POLLERR|POLLHUP|POLLNVAL)))
563 if ((flags & NGX_POST_THREAD_EVENTS) && !rev->accept) {
564 rev->posted_ready = 1;
570 if (flags & NGX_POST_EVENTS) {
571 queue = (ngx_event_t **) (rev->accept ?
572 &ngx_posted_accept_events : &ngx_posted_events);
574 ngx_locked_post_event(rev, queue);
586 && (overflow_list[i].revents
587 & (POLLOUT|POLLERR|POLLHUP|POLLNVAL)))
591 if (flags & NGX_POST_THREAD_EVENTS) {
592 wev->posted_ready = 1;
598 if (flags & NGX_POST_EVENTS) {
599 ngx_locked_post_event(wev, &ngx_posted_events);
607 ngx_mutex_unlock(ngx_posted_events_mutex);
609 if (tested >= rtscf->overflow_test) {
611 if (ngx_linux_rtsig_max) {
614 * Check the current rt queue length to prevent
617 * learn the "/proc/sys/kernel/rtsig-max" value because
618 * it can be changed since the last checking
622 name[1] = KERN_RTSIGMAX;
623 len = sizeof(rtsig_max);
625 if (sysctl(name, 2, &rtsig_max, &len, NULL, 0) == -1) {
626 ngx_log_error(NGX_LOG_ALERT, cycle->log, errno,
627 "sysctl(KERN_RTSIGMAX) failed");
631 /* name[0] = CTL_KERN; */
632 name[1] = KERN_RTSIGNR;
633 len = sizeof(rtsig_nr);
635 if (sysctl(name, 2, &rtsig_nr, &len, NULL, 0) == -1) {
636 ngx_log_error(NGX_LOG_ALERT, cycle->log, errno,
637 "sysctl(KERN_RTSIGNR) failed");
642 * drain the rt signal queue if the /"proc/sys/kernel/rtsig-nr"
644 * "/proc/sys/kernel/rtsig-max" / "rtsig_overflow_threshold"
647 if (rtsig_max / (int) rtscf->overflow_threshold < rtsig_nr) {
648 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
649 "rtsig queue state: %d/%d",
650 rtsig_nr, rtsig_max);
651 while (ngx_rtsig_process_events(cycle, 0, flags) == NGX_OK)
660 * Linux has not KERN_RTSIGMAX since 2.6.6-mm2
661 * so drain the rt signal queue unconditionally
664 while (ngx_rtsig_process_events(cycle, 0, flags) == NGX_OK) {
673 if (flags & NGX_UPDATE_TIME) {
674 ngx_time_update(0, 0);
677 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
678 "rt signal queue overflow recovered");
681 ngx_event_actions.process_events = ngx_rtsig_process_events;
688 ngx_rtsig_create_conf(ngx_cycle_t *cycle)
690 ngx_rtsig_conf_t *rtscf;
692 rtscf = ngx_palloc(cycle->pool, sizeof(ngx_rtsig_conf_t));
694 return NGX_CONF_ERROR;
697 rtscf->signo = NGX_CONF_UNSET;
698 rtscf->overflow_events = NGX_CONF_UNSET;
699 rtscf->overflow_test = NGX_CONF_UNSET;
700 rtscf->overflow_threshold = NGX_CONF_UNSET;
707 ngx_rtsig_init_conf(ngx_cycle_t *cycle, void *conf)
709 ngx_rtsig_conf_t *rtscf = conf;
711 /* LinuxThreads use the first 3 RT signals */
712 ngx_conf_init_uint_value(rtscf->signo, SIGRTMIN + 10);
714 ngx_conf_init_uint_value(rtscf->overflow_events, 16);
715 ngx_conf_init_uint_value(rtscf->overflow_test, 32);
716 ngx_conf_init_uint_value(rtscf->overflow_threshold, 10);
723 ngx_check_ngx_overflow_threshold_bounds(ngx_conf_t *cf, void *post, void *data)
725 if (ngx_linux_rtsig_max) {
726 return ngx_conf_check_num_bounds(cf, post, data);
729 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
730 "\"rtsig_overflow_threshold\" is not supported "
731 "since Linux 2.6.6-mm2, ignored");