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