From dff03e35cfd532da630f2d5b802fce36c07d4feb Mon Sep 17 00:00:00 2001 From: Chris Rienzo Date: Wed, 12 Aug 2015 18:00:19 -0400 Subject: [PATCH] FS-7951 #resolve #comment completely clean up mod_rayo if it fails to load --- src/mod/event_handlers/mod_rayo/mod_rayo.c | 159 +++++++++++------- .../mod_rayo/rayo_cpa_component.c | 4 +- .../mod_rayo/rayo_cpa_detector.c | 6 +- .../mod_rayo/rayo_input_component.c | 4 +- .../mod_rayo/rayo_output_component.c | 6 +- 5 files changed, 112 insertions(+), 67 deletions(-) diff --git a/src/mod/event_handlers/mod_rayo/mod_rayo.c b/src/mod/event_handlers/mod_rayo/mod_rayo.c index 28d7ae1013..ef6b0b7a65 100644 --- a/src/mod/event_handlers/mod_rayo/mod_rayo.c +++ b/src/mod/event_handlers/mod_rayo/mod_rayo.c @@ -885,9 +885,15 @@ static void start_deliver_message_thread(switch_memory_pool_t *pool) static void stop_all_threads(void) { globals.shutdown = 1; - switch_queue_interrupt_all(globals.msg_queue); - switch_queue_interrupt_all(globals.offer_queue); - switch_thread_rwlock_wrlock(globals.shutdown_rwlock); + if (globals.msg_queue) { + switch_queue_interrupt_all(globals.msg_queue); + } + if (globals.offer_queue) { + switch_queue_interrupt_all(globals.offer_queue); + } + if (globals.shutdown_rwlock) { + switch_thread_rwlock_wrlock(globals.shutdown_rwlock); + } } /** @@ -5054,6 +5060,79 @@ static switch_status_t list_input(const char *line, const char *cursor, switch_c return list_actors(line, cursor, matches, is_input_actor); } +/** + * Shutdown module on load failure or shutdown from FreeSWITCH core + */ +static switch_status_t do_shutdown(void) +{ + switch_console_del_complete_func("::rayo::list_all"); + switch_console_del_complete_func("::rayo::list_internal"); + switch_console_del_complete_func("::rayo::list_external"); + switch_console_del_complete_func("::rayo::list_server"); + switch_console_del_complete_func("::rayo::list_call"); + switch_console_del_complete_func("::rayo::list_component"); + switch_console_del_complete_func("::rayo::list_record"); + switch_console_del_complete_func("::rayo::list_output"); + switch_console_del_complete_func("::rayo::list_input"); + switch_console_set_complete("del rayo"); + + /* stop XMPP streams */ + if (globals.xmpp_context) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Waiting for XMPP threads to stop\n"); + xmpp_stream_context_destroy(globals.xmpp_context); + } + + /* stop threads */ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Waiting for message and offer timeout threads to stop\n"); + stop_all_threads(); + + if (globals.console) { + RAYO_RELEASE(globals.console); + RAYO_DESTROY(globals.console); + globals.console = NULL; + } + + if (globals.server) { + RAYO_RELEASE(globals.server); + RAYO_DESTROY(globals.server); + globals.server = NULL; + } + + rayo_components_shutdown(); + + switch_event_unbind_callback(route_call_event); + switch_event_unbind_callback(on_call_end_event); + switch_event_unbind_callback(route_mixer_event); + + if (globals.command_handlers) { + switch_core_hash_destroy(&globals.command_handlers); + } + if (globals.event_handlers) { + switch_core_hash_destroy(&globals.event_handlers); + } + if (globals.clients_roster) { + switch_core_hash_destroy(&globals.clients_roster); + } + if (globals.actors) { + switch_core_hash_destroy(&globals.actors); + } + if (globals.destroy_actors) { + switch_core_hash_destroy(&globals.destroy_actors); + } + if (globals.actors_by_id) { + switch_core_hash_destroy(&globals.actors_by_id); + } + if (globals.dial_gateways) { + switch_core_hash_destroy(&globals.dial_gateways); + } + if (globals.cmd_aliases) { + switch_core_hash_destroy(&globals.cmd_aliases); + } + + + return SWITCH_STATUS_SUCCESS; +} + /** * Load module */ @@ -5116,12 +5195,19 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_rayo_load) /* set up rayo components */ if (rayo_components_load(module_interface, pool, RAYO_CONFIG_FILE) != SWITCH_STATUS_SUCCESS) { - return SWITCH_STATUS_TERM; + goto error; } /* configure / open sockets */ if(do_config(globals.pool, RAYO_CONFIG_FILE) != SWITCH_STATUS_SUCCESS) { - return SWITCH_STATUS_TERM; + goto error; + } + + /* create admin client */ + globals.console = rayo_console_client_create(); + if (!globals.console) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Failed to create console client entity!\n"); + goto error; } /* start up message threads */ @@ -5133,13 +5219,6 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_rayo_load) } start_offer_timeout_thread(pool); - /* create admin client */ - globals.console = rayo_console_client_create(); - if (!globals.console) { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Failed to create console client entity!\n"); - return SWITCH_STATUS_TERM; - } - switch_console_set_complete("add rayo status"); switch_console_set_complete("add rayo msg ::rayo::list_all"); switch_console_set_complete("add rayo msg ::rayo::list_all chat"); @@ -5159,6 +5238,11 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_rayo_load) switch_console_add_complete_func("::rayo::list_input", list_input); return SWITCH_STATUS_SUCCESS; + + error: + do_shutdown(); + return SWITCH_STATUS_TERM; + } /** @@ -5166,56 +5250,9 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_rayo_load) */ SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_rayo_shutdown) { - switch_console_del_complete_func("::rayo::list_all"); - switch_console_del_complete_func("::rayo::list_internal"); - switch_console_del_complete_func("::rayo::list_external"); - switch_console_del_complete_func("::rayo::list_server"); - switch_console_del_complete_func("::rayo::list_call"); - switch_console_del_complete_func("::rayo::list_component"); - switch_console_del_complete_func("::rayo::list_record"); - switch_console_del_complete_func("::rayo::list_output"); - switch_console_del_complete_func("::rayo::list_input"); - switch_console_set_complete("del rayo"); - - /* stop XMPP streams */ - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Waiting for XMPP threads to stop\n"); - xmpp_stream_context_destroy(globals.xmpp_context); - - /* stop threads */ - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Waiting for message and offer timeout threads to stop\n"); - stop_all_threads(); - - if (globals.console) { - RAYO_RELEASE(globals.console); - RAYO_DESTROY(globals.console); - globals.console = NULL; - } - - if (globals.server) { - RAYO_RELEASE(globals.server); - RAYO_DESTROY(globals.server); - globals.server = NULL; - } - - rayo_components_shutdown(); - - /* cleanup module */ - switch_event_unbind_callback(route_call_event); - switch_event_unbind_callback(on_call_end_event); - switch_event_unbind_callback(route_mixer_event); - - switch_core_hash_destroy(&globals.command_handlers); - switch_core_hash_destroy(&globals.event_handlers); - switch_core_hash_destroy(&globals.clients_roster); - switch_core_hash_destroy(&globals.actors); - switch_core_hash_destroy(&globals.destroy_actors); - switch_core_hash_destroy(&globals.actors_by_id); - switch_core_hash_destroy(&globals.dial_gateways); - switch_core_hash_destroy(&globals.cmd_aliases); - + switch_status_t result = do_shutdown(); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Module shutdown\n"); - - return SWITCH_STATUS_SUCCESS; + return result; } /** diff --git a/src/mod/event_handlers/mod_rayo/rayo_cpa_component.c b/src/mod/event_handlers/mod_rayo/rayo_cpa_component.c index fb271bd03e..81a708b1a9 100644 --- a/src/mod/event_handlers/mod_rayo/rayo_cpa_component.c +++ b/src/mod/event_handlers/mod_rayo/rayo_cpa_component.c @@ -390,7 +390,9 @@ void rayo_cpa_component_shutdown(void) switch_event_unbind_callback(on_rayo_cpa_detector_event); switch_event_unbind_callback(on_channel_hangup_complete_event); rayo_cpa_detector_shutdown(); - switch_core_hash_destroy(&globals.subscribers); + if (globals.subscribers) { + switch_core_hash_destroy(&globals.subscribers); + } } /* For Emacs: diff --git a/src/mod/event_handlers/mod_rayo/rayo_cpa_detector.c b/src/mod/event_handlers/mod_rayo/rayo_cpa_detector.c index 702d20bec2..0905f6e547 100644 --- a/src/mod/event_handlers/mod_rayo/rayo_cpa_detector.c +++ b/src/mod/event_handlers/mod_rayo/rayo_cpa_detector.c @@ -1,6 +1,6 @@ /* * mod_rayo for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application - * Copyright (C) 2014, Grasshopper + * Copyright (C) 2014-2015, Grasshopper * * Version: MPL 1.1 * @@ -452,7 +452,9 @@ void rayo_cpa_detector_shutdown(void) { switch_console_set_complete("del rayo_cpa"); switch_console_del_complete_func("::rayo_cpa::list_signal_types"); - switch_core_hash_destroy(&globals.detectors); + if (globals.detectors) { + switch_core_hash_destroy(&globals.detectors); + } switch_event_unbind_callback(rayo_cpa_detector_event); } diff --git a/src/mod/event_handlers/mod_rayo/rayo_input_component.c b/src/mod/event_handlers/mod_rayo/rayo_input_component.c index 53dcbf3540..77855463d6 100644 --- a/src/mod/event_handlers/mod_rayo/rayo_input_component.c +++ b/src/mod/event_handlers/mod_rayo/rayo_input_component.c @@ -1066,7 +1066,9 @@ switch_status_t rayo_input_component_shutdown(void) { switch_event_unbind_callback(on_detected_speech_event); - srgs_parser_destroy(globals.parser); + if (globals.parser) { + srgs_parser_destroy(globals.parser); + } srgs_destroy(); nlsml_destroy(); diff --git a/src/mod/event_handlers/mod_rayo/rayo_output_component.c b/src/mod/event_handlers/mod_rayo/rayo_output_component.c index 267f06225c..932b3aed47 100644 --- a/src/mod/event_handlers/mod_rayo/rayo_output_component.c +++ b/src/mod/event_handlers/mod_rayo/rayo_output_component.c @@ -1,6 +1,6 @@ /* * mod_rayo for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application - * Copyright (C) 2013-2014, Grasshopper + * Copyright (C) 2013-2015, Grasshopper * * Version: MPL 1.1 * @@ -1259,7 +1259,9 @@ switch_status_t rayo_output_component_load(switch_loadable_module_interface_t ** */ switch_status_t rayo_output_component_shutdown(void) { - switch_core_hash_destroy(&fileman_globals.hash); + if (fileman_globals.hash) { + switch_core_hash_destroy(&fileman_globals.hash); + } return SWITCH_STATUS_SUCCESS; }