ngx_event.c

来自「Nginx是一个高性能的HTTP和反向代理服务器」· C语言 代码 · 共 1,266 行 · 第 1/3 页

C
1,266
字号
                   (const void *) &sndlowat, sizeof(int))        == -1)    {        ngx_connection_error(c, ngx_socket_errno,                             "setsockopt(SO_SNDLOWAT) failed");        return NGX_ERROR;    }    c->sndlowat = 1;    return NGX_OK;}static char *ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){    char                 *rv;    void               ***ctx;    ngx_uint_t            i;    ngx_conf_t            pcf;    ngx_event_module_t   *m;    /* count the number of the event modules and set up their indices */    ngx_event_max_module = 0;    for (i = 0; ngx_modules[i]; i++) {        if (ngx_modules[i]->type != NGX_EVENT_MODULE) {            continue;        }        ngx_modules[i]->ctx_index = ngx_event_max_module++;    }    ctx = ngx_pcalloc(cf->pool, sizeof(void *));    if (ctx == NULL) {        return NGX_CONF_ERROR;    }    *ctx = ngx_pcalloc(cf->pool, ngx_event_max_module * sizeof(void *));    if (*ctx == NULL) {        return NGX_CONF_ERROR;    }    *(void **) conf = ctx;    for (i = 0; ngx_modules[i]; i++) {        if (ngx_modules[i]->type != NGX_EVENT_MODULE) {            continue;        }        m = ngx_modules[i]->ctx;        if (m->create_conf) {            (*ctx)[ngx_modules[i]->ctx_index] = m->create_conf(cf->cycle);            if ((*ctx)[ngx_modules[i]->ctx_index] == NULL) {                return NGX_CONF_ERROR;            }        }    }    pcf = *cf;    cf->ctx = ctx;    cf->module_type = NGX_EVENT_MODULE;    cf->cmd_type = NGX_EVENT_CONF;    rv = ngx_conf_parse(cf, NULL);    *cf = pcf;    if (rv != NGX_CONF_OK)        return rv;    for (i = 0; ngx_modules[i]; i++) {        if (ngx_modules[i]->type != NGX_EVENT_MODULE) {            continue;        }        m = ngx_modules[i]->ctx;        if (m->init_conf) {            rv = m->init_conf(cf->cycle, (*ctx)[ngx_modules[i]->ctx_index]);            if (rv != NGX_CONF_OK) {                return rv;            }        }    }    return NGX_CONF_OK;}static char *ngx_event_connections(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){    ngx_event_conf_t  *ecf = conf;    ngx_str_t  *value;    if (ecf->connections != NGX_CONF_UNSET_UINT) {        return "is duplicate" ;    }    if (ngx_strcmp(cmd->name.data, "connections") == 0) {        ngx_conf_log_error(NGX_LOG_WARN, cf, 0,                           "the \"connections\" directive is deprecated, "                           "use the \"worker_connections\" directive instead");    }    value = cf->args->elts;    ecf->connections = ngx_atoi(value[1].data, value[1].len);    if (ecf->connections == (ngx_uint_t) NGX_ERROR) {        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,                           "invalid number \"%V\"", &value[1]);        return NGX_CONF_ERROR;    }    cf->cycle->connection_n = ecf->connections;    return NGX_CONF_OK;}static char *ngx_event_use(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){    ngx_event_conf_t  *ecf = conf;    ngx_int_t             m;    ngx_str_t            *value;    ngx_event_conf_t     *old_ecf;    ngx_event_module_t   *module;    if (ecf->use != NGX_CONF_UNSET_UINT) {        return "is duplicate" ;    }    value = cf->args->elts;    if (cf->cycle->old_cycle->conf_ctx) {        old_ecf = ngx_event_get_conf(cf->cycle->old_cycle->conf_ctx,                                     ngx_event_core_module);    } else {        old_ecf = NULL;    }    for (m = 0; ngx_modules[m]; m++) {        if (ngx_modules[m]->type != NGX_EVENT_MODULE) {            continue;        }        module = ngx_modules[m]->ctx;        if (module->name->len == value[1].len) {            if (ngx_strcmp(module->name->data, value[1].data) == 0) {                ecf->use = ngx_modules[m]->ctx_index;                ecf->name = module->name->data;                if (ngx_process == NGX_PROCESS_SINGLE                    && old_ecf                    && old_ecf->use != ecf->use)                {                    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,                               "when the server runs without a master process "                               "the \"%V\" event type must be the same as "                               "in previous configuration - \"%s\" "                               "and it can not be changed on the fly, "                               "to change it you need to stop server "                               "and start it again",                               &value[1], old_ecf->name);                    return NGX_CONF_ERROR;                }                return NGX_CONF_OK;            }        }    }    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,                       "invalid event type \"%V\"", &value[1]);    return NGX_CONF_ERROR;}static char *ngx_event_debug_connection(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){#if (NGX_DEBUG)    ngx_event_conf_t  *ecf = conf;    ngx_int_t           rc;    ngx_str_t          *value;    ngx_event_debug_t  *dc;    struct hostent     *h;    ngx_inet_cidr_t     in_cidr;    value = cf->args->elts;    /* AF_INET only */    dc = ngx_array_push(&ecf->debug_connection);    if (dc == NULL) {        return NGX_CONF_ERROR;    }    dc->addr = inet_addr((char *) value[1].data);    if (dc->addr != INADDR_NONE) {        dc->mask = 0xffffffff;        return NGX_CONF_OK;    }    rc = ngx_ptocidr(&value[1], &in_cidr);    if (rc == NGX_DONE) {        ngx_conf_log_error(NGX_LOG_WARN, cf, 0,                           "low address bits of %V are meaningless", &value[1]);        rc = NGX_OK;    }    if (rc == NGX_OK) {        dc->mask = in_cidr.mask;        dc->addr = in_cidr.addr;        return NGX_CONF_OK;    }    h = gethostbyname((char *) value[1].data);    if (h == NULL || h->h_addr_list[0] == NULL) {        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,                           "host \"%s\" not found", value[1].data);        return NGX_CONF_ERROR;    }    dc->mask = 0xffffffff;    dc->addr = *(in_addr_t *)(h->h_addr_list[0]);#else    ngx_conf_log_error(NGX_LOG_WARN, cf, 0,                       "\"debug_connection\" is ignored, you need to rebuild "                       "nginx using --with-debug option to enable it");#endif    return NGX_CONF_OK;}static void *ngx_event_create_conf(ngx_cycle_t *cycle){    ngx_event_conf_t  *ecf;    ecf = ngx_palloc(cycle->pool, sizeof(ngx_event_conf_t));    if (ecf == NULL) {        return NGX_CONF_ERROR;    }    ecf->connections = NGX_CONF_UNSET_UINT;    ecf->use = NGX_CONF_UNSET_UINT;    ecf->multi_accept = NGX_CONF_UNSET;    ecf->accept_mutex = NGX_CONF_UNSET;    ecf->accept_mutex_delay = NGX_CONF_UNSET_MSEC;    ecf->name = (void *) NGX_CONF_UNSET;#if (NGX_DEBUG)    if (ngx_array_init(&ecf->debug_connection, cycle->pool, 4,                       sizeof(ngx_event_debug_t)) == NGX_ERROR)    {        return NGX_CONF_ERROR;    }#endif    return ecf;}static char *ngx_event_init_conf(ngx_cycle_t *cycle, void *conf){    ngx_event_conf_t  *ecf = conf;#if (NGX_HAVE_EPOLL) && !(NGX_TEST_BUILD_EPOLL)    int                  fd;#endif#if (NGX_HAVE_RTSIG)    ngx_uint_t           rtsig;    ngx_core_conf_t     *ccf;#endif    ngx_int_t            i, connections;    ngx_module_t        *module;    ngx_event_module_t  *event_module;    connections = NGX_CONF_UNSET_UINT;    module = NULL;#if (NGX_HAVE_EPOLL) && !(NGX_TEST_BUILD_EPOLL)    fd = epoll_create(100);    if (fd != -1) {        close(fd);        connections = DEFAULT_CONNECTIONS;        module = &ngx_epoll_module;    } else if (ngx_errno != NGX_ENOSYS) {        connections = DEFAULT_CONNECTIONS;        module = &ngx_epoll_module;    }#endif#if (NGX_HAVE_RTSIG)    if (module == NULL) {        connections = DEFAULT_CONNECTIONS;        module = &ngx_rtsig_module;        rtsig = 1;    } else {        rtsig = 0;    }#endif#if (NGX_HAVE_DEVPOLL)    connections = DEFAULT_CONNECTIONS;    module = &ngx_devpoll_module;#endif#if (NGX_HAVE_KQUEUE)    connections = DEFAULT_CONNECTIONS;    module = &ngx_kqueue_module;#endif#if (NGX_HAVE_SELECT)    if (module == NULL) {#if (NGX_WIN32 || FD_SETSIZE >= DEFAULT_CONNECTIONS)        connections = DEFAULT_CONNECTIONS;#else        connections = FD_SETSIZE;#endif        module = &ngx_select_module;    }#endif    if (module == NULL) {        for (i = 0; ngx_modules[i]; i++) {            if (ngx_modules[i]->type == NGX_EVENT_MODULE) {                event_module = ngx_modules[i]->ctx;                if (ngx_strcmp(event_module->name->data, event_core_name.data)                    == 0)                {                    continue;                }                module = ngx_modules[i];                break;            }        }    }    if (module == NULL) {        ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, "no events module found");        return NGX_CONF_ERROR;    }    ngx_conf_init_uint_value(ecf->connections, connections);    cycle->connection_n = ecf->connections;    ngx_conf_init_uint_value(ecf->use, module->ctx_index);    event_module = module->ctx;    ngx_conf_init_ptr_value(ecf->name, event_module->name->data);    ngx_conf_init_value(ecf->multi_accept, 0);    ngx_conf_init_value(ecf->accept_mutex, 1);    ngx_conf_init_msec_value(ecf->accept_mutex_delay, 500);#if (NGX_HAVE_RTSIG)    if (!rtsig) {        return NGX_CONF_OK;    }    if (ecf->accept_mutex) {        return NGX_CONF_OK;    }    ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);    if (ccf->worker_processes == 0) {        return NGX_CONF_OK;    }    ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,                  "the \"rtsig\" method requires \"accept_mutex\" to be on");    return NGX_CONF_ERROR;#else    return NGX_CONF_OK;#endif}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?