2010-05-30 21:33:06 +00:00
|
|
|
/*
|
|
|
|
* uhub - A tiny ADC p2p connection hub
|
2011-12-09 09:29:50 +00:00
|
|
|
* Copyright (C) 2007-2011, Jan Vidar Krey
|
2010-05-30 21:33:06 +00:00
|
|
|
*
|
|
|
|
* 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 3 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "uhub.h"
|
|
|
|
|
2010-06-06 14:15:15 +00:00
|
|
|
#include "plugin_api/handle.h"
|
2010-05-30 21:33:06 +00:00
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
struct plugin_callback_data;
|
|
|
|
|
2012-04-21 07:22:06 +00:00
|
|
|
struct plugin_hub_internals* get_internals(struct plugin_handle* handle)
|
2011-12-09 09:29:50 +00:00
|
|
|
{
|
|
|
|
struct plugin_hub_internals* internals;
|
2012-05-02 19:06:46 +00:00
|
|
|
uhub_assert(handle && handle->handle && handle->handle->internals);
|
2011-12-09 09:29:50 +00:00
|
|
|
internals = (struct plugin_hub_internals*) handle->handle->internals;
|
|
|
|
return internals;
|
|
|
|
}
|
|
|
|
|
2010-06-06 14:15:15 +00:00
|
|
|
struct uhub_plugin* plugin_open(const char* filename)
|
2010-05-30 21:33:06 +00:00
|
|
|
{
|
2011-09-06 22:31:38 +00:00
|
|
|
struct uhub_plugin* plugin;
|
2011-12-09 15:36:14 +00:00
|
|
|
LOG_PLUGIN("plugin_open: \"%s\"", filename);
|
2011-09-06 22:31:38 +00:00
|
|
|
|
|
|
|
plugin = (struct uhub_plugin*) hub_malloc_zero(sizeof(struct uhub_plugin));
|
2010-05-30 21:33:06 +00:00
|
|
|
if (!plugin)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-09-06 22:31:38 +00:00
|
|
|
#ifdef HAVE_DLOPEN
|
2010-05-30 21:33:06 +00:00
|
|
|
plugin->handle = dlopen(filename, RTLD_LAZY);
|
2011-09-06 22:31:38 +00:00
|
|
|
#else
|
|
|
|
plugin->handle = LoadLibraryExA(filename, NULL, 0);
|
|
|
|
#endif
|
2010-05-30 21:33:06 +00:00
|
|
|
|
|
|
|
if (!plugin->handle)
|
|
|
|
{
|
2011-09-06 22:31:38 +00:00
|
|
|
#ifdef HAVE_DLOPEN
|
2010-05-30 21:33:06 +00:00
|
|
|
LOG_ERROR("Unable to open plugin %s: %s", filename, dlerror());
|
2011-09-06 22:31:38 +00:00
|
|
|
#else
|
2011-09-07 23:00:32 +00:00
|
|
|
LOG_ERROR("Unable to open plugin %s: %d", filename, GetLastError());
|
2011-09-06 22:31:38 +00:00
|
|
|
#endif
|
2010-05-30 21:33:06 +00:00
|
|
|
hub_free(plugin);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-01 12:37:07 +00:00
|
|
|
plugin->filename = strdup(filename);
|
2011-12-09 09:29:50 +00:00
|
|
|
plugin->internals = hub_malloc_zero(sizeof(struct plugin_hub_internals));
|
2010-05-30 21:33:06 +00:00
|
|
|
return plugin;
|
|
|
|
}
|
|
|
|
|
2010-06-06 14:15:15 +00:00
|
|
|
void plugin_close(struct uhub_plugin* plugin)
|
2010-05-30 21:33:06 +00:00
|
|
|
{
|
2011-12-09 15:39:19 +00:00
|
|
|
struct plugin_hub_internals* internals = (struct plugin_hub_internals*) plugin->internals;
|
|
|
|
|
2011-12-09 15:36:14 +00:00
|
|
|
LOG_PLUGIN("plugin_close: \"%s\"", plugin->filename);
|
2012-03-20 21:29:15 +00:00
|
|
|
plugin_callback_data_destroy(plugin->handle, internals->callback_data);
|
2011-12-09 15:39:19 +00:00
|
|
|
hub_free(internals);
|
2012-01-19 01:58:20 +00:00
|
|
|
plugin->internals = NULL;
|
2011-12-09 15:36:14 +00:00
|
|
|
|
2010-05-30 21:33:06 +00:00
|
|
|
#ifdef HAVE_DLOPEN
|
|
|
|
dlclose(plugin->handle);
|
2011-09-06 22:31:38 +00:00
|
|
|
#else
|
|
|
|
FreeLibrary((HMODULE) plugin->handle);
|
2010-05-30 21:33:06 +00:00
|
|
|
#endif
|
2011-12-01 12:37:07 +00:00
|
|
|
hub_free(plugin->filename);
|
2011-09-06 22:31:38 +00:00
|
|
|
hub_free(plugin);
|
2010-05-30 21:33:06 +00:00
|
|
|
}
|
|
|
|
|
2010-06-06 14:15:15 +00:00
|
|
|
void* plugin_lookup_symbol(struct uhub_plugin* plugin, const char* symbol)
|
2010-05-30 21:33:06 +00:00
|
|
|
{
|
|
|
|
#ifdef HAVE_DLOPEN
|
|
|
|
void* addr = dlsym(plugin->handle, symbol);
|
|
|
|
return addr;
|
|
|
|
#else
|
2011-09-06 22:31:38 +00:00
|
|
|
FARPROC addr = GetProcAddress((HMODULE) plugin->handle, symbol);
|
|
|
|
return (void*) addr;
|
2010-05-30 21:33:06 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
struct plugin_handle* plugin_load(const char* filename, const char* config, struct hub_info* hub)
|
2010-06-06 14:15:15 +00:00
|
|
|
{
|
|
|
|
plugin_register_f register_f;
|
|
|
|
plugin_unregister_f unregister_f;
|
|
|
|
int ret;
|
2011-12-09 09:29:50 +00:00
|
|
|
struct plugin_handle* handle = (struct plugin_handle*) hub_malloc_zero(sizeof(struct plugin_handle));
|
2010-06-06 14:15:15 +00:00
|
|
|
struct uhub_plugin* plugin = plugin_open(filename);
|
2011-12-09 09:29:50 +00:00
|
|
|
struct plugin_hub_internals* internals = (struct plugin_hub_internals*) plugin->internals;
|
2010-06-06 14:15:15 +00:00
|
|
|
|
|
|
|
if (!plugin)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!handle)
|
|
|
|
{
|
|
|
|
plugin_close(plugin);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
handle->handle = plugin;
|
|
|
|
register_f = plugin_lookup_symbol(plugin, "plugin_register");
|
|
|
|
unregister_f = plugin_lookup_symbol(plugin, "plugin_unregister");
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
// register hub internals
|
|
|
|
internals->unregister = unregister_f;
|
|
|
|
internals->hub = hub;
|
|
|
|
internals->callback_data = plugin_callback_data_create();
|
|
|
|
|
|
|
|
// setup callback functions, where the plugin can contact the hub.
|
|
|
|
plugin_register_callback_functions(handle);
|
|
|
|
|
2010-06-06 14:15:15 +00:00
|
|
|
if (register_f && unregister_f)
|
|
|
|
{
|
|
|
|
ret = register_f(handle, config);
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
if (handle->plugin_api_version == PLUGIN_API_VERSION && handle->plugin_funcs_size == sizeof(struct plugin_funcs))
|
|
|
|
{
|
2010-06-22 14:04:33 +00:00
|
|
|
LOG_INFO("Loaded plugin: %s: %s, version %s.", filename, handle->name, handle->version);
|
2011-12-09 15:36:14 +00:00
|
|
|
LOG_PLUGIN("Plugin API version: %d (func table size: " PRINTF_SIZE_T ")", handle->plugin_api_version, handle->plugin_funcs_size);
|
2010-06-06 14:15:15 +00:00
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG_ERROR("Unable to load plugin: %s - API version mistmatch", filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-07-29 06:41:26 +00:00
|
|
|
LOG_ERROR("Unable to load plugin: %s - Failed to initialize: %s", filename, handle->error_msg);
|
2010-06-06 14:15:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
plugin_close(plugin);
|
|
|
|
hub_free(handle);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-07-29 06:43:22 +00:00
|
|
|
void plugin_unload(struct plugin_handle* plugin)
|
2010-06-12 00:54:53 +00:00
|
|
|
{
|
2011-12-09 09:29:50 +00:00
|
|
|
struct plugin_hub_internals* internals = get_internals(plugin);
|
|
|
|
internals->unregister(plugin);
|
2012-03-20 21:29:15 +00:00
|
|
|
plugin_unregister_callback_functions(plugin);
|
2010-06-12 00:54:53 +00:00
|
|
|
plugin_close(plugin->handle);
|
2011-12-01 12:37:07 +00:00
|
|
|
hub_free(plugin);
|
2010-06-12 00:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int plugin_parse_line(char* line, int line_count, void* ptr_data)
|
|
|
|
{
|
2011-12-09 09:29:50 +00:00
|
|
|
struct hub_info* hub = (struct hub_info*) ptr_data;
|
|
|
|
struct uhub_plugins* handle = hub->plugins;
|
2010-07-18 17:57:07 +00:00
|
|
|
struct cfg_tokens* tokens = cfg_tokenize(line);
|
2011-12-09 09:29:50 +00:00
|
|
|
struct plugin_handle* plugin;
|
2010-07-10 01:36:47 +00:00
|
|
|
char *directive, *soname, *params;
|
2010-06-12 00:54:53 +00:00
|
|
|
|
2010-07-18 17:57:07 +00:00
|
|
|
if (cfg_token_count(tokens) == 0)
|
2010-07-29 06:41:26 +00:00
|
|
|
{
|
|
|
|
cfg_tokens_free(tokens);
|
2010-06-12 00:54:53 +00:00
|
|
|
return 0;
|
2010-07-29 06:41:26 +00:00
|
|
|
}
|
2010-06-12 00:54:53 +00:00
|
|
|
|
2010-07-18 17:57:07 +00:00
|
|
|
if (cfg_token_count(tokens) < 2)
|
2010-07-29 06:41:26 +00:00
|
|
|
{
|
|
|
|
cfg_tokens_free(tokens);
|
2010-07-10 01:36:47 +00:00
|
|
|
return -1;
|
2010-07-29 06:41:26 +00:00
|
|
|
}
|
2010-06-12 00:54:53 +00:00
|
|
|
|
2010-07-18 17:57:07 +00:00
|
|
|
directive = cfg_token_get_first(tokens);
|
|
|
|
soname = cfg_token_get_next(tokens);
|
|
|
|
params = cfg_token_get_next(tokens);
|
2010-06-12 00:54:53 +00:00
|
|
|
|
2010-07-10 01:36:47 +00:00
|
|
|
if (strcmp(directive, "plugin") == 0 && soname && *soname)
|
2010-06-12 00:54:53 +00:00
|
|
|
{
|
2010-07-10 01:36:47 +00:00
|
|
|
if (!params)
|
|
|
|
params = "";
|
|
|
|
|
2011-12-09 15:36:14 +00:00
|
|
|
LOG_PLUGIN("Load plugin: \"%s\", params=\"%s\"", soname, params);
|
2011-12-09 09:29:50 +00:00
|
|
|
plugin = plugin_load(soname, params, hub);
|
2010-07-10 01:36:47 +00:00
|
|
|
if (plugin)
|
2010-06-12 00:54:53 +00:00
|
|
|
{
|
2010-07-10 01:36:47 +00:00
|
|
|
list_append(handle->loaded, plugin);
|
2010-07-29 06:41:26 +00:00
|
|
|
cfg_tokens_free(tokens);
|
2010-07-10 01:36:47 +00:00
|
|
|
return 0;
|
2010-06-12 00:54:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-18 17:57:07 +00:00
|
|
|
cfg_tokens_free(tokens);
|
2010-06-12 00:54:53 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
int plugin_initialize(struct hub_config* config, struct hub_info* hub)
|
2010-06-12 00:54:53 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
hub->plugins->loaded = list_create();
|
|
|
|
if (!hub->plugins->loaded)
|
2010-06-12 00:54:53 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (config)
|
|
|
|
{
|
|
|
|
if (!*config->file_plugins)
|
|
|
|
return 0;
|
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
ret = file_read_lines(config->file_plugins, hub, &plugin_parse_line);
|
2010-06-12 00:54:53 +00:00
|
|
|
if (ret == -1)
|
2011-12-01 12:37:07 +00:00
|
|
|
{
|
2011-12-09 09:29:50 +00:00
|
|
|
list_clear(hub->plugins->loaded, hub_free);
|
|
|
|
list_destroy(hub->plugins->loaded);
|
|
|
|
hub->plugins->loaded = 0;
|
2010-06-12 00:54:53 +00:00
|
|
|
return -1;
|
2011-12-01 12:37:07 +00:00
|
|
|
}
|
2010-06-12 00:54:53 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-07-29 06:41:26 +00:00
|
|
|
void plugin_shutdown(struct uhub_plugins* handle)
|
|
|
|
{
|
2010-07-29 06:43:22 +00:00
|
|
|
struct plugin_handle* plugin = (struct plugin_handle*) list_get_first(handle->loaded);
|
2010-07-29 06:41:26 +00:00
|
|
|
while (plugin)
|
|
|
|
{
|
|
|
|
list_remove(handle->loaded, plugin);
|
|
|
|
plugin_unload(plugin);
|
2010-07-29 06:43:22 +00:00
|
|
|
plugin = (struct plugin_handle*) list_get_first(handle->loaded);
|
2010-07-29 06:41:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
list_destroy(handle->loaded);
|
|
|
|
}
|
2010-06-06 14:15:15 +00:00
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
// Used internally only
|
|
|
|
struct hub_info* plugin_get_hub(struct plugin_handle* plugin)
|
|
|
|
{
|
|
|
|
struct plugin_hub_internals* data = get_internals(plugin);
|
|
|
|
return data->hub;
|
|
|
|
}
|
|
|
|
|