php_mysqlnd.c 12 KB

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