aboutsummaryrefslogtreecommitdiffstats
path: root/main/pbx.c
diff options
context:
space:
mode:
authormurf <murf@f38db490-d61c-443f-a65b-d21fe96a405b>2008-07-15 13:14:07 +0000
committermurf <murf@f38db490-d61c-443f-a65b-d21fe96a405b>2008-07-15 13:14:07 +0000
commit0fe0bac06aee27353c279009b1c2dc74b20043c2 (patch)
treebdef23b6cbba2974e714c2b74c02ec455e70f1b2 /main/pbx.c
parent560edcd1346f6ecaf7f39c48bb096bcaf039d5a8 (diff)
Merged revisions 130145 via svnmerge from
https://origsvn.digium.com/svn/asterisk/trunk Merging this rev from trunk to 1.6.0 was not simple. Why? Because we've enhanced trunk to do a [fast] merge-and-delete operation which also solved problems with contexts having entries from different registrars. Fast as in the amount of time the contexts are locked down. That *is* fast, but traversing the entire dialplan looking for priorities to delete takes more time overall. This particular fix involved pulling in those enhancements from trunk, along with all the various fixes and refinements made along the way. Merging all this from trunk into 1.6 involved: a. mergetrunk6 in the stuff from 130145; b. revert all but the prop changes c. catalog all revisions to pbx.c since 1.6.0 was forked (at rev 105596). d. catalog all revisions to pbx.c in trunk since 1.6.0 was forked, making special note of all revs that were not merged into 1.6.0. e. study each rev in trunk not applied to 1.6.0, and determine if it was involved in the merge_and_delete enhancements in trunk. 25 commits were done in 1.6.0, all but one (106306) was a merge from trunk. Trunk had 22 additional changes, of which 7 were involved in the merge_and_delete enhancements: 106757 108894 109169 116461 123358 130145 130297 f. Go to trunk and collect patches, one by one, of the changes made by each rev across the entire source tree, using svn diff -c <num> > pfile g. Apply each patch in order to 1.6.0, and resolve all failures and compilation problems before proceding to the next patch. h. test the stuff. i. profit! ........ r130145 | murf | 2008-07-11 12:24:31 -0600 (Fri, 11 Jul 2008) | 40 lines (closes issue #13041) Reported by: eliel Tested by: murf (closes issue #12960) Reported by: mnicholson In this 'omnibus' fix, I **think** I solved both the problem in 13041, where unloading pbx_ael.so caused crashes, or incomplete removal of previous registrar'ed entries. And I added code to completely remove all includes, switches, and ignorepats that had a matching registrar entry, which should appease 12960. I also added a lot of seemingly useless brackets around single statement if's, which helped debug so much that I'm leaving them there. I added a routine to check the correlation between the extension tree lists and the hashtab tables. It can be amazingly helpful when you have lots of dialplan stuff, and need to narrow down where a problem is occurring. It's ifdef'd out by default. I cleaned up the code around the new CIDmatch code. It was leaving hanging extens with bad ptrs, getting confused over which objects to remove, etc. I tightened up the code and changed the call to remove_exten in the merge_and_delete code. I added more conditions to check for empty context worthy of deletion. It's not empty if there are any includes, switches, or ignorepats present. If I've missed anything, please re-open this bug, and be prepared to supply example dialplan code. ........ git-svn-id: http://svn.digium.com/svn/asterisk/branches/1.6.0@130946 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'main/pbx.c')
-rw-r--r--main/pbx.c949
1 files changed, 718 insertions, 231 deletions
diff --git a/main/pbx.c b/main/pbx.c
index b847e9031..7de2e46f6 100644
--- a/main/pbx.c
+++ b/main/pbx.c
@@ -48,6 +48,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/cdr.h"
#include "asterisk/config.h"
#include "asterisk/term.h"
+#include "asterisk/time.h"
#include "asterisk/manager.h"
#include "asterisk/ast_expr.h"
#include "asterisk/linkedlists.h"
@@ -142,8 +143,8 @@ struct ast_exten {
void *data; /*!< Data to use (arguments) */
void (*datad)(void *); /*!< Data destructor */
struct ast_exten *peer; /*!< Next higher priority with our extension */
- struct ast_hashtab *peer_tree; /*!< Priorities list in tree form -- only on the head of the peer list */
- struct ast_hashtab *peer_label_tree; /*!< labeled priorities in the peer list -- only on the head of the peer list */
+ struct ast_hashtab *peer_table; /*!< Priorities list in hashtab form -- only on the head of the peer list */
+ struct ast_hashtab *peer_label_table; /*!< labeled priorities in the peers -- only on the head of the peer list */
const char *registrar; /*!< Registrar */
struct ast_exten *next; /*!< Extension with a greater ID */
char stuff[0];
@@ -204,12 +205,13 @@ struct scoreboard /* make sure all fields are 0 before calling new_find_extensi
struct ast_context {
ast_rwlock_t lock; /*!< A lock to prevent multiple threads from clobbering the context */
struct ast_exten *root; /*!< The root of the list of extensions */
- struct ast_hashtab *root_tree; /*!< For exact matches on the extensions in the pattern tree, and for traversals of the pattern_tree */
+ struct ast_hashtab *root_table; /*!< For exact matches on the extensions in the pattern tree, and for traversals of the pattern_tree */
struct match_char *pattern_tree; /*!< A tree to speed up extension pattern matching */
struct ast_context *next; /*!< Link them together */
struct ast_include *includes; /*!< Include other contexts */
struct ast_ignorepat *ignorepats; /*!< Patterns for which to continue playing dialtone */
- const char *registrar; /*!< Registrar */
+ char *registrar; /*!< Registrar -- make sure you malloc this, as the registrar may have to survive module unloads */
+ int refcount; /*!< each module that would have created this context should inc/dec this as appropriate */
AST_LIST_HEAD_NOLOCK(, ast_sw) alts; /*!< Alternative switches */
ast_mutex_t macrolock; /*!< A lock to implement "exclusive" macros - held whilst a call is executing in the macro */
char name[0]; /*!< Name of the context */
@@ -329,14 +331,15 @@ static struct match_char *add_pattern_node(struct ast_context *con, struct match
static void create_match_char_tree(struct ast_context *con);
static struct ast_exten *get_canmatch_exten(struct match_char *node);
static void destroy_pattern_tree(struct match_char *pattern_tree);
-static int hashtab_compare_contexts(const void *ah_a, const void *ah_b);
+int ast_hashtab_compare_contexts(const void *ah_a, const void *ah_b);
static int hashtab_compare_extens(const void *ha_a, const void *ah_b);
static int hashtab_compare_exten_numbers(const void *ah_a, const void *ah_b);
static int hashtab_compare_exten_labels(const void *ah_a, const void *ah_b);
-static unsigned int hashtab_hash_contexts(const void *obj);
+unsigned int ast_hashtab_hash_contexts(const void *obj);
static unsigned int hashtab_hash_extens(const void *obj);
static unsigned int hashtab_hash_priority(const void *obj);
static unsigned int hashtab_hash_labels(const void *obj);
+static void __ast_internal_context_destroy( struct ast_context *con);
/* a func for qsort to use to sort a char array */
static int compare_char(const void *a, const void *b)
@@ -352,7 +355,7 @@ static int compare_char(const void *a, const void *b)
}
/* labels, contexts are case sensitive priority numbers are ints */
-static int hashtab_compare_contexts(const void *ah_a, const void *ah_b)
+int ast_hashtab_compare_contexts(const void *ah_a, const void *ah_b)
{
const struct ast_context *ac = ah_a;
const struct ast_context *bc = ah_b;
@@ -365,8 +368,10 @@ static int hashtab_compare_extens(const void *ah_a, const void *ah_b)
const struct ast_exten *ac = ah_a;
const struct ast_exten *bc = ah_b;
int x = strcmp(ac->exten, bc->exten);
- if (x) /* if exten names are diff, then return */
+ if (x) { /* if exten names are diff, then return */
return x;
+ }
+
/* but if they are the same, do the cidmatch values match? */
if (ac->matchcid && bc->matchcid) {
return strcmp(ac->cidmatch,bc->cidmatch);
@@ -391,7 +396,7 @@ static int hashtab_compare_exten_labels(const void *ah_a, const void *ah_b)
return strcmp(ac->label, bc->label);
}
-static unsigned int hashtab_hash_contexts(const void *obj)
+unsigned int ast_hashtab_hash_contexts(const void *obj)
{
const struct ast_context *ac = obj;
return ast_hashtab_hash_string(ac->name);
@@ -708,7 +713,7 @@ static struct pbx_builtin {
};
static struct ast_context *contexts;
-static struct ast_hashtab *contexts_tree = NULL;
+static struct ast_hashtab *contexts_table = NULL;
AST_RWLOCK_DEFINE_STATIC(conlock); /*!< Lock for the ast_context list */
@@ -726,6 +731,185 @@ static int stateid = 1;
static AST_RWLIST_HEAD_STATIC(hints, ast_hint);
struct ast_state_cb *statecbs;
+#ifdef CONTEXT_DEBUG
+
+/* these routines are provided for doing run-time checks
+ on the extension structures, in case you are having
+ problems, this routine might help you localize where
+ the problem is occurring. It's kinda like a debug memory
+ allocator's arena checker... It'll eat up your cpu cycles!
+ but you'll see, if you call it in the right places,
+ right where your problems began...
+*/
+
+/* you can break on the check_contexts_trouble()
+routine in your debugger to stop at the moment
+there's a problem */
+void check_contexts_trouble(void);
+
+void check_contexts_trouble(void)
+{
+ int x = 1;
+ x = 2;
+}
+
+static struct ast_context *find_context_locked(const char *context);
+int check_contexts(char *, int);
+
+int check_contexts(char *file, int line )
+{
+ struct ast_hashtab_iter *t1;
+ struct ast_context *c1, *c2;
+ int found = 0;
+ struct ast_exten *e1, *e2, *e3;
+ struct ast_exten ex;
+
+ /* try to find inconsistencies */
+ /* is every context in the context table in the context list and vice-versa ? */
+
+ if (!contexts_table) {
+ ast_log(LOG_NOTICE,"Called from: %s:%d: No contexts_table!\n", file, line);
+ usleep(500000);
+ }
+
+ t1 = ast_hashtab_start_traversal(contexts_table);
+ while( (c1 = ast_hashtab_next(t1))) {
+ for(c2=contexts;c2;c2=c2->next) {
+ if (!strcmp(c1->name, c2->name)) {
+ found = 1;
+ break;
+ }
+ }
+ if (!found) {
+ ast_log(LOG_NOTICE,"Called from: %s:%d: Could not find the %s context in the linked list\n", file, line, c1->name);
+ check_contexts_trouble();
+ }
+ }
+ ast_hashtab_end_traversal(t1);
+ for(c2=contexts;c2;c2=c2->next) {
+ c1 = find_context_locked(c2->name);
+ if (!c1) {
+ ast_log(LOG_NOTICE,"Could not find the %s context in the hashtab\n", c2->name);
+ check_contexts_trouble();
+ } else
+ ast_unlock_contexts();
+ }
+
+ /* loop thru all contexts, and verify the exten structure compares to the
+ hashtab structure */
+ for(c2=contexts;c2;c2=c2->next) {
+ c1 = find_context_locked(c2->name);
+ if (c1)
+ {
+
+ ast_unlock_contexts();
+
+ /* is every entry in the root list also in the root_table? */
+ for(e1 = c1->root; e1; e1=e1->next)
+ {
+ char dummy_name[1024];
+ ex.exten = dummy_name;
+ ex.matchcid = e1->matchcid;
+ ex.cidmatch = e1->cidmatch;
+ ast_copy_string(dummy_name, e1->exten, sizeof(dummy_name));
+ e2 = ast_hashtab_lookup(c1->root_table, &ex);
+ if (!e2) {
+ if (e1->matchcid) {
+ ast_log(LOG_NOTICE,"Called from: %s:%d: The %s context records the exten %s (CID match: %s) but it is not in its root_table\n", file, line, c2->name, dummy_name, e1->cidmatch );
+ } else {
+ ast_log(LOG_NOTICE,"Called from: %s:%d: The %s context records the exten %s but it is not in its root_table\n", file, line, c2->name, dummy_name );
+ }
+ check_contexts_trouble();
+ }
+ }
+
+ /* is every entry in the root_table also in the root list? */
+ if (!c2->root_table) {
+ if (c2->root) {
+ ast_log(LOG_NOTICE,"Called from: %s:%d: No c2->root_table for context %s!\n", file, line, c2->name);
+ usleep(500000);
+ }
+ } else {
+ t1 = ast_hashtab_start_traversal(c2->root_table);
+ while( (e2 = ast_hashtab_next(t1)) ) {
+ for(e1=c2->root;e1;e1=e1->next) {
+ if (!strcmp(e1->exten, e2->exten)) {
+ found = 1;
+ break;
+ }
+ }
+ if (!found) {
+ ast_log(LOG_NOTICE,"Called from: %s:%d: The %s context records the exten %s but it is not in its root_table\n", file, line, c2->name, e2->exten);
+ check_contexts_trouble();
+ }
+
+ }
+ ast_hashtab_end_traversal(t1);
+ }
+ }
+ /* is every priority reflected in the peer_table at the head of the list? */
+
+ /* is every entry in the root list also in the root_table? */
+ /* are the per-extension peer_tables in the right place? */
+
+ for(e1 = c2->root; e1; e1 = e1->next) {
+
+ for(e2=e1;e2;e2=e2->peer) {
+ ex.priority = e2->priority;
+ if (e2 != e1 && e2->peer_table) {
+ ast_log(LOG_NOTICE,"Called from: %s:%d: The %s context, %s exten, %d priority has a peer_table entry, and shouldn't!\n", file, line, c2->name, e1->exten, e2->priority );
+ check_contexts_trouble();
+ }
+
+ if (e2 != e1 && e2->peer_label_table) {
+ ast_log(LOG_NOTICE,"Called from: %s:%d: The %s context, %s exten, %d priority has a peer_label_table entry, and shouldn't!\n", file, line, c2->name, e1->exten, e2->priority );
+ check_contexts_trouble();
+ }
+
+ if (e2 == e1 && !e2->peer_table){
+ ast_log(LOG_NOTICE,"Called from: %s:%d: The %s context, %s exten, %d priority doesn't have a peer_table!\n", file, line, c2->name, e1->exten, e2->priority );
+ check_contexts_trouble();
+ }
+
+ if (e2 == e1 && !e2->peer_label_table) {
+ ast_log(LOG_NOTICE,"Called from: %s:%d: The %s context, %s exten, %d priority doesn't have a peer_label_table!\n", file, line, c2->name, e1->exten, e2->priority );
+ check_contexts_trouble();
+ }
+
+
+ e3 = ast_hashtab_lookup(e1->peer_table, &ex);
+ if (!e3) {
+ ast_log(LOG_NOTICE,"Called from: %s:%d: The %s context, %s exten, %d priority is not reflected in the peer_table\n", file, line, c2->name, e1->exten, e2->priority );
+ check_contexts_trouble();
+ }
+ }
+
+ if (!e1->peer_table){
+ ast_log(LOG_NOTICE,"Called from: %s:%d: No e1->peer_table!\n", file, line);
+ usleep(500000);
+ }
+
+ /* is every entry in the peer_table also in the peer list? */
+ t1 = ast_hashtab_start_traversal(e1->peer_table);
+ while( (e2 = ast_hashtab_next(t1)) ) {
+ for(e3=e1;e3;e3=e3->peer) {
+ if (e3->priority == e2->priority) {
+ found = 1;
+ break;
+ }
+ }
+ if (!found) {
+ ast_log(LOG_NOTICE,"Called from: %s:%d: The %s context, %s exten, %d priority is not reflected in the peer list\n", file, line, c2->name, e1->exten, e2->priority );
+ check_contexts_trouble();
+ }
+ }
+ ast_hashtab_end_traversal(t1);
+ }
+ }
+ return 0;
+}
+#endif
+
/*
\note This function is special. It saves the stack so that no matter
how many times it is called, it returns to the same place */
@@ -1044,7 +1228,7 @@ static void new_find_extension(const char *str, struct scoreboard *score, struct
update_scoreboard(score, length+1, spec+p->specificity, p->exten,0,callerid, p->deleted, p); \
if (!p->deleted) { \
if (action == E_FINDLABEL) { \
- if (ast_hashtab_lookup(score->exten->peer_label_tree, &pattern)) { \
+ if (ast_hashtab_lookup(score->exten->peer_label_table, &pattern)) { \
ast_debug(4, "Found label in preferred extension\n"); \
return; \
} \
@@ -1392,11 +1576,11 @@ static void create_match_char_tree(struct ast_context *con)
int biggest_bucket, resizes, numobjs, numbucks;
ast_log(LOG_DEBUG,"Creating Extension Trie for context %s\n", con->name);
- ast_hashtab_get_stats(con->root_tree, &biggest_bucket, &resizes, &numobjs, &numbucks);
+ ast_hashtab_get_stats(con->root_table, &biggest_bucket, &resizes, &numobjs, &numbucks);
ast_log(LOG_DEBUG,"This tree has %d objects in %d bucket lists, longest list=%d objects, and has resized %d times\n",
numobjs, numbucks, biggest_bucket, resizes);
#endif
- t1 = ast_hashtab_start_traversal(con->root_tree);
+ t1 = ast_hashtab_start_traversal(con->root_table);
while( (e1 = ast_hashtab_next(t1)) ) {
if (e1->exten)
add_exten_to_pattern_tree(con, e1, 0);
@@ -1787,12 +1971,13 @@ struct fake_context /* this struct is purely for matching in the hashtab */
{
ast_rwlock_t lock;
struct ast_exten *root;
- struct ast_hashtab *root_tree;
+ struct ast_hashtab *root_table;
struct match_char *pattern_tree;
struct ast_context *next;
struct ast_include *includes;
struct ast_ignorepat *ignorepats;
- const char *registrar;
+ const char *registrar;
+ int refcount;
AST_LIST_HEAD_NOLOCK(, ast_sw) alts;
ast_mutex_t macrolock;
char name[256];
@@ -1804,8 +1989,8 @@ struct ast_context *ast_context_find(const char *name)
struct fake_context item;
strncpy(item.name,name,256);
ast_rdlock_contexts();
- if( contexts_tree ) {
- tmp = ast_hashtab_lookup(contexts_tree,&item);
+ if( contexts_table ) {
+ tmp = ast_hashtab_lookup(contexts_table,&item);
} else {
while ( (tmp = ast_walk_contexts(tmp)) ) {
if (!name || !strcasecmp(name, tmp->name))
@@ -1874,7 +2059,7 @@ struct ast_exten *pbx_find_extension(struct ast_channel *chan,
else { /* look in contexts */
struct fake_context item;
strncpy(item.name,context,256);
- tmp = ast_hashtab_lookup(contexts_tree,&item);
+ tmp = ast_hashtab_lookup(contexts_table,&item);
#ifdef NOTNOW
tmp = NULL;
while ((tmp = ast_walk_contexts(tmp)) ) {
@@ -1896,7 +2081,7 @@ struct ast_exten *pbx_find_extension(struct ast_channel *chan,
score.total_specificity = 0;
score.exten = 0;
score.total_length = 0;
- if (!tmp->pattern_tree && tmp->root_tree)
+ if (!tmp->pattern_tree && tmp->root_table)
{
create_match_char_tree(tmp);
#ifdef NEED_DEBUG
@@ -1966,9 +2151,9 @@ struct ast_exten *pbx_find_extension(struct ast_channel *chan,
if (action == E_FINDLABEL && label ) {
if (q->status < STATUS_NO_LABEL)
q->status = STATUS_NO_LABEL;
- e = ast_hashtab_lookup(eroot->peer_label_tree, &pattern);
+ e = ast_hashtab_lookup(eroot->peer_label_table, &pattern);
} else {
- e = ast_hashtab_lookup(eroot->peer_tree, &pattern);
+ e = ast_hashtab_lookup(eroot->peer_table, &pattern);
}
if (e) { /* found a valid match */
q->status = STATUS_SUCCESS;
@@ -2002,9 +2187,9 @@ struct ast_exten *pbx_find_extension(struct ast_channel *chan,
if (action == E_FINDLABEL && label ) {
if (q->status < STATUS_NO_LABEL)
q->status = STATUS_NO_LABEL;
- e = ast_hashtab_lookup(eroot->peer_label_tree, &pattern);
+ e = ast_hashtab_lookup(eroot->peer_label_table, &pattern);
} else {
- e = ast_hashtab_lookup(eroot->peer_tree, &pattern);
+ e = ast_hashtab_lookup(eroot->peer_table, &pattern);
}
#ifdef NOTNOW
while ( (e = ast_walk_extension_priorities(eroot, e)) ) {
@@ -2686,7 +2871,7 @@ static void pbx_substitute_variables_helper_full(struct ast_channel *c, struct v
vare++;
}
if (brackets)
- ast_log(LOG_NOTICE, "Error in extension logic (missing '}')\n");
+ ast_log(LOG_WARNING, "Error in extension logic (missing '}')\n");
len = vare - vars - 1;
/* Skip totally over variable string */
@@ -2773,7 +2958,7 @@ static void pbx_substitute_variables_helper_full(struct ast_channel *c, struct v
vare++;
}
if (brackets)
- ast_log(LOG_NOTICE, "Error in extension logic (missing ']')\n");
+ ast_log(LOG_WARNING, "Error in extension logic (missing ']')\n");
len = vare - vars - 1;
/* Skip totally over expression */
@@ -3689,14 +3874,14 @@ static int increase_call_count(const struct ast_channel *c)
ast_mutex_lock(&maxcalllock);
if (option_maxcalls) {
if (countcalls >= option_maxcalls) {
- ast_log(LOG_NOTICE, "Maximum call limit of %d calls exceeded by '%s'!\n", option_maxcalls, c->name);
+ ast_log(LOG_WARNING, "Maximum call limit of %d calls exceeded by '%s'!\n", option_maxcalls, c->name);
failed = -1;
}
}
if (option_maxload) {
getloadavg(&curloadavg, 1);
if (curloadavg >= option_maxload) {
- ast_log(LOG_NOTICE, "Maximum loadavg limit of %f load exceeded by '%s' (currently %f)!\n", option_maxload, c->name, curloadavg);
+ ast_log(LOG_WARNING, "Maximum loadavg limit of %f load exceeded by '%s' (currently %f)!\n", option_maxload, c->name, curloadavg);
failed = -1;
}
}
@@ -3737,10 +3922,10 @@ static void destroy_exten(struct ast_exten *e)
if (e->priority == PRIORITY_HINT)
ast_remove_hint(e);
- if (e->peer_tree)
- ast_hashtab_destroy(e->peer_tree,0);
- if (e->peer_label_tree)
- ast_hashtab_destroy(e->peer_label_tree, 0);
+ if (e->peer_table)
+ ast_hashtab_destroy(e->peer_table,0);
+ if (e->peer_label_table)
+ ast_hashtab_destroy(e->peer_label_table, 0);
if (e->datad)
e->datad(e->data);
ast_free(e);
@@ -3837,8 +4022,7 @@ static struct ast_context *find_context_locked(const char *context)
ast_copy_string(item.name, context, sizeof(item.name));
ast_rdlock_contexts();
-
- c = ast_hashtab_lookup(contexts_tree,&item);
+ c = ast_hashtab_lookup(contexts_table,&item);
#ifdef NOTNOW
@@ -4017,18 +4201,20 @@ int ast_context_remove_extension_callerid2(struct ast_context *con, const char *
#ifdef NEED_DEBUG
ast_verb(3,"Removing %s/%s/%d%s%s from trees, registrar=%s\n", con->name, extension, priority, matchcid ? "/" : "", matchcid ? callerid : "", registrar);
#endif
+#ifdef CONTEXT_DEBUG
+ check_contexts(__FILE__, __LINE__);
+#endif
/* find this particular extension */
ex.exten = dummy_name;
- ex.matchcid = matchcid;
+ ex.matchcid = matchcid && !ast_strlen_zero(callerid); /* don't say match if there's no callerid */
ex.cidmatch = callerid;
ast_copy_string(dummy_name, extension, sizeof(dummy_name));
- exten = ast_hashtab_lookup(con->root_tree, &ex);
+ exten = ast_hashtab_lookup(con->root_table, &ex);
if (exten) {
- if (priority == 0)
- {
- exten2 = ast_hashtab_remove_this_object(con->root_tree, exten);
+ if (priority == 0) {
+ exten2 = ast_hashtab_remove_this_object(con->root_table, exten);
if (!exten2)
- ast_log(LOG_ERROR,"Trying to delete the exten %s from context %s, but could not remove from the root_tree\n", extension, con->name);
+ ast_log(LOG_ERROR,"Trying to delete the exten %s from context %s, but could not remove from the root_table\n", extension, con->name);
if (con->pattern_tree) {
struct match_char *x = add_exten_to_pattern_tree(con, exten, 1);
@@ -4042,24 +4228,28 @@ int ast_context_remove_extension_callerid2(struct ast_context *con, const char *
}
} else {
ex.priority = priority;
- exten2 = ast_hashtab_lookup(exten->peer_tree, &ex);
+ exten2 = ast_hashtab_lookup(exten->peer_table, &ex);
if (exten2) {
if (exten2->label) { /* if this exten has a label, remove that, too */
- exten3 = ast_hashtab_remove_this_object(exten->peer_label_tree,exten2);
+ exten3 = ast_hashtab_remove_this_object(exten->peer_label_table,exten2);
if (!exten3)
- ast_log(LOG_ERROR,"Did not remove this priority label (%d/%s) from the peer_label_tree of context %s, extension %s!\n", priority, exten2->label, con->name, exten2->exten);
+ ast_log(LOG_ERROR,"Did not remove this priority label (%d/%s) from the peer_label_table of context %s, extension %s!\n", priority, exten2->label, con->name, exten2->exten);
}
- exten3 = ast_hashtab_remove_this_object(exten->peer_tree, exten2);
+ exten3 = ast_hashtab_remove_this_object(exten->peer_table, exten2);
if (!exten3)
- ast_log(LOG_ERROR,"Did not remove this priority (%d) from the peer_tree of context %s, extension %s!\n", priority, con->name, exten2->exten);
- if (ast_hashtab_size(exten->peer_tree) == 0) {
+ ast_log(LOG_ERROR,"Did not remove this priority (%d) from the peer_table of context %s, extension %s!\n", priority, con->name, exten2->exten);
+ if (exten2 == exten && exten2->peer) {
+ exten2 = ast_hashtab_remove_this_object(con->root_table, exten);
+ ast_hashtab_insert_immediate(con->root_table, exten2->peer);
+ }
+ if (ast_hashtab_size(exten->peer_table) == 0) {
/* well, if the last priority of an exten is to be removed,
then, the extension is removed, too! */
- exten3 = ast_hashtab_remove_this_object(con->root_tree, exten);
+ exten3 = ast_hashtab_remove_this_object(con->root_table, exten);
if (!exten3)
- ast_log(LOG_ERROR,"Did not remove this exten (%s) from the context root_tree (%s) (priority %d)\n", exten->exten, con->name, priority);
+ ast_log(LOG_ERROR,"Did not remove this exten (%s) from the context root_table (%s) (priority %d)\n", exten->exten, con->name, priority);
if (con->pattern_tree) {
struct match_char *x = add_exten_to_pattern_tree(con, exten, 1);
if (x->exten) { /* this test for safety purposes */
@@ -4075,7 +4265,7 @@ int ast_context_remove_extension_callerid2(struct ast_context *con, const char *
}
} else {
/* hmmm? this exten is not in this pattern tree? */
- ast_log(LOG_WARNING,"Cannot find extension %s in root_tree in context %s\n",
+ ast_log(LOG_WARNING,"Cannot find extension %s in root_table in context %s\n",
extension, con->name);
}
#ifdef NEED_DEBUG
@@ -4088,7 +4278,8 @@ int ast_context_remove_extension_callerid2(struct ast_context *con, const char *
/* scan the extension list to find first matching extension-registrar */
for (exten = con->root; exten; prev_exten = exten, exten = exten->next) {
if (!strcmp(exten->exten, extension) &&
- (!registrar || !strcmp(exten->registrar, registrar)))
+ (!registrar || !strcmp(exten->registrar, registrar)) &&
+ (!matchcid || (!ast_strlen_zero(callerid) && !ast_strlen_zero(exten->cidmatch) && !strcmp(exten->cidmatch, callerid)) || (ast_strlen_zero(callerid) && ast_strlen_zero(exten->cidmatch))))
break;
}
if (!exten) {
@@ -4100,7 +4291,7 @@ int ast_context_remove_extension_callerid2(struct ast_context *con, const char *
/* scan the priority list to remove extension with exten->priority == priority */
for (peer = exten, next_peer = exten->peer ? exten->peer : exten->next;
- peer && !strcmp(peer->exten, extension);
+ peer && !strcmp(peer->exten, extension) && (!matchcid || (!ast_strlen_zero(callerid) && !ast_strlen_zero(peer->cidmatch) && !strcmp(peer->cidmatch,callerid)) || (ast_strlen_zero(callerid) && ast_strlen_zero(peer->cidmatch)));
peer = next_peer, next_peer = next_peer ? (next_peer->peer ? next_peer->peer : next_peer->next) : NULL) {
if ((priority == 0 || peer->priority == priority) &&
(!callerid || !matchcid || (matchcid && !strcmp(peer->cidmatch, callerid))) &&
@@ -4114,7 +4305,14 @@ int ast_context_remove_extension_callerid2(struct ast_context *con, const char *
* The next node is either the next priority or the next extension
*/
struct ast_exten *next_node = peer->peer ? peer->peer : peer->next;
-
+ if (peer->peer) {
+ /* move the peer_table and peer_label_table down to the next peer, if
+ it is there */
+ peer->peer->peer_table = peer->peer_table;
+ peer->peer->peer_label_table = peer->peer_label_table;
+ peer->peer_table = NULL;
+ peer->peer_label_table = NULL;
+ }
if (!prev_exten) { /* change the root... */
con->root = next_node;
} else {
@@ -4133,6 +4331,9 @@ int ast_context_remove_extension_callerid2(struct ast_context *con, const char *
previous_peer = peer;
}
}
+#ifdef CONTEXT_DEBUG
+ check_contexts(__FILE__, __LINE__);
+#endif
if (!already_locked)
ast_unlock_context(con);
return found ? 0 : -1;
@@ -4153,7 +4354,7 @@ int ast_context_lockmacro(const char *context)
ast_rdlock_contexts();
strncpy(item.name,context,256);
- c = ast_hashtab_lookup(contexts_tree,&item);
+ c = ast_hashtab_lookup(contexts_table,&item);
if (c)
ret = 0;
@@ -4191,7 +4392,7 @@ int ast_context_unlockmacro(const char *context)
ast_rdlock_contexts();
strncpy(item.name, context, 256);
- c = ast_hashtab_lookup(contexts_tree,&item);
+ c = ast_hashtab_lookup(contexts_table,&item);
if (c)
ret = 0;
#ifdef NOTNOW
@@ -4785,7 +4986,7 @@ static int show_dialplan_helper(int fd, const char *context, const char *exten,
if (exten) {
/* Check all includes for the requested extension */
if (includecount >= AST_PBX_MAX_STACK) {
- ast_log(LOG_NOTICE, "Maximum include depth exceeded!\n");
+ ast_log(LOG_WARNING, "Maximum include depth exceeded!\n");
} else {
int dupe = 0;
int x;
@@ -5334,42 +5535,37 @@ int ast_unregister_application(const char *app)
return tmp ? 0 : -1;
}
-static struct ast_context *__ast_context_create(struct ast_context **extcontexts, const char *name, const char *registrar, int existsokay)
+struct ast_context *ast_context_find_or_create(struct ast_context **extcontexts, struct ast_hashtab *exttable, const char *name, const char *registrar)
{
struct ast_context *tmp, **local_contexts;
struct fake_context search;
int length = sizeof(struct ast_context) + strlen(name) + 1;
- if (!contexts_tree) {
- contexts_tree = ast_hashtab_create(17,
- hashtab_compare_contexts,
+ if (!contexts_table) {
+ contexts_table = ast_hashtab_create(17,
+ ast_hashtab_compare_contexts,
ast_hashtab_resize_java,
ast_hashtab_newsize_java,
- hashtab_hash_contexts,
+ ast_hashtab_hash_contexts,
0);
}
+ strncpy(search.name,name,sizeof(search.name));
if (!extcontexts) {
ast_rdlock_contexts();
local_contexts = &contexts;
- strncpy(search.name,name,sizeof(search.name));
- tmp = ast_hashtab_lookup(contexts_tree, &search);
- if (!existsokay && tmp) {
- ast_log(LOG_WARNING, "Tried to register context '%s', already in use\n", name);
- }
+ tmp = ast_hashtab_lookup(contexts_table, &search);
ast_unlock_contexts();
- if (tmp)
+ if (tmp) {
+ tmp->refcount++;
return tmp;
+ }
} else { /* local contexts just in a linked list; search there for the new context; slow, linear search, but not frequent */
local_contexts = extcontexts;
- for (tmp = *local_contexts; tmp; tmp = tmp->next) {
- if (!strcasecmp(tmp->name, name)) {
- if (!existsokay) {
- ast_log(LOG_WARNING, "Tried to register context '%s', already in use\n", name);
- tmp = NULL;
- }
- return tmp;
- }
+ tmp = ast_hashtab_lookup(exttable, &search);
+ if (tmp) {
+ tmp->refcount++;
+ return tmp;
}
}
@@ -5378,19 +5574,26 @@ static struct ast_context *__ast_context_create(struct ast_context **extcontexts
ast_mutex_init(&tmp->macrolock);
strcpy(tmp->name, name);
tmp->root = NULL;
- tmp->root_tree = NULL;
- tmp->registrar = registrar;
+ tmp->root_table = NULL;
+ tmp->registrar = ast_strdup(registrar);
tmp->includes = NULL;
tmp->ignorepats = NULL;
+ tmp->refcount = 1;
+ } else {
+ ast_log(LOG_ERROR, "Danger! We failed to allocate a context for %s!\n", name);
+ return NULL;
}
+
if (!extcontexts) {
ast_wrlock_contexts();
tmp->next = *local_contexts;
*local_contexts = tmp;
- ast_hashtab_insert_safe(contexts_tree, tmp); /*put this context into the tree */
+ ast_hashtab_insert_safe(contexts_table, tmp); /*put this context into the tree */
ast_unlock_contexts();
} else {
tmp->next = *local_contexts;
+ if (exttable)
+ ast_hashtab_insert_immediate(exttable, tmp); /*put this context into the tree */
*local_contexts = tmp;
}
ast_debug(1, "Registered context '%s'\n", tmp->name);
@@ -5398,16 +5601,7 @@ static struct ast_context *__ast_context_create(struct ast_context **extcontexts
return tmp;
}
-struct ast_context *ast_context_create(struct ast_context **extcontexts, const char *name, const char *registrar)
-{
- return __ast_context_create(extcontexts, name, registrar, 0);
-}
-
-struct ast_context *ast_context_find_or_create(struct ast_context **extcontexts, const char *name, const char *registrar)
-{
- return __ast_context_create(extcontexts, name, registrar, 1);
-}
-void __ast_context_destroy(struct ast_context *con, const char *registrar);
+void __ast_context_destroy(struct ast_context *list, struct ast_hashtab *contexttab, struct ast_context *con, const char *registrar);
struct store_hint {
char *context;
@@ -5420,17 +5614,125 @@ struct store_hint {
AST_LIST_HEAD(store_hints, store_hint);
+static void context_merge_incls_swits_igps_other_registrars(struct ast_context *new, struct ast_context *old, const char *registrar)
+{
+ struct ast_include *i;
+ struct ast_ignorepat *ip;
+ struct ast_sw *sw;
+
+ /* copy in the includes, switches, and ignorepats */
+ /* walk through includes */
+ for (i = NULL; (i = ast_walk_context_includes(old, i)) ; ) {
+ if (strcmp(ast_get_include_registrar(i), registrar) == 0)
+ continue; /* not mine */
+ ast_context_add_include2(new, ast_get_include_name(i), ast_get_include_registrar(i));
+ }
+
+ /* walk through switches */
+ for (sw = NULL; (sw = ast_walk_context_switches(old, sw)) ; ) {
+ if (strcmp(ast_get_switch_registrar(sw), registrar) == 0)
+ continue; /* not mine */
+ ast_context_add_switch2(new, ast_get_switch_name(sw), ast_get_switch_data(sw), ast_get_switch_eval(sw), ast_get_switch_registrar(sw));
+ }
+
+ /* walk thru ignorepats ... */
+ for (ip = NULL; (ip = ast_walk_context_ignorepats(old, ip)); ) {
+ if (strcmp(ast_get_ignorepat_registrar(ip), registrar) == 0)
+ continue; /* not mine */
+ ast_context_add_ignorepat2(new, ast_get_ignorepat_name(ip), ast_get_ignorepat_registrar(ip));
+ }
+}
+
+
+/* the purpose of this routine is to duplicate a context, with all its substructure,
+ except for any extens that have a matching registrar */
+static void context_merge(struct ast_context **extcontexts, struct ast_hashtab *exttable, struct ast_context *context, const char *registrar)
+{
+ struct ast_context *new = ast_hashtab_lookup(exttable, context); /* is there a match in the new set? */
+ struct ast_exten *exten_item, *prio_item, *new_exten_item, *new_prio_item;
+ struct ast_hashtab_iter *exten_iter;
+ struct ast_hashtab_iter *prio_iter;
+ int insert_count = 0;
+
+ /* We'll traverse all the extensions/prios, and see which are not registrar'd with
+ the current registrar, and copy them to the new context. If the new context does not
+ exist, we'll create it "on demand". If no items are in this context to copy, then we'll
+ only create the empty matching context if the old one meets the criteria */
+ if (context->root_table) {
+ exten_iter = ast_hashtab_start_traversal(context->root_table);
+ while ((exten_item=ast_hashtab_next(exten_iter))) {
+ if (new) {
+ new_exten_item = ast_hashtab_lookup(new->root_table, exten_item);
+ } else {
+ new_exten_item = NULL;
+ }
+ prio_iter = ast_hashtab_start_traversal(exten_item->peer_table);
+ while ((prio_item=ast_hashtab_next(prio_iter))) {
+ int res1;
+
+ if (new_exten_item) {
+ new_prio_item = ast_hashtab_lookup(new_exten_item->peer_table, prio_item);
+ } else {
+ new_prio_item = NULL;
+ }
+ if (strcmp(prio_item->registrar,registrar) == 0) {
+ continue;
+ }
+ /* make sure the new context exists, so we have somewhere to stick this exten/prio */
+ if (!new) {
+ new = ast_context_find_or_create(extcontexts, exttable, context->name, prio_item->registrar); /* a new context created via priority from a different context in the old dialplan, gets its registrar from the prio's registrar */
+
+ /* copy in the includes, switches, and ignorepats */
+ context_merge_incls_swits_igps_other_registrars(new, context, registrar);
+ }
+ if (!new) {
+ ast_log(LOG_ERROR,"Could not allocate a new context for %s in merge_and_delete! Danger!\n", context->name);
+ return; /* no sense continuing. */
+ }
+ /* we will not replace existing entries in the new context with stuff from the old context.
+ but, if this is because of some sort of registrar conflict, we ought to say something... */
+ res1 = ast_add_extension2(new, 0, prio_item->exten, prio_item->priority, prio_item->label,
+ prio_item->cidmatch, prio_item->app, prio_item->data, prio_item->datad, prio_item->registrar);
+ if (!res1 && new_exten_item && new_prio_item){
+ ast_verb(3,"Dropping old dialplan item %s/%s/%d [%s(%s)] (registrar=%s) due to conflict with new dialplan\n",
+ context->name, prio_item->exten, prio_item->priority, prio_item->app, (char*)prio_item->data, prio_item->registrar);
+ } else {
+ prio_item->data = NULL; /* we pass the priority data from the old to the new */
+ insert_count++;
+ }
+ }
+ ast_hashtab_end_traversal(prio_iter);
+ }
+ ast_hashtab_end_traversal(exten_iter);
+ }
+
+ if (!insert_count && !new && (strcmp(context->registrar, registrar) != 0 ||
+ (strcmp(context->registrar, registrar) == 0 && context->refcount > 1))) {
+
+ /* we could have given it the registrar of the other module who incremented the refcount,
+ but that's not available, so we give it the registrar we know about */
+ new = ast_context_find_or_create(extcontexts, exttable, context->name, context->registrar);
+
+ /* copy in the includes, switches, and ignorepats */
+ context_merge_incls_swits_igps_other_registrars(new, context, registrar);
+ }
+}
+
+
/* XXX this does not check that multiple contexts are merged */
-void ast_merge_contexts_and_delete(struct ast_context **extcontexts, const char *registrar)
+void ast_merge_contexts_and_delete(struct ast_context **extcontexts, struct ast_hashtab *exttable, const char *registrar)
{
- struct ast_context *tmp, *lasttmp = NULL;
+ double ft;
+ struct ast_context *tmp, *oldcontextslist;
+ struct ast_hashtab *oldtable;
struct store_hints store = AST_LIST_HEAD_INIT_VALUE;
struct store_hint *this;
struct ast_hint *hint;
struct ast_exten *exten;
int length;
struct ast_state_cb *thiscb, *prevcb;
-
+ struct ast_hashtab_iter *iter;
+
/* it is very important that this function hold the hint list lock _and_ the conlock
during its operation; not only do we need to ensure that the list of contexts
and extensions does not change, but also that no hint callbacks (watchers) are
@@ -5439,8 +5741,29 @@ void ast_merge_contexts_and_delete(struct ast_context **extcontexts, const char
in addition, the locks _must_ be taken in this order, because there are already
other code paths that use this order
*/
+
+ struct timeval begintime, writelocktime, endlocktime, enddeltime;
+ int wrlock_ver;
+
+ begintime = ast_tvnow();
+ ast_rdlock_contexts();
+ iter = ast_hashtab_start_traversal(contexts_table);
+ while ((tmp=ast_hashtab_next(iter))) {
+ context_merge(extcontexts, exttable, tmp, registrar);
+ }
+ ast_hashtab_end_traversal(iter);
+ wrlock_ver = ast_wrlock_contexts_version();
+
+ ast_unlock_contexts(); /* this feels real retarded, but you must do
+ what you must do If this isn't done, the following
+ wrlock is a guraranteed deadlock */
ast_wrlock_contexts();
+ if (ast_wrlock_contexts_version() > wrlock_ver+1) {
+ ast_log(LOG_WARNING,"Something changed the contexts in the middle of merging contexts!\n");
+ }
+
AST_RWLIST_WRLOCK(&hints);
+ writelocktime = ast_tvnow();
/* preserve all watchers for hints associated with this registrar */
AST_RWLIST_TRAVERSE(&hints, hint, list) {
@@ -5459,36 +5782,14 @@ void ast_merge_contexts_and_delete(struct ast_context **extcontexts, const char
}
}
- tmp = *extcontexts;
- if (registrar) {
- /* XXX remove previous contexts from same registrar */
- ast_debug(1, "must remove any reg %s\n", registrar);
- __ast_context_destroy(NULL,registrar);
- while (tmp) {
- lasttmp = tmp;
- tmp = tmp->next;
- }
- } else {
- /* XXX remove contexts with the same name */
- while (tmp) {
- ast_log(LOG_WARNING, "must remove %s reg %s\n", tmp->name, tmp->registrar);
- __ast_context_destroy(tmp,tmp->registrar);
- lasttmp = tmp;
- tmp = tmp->next;
- }
- }
- tmp = *extcontexts;
- while (tmp) {
- ast_hashtab_insert_safe(contexts_tree, tmp); /*put this context into the tree */
- tmp = tmp->next;
- }
- if (lasttmp) {
- lasttmp->next = contexts;
- contexts = *extcontexts;
- *extcontexts = NULL;
- } else
- ast_log(LOG_WARNING, "Requested contexts didn't get merged\n");
+ /* save the old table and list */
+ oldtable = contexts_table;
+ oldcontextslist = contexts;
+ /* move in the new table and list */
+ contexts_table = exttable;
+ contexts = *extcontexts;
+
/* restore the watchers for hints that can be found; notify those that
cannot be restored
*/
@@ -5523,7 +5824,36 @@ void ast_merge_contexts_and_delete(struct ast_context **extcontexts, const char
AST_RWLIST_UNLOCK(&hints);
ast_unlock_contexts();
+ endlocktime = ast_tvnow();
+
+ /* the old list and hashtab no longer are relevant, delete them while the rest of asterisk
+ is now freely using the new stuff instead */
+
+ ast_hashtab_destroy(oldtable, NULL);
+
+ for (tmp = oldcontextslist; tmp; ) {
+ struct ast_context *next; /* next starting point */
+ next = tmp->next;
+ __ast_internal_context_destroy(tmp);
+ tmp = next;
+ }
+ enddeltime = ast_tvnow();
+
+ ft = ast_tvdiff_us(writelocktime, begintime);
+ ft /= 1000000.0;
+ ast_verb(3,"Time to scan old dialplan and merge leftovers back into the new: %8.6f sec\n", ft);
+
+ ft = ast_tvdiff_us(endlocktime, writelocktime);
+ ft /= 1000000.0;
+ ast_verb(3,"Time to restore hints and swap in new dialplan: %8.6f sec\n", ft);
+
+ ft = ast_tvdiff_us(enddeltime, endlocktime);
+ ft /= 1000000.0;
+ ast_verb(3,"Time to delete the old dialplan: %8.6f sec\n", ft);
+ ft = ast_tvdiff_us(enddeltime, begintime);
+ ft /= 1000000.0;
+ ast_verb(3,"Total time merge_contexts_delete: %8.6f sec\n", ft);
return;
}
@@ -6054,6 +6384,7 @@ int ast_add_extension(const char *context, int replace, const char *extension,
application, data, datad, registrar);
ast_unlock_contexts();
}
+
return ret;
}
@@ -6180,9 +6511,11 @@ static int add_pri(struct ast_context *con, struct ast_exten *tmp,
break;
}
if (!e) { /* go at the end, and ep is surely set because the list is not empty */
- ast_hashtab_insert_safe(eh->peer_tree, tmp);
- if (tmp->label)
- ast_hashtab_insert_safe(eh->peer_label_tree, tmp);
+ ast_hashtab_insert_safe(eh->peer_table, tmp);
+
+ if (tmp->label) {
+ ast_hashtab_insert_safe(eh->peer_label_table, tmp);
+ }
ep->peer = tmp;
return 0; /* success */
}
@@ -6202,25 +6535,33 @@ static int add_pri(struct ast_context *con, struct ast_exten *tmp,
tmp->next = e->next; /* not meaningful if we are not first in the peer list */
tmp->peer = e->peer; /* always meaningful */
if (ep) { /* We're in the peer list, just insert ourselves */
- ast_hashtab_remove_object_via_lookup(eh->peer_tree,e);
- if (e->label)
- ast_hashtab_remove_object_via_lookup(eh->peer_label_tree,e);
- ast_hashtab_insert_safe(eh->peer_tree,tmp);
- if (tmp->label)
- ast_hashtab_insert_safe(eh->peer_label_tree,tmp);
+ ast_hashtab_remove_object_via_lookup(eh->peer_table,e);
+
+ if (e->label) {
+ ast_hashtab_remove_object_via_lookup(eh->peer_label_table,e);
+ }
+
+ ast_hashtab_insert_safe(eh->peer_table,tmp);
+ if (tmp->label) {
+ ast_hashtab_insert_safe(eh->peer_label_table,tmp);
+ }
+
ep->peer = tmp;
} else if (el) { /* We're the first extension. Take over e's functions */
struct match_char *x = add_exten_to_pattern_tree(con, e, 1);
- tmp->peer_tree = e->peer_tree;
- tmp->peer_label_tree = e->peer_label_tree;
- ast_hashtab_remove_object_via_lookup(tmp->peer_tree,e);
- ast_hashtab_insert_safe(tmp->peer_tree,tmp);
- if (e->label)
- ast_hashtab_remove_object_via_lookup(tmp->peer_label_tree,e);
- if (tmp->label)
- ast_hashtab_insert_safe(tmp->peer_label_tree,tmp);
- ast_hashtab_remove_object_via_lookup(con->root_tree, e);
- ast_hashtab_insert_safe(con->root_tree, tmp);
+ tmp->peer_table = e->peer_table;
+ tmp->peer_label_table = e->peer_label_table;
+ ast_hashtab_remove_object_via_lookup(tmp->peer_table,e);
+ ast_hashtab_insert_safe(tmp->peer_table,tmp);
+ if (e->label) {
+ ast_hashtab_remove_object_via_lookup(tmp->peer_label_table, e);
+ }
+ if (tmp->label) {
+ ast_hashtab_insert_safe(tmp->peer_label_table, tmp);
+ }
+
+ ast_hashtab_remove_object_via_lookup(con->root_table, e);
+ ast_hashtab_insert_safe(con->root_table, tmp);
el->next = tmp;
/* The pattern trie points to this exten; replace the pointer,
and all will be well */
@@ -6233,18 +6574,21 @@ static int add_pri(struct ast_context *con, struct ast_exten *tmp,
}
} else { /* We're the very first extension. */
struct match_char *x = add_exten_to_pattern_tree(con, e, 1);
- ast_hashtab_remove_object_via_lookup(con->root_tree,e);
- ast_hashtab_insert_safe(con->root_tree,tmp);
- tmp->peer_tree = e->peer_tree;
- tmp->peer_label_tree = e->peer_label_tree;
- ast_hashtab_remove_object_via_lookup(tmp->peer_tree,e);
- ast_hashtab_insert_safe(tmp->peer_tree,tmp);
- if (e->label)
- ast_hashtab_remove_object_via_lookup(tmp->peer_label_tree,e);
- if (tmp->label)
- ast_hashtab_insert_safe(tmp->peer_label_tree,tmp);
- ast_hashtab_remove_object_via_lookup(con->root_tree, e);
- ast_hashtab_insert_safe(con->root_tree, tmp);
+ ast_hashtab_remove_object_via_lookup(con->root_table, e);
+ ast_hashtab_insert_safe(con->root_table, tmp);
+ tmp->peer_table = e->peer_table;
+ tmp->peer_label_table = e->peer_label_table;
+ ast_hashtab_remove_object_via_lookup(tmp->peer_table, e);
+ ast_hashtab_insert_safe(tmp->peer_table, tmp);
+ if (e->label) {
+ ast_hashtab_remove_object_via_lookup(tmp->peer_label_table, e);
+ }
+ if (tmp->label) {
+ ast_hashtab_insert_safe(tmp->peer_label_table, tmp);
+ }
+
+ ast_hashtab_remove_object_via_lookup(con->root_table, e);
+ ast_hashtab_insert_safe(con->root_table, tmp);
con->root = tmp;
/* The pattern trie points to this exten; replace the pointer,
and all will be well */
@@ -6266,21 +6610,22 @@ static int add_pri(struct ast_context *con, struct ast_exten *tmp,
tmp->peer = e;
tmp->next = e->next; /* extension chain, or NULL if e is not the first extension */
if (ep) { /* Easy enough, we're just in the peer list */
- if (tmp->label)
- ast_hashtab_insert_safe(eh->peer_label_tree, tmp);
- ast_hashtab_insert_safe(eh->peer_tree, tmp);
+ if (tmp->label) {
+ ast_hashtab_insert_safe(eh->peer_label_table, tmp);
+ }
+ ast_hashtab_insert_safe(eh->peer_table, tmp);
ep->peer = tmp;
} else { /* we are the first in some peer list, so link in the ext list */
- tmp->peer_tree = e->peer_tree;
- tmp->peer_label_tree = e ->peer_label_tree;
- e->peer_tree = 0;
- e->peer_label_tree = 0;
- ast_hashtab_insert_safe(tmp->peer_tree,tmp);
+ tmp->peer_table = e->peer_table;
+ tmp->peer_label_table = e->peer_label_table;
+ e->peer_table = 0;
+ e->peer_label_table = 0;
+ ast_hashtab_insert_safe(tmp->peer_table, tmp);
if (tmp->label) {
- ast_hashtab_insert_safe(tmp->peer_label_tree,tmp);
+ ast_hashtab_insert_safe(tmp->peer_label_table, tmp);
}
- ast_hashtab_remove_object_via_lookup(con->root_tree,e);
- ast_hashtab_insert_safe(con->root_tree,tmp);
+ ast_hashtab_remove_object_via_lookup(con->root_table, e);
+ ast_hashtab_insert_safe(con->root_table, tmp);
if (el)
el->next = tmp; /* in the middle... */
else
@@ -6362,6 +6707,9 @@ int ast_add_extension2(struct ast_context *con,
if (!(tmp = ast_calloc(1, length)))
return -1;
+ if (ast_strlen_zero(label)) /* let's turn empty labels to a null ptr */
+ label = 0;
+
/* use p as dst in assignments, as the fields are const char * */
p = tmp->stuff;
if (label) {
@@ -6373,7 +6721,7 @@ int ast_add_extension2(struct ast_context *con,
p += ext_strncpy(p, extension, strlen(extension) + 1) + 1;
tmp->priority = priority;
tmp->cidmatch = p; /* but use p for assignments below */
- if (callerid) {
+ if (!ast_strlen_zero(callerid)) {
p += ext_strncpy(p, callerid, strlen(callerid) + 1) + 1;
tmp->matchcid = 1;
} else {
@@ -6395,11 +6743,11 @@ int ast_add_extension2(struct ast_context *con,
dummy_exten.exten = dummy_name;
dummy_exten.matchcid = 0;
dummy_exten.cidmatch = 0;
- tmp2 = ast_hashtab_lookup(con->root_tree,&dummy_exten);
+ tmp2 = ast_hashtab_lookup(con->root_table, &dummy_exten);
if (!tmp2) {
/* hmmm, not in the trie; */
add_exten_to_pattern_tree(con, tmp, 0);
- ast_hashtab_insert_safe(con->root_tree, tmp); /* for the sake of completeness */
+ ast_hashtab_insert_safe(con->root_table, tmp); /* for the sake of completeness */
}
}
res = 0; /* some compilers will think it is uninitialized otherwise */
@@ -6433,48 +6781,50 @@ int ast_add_extension2(struct ast_context *con,
tmp->next = e;
if (el) { /* there is another exten already in this context */
el->next = tmp;
- tmp->peer_tree = ast_hashtab_create(13,
+ tmp->peer_table = ast_hashtab_create(13,
hashtab_compare_exten_numbers,
ast_hashtab_resize_java,
ast_hashtab_newsize_java,
hashtab_hash_priority,
0);
- tmp->peer_label_tree = ast_hashtab_create(7,
+ tmp->peer_label_table = ast_hashtab_create(7,
hashtab_compare_exten_labels,
ast_hashtab_resize_java,
ast_hashtab_newsize_java,
hashtab_hash_labels,
0);
- if (label)
- ast_hashtab_insert_safe(tmp->peer_label_tree,tmp);
- ast_hashtab_insert_safe(tmp->peer_tree, tmp);
-
+ if (label) {
+ ast_hashtab_insert_safe(tmp->peer_label_table, tmp);
+ }
+ ast_hashtab_insert_safe(tmp->peer_table, tmp);
} else { /* this is the first exten in this context */
- if (!con->root_tree)
- con->root_tree = ast_hashtab_create(27,
+ if (!con->root_table)
+ con->root_table = ast_hashtab_create(27,
hashtab_compare_extens,
ast_hashtab_resize_java,
ast_hashtab_newsize_java,
hashtab_hash_extens,
0);
con->root = tmp;
- con->root->peer_tree = ast_hashtab_create(13,
+ con->root->peer_table = ast_hashtab_create(13,
hashtab_compare_exten_numbers,
ast_hashtab_resize_java,
ast_hashtab_newsize_java,
hashtab_hash_priority,
0);
- con->root->peer_label_tree = ast_hashtab_create(7,
+ con->root->peer_label_table = ast_hashtab_create(7,
hashtab_compare_exten_labels,
ast_hashtab_resize_java,
ast_hashtab_newsize_java,
hashtab_hash_labels,
0);
- if (label)
- ast_hashtab_insert_safe(con->root->peer_label_tree,tmp);
- ast_hashtab_insert_safe(con->root->peer_tree, tmp);
+ if (label) {
+ ast_hashtab_insert_safe(con->root->peer_label_table, tmp);
+ }
+ ast_hashtab_insert_safe(con->root->peer_table, tmp);
+
}
- ast_hashtab_insert_safe(con->root_tree, tmp);
+ ast_hashtab_insert_safe(con->root_table, tmp);
ast_unlock_context(con);
if (tmp->priority == PRIORITY_HINT)
ast_add_hint(tmp);
@@ -6490,7 +6840,7 @@ int ast_add_extension2(struct ast_context *con,
}
if (tmp->matchcid) {
- ast_verb(3, "Added extension '%s' priority %d (CID match '%s')to %s\n",
+ ast_verb(3, "Added extension '%s' priority %d (CID match '%s') to %s\n",
tmp->exten, tmp->priority, tmp->cidmatch, con->name);
} else {
ast_verb(3, "Added extension '%s' priority %d to %s\n",
@@ -6882,68 +7232,191 @@ outgoing_app_cleanup:
return res;
}
-void __ast_context_destroy(struct ast_context *con, const char *registrar)
+/* this is the guts of destroying a context --
+ freeing up the structure, traversing and destroying the
+ extensions, switches, ignorepats, includes, etc. etc. */
+
+static void __ast_internal_context_destroy( struct ast_context *con)
{
- struct ast_context *tmp, *tmpl=NULL;
struct ast_include *tmpi;
struct ast_sw *sw;
struct ast_exten *e, *el, *en;
struct ast_ignorepat *ipi;
+ struct ast_context *tmp = con;
- for (tmp = contexts; tmp; ) {
- struct ast_context *next; /* next starting point */
- for (; tmp; tmpl = tmp, tmp = tmp->next) {
- ast_debug(1, "check ctx %s %s\n", tmp->name, tmp->registrar);
- if ( (!registrar || !strcasecmp(registrar, tmp->registrar)) &&
- (!con || !strcasecmp(tmp->name, con->name)) )
- break; /* found it */
+ for (tmpi = tmp->includes; tmpi; ) { /* Free includes */
+ struct ast_include *tmpil = tmpi;
+ tmpi = tmpi->next;
+ ast_free(tmpil);
+ }
+ for (ipi = tmp->ignorepats; ipi; ) { /* Free ignorepats */
+ struct ast_ignorepat *ipl = ipi;
+ ipi = ipi->next;
+ ast_free(ipl);
+ }
+ if (tmp->registrar)
+ ast_free(tmp->registrar);
+
+ /* destroy the hash tabs */
+ if (tmp->root_table) {
+ ast_hashtab_destroy(tmp->root_table, 0);
+ }
+ /* and destroy the pattern tree */
+ if (tmp->pattern_tree)
+ destroy_pattern_tree(tmp->pattern_tree);
+
+ while ((sw = AST_LIST_REMOVE_HEAD(&tmp->alts, list)))
+ ast_free(sw);
+ for (e = tmp->root; e;) {
+ for (en = e->peer; en;) {
+ el = en;
+ en = en->peer;
+ destroy_exten(el);
}
+ el = e;
+ e = e->next;
+ destroy_exten(el);
+ }
+ tmp->root = NULL;
+ ast_rwlock_destroy(&tmp->lock);
+ ast_free(tmp);
+}
+
+
+void __ast_context_destroy(struct ast_context *list, struct ast_hashtab *contexttab, struct ast_context *con, const char *registrar)
+{
+ struct ast_context *tmp, *tmpl=NULL;
+ struct ast_exten *exten_item, *prio_item;
+
+ for (tmp = list; tmp; ) {
+ struct ast_context *next = NULL; /* next starting point */
+ /* The following code used to skip forward to the next
+ context with matching registrar, but this didn't
+ make sense; individual priorities registrar'd to
+ the matching registrar could occur in any context! */
+ ast_debug(1, "Investigate ctx %s %s\n", tmp->name, tmp->registrar);
+ if (con) {
+ for (; tmp; tmpl = tmp, tmp = tmp->next) { /* skip to the matching context */
+ ast_debug(1, "check ctx %s %s\n", tmp->name, tmp->registrar);
+ if ( !strcasecmp(tmp->name, con->name) ) {
+ break; /* found it */
+ }
+ }
+ }
+
if (!tmp) /* not found, we are done */
break;
ast_wrlock_context(tmp);
- ast_debug(1, "delete ctx %s %s\n", tmp->name, tmp->registrar);
- ast_hashtab_remove_this_object(contexts_tree,tmp);
-
- next = tmp->next;
- if (tmpl)
- tmpl->next = next;
- else
- contexts = next;
- /* Okay, now we're safe to let it go -- in a sense, we were
- ready to let it go as soon as we locked it. */
- ast_unlock_context(tmp);
- for (tmpi = tmp->includes; tmpi; ) { /* Free includes */
- struct ast_include *tmpil = tmpi;
- tmpi = tmpi->next;
- ast_free(tmpil);
- }
- for (ipi = tmp->ignorepats; ipi; ) { /* Free ignorepats */
- struct ast_ignorepat *ipl = ipi;
- ipi = ipi->next;
- ast_free(ipl);
- }
- /* destroy the hash tabs */
- if (tmp->root_tree) {
- ast_hashtab_destroy(tmp->root_tree, 0);
- }
- /* and destroy the pattern tree */
- if (tmp->pattern_tree)
- destroy_pattern_tree(tmp->pattern_tree);
-
- while ((sw = AST_LIST_REMOVE_HEAD(&tmp->alts, list)))
- ast_free(sw);
- for (e = tmp->root; e;) {
- for (en = e->peer; en;) {
- el = en;
- en = en->peer;
- destroy_exten(el);
+
+ if (registrar) {
+ /* then search thru and remove any extens that match registrar. */
+ struct ast_hashtab_iter *exten_iter;
+ struct ast_hashtab_iter *prio_iter;
+ struct ast_ignorepat *ip, *ipl = NULL, *ipn = NULL;
+ struct ast_include *i, *pi = NULL, *ni = NULL;
+ struct ast_sw *sw = NULL;
+
+ /* remove any ignorepats whose registrar matches */
+ for (ip = tmp->ignorepats; ip; ip = ipn) {
+ ipn = ip->next;
+ if (!strcmp(ip->registrar, registrar)) {
+ if (ipl) {
+ ipl->next = ip->next;
+ ast_free(ip);
+ continue; /* don't change ipl */
+ } else {
+ tmp->ignorepats = ip->next;
+ ast_free(ip);
+ continue; /* don't change ipl */
+ }
+ }
+ ipl = ip;
+ }
+ /* remove any includes whose registrar matches */
+ for (i = tmp->includes; i; i = ni) {
+ ni = i->next;
+ if (strcmp(i->registrar, registrar) == 0) {
+ /* remove from list */
+ if (pi) {
+ pi->next = i->next;
+ /* free include */
+ ast_free(i);
+ continue; /* don't change pi */
+ } else {
+ tmp->includes = i->next;
+ /* free include */
+ ast_free(i);
+ continue; /* don't change pi */
+ }
+ }
+ pi = i;
+ }
+ /* remove any switches whose registrar matches */
+ AST_LIST_TRAVERSE_SAFE_BEGIN(&tmp->alts, sw, list) {
+ if (strcmp(sw->registrar,registrar) == 0) {
+ AST_LIST_REMOVE_CURRENT(list);
+ ast_free(sw);
+ }
+ }
+ AST_LIST_TRAVERSE_SAFE_END
+
+ if (tmp->root_table) { /* it is entirely possible that the context is EMPTY */
+ exten_iter = ast_hashtab_start_traversal(tmp->root_table);
+ while ((exten_item=ast_hashtab_next(exten_iter))) {
+ prio_iter = ast_hashtab_start_traversal(exten_item->peer_table);
+ while ((prio_item=ast_hashtab_next(prio_iter))) {
+ if (!prio_item->registrar || strcmp(prio_item->registrar, registrar) != 0) {
+ continue;
+ }
+ ast_verb(3, "Remove %s/%s/%d, registrar=%s; con=%s(%p); con->root=%p\n",
+ tmp->name, prio_item->exten, prio_item->priority, registrar, con? con->name : "<nil>", con, con? con->root_table: NULL);
+ /* set matchcid to 1 to insure we get a direct match, and NULL registrar to make sure no wildcarding is done */
+ ast_context_remove_extension_callerid2(tmp, prio_item->exten, prio_item->priority, prio_item->cidmatch, 1, NULL, 1);
+ }
+ ast_hashtab_end_traversal(prio_iter);
+ }
+ ast_hashtab_end_traversal(exten_iter);
}
- el = e;
- e = e->next;
- destroy_exten(el);
+
+ /* delete the context if it's registrar matches, is empty, has refcount of 1, */
+ /* it's not empty, if it has includes, ignorepats, or switches that are registered from
+ another registrar. It's not empty if there are any extensions */
+ if (strcmp(tmp->registrar, registrar) == 0 && tmp->refcount < 2 && !tmp->root && !tmp->ignorepats && !tmp->includes && AST_LIST_EMPTY(&tmp->alts)) {
+ ast_debug(1, "delete ctx %s %s\n", tmp->name, tmp->registrar);
+ ast_hashtab_remove_this_object(contexttab, tmp);
+
+ next = tmp->next;
+ if (tmpl)
+ tmpl->next = next;
+ else
+ contexts = next;
+ /* Okay, now we're safe to let it go -- in a sense, we were
+ ready to let it go as soon as we locked it. */
+ ast_unlock_context(tmp);
+ __ast_internal_context_destroy(tmp);
+ } else {
+ ast_debug(1,"Couldn't delete ctx %s/%s; refc=%d; tmp.root=%p\n", tmp->name, tmp->registrar,
+ tmp->refcount, tmp->root);
+ ast_unlock_context(tmp);
+ next = tmp->next;
+ tmpl = tmp;
+ }
+ } else if (con) {
+ ast_verb(3, "Deleting context %s registrar=%s\n", tmp->name, tmp->registrar);
+ ast_debug(1, "delete ctx %s %s\n", tmp->name, tmp->registrar);
+ ast_hashtab_remove_this_object(contexttab, tmp);
+
+ next = tmp->next;
+ if (tmpl)
+ tmpl->next = next;
+ else
+ contexts = next;
+ /* Okay, now we're safe to let it go -- in a sense, we were
+ ready to let it go as soon as we locked it. */
+ ast_unlock_context(tmp);
+ __ast_internal_context_destroy(tmp);
}
- ast_rwlock_destroy(&tmp->lock);
- ast_free(tmp);
+
/* if we have a specific match, we are done, otherwise continue */
tmp = con ? NULL : next;
}
@@ -6952,7 +7425,7 @@ void __ast_context_destroy(struct ast_context *con, const char *registrar)
void ast_context_destroy(struct ast_context *con, const char *registrar)
{
ast_wrlock_contexts();
- __ast_context_destroy(con,registrar);
+ __ast_context_destroy(contexts, contexts_table, con,registrar);
ast_unlock_contexts();
}
@@ -7106,7 +7579,7 @@ static int pbx_builtin_hangup(struct ast_channel *chan, void *data)
return -1;
}
- ast_log(LOG_NOTICE, "Invalid cause given to Hangup(): \"%s\"\n", (char *) data);
+ ast_log(LOG_WARNING, "Invalid cause given to Hangup(): \"%s\"\n", (char *) data);
}
if (!chan->hangupcause) {
@@ -7786,13 +8259,22 @@ int load_pbx(void)
return 0;
}
+static int conlock_wrlock_version = 0;
+
+int ast_wrlock_contexts_version(void)
+{
+ return conlock_wrlock_version;
+}
/*
* Lock context list functions ...
*/
int ast_wrlock_contexts()
{
- return ast_rwlock_wrlock(&conlock);
+ int res = ast_rwlock_wrlock(&conlock);
+ if (!res)
+ ast_atomic_fetchadd_int(&conlock_wrlock_version, 1);
+ return res;
}
int ast_rdlock_contexts()
@@ -7914,6 +8396,11 @@ const char *ast_get_switch_data(struct ast_sw *sw)
return sw ? sw->data : NULL;
}
+int ast_get_switch_eval(struct ast_sw *sw)
+{
+ return sw->eval;
+}
+
const char *ast_get_switch_registrar(struct ast_sw *sw)
{
return sw ? sw->registrar : NULL;