5 * Copyright (C) 2007-2009 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
32 static GSList *plugins = NULL;
34 struct connman_plugin {
37 struct connman_plugin_desc *desc;
40 static gint compare_priority(gconstpointer a, gconstpointer b)
42 const struct connman_plugin *plugin1 = a;
43 const struct connman_plugin *plugin2 = b;
45 return plugin2->desc->priority - plugin1->desc->priority;
48 static gboolean add_plugin(void *handle, struct connman_plugin_desc *desc)
50 struct connman_plugin *plugin;
52 if (desc->init == NULL)
55 if (g_str_equal(desc->version, CONNMAN_VERSION) == FALSE) {
56 DBG("version mismatch for %s", desc->description);
60 plugin = g_try_new0(struct connman_plugin, 1);
64 plugin->handle = handle;
65 plugin->active = FALSE;
68 plugins = g_slist_insert_sorted(plugins, plugin, compare_priority);
73 int __connman_plugin_init(const char *pattern, const char *exclude)
82 dir = g_dir_open(PLUGINDIR, 0, NULL);
84 while ((file = g_dir_read_name(dir)) != NULL) {
86 struct connman_plugin_desc *desc;
88 if (g_str_has_prefix(file, "lib") == TRUE ||
89 g_str_has_suffix(file, ".so") == FALSE)
92 filename = g_build_filename(PLUGINDIR, file, NULL);
94 handle = dlopen(filename, RTLD_NOW);
96 g_warning("Can't load %s: %s", filename,
104 desc = dlsym(handle, "connman_plugin_desc");
106 g_warning("Can't load symbol: %s", dlerror());
111 if (exclude != NULL && g_pattern_match_simple(exclude,
112 desc->name) == TRUE) {
113 DBG("excluding %s", desc->description);
118 if (pattern != NULL && g_pattern_match_simple(pattern,
119 desc->name) == FALSE) {
120 DBG("ignoring %s", desc->description);
125 if (add_plugin(handle, desc) == FALSE)
132 for (list = plugins; list; list = list->next) {
133 struct connman_plugin *plugin = list->data;
135 if (plugin->desc->init() < 0)
138 plugin->active = TRUE;
144 void __connman_plugin_cleanup(void)
150 for (list = plugins; list; list = list->next) {
151 struct connman_plugin *plugin = list->data;
153 if (plugin->active == TRUE && plugin->desc->exit)
154 plugin->desc->exit();
156 dlclose(plugin->handle);
161 g_slist_free(plugins);