mysqlnd_plugin.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2006-2018 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@php.net> |
  16. | Ulf Wendel <uw@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. #include "php.h"
  20. #include "mysqlnd.h"
  21. #include "mysqlnd_priv.h"
  22. #include "mysqlnd_statistics.h"
  23. #include "mysqlnd_debug.h"
  24. /*--------------------------------------------------------------------*/
  25. #if defined(MYSQLND_DBG_ENABLED) && MYSQLND_DBG_ENABLED == 1
  26. static enum_func_status mysqlnd_example_plugin_end(void * p);
  27. static MYSQLND_STATS * mysqlnd_plugin_example_stats = NULL;
  28. enum mysqlnd_plugin_example_collected_stats
  29. {
  30. EXAMPLE_STAT1,
  31. EXAMPLE_STAT2,
  32. EXAMPLE_STAT_LAST
  33. };
  34. static const MYSQLND_STRING mysqlnd_plugin_example_stats_values_names[EXAMPLE_STAT_LAST] =
  35. {
  36. { MYSQLND_STR_W_LEN("stat1") },
  37. { MYSQLND_STR_W_LEN("stat2") }
  38. };
  39. static struct st_mysqlnd_typeii_plugin_example mysqlnd_example_plugin =
  40. {
  41. {
  42. MYSQLND_PLUGIN_API_VERSION,
  43. "example",
  44. 10001L,
  45. "1.00.01",
  46. "PHP License",
  47. "Andrey Hristov <andrey@php.net>",
  48. {
  49. NULL, /* will be filled later */
  50. mysqlnd_plugin_example_stats_values_names,
  51. },
  52. {
  53. mysqlnd_example_plugin_end
  54. }
  55. },
  56. NULL, /* methods */
  57. 0
  58. };
  59. /* {{{ mysqlnd_example_plugin_end */
  60. static
  61. enum_func_status mysqlnd_example_plugin_end(void * p)
  62. {
  63. struct st_mysqlnd_typeii_plugin_example * plugin = (struct st_mysqlnd_typeii_plugin_example *) p;
  64. DBG_ENTER("mysqlnd_example_plugin_end");
  65. mysqlnd_stats_end(plugin->plugin_header.plugin_stats.values, 1);
  66. plugin->plugin_header.plugin_stats.values = NULL;
  67. DBG_RETURN(PASS);
  68. }
  69. /* }}} */
  70. /* {{{ mysqlnd_example_plugin_register */
  71. void
  72. mysqlnd_example_plugin_register(void)
  73. {
  74. mysqlnd_stats_init(&mysqlnd_plugin_example_stats, EXAMPLE_STAT_LAST, 1);
  75. mysqlnd_example_plugin.plugin_header.plugin_stats.values = mysqlnd_plugin_example_stats;
  76. mysqlnd_plugin_register_ex((struct st_mysqlnd_plugin_header *) &mysqlnd_example_plugin);
  77. }
  78. /* }}} */
  79. #endif /* defined(MYSQLND_DBG_ENABLED) && MYSQLND_DBG_ENABLED == 1 */
  80. /*--------------------------------------------------------------------*/
  81. static HashTable mysqlnd_registered_plugins;
  82. static unsigned int mysqlnd_plugins_counter = 0;
  83. /* {{{ mysqlnd_plugin_subsystem_init */
  84. void
  85. mysqlnd_plugin_subsystem_init(void)
  86. {
  87. zend_hash_init(&mysqlnd_registered_plugins, 4 /* initial hash size */, NULL /* hash_func */, NULL /* dtor */, TRUE /* pers */);
  88. }
  89. /* }}} */
  90. /* {{{ mysqlnd_plugin_end_apply_func */
  91. int
  92. mysqlnd_plugin_end_apply_func(zval *el)
  93. {
  94. struct st_mysqlnd_plugin_header * plugin_header = (struct st_mysqlnd_plugin_header *)Z_PTR_P(el);
  95. if (plugin_header->m.plugin_shutdown) {
  96. plugin_header->m.plugin_shutdown(plugin_header);
  97. }
  98. return ZEND_HASH_APPLY_REMOVE;
  99. }
  100. /* }}} */
  101. /* {{{ mysqlnd_plugin_subsystem_end */
  102. void
  103. mysqlnd_plugin_subsystem_end(void)
  104. {
  105. zend_hash_apply(&mysqlnd_registered_plugins, mysqlnd_plugin_end_apply_func);
  106. zend_hash_destroy(&mysqlnd_registered_plugins);
  107. }
  108. /* }}} */
  109. /* {{{ mysqlnd_plugin_register */
  110. PHPAPI unsigned int mysqlnd_plugin_register()
  111. {
  112. return mysqlnd_plugin_register_ex(NULL);
  113. }
  114. /* }}} */
  115. /* {{{ mysqlnd_plugin_register_ex */
  116. PHPAPI unsigned int mysqlnd_plugin_register_ex(struct st_mysqlnd_plugin_header * plugin)
  117. {
  118. if (plugin) {
  119. if (plugin->plugin_api_version == MYSQLND_PLUGIN_API_VERSION) {
  120. zend_hash_str_update_ptr(&mysqlnd_registered_plugins, plugin->plugin_name, strlen(plugin->plugin_name), plugin);
  121. } else {
  122. php_error_docref(NULL, E_WARNING, "Plugin API version mismatch while loading plugin %s. Expected %d, got %d",
  123. plugin->plugin_name, MYSQLND_PLUGIN_API_VERSION, plugin->plugin_api_version);
  124. return 0xCAFE;
  125. }
  126. }
  127. return mysqlnd_plugins_counter++;
  128. }
  129. /* }}} */
  130. /* {{{ mysqlnd_plugin_find */
  131. PHPAPI void * mysqlnd_plugin_find(const char * const name)
  132. {
  133. void * plugin;
  134. if ((plugin = zend_hash_str_find_ptr(&mysqlnd_registered_plugins, name, strlen(name))) != NULL) {
  135. return plugin;
  136. }
  137. return NULL;
  138. }
  139. /* }}} */
  140. /* {{{ mysqlnd_plugin_apply_with_argument */
  141. PHPAPI void mysqlnd_plugin_apply_with_argument(apply_func_arg_t apply_func, void * argument)
  142. {
  143. zval *val;
  144. int result;
  145. ZEND_HASH_FOREACH_VAL(&mysqlnd_registered_plugins, val) {
  146. result = apply_func(val, argument);
  147. if (result & ZEND_HASH_APPLY_REMOVE) {
  148. php_error_docref(NULL, E_WARNING, "mysqlnd_plugin_apply_with_argument must not remove table entries");
  149. }
  150. if (result & ZEND_HASH_APPLY_STOP) {
  151. break;
  152. }
  153. } ZEND_HASH_FOREACH_END();
  154. }
  155. /* }}} */
  156. /* {{{ mysqlnd_plugin_count */
  157. PHPAPI unsigned int mysqlnd_plugin_count()
  158. {
  159. return mysqlnd_plugins_counter;
  160. }
  161. /* }}} */
  162. /*
  163. * Local variables:
  164. * tab-width: 4
  165. * c-basic-offset: 4
  166. * End:
  167. * vim600: noet sw=4 ts=4 fdm=marker
  168. * vim<600: noet sw=4 ts=4
  169. */