merge r2972, r2994, r3133, r3142, r3143, r3174, r3175, r3176:

various perl fixes:

*) fix ngx_http_send_special() for subrequests handled by perl
*) allow perl "sub{..."
*) restore environ, this fixes segfault on reconfiguration failure when
   perl module creates new environment
*) optimize error handling
*) use ngx_conf_set_str_array_slot() for perl_require
*) allow several perl_modules
This commit is contained in:
Igor Sysoev 2009-10-26 16:22:24 +00:00
parent f10b4e07a5
commit 39625e5c46
4 changed files with 81 additions and 71 deletions

View File

@ -255,11 +255,13 @@ ngx_init_cycle(ngx_cycle_t *old_cycle)
#endif #endif
if (ngx_conf_param(&conf) != NGX_CONF_OK) { if (ngx_conf_param(&conf) != NGX_CONF_OK) {
environ = senv;
ngx_destroy_cycle_pools(&conf); ngx_destroy_cycle_pools(&conf);
return NULL; return NULL;
} }
if (ngx_conf_parse(&conf, &cycle->conf_file) != NGX_CONF_OK) { if (ngx_conf_parse(&conf, &cycle->conf_file) != NGX_CONF_OK) {
environ = senv;
ngx_destroy_cycle_pools(&conf); ngx_destroy_cycle_pools(&conf);
return NULL; return NULL;
} }
@ -280,6 +282,7 @@ ngx_init_cycle(ngx_cycle_t *old_cycle)
if (module->init_conf(cycle, cycle->conf_ctx[ngx_modules[i]->index]) if (module->init_conf(cycle, cycle->conf_ctx[ngx_modules[i]->index])
== NGX_CONF_ERROR) == NGX_CONF_ERROR)
{ {
environ = senv;
ngx_destroy_cycle_pools(&conf); ngx_destroy_cycle_pools(&conf);
return NULL; return NULL;
} }
@ -698,8 +701,8 @@ old_shm_zone_done:
if (ngx_process == NGX_PROCESS_MASTER || ngx_is_init_cycle(old_cycle)) { if (ngx_process == NGX_PROCESS_MASTER || ngx_is_init_cycle(old_cycle)) {
/* /*
* perl_destruct() frees environ if it is not the same as it was at * perl_destruct() frees environ, if it is not the same as it was at
* perl_construct() time. So we have saved an previous cycle * perl_construct() time, therefore we save the previous cycle
* environment before ngx_conf_parse() where it will be changed. * environment before ngx_conf_parse() where it will be changed.
*/ */

View File

@ -13,8 +13,8 @@
typedef struct { typedef struct {
PerlInterpreter *perl; PerlInterpreter *perl;
HV *nginx; HV *nginx;
ngx_str_t modules; ngx_array_t *modules;
ngx_array_t requires; ngx_array_t *requires;
} ngx_http_perl_main_conf_t; } ngx_http_perl_main_conf_t;
@ -57,8 +57,6 @@ static char *ngx_http_perl_init_main_conf(ngx_conf_t *cf, void *conf);
static void *ngx_http_perl_create_loc_conf(ngx_conf_t *cf); static void *ngx_http_perl_create_loc_conf(ngx_conf_t *cf);
static char *ngx_http_perl_merge_loc_conf(ngx_conf_t *cf, void *parent, static char *ngx_http_perl_merge_loc_conf(ngx_conf_t *cf, void *parent,
void *child); void *child);
static char *ngx_http_perl_require(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static char *ngx_http_perl(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); static char *ngx_http_perl(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char *ngx_http_perl_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); static char *ngx_http_perl_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
@ -74,16 +72,16 @@ static ngx_command_t ngx_http_perl_commands[] = {
{ ngx_string("perl_modules"), { ngx_string("perl_modules"),
NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot, ngx_conf_set_str_array_slot,
NGX_HTTP_MAIN_CONF_OFFSET, NGX_HTTP_MAIN_CONF_OFFSET,
offsetof(ngx_http_perl_main_conf_t, modules), offsetof(ngx_http_perl_main_conf_t, modules),
NULL }, NULL },
{ ngx_string("perl_require"), { ngx_string("perl_require"),
NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
ngx_http_perl_require, ngx_conf_set_str_array_slot,
NGX_HTTP_MAIN_CONF_OFFSET, NGX_HTTP_MAIN_CONF_OFFSET,
0, offsetof(ngx_http_perl_main_conf_t, requires),
NULL }, NULL },
{ ngx_string("perl"), { ngx_string("perl"),
@ -458,8 +456,10 @@ ngx_http_perl_ssi(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ssi_ctx,
static char * static char *
ngx_http_perl_init_interpreter(ngx_conf_t *cf, ngx_http_perl_main_conf_t *pmcf) ngx_http_perl_init_interpreter(ngx_conf_t *cf, ngx_http_perl_main_conf_t *pmcf)
{ {
ngx_str_t *m;
ngx_uint_t i;
#if (NGX_HAVE_PERL_MULTIPLICITY) #if (NGX_HAVE_PERL_MULTIPLICITY)
ngx_pool_cleanup_t *cln; ngx_pool_cleanup_t *cln;
cln = ngx_pool_cleanup_add(cf->pool, 0); cln = ngx_pool_cleanup_add(cf->pool, 0);
if (cln == NULL) { if (cln == NULL) {
@ -471,14 +471,29 @@ ngx_http_perl_init_interpreter(ngx_conf_t *cf, ngx_http_perl_main_conf_t *pmcf)
#endif #endif
#ifdef NGX_PERL_MODULES #ifdef NGX_PERL_MODULES
if (pmcf->modules.data == NULL) { if (pmcf->modules == NGX_CONF_UNSET_PTR) {
pmcf->modules.data = NGX_PERL_MODULES;
pmcf->modules = ngx_array_create(cf->pool, 1, sizeof(ngx_str_t));
if (pmcf->modules == NULL) {
return NGX_CONF_ERROR;
}
m = ngx_array_push(pmcf->modules);
if (m == NULL) {
return NGX_CONF_ERROR;
}
m->len = sizeof(NGX_PERL_MODULES) - 1;
m->data = NGX_PERL_MODULES;
} }
#endif #endif
if (pmcf->modules.data) { if (pmcf->modules != NGX_CONF_UNSET_PTR) {
if (ngx_conf_full_name(cf->cycle, &pmcf->modules, 0) != NGX_OK) { m = pmcf->modules->elts;
return NGX_CONF_ERROR; for (i = 0; i < pmcf->modules->nelts; i++) {
if (ngx_conf_full_name(cf->cycle, &m[i], 0) != NGX_OK) {
return NGX_CONF_ERROR;
}
} }
} }
@ -490,7 +505,7 @@ ngx_http_perl_init_interpreter(ngx_conf_t *cf, ngx_http_perl_main_conf_t *pmcf)
return NGX_CONF_ERROR; return NGX_CONF_ERROR;
} }
if (ngx_http_perl_run_requires(aTHX_ &pmcf->requires, cf->log) if (ngx_http_perl_run_requires(aTHX_ pmcf->requires, cf->log)
!= NGX_OK) != NGX_OK)
{ {
return NGX_CONF_ERROR; return NGX_CONF_ERROR;
@ -538,7 +553,9 @@ ngx_http_perl_create_interpreter(ngx_conf_t *cf,
int n; int n;
STRLEN len; STRLEN len;
SV *sv; SV *sv;
char *ver, *embedding[6]; char *ver, **embedding;
ngx_str_t *m;
ngx_uint_t i;
PerlInterpreter *perl; PerlInterpreter *perl;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, "create perl interpreter"); ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, "create perl interpreter");
@ -564,15 +581,21 @@ ngx_http_perl_create_interpreter(ngx_conf_t *cf,
PL_exit_flags |= PERL_EXIT_DESTRUCT_END; PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
#endif #endif
n = (pmcf->modules != NGX_CONF_UNSET_PTR) ? pmcf->modules->nelts * 2 : 0;
embedding = ngx_palloc(cf->pool, (4 + n) * sizeof(char *));
if (embedding == NULL) {
goto fail;
}
embedding[0] = ""; embedding[0] = "";
if (pmcf->modules.data) { if (n++) {
embedding[1] = "-I"; m = pmcf->modules->elts;
embedding[2] = (char *) pmcf->modules.data; for (i = 0; i < pmcf->modules->nelts; i++) {
n = 3; embedding[2 * i + 1] = "-I";
embedding[2 * i + 2] = (char *) m[i].data;
} else { }
n = 1;
} }
embedding[n++] = "-Mnginx"; embedding[n++] = "-Mnginx";
@ -596,7 +619,7 @@ ngx_http_perl_create_interpreter(ngx_conf_t *cf,
goto fail; goto fail;
} }
if (ngx_http_perl_run_requires(aTHX_ &pmcf->requires, cf->log) != NGX_OK) { if (ngx_http_perl_run_requires(aTHX_ pmcf->requires, cf->log) != NGX_OK) {
goto fail; goto fail;
} }
@ -617,26 +640,28 @@ fail:
static ngx_int_t static ngx_int_t
ngx_http_perl_run_requires(pTHX_ ngx_array_t *requires, ngx_log_t *log) ngx_http_perl_run_requires(pTHX_ ngx_array_t *requires, ngx_log_t *log)
{ {
char **script; u_char *err;
STRLEN len; STRLEN len;
ngx_str_t err; ngx_str_t *script;
ngx_uint_t i; ngx_uint_t i;
if (requires == NGX_CONF_UNSET_PTR) {
return NGX_OK;
}
script = requires->elts; script = requires->elts;
for (i = 0; i < requires->nelts; i++) { for (i = 0; i < requires->nelts; i++) {
require_pv(script[i]); require_pv((char *) script[i].data);
if (SvTRUE(ERRSV)) { if (SvTRUE(ERRSV)) {
err.data = (u_char *) SvPV(ERRSV, len); err = (u_char *) SvPV(ERRSV, len);
for (len--; err.data[len] == LF || err.data[len] == CR; len--) { while (--len && (err[len] == CR || err[len] == LF)) { /* void */ }
/* void */
}
err.len = len + 1;
ngx_log_error(NGX_LOG_EMERG, log, 0, ngx_log_error(NGX_LOG_EMERG, log, 0,
"require_pv(\"%s\") failed: \"%V\"", script[i], &err); "require_pv(\"%s\") failed: \"%*s\"",
script[i].data, len + 1, err);
return NGX_ERROR; return NGX_ERROR;
} }
@ -653,8 +678,8 @@ ngx_http_perl_call_handler(pTHX_ ngx_http_request_t *r, HV *nginx, SV *sub,
SV *sv; SV *sv;
int n, status; int n, status;
char *line; char *line;
u_char *err;
STRLEN len, n_a; STRLEN len, n_a;
ngx_str_t err;
ngx_uint_t i; ngx_uint_t i;
ngx_connection_t *c; ngx_connection_t *c;
@ -724,14 +749,11 @@ ngx_http_perl_call_handler(pTHX_ ngx_http_request_t *r, HV *nginx, SV *sub,
if (SvTRUE(ERRSV)) { if (SvTRUE(ERRSV)) {
err.data = (u_char *) SvPV(ERRSV, len); err = (u_char *) SvPV(ERRSV, len);
for (len--; err.data[len] == LF || err.data[len] == CR; len--) { while (--len && (err[len] == CR || err[len] == LF)) { /* void */ }
/* void */
}
err.len = len + 1;
ngx_log_error(NGX_LOG_ERR, c->log, 0, ngx_log_error(NGX_LOG_ERR, c->log, 0,
"call_sv(\"%V\") failed: \"%V\"", handler, &err); "call_sv(\"%V\") failed: \"%*s\"", handler, len + 1, err);
if (rv) { if (rv) {
return NGX_ERROR; return NGX_ERROR;
@ -765,7 +787,10 @@ ngx_http_perl_eval_anon_sub(pTHX_ ngx_str_t *handler, SV **sv)
} }
} }
if (ngx_strncmp(p, "sub ", 4) == 0 || ngx_strncmp(p, "use ", 4) == 0) { if (ngx_strncmp(p, "sub ", 4) == 0
|| ngx_strncmp(p, "sub{", 4) == 0
|| ngx_strncmp(p, "use ", 4) == 0)
{
*sv = eval_pv((char *) p, FALSE); *sv = eval_pv((char *) p, FALSE);
/* eval_pv() does not set ERRSV on failure */ /* eval_pv() does not set ERRSV on failure */
@ -787,11 +812,8 @@ ngx_http_perl_create_main_conf(ngx_conf_t *cf)
return NULL; return NULL;
} }
if (ngx_array_init(&pmcf->requires, cf->pool, 1, sizeof(u_char *)) pmcf->modules = NGX_CONF_UNSET_PTR;
!= NGX_OK) pmcf->requires = NGX_CONF_UNSET_PTR;
{
return NULL;
}
return pmcf; return pmcf;
} }
@ -897,28 +919,6 @@ ngx_http_perl_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
} }
static char *
ngx_http_perl_require(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_perl_main_conf_t *pmcf = conf;
u_char **p;
ngx_str_t *value;
value = cf->args->elts;
p = ngx_array_push(&pmcf->requires);
if (p == NULL) {
return NGX_CONF_ERROR;
}
*p = value[1].data;
return NGX_CONF_OK;
}
static char * static char *
ngx_http_perl(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) ngx_http_perl(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{ {

View File

@ -2706,7 +2706,14 @@ ngx_http_send_special(ngx_http_request_t *r, ngx_uint_t flags)
} }
if (flags & NGX_HTTP_LAST) { if (flags & NGX_HTTP_LAST) {
b->last_buf = 1;
if (r == r->main && !r->post_action) {
b->last_buf = 1;
} else {
b->sync = 1;
b->last_in_chain = 1;
}
} }
if (flags & NGX_HTTP_FLUSH) { if (flags & NGX_HTTP_FLUSH) {

View File

@ -2898,7 +2898,7 @@ ngx_http_upstream_finalize_request(ngx_http_request_t *r,
r->connection->log->action = "sending to client"; r->connection->log->action = "sending to client";
if (rc == 0 && r == r->main && !r->post_action) { if (rc == 0) {
rc = ngx_http_send_special(r, NGX_HTTP_LAST); rc = ngx_http_send_special(r, NGX_HTTP_LAST);
} }