static char * ngx_http_lua_init_vm(ngx_conf_t *cf, ngx_http_lua_main_conf_t *lmcf) { ngx_pool_cleanup_t *cln; ngx_http_lua_preload_hook_t *hook; lua_State *L; ngx_uint_t i; ngx_http_lua_content_length_hash = ngx_http_lua_hash_literal("content-length"); ngx_http_lua_location_hash = ngx_http_lua_hash_literal("location"); /* add new cleanup handler to config mem pool */ cln = ngx_pool_cleanup_add(cf->pool, 0); if (cln == NULL) { return NGX_CONF_ERROR; } /* create new Lua VM instance */ lmcf->lua = ngx_http_lua_new_state(cf, lmcf); if (lmcf->lua == NULL) { return NGX_CONF_ERROR; } /* register cleanup handler for Lua VM */ cln->handler = ngx_http_lua_cleanup_vm; cln->data = lmcf->lua; if (lmcf->preload_hooks) { /* register the 3rd-party module's preload hooks */ L = lmcf->lua; lua_getglobal(L, "package"); lua_getfield(L, -1, "preload"); hook = lmcf->preload_hooks->elts; for (i = 0; i < lmcf->preload_hooks->nelts; i++) { ngx_http_lua_probe_register_preload_package(L, hook[i].package); lua_pushcfunction(L, hook[i].loader); lua_setfield(L, -2, (char *) hook[i].package); } lua_pop(L, 2); } return NGX_CONF_OK; }
static ngx_int_t ngx_http_lua_init(ngx_conf_t *cf) { int multi_http_blocks; ngx_int_t rc; ngx_array_t *arr; ngx_http_handler_pt *h; volatile ngx_cycle_t *saved_cycle; ngx_http_core_main_conf_t *cmcf; ngx_http_lua_main_conf_t *lmcf; #ifndef NGX_LUA_NO_FFI_API ngx_pool_cleanup_t *cln; #endif lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_lua_module); if (ngx_http_lua_prev_cycle != ngx_cycle) { ngx_http_lua_prev_cycle = ngx_cycle; multi_http_blocks = 0; } else { multi_http_blocks = 1; } if (multi_http_blocks || lmcf->requires_capture_filter) { rc = ngx_http_lua_capture_filter_init(cf); if (rc != NGX_OK) { return rc; } } if (lmcf->postponed_to_rewrite_phase_end == NGX_CONF_UNSET) { lmcf->postponed_to_rewrite_phase_end = 0; } if (lmcf->postponed_to_access_phase_end == NGX_CONF_UNSET) { lmcf->postponed_to_access_phase_end = 0; } cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); if (lmcf->requires_rewrite) { h = ngx_array_push(&cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers); if (h == NULL) { return NGX_ERROR; } *h = ngx_http_lua_rewrite_handler; } if (lmcf->requires_access) { h = ngx_array_push(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers); if (h == NULL) { return NGX_ERROR; } *h = ngx_http_lua_access_handler; } dd("requires log: %d", (int) lmcf->requires_log); if (lmcf->requires_log) { arr = &cmcf->phases[NGX_HTTP_LOG_PHASE].handlers; h = ngx_array_push(arr); if (h == NULL) { return NGX_ERROR; } if (arr->nelts > 1) { h = arr->elts; ngx_memmove(&h[1], h, (arr->nelts - 1) * sizeof(ngx_http_handler_pt)); } *h = ngx_http_lua_log_handler; } if (multi_http_blocks || lmcf->requires_header_filter) { rc = ngx_http_lua_header_filter_init(); if (rc != NGX_OK) { return rc; } } if (multi_http_blocks || lmcf->requires_body_filter) { rc = ngx_http_lua_body_filter_init(); if (rc != NGX_OK) { return rc; } } #ifndef NGX_LUA_NO_FFI_API /* add the cleanup of semaphores after the lua_close */ cln = ngx_pool_cleanup_add(cf->pool, 0); if (cln == NULL) { return NGX_ERROR; } cln->data = lmcf; cln->handler = ngx_http_lua_cleanup_semaphore_mm; #endif if (lmcf->lua == NULL) { dd("initializing lua vm"); ngx_http_lua_content_length_hash = ngx_http_lua_hash_literal("content-length"); ngx_http_lua_location_hash = ngx_http_lua_hash_literal("location"); lmcf->lua = ngx_http_lua_init_vm(NULL, cf->cycle, cf->pool, lmcf, cf->log, NULL); if (lmcf->lua == NULL) { ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "failed to initialize Lua VM"); return NGX_ERROR; } if (!lmcf->requires_shm && lmcf->init_handler) { saved_cycle = ngx_cycle; ngx_cycle = cf->cycle; rc = lmcf->init_handler(cf->log, lmcf, lmcf->lua); ngx_cycle = saved_cycle; if (rc != NGX_OK) { /* an error happened */ return NGX_ERROR; } } dd("Lua VM initialized!"); } return NGX_OK; }