From: Jérôme Loyet Date: Sat, 8 Oct 2011 13:52:27 +0000 (+0000) Subject: - backparted from 5.4 branch (remove is_fastcgi because FPM is always fastcgi) X-Git-Tag: php-5.3.9RC1~56 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9018d9434e0dfd77a47a7628e9827a8fe18078d6;p=php - backparted from 5.4 branch (remove is_fastcgi because FPM is always fastcgi) --- diff --git a/sapi/fpm/fpm/fastcgi.c b/sapi/fpm/fpm/fastcgi.c index 22a8f3a2f0..4e1a0b7cea 100644 --- a/sapi/fpm/fpm/fastcgi.c +++ b/sapi/fpm/fpm/fastcgi.c @@ -142,7 +142,6 @@ typedef union _sa_t { static HashTable fcgi_mgmt_vars; static int is_initialized = 0; -static int is_fastcgi = 0; static int in_shutdown = 0; static in_addr_t *allowed_clients = NULL; @@ -204,9 +203,7 @@ int fcgi_init(void) return 0; } # endif - if ((GetStdHandle(STD_OUTPUT_HANDLE) == INVALID_HANDLE_VALUE) && - (GetStdHandle(STD_ERROR_HANDLE) == INVALID_HANDLE_VALUE) && - (GetStdHandle(STD_INPUT_HANDLE) != INVALID_HANDLE_VALUE)) { + { char *str; DWORD pipe_mode = PIPE_READMODE_BYTE | PIPE_WAIT; HANDLE pipe = GetStdHandle(STD_INPUT_HANDLE); @@ -225,36 +222,14 @@ int fcgi_init(void) if (str != NULL) { fcgi_accept_mutex = (HANDLE) atoi(str); } - return is_fastcgi = 1; - } else { - return is_fastcgi = 0; + return 1; } #else - errno = 0; - if (getpeername(0, (struct sockaddr *)&sa, &len) != 0 && errno == ENOTCONN) { - fcgi_setup_signals(); - return is_fastcgi = 1; - } else { - return is_fastcgi = 0; - } + fcgi_setup_signals(); + return 1; #endif } - return is_fastcgi; -} - - -int fcgi_is_fastcgi(void) -{ - if (!is_initialized) { - return fcgi_init(); - } else { - return is_fastcgi; - } -} - -void fcgi_set_is_fastcgi(int new_value) -{ - is_fastcgi = new_value; + return 1; } void fcgi_set_in_shutdown(int new_value) @@ -267,7 +242,6 @@ void fcgi_shutdown(void) if (is_initialized) { zend_hash_destroy(&fcgi_mgmt_vars); } - is_fastcgi = 0; if (allowed_clients) { free(allowed_clients); } diff --git a/sapi/fpm/fpm/fastcgi.h b/sapi/fpm/fpm/fastcgi.h index 4129def050..caac528547 100644 --- a/sapi/fpm/fpm/fastcgi.h +++ b/sapi/fpm/fpm/fastcgi.h @@ -114,12 +114,10 @@ typedef struct _fcgi_request { int fcgi_init(void); void fcgi_shutdown(void); -int fcgi_is_fastcgi(void); void fcgi_init_request(fcgi_request *req, int listen_socket); int fcgi_accept_request(fcgi_request *req); int fcgi_finish_request(fcgi_request *req, int force_close); -void fcgi_set_is_fastcgi(int new_value); void fcgi_set_in_shutdown(int); void fcgi_set_allowed_clients(char *); void fcgi_close(fcgi_request *req, int force, int destroy); diff --git a/sapi/fpm/fpm/fpm_main.c b/sapi/fpm/fpm/fpm_main.c index 80f834e7e6..485453e149 100644 --- a/sapi/fpm/fpm/fpm_main.c +++ b/sapi/fpm/fpm/fpm_main.c @@ -106,6 +106,7 @@ int __riscosify_control = __RISCOSIFY_STRICT_UNIX_SPECS; #include #include #include +#include #ifndef PHP_WIN32 /* XXX this will need to change later when threaded fastcgi is implemented. shane */ @@ -124,6 +125,7 @@ static int parent = 1; #endif static int request_body_fd; +static int fpm_is_running = 0; static char *sapi_cgibin_getenv(char *name, size_t name_len TSRMLS_DC); static void fastcgi_ini_parser(zval *arg1, zval *arg2, zval *arg3, int callback_type, void *arg TSRMLS_DC); @@ -260,34 +262,33 @@ static void print_extensions(TSRMLS_D) zend_llist_destroy(&sorted_exts); } -#ifndef STDOUT_FILENO -#define STDOUT_FILENO 1 +#ifndef STDOUT_FILENO +#define STDOUT_FILENO 1 #endif static inline size_t sapi_cgibin_single_write(const char *str, uint str_length TSRMLS_DC) { -#ifdef PHP_WRITE_STDOUT - long ret; -#else size_t ret; -#endif - if (fcgi_is_fastcgi()) { + /* sapi has started which means everyhting must be send through fcgi */ + if (fpm_is_running) { fcgi_request *request = (fcgi_request*) SG(server_context); - long ret = fcgi_write(request, FCGI_STDOUT, str, str_length); + ret = fcgi_write(request, FCGI_STDOUT, str, str_length); if (ret <= 0) { return 0; } return ret; } -#ifdef PHP_WRITE_STDOUT - ret = write(STDOUT_FILENO, str, str_length); - if (ret <= 0) return 0; + /* sapi has not started, output to stdout instead of fcgi */ +#ifdef PHP_WRITE_STDOUT + ret = write(STDOUT_FILENO, str, str_length); + if (ret <= 0) { + return 0; + } return ret; #else - ret = fwrite(str, 1, MIN(str_length, 16384), stdout); - return ret; + return fwrite(str, 1, MIN(str_length, 16384), stdout); #endif } @@ -313,17 +314,20 @@ static int sapi_cgibin_ub_write(const char *str, uint str_length TSRMLS_DC) static void sapi_cgibin_flush(void *server_context) { - if (fcgi_is_fastcgi()) { + /* fpm has started, let use fcgi instead of stdout */ + if (fpm_is_running) { fcgi_request *request = (fcgi_request*) server_context; if ( #ifndef PHP_WIN32 - !parent && + !parent && #endif - request && !fcgi_flush(request, 0)) { + request && !fcgi_flush(request, 0)) { php_handle_aborted_connection(); } return; } + + /* fpm has not started yet, let use stdout instead of fcgi */ if (fflush(stdout) == EOF) { php_handle_aborted_connection(); } @@ -490,31 +494,27 @@ static int sapi_cgi_read_post(char *buffer, uint count_bytes TSRMLS_DC) count_bytes = MIN(count_bytes, (uint) SG(request_info).content_length - SG(read_post_bytes)); while (read_bytes < count_bytes) { - if (fcgi_is_fastcgi()) { - fcgi_request *request = (fcgi_request*) SG(server_context); - if (request_body_fd == -1) { - char *request_body_filename = sapi_cgibin_getenv((char *) "REQUEST_BODY_FILE", - sizeof("REQUEST_BODY_FILE") - 1 TSRMLS_CC); - - if (request_body_filename && *request_body_filename) { - request_body_fd = open(request_body_filename, O_RDONLY); - - if (0 > request_body_fd) { - php_error(E_WARNING, "REQUEST_BODY_FILE: open('%s') failed: %s (%d)", - request_body_filename, strerror(errno), errno); - return 0; - } + fcgi_request *request = (fcgi_request*) SG(server_context); + if (request_body_fd == -1) { + char *request_body_filename = sapi_cgibin_getenv((char *) "REQUEST_BODY_FILE", + sizeof("REQUEST_BODY_FILE") - 1 TSRMLS_CC); + + if (request_body_filename && *request_body_filename) { + request_body_fd = open(request_body_filename, O_RDONLY); + + if (0 > request_body_fd) { + php_error(E_WARNING, "REQUEST_BODY_FILE: open('%s') failed: %s (%d)", + request_body_filename, strerror(errno), errno); + return 0; } } + } - /* If REQUEST_BODY_FILE variable not available - read post body from fastcgi stream */ - if (request_body_fd < 0) { - tmp_read_bytes = fcgi_read(request, buffer + read_bytes, count_bytes - read_bytes); - } else { - tmp_read_bytes = read(request_body_fd, buffer + read_bytes, count_bytes - read_bytes); - } + /* If REQUEST_BODY_FILE variable not available - read post body from fastcgi stream */ + if (request_body_fd < 0) { + tmp_read_bytes = fcgi_read(request, buffer + read_bytes, count_bytes - read_bytes); } else { - tmp_read_bytes = read(STDIN_FILENO, buffer + read_bytes, count_bytes - read_bytes); + tmp_read_bytes = read(request_body_fd, buffer + read_bytes, count_bytes - read_bytes); } if (tmp_read_bytes <= 0) { break; @@ -526,77 +526,27 @@ static int sapi_cgi_read_post(char *buffer, uint count_bytes TSRMLS_DC) static char *sapi_cgibin_getenv(char *name, size_t name_len TSRMLS_DC) { - /* when php is started by mod_fastcgi, no regular environment - * is provided to PHP. It is always sent to PHP at the start - * of a request. So we have to do our own lookup to get env - * vars. This could probably be faster somehow. */ - if (fcgi_is_fastcgi()) { + /* if fpm has started, use fcgi env */ + if (fpm_is_running) { fcgi_request *request = (fcgi_request*) SG(server_context); return fcgi_getenv(request, name, name_len); } - /* if cgi, or fastcgi and not found in fcgi env - check the regular environment */ + + /* if fpm has not started yet, use std env */ return getenv(name); } static char *_sapi_cgibin_putenv(char *name, char *value TSRMLS_DC) { int name_len; -#if !HAVE_SETENV || !HAVE_UNSETENV - int len; - char *buf; -#endif if (!name) { return NULL; } name_len = strlen(name); - /* when php is started by mod_fastcgi, no regular environment - * is provided to PHP. It is always sent to PHP at the start - * of a request. So we have to do our own lookup to get env - * vars. This could probably be faster somehow. */ - if (fcgi_is_fastcgi()) { - fcgi_request *request = (fcgi_request*) SG(server_context); - return fcgi_putenv(request, name, name_len, value); - } - -#if HAVE_SETENV - if (value) { - setenv(name, value, 1); - } -#endif -#if HAVE_UNSETENV - if (!value) { - unsetenv(name); - } -#endif - -#if !HAVE_SETENV || !HAVE_UNSETENV - /* if cgi, or fastcgi and not found in fcgi env - check the regular environment - this leaks, but it's only cgi anyway, we'll fix - it for 5.0 - */ - len = name_len + (value ? strlen(value) : 0) + sizeof("=") + 2; - buf = (char *) malloc(len); - if (buf == NULL) { - return getenv(name); - } -#endif -#if !HAVE_SETENV - if (value) { - len = slprintf(buf, len - 1, "%s=%s", name, value); - putenv(buf); - } -#endif -#if !HAVE_UNSETENV - if (!value) { - len = slprintf(buf, len - 1, "%s=", name); - putenv(buf); - } -#endif - return getenv(name); + fcgi_request *request = (fcgi_request*) SG(server_context); + return fcgi_putenv(request, name, name_len, value); } static char *sapi_cgi_read_cookies(TSRMLS_D) @@ -606,6 +556,15 @@ static char *sapi_cgi_read_cookies(TSRMLS_D) void cgi_php_import_environment_variables(zval *array_ptr TSRMLS_DC) { + fcgi_request *request; + HashPosition pos; + int magic_quotes_gpc;; + char *var, **val; + uint var_len; + ulong idx; + int filter_arg; + + if (PG(http_globals)[TRACK_VARS_ENV] && array_ptr != PG(http_globals)[TRACK_VARS_ENV] && Z_TYPE_P(PG(http_globals)[TRACK_VARS_ENV]) == IS_ARRAY && @@ -631,30 +590,24 @@ void cgi_php_import_environment_variables(zval *array_ptr TSRMLS_DC) /* call php's original import as a catch-all */ php_php_import_environment_variables(array_ptr TSRMLS_CC); - if (fcgi_is_fastcgi()) { - fcgi_request *request = (fcgi_request*) SG(server_context); - HashPosition pos; - int magic_quotes_gpc = PG(magic_quotes_gpc); - char *var, **val; - uint var_len; - ulong idx; - int filter_arg = (array_ptr == PG(http_globals)[TRACK_VARS_ENV])?PARSE_ENV:PARSE_SERVER; - - /* turn off magic_quotes while importing environment variables */ - PG(magic_quotes_gpc) = 0; - for (zend_hash_internal_pointer_reset_ex(request->env, &pos); - zend_hash_get_current_key_ex(request->env, &var, &var_len, &idx, 0, &pos) == HASH_KEY_IS_STRING && - zend_hash_get_current_data_ex(request->env, (void **) &val, &pos) == SUCCESS; - zend_hash_move_forward_ex(request->env, &pos) - ) { - unsigned int new_val_len; + request = (fcgi_request*) SG(server_context); + magic_quotes_gpc = PG(magic_quotes_gpc); + filter_arg = (array_ptr == PG(http_globals)[TRACK_VARS_ENV])?PARSE_ENV:PARSE_SERVER; - if (sapi_module.input_filter(filter_arg, var, val, strlen(*val), &new_val_len TSRMLS_CC)) { - php_register_variable_safe(var, *val, new_val_len, array_ptr TSRMLS_CC); - } + /* turn off magic_quotes while importing environment variables */ + PG(magic_quotes_gpc) = 0; + for (zend_hash_internal_pointer_reset_ex(request->env, &pos); + zend_hash_get_current_key_ex(request->env, &var, &var_len, &idx, 0, &pos) == HASH_KEY_IS_STRING && + zend_hash_get_current_data_ex(request->env, (void **) &val, &pos) == SUCCESS; + zend_hash_move_forward_ex(request->env, &pos) + ) { + unsigned int new_val_len; + + if (sapi_module.input_filter(filter_arg, var, val, strlen(*val), &new_val_len TSRMLS_CC)) { + php_register_variable_safe(var, *val, new_val_len, array_ptr TSRMLS_CC); } - PG(magic_quotes_gpc) = magic_quotes_gpc; } + PG(magic_quotes_gpc) = magic_quotes_gpc; } static void sapi_cgi_register_variables(zval *track_vars_array TSRMLS_DC) @@ -702,7 +655,7 @@ static void sapi_cgi_log_message(char *message) { TSRMLS_FETCH(); - if (fcgi_is_fastcgi() && CGIG(fcgi_logging)) { + if (CGIG(fcgi_logging)) { fcgi_request *request; request = (fcgi_request*) SG(server_context); @@ -716,10 +669,9 @@ static void sapi_cgi_log_message(char *message) free(buf); } else { fprintf(stderr, "%s\n", message); + //FIXME zlog(ZLOG_NOTICE, "PHP message: %s", message); } /* ignore return code */ - } else { - fprintf(stderr, "%s\n", message); } } @@ -877,16 +829,12 @@ static int sapi_cgi_deactivate(TSRMLS_D) 2. When the first call occurs and the request is not set up, flush fails on FastCGI. */ if (SG(sapi_started)) { - if (fcgi_is_fastcgi()) { - if ( + if ( #ifndef PHP_WIN32 - !parent && + !parent && #endif - !fcgi_finish_request((fcgi_request*)SG(server_context), 0)) { - php_handle_aborted_connection(); - } - } else { - sapi_cgibin_flush(SG(server_context)); + !fcgi_finish_request((fcgi_request*)SG(server_context), 0)) { + php_handle_aborted_connection(); } } return SUCCESS; @@ -1536,7 +1484,7 @@ PHP_FUNCTION(fastcgi_finish_request) /* {{{ */ { fcgi_request *request = (fcgi_request*) SG(server_context); - if (fcgi_is_fastcgi() && request->fd >= 0) { + if (request->fd >= 0) { php_end_ob_buffers(1 TSRMLS_CC); php_header(TSRMLS_C); @@ -1830,9 +1778,10 @@ consult the installation file that came with this distribution, or visit \n\ return FAILURE; } + fpm_is_running = 1; + fcgi_fd = fpm_run(&max_requests); parent = 0; - fcgi_set_is_fastcgi(1); /* make php call us to get _ENV vars */ php_php_import_environment_variables = php_import_environment_variables;