MXS-1929: Initialize filter in filter_alloc

Changed the filter_alloc function to fully initialize the filter. This
means that if filter_alloc returns a non-NULL pointer, the filter was
successfully loaded and an instance was successfully created.
This commit is contained in:
Markus Mäkelä 2018-07-24 08:39:57 +03:00
parent 382595a2b8
commit cea36dc7be
No known key found for this signature in database
GPG Key ID: 72D48FCE664F7B19
4 changed files with 32 additions and 65 deletions

View File

@ -58,6 +58,14 @@ static void filter_free_parameters(MXS_FILTER_DEF *filter);
*/
MXS_FILTER_DEF* filter_alloc(const char *name, const char *module, MXS_CONFIG_PARAMETER* params)
{
MXS_FILTER_OBJECT* object = (MXS_FILTER_OBJECT*)load_module(module, MODULE_FILTER);
if (object == NULL)
{
MXS_ERROR("Failed to load filter module '%s'.", module);
return NULL;
}
char* my_name = MXS_STRDUP(name);
char* my_module = MXS_STRDUP(module);
@ -72,8 +80,7 @@ MXS_FILTER_DEF* filter_alloc(const char *name, const char *module, MXS_CONFIG_PA
}
filter->name = my_name;
filter->module = my_module;
filter->filter = NULL;
filter->obj = NULL;
filter->obj = object;
filter->parameters = NULL;
spinlock_init(&filter->spin);
@ -82,6 +89,16 @@ MXS_FILTER_DEF* filter_alloc(const char *name, const char *module, MXS_CONFIG_PA
filter_add_parameter(filter, p->name, p->value);
}
if ((filter->filter = object->createInstance(name, params)) == NULL)
{
MXS_ERROR("Failed to create filter '%s' instance.", name);
filter_free_parameters(filter);
MXS_FREE(my_name);
MXS_FREE(my_module);
MXS_FREE(filter);
return NULL;
}
spinlock_acquire(&filter_spin);
filter->next = allFilters;
allFilters = filter;
@ -306,51 +323,6 @@ static void filter_free_parameters(MXS_FILTER_DEF *filter)
config_parameter_free(filter->parameters);
}
/**
* Load a filter module for use and create an instance of it for a service.
* @param filter Filter definition
* @return True if module was successfully loaded, false if an error occurred
*/
bool filter_load(MXS_FILTER_DEF* filter)
{
bool rval = false;
if (filter)
{
if (filter->filter)
{
// Already loaded and created.
rval = true;
}
else
{
if (filter->obj == NULL)
{
/* Filter not yet loaded */
if ((filter->obj = (MXS_FILTER_OBJECT*)load_module(filter->module, MODULE_FILTER)) == NULL)
{
MXS_ERROR("Failed to load filter module '%s'.", filter->module);
}
}
if (filter->obj)
{
ss_dassert(!filter->filter);
if ((filter->filter = (filter->obj->createInstance)(filter->name,
filter->parameters)))
{
rval = true;
}
else
{
MXS_ERROR("Failed to create filter '%s' instance.", filter->name);
}
}
}
}
return rval;
}
/**
* Connect the downstream filter chain for a filter.
*

View File

@ -40,7 +40,6 @@ void filter_add_parameter(MXS_FILTER_DEF *filter_def, const char *name, const ch
MXS_FILTER_DEF *filter_alloc(const char *name, const char *module, MXS_CONFIG_PARAMETER* params);
MXS_DOWNSTREAM *filter_apply(MXS_FILTER_DEF *filter_def, MXS_SESSION *session, MXS_DOWNSTREAM *downstream);
void filter_free(MXS_FILTER_DEF *filter_def);
bool filter_load(MXS_FILTER_DEF *filter_def);
int filter_standard_parameter(const char *name);
MXS_UPSTREAM *filter_upstream(MXS_FILTER_DEF *filter_def,
MXS_FILTER_SESSION *fsession,

View File

@ -1252,23 +1252,15 @@ bool service_set_filters(SERVICE* service, const char* filters)
if (MXS_FILTER_DEF* def = filter_def_find(f.c_str()))
{
if (filter_load(def))
{
flist.push_back(def);
flist.push_back(def);
const MXS_MODULE* module = get_module(def->module, MODULE_FILTER);
ss_dassert(module);
capabilities |= module->module_capabilities;
const MXS_MODULE* module = get_module(def->module, MODULE_FILTER);
ss_dassert(module);
capabilities |= module->module_capabilities;
if (def->obj->getCapabilities)
{
capabilities |= def->obj->getCapabilities(def->filter);
}
}
else
if (def->obj->getCapabilities)
{
MXS_ERROR("Failed to load filter '%s' for service '%s'.", f.c_str(), service->name);
rval = false;
capabilities |= def->obj->getCapabilities(def->filter);
}
}
else

View File

@ -33,6 +33,9 @@
#include <stdlib.h>
#include <string.h>
#include <maxscale/alloc.h>
#include <maxscale/paths.h>
#include "../internal/filter.h"
@ -45,7 +48,7 @@ test1()
{
MXS_FILTER_DEF *f1, *f2;
if ((f1 = filter_alloc("test1", "module", NULL)) == NULL)
if ((f1 = filter_alloc("test1", "qlafilter", NULL)) == NULL)
{
fprintf(stderr, "filter_alloc: test 1 failed.\n");
return 1;
@ -78,7 +81,7 @@ test2()
{
MXS_FILTER_DEF *f1;
if ((f1 = filter_alloc("test1", "module", NULL)) == NULL)
if ((f1 = filter_alloc("test1", "qlafilter", NULL)) == NULL)
{
fprintf(stderr, "filter_alloc: test 1 failed.\n");
return 1;
@ -106,7 +109,7 @@ test3()
for (i = 0; i < n_filters; i++)
{
sprintf(name, "filter%d", i);
if ((f1 = filter_alloc(name, "module", NULL)) == NULL)
if ((f1 = filter_alloc(name, "qlafilter", NULL)) == NULL)
{
fprintf(stderr,
"filter_alloc: test 3 failed with %s.\n", name);
@ -146,6 +149,7 @@ int
main(int argc, char **argv)
{
int result = 0;
set_libdir(MXS_STRDUP_A("../../modules/filter/qlafilter/"));
result += test1();
result += test2();