Allow plugins to provide an error message. Useful for reporting problems

when registering the plugins.
This commit is contained in:
Jan Vidar Krey 2010-07-29 08:41:26 +02:00
parent 920d696ff5
commit 97feb3635e
4 changed files with 45 additions and 27 deletions

View File

@ -826,22 +826,12 @@ void hub_plugins_load(struct hub_info* hub)
void hub_plugins_unload(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);
}
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_free(hub->plugins);
hub->plugins = 0; hub->plugins = 0;
}
} }
#endif #endif

View File

@ -105,7 +105,7 @@ struct uhub_plugin_handle* plugin_load(const char* filename, const char* config)
} }
else 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; char *directive, *soname, *params;
if (cfg_token_count(tokens) == 0) if (cfg_token_count(tokens) == 0)
{
cfg_tokens_free(tokens);
return 0; return 0;
}
if (cfg_token_count(tokens) < 2) if (cfg_token_count(tokens) < 2)
{
cfg_tokens_free(tokens);
return -1; return -1;
}
directive = cfg_token_get_first(tokens); directive = cfg_token_get_first(tokens);
soname = cfg_token_get_next(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) if (!params)
params = ""; params = "";
LOG_TRACE("Load plugin: \"%s\", params=\"%s\"", soname, params); LOG_TRACE("Load plugin: \"%s\", params=\"%s\"", soname, params);
struct uhub_plugin_handle* plugin = plugin_load(soname, params); struct uhub_plugin_handle* plugin = plugin_load(soname, params);
if (plugin) if (plugin)
{ {
list_append(handle->loaded, plugin); list_append(handle->loaded, plugin);
cfg_tokens_free(tokens);
return 0; return 0;
} }
} }
@ -175,5 +181,17 @@ int plugin_initialize(struct hub_config* config, struct uhub_plugins* handle)
return 0; 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 */ #endif /* PLUGIN_SUPPORT */

View File

@ -39,7 +39,6 @@ struct uhub_plugin
struct uhub_plugins struct uhub_plugins
{ {
struct linked_list* loaded; struct linked_list* loaded;
char* plugin_dir;
}; };
// High level plugin loader ode // 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 void plugin_unload(struct uhub_plugin_handle*);
extern int plugin_initialize(struct hub_config* config, struct uhub_plugins* 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) // Low level plugin loader code (used internally)
extern struct uhub_plugin* plugin_open(const char* filename); extern struct uhub_plugin* plugin_open(const char* filename);

View File

@ -11,6 +11,11 @@
#include "util/log.h" #include "util/log.h"
#include "util/config_token.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 acl_list
{ {
struct linked_list* users; 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 acl_list* list = (struct acl_list*) hub_malloc(sizeof(struct acl_list));
struct linked_list* users = list_create(); struct linked_list* users = list_create();
if (!list || !users || !filename || !*filename) if (!list || !users)
{ {
list_destroy(users); list_destroy(users);
hub_free(list); 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; 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) if (file_read_lines(filename, users, &parse_line) == -1)
{ {
fprintf(stderr, "Unable to load %s\n", filename); 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; 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) int plugin_register(struct uhub_plugin_handle* plugin, const char* config)
{ {
plugin->name = "File authentication plugin"; plugin->name = "File authentication plugin";
@ -134,7 +142,9 @@ int plugin_register(struct uhub_plugin_handle* plugin, const char* config)
plugin->funcs.auth_delete_user = delete_user; plugin->funcs.auth_delete_user = delete_user;
plugin->ptr = load_acl(config, plugin); plugin->ptr = load_acl(config, plugin);
if (plugin->ptr)
return 0; return 0;
return -1;
} }
int plugin_unregister(struct uhub_plugin_handle* plugin) int plugin_unregister(struct uhub_plugin_handle* plugin)