Imported Upstream version 1.24
[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 static GSList *plugins = NULL;
37
38 struct connman_plugin {
39         void *handle;
40         bool 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 bool add_plugin(void *handle, struct connman_plugin_desc *desc)
53 {
54         struct connman_plugin *plugin;
55
56         if (!desc->init)
57                 return false;
58
59         if (!g_str_equal(desc->version, CONNMAN_VERSION)) {
60                 connman_error("Invalid version %s for %s", desc->version,
61                                                         desc->description);
62                 return false;
63         }
64
65         plugin = g_try_new0(struct connman_plugin, 1);
66         if (!plugin)
67                 return false;
68
69         plugin->handle = handle;
70         plugin->active = false;
71         plugin->desc = desc;
72
73         __connman_log_enable(desc->debug_start, desc->debug_stop);
74
75         plugins = g_slist_insert_sorted(plugins, plugin, compare_priority);
76
77         return true;
78 }
79
80 static bool check_plugin(struct connman_plugin_desc *desc,
81                                 char **patterns, char **excludes)
82 {
83         if (excludes) {
84                 for (; *excludes; excludes++)
85                         if (g_pattern_match_simple(*excludes, desc->name))
86                                 break;
87                 if (*excludes) {
88                         connman_info("Excluding %s", desc->description);
89                         return false;
90                 }
91         }
92
93         if (patterns) {
94                 for (; *patterns; patterns++)
95                         if (g_pattern_match_simple(*patterns, desc->name))
96                                 break;
97                 if (!*patterns) {
98                         connman_info("Ignoring %s", desc->description);
99                         return false;
100                 }
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         gchar **patterns = NULL;
111         gchar **excludes = NULL;
112         GSList *list;
113         GDir *dir;
114         const gchar *file;
115         gchar *filename;
116         unsigned int i;
117
118         DBG("");
119
120         if (pattern)
121                 patterns = g_strsplit_set(pattern, ":, ", -1);
122
123         if (exclude)
124                 excludes = g_strsplit_set(exclude, ":, ", -1);
125
126         for (i = 0; __connman_builtin[i]; i++) {
127                 if (!check_plugin(__connman_builtin[i], patterns, excludes))
128                         continue;
129
130                 add_plugin(NULL, __connman_builtin[i]);
131         }
132
133         dir = g_dir_open(PLUGINDIR, 0, NULL);
134         if (dir) {
135                 while ((file = g_dir_read_name(dir))) {
136                         void *handle;
137                         struct connman_plugin_desc *desc;
138
139                         if (g_str_has_prefix(file, "lib") ||
140                                         !g_str_has_suffix(file, ".so"))
141                                 continue;
142
143                         filename = g_build_filename(PLUGINDIR, file, NULL);
144
145                         handle = dlopen(filename, RTLD_NOW);
146                         if (!handle) {
147                                 connman_error("Can't load %s: %s",
148                                                         filename, dlerror());
149                                 g_free(filename);
150                                 continue;
151                         }
152
153                         g_free(filename);
154
155                         desc = dlsym(handle, "connman_plugin_desc");
156                         if (!desc) {
157                                 connman_error("Can't load symbol: %s",
158                                                                 dlerror());
159                                 dlclose(handle);
160                                 continue;
161                         }
162
163                         if (!check_plugin(desc, patterns, excludes)) {
164                                 dlclose(handle);
165                                 continue;
166                         }
167
168                         if (!add_plugin(handle, desc))
169                                 dlclose(handle);
170                 }
171
172                 g_dir_close(dir);
173         }
174
175         for (list = plugins; list; list = list->next) {
176                 struct connman_plugin *plugin = list->data;
177
178                 if (plugin->desc->init() < 0)
179                         continue;
180
181                 plugin->active = true;
182         }
183
184         g_strfreev(patterns);
185         g_strfreev(excludes);
186
187         return 0;
188 }
189
190 void __connman_plugin_cleanup(void)
191 {
192         GSList *list;
193
194         DBG("");
195
196         for (list = plugins; list; list = list->next) {
197                 struct connman_plugin *plugin = list->data;
198
199                 if (plugin->active && plugin->desc->exit)
200                         plugin->desc->exit();
201
202                 if (plugin->handle)
203                         dlclose(plugin->handle);
204
205                 g_free(plugin);
206         }
207
208         g_slist_free(plugins);
209 }