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