xref: /PHP-8.0/ext/mysqlnd/php_mysqlnd.c (revision 56135654)
1 /*
2   +----------------------------------------------------------------------+
3   | Copyright (c) The PHP Group                                          |
4   +----------------------------------------------------------------------+
5   | This source file is subject to version 3.01 of the PHP license,      |
6   | that is bundled with this package in the file LICENSE, and is        |
7   | available through the world-wide-web at the following url:           |
8   | http://www.php.net/license/3_01.txt                                  |
9   | If you did not receive a copy of the PHP license and are unable to   |
10   | obtain it through the world-wide-web, please send a note to          |
11   | license@php.net so we can mail you a copy immediately.               |
12   +----------------------------------------------------------------------+
13   | Authors: Andrey Hristov <andrey@php.net>                             |
14   |          Ulf Wendel <uw@php.net>                                     |
15   +----------------------------------------------------------------------+
16 */
17 
18 #include "php.h"
19 #include "mysqlnd.h"
20 #include "mysqlnd_priv.h"
21 #include "mysqlnd_debug.h"
22 #include "mysqlnd_statistics.h"
23 #include "mysqlnd_reverse_api.h"
24 #include "ext/standard/info.h"
25 #include "zend_smart_str.h"
26 
27 /* {{{ mysqlnd_minfo_print_hash */
28 PHPAPI void
mysqlnd_minfo_print_hash(zval * values)29 mysqlnd_minfo_print_hash(zval *values)
30 {
31 	zval *values_entry;
32 	zend_string	*string_key;
33 
34 	ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(values), string_key, values_entry) {
35 		convert_to_string(values_entry);
36 		php_info_print_table_row(2, ZSTR_VAL(string_key), Z_STRVAL_P(values_entry));
37 	} ZEND_HASH_FOREACH_END();
38 }
39 /* }}} */
40 
41 
42 /* {{{ mysqlnd_minfo_dump_loaded_plugins */
43 static int
mysqlnd_minfo_dump_loaded_plugins(zval * el,void * buf)44 mysqlnd_minfo_dump_loaded_plugins(zval *el, void * buf)
45 {
46 	smart_str * buffer = (smart_str *) buf;
47 	struct st_mysqlnd_plugin_header * plugin_header = (struct st_mysqlnd_plugin_header *)Z_PTR_P(el);
48 	if (plugin_header->plugin_name) {
49 		if (buffer->s) {
50 			smart_str_appendc(buffer, ',');
51 		}
52 		smart_str_appends(buffer, plugin_header->plugin_name);
53 	}
54 	return ZEND_HASH_APPLY_KEEP;
55 }
56 /* }}} */
57 
58 
59 /* {{{ mysqlnd_minfo_dump_api_plugins */
60 static void
mysqlnd_minfo_dump_api_plugins(smart_str * buffer)61 mysqlnd_minfo_dump_api_plugins(smart_str * buffer)
62 {
63 	HashTable *ht = mysqlnd_reverse_api_get_api_list();
64 	MYSQLND_REVERSE_API *ext;
65 
66 	ZEND_HASH_FOREACH_PTR(ht, ext) {
67 		if (buffer->s) {
68 			smart_str_appendc(buffer, ',');
69 		}
70 		smart_str_appends(buffer, ext->module->name);
71 	} ZEND_HASH_FOREACH_END();
72 }
73 /* }}} */
74 
75 
76 /* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(mysqlnd)77 PHP_MINFO_FUNCTION(mysqlnd)
78 {
79 	char buf[32];
80 
81 	php_info_print_table_start();
82 	php_info_print_table_header(2, "mysqlnd", "enabled");
83 	php_info_print_table_row(2, "Version", mysqlnd_get_client_info());
84 	php_info_print_table_row(2, "Compression",
85 #ifdef MYSQLND_COMPRESSION_ENABLED
86 								"supported");
87 #else
88 								"not supported");
89 #endif
90 	php_info_print_table_row(2, "core SSL",
91 #ifdef MYSQLND_SSL_SUPPORTED
92 								"supported");
93 #else
94 								"not supported");
95 #endif
96 	php_info_print_table_row(2, "extended SSL",
97 #ifdef MYSQLND_HAVE_SSL
98 								"supported");
99 #else
100 								"not supported");
101 #endif
102 	snprintf(buf, sizeof(buf), ZEND_LONG_FMT, MYSQLND_G(net_cmd_buffer_size));
103 	php_info_print_table_row(2, "Command buffer size", buf);
104 	snprintf(buf, sizeof(buf), ZEND_LONG_FMT, MYSQLND_G(net_read_buffer_size));
105 	php_info_print_table_row(2, "Read buffer size", buf);
106 	snprintf(buf, sizeof(buf), ZEND_LONG_FMT, MYSQLND_G(net_read_timeout));
107 	php_info_print_table_row(2, "Read timeout", buf);
108 	php_info_print_table_row(2, "Collecting statistics", MYSQLND_G(collect_statistics)? "Yes":"No");
109 	php_info_print_table_row(2, "Collecting memory statistics", MYSQLND_G(collect_memory_statistics)? "Yes":"No");
110 
111 	php_info_print_table_row(2, "Tracing", MYSQLND_G(debug)? MYSQLND_G(debug):"n/a");
112 
113 	/* loaded plugins */
114 	{
115 		smart_str tmp_str = {0};
116 		mysqlnd_plugin_apply_with_argument(mysqlnd_minfo_dump_loaded_plugins, &tmp_str);
117 		smart_str_0(&tmp_str);
118 		php_info_print_table_row(2, "Loaded plugins", tmp_str.s? ZSTR_VAL(tmp_str.s) : "");
119 		smart_str_free(&tmp_str);
120 
121 		mysqlnd_minfo_dump_api_plugins(&tmp_str);
122 		smart_str_0(&tmp_str);
123 		php_info_print_table_row(2, "API Extensions", tmp_str.s? ZSTR_VAL(tmp_str.s) : "");
124 		smart_str_free(&tmp_str);
125 	}
126 
127 	php_info_print_table_end();
128 }
129 /* }}} */
130 
131 
ZEND_DECLARE_MODULE_GLOBALS(mysqlnd)132 PHPAPI ZEND_DECLARE_MODULE_GLOBALS(mysqlnd)
133 
134 
135 /* {{{ PHP_GINIT_FUNCTION */
136 static PHP_GINIT_FUNCTION(mysqlnd)
137 {
138 #if defined(COMPILE_DL_MYSQLND) && defined(ZTS)
139 	ZEND_TSRMLS_CACHE_UPDATE();
140 #endif
141 	mysqlnd_globals->collect_statistics = TRUE;
142 	mysqlnd_globals->collect_memory_statistics = FALSE;
143 	mysqlnd_globals->debug = NULL;	/* The actual string */
144 	mysqlnd_globals->dbg = NULL;	/* The DBG object*/
145 	mysqlnd_globals->trace_alloc_settings = NULL;
146 	mysqlnd_globals->trace_alloc = NULL;
147 	mysqlnd_globals->net_cmd_buffer_size = MYSQLND_NET_CMD_BUFFER_MIN_SIZE;
148 	mysqlnd_globals->net_read_buffer_size = 32768;
149 	mysqlnd_globals->net_read_timeout = 31536000;
150 	mysqlnd_globals->log_mask = 0;
151 	mysqlnd_globals->mempool_default_size = 16000;
152 	mysqlnd_globals->debug_emalloc_fail_threshold = -1;
153 	mysqlnd_globals->debug_ecalloc_fail_threshold = -1;
154 	mysqlnd_globals->debug_erealloc_fail_threshold = -1;
155 	mysqlnd_globals->debug_malloc_fail_threshold = -1;
156 	mysqlnd_globals->debug_calloc_fail_threshold = -1;
157 	mysqlnd_globals->debug_realloc_fail_threshold = -1;
158 	mysqlnd_globals->sha256_server_public_key = NULL;
159 	mysqlnd_globals->fetch_data_copy = FALSE;
160 }
161 /* }}} */
162 
163 
164 /* {{{ PHP_INI_MH */
PHP_INI_MH(OnUpdateNetCmdBufferSize)165 static PHP_INI_MH(OnUpdateNetCmdBufferSize)
166 {
167 	zend_long long_value;
168 
169 	ZEND_ATOL(long_value, ZSTR_VAL(new_value));
170 	if (long_value < MYSQLND_NET_CMD_BUFFER_MIN_SIZE) {
171 		return FAILURE;
172 	}
173 	MYSQLND_G(net_cmd_buffer_size) = long_value;
174 
175 	return SUCCESS;
176 }
177 /* }}} */
178 
179 
180 /* {{{ PHP_INI_BEGIN */
181 PHP_INI_BEGIN()
182 	STD_PHP_INI_BOOLEAN("mysqlnd.collect_statistics",	"1", 	PHP_INI_ALL,	OnUpdateBool,	collect_statistics, zend_mysqlnd_globals, mysqlnd_globals)
183 	STD_PHP_INI_BOOLEAN("mysqlnd.collect_memory_statistics","0",PHP_INI_SYSTEM, OnUpdateBool,	collect_memory_statistics, zend_mysqlnd_globals, mysqlnd_globals)
184 	STD_PHP_INI_ENTRY("mysqlnd.debug",					NULL, 	PHP_INI_SYSTEM, OnUpdateString,	debug, zend_mysqlnd_globals, mysqlnd_globals)
185 	STD_PHP_INI_ENTRY("mysqlnd.trace_alloc",			NULL, 	PHP_INI_SYSTEM, OnUpdateString,	trace_alloc_settings, zend_mysqlnd_globals, mysqlnd_globals)
186 	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)
187 	STD_PHP_INI_ENTRY("mysqlnd.net_read_buffer_size",	"32768",PHP_INI_ALL,	OnUpdateLong,	net_read_buffer_size,	zend_mysqlnd_globals,		mysqlnd_globals)
188 	STD_PHP_INI_ENTRY("mysqlnd.net_read_timeout",		"86400",PHP_INI_ALL,	OnUpdateLong,	net_read_timeout, zend_mysqlnd_globals, mysqlnd_globals)
189 	STD_PHP_INI_ENTRY("mysqlnd.log_mask",				"0", 	PHP_INI_ALL,	OnUpdateLong,	log_mask, zend_mysqlnd_globals, mysqlnd_globals)
190 	STD_PHP_INI_ENTRY("mysqlnd.mempool_default_size","16000",   PHP_INI_ALL,	OnUpdateLong,	mempool_default_size,	zend_mysqlnd_globals,		mysqlnd_globals)
191 	STD_PHP_INI_ENTRY("mysqlnd.sha256_server_public_key",NULL, 	PHP_INI_PERDIR, OnUpdateString,	sha256_server_public_key, zend_mysqlnd_globals, mysqlnd_globals)
192 	STD_PHP_INI_BOOLEAN("mysqlnd.fetch_data_copy",	"0", 		PHP_INI_ALL,	OnUpdateBool,	fetch_data_copy, zend_mysqlnd_globals, mysqlnd_globals)
193 #if PHP_DEBUG
194 	STD_PHP_INI_ENTRY("mysqlnd.debug_emalloc_fail_threshold","-1",   PHP_INI_SYSTEM,	OnUpdateLong,	debug_emalloc_fail_threshold,	zend_mysqlnd_globals,		mysqlnd_globals)
195 	STD_PHP_INI_ENTRY("mysqlnd.debug_ecalloc_fail_threshold","-1",   PHP_INI_SYSTEM,	OnUpdateLong,	debug_ecalloc_fail_threshold,	zend_mysqlnd_globals,		mysqlnd_globals)
196 	STD_PHP_INI_ENTRY("mysqlnd.debug_erealloc_fail_threshold","-1",   PHP_INI_SYSTEM,	OnUpdateLong,	debug_erealloc_fail_threshold,	zend_mysqlnd_globals,		mysqlnd_globals)
197 
198 	STD_PHP_INI_ENTRY("mysqlnd.debug_malloc_fail_threshold","-1",   PHP_INI_SYSTEM,	OnUpdateLong,	debug_malloc_fail_threshold,	zend_mysqlnd_globals,		mysqlnd_globals)
199 	STD_PHP_INI_ENTRY("mysqlnd.debug_calloc_fail_threshold","-1",   PHP_INI_SYSTEM,	OnUpdateLong,	debug_calloc_fail_threshold,	zend_mysqlnd_globals,		mysqlnd_globals)
200 	STD_PHP_INI_ENTRY("mysqlnd.debug_realloc_fail_threshold","-1",   PHP_INI_SYSTEM,	OnUpdateLong,	debug_realloc_fail_threshold,	zend_mysqlnd_globals,		mysqlnd_globals)
201 #endif
PHP_INI_END()202 PHP_INI_END()
203 /* }}} */
204 
205 
206 /* {{{ PHP_MINIT_FUNCTION */
207 static PHP_MINIT_FUNCTION(mysqlnd)
208 {
209 	REGISTER_INI_ENTRIES();
210 
211 	mysqlnd_library_init();
212 	return SUCCESS;
213 }
214 /* }}} */
215 
216 
217 /* {{{ PHP_MSHUTDOWN_FUNCTION */
PHP_MSHUTDOWN_FUNCTION(mysqlnd)218 static PHP_MSHUTDOWN_FUNCTION(mysqlnd)
219 {
220 	mysqlnd_library_end();
221 
222 	UNREGISTER_INI_ENTRIES();
223 	return SUCCESS;
224 }
225 /* }}} */
226 
227 
228 #if PHP_DEBUG
229 /* {{{ PHP_RINIT_FUNCTION */
PHP_RINIT_FUNCTION(mysqlnd)230 static PHP_RINIT_FUNCTION(mysqlnd)
231 {
232 	if (MYSQLND_G(debug)) {
233 		struct st_mysqlnd_plugin_trace_log * trace_log_plugin = mysqlnd_plugin_find("debug_trace");
234 		MYSQLND_G(dbg) = NULL;
235 		if (trace_log_plugin) {
236 			MYSQLND_DEBUG * dbg = trace_log_plugin->methods.trace_instance_init(mysqlnd_debug_std_no_trace_funcs);
237 			MYSQLND_DEBUG * trace_alloc = trace_log_plugin->methods.trace_instance_init(NULL);
238 			if (!dbg || !trace_alloc) {
239 				return FAILURE;
240 			}
241 			dbg->m->set_mode(dbg, MYSQLND_G(debug));
242 			trace_alloc->m->set_mode(trace_alloc, MYSQLND_G(trace_alloc_settings));
243 			MYSQLND_G(dbg) = dbg;
244 			MYSQLND_G(trace_alloc) = trace_alloc;
245 		}
246 	}
247 	return SUCCESS;
248 }
249 /* }}} */
250 #endif
251 
252 
253 #if PHP_DEBUG
254 /* {{{ PHP_RSHUTDOWN_FUNCTION */
PHP_RSHUTDOWN_FUNCTION(mysqlnd)255 static PHP_RSHUTDOWN_FUNCTION(mysqlnd)
256 {
257 	MYSQLND_DEBUG * dbg = MYSQLND_G(dbg);
258 	MYSQLND_DEBUG * trace_alloc = MYSQLND_G(trace_alloc);
259 	DBG_ENTER("RSHUTDOWN");
260 	if (dbg) {
261 		dbg->m->close(dbg);
262 		dbg->m->free_handle(dbg);
263 		MYSQLND_G(dbg) = NULL;
264 	}
265 	if (trace_alloc) {
266 		trace_alloc->m->close(trace_alloc);
267 		trace_alloc->m->free_handle(trace_alloc);
268 		MYSQLND_G(trace_alloc) = NULL;
269 	}
270 	return SUCCESS;
271 }
272 /* }}} */
273 #endif
274 
275 
276 static const zend_module_dep mysqlnd_deps[] = {
277 	ZEND_MOD_REQUIRED("standard")
278 	ZEND_MOD_END
279 };
280 
281 /* {{{ mysqlnd_module_entry */
282 zend_module_entry mysqlnd_module_entry = {
283 	STANDARD_MODULE_HEADER_EX,
284 	NULL,
285 	mysqlnd_deps,
286 	"mysqlnd",
287 	NULL,
288 	PHP_MINIT(mysqlnd),
289 	PHP_MSHUTDOWN(mysqlnd),
290 #if PHP_DEBUG
291 	PHP_RINIT(mysqlnd),
292 #else
293 	NULL,
294 #endif
295 #if PHP_DEBUG
296 	PHP_RSHUTDOWN(mysqlnd),
297 #else
298 	NULL,
299 #endif
300 	PHP_MINFO(mysqlnd),
301 	PHP_MYSQLND_VERSION,
302 	PHP_MODULE_GLOBALS(mysqlnd),
303 	PHP_GINIT(mysqlnd),
304 	NULL,
305 	NULL,
306 	STANDARD_MODULE_PROPERTIES_EX
307 };
308 /* }}} */
309 
310 /* {{{ COMPILE_DL_MYSQLND */
311 #ifdef COMPILE_DL_MYSQLND
312 #ifdef ZTS
313 ZEND_TSRMLS_CACHE_DEFINE()
314 #endif
315 ZEND_GET_MODULE(mysqlnd)
316 #endif
317 /* }}} */
318