5 * Copyright (C) 2007-2012 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #ifdef CONNMAN_PLUGIN_BUILTIN
31 #undef CONNMAN_PLUGIN_BUILTIN
36 static GSList *plugins = NULL;
38 struct connman_plugin {
41 struct connman_plugin_desc *desc;
44 static gint compare_priority(gconstpointer a, gconstpointer b)
46 const struct connman_plugin *plugin1 = a;
47 const struct connman_plugin *plugin2 = b;
49 return plugin2->desc->priority - plugin1->desc->priority;
52 static bool add_plugin(void *handle, struct connman_plugin_desc *desc)
54 struct connman_plugin *plugin;
59 if (!g_str_equal(desc->version, CONNMAN_VERSION)) {
60 connman_error("Invalid version %s for %s", desc->version,
65 plugin = g_try_new0(struct connman_plugin, 1);
69 plugin->handle = handle;
70 plugin->active = false;
73 __connman_log_enable(desc->debug_start, desc->debug_stop);
75 plugins = g_slist_insert_sorted(plugins, plugin, compare_priority);
80 static bool check_plugin(struct connman_plugin_desc *desc,
81 char **patterns, char **excludes)
84 for (; *excludes; excludes++)
85 if (g_pattern_match_simple(*excludes, desc->name))
88 connman_info("Excluding %s", desc->description);
94 for (; *patterns; patterns++)
95 if (g_pattern_match_simple(*patterns, desc->name))
98 connman_info("Ignoring %s", desc->description);
108 int __connman_plugin_init(const char *pattern, const char *exclude)
110 gchar **patterns = NULL;
111 gchar **excludes = NULL;
121 patterns = g_strsplit_set(pattern, ":, ", -1);
124 excludes = g_strsplit_set(exclude, ":, ", -1);
126 for (i = 0; __connman_builtin[i]; i++) {
127 if (!check_plugin(__connman_builtin[i], patterns, excludes))
130 add_plugin(NULL, __connman_builtin[i]);
133 dir = g_dir_open(PLUGINDIR, 0, NULL);
135 while ((file = g_dir_read_name(dir))) {
137 struct connman_plugin_desc *desc;
139 if (g_str_has_prefix(file, "lib") ||
140 !g_str_has_suffix(file, ".so"))
143 filename = g_build_filename(PLUGINDIR, file, NULL);
145 handle = dlopen(filename, RTLD_NOW);
147 connman_error("Can't load %s: %s",
148 filename, dlerror());
155 desc = dlsym(handle, "connman_plugin_desc");
157 connman_error("Can't load symbol: %s",
163 if (!check_plugin(desc, patterns, excludes)) {
168 if (!add_plugin(handle, desc))
175 for (list = plugins; list; list = list->next) {
176 struct connman_plugin *plugin = list->data;
178 if (plugin->desc->init() < 0)
181 plugin->active = true;
184 g_strfreev(patterns);
185 g_strfreev(excludes);
190 void __connman_plugin_cleanup(void)
196 for (list = plugins; list; list = list->next) {
197 struct connman_plugin *plugin = list->data;
199 if (plugin->active && plugin->desc->exit)
200 plugin->desc->exit();
203 dlclose(plugin->handle);
208 g_slist_free(plugins);