xref: /PHP-7.0/ext/mysqlnd/php_mysqlnd.c (revision 478f119a)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 2006-2017 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: Andrey Hristov <andrey@php.net>                             |
16   |          Ulf Wendel <uw@php.net>                                     |
17   |          Georg Richter <georg@php.net>                               |
18   +----------------------------------------------------------------------+
19 */
20 
21 #include "php.h"
22 #include "php_ini.h"
23 #include "mysqlnd.h"
24 #include "mysqlnd_priv.h"
25 #include "mysqlnd_debug.h"
26 #include "mysqlnd_statistics.h"
27 #include "mysqlnd_reverse_api.h"
28 #include "ext/standard/info.h"
29 #include "zend_smart_str.h"
30 
31 /* {{{ mysqlnd_functions[]
32  *
33  * Every user visible function must have an entry in mysqlnd_functions[].
34  */
35 static zend_function_entry mysqlnd_functions[] = {
36 	PHP_FE_END
37 };
38 /* }}} */
39 
40 
41 /* {{{ mysqlnd_minfo_print_hash */
42 PHPAPI void
mysqlnd_minfo_print_hash(zval * values)43 mysqlnd_minfo_print_hash(zval *values)
44 {
45 	zval *values_entry;
46 	zend_string	*string_key;
47 
48 	ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(values), string_key, values_entry) {
49 		convert_to_string(values_entry);
50 		php_info_print_table_row(2, ZSTR_VAL(string_key), Z_STRVAL_P(values_entry));
51 	} ZEND_HASH_FOREACH_END();
52 }
53 /* }}} */
54 
55 
56 /* {{{ mysqlnd_minfo_dump_plugin_stats */
57 static int
mysqlnd_minfo_dump_plugin_stats(zval * el,void * argument)58 mysqlnd_minfo_dump_plugin_stats(zval *el, void * argument)
59 {
60 	struct st_mysqlnd_plugin_header * plugin_header = (struct st_mysqlnd_plugin_header *)Z_PTR_P(el);
61 	if (plugin_header->plugin_stats.values) {
62 		char buf[64];
63 		zval values;
64 		snprintf(buf, sizeof(buf), "%s statistics", plugin_header->plugin_name);
65 
66 		mysqlnd_fill_stats_hash(plugin_header->plugin_stats.values, plugin_header->plugin_stats.names, &values ZEND_FILE_LINE_CC);
67 
68 		php_info_print_table_start();
69 		php_info_print_table_header(2, buf, "");
70 		mysqlnd_minfo_print_hash(&values);
71 		php_info_print_table_end();
72 		zval_dtor(&values);
73 	}
74 	return ZEND_HASH_APPLY_KEEP;
75 }
76 /* }}} */
77 
78 
79 /* {{{ mysqlnd_minfo_dump_loaded_plugins */
80 static int
mysqlnd_minfo_dump_loaded_plugins(zval * el,void * buf)81 mysqlnd_minfo_dump_loaded_plugins(zval *el, void * buf)
82 {
83 	smart_str * buffer = (smart_str *) buf;
84 	struct st_mysqlnd_plugin_header * plugin_header = (struct st_mysqlnd_plugin_header *)Z_PTR_P(el);
85 	if (plugin_header->plugin_name) {
86 		if (buffer->s) {
87 			smart_str_appendc(buffer, ',');
88 		}
89 		smart_str_appends(buffer, plugin_header->plugin_name);
90 	}
91 	return ZEND_HASH_APPLY_KEEP;
92 }
93 /* }}} */
94 
95 
96 /* {{{ mysqlnd_minfo_dump_api_plugins */
97 static void
mysqlnd_minfo_dump_api_plugins(smart_str * buffer)98 mysqlnd_minfo_dump_api_plugins(smart_str * buffer)
99 {
100 	HashTable *ht = mysqlnd_reverse_api_get_api_list();
101 	MYSQLND_REVERSE_API *ext;
102 
103 	ZEND_HASH_FOREACH_PTR(ht, ext) {
104 		if (buffer->s) {
105 			smart_str_appendc(buffer, ',');
106 		}
107 		smart_str_appends(buffer, ext->module->name);
108 	} ZEND_HASH_FOREACH_END();
109 }
110 /* }}} */
111 
112 
113 /* {{{ PHP_MINFO_FUNCTION
114  */
PHP_MINFO_FUNCTION(mysqlnd)115 PHP_MINFO_FUNCTION(mysqlnd)
116 {
117 	char buf[32];
118 
119 	php_info_print_table_start();
120 	php_info_print_table_header(2, "mysqlnd", "enabled");
121 	php_info_print_table_row(2, "Version", mysqlnd_get_client_info());
122 	php_info_print_table_row(2, "Compression",
123 #ifdef MYSQLND_COMPRESSION_ENABLED
124 								"supported");
125 #else
126 								"not supported");
127 #endif
128 	php_info_print_table_row(2, "core SSL",
129 #ifdef MYSQLND_SSL_SUPPORTED
130 								"supported");
131 #else
132 								"not supported");
133 #endif
134 	php_info_print_table_row(2, "extended SSL",
135 #ifdef MYSQLND_HAVE_SSL
136 								"supported");
137 #else
138 								"not supported");
139 #endif
140 	snprintf(buf, sizeof(buf), ZEND_LONG_FMT, MYSQLND_G(net_cmd_buffer_size));
141 	php_info_print_table_row(2, "Command buffer size", buf);
142 	snprintf(buf, sizeof(buf), ZEND_LONG_FMT, MYSQLND_G(net_read_buffer_size));
143 	php_info_print_table_row(2, "Read buffer size", buf);
144 	snprintf(buf, sizeof(buf), ZEND_LONG_FMT, MYSQLND_G(net_read_timeout));
145 	php_info_print_table_row(2, "Read timeout", buf);
146 	php_info_print_table_row(2, "Collecting statistics", MYSQLND_G(collect_statistics)? "Yes":"No");
147 	php_info_print_table_row(2, "Collecting memory statistics", MYSQLND_G(collect_memory_statistics)? "Yes":"No");
148 
149 	php_info_print_table_row(2, "Tracing", MYSQLND_G(debug)? MYSQLND_G(debug):"n/a");
150 
151 	/* loaded plugins */
152 	{
153 		smart_str tmp_str = {0};
154 		mysqlnd_plugin_apply_with_argument(mysqlnd_minfo_dump_loaded_plugins, &tmp_str);
155 		smart_str_0(&tmp_str);
156 		php_info_print_table_row(2, "Loaded plugins", tmp_str.s? ZSTR_VAL(tmp_str.s) : "");
157 		smart_str_free(&tmp_str);
158 
159 		mysqlnd_minfo_dump_api_plugins(&tmp_str);
160 		smart_str_0(&tmp_str);
161 		php_info_print_table_row(2, "API Extensions", tmp_str.s? ZSTR_VAL(tmp_str.s) : "");
162 		smart_str_free(&tmp_str);
163 	}
164 
165 	php_info_print_table_end();
166 
167 
168 	/* Print client stats */
169 	mysqlnd_plugin_apply_with_argument(mysqlnd_minfo_dump_plugin_stats, NULL);
170 }
171 /* }}} */
172 
173 
ZEND_DECLARE_MODULE_GLOBALS(mysqlnd)174 PHPAPI ZEND_DECLARE_MODULE_GLOBALS(mysqlnd)
175 
176 
177 /* {{{ PHP_GINIT_FUNCTION
178  */
179 static PHP_GINIT_FUNCTION(mysqlnd)
180 {
181 #if defined(COMPILE_DL_MYSQLND) && defined(ZTS)
182 	ZEND_TSRMLS_CACHE_UPDATE();
183 #endif
184 	mysqlnd_globals->collect_statistics = TRUE;
185 	mysqlnd_globals->collect_memory_statistics = FALSE;
186 	mysqlnd_globals->debug = NULL;	/* The actual string */
187 	mysqlnd_globals->dbg = NULL;	/* The DBG object*/
188 	mysqlnd_globals->trace_alloc_settings = NULL;
189 	mysqlnd_globals->trace_alloc = NULL;
190 	mysqlnd_globals->net_cmd_buffer_size = MYSQLND_NET_CMD_BUFFER_MIN_SIZE;
191 	mysqlnd_globals->net_read_buffer_size = 32768;
192 	mysqlnd_globals->net_read_timeout = 31536000;
193 	mysqlnd_globals->log_mask = 0;
194 	mysqlnd_globals->mempool_default_size = 16000;
195 	mysqlnd_globals->debug_emalloc_fail_threshold = -1;
196 	mysqlnd_globals->debug_ecalloc_fail_threshold = -1;
197 	mysqlnd_globals->debug_erealloc_fail_threshold = -1;
198 	mysqlnd_globals->debug_malloc_fail_threshold = -1;
199 	mysqlnd_globals->debug_calloc_fail_threshold = -1;
200 	mysqlnd_globals->debug_realloc_fail_threshold = -1;
201 	mysqlnd_globals->sha256_server_public_key = NULL;
202 	mysqlnd_globals->fetch_data_copy = FALSE;
203 }
204 /* }}} */
205 
206 
207 /* {{{ PHP_INI_MH
208  */
PHP_INI_MH(OnUpdateNetCmdBufferSize)209 static PHP_INI_MH(OnUpdateNetCmdBufferSize)
210 {
211 	zend_long long_value;
212 
213 	ZEND_ATOL(long_value, ZSTR_VAL(new_value));
214 	if (long_value < MYSQLND_NET_CMD_BUFFER_MIN_SIZE) {
215 		return FAILURE;
216 	}
217 	MYSQLND_G(net_cmd_buffer_size) = long_value;
218 
219 	return SUCCESS;
220 }
221 /* }}} */
222 
223 
224 /* {{{ PHP_INI_BEGIN
225 */
226 PHP_INI_BEGIN()
227 	STD_PHP_INI_BOOLEAN("mysqlnd.collect_statistics",	"1", 	PHP_INI_ALL,	OnUpdateBool,	collect_statistics, zend_mysqlnd_globals, mysqlnd_globals)
228 	STD_PHP_INI_BOOLEAN("mysqlnd.collect_memory_statistics","0",PHP_INI_SYSTEM, OnUpdateBool,	collect_memory_statistics, zend_mysqlnd_globals, mysqlnd_globals)
229 	STD_PHP_INI_ENTRY("mysqlnd.debug",					NULL, 	PHP_INI_SYSTEM, OnUpdateString,	debug, zend_mysqlnd_globals, mysqlnd_globals)
230 	STD_PHP_INI_ENTRY("mysqlnd.trace_alloc",			NULL, 	PHP_INI_SYSTEM, OnUpdateString,	trace_alloc_settings, zend_mysqlnd_globals, mysqlnd_globals)
231 	STD_PHP_INI_ENTRY("mysqlnd.net_cmd_buffer_size",	MYSQLND_NET_CMD_BUFFER_MIN_SIZE_STR,	PHP_INI_ALL,	OnUpdateNetCmdBufferSize,	net_cmd_buffer_size,	zend_mysqlnd_globals,		mysqlnd_globals)
232 	STD_PHP_INI_ENTRY("mysqlnd.net_read_buffer_size",	"32768",PHP_INI_ALL,	OnUpdateLong,	net_read_buffer_size,	zend_mysqlnd_globals,		mysqlnd_globals)
233 	STD_PHP_INI_ENTRY("mysqlnd.net_read_timeout",	"31536000",	PHP_INI_SYSTEM, OnUpdateLong,	net_read_timeout, zend_mysqlnd_globals, mysqlnd_globals)
234 	STD_PHP_INI_ENTRY("mysqlnd.log_mask",				"0", 	PHP_INI_ALL,	OnUpdateLong,	log_mask, zend_mysqlnd_globals, mysqlnd_globals)
235 	STD_PHP_INI_ENTRY("mysqlnd.mempool_default_size","16000",   PHP_INI_ALL,	OnUpdateLong,	mempool_default_size,	zend_mysqlnd_globals,		mysqlnd_globals)
236 	STD_PHP_INI_ENTRY("mysqlnd.sha256_server_public_key",NULL, 	PHP_INI_PERDIR, OnUpdateString,	sha256_server_public_key, zend_mysqlnd_globals, mysqlnd_globals)
237 	STD_PHP_INI_BOOLEAN("mysqlnd.fetch_data_copy",	"0", 		PHP_INI_ALL,	OnUpdateBool,	fetch_data_copy, zend_mysqlnd_globals, mysqlnd_globals)
238 #if PHP_DEBUG
239 	STD_PHP_INI_ENTRY("mysqlnd.debug_emalloc_fail_threshold","-1",   PHP_INI_SYSTEM,	OnUpdateLong,	debug_emalloc_fail_threshold,	zend_mysqlnd_globals,		mysqlnd_globals)
240 	STD_PHP_INI_ENTRY("mysqlnd.debug_ecalloc_fail_threshold","-1",   PHP_INI_SYSTEM,	OnUpdateLong,	debug_ecalloc_fail_threshold,	zend_mysqlnd_globals,		mysqlnd_globals)
241 	STD_PHP_INI_ENTRY("mysqlnd.debug_erealloc_fail_threshold","-1",   PHP_INI_SYSTEM,	OnUpdateLong,	debug_erealloc_fail_threshold,	zend_mysqlnd_globals,		mysqlnd_globals)
242 
243 	STD_PHP_INI_ENTRY("mysqlnd.debug_malloc_fail_threshold","-1",   PHP_INI_SYSTEM,	OnUpdateLong,	debug_malloc_fail_threshold,	zend_mysqlnd_globals,		mysqlnd_globals)
244 	STD_PHP_INI_ENTRY("mysqlnd.debug_calloc_fail_threshold","-1",   PHP_INI_SYSTEM,	OnUpdateLong,	debug_calloc_fail_threshold,	zend_mysqlnd_globals,		mysqlnd_globals)
245 	STD_PHP_INI_ENTRY("mysqlnd.debug_realloc_fail_threshold","-1",   PHP_INI_SYSTEM,	OnUpdateLong,	debug_realloc_fail_threshold,	zend_mysqlnd_globals,		mysqlnd_globals)
246 #endif
PHP_INI_END()247 PHP_INI_END()
248 /* }}} */
249 
250 
251 /* {{{ PHP_MINIT_FUNCTION
252  */
253 static PHP_MINIT_FUNCTION(mysqlnd)
254 {
255 	REGISTER_INI_ENTRIES();
256 
257 	mysqlnd_library_init();
258 	return SUCCESS;
259 }
260 /* }}} */
261 
262 
263 /* {{{ PHP_MSHUTDOWN_FUNCTION
264  */
PHP_MSHUTDOWN_FUNCTION(mysqlnd)265 static PHP_MSHUTDOWN_FUNCTION(mysqlnd)
266 {
267 	mysqlnd_library_end();
268 
269 	UNREGISTER_INI_ENTRIES();
270 	return SUCCESS;
271 }
272 /* }}} */
273 
274 
275 #if PHP_DEBUG
276 /* {{{ PHP_RINIT_FUNCTION
277  */
PHP_RINIT_FUNCTION(mysqlnd)278 static PHP_RINIT_FUNCTION(mysqlnd)
279 {
280 	if (MYSQLND_G(debug)) {
281 		struct st_mysqlnd_plugin_trace_log * trace_log_plugin = mysqlnd_plugin_find("debug_trace");
282 		MYSQLND_G(dbg) = NULL;
283 		if (trace_log_plugin) {
284 			MYSQLND_DEBUG * dbg = trace_log_plugin->methods.trace_instance_init(mysqlnd_debug_std_no_trace_funcs);
285 			MYSQLND_DEBUG * trace_alloc = trace_log_plugin->methods.trace_instance_init(NULL);
286 			if (!dbg || !trace_alloc) {
287 				return FAILURE;
288 			}
289 			dbg->m->set_mode(dbg, MYSQLND_G(debug));
290 			trace_alloc->m->set_mode(trace_alloc, MYSQLND_G(trace_alloc_settings));
291 			MYSQLND_G(dbg) = dbg;
292 			MYSQLND_G(trace_alloc) = trace_alloc;
293 		}
294 	}
295 	return SUCCESS;
296 }
297 /* }}} */
298 #endif
299 
300 
301 #if PHP_DEBUG
302 /* {{{ PHP_RSHUTDOWN_FUNCTION
303  */
PHP_RSHUTDOWN_FUNCTION(mysqlnd)304 static PHP_RSHUTDOWN_FUNCTION(mysqlnd)
305 {
306 	MYSQLND_DEBUG * dbg = MYSQLND_G(dbg);
307 	MYSQLND_DEBUG * trace_alloc = MYSQLND_G(trace_alloc);
308 	DBG_ENTER("RSHUTDOWN");
309 	if (dbg) {
310 		dbg->m->close(dbg);
311 		dbg->m->free_handle(dbg);
312 		MYSQLND_G(dbg) = NULL;
313 	}
314 	if (trace_alloc) {
315 		trace_alloc->m->close(trace_alloc);
316 		trace_alloc->m->free_handle(trace_alloc);
317 		MYSQLND_G(trace_alloc) = NULL;
318 	}
319 	return SUCCESS;
320 }
321 /* }}} */
322 #endif
323 
324 
325 static const zend_module_dep mysqlnd_deps[] = {
326 	ZEND_MOD_REQUIRED("standard")
327 	ZEND_MOD_END
328 };
329 
330 /* {{{ mysqlnd_module_entry
331  */
332 zend_module_entry mysqlnd_module_entry = {
333 	STANDARD_MODULE_HEADER_EX,
334 	NULL,
335 	mysqlnd_deps,
336 	"mysqlnd",
337 	mysqlnd_functions,
338 	PHP_MINIT(mysqlnd),
339 	PHP_MSHUTDOWN(mysqlnd),
340 #if PHP_DEBUG
341 	PHP_RINIT(mysqlnd),
342 #else
343 	NULL,
344 #endif
345 #if PHP_DEBUG
346 	PHP_RSHUTDOWN(mysqlnd),
347 #else
348 	NULL,
349 #endif
350 	PHP_MINFO(mysqlnd),
351 	PHP_MYSQLND_VERSION,
352 	PHP_MODULE_GLOBALS(mysqlnd),
353 	PHP_GINIT(mysqlnd),
354 	NULL,
355 	NULL,
356 	STANDARD_MODULE_PROPERTIES_EX
357 };
358 /* }}} */
359 
360 /* {{{ COMPILE_DL_MYSQLND */
361 #ifdef COMPILE_DL_MYSQLND
362 #ifdef ZTS
363 ZEND_TSRMLS_CACHE_DEFINE()
364 #endif
365 ZEND_GET_MODULE(mysqlnd)
366 #endif
367 /* }}} */
368 
369 /*
370  * Local variables:
371  * tab-width: 4
372  * c-basic-offset: 4
373  * End:
374  * vim600: noet sw=4 ts=4 fdm=marker
375  * vim<600: noet sw=4 ts=4
376  */
377