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