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 #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->sha256_server_public_key = NULL;
153 }
154 /* }}} */
155 
156 
157 /* {{{ PHP_INI_MH */
PHP_INI_MH(OnUpdateNetCmdBufferSize)158 static PHP_INI_MH(OnUpdateNetCmdBufferSize)
159 {
160 	zend_long long_value = ZEND_ATOL(ZSTR_VAL(new_value));
161 	if (long_value < MYSQLND_NET_CMD_BUFFER_MIN_SIZE) {
162 		return FAILURE;
163 	}
164 	MYSQLND_G(net_cmd_buffer_size) = long_value;
165 
166 	return SUCCESS;
167 }
168 /* }}} */
169 
170 
171 /* {{{ PHP_INI_BEGIN */
172 PHP_INI_BEGIN()
173 	STD_PHP_INI_BOOLEAN("mysqlnd.collect_statistics",	"1", 	PHP_INI_ALL,	OnUpdateBool,	collect_statistics, zend_mysqlnd_globals, mysqlnd_globals)
174 	STD_PHP_INI_BOOLEAN("mysqlnd.collect_memory_statistics","0",PHP_INI_SYSTEM, OnUpdateBool,	collect_memory_statistics, zend_mysqlnd_globals, mysqlnd_globals)
175 	STD_PHP_INI_ENTRY("mysqlnd.debug",					NULL, 	PHP_INI_SYSTEM, OnUpdateString,	debug, zend_mysqlnd_globals, mysqlnd_globals)
176 	STD_PHP_INI_ENTRY("mysqlnd.trace_alloc",			NULL, 	PHP_INI_SYSTEM, OnUpdateString,	trace_alloc_settings, zend_mysqlnd_globals, mysqlnd_globals)
177 	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)
178 	STD_PHP_INI_ENTRY("mysqlnd.net_read_buffer_size",	"32768",PHP_INI_ALL,	OnUpdateLong,	net_read_buffer_size,	zend_mysqlnd_globals,		mysqlnd_globals)
179 	STD_PHP_INI_ENTRY("mysqlnd.net_read_timeout",		"86400",PHP_INI_ALL,	OnUpdateLong,	net_read_timeout, zend_mysqlnd_globals, mysqlnd_globals)
180 	STD_PHP_INI_ENTRY("mysqlnd.log_mask",				"0", 	PHP_INI_ALL,	OnUpdateLong,	log_mask, zend_mysqlnd_globals, mysqlnd_globals)
181 	STD_PHP_INI_ENTRY("mysqlnd.mempool_default_size","16000",   PHP_INI_ALL,	OnUpdateLong,	mempool_default_size,	zend_mysqlnd_globals,		mysqlnd_globals)
182 	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()183 PHP_INI_END()
184 /* }}} */
185 
186 
187 /* {{{ PHP_MINIT_FUNCTION */
188 static PHP_MINIT_FUNCTION(mysqlnd)
189 {
190 	REGISTER_INI_ENTRIES();
191 
192 	mysqlnd_library_init();
193 	return SUCCESS;
194 }
195 /* }}} */
196 
197 
198 /* {{{ PHP_MSHUTDOWN_FUNCTION */
PHP_MSHUTDOWN_FUNCTION(mysqlnd)199 static PHP_MSHUTDOWN_FUNCTION(mysqlnd)
200 {
201 	mysqlnd_library_end();
202 
203 	UNREGISTER_INI_ENTRIES();
204 	return SUCCESS;
205 }
206 /* }}} */
207 
208 
209 #if PHP_DEBUG
210 /* {{{ PHP_RINIT_FUNCTION */
PHP_RINIT_FUNCTION(mysqlnd)211 static PHP_RINIT_FUNCTION(mysqlnd)
212 {
213 	if (MYSQLND_G(debug)) {
214 		struct st_mysqlnd_plugin_trace_log * trace_log_plugin = mysqlnd_plugin_find("debug_trace");
215 		MYSQLND_G(dbg) = NULL;
216 		if (trace_log_plugin) {
217 			MYSQLND_DEBUG * dbg = trace_log_plugin->methods.trace_instance_init(mysqlnd_debug_std_no_trace_funcs);
218 			MYSQLND_DEBUG * trace_alloc = trace_log_plugin->methods.trace_instance_init(NULL);
219 			if (!dbg || !trace_alloc) {
220 				return FAILURE;
221 			}
222 			dbg->m->set_mode(dbg, MYSQLND_G(debug));
223 			trace_alloc->m->set_mode(trace_alloc, MYSQLND_G(trace_alloc_settings));
224 			MYSQLND_G(dbg) = dbg;
225 			MYSQLND_G(trace_alloc) = trace_alloc;
226 		}
227 	}
228 	return SUCCESS;
229 }
230 /* }}} */
231 #endif
232 
233 
234 #if PHP_DEBUG
235 /* {{{ PHP_RSHUTDOWN_FUNCTION */
PHP_RSHUTDOWN_FUNCTION(mysqlnd)236 static PHP_RSHUTDOWN_FUNCTION(mysqlnd)
237 {
238 	MYSQLND_DEBUG * dbg = MYSQLND_G(dbg);
239 	MYSQLND_DEBUG * trace_alloc = MYSQLND_G(trace_alloc);
240 	DBG_ENTER("RSHUTDOWN");
241 	if (dbg) {
242 		dbg->m->close(dbg);
243 		dbg->m->free_handle(dbg);
244 		MYSQLND_G(dbg) = NULL;
245 	}
246 	if (trace_alloc) {
247 		trace_alloc->m->close(trace_alloc);
248 		trace_alloc->m->free_handle(trace_alloc);
249 		MYSQLND_G(trace_alloc) = NULL;
250 	}
251 	return SUCCESS;
252 }
253 /* }}} */
254 #endif
255 
256 
257 static const zend_module_dep mysqlnd_deps[] = {
258 	ZEND_MOD_REQUIRED("standard")
259 	ZEND_MOD_END
260 };
261 
262 /* {{{ mysqlnd_module_entry */
263 zend_module_entry mysqlnd_module_entry = {
264 	STANDARD_MODULE_HEADER_EX,
265 	NULL,
266 	mysqlnd_deps,
267 	"mysqlnd",
268 	NULL,
269 	PHP_MINIT(mysqlnd),
270 	PHP_MSHUTDOWN(mysqlnd),
271 #if PHP_DEBUG
272 	PHP_RINIT(mysqlnd),
273 #else
274 	NULL,
275 #endif
276 #if PHP_DEBUG
277 	PHP_RSHUTDOWN(mysqlnd),
278 #else
279 	NULL,
280 #endif
281 	PHP_MINFO(mysqlnd),
282 	PHP_MYSQLND_VERSION,
283 	PHP_MODULE_GLOBALS(mysqlnd),
284 	PHP_GINIT(mysqlnd),
285 	NULL,
286 	NULL,
287 	STANDARD_MODULE_PROPERTIES_EX
288 };
289 /* }}} */
290 
291 /* {{{ COMPILE_DL_MYSQLND */
292 #ifdef COMPILE_DL_MYSQLND
293 #ifdef ZTS
294 ZEND_TSRMLS_CACHE_DEFINE()
295 #endif
296 ZEND_GET_MODULE(mysqlnd)
297 #endif
298 /* }}} */
299