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:
@ -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_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_name = MXS_STRDUP(name);
|
||||||
char* my_module = MXS_STRDUP(module);
|
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->name = my_name;
|
||||||
filter->module = my_module;
|
filter->module = my_module;
|
||||||
filter->filter = NULL;
|
filter->obj = object;
|
||||||
filter->obj = NULL;
|
|
||||||
filter->parameters = NULL;
|
filter->parameters = NULL;
|
||||||
spinlock_init(&filter->spin);
|
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);
|
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);
|
spinlock_acquire(&filter_spin);
|
||||||
filter->next = allFilters;
|
filter->next = allFilters;
|
||||||
allFilters = filter;
|
allFilters = filter;
|
||||||
@ -306,51 +323,6 @@ static void filter_free_parameters(MXS_FILTER_DEF *filter)
|
|||||||
config_parameter_free(filter->parameters);
|
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.
|
* Connect the downstream filter chain for a filter.
|
||||||
*
|
*
|
||||||
|
@ -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_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);
|
MXS_DOWNSTREAM *filter_apply(MXS_FILTER_DEF *filter_def, MXS_SESSION *session, MXS_DOWNSTREAM *downstream);
|
||||||
void filter_free(MXS_FILTER_DEF *filter_def);
|
void filter_free(MXS_FILTER_DEF *filter_def);
|
||||||
bool filter_load(MXS_FILTER_DEF *filter_def);
|
|
||||||
int filter_standard_parameter(const char *name);
|
int filter_standard_parameter(const char *name);
|
||||||
MXS_UPSTREAM *filter_upstream(MXS_FILTER_DEF *filter_def,
|
MXS_UPSTREAM *filter_upstream(MXS_FILTER_DEF *filter_def,
|
||||||
MXS_FILTER_SESSION *fsession,
|
MXS_FILTER_SESSION *fsession,
|
||||||
|
@ -1251,8 +1251,6 @@ bool service_set_filters(SERVICE* service, const char* filters)
|
|||||||
fix_object_name(&f[0]);
|
fix_object_name(&f[0]);
|
||||||
|
|
||||||
if (MXS_FILTER_DEF* def = filter_def_find(f.c_str()))
|
if (MXS_FILTER_DEF* def = filter_def_find(f.c_str()))
|
||||||
{
|
|
||||||
if (filter_load(def))
|
|
||||||
{
|
{
|
||||||
flist.push_back(def);
|
flist.push_back(def);
|
||||||
|
|
||||||
@ -1266,12 +1264,6 @@ bool service_set_filters(SERVICE* service, const char* filters)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
|
||||||
MXS_ERROR("Failed to load filter '%s' for service '%s'.", f.c_str(), service->name);
|
|
||||||
rval = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
MXS_ERROR("Unable to find filter '%s' for service '%s'", f.c_str(), service->name);
|
MXS_ERROR("Unable to find filter '%s' for service '%s'", f.c_str(), service->name);
|
||||||
rval = false;
|
rval = false;
|
||||||
|
@ -33,6 +33,9 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <maxscale/alloc.h>
|
||||||
|
#include <maxscale/paths.h>
|
||||||
|
|
||||||
#include "../internal/filter.h"
|
#include "../internal/filter.h"
|
||||||
|
|
||||||
|
|
||||||
@ -45,7 +48,7 @@ test1()
|
|||||||
{
|
{
|
||||||
MXS_FILTER_DEF *f1, *f2;
|
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");
|
fprintf(stderr, "filter_alloc: test 1 failed.\n");
|
||||||
return 1;
|
return 1;
|
||||||
@ -78,7 +81,7 @@ test2()
|
|||||||
{
|
{
|
||||||
MXS_FILTER_DEF *f1;
|
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");
|
fprintf(stderr, "filter_alloc: test 1 failed.\n");
|
||||||
return 1;
|
return 1;
|
||||||
@ -106,7 +109,7 @@ test3()
|
|||||||
for (i = 0; i < n_filters; i++)
|
for (i = 0; i < n_filters; i++)
|
||||||
{
|
{
|
||||||
sprintf(name, "filter%d", i);
|
sprintf(name, "filter%d", i);
|
||||||
if ((f1 = filter_alloc(name, "module", NULL)) == NULL)
|
if ((f1 = filter_alloc(name, "qlafilter", NULL)) == NULL)
|
||||||
{
|
{
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"filter_alloc: test 3 failed with %s.\n", name);
|
"filter_alloc: test 3 failed with %s.\n", name);
|
||||||
@ -146,6 +149,7 @@ int
|
|||||||
main(int argc, char **argv)
|
main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int result = 0;
|
int result = 0;
|
||||||
|
set_libdir(MXS_STRDUP_A("../../modules/filter/qlafilter/"));
|
||||||
|
|
||||||
result += test1();
|
result += test1();
|
||||||
result += test2();
|
result += test2();
|
||||||
|
Reference in New Issue
Block a user