PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - sapi/cgi - cgi_main.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 501 1070 46.8 %
Date: 2015-06-27 Functions: 20 42 47.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2015 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Authors: Rasmus Lerdorf <rasmus@lerdorf.on.ca>                       |
      16             :    |          Stig Bakken <ssb@php.net>                                   |
      17             :    |          Zeev Suraski <zeev@zend.com>                                |
      18             :    | FastCGI: Ben Mansell <php@slimyhorror.com>                           |
      19             :    |          Shane Caraveo <shane@caraveo.com>                           |
      20             :    |          Dmitry Stogov <dmitry@zend.com>                             |
      21             :    +----------------------------------------------------------------------+
      22             : */
      23             : 
      24             : /* $Id$ */
      25             : 
      26             : #include "php.h"
      27             : #include "php_globals.h"
      28             : #include "php_variables.h"
      29             : #include "zend_modules.h"
      30             : 
      31             : #include "SAPI.h"
      32             : 
      33             : #include <stdio.h>
      34             : #include "php.h"
      35             : 
      36             : #ifdef PHP_WIN32
      37             : # include "win32/time.h"
      38             : # include "win32/signal.h"
      39             : # include <process.h>
      40             : #endif
      41             : 
      42             : #if HAVE_SYS_TIME_H
      43             : # include <sys/time.h>
      44             : #endif
      45             : 
      46             : #if HAVE_UNISTD_H
      47             : # include <unistd.h>
      48             : #endif
      49             : 
      50             : #if HAVE_SIGNAL_H
      51             : # include <signal.h>
      52             : #endif
      53             : 
      54             : #if HAVE_SETLOCALE
      55             : # include <locale.h>
      56             : #endif
      57             : 
      58             : #if HAVE_SYS_TYPES_H
      59             : # include <sys/types.h>
      60             : #endif
      61             : 
      62             : #if HAVE_SYS_WAIT_H
      63             : # include <sys/wait.h>
      64             : #endif
      65             : 
      66             : #include "zend.h"
      67             : #include "zend_extensions.h"
      68             : #include "php_ini.h"
      69             : #include "php_globals.h"
      70             : #include "php_main.h"
      71             : #include "fopen_wrappers.h"
      72             : #include "http_status_codes.h"
      73             : #include "ext/standard/php_standard.h"
      74             : #include "ext/standard/url.h"
      75             : 
      76             : #ifdef PHP_WIN32
      77             : # include <io.h>
      78             : # include <fcntl.h>
      79             : # include "win32/php_registry.h"
      80             : #endif
      81             : 
      82             : #ifdef __riscos__
      83             : # include <unixlib/local.h>
      84             : int __riscosify_control = __RISCOSIFY_STRICT_UNIX_SPECS;
      85             : #endif
      86             : 
      87             : #include "zend_compile.h"
      88             : #include "zend_execute.h"
      89             : #include "zend_highlight.h"
      90             : 
      91             : #include "php_getopt.h"
      92             : 
      93             : #include "fastcgi.h"
      94             : 
      95             : #ifndef PHP_WIN32
      96             : /* XXX this will need to change later when threaded fastcgi is implemented.  shane */
      97             : struct sigaction act, old_term, old_quit, old_int;
      98             : #endif
      99             : 
     100             : static void (*php_php_import_environment_variables)(zval *array_ptr);
     101             : 
     102             : #ifndef PHP_WIN32
     103             : /* these globals used for forking children on unix systems */
     104             : /**
     105             :  * Number of child processes that will get created to service requests
     106             :  */
     107             : static int children = 0;
     108             : 
     109             : 
     110             : /**
     111             :  * Set to non-zero if we are the parent process
     112             :  */
     113             : static int parent = 1;
     114             : 
     115             : /* Did parent received exit signals SIG_TERM/SIG_INT/SIG_QUIT */
     116             : static int exit_signal = 0;
     117             : 
     118             : /* Is Parent waiting for children to exit */
     119             : static int parent_waiting = 0;
     120             : 
     121             : /**
     122             :  * Process group
     123             :  */
     124             : static pid_t pgroup;
     125             : #endif
     126             : 
     127             : #define PHP_MODE_STANDARD       1
     128             : #define PHP_MODE_HIGHLIGHT      2
     129             : #define PHP_MODE_LINT           4
     130             : #define PHP_MODE_STRIP          5
     131             : 
     132             : static char *php_optarg = NULL;
     133             : static int php_optind = 1;
     134             : static zend_module_entry cgi_module_entry;
     135             : 
     136             : static const opt_struct OPTIONS[] = {
     137             :         {'a', 0, "interactive"},
     138             :         {'b', 1, "bindpath"},
     139             :         {'C', 0, "no-chdir"},
     140             :         {'c', 1, "php-ini"},
     141             :         {'d', 1, "define"},
     142             :         {'e', 0, "profile-info"},
     143             :         {'f', 1, "file"},
     144             :         {'h', 0, "help"},
     145             :         {'i', 0, "info"},
     146             :         {'l', 0, "syntax-check"},
     147             :         {'m', 0, "modules"},
     148             :         {'n', 0, "no-php-ini"},
     149             :         {'q', 0, "no-header"},
     150             :         {'s', 0, "syntax-highlight"},
     151             :         {'s', 0, "syntax-highlighting"},
     152             :         {'w', 0, "strip"},
     153             :         {'?', 0, "usage"},/* help alias (both '?' and 'usage') */
     154             :         {'v', 0, "version"},
     155             :         {'z', 1, "zend-extension"},
     156             :         {'T', 1, "timing"},
     157             :         {'-', 0, NULL} /* end of args */
     158             : };
     159             : 
     160             : typedef struct _php_cgi_globals_struct {
     161             :         HashTable user_config_cache;
     162             :         char *redirect_status_env;
     163             :         zend_bool rfc2616_headers;
     164             :         zend_bool nph;
     165             :         zend_bool check_shebang_line;
     166             :         zend_bool fix_pathinfo;
     167             :         zend_bool force_redirect;
     168             :         zend_bool discard_path;
     169             :         zend_bool fcgi_logging;
     170             : #ifdef PHP_WIN32
     171             :         zend_bool impersonate;
     172             : #endif
     173             : } php_cgi_globals_struct;
     174             : 
     175             : /* {{{ user_config_cache
     176             :  *
     177             :  * Key for each cache entry is dirname(PATH_TRANSLATED).
     178             :  *
     179             :  * NOTE: Each cache entry config_hash contains the combination from all user ini files found in
     180             :  *       the path starting from doc_root throught to dirname(PATH_TRANSLATED).  There is no point
     181             :  *       storing per-file entries as it would not be possible to detect added / deleted entries
     182             :  *       between separate files.
     183             :  */
     184             : typedef struct _user_config_cache_entry {
     185             :         time_t expires;
     186             :         HashTable *user_config;
     187             : } user_config_cache_entry;
     188             : 
     189           0 : static void user_config_cache_entry_dtor(zval *el)
     190             : {
     191           0 :         user_config_cache_entry *entry = (user_config_cache_entry *)Z_PTR_P(el);
     192           0 :         zend_hash_destroy(entry->user_config);
     193           0 :         free(entry->user_config);
     194           0 :         free(entry);
     195           0 : }
     196             : /* }}} */
     197             : 
     198             : #ifdef ZTS
     199             : static int php_cgi_globals_id;
     200             : #define CGIG(v) ZEND_TSRMG(php_cgi_globals_id, php_cgi_globals_struct *, v)
     201             : #if defined(PHP_WIN32)
     202             : ZEND_TSRMLS_CACHE_DEFINE();
     203             : #endif
     204             : #else
     205             : static php_cgi_globals_struct php_cgi_globals;
     206             : #define CGIG(v) (php_cgi_globals.v)
     207             : #endif
     208             : 
     209             : #ifdef PHP_WIN32
     210             : #define TRANSLATE_SLASHES(path) \
     211             :         { \
     212             :                 char *tmp = path; \
     213             :                 while (*tmp) { \
     214             :                         if (*tmp == '\\') *tmp = '/'; \
     215             :                         tmp++; \
     216             :                 } \
     217             :         }
     218             : #else
     219             : #define TRANSLATE_SLASHES(path)
     220             : #endif
     221             : 
     222             : #ifndef HAVE_ATTRIBUTE_WEAK
     223             : static void fcgi_log(int type, const char *format, ...) {
     224             :         va_list ap;
     225             : 
     226             :         va_start(ap, format);
     227             :         vfprintf(stderr, format, ap);
     228             :         va_end(ap);
     229             : }
     230             : #endif
     231             : 
     232           0 : static int print_module_info(zval *element)
     233             : {
     234           0 :         zend_module_entry *module = Z_PTR_P(element);
     235           0 :         php_printf("%s\n", module->name);
     236           0 :         return ZEND_HASH_APPLY_KEEP;
     237             : }
     238             : 
     239           0 : static int module_name_cmp(const void *a, const void *b)
     240             : {
     241           0 :         Bucket *f = (Bucket *) a;
     242           0 :         Bucket *s = (Bucket *) b;
     243             : 
     244           0 :         return strcasecmp(      ((zend_module_entry *)Z_PTR(f->val))->name,
     245           0 :                                                 ((zend_module_entry *)Z_PTR(s->val))->name);
     246             : }
     247             : 
     248           0 : static void print_modules(void)
     249             : {
     250             :         HashTable sorted_registry;
     251             : 
     252           0 :         zend_hash_init(&sorted_registry, 64, NULL, NULL, 1);
     253           0 :         zend_hash_copy(&sorted_registry, &module_registry, NULL);
     254           0 :         zend_hash_sort(&sorted_registry, module_name_cmp, 0);
     255           0 :         zend_hash_apply(&sorted_registry, print_module_info);
     256           0 :         zend_hash_destroy(&sorted_registry);
     257           0 : }
     258             : 
     259           0 : static int print_extension_info(zend_extension *ext, void *arg)
     260             : {
     261           0 :         php_printf("%s\n", ext->name);
     262           0 :         return 0;
     263             : }
     264             : 
     265           0 : static int extension_name_cmp(const zend_llist_element **f, const zend_llist_element **s)
     266             : {
     267           0 :         return strcmp(  ((zend_extension *)(*f)->data)->name,
     268           0 :                                         ((zend_extension *)(*s)->data)->name);
     269             : }
     270             : 
     271           0 : static void print_extensions(void)
     272             : {
     273             :         zend_llist sorted_exts;
     274             : 
     275           0 :         zend_llist_copy(&sorted_exts, &zend_extensions);
     276           0 :         sorted_exts.dtor = NULL;
     277           0 :         zend_llist_sort(&sorted_exts, extension_name_cmp);
     278           0 :         zend_llist_apply_with_argument(&sorted_exts, (llist_apply_with_arg_func_t) print_extension_info, NULL);
     279           0 :         zend_llist_destroy(&sorted_exts);
     280           0 : }
     281             : 
     282             : #ifndef STDOUT_FILENO
     283             : #define STDOUT_FILENO 1
     284             : #endif
     285             : 
     286        7546 : static inline size_t sapi_cgi_single_write(const char *str, size_t str_length)
     287             : {
     288             : #ifdef PHP_WRITE_STDOUT
     289             :         int ret;
     290             : 
     291        7546 :         ret = write(STDOUT_FILENO, str, str_length);
     292        7546 :         if (ret <= 0) return 0;
     293        7546 :         return ret;
     294             : #else
     295             :         size_t ret;
     296             : 
     297             :         ret = fwrite(str, 1, MIN(str_length, 16384), stdout);
     298             :         return ret;
     299             : #endif
     300             : }
     301             : 
     302        7546 : static size_t sapi_cgi_ub_write(const char *str, size_t str_length)
     303             : {
     304        7546 :         const char *ptr = str;
     305        7546 :         size_t remaining = str_length;
     306             :         size_t ret;
     307             : 
     308       22638 :         while (remaining > 0) {
     309        7546 :                 ret = sapi_cgi_single_write(ptr, remaining);
     310        7546 :                 if (!ret) {
     311           0 :                         php_handle_aborted_connection();
     312           0 :                         return str_length - remaining;
     313             :                 }
     314        7546 :                 ptr += ret;
     315        7546 :                 remaining -= ret;
     316             :         }
     317             : 
     318        7546 :         return str_length;
     319             : }
     320             : 
     321           0 : static size_t sapi_fcgi_ub_write(const char *str, size_t str_length)
     322             : {
     323           0 :         const char *ptr = str;
     324           0 :         size_t remaining = str_length;
     325           0 :         fcgi_request *request = (fcgi_request*) SG(server_context);
     326             : 
     327           0 :         while (remaining > 0) {
     328           0 :                 int to_write = remaining > INT_MAX ? INT_MAX : (int)remaining;
     329           0 :                 int ret = fcgi_write(request, FCGI_STDOUT, ptr, to_write);
     330             : 
     331           0 :                 if (ret <= 0) {
     332           0 :                         php_handle_aborted_connection();
     333           0 :                         return str_length - remaining;
     334             :                 }
     335           0 :                 ptr += ret;
     336           0 :                 remaining -= ret;
     337             :         }
     338             : 
     339           0 :         return str_length;
     340             : }
     341             : 
     342         807 : static void sapi_cgi_flush(void *server_context)
     343             : {
     344         807 :         if (fflush(stdout) == EOF) {
     345           0 :                 php_handle_aborted_connection();
     346             :         }
     347         807 : }
     348             : 
     349           0 : static void sapi_fcgi_flush(void *server_context)
     350             : {
     351           0 :         fcgi_request *request = (fcgi_request*) server_context;
     352             : 
     353           0 :         if (
     354             : #ifndef PHP_WIN32
     355           0 :                 !parent &&
     356             : #endif
     357           0 :                 request && !fcgi_flush(request, 0)) {
     358             : 
     359           0 :                 php_handle_aborted_connection();
     360             :         }
     361           0 : }
     362             : 
     363             : #define SAPI_CGI_MAX_HEADER_LENGTH 1024
     364             : 
     365         240 : static int sapi_cgi_send_headers(sapi_headers_struct *sapi_headers)
     366             : {
     367             :         char buf[SAPI_CGI_MAX_HEADER_LENGTH];
     368             :         sapi_header_struct *h;
     369             :         zend_llist_position pos;
     370         240 :         zend_bool ignore_status = 0;
     371         240 :         int response_status = SG(sapi_headers).http_response_code;
     372             : 
     373         240 :         if (SG(request_info).no_headers == 1) {
     374           0 :                 return  SAPI_HEADER_SENT_SUCCESSFULLY;
     375             :         }
     376             : 
     377         240 :         if (CGIG(nph) || SG(sapi_headers).http_response_code != 200)
     378             :         {
     379             :                 int len;
     380          43 :                 zend_bool has_status = 0;
     381             : 
     382          43 :                 if (CGIG(rfc2616_headers) && SG(sapi_headers).http_status_line) {
     383             :                         char *s;
     384           0 :                         len = slprintf(buf, SAPI_CGI_MAX_HEADER_LENGTH, "%s\r\n", SG(sapi_headers).http_status_line);
     385           0 :                         if ((s = strchr(SG(sapi_headers).http_status_line, ' '))) {
     386           0 :                                 response_status = atoi((s + 1));
     387             :                         }
     388             : 
     389           0 :                         if (len > SAPI_CGI_MAX_HEADER_LENGTH) {
     390           0 :                                 len = SAPI_CGI_MAX_HEADER_LENGTH;
     391             :                         }
     392             : 
     393             :                 } else {
     394             :                         char *s;
     395             : 
     396         191 :                         if (SG(sapi_headers).http_status_line &&
     397          37 :                                 (s = strchr(SG(sapi_headers).http_status_line, ' ')) != 0 &&
     398          37 :                                 (s - SG(sapi_headers).http_status_line) >= 5 &&
     399          37 :                                 strncasecmp(SG(sapi_headers).http_status_line, "HTTP/", 5) == 0
     400             :                         ) {
     401          37 :                                 len = slprintf(buf, sizeof(buf), "Status:%s\r\n", s);
     402          37 :                                 response_status = atoi((s + 1));
     403             :                         } else {
     404           6 :                                 h = (sapi_header_struct*)zend_llist_get_first_ex(&sapi_headers->headers, &pos);
     405          28 :                                 while (h) {
     406          32 :                                         if (h->header_len > sizeof("Status:")-1 &&
     407          16 :                                                 strncasecmp(h->header, "Status:", sizeof("Status:")-1) == 0
     408             :                                         ) {
     409           0 :                                                 has_status = 1;
     410           0 :                                                 break;
     411             :                                         }
     412          16 :                                         h = (sapi_header_struct*)zend_llist_get_next_ex(&sapi_headers->headers, &pos);
     413             :                                 }
     414           6 :                                 if (!has_status) {
     415           6 :                                         http_response_status_code_pair *err = (http_response_status_code_pair*)http_status_map;
     416             : 
     417         118 :                                         while (err->code != 0) {
     418         112 :                                                 if (err->code == SG(sapi_headers).http_response_code) {
     419           6 :                                                         break;
     420             :                                                 }
     421         106 :                                                 err++;
     422             :                                         }
     423           6 :                                         if (err->str) {
     424           6 :                                                 len = slprintf(buf, sizeof(buf), "Status: %d %s\r\n", SG(sapi_headers).http_response_code, err->str);
     425             :                                         } else {
     426           0 :                                                 len = slprintf(buf, sizeof(buf), "Status: %d\r\n", SG(sapi_headers).http_response_code);
     427             :                                         }
     428             :                                 }
     429             :                         }
     430             :                 }
     431             : 
     432          43 :                 if (!has_status) {
     433          43 :                         PHPWRITE_H(buf, len);
     434          43 :                         ignore_status = 1;
     435             :                 }
     436             :         }
     437             : 
     438         240 :         h = (sapi_header_struct*)zend_llist_get_first_ex(&sapi_headers->headers, &pos);
     439        1073 :         while (h) {
     440             :                 /* prevent CRLFCRLF */
     441         593 :                 if (h->header_len) {
     442        1188 :                         if (h->header_len > sizeof("Status:")-1 &&
     443         593 :                                 strncasecmp(h->header, "Status:", sizeof("Status:")-1) == 0
     444             :                         ) {
     445           2 :                                 if (!ignore_status) {
     446           1 :                                         ignore_status = 1;
     447           1 :                                         PHPWRITE_H(h->header, h->header_len);
     448           1 :                                         PHPWRITE_H("\r\n", 2);
     449             :                                 }
     450         594 :                         } else if (response_status == 304 && h->header_len > sizeof("Content-Type:")-1 &&
     451           3 :                                 strncasecmp(h->header, "Content-Type:", sizeof("Content-Type:")-1) == 0
     452             :                         ) {
     453           1 :                                 h = (sapi_header_struct*)zend_llist_get_next_ex(&sapi_headers->headers, &pos);
     454           1 :                                 continue;
     455             :                         } else {
     456         590 :                                 PHPWRITE_H(h->header, h->header_len);
     457         590 :                                 PHPWRITE_H("\r\n", 2);
     458             :                         }
     459             :                 }
     460         592 :                 h = (sapi_header_struct*)zend_llist_get_next_ex(&sapi_headers->headers, &pos);
     461             :         }
     462         240 :         PHPWRITE_H("\r\n", 2);
     463             : 
     464         240 :         return SAPI_HEADER_SENT_SUCCESSFULLY;
     465             : }
     466             : 
     467             : #ifndef STDIN_FILENO
     468             : # define STDIN_FILENO 0
     469             : #endif
     470             : 
     471         705 : static size_t sapi_cgi_read_post(char *buffer, size_t count_bytes)
     472             : {
     473         705 :         size_t read_bytes = 0;
     474             :         int tmp_read_bytes;
     475             :         size_t remaining_bytes;
     476             : 
     477             :         assert(SG(request_info).content_length >= SG(read_post_bytes));
     478             : 
     479         705 :         remaining_bytes = (size_t)(SG(request_info).content_length - SG(read_post_bytes));
     480             : 
     481         705 :         count_bytes = MIN(count_bytes, remaining_bytes);
     482        1490 :         while (read_bytes < count_bytes) {
     483             : #ifdef PHP_WIN32
     484             :                 size_t diff = count_bytes - read_bytes;
     485             :                 unsigned int to_read = (diff > UINT_MAX) ? UINT_MAX : (unsigned int)diff;
     486             : 
     487             :                 tmp_read_bytes = read(STDIN_FILENO, buffer + read_bytes, to_read);
     488             : #else
     489          80 :                 tmp_read_bytes = read(STDIN_FILENO, buffer + read_bytes, count_bytes - read_bytes);
     490             : #endif
     491          80 :                 if (tmp_read_bytes <= 0) {
     492           0 :                         break;
     493             :                 }
     494          80 :                 read_bytes += tmp_read_bytes;
     495             :         }
     496         705 :         return read_bytes;
     497             : }
     498             : 
     499           0 : static size_t sapi_fcgi_read_post(char *buffer, size_t count_bytes)
     500             : {
     501           0 :         size_t read_bytes = 0;
     502             :         int tmp_read_bytes;
     503           0 :         fcgi_request *request = (fcgi_request*) SG(server_context);
     504           0 :         size_t remaining = SG(request_info).content_length - SG(read_post_bytes);
     505             : 
     506           0 :         if (remaining < count_bytes) {
     507           0 :                 count_bytes = remaining;
     508             :         }
     509           0 :         while (read_bytes < count_bytes) {
     510           0 :                 size_t diff = count_bytes - read_bytes;
     511           0 :                 int to_read = (diff > INT_MAX) ? INT_MAX : (int)diff;
     512             : 
     513           0 :                 tmp_read_bytes = fcgi_read(request, buffer + read_bytes, to_read);
     514           0 :                 if (tmp_read_bytes <= 0) {
     515           0 :                         break;
     516             :                 }
     517           0 :                 read_bytes += tmp_read_bytes;
     518             :         }
     519           0 :         return read_bytes;
     520             : }
     521             : 
     522           8 : static char *sapi_cgi_getenv(char *name, size_t name_len)
     523             : {
     524           8 :         return getenv(name);
     525             : }
     526             : 
     527           0 : static char *sapi_fcgi_getenv(char *name, size_t name_len)
     528             : {
     529             :         /* when php is started by mod_fastcgi, no regular environment
     530             :          * is provided to PHP.  It is always sent to PHP at the start
     531             :          * of a request.  So we have to do our own lookup to get env
     532             :          * vars.  This could probably be faster somehow.  */
     533           0 :         fcgi_request *request = (fcgi_request*) SG(server_context);
     534           0 :         char *ret = fcgi_getenv(request, name, (int)name_len);
     535             : 
     536           0 :         if (ret) return ret;
     537             :         /*  if cgi, or fastcgi and not found in fcgi env
     538             :                 check the regular environment */
     539           0 :         return getenv(name);
     540             : }
     541             : 
     542          12 : static char *_sapi_cgi_putenv(char *name, size_t name_len, char *value)
     543             : {
     544             : #if !HAVE_SETENV || !HAVE_UNSETENV
     545             :         size_t len;
     546             :         char *buf;
     547             : #endif
     548             : 
     549             : #if HAVE_SETENV
     550          12 :         if (value) {
     551           8 :                 setenv(name, value, 1);
     552             :         }
     553             : #endif
     554             : #if HAVE_UNSETENV
     555          12 :         if (!value) {
     556           4 :                 unsetenv(name);
     557             :         }
     558             : #endif
     559             : 
     560             : #if !HAVE_SETENV || !HAVE_UNSETENV
     561             :         /*  if cgi, or fastcgi and not found in fcgi env
     562             :                 check the regular environment
     563             :                 this leaks, but it's only cgi anyway, we'll fix
     564             :                 it for 5.0
     565             :         */
     566             :         len = name_len + (value ? strlen(value) : 0) + sizeof("=") + 2;
     567             :         buf = (char *) malloc(len);
     568             :         if (buf == NULL) {
     569             :                 return getenv(name);
     570             :         }
     571             : #endif
     572             : #if !HAVE_SETENV
     573             :         if (value) {
     574             :                 len = slprintf(buf, len - 1, "%s=%s", name, value);
     575             :                 putenv(buf);
     576             :         }
     577             : #endif
     578             : #if !HAVE_UNSETENV
     579             :         if (!value) {
     580             :                 len = slprintf(buf, len - 1, "%s=", name);
     581             :                 putenv(buf);
     582             :         }
     583             : #endif
     584          12 :         return getenv(name);
     585             : }
     586             : 
     587         406 : static char *sapi_cgi_read_cookies(void)
     588             : {
     589         406 :         return getenv("HTTP_COOKIE");
     590             : }
     591             : 
     592           0 : static char *sapi_fcgi_read_cookies(void)
     593             : {
     594           0 :         fcgi_request *request = (fcgi_request*) SG(server_context);
     595             : 
     596           0 :         return FCGI_GETENV(request, "HTTP_COOKIE");
     597             : }
     598             : 
     599           0 : static void cgi_php_load_env_var(char *var, unsigned int var_len, char *val, unsigned int val_len, void *arg)
     600             : {
     601           0 :         zval *array_ptr = (zval*)arg;
     602           0 :         int filter_arg = (Z_ARR_P(array_ptr) == Z_ARR(PG(http_globals)[TRACK_VARS_ENV]))?PARSE_ENV:PARSE_SERVER;
     603             :         size_t new_val_len;
     604             : 
     605           0 :         if (sapi_module.input_filter(filter_arg, var, &val, strlen(val), &new_val_len)) {
     606           0 :                 php_register_variable_safe(var, val, new_val_len, array_ptr);
     607             :         }
     608           0 : }
     609             : 
     610           0 : static void cgi_php_import_environment_variables(zval *array_ptr)
     611             : {
     612           0 :         if (Z_TYPE(PG(http_globals)[TRACK_VARS_ENV]) == IS_ARRAY &&
     613           0 :                 Z_ARR_P(array_ptr) != Z_ARR(PG(http_globals)[TRACK_VARS_ENV]) &&
     614           0 :                 zend_hash_num_elements(Z_ARRVAL(PG(http_globals)[TRACK_VARS_ENV])) > 0
     615             :         ) {
     616             :                 zval_dtor(array_ptr);
     617           0 :                 ZVAL_DUP(array_ptr, &PG(http_globals)[TRACK_VARS_ENV]);
     618           0 :                 return;
     619           0 :         } else if (Z_TYPE(PG(http_globals)[TRACK_VARS_SERVER]) == IS_ARRAY &&
     620           0 :                 Z_ARR_P(array_ptr) != Z_ARR(PG(http_globals)[TRACK_VARS_SERVER]) &&
     621           0 :                 zend_hash_num_elements(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER])) > 0
     622             :         ) {
     623             :                 zval_dtor(array_ptr);
     624           0 :                 ZVAL_DUP(array_ptr, &PG(http_globals)[TRACK_VARS_SERVER]);
     625           0 :                 return;
     626             :         }
     627             : 
     628             :         /* call php's original import as a catch-all */
     629           0 :         php_php_import_environment_variables(array_ptr);
     630             : 
     631           0 :         if (fcgi_is_fastcgi()) {
     632           0 :                 fcgi_request *request = (fcgi_request*) SG(server_context);
     633           0 :                 fcgi_loadenv(request, cgi_php_load_env_var, array_ptr);
     634             :         }
     635             : }
     636             : 
     637         111 : static void sapi_cgi_register_variables(zval *track_vars_array)
     638             : {
     639             :         size_t php_self_len;
     640             :         char *php_self;
     641             : 
     642             :         /* In CGI mode, we consider the environment to be a part of the server
     643             :          * variables
     644             :          */
     645         111 :         php_import_environment_variables(track_vars_array);
     646             : 
     647         111 :         if (CGIG(fix_pathinfo)) {
     648         111 :                 char *script_name = SG(request_info).request_uri;
     649             :                 char *path_info;
     650             :                 int free_php_self;
     651             :                 ALLOCA_FLAG(use_heap)
     652             : 
     653         111 :                 if (fcgi_is_fastcgi()) {
     654           0 :                         fcgi_request *request = (fcgi_request*) SG(server_context);
     655             : 
     656           0 :                         path_info = FCGI_GETENV(request, "PATH_INFO");
     657             :                 } else {
     658         111 :                         path_info = getenv("PATH_INFO");
     659             :                 }
     660             : 
     661         111 :                 if (path_info) {
     662          82 :                         size_t path_info_len = strlen(path_info);
     663             : 
     664          82 :                         if (script_name) {
     665          82 :                                 size_t script_name_len = strlen(script_name);
     666             : 
     667          82 :                                 php_self_len = script_name_len + path_info_len;
     668          82 :                                 php_self = do_alloca(php_self_len + 1, use_heap);
     669          82 :                                 memcpy(php_self, script_name, script_name_len + 1);
     670          82 :                                 memcpy(php_self + script_name_len, path_info, path_info_len + 1);
     671          82 :                                 free_php_self = 1;
     672             :                         }  else {
     673           0 :                                 php_self = path_info;
     674           0 :                                 php_self_len = path_info_len;
     675           0 :                                 free_php_self = 0;
     676             :                         }
     677          29 :                 } else if (script_name) {
     678          10 :                         php_self = script_name;
     679          10 :                         php_self_len = strlen(script_name);
     680          10 :                         free_php_self = 0;
     681             :                 } else {
     682          19 :                         php_self = "";
     683          19 :                         php_self_len = 0;
     684          19 :                         free_php_self = 0;
     685             :                 }
     686             : 
     687             :                 /* Build the special-case PHP_SELF variable for the CGI version */
     688         111 :                 if (sapi_module.input_filter(PARSE_SERVER, "PHP_SELF", &php_self, php_self_len, &php_self_len)) {
     689           0 :                         php_register_variable_safe("PHP_SELF", php_self, php_self_len, track_vars_array);
     690             :                 }
     691         111 :                 if (free_php_self) {
     692          82 :                         free_alloca(php_self, use_heap);
     693             :                 }
     694             :         } else {
     695           0 :                 php_self = SG(request_info).request_uri ? SG(request_info).request_uri : "";
     696           0 :                 php_self_len = strlen(php_self);
     697           0 :                 if (sapi_module.input_filter(PARSE_SERVER, "PHP_SELF", &php_self, php_self_len, &php_self_len)) {
     698           0 :                         php_register_variable_safe("PHP_SELF", php_self, php_self_len, track_vars_array);
     699             :                 }
     700             :         }
     701         111 : }
     702             : 
     703           0 : static void sapi_cgi_log_message(char *message)
     704             : {
     705           0 :         if (fcgi_is_fastcgi() && CGIG(fcgi_logging)) {
     706             :                 fcgi_request *request;
     707             : 
     708           0 :                 request = (fcgi_request*) SG(server_context);
     709           0 :                 if (request) {
     710           0 :                         int ret, len = (int)strlen(message);
     711           0 :                         char *buf = malloc(len+2);
     712             : 
     713           0 :                         memcpy(buf, message, len);
     714           0 :                         memcpy(buf + len, "\n", sizeof("\n"));
     715           0 :                         ret = fcgi_write(request, FCGI_STDERR, buf, (int)(len + 1));
     716           0 :                         free(buf);
     717           0 :                         if (ret < 0) {
     718           0 :                                 php_handle_aborted_connection();
     719             :                         }
     720             :                 } else {
     721           0 :                         fprintf(stderr, "%s\n", message);
     722             :                 }
     723             :                 /* ignore return code */
     724             :         } else {
     725           0 :                 fprintf(stderr, "%s\n", message);
     726             :         }
     727           0 : }
     728             : 
     729             : /* {{{ php_cgi_ini_activate_user_config
     730             :  */
     731           0 : static void php_cgi_ini_activate_user_config(char *path, size_t path_len, const char *doc_root, size_t doc_root_len, int start)
     732             : {
     733             :         char *ptr;
     734             :         user_config_cache_entry *new_entry, *entry;
     735           0 :         time_t request_time = (time_t)sapi_get_request_time();
     736             : 
     737             :         /* Find cached config entry: If not found, create one */
     738           0 :         if ((entry = zend_hash_str_find_ptr(&CGIG(user_config_cache), path, path_len)) == NULL) {
     739           0 :                 new_entry = pemalloc(sizeof(user_config_cache_entry), 1);
     740           0 :                 new_entry->expires = 0;
     741           0 :                 new_entry->user_config = (HashTable *) pemalloc(sizeof(HashTable), 1);
     742           0 :                 zend_hash_init(new_entry->user_config, 8, NULL, (dtor_func_t) config_zval_dtor, 1);
     743           0 :                 entry = zend_hash_str_update_ptr(&CGIG(user_config_cache), path, path_len, new_entry);
     744             :         }
     745             : 
     746             :         /* Check whether cache entry has expired and rescan if it is */
     747           0 :         if (request_time > entry->expires) {
     748           0 :                 char *real_path = NULL;
     749             :                 size_t real_path_len;
     750             :                 char *s1, *s2;
     751             :                 size_t s_len;
     752             : 
     753             :                 /* Clear the expired config */
     754           0 :                 zend_hash_clean(entry->user_config);
     755             : 
     756           0 :                 if (!IS_ABSOLUTE_PATH(path, path_len)) {
     757           0 :                         real_path = tsrm_realpath(path, NULL);
     758           0 :                         if (real_path == NULL) {
     759           0 :                                 return;
     760             :                         }
     761           0 :                         real_path_len = strlen(real_path);
     762           0 :                         path = real_path;
     763           0 :                         path_len = real_path_len;
     764             :                 }
     765             : 
     766           0 :                 if (path_len > doc_root_len) {
     767           0 :                         s1 = (char *) doc_root;
     768           0 :                         s2 = path;
     769           0 :                         s_len = doc_root_len;
     770             :                 } else {
     771           0 :                         s1 = path;
     772           0 :                         s2 = (char *) doc_root;
     773           0 :                         s_len = path_len;
     774             :                 }
     775             : 
     776             :                 /* we have to test if path is part of DOCUMENT_ROOT.
     777             :                   if it is inside the docroot, we scan the tree up to the docroot
     778             :                         to find more user.ini, if not we only scan the current path.
     779             :                   */
     780             : #ifdef PHP_WIN32
     781             :                 if (strnicmp(s1, s2, s_len) == 0) {
     782             : #else
     783           0 :                 if (strncmp(s1, s2, s_len) == 0) {
     784             : #endif
     785           0 :                         ptr = s2 + start;  /* start is the point where doc_root ends! */
     786           0 :                         while ((ptr = strchr(ptr, DEFAULT_SLASH)) != NULL) {
     787           0 :                                 *ptr = 0;
     788           0 :                                 php_parse_user_ini_file(path, PG(user_ini_filename), entry->user_config);
     789           0 :                                 *ptr = '/';
     790           0 :                                 ptr++;
     791             :                         }
     792             :                 } else {
     793           0 :                         php_parse_user_ini_file(path, PG(user_ini_filename), entry->user_config);
     794             :                 }
     795             : 
     796           0 :                 if (real_path) {
     797           0 :                         efree(real_path);
     798             :                 }
     799           0 :                 entry->expires = request_time + PG(user_ini_cache_ttl);
     800             :         }
     801             : 
     802             :         /* Activate ini entries with values from the user config hash */
     803           0 :         php_ini_activate_config(entry->user_config, PHP_INI_PERDIR, PHP_INI_STAGE_HTACCESS);
     804             : }
     805             : /* }}} */
     806             : 
     807         406 : static int sapi_cgi_activate(void)
     808             : {
     809             :         char *path, *doc_root, *server_name;
     810             :         size_t path_len, doc_root_len, server_name_len;
     811             : 
     812             :         /* PATH_TRANSLATED should be defined at this stage but better safe than sorry :) */
     813         406 :         if (!SG(request_info).path_translated) {
     814           3 :                 return FAILURE;
     815             :         }
     816             : 
     817         403 :         if (php_ini_has_per_host_config()) {
     818             :                 /* Activate per-host-system-configuration defined in php.ini and stored into configuration_hash during startup */
     819           0 :                 if (fcgi_is_fastcgi()) {
     820           0 :                         fcgi_request *request = (fcgi_request*) SG(server_context);
     821             : 
     822           0 :                         server_name = FCGI_GETENV(request, "SERVER_NAME");
     823             :                 } else {
     824           0 :                         server_name = getenv("SERVER_NAME");
     825             :                 }
     826             :                 /* SERVER_NAME should also be defined at this stage..but better check it anyway */
     827           0 :                 if (server_name) {
     828           0 :                         server_name_len = strlen(server_name);
     829           0 :                         server_name = estrndup(server_name, server_name_len);
     830           0 :                         zend_str_tolower(server_name, server_name_len);
     831           0 :                         php_ini_activate_per_host_config(server_name, server_name_len);
     832           0 :                         efree(server_name);
     833             :                 }
     834             :         }
     835             : 
     836        1209 :         if (php_ini_has_per_dir_config() ||
     837         806 :                 (PG(user_ini_filename) && *PG(user_ini_filename))
     838             :         ) {
     839             :                 /* Prepare search path */
     840         403 :                 path_len = strlen(SG(request_info).path_translated);
     841             : 
     842             :                 /* Make sure we have trailing slash! */
     843         403 :                 if (!IS_SLASH(SG(request_info).path_translated[path_len])) {
     844         403 :                         path = emalloc(path_len + 2);
     845         403 :                         memcpy(path, SG(request_info).path_translated, path_len + 1);
     846         403 :                         path_len = zend_dirname(path, path_len);
     847         403 :                         path[path_len++] = DEFAULT_SLASH;
     848             :                 } else {
     849           0 :                         path = estrndup(SG(request_info).path_translated, path_len);
     850           0 :                         path_len = zend_dirname(path, path_len);
     851             :                 }
     852         403 :                 path[path_len] = 0;
     853             : 
     854             :                 /* Activate per-dir-system-configuration defined in php.ini and stored into configuration_hash during startup */
     855         403 :                 php_ini_activate_per_dir_config(path, path_len); /* Note: for global settings sake we check from root to path */
     856             : 
     857             :                 /* Load and activate user ini files in path starting from DOCUMENT_ROOT */
     858         403 :                 if (PG(user_ini_filename) && *PG(user_ini_filename)) {
     859         403 :                         if (fcgi_is_fastcgi()) {
     860           0 :                                 fcgi_request *request = (fcgi_request*) SG(server_context);
     861             : 
     862           0 :                                 doc_root = FCGI_GETENV(request, "DOCUMENT_ROOT");
     863             :                         } else {
     864         403 :                                 doc_root = getenv("DOCUMENT_ROOT");
     865             :                         }
     866             :                         /* DOCUMENT_ROOT should also be defined at this stage..but better check it anyway */
     867         403 :                         if (doc_root) {
     868           0 :                                 doc_root_len = strlen(doc_root);
     869           0 :                                 if (doc_root_len > 0 && IS_SLASH(doc_root[doc_root_len - 1])) {
     870           0 :                                         --doc_root_len;
     871             :                                 }
     872             : #ifdef PHP_WIN32
     873             :                                 /* paths on windows should be case-insensitive */
     874             :                                 doc_root = estrndup(doc_root, doc_root_len);
     875             :                                 zend_str_tolower(doc_root, doc_root_len);
     876             : #endif
     877           0 :                                 php_cgi_ini_activate_user_config(path, path_len, doc_root, doc_root_len, (doc_root_len > 0 && (doc_root_len - 1)));
     878             : 
     879             : #ifdef PHP_WIN32
     880             :                                 efree(doc_root);
     881             : #endif
     882             :                         }
     883             :                 }
     884             : 
     885         403 :                 efree(path);
     886             :         }
     887             : 
     888         403 :         return SUCCESS;
     889             : }
     890             : 
     891         809 : static int sapi_cgi_deactivate(void)
     892             : {
     893             :         /* flush only when SAPI was started. The reasons are:
     894             :                 1. SAPI Deactivate is called from two places: module init and request shutdown
     895             :                 2. When the first call occurs and the request is not set up, flush fails on FastCGI.
     896             :         */
     897         809 :         if (SG(sapi_started)) {
     898         403 :                 if (fcgi_is_fastcgi()) {
     899           0 :                         if (
     900             : #ifndef PHP_WIN32
     901           0 :                                 !parent &&
     902             : #endif
     903           0 :                                 !fcgi_finish_request((fcgi_request*)SG(server_context), 0)) {
     904           0 :                                 php_handle_aborted_connection();
     905             :                         }
     906             :                 } else {
     907         403 :                         sapi_cgi_flush(SG(server_context));
     908             :                 }
     909             :         }
     910         809 :         return SUCCESS;
     911             : }
     912             : 
     913         406 : static int php_cgi_startup(sapi_module_struct *sapi_module)
     914             : {
     915         406 :         if (php_module_startup(sapi_module, &cgi_module_entry, 1) == FAILURE) {
     916           0 :                 return FAILURE;
     917             :         }
     918         406 :         return SUCCESS;
     919             : }
     920             : 
     921             : /* {{{ sapi_module_struct cgi_sapi_module
     922             :  */
     923             : static sapi_module_struct cgi_sapi_module = {
     924             :         "cgi-fcgi",                                           /* name */
     925             :         "CGI/FastCGI",                                        /* pretty name */
     926             : 
     927             :         php_cgi_startup,                                /* startup */
     928             :         php_module_shutdown_wrapper,    /* shutdown */
     929             : 
     930             :         sapi_cgi_activate,                              /* activate */
     931             :         sapi_cgi_deactivate,                    /* deactivate */
     932             : 
     933             :         sapi_cgi_ub_write,                              /* unbuffered write */
     934             :         sapi_cgi_flush,                                 /* flush */
     935             :         NULL,                                                   /* get uid */
     936             :         sapi_cgi_getenv,                                /* getenv */
     937             : 
     938             :         php_error,                                              /* error handler */
     939             : 
     940             :         NULL,                                                   /* header handler */
     941             :         sapi_cgi_send_headers,                  /* send headers handler */
     942             :         NULL,                                                   /* send header handler */
     943             : 
     944             :         sapi_cgi_read_post,                             /* read POST data */
     945             :         sapi_cgi_read_cookies,                  /* read Cookies */
     946             : 
     947             :         sapi_cgi_register_variables,    /* register server variables */
     948             :         sapi_cgi_log_message,                   /* Log message */
     949             :         NULL,                                                   /* Get request time */
     950             :         NULL,                                                   /* Child terminate */
     951             : 
     952             :         STANDARD_SAPI_MODULE_PROPERTIES
     953             : };
     954             : /* }}} */
     955             : 
     956             : /* {{{ arginfo ext/standard/dl.c */
     957             : ZEND_BEGIN_ARG_INFO(arginfo_dl, 0)
     958             :         ZEND_ARG_INFO(0, extension_filename)
     959             : ZEND_END_ARG_INFO()
     960             : /* }}} */
     961             : 
     962             : static const zend_function_entry additional_functions[] = {
     963             :         ZEND_FE(dl, arginfo_dl)
     964             :         {NULL, NULL, NULL}
     965             : };
     966             : 
     967             : /* {{{ php_cgi_usage
     968             :  */
     969           0 : static void php_cgi_usage(char *argv0)
     970             : {
     971             :         char *prog;
     972             : 
     973           0 :         prog = strrchr(argv0, '/');
     974           0 :         if (prog) {
     975           0 :                 prog++;
     976             :         } else {
     977           0 :                 prog = "php";
     978             :         }
     979             : 
     980           0 :         php_printf(     "Usage: %s [-q] [-h] [-s] [-v] [-i] [-f <file>]\n"
     981             :                                 "       %s <file> [args...]\n"
     982             :                                 "  -a               Run interactively\n"
     983             :                                 "  -b <address:port>|<port> Bind Path for external FASTCGI Server mode\n"
     984             :                                 "  -C               Do not chdir to the script's directory\n"
     985             :                                 "  -c <path>|<file> Look for php.ini file in this directory\n"
     986             :                                 "  -n               No php.ini file will be used\n"
     987             :                                 "  -d foo[=bar]     Define INI entry foo with value 'bar'\n"
     988             :                                 "  -e               Generate extended information for debugger/profiler\n"
     989             :                                 "  -f <file>        Parse <file>.  Implies `-q'\n"
     990             :                                 "  -h               This help\n"
     991             :                                 "  -i               PHP information\n"
     992             :                                 "  -l               Syntax check only (lint)\n"
     993             :                                 "  -m               Show compiled in modules\n"
     994             :                                 "  -q               Quiet-mode.  Suppress HTTP Header output.\n"
     995             :                                 "  -s               Display colour syntax highlighted source.\n"
     996             :                                 "  -v               Version number\n"
     997             :                                 "  -w               Display source with stripped comments and whitespace.\n"
     998             :                                 "  -z <file>        Load Zend extension <file>.\n"
     999             :                                 "  -T <count>       Measure execution time of script repeated <count> times.\n",
    1000             :                                 prog, prog);
    1001           0 : }
    1002             : /* }}} */
    1003             : 
    1004             : /* {{{ is_valid_path
    1005             :  *
    1006             :  * some server configurations allow '..' to slip through in the
    1007             :  * translated path.   We'll just refuse to handle such a path.
    1008             :  */
    1009         219 : static int is_valid_path(const char *path)
    1010             : {
    1011         219 :         const char *p = path;
    1012             : 
    1013         219 :         if (UNEXPECTED(!p)) {
    1014           0 :                 return 0;
    1015             :         }
    1016         219 :         if (UNEXPECTED(*p == '.') && *(p+1) == '.' && (!*(p+2) || IS_SLASH(*(p+2)))) {
    1017           0 :                 return 0;
    1018             :         }
    1019       12143 :         while (*p) {
    1020       11705 :                 if (IS_SLASH(*p)) {
    1021        1552 :                         p++;
    1022        1552 :                         if (UNEXPECTED(*p == '.')) {
    1023           0 :                                 p++;
    1024           0 :                                 if (UNEXPECTED(*p == '.')) {
    1025           0 :                                         p++;
    1026           0 :                                         if (UNEXPECTED(!*p) || UNEXPECTED(IS_SLASH(*p))) {
    1027           0 :                                                 return 0;
    1028             :                                         }
    1029             :                                 }
    1030             :                         }
    1031             :                 }
    1032       11705 :                 p++;
    1033             :         }
    1034         219 :         return 1;
    1035             : }
    1036             : /* }}} */
    1037             : 
    1038             : #define CGI_GETENV(name) \
    1039             :         ((has_env) ? \
    1040             :                 FCGI_GETENV(request, name) : \
    1041             :         getenv(name))
    1042             : 
    1043             : #define CGI_PUTENV(name, value) \
    1044             :         ((has_env) ? \
    1045             :                 FCGI_PUTENV(request, name, value) : \
    1046             :                 _sapi_cgi_putenv(name, sizeof(name)-1, value))
    1047             : 
    1048             : /* {{{ init_request_info
    1049             : 
    1050             :   initializes request_info structure
    1051             : 
    1052             :   specificly in this section we handle proper translations
    1053             :   for:
    1054             : 
    1055             :   PATH_INFO
    1056             :         derived from the portion of the URI path following
    1057             :         the script name but preceding any query data
    1058             :         may be empty
    1059             : 
    1060             :   PATH_TRANSLATED
    1061             :     derived by taking any path-info component of the
    1062             :         request URI and performing any virtual-to-physical
    1063             :         translation appropriate to map it onto the server's
    1064             :         document repository structure
    1065             : 
    1066             :         empty if PATH_INFO is empty
    1067             : 
    1068             :         The env var PATH_TRANSLATED **IS DIFFERENT** than the
    1069             :         request_info.path_translated variable, the latter should
    1070             :         match SCRIPT_FILENAME instead.
    1071             : 
    1072             :   SCRIPT_NAME
    1073             :     set to a URL path that could identify the CGI script
    1074             :         rather than the interpreter.  PHP_SELF is set to this
    1075             : 
    1076             :   REQUEST_URI
    1077             :     uri section following the domain:port part of a URI
    1078             : 
    1079             :   SCRIPT_FILENAME
    1080             :     The virtual-to-physical translation of SCRIPT_NAME (as per
    1081             :         PATH_TRANSLATED)
    1082             : 
    1083             :   These settings are documented at
    1084             :   http://cgi-spec.golux.com/
    1085             : 
    1086             : 
    1087             :   Based on the following URL request:
    1088             : 
    1089             :   http://localhost/info.php/test?a=b
    1090             : 
    1091             :   should produce, which btw is the same as if
    1092             :   we were running under mod_cgi on apache (ie. not
    1093             :   using ScriptAlias directives):
    1094             : 
    1095             :   PATH_INFO=/test
    1096             :   PATH_TRANSLATED=/docroot/test
    1097             :   SCRIPT_NAME=/info.php
    1098             :   REQUEST_URI=/info.php/test?a=b
    1099             :   SCRIPT_FILENAME=/docroot/info.php
    1100             :   QUERY_STRING=a=b
    1101             : 
    1102             :   but what we get is (cgi/mod_fastcgi under apache):
    1103             : 
    1104             :   PATH_INFO=/info.php/test
    1105             :   PATH_TRANSLATED=/docroot/info.php/test
    1106             :   SCRIPT_NAME=/php/php-cgi  (from the Action setting I suppose)
    1107             :   REQUEST_URI=/info.php/test?a=b
    1108             :   SCRIPT_FILENAME=/path/to/php/bin/php-cgi  (Action setting translated)
    1109             :   QUERY_STRING=a=b
    1110             : 
    1111             :   Comments in the code below refer to using the above URL in a request
    1112             : 
    1113             :  */
    1114         406 : static void init_request_info(fcgi_request *request)
    1115             : {
    1116         406 :         int has_env = fcgi_has_env(request);
    1117         406 :         char *env_script_filename = CGI_GETENV("SCRIPT_FILENAME");
    1118         406 :         char *env_path_translated = CGI_GETENV("PATH_TRANSLATED");
    1119         406 :         char *script_path_translated = env_script_filename;
    1120             : 
    1121             :         /* some broken servers do not have script_filename or argv0
    1122             :          * an example, IIS configured in some ways.  then they do more
    1123             :          * broken stuff and set path_translated to the cgi script location */
    1124         406 :         if (!script_path_translated && env_path_translated) {
    1125           0 :                 script_path_translated = env_path_translated;
    1126             :         }
    1127             : 
    1128             :         /* initialize the defaults */
    1129         406 :         SG(request_info).path_translated = NULL;
    1130         406 :         SG(request_info).request_method = NULL;
    1131         406 :         SG(request_info).proto_num = 1000;
    1132         406 :         SG(request_info).query_string = NULL;
    1133         406 :         SG(request_info).request_uri = NULL;
    1134         406 :         SG(request_info).content_type = NULL;
    1135         406 :         SG(request_info).content_length = 0;
    1136         406 :         SG(sapi_headers).http_response_code = 200;
    1137             : 
    1138             :         /* script_path_translated being set is a good indication that
    1139             :          * we are running in a cgi environment, since it is always
    1140             :          * null otherwise.  otherwise, the filename
    1141             :          * of the script will be retreived later via argc/argv */
    1142         406 :         if (script_path_translated) {
    1143             :                 const char *auth;
    1144         219 :                 char *content_length = CGI_GETENV("CONTENT_LENGTH");
    1145         219 :                 char *content_type = CGI_GETENV("CONTENT_TYPE");
    1146         219 :                 char *env_path_info = CGI_GETENV("PATH_INFO");
    1147         219 :                 char *env_script_name = CGI_GETENV("SCRIPT_NAME");
    1148             : 
    1149             : #ifdef PHP_WIN32
    1150             :                 /* Hack for buggy IIS that sets incorrect PATH_INFO */
    1151             :                 char *env_server_software = CGI_GETENV("SERVER_SOFTWARE");
    1152             : 
    1153             :                 if (env_server_software &&
    1154             :                         env_script_name &&
    1155             :                         env_path_info &&
    1156             :                         strncmp(env_server_software, "Microsoft-IIS", sizeof("Microsoft-IIS")-1) == 0 &&
    1157             :                         strncmp(env_path_info, env_script_name, strlen(env_script_name)) == 0
    1158             :                 ) {
    1159             :                         env_path_info = CGI_PUTENV("ORIG_PATH_INFO", env_path_info);
    1160             :                         env_path_info += strlen(env_script_name);
    1161             :                         if (*env_path_info == 0) {
    1162             :                                 env_path_info = NULL;
    1163             :                         }
    1164             :                         env_path_info = CGI_PUTENV("PATH_INFO", env_path_info);
    1165             :                 }
    1166             : #endif
    1167             : 
    1168         219 :                 if (CGIG(fix_pathinfo)) {
    1169             :                         zend_stat_t st;
    1170         219 :                         char *real_path = NULL;
    1171         219 :                         char *env_redirect_url = CGI_GETENV("REDIRECT_URL");
    1172         219 :                         char *env_document_root = CGI_GETENV("DOCUMENT_ROOT");
    1173         219 :                         char *orig_path_translated = env_path_translated;
    1174         219 :                         char *orig_path_info = env_path_info;
    1175         219 :                         char *orig_script_name = env_script_name;
    1176         219 :                         char *orig_script_filename = env_script_filename;
    1177             :                         size_t script_path_translated_len;
    1178             : 
    1179         219 :                         if (!env_document_root && PG(doc_root)) {
    1180           0 :                                 env_document_root = CGI_PUTENV("DOCUMENT_ROOT", PG(doc_root));
    1181             :                                 /* fix docroot */
    1182             :                                 TRANSLATE_SLASHES(env_document_root);
    1183             :                         }
    1184             : 
    1185         219 :                         if (env_path_translated != NULL && env_redirect_url != NULL &&
    1186             :                             env_path_translated != script_path_translated &&
    1187           0 :                             strcmp(env_path_translated, script_path_translated) != 0) {
    1188             :                                 /*
    1189             :                                  * pretty much apache specific.  If we have a redirect_url
    1190             :                                  * then our script_filename and script_name point to the
    1191             :                                  * php executable
    1192             :                                  */
    1193           0 :                                 script_path_translated = env_path_translated;
    1194             :                                 /* we correct SCRIPT_NAME now in case we don't have PATH_INFO */
    1195           0 :                                 env_script_name = env_redirect_url;
    1196             :                         }
    1197             : 
    1198             : #ifdef __riscos__
    1199             :                         /* Convert path to unix format*/
    1200             :                         __riscosify_control |= __RISCOSIFY_DONT_CHECK_DIR;
    1201             :                         script_path_translated = __unixify(script_path_translated, 0, NULL, 1, 0);
    1202             : #endif
    1203             : 
    1204             :                         /*
    1205             :                          * if the file doesn't exist, try to extract PATH_INFO out
    1206             :                          * of it by stat'ing back through the '/'
    1207             :                          * this fixes url's like /info.php/test
    1208             :                          */
    1209         661 :                         if (script_path_translated &&
    1210             :                                 (script_path_translated_len = strlen(script_path_translated)) > 0 &&
    1211         219 :                                 (script_path_translated[script_path_translated_len-1] == '/' ||
    1212             : #ifdef PHP_WIN32
    1213             :                                 script_path_translated[script_path_translated_len-1] == '\\' ||
    1214             : #endif
    1215         219 :                                 (real_path = tsrm_realpath(script_path_translated, NULL)) == NULL)
    1216             :                         ) {
    1217           4 :                                 char *pt = estrndup(script_path_translated, script_path_translated_len);
    1218           4 :                                 size_t len = script_path_translated_len;
    1219             :                                 char *ptr;
    1220             : 
    1221           8 :                                 while ((ptr = strrchr(pt, '/')) || (ptr = strrchr(pt, '\\'))) {
    1222           4 :                                         *ptr = 0;
    1223           4 :                                         if (zend_stat(pt, &st) == 0 && S_ISREG(st.st_mode)) {
    1224             :                                                 /*
    1225             :                                                  * okay, we found the base script!
    1226             :                                                  * work out how many chars we had to strip off;
    1227             :                                                  * then we can modify PATH_INFO
    1228             :                                                  * accordingly
    1229             :                                                  *
    1230             :                                                  * we now have the makings of
    1231             :                                                  * PATH_INFO=/test
    1232             :                                                  * SCRIPT_FILENAME=/docroot/info.php
    1233             :                                                  *
    1234             :                                                  * we now need to figure out what docroot is.
    1235             :                                                  * if DOCUMENT_ROOT is set, this is easy, otherwise,
    1236             :                                                  * we have to play the game of hide and seek to figure
    1237             :                                                  * out what SCRIPT_NAME should be
    1238             :                                                  */
    1239           4 :                                                 size_t slen = len - strlen(pt);
    1240           4 :                                                 size_t pilen = env_path_info ? strlen(env_path_info) : 0;
    1241           4 :                                                 char *path_info = env_path_info ? env_path_info + pilen - slen : NULL;
    1242             : 
    1243           4 :                                                 if (orig_path_info != path_info) {
    1244           0 :                                                         if (orig_path_info) {
    1245             :                                                                 char old;
    1246             : 
    1247           0 :                                                                 CGI_PUTENV("ORIG_PATH_INFO", orig_path_info);
    1248           0 :                                                                 old = path_info[0];
    1249           0 :                                                                 path_info[0] = 0;
    1250           0 :                                                                 if (!orig_script_name ||
    1251           0 :                                                                         strcmp(orig_script_name, env_path_info) != 0) {
    1252           0 :                                                                         if (orig_script_name) {
    1253           0 :                                                                                 CGI_PUTENV("ORIG_SCRIPT_NAME", orig_script_name);
    1254             :                                                                         }
    1255           0 :                                                                         SG(request_info).request_uri = CGI_PUTENV("SCRIPT_NAME", env_path_info);
    1256             :                                                                 } else {
    1257           0 :                                                                         SG(request_info).request_uri = orig_script_name;
    1258             :                                                                 }
    1259           0 :                                                                 path_info[0] = old;
    1260             :                                                         }
    1261           0 :                                                         env_path_info = CGI_PUTENV("PATH_INFO", path_info);
    1262             :                                                 }
    1263           8 :                                                 if (!orig_script_filename ||
    1264           4 :                                                         strcmp(orig_script_filename, pt) != 0) {
    1265           4 :                                                         if (orig_script_filename) {
    1266           4 :                                                                 CGI_PUTENV("ORIG_SCRIPT_FILENAME", orig_script_filename);
    1267             :                                                         }
    1268           4 :                                                         script_path_translated = CGI_PUTENV("SCRIPT_FILENAME", pt);
    1269             :                                                 }
    1270             :                                                 TRANSLATE_SLASHES(pt);
    1271             : 
    1272             :                                                 /* figure out docroot
    1273             :                                                  * SCRIPT_FILENAME minus SCRIPT_NAME
    1274             :                                                  */
    1275           4 :                                                 if (env_document_root) {
    1276           0 :                                                         size_t l = strlen(env_document_root);
    1277           0 :                                                         size_t path_translated_len = 0;
    1278           0 :                                                         char *path_translated = NULL;
    1279             : 
    1280           0 :                                                         if (l && env_document_root[l - 1] == '/') {
    1281           0 :                                                                 --l;
    1282             :                                                         }
    1283             : 
    1284             :                                                         /* we have docroot, so we should have:
    1285             :                                                          * DOCUMENT_ROOT=/docroot
    1286             :                                                          * SCRIPT_FILENAME=/docroot/info.php
    1287             :                                                          */
    1288             : 
    1289             :                                                         /* PATH_TRANSLATED = DOCUMENT_ROOT + PATH_INFO */
    1290           0 :                                                         path_translated_len = l + (env_path_info ? strlen(env_path_info) : 0);
    1291           0 :                                                         path_translated = (char *) emalloc(path_translated_len + 1);
    1292           0 :                                                         memcpy(path_translated, env_document_root, l);
    1293           0 :                                                         if (env_path_info) {
    1294           0 :                                                                 memcpy(path_translated + l, env_path_info, (path_translated_len - l));
    1295             :                                                         }
    1296           0 :                                                         path_translated[path_translated_len] = '\0';
    1297           0 :                                                         if (orig_path_translated) {
    1298           0 :                                                                 CGI_PUTENV("ORIG_PATH_TRANSLATED", orig_path_translated);
    1299             :                                                         }
    1300           0 :                                                         env_path_translated = CGI_PUTENV("PATH_TRANSLATED", path_translated);
    1301           0 :                                                         efree(path_translated);
    1302           4 :                                                 } else if (     env_script_name &&
    1303             :                                                                         strstr(pt, env_script_name)
    1304           0 :                                                 ) {
    1305             :                                                         /* PATH_TRANSLATED = PATH_TRANSLATED - SCRIPT_NAME + PATH_INFO */
    1306           0 :                                                         size_t ptlen = strlen(pt) - strlen(env_script_name);
    1307           0 :                                                         size_t path_translated_len = ptlen + (env_path_info ? strlen(env_path_info) : 0);
    1308           0 :                                                         char *path_translated = NULL;
    1309             : 
    1310           0 :                                                         path_translated = (char *) emalloc(path_translated_len + 1);
    1311           0 :                                                         memcpy(path_translated, pt, ptlen);
    1312           0 :                                                         if (env_path_info) {
    1313           0 :                                                                 memcpy(path_translated + ptlen, env_path_info, path_translated_len - ptlen);
    1314             :                                                         }
    1315           0 :                                                         path_translated[path_translated_len] = '\0';
    1316           0 :                                                         if (orig_path_translated) {
    1317           0 :                                                                 CGI_PUTENV("ORIG_PATH_TRANSLATED", orig_path_translated);
    1318             :                                                         }
    1319           0 :                                                         env_path_translated = CGI_PUTENV("PATH_TRANSLATED", path_translated);
    1320           0 :                                                         efree(path_translated);
    1321             :                                                 }
    1322           4 :                                                 break;
    1323             :                                         }
    1324             :                                 }
    1325           4 :                                 if (!ptr) {
    1326             :                                         /*
    1327             :                                          * if we stripped out all the '/' and still didn't find
    1328             :                                          * a valid path... we will fail, badly. of course we would
    1329             :                                          * have failed anyway... we output 'no input file' now.
    1330             :                                          */
    1331           0 :                                         if (orig_script_filename) {
    1332           0 :                                                 CGI_PUTENV("ORIG_SCRIPT_FILENAME", orig_script_filename);
    1333             :                                         }
    1334           0 :                                         script_path_translated = CGI_PUTENV("SCRIPT_FILENAME", NULL);
    1335           0 :                                         SG(sapi_headers).http_response_code = 404;
    1336             :                                 }
    1337           4 :                                 if (!SG(request_info).request_uri) {
    1338           8 :                                         if (!orig_script_name ||
    1339           0 :                                                 strcmp(orig_script_name, env_script_name) != 0) {
    1340           4 :                                                 if (orig_script_name) {
    1341           0 :                                                         CGI_PUTENV("ORIG_SCRIPT_NAME", orig_script_name);
    1342             :                                                 }
    1343           4 :                                                 SG(request_info).request_uri = CGI_PUTENV("SCRIPT_NAME", env_script_name);
    1344             :                                         } else {
    1345           0 :                                                 SG(request_info).request_uri = orig_script_name;
    1346             :                                         }
    1347             :                                 }
    1348           4 :                                 if (pt) {
    1349           4 :                                         efree(pt);
    1350             :                                 }
    1351             :                         } else {
    1352             :                                 /* make sure path_info/translated are empty */
    1353         215 :                                 if (!orig_script_filename ||
    1354             :                                         (script_path_translated != orig_script_filename &&
    1355           0 :                                         strcmp(script_path_translated, orig_script_filename) != 0)) {
    1356           0 :                                         if (orig_script_filename) {
    1357           0 :                                                 CGI_PUTENV("ORIG_SCRIPT_FILENAME", orig_script_filename);
    1358             :                                         }
    1359           0 :                                         script_path_translated = CGI_PUTENV("SCRIPT_FILENAME", script_path_translated);
    1360             :                                 }
    1361         215 :                                 if (env_redirect_url) {
    1362           0 :                                         if (orig_path_info) {
    1363           0 :                                                 CGI_PUTENV("ORIG_PATH_INFO", orig_path_info);
    1364           0 :                                                 CGI_PUTENV("PATH_INFO", NULL);
    1365             :                                         }
    1366           0 :                                         if (orig_path_translated) {
    1367           0 :                                                 CGI_PUTENV("ORIG_PATH_TRANSLATED", orig_path_translated);
    1368           0 :                                                 CGI_PUTENV("PATH_TRANSLATED", NULL);
    1369             :                                         }
    1370             :                                 }
    1371         215 :                                 if (env_script_name != orig_script_name) {
    1372           0 :                                         if (orig_script_name) {
    1373           0 :                                                 CGI_PUTENV("ORIG_SCRIPT_NAME", orig_script_name);
    1374             :                                         }
    1375           0 :                                         SG(request_info).request_uri = CGI_PUTENV("SCRIPT_NAME", env_script_name);
    1376             :                                 } else {
    1377         215 :                                         SG(request_info).request_uri = env_script_name;
    1378             :                                 }
    1379         215 :                                 efree(real_path);
    1380             :                         }
    1381             :                 } else {
    1382             :                         /* pre 4.3 behaviour, shouldn't be used but provides BC */
    1383           0 :                         if (env_path_info) {
    1384           0 :                                 SG(request_info).request_uri = env_path_info;
    1385             :                         } else {
    1386           0 :                                 SG(request_info).request_uri = env_script_name;
    1387             :                         }
    1388           0 :                         if (!CGIG(discard_path) && env_path_translated) {
    1389           0 :                                 script_path_translated = env_path_translated;
    1390             :                         }
    1391             :                 }
    1392             : 
    1393         219 :                 if (is_valid_path(script_path_translated)) {
    1394         219 :                         SG(request_info).path_translated = estrdup(script_path_translated);
    1395             :                 }
    1396             : 
    1397         219 :                 SG(request_info).request_method = CGI_GETENV("REQUEST_METHOD");
    1398             :                 /* FIXME - Work out proto_num here */
    1399         219 :                 SG(request_info).query_string = CGI_GETENV("QUERY_STRING");
    1400         219 :                 SG(request_info).content_type = (content_type ? content_type : "" );
    1401         219 :                 SG(request_info).content_length = (content_length ? atol(content_length) : 0);
    1402             : 
    1403             :                 /* The CGI RFC allows servers to pass on unvalidated Authorization data */
    1404         219 :                 auth = CGI_GETENV("HTTP_AUTHORIZATION");
    1405         219 :                 php_handle_auth_data(auth);
    1406             :         }
    1407         406 : }
    1408             : /* }}} */
    1409             : 
    1410             : #ifndef PHP_WIN32
    1411             : /**
    1412             :  * Clean up child processes upon exit
    1413             :  */
    1414           0 : void fastcgi_cleanup(int signal)
    1415             : {
    1416             : #ifdef DEBUG_FASTCGI
    1417             :         fprintf(stderr, "FastCGI shutdown, pid %d\n", getpid());
    1418             : #endif
    1419             : 
    1420           0 :         sigaction(SIGTERM, &old_term, 0);
    1421             : 
    1422             :         /* Kill all the processes in our process group */
    1423           0 :         kill(-pgroup, SIGTERM);
    1424             : 
    1425           0 :         if (parent && parent_waiting) {
    1426           0 :                 exit_signal = 1;
    1427             :         } else {
    1428           0 :                 exit(0);
    1429             :         }
    1430           0 : }
    1431             : #endif
    1432             : 
    1433             : PHP_INI_BEGIN()
    1434             :         STD_PHP_INI_ENTRY("cgi.rfc2616_headers",     "0",  PHP_INI_ALL,    OnUpdateBool,   rfc2616_headers, php_cgi_globals_struct, php_cgi_globals)
    1435             :         STD_PHP_INI_ENTRY("cgi.nph",                 "0",  PHP_INI_ALL,    OnUpdateBool,   nph, php_cgi_globals_struct, php_cgi_globals)
    1436             :         STD_PHP_INI_ENTRY("cgi.check_shebang_line",  "1",  PHP_INI_SYSTEM, OnUpdateBool,   check_shebang_line, php_cgi_globals_struct, php_cgi_globals)
    1437             :         STD_PHP_INI_ENTRY("cgi.force_redirect",      "1",  PHP_INI_SYSTEM, OnUpdateBool,   force_redirect, php_cgi_globals_struct, php_cgi_globals)
    1438             :         STD_PHP_INI_ENTRY("cgi.redirect_status_env", NULL, PHP_INI_SYSTEM, OnUpdateString, redirect_status_env, php_cgi_globals_struct, php_cgi_globals)
    1439             :         STD_PHP_INI_ENTRY("cgi.fix_pathinfo",        "1",  PHP_INI_SYSTEM, OnUpdateBool,   fix_pathinfo, php_cgi_globals_struct, php_cgi_globals)
    1440             :         STD_PHP_INI_ENTRY("cgi.discard_path",        "0",  PHP_INI_SYSTEM, OnUpdateBool,   discard_path, php_cgi_globals_struct, php_cgi_globals)
    1441             :         STD_PHP_INI_ENTRY("fastcgi.logging",         "1",  PHP_INI_SYSTEM, OnUpdateBool,   fcgi_logging, php_cgi_globals_struct, php_cgi_globals)
    1442             : #ifdef PHP_WIN32
    1443             :         STD_PHP_INI_ENTRY("fastcgi.impersonate",     "0",  PHP_INI_SYSTEM, OnUpdateBool,   impersonate, php_cgi_globals_struct, php_cgi_globals)
    1444             : #endif
    1445             : PHP_INI_END()
    1446             : 
    1447             : /* {{{ php_cgi_globals_ctor
    1448             :  */
    1449         406 : static void php_cgi_globals_ctor(php_cgi_globals_struct *php_cgi_globals)
    1450             : {
    1451             : #ifdef ZTS
    1452             :         ZEND_TSRMLS_CACHE_UPDATE();
    1453             : #endif
    1454         406 :         php_cgi_globals->rfc2616_headers = 0;
    1455         406 :         php_cgi_globals->nph = 0;
    1456         406 :         php_cgi_globals->check_shebang_line = 1;
    1457         406 :         php_cgi_globals->force_redirect = 1;
    1458         406 :         php_cgi_globals->redirect_status_env = NULL;
    1459         406 :         php_cgi_globals->fix_pathinfo = 1;
    1460         406 :         php_cgi_globals->discard_path = 0;
    1461         406 :         php_cgi_globals->fcgi_logging = 1;
    1462             : #ifdef PHP_WIN32
    1463             :         php_cgi_globals->impersonate = 0;
    1464             : #endif
    1465         406 :         zend_hash_init(&php_cgi_globals->user_config_cache, 8, NULL, user_config_cache_entry_dtor, 1);
    1466         406 : }
    1467             : /* }}} */
    1468             : 
    1469             : /* {{{ PHP_MINIT_FUNCTION
    1470             :  */
    1471         406 : static PHP_MINIT_FUNCTION(cgi)
    1472             : {
    1473             : #ifdef ZTS
    1474             :         ts_allocate_id(&php_cgi_globals_id, sizeof(php_cgi_globals_struct), (ts_allocate_ctor) php_cgi_globals_ctor, NULL);
    1475             : #else
    1476         406 :         php_cgi_globals_ctor(&php_cgi_globals);
    1477             : #endif
    1478         406 :         REGISTER_INI_ENTRIES();
    1479         406 :         return SUCCESS;
    1480             : }
    1481             : /* }}} */
    1482             : 
    1483             : /* {{{ PHP_MSHUTDOWN_FUNCTION
    1484             :  */
    1485         403 : static PHP_MSHUTDOWN_FUNCTION(cgi)
    1486             : {
    1487         403 :         zend_hash_destroy(&CGIG(user_config_cache));
    1488             : 
    1489         403 :         UNREGISTER_INI_ENTRIES();
    1490         403 :         return SUCCESS;
    1491             : }
    1492             : /* }}} */
    1493             : 
    1494             : /* {{{ PHP_MINFO_FUNCTION
    1495             :  */
    1496           1 : static PHP_MINFO_FUNCTION(cgi)
    1497             : {
    1498           1 :         DISPLAY_INI_ENTRIES();
    1499           1 : }
    1500             : /* }}} */
    1501             : 
    1502           0 : PHP_FUNCTION(apache_child_terminate) /* {{{ */
    1503             : {
    1504           0 :         if (zend_parse_parameters_none()) {
    1505           0 :                 return;
    1506             :         }
    1507           0 :         if (fcgi_is_fastcgi()) {
    1508           0 :                 fcgi_terminate();
    1509             :         }
    1510             : }
    1511             : /* }}} */
    1512             : 
    1513           0 : static void add_request_header(char *var, unsigned int var_len, char *val, unsigned int val_len, void *arg) /* {{{ */
    1514             : {
    1515           0 :         zval *return_value = (zval*)arg;
    1516           0 :         char *str = NULL;
    1517             :         char *p;
    1518             :         ALLOCA_FLAG(use_heap)
    1519             : 
    1520           0 :         if (var_len > 5 &&
    1521           0 :             var[0] == 'H' &&
    1522           0 :             var[1] == 'T' &&
    1523           0 :             var[2] == 'T' &&
    1524           0 :             var[3] == 'P' &&
    1525           0 :             var[4] == '_') {
    1526             : 
    1527           0 :                 var_len -= 5;
    1528           0 :                 p = var + 5;
    1529           0 :                 var = str = do_alloca(var_len + 1, use_heap);
    1530           0 :                 *str++ = *p++;
    1531           0 :                 while (*p) {
    1532           0 :                         if (*p == '_') {
    1533           0 :                                 *str++ = '-';
    1534           0 :                                 p++;
    1535           0 :                                 if (*p) {
    1536           0 :                                         *str++ = *p++;
    1537             :                                 }
    1538           0 :                         } else if (*p >= 'A' && *p <= 'Z') {
    1539           0 :                                 *str++ = (*p++ - 'A' + 'a');
    1540             :                         } else {
    1541           0 :                                 *str++ = *p++;
    1542             :                         }
    1543             :                 }
    1544           0 :                 *str = 0;
    1545           0 :         } else if (var_len == sizeof("CONTENT_TYPE")-1 &&
    1546           0 :                    memcmp(var, "CONTENT_TYPE", sizeof("CONTENT_TYPE")-1) == 0) {
    1547           0 :                 var = "Content-Type";
    1548           0 :         } else if (var_len == sizeof("CONTENT_LENGTH")-1 &&
    1549           0 :                    memcmp(var, "CONTENT_LENGTH", sizeof("CONTENT_LENGTH")-1) == 0) {
    1550           0 :                 var = "Content-Length";
    1551             :         } else {
    1552           0 :                 return;
    1553             :         }
    1554           0 :         add_assoc_stringl_ex(return_value, var, var_len, val, val_len);
    1555           0 :         if (str) {
    1556           0 :                 free_alloca(var, use_heap);
    1557             :         }
    1558             : }
    1559             : /* }}} */
    1560             : 
    1561           4 : PHP_FUNCTION(apache_request_headers) /* {{{ */
    1562             : {
    1563           4 :         if (zend_parse_parameters_none()) {
    1564           0 :                 return;
    1565             :         }
    1566           4 :         array_init(return_value);
    1567           4 :         if (fcgi_is_fastcgi()) {
    1568           0 :                 fcgi_request *request = (fcgi_request*) SG(server_context);
    1569             : 
    1570           0 :                 fcgi_loadenv(request, add_request_header, return_value);
    1571             :         } else {
    1572             :                 char buf[128];
    1573           4 :                 char **env, *p, *q, *var, *val, *t = buf;
    1574           4 :                 size_t alloc_size = sizeof(buf);
    1575             :                 zend_ulong var_len;
    1576             : 
    1577         187 :                 for (env = environ; env != NULL && *env != NULL; env++) {
    1578         183 :                         val = strchr(*env, '=');
    1579         183 :                         if (!val) {                             /* malformed entry? */
    1580           0 :                                 continue;
    1581             :                         }
    1582         183 :                         var_len = val - *env;
    1583         183 :                         if (var_len >= alloc_size) {
    1584           0 :                                 alloc_size = var_len + 64;
    1585           0 :                                 t = (t == buf ? emalloc(alloc_size): erealloc(t, alloc_size));
    1586             :                         }
    1587         183 :                         var = *env;
    1588         345 :                         if (var_len > 5 &&
    1589         147 :                             var[0] == 'H' &&
    1590           3 :                             var[1] == 'T' &&
    1591           3 :                             var[2] == 'T' &&
    1592           3 :                             var[3] == 'P' &&
    1593           3 :                             var[4] == '_') {
    1594             : 
    1595           3 :                                 var_len -= 5;
    1596             : 
    1597           3 :                                 if (var_len >= alloc_size) {
    1598           0 :                                         alloc_size = var_len + 64;
    1599           0 :                                         t = (t == buf ? emalloc(alloc_size): erealloc(t, alloc_size));
    1600             :                                 }
    1601           3 :                                 p = var + 5;
    1602             : 
    1603           3 :                                 var = q = t;
    1604             :                                 /* First char keep uppercase */
    1605           3 :                                 *q++ = *p++;
    1606          16 :                                 while (*p) {
    1607          13 :                                         if (*p == '=') {
    1608             :                                                 /* End of name */
    1609           3 :                                                 break;
    1610          10 :                                         } else if (*p == '_') {
    1611           3 :                                                 *q++ = '-';
    1612           3 :                                                 p++;
    1613             :                                                 /* First char after - keep uppercase */
    1614           3 :                                                 if (*p && *p!='=') {
    1615           2 :                                                         *q++ = *p++;
    1616             :                                                 }
    1617          14 :                                         } else if (*p >= 'A' && *p <= 'Z') {
    1618             :                                                 /* lowercase */
    1619           7 :                                                 *q++ = (*p++ - 'A' + 'a');
    1620             :                                         } else {
    1621           0 :                                                 *q++ = *p++;
    1622             :                                         }
    1623             :                                 }
    1624           3 :                                 *q = 0;
    1625         184 :                         } else if (var_len == sizeof("CONTENT_TYPE")-1 &&
    1626           4 :                                    memcmp(var, "CONTENT_TYPE", sizeof("CONTENT_TYPE")-1) == 0) {
    1627           0 :                                 var = "Content-Type";
    1628         188 :                         } else if (var_len == sizeof("CONTENT_LENGTH")-1 &&
    1629           8 :                                    memcmp(var, "CONTENT_LENGTH", sizeof("CONTENT_LENGTH")-1) == 0) {
    1630           0 :                                 var = "Content-Length";
    1631             :                         } else {
    1632         180 :                                 continue;
    1633             :                         }
    1634           3 :                         val++;
    1635           3 :                         add_assoc_string_ex(return_value, var, var_len, val);
    1636             :                 }
    1637           4 :                 if (t != buf && t != NULL) {
    1638           0 :                         efree(t);
    1639             :                 }
    1640             :         }
    1641             : }
    1642             : /* }}} */
    1643             : 
    1644           0 : static void add_response_header(sapi_header_struct *h, zval *return_value) /* {{{ */
    1645             : {
    1646             :         char *s, *p;
    1647           0 :         size_t len = 0;
    1648             :         ALLOCA_FLAG(use_heap)
    1649             : 
    1650           0 :         if (h->header_len > 0) {
    1651           0 :                 p = strchr(h->header, ':');
    1652           0 :                 if (NULL != p) {
    1653           0 :                         len = p - h->header;
    1654             :                 }
    1655           0 :                 if (len > 0) {
    1656             :                         do {
    1657           0 :                                 len--;
    1658           0 :                         } while (len != 0 && (h->header[len-1] == ' ' || h->header[len-1] == '\t'));
    1659           0 :                         if (len) {
    1660           0 :                                 s = do_alloca(len + 1, use_heap);
    1661           0 :                                 memcpy(s, h->header, len);
    1662           0 :                                 s[len] = 0;
    1663             :                                 do {
    1664           0 :                                         p++;
    1665           0 :                                 } while (*p == ' ' || *p == '\t');
    1666           0 :                                 add_assoc_stringl_ex(return_value, s, len, p, h->header_len - (p - h->header));
    1667           0 :                                 free_alloca(s, use_heap);
    1668             :                         }
    1669             :                 }
    1670             :         }
    1671           0 : }
    1672             : /* }}} */
    1673             : 
    1674           0 : PHP_FUNCTION(apache_response_headers) /* {{{ */
    1675             : {
    1676           0 :         if (zend_parse_parameters_none() == FAILURE) {
    1677           0 :                 return;
    1678             :         }
    1679             : 
    1680           0 :         array_init(return_value);
    1681           0 :         zend_llist_apply_with_argument(&SG(sapi_headers).headers, (llist_apply_with_arg_func_t)add_response_header, return_value);
    1682             : }
    1683             : /* }}} */
    1684             : 
    1685             : ZEND_BEGIN_ARG_INFO(arginfo_no_args, 0)
    1686             : ZEND_END_ARG_INFO()
    1687             : 
    1688             : const zend_function_entry cgi_functions[] = {
    1689             :         PHP_FE(apache_child_terminate, arginfo_no_args)
    1690             :         PHP_FE(apache_request_headers, arginfo_no_args)
    1691             :         PHP_FE(apache_response_headers, arginfo_no_args)
    1692             :         PHP_FALIAS(getallheaders, apache_request_headers, arginfo_no_args)
    1693             :         {NULL, NULL, NULL}
    1694             : };
    1695             : 
    1696             : static zend_module_entry cgi_module_entry = {
    1697             :         STANDARD_MODULE_HEADER,
    1698             :         "cgi-fcgi",
    1699             :         cgi_functions,
    1700             :         PHP_MINIT(cgi),
    1701             :         PHP_MSHUTDOWN(cgi),
    1702             :         NULL,
    1703             :         NULL,
    1704             :         PHP_MINFO(cgi),
    1705             :         NO_VERSION_YET,
    1706             :         STANDARD_MODULE_PROPERTIES
    1707             : };
    1708             : 
    1709             : /* {{{ main
    1710             :  */
    1711         406 : int main(int argc, char *argv[])
    1712             : {
    1713         406 :         int free_query_string = 0;
    1714         406 :         int exit_status = SUCCESS;
    1715         406 :         int cgi = 0, c, i;
    1716             :         size_t len;
    1717             :         zend_file_handle file_handle;
    1718             :         char *s;
    1719             : 
    1720             :         /* temporary locals */
    1721         406 :         int behavior = PHP_MODE_STANDARD;
    1722         406 :         int no_headers = 0;
    1723         406 :         int orig_optind = php_optind;
    1724         406 :         char *orig_optarg = php_optarg;
    1725         406 :         char *script_file = NULL;
    1726         406 :         size_t ini_entries_len = 0;
    1727             :         /* end of temporary locals */
    1728             : 
    1729             : #ifdef ZTS
    1730             :         void ***tsrm_ls;
    1731             : #endif
    1732             : 
    1733         406 :         int max_requests = 500;
    1734         406 :         int requests = 0;
    1735             :         int fastcgi;
    1736         406 :         char *bindpath = NULL;
    1737         406 :         int fcgi_fd = 0;
    1738         406 :         fcgi_request *request = NULL;
    1739         406 :         int warmup_repeats = 0;
    1740         406 :         int repeats = 1;
    1741         406 :         int benchmark = 0;
    1742             : #if HAVE_GETTIMEOFDAY
    1743             :         struct timeval start, end;
    1744             : #else
    1745             :         time_t start, end;
    1746             : #endif
    1747             : #ifndef PHP_WIN32
    1748         406 :         int status = 0;
    1749             : #endif
    1750             :         char *query_string;
    1751             :         char *decoded_query_string;
    1752         406 :         int skip_getopt = 0;
    1753             : 
    1754             : #if 0 && defined(PHP_DEBUG)
    1755             :         /* IIS is always making things more difficult.  This allows
    1756             :          * us to stop PHP and attach a debugger before much gets started */
    1757             :         {
    1758             :                 char szMessage [256];
    1759             :                 wsprintf (szMessage, "Please attach a debugger to the process 0x%X [%d] (%s) and click OK", GetCurrentProcessId(), GetCurrentProcessId(), argv[0]);
    1760             :                 MessageBox(NULL, szMessage, "CGI Debug Time!", MB_OK|MB_SERVICE_NOTIFICATION);
    1761             :         }
    1762             : #endif
    1763             : 
    1764             : #ifdef HAVE_SIGNAL_H
    1765             : #if defined(SIGPIPE) && defined(SIG_IGN)
    1766         406 :         signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE in standalone mode so
    1767             :                                                                 that sockets created via fsockopen()
    1768             :                                                                 don't kill PHP if the remote site
    1769             :                                                                 closes it.  in apache|apxs mode apache
    1770             :                                                                 does that for us!  thies@thieso.net
    1771             :                                                                 20000419 */
    1772             : #endif
    1773             : #endif
    1774             : 
    1775             : #ifdef ZTS
    1776             :         tsrm_startup(1, 1, 0, NULL);
    1777             :         tsrm_ls = ts_resource(0);
    1778             :         ZEND_TSRMLS_CACHE_UPDATE();
    1779             : #endif
    1780             : 
    1781         406 :         sapi_startup(&cgi_sapi_module);
    1782         406 :         fastcgi = fcgi_is_fastcgi();
    1783         406 :         cgi_sapi_module.php_ini_path_override = NULL;
    1784             : 
    1785             : #ifdef PHP_WIN32
    1786             :         _fmode = _O_BINARY; /* sets default for file streams to binary */
    1787             :         setmode(_fileno(stdin),  O_BINARY);     /* make the stdio mode be binary */
    1788             :         setmode(_fileno(stdout), O_BINARY);     /* make the stdio mode be binary */
    1789             :         setmode(_fileno(stderr), O_BINARY);     /* make the stdio mode be binary */
    1790             : #endif
    1791             : 
    1792         406 :         if (!fastcgi) {
    1793             :                 /* Make sure we detect we are a cgi - a bit redundancy here,
    1794             :                  * but the default case is that we have to check only the first one. */
    1795        1624 :                 if (getenv("SERVER_SOFTWARE") ||
    1796         406 :                         getenv("SERVER_NAME") ||
    1797         406 :                         getenv("GATEWAY_INTERFACE") ||
    1798             :                         getenv("REQUEST_METHOD")
    1799         406 :                 ) {
    1800         215 :                         cgi = 1;
    1801             :                 }
    1802             :         }
    1803             : 
    1804         406 :         if((query_string = getenv("QUERY_STRING")) != NULL && strchr(query_string, '=') == NULL) {
    1805             :                 /* we've got query string that has no = - apache CGI will pass it to command line */
    1806             :                 unsigned char *p;
    1807           5 :                 decoded_query_string = strdup(query_string);
    1808           5 :                 php_url_decode(decoded_query_string, strlen(decoded_query_string));
    1809           5 :                 for (p = (unsigned char *)decoded_query_string; *p &&  *p <= ' '; p++) {
    1810             :                         /* skip all leading spaces */
    1811             :                 }
    1812           5 :                 if(*p == '-') {
    1813           0 :                         skip_getopt = 1;
    1814             :                 }
    1815           5 :                 free(decoded_query_string);
    1816             :         }
    1817             : 
    1818       14921 :         while (!skip_getopt && (c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) {
    1819       14109 :                 switch (c) {
    1820             :                         case 'c':
    1821         368 :                                 if (cgi_sapi_module.php_ini_path_override) {
    1822           0 :                                         free(cgi_sapi_module.php_ini_path_override);
    1823             :                                 }
    1824         368 :                                 cgi_sapi_module.php_ini_path_override = strdup(php_optarg);
    1825         368 :                                 break;
    1826             :                         case 'n':
    1827         406 :                                 cgi_sapi_module.php_ini_ignore = 1;
    1828         406 :                                 break;
    1829             :                         case 'd': {
    1830             :                                 /* define ini entries on command line */
    1831       12576 :                                 size_t len = strlen(php_optarg);
    1832             :                                 char *val;
    1833             : 
    1834       12576 :                                 if ((val = strchr(php_optarg, '='))) {
    1835       12576 :                                         val++;
    1836       13739 :                                         if (!isalnum(*val) && *val != '"' && *val != '\'' && *val != '\0') {
    1837        1163 :                                                 cgi_sapi_module.ini_entries = realloc(cgi_sapi_module.ini_entries, ini_entries_len + len + sizeof("\"\"\n\0"));
    1838        1163 :                                                 memcpy(cgi_sapi_module.ini_entries + ini_entries_len, php_optarg, (val - php_optarg));
    1839        1163 :                                                 ini_entries_len += (val - php_optarg);
    1840        1163 :                                                 memcpy(cgi_sapi_module.ini_entries + ini_entries_len, "\"", 1);
    1841        1163 :                                                 ini_entries_len++;
    1842        1163 :                                                 memcpy(cgi_sapi_module.ini_entries + ini_entries_len, val, len - (val - php_optarg));
    1843        1163 :                                                 ini_entries_len += len - (val - php_optarg);
    1844        1163 :                                                 memcpy(cgi_sapi_module.ini_entries + ini_entries_len, "\"\n\0", sizeof("\"\n\0"));
    1845        1163 :                                                 ini_entries_len += sizeof("\n\0\"") - 2;
    1846             :                                         } else {
    1847       11413 :                                                 cgi_sapi_module.ini_entries = realloc(cgi_sapi_module.ini_entries, ini_entries_len + len + sizeof("\n\0"));
    1848       11413 :                                                 memcpy(cgi_sapi_module.ini_entries + ini_entries_len, php_optarg, len);
    1849       11413 :                                                 memcpy(cgi_sapi_module.ini_entries + ini_entries_len + len, "\n\0", sizeof("\n\0"));
    1850       11413 :                                                 ini_entries_len += len + sizeof("\n\0") - 2;
    1851             :                                         }
    1852             :                                 } else {
    1853           0 :                                         cgi_sapi_module.ini_entries = realloc(cgi_sapi_module.ini_entries, ini_entries_len + len + sizeof("=1\n\0"));
    1854           0 :                                         memcpy(cgi_sapi_module.ini_entries + ini_entries_len, php_optarg, len);
    1855           0 :                                         memcpy(cgi_sapi_module.ini_entries + ini_entries_len + len, "=1\n\0", sizeof("=1\n\0"));
    1856           0 :                                         ini_entries_len += len + sizeof("=1\n\0") - 2;
    1857             :                                 }
    1858       12576 :                                 break;
    1859             :                         }
    1860             :                         /* if we're started on command line, check to see if
    1861             :                          * we are being started as an 'external' fastcgi
    1862             :                          * server by accepting a bindpath parameter. */
    1863             :                         case 'b':
    1864           0 :                                 if (!fastcgi) {
    1865           0 :                                         bindpath = strdup(php_optarg);
    1866             :                                 }
    1867           0 :                                 break;
    1868             :                         case 's': /* generate highlighted HTML from source */
    1869           3 :                                 behavior = PHP_MODE_HIGHLIGHT;
    1870             :                                 break;
    1871             :                 }
    1872             :         }
    1873         406 :         php_optind = orig_optind;
    1874         406 :         php_optarg = orig_optarg;
    1875             : 
    1876         406 :         if (fastcgi || bindpath) {
    1877             :                 /* Override SAPI callbacks */
    1878           0 :                 cgi_sapi_module.ub_write     = sapi_fcgi_ub_write;
    1879           0 :                 cgi_sapi_module.flush        = sapi_fcgi_flush;
    1880           0 :                 cgi_sapi_module.read_post    = sapi_fcgi_read_post;
    1881           0 :                 cgi_sapi_module.getenv       = sapi_fcgi_getenv;
    1882           0 :                 cgi_sapi_module.read_cookies = sapi_fcgi_read_cookies;
    1883             :         }
    1884             : 
    1885             : #ifdef ZTS
    1886             :         SG(request_info).path_translated = NULL;
    1887             : #endif
    1888             : 
    1889         406 :         cgi_sapi_module.executable_location = argv[0];
    1890         406 :         if (!cgi && !fastcgi && !bindpath) {
    1891         191 :                 cgi_sapi_module.additional_functions = additional_functions;
    1892             :         }
    1893             : 
    1894             :         /* startup after we get the above ini override se we get things right */
    1895         406 :         if (cgi_sapi_module.startup(&cgi_sapi_module) == FAILURE) {
    1896             : #ifdef ZTS
    1897             :                 tsrm_shutdown();
    1898             : #endif
    1899           0 :                 return FAILURE;
    1900             :         }
    1901             : 
    1902             :         /* check force_cgi after startup, so we have proper output */
    1903         406 :         if (cgi && CGIG(force_redirect)) {
    1904             :                 /* Apache will generate REDIRECT_STATUS,
    1905             :                  * Netscape and redirect.so will generate HTTP_REDIRECT_STATUS.
    1906             :                  * redirect.so and installation instructions available from
    1907             :                  * http://www.koehntopp.de/php.
    1908             :                  *   -- kk@netuse.de
    1909             :                  */
    1910         215 :                 if (!getenv("REDIRECT_STATUS") &&
    1911           0 :                         !getenv ("HTTP_REDIRECT_STATUS") &&
    1912             :                         /* this is to allow a different env var to be configured
    1913             :                          * in case some server does something different than above */
    1914           0 :                         (!CGIG(redirect_status_env) || !getenv(CGIG(redirect_status_env)))
    1915             :                 ) {
    1916           0 :                         zend_try {
    1917           0 :                                 SG(sapi_headers).http_response_code = 400;
    1918           0 :                                 PUTS("<b>Security Alert!</b> The PHP CGI cannot be accessed directly.\n\n\
    1919             : <p>This PHP CGI binary was compiled with force-cgi-redirect enabled.  This\n\
    1920             : means that a page will only be served up if the REDIRECT_STATUS CGI variable is\n\
    1921             : set, e.g. via an Apache Action directive.</p>\n\
    1922             : <p>For more information as to <i>why</i> this behaviour exists, see the <a href=\"http://php.net/security.cgi-bin\">\
    1923             : manual page for CGI security</a>.</p>\n\
    1924             : <p>For more information about changing this behaviour or re-enabling this webserver,\n\
    1925             : consult the installation file that came with this distribution, or visit \n\
    1926             : <a href=\"http://php.net/install.windows\">the manual page</a>.</p>\n");
    1927           0 :                         } zend_catch {
    1928           0 :                         } zend_end_try();
    1929             : #if defined(ZTS) && !defined(PHP_DEBUG)
    1930             :                         /* XXX we're crashing here in msvc6 debug builds at
    1931             :                          * php_message_handler_for_zend:839 because
    1932             :                          * SG(request_info).path_translated is an invalid pointer.
    1933             :                          * It still happens even though I set it to null, so something
    1934             :                          * weird is going on.
    1935             :                          */
    1936             :                         tsrm_shutdown();
    1937             : #endif
    1938           0 :                         return FAILURE;
    1939             :                 }
    1940             :         }
    1941             : 
    1942             : #ifndef HAVE_ATTRIBUTE_WEAK
    1943             :         fcgi_set_logger(fcgi_log);
    1944             : #endif
    1945             : 
    1946         406 :         if (bindpath) {
    1947           0 :                 int backlog = 128;
    1948           0 :                 if (getenv("PHP_FCGI_BACKLOG")) {
    1949           0 :                         backlog = atoi(getenv("PHP_FCGI_BACKLOG"));
    1950             :                 }
    1951           0 :                 fcgi_fd = fcgi_listen(bindpath, backlog);
    1952           0 :                 if (fcgi_fd < 0) {
    1953           0 :                         fprintf(stderr, "Couldn't create FastCGI listen socket on port %s\n", bindpath);
    1954             : #ifdef ZTS
    1955             :                         tsrm_shutdown();
    1956             : #endif
    1957           0 :                         return FAILURE;
    1958             :                 }
    1959           0 :                 fastcgi = fcgi_is_fastcgi();
    1960             :         }
    1961         406 :         if (fastcgi) {
    1962             :                 /* How many times to run PHP scripts before dying */
    1963           0 :                 if (getenv("PHP_FCGI_MAX_REQUESTS")) {
    1964           0 :                         max_requests = atoi(getenv("PHP_FCGI_MAX_REQUESTS"));
    1965           0 :                         if (max_requests < 0) {
    1966           0 :                                 fprintf(stderr, "PHP_FCGI_MAX_REQUESTS is not valid\n");
    1967           0 :                                 return FAILURE;
    1968             :                         }
    1969             :                 }
    1970             : 
    1971             :                 /* make php call us to get _ENV vars */
    1972           0 :                 php_php_import_environment_variables = php_import_environment_variables;
    1973           0 :                 php_import_environment_variables = cgi_php_import_environment_variables;
    1974             : 
    1975             :                 /* library is already initialized, now init our request */
    1976           0 :                 request = fcgi_init_request(fcgi_fd, NULL, NULL, NULL);
    1977             : 
    1978             : #ifndef PHP_WIN32
    1979             :                 /* Pre-fork, if required */
    1980           0 :                 if (getenv("PHP_FCGI_CHILDREN")) {
    1981           0 :                         char * children_str = getenv("PHP_FCGI_CHILDREN");
    1982           0 :                         children = atoi(children_str);
    1983           0 :                         if (children < 0) {
    1984           0 :                                 fprintf(stderr, "PHP_FCGI_CHILDREN is not valid\n");
    1985           0 :                                 return FAILURE;
    1986             :                         }
    1987           0 :                         fcgi_set_mgmt_var("FCGI_MAX_CONNS", sizeof("FCGI_MAX_CONNS")-1, children_str, strlen(children_str));
    1988             :                         /* This is the number of concurrent requests, equals FCGI_MAX_CONNS */
    1989           0 :                         fcgi_set_mgmt_var("FCGI_MAX_REQS",  sizeof("FCGI_MAX_REQS")-1,  children_str, strlen(children_str));
    1990             :                 } else {
    1991           0 :                         fcgi_set_mgmt_var("FCGI_MAX_CONNS", sizeof("FCGI_MAX_CONNS")-1, "1", sizeof("1")-1);
    1992           0 :                         fcgi_set_mgmt_var("FCGI_MAX_REQS",  sizeof("FCGI_MAX_REQS")-1,  "1", sizeof("1")-1);
    1993             :                 }
    1994             : 
    1995           0 :                 if (children) {
    1996           0 :                         int running = 0;
    1997             :                         pid_t pid;
    1998             : 
    1999             :                         /* Create a process group for ourself & children */
    2000           0 :                         setsid();
    2001           0 :                         pgroup = getpgrp();
    2002             : #ifdef DEBUG_FASTCGI
    2003             :                         fprintf(stderr, "Process group %d\n", pgroup);
    2004             : #endif
    2005             : 
    2006             :                         /* Set up handler to kill children upon exit */
    2007           0 :                         act.sa_flags = 0;
    2008           0 :                         act.sa_handler = fastcgi_cleanup;
    2009           0 :                         if (sigaction(SIGTERM, &act, &old_term) ||
    2010           0 :                                 sigaction(SIGINT,  &act, &old_int)  ||
    2011             :                                 sigaction(SIGQUIT, &act, &old_quit)
    2012           0 :                         ) {
    2013           0 :                                 perror("Can't set signals");
    2014           0 :                                 exit(1);
    2015             :                         }
    2016             : 
    2017           0 :                         if (fcgi_in_shutdown()) {
    2018           0 :                                 goto parent_out;
    2019             :                         }
    2020             : 
    2021           0 :                         while (parent) {
    2022             :                                 do {
    2023             : #ifdef DEBUG_FASTCGI
    2024             :                                         fprintf(stderr, "Forking, %d running\n", running);
    2025             : #endif
    2026           0 :                                         pid = fork();
    2027           0 :                                         switch (pid) {
    2028             :                                         case 0:
    2029             :                                                 /* One of the children.
    2030             :                                                  * Make sure we don't go round the
    2031             :                                                  * fork loop any more
    2032             :                                                  */
    2033           0 :                                                 parent = 0;
    2034             : 
    2035             :                                                 /* don't catch our signals */
    2036           0 :                                                 sigaction(SIGTERM, &old_term, 0);
    2037           0 :                                                 sigaction(SIGQUIT, &old_quit, 0);
    2038           0 :                                                 sigaction(SIGINT,  &old_int,  0);
    2039           0 :                                                 break;
    2040             :                                         case -1:
    2041           0 :                                                 perror("php (pre-forking)");
    2042           0 :                                                 exit(1);
    2043             :                                                 break;
    2044             :                                         default:
    2045             :                                                 /* Fine */
    2046           0 :                                                 running++;
    2047             :                                                 break;
    2048             :                                         }
    2049           0 :                                 } while (parent && (running < children));
    2050             : 
    2051           0 :                                 if (parent) {
    2052             : #ifdef DEBUG_FASTCGI
    2053             :                                         fprintf(stderr, "Wait for kids, pid %d\n", getpid());
    2054             : #endif
    2055           0 :                                         parent_waiting = 1;
    2056             :                                         while (1) {
    2057           0 :                                                 if (wait(&status) >= 0) {
    2058           0 :                                                         running--;
    2059           0 :                                                         break;
    2060           0 :                                                 } else if (exit_signal) {
    2061           0 :                                                         break;
    2062             :                                                 }
    2063           0 :                                         }
    2064           0 :                                         if (exit_signal) {
    2065             : #if 0
    2066             :                                                 while (running > 0) {
    2067             :                                                         while (wait(&status) < 0) {
    2068             :                                                         }
    2069             :                                                         running--;
    2070             :                                                 }
    2071             : #endif
    2072           0 :                                                 goto parent_out;
    2073             :                                         }
    2074             :                                 }
    2075             :                         }
    2076             :                 } else {
    2077           0 :                         parent = 0;
    2078             :                 }
    2079             : 
    2080             : #endif /* WIN32 */
    2081             :         }
    2082             : 
    2083         406 :         zend_first_try {
    2084       14921 :                 while (!skip_getopt && (c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 1, 2)) != -1) {
    2085       14109 :                         switch (c) {
    2086             :                                 case 'T':
    2087           0 :                                         benchmark = 1;
    2088             :                                         {
    2089           0 :                                                 char *comma = strchr(php_optarg, ',');
    2090           0 :                                                 if (comma) {
    2091           0 :                                                         warmup_repeats = atoi(php_optarg);
    2092           0 :                                                         repeats = atoi(comma + 1);
    2093             :                                                 } else {
    2094           0 :                                                         repeats = atoi(php_optarg);
    2095             :                                                 }
    2096             :                                         }
    2097             : #ifdef HAVE_GETTIMEOFDAY
    2098           0 :                                         gettimeofday(&start, NULL);
    2099             : #else
    2100             :                                         time(&start);
    2101             : #endif
    2102           0 :                                         break;
    2103             :                                 case 'h':
    2104             :                                 case '?':
    2105           0 :                                         if (request) {
    2106           0 :                                                 fcgi_destroy_request(request);
    2107             :                                         }
    2108           0 :                                         fcgi_shutdown();
    2109           0 :                                         no_headers = 1;
    2110           0 :                                         SG(headers_sent) = 1;
    2111           0 :                                         php_cgi_usage(argv[0]);
    2112           0 :                                         php_output_end_all();
    2113           0 :                                         exit_status = 0;
    2114           0 :                                         goto out;
    2115             :                         }
    2116             :                 }
    2117         406 :                 php_optind = orig_optind;
    2118         406 :                 php_optarg = orig_optarg;
    2119             : 
    2120             :                 /* start of FAST CGI loop */
    2121             :                 /* Initialise FastCGI request structure */
    2122             : #ifdef PHP_WIN32
    2123             :                 /* attempt to set security impersonation for fastcgi
    2124             :                  * will only happen on NT based OS, others will ignore it. */
    2125             :                 if (fastcgi && CGIG(impersonate)) {
    2126             :                         fcgi_impersonate();
    2127             :                 }
    2128             : #endif
    2129         812 :                 while (!fastcgi || fcgi_accept_request(request) >= 0) {
    2130         406 :                         SG(server_context) = fastcgi ? (void *)request : (void *) 1;
    2131         406 :                         init_request_info(request);
    2132             : 
    2133         406 :                         if (!cgi && !fastcgi) {
    2134        6589 :                                 while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) {
    2135        6208 :                                         switch (c) {
    2136             : 
    2137             :                                                 case 'a':       /* interactive mode */
    2138           2 :                                                         printf("Interactive mode enabled\n\n");
    2139           2 :                                                         break;
    2140             : 
    2141             :                                                 case 'C': /* don't chdir to the script directory */
    2142         153 :                                                         SG(options) |= SAPI_OPTION_NO_CHDIR;
    2143         153 :                                                         break;
    2144             : 
    2145             :                                                 case 'e': /* enable extended info output */
    2146           0 :                                                         CG(compiler_options) |= ZEND_COMPILE_EXTENDED_INFO;
    2147           0 :                                                         break;
    2148             : 
    2149             :                                                 case 'f': /* parse file */
    2150           6 :                                                         if (script_file) {
    2151           1 :                                                                 efree(script_file);
    2152             :                                                         }
    2153           6 :                                                         script_file = estrdup(php_optarg);
    2154           6 :                                                         no_headers = 1;
    2155           6 :                                                         break;
    2156             : 
    2157             :                                                 case 'i': /* php info & quit */
    2158           0 :                                                         if (script_file) {
    2159           0 :                                                                 efree(script_file);
    2160             :                                                         }
    2161           0 :                                                         if (php_request_startup() == FAILURE) {
    2162           0 :                                                                 SG(server_context) = NULL;
    2163           0 :                                                                 php_module_shutdown();
    2164           0 :                                                                 return FAILURE;
    2165             :                                                         }
    2166           0 :                                                         if (no_headers) {
    2167           0 :                                                                 SG(headers_sent) = 1;
    2168           0 :                                                                 SG(request_info).no_headers = 1;
    2169             :                                                         }
    2170           0 :                                                         php_print_info(0xFFFFFFFF);
    2171           0 :                                                         php_request_shutdown((void *) 0);
    2172           0 :                                                         fcgi_shutdown();
    2173           0 :                                                         exit_status = 0;
    2174           0 :                                                         goto out;
    2175             : 
    2176             :                                                 case 'l': /* syntax check mode */
    2177           4 :                                                         no_headers = 1;
    2178           4 :                                                         behavior = PHP_MODE_LINT;
    2179           4 :                                                         break;
    2180             : 
    2181             :                                                 case 'm': /* list compiled in modules */
    2182           0 :                                                         if (script_file) {
    2183           0 :                                                                 efree(script_file);
    2184             :                                                         }
    2185           0 :                                                         SG(headers_sent) = 1;
    2186           0 :                                                         php_printf("[PHP Modules]\n");
    2187           0 :                                                         print_modules();
    2188           0 :                                                         php_printf("\n[Zend Modules]\n");
    2189           0 :                                                         print_extensions();
    2190           0 :                                                         php_printf("\n");
    2191           0 :                                                         php_output_end_all();
    2192           0 :                                                         fcgi_shutdown();
    2193           0 :                                                         exit_status = 0;
    2194           0 :                                                         goto out;
    2195             : 
    2196             :                                                 case 'q': /* do not generate HTTP headers */
    2197         156 :                                                         no_headers = 1;
    2198         156 :                                                         break;
    2199             : 
    2200             :                                                 case 'v': /* show php version & quit */
    2201           1 :                                                         if (script_file) {
    2202           0 :                                                                 efree(script_file);
    2203             :                                                         }
    2204           1 :                                                         no_headers = 1;
    2205           1 :                                                         if (php_request_startup() == FAILURE) {
    2206           0 :                                                                 SG(server_context) = NULL;
    2207           0 :                                                                 php_module_shutdown();
    2208           0 :                                                                 return FAILURE;
    2209             :                                                         }
    2210           1 :                                                         if (no_headers) {
    2211           1 :                                                                 SG(headers_sent) = 1;
    2212           1 :                                                                 SG(request_info).no_headers = 1;
    2213             :                                                         }
    2214             : #if ZEND_DEBUG
    2215             :                                                         php_printf("PHP %s (%s) (built: %s %s) (DEBUG)\nCopyright (c) 1997-2015 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
    2216             : #else
    2217           1 :                                                         php_printf("PHP %s (%s) (built: %s %s)\nCopyright (c) 1997-2015 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
    2218             : #endif
    2219           1 :                                                         php_request_shutdown((void *) 0);
    2220           1 :                                                         fcgi_shutdown();
    2221           1 :                                                         exit_status = 0;
    2222           1 :                                                         goto out;
    2223             : 
    2224             :                                                 case 'w':
    2225           4 :                                                         behavior = PHP_MODE_STRIP;
    2226           4 :                                                         break;
    2227             : 
    2228             :                                                 case 'z': /* load extension file */
    2229           0 :                                                         zend_load_extension(php_optarg);
    2230             :                                                         break;
    2231             : 
    2232             :                                                 default:
    2233             :                                                         break;
    2234             :                                         }
    2235             :                                 }
    2236             : 
    2237         190 :                                 if (script_file) {
    2238             :                                         /* override path_translated if -f on command line */
    2239           5 :                                         if (SG(request_info).path_translated) efree(SG(request_info).path_translated);
    2240           5 :                                         SG(request_info).path_translated = script_file;
    2241             :                                         /* before registering argv to module exchange the *new* argv[0] */
    2242             :                                         /* we can achieve this without allocating more memory */
    2243           5 :                                         SG(request_info).argc = argc - (php_optind - 1);
    2244           5 :                                         SG(request_info).argv = &argv[php_optind - 1];
    2245           5 :                                         SG(request_info).argv[0] = script_file;
    2246         185 :                                 } else if (argc > php_optind) {
    2247             :                                         /* file is on command line, but not in -f opt */
    2248         183 :                                         if (SG(request_info).path_translated) efree(SG(request_info).path_translated);
    2249         183 :                                         SG(request_info).path_translated = estrdup(argv[php_optind]);
    2250             :                                         /* arguments after the file are considered script args */
    2251         183 :                                         SG(request_info).argc = argc - php_optind;
    2252         183 :                                         SG(request_info).argv = &argv[php_optind];
    2253             :                                 }
    2254             : 
    2255         190 :                                 if (no_headers) {
    2256         165 :                                         SG(headers_sent) = 1;
    2257         165 :                                         SG(request_info).no_headers = 1;
    2258             :                                 }
    2259             : 
    2260             :                                 /* all remaining arguments are part of the query string
    2261             :                                  * this section of code concatenates all remaining arguments
    2262             :                                  * into a single string, separating args with a &
    2263             :                                  * this allows command lines like:
    2264             :                                  *
    2265             :                                  *  test.php v1=test v2=hello+world!
    2266             :                                  *  test.php "v1=test&v2=hello world!"
    2267             :                                  *  test.php v1=test "v2=hello world!"
    2268             :                                 */
    2269         190 :                                 if (!SG(request_info).query_string && argc > php_optind) {
    2270         183 :                                         size_t slen = strlen(PG(arg_separator).input);
    2271         183 :                                         len = 0;
    2272         366 :                                         for (i = php_optind; i < argc; i++) {
    2273         183 :                                                 if (i < (argc - 1)) {
    2274           0 :                                                         len += strlen(argv[i]) + slen;
    2275             :                                                 } else {
    2276         183 :                                                         len += strlen(argv[i]);
    2277             :                                                 }
    2278             :                                         }
    2279             : 
    2280         183 :                                         len += 2;
    2281         183 :                                         s = malloc(len);
    2282         183 :                                         *s = '\0';                      /* we are pretending it came from the environment  */
    2283         366 :                                         for (i = php_optind; i < argc; i++) {
    2284         183 :                                                 strlcat(s, argv[i], len);
    2285         183 :                                                 if (i < (argc - 1)) {
    2286           0 :                                                         strlcat(s, PG(arg_separator).input, len);
    2287             :                                                 }
    2288             :                                         }
    2289         183 :                                         SG(request_info).query_string = s;
    2290         183 :                                         free_query_string = 1;
    2291             :                                 }
    2292             :                         } /* end !cgi && !fastcgi */
    2293             : 
    2294             :                         /*
    2295             :                                 we never take stdin if we're (f)cgi, always
    2296             :                                 rely on the web server giving us the info
    2297             :                                 we need in the environment.
    2298             :                         */
    2299         808 :                         if (SG(request_info).path_translated || cgi || fastcgi) {
    2300         403 :                                 file_handle.type = ZEND_HANDLE_FILENAME;
    2301         403 :                                 file_handle.filename = SG(request_info).path_translated;
    2302         403 :                                 file_handle.handle.fp = NULL;
    2303             :                         } else {
    2304           2 :                                 file_handle.filename = "-";
    2305           2 :                                 file_handle.type = ZEND_HANDLE_FP;
    2306           2 :                                 file_handle.handle.fp = stdin;
    2307             :                         }
    2308             : 
    2309         405 :                         file_handle.opened_path = NULL;
    2310         405 :                         file_handle.free_filename = 0;
    2311             : 
    2312             :                         /* request startup only after we've done all we can to
    2313             :                          * get path_translated */
    2314         405 :                         if (php_request_startup() == FAILURE) {
    2315           0 :                                 if (fastcgi) {
    2316           0 :                                         fcgi_finish_request(request, 1);
    2317             :                                 }
    2318           0 :                                 SG(server_context) = NULL;
    2319           0 :                                 php_module_shutdown();
    2320           0 :                                 return FAILURE;
    2321             :                         }
    2322         405 :                         if (no_headers) {
    2323         165 :                                 SG(headers_sent) = 1;
    2324         165 :                                 SG(request_info).no_headers = 1;
    2325             :                         }
    2326             : 
    2327             :                         /*
    2328             :                                 at this point path_translated will be set if:
    2329             :                                 1. we are running from shell and got filename was there
    2330             :                                 2. we are running as cgi or fastcgi
    2331             :                         */
    2332         405 :                         if (cgi || fastcgi || SG(request_info).path_translated) {
    2333         403 :                                 if (php_fopen_primary_script(&file_handle) == FAILURE) {
    2334           7 :                                         zend_try {
    2335           7 :                                                 if (errno == EACCES) {
    2336           0 :                                                         SG(sapi_headers).http_response_code = 403;
    2337           0 :                                                         PUTS("Access denied.\n");
    2338             :                                                 } else {
    2339           7 :                                                         SG(sapi_headers).http_response_code = 404;
    2340           7 :                                                         PUTS("No input file specified.\n");
    2341             :                                                 }
    2342           0 :                                         } zend_catch {
    2343           7 :                                         } zend_end_try();
    2344             :                                         /* we want to serve more requests if this is fastcgi
    2345             :                                          * so cleanup and continue, request shutdown is
    2346             :                                          * handled later */
    2347           7 :                                         if (fastcgi) {
    2348           0 :                                                 goto fastcgi_request_done;
    2349             :                                         }
    2350             : 
    2351           7 :                                         if (SG(request_info).path_translated) efree(SG(request_info).path_translated);
    2352             : 
    2353           7 :                                         if (free_query_string && SG(request_info).query_string) {
    2354           3 :                                                 free(SG(request_info).query_string);
    2355           3 :                                                 SG(request_info).query_string = NULL;
    2356             :                                         }
    2357             : 
    2358           7 :                                         php_request_shutdown((void *) 0);
    2359           7 :                                         SG(server_context) = NULL;
    2360           7 :                                         php_module_shutdown();
    2361           7 :                                         sapi_shutdown();
    2362             : #ifdef ZTS
    2363             :                                         tsrm_shutdown();
    2364             : #endif
    2365           7 :                                         return FAILURE;
    2366             :                                 }
    2367             :                         }
    2368             : 
    2369         398 :                         if (CGIG(check_shebang_line)) {
    2370             :                                 /* #!php support */
    2371         398 :                                 switch (file_handle.type) {
    2372             :                                         case ZEND_HANDLE_FD:
    2373           0 :                                                 if (file_handle.handle.fd < 0) {
    2374           0 :                                                         break;
    2375             :                                                 }
    2376           0 :                                                 file_handle.type = ZEND_HANDLE_FP;
    2377           0 :                                                 file_handle.handle.fp = fdopen(file_handle.handle.fd, "rb");
    2378             :                                                 /* break missing intentionally */
    2379             :                                         case ZEND_HANDLE_FP:
    2380           4 :                                                 if (!file_handle.handle.fp ||
    2381           2 :                                                     (file_handle.handle.fp == stdin)) {
    2382             :                                                         break;
    2383             :                                                 }
    2384           0 :                                                 c = fgetc(file_handle.handle.fp);
    2385           0 :                                                 if (c == '#') {
    2386           0 :                                                         while (c != '\n' && c != '\r' && c != EOF) {
    2387           0 :                                                                 c = fgetc(file_handle.handle.fp);       /* skip to end of line */
    2388             :                                                         }
    2389             :                                                         /* handle situations where line is terminated by \r\n */
    2390           0 :                                                         if (c == '\r') {
    2391           0 :                                                                 if (fgetc(file_handle.handle.fp) != '\n') {
    2392           0 :                                                                         zend_long pos = zend_ftell(file_handle.handle.fp);
    2393           0 :                                                                         zend_fseek(file_handle.handle.fp, pos - 1, SEEK_SET);
    2394             :                                                                 }
    2395             :                                                         }
    2396           0 :                                                         CG(start_lineno) = 2;
    2397             :                                                 } else {
    2398           0 :                                                         rewind(file_handle.handle.fp);
    2399             :                                                 }
    2400           0 :                                                 break;
    2401             :                                         case ZEND_HANDLE_STREAM:
    2402           1 :                                                 c = php_stream_getc((php_stream*)file_handle.handle.stream.handle);
    2403           1 :                                                 if (c == '#') {
    2404           0 :                                                         while (c != '\n' && c != '\r' && c != EOF) {
    2405           0 :                                                                 c = php_stream_getc((php_stream*)file_handle.handle.stream.handle);     /* skip to end of line */
    2406             :                                                         }
    2407             :                                                         /* handle situations where line is terminated by \r\n */
    2408           0 :                                                         if (c == '\r') {
    2409           0 :                                                                 if (php_stream_getc((php_stream*)file_handle.handle.stream.handle) != '\n') {
    2410           0 :                                                                         zend_off_t pos = php_stream_tell((php_stream*)file_handle.handle.stream.handle);
    2411           0 :                                                                         php_stream_seek((php_stream*)file_handle.handle.stream.handle, pos - 1, SEEK_SET);
    2412             :                                                                 }
    2413             :                                                         }
    2414           0 :                                                         CG(start_lineno) = 2;
    2415             :                                                 } else {
    2416           1 :                                                         php_stream_rewind((php_stream*)file_handle.handle.stream.handle);
    2417             :                                                 }
    2418           1 :                                                 break;
    2419             :                                         case ZEND_HANDLE_MAPPED:
    2420         395 :                                                 if (file_handle.handle.stream.mmap.buf[0] == '#') {
    2421           0 :                                                     size_t i = 1;
    2422             : 
    2423           0 :                                                     c = file_handle.handle.stream.mmap.buf[i++];
    2424           0 :                                                         while (c != '\n' && c != '\r' && i < file_handle.handle.stream.mmap.len) {
    2425           0 :                                                                 c = file_handle.handle.stream.mmap.buf[i++];
    2426             :                                                         }
    2427           0 :                                                         if (c == '\r') {
    2428           0 :                                                                 if (i < file_handle.handle.stream.mmap.len && file_handle.handle.stream.mmap.buf[i] == '\n') {
    2429           0 :                                                                         i++;
    2430             :                                                                 }
    2431             :                                                         }
    2432           0 :                                                         if(i > file_handle.handle.stream.mmap.len) {
    2433           0 :                                                                 i = file_handle.handle.stream.mmap.len;
    2434             :                                                         }
    2435           0 :                                                         file_handle.handle.stream.mmap.buf += i;
    2436           0 :                                                         file_handle.handle.stream.mmap.len -= i;
    2437             :                                                 }
    2438             :                                                 break;
    2439             :                                         default:
    2440             :                                                 break;
    2441             :                                 }
    2442             :                         }
    2443             : 
    2444         398 :                         switch (behavior) {
    2445             :                                 case PHP_MODE_STANDARD:
    2446         392 :                                         php_execute_script(&file_handle);
    2447         392 :                                         break;
    2448             :                                 case PHP_MODE_LINT:
    2449           3 :                                         PG(during_request_startup) = 0;
    2450           3 :                                         exit_status = php_lint_script(&file_handle);
    2451           3 :                                         if (exit_status == SUCCESS) {
    2452           2 :                                                 zend_printf("No syntax errors detected in %s\n", file_handle.filename);
    2453             :                                         } else {
    2454           1 :                                                 zend_printf("Errors parsing %s\n", file_handle.filename);
    2455             :                                         }
    2456           3 :                                         break;
    2457             :                                 case PHP_MODE_STRIP:
    2458           2 :                                         if (open_file_for_scanning(&file_handle) == SUCCESS) {
    2459           2 :                                                 zend_strip();
    2460           2 :                                                 zend_file_handle_dtor(&file_handle);
    2461           2 :                                                 php_output_teardown();
    2462             :                                         }
    2463           2 :                                         return SUCCESS;
    2464             :                                         break;
    2465             :                                 case PHP_MODE_HIGHLIGHT:
    2466             :                                         {
    2467             :                                                 zend_syntax_highlighter_ini syntax_highlighter_ini;
    2468             : 
    2469           1 :                                                 if (open_file_for_scanning(&file_handle) == SUCCESS) {
    2470           1 :                                                         php_get_highlight_struct(&syntax_highlighter_ini);
    2471           1 :                                                         zend_highlight(&syntax_highlighter_ini);
    2472           1 :                                                         if (fastcgi) {
    2473           0 :                                                                 goto fastcgi_request_done;
    2474             :                                                         }
    2475           1 :                                                         zend_file_handle_dtor(&file_handle);
    2476           1 :                                                         php_output_teardown();
    2477             :                                                 }
    2478           1 :                                                 return SUCCESS;
    2479             :                                         }
    2480             :                                         break;
    2481             :                         }
    2482             : 
    2483             : fastcgi_request_done:
    2484             :                         {
    2485         395 :                                 if (SG(request_info).path_translated) efree(SG(request_info).path_translated);
    2486             : 
    2487         395 :                                 php_request_shutdown((void *) 0);
    2488             : 
    2489         395 :                                 if (exit_status == 0) {
    2490         394 :                                         exit_status = EG(exit_status);
    2491             :                                 }
    2492             : 
    2493         395 :                                 if (free_query_string && SG(request_info).query_string) {
    2494         178 :                                         free(SG(request_info).query_string);
    2495         178 :                                         SG(request_info).query_string = NULL;
    2496             :                                 }
    2497             :                         }
    2498             : 
    2499         395 :                         if (!fastcgi) {
    2500         395 :                                 if (benchmark) {
    2501           0 :                                         if (warmup_repeats) {
    2502           0 :                                                 warmup_repeats--;
    2503           0 :                                                 if (!warmup_repeats) {
    2504             : #ifdef HAVE_GETTIMEOFDAY
    2505           0 :                                                         gettimeofday(&start, NULL);
    2506             : #else
    2507             :                                                         time(&start);
    2508             : #endif
    2509             :                                                 }
    2510           0 :                                                 continue;
    2511             :                                         } else {
    2512           0 :                                                 repeats--;
    2513           0 :                                                 if (repeats > 0) {
    2514           0 :                                                         script_file = NULL;
    2515           0 :                                                         php_optind = orig_optind;
    2516           0 :                                                         php_optarg = orig_optarg;
    2517           0 :                                                         continue;
    2518             :                                                 }
    2519             :                                         }
    2520             :                                 }
    2521         395 :                                 break;
    2522             :                         }
    2523             : 
    2524             :                         /* only fastcgi will get here */
    2525           0 :                         requests++;
    2526           0 :                         if (max_requests && (requests == max_requests)) {
    2527           0 :                                 fcgi_finish_request(request, 1);
    2528           0 :                                 if (bindpath) {
    2529           0 :                                         free(bindpath);
    2530             :                                 }
    2531           0 :                                 if (max_requests != 1) {
    2532             :                                         /* no need to return exit_status of the last request */
    2533           0 :                                         exit_status = 0;
    2534             :                                 }
    2535           0 :                                 break;
    2536             :                         }
    2537             :                         /* end of fastcgi loop */
    2538             :                 }
    2539             :                 
    2540         395 :                 if (request) {
    2541           0 :                         fcgi_destroy_request(request);
    2542             :                 }
    2543         395 :                 fcgi_shutdown();
    2544             : 
    2545         395 :                 if (cgi_sapi_module.php_ini_path_override) {
    2546         368 :                         free(cgi_sapi_module.php_ini_path_override);
    2547             :                 }
    2548         395 :                 if (cgi_sapi_module.ini_entries) {
    2549         383 :                         free(cgi_sapi_module.ini_entries);
    2550             :                 }
    2551           0 :         } zend_catch {
    2552           0 :                 exit_status = 255;
    2553         395 :         } zend_end_try();
    2554             : 
    2555             : out:
    2556         396 :         if (benchmark) {
    2557             :                 int sec;
    2558             : #ifdef HAVE_GETTIMEOFDAY
    2559             :                 int usec;
    2560             : 
    2561           0 :                 gettimeofday(&end, NULL);
    2562           0 :                 sec = (int)(end.tv_sec - start.tv_sec);
    2563           0 :                 if (end.tv_usec >= start.tv_usec) {
    2564           0 :                         usec = (int)(end.tv_usec - start.tv_usec);
    2565             :                 } else {
    2566           0 :                         sec -= 1;
    2567           0 :                         usec = (int)(end.tv_usec + 1000000 - start.tv_usec);
    2568             :                 }
    2569           0 :                 fprintf(stderr, "\nElapsed time: %d.%06d sec\n", sec, usec);
    2570             : #else
    2571             :                 time(&end);
    2572             :                 sec = (int)(end - start);
    2573             :                 fprintf(stderr, "\nElapsed time: %d sec\n", sec);
    2574             : #endif
    2575             :         }
    2576             : 
    2577             : #ifndef PHP_WIN32
    2578             : parent_out:
    2579             : #endif
    2580             : 
    2581         396 :         SG(server_context) = NULL;
    2582         396 :         php_module_shutdown();
    2583         396 :         sapi_shutdown();
    2584             : 
    2585             : #ifdef ZTS
    2586             :         tsrm_shutdown();
    2587             : #endif
    2588             : 
    2589             : #if defined(PHP_WIN32) && ZEND_DEBUG && 0
    2590             :         _CrtDumpMemoryLeaks();
    2591             : #endif
    2592             : 
    2593         396 :         return exit_status;
    2594             : }
    2595             : /* }}} */
    2596             : 
    2597             : /*
    2598             :  * Local variables:
    2599             :  * tab-width: 4
    2600             :  * c-basic-offset: 4
    2601             :  * End:
    2602             :  * vim600: sw=4 ts=4 fdm=marker
    2603             :  * vim<600: sw=4 ts=4
    2604             :  */

Generated by: LCOV version 1.10

Generated at Sat, 27 Jun 2015 09:41:25 +0000 (6 days ago)

Copyright © 2005-2015 The PHP Group
All rights reserved.