From 97feb3635e571746b1957508576be9d68efdacd9 Mon Sep 17 00:00:00 2001 From: Jan Vidar Krey Date: Thu, 29 Jul 2010 08:41:26 +0200 Subject: [PATCH] Allow plugins to provide an error message. Useful for reporting problems when registering the plugins. --- src/core/hub.c | 18 ++++-------------- src/core/pluginloader.c | 22 ++++++++++++++++++++-- src/core/pluginloader.h | 2 +- src/plugins/mod_auth_simple.c | 30 ++++++++++++++++++++---------- 4 files changed, 45 insertions(+), 27 deletions(-) diff --git a/src/core/hub.c b/src/core/hub.c index 3560125..5a15a35 100644 --- a/src/core/hub.c +++ b/src/core/hub.c @@ -826,22 +826,12 @@ void hub_plugins_load(struct hub_info* hub) void hub_plugins_unload(struct hub_info* hub) { - if (!hub->plugins || !hub->plugins->loaded) + if (hub->plugins) { - return; + plugin_shutdown(hub->plugins); + hub_free(hub->plugins); + hub->plugins = 0; } - - struct uhub_plugin_handle* plugin = (struct uhub_plugin_handle*) list_get_first(hub->plugins->loaded); - while (plugin) - { - plugin_unload(plugin); - plugin = (struct uhub_plugin_handle*) list_get_next(hub->plugins->loaded); - } - - list_destroy(hub->plugins->loaded); - hub_free(hub->plugins->plugin_dir); - hub_free(hub->plugins); - hub->plugins = 0; } #endif diff --git a/src/core/pluginloader.c b/src/core/pluginloader.c index 9d3e516..5af3e57 100644 --- a/src/core/pluginloader.c +++ b/src/core/pluginloader.c @@ -105,7 +105,7 @@ struct uhub_plugin_handle* plugin_load(const char* filename, const char* config) } else { - LOG_ERROR("Unable to load plugin: %s - Failed to initialize", filename); + LOG_ERROR("Unable to load plugin: %s - Failed to initialize: %s", filename, handle->error_msg); } } @@ -127,10 +127,16 @@ static int plugin_parse_line(char* line, int line_count, void* ptr_data) char *directive, *soname, *params; if (cfg_token_count(tokens) == 0) + { + cfg_tokens_free(tokens); return 0; + } if (cfg_token_count(tokens) < 2) + { + cfg_tokens_free(tokens); return -1; + } directive = cfg_token_get_first(tokens); soname = cfg_token_get_next(tokens); @@ -141,12 +147,12 @@ static int plugin_parse_line(char* line, int line_count, void* ptr_data) if (!params) params = ""; - LOG_TRACE("Load plugin: \"%s\", params=\"%s\"", soname, params); struct uhub_plugin_handle* plugin = plugin_load(soname, params); if (plugin) { list_append(handle->loaded, plugin); + cfg_tokens_free(tokens); return 0; } } @@ -175,5 +181,17 @@ int plugin_initialize(struct hub_config* config, struct uhub_plugins* handle) return 0; } +void plugin_shutdown(struct uhub_plugins* handle) +{ + struct uhub_plugin_handle* plugin = (struct uhub_plugin_handle*) list_get_first(handle->loaded); + while (plugin) + { + list_remove(handle->loaded, plugin); + plugin_unload(plugin); + plugin = (struct uhub_plugin_handle*) list_get_first(handle->loaded); + } + + list_destroy(handle->loaded); +} #endif /* PLUGIN_SUPPORT */ diff --git a/src/core/pluginloader.h b/src/core/pluginloader.h index 719f51c..e5d6bf5 100644 --- a/src/core/pluginloader.h +++ b/src/core/pluginloader.h @@ -39,7 +39,6 @@ struct uhub_plugin struct uhub_plugins { struct linked_list* loaded; - char* plugin_dir; }; // High level plugin loader ode @@ -48,6 +47,7 @@ extern void plugin_unload(struct uhub_plugin_handle* plugin); // extern void plugin_unload(struct uhub_plugin_handle*); extern int plugin_initialize(struct hub_config* config, struct uhub_plugins* handle); +extern void plugin_shutdown(struct uhub_plugins* handle); // Low level plugin loader code (used internally) extern struct uhub_plugin* plugin_open(const char* filename); diff --git a/src/plugins/mod_auth_simple.c b/src/plugins/mod_auth_simple.c index 1ba57d1..8fe3eee 100644 --- a/src/plugins/mod_auth_simple.c +++ b/src/plugins/mod_auth_simple.c @@ -11,6 +11,11 @@ #include "util/log.h" #include "util/config_token.h" +static void set_error_message(struct uhub_plugin_handle* plugin, const char* msg) +{ + plugin->error_msg = msg; +} + struct acl_list { struct linked_list* users; @@ -59,10 +64,19 @@ static struct acl_list* load_acl(const char* filename, struct uhub_plugin_handle struct acl_list* list = (struct acl_list*) hub_malloc(sizeof(struct acl_list)); struct linked_list* users = list_create(); - if (!list || !users || !filename || !*filename) + if (!list || !users) { list_destroy(users); hub_free(list); + set_error_message(handle, "Unable to allocate memory"); + return 0; + } + + if (!filename || !*filename) + { + list_destroy(users); + hub_free(list); + set_error_message(handle, "No configuration file given"); return 0; } @@ -71,6 +85,7 @@ static struct acl_list* load_acl(const char* filename, struct uhub_plugin_handle if (file_read_lines(filename, users, &parse_line) == -1) { fprintf(stderr, "Unable to load %s\n", filename); + set_error_message(handle, "Unable to load file"); } } @@ -111,13 +126,6 @@ static plugin_st delete_user(struct auth_info* user) return st_deny; } -static void set_error_message(struct uhub_plugin_handle* plugin, const char* msg) -{ - // if (plugin->error_msg) - // hub_free(plugin->error_msg); - plugin->error_msg = msg; -} - int plugin_register(struct uhub_plugin_handle* plugin, const char* config) { plugin->name = "File authentication plugin"; @@ -133,8 +141,10 @@ int plugin_register(struct uhub_plugin_handle* plugin, const char* config) plugin->funcs.auth_update_user = update_user; plugin->funcs.auth_delete_user = delete_user; - plugin->ptr = load_acl(config, plugin); - return 0; + plugin->ptr = load_acl(config, plugin); + if (plugin->ptr) + return 0; + return -1; } int plugin_unregister(struct uhub_plugin_handle* plugin)