ipconfig: Add Function to Stringify ipconfig Type
[framework/connectivity/connman.git] / src / plugin.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  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                                 char **patterns, char **excludes)
90 {
91         if (excludes) {
92                 for (; *excludes; excludes++)
93                         if (g_pattern_match_simple(*excludes, desc->name))
94                                 break;
95                 if (*excludes) {
96                         connman_info("Excluding %s", desc->description);
97                         return FALSE;
98                 }
99         }
100
101         if (patterns) {
102                 for (; *patterns; patterns++)
103                         if (g_pattern_match_simple(*patterns, desc->name))
104                                 break;
105                 if (!*patterns) {
106                         connman_info("Ignoring %s", desc->description);
107                         return FALSE;
108                 }
109         }
110
111         return TRUE;
112 }
113
114 #include "builtin.h"
115
116 int __connman_plugin_init(const char *pattern, const char *exclude)
117 {
118         gchar **patterns = NULL;
119         gchar **excludes = NULL;
120         GSList *list;
121         GDir *dir;
122         const gchar *file;
123         gchar *filename;
124         unsigned int i;
125
126         DBG("");
127
128         if (pattern)
129                 patterns = g_strsplit_set(pattern, ":, ", -1);
130
131         if (exclude)
132                 excludes = g_strsplit_set(exclude, ":, ", -1);
133
134         for (i = 0; __connman_builtin[i]; i++) {
135                 if (check_plugin(__connman_builtin[i],
136                                                 patterns, excludes) == FALSE)
137                         continue;
138
139                 add_plugin(NULL, __connman_builtin[i]);
140         }
141
142         dir = g_dir_open(PLUGINDIR, 0, NULL);
143         if (dir != NULL) {
144                 while ((file = g_dir_read_name(dir)) != NULL) {
145                         void *handle;
146                         struct connman_plugin_desc *desc;
147
148                         if (g_str_has_prefix(file, "lib") == TRUE ||
149                                         g_str_has_suffix(file, ".so") == FALSE)
150                                 continue;
151
152                         filename = g_build_filename(PLUGINDIR, file, NULL);
153
154                         handle = dlopen(filename, PLUGINFLAG);
155                         if (handle == NULL) {
156                                 connman_error("Can't load %s: %s",
157                                                         filename, dlerror());
158                                 g_free(filename);
159                                 continue;
160                         }
161
162                         g_free(filename);
163
164                         desc = dlsym(handle, "connman_plugin_desc");
165                         if (desc == NULL) {
166                                 connman_error("Can't load symbol: %s",
167                                                                 dlerror());
168                                 dlclose(handle);
169                                 continue;
170                         }
171
172                         if (check_plugin(desc, patterns, excludes) == FALSE) {
173                                 dlclose(handle);
174                                 continue;
175                         }
176
177                         if (add_plugin(handle, desc) == FALSE)
178                                 dlclose(handle);
179                 }
180
181                 g_dir_close(dir);
182         }
183
184         for (list = plugins; list; list = list->next) {
185                 struct connman_plugin *plugin = list->data;
186
187                 if (plugin->desc->init() < 0)
188                         continue;
189
190                 plugin->active = TRUE;
191         }
192
193         g_strfreev(patterns);
194         g_strfreev(excludes);
195
196         return 0;
197 }
198
199 void __connman_plugin_cleanup(void)
200 {
201         GSList *list;
202
203         DBG("");
204
205         for (list = plugins; list; list = list->next) {
206                 struct connman_plugin *plugin = list->data;
207
208                 if (plugin->active == TRUE && plugin->desc->exit)
209                         plugin->desc->exit();
210
211                 if (plugin->handle != NULL)
212                         dlclose(plugin->handle);
213
214                 g_free(plugin);
215         }
216
217         g_slist_free(plugins);
218 }