From f5206387179b8ace65323a5007922c78bb5b71e9 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Fri, 5 Sep 2008 21:21:02 +0200 Subject: [PATCH] obexd: Add plugin infrastructure --- obexd/src/main.c | 9 ++- obexd/src/obexd.h | 3 + obexd/src/plugin.c | 152 +++++++++++++++++++++++++++++++++++++++++++++ obexd/src/plugin.h | 33 ++++++++++ 4 files changed, 195 insertions(+), 2 deletions(-) create mode 100644 obexd/src/plugin.c create mode 100644 obexd/src/plugin.h diff --git a/obexd/src/main.c b/obexd/src/main.c index 4696dad32..c28e470b1 100644 --- a/obexd/src/main.c +++ b/obexd/src/main.c @@ -250,16 +250,19 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } + plugin_init(); + if (opush) server_start(OBEX_OPUSH, root_path, auto_accept, - NULL, devnode); + NULL, devnode); if (ftp) server_start(OBEX_FTP, root_path, auto_accept, - capability, devnode); + capability, devnode); if (!manager_init(conn)) { error("manager_init failed"); + plugin_cleanup(); exit(EXIT_FAILURE); } @@ -272,6 +275,8 @@ int main(int argc, char *argv[]) manager_cleanup(); + plugin_cleanup(); + server_stop(); dbus_connection_unref(conn); diff --git a/obexd/src/obexd.h b/obexd/src/obexd.h index 7baac9167..7ce738e13 100644 --- a/obexd/src/obexd.h +++ b/obexd/src/obexd.h @@ -26,6 +26,9 @@ #define DBG(fmt, arg...) printf("%s: " fmt "\n" , __FUNCTION__ , ## arg) //#define DBG(fmt, arg...) +gboolean plugin_init(void); +void plugin_cleanup(void); + #include #define OPENOBEX_SERVICE "org.openobex" diff --git a/obexd/src/plugin.c b/obexd/src/plugin.c new file mode 100644 index 000000000..d0dc5b16a --- /dev/null +++ b/obexd/src/plugin.c @@ -0,0 +1,152 @@ +/* + * + * OBEX Server + * + * Copyright (C) 2007-2008 Marcel Holtmann + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include + +#include +#include + +#include "logging.h" + +#include "plugin.h" + +static GSList *plugins = NULL; + +struct obex_plugin { + GModule *module; + struct obex_plugin_desc *desc; +}; + +static gboolean add_plugin(GModule *module, struct obex_plugin_desc *desc) +{ + struct obex_plugin *plugin; + + if (desc->init() < 0) + return FALSE; + + plugin = g_try_new0(struct obex_plugin, 1); + if (plugin == NULL) + return FALSE; + + plugin->module = module; + plugin->desc = desc; + + plugins = g_slist_append(plugins, plugin); + + return TRUE; +} + +gboolean plugin_init(void) +{ + GDir *dir; + const gchar *file; + + if (strlen(PLUGINDIR) == 0) + return FALSE; + + debug("Loading plugins %s", PLUGINDIR); + + dir = g_dir_open(PLUGINDIR, 0, NULL); + if (!dir) + return FALSE; + + while ((file = g_dir_read_name(dir)) != NULL) { + GModule *module; + struct obex_plugin_desc *desc; + gchar *filename; + struct stat st; + + if (g_str_has_prefix(file, "lib") == TRUE || + g_str_has_suffix(file, ".so") == FALSE) + continue; + + filename = g_build_filename(PLUGINDIR, file, NULL); + + if (stat(filename, &st) < 0) { + error("Can't load plugin %s: %s (%d)", filename, + strerror(errno), errno); + g_free(filename); + continue; + } + + module = g_module_open(filename, G_MODULE_BIND_LOCAL); + if (module == NULL) { + error("Can't load plugin: %s", g_module_error()); + g_free(filename); + continue; + } + + g_free(filename); + + debug("%s", g_module_name(module)); + + if (g_module_symbol(module, "obex_plugin_desc", + (gpointer) &desc) == FALSE) { + error("Can't load plugin description"); + g_module_close(module); + continue; + } + + if (desc == NULL || desc->init == NULL) { + g_module_close(module); + continue; + } + + if (add_plugin(module, desc) == FALSE) { + error("Can't init plugin %s", g_module_name(module)); + g_module_close(module); + } + } + + g_dir_close(dir); + + return TRUE; +} + +void plugin_cleanup(void) +{ + GSList *list; + + debug("Cleanup plugins"); + + for (list = plugins; list; list = list->next) { + struct obex_plugin *plugin = list->data; + + debug("%s", g_module_name(plugin->module)); + + if (plugin->desc->exit) + plugin->desc->exit(); + + g_module_close(plugin->module); + + g_free(plugin); + } + + g_slist_free(plugins); +} diff --git a/obexd/src/plugin.h b/obexd/src/plugin.h new file mode 100644 index 000000000..29ec7f7fa --- /dev/null +++ b/obexd/src/plugin.h @@ -0,0 +1,33 @@ +/* + * + * OBEX Server + * + * Copyright (C) 2007-2008 Marcel Holtmann + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +struct obex_plugin_desc { + const char *name; + int (*init) (void); + void (*exit) (void); +}; + +#define OBEX_PLUGIN_DEFINE(name,init,exit) \ + struct obex_plugin_desc obex_plugin_desc = { \ + name, init, exit \ + }; -- 2.47.3