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