Skip to content

Commit 34e6952

Browse files
committed
synchronized with meta-lua-module #da938b5.
1 parent 2c67417 commit 34e6952

9 files changed

+122
-49
lines changed

src/ngx_stream_lua_common.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@
6363
#endif
6464

6565

66+
#if !defined(LUAJIT_VERSION_NUM) || (LUAJIT_VERSION_NUM < 20000)
67+
# error unsupported LuaJIT version
68+
#endif
69+
70+
6671
#if (!defined OPENSSL_NO_OCSP && defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB)
6772
# define NGX_STREAM_LUA_USE_OCSP 1
6873
#endif
@@ -178,8 +183,6 @@ struct ngx_stream_lua_main_conf_s {
178183
ngx_cycle_t *cycle;
179184
ngx_pool_t *pool;
180185

181-
ngx_flag_t load_resty_core;
182-
183186
ngx_int_t max_pending_timers;
184187
ngx_int_t pending_timers;
185188

src/ngx_stream_lua_directive.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,19 @@ ngx_stream_lua_code_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
177177
}
178178

179179

180+
char *
181+
ngx_stream_lua_load_resty_core(ngx_conf_t *cf, ngx_command_t *cmd,
182+
void *conf)
183+
{
184+
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
185+
"lua_load_resty_core is deprecated (the lua-resty-core "
186+
"library is required since "
187+
"ngx_stream_lua v0.0.8)");
188+
189+
return NGX_CONF_OK;
190+
}
191+
192+
180193
char *
181194
ngx_stream_lua_package_cpath(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
182195
{

src/ngx_stream_lua_directive.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ char *ngx_stream_lua_init_worker_by_lua_block(ngx_conf_t *cf,
4545
char *ngx_stream_lua_init_worker_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
4646
void *conf);
4747
char *ngx_stream_lua_code_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
48+
char *ngx_stream_lua_load_resty_core(ngx_conf_t *cf, ngx_command_t *cmd,
49+
void *conf);
4850

4951

5052
char *

src/ngx_stream_lua_initworkerby.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ ngx_stream_lua_init_worker(ngx_cycle_t *cycle)
9696

9797
conf_ctx = (ngx_stream_conf_ctx_t *)
9898
cycle->conf_ctx[ngx_stream_module.index];
99-
stream_ctx.main_conf = conf_ctx->main_conf;
10099

101100
top_clcf = conf_ctx->srv_conf[ngx_stream_core_module.ctx_index];
102101
top_llcf = conf_ctx->srv_conf[ngx_stream_lua_module.ctx_index];
@@ -209,6 +208,12 @@ ngx_stream_lua_init_worker(ngx_cycle_t *cycle)
209208
return NGX_ERROR;
210209
}
211210

211+
stream_ctx.main_conf = ngx_pcalloc(conf.pool,
212+
sizeof(void *) * ngx_stream_max_module);
213+
if (stream_ctx.main_conf == NULL) {
214+
return NGX_ERROR;
215+
}
216+
212217
#if defined(nginx_version) && nginx_version >= 1009011
213218
modules = cycle->modules;
214219
#else
@@ -222,6 +227,21 @@ ngx_stream_lua_init_worker(ngx_cycle_t *cycle)
222227

223228
module = modules[i]->ctx;
224229

230+
if (module->create_main_conf) {
231+
cur = module->create_main_conf(&conf);
232+
if (cur == NULL) {
233+
return NGX_ERROR;
234+
}
235+
236+
if (ngx_modules[i]->index == ngx_stream_lua_module.index) {
237+
ngx_memcpy(cur,
238+
conf_ctx->main_conf[ngx_stream_lua_module.ctx_index],
239+
sizeof(ngx_stream_lua_main_conf_t));
240+
}
241+
242+
stream_ctx.main_conf[modules[i]->ctx_index] = cur;
243+
}
244+
225245
if (module->create_srv_conf) {
226246
cur = module->create_srv_conf(&conf);
227247
if (cur == NULL) {

src/ngx_stream_lua_log.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ log_wrapper(ngx_log_t *log, const char *ident, ngx_uint_t level,
203203
*p++ = ':';
204204

205205
p = ngx_snprintf(p, NGX_INT_T_LEN, "%d",
206-
ar.currentline ? ar.currentline : ar.linedefined);
206+
ar.currentline > 0 ? ar.currentline : ar.linedefined);
207207

208208
*p++ = ':'; *p++ = ' ';
209209

src/ngx_stream_lua_module.c

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ static ngx_command_t ngx_stream_lua_cmds[] = {
8282

8383
{ ngx_string("lua_load_resty_core"),
8484
NGX_STREAM_MAIN_CONF|NGX_CONF_FLAG,
85-
ngx_conf_set_flag_slot,
85+
ngx_stream_lua_load_resty_core,
8686
NGX_STREAM_MAIN_CONF_OFFSET,
87-
offsetof(ngx_stream_lua_main_conf_t, load_resty_core),
87+
0,
8888
NULL },
8989

9090
{ ngx_string("lua_max_running_timers"),
@@ -554,14 +554,33 @@ ngx_stream_lua_init(ngx_conf_t *cf)
554554
#endif
555555

556556

557-
lmcf->lua = ngx_stream_lua_init_vm(NULL, cf->cycle, cf->pool, lmcf,
558-
cf->log, NULL);
559-
if (lmcf->lua == NULL) {
560-
ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
561-
"failed to initialize Lua VM");
562-
return NGX_ERROR;
557+
rc = ngx_stream_lua_init_vm(&lmcf->lua, NULL, cf->cycle, cf->pool,
558+
lmcf, cf->log, NULL);
559+
if (rc != NGX_OK) {
560+
if (rc == NGX_DECLINED) {
561+
ngx_stream_lua_assert(lmcf->lua != NULL);
562+
563+
ngx_conf_log_error(NGX_LOG_ALERT, cf, 0,
564+
"failed to load the 'resty.core' module "
565+
"(https://github.com/openresty/lua-resty"
566+
"-core); ensure you are using an OpenResty "
567+
"release from https://openresty.org/en/"
568+
"download.html (reason: %s)",
569+
lua_tostring(lmcf->lua, -1));
570+
571+
} else {
572+
/* rc == NGX_ERROR */
573+
ngx_conf_log_error(NGX_LOG_ALERT, cf, 0,
574+
"failed to initialize Lua VM");
575+
}
576+
577+
return NGX_ERROR;
563578
}
564579

580+
/* rc == NGX_OK */
581+
582+
ngx_stream_lua_assert(lmcf->lua != NULL);
583+
565584
if (!lmcf->requires_shm && lmcf->init_handler) {
566585
saved_cycle = ngx_cycle;
567586
ngx_cycle = cf->cycle;
@@ -649,7 +668,6 @@ ngx_stream_lua_create_main_conf(ngx_conf_t *cf)
649668
*/
650669

651670
lmcf->pool = cf->pool;
652-
lmcf->load_resty_core = NGX_CONF_UNSET;
653671
lmcf->max_pending_timers = NGX_CONF_UNSET;
654672
lmcf->max_running_timers = NGX_CONF_UNSET;
655673
#if (NGX_PCRE)
@@ -681,10 +699,6 @@ ngx_stream_lua_init_main_conf(ngx_conf_t *cf, void *conf)
681699
{
682700
ngx_stream_lua_main_conf_t *lmcf = conf;
683701

684-
if (lmcf->load_resty_core == NGX_CONF_UNSET) {
685-
lmcf->load_resty_core = 1;
686-
}
687-
688702
#if (NGX_PCRE)
689703
if (lmcf->regex_cache_max_entries == NGX_CONF_UNSET) {
690704
lmcf->regex_cache_max_entries = 1024;

src/ngx_stream_lua_semaphore.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ ngx_stream_lua_alloc_sema(void)
9393
lmcf = ngx_stream_cycle_get_module_main_conf(ngx_cycle,
9494
ngx_stream_lua_module);
9595

96+
ngx_stream_lua_assert(lmcf != NULL);
97+
9698
mm = lmcf->sema_mm;
9799

98100
if (!ngx_queue_empty(&mm->free_queue)) {
@@ -179,6 +181,8 @@ ngx_stream_lua_sema_mm_cleanup(void *data)
179181
sem = ngx_queue_data(q, ngx_stream_lua_sema_t, chain);
180182
block = sem->block;
181183

184+
ngx_stream_lua_assert(block != NULL);
185+
182186
if (block->used == 0) {
183187
iter = (ngx_stream_lua_sema_t *) (block + 1);
184188

src/ngx_stream_lua_util.c

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@ ngx_stream_lua_new_state(lua_State *parent_vm, ngx_cycle_t *cycle,
251251

252252
lua_pushliteral(L, LUA_DEFAULT_PATH ";"); /* package default */
253253
lua_getfield(L, -2, "path"); /* package default old */
254-
old_path = lua_tolstring(L, -1, &old_path_len);
255254
lua_concat(L, 2); /* package new */
256255
lua_setfield(L, -2, "path"); /* package */
257256
#endif
@@ -3267,9 +3266,10 @@ ngx_stream_lua_close_fake_connection(ngx_connection_t *c)
32673266
}
32683267

32693268

3270-
lua_State *
3271-
ngx_stream_lua_init_vm(lua_State *parent_vm, ngx_cycle_t *cycle,
3272-
ngx_pool_t *pool, ngx_stream_lua_main_conf_t *lmcf, ngx_log_t *log,
3269+
ngx_int_t
3270+
ngx_stream_lua_init_vm(lua_State **new_vm, lua_State *parent_vm,
3271+
ngx_cycle_t *cycle, ngx_pool_t *pool,
3272+
ngx_stream_lua_main_conf_t *lmcf, ngx_log_t *log,
32733273
ngx_pool_cleanup_t **pcln)
32743274
{
32753275
int rc;
@@ -3282,13 +3282,13 @@ ngx_stream_lua_init_vm(lua_State *parent_vm, ngx_cycle_t *cycle,
32823282

32833283
cln = ngx_pool_cleanup_add(pool, 0);
32843284
if (cln == NULL) {
3285-
return NULL;
3285+
return NGX_ERROR;
32863286
}
32873287

32883288
/* create new Lua VM instance */
32893289
L = ngx_stream_lua_new_state(parent_vm, cycle, lmcf, log);
32903290
if (L == NULL) {
3291-
return NULL;
3291+
return NGX_ERROR;
32923292
}
32933293

32943294
ngx_log_debug1(NGX_LOG_DEBUG_STREAM, log, 0, "lua initialize the "
@@ -3299,7 +3299,7 @@ ngx_stream_lua_init_vm(lua_State *parent_vm, ngx_cycle_t *cycle,
32993299

33003300
state = ngx_alloc(sizeof(ngx_stream_lua_vm_state_t), log);
33013301
if (state == NULL) {
3302-
return NULL;
3302+
return NGX_ERROR;
33033303
}
33043304
state->vm = L;
33053305
state->count = 1;
@@ -3332,7 +3332,8 @@ ngx_stream_lua_init_vm(lua_State *parent_vm, ngx_cycle_t *cycle,
33323332

33333333
for (i = 0; i < lmcf->preload_hooks->nelts; i++) {
33343334

3335-
ngx_stream_lua_probe_register_preload_package(L, hook[i].package);
3335+
ngx_stream_lua_probe_register_preload_package(L,
3336+
hook[i].package);
33363337

33373338
lua_pushcfunction(L, hook[i].loader);
33383339
lua_setfield(L, -2, (char *) hook[i].package);
@@ -3341,22 +3342,17 @@ ngx_stream_lua_init_vm(lua_State *parent_vm, ngx_cycle_t *cycle,
33413342
lua_pop(L, 2);
33423343
}
33433344

3344-
if (lmcf->load_resty_core) {
3345-
lua_getglobal(L, "require");
3346-
lua_pushstring(L, "resty.core");
3345+
*new_vm = L;
33473346

3348-
rc = lua_pcall(L, 1, 1, 0);
3349-
if (rc != 0) {
3350-
ngx_log_error(NGX_LOG_ERR, log, 0,
3351-
"lua_load_resty_core failed to load the resty.core "
3352-
"module from https://github.com/openresty/lua-resty"
3353-
"-core; ensure you are using an OpenResty release "
3354-
"from https://openresty.org/en/download.html "
3355-
"(rc: %i, reason: %s)", rc, lua_tostring(L, -1));
3356-
}
3357-
}
3347+
lua_getglobal(L, "require");
3348+
lua_pushstring(L, "resty.core");
33583349

3359-
return L;
3350+
rc = lua_pcall(L, 1, 1, 0);
3351+
if (rc != 0) {
3352+
return NGX_DECLINED;
3353+
}
3354+
3355+
return NGX_OK;
33603356
}
33613357

33623358

src/ngx_stream_lua_util.h

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,9 @@ ngx_stream_lua_ffi_check_context(ngx_stream_lua_ctx_t *ctx,
132132
SSL_get_ex_data(ssl_conn, ngx_stream_lua_ssl_ctx_index)
133133

134134

135-
lua_State *ngx_stream_lua_init_vm(lua_State *parent_vm, ngx_cycle_t *cycle,
136-
ngx_pool_t *pool, ngx_stream_lua_main_conf_t *lmcf, ngx_log_t *log,
135+
ngx_int_t ngx_stream_lua_init_vm(lua_State **new_vm, lua_State *parent_vm,
136+
ngx_cycle_t *cycle, ngx_pool_t *pool,
137+
ngx_stream_lua_main_conf_t *lmcf, ngx_log_t *log,
137138
ngx_pool_cleanup_t **pcln);
138139

139140
lua_State *ngx_stream_lua_new_thread(ngx_stream_lua_request_t *r, lua_State *l,
@@ -272,7 +273,8 @@ ngx_stream_lua_init_ctx(ngx_stream_lua_request_t *r, ngx_stream_lua_ctx_t *ctx)
272273
static ngx_inline ngx_stream_lua_ctx_t *
273274
ngx_stream_lua_create_ctx(ngx_stream_session_t *r)
274275
{
275-
lua_State *L;
276+
ngx_int_t rc;
277+
lua_State *L = NULL;
276278
ngx_stream_lua_ctx_t *ctx;
277279
ngx_pool_cleanup_t *cln;
278280
ngx_stream_lua_loc_conf_t *llcf;
@@ -311,8 +313,8 @@ ngx_stream_lua_create_ctx(ngx_stream_session_t *r)
311313
* the correct semantics.
312314
*/
313315

314-
L = ngx_stream_lua_init_vm(lmcf->lua, lmcf->cycle, sreq->pool, lmcf,
315-
r->connection->log, &cln);
316+
rc = ngx_stream_lua_init_vm(&L, lmcf->lua, lmcf->cycle, sreq->pool,
317+
lmcf, r->connection->log, &cln);
316318

317319
while (cln->next != NULL) {
318320
cln = cln->next;
@@ -324,11 +326,30 @@ ngx_stream_lua_create_ctx(ngx_stream_session_t *r)
324326
sreq->pool->cleanup = cln->next;
325327
cln->next = NULL;
326328

327-
if (L == NULL) {
328-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
329-
"failed to initialize Lua VM");
330-
return NULL;
331-
}
329+
if (rc != NGX_OK) {
330+
if (rc == NGX_DECLINED) {
331+
ngx_stream_lua_assert(L != NULL);
332+
333+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
334+
"failed to load the 'resty.core' module "
335+
"(https://github.com/openresty/lua-resty"
336+
"-core); ensure you are using an OpenResty "
337+
"release from https://openresty.org/en/"
338+
"download.html (reason: %s)",
339+
lua_tostring(L, -1));
340+
341+
} else {
342+
/* rc == NGX_ERROR */
343+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
344+
"failed to initialize Lua VM");
345+
}
346+
347+
return NULL;
348+
}
349+
350+
/* rc == NGX_OK */
351+
352+
ngx_stream_lua_assert(L != NULL);
332353

333354
if (lmcf->init_handler) {
334355
if (lmcf->init_handler(r->connection->log, lmcf, L) != NGX_OK) {

0 commit comments

Comments
 (0)