add locking to protect the list of global dialplan variables
authorRussell Bryant <russell@russellbryant.com>
Sun, 12 Mar 2006 19:28:57 +0000 (19:28 +0000)
committerRussell Bryant <russell@russellbryant.com>
Sun, 12 Mar 2006 19:28:57 +0000 (19:28 +0000)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@12647 65c4cc65-6c06-0410-ace0-fbb531ad65f3

pbx.c

diff --git a/pbx.c b/pbx.c
index cef327d..458f9d7 100644 (file)
--- a/pbx.c
+++ b/pbx.c
@@ -222,6 +222,7 @@ static int pbx_builtin_sayphonetic(struct ast_channel *, void *);
 int pbx_builtin_setvar(struct ast_channel *, void *);
 static int pbx_builtin_importvar(struct ast_channel *, void *);
 
+AST_MUTEX_DEFINE_STATIC(globalslock);
 static struct varshead globals;
 
 static int autofallthrough = 0;
@@ -974,12 +975,16 @@ void pbx_retrieve_variable(struct ast_channel *c, const char *var, char **ret, c
                struct ast_var_t *variables;
                if (!places[i])
                        continue;
+               if (places[i] == &globals)
+                       ast_mutex_lock(&globalslock);
                AST_LIST_TRAVERSE(places[i], variables, entries) {
                        if (strcasecmp(ast_var_name(variables), var)==0) {
                                s = ast_var_value(variables);
                                break;
                        }
                }
+               if (places[i] == &globals)
+                       ast_mutex_unlock(&globalslock);
        }
        if (s == &not_found || s == NULL)
                *ret = NULL;
@@ -4390,10 +4395,12 @@ int ast_add_extension2(struct ast_context *con,
        /* if we are adding a hint, and there are global variables, and the hint
           contains variable references, then expand them
        */
+       ast_mutex_lock(&globalslock);
        if ((priority == PRIORITY_HINT) && AST_LIST_FIRST(&globals) && strstr(application, "${")) {
                pbx_substitute_variables_varshead(&globals, application, expand_buf, sizeof(expand_buf));
                application = expand_buf;
        }
+       ast_mutex_unlock(&globalslock);
 
        length = sizeof(struct ast_exten);
        length += strlen(extension) + 1;
@@ -5500,6 +5507,7 @@ int pbx_builtin_serialize_variables(struct ast_channel *chan, char *buf, size_t
 const char *pbx_builtin_getvar_helper(struct ast_channel *chan, const char *name) 
 {
        struct ast_var_t *variables;
+       const char *ret = NULL;
        int i;
        struct varshead *places[2] = { NULL, &globals };
        
@@ -5509,14 +5517,23 @@ const char *pbx_builtin_getvar_helper(struct ast_channel *chan, const char *name
                places[0] = &chan->varshead;
 
        for (i = 0; i < 2; i++) {
-               if (places[i]) {
-                       AST_LIST_TRAVERSE(places[i], variables, entries) {
-                               if (!strcmp(name, ast_var_name(variables)))
-                                       return ast_var_value(variables);
+               if (!places[i])
+                       continue;
+               if (places[i] == &globals)
+                       ast_mutex_lock(&globalslock);
+               AST_LIST_TRAVERSE(places[i], variables, entries) {
+                       if (!strcmp(name, ast_var_name(variables))) {
+                               ret = ast_var_value(variables);
+                               break;
                        }
                }
+               if (places[i] == &globals)
+                       ast_mutex_unlock(&globalslock);
+               if (ret)
+                       break;
        }
-       return NULL;
+
+       return ret;
 }
 
 void pbx_builtin_pushvar_helper(struct ast_channel *chan, const char *name, const char *value)
@@ -5537,8 +5554,12 @@ void pbx_builtin_pushvar_helper(struct ast_channel *chan, const char *name, cons
        if (value) {
                if ((option_verbose > 1) && (headp == &globals))
                        ast_verbose(VERBOSE_PREFIX_2 "Setting global variable '%s' to '%s'\n", name, value);
-               newvariable = ast_var_assign(name, value);      
+               newvariable = ast_var_assign(name, value);
+               if (headp == &globals)
+                       ast_mutex_lock(&globalslock);
                AST_LIST_INSERT_HEAD(headp, newvariable, entries);
+               if (headp == &globals)
+                       ast_mutex_unlock(&globalslock);
        }
 }
 
@@ -5564,6 +5585,8 @@ void pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const
                        nametail++;
        }
 
+       if (headp == &globals)
+               ast_mutex_lock(&globalslock);
        AST_LIST_TRAVERSE (headp, newvariable, entries) {
                if (strcasecmp(ast_var_name(newvariable), nametail) == 0) {
                        /* there is already such a variable, delete it */
@@ -5571,14 +5594,17 @@ void pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const
                        ast_var_delete(newvariable);
                        break;
                }
-       } 
-
+       }
+       
        if (value) {
                if ((option_verbose > 1) && (headp == &globals))
                        ast_verbose(VERBOSE_PREFIX_2 "Setting global variable '%s' to '%s'\n", name, value);
                newvariable = ast_var_assign(name, value);      
                AST_LIST_INSERT_HEAD(headp, newvariable, entries);
        }
+       
+       if (headp == &globals)
+               ast_mutex_unlock(&globalslock);
 }
 
 int pbx_builtin_setvar(struct ast_channel *chan, void *data)
@@ -5681,10 +5707,11 @@ static int pbx_builtin_noop(struct ast_channel *chan, void *data)
 void pbx_builtin_clear_globals(void)
 {
        struct ast_var_t *vardata;
-       while (!AST_LIST_EMPTY(&globals)) {
-               vardata = AST_LIST_REMOVE_HEAD(&globals, entries);
+
+       ast_mutex_lock(&globalslock);
+       while ((vardata = AST_LIST_REMOVE_HEAD(&globals, entries)))
                ast_var_delete(vardata);
-       }
+       ast_mutex_unlock(&globalslock);
 }
 
 static int pbx_checkcondition(char *condition)