mysqlnd_plugin.c 6.1 KB

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