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