From 86cc41d64631a434e9304ca85fb03e8514cbdd7d Mon Sep 17 00:00:00 2001 From: Michael Jerris Date: Fri, 4 Apr 2008 18:53:24 +0000 Subject: [PATCH] Avoid deadlock, don't leak on duplicate gateway name, error on duplicate gateway name git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@8023 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- src/mod/endpoints/mod_sofia/sofia.c | 19 +++++++++++-------- src/mod/endpoints/mod_sofia/sofia_glue.c | 5 ++++- src/mod/endpoints/mod_sofia/sofia_reg.c | 5 ++++- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/mod/endpoints/mod_sofia/sofia.c b/src/mod/endpoints/mod_sofia/sofia.c index 5418131c15..e30b7d07ec 100644 --- a/src/mod/endpoints/mod_sofia/sofia.c +++ b/src/mod/endpoints/mod_sofia/sofia.c @@ -737,6 +737,14 @@ static void parse_gateways(sofia_profile_t *profile, switch_xml_t gateways_tag) name = "anonymous"; } + switch_mutex_lock(mod_sofia_globals.hash_mutex); + if ((gp = switch_core_hash_find(mod_sofia_globals.gateway_hash, name))) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Ignoring duplicate gateway '%s'\n", name); + switch_mutex_unlock(mod_sofia_globals.hash_mutex); + goto skip; + } + switch_mutex_unlock(mod_sofia_globals.hash_mutex); + if ((gateway = switch_core_alloc(profile->pool, sizeof(*gateway)))) { char *register_str = "true", *scheme = "Digest", *realm = NULL, @@ -893,14 +901,9 @@ static void parse_gateways(sofia_profile_t *profile, switch_xml_t gateways_tag) } gateway->freq -= 2; - if ((gp = sofia_reg_find_gateway(gateway->name))) { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Ignoring duplicate gateway '%s'\n", gateway->name); - sofia_reg_release_gateway(gp); - } else { - gateway->next = profile->gateways; - profile->gateways = gateway; - sofia_reg_add_gateway(gateway->name, gateway); - } + gateway->next = profile->gateways; + profile->gateways = gateway; + sofia_reg_add_gateway(gateway->name, gateway); } skip: diff --git a/src/mod/endpoints/mod_sofia/sofia_glue.c b/src/mod/endpoints/mod_sofia/sofia_glue.c index e1e0689537..1cc43f6365 100644 --- a/src/mod/endpoints/mod_sofia/sofia_glue.c +++ b/src/mod/endpoints/mod_sofia/sofia_glue.c @@ -2285,7 +2285,8 @@ sofia_profile_t *sofia_glue_find_profile__(const char *file, const char *func, i switch_mutex_lock(mod_sofia_globals.hash_mutex); if ((profile = (sofia_profile_t *) switch_core_hash_find(mod_sofia_globals.profile_hash, key))) { if (!(profile->pflags & PFLAG_RUNNING)) { - return NULL; + profile = NULL; + goto done; } if (switch_thread_rwlock_tryrdlock(profile->rwlock) != SWITCH_STATUS_SUCCESS) { #ifdef SOFIA_DEBUG_RWLOCKS @@ -2303,6 +2304,8 @@ sofia_profile_t *sofia_glue_find_profile__(const char *file, const char *func, i switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, SWITCH_LOG_ERROR, "XXXXXXXXXXXXXX LOCK %s\n", profile->name); } #endif + + done: switch_mutex_unlock(mod_sofia_globals.hash_mutex); return profile; diff --git a/src/mod/endpoints/mod_sofia/sofia_reg.c b/src/mod/endpoints/mod_sofia/sofia_reg.c index 2e22a31788..a84d7fa88a 100644 --- a/src/mod/endpoints/mod_sofia/sofia_reg.c +++ b/src/mod/endpoints/mod_sofia/sofia_reg.c @@ -1149,7 +1149,8 @@ sofia_gateway_t *sofia_reg_find_gateway__(const char *file, const char *func, in switch_mutex_lock(mod_sofia_globals.hash_mutex); if ((gateway = (sofia_gateway_t *) switch_core_hash_find(mod_sofia_globals.gateway_hash, key))) { if (!(gateway->profile->pflags & PFLAG_RUNNING)) { - return NULL; + gateway = NULL; + goto done; } if (switch_thread_rwlock_tryrdlock(gateway->profile->rwlock) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_ERROR, "Profile %s is locked\n", gateway->profile->name); @@ -1161,6 +1162,8 @@ sofia_gateway_t *sofia_reg_find_gateway__(const char *file, const char *func, in switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, SWITCH_LOG_ERROR, "XXXXXXXXXXXXXX GW LOCK %s\n", gateway->profile->name); #endif } + + done: switch_mutex_unlock(mod_sofia_globals.hash_mutex); return gateway; }