sms: Use __ofono_atom_find
[platform/upstream/ofono.git] / src / plugin.c
1 /*
2  *
3  *  oFono - Open Source Telephony
4  *
5  *  Copyright (C) 2008-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 #include "ofono.h"
31
32 static GSList *plugins = NULL;
33
34 struct ofono_plugin {
35         void *handle;
36         gboolean active;
37         struct ofono_plugin_desc *desc;
38 };
39
40 static gint compare_priority(gconstpointer a, gconstpointer b)
41 {
42         const struct ofono_plugin *plugin1 = a;
43         const struct ofono_plugin *plugin2 = b;
44
45         return plugin2->desc->priority - plugin1->desc->priority;
46 }
47
48 static gboolean add_plugin(void *handle, struct ofono_plugin_desc *desc)
49 {
50         struct ofono_plugin *plugin;
51
52         if (desc->init == NULL)
53                 return FALSE;
54
55         if (g_str_equal(desc->version, OFONO_VERSION) == FALSE) {
56                 ofono_error("Invalid version %s for %s", desc->version,
57                                                         desc->description);
58                 return FALSE;
59         }
60
61         plugin = g_try_new0(struct ofono_plugin, 1);
62         if (plugin == NULL)
63                 return FALSE;
64
65         plugin->handle = handle;
66         plugin->active = FALSE;
67         plugin->desc = desc;
68
69         __ofono_log_enable(desc->debug_start, desc->debug_stop);
70
71         plugins = g_slist_insert_sorted(plugins, plugin, compare_priority);
72
73         return TRUE;
74 }
75
76 static gboolean check_plugin(struct ofono_plugin_desc *desc,
77                                 char **patterns, char **excludes)
78 {
79         if (excludes) {
80                 for (; *excludes; excludes++)
81                         if (g_pattern_match_simple(*excludes, desc->name))
82                                 break;
83                 if (*excludes) {
84                         ofono_info("Excluding %s", desc->description);
85                         return FALSE;
86                 }
87         }
88
89         if (patterns) {
90                 for (; *patterns; patterns++)
91                         if (g_pattern_match_simple(*patterns, desc->name))
92                                 break;
93                 if (*patterns == NULL) {
94                         ofono_info("Ignoring %s", desc->description);
95                         return FALSE;
96                 }
97         }
98
99         return TRUE;
100 }
101
102 #include "builtin.h"
103
104 int __ofono_plugin_init(const char *pattern, const char *exclude)
105 {
106         gchar **patterns = NULL;
107         gchar **excludes = NULL;
108         GSList *list;
109         GDir *dir;
110         const gchar *file;
111         gchar *filename;
112         unsigned int i;
113
114         DBG("");
115
116         if (pattern)
117                 patterns = g_strsplit_set(pattern, ":, ", -1);
118
119         if (exclude)
120                 excludes = g_strsplit_set(exclude, ":, ", -1);
121
122         for (i = 0; __ofono_builtin[i]; i++) {
123                 if (check_plugin(__ofono_builtin[i],
124                                         patterns, excludes) == FALSE)
125                         continue;
126
127                 add_plugin(NULL, __ofono_builtin[i]);
128         }
129
130         dir = g_dir_open(PLUGINDIR, 0, NULL);
131         if (dir != NULL) {
132                 while ((file = g_dir_read_name(dir)) != NULL) {
133                         void *handle;
134                         struct ofono_plugin_desc *desc;
135
136                         if (g_str_has_prefix(file, "lib") == TRUE ||
137                                         g_str_has_suffix(file, ".so") == FALSE)
138                                 continue;
139
140                         filename = g_build_filename(PLUGINDIR, file, NULL);
141
142                         handle = dlopen(filename, RTLD_NOW);
143                         if (handle == NULL) {
144                                 ofono_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, "ofono_plugin_desc");
153                         if (desc == NULL) {
154                                 ofono_error("Can't load symbol: %s",
155                                                                 dlerror());
156                                 dlclose(handle);
157                                 continue;
158                         }
159
160                         if (check_plugin(desc, patterns, excludes) == FALSE) {
161                                 dlclose(handle);
162                                 continue;
163                         }
164
165                         if (add_plugin(handle, desc) == FALSE)
166                                 dlclose(handle);
167                 }
168
169                 g_dir_close(dir);
170         }
171
172         for (list = plugins; list; list = list->next) {
173                 struct ofono_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 __ofono_plugin_cleanup(void)
188 {
189         GSList *list;
190
191         DBG("");
192
193         for (list = plugins; list; list = list->next) {
194                 struct ofono_plugin *plugin = list->data;
195
196                 if (plugin->active == TRUE && 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 }