xref: /PHP-7.0/ext/mysqlnd/mysqlnd_plugin.c (revision 478f119a)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 2006-2017 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   |          Georg Richter <georg@php.net>                               |
18   +----------------------------------------------------------------------+
19 */
20 
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 /*--------------------------------------------------------------------*/
28 #if defined(MYSQLND_DBG_ENABLED) && MYSQLND_DBG_ENABLED == 1
29 static enum_func_status mysqlnd_example_plugin_end(void * p);
30 
31 static MYSQLND_STATS * mysqlnd_plugin_example_stats = NULL;
32 
33 enum mysqlnd_plugin_example_collected_stats
34 {
35 	EXAMPLE_STAT1,
36 	EXAMPLE_STAT2,
37 	EXAMPLE_STAT_LAST
38 };
39 
40 static const MYSQLND_STRING mysqlnd_plugin_example_stats_values_names[EXAMPLE_STAT_LAST] =
41 {
42 	{ MYSQLND_STR_W_LEN("stat1") },
43 	{ MYSQLND_STR_W_LEN("stat2") }
44 };
45 
46 static struct st_mysqlnd_typeii_plugin_example mysqlnd_example_plugin =
47 {
48 	{
49 		MYSQLND_PLUGIN_API_VERSION,
50 		"example",
51 		10001L,
52 		"1.00.01",
53 		"PHP License",
54 		"Andrey Hristov <andrey@php.net>",
55 		{
56 			NULL, /* will be filled later */
57 			mysqlnd_plugin_example_stats_values_names,
58 		},
59 		{
60 			mysqlnd_example_plugin_end
61 		}
62 	},
63 	NULL,	/* methods */
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 
192 
193 /*
194  * Local variables:
195  * tab-width: 4
196  * c-basic-offset: 4
197  * End:
198  * vim600: noet sw=4 ts=4 fdm=marker
199  * vim<600: noet sw=4 ts=4
200  */
201