root/sapi/embed/php_embed.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. ZEND_TSRMLS_CACHE_DEFINE
  2. php_embed_deactivate
  3. php_embed_single_write
  4. php_embed_ub_write
  5. php_embed_flush
  6. php_embed_send_header
  7. php_embed_log_message
  8. php_embed_register_variables
  9. php_embed_startup
  10. php_embed_init
  11. php_embed_shutdown

   1 /*
   2    +----------------------------------------------------------------------+
   3    | PHP Version 7                                                        |
   4    +----------------------------------------------------------------------+
   5    | Copyright (c) 1997-2016 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    | Author: Edin Kadribasic <edink@php.net>                              |
  16    +----------------------------------------------------------------------+
  17 */
  18 /* $Id$ */
  19 
  20 #include "php_embed.h"
  21 #include "ext/standard/php_standard.h"
  22 
  23 #ifdef PHP_WIN32
  24 #include <io.h>
  25 #include <fcntl.h>
  26 #endif
  27 
  28 const char HARDCODED_INI[] =
  29         "html_errors=0\n"
  30         "register_argc_argv=1\n"
  31         "implicit_flush=1\n"
  32         "output_buffering=0\n"
  33         "max_execution_time=0\n"
  34         "max_input_time=-1\n\0";
  35 
  36 #if defined(PHP_WIN32) && defined(ZTS)
  37 ZEND_TSRMLS_CACHE_DEFINE()
  38 #endif
  39 
  40 static char* php_embed_read_cookies(void)
  41 {
  42         return NULL;
  43 }
  44 
  45 static int php_embed_deactivate(void)
  46 {
  47         fflush(stdout);
  48         return SUCCESS;
  49 }
  50 
  51 static inline size_t php_embed_single_write(const char *str, size_t str_length)
  52 {
  53 #ifdef PHP_WRITE_STDOUT
  54         zend_long ret;
  55 
  56         ret = write(STDOUT_FILENO, str, str_length);
  57         if (ret <= 0) return 0;
  58         return ret;
  59 #else
  60         size_t ret;
  61 
  62         ret = fwrite(str, 1, MIN(str_length, 16384), stdout);
  63         return ret;
  64 #endif
  65 }
  66 
  67 
  68 static size_t php_embed_ub_write(const char *str, size_t str_length)
  69 {
  70         const char *ptr = str;
  71         size_t remaining = str_length;
  72         size_t ret;
  73 
  74         while (remaining > 0) {
  75                 ret = php_embed_single_write(ptr, remaining);
  76                 if (!ret) {
  77                         php_handle_aborted_connection();
  78                 }
  79                 ptr += ret;
  80                 remaining -= ret;
  81         }
  82 
  83         return str_length;
  84 }
  85 
  86 static void php_embed_flush(void *server_context)
  87 {
  88         if (fflush(stdout)==EOF) {
  89                 php_handle_aborted_connection();
  90         }
  91 }
  92 
  93 static void php_embed_send_header(sapi_header_struct *sapi_header, void *server_context)
  94 {
  95 }
  96 
  97 static void php_embed_log_message(char *message)
  98 {
  99         fprintf (stderr, "%s\n", message);
 100 }
 101 
 102 static void php_embed_register_variables(zval *track_vars_array)
 103 {
 104         php_import_environment_variables(track_vars_array);
 105 }
 106 
 107 static int php_embed_startup(sapi_module_struct *sapi_module)
 108 {
 109         if (php_module_startup(sapi_module, NULL, 0)==FAILURE) {
 110                 return FAILURE;
 111         }
 112         return SUCCESS;
 113 }
 114 
 115 EMBED_SAPI_API sapi_module_struct php_embed_module = {
 116         "embed",                       /* name */
 117         "PHP Embedded Library",        /* pretty name */
 118 
 119         php_embed_startup,              /* startup */
 120         php_module_shutdown_wrapper,   /* shutdown */
 121 
 122         NULL,                          /* activate */
 123         php_embed_deactivate,           /* deactivate */
 124 
 125         php_embed_ub_write,             /* unbuffered write */
 126         php_embed_flush,                /* flush */
 127         NULL,                          /* get uid */
 128         NULL,                          /* getenv */
 129 
 130         php_error,                     /* error handler */
 131 
 132         NULL,                          /* header handler */
 133         NULL,                          /* send headers handler */
 134         php_embed_send_header,          /* send header handler */
 135 
 136         NULL,                          /* read POST data */
 137         php_embed_read_cookies,         /* read Cookies */
 138 
 139         php_embed_register_variables,   /* register server variables */
 140         php_embed_log_message,          /* Log message */
 141         NULL,                                                   /* Get request time */
 142         NULL,                                                   /* Child terminate */
 143 
 144         STANDARD_SAPI_MODULE_PROPERTIES
 145 };
 146 /* }}} */
 147 
 148 /* {{{ arginfo ext/standard/dl.c */
 149 ZEND_BEGIN_ARG_INFO(arginfo_dl, 0)
 150         ZEND_ARG_INFO(0, extension_filename)
 151 ZEND_END_ARG_INFO()
 152 /* }}} */
 153 
 154 static const zend_function_entry additional_functions[] = {
 155         ZEND_FE(dl, arginfo_dl)
 156         {NULL, NULL, NULL}
 157 };
 158 
 159 EMBED_SAPI_API int php_embed_init(int argc, char **argv)
 160 {
 161         zend_llist global_vars;
 162 
 163 #ifdef HAVE_SIGNAL_H
 164 #if defined(SIGPIPE) && defined(SIG_IGN)
 165         signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE in standalone mode so
 166                                                                  that sockets created via fsockopen()
 167                                                                  don't kill PHP if the remote site
 168                                                                  closes it.  in apache|apxs mode apache
 169                                                                  does that for us!  thies@thieso.net
 170                                                                  20000419 */
 171 #endif
 172 #endif
 173 
 174 #ifdef ZTS
 175   tsrm_startup(1, 1, 0, NULL);
 176   (void)ts_resource(0);
 177   ZEND_TSRMLS_CACHE_UPDATE();
 178 #endif
 179 
 180 #ifdef ZEND_SIGNALS
 181         zend_signal_startup();
 182 #endif
 183 
 184   sapi_startup(&php_embed_module);
 185 
 186 #ifdef PHP_WIN32
 187   _fmode = _O_BINARY;                   /*sets default for file streams to binary */
 188   setmode(_fileno(stdin), O_BINARY);            /* make the stdio mode be binary */
 189   setmode(_fileno(stdout), O_BINARY);           /* make the stdio mode be binary */
 190   setmode(_fileno(stderr), O_BINARY);           /* make the stdio mode be binary */
 191 #endif
 192 
 193   php_embed_module.ini_entries = malloc(sizeof(HARDCODED_INI));
 194   memcpy(php_embed_module.ini_entries, HARDCODED_INI, sizeof(HARDCODED_INI));
 195 
 196   php_embed_module.additional_functions = additional_functions;
 197 
 198   if (argv) {
 199         php_embed_module.executable_location = argv[0];
 200   }
 201 
 202   if (php_embed_module.startup(&php_embed_module)==FAILURE) {
 203           return FAILURE;
 204   }
 205 
 206   zend_llist_init(&global_vars, sizeof(char *), NULL, 0);
 207 
 208   /* Set some Embedded PHP defaults */
 209   SG(options) |= SAPI_OPTION_NO_CHDIR;
 210   SG(request_info).argc=argc;
 211   SG(request_info).argv=argv;
 212 
 213   if (php_request_startup()==FAILURE) {
 214           php_module_shutdown();
 215           return FAILURE;
 216   }
 217 
 218   SG(headers_sent) = 1;
 219   SG(request_info).no_headers = 1;
 220   php_register_variable("PHP_SELF", "-", NULL);
 221 
 222   return SUCCESS;
 223 }
 224 
 225 EMBED_SAPI_API void php_embed_shutdown(void)
 226 {
 227         php_request_shutdown((void *) 0);
 228         php_module_shutdown();
 229         sapi_shutdown();
 230 #ifdef ZTS
 231     tsrm_shutdown();
 232 #endif
 233         if (php_embed_module.ini_entries) {
 234                 free(php_embed_module.ini_entries);
 235                 php_embed_module.ini_entries = NULL;
 236         }
 237 }
 238 
 239 /*
 240  * Local variables:
 241  * tab-width: 4
 242  * c-basic-offset: 4
 243  * End:
 244  * vim600: sw=4 ts=4 fdm=marker
 245  * vim<600: sw=4 ts=4
 246  */

/* [<][>][^][v][top][bottom][index][help] */