static ngx_int_t ngx_http_vhost_traffic_status_init_zone(ngx_shm_zone_t *shm_zone, void *data) { ngx_http_vhost_traffic_status_ctx_t *octx = data; size_t len; ngx_slab_pool_t *shpool; ngx_rbtree_node_t *sentinel; ngx_http_vhost_traffic_status_ctx_t *ctx; ctx = shm_zone->data; if (octx) { ctx->rbtree = octx->rbtree; return NGX_OK; } shpool = (ngx_slab_pool_t *) shm_zone->shm.addr; if (shm_zone->shm.exists) { ctx->rbtree = shpool->data; return NGX_OK; } ctx->rbtree = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_t)); if (ctx->rbtree == NULL) { return NGX_ERROR; } shpool->data = ctx->rbtree; sentinel = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_node_t)); if (sentinel == NULL) { return NGX_ERROR; } ngx_rbtree_init(ctx->rbtree, sentinel, ngx_http_vhost_traffic_status_rbtree_insert_value); len = sizeof(" in vhost_traffic_status_zone \"\"") + shm_zone->shm.name.len; shpool->log_ctx = ngx_slab_alloc(shpool, len); if (shpool->log_ctx == NULL) { return NGX_ERROR; } ngx_sprintf(shpool->log_ctx, " in vhost_traffic_status_zone \"%V\"%Z", &shm_zone->shm.name); return NGX_OK; }
static ngx_int_t ngx_http_limit_req_init_zone(ngx_shm_zone_t *shm_zone, void *data) { ngx_http_limit_req_ctx_t *octx = data; ngx_rbtree_node_t *sentinel; ngx_http_limit_req_ctx_t *ctx; ctx = shm_zone->data; if (octx) { if (ngx_strcmp(ctx->var.data, octx->var.data) != 0) { ngx_log_error(NGX_LOG_EMERG, shm_zone->shm.log, 0, "limit_req \"%V\" uses the \"%V\" variable " "while previously it used the \"%V\" variable", &shm_zone->name, &ctx->var, &octx->var); return NGX_ERROR; } ctx->rbtree = octx->rbtree; ctx->queue = octx->queue; ctx->shpool = octx->shpool; return NGX_OK; } ctx->shpool = (ngx_slab_pool_t *) shm_zone->shm.addr; ctx->rbtree = ngx_slab_alloc(ctx->shpool, sizeof(ngx_rbtree_t)); if (ctx->rbtree == NULL) { return NGX_ERROR; } sentinel = ngx_slab_alloc(ctx->shpool, sizeof(ngx_rbtree_node_t)); if (sentinel == NULL) { return NGX_ERROR; } ngx_rbtree_init(ctx->rbtree, sentinel, ngx_http_limit_req_rbtree_insert_value); ctx->queue = ngx_slab_alloc(ctx->shpool, sizeof(ngx_queue_t)); if (ctx->queue == NULL) { return NGX_ERROR; } ngx_queue_init(ctx->queue); return NGX_OK; }
ngx_int_t ngx_http_tfs_rc_server_init_zone(ngx_shm_zone_t *shm_zone, void *data) { ngx_http_tfs_rc_ctx_t *octx = data; size_t len; ngx_http_tfs_rc_ctx_t *ctx; ctx = shm_zone->data; if (octx) { ctx->sh = octx->sh; ctx->shpool = octx->shpool; return NGX_OK; } ctx->shpool = (ngx_slab_pool_t *) shm_zone->shm.addr; if (shm_zone->shm.exists) { ctx->sh = ctx->shpool->data; return NGX_OK; } ctx->sh = ngx_slab_alloc(ctx->shpool, sizeof(ngx_http_tfs_rc_shctx_t)); if (ctx->sh == NULL) { return NGX_ERROR; } ctx->shpool->data = ctx->sh; ngx_rbtree_init(&ctx->sh->rbtree, &ctx->sh->sentinel, ngx_http_tfs_rcs_rbtree_insert_value); ngx_queue_init(&ctx->sh->queue); ngx_queue_init(&ctx->sh->kp_queue); len = sizeof(" in tfs rc servers zone \"\"") + shm_zone->shm.name.len; ctx->shpool->log_ctx = ngx_slab_alloc(ctx->shpool, len); if (ctx->shpool->log_ctx == NULL) { return NGX_ERROR; } ngx_sprintf(ctx->shpool->log_ctx, " in tfs rc servers zone \"%V\"%Z", &shm_zone->shm.name); return NGX_OK; }
ngx_int_t ngx_ssl_session_cache_init(ngx_shm_zone_t *shm_zone, void *data) { size_t len; ngx_slab_pool_t *shpool; ngx_ssl_session_cache_t *cache; if (data) { shm_zone->data = data; return NGX_OK; } if (shm_zone->shm.exists) { shm_zone->data = data; return NGX_OK; } /* * Much like ngx_event_openssl, we use a red-black tree and a queue as * the backing store for our cache. */ shpool = (ngx_slab_pool_t *) shm_zone->shm.addr; cache = ngx_slab_alloc(shpool, sizeof(ngx_ssl_session_cache_t)); if (cache == NULL) { return NGX_ERROR; } shpool->data = cache; shm_zone->data = cache; ngx_rbtree_init(&cache->session_rbtree, &cache->sentinel, ngx_ssl_session_rbtree_insert_value); ngx_queue_init(&cache->expire_queue); len = sizeof(" in SSL session shared cache \"\"") + shm_zone->shm.name.len; shpool->log_ctx = ngx_slab_alloc(shpool, len); if (shpool->log_ctx == NULL) { return NGX_ERROR; } ngx_sprintf(shpool->log_ctx, " in SSL session shared cache \"%V\"%Z", &shm_zone->shm.name); return NGX_OK; }
static void ngx_buffer_cache_reset(ngx_buffer_cache_sh_t *cache) { cache->entries_end = cache->entries_start; cache->buffers_start = cache->buffers_end; cache->buffers_read = cache->buffers_end; cache->buffers_write = cache->buffers_end; ngx_rbtree_init(&cache->rbtree, &cache->sentinel, ngx_buffer_cache_rbtree_insert_value); ngx_queue_init(&cache->used_queue); ngx_queue_init(&cache->free_queue); // update stats (everything is evicted) cache->stats.evicted = cache->stats.store_ok; cache->stats.evicted_bytes = cache->stats.store_bytes; }
static ngx_int_t ngx_lua_session_init(ngx_shm_zone_t *shm_zone, void *data) { ngx_lua_session_conf_t *olscf = data; size_t len; ngx_lua_session_conf_t *lscf; lscf = shm_zone->data; if (olscf) { lscf->shm = olscf->shm; lscf->pool = olscf->pool; return NGX_OK; } lscf->pool = (ngx_slab_pool_t *) shm_zone->shm.addr; if (shm_zone->shm.exists) { lscf->shm = lscf->pool->data; return NGX_OK; } lscf->shm = ngx_slab_alloc(lscf->pool, sizeof(ngx_lua_session_shm_t)); if (lscf->shm == NULL) { return NGX_ERROR; } lscf->pool->data = lscf->shm; ngx_rbtree_init(&lscf->shm->rbtree, &lscf->shm->sentinel, ngx_lua_session_insert_value); ngx_queue_init(&lscf->shm->queue); len = sizeof(" in lua session \"\"") + shm_zone->shm.name.len; lscf->pool->log_ctx = ngx_slab_alloc(lscf->pool, len); if (lscf->pool->log_ctx == NULL) { return NGX_ERROR; } ngx_sprintf(lscf->pool->log_ctx, " in lua session \"%V\"%Z", &shm_zone->shm.name); return NGX_OK; }
static ngx_int_t ngx_limit_tcp_init_zone(ngx_shm_zone_t *shm_zone, void *data) { size_t len; ngx_limit_tcp_ctx_t *ctx, *octx; ctx = shm_zone->data; octx = data; if (octx) { ctx->shpool = octx->shpool; ctx->sh = octx->sh; return NGX_OK; } ctx->shpool = (ngx_slab_pool_t *) shm_zone->shm.addr; if (shm_zone->shm.exists) { ctx->sh = ctx->shpool->data; } /* init sh */ ctx->sh = ngx_slab_alloc(ctx->shpool, sizeof(ngx_limit_tcp_shctx_t)); if (ctx->sh == NULL) { return NGX_ERROR; } ctx->shpool->data = ctx->sh; ngx_rbtree_init(&ctx->sh->rbtree, &ctx->sh->sentinel, ngx_limit_tcp_rbtree_insert_value); ngx_queue_init(&ctx->sh->queue); len = sizeof(" in limit_tcp zone \"\"") + shm_zone->shm.name.len; ctx->shpool->log_ctx = ngx_slab_alloc(ctx->shpool, len); if (ctx->shpool->log_ctx == NULL) { return NGX_ERROR; } ngx_sprintf(ctx->shpool->log_ctx, " in limit_tcp zone \"%V\"%Z", &shm_zone->shm.name); return NGX_OK; }
static ngx_int_t ngx_http_reqstat_init_zone(ngx_shm_zone_t *shm_zone, void *data) { ngx_http_reqstat_ctx_t *ctx, *octx; octx = data; ctx = shm_zone->data; if (octx != NULL) { if (ngx_strcmp(ctx->val->data, octx->val->data) != 0) { ngx_log_error(NGX_LOG_EMERG, shm_zone->shm.log, 0, "reqstat \"%V\" uses the value str \"%V\" " "while previously it used \"%V\"", &shm_zone->shm.name, ctx->val, octx->val); return NGX_ERROR; } ctx->shpool = octx->shpool; ctx->sh = octx->sh; return NGX_OK; } ctx->shpool = (ngx_slab_pool_t *) shm_zone->shm.addr; ctx->sh = ngx_slab_alloc(ctx->shpool, sizeof(ngx_http_reqstat_shctx_t)); if (ctx->sh == NULL) { return NGX_ERROR; } ctx->shpool->data = ctx->sh; ngx_rbtree_init(&ctx->sh->rbtree, &ctx->sh->sentinel, ngx_http_reqstat_rbtree_insert_value); ngx_queue_init(&ctx->sh->queue); ngx_queue_init(&ctx->sh->visit); return NGX_OK; }
// shared memory zone initializer static ngx_int_t ngx_http_push_init_shm_zone(ngx_shm_zone_t * shm_zone, void *data) { if(data) { /* zone already initialized */ shm_zone->data = data; return NGX_OK; } ngx_slab_pool_t *shpool = (ngx_slab_pool_t *) shm_zone->shm.addr; ngx_rbtree_node_t *sentinel; ngx_http_push_shm_data_t *d; if ((d = (ngx_http_push_shm_data_t *)ngx_slab_alloc(shpool, sizeof(*d))) == NULL) { //shm_data plus an array. return NGX_ERROR; } shm_zone->data = d; d->ipc=NULL; //initialize rbtree if ((sentinel = ngx_slab_alloc(shpool, sizeof(*sentinel)))==NULL) { return NGX_ERROR; } ngx_rbtree_init(&d->tree, sentinel, ngx_http_push_rbtree_insert); return NGX_OK; }
ngx_int_t rbtree_init(rbtree_seed_t *seed, char *name, void *(*id)(void *), uint32_t (*hash)(void *), ngx_int_t (*compare)(void *, void *)) { seed->name=name; assert(id != NULL); if(hash==NULL) { hash = &rbtree_hash_crc32; } if(compare == NULL) { compare = &rbtree_compare_str; } seed->id = id; seed->hash = hash; seed->compare = compare; seed->active_nodes = 0; seed->allocd_nodes = 0; #if NCHAN_RBTREE_DBG /* //super-heavy debugging setup ngx_int_t max = sizeof(seed->actives); ngx_memzero(seed->actives, sizeof(seed->actives)); */ #endif ngx_rbtree_init(&seed->tree, &seed->sentinel, &rbtree_insert_generic); return NGX_OK; }
static ngx_int_t ngx_http_ip_blacklist_init_shm_zone(ngx_shm_zone_t *shm_zone, void *data) { ngx_slab_pool_t *shpool; ngx_http_ip_blacklist_tree_t *blacklist_tree; ngx_rbtree_node_t *sentinel; if (data) { shm_zone->data = data; return NGX_OK; } shpool = (ngx_slab_pool_t *) shm_zone->shm.addr; blacklist_tree = ngx_slab_alloc(shpool, sizeof(ngx_http_ip_blacklist_tree_t)); if (blacklist_tree == NULL) { return NGX_ERROR; } memset(blacklist_tree, 0, sizeof(ngx_http_ip_blacklist_tree_t)); sentinel = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_node_t)); if (sentinel == NULL) { return NGX_ERROR; } ngx_rbtree_init(&blacklist_tree->blacklist, sentinel, ngx_http_ip_blacklist_rbtree_insert_value); ngx_queue_init(&blacklist_tree->garbage); blacklist_tree->shpool = shpool; shm_zone->data = blacklist_tree; return NGX_OK; }
static ngx_int_t ngx_http_cloudrouter_init_shm(ngx_shm_zone_t *shm_zone, void *data) { ngx_rbtree_node_t *sentinel; if (data) { shm_zone->data = data; return NGX_OK; } ngx_http_cloudrouter_shpool = (ngx_slab_pool_t *) shm_zone->shm.addr; shm_zone->data = ngx_http_cloudrouter_shpool; sentinel = ngx_slab_alloc(ngx_http_cloudrouter_shpool, sizeof *sentinel); if (sentinel == NULL) return NGX_ERROR; ngx_http_cloudrouter_rbtree = ngx_slab_alloc(ngx_http_cloudrouter_shpool, sizeof *ngx_http_cloudrouter_rbtree); if (ngx_http_cloudrouter_rbtree == NULL) return NGX_ERROR; ngx_rbtree_init(ngx_http_cloudrouter_rbtree, sentinel, ngx_rbtree_insert_value); return NGX_OK; }
static ngx_btt_torrent_t * ngx_btt_get_torrent(ngx_btt_conf_t *bcf, ngx_btt_ctx_t *ctx, ngx_rbtree_t *tree, ngx_queue_t *queue) { ngx_queue_t *q; ngx_btt_torrent_t *t; t = NULL; if (!ngx_queue_empty(&bcf->btt->free_torrents)) { q = ngx_queue_head(&bcf->btt->free_torrents); ngx_queue_remove(q); t = ngx_queue_data(q, ngx_btt_torrent_t, queue); } if (t == NULL) { t = ngx_slab_alloc_locked(bcf->pool, sizeof(ngx_btt_torrent_t)); if (t == NULL) { return NULL; } } /* TODO */ ngx_rbtree_init(&t->peers_rbtree, &t->peers_sentinel, ngx_btt_insert_peer); ngx_queue_init(&t->peers_queue); ngx_memcpy(t->info_hash, ctx->info_hash, sizeof(ctx->info_hash)); t->node.key = ngx_crc32_short(ctx->info_hash, sizeof(ctx->info_hash)); ngx_rbtree_insert(tree, &t->node); ngx_queue_insert_head(queue, &t->queue); return t; }
static ngx_int_t ngx_http_limit_req_init_zone(ngx_shm_zone_t *shm_zone, void *data) { ngx_http_limit_req_ctx_t *octx = data; size_t len; ngx_http_limit_req_ctx_t *ctx; ctx = shm_zone->data; if (octx) { if (ctx->key.value.len != octx->key.value.len || ngx_strncmp(ctx->key.value.data, octx->key.value.data, ctx->key.value.len) != 0) { ngx_log_error(NGX_LOG_EMERG, shm_zone->shm.log, 0, "limit_req \"%V\" uses the \"%V\" key " "while previously it used the \"%V\" key", &shm_zone->shm.name, &ctx->key.value, &octx->key.value); return NGX_ERROR; } ctx->sh = octx->sh; ctx->shpool = octx->shpool; return NGX_OK; } ctx->shpool = (ngx_slab_pool_t *) shm_zone->shm.addr; if (shm_zone->shm.exists) { ctx->sh = ctx->shpool->data; return NGX_OK; } ctx->sh = ngx_slab_alloc(ctx->shpool, sizeof(ngx_http_limit_req_shctx_t)); if (ctx->sh == NULL) { return NGX_ERROR; } ctx->shpool->data = ctx->sh; ngx_rbtree_init(&ctx->sh->rbtree, &ctx->sh->sentinel, ngx_http_limit_req_rbtree_insert_value); ngx_queue_init(&ctx->sh->queue); len = sizeof(" in limit_req zone \"\"") + shm_zone->shm.name.len; ctx->shpool->log_ctx = ngx_slab_alloc(ctx->shpool, len); if (ctx->shpool->log_ctx == NULL) { return NGX_ERROR; } ngx_sprintf(ctx->shpool->log_ctx, " in limit_req zone \"%V\"%Z", &shm_zone->shm.name); ctx->shpool->log_nomem = 0; return NGX_OK; }
static ngx_int_t ngx_http_limit_speed_init_zone(ngx_shm_zone_t *shm_zone, void *data) { ngx_http_limit_speed_ctx_t *octx = data; size_t len; ngx_slab_pool_t *shpool; ngx_rbtree_node_t *sentinel; ngx_http_limit_speed_ctx_t *ctx; ctx = shm_zone->data; if (octx) { if (ngx_strcmp(ctx->var.data, octx->var.data) != 0) { ngx_log_error(NGX_LOG_EMERG, shm_zone->shm.log, 0, "limit_speed \"%V\" uses the \"%V\" variable " "while previously it used the \"%V\" variable", &shm_zone->shm.name, &ctx->var, &octx->var); return NGX_ERROR; } ctx->rbtree = octx->rbtree; return NGX_OK; } shpool = (ngx_slab_pool_t *) shm_zone->shm.addr; if (shm_zone->shm.exists) { ctx->rbtree = shpool->data; return NGX_OK; } ctx->rbtree = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_t)); if (ctx->rbtree == NULL) { return NGX_ERROR; } shpool->data = ctx->rbtree; sentinel = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_node_t)); if (sentinel == NULL) { return NGX_ERROR; } ngx_rbtree_init(ctx->rbtree, sentinel, ngx_http_limit_speed_rbtree_insert_value); len = sizeof(" in limit_speed \"\"") + shm_zone->shm.name.len; shpool->log_ctx = ngx_slab_alloc(shpool, len); if (shpool->log_ctx == NULL) { return NGX_ERROR; } ngx_http_next_body_filter = ngx_http_top_body_filter; ngx_http_top_body_filter = ngx_http_limit_speed_body_filter; ngx_sprintf(shpool->log_ctx, " in limit_speed \"%V\"%Z", &shm_zone->shm.name); return NGX_OK; }
static ngx_int_t ngx_http_limit_req2_init_zone(ngx_shm_zone_t *shm_zone, void *data) { ngx_http_limit_req2_ctx_t *octx = data; size_t len; ngx_uint_t i, j; ngx_http_limit_req2_ctx_t *ctx; ngx_http_limit_req2_variable_t *v1, *v2; ctx = shm_zone->data; v1 = ctx->limit_vars->elts; if (octx) { v2 = octx->limit_vars->elts; if (ctx->limit_vars->nelts != octx->limit_vars->nelts) { ngx_log_error(NGX_LOG_EMERG, shm_zone->shm.log, 0, "limit_req2 \"%V\" uses the \"%V\" variable " "while previously it used the \"%V\" variable", &shm_zone->shm.name, &v1[0].var, &v2[0].var); return NGX_ERROR; } for (i = 0, j = 0; i < ctx->limit_vars->nelts && j < octx->limit_vars->nelts; i++, j++) { if (ngx_strcmp(v1[i].var.data, v2[j].var.data) != 0) { ngx_log_error(NGX_LOG_EMERG, shm_zone->shm.log, 0, "limit_req2 \"%V\" uses the \"%V\" variable " "while previously it used the \"%V\" variable", &shm_zone->shm.name, &v1[i].var, &v2[j].var); return NGX_ERROR; } } ctx->sh = octx->sh; ctx->shpool = octx->shpool; return NGX_OK; } ctx->shpool = (ngx_slab_pool_t *) shm_zone->shm.addr; if (shm_zone->shm.exists) { ctx->sh = ctx->shpool->data; return NGX_OK; } ctx->sh = ngx_slab_alloc(ctx->shpool, sizeof(ngx_http_limit_req2_shctx_t)); if (ctx->sh == NULL) { return NGX_ERROR; } ctx->shpool->data = ctx->sh; ngx_rbtree_init(&ctx->sh->rbtree, &ctx->sh->sentinel, ngx_http_limit_req2_rbtree_insert_value); ngx_queue_init(&ctx->sh->queue); len = sizeof(" in limit_req2 zone \"\"") + shm_zone->shm.name.len; ctx->shpool->log_ctx = ngx_slab_alloc(ctx->shpool, len); if (ctx->shpool->log_ctx == NULL) { return NGX_ERROR; } ngx_sprintf(ctx->shpool->log_ctx, " in limit_req2 zone \"%V\"%Z", &shm_zone->shm.name); return NGX_OK; }
static ngx_int_t ngx_status_ex_init_zone(ngx_shm_zone_t *shm_zone, void *data) { ngx_status_ex_host_ctx_t *octx = data; size_t len; ngx_slab_pool_t *shpool; ngx_rbtree_node_t *sentinel; ngx_status_ex_host_ctx_t *ctx; ctx = shm_zone->data; if (octx) { ctx->rbtree = octx->rbtree; status_ex_shm_zone = shm_zone; return NGX_OK; } shpool = (ngx_slab_pool_t *) shm_zone->shm.addr; if (shm_zone->shm.exists) { ctx->rbtree = shpool->data; status_ex_shm_zone = shm_zone; return NGX_OK; } ctx->rbtree = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_t)); if (ctx->rbtree == NULL) { return NGX_ERROR; } shpool->data = ctx->rbtree; sentinel = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_node_t)); if (sentinel == NULL) { return NGX_ERROR; } statu_io_info = ngx_slab_alloc(shpool, sizeof(ngx_tcp_io)); if (statu_io_info == NULL) return NGX_ERROR; statu_url_io_array = ngx_slab_alloc(shpool, sizeof(statu_url_io_array)+ sizeof(ngx_tcp_io*) * STATU_MONITOR_DOMAINS); if (statu_url_io_array == NULL) return NGX_ERROR; statu_url_io_array->number = 0; ngx_rbtree_init(ctx->rbtree, sentinel, ngx_status_ex_host_rbtree_insert_value); len = sizeof(" in status_ex zone \"\"") + shm_zone->shm.name.len; shpool->log_ctx = ngx_slab_alloc(shpool, len); if (shpool->log_ctx == NULL) { return NGX_ERROR; } ngx_sprintf(shpool->log_ctx, " in status_ex zone \"%V\"%Z", &shm_zone->shm.name); status_ex_shm_zone = shm_zone; return NGX_OK; }
ngx_int_t ngx_event_timer_init(ngx_log_t *log) { ngx_rbtree_init(&ngx_event_timer_rbtree, &ngx_event_timer_sentinel, ngx_rbtree_insert_timer_value); return NGX_OK; }
ngx_inline void yy_sec_waf_re_cache_init_rbtree(ngx_rbtree_t *rbtree, ngx_rbtree_node_t *sentinel) { ngx_rbtree_init(rbtree, sentinel, ngx_str_rbtree_insert_value); }
ngx_int_t ngx_http_lua_mqtt_init_shm_zone(ngx_shm_zone_t *shm_zone, void *data) { ngx_http_lua_mqtt_global_shm_data_t *global_shm_data = (ngx_http_lua_mqtt_global_shm_data_t *) ngx_http_lua_mqtt_global_shm_zone->data; ngx_http_lua_mqtt_main_conf_t *mcf = shm_zone->data; ngx_http_lua_mqtt_shm_data_t *d; int i; mcf->shm_zone = shm_zone; mcf->shpool = (ngx_slab_pool_t *) shm_zone->shm.addr; if (data) { /* zone already initialized */ shm_zone->data = data; d = (ngx_http_lua_mqtt_shm_data_t *) data; d->mcf = mcf; d->shm_zone = shm_zone; d->shpool = mcf->shpool; mcf->shm_data = data; ngx_queue_insert_tail(&global_shm_data->shm_datas_queue, &d->shm_data_queue); return NGX_OK; } ngx_rbtree_node_t *sentinel; if ((d = (ngx_http_lua_mqtt_shm_data_t *) ngx_slab_alloc(mcf->shpool, sizeof(*d))) == NULL) { //shm_data plus an array. return NGX_ERROR; } d->mcf = mcf; mcf->shm_data = d; shm_zone->data = d; for (i = 0; i < NGX_MAX_PROCESSES; i++) { d->ipc[i].pid = -1; d->ipc[i].startup = 0; d->ipc[i].subscribers = 0; ngx_queue_init(&d->ipc[i].messages_queue); ngx_queue_init(&d->ipc[i].subscribers_queue); } d->channels = 0; d->wildcard_channels = 0; d->published_messages = 0; d->stored_messages = 0; d->subscribers = 0; d->channels_in_trash = 0; d->messages_in_trash = 0; d->startup = ngx_time(); d->last_message_time = 0; d->last_message_tag = 0; d->shm_zone = shm_zone; d->shpool = mcf->shpool; d->slots_for_census = 0; // initialize rbtree if ((sentinel = ngx_slab_alloc(mcf->shpool, sizeof(*sentinel))) == NULL) { return NGX_ERROR; } ngx_rbtree_init(&d->tree, sentinel, ngx_http_lua_mqtt_rbtree_insert); ngx_queue_init(&d->messages_trash); ngx_queue_init(&d->channels_queue); ngx_queue_init(&d->channels_to_delete); ngx_queue_init(&d->channels_trash); ngx_queue_insert_tail(&global_shm_data->shm_datas_queue, &d->shm_data_queue); if (ngx_http_lua_mqtt_create_shmtx(&d->messages_trash_mutex, &d->messages_trash_lock, (u_char *) "lua_mqtt_messages_trash") != NGX_OK) { return NGX_ERROR; } if (ngx_http_lua_mqtt_create_shmtx(&d->channels_queue_mutex, &d->channels_queue_lock, (u_char *) "lua_mqtt_channels_queue") != NGX_OK) { return NGX_ERROR; } if (ngx_http_lua_mqtt_create_shmtx(&d->channels_to_delete_mutex, &d->channels_to_delete_lock, (u_char *) "lua_mqtt_channels_to_delete") != NGX_OK) { return NGX_ERROR; } if (ngx_http_lua_mqtt_create_shmtx(&d->channels_trash_mutex, &d->channels_trash_lock, (u_char *) "lua_mqtt_channels_trash") != NGX_OK) { return NGX_ERROR; } if (ngx_http_lua_mqtt_create_shmtx(&d->cleanup_mutex, &d->cleanup_lock, (u_char *) "lua_mqtt_cleanup") != NGX_OK) { return NGX_ERROR; } u_char lock_name[25]; for (i = 0; i < 10; i++) { ngx_sprintf(lock_name, "lua_mqtt_channels_%d", i); if (ngx_http_lua_mqtt_create_shmtx(&d->channels_mutex[i], &d->channels_lock[i], lock_name) != NGX_OK) { return NGX_ERROR; } } d->mutex_round_robin = 0; if (mcf->events_channel_id.len > 0) { if ((mcf->events_channel = ngx_http_lua_mqtt_get_channel(&mcf->events_channel_id, ngx_cycle->log, mcf)) == NULL) { ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "push stream module: unable to create events channel"); return NGX_ERROR; } if (ngx_http_lua_mqtt_create_shmtx(&d->events_channel_mutex, &d->events_channel_lock, (u_char *) "lua_mqtt_events_channel") != NGX_OK) { return NGX_ERROR; } mcf->events_channel->mutex = &d->events_channel_mutex; } return NGX_OK; }
static ngx_int_t ngx_http_file_cache_init(ngx_shm_zone_t *shm_zone, void *data) { ngx_http_file_cache_t *ocache = data; size_t len; ngx_uint_t n; ngx_http_file_cache_t *cache; cache = shm_zone->data; if (ocache) { if (ngx_strcmp(cache->path->name.data, ocache->path->name.data) != 0) { ngx_log_error(NGX_LOG_EMERG, shm_zone->shm.log, 0, "cache \"%V\" uses the \"%V\" cache path " "while previously it used the \"%V\" cache path", &shm_zone->shm.name, &cache->path->name, &ocache->path->name); return NGX_ERROR; } for (n = 0; n < 3; n++) { if (cache->path->level[n] != ocache->path->level[n]) { ngx_log_error(NGX_LOG_EMERG, shm_zone->shm.log, 0, "cache \"%V\" had previously different levels", &shm_zone->shm.name); return NGX_ERROR; } } cache->sh = ocache->sh; cache->shpool = ocache->shpool; cache->bsize = ocache->bsize; cache->max_size /= cache->bsize; if (!cache->sh->cold || cache->sh->loading) { cache->path->loader = NULL; } return NGX_OK; } cache->shpool = (ngx_slab_pool_t *) shm_zone->shm.addr; if (shm_zone->shm.exists) { cache->sh = cache->shpool->data; cache->bsize = ngx_fs_bsize(cache->path->name.data); return NGX_OK; } cache->sh = ngx_slab_alloc(cache->shpool, sizeof(ngx_http_file_cache_sh_t)); if (cache->sh == NULL) { return NGX_ERROR; } cache->shpool->data = cache->sh; ngx_rbtree_init(&cache->sh->rbtree, &cache->sh->sentinel, ngx_http_file_cache_rbtree_insert_value); ngx_queue_init(&cache->sh->queue); cache->sh->cold = 1; cache->sh->loading = 0; cache->sh->size = 0; cache->bsize = ngx_fs_bsize(cache->path->name.data); cache->max_size /= cache->bsize; len = sizeof(" in cache keys zone \"\"") + shm_zone->shm.name.len; cache->shpool->log_ctx = ngx_slab_alloc(cache->shpool, len); if (cache->shpool->log_ctx == NULL) { return NGX_ERROR; } ngx_sprintf(cache->shpool->log_ctx, " in cache keys zone \"%V\"%Z", &shm_zone->shm.name); return NGX_OK; }
ngx_resolver_t * ngx_resolver_create(ngx_conf_t *cf, ngx_addr_t *addr) { ngx_resolver_t *r; ngx_pool_cleanup_t *cln; ngx_udp_connection_t *uc; cln = ngx_pool_cleanup_add(cf->pool, 0); if (cln == NULL) { return NULL; } cln->handler = ngx_resolver_cleanup; r = ngx_calloc(sizeof(ngx_resolver_t), cf->log); if (r == NULL) { return NULL; } cln->data = r; r->event = ngx_calloc(sizeof(ngx_event_t), cf->log); if (r->event == NULL) { return NULL; } ngx_rbtree_init(&r->name_rbtree, &r->name_sentinel, ngx_resolver_rbtree_insert_value); ngx_rbtree_init(&r->addr_rbtree, &r->addr_sentinel, ngx_rbtree_insert_value); ngx_queue_init(&r->name_resend_queue); ngx_queue_init(&r->addr_resend_queue); ngx_queue_init(&r->name_expire_queue); ngx_queue_init(&r->addr_expire_queue); r->event->handler = ngx_resolver_resend_handler; r->event->data = r; r->event->log = &cf->cycle->new_log; r->ident = -1; r->resend_timeout = 5; r->expire = 30; r->valid = 300; r->log = &cf->cycle->new_log; r->log_level = NGX_LOG_ALERT; if (addr) { uc = ngx_calloc(sizeof(ngx_udp_connection_t), cf->log); if (uc == NULL) { return NULL; } r->udp_connection = uc; uc->sockaddr = addr->sockaddr; uc->socklen = addr->socklen; uc->server = addr->name; uc->log = cf->cycle->new_log; uc->log.handler = ngx_resolver_log_error; uc->log.data = uc; uc->log.action = "resolving"; } return r; }
ngx_cycle_t * ngx_init_cycle(ngx_cycle_t *old_cycle) { void *rv; char **senv; ngx_uint_t i, n; ngx_log_t *log; ngx_time_t *tp; ngx_conf_t conf; ngx_pool_t *pool; ngx_cycle_t *cycle, **old; ngx_shm_zone_t *shm_zone, *oshm_zone; ngx_list_part_t *part, *opart; ngx_open_file_t *file; ngx_listening_t *ls, *nls; ngx_core_conf_t *ccf, *old_ccf; ngx_core_module_t *module; char hostname[NGX_MAXHOSTNAMELEN]; ngx_timezone_update(); /* force localtime update with a new timezone */ tp = ngx_timeofday(); tp->sec = 0; ngx_time_update(); log = old_cycle->log; pool = ngx_create_pool(NGX_CYCLE_POOL_SIZE, log); if (pool == NULL) { return NULL; } pool->log = log; cycle = ngx_pcalloc(pool, sizeof(ngx_cycle_t)); if (cycle == NULL) { ngx_destroy_pool(pool); return NULL; } cycle->pool = pool; cycle->log = log; cycle->old_cycle = old_cycle; cycle->conf_prefix.len = old_cycle->conf_prefix.len; cycle->conf_prefix.data = ngx_pstrdup(pool, &old_cycle->conf_prefix); if (cycle->conf_prefix.data == NULL) { ngx_destroy_pool(pool); return NULL; } cycle->prefix.len = old_cycle->prefix.len; cycle->prefix.data = ngx_pstrdup(pool, &old_cycle->prefix); if (cycle->prefix.data == NULL) { ngx_destroy_pool(pool); return NULL; } cycle->conf_file.len = old_cycle->conf_file.len; cycle->conf_file.data = ngx_pnalloc(pool, old_cycle->conf_file.len + 1); if (cycle->conf_file.data == NULL) { ngx_destroy_pool(pool); return NULL; } ngx_cpystrn(cycle->conf_file.data, old_cycle->conf_file.data, old_cycle->conf_file.len + 1); cycle->conf_param.len = old_cycle->conf_param.len; cycle->conf_param.data = ngx_pstrdup(pool, &old_cycle->conf_param); if (cycle->conf_param.data == NULL) { ngx_destroy_pool(pool); return NULL; } n = old_cycle->paths.nelts ? old_cycle->paths.nelts : 10; if (ngx_array_init(&cycle->paths, pool, n, sizeof(ngx_path_t *)) != NGX_OK) { ngx_destroy_pool(pool); return NULL; } ngx_memzero(cycle->paths.elts, n * sizeof(ngx_path_t *)); if (ngx_array_init(&cycle->config_dump, pool, 1, sizeof(ngx_conf_dump_t)) != NGX_OK) { ngx_destroy_pool(pool); return NULL; } ngx_rbtree_init(&cycle->config_dump_rbtree, &cycle->config_dump_sentinel, ngx_str_rbtree_insert_value); if (old_cycle->open_files.part.nelts) { n = old_cycle->open_files.part.nelts; for (part = old_cycle->open_files.part.next; part; part = part->next) { n += part->nelts; } } else { n = 20; } if (ngx_list_init(&cycle->open_files, pool, n, sizeof(ngx_open_file_t)) != NGX_OK) { ngx_destroy_pool(pool); return NULL; } if (old_cycle->shared_memory.part.nelts) { n = old_cycle->shared_memory.part.nelts; for (part = old_cycle->shared_memory.part.next; part; part = part->next) { n += part->nelts; } } else { n = 1; } if (ngx_list_init(&cycle->shared_memory, pool, n, sizeof(ngx_shm_zone_t)) != NGX_OK) { ngx_destroy_pool(pool); return NULL; } n = old_cycle->listening.nelts ? old_cycle->listening.nelts : 10; if (ngx_array_init(&cycle->listening, pool, n, sizeof(ngx_listening_t)) != NGX_OK) { ngx_destroy_pool(pool); return NULL; } ngx_memzero(cycle->listening.elts, n * sizeof(ngx_listening_t)); ngx_queue_init(&cycle->reusable_connections_queue); cycle->conf_ctx = ngx_pcalloc(pool, ngx_max_module * sizeof(void *)); if (cycle->conf_ctx == NULL) { ngx_destroy_pool(pool); return NULL; } if (gethostname(hostname, NGX_MAXHOSTNAMELEN) == -1) { ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "gethostname() failed"); ngx_destroy_pool(pool); return NULL; } /* on Linux gethostname() silently truncates name that does not fit */ hostname[NGX_MAXHOSTNAMELEN - 1] = '\0'; cycle->hostname.len = ngx_strlen(hostname); cycle->hostname.data = ngx_pnalloc(pool, cycle->hostname.len); if (cycle->hostname.data == NULL) { ngx_destroy_pool(pool); return NULL; } ngx_strlow(cycle->hostname.data, (u_char *) hostname, cycle->hostname.len); if (ngx_cycle_modules(cycle) != NGX_OK) { ngx_destroy_pool(pool); return NULL; } for (i = 0; cycle->modules[i]; i++) { if (cycle->modules[i]->type != NGX_CORE_MODULE) { continue; } module = cycle->modules[i]->ctx; if (module->create_conf) { rv = module->create_conf(cycle); if (rv == NULL) { ngx_destroy_pool(pool); return NULL; } cycle->conf_ctx[cycle->modules[i]->index] = rv; } } senv = environ; ngx_memzero(&conf, sizeof(ngx_conf_t)); /* STUB: init array ? */ conf.args = ngx_array_create(pool, 10, sizeof(ngx_str_t)); if (conf.args == NULL) { ngx_destroy_pool(pool); return NULL; } conf.temp_pool = ngx_create_pool(NGX_CYCLE_POOL_SIZE, log); if (conf.temp_pool == NULL) { ngx_destroy_pool(pool); return NULL; } conf.ctx = cycle->conf_ctx; conf.cycle = cycle; conf.pool = pool; conf.log = log; conf.module_type = NGX_CORE_MODULE; conf.cmd_type = NGX_MAIN_CONF; #if 0 log->log_level = NGX_LOG_DEBUG_ALL; #endif if (ngx_conf_param(&conf) != NGX_CONF_OK) { environ = senv; ngx_destroy_cycle_pools(&conf); return NULL; } if (ngx_conf_parse(&conf, &cycle->conf_file) != NGX_CONF_OK) { environ = senv; ngx_destroy_cycle_pools(&conf); return NULL; } if (ngx_test_config && !ngx_quiet_mode) { ngx_log_stderr(0, "the configuration file %s syntax is ok", cycle->conf_file.data); } for (i = 0; cycle->modules[i]; i++) { if (cycle->modules[i]->type != NGX_CORE_MODULE) { continue; } module = cycle->modules[i]->ctx; if (module->init_conf) { if (module->init_conf(cycle, cycle->conf_ctx[cycle->modules[i]->index]) == NGX_CONF_ERROR) { environ = senv; ngx_destroy_cycle_pools(&conf); return NULL; } } } if (ngx_process == NGX_PROCESS_SIGNALLER) { return cycle; } ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); if (ngx_test_config) { if (ngx_create_pidfile(&ccf->pid, log) != NGX_OK) { goto failed; } } else if (!ngx_is_init_cycle(old_cycle)) { /* * we do not create the pid file in the first ngx_init_cycle() call * because we need to write the demonized process pid */ old_ccf = (ngx_core_conf_t *) ngx_get_conf(old_cycle->conf_ctx, ngx_core_module); if (ccf->pid.len != old_ccf->pid.len || ngx_strcmp(ccf->pid.data, old_ccf->pid.data) != 0) { /* new pid file name */ if (ngx_create_pidfile(&ccf->pid, log) != NGX_OK) { goto failed; } ngx_delete_pidfile(old_cycle); } } if (ngx_test_lockfile(cycle->lock_file.data, log) != NGX_OK) { goto failed; } if (ngx_create_paths(cycle, ccf->user) != NGX_OK) { goto failed; } if (ngx_log_open_default(cycle) != NGX_OK) { goto failed; } /* open the new files */ part = &cycle->open_files.part; file = part->elts; for (i = 0; /* void */ ; i++) { if (i >= part->nelts) { if (part->next == NULL) { break; } part = part->next; file = part->elts; i = 0; } if (file[i].name.len == 0) { continue; } file[i].fd = ngx_open_file(file[i].name.data, NGX_FILE_APPEND, NGX_FILE_CREATE_OR_OPEN, NGX_FILE_DEFAULT_ACCESS); ngx_log_debug3(NGX_LOG_DEBUG_CORE, log, 0, "log: %p %d \"%s\"", &file[i], file[i].fd, file[i].name.data); if (file[i].fd == NGX_INVALID_FILE) { ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, ngx_open_file_n " \"%s\" failed", file[i].name.data); goto failed; } #if !(NGX_WIN32) if (fcntl(file[i].fd, F_SETFD, FD_CLOEXEC) == -1) { ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "fcntl(FD_CLOEXEC) \"%s\" failed", file[i].name.data); goto failed; } #endif } cycle->log = &cycle->new_log; pool->log = &cycle->new_log; /* create shared memory */ part = &cycle->shared_memory.part; shm_zone = part->elts; for (i = 0; /* void */ ; i++) { if (i >= part->nelts) { if (part->next == NULL) { break; } part = part->next; shm_zone = part->elts; i = 0; } if (shm_zone[i].shm.size == 0) { ngx_log_error(NGX_LOG_EMERG, log, 0, "zero size shared memory zone \"%V\"", &shm_zone[i].shm.name); goto failed; } shm_zone[i].shm.log = cycle->log; opart = &old_cycle->shared_memory.part; oshm_zone = opart->elts; for (n = 0; /* void */ ; n++) { if (n >= opart->nelts) { if (opart->next == NULL) { break; } opart = opart->next; oshm_zone = opart->elts; n = 0; } if (shm_zone[i].shm.name.len != oshm_zone[n].shm.name.len) { continue; } if (ngx_strncmp(shm_zone[i].shm.name.data, oshm_zone[n].shm.name.data, shm_zone[i].shm.name.len) != 0) { continue; } if (shm_zone[i].tag == oshm_zone[n].tag && shm_zone[i].shm.size == oshm_zone[n].shm.size && !shm_zone[i].noreuse) { shm_zone[i].shm.addr = oshm_zone[n].shm.addr; #if (NGX_WIN32) shm_zone[i].shm.handle = oshm_zone[n].shm.handle; #endif if (shm_zone[i].init(&shm_zone[i], oshm_zone[n].data) != NGX_OK) { goto failed; } goto shm_zone_found; } break; } if (ngx_shm_alloc(&shm_zone[i].shm) != NGX_OK) { goto failed; } if (ngx_init_zone_pool(cycle, &shm_zone[i]) != NGX_OK) { goto failed; } if (shm_zone[i].init(&shm_zone[i], NULL) != NGX_OK) { goto failed; } shm_zone_found: continue; } /* handle the listening sockets */ if (old_cycle->listening.nelts) { ls = old_cycle->listening.elts; for (i = 0; i < old_cycle->listening.nelts; i++) { ls[i].remain = 0; } nls = cycle->listening.elts; for (n = 0; n < cycle->listening.nelts; n++) { for (i = 0; i < old_cycle->listening.nelts; i++) { if (ls[i].ignore) { continue; } if (ls[i].remain) { continue; } if (ls[i].type != nls[n].type) { continue; } if (ngx_cmp_sockaddr(nls[n].sockaddr, nls[n].socklen, ls[i].sockaddr, ls[i].socklen, 1) == NGX_OK) { nls[n].fd = ls[i].fd; nls[n].previous = &ls[i]; ls[i].remain = 1; if (ls[i].backlog != nls[n].backlog) { nls[n].listen = 1; } #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER) /* * FreeBSD, except the most recent versions, * could not remove accept filter */ nls[n].deferred_accept = ls[i].deferred_accept; if (ls[i].accept_filter && nls[n].accept_filter) { if (ngx_strcmp(ls[i].accept_filter, nls[n].accept_filter) != 0) { nls[n].delete_deferred = 1; nls[n].add_deferred = 1; } } else if (ls[i].accept_filter) { nls[n].delete_deferred = 1; } else if (nls[n].accept_filter) { nls[n].add_deferred = 1; } #endif #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT) if (ls[i].deferred_accept && !nls[n].deferred_accept) { nls[n].delete_deferred = 1; } else if (ls[i].deferred_accept != nls[n].deferred_accept) { nls[n].add_deferred = 1; } #endif #if (NGX_HAVE_REUSEPORT) if (nls[n].reuseport && !ls[i].reuseport) { nls[n].add_reuseport = 1; } #endif break; } } if (nls[n].fd == (ngx_socket_t) -1) { nls[n].open = 1; #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER) if (nls[n].accept_filter) { nls[n].add_deferred = 1; } #endif #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT) if (nls[n].deferred_accept) { nls[n].add_deferred = 1; } #endif } } } else { ls = cycle->listening.elts; for (i = 0; i < cycle->listening.nelts; i++) { ls[i].open = 1; #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER) if (ls[i].accept_filter) { ls[i].add_deferred = 1; } #endif #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT) if (ls[i].deferred_accept) { ls[i].add_deferred = 1; } #endif } } if (ngx_open_listening_sockets(cycle) != NGX_OK) { goto failed; } if (!ngx_test_config) { ngx_configure_listening_sockets(cycle); } /* commit the new cycle configuration */ if (!ngx_use_stderr) { (void) ngx_log_redirect_stderr(cycle); } pool->log = cycle->log; if (ngx_init_modules(cycle) != NGX_OK) { /* fatal */ exit(1); } /* close and delete stuff that lefts from an old cycle */ /* free the unnecessary shared memory */ opart = &old_cycle->shared_memory.part; oshm_zone = opart->elts; for (i = 0; /* void */ ; i++) { if (i >= opart->nelts) { if (opart->next == NULL) { goto old_shm_zone_done; } opart = opart->next; oshm_zone = opart->elts; i = 0; } part = &cycle->shared_memory.part; shm_zone = part->elts; for (n = 0; /* void */ ; n++) { if (n >= part->nelts) { if (part->next == NULL) { break; } part = part->next; shm_zone = part->elts; n = 0; } if (oshm_zone[i].shm.name.len != shm_zone[n].shm.name.len) { continue; } if (ngx_strncmp(oshm_zone[i].shm.name.data, shm_zone[n].shm.name.data, oshm_zone[i].shm.name.len) != 0) { continue; } if (oshm_zone[i].tag == shm_zone[n].tag && oshm_zone[i].shm.size == shm_zone[n].shm.size && !oshm_zone[i].noreuse) { goto live_shm_zone; } break; } ngx_shm_free(&oshm_zone[i].shm); live_shm_zone: continue; } old_shm_zone_done: /* close the unnecessary listening sockets */ ls = old_cycle->listening.elts; for (i = 0; i < old_cycle->listening.nelts; i++) { if (ls[i].remain || ls[i].fd == (ngx_socket_t) -1) { continue; } if (ngx_close_socket(ls[i].fd) == -1) { ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, ngx_close_socket_n " listening socket on %V failed", &ls[i].addr_text); } #if (NGX_HAVE_UNIX_DOMAIN) if (ls[i].sockaddr->sa_family == AF_UNIX) { u_char *name; name = ls[i].addr_text.data + sizeof("unix:") - 1; ngx_log_error(NGX_LOG_WARN, cycle->log, 0, "deleting socket %s", name); if (ngx_delete_file(name) == NGX_FILE_ERROR) { ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_socket_errno, ngx_delete_file_n " %s failed", name); } } #endif } /* close the unnecessary open files */ part = &old_cycle->open_files.part; file = part->elts; for (i = 0; /* void */ ; i++) { if (i >= part->nelts) { if (part->next == NULL) { break; } part = part->next; file = part->elts; i = 0; } if (file[i].fd == NGX_INVALID_FILE || file[i].fd == ngx_stderr) { continue; } if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) { ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, ngx_close_file_n " \"%s\" failed", file[i].name.data); } } ngx_destroy_pool(conf.temp_pool); if (ngx_process == NGX_PROCESS_MASTER || ngx_is_init_cycle(old_cycle)) { ngx_destroy_pool(old_cycle->pool); cycle->old_cycle = NULL; return cycle; } if (ngx_temp_pool == NULL) { ngx_temp_pool = ngx_create_pool(128, cycle->log); if (ngx_temp_pool == NULL) { ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, "could not create ngx_temp_pool"); exit(1); } n = 10; if (ngx_array_init(&ngx_old_cycles, ngx_temp_pool, n, sizeof(ngx_cycle_t *)) != NGX_OK) { exit(1); } ngx_memzero(ngx_old_cycles.elts, n * sizeof(ngx_cycle_t *)); ngx_cleaner_event.handler = ngx_clean_old_cycles; ngx_cleaner_event.log = cycle->log; ngx_cleaner_event.data = &dumb; dumb.fd = (ngx_socket_t) -1; } ngx_temp_pool->log = cycle->log; old = ngx_array_push(&ngx_old_cycles); if (old == NULL) { exit(1); } *old = old_cycle; if (!ngx_cleaner_event.timer_set) { ngx_add_timer(&ngx_cleaner_event, 30000); ngx_cleaner_event.timer_set = 1; } return cycle; failed: if (!ngx_is_init_cycle(old_cycle)) { old_ccf = (ngx_core_conf_t *) ngx_get_conf(old_cycle->conf_ctx, ngx_core_module); if (old_ccf->environment) { environ = old_ccf->environment; } } /* rollback the new cycle configuration */ part = &cycle->open_files.part; file = part->elts; for (i = 0; /* void */ ; i++) { if (i >= part->nelts) { if (part->next == NULL) { break; } part = part->next; file = part->elts; i = 0; } if (file[i].fd == NGX_INVALID_FILE || file[i].fd == ngx_stderr) { continue; } if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) { ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, ngx_close_file_n " \"%s\" failed", file[i].name.data); } } if (ngx_test_config) { ngx_destroy_cycle_pools(&conf); return NULL; } ls = cycle->listening.elts; for (i = 0; i < cycle->listening.nelts; i++) { if (ls[i].fd == (ngx_socket_t) -1 || !ls[i].open) { continue; } if (ngx_close_socket(ls[i].fd) == -1) { ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, ngx_close_socket_n " %V failed", &ls[i].addr_text); } } ngx_destroy_cycle_pools(&conf); return NULL; }
ngx_resolver_t * ngx_resolver_create(ngx_conf_t *cf, ngx_str_t *names, ngx_uint_t n) { ngx_str_t s; ngx_url_t u; ngx_uint_t i; ngx_resolver_t *r; ngx_pool_cleanup_t *cln; ngx_udp_connection_t *uc; cln = ngx_pool_cleanup_add(cf->pool, 0); if (cln == NULL) { return NULL; } cln->handler = ngx_resolver_cleanup; r = ngx_calloc(sizeof(ngx_resolver_t), cf->log); if (r == NULL) { return NULL; } if (n) { if (ngx_array_init(&r->udp_connections, cf->pool, n, sizeof(ngx_udp_connection_t)) != NGX_OK) { return NULL; } } cln->data = r; r->event = ngx_calloc(sizeof(ngx_event_t), cf->log); if (r->event == NULL) { return NULL; } ngx_rbtree_init(&r->name_rbtree, &r->name_sentinel, ngx_resolver_rbtree_insert_value); ngx_rbtree_init(&r->addr_rbtree, &r->addr_sentinel, ngx_rbtree_insert_value); ngx_queue_init(&r->name_resend_queue); ngx_queue_init(&r->addr_resend_queue); ngx_queue_init(&r->name_expire_queue); ngx_queue_init(&r->addr_expire_queue); r->event->handler = ngx_resolver_resend_handler; r->event->data = r; r->event->log = &cf->cycle->new_log; r->ident = -1; r->resend_timeout = 5; r->expire = 30; r->valid = 0; r->log = &cf->cycle->new_log; r->log_level = NGX_LOG_ERR; for (i = 0; i < n; i++) { if (ngx_strncmp(names[i].data, "valid=", 6) == 0) { s.len = names[i].len - 6; s.data = names[i].data + 6; r->valid = ngx_parse_time(&s, 1); if (r->valid == NGX_ERROR) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter: %V", &names[i]); return NULL; } continue; } ngx_memzero(&u, sizeof(ngx_url_t)); u.host = names[i]; u.port = 53; if (ngx_inet_resolve_host(cf->pool, &u) != NGX_OK) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%V: %s", &u.host, u.err); return NULL; } uc = ngx_array_push(&r->udp_connections); if (uc == NULL) { return NULL; } ngx_memzero(uc, sizeof(ngx_udp_connection_t)); uc->sockaddr = u.addrs->sockaddr; uc->socklen = u.addrs->socklen; uc->server = u.addrs->name; uc->log = cf->cycle->new_log; uc->log.handler = ngx_resolver_log_error; uc->log.data = uc; uc->log.action = "resolving"; } return r; }
ngx_int_t ngx_tcp_lua_shdict_init_zone(ngx_shm_zone_t *shm_zone, void *data) { ngx_tcp_lua_shdict_ctx_t *octx = data; size_t len; ngx_tcp_lua_shdict_ctx_t *ctx; ngx_tcp_lua_main_conf_t *lmcf; dd("init zone"); ctx = shm_zone->data; if (octx) { ctx->sh = octx->sh; ctx->shpool = octx->shpool; goto done; } ctx->shpool = (ngx_slab_pool_t *) shm_zone->shm.addr; if (shm_zone->shm.exists) { ctx->sh = ctx->shpool->data; goto done; } ctx->sh = ngx_slab_alloc(ctx->shpool, sizeof(ngx_tcp_lua_shdict_shctx_t)); if (ctx->sh == NULL) { return NGX_ERROR; } ctx->shpool->data = ctx->sh; ngx_rbtree_init(&ctx->sh->rbtree, &ctx->sh->sentinel, ngx_tcp_lua_shdict_rbtree_insert_value); ngx_queue_init(&ctx->sh->queue); len = sizeof(" in lua_shared_dict zone \"\"") + shm_zone->shm.name.len; ctx->shpool->log_ctx = ngx_slab_alloc(ctx->shpool, len); if (ctx->shpool->log_ctx == NULL) { return NGX_ERROR; } ngx_sprintf(ctx->shpool->log_ctx, " in lua_shared_dict zone \"%V\"%Z", &shm_zone->shm.name); #if defined(nginx_version) && nginx_version >= 1005013 ctx->shpool->log_nomem = 0; #endif done: dd("get lmcf"); lmcf = ctx->main_conf; dd("lmcf->lua: %p", lmcf->lua); lmcf->shm_zones_inited++; //to make sure init_by_lua always run after shm_zone if (lmcf->shm_zones_inited == lmcf->shm_zones->nelts && lmcf->init_handler) { if (lmcf->init_handler(ctx->log, lmcf, lmcf->lua) != NGX_OK) { /* an error happened */ return NGX_ERROR; } } return NGX_OK; }
static ngx_int_t ngx_tcp_cmd_process_init(ngx_cycle_t *cycle) { ngx_str_t cmdso_path = CMDSO_PATH_STR; ngx_uint_t i; ngx_tcp_cmdso_t *cmdsos; ngx_tcp_cycle_ctx_t *cycle_ctx; cmdso_mgr = ngx_pcalloc(cycle->pool, sizeof(ngx_tcp_cmdso_mgr_t)); if (cmdso_mgr == NULL) { goto failed; } cycle_ctx = ngx_pcalloc(cycle->pool, sizeof(ngx_tcp_cycle_ctx_t)); if (NULL == cycle_ctx) { goto failed; } cycle_ctx->conf_get_str = (ngx_tcp_conf_get_str_pt)ngx_tcp_cmd_conf_get_str; cycle_ctx->tcp_log_t.log = cycle->log; cycle_ctx->tcp_log_t.log_level = cycle->log->log_level; cycle_ctx->tcp_log_t.log_error=(ngx_tcp_log_error_pt)ngx_log_error_core; ngx_rbtree_init(&cmdso_mgr->pkg_handler_mgr.rbtree, &cmdso_mgr->pkg_handler_mgr.sentinel, ngx_rbtree_insert_value); if (NGX_OK != ngx_array_init(&cmdso_mgr->cmdsos, cycle->pool, 4, sizeof(ngx_tcp_cmdso_t))) { goto failed; } if (NGX_OK != ngx_tcp_cmd_pkg_handler_add(cycle, NGX_TCP_CMD_KEEPALIVE, NGX_TCP_CMD_KEEPALIVE, ngx_tcp_cmd_keepalive_handler)) { goto failed; } if (NGX_OK != ngx_tcp_cmd_pkg_handler_add(cycle, NGX_TCP_CMD_TRAN, NGX_TCP_CMD_TRAN, ngx_tcp_cmd_tran_handler)) { goto failed; } /* the cmdso_path->data will end with '\0' */ ngx_conf_full_name(cycle, &cmdso_path, 0); if (NGX_OK != ngx_tcp_cmd_load_cmdso(cycle, (const char *) cmdso_path.data)) { goto failed; } cmdsos = cmdso_mgr->cmdsos.elts; for (i=0; i < cmdso_mgr->cmdsos.nelts; ++i) { if (cmdsos[i].cmdso_load(cycle, ngx_tcp_cmd_pkg_handler_add, i, cycle_ctx) != NGX_OK) { goto failed; } } return NGX_OK; failed: return NGX_ERROR; }