23caa0a892ab68650af4aa0040251020971960cd
[connman] / src / plugin.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <dlfcn.h>
27
28 #include <glib.h>
29
30 #ifdef CONNMAN_PLUGIN_BUILTIN
31 #undef CONNMAN_PLUGIN_BUILTIN
32 #endif
33
34 #include "connman.h"
35
36 static GSList *plugins = NULL;
37
38 struct connman_plugin {
39         void *handle;
40         gboolean active;
41         struct connman_plugin_desc *desc;
42 };
43
44 static gint compare_priority(gconstpointer a, gconstpointer b)
45 {
46         const struct connman_plugin *plugin1 = a;
47         const struct connman_plugin *plugin2 = b;
48
49         return plugin2->desc->priority - plugin1->desc->priority;
50 }
51
52 static gboolean add_plugin(void *handle, struct connman_plugin_desc *desc)
53 {
54         struct connman_plugin *plugin;
55
56         if (desc->init == NULL)
57                 return FALSE;
58
59         if (g_str_equal(desc->version, CONNMAN_VERSION) == FALSE) {
60                 DBG("version mismatch for %s", desc->description);
61                 return FALSE;
62         }
63
64         plugin = g_try_new0(struct connman_plugin, 1);
65         if (plugin == NULL)
66                 return FALSE;
67
68         plugin->handle = handle;
69         plugin->active = FALSE;
70         plugin->desc = desc;
71
72         plugins = g_slist_insert_sorted(plugins, plugin, compare_priority);
73
74         return TRUE;
75 }
76
77 #include "builtin.h"
78
79 int __connman_plugin_init(const char *pattern, const char *exclude)
80 {
81         GSList *list;
82         GDir *dir;
83         const gchar *file;
84         gchar *filename;
85         unsigned int i;
86
87         DBG("");
88
89         for (i = 0; __connman_builtin[i]; i++)
90                 add_plugin(NULL, __connman_builtin[i]);
91
92         dir = g_dir_open(PLUGINDIR, 0, NULL);
93         if (dir != NULL) {
94                 while ((file = g_dir_read_name(dir)) != NULL) {
95                         void *handle;
96                         struct connman_plugin_desc *desc;
97
98                         if (g_str_has_prefix(file, "lib") == TRUE ||
99                                         g_str_has_suffix(file, ".so") == FALSE)
100                                 continue;
101
102                         filename = g_build_filename(PLUGINDIR, file, NULL);
103
104                         handle = dlopen(filename, RTLD_NOW);
105                         if (handle == NULL) {
106                                 g_warning("Can't load %s: %s", filename,
107                                                                 dlerror());
108                                 g_free(filename);
109                                 continue;
110                         }
111
112                         g_free(filename);
113
114                         desc = dlsym(handle, "connman_plugin_desc");
115                         if (desc == NULL) {
116                                 g_warning("Can't load symbol: %s", dlerror());
117                                 dlclose(handle);
118                                 continue;
119                         }
120
121                         if (exclude != NULL && g_pattern_match_simple(exclude,
122                                                         desc->name) == TRUE) {
123                                 DBG("excluding %s", desc->description);
124                                 dlclose(handle);
125                                 continue;
126                         }
127
128                         if (pattern != NULL && g_pattern_match_simple(pattern,
129                                                         desc->name) == FALSE) {
130                                 DBG("ignoring %s", desc->description);
131                                 dlclose(handle);
132                                 continue;
133                         }
134
135                         if (add_plugin(handle, desc) == FALSE)
136                                 dlclose(handle);
137                 }
138
139                 g_dir_close(dir);
140         }
141
142         for (list = plugins; list; list = list->next) {
143                 struct connman_plugin *plugin = list->data;
144
145                 if (plugin->desc->init() < 0)
146                         continue;
147
148                 plugin->active = TRUE;
149         }
150
151         return 0;
152 }
153
154 void __connman_plugin_cleanup(void)
155 {
156         GSList *list;
157
158         DBG("");
159
160         for (list = plugins; list; list = list->next) {
161                 struct connman_plugin *plugin = list->data;
162
163                 if (plugin->active == TRUE && plugin->desc->exit)
164                         plugin->desc->exit();
165
166                 if (plugin->handle != NULL)
167                         dlclose(plugin->handle);
168
169                 g_free(plugin);
170         }
171
172         g_slist_free(plugins);
173 }