From 7b898dd8e1aab964ad96645da31a8c3f7ef1e8f9 Mon Sep 17 00:00:00 2001
From: Chris-Savinovich <csavinovich@digium.com>
Date: Wed, 15 Aug 2018 15:27:52 -0400
Subject: [PATCH] pbx_config.c: Fix reloading module if initially declined to
 load

Added decline if extensions.conf file not available
when loading pbx_config, and also made sure everything
gets properly unregistered and/or destroyed on unload.

Change-Id: Ib00665106043b1be5148ffa7a477396038915854
---
 pbx/pbx_config.c | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)

diff --git a/pbx/pbx_config.c b/pbx/pbx_config.c
index 65daa2a079..1723b8d48f 100644
--- a/pbx/pbx_config.c
+++ b/pbx/pbx_config.c
@@ -1599,15 +1599,15 @@ static struct ast_cli_entry cli_dialplan_save =
  */
 static int unload_module(void)
 {
-	if (static_config && !write_protect_config)
-		ast_cli_unregister(&cli_dialplan_save);
-	if (overrideswitch_config) {
-		ast_free(overrideswitch_config);
-	}
+	ast_cli_unregister(&cli_dialplan_save);
+	ast_free(overrideswitch_config);
+	overrideswitch_config = NULL;
+
 	ast_cli_unregister_multiple(cli_pbx_config, ARRAY_LEN(cli_pbx_config));
 	ast_manager_unregister(AMI_EXTENSION_ADD);
 	ast_manager_unregister(AMI_EXTENSION_REMOVE);
 	ast_context_destroy(NULL, registrar);
+
 	return 0;
 }
 
@@ -2045,10 +2045,17 @@ static int pbx_load_module(void)
 
 	ast_mutex_lock(&reload_lock);
 
-	if (!local_table)
+	if (!local_table) {
 		local_table = ast_hashtab_create(17, ast_hashtab_compare_contexts, ast_hashtab_resize_java, ast_hashtab_newsize_java, ast_hashtab_hash_contexts, 0);
+		if (!local_table) {
+			ast_mutex_unlock(&reload_lock);
+			return AST_MODULE_LOAD_DECLINE;
+		}
+	}
 
 	if (!pbx_load_config(config)) {
+		ast_hashtab_destroy(local_table, NULL);
+		local_table = NULL;
 		ast_mutex_unlock(&reload_lock);
 		return AST_MODULE_LOAD_DECLINE;
 	}
@@ -2075,6 +2082,11 @@ static int load_module(void)
 {
 	int res;
 
+	if (pbx_load_module()) {
+		unload_module();
+		return AST_MODULE_LOAD_DECLINE;
+	}
+
 	if (static_config && !write_protect_config)
 		ast_cli_register(&cli_dialplan_save);
 	ast_cli_register_multiple(cli_pbx_config, ARRAY_LEN(cli_pbx_config));
@@ -2089,9 +2101,6 @@ static int load_module(void)
 		return AST_MODULE_LOAD_DECLINE;
 	}
 
-	if (pbx_load_module())
-		return AST_MODULE_LOAD_DECLINE;
-
 	return AST_MODULE_LOAD_SUCCESS;
 }
 
-- 
GitLab