Add requirement of python-xml for test subpackage
[profile/ivi/neard.git] / src / plugin.c
1 /*
2  *
3  *  neard - Near Field Communication manager
4  *
5  *  Copyright (C) 2011  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 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29
30 #include <dlfcn.h>
31
32 #include <glib.h>
33
34 #ifdef NEAR_PLUGIN_BUILTIN
35 #undef NEAR_PLUGIN_BUILTIN
36 #endif
37
38 #include "near.h"
39
40 static GSList *plugins = NULL;
41
42 struct near_plugin {
43         void *handle;
44         gboolean active;
45         struct near_plugin_desc *desc;
46 };
47
48 static gint compare_priority(gconstpointer a, gconstpointer b)
49 {
50         const struct near_plugin *plugin1 = a;
51         const struct near_plugin *plugin2 = b;
52
53         return plugin2->desc->priority - plugin1->desc->priority;
54 }
55
56 static gboolean add_plugin(void *handle, struct near_plugin_desc *desc)
57 {
58         struct near_plugin *plugin;
59
60         if (desc->init == NULL)
61                 return FALSE;
62
63         if (g_str_equal(desc->version, NEAR_VERSION) == FALSE) {
64                 near_error("Version mismatch for %s", desc->description);
65                 return FALSE;
66         }
67
68         plugin = g_try_new0(struct near_plugin, 1);
69         if (plugin == NULL)
70                 return FALSE;
71
72         plugin->handle = handle;
73         plugin->active = FALSE;
74         plugin->desc = desc;
75
76         plugins = g_slist_insert_sorted(plugins, plugin, compare_priority);
77
78         return TRUE;
79 }
80
81 static gboolean check_plugin(struct near_plugin_desc *desc,
82                                 char **patterns, char **excludes)
83 {
84         if (excludes) {
85                 for (; *excludes; excludes++)
86                         if (g_pattern_match_simple(*excludes, desc->name))
87                                 break;
88                 if (*excludes) {
89                         near_info("Excluding %s", desc->description);
90                         return FALSE;
91                 }
92         }
93
94         if (patterns) {
95                 for (; *patterns; patterns++)
96                         if (g_pattern_match_simple(*patterns, desc->name))
97                                 break;
98                 if (!*patterns) {
99                         near_info("Ignoring %s", desc->description);
100                         return FALSE;
101                 }
102         }
103
104         return TRUE;
105 }
106
107 #include "builtin.h"
108
109 int __near_plugin_init(const char *pattern, const char *exclude)
110 {
111         gchar **patterns = NULL;
112         gchar **excludes = NULL;
113         GSList *list;
114         GDir *dir;
115         const gchar *file;
116         gchar *filename;
117         unsigned int i;
118
119         DBG("");
120
121         if (pattern)
122                 patterns = g_strsplit_set(pattern, ":, ", -1);
123
124         if (exclude)
125                 excludes = g_strsplit_set(exclude, ":, ", -1);
126
127         for (i = 0; __near_builtin[i]; i++) {
128                 if (check_plugin(__near_builtin[i],
129                                                 patterns, excludes) == FALSE)
130                         continue;
131
132                 add_plugin(NULL, __near_builtin[i]);
133         }
134
135         dir = g_dir_open(PLUGINDIR, 0, NULL);
136         if (dir != NULL) {
137                 while ((file = g_dir_read_name(dir)) != NULL) {
138                         void *handle;
139                         struct near_plugin_desc *desc;
140
141                         if (g_str_has_prefix(file, "lib") == TRUE ||
142                                         g_str_has_suffix(file, ".so") == FALSE)
143                                 continue;
144
145                         filename = g_build_filename(PLUGINDIR, file, NULL);
146
147                         handle = dlopen(filename, RTLD_NOW);
148                         if (handle == NULL) {
149                                 near_error("Can't load %s: %s",
150                                                         filename, dlerror());
151                                 g_free(filename);
152                                 continue;
153                         }
154
155                         g_free(filename);
156
157                         desc = dlsym(handle, "near_plugin_desc");
158                         if (desc == NULL) {
159                                 near_error("Can't load symbol: %s",
160                                                                 dlerror());
161                                 dlclose(handle);
162                                 continue;
163                         }
164
165                         if (check_plugin(desc, patterns, excludes) == FALSE) {
166                                 dlclose(handle);
167                                 continue;
168                         }
169
170                         if (add_plugin(handle, desc) == FALSE)
171                                 dlclose(handle);
172                 }
173
174                 g_dir_close(dir);
175         }
176
177         for (list = plugins; list; list = list->next) {
178                 struct near_plugin *plugin = list->data;
179
180                 if (plugin->desc->init() < 0)
181                         continue;
182
183                 plugin->active = TRUE;
184         }
185
186         g_strfreev(patterns);
187         g_strfreev(excludes);
188
189         return 0;
190 }
191
192 void __near_plugin_cleanup(void)
193 {
194         GSList *list;
195
196         DBG("");
197
198         for (list = plugins; list; list = list->next) {
199                 struct near_plugin *plugin = list->data;
200
201                 if (plugin->active == TRUE && plugin->desc->exit)
202                         plugin->desc->exit();
203
204                 if (plugin->handle != NULL)
205                         dlclose(plugin->handle);
206
207                 g_free(plugin);
208         }
209
210         g_slist_free(plugins);
211 }