Use locking when accessing the registrations list. This list is not actually

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.

In passing, go ahead and convert this list to use the linked list macros.


git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/1.4@48363 65c4cc65-6c06-0410-ace0-fbb531ad65f3
1.4
Russell Bryant 19 years ago
parent 0bcedc688d
commit eeb415e8dd

@ -426,11 +426,11 @@ struct iax2_registry {
int messages; /*!< Message count, low 8 bits = new, high 8 bits = old */ int messages; /*!< Message count, low 8 bits = new, high 8 bits = old */
int callno; /*!< Associated call number if applicable */ int callno; /*!< Associated call number if applicable */
struct sockaddr_in us; /*!< Who the server thinks we are */ struct sockaddr_in us; /*!< Who the server thinks we are */
struct iax2_registry *next;
struct ast_dnsmgr_entry *dnsmgr; /*!< DNS refresh manager */ struct ast_dnsmgr_entry *dnsmgr; /*!< DNS refresh manager */
AST_LIST_ENTRY(iax2_registry) entry;
}; };
static struct iax2_registry *registrations; static AST_LIST_HEAD_STATIC(registrations, iax2_registry);
/* Don't retry more frequently than every 10 ms, or less frequently than every 5 seconds */ /* Don't retry more frequently than every 10 ms, or less frequently than every 5 seconds */
#define MIN_RETRY_TIME 100 #define MIN_RETRY_TIME 100
@ -4242,9 +4242,9 @@ static int iax2_show_registry(int fd, int argc, char *argv[])
char perceived[80]; char perceived[80];
if (argc != 3) if (argc != 3)
return RESULT_SHOWUSAGE; return RESULT_SHOWUSAGE;
AST_LIST_LOCK(&peers);
ast_cli(fd, FORMAT2, "Host", "dnsmgr", "Username", "Perceived", "Refresh", "State"); ast_cli(fd, FORMAT2, "Host", "dnsmgr", "Username", "Perceived", "Refresh", "State");
for (reg = registrations;reg;reg = reg->next) { AST_LIST_LOCK(&registrations);
AST_LIST_TRAVERSE(&registrations, reg, entry) {
snprintf(host, sizeof(host), "%s:%d", ast_inet_ntoa(reg->addr.sin_addr), ntohs(reg->addr.sin_port)); snprintf(host, sizeof(host), "%s:%d", ast_inet_ntoa(reg->addr.sin_addr), ntohs(reg->addr.sin_port));
if (reg->us.sin_addr.s_addr) if (reg->us.sin_addr.s_addr)
snprintf(perceived, sizeof(perceived), "%s:%d", ast_inet_ntoa(reg->us.sin_addr), ntohs(reg->us.sin_port)); snprintf(perceived, sizeof(perceived), "%s:%d", ast_inet_ntoa(reg->us.sin_addr), ntohs(reg->us.sin_port));
@ -4254,7 +4254,7 @@ static int iax2_show_registry(int fd, int argc, char *argv[])
(reg->dnsmgr) ? "Y" : "N", (reg->dnsmgr) ? "Y" : "N",
reg->username, perceived, reg->refresh, regstate2str(reg->regstate)); reg->username, perceived, reg->refresh, regstate2str(reg->regstate));
} }
AST_LIST_UNLOCK(&peers); AST_LIST_UNLOCK(&registrations);
return RESULT_SUCCESS; return RESULT_SUCCESS;
#undef FORMAT #undef FORMAT
#undef FORMAT2 #undef FORMAT2
@ -5413,9 +5413,9 @@ static int iax2_register(char *value, int lineno)
reg->refresh = IAX_DEFAULT_REG_EXPIRE; reg->refresh = IAX_DEFAULT_REG_EXPIRE;
reg->addr.sin_family = AF_INET; reg->addr.sin_family = AF_INET;
reg->addr.sin_port = porta ? htons(atoi(porta)) : htons(IAX_DEFAULT_PORTNO); reg->addr.sin_port = porta ? htons(atoi(porta)) : htons(IAX_DEFAULT_PORTNO);
reg->next = registrations; AST_LIST_LOCK(&registrations);
reg->callno = 0; AST_LIST_INSERT_HEAD(&registrations, reg, entry);
registrations = reg; AST_LIST_UNLOCK(&registrations);
return 0; return 0;
} }
@ -8622,35 +8622,32 @@ static struct iax2_user *build_user(const char *name, struct ast_variable *v, st
static void delete_users(void) static void delete_users(void)
{ {
struct iax2_user *user = NULL; struct iax2_user *user;
struct iax2_peer *peer = NULL; struct iax2_peer *peer;
struct iax2_registry *reg, *regl; struct iax2_registry *reg;
AST_LIST_LOCK(&users); AST_LIST_LOCK(&users);
AST_LIST_TRAVERSE(&users, user, entry) AST_LIST_TRAVERSE(&users, user, entry)
ast_set_flag(user, IAX_DELME); ast_set_flag(user, IAX_DELME);
AST_LIST_UNLOCK(&users); AST_LIST_UNLOCK(&users);
for (reg = registrations;reg;) { AST_LIST_LOCK(&registrations);
regl = reg; while ((reg = AST_LIST_REMOVE_HEAD(&registrations, entry))) {
reg = reg->next; if (reg->expire > -1)
if (regl->expire > -1) { ast_sched_del(sched, reg->expire);
ast_sched_del(sched, regl->expire); if (reg->callno) {
} ast_mutex_lock(&iaxsl[reg->callno]);
if (regl->callno) { if (iaxs[reg->callno]) {
/* XXX Is this a potential lock? I don't think so, but you never know */ iaxs[reg->callno]->reg = NULL;
ast_mutex_lock(&iaxsl[regl->callno]); iax2_destroy(reg->callno);
if (iaxs[regl->callno]) {
iaxs[regl->callno]->reg = NULL;
iax2_destroy(regl->callno);
} }
ast_mutex_unlock(&iaxsl[regl->callno]); ast_mutex_unlock(&iaxsl[reg->callno]);
} }
if (regl->dnsmgr) if (reg->dnsmgr)
ast_dnsmgr_release(regl->dnsmgr); ast_dnsmgr_release(reg->dnsmgr);
free(regl); free(reg);
} }
registrations = NULL; AST_LIST_UNLOCK(&registrations);
AST_LIST_LOCK(&peers); AST_LIST_LOCK(&peers);
AST_LIST_TRAVERSE(&peers, peer, entry) AST_LIST_TRAVERSE(&peers, peer, entry)
@ -9092,7 +9089,7 @@ static int reload_config(void)
{ {
char *config = "iax.conf"; char *config = "iax.conf";
struct iax2_registry *reg; struct iax2_registry *reg;
struct iax2_peer *peer = NULL; struct iax2_peer *peer;
strcpy(accountcode, ""); strcpy(accountcode, "");
strcpy(language, ""); strcpy(language, "");
@ -9105,11 +9102,13 @@ static int reload_config(void)
ast_clear_flag((&globalflags), IAX_USEJITTERBUF); ast_clear_flag((&globalflags), IAX_USEJITTERBUF);
ast_clear_flag((&globalflags), IAX_FORCEJITTERBUF); ast_clear_flag((&globalflags), IAX_FORCEJITTERBUF);
delete_users(); delete_users();
set_config(config,1); set_config(config, 1);
prune_peers(); prune_peers();
prune_users(); prune_users();
for (reg = registrations; reg; reg = reg->next) AST_LIST_LOCK(&registrations);
AST_LIST_TRAVERSE(&registrations, reg, entry)
iax2_do_register(reg); iax2_do_register(reg);
AST_LIST_UNLOCK(&registrations);
/* Qualify hosts, too */ /* Qualify hosts, too */
AST_LIST_LOCK(&peers); AST_LIST_LOCK(&peers);
AST_LIST_TRAVERSE(&peers, peer, entry) AST_LIST_TRAVERSE(&peers, peer, entry)
@ -10024,8 +10023,11 @@ static int load_module(void)
ast_netsock_release(netsock); ast_netsock_release(netsock);
} }
for (reg = registrations; reg; reg = reg->next) AST_LIST_LOCK(&registrations);
AST_LIST_TRAVERSE(&registrations, reg, entry)
iax2_do_register(reg); iax2_do_register(reg);
AST_LIST_UNLOCK(&registrations);
AST_LIST_LOCK(&peers); AST_LIST_LOCK(&peers);
AST_LIST_TRAVERSE(&peers, peer, entry) { AST_LIST_TRAVERSE(&peers, peer, entry) {
if (peer->sockfd < 0) if (peer->sockfd < 0)

Loading…
Cancel
Save