nginx-quic/src/event/modules/ngx_rtsig_module.c

769 lines
20 KiB
C
Raw Normal View History

2004-02-17 14:53:12 -03:00
/*
* Copyright (C) 2002-2004 Igor Sysoev, http://sysoev.ru/en/
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_event.h>
#if (TEST_BUILD_RTSIG)
2004-06-09 03:45:27 -04:00
#define F_SETSIG 10
#define SIGRTMIN 33
#define si_fd __spare__[0]
#define KERN_RTSIGNR 30
#define KERN_RTSIGMAX 31
2004-02-17 14:53:12 -03:00
int sigtimedwait(const sigset_t *set, siginfo_t *info,
const struct timespec *timeout)
{
return -1;
}
#endif
typedef struct {
2004-06-10 14:36:57 -04:00
int signo;
ngx_int_t overflow_events;
ngx_int_t overflow_test;
ngx_int_t overflow_threshold;
2004-02-17 14:53:12 -03:00
} ngx_rtsig_conf_t;
2004-02-17 18:11:27 -03:00
extern ngx_event_module_t ngx_poll_module_ctx;
2004-06-09 03:45:27 -04:00
static ngx_int_t ngx_rtsig_init(ngx_cycle_t *cycle);
2004-02-17 14:53:12 -03:00
static void ngx_rtsig_done(ngx_cycle_t *cycle);
2004-06-09 03:45:27 -04:00
static ngx_int_t ngx_rtsig_add_connection(ngx_connection_t *c);
static ngx_int_t ngx_rtsig_del_connection(ngx_connection_t *c, u_int flags);
static ngx_int_t ngx_rtsig_process_events(ngx_cycle_t *cycle);
static ngx_int_t ngx_rtsig_process_overflow(ngx_cycle_t *cycle);
2004-02-17 14:53:12 -03:00
static void *ngx_rtsig_create_conf(ngx_cycle_t *cycle);
static char *ngx_rtsig_init_conf(ngx_cycle_t *cycle, void *conf);
2004-06-10 14:36:57 -04:00
static sigset_t set;
static ngx_uint_t overflow, overflow_current;
static struct pollfd *overflow_list;
2004-02-17 14:53:12 -03:00
static ngx_str_t rtsig_name = ngx_string("rtsig");
2004-06-10 14:36:57 -04:00
static ngx_conf_num_bounds_t ngx_overflow_threshold_bounds = {
ngx_conf_check_num_bounds, 2, 10
};
2004-02-17 14:53:12 -03:00
static ngx_command_t ngx_rtsig_commands[] = {
{ngx_string("rtsig_signo"),
NGX_EVENT_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
0,
offsetof(ngx_rtsig_conf_t, signo),
NULL},
2004-06-10 14:36:57 -04:00
{ngx_string("rtsig_overflow_events"),
NGX_EVENT_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
0,
offsetof(ngx_rtsig_conf_t, overflow_events),
NULL},
{ngx_string("rtsig_overflow_test"),
NGX_EVENT_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
0,
offsetof(ngx_rtsig_conf_t, overflow_test),
NULL},
{ngx_string("rtsig_overflow_threshold"),
NGX_EVENT_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
0,
offsetof(ngx_rtsig_conf_t, overflow_threshold),
&ngx_overflow_threshold_bounds},
2004-02-17 14:53:12 -03:00
ngx_null_command
};
ngx_event_module_t ngx_rtsig_module_ctx = {
&rtsig_name,
ngx_rtsig_create_conf, /* create configuration */
ngx_rtsig_init_conf, /* init configuration */
{
NULL, /* add an event */
NULL, /* delete an event */
NULL, /* enable an event */
NULL, /* disable an event */
ngx_rtsig_add_connection, /* add an connection */
ngx_rtsig_del_connection, /* delete an connection */
2004-07-07 02:15:04 -04:00
NULL, /* process the changes */
2004-02-17 14:53:12 -03:00
ngx_rtsig_process_events, /* process the events */
ngx_rtsig_init, /* init the events */
ngx_rtsig_done, /* done the events */
}
};
ngx_module_t ngx_rtsig_module = {
NGX_MODULE,
&ngx_rtsig_module_ctx, /* module context */
ngx_rtsig_commands, /* module directives */
NGX_EVENT_MODULE, /* module type */
NULL, /* init module */
2004-07-07 02:15:04 -04:00
NULL /* init process */
2004-02-17 14:53:12 -03:00
};
2004-06-09 03:45:27 -04:00
static ngx_int_t ngx_rtsig_init(ngx_cycle_t *cycle)
2004-02-17 14:53:12 -03:00
{
ngx_rtsig_conf_t *rtscf;
2004-02-17 18:11:27 -03:00
if (ngx_poll_module_ctx.actions.init(cycle) == NGX_ERROR) {
return NGX_ERROR;
}
2004-02-17 14:53:12 -03:00
rtscf = ngx_event_get_conf(cycle->conf_ctx, ngx_rtsig_module);
sigemptyset(&set);
sigaddset(&set, rtscf->signo);
2004-06-01 02:04:46 -04:00
sigaddset(&set, rtscf->signo + 1);
2004-02-17 14:53:12 -03:00
sigaddset(&set, SIGIO);
if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) {
ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
"sigprocmask() failed");
return NGX_ERROR;
}
2004-06-10 14:36:57 -04:00
if (overflow_list) {
ngx_free(overflow_list);
}
overflow_list = ngx_alloc(sizeof(struct pollfd) * rtscf->overflow_events,
cycle->log);
if (overflow_list == NULL) {
return NGX_ERROR;
}
2004-02-17 14:53:12 -03:00
ngx_io = ngx_os_io;
ngx_event_actions = ngx_rtsig_module_ctx.actions;
2004-07-07 11:01:00 -04:00
ngx_event_flags = NGX_USE_RTSIG_EVENT|NGX_HAVE_GREEDY_EVENT;
2004-02-17 14:53:12 -03:00
return NGX_OK;
}
static void ngx_rtsig_done(ngx_cycle_t *cycle)
{
2004-02-17 18:11:27 -03:00
ngx_poll_module_ctx.actions.done(cycle);
2004-02-17 14:53:12 -03:00
}
2004-06-09 03:45:27 -04:00
static ngx_int_t ngx_rtsig_add_connection(ngx_connection_t *c)
2004-02-17 14:53:12 -03:00
{
2004-06-01 02:04:46 -04:00
int signo;
2004-02-17 14:53:12 -03:00
ngx_rtsig_conf_t *rtscf;
2004-06-09 12:36:55 -04:00
if (c->read->accept && c->read->disabled) {
2004-06-15 03:55:11 -04:00
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
"rtsig enable connection: fd:%d", c->fd);
2004-06-09 12:36:55 -04:00
if (fcntl(c->fd, F_SETOWN, ngx_pid) == -1) {
ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
"fcntl(F_SETOWN) failed");
return NGX_ERROR;
}
c->read->active = 1;
c->read->disabled = 0;
}
2004-02-17 14:53:12 -03:00
rtscf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_rtsig_module);
2004-06-01 02:04:46 -04:00
signo = rtscf->signo + c->read->instance;
2004-02-17 14:53:12 -03:00
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
2004-06-01 02:04:46 -04:00
"rtsig add connection: fd:%d signo:%d", c->fd, signo);
2004-02-17 14:53:12 -03:00
if (fcntl(c->fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC) == -1) {
ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
"fcntl(O_RDWR|O_NONBLOCK|O_ASYNC) failed");
return NGX_ERROR;
}
2004-06-01 02:04:46 -04:00
if (fcntl(c->fd, F_SETSIG, signo) == -1) {
2004-02-17 14:53:12 -03:00
ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
"fcntl(F_SETSIG) failed");
return NGX_ERROR;
}
2004-06-01 02:04:46 -04:00
if (fcntl(c->fd, F_SETOWN, ngx_pid) == -1) {
2004-02-17 14:53:12 -03:00
ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
"fcntl(F_SETOWN) failed");
return NGX_ERROR;
}
#if (HAVE_ONESIGFD)
if (fcntl(c->fd, F_SETAUXFL, O_ONESIGFD) == -1) {
ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
"fcntl(F_SETAUXFL) failed");
return NGX_ERROR;
}
#endif
c->read->active = 1;
c->write->active = 1;
return NGX_OK;
}
2004-06-09 03:45:27 -04:00
static ngx_int_t ngx_rtsig_del_connection(ngx_connection_t *c, u_int flags)
2004-02-17 14:53:12 -03:00
{
2004-06-01 02:04:46 -04:00
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
"rtsig del connection: fd:%d", c->fd);
2004-06-09 12:36:55 -04:00
if ((flags & NGX_DISABLE_EVENT) && c->read->accept) {
2004-06-15 03:55:11 -04:00
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
"rtsig disable connection: fd:%d", c->fd);
2004-06-09 12:36:55 -04:00
c->read->active = 0;
2004-06-15 03:55:11 -04:00
c->read->disabled = 1;
2004-06-09 12:36:55 -04:00
return NGX_OK;
}
if (flags & NGX_CLOSE_EVENT) {
c->read->active = 0;
c->write->active = 0;
return NGX_OK;
}
if (fcntl(c->fd, F_SETFL, O_RDWR|O_NONBLOCK) == -1) {
ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
"fcntl(O_RDWR|O_NONBLOCK) failed");
return NGX_ERROR;
2004-02-17 18:11:27 -03:00
}
2004-02-17 14:53:12 -03:00
c->read->active = 0;
c->write->active = 0;
return NGX_OK;
}
2004-06-09 03:45:27 -04:00
ngx_int_t ngx_rtsig_process_events(ngx_cycle_t *cycle)
2004-02-17 14:53:12 -03:00
{
int signo;
ngx_int_t instance, i;
2004-04-14 16:34:05 -04:00
ngx_uint_t expire;
2004-02-17 14:53:12 -03:00
size_t n;
ngx_msec_t timer;
ngx_err_t err;
siginfo_t si;
2004-07-07 11:01:00 -04:00
ngx_event_t *rev, *wev;
2004-02-17 14:53:12 -03:00
struct timeval tv;
2004-02-17 18:11:27 -03:00
struct timespec ts, *tp;
2004-02-17 14:53:12 -03:00
struct sigaction sa;
ngx_epoch_msec_t delta;
2004-04-14 16:34:05 -04:00
ngx_connection_t *c;
2004-02-17 14:53:12 -03:00
ngx_rtsig_conf_t *rtscf;
2004-06-09 03:45:27 -04:00
if (overflow) {
timer = 0;
expire = 0;
2004-04-14 16:34:05 -04:00
2004-06-09 03:45:27 -04:00
} else {
for ( ;; ) {
timer = ngx_event_find_timer();
2004-07-07 11:01:00 -04:00
#if (NGX_THREADS)
if (timer == NGX_TIMER_ERROR) {
return NGX_ERROR;
}
if (timer == NGX_TIMER_INFINITE || timer > 500) {
timer = 500;
break;
}
#endif
2004-06-09 03:45:27 -04:00
if (timer != 0) {
break;
}
2004-02-17 14:53:12 -03:00
2004-06-09 03:45:27 -04:00
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
"rtsig expired timer");
2004-04-14 16:34:05 -04:00
2004-06-09 03:45:27 -04:00
ngx_event_expire_timers((ngx_msec_t)
2004-04-16 01:14:16 -04:00
(ngx_elapsed_msec - ngx_old_elapsed_msec));
2004-07-07 11:01:00 -04:00
if (ngx_posted_events && ngx_threaded) {
ngx_wakeup_worker_thread(cycle);
}
2004-06-09 03:45:27 -04:00
}
2004-04-14 16:34:05 -04:00
2004-06-09 03:45:27 -04:00
expire = 1;
2004-04-14 16:34:05 -04:00
2004-06-09 03:45:27 -04:00
if (ngx_accept_mutex) {
if (ngx_accept_disabled > 0) {
ngx_accept_disabled--;
2004-04-21 14:54:33 -04:00
2004-06-09 03:45:27 -04:00
} else {
2004-06-15 03:55:11 -04:00
ngx_accept_mutex_held = 0;
2004-06-09 03:45:27 -04:00
if (ngx_trylock_accept_mutex(cycle) == NGX_ERROR) {
return NGX_ERROR;
}
2004-04-14 16:34:05 -04:00
2004-06-09 03:45:27 -04:00
if (ngx_accept_mutex_held == 0
&& (timer == NGX_TIMER_INFINITE
|| timer > ngx_accept_mutex_delay))
{
timer = ngx_accept_mutex_delay;
expire = 0;
}
}
2004-04-14 16:34:05 -04:00
}
}
if (timer == NGX_TIMER_INFINITE) {
tp = NULL;
expire = 0;
} else {
2004-02-17 14:53:12 -03:00
ts.tv_sec = timer / 1000;
ts.tv_nsec = (timer % 1000) * 1000000;
2004-02-17 18:11:27 -03:00
tp = &ts;
2004-02-17 14:53:12 -03:00
}
2004-04-14 16:34:05 -04:00
ngx_old_elapsed_msec = ngx_elapsed_msec;
2004-04-02 11:13:20 -04:00
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
"rtsig timer: %d", timer);
2004-02-17 14:53:12 -03:00
2004-07-07 11:01:00 -04:00
/* Linux's sigwaitinfo() is sigtimedwait() with the NULL timeout pointer */
2004-02-17 18:11:27 -03:00
signo = sigtimedwait(&set, &si, tp);
2004-02-17 14:53:12 -03:00
if (signo == -1) {
err = ngx_errno;
2004-06-09 03:45:27 -04:00
2004-07-07 15:48:31 -04:00
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, err,
"rtsig signo:%d", signo);
2004-06-09 03:45:27 -04:00
if (err == NGX_EAGAIN) {
if (timer == NGX_TIMER_INFINITE) {
ngx_accept_mutex_unlock();
ngx_log_error(NGX_LOG_ALERT, cycle->log, err,
"sigtimedwait() returned EAGAIN without timeout");
return NGX_ERROR;
}
err = 0;
}
2004-02-17 14:53:12 -03:00
} else {
err = 0;
2004-07-07 15:48:31 -04:00
ngx_log_debug3(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
"rtsig signo:%d fd:%d band:%X",
signo, si.si_fd, si.si_band);
2004-02-17 14:53:12 -03:00
}
ngx_gettimeofday(&tv);
ngx_time_update(tv.tv_sec);
delta = ngx_elapsed_msec;
2004-07-13 13:59:12 -04:00
ngx_elapsed_msec = (ngx_epoch_msec_t) tv.tv_sec * 1000
+ tv.tv_usec / 1000 - ngx_start_msec;
2004-02-17 14:53:12 -03:00
2004-02-17 18:11:27 -03:00
if (err) {
2004-06-01 02:04:46 -04:00
ngx_accept_mutex_unlock();
2004-02-17 14:53:12 -03:00
ngx_log_error((err == NGX_EINTR) ? NGX_LOG_INFO : NGX_LOG_ALERT,
2004-04-02 11:13:20 -04:00
cycle->log, err, "sigtimedwait() failed");
2004-02-17 14:53:12 -03:00
return NGX_ERROR;
}
2004-04-14 16:34:05 -04:00
if (timer != NGX_TIMER_INFINITE) {
2004-02-17 14:53:12 -03:00
delta = ngx_elapsed_msec - delta;
2004-04-02 11:13:20 -04:00
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
2004-02-17 14:53:12 -03:00
"rtsig timer: %d, delta: %d", timer, (int) delta);
}
rtscf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_rtsig_module);
2004-06-01 02:04:46 -04:00
if (signo == rtscf->signo || signo == rtscf->signo + 1) {
2004-02-17 14:53:12 -03:00
2004-06-10 14:36:57 -04:00
if (overflow && (ngx_uint_t) si.si_fd > overflow_current) {
2004-06-09 03:45:27 -04:00
return NGX_OK;
}
2004-02-17 14:53:12 -03:00
/* TODO: old_cycles */
2004-04-02 11:13:20 -04:00
2004-02-17 14:53:12 -03:00
c = &ngx_cycle->connections[si.si_fd];
2004-06-01 02:04:46 -04:00
instance = signo - rtscf->signo;
2004-07-07 11:01:00 -04:00
rev = c->read;
2004-06-01 02:04:46 -04:00
if (c->read->instance != instance) {
/*
* the stale event from a file descriptor
* that was just closed in this iteration
*/
ngx_accept_mutex_unlock();
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
"rtsig: stale event " PTR_FMT, c);
return NGX_OK;
}
2004-02-17 14:53:12 -03:00
if (si.si_band & (POLLIN|POLLHUP|POLLERR)) {
2004-07-07 11:01:00 -04:00
if (rev->active) {
2004-04-14 16:34:05 -04:00
2004-07-07 11:01:00 -04:00
if (ngx_threaded && !rev->accept) {
2004-04-14 16:34:05 -04:00
if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
ngx_accept_mutex_unlock();
return NGX_ERROR;
}
2004-07-07 11:01:00 -04:00
rev->posted_ready = 1;
ngx_post_event(rev);
2004-04-14 16:34:05 -04:00
ngx_mutex_unlock(ngx_posted_events_mutex);
2004-07-07 11:01:00 -04:00
} else {
rev->ready = 1;
if (!ngx_threaded && !ngx_accept_mutex_held) {
rev->event_handler(rev);
} else if (rev->accept) {
if (ngx_accept_disabled <= 0) {
rev->event_handler(rev);
}
} else {
ngx_post_event(rev);
}
2004-04-14 16:34:05 -04:00
}
2004-02-17 14:53:12 -03:00
}
}
2004-07-07 11:01:00 -04:00
wev = c->write;
2004-04-14 16:34:05 -04:00
2004-07-07 11:01:00 -04:00
if (si.si_band & (POLLOUT|POLLHUP|POLLERR)) {
if (wev->active) {
2004-04-14 16:34:05 -04:00
2004-07-07 11:01:00 -04:00
if (ngx_threaded) {
2004-04-14 16:34:05 -04:00
if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
ngx_accept_mutex_unlock();
return NGX_ERROR;
}
2004-07-07 11:01:00 -04:00
wev->posted_ready = 1;
ngx_post_event(wev);
2004-04-14 16:34:05 -04:00
ngx_mutex_unlock(ngx_posted_events_mutex);
2004-07-07 11:01:00 -04:00
} else {
wev->ready = 1;
if (!ngx_threaded && !ngx_accept_mutex_held) {
wev->event_handler(wev);
} else {
ngx_post_event(wev);
}
2004-04-14 16:34:05 -04:00
}
2004-02-17 14:53:12 -03:00
}
}
} else if (signo == SIGIO) {
2004-04-14 16:34:05 -04:00
ngx_accept_mutex_unlock();
2004-06-09 03:45:27 -04:00
ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
"rt signal queue overflowed");
2004-02-17 14:53:12 -03:00
2004-06-09 03:45:27 -04:00
/* flush the RT signal queue */
2004-02-17 18:11:27 -03:00
2004-02-17 14:53:12 -03:00
ngx_memzero(&sa, sizeof(struct sigaction));
sa.sa_handler = SIG_DFL;
sigemptyset(&sa.sa_mask);
2004-06-09 03:45:27 -04:00
2004-02-17 14:53:12 -03:00
if (sigaction(rtscf->signo, &sa, NULL) == -1) {
2004-04-02 11:13:20 -04:00
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
2004-02-17 14:53:12 -03:00
"sigaction(%d, SIG_DFL) failed", rtscf->signo);
}
2004-06-09 03:45:27 -04:00
if (sigaction(rtscf->signo + 1, &sa, NULL) == -1) {
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
"sigaction(%d, SIG_DFL) failed", rtscf->signo + 1);
}
2004-02-17 18:11:27 -03:00
2004-06-09 03:45:27 -04:00
overflow = 1;
2004-06-10 14:36:57 -04:00
overflow_current = 0;
2004-07-07 02:15:04 -04:00
ngx_event_actions.process_events = ngx_rtsig_process_overflow;
2004-02-17 18:11:27 -03:00
2004-06-11 02:15:08 -04:00
return NGX_ERROR;
2004-02-17 18:11:27 -03:00
2004-06-09 03:45:27 -04:00
} else if (signo != -1) {
2004-06-01 02:04:46 -04:00
ngx_accept_mutex_unlock();
2004-04-02 11:13:20 -04:00
ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
2004-02-17 18:11:27 -03:00
"sigtimedwait() returned unexpected signal: %d", signo);
2004-06-01 02:04:46 -04:00
2004-02-17 14:53:12 -03:00
return NGX_ERROR;
}
2004-04-14 16:34:05 -04:00
ngx_accept_mutex_unlock();
if (expire && delta) {
2004-02-17 14:53:12 -03:00
ngx_event_expire_timers((ngx_msec_t) delta);
}
2004-07-07 11:01:00 -04:00
if (ngx_posted_events) {
if (ngx_threaded) {
ngx_wakeup_worker_thread(cycle);
} else {
ngx_event_process_posted(cycle);
}
2004-04-14 16:34:05 -04:00
}
2004-06-09 12:36:55 -04:00
if (signo == -1) {
return NGX_AGAIN;
} else {
return NGX_OK;
}
2004-02-17 14:53:12 -03:00
}
2004-06-10 14:36:57 -04:00
/* TODO: old cylces */
2004-06-09 03:45:27 -04:00
static ngx_int_t ngx_rtsig_process_overflow(ngx_cycle_t *cycle)
2004-02-17 18:11:27 -03:00
{
2004-06-15 03:55:11 -04:00
int name[2], rtsig_max, rtsig_nr, events, ready;
size_t len;
2004-06-10 14:36:57 -04:00
ngx_int_t tested, n, i;
ngx_err_t err;
2004-07-07 11:01:00 -04:00
ngx_event_t *rev, *wev;
2004-06-09 03:45:27 -04:00
ngx_connection_t *c;
2004-06-10 14:36:57 -04:00
ngx_rtsig_conf_t *rtscf;
rtscf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_rtsig_module);
2004-06-09 03:45:27 -04:00
2004-06-10 14:36:57 -04:00
tested = 0;
2004-06-09 03:45:27 -04:00
2004-06-10 14:36:57 -04:00
for ( ;; ) {
2004-06-09 03:45:27 -04:00
2004-06-10 14:36:57 -04:00
n = 0;
while (n < rtscf->overflow_events) {
2004-06-09 03:45:27 -04:00
2004-06-10 14:36:57 -04:00
if (overflow_current == cycle->connection_n) {
break;
}
c = &cycle->connections[overflow_current++];
if (c->fd == -1) {
continue;
}
events = 0;
if (c->read->active && c->read->event_handler) {
events |= POLLIN;
}
if (c->write->active && c->write->event_handler) {
events |= POLLOUT;
}
if (events == 0) {
continue;
}
2004-06-09 03:45:27 -04:00
2004-06-10 14:36:57 -04:00
overflow_list[n].fd = c->fd;
overflow_list[n].events = events;
overflow_list[n].revents = 0;
2004-06-09 16:03:54 -04:00
n++;
2004-06-10 14:36:57 -04:00
}
2004-06-09 03:45:27 -04:00
2004-06-10 14:36:57 -04:00
if (n == 0) {
break;
}
2004-06-09 03:45:27 -04:00
2004-06-10 14:36:57 -04:00
for ( ;; ) {
ready = poll(overflow_list, n, 0);
2004-06-09 03:45:27 -04:00
2004-06-10 14:36:57 -04:00
if (ready == -1) {
err = ngx_errno;
ngx_log_error((err == NGX_EINTR) ? NGX_LOG_INFO : NGX_LOG_ALERT,
cycle->log, 0,
"poll() failed while the overflow recover");
2004-06-09 03:45:27 -04:00
2004-07-07 11:01:00 -04:00
if (err != NGX_EINTR) {
break;
2004-06-10 14:36:57 -04:00
}
2004-06-09 03:45:27 -04:00
}
}
2004-06-10 14:36:57 -04:00
if (ready <= 0) {
continue;
}
2004-07-07 11:01:00 -04:00
if (n) {
if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
return NGX_ERROR;
}
}
2004-06-10 14:36:57 -04:00
for (i = 0; i < n; i++) {
c = &cycle->connections[overflow_list[i].fd];
2004-07-07 11:01:00 -04:00
rev = c->read;
2004-06-10 14:36:57 -04:00
if (overflow_list[i].revents & (POLLIN|POLLERR|POLLHUP|POLLNVAL)) {
tested++;
2004-07-07 11:01:00 -04:00
if (ngx_threaded) {
rev->posted_ready = 1;
ngx_post_event(rev);
2004-06-10 14:36:57 -04:00
} else {
2004-07-07 11:01:00 -04:00
rev->ready = 1;
rev->event_handler(rev);
2004-06-10 14:36:57 -04:00
}
}
2004-07-07 11:01:00 -04:00
wev = c->write;
2004-06-10 14:36:57 -04:00
if (overflow_list[i].revents & (POLLOUT|POLLERR|POLLHUP|POLLNVAL)) {
tested++;
2004-06-09 03:45:27 -04:00
2004-07-07 11:01:00 -04:00
if (ngx_threaded) {
wev->posted_ready = 1;
ngx_post_event(wev);
2004-06-09 03:45:27 -04:00
2004-07-07 11:01:00 -04:00
} else {
wev->ready = 1;
wev->event_handler(wev);
2004-06-10 14:36:57 -04:00
}
2004-06-09 03:45:27 -04:00
}
}
2004-07-07 11:01:00 -04:00
if (n) {
ngx_mutex_unlock(ngx_posted_events_mutex);
}
2004-06-10 14:36:57 -04:00
if (tested >= rtscf->overflow_test) {
2004-06-09 03:45:27 -04:00
/*
* Check the current rt queue length to prevent the new overflow.
*
* Learn the /proc/sys/kernel/rtsig-max value because
2004-06-09 16:03:54 -04:00
* it can be changed sisnce the last checking.
2004-06-09 03:45:27 -04:00
*/
name[0] = CTL_KERN;
name[1] = KERN_RTSIGMAX;
len = sizeof(rtsig_max);
if (sysctl(name, sizeof(name), &rtsig_max, &len, NULL, 0) == -1) {
ngx_log_error(NGX_LOG_ALERT, cycle->log, errno,
"sysctl(KERN_RTSIGMAX) failed");
return NGX_ERROR;
}
name[0] = CTL_KERN;
name[1] = KERN_RTSIGNR;
len = sizeof(rtsig_nr);
if (sysctl(name, sizeof(name), &rtsig_nr, &len, NULL, 0) == -1) {
ngx_log_error(NGX_LOG_ALERT, cycle->log, errno,
"sysctl(KERN_RTSIGNR) failed");
return NGX_ERROR;
}
/*
2004-06-10 14:36:57 -04:00
* drain rt signal queue if the /proc/sys/kernel/rtsig-nr is bigger
* than "/proc/sys/kernel/rtsig-max / rtsig_overflow_threshold"
2004-06-09 03:45:27 -04:00
*/
2004-06-10 14:36:57 -04:00
if (rtsig_max / rtscf->overflow_threshold < rtsig_nr) {
2004-06-09 16:03:54 -04:00
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
"rtsig queue state: %d/%d", rtsig_nr, rtsig_max);
2004-06-09 12:36:55 -04:00
while (ngx_rtsig_process_events(cycle) == NGX_OK) { /* void */ }
2004-06-09 03:45:27 -04:00
}
2004-06-10 14:36:57 -04:00
tested = 0;
2004-06-09 03:45:27 -04:00
}
}
2004-07-07 11:01:00 -04:00
if (ngx_posted_events) {
if (ngx_threaded) {
ngx_wakeup_worker_thread(cycle);
} else {
ngx_event_process_posted(cycle);
}
2004-02-17 18:11:27 -03:00
}
2004-06-09 03:45:27 -04:00
ngx_log_error(NGX_LOG_INFO, cycle->log, 0,
"rt signal queue overflow recovered");
overflow = 0;
2004-07-07 02:15:04 -04:00
ngx_event_actions.process_events = ngx_rtsig_process_events;
2004-06-09 03:45:27 -04:00
2004-02-17 18:11:27 -03:00
return NGX_OK;
}
2004-02-17 14:53:12 -03:00
static void *ngx_rtsig_create_conf(ngx_cycle_t *cycle)
{
ngx_rtsig_conf_t *rtscf;
ngx_test_null(rtscf, ngx_palloc(cycle->pool, sizeof(ngx_rtsig_conf_t)),
NGX_CONF_ERROR);
rtscf->signo = NGX_CONF_UNSET;
2004-06-10 14:36:57 -04:00
rtscf->overflow_events = NGX_CONF_UNSET;
rtscf->overflow_test = NGX_CONF_UNSET;
rtscf->overflow_threshold = NGX_CONF_UNSET;
2004-02-17 14:53:12 -03:00
return rtscf;
}
static char *ngx_rtsig_init_conf(ngx_cycle_t *cycle, void *conf)
{
ngx_rtsig_conf_t *rtscf = conf;
/* LinuxThreads use the first 3 RT signals */
ngx_conf_init_value(rtscf->signo, SIGRTMIN + 10);
2004-06-10 14:36:57 -04:00
ngx_conf_init_value(rtscf->overflow_events, 16);
2004-06-15 03:55:11 -04:00
ngx_conf_init_value(rtscf->overflow_test, 32);
ngx_conf_init_value(rtscf->overflow_threshold, 10);
2004-06-10 14:36:57 -04:00
2004-02-17 14:53:12 -03:00
return NGX_CONF_OK;
}