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