2013-01-25 14:01:04 +00:00
|
|
|
/*
|
|
|
|
* Asterisk -- An open source telephony toolkit.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 - 2013, Digium, Inc.
|
|
|
|
*
|
|
|
|
* Joshua Colp <jcolp@digium.com>
|
|
|
|
*
|
|
|
|
* See http://www.asterisk.org for more information about
|
|
|
|
* the Asterisk project. Please do not directly contact
|
|
|
|
* any of the maintainers of this project for assistance;
|
|
|
|
* the project provides a web site, mailing lists and IRC
|
|
|
|
* channels for your use.
|
|
|
|
*
|
|
|
|
* This program is free software, distributed under the terms of
|
|
|
|
* the GNU General Public License Version 2. See the LICENSE file
|
|
|
|
* at the top of the source tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \file
|
|
|
|
*
|
|
|
|
* \brief Sorcery Configuration File Object Wizard
|
|
|
|
*
|
|
|
|
* \author Joshua Colp <jcolp@digium.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*** MODULEINFO
|
|
|
|
<support_level>core</support_level>
|
|
|
|
***/
|
|
|
|
|
|
|
|
#include "asterisk.h"
|
|
|
|
|
2013-02-16 16:24:21 +00:00
|
|
|
#include <regex.h>
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
#include "asterisk/module.h"
|
|
|
|
#include "asterisk/sorcery.h"
|
|
|
|
#include "asterisk/astobj2.h"
|
|
|
|
#include "asterisk/config.h"
|
2013-02-07 17:57:10 +00:00
|
|
|
#include "asterisk/uuid.h"
|
2015-02-15 17:43:21 +00:00
|
|
|
#include "asterisk/hashtab.h"
|
2013-01-25 14:01:04 +00:00
|
|
|
|
|
|
|
/*! \brief Structure for storing configuration file sourced objects */
|
|
|
|
struct sorcery_config {
|
2013-02-07 17:57:10 +00:00
|
|
|
/*! \brief UUID for identifying us when opening a configuration file */
|
|
|
|
char uuid[AST_UUID_STR_LEN];
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
/*! \brief Objects retrieved from the configuration file */
|
|
|
|
struct ao2_global_obj objects;
|
|
|
|
|
|
|
|
/*! \brief Any specific variable criteria for considering a defined category for this object */
|
|
|
|
struct ast_variable *criteria;
|
|
|
|
|
|
|
|
/*! \brief Number of buckets to use for objects */
|
|
|
|
unsigned int buckets;
|
|
|
|
|
|
|
|
/*! \brief Enable file level integrity instead of object level */
|
|
|
|
unsigned int file_integrity:1;
|
|
|
|
|
|
|
|
/*! \brief Filename of the configuration file */
|
|
|
|
char filename[];
|
|
|
|
};
|
|
|
|
|
|
|
|
/*! \brief Structure used for fields comparison */
|
|
|
|
struct sorcery_config_fields_cmp_params {
|
|
|
|
/*! \brief Pointer to the sorcery structure */
|
|
|
|
const struct ast_sorcery *sorcery;
|
|
|
|
|
|
|
|
/*! \brief Pointer to the fields to check */
|
|
|
|
const struct ast_variable *fields;
|
|
|
|
|
2013-02-16 16:24:21 +00:00
|
|
|
/*! \brief Regular expression for checking object id */
|
|
|
|
regex_t *regex;
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
/*! \brief Optional container to put object into */
|
|
|
|
struct ao2_container *container;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void *sorcery_config_open(const char *data);
|
|
|
|
static void sorcery_config_load(void *data, const struct ast_sorcery *sorcery, const char *type);
|
|
|
|
static void sorcery_config_reload(void *data, const struct ast_sorcery *sorcery, const char *type);
|
|
|
|
static void *sorcery_config_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id);
|
|
|
|
static void *sorcery_config_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields);
|
|
|
|
static void sorcery_config_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects,
|
|
|
|
const struct ast_variable *fields);
|
2013-02-16 16:24:21 +00:00
|
|
|
static void sorcery_config_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex);
|
2013-01-25 14:01:04 +00:00
|
|
|
static void sorcery_config_close(void *data);
|
|
|
|
|
|
|
|
static struct ast_sorcery_wizard config_object_wizard = {
|
|
|
|
.name = "config",
|
|
|
|
.open = sorcery_config_open,
|
|
|
|
.load = sorcery_config_load,
|
|
|
|
.reload = sorcery_config_reload,
|
|
|
|
.retrieve_id = sorcery_config_retrieve_id,
|
|
|
|
.retrieve_fields = sorcery_config_retrieve_fields,
|
|
|
|
.retrieve_multiple = sorcery_config_retrieve_multiple,
|
2013-02-16 16:24:21 +00:00
|
|
|
.retrieve_regex = sorcery_config_retrieve_regex,
|
2013-01-25 14:01:04 +00:00
|
|
|
.close = sorcery_config_close,
|
|
|
|
};
|
|
|
|
|
|
|
|
/*! \brief Destructor function for sorcery config */
|
|
|
|
static void sorcery_config_destructor(void *obj)
|
|
|
|
{
|
|
|
|
struct sorcery_config *config = obj;
|
|
|
|
|
|
|
|
ao2_global_obj_release(config->objects);
|
|
|
|
ast_rwlock_destroy(&config->objects.lock);
|
|
|
|
ast_variables_destroy(config->criteria);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sorcery_config_fields_cmp(void *obj, void *arg, int flags)
|
|
|
|
{
|
|
|
|
const struct sorcery_config_fields_cmp_params *params = arg;
|
|
|
|
RAII_VAR(struct ast_variable *, objset, NULL, ast_variables_destroy);
|
|
|
|
|
2013-02-16 16:24:21 +00:00
|
|
|
if (params->regex) {
|
|
|
|
/* If a regular expression has been provided see if it matches, otherwise move on */
|
|
|
|
if (!regexec(params->regex, ast_sorcery_object_get_id(obj), 0, NULL, 0)) {
|
|
|
|
ao2_link(params->container, obj);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
} else if (params->fields &&
|
2013-01-25 14:01:04 +00:00
|
|
|
(!(objset = ast_sorcery_objectset_create(params->sorcery, obj)) ||
|
sorcery/res_pjsip: Refactor for realtime performance
There were a number of places in the res_pjsip stack that were getting
all endpoints or all aors, and then filtering them locally.
A good example is pjsip_options which, on startup, retrieves all
endpoints, then the aors for those endpoints, then tests the aors to see
if the qualify_frequency is > 0. One issue was that it never did
anything with the endpoints other than retrieve the aors so we probably
could have skipped a step and just retrieved all aors. But nevermind.
This worked reasonably well with local config files but with a realtime
backend and thousands of objects, this was a nightmare. The issue
really boiled down to the fact that while realtime supports predicates
that are passed to the database engine, the non-realtime sorcery
backends didn't.
They do now.
The realtime engines have a scheme for doing simple comparisons. They
take in an ast_variable (or list) for matching, and the name of each
variable can contain an operator. For instance, a name of
"qualify_frequency >" and a value of "0" would create a SQL predicate
that looks like "where qualify_frequency > '0'". If there's no operator
after the name, the engines add an '=' so a simple name of
"qualify_frequency" and a value of "10" would return exact matches.
The non-realtime backends decide whether to include an object in a
result set by calling ast_sorcery_changeset_create on every object in
the internal container. However, ast_sorcery_changeset_create only does
exact string matches though so a name of "qualify_frequency >" and a
value of "0" returns nothing because the literal "qualify_frequency >"
doesn't match any name in the objset set.
So, the real task was to create a generic string matcher that can take a
left value, operator and a right value and perform the match. To that
end, strings.c has a new ast_strings_match(left, operator, right)
function. Left and right are the strings to operate on and the operator
can be a string containing any of the following: = (or NULL or ""), !=,
>, >=, <, <=, like or regex. If the operator is like or regex, the
right string should be a %-pattern or a regex expression. If both left
and right can be converted to float, then a numeric comparison is
performed, otherwise a string comparison is performed.
To use this new function on ast_variables, 2 new functions were added to
config.c. One that compares 2 ast_variables, and one that compares 2
ast_variable lists. The former is useful when you want to compare 2
ast_variables that happen to be in a list but don't want to traverse the
list. The latter will traverse the right list and return true if all
the variables in it match the left list.
Now, the backends' fields_cmp functions call ast_variable_lists_match
instead of ast_sorcery_changeset_create and they can now process the
same syntax as the realtime engines. The realtime backend just passes
the variable list unaltered to the engine. The only gotcha is that
there's no common realtime engine support for regex so that's been noted
in the api docs for ast_sorcery_retrieve_by_fields.
Only one more change to sorcery was done... A new config flag
"allow_unqualified_fetch" was added to reg_sorcery_realtime.
"no": ignore fetches if no predicate fields were supplied.
"error": same as no but emit an error. (good for testing)
"yes": allow (the default);
"warn": allow but emit a warning. (good for testing)
Now on to res_pjsip...
pjsip_options was modified to retrieve aors with qualify_frequency > 0
rather than all endpoints then all aors. Not only was this a big
improvement in realtime retrieval but even for config files there's an
improvement because we're not going through endpoints anymore.
res_pjsip_mwi was modified to retieve only endpoints with something in
the mailboxes field instead of all endpoints then testing mailboxes.
res_pjsip_registrar_expire was completely refactored. It was retrieving
all contacts then setting up scheduler entries to check for expiration.
Now, it's a single thread (like keepalive) that periodically retrieves
only contacts whose expiration time is < now and deletes them. A new
contact_expiration_check_interval was added to global with a default of
30 seconds.
Ross Beer reports that with this patch, his Asterisk startup time dropped
from around an hour to under 30 seconds.
There are still objects that can't be filtered at the database like
identifies, transports, and registrations. These are not going to be
anywhere near as numerous as endpoints, aors, auths, contacts however.
Back to allow_unqualified_fetch. If this is set to yes and you have a
very large number of objects in the database, the pjsip CLI commands
will attempt to retrive ALL of them if not qualified with a LIKE.
Worse, if you type "pjsip show endpoint <tab>" guess what's going to
happen? :) Having a cache helps but all the objects will have to be
retrieved at least once to fill the cache. Setting
allow_unqualified_fetch=no prevents the mass retrieve and should be used
on endpoints, auths, aors, and contacts. It should NOT be used for
identifies, registrations and transports since these MUST be
retrieved in bulk.
Example sorcery.conf:
[res_pjsip]
endpoint=config,pjsip.conf,criteria=type=endpoint
endpoint=realtime,ps_endpoints,allow_unqualified_fetch=error
ASTERISK-25826 #close
Reported-by: Ross Beer
Tested-by: Ross Beer
Change-Id: Id2691e447db90892890036e663aaf907b2dc1c67
2016-03-08 21:55:30 +00:00
|
|
|
(!ast_variable_lists_match(objset, params->fields, 0)))) {
|
2013-02-16 16:24:21 +00:00
|
|
|
/* If we can't turn the object into an object set OR if differences exist between the fields
|
sorcery/res_pjsip: Refactor for realtime performance
There were a number of places in the res_pjsip stack that were getting
all endpoints or all aors, and then filtering them locally.
A good example is pjsip_options which, on startup, retrieves all
endpoints, then the aors for those endpoints, then tests the aors to see
if the qualify_frequency is > 0. One issue was that it never did
anything with the endpoints other than retrieve the aors so we probably
could have skipped a step and just retrieved all aors. But nevermind.
This worked reasonably well with local config files but with a realtime
backend and thousands of objects, this was a nightmare. The issue
really boiled down to the fact that while realtime supports predicates
that are passed to the database engine, the non-realtime sorcery
backends didn't.
They do now.
The realtime engines have a scheme for doing simple comparisons. They
take in an ast_variable (or list) for matching, and the name of each
variable can contain an operator. For instance, a name of
"qualify_frequency >" and a value of "0" would create a SQL predicate
that looks like "where qualify_frequency > '0'". If there's no operator
after the name, the engines add an '=' so a simple name of
"qualify_frequency" and a value of "10" would return exact matches.
The non-realtime backends decide whether to include an object in a
result set by calling ast_sorcery_changeset_create on every object in
the internal container. However, ast_sorcery_changeset_create only does
exact string matches though so a name of "qualify_frequency >" and a
value of "0" returns nothing because the literal "qualify_frequency >"
doesn't match any name in the objset set.
So, the real task was to create a generic string matcher that can take a
left value, operator and a right value and perform the match. To that
end, strings.c has a new ast_strings_match(left, operator, right)
function. Left and right are the strings to operate on and the operator
can be a string containing any of the following: = (or NULL or ""), !=,
>, >=, <, <=, like or regex. If the operator is like or regex, the
right string should be a %-pattern or a regex expression. If both left
and right can be converted to float, then a numeric comparison is
performed, otherwise a string comparison is performed.
To use this new function on ast_variables, 2 new functions were added to
config.c. One that compares 2 ast_variables, and one that compares 2
ast_variable lists. The former is useful when you want to compare 2
ast_variables that happen to be in a list but don't want to traverse the
list. The latter will traverse the right list and return true if all
the variables in it match the left list.
Now, the backends' fields_cmp functions call ast_variable_lists_match
instead of ast_sorcery_changeset_create and they can now process the
same syntax as the realtime engines. The realtime backend just passes
the variable list unaltered to the engine. The only gotcha is that
there's no common realtime engine support for regex so that's been noted
in the api docs for ast_sorcery_retrieve_by_fields.
Only one more change to sorcery was done... A new config flag
"allow_unqualified_fetch" was added to reg_sorcery_realtime.
"no": ignore fetches if no predicate fields were supplied.
"error": same as no but emit an error. (good for testing)
"yes": allow (the default);
"warn": allow but emit a warning. (good for testing)
Now on to res_pjsip...
pjsip_options was modified to retrieve aors with qualify_frequency > 0
rather than all endpoints then all aors. Not only was this a big
improvement in realtime retrieval but even for config files there's an
improvement because we're not going through endpoints anymore.
res_pjsip_mwi was modified to retieve only endpoints with something in
the mailboxes field instead of all endpoints then testing mailboxes.
res_pjsip_registrar_expire was completely refactored. It was retrieving
all contacts then setting up scheduler entries to check for expiration.
Now, it's a single thread (like keepalive) that periodically retrieves
only contacts whose expiration time is < now and deletes them. A new
contact_expiration_check_interval was added to global with a default of
30 seconds.
Ross Beer reports that with this patch, his Asterisk startup time dropped
from around an hour to under 30 seconds.
There are still objects that can't be filtered at the database like
identifies, transports, and registrations. These are not going to be
anywhere near as numerous as endpoints, aors, auths, contacts however.
Back to allow_unqualified_fetch. If this is set to yes and you have a
very large number of objects in the database, the pjsip CLI commands
will attempt to retrive ALL of them if not qualified with a LIKE.
Worse, if you type "pjsip show endpoint <tab>" guess what's going to
happen? :) Having a cache helps but all the objects will have to be
retrieved at least once to fill the cache. Setting
allow_unqualified_fetch=no prevents the mass retrieve and should be used
on endpoints, auths, aors, and contacts. It should NOT be used for
identifies, registrations and transports since these MUST be
retrieved in bulk.
Example sorcery.conf:
[res_pjsip]
endpoint=config,pjsip.conf,criteria=type=endpoint
endpoint=realtime,ps_endpoints,allow_unqualified_fetch=error
ASTERISK-25826 #close
Reported-by: Ross Beer
Tested-by: Ross Beer
Change-Id: Id2691e447db90892890036e663aaf907b2dc1c67
2016-03-08 21:55:30 +00:00
|
|
|
* passed in and what are present on the object they are not a match.
|
|
|
|
*/
|
2013-01-25 14:01:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-08-05 01:00:59 +00:00
|
|
|
/* We want this object */
|
2013-01-25 14:01:04 +00:00
|
|
|
if (params->container) {
|
2016-08-05 01:00:59 +00:00
|
|
|
/*
|
|
|
|
* We are putting the found objects into the given container instead
|
|
|
|
* of the normal container traversal return mechanism.
|
|
|
|
*/
|
2013-01-25 14:01:04 +00:00
|
|
|
ao2_link(params->container, obj);
|
|
|
|
return 0;
|
|
|
|
} else {
|
2016-08-05 01:00:59 +00:00
|
|
|
return CMP_MATCH;
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *sorcery_config_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields)
|
|
|
|
{
|
|
|
|
struct sorcery_config *config = data;
|
|
|
|
RAII_VAR(struct ao2_container *, objects, ao2_global_obj_ref(config->objects), ao2_cleanup);
|
|
|
|
struct sorcery_config_fields_cmp_params params = {
|
|
|
|
.sorcery = sorcery,
|
|
|
|
.fields = fields,
|
|
|
|
.container = NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* If no fields are present return nothing, we require *something*, same goes if no objects exist yet */
|
|
|
|
if (!objects || !fields) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ao2_callback(objects, 0, sorcery_config_fields_cmp, ¶ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *sorcery_config_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id)
|
|
|
|
{
|
|
|
|
struct sorcery_config *config = data;
|
|
|
|
RAII_VAR(struct ao2_container *, objects, ao2_global_obj_ref(config->objects), ao2_cleanup);
|
|
|
|
|
2016-08-05 01:00:59 +00:00
|
|
|
return objects ? ao2_find(objects, id, OBJ_SEARCH_KEY) : NULL;
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void sorcery_config_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const struct ast_variable *fields)
|
|
|
|
{
|
|
|
|
struct sorcery_config *config = data;
|
|
|
|
RAII_VAR(struct ao2_container *, config_objects, ao2_global_obj_ref(config->objects), ao2_cleanup);
|
|
|
|
struct sorcery_config_fields_cmp_params params = {
|
|
|
|
.sorcery = sorcery,
|
|
|
|
.fields = fields,
|
|
|
|
.container = objects,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!config_objects) {
|
|
|
|
return;
|
|
|
|
}
|
sorcery/res_pjsip: Refactor for realtime performance
There were a number of places in the res_pjsip stack that were getting
all endpoints or all aors, and then filtering them locally.
A good example is pjsip_options which, on startup, retrieves all
endpoints, then the aors for those endpoints, then tests the aors to see
if the qualify_frequency is > 0. One issue was that it never did
anything with the endpoints other than retrieve the aors so we probably
could have skipped a step and just retrieved all aors. But nevermind.
This worked reasonably well with local config files but with a realtime
backend and thousands of objects, this was a nightmare. The issue
really boiled down to the fact that while realtime supports predicates
that are passed to the database engine, the non-realtime sorcery
backends didn't.
They do now.
The realtime engines have a scheme for doing simple comparisons. They
take in an ast_variable (or list) for matching, and the name of each
variable can contain an operator. For instance, a name of
"qualify_frequency >" and a value of "0" would create a SQL predicate
that looks like "where qualify_frequency > '0'". If there's no operator
after the name, the engines add an '=' so a simple name of
"qualify_frequency" and a value of "10" would return exact matches.
The non-realtime backends decide whether to include an object in a
result set by calling ast_sorcery_changeset_create on every object in
the internal container. However, ast_sorcery_changeset_create only does
exact string matches though so a name of "qualify_frequency >" and a
value of "0" returns nothing because the literal "qualify_frequency >"
doesn't match any name in the objset set.
So, the real task was to create a generic string matcher that can take a
left value, operator and a right value and perform the match. To that
end, strings.c has a new ast_strings_match(left, operator, right)
function. Left and right are the strings to operate on and the operator
can be a string containing any of the following: = (or NULL or ""), !=,
>, >=, <, <=, like or regex. If the operator is like or regex, the
right string should be a %-pattern or a regex expression. If both left
and right can be converted to float, then a numeric comparison is
performed, otherwise a string comparison is performed.
To use this new function on ast_variables, 2 new functions were added to
config.c. One that compares 2 ast_variables, and one that compares 2
ast_variable lists. The former is useful when you want to compare 2
ast_variables that happen to be in a list but don't want to traverse the
list. The latter will traverse the right list and return true if all
the variables in it match the left list.
Now, the backends' fields_cmp functions call ast_variable_lists_match
instead of ast_sorcery_changeset_create and they can now process the
same syntax as the realtime engines. The realtime backend just passes
the variable list unaltered to the engine. The only gotcha is that
there's no common realtime engine support for regex so that's been noted
in the api docs for ast_sorcery_retrieve_by_fields.
Only one more change to sorcery was done... A new config flag
"allow_unqualified_fetch" was added to reg_sorcery_realtime.
"no": ignore fetches if no predicate fields were supplied.
"error": same as no but emit an error. (good for testing)
"yes": allow (the default);
"warn": allow but emit a warning. (good for testing)
Now on to res_pjsip...
pjsip_options was modified to retrieve aors with qualify_frequency > 0
rather than all endpoints then all aors. Not only was this a big
improvement in realtime retrieval but even for config files there's an
improvement because we're not going through endpoints anymore.
res_pjsip_mwi was modified to retieve only endpoints with something in
the mailboxes field instead of all endpoints then testing mailboxes.
res_pjsip_registrar_expire was completely refactored. It was retrieving
all contacts then setting up scheduler entries to check for expiration.
Now, it's a single thread (like keepalive) that periodically retrieves
only contacts whose expiration time is < now and deletes them. A new
contact_expiration_check_interval was added to global with a default of
30 seconds.
Ross Beer reports that with this patch, his Asterisk startup time dropped
from around an hour to under 30 seconds.
There are still objects that can't be filtered at the database like
identifies, transports, and registrations. These are not going to be
anywhere near as numerous as endpoints, aors, auths, contacts however.
Back to allow_unqualified_fetch. If this is set to yes and you have a
very large number of objects in the database, the pjsip CLI commands
will attempt to retrive ALL of them if not qualified with a LIKE.
Worse, if you type "pjsip show endpoint <tab>" guess what's going to
happen? :) Having a cache helps but all the objects will have to be
retrieved at least once to fill the cache. Setting
allow_unqualified_fetch=no prevents the mass retrieve and should be used
on endpoints, auths, aors, and contacts. It should NOT be used for
identifies, registrations and transports since these MUST be
retrieved in bulk.
Example sorcery.conf:
[res_pjsip]
endpoint=config,pjsip.conf,criteria=type=endpoint
endpoint=realtime,ps_endpoints,allow_unqualified_fetch=error
ASTERISK-25826 #close
Reported-by: Ross Beer
Tested-by: Ross Beer
Change-Id: Id2691e447db90892890036e663aaf907b2dc1c67
2016-03-08 21:55:30 +00:00
|
|
|
|
2016-08-05 01:00:59 +00:00
|
|
|
ao2_callback(config_objects, OBJ_NODATA | OBJ_MULTIPLE, sorcery_config_fields_cmp, ¶ms);
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
2013-02-16 16:24:21 +00:00
|
|
|
static void sorcery_config_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex)
|
|
|
|
{
|
|
|
|
struct sorcery_config *config = data;
|
|
|
|
RAII_VAR(struct ao2_container *, config_objects, ao2_global_obj_ref(config->objects), ao2_cleanup);
|
|
|
|
regex_t expression;
|
|
|
|
struct sorcery_config_fields_cmp_params params = {
|
|
|
|
.sorcery = sorcery,
|
|
|
|
.container = objects,
|
|
|
|
.regex = &expression,
|
|
|
|
};
|
|
|
|
|
2016-08-01 16:08:15 +00:00
|
|
|
if (ast_strlen_zero(regex)) {
|
|
|
|
regex = ".";
|
|
|
|
}
|
|
|
|
|
2013-02-16 16:24:21 +00:00
|
|
|
if (!config_objects || regcomp(&expression, regex, REG_EXTENDED | REG_NOSUB)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-05 01:00:59 +00:00
|
|
|
ao2_callback(config_objects, OBJ_NODATA | OBJ_MULTIPLE, sorcery_config_fields_cmp, ¶ms);
|
2013-02-16 16:24:21 +00:00
|
|
|
regfree(&expression);
|
|
|
|
}
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
/*! \brief Internal function which determines if criteria has been met for considering an object set applicable */
|
|
|
|
static int sorcery_is_criteria_met(struct ast_variable *objset, struct ast_variable *criteria)
|
|
|
|
{
|
|
|
|
RAII_VAR(struct ast_variable *, diff, NULL, ast_variables_destroy);
|
|
|
|
|
|
|
|
return (!criteria || (!ast_sorcery_changeset_create(objset, criteria, &diff) && !diff)) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sorcery_config_internal_load(void *data, const struct ast_sorcery *sorcery, const char *type, unsigned int reload)
|
|
|
|
{
|
|
|
|
struct sorcery_config *config = data;
|
|
|
|
struct ast_flags flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
|
2013-02-07 17:57:10 +00:00
|
|
|
struct ast_config *cfg = ast_config_load2(config->filename, config->uuid, flags);
|
2014-10-13 16:12:17 +00:00
|
|
|
struct ast_category *category = NULL;
|
2013-01-25 14:01:04 +00:00
|
|
|
RAII_VAR(struct ao2_container *, objects, NULL, ao2_cleanup);
|
|
|
|
const char *id = NULL;
|
2015-02-15 17:43:21 +00:00
|
|
|
unsigned int buckets = 0;
|
2013-01-25 14:01:04 +00:00
|
|
|
|
|
|
|
if (!cfg) {
|
2013-01-25 14:23:46 +00:00
|
|
|
ast_log(LOG_ERROR, "Unable to load config file '%s'\n", config->filename);
|
2013-01-25 14:01:04 +00:00
|
|
|
return;
|
|
|
|
} else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
|
|
|
|
ast_debug(1, "Config file '%s' was unchanged\n", config->filename);
|
|
|
|
return;
|
|
|
|
} else if (cfg == CONFIG_STATUS_FILEINVALID) {
|
|
|
|
ast_log(LOG_ERROR, "Contents of config file '%s' are invalid and cannot be parsed\n", config->filename);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-15 17:43:21 +00:00
|
|
|
if (!config->buckets) {
|
|
|
|
while ((category = ast_category_browse_filtered(cfg, NULL, category, NULL))) {
|
|
|
|
|
|
|
|
/* If given criteria has not been met skip the category, it is not applicable */
|
|
|
|
if (!sorcery_is_criteria_met(ast_category_first(category), config->criteria)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
buckets++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine the optimal number of buckets */
|
|
|
|
while (buckets && !ast_is_prime(buckets)) {
|
|
|
|
/* This purposely goes backwards to ensure that the container doesn't have a ton of
|
|
|
|
* empty buckets for objects that will never get added.
|
|
|
|
*/
|
|
|
|
buckets--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!buckets) {
|
|
|
|
buckets = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
buckets = config->buckets;
|
|
|
|
}
|
|
|
|
|
|
|
|
ast_debug(2, "Using bucket size of '%d' for objects of type '%s' from '%s'\n",
|
|
|
|
buckets, type, config->filename);
|
|
|
|
|
2016-08-05 01:00:59 +00:00
|
|
|
objects = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, buckets,
|
|
|
|
ast_sorcery_object_id_hash, NULL, ast_sorcery_object_id_compare);
|
|
|
|
if (!objects) {
|
2013-01-25 14:01:04 +00:00
|
|
|
ast_log(LOG_ERROR, "Could not create bucket for new objects from '%s', keeping existing objects\n",
|
|
|
|
config->filename);
|
|
|
|
ast_config_destroy(cfg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-13 16:12:17 +00:00
|
|
|
while ((category = ast_category_browse_filtered(cfg, NULL, category, NULL))) {
|
2013-01-25 14:01:04 +00:00
|
|
|
RAII_VAR(void *, obj, NULL, ao2_cleanup);
|
2014-10-13 16:12:17 +00:00
|
|
|
id = ast_category_get_name(category);
|
2013-01-25 14:01:04 +00:00
|
|
|
|
|
|
|
/* If given criteria has not been met skip the category, it is not applicable */
|
2014-10-13 16:12:17 +00:00
|
|
|
if (!sorcery_is_criteria_met(ast_category_first(category), config->criteria)) {
|
2013-01-25 14:01:04 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-04-28 05:29:42 +00:00
|
|
|
/* Confirm an object with this id does not already exist in the bucket.
|
|
|
|
* If it exists, however, the configuration is invalid so stop
|
|
|
|
* processing and destroy it. */
|
2016-08-05 01:00:59 +00:00
|
|
|
obj = ao2_find(objects, id, OBJ_SEARCH_KEY);
|
2015-04-28 05:29:42 +00:00
|
|
|
if (obj) {
|
|
|
|
ast_log(LOG_ERROR, "Config file '%s' could not be loaded; configuration contains a duplicate object: '%s' of type '%s'\n",
|
|
|
|
config->filename, id, type);
|
|
|
|
ast_config_destroy(cfg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
if (!(obj = ast_sorcery_alloc(sorcery, type, id)) ||
|
2014-10-13 16:12:17 +00:00
|
|
|
ast_sorcery_objectset_apply(sorcery, obj, ast_category_first(category))) {
|
2013-01-25 14:01:04 +00:00
|
|
|
|
|
|
|
if (config->file_integrity) {
|
|
|
|
ast_log(LOG_ERROR, "Config file '%s' could not be loaded due to error with object '%s' of type '%s'\n",
|
|
|
|
config->filename, id, type);
|
|
|
|
ast_config_destroy(cfg);
|
|
|
|
return;
|
2013-12-17 18:35:39 +00:00
|
|
|
} else {
|
|
|
|
ast_log(LOG_ERROR, "Could not create an object of type '%s' with id '%s' from configuration file '%s'\n",
|
|
|
|
type, id, config->filename);
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ao2_cleanup(obj);
|
|
|
|
|
|
|
|
/* To ensure we don't lose the object that already exists we retrieve it from the old objects container and add it to the new one */
|
|
|
|
if (!(obj = sorcery_config_retrieve_id(sorcery, data, type, id))) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-12-12 14:04:06 +00:00
|
|
|
|
|
|
|
ast_log(LOG_NOTICE, "Retaining existing configuration for object of type '%s' with id '%s'\n", type, id);
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
2015-02-15 17:43:21 +00:00
|
|
|
ao2_link(objects, obj);
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ao2_global_obj_replace_unref(config->objects, objects);
|
|
|
|
ast_config_destroy(cfg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sorcery_config_load(void *data, const struct ast_sorcery *sorcery, const char *type)
|
|
|
|
{
|
|
|
|
sorcery_config_internal_load(data, sorcery, type, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sorcery_config_reload(void *data, const struct ast_sorcery *sorcery, const char *type)
|
|
|
|
{
|
|
|
|
sorcery_config_internal_load(data, sorcery, type, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *sorcery_config_open(const char *data)
|
|
|
|
{
|
2015-07-19 14:11:18 +00:00
|
|
|
char *tmp;
|
|
|
|
char *filename;
|
|
|
|
char *option;
|
2013-01-25 14:01:04 +00:00
|
|
|
struct sorcery_config *config;
|
|
|
|
|
2015-07-19 14:11:18 +00:00
|
|
|
if (ast_strlen_zero(data)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = ast_strdupa(data);
|
|
|
|
filename = strsep(&tmp, ",");
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
if (ast_strlen_zero(filename) || !(config = ao2_alloc_options(sizeof(*config) + strlen(filename) + 1, sorcery_config_destructor, AO2_ALLOC_OPT_LOCK_NOLOCK))) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-03-28 23:59:20 +00:00
|
|
|
ast_uuid_generate_str(config->uuid, sizeof(config->uuid));
|
2013-02-07 17:57:10 +00:00
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
ast_rwlock_init(&config->objects.lock);
|
|
|
|
strcpy(config->filename, filename);
|
|
|
|
|
|
|
|
while ((option = strsep(&tmp, ","))) {
|
|
|
|
char *name = strsep(&option, "="), *value = option;
|
|
|
|
|
|
|
|
if (!strcasecmp(name, "buckets")) {
|
2014-05-09 22:49:26 +00:00
|
|
|
if (sscanf(value, "%30u", &config->buckets) != 1) {
|
2015-02-15 17:43:21 +00:00
|
|
|
ast_log(LOG_ERROR, "Unsupported bucket size of '%s' used for configuration file '%s', defaulting to automatic determination\n",
|
|
|
|
value, filename);
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
} else if (!strcasecmp(name, "integrity")) {
|
|
|
|
if (!strcasecmp(value, "file")) {
|
|
|
|
config->file_integrity = 1;
|
|
|
|
} else if (!strcasecmp(value, "object")) {
|
|
|
|
config->file_integrity = 0;
|
|
|
|
} else {
|
|
|
|
ast_log(LOG_ERROR, "Unsupported integrity value of '%s' used for configuration file '%s', defaulting to 'object'\n",
|
|
|
|
value, filename);
|
|
|
|
}
|
|
|
|
} else if (!strcasecmp(name, "criteria")) {
|
|
|
|
char *field = strsep(&value, "=");
|
|
|
|
struct ast_variable *criteria = ast_variable_new(field, value, "");
|
|
|
|
|
|
|
|
if (criteria) {
|
|
|
|
criteria->next = config->criteria;
|
|
|
|
config->criteria = criteria;
|
|
|
|
} else {
|
|
|
|
/* This is fatal since not following criteria would potentially yield invalid objects */
|
|
|
|
ast_log(LOG_ERROR, "Could not create criteria entry of field '%s' with value '%s' for configuration file '%s'\n",
|
|
|
|
field, value, filename);
|
|
|
|
ao2_ref(config, -1);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ast_log(LOG_ERROR, "Unsupported option '%s' used for configuration file '%s'\n", name, filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sorcery_config_close(void *data)
|
|
|
|
{
|
|
|
|
struct sorcery_config *config = data;
|
|
|
|
|
|
|
|
ao2_ref(config, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int load_module(void)
|
|
|
|
{
|
|
|
|
if (ast_sorcery_wizard_register(&config_object_wizard)) {
|
|
|
|
return AST_MODULE_LOAD_DECLINE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return AST_MODULE_LOAD_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int unload_module(void)
|
|
|
|
{
|
|
|
|
ast_sorcery_wizard_unregister(&config_object_wizard);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-25 20:46:09 +00:00
|
|
|
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Sorcery Configuration File Object Wizard",
|
2014-07-25 16:47:17 +00:00
|
|
|
.support_level = AST_MODULE_SUPPORT_CORE,
|
2013-01-25 14:01:04 +00:00
|
|
|
.load = load_module,
|
|
|
|
.unload = unload_module,
|
2013-03-07 21:14:18 +00:00
|
|
|
.load_pri = AST_MODPRI_REALTIME_DRIVER,
|
2013-01-25 14:01:04 +00:00
|
|
|
);
|