2008-03-13 14:14:44 +00:00
|
|
|
/*
|
2010-02-23 15:20:38 +00:00
|
|
|
* Copyright (C) 2010 Tobias Brunner
|
2008-03-13 14:14:44 +00:00
|
|
|
* Copyright (C) 2007 Martin Willi
|
|
|
|
* Hochschule fuer Technik Rapperswil
|
|
|
|
*
|
|
|
|
* 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. See <http://www.fsf.org/copyleft/gpl.txt>.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2008-06-24 12:49:04 +00:00
|
|
|
#define _GNU_SOURCE
|
2008-03-13 14:14:44 +00:00
|
|
|
#include "plugin_loader.h"
|
|
|
|
|
2008-05-15 14:01:26 +00:00
|
|
|
#include <string.h>
|
2008-03-13 14:14:44 +00:00
|
|
|
#include <dlfcn.h>
|
2008-05-15 14:01:26 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
2008-03-13 14:14:44 +00:00
|
|
|
|
|
|
|
#include <debug.h>
|
2011-05-19 17:38:46 +00:00
|
|
|
#include <library.h>
|
2009-06-18 15:50:28 +00:00
|
|
|
#include <integrity_checker.h>
|
2008-03-13 14:14:44 +00:00
|
|
|
#include <utils/linked_list.h>
|
|
|
|
#include <plugins/plugin.h>
|
|
|
|
|
|
|
|
typedef struct private_plugin_loader_t private_plugin_loader_t;
|
2011-05-18 15:00:46 +00:00
|
|
|
typedef struct plugin_entry_t plugin_entry_t;
|
2008-03-13 14:14:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* private data of plugin_loader
|
|
|
|
*/
|
|
|
|
struct private_plugin_loader_t {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* public functions
|
|
|
|
*/
|
|
|
|
plugin_loader_t public;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-03-13 14:14:44 +00:00
|
|
|
/**
|
2011-05-18 15:00:46 +00:00
|
|
|
* List of plugins, as plugin_entry_t
|
2008-03-13 14:14:44 +00:00
|
|
|
*/
|
|
|
|
linked_list_t *plugins;
|
|
|
|
};
|
|
|
|
|
2011-05-18 15:00:46 +00:00
|
|
|
/**
|
|
|
|
* Entry for a plugin
|
|
|
|
*/
|
|
|
|
struct plugin_entry_t {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Plugin instance
|
|
|
|
*/
|
|
|
|
plugin_t *plugin;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* dlopen handle, if in separate lib
|
|
|
|
*/
|
|
|
|
void *handle;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of loaded features
|
|
|
|
*/
|
|
|
|
linked_list_t *loaded;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy a plugin entry
|
|
|
|
*/
|
|
|
|
static void plugin_entry_destroy(plugin_entry_t *entry)
|
|
|
|
{
|
|
|
|
DESTROY_IF(entry->plugin);
|
|
|
|
if (entry->handle)
|
|
|
|
{
|
|
|
|
dlclose(entry->handle);
|
|
|
|
}
|
|
|
|
entry->loaded->destroy(entry->loaded);
|
|
|
|
free(entry);
|
|
|
|
}
|
|
|
|
|
2010-03-01 15:07:07 +00:00
|
|
|
/**
|
2011-02-10 15:46:06 +00:00
|
|
|
* create a plugin
|
|
|
|
* returns: NOT_FOUND, if the constructor was not found
|
|
|
|
* FAILED, if the plugin could not be constructed
|
2010-03-01 15:07:07 +00:00
|
|
|
*/
|
2011-02-10 15:46:06 +00:00
|
|
|
static status_t create_plugin(private_plugin_loader_t *this, void *handle,
|
2011-05-18 15:00:46 +00:00
|
|
|
char *name, bool integrity, plugin_entry_t **entry)
|
2010-03-01 15:07:07 +00:00
|
|
|
{
|
|
|
|
char create[128];
|
2011-05-18 15:00:46 +00:00
|
|
|
plugin_t *plugin;
|
2010-03-01 15:07:07 +00:00
|
|
|
plugin_constructor_t constructor;
|
|
|
|
|
|
|
|
if (snprintf(create, sizeof(create), "%s_plugin_create",
|
|
|
|
name) >= sizeof(create))
|
|
|
|
{
|
2011-02-10 15:46:06 +00:00
|
|
|
return FAILED;
|
2010-03-01 15:07:07 +00:00
|
|
|
}
|
2010-03-08 14:26:09 +00:00
|
|
|
translate(create, "-", "_");
|
2011-02-10 15:46:06 +00:00
|
|
|
constructor = dlsym(handle, create);
|
2010-03-01 15:07:07 +00:00
|
|
|
if (constructor == NULL)
|
|
|
|
{
|
2011-02-10 15:46:06 +00:00
|
|
|
return NOT_FOUND;
|
|
|
|
}
|
|
|
|
if (integrity && lib->integrity)
|
|
|
|
{
|
|
|
|
if (!lib->integrity->check_segment(lib->integrity, name, constructor))
|
|
|
|
{
|
|
|
|
DBG1(DBG_LIB, "plugin '%s': failed segment integrity test", name);
|
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
DBG1(DBG_LIB, "plugin '%s': passed file and segment integrity tests",
|
|
|
|
name);
|
2010-03-01 15:07:07 +00:00
|
|
|
}
|
2011-05-18 15:00:46 +00:00
|
|
|
plugin = constructor();
|
|
|
|
if (plugin == NULL)
|
2010-03-01 15:07:07 +00:00
|
|
|
{
|
2010-03-31 15:28:46 +00:00
|
|
|
DBG1(DBG_LIB, "plugin '%s': failed to load - %s returned NULL", name,
|
|
|
|
create);
|
2011-02-10 15:46:06 +00:00
|
|
|
return FAILED;
|
2010-03-01 15:07:07 +00:00
|
|
|
}
|
2011-05-18 15:00:46 +00:00
|
|
|
INIT(*entry,
|
|
|
|
.plugin = plugin,
|
|
|
|
.loaded = linked_list_create(),
|
|
|
|
);
|
2010-03-31 15:28:46 +00:00
|
|
|
DBG2(DBG_LIB, "plugin '%s': loaded successfully", name);
|
2011-02-10 15:46:06 +00:00
|
|
|
return SUCCESS;
|
2010-03-01 15:07:07 +00:00
|
|
|
}
|
2011-02-10 15:46:06 +00:00
|
|
|
|
2008-03-13 14:14:44 +00:00
|
|
|
/**
|
2008-05-15 14:01:26 +00:00
|
|
|
* load a single plugin
|
2008-03-13 14:14:44 +00:00
|
|
|
*/
|
2011-05-18 15:00:46 +00:00
|
|
|
static bool load_plugin(private_plugin_loader_t *this, char *name, char *file)
|
2008-03-13 14:14:44 +00:00
|
|
|
{
|
2011-05-18 15:00:46 +00:00
|
|
|
plugin_entry_t *entry;
|
2008-03-13 14:14:44 +00:00
|
|
|
void *handle;
|
2011-02-10 15:46:06 +00:00
|
|
|
|
2011-05-18 15:00:46 +00:00
|
|
|
switch (create_plugin(this, RTLD_DEFAULT, name, FALSE, &entry))
|
2011-02-10 15:46:06 +00:00
|
|
|
{
|
|
|
|
case SUCCESS:
|
2011-05-18 15:00:46 +00:00
|
|
|
this->plugins->insert_last(this->plugins, entry);
|
|
|
|
return TRUE;
|
2011-02-10 15:46:06 +00:00
|
|
|
case NOT_FOUND:
|
|
|
|
/* try to load the plugin from a file */
|
|
|
|
break;
|
|
|
|
default:
|
2011-05-18 15:00:46 +00:00
|
|
|
return FALSE;
|
2010-02-23 15:20:38 +00:00
|
|
|
}
|
2009-07-17 15:00:17 +00:00
|
|
|
if (lib->integrity)
|
2009-06-18 15:50:28 +00:00
|
|
|
{
|
2009-07-17 15:00:17 +00:00
|
|
|
if (!lib->integrity->check_file(lib->integrity, name, file))
|
|
|
|
{
|
2010-03-31 15:28:46 +00:00
|
|
|
DBG1(DBG_LIB, "plugin '%s': failed file integrity test of '%s'",
|
|
|
|
name, file);
|
2011-05-18 15:00:46 +00:00
|
|
|
return FALSE;
|
2009-07-17 15:00:17 +00:00
|
|
|
}
|
2009-06-18 15:50:28 +00:00
|
|
|
}
|
2008-05-15 14:01:26 +00:00
|
|
|
handle = dlopen(file, RTLD_LAZY);
|
|
|
|
if (handle == NULL)
|
2008-03-13 14:14:44 +00:00
|
|
|
{
|
2010-05-05 09:15:10 +00:00
|
|
|
DBG1(DBG_LIB, "plugin '%s' failed to load: %s", name, dlerror());
|
2011-05-18 15:00:46 +00:00
|
|
|
return FALSE;
|
2008-03-13 14:14:44 +00:00
|
|
|
}
|
2011-05-18 15:00:46 +00:00
|
|
|
if (create_plugin(this, handle, name, TRUE, &entry) != SUCCESS)
|
2008-05-15 14:01:26 +00:00
|
|
|
{
|
|
|
|
dlclose(handle);
|
2011-05-18 15:00:46 +00:00
|
|
|
return FALSE;
|
2008-05-15 14:01:26 +00:00
|
|
|
}
|
2011-05-18 15:00:46 +00:00
|
|
|
entry->handle = handle;
|
|
|
|
this->plugins->insert_last(this->plugins, entry);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert enumerated entries to plugin_t
|
|
|
|
*/
|
2011-06-29 12:49:32 +00:00
|
|
|
static bool plugin_filter(void *null, plugin_entry_t **entry, plugin_t **plugin,
|
|
|
|
void *in, linked_list_t **list)
|
2011-05-18 15:00:46 +00:00
|
|
|
{
|
|
|
|
*plugin = (*entry)->plugin;
|
2011-06-29 12:49:32 +00:00
|
|
|
if (list)
|
|
|
|
{
|
|
|
|
*list = (*entry)->loaded;
|
|
|
|
}
|
2011-05-18 15:00:46 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
METHOD(plugin_loader_t, create_plugin_enumerator, enumerator_t*,
|
|
|
|
private_plugin_loader_t *this)
|
|
|
|
{
|
|
|
|
return enumerator_create_filter(
|
|
|
|
this->plugins->create_enumerator(this->plugins),
|
|
|
|
(void*)plugin_filter, NULL, NULL);
|
2008-05-15 14:01:26 +00:00
|
|
|
}
|
|
|
|
|
2010-11-23 10:06:46 +00:00
|
|
|
/**
|
|
|
|
* Check if a plugin is already loaded
|
|
|
|
*/
|
|
|
|
static bool plugin_loaded(private_plugin_loader_t *this, char *name)
|
|
|
|
{
|
|
|
|
enumerator_t *enumerator;
|
|
|
|
bool found = FALSE;
|
2011-04-11 16:54:18 +00:00
|
|
|
plugin_t *plugin;
|
2010-11-23 10:06:46 +00:00
|
|
|
|
2011-05-18 15:00:46 +00:00
|
|
|
enumerator = create_plugin_enumerator(this);
|
|
|
|
while (enumerator->enumerate(enumerator, &plugin, NULL))
|
2010-11-23 10:06:46 +00:00
|
|
|
{
|
2011-04-11 16:54:18 +00:00
|
|
|
if (streq(plugin->get_name(plugin), name))
|
2010-11-23 10:06:46 +00:00
|
|
|
{
|
|
|
|
found = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
enumerator->destroy(enumerator);
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2011-05-18 15:00:46 +00:00
|
|
|
/**
|
|
|
|
* Check if a feature of a plugin is already loaded
|
|
|
|
*/
|
|
|
|
static bool feature_loaded(private_plugin_loader_t *this, plugin_entry_t *entry,
|
|
|
|
plugin_feature_t *feature)
|
|
|
|
{
|
|
|
|
return entry->loaded->find_first(entry->loaded, NULL,
|
|
|
|
(void**)&feature) == SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if dependencies are satisfied
|
|
|
|
*/
|
2011-09-23 08:45:38 +00:00
|
|
|
static bool dependencies_satisfied(private_plugin_loader_t *this,
|
|
|
|
plugin_entry_t *entry, bool soft, bool report,
|
|
|
|
plugin_feature_t *features, int count)
|
2011-05-18 15:00:46 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* first entry is provided feature, followed by dependencies */
|
|
|
|
for (i = 1; i < count; i++)
|
|
|
|
{
|
|
|
|
enumerator_t *entries, *loaded;
|
|
|
|
plugin_feature_t *feature;
|
2011-09-23 08:45:38 +00:00
|
|
|
plugin_entry_t *current;
|
2011-05-18 15:00:46 +00:00
|
|
|
bool found = FALSE;
|
|
|
|
|
|
|
|
if (features[i].kind != FEATURE_DEPENDS &&
|
|
|
|
features[i].kind != FEATURE_SDEPEND)
|
|
|
|
{ /* end of dependencies */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
entries = this->plugins->create_enumerator(this->plugins);
|
2011-09-23 08:45:38 +00:00
|
|
|
while (entries->enumerate(entries, ¤t))
|
2011-05-18 15:00:46 +00:00
|
|
|
{
|
2011-09-23 08:45:38 +00:00
|
|
|
loaded = current->loaded->create_enumerator(current->loaded);
|
2011-05-18 15:00:46 +00:00
|
|
|
while (loaded->enumerate(loaded, &feature))
|
|
|
|
{
|
|
|
|
if (plugin_feature_matches(&features[i], feature))
|
|
|
|
{
|
|
|
|
found = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
loaded->destroy(loaded);
|
|
|
|
}
|
|
|
|
entries->destroy(entries);
|
|
|
|
|
2011-09-22 15:59:17 +00:00
|
|
|
if (!found && (features[i].kind != FEATURE_SDEPEND || soft))
|
2011-05-18 15:00:46 +00:00
|
|
|
{
|
|
|
|
if (report)
|
|
|
|
{
|
2011-09-23 08:45:38 +00:00
|
|
|
char *provide, *depend, *name;
|
2011-05-18 15:00:46 +00:00
|
|
|
|
2011-09-23 08:45:38 +00:00
|
|
|
name = entry->plugin->get_name(entry->plugin);
|
2011-05-18 15:00:46 +00:00
|
|
|
provide = plugin_feature_get_string(&features[0]);
|
|
|
|
depend = plugin_feature_get_string(&features[i]);
|
|
|
|
DBG1(DBG_LIB, "feature %s in '%s' plugin has unsatisfied "
|
|
|
|
"dependency: %s", provide, name, depend);
|
|
|
|
free(provide);
|
|
|
|
free(depend);
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-09-23 08:45:38 +00:00
|
|
|
* Check if a given feature is still required as dependency
|
2011-05-18 15:00:46 +00:00
|
|
|
*/
|
2011-09-23 08:45:38 +00:00
|
|
|
static bool dependency_required(private_plugin_loader_t *this,
|
|
|
|
plugin_feature_t *dep)
|
2011-05-18 15:00:46 +00:00
|
|
|
{
|
2011-09-23 08:45:38 +00:00
|
|
|
enumerator_t *enumerator;
|
|
|
|
plugin_feature_t *features;
|
|
|
|
plugin_entry_t *entry;
|
|
|
|
int count, i;
|
2011-05-18 15:00:46 +00:00
|
|
|
|
2011-09-23 08:45:38 +00:00
|
|
|
enumerator = this->plugins->create_enumerator(this->plugins);
|
|
|
|
while (enumerator->enumerate(enumerator, &entry))
|
2011-05-18 15:00:46 +00:00
|
|
|
{
|
2011-09-23 08:45:38 +00:00
|
|
|
if (!entry->plugin->get_features)
|
|
|
|
{ /* features not supported */
|
|
|
|
continue;
|
2011-05-18 15:00:46 +00:00
|
|
|
}
|
2011-09-23 08:45:38 +00:00
|
|
|
count = entry->plugin->get_features(entry->plugin, &features);
|
|
|
|
for (i = 0; i < count; i++)
|
2011-05-18 15:00:46 +00:00
|
|
|
{
|
2011-09-23 08:45:38 +00:00
|
|
|
if (feature_loaded(this, entry, &features[i]))
|
|
|
|
{
|
|
|
|
while (++i < count && (features[i].kind == FEATURE_DEPENDS ||
|
|
|
|
features[i].kind == FEATURE_SDEPEND))
|
|
|
|
{
|
|
|
|
if (plugin_feature_matches(&features[i], dep))
|
|
|
|
{
|
|
|
|
enumerator->destroy(enumerator);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-05-18 15:00:46 +00:00
|
|
|
}
|
|
|
|
}
|
2011-09-23 08:45:38 +00:00
|
|
|
enumerator->destroy(enumerator);
|
|
|
|
return FALSE;
|
2011-05-18 15:00:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load plugin features in correct order
|
|
|
|
*/
|
|
|
|
static int load_features(private_plugin_loader_t *this, bool soft, bool report)
|
|
|
|
{
|
|
|
|
enumerator_t *enumerator;
|
2011-09-23 08:45:38 +00:00
|
|
|
plugin_feature_t *feature, *reg = NULL;
|
2011-05-18 15:00:46 +00:00
|
|
|
plugin_entry_t *entry;
|
|
|
|
int count, i, loaded = 0;
|
|
|
|
|
|
|
|
enumerator = this->plugins->create_enumerator(this->plugins);
|
|
|
|
while (enumerator->enumerate(enumerator, &entry))
|
|
|
|
{
|
|
|
|
if (!entry->plugin->get_features)
|
|
|
|
{ /* feature interface not supported */
|
|
|
|
continue;
|
|
|
|
}
|
2011-09-23 08:45:38 +00:00
|
|
|
count = entry->plugin->get_features(entry->plugin, &feature);
|
2011-05-18 15:00:46 +00:00
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
2011-09-23 08:45:38 +00:00
|
|
|
switch (feature->kind)
|
2011-05-18 15:00:46 +00:00
|
|
|
{
|
|
|
|
case FEATURE_PROVIDE:
|
2011-09-23 08:45:38 +00:00
|
|
|
if (!feature_loaded(this, entry, feature) &&
|
|
|
|
dependencies_satisfied(this, entry, soft, report,
|
|
|
|
feature, count - i) &&
|
|
|
|
plugin_feature_load(entry->plugin, feature, reg))
|
2011-05-18 15:00:46 +00:00
|
|
|
{
|
2011-09-23 08:45:38 +00:00
|
|
|
entry->loaded->insert_last(entry->loaded, feature);
|
2011-05-18 15:00:46 +00:00
|
|
|
loaded++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case FEATURE_REGISTER:
|
|
|
|
case FEATURE_CALLBACK:
|
2011-09-23 08:45:38 +00:00
|
|
|
reg = feature;
|
2011-05-18 15:00:46 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2011-09-23 08:45:38 +00:00
|
|
|
feature++;
|
2011-05-18 15:00:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
enumerator->destroy(enumerator);
|
|
|
|
return loaded;
|
|
|
|
}
|
|
|
|
|
2011-09-23 08:45:38 +00:00
|
|
|
/**
|
|
|
|
* Try to unload plugin features on which is not depended anymore
|
|
|
|
*/
|
|
|
|
static int unload_features(private_plugin_loader_t *this, plugin_entry_t *entry)
|
|
|
|
{
|
|
|
|
plugin_feature_t *feature, *reg = NULL;
|
|
|
|
int count, i, unloaded = 0;
|
|
|
|
|
|
|
|
count = entry->plugin->get_features(entry->plugin, &feature);
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
switch (feature->kind)
|
|
|
|
{
|
|
|
|
case FEATURE_PROVIDE:
|
|
|
|
if (feature_loaded(this, entry, feature) &&
|
|
|
|
!dependency_required(this, feature) &&
|
|
|
|
plugin_feature_unload(entry->plugin, feature, reg))
|
|
|
|
{
|
|
|
|
entry->loaded->remove(entry->loaded, feature, NULL);
|
|
|
|
unloaded++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case FEATURE_REGISTER:
|
|
|
|
case FEATURE_CALLBACK:
|
|
|
|
reg = feature;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
feature++;
|
|
|
|
}
|
|
|
|
return unloaded;
|
|
|
|
}
|
|
|
|
|
2011-09-22 16:04:53 +00:00
|
|
|
/**
|
|
|
|
* Remove plugins that we were not able to load any features from.
|
|
|
|
*/
|
|
|
|
static void purge_plugins(private_plugin_loader_t *this)
|
|
|
|
{
|
|
|
|
enumerator_t *enumerator;
|
|
|
|
plugin_entry_t *entry;
|
|
|
|
|
|
|
|
enumerator = this->plugins->create_enumerator(this->plugins);
|
|
|
|
while (enumerator->enumerate(enumerator, &entry))
|
|
|
|
{
|
|
|
|
if (!entry->plugin->get_features)
|
|
|
|
{ /* feature interface not supported */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!entry->loaded->get_count(entry->loaded))
|
|
|
|
{
|
|
|
|
this->plugins->remove_at(this->plugins, enumerator);
|
|
|
|
plugin_entry_destroy(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
enumerator->destroy(enumerator);
|
|
|
|
}
|
|
|
|
|
2011-04-11 14:25:58 +00:00
|
|
|
METHOD(plugin_loader_t, load_plugins, bool,
|
|
|
|
private_plugin_loader_t *this, char *path, char *list)
|
2008-05-15 14:01:26 +00:00
|
|
|
{
|
2008-07-02 08:19:43 +00:00
|
|
|
enumerator_t *enumerator;
|
|
|
|
char *token;
|
2009-09-01 14:08:28 +00:00
|
|
|
bool critical_failed = FALSE;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2009-09-08 17:48:34 +00:00
|
|
|
if (path == NULL)
|
|
|
|
{
|
|
|
|
path = PLUGINDIR;
|
|
|
|
}
|
|
|
|
|
2008-07-02 08:19:43 +00:00
|
|
|
enumerator = enumerator_create_token(list, " ", " ");
|
2009-09-01 14:08:28 +00:00
|
|
|
while (!critical_failed && enumerator->enumerate(enumerator, &token))
|
2008-05-15 14:01:26 +00:00
|
|
|
{
|
2009-09-01 14:08:28 +00:00
|
|
|
bool critical = FALSE;
|
2011-05-18 15:00:46 +00:00
|
|
|
char file[PATH_MAX];
|
2009-09-01 14:08:28 +00:00
|
|
|
int len;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2009-09-01 14:08:28 +00:00
|
|
|
token = strdup(token);
|
|
|
|
len = strlen(token);
|
|
|
|
if (token[len-1] == '!')
|
|
|
|
{
|
|
|
|
critical = TRUE;
|
|
|
|
token[len-1] = '\0';
|
|
|
|
}
|
2010-11-23 10:06:46 +00:00
|
|
|
if (plugin_loaded(this, token))
|
|
|
|
{
|
|
|
|
free(token);
|
|
|
|
continue;
|
|
|
|
}
|
2011-05-18 15:00:46 +00:00
|
|
|
if (snprintf(file, sizeof(file), "%s/libstrongswan-%s.so",
|
|
|
|
path, token) >= sizeof(file))
|
2009-09-01 14:08:28 +00:00
|
|
|
{
|
2011-05-18 15:00:46 +00:00
|
|
|
return NULL;
|
2009-09-01 14:08:28 +00:00
|
|
|
}
|
2011-05-18 15:00:46 +00:00
|
|
|
if (!load_plugin(this, token, file) && critical)
|
2009-09-01 14:08:28 +00:00
|
|
|
{
|
2011-05-18 15:00:46 +00:00
|
|
|
critical_failed = TRUE;
|
|
|
|
DBG1(DBG_LIB, "loading critical plugin '%s' failed", token);
|
2008-03-13 14:14:44 +00:00
|
|
|
}
|
2011-04-11 16:54:18 +00:00
|
|
|
free(token);
|
2008-03-13 14:14:44 +00:00
|
|
|
}
|
2008-07-02 08:19:43 +00:00
|
|
|
enumerator->destroy(enumerator);
|
2011-05-18 15:00:46 +00:00
|
|
|
if (!critical_failed)
|
|
|
|
{
|
|
|
|
while (load_features(this, TRUE, FALSE))
|
|
|
|
{
|
|
|
|
/* try load new features until we don't get new ones */
|
|
|
|
}
|
|
|
|
while (load_features(this, FALSE, FALSE))
|
|
|
|
{
|
|
|
|
/* second round, ignoring soft dependencies */
|
|
|
|
}
|
|
|
|
/* report missing dependencies */
|
|
|
|
load_features(this, FALSE, TRUE);
|
2011-09-22 16:04:53 +00:00
|
|
|
/* unload plugins that we were not able to load any features for */
|
|
|
|
purge_plugins(this);
|
2011-05-18 15:00:46 +00:00
|
|
|
}
|
2009-09-01 14:08:28 +00:00
|
|
|
return !critical_failed;
|
2008-03-13 14:14:44 +00:00
|
|
|
}
|
|
|
|
|
2011-04-11 14:25:58 +00:00
|
|
|
METHOD(plugin_loader_t, unload, void,
|
|
|
|
private_plugin_loader_t *this)
|
2008-05-15 14:01:26 +00:00
|
|
|
{
|
2011-09-23 08:45:38 +00:00
|
|
|
enumerator_t *enumerator;
|
2011-05-18 15:00:46 +00:00
|
|
|
plugin_entry_t *entry;
|
2011-09-23 08:45:38 +00:00
|
|
|
linked_list_t *list;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2011-09-23 08:45:38 +00:00
|
|
|
/* unload plugins in reverse order, for those not supporting features */
|
|
|
|
list = linked_list_create();
|
|
|
|
while (this->plugins->remove_last(this->plugins, (void**)&entry) == SUCCESS)
|
2008-05-15 14:01:26 +00:00
|
|
|
{
|
2011-09-23 08:45:38 +00:00
|
|
|
list->insert_last(list, entry);
|
|
|
|
}
|
|
|
|
while (list->remove_last(list, (void**)&entry) == SUCCESS)
|
|
|
|
{
|
|
|
|
this->plugins->insert_first(this->plugins, entry);
|
|
|
|
}
|
|
|
|
while (this->plugins->get_count(this->plugins))
|
|
|
|
{
|
|
|
|
enumerator = this->plugins->create_enumerator(this->plugins);
|
|
|
|
while (enumerator->enumerate(enumerator, &entry))
|
|
|
|
{
|
|
|
|
if (entry->plugin->get_features)
|
|
|
|
{ /* supports features */
|
|
|
|
while (unload_features(this, entry));
|
|
|
|
}
|
|
|
|
if (entry->loaded->get_count(entry->loaded) == 0)
|
|
|
|
{
|
|
|
|
if (lib->leak_detective)
|
|
|
|
{ /* keep handle to report leaks properly */
|
|
|
|
entry->handle = NULL;
|
|
|
|
}
|
|
|
|
this->plugins->remove_at(this->plugins, enumerator);
|
|
|
|
plugin_entry_destroy(entry);
|
|
|
|
}
|
2011-05-18 15:00:46 +00:00
|
|
|
}
|
2011-09-23 08:45:38 +00:00
|
|
|
enumerator->destroy(enumerator);
|
2008-05-15 14:01:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-11 17:40:30 +00:00
|
|
|
/**
|
|
|
|
* Reload a plugin by name, NULL for all
|
|
|
|
*/
|
|
|
|
static u_int reload_by_name(private_plugin_loader_t *this, char *name)
|
|
|
|
{
|
|
|
|
u_int reloaded = 0;
|
|
|
|
enumerator_t *enumerator;
|
|
|
|
plugin_t *plugin;
|
|
|
|
|
|
|
|
enumerator = create_plugin_enumerator(this);
|
2011-05-18 15:00:46 +00:00
|
|
|
while (enumerator->enumerate(enumerator, &plugin, NULL))
|
2011-04-11 17:40:30 +00:00
|
|
|
{
|
|
|
|
if (name == NULL || streq(name, plugin->get_name(plugin)))
|
|
|
|
{
|
2011-05-18 15:00:46 +00:00
|
|
|
if (plugin->reload && plugin->reload(plugin))
|
2011-04-11 17:40:30 +00:00
|
|
|
{
|
|
|
|
DBG2(DBG_LIB, "reloaded configuration of '%s' plugin",
|
|
|
|
plugin->get_name(plugin));
|
|
|
|
reloaded++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
enumerator->destroy(enumerator);
|
|
|
|
return reloaded;
|
|
|
|
}
|
|
|
|
|
|
|
|
METHOD(plugin_loader_t, reload, u_int,
|
|
|
|
private_plugin_loader_t *this, char *list)
|
|
|
|
{
|
|
|
|
u_int reloaded = 0;
|
|
|
|
enumerator_t *enumerator;
|
|
|
|
char *name;
|
|
|
|
|
|
|
|
if (list == NULL)
|
|
|
|
{
|
|
|
|
return reload_by_name(this, NULL);
|
|
|
|
}
|
|
|
|
enumerator = enumerator_create_token(list, " ", "");
|
|
|
|
while (enumerator->enumerate(enumerator, &name))
|
|
|
|
{
|
|
|
|
reloaded += reload_by_name(this, name);
|
|
|
|
}
|
|
|
|
enumerator->destroy(enumerator);
|
|
|
|
return reloaded;
|
|
|
|
}
|
|
|
|
|
2011-04-11 14:25:58 +00:00
|
|
|
METHOD(plugin_loader_t, destroy, void,
|
|
|
|
private_plugin_loader_t *this)
|
2008-03-13 14:14:44 +00:00
|
|
|
{
|
2011-05-18 15:00:46 +00:00
|
|
|
unload(this);
|
|
|
|
this->plugins->destroy(this->plugins);
|
2008-03-13 14:14:44 +00:00
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* see header file
|
|
|
|
*/
|
|
|
|
plugin_loader_t *plugin_loader_create()
|
|
|
|
{
|
2011-04-11 14:25:58 +00:00
|
|
|
private_plugin_loader_t *this;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2011-04-11 14:25:58 +00:00
|
|
|
INIT(this,
|
|
|
|
.public = {
|
2011-04-11 14:41:25 +00:00
|
|
|
.load = _load_plugins,
|
2011-04-11 17:40:30 +00:00
|
|
|
.reload = _reload,
|
2011-04-11 14:25:58 +00:00
|
|
|
.unload = _unload,
|
|
|
|
.create_plugin_enumerator = _create_plugin_enumerator,
|
|
|
|
.destroy = _destroy,
|
|
|
|
},
|
|
|
|
.plugins = linked_list_create(),
|
|
|
|
);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-03-13 14:14:44 +00:00
|
|
|
return &this->public;
|
|
|
|
}
|
|
|
|
|