📄 ngx_rtsig_module.c
字号:
/* * Copyright (C) Igor Sysoev */#include <ngx_config.h>#include <ngx_core.h>#include <ngx_event.h>#if (NGX_TEST_BUILD_RTSIG)#define F_SETSIG 10#define SIGRTMIN 33#define si_fd __spare__[0]#define KERN_RTSIGNR 30#define KERN_RTSIGMAX 31int sigtimedwait(const sigset_t *set, siginfo_t *info, const struct timespec *timeout){ return -1;}int ngx_linux_rtsig_max;#endiftypedef struct { ngx_uint_t signo; ngx_uint_t overflow_events; ngx_uint_t overflow_test; ngx_uint_t overflow_threshold;} ngx_rtsig_conf_t;extern ngx_event_module_t ngx_poll_module_ctx;static ngx_int_t ngx_rtsig_init(ngx_cycle_t *cycle, ngx_msec_t timer);static void ngx_rtsig_done(ngx_cycle_t *cycle);static ngx_int_t ngx_rtsig_add_connection(ngx_connection_t *c);static ngx_int_t ngx_rtsig_del_connection(ngx_connection_t *c, ngx_uint_t flags);static ngx_int_t ngx_rtsig_process_events(ngx_cycle_t *cycle, ngx_msec_t timer, ngx_uint_t flags);static ngx_int_t ngx_rtsig_process_overflow(ngx_cycle_t *cycle, ngx_msec_t timer, ngx_uint_t flags);static void *ngx_rtsig_create_conf(ngx_cycle_t *cycle);static char *ngx_rtsig_init_conf(ngx_cycle_t *cycle, void *conf);static char *ngx_check_ngx_overflow_threshold_bounds(ngx_conf_t *cf, void *post, void *data);static sigset_t set;static ngx_uint_t overflow, overflow_current;static struct pollfd *overflow_list;static ngx_str_t rtsig_name = ngx_string("rtsig");static ngx_conf_num_bounds_t ngx_overflow_threshold_bounds = { ngx_check_ngx_overflow_threshold_bounds, 2, 10};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 }, { 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 }, 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 */ NULL, /* process the changes */ 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_V1, &ngx_rtsig_module_ctx, /* module context */ ngx_rtsig_commands, /* module directives */ NGX_EVENT_MODULE, /* module type */ NULL, /* init master */ NULL, /* init module */ NULL, /* init process */ NULL, /* init thread */ NULL, /* exit thread */ NULL, /* exit process */ NULL, /* exit master */ NGX_MODULE_V1_PADDING};static ngx_int_tngx_rtsig_init(ngx_cycle_t *cycle, ngx_msec_t timer){ ngx_rtsig_conf_t *rtscf; rtscf = ngx_event_get_conf(cycle->conf_ctx, ngx_rtsig_module); sigemptyset(&set); sigaddset(&set, (int) rtscf->signo); sigaddset(&set, (int) rtscf->signo + 1); sigaddset(&set, SIGIO); sigaddset(&set, SIGALRM); if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) { ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, "sigprocmask() failed"); return NGX_ERROR; } 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; } ngx_io = ngx_os_io; ngx_event_actions = ngx_rtsig_module_ctx.actions; ngx_event_flags = NGX_USE_RTSIG_EVENT |NGX_USE_GREEDY_EVENT |NGX_USE_FD_EVENT; return NGX_OK;}static voidngx_rtsig_done(ngx_cycle_t *cycle){ ngx_free(overflow_list); overflow_list = NULL;}static ngx_int_tngx_rtsig_add_connection(ngx_connection_t *c){ ngx_uint_t signo; ngx_rtsig_conf_t *rtscf; if (c->read->accept && c->read->disabled) { ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "rtsig enable connection: fd:%d", c->fd); 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; } rtscf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_rtsig_module); signo = rtscf->signo + c->read->instance; ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0, "rtsig add connection: fd:%d signo:%ui", c->fd, signo); 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; } if (fcntl(c->fd, F_SETSIG, (int) signo) == -1) { ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno, "fcntl(F_SETSIG) failed"); return NGX_ERROR; } 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; }#if (NGX_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;}static ngx_int_tngx_rtsig_del_connection(ngx_connection_t *c, ngx_uint_t flags){ ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "rtsig del connection: fd:%d", c->fd); if ((flags & NGX_DISABLE_EVENT) && c->read->accept) { ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "rtsig disable connection: fd:%d", c->fd); c->read->active = 0; c->read->disabled = 1; 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; } c->read->active = 0; c->write->active = 0; return NGX_OK;}static ngx_int_tngx_rtsig_process_events(ngx_cycle_t *cycle, ngx_msec_t timer, ngx_uint_t flags){ int signo; ngx_int_t instance; ngx_err_t err; siginfo_t si; ngx_event_t *rev, *wev, **queue; struct timespec ts, *tp; struct sigaction sa; ngx_connection_t *c; ngx_rtsig_conf_t *rtscf; if (timer == NGX_TIMER_INFINITE) { tp = NULL; } else { ts.tv_sec = timer / 1000; ts.tv_nsec = (timer % 1000) * 1000000; tp = &ts; } ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "rtsig timer: %M", timer); /* Linux's sigwaitinfo() is sigtimedwait() with the NULL timeout pointer */ signo = sigtimedwait(&set, &si, tp); if (signo == -1) { err = ngx_errno; ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, err, "rtsig signo:%d", signo); if (flags & NGX_UPDATE_TIME) { ngx_time_update(0, 0); } if (err == NGX_EAGAIN) { /* timeout */ if (timer != NGX_TIMER_INFINITE) { return NGX_AGAIN; } ngx_log_error(NGX_LOG_ALERT, cycle->log, err, "sigtimedwait() returned EAGAIN without timeout"); return NGX_ERROR; } ngx_log_error((err == NGX_EINTR) ? NGX_LOG_INFO : NGX_LOG_ALERT, cycle->log, err, "sigtimedwait() failed"); return NGX_ERROR; } ngx_log_debug3(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "rtsig signo:%d fd:%d band:%04Xd", signo, si.si_fd, si.si_band); if (flags & NGX_UPDATE_TIME) { ngx_time_update(0, 0); } rtscf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_rtsig_module); if (signo == (int) rtscf->signo || signo == (int) rtscf->signo + 1) { if (overflow && (ngx_uint_t) si.si_fd > overflow_current) { return NGX_OK; } c = ngx_cycle->files[si.si_fd]; if (c == NULL) { /* the stale event */ return NGX_OK; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -