xref: /PHP-7.2/sapi/embed/php_embed.c (revision 2d3bc71e)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2018 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)
ZEND_TSRMLS_CACHE_DEFINE()37 ZEND_TSRMLS_CACHE_DEFINE()
38 #endif
39 
40 static char* php_embed_read_cookies(void)
41 {
42 	return NULL;
43 }
44 
php_embed_deactivate(void)45 static int php_embed_deactivate(void)
46 {
47 	fflush(stdout);
48 	return SUCCESS;
49 }
50 
php_embed_single_write(const char * str,size_t str_length)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 
php_embed_ub_write(const char * str,size_t str_length)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 
php_embed_flush(void * server_context)86 static void php_embed_flush(void *server_context)
87 {
88 	if (fflush(stdout)==EOF) {
89 		php_handle_aborted_connection();
90 	}
91 }
92 
php_embed_send_header(sapi_header_struct * sapi_header,void * server_context)93 static void php_embed_send_header(sapi_header_struct *sapi_header, void *server_context)
94 {
95 }
96 
php_embed_log_message(char * message,int syslog_type_int)97 static void php_embed_log_message(char *message, int syslog_type_int)
98 {
99 	fprintf (stderr, "%s\n", message);
100 }
101 
php_embed_register_variables(zval * track_vars_array)102 static void php_embed_register_variables(zval *track_vars_array)
103 {
104 	php_import_environment_variables(track_vars_array);
105 }
106 
php_embed_startup(sapi_module_struct * sapi_module)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 
php_embed_init(int argc,char ** argv)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 	zend_signal_startup();
181 
182   sapi_startup(&php_embed_module);
183 
184 #ifdef PHP_WIN32
185   _fmode = _O_BINARY;			/*sets default for file streams to binary */
186   setmode(_fileno(stdin), O_BINARY);		/* make the stdio mode be binary */
187   setmode(_fileno(stdout), O_BINARY);		/* make the stdio mode be binary */
188   setmode(_fileno(stderr), O_BINARY);		/* make the stdio mode be binary */
189 #endif
190 
191   php_embed_module.ini_entries = malloc(sizeof(HARDCODED_INI));
192   memcpy(php_embed_module.ini_entries, HARDCODED_INI, sizeof(HARDCODED_INI));
193 
194   php_embed_module.additional_functions = additional_functions;
195 
196   if (argv) {
197 	php_embed_module.executable_location = argv[0];
198   }
199 
200   if (php_embed_module.startup(&php_embed_module)==FAILURE) {
201 	  return FAILURE;
202   }
203 
204   zend_llist_init(&global_vars, sizeof(char *), NULL, 0);
205 
206   /* Set some Embedded PHP defaults */
207   SG(options) |= SAPI_OPTION_NO_CHDIR;
208   SG(request_info).argc=argc;
209   SG(request_info).argv=argv;
210 
211   if (php_request_startup()==FAILURE) {
212 	  php_module_shutdown();
213 	  return FAILURE;
214   }
215 
216   SG(headers_sent) = 1;
217   SG(request_info).no_headers = 1;
218   php_register_variable("PHP_SELF", "-", NULL);
219 
220   return SUCCESS;
221 }
222 
php_embed_shutdown(void)223 EMBED_SAPI_API void php_embed_shutdown(void)
224 {
225 	php_request_shutdown((void *) 0);
226 	php_module_shutdown();
227 	sapi_shutdown();
228 #ifdef ZTS
229     tsrm_shutdown();
230 #endif
231 	if (php_embed_module.ini_entries) {
232 		free(php_embed_module.ini_entries);
233 		php_embed_module.ini_entries = NULL;
234 	}
235 }
236 
237 /*
238  * Local variables:
239  * tab-width: 4
240  * c-basic-offset: 4
241  * End:
242  * vim600: sw=4 ts=4 fdm=marker
243  * vim<600: sw=4 ts=4
244  */
245