From 1ac009651265460a3093671de1b33dc4ad6eda0b Mon Sep 17 00:00:00 2001 From: George Joseph <gjoseph@digium.com> Date: Tue, 13 Jun 2017 10:33:34 -0600 Subject: [PATCH] res_ari: Add "module loaded" check to ari stubs The recent change to make the use of LOAD_DECLINE more consistent caused res_ari to unload itself before declining if the ari.conf file wasn't found. The ari stubs though still tried to use the configuration resulting in segfaults. This patch creates a new CHECK_ARI_MODULE_LOADED macro which tests to see if res_ari is actually loaded and causes the stubs to also decline if it isn't. The macro was then added to the mustache template's "load_module" function. ASTERISK-27026 #close Reported-by: Ronald Raikes Change-Id: I263d56efa628ee3c411bdcd16d49af6260c6c91d --- include/asterisk/ari.h | 10 +++++ res/res_ari_applications.c | 4 ++ res/res_ari_asterisk.c | 4 ++ res/res_ari_bridges.c | 4 ++ res/res_ari_channels.c | 4 ++ res/res_ari_device_states.c | 4 ++ res/res_ari_endpoints.c | 4 ++ res/res_ari_events.c | 43 ++++++++++-------- res/res_ari_mailboxes.c | 4 ++ res/res_ari_playbacks.c | 4 ++ res/res_ari_recordings.c | 4 ++ res/res_ari_sounds.c | 4 ++ .../res_ari_resource.c.mustache | 45 +++++++++++-------- 13 files changed, 101 insertions(+), 37 deletions(-) diff --git a/include/asterisk/ari.h b/include/asterisk/ari.h index 865b4b00cc..f83d5963f3 100644 --- a/include/asterisk/ari.h +++ b/include/asterisk/ari.h @@ -268,4 +268,14 @@ void ast_ari_response_created(struct ast_ari_response *response, */ void ast_ari_response_alloc_failed(struct ast_ari_response *response); +/*! \brief Determines whether the res_ari module is loaded */ +#define CHECK_ARI_MODULE_LOADED() \ + do { \ + if (!ast_module_check("res_ari.so") \ + || !ast_ari_oom_json()) { \ + return AST_MODULE_LOAD_DECLINE; \ + } \ + } while(0) + + #endif /* _ASTERISK_ARI_H */ diff --git a/res/res_ari_applications.c b/res/res_ari_applications.c index 21d9f56eb0..cf700c464e 100644 --- a/res/res_ari_applications.c +++ b/res/res_ari_applications.c @@ -500,6 +500,10 @@ static int unload_module(void) static int load_module(void) { int res = 0; + + CHECK_ARI_MODULE_LOADED(); + + stasis_app_ref(); res |= ast_ari_add_handler(&applications); if (res) { diff --git a/res/res_ari_asterisk.c b/res/res_ari_asterisk.c index 89517ccae0..eb0617b4c7 100644 --- a/res/res_ari_asterisk.c +++ b/res/res_ari_asterisk.c @@ -1221,6 +1221,10 @@ static int unload_module(void) static int load_module(void) { int res = 0; + + CHECK_ARI_MODULE_LOADED(); + + stasis_app_ref(); res |= ast_ari_add_handler(&asterisk); if (res) { diff --git a/res/res_ari_bridges.c b/res/res_ari_bridges.c index 5402c2b995..65bf7ed3e7 100644 --- a/res/res_ari_bridges.c +++ b/res/res_ari_bridges.c @@ -1547,6 +1547,10 @@ static int unload_module(void) static int load_module(void) { int res = 0; + + CHECK_ARI_MODULE_LOADED(); + + stasis_app_ref(); res |= ast_ari_add_handler(&bridges); if (res) { diff --git a/res/res_ari_channels.c b/res/res_ari_channels.c index 9d218e2bee..f6befcc90d 100644 --- a/res/res_ari_channels.c +++ b/res/res_ari_channels.c @@ -2851,6 +2851,10 @@ static int unload_module(void) static int load_module(void) { int res = 0; + + CHECK_ARI_MODULE_LOADED(); + + stasis_app_ref(); res |= ast_ari_add_handler(&channels); if (res) { diff --git a/res/res_ari_device_states.c b/res/res_ari_device_states.c index d6de5dff86..f393935626 100644 --- a/res/res_ari_device_states.c +++ b/res/res_ari_device_states.c @@ -331,6 +331,10 @@ static int unload_module(void) static int load_module(void) { int res = 0; + + CHECK_ARI_MODULE_LOADED(); + + stasis_app_ref(); res |= ast_ari_add_handler(&deviceStates); if (res) { diff --git a/res/res_ari_endpoints.c b/res/res_ari_endpoints.c index d96de0877b..d1242c0fbc 100644 --- a/res/res_ari_endpoints.c +++ b/res/res_ari_endpoints.c @@ -455,6 +455,10 @@ static int unload_module(void) static int load_module(void) { int res = 0; + + CHECK_ARI_MODULE_LOADED(); + + stasis_app_ref(); res |= ast_ari_add_handler(&endpoints); if (res) { diff --git a/res/res_ari_events.c b/res/res_ari_events.c index 3f5b89a440..f916d0e4ee 100644 --- a/res/res_ari_events.c +++ b/res/res_ari_events.c @@ -430,28 +430,35 @@ static int unload_module(void) static int load_module(void) { int res = 0; - struct ast_websocket_protocol *protocol; - if (ast_ari_websocket_events_event_websocket_init() == -1) { - return AST_MODULE_LOAD_DECLINE; - } + CHECK_ARI_MODULE_LOADED(); - events.ws_server = ast_websocket_server_create(); - if (!events.ws_server) { - ast_ari_websocket_events_event_websocket_dtor(); - return AST_MODULE_LOAD_DECLINE; - } + /* This is scoped to not conflict with CHECK_ARI_MODULE_LOADED */ + { + struct ast_websocket_protocol *protocol; - protocol = ast_websocket_sub_protocol_alloc("ari"); - if (!protocol) { - ao2_ref(events.ws_server, -1); - events.ws_server = NULL; - ast_ari_websocket_events_event_websocket_dtor(); - return AST_MODULE_LOAD_DECLINE; + if (ast_ari_websocket_events_event_websocket_init() == -1) { + return AST_MODULE_LOAD_DECLINE; + } + + events.ws_server = ast_websocket_server_create(); + if (!events.ws_server) { + ast_ari_websocket_events_event_websocket_dtor(); + return AST_MODULE_LOAD_DECLINE; + } + + protocol = ast_websocket_sub_protocol_alloc("ari"); + if (!protocol) { + ao2_ref(events.ws_server, -1); + events.ws_server = NULL; + ast_ari_websocket_events_event_websocket_dtor(); + return AST_MODULE_LOAD_DECLINE; + } + protocol->session_attempted = ast_ari_events_event_websocket_ws_attempted_cb; + protocol->session_established = ast_ari_events_event_websocket_ws_established_cb; + res |= ast_websocket_server_add_protocol2(events.ws_server, protocol); } - protocol->session_attempted = ast_ari_events_event_websocket_ws_attempted_cb; - protocol->session_established = ast_ari_events_event_websocket_ws_established_cb; - res |= ast_websocket_server_add_protocol2(events.ws_server, protocol); + stasis_app_ref(); res |= ast_ari_add_handler(&events); if (res) { diff --git a/res/res_ari_mailboxes.c b/res/res_ari_mailboxes.c index 12c33cd884..1f6d2cc81b 100644 --- a/res/res_ari_mailboxes.c +++ b/res/res_ari_mailboxes.c @@ -337,6 +337,10 @@ static int unload_module(void) static int load_module(void) { int res = 0; + + CHECK_ARI_MODULE_LOADED(); + + stasis_app_ref(); res |= ast_ari_add_handler(&mailboxes); if (res) { diff --git a/res/res_ari_playbacks.c b/res/res_ari_playbacks.c index e2c432db03..40099cfb48 100644 --- a/res/res_ari_playbacks.c +++ b/res/res_ari_playbacks.c @@ -289,6 +289,10 @@ static int unload_module(void) static int load_module(void) { int res = 0; + + CHECK_ARI_MODULE_LOADED(); + + stasis_app_ref(); res |= ast_ari_add_handler(&playbacks); if (res) { diff --git a/res/res_ari_recordings.c b/res/res_ari_recordings.c index 57d80f2e0f..fe3d343ee2 100644 --- a/res/res_ari_recordings.c +++ b/res/res_ari_recordings.c @@ -873,6 +873,10 @@ static int unload_module(void) static int load_module(void) { int res = 0; + + CHECK_ARI_MODULE_LOADED(); + + stasis_app_ref(); res |= ast_ari_add_handler(&recordings); if (res) { diff --git a/res/res_ari_sounds.c b/res/res_ari_sounds.c index fded7fbced..8d5928a504 100644 --- a/res/res_ari_sounds.c +++ b/res/res_ari_sounds.c @@ -219,6 +219,10 @@ static int unload_module(void) static int load_module(void) { int res = 0; + + CHECK_ARI_MODULE_LOADED(); + + stasis_app_ref(); res |= ast_ari_add_handler(&sounds); if (res) { diff --git a/rest-api-templates/res_ari_resource.c.mustache b/rest-api-templates/res_ari_resource.c.mustache index 3ccafcd082..d4ccda9e8e 100644 --- a/rest-api-templates/res_ari_resource.c.mustache +++ b/rest-api-templates/res_ari_resource.c.mustache @@ -273,36 +273,43 @@ static int unload_module(void) static int load_module(void) { int res = 0; + + CHECK_ARI_MODULE_LOADED(); + {{#apis}} {{#operations}} {{#has_websocket}} - struct ast_websocket_protocol *protocol; + /* This is scoped to not conflict with CHECK_ARI_MODULE_LOADED */ + { + struct ast_websocket_protocol *protocol; - if (ast_ari_websocket_{{c_name}}_{{c_nickname}}_init() == -1) { - return AST_MODULE_LOAD_DECLINE; - } + if (ast_ari_websocket_{{c_name}}_{{c_nickname}}_init() == -1) { + return AST_MODULE_LOAD_DECLINE; + } - {{full_name}}.ws_server = ast_websocket_server_create(); - if (!{{full_name}}.ws_server) { - ast_ari_websocket_events_event_websocket_dtor(); - return AST_MODULE_LOAD_DECLINE; - } + {{full_name}}.ws_server = ast_websocket_server_create(); + if (!{{full_name}}.ws_server) { + ast_ari_websocket_events_event_websocket_dtor(); + return AST_MODULE_LOAD_DECLINE; + } - protocol = ast_websocket_sub_protocol_alloc("{{websocket_protocol}}"); - if (!protocol) { - ao2_ref({{full_name}}.ws_server, -1); - {{full_name}}.ws_server = NULL; - ast_ari_websocket_events_event_websocket_dtor(); - return AST_MODULE_LOAD_DECLINE; - } - protocol->session_attempted = ast_ari_{{c_name}}_{{c_nickname}}_ws_attempted_cb; - protocol->session_established = ast_ari_{{c_name}}_{{c_nickname}}_ws_established_cb; + protocol = ast_websocket_sub_protocol_alloc("{{websocket_protocol}}"); + if (!protocol) { + ao2_ref({{full_name}}.ws_server, -1); + {{full_name}}.ws_server = NULL; + ast_ari_websocket_events_event_websocket_dtor(); + return AST_MODULE_LOAD_DECLINE; + } + protocol->session_attempted = ast_ari_{{c_name}}_{{c_nickname}}_ws_attempted_cb; + protocol->session_established = ast_ari_{{c_name}}_{{c_nickname}}_ws_established_cb; {{/has_websocket}} {{#is_websocket}} - res |= ast_websocket_server_add_protocol2({{full_name}}.ws_server, protocol); + res |= ast_websocket_server_add_protocol2({{full_name}}.ws_server, protocol); + } {{/is_websocket}} {{/operations}} {{/apis}} + stasis_app_ref(); res |= ast_ari_add_handler(&{{root_full_name}}); if (res) { -- GitLab