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