static void ngx_http_tfs_check_exit_worker(ngx_cycle_t *cycle) { ngx_int_t i; ngx_http_tfs_main_conf_t *tmcf; ngx_http_tfs_tair_instance_t *dup_instance; tmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_tfs_module); if (tmcf == NULL) { return; } /* destroy duplicate server */ for (i = 0; i < NGX_HTTP_TFS_MAX_CLUSTER_COUNT; i++) { dup_instance = &tmcf->dup_instances[i]; if (dup_instance->server != NULL) { ngx_http_etair_destory_server(dup_instance->server, (ngx_cycle_t *) ngx_cycle); } } /* destroy remote block cache server */ if (tmcf->remote_block_cache_instance.server != NULL) { ngx_http_etair_destory_server(tmcf->remote_block_cache_instance.server, (ngx_cycle_t *) ngx_cycle); } }
static ngx_int_t ngx_http_tfs_check_init_worker(ngx_cycle_t *cycle) { ngx_uint_t i; ngx_http_tfs_upstream_t **tup; ngx_http_tfs_main_conf_t *tmcf; /* rc keepalive */ tmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_tfs_module); if (tmcf == NULL) { return NGX_ERROR; } tup = tmcf->upstreams.elts; for (i = 0; i < tmcf->upstreams.nelts; i++) { if (!tup[i]->enable_rcs || !tup[i]->lock_file.len || !tup[i]->used) { return NGX_OK; } if (ngx_http_tfs_add_rcs_timers(cycle, tup[i]->timer_data) == NGX_ERROR) { return NGX_ERROR; } } return NGX_OK; }
ngx_int_t ngx_http_psgi_perl_init_worker(ngx_cycle_t *cycle) { ngx_http_psgi_main_conf_t *psgimcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_psgi_module); ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cycle->log, 0, "Init Perl interpreter in worker %d", ngx_pid); if (psgimcf) { dTHXa(psgimcf->perl); PERL_SET_CONTEXT(psgimcf->perl); /* FIXME: It looks very wrong. * Has new worker it's own Perl instance? * I think I should perl_clone() or something like that * Also $0 (script path) should be set somewhere. * I don't think it's right place for it. It should be done somewhere in local conf init stuff * Or, if many handlers share single Perl interpreter - before each handler call * * TODO * Test PID and related stuff * Test what happens if user try to change * Test what happens if user does 'fork' inside PSGI app */ sv_setiv(GvSV(gv_fetchpv("$$", TRUE, SVt_PV)), (I32) ngx_pid); } else { ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, "PSGI panic: no main configuration supplied for init worker %d", ngx_pid); return NGX_ERROR; } return NGX_OK; }
static void ngx_http_alog_process_exit(ngx_cycle_t *cycle) { ngx_http_alog_main_conf_t *lmcf; size_t write_bytes; alog_log_thread_ctx.stop = 1; alog_mq_msg_t mq_msg; mq_msg.msg.data = NULL; mq_msg.msg.len = 0; /* wakeup log thread */ alog_mq_push(alog_log_thread_ctx.mq, &mq_msg); /* wait thread stop */ pthread_join(alog_log_thread_ctx.tid, NULL); lmcf = (ngx_http_alog_main_conf_t *) ngx_http_cycle_get_module_main_conf(cycle, ngx_http_alog_module); if (lmcf == NULL) { return; } if (lmcf->buf_size != 0 && lmcf->log_buf.pos != lmcf->log_buf.start) { write_bytes = lmcf->log_buf.pos - lmcf->log_buf.start; write(alog_log_thread_ctx.log_file->fd, lmcf->log_buf.start, write_bytes); lmcf->log_buf.pos = lmcf->log_buf.start; } alog_mq_release(alog_log_thread_ctx.mq); ngx_memzero(&alog_log_thread_ctx, sizeof(alog_log_thread_ctx_t)); alog_log_thread_ctx.stop = 1; }
static void ngx_http_upstream_dynamic_server_resolve(ngx_event_t *ev) { ngx_http_upstream_dynamic_server_main_conf_t *udsmcf = ngx_http_cycle_get_module_main_conf(ngx_cycle, ngx_http_upstream_dynamic_servers_module); ngx_http_upstream_dynamic_server_conf_t *dynamic_server; ngx_resolver_ctx_t *ctx; dynamic_server = ev->data; ctx = ngx_resolve_start(udsmcf->resolver, NULL); if (ctx == NULL) { ngx_log_error(NGX_LOG_ALERT, ev->log, 0, "upstream-dynamic-servers: resolver start error for '%V'", &dynamic_server->host); return; } if (ctx == NGX_NO_RESOLVER) { ngx_log_error(NGX_LOG_ALERT, ev->log, 0, "upstream-dynamic-servers: no resolver defined to resolve '%V'", &dynamic_server->host); return; } ctx->name = dynamic_server->host; ctx->handler = ngx_http_upstream_dynamic_server_resolve_handler; ctx->data = dynamic_server; ctx->timeout = udsmcf->resolver_timeout; ngx_log_debug(NGX_LOG_DEBUG_CORE, ev->log, 0, "upstream-dynamic-servers: Resolving '%V'", &ctx->name); if (ngx_resolve_name(ctx) != NGX_OK) { ngx_log_error(NGX_LOG_ALERT, ev->log, 0, "upstream-dynamic-servers: ngx_resolve_name failed for '%V'", &ctx->name); ngx_add_timer(&dynamic_server->timer, 1000); } }
void ngx_http_kafka_exit_worker(ngx_cycle_t *cycle) { ngx_http_kafka_main_conf_t *main_conf; main_conf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_kafka_module); // TODO: rd_kafka_topic_destroy(each loc conf rkt ); rd_kafka_destroy(main_conf->rk); }
static ngx_int_t ngx_http_gettoken_init_connections(ngx_cycle_t *cycle) { ngx_http_gettoken_connection_t *c; ngx_http_gettoken_main_conf_t *halmcf; ngx_http_gettoken_server_t *server; ngx_pool_cleanup_t *cleanup; ngx_connection_t *dummy_conn; ngx_uint_t i, j; //int option; halmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_gettoken_module); if (halmcf == NULL || halmcf->servers == NULL) { return NGX_OK; } for (i = 0; i < halmcf->servers->nelts; i++) { server = &((ngx_http_gettoken_server_t *) halmcf->servers->elts)[i]; ngx_queue_init(&server->free_connections); ngx_queue_init(&server->waiting_requests); if (server->connections <= 1) { server->connections = 1; } for (j = 0; j < server->connections; j++) { c = ngx_pcalloc(cycle->pool, sizeof(ngx_http_gettoken_connection_t)); cleanup = ngx_pool_cleanup_add(cycle->pool, 0); dummy_conn = ngx_pcalloc(cycle->pool, sizeof(ngx_connection_t)); if (c == NULL || cleanup == NULL || dummy_conn == NULL) { return NGX_ERROR; } cleanup->handler = &ngx_http_gettoken_connection_cleanup; cleanup->data = c; c->log = cycle->log; c->server = server; c->state = STATE_DISCONNECTED; /* Various debug logging around timer management assume that the field 'data' in ngx_event_t is a pointer to ngx_connection_t, therefore we have a dummy such structure around so that it does not crash etc. */ dummy_conn->data = c; c->reconnect_event.log = c->log; c->reconnect_event.data = dummy_conn; c->reconnect_event.handler = ngx_http_gettoken_reconnect_handler; ngx_http_gettoken_connect(c); } } return NGX_OK; }
ngx_int_t ngx_http_stats_enabled(ngx_cycle_t *cycle) { ngx_http_statistics_conf_t *smcf; smcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_statistics_module); return smcf->enabled; }
static void ngx_http_upstream_dynamic_servers_exit_process(ngx_cycle_t *cycle) { ngx_http_upstream_dynamic_server_main_conf_t *udsmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_upstream_dynamic_servers_module); ngx_http_upstream_dynamic_server_conf_t *dynamic_server = udsmcf->dynamic_servers.elts; ngx_uint_t i; for (i = 0; i < udsmcf->dynamic_servers.nelts; i++) { if (dynamic_server[i].pool) { ngx_destroy_pool(dynamic_server[i].pool); dynamic_server[i].pool = NULL; } } }
void ngx_http_accounting_worker_process_exit(ngx_cycle_t *cycle) { ngx_http_accounting_main_conf_t *amcf; amcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_accounting_module); if (!amcf->enable) { return; } worker_process_alarm_handler(NULL); }
static ngx_int_t ngx_http_perl_init_worker(ngx_cycle_t *cycle) { ngx_http_perl_main_conf_t *pmcf; pmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_perl_module); if (pmcf) { dTHXa(pmcf->perl); PERL_SET_CONTEXT(pmcf->perl); /* set worker's $$ */ sv_setiv(GvSV(gv_fetchpv("$", TRUE, SVt_PV)), (I32) ngx_pid); } return NGX_OK; }
static ngx_http_upstream_main_conf_t * ngx_http_lua_upstream_get_upstream_main_conf(lua_State *L) { ngx_http_request_t *r; r = ngx_http_lua_get_request(L); if (r == NULL) { return ngx_http_cycle_get_module_main_conf(ngx_cycle, ngx_http_upstream_module); } return ngx_http_get_module_main_conf(r, ngx_http_upstream_module); }
void ngx_http_accounting_worker_process_exit(ngx_cycle_t *cycle) { ngx_http_accounting_main_conf_t *amcf; amcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_accounting_module); if (!amcf->enable) { return; } worker_process_alarm_handler(NULL); syslog(LOG_INFO, "pid:%i|Process:exit", ngx_getpid()); }
void ngx_http_psgi_perl_exit(ngx_cycle_t *cycle) { ngx_http_psgi_main_conf_t *psgimcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_psgi_module); ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cycle->log, 0, "psgi perl term"); (void) perl_destruct(psgimcf->perl); perl_free(psgimcf->perl); PERL_SYS_TERM(); }
static void ngx_themis_timer_handler(ngx_event_t *ev) { /* TODO: channel event handler */ void **configs; ngx_int_t hash, rc; ngx_uint_t i, j; ngx_cycle_t *cycle; ngx_hash_key_t *key, *keys; ngx_themis_module_t *m; ngx_http_themis_main_conf_t *tmcf; cycle = ev->data; tmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_themis_module); keys = tmcf->configs.elts; for (i = 0; i < tmcf->configs.nelts; i++) { key = &keys[i]; for (j = 0; ngx_modules[j]; j++) { if (ngx_modules[j]->type != NGX_THEMIS_MODULE) { continue; } m = ngx_modules[j]->ctx; if (!m->update_config) { continue; } hash = ngx_hash_key_lc(key->key.data, key->key.len); configs = ngx_hash_find(&tmcf->configs_hash, hash, key->key.data, key->key.len); if (!configs[j]) { continue; } rc = m->update_config(cycle, (void *)((uintptr_t) configs[j] & (uintptr_t) ~0x1)); if (rc != NGX_OK) { return; } configs[j] = (void *)((uintptr_t) configs[j] | (uintptr_t) 0x1); } } ngx_add_timer(ev, 10000); }
static void ngx_http_timer_exit_worker(ngx_cycle_t *cycle) { ngx_http_timer_conf_t *tcf; if (NGX_PROCESS_WORKER != ngx_process) { return; } tcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_timer_module); if (tcf->event.timer_set) { ngx_del_timer(&tcf->event); } }
static ngx_int_t ngx_http_tfs_module_init(ngx_cycle_t *cycle) { ngx_http_tfs_main_conf_t *tmcf; tmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_tfs_module); if (tmcf == NULL) { return NGX_ERROR; } if (tmcf->enable_rcs == NGX_HTTP_TFS_NO) { return NGX_OK; } return ngx_http_tfs_timers_init(cycle, tmcf->lock_file.data); }
static void ngx_http_perl_exit(ngx_cycle_t *cycle) { ngx_http_perl_main_conf_t *pmcf; pmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_perl_module); { dTHXa(pmcf->perl); PERL_SET_CONTEXT(pmcf->perl); PERL_SYS_TERM(); } }
static ngx_int_t ngx_http_static_init(ngx_cycle_t *cycle) { ngx_http_handler_pt *h; ngx_http_core_main_conf_t *cmcf; cmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_core_module); h = ngx_push_array(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers); if (h == NULL) { return NGX_ERROR; } *h = ngx_http_static_handler; return NGX_OK; }
static ngx_int_t ngx_http_photo_thrift_init_connection(ngx_cycle_t *cycle) { ngx_photo_thrift_main_conf_t *photo_thrift_main_conf = ngx_http_cycle_get_module_main_conf(cycle, ngx_photo_thrift_module); photo_thrift_conf_t **clcf; signal(SIGPIPE, SIG_IGN); clcf = photo_thrift_main_conf->loc_confs.elts; char content_server_address[20]; char meta_server_address[20]; ngx_int_t content_server_port, meta_server_port; content_server_port = ngx_atoi(clcf[0]->content_thrift_port.data, clcf[0]->content_thrift_port.len); meta_server_port = ngx_atoi(clcf[0]->meta_thrift_port.data, clcf[0]->meta_thrift_port.len); strncpy(content_server_address, (const char *) clcf[0]->content_thrift_server.data, clcf[0]->content_thrift_server.len); strncpy(meta_server_address, (const char *) clcf[0]->meta_thrift_server.data, clcf[0]->meta_thrift_server.len); init_thrift_connection_pool(30, meta_server_address, (int) meta_server_port, content_server_address, (int) content_server_port); return NGX_OK; }
static ngx_int_t ngx_http_timer_init_worker(ngx_cycle_t *cycle) { ngx_http_timer_conf_t *tcf; if (NGX_PROCESS_WORKER != ngx_process) { return NGX_OK; } tcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_timer_module); if (tcf->enabled) { ngx_add_timer(&tcf->event, tcf->period); } return NGX_OK; }
void * ngx_themis_get_masked_module_conf(ngx_str_t *name, ngx_int_t index) { void **configs; ngx_int_t key; ngx_http_themis_main_conf_t *tmcf; tmcf = ngx_http_cycle_get_module_main_conf(ngx_cycle, ngx_http_themis_module); key = ngx_hash_key_lc(name->data, name->len); configs = ngx_hash_find(&tmcf->configs_hash, key, name->data, name->len); if (configs == NULL) { return NULL; } return configs[index]; }
static ngx_int_t ngx_http_poller_init_process(ngx_cycle_t *cycle) { ngx_http_poller_conf_t *pcf; ngx_http_poller_t *poller; ngx_log_t *log; ngx_event_t *ev; ngx_uint_t i; pcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_poller_module); if (pcf == NULL) { ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, "[poller] no module context"); return NGX_ERROR; } poller = pcf->pollers.elts; for (i = 0; i < pcf->pollers.nelts; ++i) { log = &poller[i].log; log->action = "initializing"; log->file = cycle->new_log.file; /* only install the poll event if the endpoint is defined. */ if (poller[i].endpoint.data != NULL) { ev = &poller[i].poll_event; ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "[poller] %V: activated (endpoint %V)", &poller[i].name, &poller[i].endpoint); ev->data = &poller[i]; ev->log = log; ev->handler = ngx_http_poller_event; ngx_add_timer(ev, 0); } else { ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "[poller] %V: inactive (no endpoint)", &poller[i].name); } } return NGX_OK; }
static char *ngx_http_upstream_dynamic_servers_merge_conf(ngx_conf_t *cf, void *parent, void *child) { // If any dynamic servers are present, verify that a "resolver" is setup as // the http level. ngx_http_upstream_dynamic_server_main_conf_t *udsmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_upstream_dynamic_servers_module); if (udsmcf->dynamic_servers.nelts > 0) { ngx_http_core_loc_conf_t *core_loc_conf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); #if nginx_version >= 1009011 if (core_loc_conf->resolver == NULL || core_loc_conf->resolver->connections.nelts == 0) { #else if (core_loc_conf->resolver == NULL || core_loc_conf->resolver->udp_connections.nelts == 0) { #endif ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "resolver must be defined at the 'http' level of the config"); return NGX_CONF_ERROR; } udsmcf->conf_ctx = cf->ctx; udsmcf->resolver = core_loc_conf->resolver; ngx_conf_merge_msec_value(udsmcf->resolver_timeout, core_loc_conf->resolver_timeout, 30000); } return NGX_CONF_OK; } static ngx_int_t ngx_http_upstream_dynamic_servers_init_process(ngx_cycle_t *cycle) { ngx_http_upstream_dynamic_server_main_conf_t *udsmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_upstream_dynamic_servers_module); ngx_http_upstream_dynamic_server_conf_t *dynamic_server = udsmcf->dynamic_servers.elts; ngx_uint_t i; ngx_event_t *timer; ngx_uint_t refresh_in; for (i = 0; i < udsmcf->dynamic_servers.nelts; i++) { timer = &dynamic_server[i].timer; timer->handler = ngx_http_upstream_dynamic_server_resolve; timer->log = cycle->log; timer->data = &dynamic_server[i]; refresh_in = ngx_random() % 1000; ngx_log_debug(NGX_LOG_DEBUG_CORE, cycle->log, 0, "upstream-dynamic-servers: Initial DNS refresh of '%V' in %ims", &dynamic_server[i].host, refresh_in); ngx_add_timer(timer, refresh_in); } return NGX_OK; }
ngx_int_t ngx_http_kafka_init_worker(ngx_cycle_t *cycle) { size_t n; ngx_http_kafka_main_conf_t *main_conf; main_conf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_kafka_module); main_conf->rkc = rd_kafka_conf_new(); rd_kafka_conf_set_dr_cb(main_conf->rkc, kafka_callback_handler); main_conf->rk = rd_kafka_new(RD_KAFKA_PRODUCER, main_conf->rkc, NULL, 0); for (n = 0; n != main_conf->nbrokers; ++n) { ngx_str_helper(&main_conf->brokers[n], ngx_str_push); rd_kafka_brokers_add(main_conf->rk, (const char *)main_conf->brokers[n].data); ngx_str_helper(&main_conf->brokers[n], ngx_str_pop); } return 0; }
static ngx_int_t ngx_http_tfs_check_init_worker(ngx_cycle_t *cycle) { ngx_http_tfs_main_conf_t *tmcf; /* rc keepalive */ tmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_tfs_module); if (tmcf == NULL) { return NGX_ERROR; } if (tmcf->enable_rcs == NGX_HTTP_TFS_NO) { return NGX_OK; } if (tmcf->rcs_kp_enable) { return ngx_http_tfs_add_rcs_timers(cycle, tmcf); } return NGX_OK; }
void ngx_http_statistics_server_del(ngx_cycle_t *cycle, ngx_str_t *name) { ngx_http_statistics_ctx_t *ctx; ngx_http_statistics_conf_t *smcf; ngx_http_statistics_server_t *server; uint32_t hash; smcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_statistics_module); if (smcf->enabled == 0) { return; } ctx = statistics_shm_zone->data; ngx_shmtx_lock(&ctx->shpool->mutex); hash = ngx_crc32_short(name->data, name->len); server = (ngx_http_statistics_server_t *) ngx_http_statistics_lookup( &ctx->sh->server_tree, name, hash); if (server != NULL) { /* found a server node */ ngx_log_error(NGX_LOG_DEBUG, cycle->log, 0, "server->ref: %i", server->ref); server->ref--; if (server->ref == 0) { ngx_rbtree_delete(&ctx->sh->server_tree, &server->node); ngx_queue_remove(&server->queue); ngx_slab_free_locked(ctx->shpool, server->name.data); ngx_slab_free_locked(ctx->shpool, server); } } ngx_shmtx_unlock(&ctx->shpool->mutex); }
static ngx_int_t ngx_http_tfs_module_init(ngx_cycle_t *cycle) { ngx_uint_t i; ngx_http_tfs_upstream_t **tup; ngx_http_tfs_main_conf_t *tmcf; ngx_http_tfs_timers_data_t *data; tmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_tfs_module); if (tmcf == NULL) { return NGX_ERROR; } tup = tmcf->upstreams.elts; for (i = 0; i < tmcf->upstreams.nelts; i++) { if (!tup[i]->enable_rcs || !tup[i]->lock_file.len || !tup[i]->used) { return NGX_OK; } data = ngx_pcalloc(cycle->pool, sizeof(ngx_http_tfs_timers_data_t)); if (data == NULL) { return NGX_ERROR; } data->main_conf = tmcf; data->upstream = tup[i]; data->lock = ngx_http_tfs_timers_init(cycle, tup[i]->lock_file.data); if (data->lock == NULL) { return NGX_ERROR; } tup[i]->timer_data = data; } return NGX_OK; }
ngx_int_t ngx_http_accounting_worker_process_init(ngx_cycle_t *cycle) { ngx_int_t rc; ngx_time_t *time; ngx_http_accounting_main_conf_t *amcf; amcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_accounting_module); if (!amcf->enable) { return NGX_OK; } init_http_status_code_map(); time = ngx_timeofday(); ngx_http_accounting_old_time = time->sec; ngx_http_accounting_new_time = time->sec; openlog((char *)ngx_http_accounting_title, LOG_NDELAY, LOG_SYSLOG); rc = ngx_http_accounting_hash_init(&stats_hash, NGX_HTTP_ACCOUNTING_NR_BUCKETS, cycle->pool); if (rc != NGX_OK) { return rc; } ngx_memzero(&write_out_ev, sizeof(ngx_event_t)); write_out_ev.data = NULL; write_out_ev.log = cycle->log; write_out_ev.handler = worker_process_alarm_handler; worker_process_interval = amcf->interval; srand(ngx_getpid()); ngx_add_timer(&write_out_ev, worker_process_interval*(1000-rand()%200)); return NGX_OK; }
static ngx_int_t ngx_http_ssl_init_process(ngx_cycle_t *cycle) { ngx_uint_t i; ngx_http_ssl_srv_conf_t *sscf; ngx_http_core_srv_conf_t **cscfp; ngx_http_core_main_conf_t *cmcf; cmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_core_module); cscfp = cmcf->servers.elts; for (i = 0; i < cmcf->servers.nelts; i++) { sscf = cscfp[i]->ctx->srv_conf[ngx_http_ssl_module.ctx_index]; if (sscf->enable) { cscfp[i]->recv = ngx_ssl_recv; cscfp[i]->send_chain = ngx_ssl_send_chain; } } return NGX_OK; }