improvement towards stable mod_sofia that can start/stop profiles (see i told ya this kind of thing is a slippery slope)

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@5074 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Anthony Minessale 2007-05-03 01:55:25 +00:00
parent 81801594f5
commit 9c77125eac
6 changed files with 120 additions and 66 deletions

View File

@ -1022,7 +1022,7 @@ static switch_status_t cmd_status(char **argv, int argc, switch_stream_handle_t
stream->write_function(stream, "%s\n", line); stream->write_function(stream, "%s\n", line);
switch_thread_rwlock_unlock(profile->rwlock); sofia_glue_release_profile(profile);
} else { } else {
stream->write_function(stream, "Invalid Profile!\n"); stream->write_function(stream, "Invalid Profile!\n");
} }
@ -1047,8 +1047,8 @@ static switch_status_t cmd_status(char **argv, int argc, switch_stream_handle_t
ac++; ac++;
stream->write_function(stream, "%25s\t%s\t %32s\t%s\n", vvar, " alias", profile->name, "ALIASED"); stream->write_function(stream, "%25s\t%s\t %32s\t%s\n", vvar, " alias", profile->name, "ALIASED");
} else { } else {
stream->write_function(stream, "%25s\t%s\t %32s\t%s\n", profile->name, "profile", profile->url, stream->write_function(stream, "%25s\t%s\t %32s\t%s (%u)\n", profile->name, "profile", profile->url,
sofia_test_pflag(profile, PFLAG_RUNNING) ? "RUNNING" : "DOWN"); sofia_test_pflag(profile, PFLAG_RUNNING) ? "RUNNING" : "DOWN", profile->inuse);
c++; c++;
for (gp = profile->gateways; gp; gp = gp->next) { for (gp = profile->gateways; gp; gp = gp->next) {
@ -1072,24 +1072,21 @@ static switch_status_t cmd_profile(char **argv, int argc, switch_stream_handle_t
{ {
sofia_profile_t *profile = NULL; sofia_profile_t *profile = NULL;
char *profile_name = argv[0]; char *profile_name = argv[0];
const char *err;
switch_xml_t xml_root;
if (argc < 2) { if (argc < 2) {
stream->write_function(stream, "Invalid Args!\n"); stream->write_function(stream, "Invalid Args!\n");
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
} }
if (!strcasecmp(argv[1], "start")) {
if (argc > 2 && !strcasecmp(argv[2], "reloadxml")) { if (argc > 2 && !strcasecmp(argv[2], "reloadxml")) {
const char *err;
switch_xml_t xml_root;
if ((xml_root = switch_xml_open_root(1, &err))) { if ((xml_root = switch_xml_open_root(1, &err))) {
switch_xml_free(xml_root); switch_xml_free(xml_root);
} }
stream->write_function(stream, "Reload XML [%s]\n", err); stream->write_function(stream, "Reload XML [%s]\n", err);
} }
if (!strcasecmp(argv[1], "start")) {
if (config_sofia(1, argv[0]) == SWITCH_STATUS_SUCCESS) { if (config_sofia(1, argv[0]) == SWITCH_STATUS_SUCCESS) {
stream->write_function(stream, "%s started successfully\n", argv[0]); stream->write_function(stream, "%s started successfully\n", argv[0]);
} else { } else {
@ -1103,23 +1100,35 @@ static switch_status_t cmd_profile(char **argv, int argc, switch_stream_handle_t
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
} }
if (!strcasecmp(argv[1], "stop") || !strcasecmp(argv[1], "restart")) {
int rsec = 30;
int diff = (int) (time(NULL) - profile->started);
int remain = rsec - diff;
if (diff < rsec) {
stream->write_function(stream, "Profile %s must be up for at least %d seconds to stop/restart.\nPlease wait %d second%s\n",
profile->name, rsec, remain, remain == 1 ? "" : "s");
} else {
if (argc > 2 && !strcasecmp(argv[2], "reloadxml")) {
if ((xml_root = switch_xml_open_root(1, &err))) {
switch_xml_free(xml_root);
}
stream->write_function(stream, "Reload XML [%s]\n", err);
}
if (!strcasecmp(argv[1], "stop")) { if (!strcasecmp(argv[1], "stop")) {
sofia_clear_pflag_locked(profile, PFLAG_RUNNING); sofia_clear_pflag_locked(profile, PFLAG_RUNNING);
stream->write_function(stream, "stopping: %s", profile->name); stream->write_function(stream, "stopping: %s", profile->name);
} else if (!strcasecmp(argv[1], "restart")) {
int rsec = 30;
if (time(NULL) - profile->started < rsec) {
stream->write_function(stream, "Profile %s must be up for at least %d seconds to restart\n", rsec, profile->name);
} else { } else {
sofia_set_pflag_locked(profile, PFLAG_RESPAWN); sofia_set_pflag_locked(profile, PFLAG_RESPAWN);
sofia_clear_pflag_locked(profile, PFLAG_RUNNING); sofia_clear_pflag_locked(profile, PFLAG_RUNNING);
stream->write_function(stream, "restarting: %s", profile->name); stream->write_function(stream, "restarting: %s", profile->name);
} }
} }
}
if (profile) { if (profile) {
switch_thread_rwlock_unlock(profile->rwlock); sofia_glue_release_profile(profile);
} }
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
@ -1328,7 +1337,6 @@ static switch_call_cause_t sofia_outgoing_channel(switch_core_session_t *session
tech_pvt->dest = switch_core_session_sprintf(nsession, "sip:%s", dest); tech_pvt->dest = switch_core_session_sprintf(nsession, "sip:%s", dest);
} }
tech_pvt->invite_contact = switch_core_session_strdup(nsession, gateway_ptr->register_contact); tech_pvt->invite_contact = switch_core_session_strdup(nsession, gateway_ptr->register_contact);
sofia_reg_release_gateway(gateway_ptr);
} else { } else {
if (!(dest = strchr(profile_name, '/'))) { if (!(dest = strchr(profile_name, '/'))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid URL\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid URL\n");
@ -1416,7 +1424,7 @@ static switch_call_cause_t sofia_outgoing_channel(switch_core_session_t *session
done: done:
if (profile) { if (profile) {
switch_thread_rwlock_unlock(profile->rwlock); sofia_glue_release_profile(profile);
} }
return cause; return cause;

View File

@ -439,15 +439,13 @@ void sofia_presence_handle_sip_i_subscribe(int status,
char const *phrase, char const *phrase,
nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, sofia_private_t *sofia_private, sip_t const *sip, tagi_t tags[]); nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, sofia_private_t *sofia_private, sip_t const *sip, tagi_t tags[]);
sofia_profile_t *sofia_glue_find_profile(char *key);
switch_status_t sofia_glue_add_profile(char *key, sofia_profile_t *profile);
void sofia_glue_execute_sql(sofia_profile_t *profile, switch_bool_t master, char *sql, switch_mutex_t *mutex); void sofia_glue_execute_sql(sofia_profile_t *profile, switch_bool_t master, char *sql, switch_mutex_t *mutex);
void sofia_reg_check_expire(sofia_profile_t *profile, time_t now); void sofia_reg_check_expire(sofia_profile_t *profile, time_t now);
void sofia_reg_check_gateway(sofia_profile_t *profile, time_t now); void sofia_reg_check_gateway(sofia_profile_t *profile, time_t now);
void sofia_reg_unregister(sofia_profile_t *profile); void sofia_reg_unregister(sofia_profile_t *profile);
switch_status_t sofia_glue_ext_address_lookup(char **ip, switch_port_t *port, char *sourceip, switch_memory_pool_t *pool); switch_status_t sofia_glue_ext_address_lookup(char **ip, switch_port_t *port, char *sourceip, switch_memory_pool_t *pool);
sofia_gateway_t *sofia_reg_find_gateway(char *key);
switch_status_t sofia_reg_add_gateway(char *key, sofia_gateway_t *gateway);
void sofia_glue_pass_sdp(private_object_t *tech_pvt, char *sdp); void sofia_glue_pass_sdp(private_object_t *tech_pvt, char *sdp);
int sofia_glue_get_user_host(char *in, char **user, char **host); int sofia_glue_get_user_host(char *in, char **user, char **host);
switch_call_cause_t sofia_glue_sip_cause_to_freeswitch(int status); switch_call_cause_t sofia_glue_sip_cause_to_freeswitch(int status);
@ -469,5 +467,20 @@ switch_bool_t sofia_glue_execute_sql_callback(sofia_profile_t *profile,
void *pdata); void *pdata);
char *sofia_glue_execute_sql2str(sofia_profile_t *profile, switch_mutex_t *mutex, char *sql, char *resbuf, size_t len); char *sofia_glue_execute_sql2str(sofia_profile_t *profile, switch_mutex_t *mutex, char *sql, char *resbuf, size_t len);
void sofia_glue_check_video_codecs(private_object_t *tech_pvt); void sofia_glue_check_video_codecs(private_object_t *tech_pvt);
void sofia_reg_release_gateway(sofia_gateway_t *gateway);
void sofia_glue_del_profile(sofia_profile_t *profile); void sofia_glue_del_profile(sofia_profile_t *profile);
switch_status_t sofia_glue_add_profile(char *key, sofia_profile_t *profile);
void sofia_glue_release_profile__(const char *file, const char *func, int line, sofia_profile_t *profile);
#define sofia_glue_release_profile(x) sofia_glue_release_profile__(__FILE__, __SWITCH_FUNC__, __LINE__, x)
sofia_profile_t *sofia_glue_find_profile__(const char *file, const char *func, int line, char *key);
#define sofia_glue_find_profile(x) sofia_glue_find_profile__(__FILE__, __SWITCH_FUNC__, __LINE__, x)
switch_status_t sofia_reg_add_gateway(char *key, sofia_gateway_t *gateway);
sofia_gateway_t *sofia_reg_find_gateway__(const char *file, const char *func, int line, char *key);
#define sofia_reg_find_gateway(x) sofia_reg_find_gateway__(__FILE__, __SWITCH_FUNC__, __LINE__, x)
void sofia_reg_release_gateway__(const char *file, const char *func, int line, sofia_gateway_t *gateway);
#define sofia_reg_release_gateway(x) sofia_reg_release_gateway__(__FILE__, __SWITCH_FUNC__, __LINE__, x);

View File

@ -237,7 +237,7 @@ void event_handler(switch_event_t *event)
} }
if (profile) { if (profile) {
switch_thread_rwlock_unlock(profile->rwlock); sofia_glue_release_profile(profile);
} }
} }
} }
@ -615,7 +615,7 @@ switch_status_t config_sofia(int reload, char *profile_name)
if (!switch_strlen_zero(profile_name) && (profile = sofia_glue_find_profile(profile_name))) { if (!switch_strlen_zero(profile_name) && (profile = sofia_glue_find_profile(profile_name))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Profile [%s] Already exists.\n", switch_str_nil(profile_name)); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Profile [%s] Already exists.\n", switch_str_nil(profile_name));
status = SWITCH_STATUS_FALSE; status = SWITCH_STATUS_FALSE;
switch_thread_rwlock_unlock(profile->rwlock); sofia_glue_release_profile(profile);
return status; return status;
} }

View File

@ -1418,29 +1418,44 @@ char *sofia_glue_get_url_from_contact(char *buf, uint8_t to_dup)
return url; return url;
} }
sofia_profile_t *sofia_glue_find_profile__(const char *file, const char *func, int line, char *key)
sofia_profile_t *sofia_glue_find_profile(char *key)
{ {
sofia_profile_t *profile; sofia_profile_t *profile;
switch_mutex_lock(mod_sofia_globals.hash_mutex); 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 = (sofia_profile_t *) switch_core_hash_find(mod_sofia_globals.profile_hash, key))) {
if (!sofia_test_pflag(profile, PFLAG_RUNNING)) { if (switch_thread_rwlock_tryrdlock(profile->rwlock) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Profile %s is not running\n", profile->name); #ifdef SOFIA_DEBUG_RWLOCKS
profile = NULL; switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, SWITCH_LOG_ERROR, "Profile %s is locked\n", profile->name);
} else if (switch_thread_rwlock_tryrdlock(profile->rwlock) != SWITCH_STATUS_SUCCESS) { #endif
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Profile %s is locked\n", profile->name);
profile = NULL; profile = NULL;
} }
} else { } else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Profile %s is not in the hash\n", profile->name); #ifdef SOFIA_DEBUG_RWLOCKS
switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, SWITCH_LOG_ERROR, "Profile %s is not in the hash\n", key);
#endif
} }
#ifdef SOFIA_DEBUG_RWLOCKS
if (profile) {
switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, SWITCH_LOG_ERROR, "XXXXXXXXXXXXXX LOCK %s\n", profile->name);
}
#endif
switch_mutex_unlock(mod_sofia_globals.hash_mutex); switch_mutex_unlock(mod_sofia_globals.hash_mutex);
return profile; return profile;
} }
void sofia_glue_release_profile__(const char *file, const char *func, int line, sofia_profile_t *profile)
{
if (profile) {
#ifdef SOFIA_DEBUG_RWLOCKS
switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, SWITCH_LOG_ERROR, "XXXXXXXXXXXXXX UNLOCK %s\n", profile->name);
#endif
switch_thread_rwlock_unlock(profile->rwlock);
}
}
switch_status_t sofia_glue_add_profile(char *key, sofia_profile_t *profile) switch_status_t sofia_glue_add_profile(char *key, sofia_profile_t *profile)
{ {
switch_status_t status = SWITCH_STATUS_FALSE; switch_status_t status = SWITCH_STATUS_FALSE;

View File

@ -234,11 +234,9 @@ void sofia_presence_mwi_event_handler(switch_event_t *event)
switch_safe_free(sql); switch_safe_free(sql);
switch_safe_free(dup_account); switch_safe_free(dup_account);
if (profile) { if (profile) {
switch_thread_rwlock_unlock(profile->rwlock); sofia_glue_release_profile(profile);
} }
} }
void sofia_presence_event_handler(switch_event_t *event) void sofia_presence_event_handler(switch_event_t *event)
@ -374,7 +372,7 @@ void sofia_presence_event_handler(switch_event_t *event)
profile); profile);
switch_thread_rwlock_unlock(profile->rwlock); sofia_glue_release_profile(profile);
switch_safe_free(sql); switch_safe_free(sql);
} }
@ -619,9 +617,8 @@ static int sofia_presence_mwi_callback(void *pArg, int argc, char **argv, char *
switch_safe_free(id); switch_safe_free(id);
switch_safe_free(exp); switch_safe_free(exp);
if (profile) { sofia_glue_release_profile(profile);
switch_thread_rwlock_unlock(profile->rwlock);
}
return 0; return 0;
} }

