nxt_php_sapi.c (1583:0d343e154c46) nxt_php_sapi.c (1622:773f29e26072)
1/*
2 * Copyright (C) Max Romanov
3 * Copyright (C) Valentin V. Bartenev
4 * Copyright (C) NGINX, Inc.
5 */
6
7#include "php.h"
8#include "SAPI.h"
9#include "php_main.h"
10#include "php_variables.h"
11
12#include <nxt_main.h>
13#include <nxt_router.h>
14#include <nxt_unit.h>
15#include <nxt_unit_request.h>
16
1/*
2 * Copyright (C) Max Romanov
3 * Copyright (C) Valentin V. Bartenev
4 * Copyright (C) NGINX, Inc.
5 */
6
7#include "php.h"
8#include "SAPI.h"
9#include "php_main.h"
10#include "php_variables.h"
11
12#include <nxt_main.h>
13#include <nxt_router.h>
14#include <nxt_unit.h>
15#include <nxt_unit_request.h>
16
17#include NXT_PHP_MOUNTS_H
18
17
19
20#if PHP_VERSION_ID >= 50400
21#define NXT_HAVE_PHP_IGNORE_CWD 1
22#endif
23
24#if PHP_VERSION_ID >= 70100
25#define NXT_HAVE_PHP_LOG_MESSAGE_WITH_SYSLOG_TYPE 1
26#else
27#define NXT_HAVE_PHP_INTERRUPTS 1

--- 46 unchanged lines hidden (view full) ---

74typedef int (*nxt_php_disable_t)(char *p, uint TSRMLS_DC);
75#endif
76
77#if PHP_VERSION_ID < 70200
78typedef void (*zif_handler)(INTERNAL_FUNCTION_PARAMETERS);
79#endif
80
81
18#if PHP_VERSION_ID >= 50400
19#define NXT_HAVE_PHP_IGNORE_CWD 1
20#endif
21
22#if PHP_VERSION_ID >= 70100
23#define NXT_HAVE_PHP_LOG_MESSAGE_WITH_SYSLOG_TYPE 1
24#else
25#define NXT_HAVE_PHP_INTERRUPTS 1

--- 46 unchanged lines hidden (view full) ---

72typedef int (*nxt_php_disable_t)(char *p, uint TSRMLS_DC);
73#endif
74
75#if PHP_VERSION_ID < 70200
76typedef void (*zif_handler)(INTERNAL_FUNCTION_PARAMETERS);
77#endif
78
79
80static nxt_int_t nxt_php_setup(nxt_task_t *task, nxt_process_t *process,
81 nxt_common_app_conf_t *conf);
82static nxt_int_t nxt_php_start(nxt_task_t *task, nxt_process_data_t *data);
83static nxt_int_t nxt_php_set_target(nxt_task_t *task, nxt_php_target_t *target,
84 nxt_conf_value_t *conf);
82static nxt_int_t nxt_php_start(nxt_task_t *task, nxt_process_data_t *data);
83static nxt_int_t nxt_php_set_target(nxt_task_t *task, nxt_php_target_t *target,
84 nxt_conf_value_t *conf);
85static nxt_int_t nxt_php_set_ini_path(nxt_task_t *task, nxt_str_t *path,
86 char *workdir);
85static void nxt_php_set_options(nxt_task_t *task, nxt_conf_value_t *options,
86 int type);
87static nxt_int_t nxt_php_alter_option(nxt_str_t *name, nxt_str_t *value,
88 int type);
89static void nxt_php_disable(nxt_task_t *task, const char *type,
90 nxt_str_t *value, char **ptr, nxt_php_disable_t disable);
91
92static nxt_int_t nxt_php_dirname(const nxt_str_t *file, nxt_str_t *dir);

--- 154 unchanged lines hidden (view full) ---

