6268652d1e172fd2c900aab154927cb81af16382
[framework/connectivity/connman.git] / 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 /*
37  * Plugins that are using libraries with threads and their own mainloop
38  * will crash on exit. This is a bug inside these libraries, but there is
39  * nothing much that can be done about it.
40  */
41 #ifdef NEED_THREADS
42 #define PLUGINFLAG (RTLD_NOW | RTLD_NODELETE)
43 #else
44 #define PLUGINFLAG (RTLD_NOW)
45 #endif
46
47 static GSList *plugins = NULL;
48
49 struct connman_plugin {
50         void *handle;
51         gboolean active;
52         struct connman_plugin_desc *desc;
53 };
54
55 static gint compare_priority(gconstpointer a, gconstpointer b)
56 {
57         const struct connman_plugin *plugin1 = a;
58         const struct connman_plugin *plugin2 = b;
59
60         return plugin2->desc->priority - plugin1->desc->priority;
61 }
62
63 static gboolean add_plugin(void *handle, struct connman_plugin_desc *desc)
64 {
65         struct connman_plugin *plugin;
66
67         if (desc->init == NULL)
68                 return FALSE;
69
70         if (g_str_equal(desc->version, CONNMAN_VERSION) == FALSE) {
71                 connman_error("Version mismatch for %s", desc->description);
72                 return FALSE;
73         }
74
75         plugin = g_try_new0(struct connman_plugin, 1);
76         if (plugin == NULL)
77                 return FALSE;
78
79         plugin->handle = handle;
80         plugin->active = FALSE;
81         plugin->desc = desc;
82
83         plugins = g_slist_insert_sorted(plugins, plugin, compare_priority);
84
85         return TRUE;
86 }
87
88 static gboolean check_plugin(struct connman_plugin_desc *desc,
89                                 const char *pattern, const char *exclude)
90 {
91         if (exclude != NULL &&
92                         g_pattern_match_simple(exclude, desc->name) == TRUE) {
93                 connman_info("Excluding %s", desc->description);
94                 return FALSE;
95         }
96
97         if (pattern != NULL &&
98                         g_pattern_match_simple(pattern, desc->name) == FALSE) {
99                 connman_info("Ignoring %s", desc->description);
100                 return FALSE;
101         }
102
103         return TRUE;
104 }
105
106 #include "builtin.h"
107
108 int __connman_plugin_init(const char *pattern, const char *exclude)
109 {
110         GSList *list;
111         GDir *dir;
112         const gchar *file;
113         gchar *filename;
114         unsigned int i;
115
116         DBG("");
117
118         for (i = 0; __connman_builtin[i]; i++) {
119                 if (check_plugin(__connman_builtin[i],
120                                                 pattern, exclude) == FALSE)
121                         continue;
122
123                 add_plugin(NULL, __connman_builtin[i]);
124         }
125
126         dir = g_dir_open(PLUGINDIR, 0, NULL);
127         if (dir != NULL) {
128                 while ((file = g_dir_read_name(dir)) != NULL) {
129                         void *handle;
130                         struct connman_plugin_desc *desc;
131
132                         if (g_str_has_prefix(file, "lib") == TRUE ||
133                                         g_str_has_suffix(file, ".so") == FALSE)
134                                 continue;
135
136                         filename = g_build_filename(PLUGINDIR, file, NULL);
137
138                         handle = dlopen(filename, PLUGINFLAG);
139                         if (handle == NULL) {
140                                 connman_error("Can't load %s: %s",
141                                                         filename, dlerror());
142                                 g_free(filename);
143                                 continue;
144                         }
145
146                         g_free(filename);
147
148                         desc = dlsym(handle, "connman_plugin_desc");
149                         if (desc == NULL) {
150                                 connman_error("Can't load symbol: %s",
151                                                                 dlerror());
152                                 dlclose(handle);
153                                 continue;
154                         }
155
156                         if (check_plugin(desc, pattern, exclude) == FALSE) {
157                                 dlclose(handle);
158                                 continue;
159                         }
160
161                         if (add_plugin(handle, desc) == FALSE)
162                                 dlclose(handle);
163                 }
164
165                 g_dir_close(dir);
166         }
167
168         for (list = plugins; list; list = list->next) {
169                 struct connman_plugin *plugin = list->data;
170
171                 if (plugin->desc->init() < 0)
172                         continue;
173
174                 plugin->active = TRUE;
175         }
176
177         return 0;
178 }
179
180 void __connman_plugin_cleanup(void)
181 {
182         GSList *list;
183
184         DBG("");
185
186         for (list = plugins; list; list = list->next) {
187                 struct connman_plugin *plugin = list->data;
188
189                 if (plugin->active == TRUE && plugin->desc->exit)
190                         plugin->desc->exit();
191
192                 if (plugin->handle != NULL)
193                         dlclose(plugin->handle);
194
195                 g_free(plugin);
196         }
197
198         g_slist_free(plugins);
199 }