Diff between 2d9cd8eee60fa3c44ca76ebcfaef47b31a6db5df and f5206387179b8ace65323a5007922c78bb5b71e9

Changed Files

File Additions Deletions Status
obexd/src/main.c +7 -2 modified
obexd/src/obexd.h +3 -0 modified
obexd/src/plugin.c +152 -0 added
obexd/src/plugin.h +33 -0 added

Full Patch

diff --git a/obexd/src/main.c b/obexd/src/main.c
index 4696dad..c28e470 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 7baac91..7ce738e 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 <dbus/dbus.h>
 
 #define OPENOBEX_SERVICE  "org.openobex"
diff --git a/obexd/src/plugin.c b/obexd/src/plugin.c
new file mode 100644
index 0000000..d0dc5b1
--- /dev/null
+++ b/obexd/src/plugin.c
@@ -0,0 +1,152 @@
+/*
+ *
+ *  OBEX Server
+ *
+ *  Copyright (C) 2007-2008  Marcel Holtmann <marcel@holtmann.org>
+ *
+ *
+ *  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 <config.h>
+#endif
+
+#include <glib.h>
+#include <gmodule.h>
+#include <string.h>
+
+#include <sys/stat.h>
+#include <errno.h>
+
+#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 0000000..29ec7f7
--- /dev/null
+++ b/obexd/src/plugin.h
@@ -0,0 +1,33 @@
+/*
+ *
+ *  OBEX Server
+ *
+ *  Copyright (C) 2007-2008  Marcel Holtmann <marcel@holtmann.org>
+ *
+ *
+ *  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 \
+		};