Mercurial > hg > nginx
annotate src/http/modules/perl/ngx_http_perl_module.c @ 1165:efb7d84db340
fix segfault when
*) perl was built without multiplicity
*) when configuration was invalid
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Wed, 18 Apr 2007 11:28:11 +0000 |
parents | 8288459f15c9 |
children | aa653367028e |
rev | line source |
---|---|
599 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 */ | |
5 | |
6 | |
7 #include <ngx_config.h> | |
8 #include <ngx_core.h> | |
9 #include <ngx_http.h> | |
10 #include <ngx_http_perl_module.h> | |
11 | |
12 | |
13 typedef struct { | |
14 PerlInterpreter *perl; | |
873
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
15 HV *nginx; |
599 | 16 ngx_str_t modules; |
17 ngx_array_t requires; | |
18 } ngx_http_perl_main_conf_t; | |
19 | |
20 | |
21 typedef struct { | |
22 SV *sub; | |
23 ngx_str_t handler; | |
24 } ngx_http_perl_loc_conf_t; | |
25 | |
26 | |
27 typedef struct { | |
28 SV *sub; | |
29 ngx_str_t handler; | |
30 } ngx_http_perl_variable_t; | |
31 | |
32 | |
653 | 33 typedef struct { |
34 SV *sv; | |
35 PerlInterpreter *perl; | |
36 } ngx_http_perl_cleanup_t; | |
37 | |
38 | |
617 | 39 #if (NGX_HTTP_SSI) |
599 | 40 static ngx_int_t ngx_http_perl_ssi(ngx_http_request_t *r, |
41 ngx_http_ssi_ctx_t *ssi_ctx, ngx_str_t **params); | |
617 | 42 #endif |
43 | |
911 | 44 static void ngx_http_perl_sleep_handler(ngx_http_request_t *r); |
599 | 45 static char *ngx_http_perl_init_interpreter(ngx_conf_t *cf, |
46 ngx_http_perl_main_conf_t *pmcf); | |
1069 | 47 static PerlInterpreter *ngx_http_perl_create_interpreter(ngx_conf_t *cf, |
48 ngx_http_perl_main_conf_t *pmcf); | |
653 | 49 static ngx_int_t ngx_http_perl_run_requires(pTHX_ ngx_array_t *requires, |
649 | 50 ngx_log_t *log); |
599 | 51 static ngx_int_t ngx_http_perl_call_handler(pTHX_ ngx_http_request_t *r, |
873
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
52 HV *nginx, SV *sub, ngx_str_t **args, ngx_str_t *handler, ngx_str_t *rv); |
599 | 53 static void ngx_http_perl_eval_anon_sub(pTHX_ ngx_str_t *handler, SV **sv); |
54 | |
55 static ngx_int_t ngx_http_perl_preconfiguration(ngx_conf_t *cf); | |
56 static void *ngx_http_perl_create_main_conf(ngx_conf_t *cf); | |
57 static char *ngx_http_perl_init_main_conf(ngx_conf_t *cf, void *conf); | |
58 static void *ngx_http_perl_create_loc_conf(ngx_conf_t *cf); | |
59 static char *ngx_http_perl_merge_loc_conf(ngx_conf_t *cf, void *parent, | |
60 void *child); | |
61 static char *ngx_http_perl_require(ngx_conf_t *cf, ngx_command_t *cmd, | |
62 void *conf); | |
63 static char *ngx_http_perl(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); | |
64 static char *ngx_http_perl_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); | |
872
1c4a5b3f9110
Axe several perl interpreter instances: they may be useful in currently
Igor Sysoev <igor@sysoev.ru>
parents:
871
diff
changeset
|
65 |
1c4a5b3f9110
Axe several perl interpreter instances: they may be useful in currently
Igor Sysoev <igor@sysoev.ru>
parents:
871
diff
changeset
|
66 #if (NGX_HAVE_PERL_MULTIPLICITY) |
599 | 67 static void ngx_http_perl_cleanup_perl(void *data); |
649 | 68 #endif |
599 | 69 |
70 | |
71 static ngx_command_t ngx_http_perl_commands[] = { | |
72 | |
73 { ngx_string("perl_modules"), | |
74 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, | |
75 ngx_conf_set_str_slot, | |
76 NGX_HTTP_MAIN_CONF_OFFSET, | |
77 offsetof(ngx_http_perl_main_conf_t, modules), | |
78 NULL }, | |
79 | |
80 { ngx_string("perl_require"), | |
81 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, | |
82 ngx_http_perl_require, | |
83 NGX_HTTP_MAIN_CONF_OFFSET, | |
84 0, | |
85 NULL }, | |
86 | |
87 { ngx_string("perl"), | |
922
a5b9cdfe3e19
allow "perl" inside "limit_except"
Igor Sysoev <igor@sysoev.ru>
parents:
913
diff
changeset
|
88 NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_TAKE1, |
599 | 89 ngx_http_perl, |
90 NGX_HTTP_LOC_CONF_OFFSET, | |
91 0, | |
92 NULL }, | |
93 | |
94 { ngx_string("perl_set"), | |
95 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE2, | |
96 ngx_http_perl_set, | |
97 NGX_HTTP_LOC_CONF_OFFSET, | |
98 0, | |
99 NULL }, | |
100 | |
101 ngx_null_command | |
102 }; | |
103 | |
104 | |
105 static ngx_http_module_t ngx_http_perl_module_ctx = { | |
106 ngx_http_perl_preconfiguration, /* preconfiguration */ | |
107 NULL, /* postconfiguration */ | |
108 | |
109 ngx_http_perl_create_main_conf, /* create main configuration */ | |
110 ngx_http_perl_init_main_conf, /* init main configuration */ | |
111 | |
112 NULL, /* create server configuration */ | |
113 NULL, /* merge server configuration */ | |
114 | |
115 ngx_http_perl_create_loc_conf, /* create location configuration */ | |
116 ngx_http_perl_merge_loc_conf /* merge location configuration */ | |
117 }; | |
118 | |
119 | |
120 ngx_module_t ngx_http_perl_module = { | |
121 NGX_MODULE_V1, | |
122 &ngx_http_perl_module_ctx, /* module context */ | |
123 ngx_http_perl_commands, /* module directives */ | |
124 NGX_HTTP_MODULE, /* module type */ | |
125 NULL, /* init master */ | |
126 NULL, /* init module */ | |
127 NULL, /* init process */ | |
128 NULL, /* init thread */ | |
129 NULL, /* exit thread */ | |
130 NULL, /* exit process */ | |
131 NULL, /* exit master */ | |
132 NGX_MODULE_V1_PADDING | |
133 }; | |
134 | |
135 | |
617 | 136 #if (NGX_HTTP_SSI) |
137 | |
599 | 138 #define NGX_HTTP_PERL_SSI_SUB 0 |
139 #define NGX_HTTP_PERL_SSI_ARG 1 | |
140 | |
141 | |
142 static ngx_http_ssi_param_t ngx_http_perl_ssi_params[] = { | |
143 { ngx_string("sub"), NGX_HTTP_PERL_SSI_SUB, 1, 0 }, | |
144 { ngx_string("arg"), NGX_HTTP_PERL_SSI_ARG, 0, 1 }, | |
145 { ngx_null_string, 0, 0, 0 } | |
146 }; | |
147 | |
148 static ngx_http_ssi_command_t ngx_http_perl_ssi_command = { | |
667 | 149 ngx_string("perl"), ngx_http_perl_ssi, ngx_http_perl_ssi_params, 0, 0, 1 |
599 | 150 }; |
151 | |
617 | 152 #endif |
153 | |
599 | 154 |
681 | 155 static ngx_str_t ngx_null_name = ngx_null_string; |
156 | |
157 | |
633 | 158 static HV *nginx_stash; |
159 | |
599 | 160 static void |
161 ngx_http_perl_xs_init(pTHX) | |
162 { | |
163 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, __FILE__); | |
633 | 164 |
165 nginx_stash = gv_stashpv("nginx", TRUE); | |
599 | 166 } |
167 | |
168 | |
169 static ngx_int_t | |
170 ngx_http_perl_handler(ngx_http_request_t *r) | |
171 { | |
629 | 172 /* TODO: Win32 */ |
173 if (r->zero_in_uri) { | |
174 return NGX_HTTP_NOT_FOUND; | |
175 } | |
176 | |
681 | 177 ngx_http_perl_handle_request(r); |
178 | |
179 return NGX_DONE; | |
629 | 180 } |
181 | |
182 | |
681 | 183 void |
629 | 184 ngx_http_perl_handle_request(ngx_http_request_t *r) |
185 { | |
681 | 186 SV *sub; |
599 | 187 ngx_int_t rc; |
681 | 188 ngx_str_t uri, args, *handler; |
599 | 189 ngx_http_perl_ctx_t *ctx; |
190 ngx_http_perl_loc_conf_t *plcf; | |
191 ngx_http_perl_main_conf_t *pmcf; | |
192 | |
193 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "perl handler"); | |
194 | |
195 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); | |
196 | |
197 if (ctx == NULL) { | |
198 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_perl_ctx_t)); | |
199 if (ctx == NULL) { | |
681 | 200 ngx_http_finalize_request(r, NGX_ERROR); |
1025 | 201 return; |
599 | 202 } |
203 | |
204 ngx_http_set_ctx(r, ctx, ngx_http_perl_module); | |
205 } | |
206 | |
207 pmcf = ngx_http_get_module_main_conf(r, ngx_http_perl_module); | |
208 | |
209 { | |
210 | |
872
1c4a5b3f9110
Axe several perl interpreter instances: they may be useful in currently
Igor Sysoev <igor@sysoev.ru>
parents:
871
diff
changeset
|
211 dTHXa(pmcf->perl); |
873
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
212 PERL_SET_CONTEXT(pmcf->perl); |
599 | 213 |
681 | 214 if (ctx->next == NULL) { |
215 plcf = ngx_http_get_module_loc_conf(r, ngx_http_perl_module); | |
216 sub = plcf->sub; | |
217 handler = &plcf->handler; | |
599 | 218 |
681 | 219 } else { |
220 sub = ctx->next; | |
221 handler = &ngx_null_name; | |
222 ctx->next = NULL; | |
223 } | |
224 | |
873
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
225 rc = ngx_http_perl_call_handler(aTHX_ r, pmcf->nginx, sub, NULL, handler, |
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
226 NULL); |
599 | 227 |
228 } | |
229 | |
230 if (rc > 600) { | |
231 rc = NGX_OK; | |
232 } | |
233 | |
234 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
235 "perl handler done: %i", rc); | |
236 | |
237 if (ctx->redirect_uri.len) { | |
238 uri = ctx->redirect_uri; | |
239 args = ctx->redirect_args; | |
629 | 240 |
241 } else { | |
242 uri.len = 0; | |
599 | 243 } |
244 | |
633 | 245 ctx->filename.data = NULL; |
599 | 246 ctx->redirect_uri.len = 0; |
247 | |
911 | 248 if (ctx->sleep) { |
249 ngx_add_timer(r->connection->write, (ngx_msec_t) ctx->sleep); | |
250 r->write_event_handler = ngx_http_perl_sleep_handler; | |
251 ctx->sleep = 0; | |
252 } | |
253 | |
681 | 254 if (ctx->done || ctx->next) { |
255 return; | |
256 } | |
257 | |
599 | 258 if (uri.len) { |
629 | 259 ngx_http_internal_redirect(r, &uri, &args); |
260 return; | |
599 | 261 } |
262 | |
263 if (rc == NGX_OK || rc == NGX_HTTP_OK) { | |
629 | 264 ngx_http_send_special(r, NGX_HTTP_LAST); |
681 | 265 ctx->done = 1; |
599 | 266 } |
267 | |
629 | 268 ngx_http_finalize_request(r, rc); |
599 | 269 } |
270 | |
271 | |
911 | 272 static void |
273 ngx_http_perl_sleep_handler(ngx_http_request_t *r) | |
274 { | |
913
90ce4d0e3241
fix ngx_http_perl_sleep_handler()
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
275 ngx_event_t *wev; |
90ce4d0e3241
fix ngx_http_perl_sleep_handler()
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
276 |
911 | 277 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
278 "perl sleep handler"); | |
279 | |
913
90ce4d0e3241
fix ngx_http_perl_sleep_handler()
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
280 wev = r->connection->write; |
90ce4d0e3241
fix ngx_http_perl_sleep_handler()
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
281 |
90ce4d0e3241
fix ngx_http_perl_sleep_handler()
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
282 if (wev->timedout) { |
90ce4d0e3241
fix ngx_http_perl_sleep_handler()
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
283 wev->timedout = 0; |
90ce4d0e3241
fix ngx_http_perl_sleep_handler()
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
284 ngx_http_perl_handle_request(r); |
90ce4d0e3241
fix ngx_http_perl_sleep_handler()
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
285 return; |
90ce4d0e3241
fix ngx_http_perl_sleep_handler()
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
286 } |
90ce4d0e3241
fix ngx_http_perl_sleep_handler()
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
287 |
90ce4d0e3241
fix ngx_http_perl_sleep_handler()
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
288 if (ngx_handle_write_event(wev, 0) == NGX_ERROR) { |
90ce4d0e3241
fix ngx_http_perl_sleep_handler()
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
289 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
90ce4d0e3241
fix ngx_http_perl_sleep_handler()
Igor Sysoev <igor@sysoev.ru>
parents:
911
diff
changeset
|
290 } |
911 | 291 } |
292 | |
293 | |
599 | 294 static ngx_int_t |
295 ngx_http_perl_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, | |
296 uintptr_t data) | |
297 { | |
298 ngx_http_perl_variable_t *pv = (ngx_http_perl_variable_t *) data; | |
299 | |
300 ngx_int_t rc; | |
301 ngx_str_t value; | |
302 ngx_http_perl_ctx_t *ctx; | |
303 ngx_http_perl_main_conf_t *pmcf; | |
304 | |
305 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
306 "perl variable handler"); | |
307 | |
308 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); | |
309 | |
310 if (ctx == NULL) { | |
311 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_perl_ctx_t)); | |
312 if (ctx == NULL) { | |
313 return NGX_ERROR; | |
314 } | |
315 | |
316 ngx_http_set_ctx(r, ctx, ngx_http_perl_module); | |
872
1c4a5b3f9110
Axe several perl interpreter instances: they may be useful in currently
Igor Sysoev <igor@sysoev.ru>
parents:
871
diff
changeset
|
317 } |
599 | 318 |
872
1c4a5b3f9110
Axe several perl interpreter instances: they may be useful in currently
Igor Sysoev <igor@sysoev.ru>
parents:
871
diff
changeset
|
319 pmcf = ngx_http_get_module_main_conf(r, ngx_http_perl_module); |
599 | 320 |
321 value.data = NULL; | |
322 | |
323 { | |
324 | |
872
1c4a5b3f9110
Axe several perl interpreter instances: they may be useful in currently
Igor Sysoev <igor@sysoev.ru>
parents:
871
diff
changeset
|
325 dTHXa(pmcf->perl); |
873
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
326 PERL_SET_CONTEXT(pmcf->perl); |
599 | 327 |
873
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
328 rc = ngx_http_perl_call_handler(aTHX_ r, pmcf->nginx, pv->sub, NULL, |
599 | 329 &pv->handler, &value); |
330 | |
331 } | |
332 | |
333 if (value.data) { | |
334 v->len = value.len; | |
335 v->valid = 1; | |
336 v->no_cachable = 0; | |
337 v->not_found = 0; | |
338 v->data = value.data; | |
339 | |
340 } else { | |
341 v->not_found = 1; | |
342 } | |
343 | |
633 | 344 ctx->filename.data = NULL; |
599 | 345 ctx->redirect_uri.len = 0; |
346 | |
871 | 347 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
348 "perl variable done"); | |
349 | |
599 | 350 return rc; |
351 } | |
352 | |
353 | |
617 | 354 #if (NGX_HTTP_SSI) |
355 | |
599 | 356 static ngx_int_t |
357 ngx_http_perl_ssi(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ssi_ctx, | |
358 ngx_str_t **params) | |
359 { | |
360 SV *sv; | |
361 ngx_int_t rc; | |
362 ngx_str_t *handler; | |
363 ngx_http_perl_ctx_t *ctx; | |
364 ngx_http_perl_main_conf_t *pmcf; | |
365 | |
366 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
367 "perl ssi handler"); | |
368 | |
369 ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); | |
370 | |
371 if (ctx == NULL) { | |
372 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_perl_ctx_t)); | |
373 if (ctx == NULL) { | |
374 return NGX_ERROR; | |
375 } | |
376 | |
377 ngx_http_set_ctx(r, ctx, ngx_http_perl_module); | |
378 } | |
379 | |
872
1c4a5b3f9110
Axe several perl interpreter instances: they may be useful in currently
Igor Sysoev <igor@sysoev.ru>
parents:
871
diff
changeset
|
380 pmcf = ngx_http_get_module_main_conf(r, ngx_http_perl_module); |
599 | 381 |
382 ctx->ssi = ssi_ctx; | |
383 | |
384 handler = params[NGX_HTTP_PERL_SSI_SUB]; | |
385 handler->data[handler->len] = '\0'; | |
386 | |
387 { | |
388 | |
872
1c4a5b3f9110
Axe several perl interpreter instances: they may be useful in currently
Igor Sysoev <igor@sysoev.ru>
parents:
871
diff
changeset
|
389 dTHXa(pmcf->perl); |
873
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
390 PERL_SET_CONTEXT(pmcf->perl); |
599 | 391 |
651 | 392 #if 0 |
393 | |
394 /* the code is disabled to force the precompiled perl code using only */ | |
395 | |
396 ngx_http_perl_eval_anon_sub(aTHX_ handler, &sv); | |
397 | |
398 if (sv == &PL_sv_undef) { | |
399 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
400 "eval_pv(\"%V\") failed", handler); | |
401 return NGX_ERROR; | |
402 } | |
403 | |
404 if (sv == NULL) { | |
405 sv = newSVpvn((char *) handler->data, handler->len); | |
406 } | |
407 | |
408 #endif | |
409 | |
599 | 410 sv = newSVpvn((char *) handler->data, handler->len); |
411 | |
873
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
412 rc = ngx_http_perl_call_handler(aTHX_ r, pmcf->nginx, sv, |
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
413 ¶ms[NGX_HTTP_PERL_SSI_ARG], |
599 | 414 handler, NULL); |
415 | |
416 SvREFCNT_dec(sv); | |
417 | |
418 } | |
419 | |
633 | 420 ctx->filename.data = NULL; |
599 | 421 ctx->redirect_uri.len = 0; |
422 ctx->ssi = NULL; | |
423 | |
871 | 424 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "perl ssi done"); |
425 | |
599 | 426 return rc; |
427 } | |
428 | |
617 | 429 #endif |
430 | |
599 | 431 |
432 static char * | |
433 ngx_http_perl_init_interpreter(ngx_conf_t *cf, ngx_http_perl_main_conf_t *pmcf) | |
434 { | |
872
1c4a5b3f9110
Axe several perl interpreter instances: they may be useful in currently
Igor Sysoev <igor@sysoev.ru>
parents:
871
diff
changeset
|
435 #if (NGX_HAVE_PERL_MULTIPLICITY) |
653 | 436 ngx_pool_cleanup_t *cln; |
599 | 437 |
438 cln = ngx_pool_cleanup_add(cf->pool, 0); | |
439 if (cln == NULL) { | |
440 return NGX_CONF_ERROR; | |
441 } | |
442 | |
649 | 443 #else |
444 static PerlInterpreter *perl; | |
445 #endif | |
446 | |
599 | 447 #ifdef NGX_PERL_MODULES |
448 if (pmcf->modules.data == NULL) { | |
449 pmcf->modules.data = NGX_PERL_MODULES; | |
450 } | |
451 #endif | |
452 | |
639 | 453 if (pmcf->modules.data) { |
454 if (ngx_conf_full_name(cf->cycle, &pmcf->modules) != NGX_OK) { | |
455 return NGX_CONF_ERROR; | |
456 } | |
629 | 457 } |
458 | |
872
1c4a5b3f9110
Axe several perl interpreter instances: they may be useful in currently
Igor Sysoev <igor@sysoev.ru>
parents:
871
diff
changeset
|
459 #if !(NGX_HAVE_PERL_MULTIPLICITY) |
649 | 460 |
461 if (perl) { | |
1165 | 462 |
463 if (ngx_set_environment(cf->cycle, NULL) == NULL) { | |
464 return NGX_CONF_ERROR; | |
465 } | |
466 | |
653 | 467 if (ngx_http_perl_run_requires(aTHX_ &pmcf->requires, cf->log) |
468 != NGX_OK) | |
469 { | |
649 | 470 return NGX_CONF_ERROR; |
471 } | |
472 | |
473 pmcf->perl = perl; | |
1163
8288459f15c9
set nginx_stash after reconfiguration in single interpreter perl
Igor Sysoev <igor@sysoev.ru>
parents:
1069
diff
changeset
|
474 pmcf->nginx = nginx_stash; |
649 | 475 |
476 return NGX_CONF_OK; | |
477 } | |
478 | |
479 #endif | |
480 | |
599 | 481 PERL_SYS_INIT(&ngx_argc, &ngx_argv); |
482 | |
1069 | 483 pmcf->perl = ngx_http_perl_create_interpreter(cf, pmcf); |
599 | 484 |
485 if (pmcf->perl == NULL) { | |
486 PERL_SYS_TERM(); | |
487 return NGX_CONF_ERROR; | |
488 } | |
489 | |
873
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
490 pmcf->nginx = nginx_stash; |
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
491 |
872
1c4a5b3f9110
Axe several perl interpreter instances: they may be useful in currently
Igor Sysoev <igor@sysoev.ru>
parents:
871
diff
changeset
|
492 #if (NGX_HAVE_PERL_MULTIPLICITY) |
649 | 493 |
599 | 494 cln->handler = ngx_http_perl_cleanup_perl; |
495 cln->data = pmcf->perl; | |
496 | |
649 | 497 #else |
498 | |
499 perl = pmcf->perl; | |
500 | |
501 #endif | |
502 | |
599 | 503 return NGX_CONF_OK; |
504 } | |
505 | |
506 | |
507 static PerlInterpreter * | |
1069 | 508 ngx_http_perl_create_interpreter(ngx_conf_t *cf, |
509 ngx_http_perl_main_conf_t *pmcf) | |
599 | 510 { |
511 int n; | |
775 | 512 STRLEN len; |
513 SV *sv; | |
514 char *ver, *embedding[6]; | |
599 | 515 PerlInterpreter *perl; |
516 | |
1069 | 517 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, "create perl interpreter"); |
518 | |
519 if (ngx_set_environment(cf->cycle, NULL) == NULL) { | |
520 return NULL; | |
521 } | |
599 | 522 |
523 perl = perl_alloc(); | |
524 if (perl == NULL) { | |
1069 | 525 ngx_log_error(NGX_LOG_ALERT, cf->log, 0, "perl_alloc() failed"); |
599 | 526 return NULL; |
527 } | |
528 | |
529 { | |
530 | |
531 dTHXa(perl); | |
873
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
532 PERL_SET_CONTEXT(perl); |
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
533 |
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
534 perl_construct(perl); |
599 | 535 |
536 #ifdef PERL_EXIT_DESTRUCT_END | |
537 PL_exit_flags |= PERL_EXIT_DESTRUCT_END; | |
538 #endif | |
539 | |
540 embedding[0] = ""; | |
541 | |
542 if (pmcf->modules.data) { | |
543 embedding[1] = "-I"; | |
544 embedding[2] = (char *) pmcf->modules.data; | |
545 n = 3; | |
546 | |
547 } else { | |
548 n = 1; | |
549 } | |
550 | |
551 embedding[n++] = "-Mnginx"; | |
552 embedding[n++] = "-e"; | |
553 embedding[n++] = "0"; | |
554 | |
555 n = perl_parse(perl, ngx_http_perl_xs_init, n, embedding, NULL); | |
556 | |
557 if (n != 0) { | |
1069 | 558 ngx_log_error(NGX_LOG_ALERT, cf->log, 0, "perl_parse() failed: %d", n); |
599 | 559 goto fail; |
560 } | |
561 | |
775 | 562 sv = get_sv("nginx::VERSION", FALSE); |
563 ver = SvPV(sv, len); | |
564 | |
565 if (ngx_strcmp(ver, NGINX_VERSION) != 0) { | |
1069 | 566 ngx_log_error(NGX_LOG_ALERT, cf->log, 0, |
775 | 567 "version " NGINX_VERSION " of nginx.pm is required, " |
568 "but %s was found", ver); | |
569 goto fail; | |
570 } | |
571 | |
1069 | 572 if (ngx_http_perl_run_requires(aTHX_ &pmcf->requires, cf->log) != NGX_OK) { |
649 | 573 goto fail; |
599 | 574 } |
575 | |
576 } | |
577 | |
578 return perl; | |
579 | |
580 fail: | |
581 | |
582 (void) perl_destruct(perl); | |
583 | |
584 perl_free(perl); | |
585 | |
586 return NULL; | |
587 } | |
588 | |
589 | |
649 | 590 static ngx_int_t |
653 | 591 ngx_http_perl_run_requires(pTHX_ ngx_array_t *requires, ngx_log_t *log) |
649 | 592 { |
593 char **script; | |
594 STRLEN len; | |
595 ngx_str_t err; | |
596 ngx_uint_t i; | |
597 | |
598 script = requires->elts; | |
599 for (i = 0; i < requires->nelts; i++) { | |
600 | |
601 require_pv(script[i]); | |
602 | |
603 if (SvTRUE(ERRSV)) { | |
604 | |
605 err.data = (u_char *) SvPV(ERRSV, len); | |
606 for (len--; err.data[len] == LF || err.data[len] == CR; len--) { | |
607 /* void */ | |
608 } | |
609 err.len = len + 1; | |
610 | |
611 ngx_log_error(NGX_LOG_EMERG, log, 0, | |
612 "require_pv(\"%s\") failed: \"%V\"", script[i], &err); | |
613 | |
614 return NGX_ERROR; | |
615 } | |
616 } | |
617 | |
618 return NGX_OK; | |
619 } | |
620 | |
621 | |
599 | 622 static ngx_int_t |
873
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
623 ngx_http_perl_call_handler(pTHX_ ngx_http_request_t *r, HV *nginx, SV *sub, |
599 | 624 ngx_str_t **args, ngx_str_t *handler, ngx_str_t *rv) |
625 { | |
626 SV *sv; | |
627 int n, status; | |
628 char *line; | |
629 STRLEN len, n_a; | |
630 ngx_str_t err; | |
631 ngx_uint_t i; | |
632 | |
633 dSP; | |
634 | |
635 status = 0; | |
636 | |
637 ENTER; | |
638 SAVETMPS; | |
639 | |
640 PUSHMARK(sp); | |
641 | |
873
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
642 sv = sv_2mortal(sv_bless(newRV_noinc(newSViv(PTR2IV(r))), nginx)); |
599 | 643 XPUSHs(sv); |
644 | |
645 if (args) { | |
646 for (i = 0; args[i]; i++) { /* void */ } | |
647 | |
648 EXTEND(sp, (int) i); | |
649 | |
650 for (i = 0; args[i]; i++) { | |
651 PUSHs(sv_2mortal(newSVpvn((char *) args[i]->data, args[i]->len))); | |
652 } | |
653 } | |
654 | |
655 PUTBACK; | |
656 | |
657 n = call_sv(sub, G_EVAL); | |
658 | |
659 SPAGAIN; | |
660 | |
661 if (n) { | |
662 if (rv == NULL) { | |
663 status = POPi; | |
664 | |
665 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
666 "call_sv: %d", status); | |
667 | |
668 } else { | |
619 | 669 line = SvPVx(POPs, n_a); |
599 | 670 rv->len = n_a; |
671 | |
672 rv->data = ngx_palloc(r->pool, n_a); | |
673 if (rv->data == NULL) { | |
674 return NGX_ERROR; | |
675 } | |
676 | |
677 ngx_memcpy(rv->data, line, n_a); | |
678 } | |
679 } | |
680 | |
681 PUTBACK; | |
682 | |
683 FREETMPS; | |
684 LEAVE; | |
685 | |
686 /* check $@ */ | |
687 | |
688 if (SvTRUE(ERRSV)) { | |
689 | |
690 err.data = (u_char *) SvPV(ERRSV, len); | |
691 for (len--; err.data[len] == LF || err.data[len] == CR; len--) { | |
692 /* void */ | |
693 } | |
694 err.len = len + 1; | |
695 | |
696 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
697 "call_sv(\"%V\") failed: \"%V\"", | |
698 handler, &err); | |
699 | |
700 if (rv) { | |
701 return NGX_ERROR; | |
702 } | |
703 | |
704 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
705 } | |
706 | |
707 if (n != 1) { | |
708 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, | |
709 "call_sv(\"%V\") returned %d results", handler, n); | |
710 status = NGX_OK; | |
711 } | |
712 | |
713 if (rv) { | |
714 return NGX_OK; | |
715 } | |
716 | |
717 return (ngx_int_t) status; | |
718 } | |
719 | |
720 | |
721 static void | |
722 ngx_http_perl_eval_anon_sub(pTHX_ ngx_str_t *handler, SV **sv) | |
723 { | |
623 | 724 u_char *p; |
725 | |
726 for (p = handler->data; *p; p++) { | |
727 if (*p != ' ' && *p != '\t' && *p != CR && *p != LF) { | |
728 break; | |
729 } | |
730 } | |
731 | |
649 | 732 if (ngx_strncmp(p, "sub ", 4) == 0 || ngx_strncmp(p, "use ", 4) == 0) { |
623 | 733 *sv = eval_pv((char *) p, FALSE); |
874 | 734 |
735 /* eval_pv() does not set ERRSV on failure */ | |
736 | |
599 | 737 return; |
738 } | |
739 | |
740 *sv = NULL; | |
741 } | |
742 | |
743 | |
744 static void * | |
745 ngx_http_perl_create_main_conf(ngx_conf_t *cf) | |
746 { | |
747 ngx_http_perl_main_conf_t *pmcf; | |
748 | |
749 pmcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_perl_main_conf_t)); | |
750 if (pmcf == NULL) { | |
751 return NGX_CONF_ERROR; | |
752 } | |
753 | |
754 if (ngx_array_init(&pmcf->requires, cf->pool, 1, sizeof(u_char *)) | |
755 != NGX_OK) | |
756 { | |
757 return NULL; | |
758 } | |
759 | |
760 return pmcf; | |
761 } | |
762 | |
763 | |
764 static char * | |
765 ngx_http_perl_init_main_conf(ngx_conf_t *cf, void *conf) | |
766 { | |
767 ngx_http_perl_main_conf_t *pmcf = conf; | |
768 | |
769 if (pmcf->perl == NULL) { | |
770 if (ngx_http_perl_init_interpreter(cf, pmcf) != NGX_CONF_OK) { | |
771 return NGX_CONF_ERROR; | |
772 } | |
773 } | |
774 | |
775 return NGX_CONF_OK; | |
776 } | |
777 | |
778 | |
872
1c4a5b3f9110
Axe several perl interpreter instances: they may be useful in currently
Igor Sysoev <igor@sysoev.ru>
parents:
871
diff
changeset
|
779 #if (NGX_HAVE_PERL_MULTIPLICITY) |
649 | 780 |
599 | 781 static void |
782 ngx_http_perl_cleanup_perl(void *data) | |
783 { | |
649 | 784 PerlInterpreter *perl = data; |
599 | 785 |
873
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
786 PERL_SET_CONTEXT(perl); |
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
787 |
599 | 788 (void) perl_destruct(perl); |
789 | |
790 perl_free(perl); | |
791 | |
792 PERL_SYS_TERM(); | |
793 } | |
794 | |
649 | 795 #endif |
796 | |
797 | |
599 | 798 static ngx_int_t |
799 ngx_http_perl_preconfiguration(ngx_conf_t *cf) | |
800 { | |
617 | 801 #if (NGX_HTTP_SSI) |
599 | 802 ngx_int_t rc; |
803 ngx_http_ssi_main_conf_t *smcf; | |
804 | |
805 smcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_ssi_filter_module); | |
806 | |
807 rc = ngx_hash_add_key(&smcf->commands, &ngx_http_perl_ssi_command.name, | |
808 &ngx_http_perl_ssi_command, NGX_HASH_READONLY_KEY); | |
809 | |
810 if (rc != NGX_OK) { | |
811 if (rc == NGX_BUSY) { | |
812 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
813 "conflicting SSI command \"%V\"", | |
814 &ngx_http_perl_ssi_command.name); | |
815 } | |
816 | |
817 return NGX_ERROR; | |
818 } | |
617 | 819 #endif |
599 | 820 |
821 return NGX_OK; | |
822 } | |
823 | |
824 | |
825 static void * | |
826 ngx_http_perl_create_loc_conf(ngx_conf_t *cf) | |
827 { | |
828 ngx_http_perl_loc_conf_t *plcf; | |
829 | |
830 plcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_perl_loc_conf_t)); | |
831 if (plcf == NULL) { | |
832 return NGX_CONF_ERROR; | |
833 } | |
834 | |
835 /* | |
836 * set by ngx_pcalloc(): | |
837 * | |
838 * plcf->handler = { 0, NULL }; | |
839 */ | |
840 | |
841 return plcf; | |
842 } | |
843 | |
844 | |
845 static char * | |
846 ngx_http_perl_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) | |
847 { | |
848 ngx_http_perl_loc_conf_t *prev = parent; | |
849 ngx_http_perl_loc_conf_t *conf = child; | |
850 | |
851 if (conf->sub == NULL) { | |
852 conf->sub = prev->sub; | |
853 conf->handler = prev->handler; | |
854 } | |
855 | |
856 return NGX_CONF_OK; | |
857 } | |
858 | |
859 | |
860 static char * | |
861 ngx_http_perl_require(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
862 { | |
863 ngx_http_perl_main_conf_t *pmcf = conf; | |
864 | |
865 u_char **p; | |
866 ngx_str_t *value; | |
867 | |
868 value = cf->args->elts; | |
869 | |
870 p = ngx_array_push(&pmcf->requires); | |
871 | |
872 if (p == NULL) { | |
873 return NGX_CONF_ERROR; | |
874 } | |
875 | |
876 *p = value[1].data; | |
877 | |
878 return NGX_CONF_OK; | |
879 } | |
880 | |
881 | |
882 static char * | |
883 ngx_http_perl(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
884 { | |
885 ngx_http_perl_loc_conf_t *plcf = conf; | |
886 | |
887 ngx_str_t *value; | |
888 ngx_http_core_loc_conf_t *clcf; | |
889 ngx_http_perl_main_conf_t *pmcf; | |
890 | |
891 value = cf->args->elts; | |
892 | |
893 if (plcf->handler.data) { | |
894 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
895 "duplicate perl handler \"%V\"", &value[1]); | |
896 return NGX_CONF_ERROR; | |
897 } | |
898 | |
899 pmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_perl_module); | |
900 | |
901 if (pmcf->perl == NULL) { | |
902 if (ngx_http_perl_init_interpreter(cf, pmcf) != NGX_CONF_OK) { | |
903 return NGX_CONF_ERROR; | |
904 } | |
905 } | |
906 | |
907 plcf->handler = value[1]; | |
908 | |
909 { | |
910 | |
911 dTHXa(pmcf->perl); | |
873
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
912 PERL_SET_CONTEXT(pmcf->perl); |
599 | 913 |
914 ngx_http_perl_eval_anon_sub(aTHX_ &value[1], &plcf->sub); | |
915 | |
916 if (plcf->sub == &PL_sv_undef) { | |
917 ngx_conf_log_error(NGX_LOG_ERR, cf, 0, | |
918 "eval_pv(\"%V\") failed", &value[1]); | |
919 return NGX_CONF_ERROR; | |
920 } | |
921 | |
922 if (plcf->sub == NULL) { | |
923 plcf->sub = newSVpvn((char *) value[1].data, value[1].len); | |
924 } | |
925 | |
926 } | |
927 | |
928 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); | |
929 clcf->handler = ngx_http_perl_handler; | |
930 | |
931 return NGX_CONF_OK; | |
932 } | |
933 | |
934 | |
935 static char * | |
936 ngx_http_perl_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
937 { | |
938 ngx_int_t index; | |
939 ngx_str_t *value; | |
940 ngx_http_variable_t *v; | |
941 ngx_http_perl_variable_t *pv; | |
942 ngx_http_perl_main_conf_t *pmcf; | |
943 | |
944 value = cf->args->elts; | |
945 | |
946 if (value[1].data[0] != '$') { | |
947 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
948 "invalid variable name \"%V\"", &value[1]); | |
949 return NGX_CONF_ERROR; | |
950 } | |
951 | |
952 value[1].len--; | |
953 value[1].data++; | |
954 | |
955 v = ngx_http_add_variable(cf, &value[1], NGX_HTTP_VAR_CHANGABLE); | |
956 if (v == NULL) { | |
957 return NGX_CONF_ERROR; | |
958 } | |
959 | |
960 pv = ngx_palloc(cf->pool, sizeof(ngx_http_perl_variable_t)); | |
961 if (pv == NULL) { | |
962 return NGX_CONF_ERROR; | |
963 } | |
964 | |
965 index = ngx_http_get_variable_index(cf, &value[1]); | |
966 if (index == NGX_ERROR) { | |
967 return NGX_CONF_ERROR; | |
968 } | |
969 | |
970 pmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_perl_module); | |
971 | |
972 if (pmcf->perl == NULL) { | |
973 if (ngx_http_perl_init_interpreter(cf, pmcf) != NGX_CONF_OK) { | |
974 return NGX_CONF_ERROR; | |
975 } | |
976 } | |
977 | |
978 pv->handler = value[2]; | |
979 | |
980 { | |
981 | |
982 dTHXa(pmcf->perl); | |
873
f92ad15c2db1
fix segfaults in future workers those will use old configuration,
Igor Sysoev <igor@sysoev.ru>
parents:
872
diff
changeset
|
983 PERL_SET_CONTEXT(pmcf->perl); |
599 | 984 |
985 ngx_http_perl_eval_anon_sub(aTHX_ &value[2], &pv->sub); | |
986 | |
987 if (pv->sub == &PL_sv_undef) { | |
988 ngx_conf_log_error(NGX_LOG_ERR, cf, 0, | |
989 "eval_pv(\"%V\") failed", &value[2]); | |
990 return NGX_CONF_ERROR; | |
991 } | |
992 | |
993 if (pv->sub == NULL) { | |
994 pv->sub = newSVpvn((char *) value[2].data, value[2].len); | |
995 } | |
996 | |
997 } | |
998 | |
637 | 999 v->get_handler = ngx_http_perl_variable; |
599 | 1000 v->data = (uintptr_t) pv; |
1001 | |
1002 return NGX_CONF_OK; | |
1003 } |