Implemented feature unloading, moved feature registration plugin_features.c

This commit is contained in:
Martin Willi
2011-10-14 10:05:47 +02:00
parent c293c87761
commit d965872d66
3 changed files with 280 additions and 126 deletions
+150
View File
@@ -18,6 +18,8 @@
#include "plugin_feature.h" #include "plugin_feature.h"
#include <debug.h>
ENUM(plugin_feature_names, FEATURE_NONE, FEATURE_CUSTOM, ENUM(plugin_feature_names, FEATURE_NONE, FEATURE_CUSTOM,
"NONE", "NONE",
"CRYPTER", "CRYPTER",
@@ -234,3 +236,151 @@ char* plugin_feature_get_string(plugin_feature_t *feature)
} }
return str; return str;
} }
/**
* See header.
*/
bool plugin_feature_load(plugin_t *plugin, plugin_feature_t *feature,
plugin_feature_t *reg)
{
char *name;
if (!reg)
{ /* noting to do for this feature */
return TRUE;
}
if (reg->kind == FEATURE_CALLBACK)
{
if (reg->arg.cb.f(plugin, feature, TRUE, reg->arg.cb.data))
{
return TRUE;
}
return FALSE;
}
name = plugin->get_name(plugin);
switch (feature->type)
{
case FEATURE_CRYPTER:
lib->crypto->add_crypter(lib->crypto, feature->arg.crypter.alg,
name, reg->arg.reg.f);
break;
case FEATURE_AEAD:
lib->crypto->add_aead(lib->crypto, feature->arg.aead.alg,
name, reg->arg.reg.f);
break;
case FEATURE_SIGNER:
lib->crypto->add_signer(lib->crypto, feature->arg.signer,
name, reg->arg.reg.f);
break;
case FEATURE_HASHER:
lib->crypto->add_hasher(lib->crypto, feature->arg.hasher,
name, reg->arg.reg.f);
break;
case FEATURE_PRF:
lib->crypto->add_prf(lib->crypto, feature->arg.prf,
name, reg->arg.reg.f);
break;
case FEATURE_DH:
lib->crypto->add_dh(lib->crypto, feature->arg.dh_group,
name, reg->arg.reg.f);
break;
case FEATURE_RNG:
lib->crypto->add_rng(lib->crypto, feature->arg.rng_quality,
name, reg->arg.reg.f);
break;
case FEATURE_PRIVKEY:
case FEATURE_PRIVKEY_GEN:
lib->creds->add_builder(lib->creds, CRED_PRIVATE_KEY,
feature->arg.privkey, reg->arg.reg.final,
reg->arg.reg.f);
break;
case FEATURE_PUBKEY:
lib->creds->add_builder(lib->creds, CRED_PUBLIC_KEY,
feature->arg.pubkey, reg->arg.reg.final,
reg->arg.reg.f);
break;
case FEATURE_CERT_DECODE:
case FEATURE_CERT_ENCODE:
lib->creds->add_builder(lib->creds, CRED_CERTIFICATE,
feature->arg.cert, reg->arg.reg.final,
reg->arg.reg.f);
break;
case FEATURE_DATABASE:
lib->db->add_database(lib->db, reg->arg.reg.f);
break;
case FEATURE_FETCHER:
lib->fetcher->add_fetcher(lib->fetcher, reg->arg.reg.f,
feature->arg.fetcher);
break;
default:
break;
}
return TRUE;
}
/**
* See header.
*/
bool plugin_feature_unload(plugin_t *plugin, plugin_feature_t *feature,
plugin_feature_t *reg)
{
char *name;
if (!reg)
{ /* noting to do for this feature */
return TRUE;
}
name = plugin->get_name(plugin);
if (reg->kind == FEATURE_CALLBACK)
{
if (reg->arg.cb.f(plugin, feature, FALSE, reg->arg.cb.data))
{
return TRUE;
}
return FALSE;
}
switch (feature->type)
{
case FEATURE_CRYPTER:
lib->crypto->remove_crypter(lib->crypto, reg->arg.reg.f);
break;
case FEATURE_AEAD:
lib->crypto->remove_aead(lib->crypto, reg->arg.reg.f);
break;
case FEATURE_SIGNER:
lib->crypto->remove_signer(lib->crypto, reg->arg.reg.f);
break;
case FEATURE_HASHER:
lib->crypto->remove_hasher(lib->crypto, reg->arg.reg.f);
break;
case FEATURE_PRF:
lib->crypto->remove_prf(lib->crypto, reg->arg.reg.f);
break;
case FEATURE_DH:
lib->crypto->remove_dh(lib->crypto, reg->arg.reg.f);
break;
case FEATURE_RNG:
lib->crypto->remove_rng(lib->crypto, reg->arg.reg.f);
break;
case FEATURE_PRIVKEY:
case FEATURE_PRIVKEY_GEN:
lib->creds->remove_builder(lib->creds, reg->arg.reg.f);
break;
case FEATURE_PUBKEY:
lib->creds->remove_builder(lib->creds, reg->arg.reg.f);
break;
case FEATURE_CERT_DECODE:
case FEATURE_CERT_ENCODE:
lib->creds->remove_builder(lib->creds, reg->arg.reg.f);
break;
case FEATURE_DATABASE:
lib->db->remove_database(lib->db, reg->arg.reg.f);
break;
case FEATURE_FETCHER:
lib->fetcher->remove_fetcher(lib->fetcher, reg->arg.reg.f);
break;
default:
break;
}
return TRUE;
}
@@ -308,4 +308,24 @@ bool plugin_feature_matches(plugin_feature_t *a, plugin_feature_t *b);
*/ */
char* plugin_feature_get_string(plugin_feature_t *feature); char* plugin_feature_get_string(plugin_feature_t *feature);
/**
* Load a plugin feature using a REGISTER/CALLBACK feature entry.
*
* @param plugin plugin providing feature
* @param feature feature to load
* @param reg REGISTER/CALLBACK feature entry to use for registration
*/
bool plugin_feature_load(plugin_t *plugin, plugin_feature_t *feature,
plugin_feature_t *reg);
/**
* Unload a plugin feature using a REGISTER/CALLBACK feature entry.
*
* @param plugin plugin providing feature
* @param feature feature to unload
* @param reg REGISTER/CALLBACK feature entry to use for deregistration
*/
bool plugin_feature_unload(plugin_t *plugin, plugin_feature_t *feature,
plugin_feature_t *reg);
#endif /** PLUGIN_FEATURE_H_ @}*/ #endif /** PLUGIN_FEATURE_H_ @}*/
+110 -126
View File
@@ -231,8 +231,9 @@ static bool feature_loaded(private_plugin_loader_t *this, plugin_entry_t *entry,
/** /**
* Check if dependencies are satisfied * Check if dependencies are satisfied
*/ */
static bool dependencies_satisfied(private_plugin_loader_t *this, char *name, static bool dependencies_satisfied(private_plugin_loader_t *this,
bool soft, bool report, plugin_feature_t *features, int count) plugin_entry_t *entry, bool soft, bool report,
plugin_feature_t *features, int count)
{ {
int i; int i;
@@ -241,7 +242,7 @@ static bool dependencies_satisfied(private_plugin_loader_t *this, char *name,
{ {
enumerator_t *entries, *loaded; enumerator_t *entries, *loaded;
plugin_feature_t *feature; plugin_feature_t *feature;
plugin_entry_t *entry; plugin_entry_t *current;
bool found = FALSE; bool found = FALSE;
if (features[i].kind != FEATURE_DEPENDS && if (features[i].kind != FEATURE_DEPENDS &&
@@ -250,9 +251,9 @@ static bool dependencies_satisfied(private_plugin_loader_t *this, char *name,
break; break;
} }
entries = this->plugins->create_enumerator(this->plugins); entries = this->plugins->create_enumerator(this->plugins);
while (entries->enumerate(entries, &entry)) while (entries->enumerate(entries, &current))
{ {
loaded = entry->loaded->create_enumerator(entry->loaded); loaded = current->loaded->create_enumerator(current->loaded);
while (loaded->enumerate(loaded, &feature)) while (loaded->enumerate(loaded, &feature))
{ {
if (plugin_feature_matches(&features[i], feature)) if (plugin_feature_matches(&features[i], feature))
@@ -269,8 +270,9 @@ static bool dependencies_satisfied(private_plugin_loader_t *this, char *name,
{ {
if (report) if (report)
{ {
char *provide, *depend; char *provide, *depend, *name;
name = entry->plugin->get_name(entry->plugin);
provide = plugin_feature_get_string(&features[0]); provide = plugin_feature_get_string(&features[0]);
depend = plugin_feature_get_string(&features[i]); depend = plugin_feature_get_string(&features[i]);
DBG1(DBG_LIB, "feature %s in '%s' plugin has unsatisfied " DBG1(DBG_LIB, "feature %s in '%s' plugin has unsatisfied "
@@ -285,117 +287,42 @@ static bool dependencies_satisfied(private_plugin_loader_t *this, char *name,
} }
/** /**
* Load a plugin feature * Check if a given feature is still required as dependency
*/ */
static bool load_feature(private_plugin_loader_t *this, plugin_entry_t *entry, static bool dependency_required(private_plugin_loader_t *this,
char *name, plugin_feature_t *feature, plugin_feature_t *reg) plugin_feature_t *dep)
{ {
char *str; enumerator_t *enumerator;
plugin_feature_t *features;
plugin_entry_t *entry;
int count, i;
str = plugin_feature_get_string(feature); enumerator = this->plugins->create_enumerator(this->plugins);
switch (feature->type) while (enumerator->enumerate(enumerator, &entry))
{ {
case FEATURE_CRYPTER: if (!entry->plugin->get_features)
case FEATURE_AEAD: { /* features not supported */
case FEATURE_SIGNER: continue;
case FEATURE_HASHER: }
case FEATURE_PRF: count = entry->plugin->get_features(entry->plugin, &features);
case FEATURE_DH: for (i = 0; i < count; i++)
case FEATURE_RNG: {
case FEATURE_PRIVKEY: if (feature_loaded(this, entry, &features[i]))
case FEATURE_PRIVKEY_GEN:
case FEATURE_PUBKEY:
case FEATURE_CERT_DECODE:
case FEATURE_CERT_ENCODE:
case FEATURE_DATABASE:
case FEATURE_FETCHER:
/* require a registration function */
if (!reg ||
(reg->kind == FEATURE_REGISTER && reg->type != feature->type))
{ {
DBG1(DBG_LIB, "loading '%s' plugin feature %s failed: " while (++i < count && (features[i].kind == FEATURE_DEPENDS ||
"invalid registration function", name, str); features[i].kind == FEATURE_SDEPEND))
free(str); {
return FALSE; if (plugin_feature_matches(&features[i], dep))
{
enumerator->destroy(enumerator);
return TRUE;
}
}
} }
break;
default:
break;
}
if (reg && reg->kind == FEATURE_CALLBACK)
{
if (!reg->arg.cb.f(entry->plugin, feature, TRUE, reg->arg.cb.data))
{
DBG1(DBG_LIB, "loading '%s' plugin feature %s with callback failed",
name, str);
free(str);
return FALSE;
} }
} }
else enumerator->destroy(enumerator);
{ return FALSE;
switch (feature->type)
{
case FEATURE_CRYPTER:
lib->crypto->add_crypter(lib->crypto, feature->arg.crypter.alg,
name, reg->arg.reg.f);
break;
case FEATURE_AEAD:
lib->crypto->add_aead(lib->crypto, feature->arg.aead.alg,
name, reg->arg.reg.f);
break;
case FEATURE_SIGNER:
lib->crypto->add_signer(lib->crypto, feature->arg.signer,
name, reg->arg.reg.f);
break;
case FEATURE_HASHER:
lib->crypto->add_hasher(lib->crypto, feature->arg.hasher,
name, reg->arg.reg.f);
break;
case FEATURE_PRF:
lib->crypto->add_prf(lib->crypto, feature->arg.prf,
name, reg->arg.reg.f);
break;
case FEATURE_DH:
lib->crypto->add_dh(lib->crypto, feature->arg.dh_group,
name, reg->arg.reg.f);
break;
case FEATURE_RNG:
lib->crypto->add_rng(lib->crypto, feature->arg.rng_quality,
name, reg->arg.reg.f);
break;
case FEATURE_PRIVKEY:
case FEATURE_PRIVKEY_GEN:
lib->creds->add_builder(lib->creds, CRED_PRIVATE_KEY,
feature->arg.privkey, reg->arg.reg.final,
reg->arg.reg.f);
break;
case FEATURE_PUBKEY:
lib->creds->add_builder(lib->creds, CRED_PUBLIC_KEY,
feature->arg.pubkey, reg->arg.reg.final,
reg->arg.reg.f);
break;
case FEATURE_CERT_DECODE:
case FEATURE_CERT_ENCODE:
lib->creds->add_builder(lib->creds, CRED_CERTIFICATE,
feature->arg.cert, reg->arg.reg.final,
reg->arg.reg.f);
break;
case FEATURE_DATABASE:
lib->db->add_database(lib->db, reg->arg.reg.f);
break;
case FEATURE_FETCHER:
lib->fetcher->add_fetcher(lib->fetcher, reg->arg.reg.f,
feature->arg.fetcher);
break;
default:
break;
}
}
DBG2(DBG_LIB, "loaded '%s' plugin feature %s", name, str);
free(str);
entry->loaded->insert_last(entry->loaded, feature);
return TRUE;
} }
/** /**
@@ -404,10 +331,9 @@ static bool load_feature(private_plugin_loader_t *this, plugin_entry_t *entry,
static int load_features(private_plugin_loader_t *this, bool soft, bool report) static int load_features(private_plugin_loader_t *this, bool soft, bool report)
{ {
enumerator_t *enumerator; enumerator_t *enumerator;
plugin_feature_t *features, *reg = NULL; plugin_feature_t *feature, *reg = NULL;
plugin_entry_t *entry; plugin_entry_t *entry;
int count, i, loaded = 0; int count, i, loaded = 0;
char *name;
enumerator = this->plugins->create_enumerator(this->plugins); enumerator = this->plugins->create_enumerator(this->plugins);
while (enumerator->enumerate(enumerator, &entry)) while (enumerator->enumerate(enumerator, &entry))
@@ -416,34 +342,69 @@ static int load_features(private_plugin_loader_t *this, bool soft, bool report)
{ /* feature interface not supported */ { /* feature interface not supported */
continue; continue;
} }
name = entry->plugin->get_name(entry->plugin); count = entry->plugin->get_features(entry->plugin, &feature);
count = entry->plugin->get_features(entry->plugin, &features);
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
{ {
switch (features[i].kind) switch (feature->kind)
{ {
case FEATURE_PROVIDE: case FEATURE_PROVIDE:
if (!feature_loaded(this, entry, &features[i]) && if (!feature_loaded(this, entry, feature) &&
dependencies_satisfied(this, name, soft, report, dependencies_satisfied(this, entry, soft, report,
&features[i], count - i) && feature, count - i) &&
load_feature(this, entry, name, &features[i], reg)) plugin_feature_load(entry->plugin, feature, reg))
{ {
entry->loaded->insert_last(entry->loaded, feature);
loaded++; loaded++;
} }
break; break;
case FEATURE_REGISTER: case FEATURE_REGISTER:
case FEATURE_CALLBACK: case FEATURE_CALLBACK:
reg = &features[i]; reg = feature;
break; break;
default: default:
break; break;
} }
feature++;
} }
} }
enumerator->destroy(enumerator); enumerator->destroy(enumerator);
return loaded; return loaded;
} }
/**
* 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;
}
/** /**
* Remove plugins that we were not able to load any features from. * Remove plugins that we were not able to load any features from.
*/ */
@@ -533,17 +494,40 @@ METHOD(plugin_loader_t, load_plugins, bool,
METHOD(plugin_loader_t, unload, void, METHOD(plugin_loader_t, unload, void,
private_plugin_loader_t *this) private_plugin_loader_t *this)
{ {
enumerator_t *enumerator;
plugin_entry_t *entry; plugin_entry_t *entry;
linked_list_t *list;
/* unload plugins in reverse order */ /* unload plugins in reverse order, for those not supporting features */
while (this->plugins->remove_last(this->plugins, list = linked_list_create();
(void**)&entry) == SUCCESS) while (this->plugins->remove_last(this->plugins, (void**)&entry) == SUCCESS)
{ {
if (lib->leak_detective) list->insert_last(list, entry);
{ /* keep handle to report leaks properly */ }
entry->handle = NULL; 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);
}
} }
plugin_entry_destroy(entry); enumerator->destroy(enumerator);
} }
} }