247};
248
249
250NXT_EXPORT nxt_app_module_t nxt_app_module = {
251 sizeof(compat),
252 compat,
253 nxt_string("php"),
254 PHP_VERSION,
87static void nxt_php_set_options(nxt_task_t *task, nxt_conf_value_t *options,
88 int type);
89static nxt_int_t nxt_php_alter_option(nxt_str_t *name, nxt_str_t *value,
90 int type);
91static void nxt_php_disable(nxt_task_t *task, const char *type,
92 nxt_str_t *value, char **ptr, nxt_php_disable_t disable);
93
94static nxt_int_t nxt_php_dirname(const nxt_str_t *file, nxt_str_t *dir);

--- 154 unchanged lines hidden (view full) ---

249};
250
251
252NXT_EXPORT nxt_app_module_t nxt_app_module = {
253 sizeof(compat),
254 compat,
255 nxt_string("php"),
256 PHP_VERSION,
255 nxt_php_mounts,
256 nxt_nitems(nxt_php_mounts),
257 NULL,
257 NULL,
258 0,
259 nxt_php_setup,
258 nxt_php_start,
259};
260
261
262static nxt_php_target_t *nxt_php_targets;
263static nxt_int_t nxt_php_last_target = -1;
264
265static nxt_unit_ctx_t *nxt_php_unit_ctx;
266#if defined(ZTS) && PHP_VERSION_ID < 70400
267static void ***tsrm_ls;
268#endif
269
270
271static nxt_int_t
260 nxt_php_start,
261};
262
263
264static nxt_php_target_t *nxt_php_targets;
265static nxt_int_t nxt_php_last_target = -1;
266
267static nxt_unit_ctx_t *nxt_php_unit_ctx;
268#if defined(ZTS) && PHP_VERSION_ID < 70400
269static void ***tsrm_ls;
270#endif
271
272
273static nxt_int_t
272nxt_php_start(nxt_task_t *task, nxt_process_data_t *data)
274nxt_php_setup(nxt_task_t *task, nxt_process_t *process,
275 nxt_common_app_conf_t *conf)
273{
276{
274 u_char *p;
275 uint32_t next;
276 nxt_str_t ini_path, name;
277 nxt_int_t ret;
278 nxt_uint_t n;
279 nxt_unit_ctx_t *unit_ctx;
280 nxt_unit_init_t php_init;
281 nxt_conf_value_t *value;
282 nxt_php_app_conf_t *c;
283 nxt_common_app_conf_t *conf;
277 nxt_str_t ini_path;
278 nxt_int_t ret;
279 nxt_conf_value_t *value;
280 nxt_php_app_conf_t *c;
284
285 static nxt_str_t file_str = nxt_string("file");
286 static nxt_str_t user_str = nxt_string("user");
287 static nxt_str_t admin_str = nxt_string("admin");
288
281
282 static nxt_str_t file_str = nxt_string("file");
283 static nxt_str_t user_str = nxt_string("user");
284 static nxt_str_t admin_str = nxt_string("admin");
285
289 conf = data->app;
290 c = &conf->u.php;
291
286 c = &conf->u.php;
287
292 n = (c->targets != NULL) ? nxt_conf_object_members_count(c->targets) : 1;
293
294 nxt_php_targets = nxt_zalloc(sizeof(nxt_php_target_t) * n);
295 if (nxt_slow_path(nxt_php_targets == NULL)) {
296 return NXT_ERROR;
297 }
298
299 if (c->targets != NULL) {
300 next = 0;
301
302 for (n = 0; /* void */; n++) {
303 value = nxt_conf_next_object_member(c->targets, &name, &next);
304 if (value == NULL) {
305 break;
306 }
307
308 ret = nxt_php_set_target(task, &nxt_php_targets[n], value);
309 if (nxt_slow_path(ret != NXT_OK)) {
310 return NXT_ERROR;
311 }
312 }
313
314 } else {
315 ret = nxt_php_set_target(task, &nxt_php_targets[0], conf->self);
316 if (nxt_slow_path(ret != NXT_OK)) {
317 return NXT_ERROR;
318 }
319 }
320
321#ifdef ZTS
322
323#if PHP_VERSION_ID >= 70400
324 php_tsrm_startup();
325#else
326 tsrm_startup(1, 1, 0, NULL);
327 tsrm_ls = ts_resource(0);
328#endif

--- 13 unchanged lines hidden (view full) ---

342 sapi_startup(&nxt_php_sapi_module);
343
344 if (c->options != NULL) {
345 value = nxt_conf_get_object_member(c->options, &file_str, NULL);
346
347 if (value != NULL) {
348 nxt_conf_get_string(value, &ini_path);
349
288#ifdef ZTS
289
290#if PHP_VERSION_ID >= 70400
291 php_tsrm_startup();
292#else
293 tsrm_startup(1, 1, 0, NULL);
294 tsrm_ls = ts_resource(0);
295#endif

--- 13 unchanged lines hidden (view full) ---

309 sapi_startup(&nxt_php_sapi_module);
310
311 if (c->options != NULL) {
312 value = nxt_conf_get_object_member(c->options, &file_str, NULL);
313
314 if (value != NULL) {
315 nxt_conf_get_string(value, &ini_path);
316
350 p = nxt_malloc(ini_path.length + 1);
351 if (nxt_slow_path(p == NULL)) {
317 ret = nxt_php_set_ini_path(task, &ini_path,
318 conf->working_directory);
319
320 if (nxt_slow_path(ret != NXT_OK)) {
352 return NXT_ERROR;
353 }
321 return NXT_ERROR;
322 }
354
355 nxt_php_sapi_module.php_ini_path_override = (char *) p;
356
357 p = nxt_cpymem(p, ini_path.start, ini_path.length);
358 *p = '\0';
359 }
360 }
361
362 if (nxt_slow_path(nxt_php_startup(&nxt_php_sapi_module) == FAILURE)) {
363 nxt_alert(task, "failed to initialize SAPI module and extension");
364 return NXT_ERROR;
365 }
366
367 if (c->options != NULL) {
368 value = nxt_conf_get_object_member(c->options, &admin_str, NULL);
369 nxt_php_set_options(task, value, ZEND_INI_SYSTEM);
370
371 value = nxt_conf_get_object_member(c->options, &user_str, NULL);
372 nxt_php_set_options(task, value, ZEND_INI_USER);
373 }
374
323 }
324 }
325
326 if (nxt_slow_path(nxt_php_startup(&nxt_php_sapi_module) == FAILURE)) {
327 nxt_alert(task, "failed to initialize SAPI module and extension");
328 return NXT_ERROR;
329 }
330
331 if (c->options != NULL) {
332 value = nxt_conf_get_object_member(c->options, &admin_str, NULL);
333 nxt_php_set_options(task, value, ZEND_INI_SYSTEM);
334
335 value = nxt_conf_get_object_member(c->options, &user_str, NULL);
336 nxt_php_set_options(task, value, ZEND_INI_USER);
337 }
338
339 return NXT_OK;
340}
341
342
343static nxt_int_t
344nxt_php_start(nxt_task_t *task, nxt_process_data_t *data)
345{
346 uint32_t next;
347 nxt_int_t ret;
348 nxt_str_t name;
349 nxt_uint_t n;
350 nxt_unit_ctx_t *unit_ctx;
351 nxt_unit_init_t php_init;
352 nxt_conf_value_t *value;
353 nxt_php_app_conf_t *c;
354 nxt_common_app_conf_t *conf;
355
356 conf = data->app;
357 c = &conf->u.php;
358
359 n = (c->targets != NULL) ? nxt_conf_object_members_count(c->targets) : 1;
360
361 nxt_php_targets = nxt_zalloc(sizeof(nxt_php_target_t) * n);
362 if (nxt_slow_path(nxt_php_targets == NULL)) {
363 return NXT_ERROR;
364 }
365
366 if (c->targets != NULL) {
367 next = 0;
368
369 for (n = 0; /* void */; n++) {
370 value = nxt_conf_next_object_member(c->targets, &name, &next);
371 if (value == NULL) {
372 break;
373 }
374
375 ret = nxt_php_set_target(task, &nxt_php_targets[n], value);
376 if (nxt_slow_path(ret != NXT_OK)) {
377 return NXT_ERROR;
378 }
379 }
380
381 } else {
382 ret = nxt_php_set_target(task, &nxt_php_targets[0], conf->self);
383 if (nxt_slow_path(ret != NXT_OK)) {
384 return NXT_ERROR;
385 }
386 }
387
375 ret = nxt_unit_default_init(task, &php_init);
376 if (nxt_slow_path(ret != NXT_OK)) {
377 nxt_alert(task, "nxt_unit_default_init() failed");
378 return ret;
379 }
380
381 php_init.callbacks.request_handler = nxt_php_request_handler;
382 php_init.shm_limit = conf->shm_limit;
383
384 unit_ctx = nxt_unit_init(&php_init);
385 if (nxt_slow_path(unit_ctx == NULL)) {
386 return NXT_ERROR;
387 }
388
389 nxt_php_unit_ctx = unit_ctx;
390
388 ret = nxt_unit_default_init(task, &php_init);
389 if (nxt_slow_path(ret != NXT_OK)) {
390 nxt_alert(task, "nxt_unit_default_init() failed");
391 return ret;
392 }
393
394 php_init.callbacks.request_handler = nxt_php_request_handler;
395 php_init.shm_limit = conf->shm_limit;
396
397 unit_ctx = nxt_unit_init(&php_init);
398 if (nxt_slow_path(unit_ctx == NULL)) {
399 return NXT_ERROR;
400 }
401
402 nxt_php_unit_ctx = unit_ctx;
403
391 nxt_unit_run(unit_ctx);
404 nxt_unit_run(nxt_php_unit_ctx);
405 nxt_unit_done(nxt_php_unit_ctx);
392
406
393 nxt_unit_done(unit_ctx);
394
395 exit(0);
396
397 return NXT_OK;
398}
399
400
401static nxt_int_t
402nxt_php_set_target(nxt_task_t *task, nxt_php_target_t *target,

--- 104 unchanged lines hidden (view full) ---

507 nxt_str_set(&target->index, "index.php");
508 }
509 }
510
511 return NXT_OK;
512}
513
514
407 exit(0);
408
409 return NXT_OK;
410}
411
412
413static nxt_int_t
414nxt_php_set_target(nxt_task_t *task, nxt_php_target_t *target,

--- 104 unchanged lines hidden (view full) ---

519 nxt_str_set(&target->index, "index.php");
520 }
521 }
522
523 return NXT_OK;
524}
525
526
527static nxt_int_t
528nxt_php_set_ini_path(nxt_task_t *task, nxt_str_t *ini_path, char *workdir)
529{
530 size_t wdlen;
531 u_char *p, *start;
532
533 if (ini_path->start[0] == '/' || workdir == NULL) {
534 p = nxt_malloc(ini_path->length + 1);
535 if (nxt_slow_path(p == NULL)) {
536 return NXT_ERROR;
537 }
538
539 start = p;
540
541 } else {
542 wdlen = nxt_strlen(workdir);
543
544 p = nxt_malloc(wdlen + ini_path->length + 2);
545 if (nxt_slow_path(p == NULL)) {
546 return NXT_ERROR;
547 }
548
549 start = p;
550
551 p = nxt_cpymem(p, workdir, wdlen);
552
553 if (workdir[wdlen - 1] != '/') {
554 *p++ = '/';
555 }
556 }
557
558 p = nxt_cpymem(p, ini_path->start, ini_path->length);
559 *p = '\0';
560
561 nxt_php_sapi_module.php_ini_path_override = (char *) start;
562
563 return NXT_OK;
564}
565
566
515static void
516nxt_php_set_options(nxt_task_t *task, nxt_conf_value_t *options, int type)
517{
518 uint32_t next;
519 nxt_str_t name, value;
520 nxt_conf_value_t *value_obj;
521
522 if (options != NULL) {

--- 757 unchanged lines hidden ---
567static void
568nxt_php_set_options(nxt_task_t *task, nxt_conf_value_t *options, int type)
569{
570 uint32_t next;
571 nxt_str_t name, value;
572 nxt_conf_value_t *value_obj;
573
574 if (options != NULL) {

--- 757 unchanged lines hidden ---