View File

@ -553,7 +553,7 @@ void sofia_reg_handle_sip_r_challenge(int status,
} }
if (profile) { if (profile) {
sofia_gateway_t *gateway_ptr; sofia_gateway_t *gateway_ptr = NULL;
if ((duprealm = strdup(realm))) { if ((duprealm = strdup(realm))) {
qrealm = duprealm; qrealm = duprealm;
@ -585,11 +585,15 @@ void sofia_reg_handle_sip_r_challenge(int status,
&& !strcasecmp(gateway_ptr->register_realm, qrealm)) { && !strcasecmp(gateway_ptr->register_realm, qrealm)) {
gateway = gateway_ptr; gateway = gateway_ptr;
if (!switch_test_flag(gateway->profile, PFLAG_RUNNING)) { if (switch_thread_rwlock_tryrdlock(gateway->profile->rwlock) != SWITCH_STATUS_SUCCESS) {
gateway = NULL; #ifdef SOFIA_DEBUG_RWLOCKS
} else if (switch_thread_rwlock_tryrdlock(gateway->profile->rwlock) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Profile %s is locked\n", gateway->profile->name);
#endif
gateway = NULL; gateway = NULL;
} }
#ifdef SOFIA_DEBUG_RWLOCKS
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "XXXXXXXXXXXXXX GW LOCK %s\n", gateway->profile->name);
#endif
break; break;
} }
} }
@ -598,9 +602,14 @@ void sofia_reg_handle_sip_r_challenge(int status,
if (!gateway) { if (!gateway) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No Match for Scheme [%s] Realm [%s]\n", scheme, qrealm); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No Match for Scheme [%s] Realm [%s]\n", scheme, qrealm);
}
switch_safe_free(duprealm);
if (!gateway) {
goto cancel; goto cancel;
} }
switch_safe_free(duprealm);
} else { } else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Memory Error!\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Memory Error!\n");
goto cancel; goto cancel;
@ -617,10 +626,16 @@ void sofia_reg_handle_sip_r_challenge(int status,
tl_gets(tags, NUTAG_CALLSTATE_REF(ss_state), SIPTAG_WWW_AUTHENTICATE_REF(authenticate), TAG_END()); tl_gets(tags, NUTAG_CALLSTATE_REF(ss_state), SIPTAG_WWW_AUTHENTICATE_REF(authenticate), TAG_END());
nua_authenticate(nh, SIPTAG_EXPIRES_STR(gateway->expires_str), NUTAG_AUTH(authentication), TAG_END()); nua_authenticate(nh, SIPTAG_EXPIRES_STR(gateway->expires_str), NUTAG_AUTH(authentication), TAG_END());
if (gateway) {
sofia_reg_release_gateway(gateway); sofia_reg_release_gateway(gateway);
gateway = NULL;
}
return; return;
cancel: cancel:
if (gateway) {
sofia_reg_release_gateway(gateway);
}
if (session) { if (session) {
switch_channel_t *channel = switch_core_session_get_channel(session); switch_channel_t *channel = switch_core_session_get_channel(session);
switch_channel_hangup(channel, SWITCH_CAUSE_MANDATORY_IE_MISSING); switch_channel_hangup(channel, SWITCH_CAUSE_MANDATORY_IE_MISSING);
@ -816,26 +831,32 @@ auth_res_t sofia_reg_parse_auth(sofia_profile_t *profile, sip_authorization_t co
} }
sofia_gateway_t *sofia_reg_find_gateway(char *key) sofia_gateway_t *sofia_reg_find_gateway__(const char *file, const char *func, int line, char *key)
{ {
sofia_gateway_t *gateway = NULL; sofia_gateway_t *gateway = NULL;
switch_mutex_lock(mod_sofia_globals.hash_mutex); 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 = (sofia_gateway_t *) switch_core_hash_find(mod_sofia_globals.gateway_hash, key))) {
if (!sofia_test_pflag(gateway->profile, PFLAG_RUNNING)) { if (switch_thread_rwlock_tryrdlock(gateway->profile->rwlock) != SWITCH_STATUS_SUCCESS) {
gateway = NULL; switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, SWITCH_LOG_ERROR, "Profile %s is locked\n", gateway->profile->name);
} else if (switch_thread_rwlock_tryrdlock(gateway->profile->rwlock) != SWITCH_STATUS_SUCCESS) {
gateway = NULL; gateway = NULL;
} }
} }
if (gateway) {
#ifdef SOFIA_DEBUG_RWLOCKS
switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, SWITCH_LOG_ERROR, "XXXXXXXXXXXXXX GW LOCK %s\n", gateway->profile->name);
#endif
}
switch_mutex_unlock(mod_sofia_globals.hash_mutex); switch_mutex_unlock(mod_sofia_globals.hash_mutex);
return gateway; return gateway;
} }
void sofia_reg_release_gateway(sofia_gateway_t *gateway) void sofia_reg_release_gateway__(const char *file, const char *func, int line, sofia_gateway_t *gateway)
{ {
switch_thread_rwlock_unlock(gateway->profile->rwlock); switch_thread_rwlock_unlock(gateway->profile->rwlock);
#ifdef SOFIA_DEBUG_RWLOCKS
switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, SWITCH_LOG_ERROR, "XXXXXXXXXXXXXX GW UNLOCK %s\n", gateway->profile->name);
#endif
} }
switch_status_t sofia_reg_add_gateway(char *key, sofia_gateway_t *gateway) switch_status_t sofia_reg_add_gateway(char *key, sofia_gateway_t *gateway)