]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
Use locking when accessing the registrations list. This list is not actually
authorRussell Bryant <russell@russellbryant.com>
Sat, 9 Dec 2006 15:45:37 +0000 (15:45 +0000)
committerRussell Bryant <russell@russellbryant.com>
Sat, 9 Dec 2006 15:45:37 +0000 (15:45 +0000)
used very often, so the likelihood of there being a problem is pretty small,
but still possible.  For example, if the CLI command to list the registrations
was called at the same time that a reload was occurring and the registrations
list was getting destroyed and rebuilt, a crash could occur.

git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/1.2@48361 65c4cc65-6c06-0410-ace0-fbb531ad65f3

channels/chan_iax2.c

index 21fcf2604977795c4ff0fec181c69861a0ff22f4..e896abeb3eab4d7c4200fcc6237fa9bd3a626533 100644 (file)
@@ -410,6 +410,7 @@ struct iax2_registry {
 };
 
 static struct iax2_registry *registrations;
+AST_MUTEX_DEFINE_STATIC(reg_lock);
 
 /* Don't retry more frequently than every 10 ms, or less frequently than every 5 seconds */
 #define MIN_RETRY_TIME         100
@@ -4399,8 +4400,8 @@ static int iax2_show_registry(int fd, int argc, char *argv[])
        char iabuf[INET_ADDRSTRLEN];
        if (argc != 3)
                return RESULT_SHOWUSAGE;
-       ast_mutex_lock(&peerl.lock);
        ast_cli(fd, FORMAT2, "Host", "Username", "Perceived", "Refresh", "State");
+       ast_mutex_lock(&reg_lock);
        for (reg = registrations;reg;reg = reg->next) {
                snprintf(host, sizeof(host), "%s:%d", ast_inet_ntoa(iabuf, sizeof(iabuf), reg->addr.sin_addr), ntohs(reg->addr.sin_port));
                if (reg->us.sin_addr.s_addr) 
@@ -4410,7 +4411,7 @@ static int iax2_show_registry(int fd, int argc, char *argv[])
                ast_cli(fd, FORMAT, host, 
                                        reg->username, perceived, reg->refresh, regstate2str(reg->regstate));
        }
-       ast_mutex_unlock(&peerl.lock);
+       ast_mutex_unlock(&reg_lock);
        return RESULT_SUCCESS;
 #undef FORMAT
 #undef FORMAT2
@@ -5624,9 +5625,11 @@ static int iax2_register(char *value, int lineno)
                reg->addr.sin_family = AF_INET;
                memcpy(&reg->addr.sin_addr, hp->h_addr, sizeof(&reg->addr.sin_addr));
                reg->addr.sin_port = porta ? htons(atoi(porta)) : htons(IAX_DEFAULT_PORTNO);
+               ast_mutex_lock(&reg_lock);
                reg->next = registrations;
                reg->callno = 0;
                registrations = reg;
+               ast_mutex_unlock(&reg_lock);
        } else {
                ast_log(LOG_ERROR, "Out of memory\n");
                return -1;
@@ -8582,6 +8585,7 @@ static void delete_users(void)
                user = user->next;
        }
        ast_mutex_unlock(&userl.lock);
+       ast_mutex_lock(&reg_lock);
        for (reg = registrations;reg;) {
                regl = reg;
                reg = reg->next;
@@ -8600,6 +8604,7 @@ static void delete_users(void)
                free(regl);
        }
        registrations = NULL;
+       ast_mutex_unlock(&reg_lock);
        ast_mutex_lock(&peerl.lock);
        for (peer=peerl.peers;peer;) {
                /* Assume all will be deleted, and we'll find out for sure later */
@@ -8976,8 +8981,10 @@ static int reload_config(void)
        set_config(config,1);
        prune_peers();
        prune_users();
+       ast_mutex_lock(&reg_lock);
        for (reg = registrations; reg; reg = reg->next)
                iax2_do_register(reg);
+       ast_mutex_unlock(&reg_lock);
        /* Qualify hosts, too */
        ast_mutex_lock(&peerl.lock);
        for (peer = peerl.peers; peer; peer = peer->next)
@@ -9742,8 +9749,10 @@ int load_module(void)
                ast_netsock_release(netsock);
        }
 
+       ast_mutex_lock(&reg_lock);
        for (reg = registrations; reg; reg = reg->next)
                iax2_do_register(reg);
+       ast_mutex_unlock(&reg_lock);
        ast_mutex_lock(&peerl.lock);
        for (peer = peerl.peers; peer; peer = peer->next) {
                if (peer->sockfd < 0)