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