Back to index

php5  5.3.10
php_embed.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Author: Edin Kadribasic <edink@php.net>                              |
00016    +----------------------------------------------------------------------+
00017 */
00018 /* $Id: php_embed.c 321634 2012-01-01 13:15:04Z felipe $ */
00019 
00020 #include "php_embed.h"
00021 #include "ext/standard/php_standard.h"
00022 
00023 #ifdef PHP_WIN32
00024 #include <io.h>
00025 #include <fcntl.h>
00026 #endif
00027 
00028 const char HARDCODED_INI[] =
00029        "html_errors=0\n"
00030        "register_argc_argv=1\n"
00031        "implicit_flush=1\n"
00032        "output_buffering=0\n"
00033        "max_execution_time=0\n"
00034        "max_input_time=-1\n\0";
00035 
00036 static char* php_embed_read_cookies(TSRMLS_D)
00037 {
00038        return NULL;
00039 }
00040 
00041 static int php_embed_deactivate(TSRMLS_D)
00042 {
00043        fflush(stdout);
00044        return SUCCESS;
00045 }
00046 
00047 static inline size_t php_embed_single_write(const char *str, uint str_length)
00048 {
00049 #ifdef PHP_WRITE_STDOUT
00050        long ret;
00051 
00052        ret = write(STDOUT_FILENO, str, str_length);
00053        if (ret <= 0) return 0;
00054        return ret;
00055 #else
00056        size_t ret;
00057 
00058        ret = fwrite(str, 1, MIN(str_length, 16384), stdout);
00059        return ret;
00060 #endif
00061 }
00062 
00063 
00064 static int php_embed_ub_write(const char *str, uint str_length TSRMLS_DC)
00065 {
00066        const char *ptr = str;
00067        uint remaining = str_length;
00068        size_t ret;
00069 
00070        while (remaining > 0) {
00071               ret = php_embed_single_write(ptr, remaining);
00072               if (!ret) {
00073                      php_handle_aborted_connection();
00074               }
00075               ptr += ret;
00076               remaining -= ret;
00077        }
00078 
00079        return str_length;
00080 }
00081 
00082 static void php_embed_flush(void *server_context)
00083 {
00084        if (fflush(stdout)==EOF) {
00085               php_handle_aborted_connection();
00086        }
00087 }
00088 
00089 static void php_embed_send_header(sapi_header_struct *sapi_header, void *server_context TSRMLS_DC)
00090 {
00091 }
00092 
00093 static void php_embed_log_message(char *message)
00094 {
00095        fprintf (stderr, "%s\n", message);
00096 }
00097 
00098 static void php_embed_register_variables(zval *track_vars_array TSRMLS_DC)
00099 {
00100        php_import_environment_variables(track_vars_array TSRMLS_CC);
00101 }
00102 
00103 static int php_embed_startup(sapi_module_struct *sapi_module)
00104 {
00105        if (php_module_startup(sapi_module, NULL, 0)==FAILURE) {
00106               return FAILURE;
00107        }
00108        return SUCCESS;
00109 }
00110 
00111 extern EMBED_SAPI_API sapi_module_struct php_embed_module = {
00112        "embed",                       /* name */
00113        "PHP Embedded Library",        /* pretty name */
00114        
00115        php_embed_startup,              /* startup */
00116        php_module_shutdown_wrapper,   /* shutdown */
00117   
00118        NULL,                          /* activate */
00119        php_embed_deactivate,           /* deactivate */
00120   
00121        php_embed_ub_write,             /* unbuffered write */
00122        php_embed_flush,                /* flush */
00123        NULL,                          /* get uid */
00124        NULL,                          /* getenv */
00125   
00126        php_error,                     /* error handler */
00127   
00128        NULL,                          /* header handler */
00129        NULL,                          /* send headers handler */
00130        php_embed_send_header,          /* send header handler */
00131        
00132        NULL,                          /* read POST data */
00133        php_embed_read_cookies,         /* read Cookies */
00134   
00135        php_embed_register_variables,   /* register server variables */
00136        php_embed_log_message,          /* Log message */
00137        NULL,                                            /* Get request time */
00138        NULL,                                            /* Child terminate */
00139   
00140        STANDARD_SAPI_MODULE_PROPERTIES
00141 };
00142 /* }}} */
00143 
00144 /* {{{ arginfo ext/standard/dl.c */
00145 ZEND_BEGIN_ARG_INFO(arginfo_dl, 0)
00146        ZEND_ARG_INFO(0, extension_filename)
00147 ZEND_END_ARG_INFO()
00148 /* }}} */
00149 
00150 static const zend_function_entry additional_functions[] = {
00151        ZEND_FE(dl, arginfo_dl)
00152        {NULL, NULL, NULL}
00153 };
00154 
00155 EMBED_SAPI_API int php_embed_init(int argc, char **argv PTSRMLS_DC)
00156 {
00157        zend_llist global_vars;
00158 #ifdef ZTS
00159        void ***tsrm_ls = NULL;
00160 #endif
00161 
00162 #ifdef HAVE_SIGNAL_H
00163 #if defined(SIGPIPE) && defined(SIG_IGN)
00164        signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE in standalone mode so
00165                                                          that sockets created via fsockopen()
00166                                                          don't kill PHP if the remote site
00167                                                          closes it.  in apache|apxs mode apache
00168                                                          does that for us!  thies@thieso.net
00169                                                          20000419 */
00170 #endif
00171 #endif
00172 
00173 #ifdef ZTS
00174   tsrm_startup(1, 1, 0, NULL);
00175   tsrm_ls = ts_resource(0);
00176   *ptsrm_ls = tsrm_ls;
00177 #endif
00178 
00179   sapi_startup(&php_embed_module);
00180 
00181 #ifdef PHP_WIN32
00182   _fmode = _O_BINARY;                     /*sets default for file streams to binary */
00183   setmode(_fileno(stdin), O_BINARY);             /* make the stdio mode be binary */
00184   setmode(_fileno(stdout), O_BINARY);            /* make the stdio mode be binary */
00185   setmode(_fileno(stderr), O_BINARY);            /* make the stdio mode be binary */
00186 #endif
00187 
00188   php_embed_module.ini_entries = malloc(sizeof(HARDCODED_INI));
00189   memcpy(php_embed_module.ini_entries, HARDCODED_INI, sizeof(HARDCODED_INI));
00190 
00191   php_embed_module.additional_functions = additional_functions;
00192 
00193   if (argv) {
00194        php_embed_module.executable_location = argv[0];
00195   }
00196 
00197   if (php_embed_module.startup(&php_embed_module)==FAILURE) {
00198          return FAILURE;
00199   }
00200  
00201   zend_llist_init(&global_vars, sizeof(char *), NULL, 0);  
00202 
00203   /* Set some Embedded PHP defaults */
00204   SG(options) |= SAPI_OPTION_NO_CHDIR;
00205   SG(request_info).argc=argc;
00206   SG(request_info).argv=argv;
00207 
00208   if (php_request_startup(TSRMLS_C)==FAILURE) {
00209          php_module_shutdown(TSRMLS_C);
00210          return FAILURE;
00211   }
00212   
00213   SG(headers_sent) = 1;
00214   SG(request_info).no_headers = 1;
00215   php_register_variable("PHP_SELF", "-", NULL TSRMLS_CC);
00216 
00217   return SUCCESS;
00218 }
00219 
00220 EMBED_SAPI_API void php_embed_shutdown(TSRMLS_D)
00221 {
00222        php_request_shutdown((void *) 0);
00223        php_module_shutdown(TSRMLS_C);
00224        sapi_shutdown();
00225 #ifdef ZTS
00226     tsrm_shutdown();
00227 #endif
00228        if (php_embed_module.ini_entries) {
00229               free(php_embed_module.ini_entries);
00230               php_embed_module.ini_entries = NULL;
00231        }
00232 }
00233 
00234 /*
00235  * Local variables:
00236  * tab-width: 4
00237  * c-basic-offset: 4
00238  * End:
00239  * vim600: sw=4 ts=4 fdm=marker
00240  * vim<600: sw=4 ts=4
00241  */