php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
fpm_main.c
Go to the documentation of this file.
1/*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | https://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Authors: Rasmus Lerdorf <rasmus@lerdorf.on.ca> |
14 | Stig Bakken <ssb@php.net> |
15 | Zeev Suraski <zeev@php.net> |
16 | FastCGI: Ben Mansell <php@slimyhorror.com> |
17 | Shane Caraveo <shane@caraveo.com> |
18 | Dmitry Stogov <dmitry@php.net> |
19 +----------------------------------------------------------------------+
20*/
21
22#include "php.h"
23#include "php_globals.h"
24#include "php_variables.h"
25#include "php_ini_builder.h"
26#include "zend_modules.h"
27#include "php.h"
28#include "zend_ini_scanner.h"
29#include "zend_globals.h"
30#include "zend_stream.h"
31
32#include "SAPI.h"
33
34#include <stdio.h>
35#include "php.h"
36
37#ifdef HAVE_SYS_TIME_H
38# include <sys/time.h>
39#endif
40
41#ifdef HAVE_UNISTD_H
42# include <unistd.h>
43#endif
44
45#include <signal.h>
46
47#include <locale.h>
48
49#ifdef HAVE_SYS_TYPES_H
50# include <sys/types.h>
51#endif
52
53#ifdef HAVE_SYS_WAIT_H
54# include <sys/wait.h>
55#endif
56
57#ifdef HAVE_FCNTL_H
58# include <fcntl.h>
59#endif
60
61#include "zend.h"
62#include "zend_extensions.h"
63#include "php_ini.h"
64#include "php_globals.h"
65#include "php_main.h"
66#include "fopen_wrappers.h"
68
69#ifdef __riscos__
70# include <unixlib/local.h>
71int __riscosify_control = __RISCOSIFY_STRICT_UNIX_SPECS;
72#endif
73
74#include "zend_compile.h"
75#include "zend_execute.h"
76#include "zend_highlight.h"
77
78#include "php_getopt.h"
79
80#include "http_status_codes.h"
81
82#include "fastcgi.h"
83
84#include <php_config.h>
85#include "fpm.h"
86#include "fpm_main_arginfo.h"
87#include "fpm_request.h"
88#include "fpm_status.h"
89#include "fpm_signals.h"
90#include "fpm_stdio.h"
91#include "fpm_conf.h"
92#include "fpm_php.h"
93#include "fpm_log.h"
94#include "zlog.h"
95
96static void (*php_php_import_environment_variables)(zval *array_ptr);
97
98/* these globals used for forking children on unix systems */
99
103static int parent = 1;
104
105static int request_body_fd;
106static int fpm_is_running = 0;
107
108static char *sapi_cgibin_getenv(const char *name, size_t name_len);
109static void fastcgi_ini_parser(zval *arg1, zval *arg2, zval *arg3, int callback_type, void *arg);
110
111#define PHP_MODE_STANDARD 1
112#define PHP_MODE_HIGHLIGHT 2
113#define PHP_MODE_INDENT 3
114#define PHP_MODE_LINT 4
115#define PHP_MODE_STRIP 5
116
117static char *php_optarg = NULL;
118static int php_optind = 1;
119static zend_module_entry cgi_module_entry;
120
121static const opt_struct OPTIONS[] = {
122 {'c', 1, "php-ini"},
123 {'d', 1, "define"},
124 {'e', 0, "profile-info"},
125 {'h', 0, "help"},
126 {'i', 0, "info"},
127 {'m', 0, "modules"},
128 {'n', 0, "no-php-ini"},
129 {'?', 0, "usage"},/* help alias (both '?' and 'usage') */
130 {'v', 0, "version"},
131 {'y', 1, "fpm-config"},
132 {'t', 0, "test"},
133 {'p', 1, "prefix"},
134 {'g', 1, "pid"},
135 {'R', 0, "allow-to-run-as-root"},
136 {'D', 0, "daemonize"},
137 {'F', 0, "nodaemonize"},
138 {'O', 0, "force-stderr"},
139 {'-', 0, NULL} /* end of args */
140};
141
142typedef struct _php_cgi_globals_struct {
143 bool rfc2616_headers;
144 bool nph;
145 bool fix_pathinfo;
146 bool force_redirect;
147 bool discard_path;
148 bool fcgi_logging;
155
156/* {{{ user_config_cache
157 *
158 * Key for each cache entry is dirname(PATH_TRANSLATED).
159 *
160 * NOTE: Each cache entry config_hash contains the combination from all user ini files found in
161 * the path starting from doc_root through to dirname(PATH_TRANSLATED). There is no point
162 * storing per-file entries as it would not be possible to detect added / deleted entries
163 * between separate files.
164 */
165typedef struct _user_config_cache_entry {
166 time_t expires;
169
170static void user_config_cache_entry_dtor(zval *el)
171{
174 free(entry->user_config);
175 free(entry);
176}
177/* }}} */
178
179#ifdef ZTS
180static int php_cgi_globals_id;
181#define CGIG(v) ZEND_TSRMG(php_cgi_globals_id, php_cgi_globals_struct *, v)
182#else
183static php_cgi_globals_struct php_cgi_globals;
184#define CGIG(v) (php_cgi_globals.v)
185#endif
186
187static int module_name_cmp(Bucket *f, Bucket *s) /* {{{ */
188{
189 return strcasecmp( ((zend_module_entry *) Z_PTR(f->val))->name,
190 ((zend_module_entry *) Z_PTR(s->val))->name);
191}
192/* }}} */
193
194static void print_modules(void) /* {{{ */
195{
196 HashTable sorted_registry;
197 zend_module_entry *module;
198
199 zend_hash_init(&sorted_registry, 50, NULL, NULL, 1);
200 zend_hash_copy(&sorted_registry, &module_registry, NULL);
201 zend_hash_sort(&sorted_registry, module_name_cmp, 0);
202 ZEND_HASH_MAP_FOREACH_PTR(&sorted_registry, module) {
203 php_printf("%s\n", module->name);
205 zend_hash_destroy(&sorted_registry);
206}
207/* }}} */
208
209static void print_extension_info(zend_extension *ext) /* {{{ */
210{
211 php_printf("%s\n", ext->name);
212}
213/* }}} */
214
215static int extension_name_cmp(const zend_llist_element **f, const zend_llist_element **s) /* {{{ */
216{
217 zend_extension *fe = (zend_extension*)(*f)->data;
218 zend_extension *se = (zend_extension*)(*s)->data;
219 return strcmp(fe->name, se->name);
220}
221/* }}} */
222
223static void print_extensions(void) /* {{{ */
224{
225 zend_llist sorted_exts;
226
227 zend_llist_copy(&sorted_exts, &zend_extensions);
228 sorted_exts.dtor = NULL;
229 zend_llist_sort(&sorted_exts, extension_name_cmp);
230 zend_llist_apply(&sorted_exts, (llist_apply_func_t) print_extension_info);
231 zend_llist_destroy(&sorted_exts);
232}
233/* }}} */
234
235#ifndef STDOUT_FILENO
236#define STDOUT_FILENO 1
237#endif
238
239static inline size_t sapi_cgibin_single_write(const char *str, uint32_t str_length) /* {{{ */
240{
241 ssize_t ret;
242
243 /* sapi has started which means everything must be send through fcgi */
244 if (fpm_is_running) {
245 fcgi_request *request = (fcgi_request*) SG(server_context);
246 ret = fcgi_write(request, FCGI_STDOUT, str, str_length);
247 if (ret <= 0) {
248 return 0;
249 }
250 return (size_t)ret;
251 }
252
253 /* sapi has not started, output to stdout instead of fcgi */
254#ifdef PHP_WRITE_STDOUT
255 ret = write(STDOUT_FILENO, str, str_length);
256 if (ret <= 0) {
257 return 0;
258 }
259 return (size_t)ret;
260#else
261 return fwrite(str, 1, MIN(str_length, 16384), stdout);
262#endif
263}
264/* }}} */
265
266static size_t sapi_cgibin_ub_write(const char *str, size_t str_length) /* {{{ */
267{
268 const char *ptr = str;
269 uint32_t remaining = str_length;
270 size_t ret;
271
272 while (remaining > 0) {
273 ret = sapi_cgibin_single_write(ptr, remaining);
274 if (!ret) {
276 return str_length - remaining;
277 }
278 ptr += ret;
279 remaining -= ret;
280 }
281
282 return str_length;
283}
284/* }}} */
285
286static void sapi_cgibin_flush(void *server_context) /* {{{ */
287{
288 /* fpm has started, let use fcgi instead of stdout */
289 if (fpm_is_running) {
290 fcgi_request *request = (fcgi_request*) server_context;
291 if (!parent && request) {
293 if (!fcgi_flush(request, 0)) {
295 }
296 }
297 return;
298 }
299
300 /* fpm has not started yet, let use stdout instead of fcgi */
301 if (fflush(stdout) == EOF) {
303 }
304}
305/* }}} */
306
307#define SAPI_CGI_MAX_HEADER_LENGTH 1024
308
309static int sapi_cgi_send_headers(sapi_headers_struct *sapi_headers) /* {{{ */
310{
314 bool ignore_status = 0;
315 int response_status = SG(sapi_headers).http_response_code;
316
317 if (SG(request_info).no_headers == 1) {
319 }
320
321 if (CGIG(nph) || SG(sapi_headers).http_response_code != 200)
322 {
323 int len;
324 bool has_status = 0;
325
326 if (CGIG(rfc2616_headers) && SG(sapi_headers).http_status_line) {
327 char *s;
328 len = slprintf(buf, SAPI_CGI_MAX_HEADER_LENGTH, "%s", SG(sapi_headers).http_status_line);
329 if ((s = strchr(SG(sapi_headers).http_status_line, ' '))) {
330 response_status = atoi((s + 1));
331 }
332
335 }
336
337 } else {
338 char *s;
339
340 if (SG(sapi_headers).http_status_line &&
341 (s = strchr(SG(sapi_headers).http_status_line, ' ')) != 0 &&
342 (s - SG(sapi_headers).http_status_line) >= 5 &&
343 strncasecmp(SG(sapi_headers).http_status_line, "HTTP/", 5) == 0
344 ) {
345 len = slprintf(buf, sizeof(buf), "Status:%s", s);
346 response_status = atoi((s + 1));
347 } else {
349 while (h) {
350 if (h->header_len > sizeof("Status:") - 1 &&
351 strncasecmp(h->header, "Status:", sizeof("Status:") - 1) == 0
352 ) {
353 has_status = 1;
354 break;
355 }
357 }
358 if (!has_status) {
360
361 while (err->code != 0) {
362 if (err->code == SG(sapi_headers).http_response_code) {
363 break;
364 }
365 err++;
366 }
367 if (err->str) {
368 len = slprintf(buf, sizeof(buf), "Status: %d %s", SG(sapi_headers).http_response_code, err->str);
369 } else {
370 len = slprintf(buf, sizeof(buf), "Status: %d", SG(sapi_headers).http_response_code);
371 }
372 }
373 }
374 }
375
376 if (!has_status) {
378 PHPWRITE_H("\r\n", 2);
379 ignore_status = 1;
380 }
381 }
382
384 while (h) {
385 /* prevent CRLFCRLF */
386 if (h->header_len) {
387 if (h->header_len > sizeof("Status:") - 1 &&
388 strncasecmp(h->header, "Status:", sizeof("Status:") - 1) == 0
389 ) {
390 if (!ignore_status) {
391 ignore_status = 1;
393 PHPWRITE_H("\r\n", 2);
394 }
395 } else if (response_status == 304 && h->header_len > sizeof("Content-Type:") - 1 &&
396 strncasecmp(h->header, "Content-Type:", sizeof("Content-Type:") - 1) == 0
397 ) {
399 continue;
400 } else {
402 PHPWRITE_H("\r\n", 2);
403 }
404 }
406 }
407 PHPWRITE_H("\r\n", 2);
408
410}
411/* }}} */
412
413#ifndef STDIN_FILENO
414# define STDIN_FILENO 0
415#endif
416
417#ifndef HAVE_ATTRIBUTE_WEAK
418static void fpm_fcgi_log(int type, const char *fmt, ...) /* {{{ */
419#else
420void fcgi_log(int type, const char *fmt, ...)
421#endif
422{
423 va_list args;
424 va_start(args, fmt);
425 vzlog("", 0, type, fmt, args);
426 va_end(args);
427}
428/* }}} */
429
430static size_t sapi_cgi_read_post(char *buffer, size_t count_bytes) /* {{{ */
431{
432 uint32_t read_bytes = 0;
433 int tmp_read_bytes;
434 size_t remaining = SG(request_info).content_length - SG(read_post_bytes);
435
436 if (remaining < count_bytes) {
437 count_bytes = remaining;
438 }
439 while (read_bytes < count_bytes) {
440 fcgi_request *request = (fcgi_request*) SG(server_context);
441 if (request_body_fd == -1) {
442 char *request_body_filename = FCGI_GETENV(request, "REQUEST_BODY_FILE");
443
444 if (request_body_filename && *request_body_filename) {
445 request_body_fd = open(request_body_filename, O_RDONLY);
446
447 if (0 > request_body_fd) {
448 php_error(E_WARNING, "REQUEST_BODY_FILE: open('%s') failed: %s (%d)",
449 request_body_filename, strerror(errno), errno);
450 return 0;
451 }
452 }
453 }
454
455 /* If REQUEST_BODY_FILE variable not available - read post body from fastcgi stream */
456 if (request_body_fd < 0) {
457 tmp_read_bytes = fcgi_read(request, buffer + read_bytes, count_bytes - read_bytes);
458 } else {
459 tmp_read_bytes = read(request_body_fd, buffer + read_bytes, count_bytes - read_bytes);
460 }
461 if (tmp_read_bytes <= 0) {
462 break;
463 }
464 read_bytes += tmp_read_bytes;
465 }
466 return read_bytes;
467}
468/* }}} */
469
470static char *sapi_cgibin_getenv(const char *name, size_t name_len) /* {{{ */
471{
472 /* if fpm has started, use fcgi env */
473 if (fpm_is_running) {
474 fcgi_request *request = (fcgi_request*) SG(server_context);
475 return fcgi_getenv(request, name, name_len);
476 }
477
478 /* if fpm has not started yet, use std env */
479 return getenv(name);
480}
481/* }}} */
482
483#if 0
484static char *_sapi_cgibin_putenv(char *name, char *value) /* {{{ */
485{
486 int name_len;
487
488 if (!name) {
489 return NULL;
490 }
491 name_len = strlen(name);
492
493 fcgi_request *request = (fcgi_request*) SG(server_context);
494 return fcgi_putenv(request, name, name_len, value);
495}
496/* }}} */
497#endif
498
499static char *sapi_cgi_read_cookies(void) /* {{{ */
500{
501 fcgi_request *request = (fcgi_request*) SG(server_context);
502
503 return FCGI_GETENV(request, "HTTP_COOKIE");
504}
505/* }}} */
506
507static void cgi_php_load_env_var(const char *var, unsigned int var_len, char *val, unsigned int val_len, void *arg) /* {{{ */
508{
509 zval *array_ptr = (zval *) arg;
510 int filter_arg = (Z_ARR_P(array_ptr) == Z_ARR(PG(http_globals)[TRACK_VARS_ENV])) ? PARSE_ENV : PARSE_SERVER;
511 size_t new_val_len;
512
513 if (sapi_module.input_filter(filter_arg, var, &val, strlen(val), &new_val_len)) {
514 php_register_variable_safe(var, val, new_val_len, array_ptr);
515 }
516}
517/* }}} */
518
519static void cgi_php_load_env_var_unfilterd(const char *var, unsigned int var_len, char *val, unsigned int val_len, void *arg)
520{
521 zval *array_ptr = (zval *) arg;
522 php_register_variable_safe(var, val, val_len, array_ptr);
523}
524
525static void cgi_php_load_environment_variables(zval *array_ptr)
526{
527 php_php_import_environment_variables(array_ptr);
528
529 fcgi_request *request = (fcgi_request*) SG(server_context);
530 fcgi_loadenv(request, cgi_php_load_env_var_unfilterd, array_ptr);
531}
532
533static void cgi_php_import_environment_variables(zval *array_ptr)
534{
535 fcgi_request *request = NULL;
536
537 if (Z_TYPE(PG(http_globals)[TRACK_VARS_ENV]) == IS_ARRAY &&
538 Z_ARR_P(array_ptr) != Z_ARR(PG(http_globals)[TRACK_VARS_ENV]) &&
539 zend_hash_num_elements(Z_ARRVAL(PG(http_globals)[TRACK_VARS_ENV])) > 0
540 ) {
541 zend_array_destroy(Z_ARR_P(array_ptr));
542 Z_ARR_P(array_ptr) = zend_array_dup(Z_ARR(PG(http_globals)[TRACK_VARS_ENV]));
543 return;
544 } else if (Z_TYPE(PG(http_globals)[TRACK_VARS_SERVER]) == IS_ARRAY &&
545 Z_ARR_P(array_ptr) != Z_ARR(PG(http_globals)[TRACK_VARS_SERVER]) &&
546 zend_hash_num_elements(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER])) > 0
547 ) {
548 zend_array_destroy(Z_ARR_P(array_ptr));
549 Z_ARR_P(array_ptr) = zend_array_dup(Z_ARR(PG(http_globals)[TRACK_VARS_SERVER]));
550 return;
551 }
552
553 /* call php's original import as a catch-all */
554 php_php_import_environment_variables(array_ptr);
555
556 request = (fcgi_request*) SG(server_context);
557 fcgi_loadenv(request, cgi_php_load_env_var, array_ptr);
558}
559
560static void sapi_cgi_register_variables(zval *track_vars_array) /* {{{ */
561{
562 size_t php_self_len;
563 char *php_self;
564
565 /* In CGI mode, we consider the environment to be a part of the server
566 * variables
567 */
568 php_import_environment_variables(track_vars_array);
569
570 if (CGIG(fix_pathinfo)) {
571 char *script_name = SG(request_info).request_uri;
572 unsigned int script_name_len = script_name ? strlen(script_name) : 0;
573 char *path_info = sapi_cgibin_getenv("PATH_INFO", sizeof("PATH_INFO") - 1);
574 unsigned int path_info_len = path_info ? strlen(path_info) : 0;
575
576 php_self_len = script_name_len + path_info_len;
577 php_self = emalloc(php_self_len + 1);
578
579 /* Concat script_name and path_info into php_self */
580 if (script_name) {
581 memcpy(php_self, script_name, script_name_len + 1);
582 }
583 if (path_info) {
584 memcpy(php_self + script_name_len, path_info, path_info_len + 1);
585 }
586
587 /* Build the special-case PHP_SELF variable for the CGI version */
588 if (sapi_module.input_filter(PARSE_SERVER, "PHP_SELF", &php_self, php_self_len, &php_self_len)) {
589 php_register_variable_safe("PHP_SELF", php_self, php_self_len, track_vars_array);
590 }
591 efree(php_self);
592 } else {
593 php_self = SG(request_info).request_uri ? SG(request_info).request_uri : "";
594 php_self_len = strlen(php_self);
595 if (sapi_module.input_filter(PARSE_SERVER, "PHP_SELF", &php_self, php_self_len, &php_self_len)) {
596 php_register_variable_safe("PHP_SELF", php_self, php_self_len, track_vars_array);
597 }
598 }
599}
600/* }}} */
601
602/* {{{ sapi_cgi_log_fastcgi
603 *
604 * Ignore level, we want to send all messages through fastcgi
605 */
606static void sapi_cgi_log_fastcgi(int level, char *message, size_t len)
607{
608
609 fcgi_request *request = (fcgi_request*) SG(server_context);
610
611 /* message is written to FCGI_STDERR if following conditions are met:
612 * - logging is enabled (fastcgi.logging in php.ini)
613 * - we are currently dealing with a request
614 * - the message is not empty
615 */
616 if (CGIG(fcgi_logging) && request && message && len > 0) {
617 if (CGIG(fcgi_logging_request_started)) {
618 fcgi_write(request, FCGI_STDERR, "; ", 2);
619 } else {
620 CGIG(fcgi_logging_request_started) = true;
621 }
622 if (fcgi_write(request, FCGI_STDERR, message, len) < 0) {
624 }
625 }
626}
627/* }}} */
628
629/* {{{ sapi_cgi_log_message */
630static void sapi_cgi_log_message(const char *message, int syslog_type_int)
631{
632 zlog_msg(ZLOG_NOTICE, "PHP message: ", message);
633}
634/* }}} */
635
636/* {{{ php_cgi_ini_activate_user_config */
637static void php_cgi_ini_activate_user_config(char *path, int path_len, const char *doc_root, int doc_root_len)
638{
639 char *ptr;
640 time_t request_time = sapi_get_request_time();
641 user_config_cache_entry *entry = zend_hash_str_find_ptr(&CGIG(user_config_cache), path, path_len);
642
643 /* Find cached config entry: If not found, create one */
644 if (!entry) {
645 entry = pemalloc(sizeof(user_config_cache_entry), 1);
646 entry->expires = 0;
647 entry->user_config = (HashTable *) pemalloc(sizeof(HashTable), 1);
649 zend_hash_str_update_ptr(&CGIG(user_config_cache), path, path_len, entry);
650 }
651
652 /* Check whether cache entry has expired and rescan if it is */
653 if (request_time > entry->expires) {
654 char * real_path;
655 int real_path_len;
656 char *s1, *s2;
657 int s_len;
658
659 /* Clear the expired config */
661
662 if (!IS_ABSOLUTE_PATH(path, path_len)) {
663 real_path = tsrm_realpath(path, NULL);
664 if (real_path == NULL) {
665 return;
666 }
667 real_path_len = strlen(real_path);
668 path = real_path;
669 path_len = real_path_len;
670 }
671
672 if (path_len > doc_root_len) {
673 s1 = (char *) doc_root;
674 s2 = path;
675 s_len = doc_root_len;
676 } else {
677 s1 = path;
678 s2 = (char *) doc_root;
679 s_len = path_len;
680 }
681
682 /* we have to test if path is part of DOCUMENT_ROOT.
683 if it is inside the docroot, we scan the tree up to the docroot
684 to find more user.ini, if not we only scan the current path.
685 */
686 if (strncmp(s1, s2, s_len) == 0) {
687 ptr = s2 + doc_root_len;
688 while ((ptr = strchr(ptr, DEFAULT_SLASH)) != NULL) {
689 *ptr = 0;
690 php_parse_user_ini_file(path, PG(user_ini_filename), entry->user_config);
691 *ptr = '/';
692 ptr++;
693 }
694 } else {
695 php_parse_user_ini_file(path, PG(user_ini_filename), entry->user_config);
696 }
697
698 entry->expires = request_time + PG(user_ini_cache_ttl);
699 }
700
701 /* Activate ini entries with values from the user config hash */
703}
704/* }}} */
705
706static int sapi_cgi_activate(void) /* {{{ */
707{
708 fcgi_request *request = (fcgi_request*) SG(server_context);
709 char *path, *doc_root, *server_name;
710 uint32_t path_len, doc_root_len, server_name_len;
711
712 /* PATH_TRANSLATED should be defined at this stage but better safe than sorry :) */
713 if (!SG(request_info).path_translated) {
714 return FAILURE;
715 }
716
718 /* Activate per-host-system-configuration defined in php.ini and stored into configuration_hash during startup */
719 server_name = FCGI_GETENV(request, "SERVER_NAME");
720 /* SERVER_NAME should also be defined at this stage..but better check it anyway */
721 if (server_name) {
722 server_name_len = strlen(server_name);
723 server_name = estrndup(server_name, server_name_len);
724 zend_str_tolower(server_name, server_name_len);
725 php_ini_activate_per_host_config(server_name, server_name_len);
726 efree(server_name);
727 }
728 }
729
731 (PG(user_ini_filename) && *PG(user_ini_filename))
732 ) {
733 /* Prepare search path */
734 path_len = strlen(SG(request_info).path_translated);
735
736 /* Make sure we have trailing slash! */
737 if (!IS_SLASH(SG(request_info).path_translated[path_len])) {
738 path = emalloc(path_len + 2);
739 memcpy(path, SG(request_info).path_translated, path_len + 1);
740 path_len = zend_dirname(path, path_len);
741 path[path_len++] = DEFAULT_SLASH;
742 } else {
743 path = estrndup(SG(request_info).path_translated, path_len);
744 path_len = zend_dirname(path, path_len);
745 }
746 path[path_len] = 0;
747
748 /* Activate per-dir-system-configuration defined in php.ini and stored into configuration_hash during startup */
749 php_ini_activate_per_dir_config(path, path_len); /* Note: for global settings sake we check from root to path */
750
751 /* Load and activate user ini files in path starting from DOCUMENT_ROOT */
752 if (PG(user_ini_filename) && *PG(user_ini_filename)) {
753 doc_root = FCGI_GETENV(request, "DOCUMENT_ROOT");
754 /* DOCUMENT_ROOT should also be defined at this stage..but better check it anyway */
755 if (doc_root) {
756 doc_root_len = strlen(doc_root);
757 if (doc_root_len > 0 && IS_SLASH(doc_root[doc_root_len - 1])) {
758 --doc_root_len;
759 }
760
761 php_cgi_ini_activate_user_config(path, path_len, doc_root, doc_root_len);
762 }
763 }
764
765 efree(path);
766 }
767
768 return SUCCESS;
769}
770/* }}} */
771
772static int sapi_cgi_deactivate(void) /* {{{ */
773{
774 /* flush only when SAPI was started. The reasons are:
775 1. SAPI Deactivate is called from two places: module init and request shutdown
776 2. When the first call occurs and the request is not set up, flush fails on FastCGI.
777 */
778 if (SG(sapi_started)) {
779 if (!parent && !fcgi_finish_request((fcgi_request*)SG(server_context), 0)) {
781 }
782 }
783 return SUCCESS;
784}
785/* }}} */
786
787static int php_cgi_startup(sapi_module_struct *sapi_module) /* {{{ */
788{
789 return php_module_startup(sapi_module, &cgi_module_entry);
790}
791/* }}} */
792
793/* {{{ sapi_module_struct cgi_sapi_module */
794static sapi_module_struct cgi_sapi_module = {
795 "fpm-fcgi", /* name */
796 "FPM/FastCGI", /* pretty name */
797
798 php_cgi_startup, /* startup */
799 php_module_shutdown_wrapper, /* shutdown */
800
801 sapi_cgi_activate, /* activate */
802 sapi_cgi_deactivate, /* deactivate */
803
804 sapi_cgibin_ub_write, /* unbuffered write */
805 sapi_cgibin_flush, /* flush */
806 NULL, /* get uid */
807 sapi_cgibin_getenv, /* getenv */
808
809 php_error, /* error handler */
810
811 NULL, /* header handler */
812 sapi_cgi_send_headers, /* send headers handler */
813 NULL, /* send header handler */
814
815 sapi_cgi_read_post, /* read POST data */
816 sapi_cgi_read_cookies, /* read Cookies */
817
818 sapi_cgi_register_variables, /* register server variables */
819 sapi_cgi_log_message, /* Log message */
820 NULL, /* Get request time */
821 NULL, /* Child terminate */
822
824};
825/* }}} */
826
827/* {{{ php_cgi_usage */
828static void php_cgi_usage(char *argv0)
829{
830 char *prog;
831
832 prog = strrchr(argv0, '/');
833 if (prog) {
834 prog++;
835 } else {
836 prog = "php";
837 }
838
839 php_printf( "Usage: %s [-n] [-e] [-h] [-i] [-m] [-v] [-t] [-p <prefix>] [-g <pid>] [-c <file>] [-d foo[=bar]] [-y <file>] [-D] [-F [-O]]\n"
840 " -c <path>|<file> Look for php.ini file in this directory\n"
841 " -n No php.ini file will be used\n"
842 " -d foo[=bar] Define INI entry foo with value 'bar'\n"
843 " -e Generate extended information for debugger/profiler\n"
844 " -h This help\n"
845 " -i PHP information\n"
846 " -m Show compiled in modules\n"
847 " -v Version number\n"
848 " -p, --prefix <dir>\n"
849 " Specify alternative prefix path to FastCGI process manager (default: %s).\n"
850 " -g, --pid <file>\n"
851 " Specify the PID file location.\n"
852 " -y, --fpm-config <file>\n"
853 " Specify alternative path to FastCGI process manager config file.\n"
854 " -t, --test Test FPM configuration and exit\n"
855 " -D, --daemonize force to run in background, and ignore daemonize option from config file\n"
856 " -F, --nodaemonize\n"
857 " force to stay in foreground, and ignore daemonize option from config file\n"
858 " -O, --force-stderr\n"
859 " force output to stderr in nodaemonize even if stderr is not a TTY\n"
860 " -R, --allow-to-run-as-root\n"
861 " Allow pool to run as root (disabled by default)\n",
862 prog, PHP_PREFIX);
863}
864/* }}} */
865
866/* {{{ is_valid_path
867 *
868 * some server configurations allow '..' to slip through in the
869 * translated path. We'll just refuse to handle such a path.
870 */
871static int is_valid_path(const char *path)
872{
873 const char *p;
874
875 if (!path) {
876 return 0;
877 }
878 p = strstr(path, "..");
879 if (p) {
880 if ((p == path || IS_SLASH(*(p-1))) &&
881 (*(p+2) == 0 || IS_SLASH(*(p+2)))
882 ) {
883 return 0;
884 }
885 while (1) {
886 p = strstr(p+1, "..");
887 if (!p) {
888 break;
889 }
890 if (IS_SLASH(*(p-1)) &&
891 (*(p+2) == 0 || IS_SLASH(*(p+2)))
892 ) {
893 return 0;
894 }
895 }
896 }
897 return 1;
898}
899/* }}} */
900
901/* {{{ init_request_info
902
903 initializes request_info structure
904
905 specifically in this section we handle proper translations
906 for:
907
908 PATH_INFO
909 derived from the portion of the URI path following
910 the script name but preceding any query data
911 may be empty
912
913 PATH_TRANSLATED
914 derived by taking any path-info component of the
915 request URI and performing any virtual-to-physical
916 translation appropriate to map it onto the server's
917 document repository structure
918
919 empty if PATH_INFO is empty
920
921 The env var PATH_TRANSLATED **IS DIFFERENT** than the
922 request_info.path_translated variable, the latter should
923 match SCRIPT_FILENAME instead.
924
925 SCRIPT_NAME
926 set to a URL path that could identify the CGI script
927 rather than the interpreter. PHP_SELF is set to this
928
929 REQUEST_URI
930 uri section following the domain:port part of a URI
931
932 SCRIPT_FILENAME
933 The virtual-to-physical translation of SCRIPT_NAME (as per
934 PATH_TRANSLATED)
935
936 These settings are documented at
937 http://cgi-spec.golux.com/
938
939
940 Based on the following URL request:
941
942 http://localhost/info.php/test?a=b
943
944 should produce, which btw is the same as if
945 we were running under mod_cgi on apache (ie. not
946 using ScriptAlias directives):
947
948 PATH_INFO=/test
949 PATH_TRANSLATED=/docroot/test
950 SCRIPT_NAME=/info.php
951 REQUEST_URI=/info.php/test?a=b
952 SCRIPT_FILENAME=/docroot/info.php
953 QUERY_STRING=a=b
954
955 but what we get is (cgi/mod_fastcgi under apache):
956
957 PATH_INFO=/info.php/test
958 PATH_TRANSLATED=/docroot/info.php/test
959 SCRIPT_NAME=/php/php-cgi (from the Action setting I suppose)
960 REQUEST_URI=/info.php/test?a=b
961 SCRIPT_FILENAME=/path/to/php/bin/php-cgi (Action setting translated)
962 QUERY_STRING=a=b
963
964 Comments in the code below refer to using the above URL in a request
965
966 */
967static void init_request_info(void)
968{
969 fcgi_request *request = (fcgi_request*) SG(server_context);
970 char *env_script_filename = FCGI_GETENV(request, "SCRIPT_FILENAME");
971 char *env_path_translated = FCGI_GETENV(request, "PATH_TRANSLATED");
972 char *script_path_translated = env_script_filename;
973 char *ini;
974 int apache_was_here = 0;
975
976 /* some broken servers do not have script_filename or argv0
977 * an example, IIS configured in some ways. then they do more
978 * broken stuff and set path_translated to the cgi script location */
979 if (!script_path_translated && env_path_translated) {
980 script_path_translated = env_path_translated;
981 }
982
983 /* initialize the defaults */
984 SG(request_info).path_translated = NULL;
985 SG(request_info).request_method = FCGI_GETENV(request, "REQUEST_METHOD");
986 SG(request_info).proto_num = 1000;
987 SG(request_info).query_string = NULL;
988 SG(request_info).request_uri = NULL;
989 SG(request_info).content_type = NULL;
990 SG(request_info).content_length = 0;
991 SG(sapi_headers).http_response_code = 200;
992
993 /* if script_path_translated is not set, then there is no point to carry on
994 * as the response is 404 and there is no further processing. */
995 if (script_path_translated) {
996 const char *auth;
997 char *content_length = FCGI_GETENV(request, "CONTENT_LENGTH");
998 char *content_type = FCGI_GETENV(request, "CONTENT_TYPE");
999 char *env_path_info = FCGI_GETENV(request, "PATH_INFO");
1000 char *env_script_name = FCGI_GETENV(request, "SCRIPT_NAME");
1001
1002 /* Hack for buggy IIS that sets incorrect PATH_INFO */
1003 char *env_server_software = FCGI_GETENV(request, "SERVER_SOFTWARE");
1004 if (env_server_software &&
1005 env_script_name &&
1006 env_path_info &&
1007 strncmp(env_server_software, "Microsoft-IIS", sizeof("Microsoft-IIS") - 1) == 0 &&
1008 strncmp(env_path_info, env_script_name, strlen(env_script_name)) == 0
1009 ) {
1010 env_path_info = FCGI_PUTENV(request, "ORIG_PATH_INFO", env_path_info);
1011 env_path_info += strlen(env_script_name);
1012 if (*env_path_info == 0) {
1013 env_path_info = NULL;
1014 }
1015 env_path_info = FCGI_PUTENV(request, "PATH_INFO", env_path_info);
1016 }
1017
1018#define APACHE_PROXY_FCGI_PREFIX "proxy:fcgi://"
1019#define APACHE_PROXY_BALANCER_PREFIX "proxy:balancer://"
1020 /* Fix proxy URLs in SCRIPT_FILENAME generated by Apache mod_proxy_fcgi and mod_proxy_balancer:
1021 * proxy:fcgi://localhost:9000/some-dir/info.php/test?foo=bar
1022 * proxy:balancer://localhost:9000/some-dir/info.php/test?foo=bar
1023 * should be changed to:
1024 * /some-dir/info.php/test
1025 * See: http://bugs.php.net/bug.php?id=54152
1026 * http://bugs.php.net/bug.php?id=62172
1027 * https://issues.apache.org/bugzilla/show_bug.cgi?id=50851
1028 */
1029 if (env_script_filename &&
1030 strncasecmp(env_script_filename, APACHE_PROXY_FCGI_PREFIX, sizeof(APACHE_PROXY_FCGI_PREFIX) - 1) == 0) {
1031 /* advance to first character of hostname */
1032 char *p = env_script_filename + (sizeof(APACHE_PROXY_FCGI_PREFIX) - 1);
1033 while (*p != '\0' && *p != '/') {
1034 p++; /* move past hostname and port */
1035 }
1036 if (*p != '\0') {
1037 /* Copy path portion in place to avoid memory leak. Note
1038 * that this also affects what script_path_translated points
1039 * to. */
1040 memmove(env_script_filename, p, strlen(p) + 1);
1041 apache_was_here = 1;
1042 }
1043 /* ignore query string if sent by Apache (RewriteRule) */
1044 p = strchr(env_script_filename, '?');
1045 if (p) {
1046 *p =0;
1047 }
1048 }
1049
1050 if (env_script_filename &&
1051 strncasecmp(env_script_filename, APACHE_PROXY_BALANCER_PREFIX, sizeof(APACHE_PROXY_BALANCER_PREFIX) - 1) == 0) {
1052 /* advance to first character of hostname */
1053 char *p = env_script_filename + (sizeof(APACHE_PROXY_BALANCER_PREFIX) - 1);
1054 while (*p != '\0' && *p != '/') {
1055 p++; /* move past hostname and port */
1056 }
1057 if (*p != '\0') {
1058 /* Copy path portion in place to avoid memory leak. Note
1059 * that this also affects what script_path_translated points
1060 * to. */
1061 memmove(env_script_filename, p, strlen(p) + 1);
1062 apache_was_here = 1;
1063 }
1064 /* ignore query string if sent by Apache (RewriteRule) */
1065 p = strchr(env_script_filename, '?');
1066 if (p) {
1067 *p =0;
1068 }
1069 }
1070
1071 if (CGIG(fix_pathinfo)) {
1072 struct stat st;
1073 char *real_path = NULL;
1074 char *env_redirect_url = FCGI_GETENV(request, "REDIRECT_URL");
1075 char *env_document_root = FCGI_GETENV(request, "DOCUMENT_ROOT");
1076 char *orig_path_translated = env_path_translated;
1077 char *orig_path_info = env_path_info;
1078 char *orig_script_name = env_script_name;
1079 char *orig_script_filename = env_script_filename;
1080 int script_path_translated_len;
1081
1082 if (!env_document_root && PG(doc_root)) {
1083 env_document_root = FCGI_PUTENV(request, "DOCUMENT_ROOT", PG(doc_root));
1084 }
1085
1086 if (!apache_was_here && env_path_translated != NULL && env_redirect_url != NULL &&
1087 env_path_translated != script_path_translated &&
1088 strcmp(env_path_translated, script_path_translated) != 0) {
1089 /*
1090 * pretty much apache specific. If we have a redirect_url
1091 * then our script_filename and script_name point to the
1092 * php executable
1093 * we don't want to do this for the new mod_proxy_fcgi approach,
1094 * where redirect_url may also exist but the below will break
1095 * with rewrites to PATH_INFO, hence the !apache_was_here check
1096 */
1097 script_path_translated = env_path_translated;
1098 /* we correct SCRIPT_NAME now in case we don't have PATH_INFO */
1099 env_script_name = env_redirect_url;
1100 }
1101
1102#ifdef __riscos__
1103 /* Convert path to unix format*/
1104 __riscosify_control |= __RISCOSIFY_DONT_CHECK_DIR;
1105 script_path_translated = __unixify(script_path_translated, 0, NULL, 1, 0);
1106#endif
1107
1108 /*
1109 * if the file doesn't exist, try to extract PATH_INFO out
1110 * of it by stat'ing back through the '/'
1111 * this fixes url's like /info.php/test
1112 */
1113 if (script_path_translated &&
1114 (script_path_translated_len = strlen(script_path_translated)) > 0 &&
1115 (script_path_translated[script_path_translated_len-1] == '/' ||
1116 (real_path = tsrm_realpath(script_path_translated, NULL)) == NULL)
1117 ) {
1118 char *pt = estrndup(script_path_translated, script_path_translated_len);
1119 int len = script_path_translated_len;
1120 char *ptr;
1121
1122 if (pt) {
1123 while ((ptr = strrchr(pt, '/')) || (ptr = strrchr(pt, '\\'))) {
1124 *ptr = 0;
1125 if (stat(pt, &st) == 0 && S_ISREG(st.st_mode)) {
1126 /*
1127 * okay, we found the base script!
1128 * work out how many chars we had to strip off;
1129 * then we can modify PATH_INFO
1130 * accordingly
1131 *
1132 * we now have the makings of
1133 * PATH_INFO=/test
1134 * SCRIPT_FILENAME=/docroot/info.php
1135 *
1136 * we now need to figure out what docroot is.
1137 * if DOCUMENT_ROOT is set, this is easy, otherwise,
1138 * we have to play the game of hide and seek to figure
1139 * out what SCRIPT_NAME should be
1140 */
1141 int ptlen = strlen(pt);
1142 int slen = len - ptlen;
1143 int pilen = env_path_info ? strlen(env_path_info) : 0;
1144 int tflag = 0;
1145 char *path_info;
1146 if (apache_was_here) {
1147 /* recall that PATH_INFO won't exist */
1148 path_info = script_path_translated + ptlen;
1149 tflag = (slen != 0 && (!orig_path_info || strcmp(orig_path_info, path_info) != 0));
1150 } else {
1151 path_info = (env_path_info && pilen > slen) ? env_path_info + pilen - slen : NULL;
1152 tflag = path_info && (orig_path_info != path_info);
1153 }
1154
1155 if (tflag) {
1156 char *decoded_path_info = NULL;
1157 if (orig_path_info) {
1158 char old;
1159
1160 FCGI_PUTENV(request, "ORIG_PATH_INFO", orig_path_info);
1161 old = path_info[0];
1162 path_info[0] = 0;
1163 if (!orig_script_name ||
1164 strcmp(orig_script_name, env_path_info) != 0) {
1165 if (orig_script_name) {
1166 FCGI_PUTENV(request, "ORIG_SCRIPT_NAME", orig_script_name);
1167 }
1168 SG(request_info).request_uri = FCGI_PUTENV(request, "SCRIPT_NAME", env_path_info);
1169 } else {
1170 SG(request_info).request_uri = orig_script_name;
1171 }
1172 path_info[0] = old;
1173 } else if (apache_was_here && env_script_name) {
1174 /* Using mod_proxy_fcgi and ProxyPass, apache cannot set PATH_INFO
1175 * As we can extract PATH_INFO from PATH_TRANSLATED
1176 * it is probably also in SCRIPT_NAME and need to be removed
1177 */
1178 size_t decoded_path_info_len = 0;
1179 if (strchr(path_info, '%')) {
1180 decoded_path_info = estrdup(path_info);
1181 decoded_path_info_len = php_raw_url_decode(decoded_path_info, strlen(path_info));
1182 }
1183 size_t snlen = strlen(env_script_name);
1184 size_t env_script_file_info_start = 0;
1185 if (
1186 (
1187 snlen > slen &&
1188 !strcmp(env_script_name + (env_script_file_info_start = snlen - slen), path_info)
1189 ) ||
1190 (
1191 decoded_path_info &&
1192 snlen > decoded_path_info_len &&
1193 !strcmp(env_script_name + (env_script_file_info_start = snlen - decoded_path_info_len), decoded_path_info)
1194 )
1195 ) {
1196 FCGI_PUTENV(request, "ORIG_SCRIPT_NAME", orig_script_name);
1197 env_script_name[env_script_file_info_start] = 0;
1198 SG(request_info).request_uri = FCGI_PUTENV(request, "SCRIPT_NAME", env_script_name);
1199 }
1200 }
1201 if (decoded_path_info) {
1202 env_path_info = FCGI_PUTENV(request, "PATH_INFO", decoded_path_info);
1203 efree(decoded_path_info);
1204 } else {
1205 env_path_info = FCGI_PUTENV(request, "PATH_INFO", path_info);
1206 }
1207 }
1208 if (!orig_script_filename ||
1209 strcmp(orig_script_filename, pt) != 0) {
1210 if (orig_script_filename) {
1211 FCGI_PUTENV(request, "ORIG_SCRIPT_FILENAME", orig_script_filename);
1212 }
1213 script_path_translated = FCGI_PUTENV(request, "SCRIPT_FILENAME", pt);
1214 }
1215
1216 /* figure out docroot
1217 * SCRIPT_FILENAME minus SCRIPT_NAME
1218 */
1219 if (env_document_root) {
1220 int l = strlen(env_document_root);
1221 int path_translated_len = 0;
1222 char *path_translated = NULL;
1223
1224 if (l && env_document_root[l - 1] == '/') {
1225 --l;
1226 }
1227
1228 /* we have docroot, so we should have:
1229 * DOCUMENT_ROOT=/docroot
1230 * SCRIPT_FILENAME=/docroot/info.php
1231 */
1232
1233 /* PATH_TRANSLATED = DOCUMENT_ROOT + PATH_INFO */
1234 path_translated_len = l + (env_path_info ? strlen(env_path_info) : 0);
1235 path_translated = (char *) emalloc(path_translated_len + 1);
1236 memcpy(path_translated, env_document_root, l);
1237 if (env_path_info) {
1238 memcpy(path_translated + l, env_path_info, (path_translated_len - l));
1239 }
1240 path_translated[path_translated_len] = '\0';
1241 if (orig_path_translated) {
1242 FCGI_PUTENV(request, "ORIG_PATH_TRANSLATED", orig_path_translated);
1243 }
1244 env_path_translated = FCGI_PUTENV(request, "PATH_TRANSLATED", path_translated);
1245 efree(path_translated);
1246 } else if ( env_script_name &&
1247 strstr(pt, env_script_name)
1248 ) {
1249 /* PATH_TRANSLATED = PATH_TRANSLATED - SCRIPT_NAME + PATH_INFO */
1250 int ptlen = strlen(pt) - strlen(env_script_name);
1251 int path_translated_len = ptlen + (env_path_info ? strlen(env_path_info) : 0);
1252 char *path_translated = NULL;
1253
1254 path_translated = (char *) emalloc(path_translated_len + 1);
1255 memcpy(path_translated, pt, ptlen);
1256 if (env_path_info) {
1257 memcpy(path_translated + ptlen, env_path_info, path_translated_len - ptlen);
1258 }
1259 path_translated[path_translated_len] = '\0';
1260 if (orig_path_translated) {
1261 FCGI_PUTENV(request, "ORIG_PATH_TRANSLATED", orig_path_translated);
1262 }
1263 env_path_translated = FCGI_PUTENV(request, "PATH_TRANSLATED", path_translated);
1264 efree(path_translated);
1265 }
1266 break;
1267 }
1268 }
1269 } else {
1270 ptr = NULL;
1271 }
1272 if (!ptr) {
1273 /*
1274 * if we stripped out all the '/' and still didn't find
1275 * a valid path... we will fail, badly. of course we would
1276 * have failed anyway... we output 'no input file' now.
1277 */
1278 if (orig_script_filename) {
1279 FCGI_PUTENV(request, "ORIG_SCRIPT_FILENAME", orig_script_filename);
1280 }
1281 script_path_translated = FCGI_PUTENV(request, "SCRIPT_FILENAME", NULL);
1282 SG(sapi_headers).http_response_code = 404;
1283 }
1284 if (!SG(request_info).request_uri) {
1285 if (!orig_script_name ||
1286 strcmp(orig_script_name, env_script_name) != 0) {
1287 if (orig_script_name) {
1288 FCGI_PUTENV(request, "ORIG_SCRIPT_NAME", orig_script_name);
1289 }
1290 SG(request_info).request_uri = FCGI_PUTENV(request, "SCRIPT_NAME", env_script_name);
1291 } else {
1292 SG(request_info).request_uri = orig_script_name;
1293 }
1294 }
1295 if (pt) {
1296 efree(pt);
1297 }
1298 } else {
1299 /* make sure original values are remembered in ORIG_ copies if we've changed them */
1300 if (!orig_script_filename ||
1301 (script_path_translated != orig_script_filename &&
1302 strcmp(script_path_translated, orig_script_filename) != 0)) {
1303 if (orig_script_filename) {
1304 FCGI_PUTENV(request, "ORIG_SCRIPT_FILENAME", orig_script_filename);
1305 }
1306 script_path_translated = FCGI_PUTENV(request, "SCRIPT_FILENAME", script_path_translated);
1307 }
1308 if (!apache_was_here && env_redirect_url) {
1309 /* if we used PATH_TRANSLATED to work around Apache mod_fastcgi (but not mod_proxy_fcgi,
1310 * hence !apache_was_here) weirdness, strip info accordingly */
1311 if (orig_path_info) {
1312 FCGI_PUTENV(request, "ORIG_PATH_INFO", orig_path_info);
1313 FCGI_PUTENV(request, "PATH_INFO", NULL);
1314 }
1315 if (orig_path_translated) {
1316 FCGI_PUTENV(request, "ORIG_PATH_TRANSLATED", orig_path_translated);
1317 FCGI_PUTENV(request, "PATH_TRANSLATED", NULL);
1318 }
1319 }
1320 if (env_script_name != orig_script_name) {
1321 if (orig_script_name) {
1322 FCGI_PUTENV(request, "ORIG_SCRIPT_NAME", orig_script_name);
1323 }
1324 SG(request_info).request_uri = FCGI_PUTENV(request, "SCRIPT_NAME", env_script_name);
1325 } else {
1326 SG(request_info).request_uri = env_script_name;
1327 }
1328 efree(real_path);
1329 }
1330 } else {
1331 /* pre 4.3 behaviour, shouldn't be used but provides BC */
1332 if (env_path_info) {
1333 SG(request_info).request_uri = env_path_info;
1334 } else {
1335 SG(request_info).request_uri = env_script_name;
1336 }
1337 if (!CGIG(discard_path) && env_path_translated) {
1338 script_path_translated = env_path_translated;
1339 }
1340 }
1341
1342 if (is_valid_path(script_path_translated)) {
1343 SG(request_info).path_translated = estrdup(script_path_translated);
1344 }
1345
1346 /* FIXME - Work out proto_num here */
1347 SG(request_info).query_string = FCGI_GETENV(request, "QUERY_STRING");
1348 SG(request_info).content_type = (content_type ? content_type : "" );
1349 SG(request_info).content_length = (content_length ? atol(content_length) : 0);
1350
1351 /* The CGI RFC allows servers to pass on unvalidated Authorization data */
1352 auth = FCGI_GETENV(request, "HTTP_AUTHORIZATION");
1354 }
1355
1356 /* INI stuff */
1357 ini = FCGI_GETENV(request, "PHP_VALUE");
1358 if (ini) {
1359 int mode = ZEND_INI_USER;
1360 char *tmp;
1361 spprintf(&tmp, 0, "%s\n", ini);
1363 efree(tmp);
1364 }
1365
1366 ini = FCGI_GETENV(request, "PHP_ADMIN_VALUE");
1367 if (ini) {
1368 int mode = ZEND_INI_SYSTEM;
1369 char *tmp;
1370 spprintf(&tmp, 0, "%s\n", ini);
1372 efree(tmp);
1373 }
1374}
1375/* }}} */
1376
1377static fcgi_request *fpm_init_request(int listen_fd) /* {{{ */ {
1378 fcgi_request *req = fcgi_init_request(listen_fd,
1382 return req;
1383}
1384/* }}} */
1385
1386static void fastcgi_ini_parser(zval *arg1, zval *arg2, zval *arg3, int callback_type, void *arg) /* {{{ */
1387{
1388 int *mode = (int *)arg;
1389 char *key;
1390 char *value = NULL;
1391 struct key_value_s kv;
1392
1393 if (!mode || !arg1) return;
1394
1395 if (callback_type != ZEND_INI_PARSER_ENTRY) {
1396 zlog(ZLOG_ERROR, "Passing INI directive through FastCGI: only classic entries are allowed");
1397 return;
1398 }
1399
1400 key = Z_STRVAL_P(arg1);
1401
1402 if (!key || strlen(key) < 1) {
1403 zlog(ZLOG_ERROR, "Passing INI directive through FastCGI: empty key");
1404 return;
1405 }
1406
1407 if (arg2) {
1409 }
1410
1411 if (!value) {
1412 zlog(ZLOG_ERROR, "Passing INI directive through FastCGI: empty value for key '%s'", key);
1413 return;
1414 }
1415
1416 kv.key = key;
1417 kv.value = value;
1418 kv.next = NULL;
1419 if (fpm_php_apply_defines_ex(&kv, *mode) == -1) {
1420 zlog(ZLOG_ERROR, "Passing INI directive through FastCGI: unable to set '%s'", key);
1421 }
1422}
1423/* }}} */
1424
1426 STD_PHP_INI_BOOLEAN("cgi.rfc2616_headers", "0", PHP_INI_ALL, OnUpdateBool, rfc2616_headers, php_cgi_globals_struct, php_cgi_globals)
1427 STD_PHP_INI_BOOLEAN("cgi.nph", "0", PHP_INI_ALL, OnUpdateBool, nph, php_cgi_globals_struct, php_cgi_globals)
1428 STD_PHP_INI_BOOLEAN("cgi.force_redirect", "1", PHP_INI_SYSTEM, OnUpdateBool, force_redirect, php_cgi_globals_struct, php_cgi_globals)
1429 STD_PHP_INI_ENTRY("cgi.redirect_status_env", NULL, PHP_INI_SYSTEM, OnUpdateString, redirect_status_env, php_cgi_globals_struct, php_cgi_globals)
1430 STD_PHP_INI_BOOLEAN("cgi.fix_pathinfo", "1", PHP_INI_SYSTEM, OnUpdateBool, fix_pathinfo, php_cgi_globals_struct, php_cgi_globals)
1431 STD_PHP_INI_BOOLEAN("cgi.discard_path", "0", PHP_INI_SYSTEM, OnUpdateBool, discard_path, php_cgi_globals_struct, php_cgi_globals)
1432 STD_PHP_INI_BOOLEAN("fastcgi.logging", "1", PHP_INI_SYSTEM, OnUpdateBool, fcgi_logging, php_cgi_globals_struct, php_cgi_globals)
1433 STD_PHP_INI_ENTRY("fastcgi.error_header", NULL, PHP_INI_SYSTEM, OnUpdateString, error_header, php_cgi_globals_struct, php_cgi_globals)
1434 STD_PHP_INI_ENTRY("fpm.config", NULL, PHP_INI_SYSTEM, OnUpdateString, fpm_config, php_cgi_globals_struct, php_cgi_globals)
1436
1437/* {{{ php_cgi_globals_ctor */
1438static void php_cgi_globals_ctor(php_cgi_globals_struct *php_cgi_globals)
1439{
1440 php_cgi_globals->rfc2616_headers = 0;
1441 php_cgi_globals->nph = 0;
1442 php_cgi_globals->force_redirect = 1;
1443 php_cgi_globals->redirect_status_env = NULL;
1444 php_cgi_globals->fix_pathinfo = 1;
1445 php_cgi_globals->discard_path = 0;
1446 php_cgi_globals->fcgi_logging = 1;
1447 php_cgi_globals->fcgi_logging_request_started = false;
1448 zend_hash_init(&php_cgi_globals->user_config_cache, 0, NULL, user_config_cache_entry_dtor, 1);
1449 php_cgi_globals->error_header = NULL;
1450 php_cgi_globals->fpm_config = NULL;
1451}
1452/* }}} */
1453
1454/* {{{ PHP_MINIT_FUNCTION */
1455static PHP_MINIT_FUNCTION(cgi)
1456{
1457#ifdef ZTS
1458 ts_allocate_id(&php_cgi_globals_id, sizeof(php_cgi_globals_struct), (ts_allocate_ctor) php_cgi_globals_ctor, NULL);
1459#else
1460 php_cgi_globals_ctor(&php_cgi_globals);
1461#endif
1463 return SUCCESS;
1464}
1465/* }}} */
1466
1467/* {{{ PHP_MSHUTDOWN_FUNCTION */
1468static PHP_MSHUTDOWN_FUNCTION(cgi)
1469{
1470 zend_hash_destroy(&CGIG(user_config_cache));
1471
1473 return SUCCESS;
1474}
1475/* }}} */
1476
1477/* {{{ PHP_MINFO_FUNCTION */
1478static PHP_MINFO_FUNCTION(cgi)
1479{
1481 php_info_print_table_row(2, "php-fpm", "active");
1483
1485}
1486/* }}} */
1487
1489{
1490 fcgi_request *request = (fcgi_request*) SG(server_context);
1491
1495
1496 if (!fcgi_is_closed(request)) {
1498 php_header();
1499
1500 fcgi_end(request);
1501 fcgi_close(request, 0, 0);
1503 }
1504
1506
1507}
1508/* }}} */
1509
1511{
1512 fcgi_request *request;
1513
1515 RETURN_THROWS();
1516 }
1517
1519 if ((request = (fcgi_request*) SG(server_context))) {
1521 }
1522} /* }}} */
1523
1524/* {{{ Returns the status of the fastcgi process manager */
1526{
1528 RETURN_THROWS();
1529 }
1530
1533 }
1534}
1535/* }}} */
1536
1537static zend_module_entry cgi_module_entry = {
1539 "cgi-fcgi",
1540 ext_functions,
1541 PHP_MINIT(cgi),
1542 PHP_MSHUTDOWN(cgi),
1543 NULL,
1544 NULL,
1545 PHP_MINFO(cgi),
1548};
1549
1550/* {{{ main */
1551int main(int argc, char *argv[])
1552{
1553 int exit_status = FPM_EXIT_OK;
1554 int cgi = 0, c, use_extended_info = 0;
1555 zend_file_handle file_handle;
1556
1557 /* temporary locals */
1558 int orig_optind = php_optind;
1559 char *orig_optarg = php_optarg;
1560 struct php_ini_builder ini_builder;
1561 /* end of temporary locals */
1562
1563 int max_requests = 0;
1564 int requests = 0;
1565 int fcgi_fd = 0;
1566 fcgi_request *request;
1567 char *fpm_config = NULL;
1568 char *fpm_prefix = NULL;
1569 char *fpm_pid = NULL;
1570 int test_conf = 0;
1571 int force_daemon = -1;
1572 int force_stderr = 0;
1573 int php_information = 0;
1574 int php_allow_to_run_as_root = 0;
1575#if ZEND_RC_DEBUG
1576 bool old_rc_debug;
1577#endif
1578
1579#if defined(SIGPIPE) && defined(SIG_IGN)
1580 signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE in standalone mode so
1581 that sockets created via fsockopen()
1582 don't kill PHP if the remote site
1583 closes it. in apache|apxs mode apache
1584 does that for us! thies@thieso.net
1585 20000419 */
1586
1587 if (0 > fpm_signals_init_mask() || 0 > fpm_signals_block()) {
1588 zlog(ZLOG_WARNING, "Could die in the case of too early reload signal");
1589 }
1590 zlog(ZLOG_DEBUG, "Blocked some signals");
1591#endif
1592
1593#ifdef ZTS
1594 php_tsrm_startup();
1595#endif
1596
1598
1599 sapi_startup(&cgi_sapi_module);
1600 cgi_sapi_module.php_ini_path_override = NULL;
1601 cgi_sapi_module.php_ini_ignore_cwd = 1;
1602
1603#ifndef HAVE_ATTRIBUTE_WEAK
1604 fcgi_set_logger(fpm_fcgi_log);
1605#endif
1606
1607 fcgi_init();
1608
1609 php_ini_builder_init(&ini_builder);
1610
1611 while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) {
1612 switch (c) {
1613 case 'c':
1614 if (cgi_sapi_module.php_ini_path_override) {
1615 free(cgi_sapi_module.php_ini_path_override);
1616 }
1617 cgi_sapi_module.php_ini_path_override = strdup(php_optarg);
1618 break;
1619
1620 case 'n':
1621 cgi_sapi_module.php_ini_ignore = 1;
1622 break;
1623
1624 case 'd':
1625 /* define ini entries on command line */
1626 php_ini_builder_define(&ini_builder, php_optarg);
1627 /* main can terminate without finishing or deiniting the ini builder, call finish each iteration to avoid leaking the buffer */
1628 cgi_sapi_module.ini_entries = php_ini_builder_finish(&ini_builder);
1629 break;
1630
1631 case 'y':
1632 fpm_config = php_optarg;
1633 break;
1634
1635 case 'p':
1636 fpm_prefix = php_optarg;
1637 break;
1638
1639 case 'g':
1640 fpm_pid = php_optarg;
1641 break;
1642
1643 case 'e': /* enable extended info output */
1644 use_extended_info = 1;
1645 break;
1646
1647 case 't':
1648 test_conf++;
1649 break;
1650
1651 case 'm': /* list compiled in modules */
1652 cgi_sapi_module.startup(&cgi_sapi_module);
1654 SG(headers_sent) = 1;
1655 php_printf("[PHP Modules]\n");
1656 print_modules();
1657 php_printf("\n[Zend Modules]\n");
1658 print_extensions();
1659 php_printf("\n");
1662 fcgi_shutdown();
1663 exit_status = FPM_EXIT_OK;
1664 goto out;
1665
1666 case 'i': /* php info & quit */
1667 php_information = 1;
1668 break;
1669
1670 case 'R': /* allow to run as root */
1671 php_allow_to_run_as_root = 1;
1672 break;
1673
1674 case 'D': /* daemonize */
1675 force_daemon = 1;
1676 break;
1677
1678 case 'F': /* nodaemonize */
1679 force_daemon = 0;
1680 break;
1681
1682 case 'O': /* force stderr even on non tty */
1683 force_stderr = 1;
1684 break;
1685
1686 default:
1687 case 'h':
1688 case '?':
1690 cgi_sapi_module.startup(&cgi_sapi_module);
1692 SG(headers_sent) = 1;
1693 php_cgi_usage(argv[0]);
1696 fcgi_shutdown();
1697 exit_status = (c != PHP_GETOPT_INVALID_ARG) ? FPM_EXIT_OK : FPM_EXIT_USAGE;
1698 goto out;
1699
1700 case 'v': /* show php version & quit */
1701 cgi_sapi_module.startup(&cgi_sapi_module);
1702 if (php_request_startup() == FAILURE) {
1703 SG(server_context) = NULL;
1705 return FPM_EXIT_SOFTWARE;
1706 }
1707 SG(headers_sent) = 1;
1708 SG(request_info).no_headers = 1;
1709
1711 php_request_shutdown((void *) 0);
1712 fcgi_shutdown();
1713 exit_status = FPM_EXIT_OK;
1714 goto out;
1715 }
1716 }
1717
1718 cgi_sapi_module.ini_entries = php_ini_builder_finish(&ini_builder);
1719
1720 if (php_information) {
1721 cgi_sapi_module.phpinfo_as_text = 1;
1722 cgi_sapi_module.startup(&cgi_sapi_module);
1723 if (php_request_startup() == FAILURE) {
1724 SG(server_context) = NULL;
1726 return FPM_EXIT_SOFTWARE;
1727 }
1728 SG(headers_sent) = 1;
1729 SG(request_info).no_headers = 1;
1730 php_print_info(0xFFFFFFFF);
1731 php_request_shutdown((void *) 0);
1732 fcgi_shutdown();
1733 exit_status = FPM_EXIT_OK;
1734 goto out;
1735 }
1736
1737 /* No other args are permitted here as there is no interactive mode */
1738 if (argc != php_optind) {
1739 cgi_sapi_module.startup(&cgi_sapi_module);
1741 SG(headers_sent) = 1;
1742 php_cgi_usage(argv[0]);
1745 fcgi_shutdown();
1746 exit_status = FPM_EXIT_USAGE;
1747 goto out;
1748 }
1749
1750 php_optind = orig_optind;
1751 php_optarg = orig_optarg;
1752
1753#ifdef ZTS
1754 SG(request_info).path_translated = NULL;
1755#endif
1756
1757 cgi_sapi_module.additional_functions = NULL;
1758 cgi_sapi_module.executable_location = argv[0];
1759
1760 /* startup after we get the above ini override se we get things right */
1761 if (cgi_sapi_module.startup(&cgi_sapi_module) == FAILURE) {
1762#ifdef ZTS
1763 tsrm_shutdown();
1764#endif
1765 return FPM_EXIT_SOFTWARE;
1766 }
1767
1768 if (use_extended_info) {
1769 CG(compiler_options) |= ZEND_COMPILE_EXTENDED_INFO;
1770 }
1771
1772 /* check force_cgi after startup, so we have proper output */
1773 if (cgi && CGIG(force_redirect)) {
1774 /* Apache will generate REDIRECT_STATUS,
1775 * Netscape and redirect.so will generate HTTP_REDIRECT_STATUS.
1776 * redirect.so and installation instructions available from
1777 * http://www.koehntopp.de/php.
1778 * -- kk@netuse.de
1779 */
1780 if (!getenv("REDIRECT_STATUS") &&
1781 !getenv ("HTTP_REDIRECT_STATUS") &&
1782 /* this is to allow a different env var to be configured
1783 * in case some server does something different than above */
1784 (!CGIG(redirect_status_env) || !getenv(CGIG(redirect_status_env)))
1785 ) {
1786 zend_try {
1787 SG(sapi_headers).http_response_code = 400;
1788 PUTS("<b>Security Alert!</b> The PHP CGI cannot be accessed directly.\n\n\
1789<p>This PHP CGI binary was compiled with force-cgi-redirect enabled. This\n\
1790means that a page will only be served up if the REDIRECT_STATUS CGI variable is\n\
1791set, e.g. via an Apache Action directive.</p>\n\
1792<p>For more information as to <i>why</i> this behaviour exists, see the <a href=\"http://php.net/security.cgi-bin\">\
1793manual page for CGI security</a>.</p>\n\
1794<p>For more information about changing this behaviour or re-enabling this webserver,\n\
1795consult the installation file that came with this distribution, or visit \n\
1796<a href=\"http://php.net/install.windows\">the manual page</a>.</p>\n");
1797 } zend_catch {
1798 } zend_end_try();
1799#if defined(ZTS) && !PHP_DEBUG
1800 /* XXX we're crashing here in msvc6 debug builds at
1801 * php_message_handler_for_zend:839 because
1802 * SG(request_info).path_translated is an invalid pointer.
1803 * It still happens even though I set it to null, so something
1804 * weird is going on.
1805 */
1806 tsrm_shutdown();
1807#endif
1808 return FPM_EXIT_SOFTWARE;
1809 }
1810 }
1811
1812#if ZEND_RC_DEBUG
1813 old_rc_debug = zend_rc_debug;
1814 zend_rc_debug = 0;
1815#endif
1816
1817 enum fpm_init_return_status ret = fpm_init(argc, argv, fpm_config ? fpm_config : CGIG(fpm_config), fpm_prefix, fpm_pid, test_conf, php_allow_to_run_as_root, force_daemon, force_stderr);
1818
1819#if ZEND_RC_DEBUG
1820 zend_rc_debug = old_rc_debug;
1821#endif
1822
1823 if (ret == FPM_INIT_ERROR) {
1824 if (fpm_globals.send_config_pipe[1]) {
1825 int writeval = 0;
1826 zlog(ZLOG_DEBUG, "Sending \"0\" (error) to parent via fd=%d", fpm_globals.send_config_pipe[1]);
1827 zend_quiet_write(fpm_globals.send_config_pipe[1], &writeval, sizeof(writeval));
1828 close(fpm_globals.send_config_pipe[1]);
1829 }
1830 exit_status = FPM_EXIT_CONFIG;
1831 goto out;
1832 } else if (ret == FPM_INIT_EXIT_OK) {
1833 exit_status = FPM_EXIT_OK;
1834 goto out;
1835 }
1836
1837 if (fpm_globals.send_config_pipe[1]) {
1838 int writeval = 1;
1839 zlog(ZLOG_DEBUG, "Sending \"1\" (OK) to parent via fd=%d", fpm_globals.send_config_pipe[1]);
1840 zend_quiet_write(fpm_globals.send_config_pipe[1], &writeval, sizeof(writeval));
1841 close(fpm_globals.send_config_pipe[1]);
1842 }
1843 fpm_is_running = 1;
1844
1845 fcgi_fd = fpm_run(&max_requests);
1846 parent = 0;
1847
1848 /* onced forked tell zlog to also send messages through sapi_cgi_log_fastcgi() */
1849 zlog_set_external_logger(sapi_cgi_log_fastcgi);
1850
1851 /* make php call us to get _ENV vars */
1852 php_php_import_environment_variables = php_import_environment_variables;
1853 php_import_environment_variables = cgi_php_import_environment_variables;
1854 php_load_environment_variables = cgi_php_load_environment_variables;
1855
1856 /* library is already initialized, now init our request */
1857 request = fpm_init_request(fcgi_fd);
1858
1860 while (EXPECTED(fcgi_accept_request(request) >= 0)) {
1861 char *primary_script = NULL;
1862 request_body_fd = -1;
1863 SG(server_context) = (void *) request;
1864 CGIG(fcgi_logging_request_started) = false;
1865 init_request_info();
1866
1868
1869 /* request startup only after we've done all we can to
1870 * get path_translated */
1872 fcgi_finish_request(request, 1);
1873 SG(server_context) = NULL;
1875 return FPM_EXIT_SOFTWARE;
1876 }
1877
1878 /* check if request_method has been sent.
1879 * if not, it's certainly not an HTTP over fcgi request */
1880 if (UNEXPECTED(!SG(request_info).request_method)) {
1881 goto fastcgi_request_done;
1882 }
1883
1885 goto fastcgi_request_done;
1886 }
1887
1888 /* If path_translated is NULL, terminate here with a 404 */
1889 if (UNEXPECTED(!SG(request_info).path_translated)) {
1890 zend_try {
1891 zlog(ZLOG_DEBUG, "Primary script unknown");
1892 SG(sapi_headers).http_response_code = 404;
1893 PUTS("File not found.\n");
1894 } zend_catch {
1895 } zend_end_try();
1896 goto fastcgi_request_done;
1897 }
1898
1899 if (UNEXPECTED(fpm_php_limit_extensions(SG(request_info).path_translated))) {
1900 SG(sapi_headers).http_response_code = 403;
1901 PUTS("Access denied.\n");
1902 goto fastcgi_request_done;
1903 }
1904
1905 /*
1906 * have to duplicate SG(request_info).path_translated to be able to log errors
1907 * php_fopen_primary_script seems to delete SG(request_info).path_translated on failure
1908 */
1909 primary_script = estrdup(SG(request_info).path_translated);
1910
1911 /* path_translated exists, we can continue ! */
1912 if (UNEXPECTED(php_fopen_primary_script(&file_handle) == FAILURE)) {
1913 zend_try {
1914 zlog(ZLOG_ERROR, "Unable to open primary script: %s (%s)", primary_script, strerror(errno));
1915 if (errno == EACCES) {
1916 SG(sapi_headers).http_response_code = 403;
1917 PUTS("Access denied.\n");
1918 } else {
1919 SG(sapi_headers).http_response_code = 404;
1920 PUTS("No input file specified.\n");
1921 }
1922 } zend_catch {
1923 } zend_end_try();
1924 /* We want to serve more requests if this is fastcgi so cleanup and continue,
1925 * request shutdown is handled later. */
1926 } else {
1928
1929 /* Reset exit status from the previous execution */
1930 EG(exit_status) = 0;
1931
1932 php_execute_script(&file_handle);
1933 }
1934
1935 /* Without opcache, or the first time with opcache, the file handle will be placed
1936 * in the CG(open_files) list by open_file_for_scanning(). Starting from the second
1937 * request in opcache, the file handle won't be in the list and therefore won't be destroyed for us. */
1938 if (!file_handle.in_list) {
1939 zend_destroy_file_handle(&file_handle);
1940 }
1941
1942fastcgi_request_done:
1943 if (EXPECTED(primary_script)) {
1944 efree(primary_script);
1945 }
1946
1947 if (UNEXPECTED(request_body_fd != -1)) {
1948 close(request_body_fd);
1949 }
1950 request_body_fd = -2;
1951
1952 if (UNEXPECTED(EG(exit_status) == 255)) {
1953 if (CGIG(error_header) && *CGIG(error_header) && !SG(headers_sent)) {
1954 sapi_header_line ctr = {0};
1955
1956 ctr.line = CGIG(error_header);
1957 ctr.line_len = strlen(CGIG(error_header));
1959 }
1960 }
1961
1964
1965 efree(SG(request_info).path_translated);
1966 SG(request_info).path_translated = NULL;
1967
1968 php_request_shutdown((void *) 0);
1969
1971
1972 requests++;
1973 if (UNEXPECTED(max_requests && (requests == max_requests))) {
1974 fcgi_request_set_keep(request, 0);
1975 fcgi_finish_request(request, 0);
1976 break;
1977 }
1978 /* end of fastcgi loop */
1979 }
1980 fcgi_destroy_request(request);
1981 fcgi_shutdown();
1982
1983 if (cgi_sapi_module.php_ini_path_override) {
1984 free(cgi_sapi_module.php_ini_path_override);
1985 }
1986 php_ini_builder_deinit(&ini_builder);
1987 } zend_catch {
1988 exit_status = FPM_EXIT_SOFTWARE;
1989 } zend_end_try();
1990
1991out:
1992
1993 SG(server_context) = NULL;
1995
1996 if (parent) {
1997 sapi_shutdown();
1998 }
1999
2000#ifdef ZTS
2001 tsrm_shutdown();
2002#endif
2003
2004 return exit_status;
2005}
2006/* }}} */
SAPI_API double sapi_get_request_time(void)
Definition SAPI.c:1088
SAPI_API sapi_module_struct sapi_module
Definition SAPI.c:65
SAPI_API int sapi_send_headers(void)
Definition SAPI.c:843
SAPI_API void sapi_add_request_header(const char *var, unsigned int var_len, char *val, unsigned int val_len, void *arg)
Definition SAPI.c:1110
SAPI_API int sapi_header_op(sapi_header_op_enum op, void *arg)
Definition SAPI.c:666
SAPI_API void sapi_startup(sapi_module_struct *sf)
Definition SAPI.c:68
SAPI_API void sapi_shutdown(void)
Definition SAPI.c:89
@ SAPI_HEADER_REPLACE
Definition SAPI.h:192
#define SG(v)
Definition SAPI.h:160
#define SAPI_HEADER_SENT_SUCCESSFULLY
Definition SAPI.h:303
#define STANDARD_SAPI_MODULE_PROPERTIES
Definition SAPI.h:324
struct _sapi_module_struct sapi_module_struct
Definition SAPI.h:60
size_t len
Definition apprentice.c:174
file_private const char ext[]
getenv(?string $name=null, bool $local_only=false)
http_response_code(int $response_code=0)
strrchr(string $haystack, string $needle, bool $before_needle=false)
fwrite($stream, string $data, ?int $length=null)
fflush($stream)
strstr(string $haystack, string $needle, bool $before_needle=false)
stat(string $filename)
strchr(string $haystack, string $needle, bool $before_needle=false)
headers_sent(&$filename=null, &$line=null)
char s[4]
Definition cdf.c:77
struct _php_cgi_globals_struct php_cgi_globals_struct
struct _user_config_cache_entry user_config_cache_entry
#define STDOUT_FILENO
Definition cgi_main.c:291
#define SAPI_CGI_MAX_HEADER_LENGTH
Definition cgi_main.c:369
void fcgi_request_set_keep(fcgi_request *req, int new_value)
Definition fastcgi.c:461
int fcgi_init(void)
Definition fastcgi.c:480
int fcgi_accept_request(fcgi_request *req)
Definition fastcgi.c:1351
void fcgi_destroy_request(fcgi_request *req)
Definition fastcgi.c:905
int fcgi_read(fcgi_request *req, char *str, int len)
Definition fastcgi.c:1204
int fcgi_flush(fcgi_request *req, int end)
Definition fastcgi.c:1510
fcgi_request * fcgi_init_request(int listen_socket, void(*on_accept)(void), void(*on_read)(void), void(*on_close)(void))
Definition fastcgi.c:870
void fcgi_loadenv(fcgi_request *req, fcgi_apply_func func, zval *array)
Definition fastcgi.c:1710
int fcgi_end(fcgi_request *req)
Definition fastcgi.c:1648
void fcgi_shutdown(void)
Definition fastcgi.c:550
int fcgi_is_closed(fcgi_request *req)
Definition fastcgi.c:1308
int fcgi_write(fcgi_request *req, fcgi_request_type type, const char *str, int len)
Definition fastcgi.c:1540
char * fcgi_getenv(fcgi_request *req, const char *var, int var_len)
Definition fastcgi.c:1673
int fcgi_finish_request(fcgi_request *req, int force_close)
Definition fastcgi.c:1657
char * fcgi_putenv(fcgi_request *req, char *var, int var_len, char *val)
Definition fastcgi.c:1689
void fcgi_close(fcgi_request *req, int force, int destroy)
Definition fastcgi.c:1257
#define FCGI_PUTENV(request, name, value)
Definition fastcgi.h:39
void void fcgi_set_logger(fcgi_logger lg)
Definition fastcgi.h:105
#define FCGI_GETENV(request, name)
Definition fastcgi.h:36
@ FCGI_STDERR
Definition fastcgi.h:63
@ FCGI_STDOUT
Definition fastcgi.h:62
struct _fcgi_request fcgi_request
Definition fastcgi.h:84
zend_ffi_type * type
Definition ffi.c:3812
void * ptr
Definition ffi.c:3814
memcpy(ptr1, ptr2, size)
char * err
Definition ffi.c:3029
zval * arg
Definition ffi.c:3975
zval * val
Definition ffi.c:4262
zend_ffi_ctype_name_buf buf
Definition ffi.c:4685
PHPAPI int php_fopen_primary_script(zend_file_handle *file_handle)
int fpm_run(int *max_requests)
Definition fpm.c:91
struct fpm_globals_s fpm_globals
Definition fpm.c:24
enum fpm_init_return_status fpm_init(int argc, char **argv, char *config, char *prefix, char *pid, int test_conf, int run_as_root, int force_daemon, int force_stderr)
Definition fpm.c:44
fpm_init_return_status
Definition fpm.h:37
@ FPM_INIT_ERROR
Definition fpm.h:38
@ FPM_INIT_EXIT_OK
Definition fpm.h:40
#define FPM_EXIT_SOFTWARE
Definition fpm.h:27
#define FPM_EXIT_OK
Definition fpm.h:15
#define FPM_EXIT_CONFIG
Definition fpm.h:33
#define FPM_EXIT_USAGE
Definition fpm.h:21
int fpm_log_write(char *log_format)
Definition fpm_log.c:108
#define APACHE_PROXY_FCGI_PREFIX
#define APACHE_PROXY_BALANCER_PREFIX
#define CGIG(v)
Definition fpm_main.c:184
fpm_get_status()
fastcgi_finish_request()
int fpm_php_limit_extensions(char *path)
Definition fpm_php.c:260
int fpm_php_apply_defines_ex(struct key_value_s *kv, int mode)
Definition fpm_php.c:85
void fpm_request_accepting(void)
Definition fpm_request.c:37
void fpm_request_finished(void)
void fpm_request_info(void)
void fpm_request_reading_headers(void)
Definition fpm_request.c:60
void fpm_request_end(void)
void fpm_request_executing(void)
int fpm_signals_init_mask(void)
int fpm_signals_block(void)
int fpm_status_handle_request(void)
Definition fpm_status.c:142
int fpm_status_export_to_zval(zval *status)
Definition fpm_status.c:48
int fpm_stdio_flush_child(void)
Definition fpm_stdio.c:164
char * mode
#define NULL
Definition gdcache.h:45
int main(void)
Definition gddemo.c:7
PHPAPI int php_getopt(int argc, char *const *argv, const opt_struct opts[], char **optarg, int *optind, int show_err, int arg_start)
Definition getopt.c:55
#define SUCCESS
Definition hash_sha3.c:261
PHPAPI bool php_header(void)
Definition head.c:70
struct _http_response_status_code_pair http_response_status_code_pair
PHPAPI ZEND_COLD void php_print_info(int flag)
Definition info.c:762
PHPAPI size_t php_printf(const char *format,...)
Definition main.c:938
void php_request_shutdown(void *dummy)
Definition main.c:1885
zend_result php_module_startup(sapi_module_struct *sf, zend_module_entry *additional_module)
Definition main.c:2103
PHPAPI void php_handle_aborted_connection(void)
Definition main.c:2656
PHPAPI bool php_execute_script(zend_file_handle *primary_file)
Definition main.c:2613
void php_module_shutdown(void)
Definition main.c:2424
PHPAPI void php_print_version(sapi_module_struct *sapi_module)
Definition main.c:144
zend_result php_request_startup(void)
Definition main.c:1801
PHPAPI int php_handle_auth_data(const char *auth)
Definition main.c:2669
int php_module_shutdown_wrapper(sapi_module_struct *sapi_globals)
Definition main.c:2416
const PHP_PREFIX
Definition main.stub.php:81
PHPAPI void php_output_end_all(void)
Definition output.c:322
PHPAPI int php_output_activate(void)
Definition output.c:159
PHPAPI void php_output_deactivate(void)
Definition output.c:176
const SIG_IGN
const SIGPIPE
#define memmove(a, b, c)
php_info_print_table_start()
Definition info.c:1064
php_info_print_table_row(2, "PDO Driver for Firebird", "enabled")
php_info_print_table_end()
Definition info.c:1074
ini
Definition phar.c:63
#define PHP_FUNCTION
Definition php.h:364
#define PHP_MSHUTDOWN_FUNCTION
Definition php.h:401
#define PHP_MINFO
Definition php.h:396
#define PHP_MINIT_FUNCTION
Definition php.h:400
#define PHP_MSHUTDOWN
Definition php.h:393
#define PHP_MINFO_FUNCTION
Definition php.h:404
#define PHP_MINIT
Definition php.h:392
#define php_error
Definition php.h:310
unsigned const char * pos
Definition php_ffi.h:52
apache_request_headers()
#define PHP_GETOPT_INVALID_ARG
Definition php_getopt.h:37
struct _opt_struct opt_struct
#define TRACK_VARS_ENV
Definition php_globals.h:44
#define TRACK_VARS_SERVER
Definition php_globals.h:43
#define PG(v)
Definition php_globals.h:31
PHPAPI void config_zval_dtor(zval *zvalue)
Definition php_ini.c:166
PHPAPI int php_parse_user_ini_file(const char *dirname, const char *ini_filename, HashTable *target_hash)
Definition php_ini.c:759
PHPAPI int php_ini_has_per_host_config(void)
Definition php_ini.c:864
PHPAPI void php_ini_activate_config(HashTable *source_hash, int modify_type, int stage)
Definition php_ini.c:800
PHPAPI int php_ini_has_per_dir_config(void)
Definition php_ini.c:815
PHPAPI void php_ini_activate_per_host_config(const char *host, size_t host_len)
Definition php_ini.c:871
PHPAPI void php_ini_activate_per_dir_config(char *path, size_t path_len)
Definition php_ini.c:822
#define PHP_INI_PERDIR
Definition php_ini.h:42
#define PHP_INI_ALL
Definition php_ini.h:45
#define PHP_INI_BEGIN
Definition php_ini.h:52
#define STD_PHP_INI_ENTRY
Definition php_ini.h:64
#define STD_PHP_INI_BOOLEAN
Definition php_ini.h:66
#define PHP_INI_SYSTEM
Definition php_ini.h:43
#define PHP_INI_END
Definition php_ini.h:53
#define PHP_INI_STAGE_HTACCESS
Definition php_ini.h:76
PHPAPI void php_ini_builder_define(struct php_ini_builder *b, const char *arg)
#define PHPWRITE_H(str, str_len)
#define PUTS(str)
unsigned char key[REFLECTION_KEY_LEN]
PHPAPI void(* php_load_environment_variables)(zval *array_ptr)
PHPAPI void(* php_import_environment_variables)(zval *array_ptr)
PHPAPI void php_register_variable_safe(const char *var, const char *strval, size_t str_len, zval *track_vars_array)
#define PARSE_SERVER
#define PARSE_ENV
#define PHP_VERSION
Definition php_version.h:7
p
Definition session.c:1105
#define slprintf
Definition snprintf.h:89
#define spprintf
Definition spprintf.h:29
zval val
Definition zend_types.h:381
HashTable user_config_cache
Definition cgi_main.c:167
Definition cgi_main.c:190
time_t expires
Definition cgi_main.c:191
HashTable * user_config
Definition cgi_main.c:192
const char * name
llist_dtor_func_t dtor
Definition zend_llist.h:42
Definition file.h:177
const char * line
Definition SAPI.h:186
size_t line_len
Definition SAPI.h:187
char * header
Definition SAPI.h:45
size_t header_len
Definition SAPI.h:46
zend_llist headers
Definition SAPI.h:51
PHPAPI size_t php_raw_url_decode(char *str, size_t len)
Definition url.c:644
#define close(a)
#define errno
#define zend_first_try
Definition zend.h:284
#define zend_catch
Definition zend.h:277
#define zend_try
Definition zend.h:270
#define zend_end_try()
Definition zend.h:280
ZEND_API HashTable module_registry
Definition zend_API.c:41
#define RETURN_FALSE
Definition zend_API.h:1058
#define zend_parse_parameters_none()
Definition zend_API.h:353
#define RETURN_THROWS()
Definition zend_API.h:1060
#define RETURN_TRUE
Definition zend_API.h:1059
#define array_init(arg)
Definition zend_API.h:537
#define estrndup(s, length)
Definition zend_alloc.h:165
#define efree(ptr)
Definition zend_alloc.h:155
#define estrdup(s)
Definition zend_alloc.h:164
#define pemalloc(size, persistent)
Definition zend_alloc.h:189
#define emalloc(size)
Definition zend_alloc.h:151
struct _zval_struct zval
strlen(string $string)
strncmp(string $string1, string $string2, int $length)
strcmp(string $string1, string $string2)
zval * args
ZEND_API size_t zend_dirname(char *path, size_t len)
#define ZEND_COMPILE_EXTENDED_INFO
ZEND_API void zend_destroy_file_handle(zend_file_handle *file_handle)
#define strncasecmp(s1, s2, n)
#define strcasecmp(s1, s2)
#define E_WARNING
Definition zend_errors.h:24
ZEND_API void(ZEND_FASTCALL *zend_touch_vm_stack_data)(void *vm_stack_data)
ZEND_API zend_llist zend_extensions
struct _zend_extension zend_extension
#define CG(v)
#define EG(v)
ZEND_API void ZEND_FASTCALL zend_hash_destroy(HashTable *ht)
Definition zend_hash.c:1727
ZEND_API void ZEND_FASTCALL zend_hash_clean(HashTable *ht)
Definition zend_hash.c:1869
ZEND_API void ZEND_FASTCALL zend_hash_copy(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor)
Definition zend_hash.c:2240
ZEND_API void ZEND_FASTCALL zend_array_destroy(HashTable *ht)
Definition zend_hash.c:1808
ZEND_API HashTable *ZEND_FASTCALL zend_array_dup(HashTable *source)
Definition zend_hash.c:2438
#define zend_hash_init(ht, nSize, pHashFunction, pDestructor, persistent)
Definition zend_hash.h:108
#define ZEND_HASH_MAP_FOREACH_PTR(ht, _ptr)
Definition zend_hash.h:1326
#define ZEND_HASH_FOREACH_END()
Definition zend_hash.h:1086
#define ZEND_INI_SYSTEM
Definition zend_ini.h:26
#define ZEND_INI_PARSER_ENTRY
Definition zend_ini.h:244
void(* zend_ini_parser_cb_t)(zval *arg1, zval *arg2, zval *arg3, int callback_type, void *arg)
Definition zend_ini.h:237
#define UNREGISTER_INI_ENTRIES()
Definition zend_ini.h:204
#define REGISTER_INI_ENTRIES()
Definition zend_ini.h:203
#define DISPLAY_INI_ENTRIES()
Definition zend_ini.h:205
ZEND_API zend_result zend_parse_ini_string(const char *str, bool unbuffered_errors, int scanner_mode, zend_ini_parser_cb_t ini_parser_cb, void *arg)
#define ZEND_INI_USER
Definition zend_ini.h:24
struct _zend_file_handle zend_file_handle
#define ZEND_INI_SCANNER_NORMAL
ZEND_API void zend_llist_destroy(zend_llist *l)
Definition zend_llist.c:102
ZEND_API void zend_llist_copy(zend_llist *dst, zend_llist *src)
Definition zend_llist.c:151
ZEND_API void * zend_llist_get_next_ex(zend_llist *l, zend_llist_position *pos)
Definition zend_llist.c:286
ZEND_API void zend_llist_sort(zend_llist *l, llist_compare_func_t comp_func)
Definition zend_llist.c:196
ZEND_API void zend_llist_apply(zend_llist *l, llist_apply_func_t func)
Definition zend_llist.c:179
ZEND_API void * zend_llist_get_first_ex(zend_llist *l, zend_llist_position *pos)
Definition zend_llist.c:260
struct _zend_llist_element zend_llist_element
zend_llist_element * zend_llist_position
Definition zend_llist.h:47
void(* llist_apply_func_t)(void *)
Definition zend_llist.h:35
struct _zend_llist zend_llist
#define STANDARD_MODULE_HEADER
struct _zend_module_entry zend_module_entry
#define STANDARD_MODULE_PROPERTIES
ZEND_API void ZEND_FASTCALL zend_str_tolower(char *str, size_t length)
#define zend_quiet_write(...)
#define MIN(a, b)
#define EXPECTED(condition)
#define UNEXPECTED(condition)
#define zend_signal_startup()
#define Z_STRVAL_P(zval_p)
Definition zend_types.h:975
struct _zend_array HashTable
Definition zend_types.h:386
#define IS_ARRAY
Definition zend_types.h:607
#define Z_PTR_P(zval_p)
#define Z_PTR(zval)
@ FAILURE
Definition zend_types.h:61
#define Z_ARR(zval)
Definition zend_types.h:983
struct _Bucket Bucket
#define Z_TYPE(zval)
Definition zend_types.h:659
#define Z_ARRVAL(zval)
Definition zend_types.h:986
#define Z_ARR_P(zval_p)
Definition zend_types.h:984
CWD_API char * tsrm_realpath(const char *path, char *real_path)
#define IS_SLASH(c)
#define DEFAULT_SLASH
#define IS_ABSOLUTE_PATH(path, len)
#define S_ISREG(mode)
zval * return_value
zval * arg1
zval * arg2
zend_string * name
zval * arg3
zval * ret
value
out($f, $s)
void zlog_set_external_logger(void(*logger)(int, char *, size_t))
Definition zlog.c:53
void vzlog(const char *function, int line, int flags, const char *fmt, va_list args)
Definition zlog.c:198
#define zlog_msg(flags, prefix, msg)
Definition zlog.h:10
@ ZLOG_DEBUG
Definition zlog.h:42
@ ZLOG_ERROR
Definition zlog.h:45
@ ZLOG_NOTICE
Definition zlog.h:43
@ ZLOG_WARNING
Definition zlog.h:44
#define zlog(flags,...)
Definition zlog.h:9