5 * Copyright (C) 2007-2010 Marcel Holtmann <marcel@holtmann.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
40 * Plugins that are using libraries with threads and their own mainloop
41 * will crash on exit. This is a bug inside these libraries, but there is
42 * nothing much that can be done about it. One bad example is libebook.
45 #define PLUGINFLAG (RTLD_NOW | RTLD_NODELETE)
47 #define PLUGINFLAG (RTLD_NOW)
50 static GSList *plugins = NULL;
54 struct obex_plugin_desc *desc;
57 static gboolean add_plugin(void *handle, struct obex_plugin_desc *desc)
59 struct obex_plugin *plugin;
61 if (desc->init == NULL)
64 plugin = g_try_new0(struct obex_plugin, 1);
68 plugin->handle = handle;
71 if (desc->init() < 0) {
76 plugins = g_slist_append(plugins, plugin);
77 DBG("Plugin %s loaded", desc->name);
82 static gboolean check_plugin(struct obex_plugin_desc *desc,
83 char **patterns, char **excludes)
86 for (; *excludes; excludes++)
87 if (g_pattern_match_simple(*excludes, desc->name))
90 info("Excluding %s", desc->name);
96 for (; *patterns; patterns++)
97 if (g_pattern_match_simple(*patterns, desc->name))
99 if (*patterns == NULL) {
100 info("Ignoring %s", desc->name);
111 gboolean plugin_init(const char *pattern, const char *exclude)
113 char **patterns = NULL;
114 char **excludes = NULL;
119 if (strlen(PLUGINDIR) == 0)
123 patterns = g_strsplit_set(pattern, ":, ", -1);
126 excludes = g_strsplit_set(exclude, ":, ", -1);
128 DBG("Loading builtin plugins");
130 for (i = 0; __obex_builtin[i]; i++) {
131 if (check_plugin(__obex_builtin[i],
132 patterns, excludes) == FALSE)
135 add_plugin(NULL, __obex_builtin[i]);
138 DBG("Loading plugins %s", PLUGINDIR);
140 dir = g_dir_open(PLUGINDIR, 0, NULL);
144 while ((file = g_dir_read_name(dir)) != NULL) {
145 struct obex_plugin_desc *desc;
149 if (g_str_has_prefix(file, "lib") == TRUE ||
150 g_str_has_suffix(file, ".so") == FALSE)
153 filename = g_build_filename(PLUGINDIR, file, NULL);
155 handle = dlopen(filename, PLUGINFLAG);
156 if (handle == NULL) {
157 error("Can't load plugin %s: %s", filename,
165 desc = dlsym(handle, "obex_plugin_desc");
167 error("Can't load plugin description: %s", dlerror());
172 if (check_plugin(desc, patterns, excludes) == FALSE) {
177 if (add_plugin(handle, desc) == FALSE)
182 g_strfreev(patterns);
183 g_strfreev(excludes);
188 void plugin_cleanup(void)
192 DBG("Cleanup plugins");
194 for (list = plugins; list; list = list->next) {
195 struct obex_plugin *plugin = list->data;
197 if (plugin->desc->exit)
198 plugin->desc->exit();
200 if (plugin->handle != NULL)
201 dlclose(plugin->handle);
206 g_slist_free(plugins);