[Asterisk-code-review] func periodic hook.c: Cleanup module resources on failure. (asterisk[master])

George Joseph asteriskteam at digium.com
Fri Oct 5 14:32:22 CDT 2018


George Joseph has submitted this change and it was merged. ( https://gerrit.asterisk.org/10439 )

Change subject: func_periodic_hook.c: Cleanup module resources on failure.
......................................................................

func_periodic_hook.c: Cleanup module resources on failure.

* Make load_module() cleanup if it failed to setup the module.

* Make unload_module() always return 0.  It is silly to fail unloading if
the hook function we try to unregister was not even registered.

Change-Id: I280fc6e8ba2a7ee2588ca01d870eebaf74b4ffe6
---
M funcs/func_periodic_hook.c
1 file changed, 15 insertions(+), 10 deletions(-)

Approvals:
  Corey Farrell: Looks good to me, but someone else must approve
  George Joseph: Looks good to me, approved; Approved for Submit



diff --git a/funcs/func_periodic_hook.c b/funcs/func_periodic_hook.c
index 1f66747..31c534c 100644
--- a/funcs/func_periodic_hook.c
+++ b/funcs/func_periodic_hook.c
@@ -446,7 +446,8 @@
 {
 	ast_context_destroy(NULL, AST_MODULE);
 
-	return ast_custom_function_unregister(&hook_function);
+	ast_custom_function_unregister(&hook_function);
+	return 0;
 }
 
 static int load_module(void)
@@ -461,32 +462,36 @@
 	/*
 	 * Based on a handy recipe from the Asterisk Cookbook.
 	 */
-	ast_add_extension(context_name, 1, exten_name, 1, "", "",
+	res = ast_add_extension(context_name, 1, exten_name, 1, "", "",
 			"Set", "EncodedChannel=${CUT(HOOK_CHANNEL,-,1-2)}",
 			NULL, AST_MODULE);
-	ast_add_extension(context_name, 1, exten_name, 2, "", "",
+	res |= ast_add_extension(context_name, 1, exten_name, 2, "", "",
 			"Set", "GROUP_NAME=${EncodedChannel}${HOOK_ID}",
 			NULL, AST_MODULE);
-	ast_add_extension(context_name, 1, exten_name, 3, "", "",
+	res |= ast_add_extension(context_name, 1, exten_name, 3, "", "",
 			"Set", "GROUP(periodic-hook)=${GROUP_NAME}",
 			NULL, AST_MODULE);
-	ast_add_extension(context_name, 1, exten_name, 4, "", "", "ExecIf",
+	res |= ast_add_extension(context_name, 1, exten_name, 4, "", "", "ExecIf",
 			"$[${GROUP_COUNT(${GROUP_NAME}@periodic-hook)} > 1]?Hangup()",
 			NULL, AST_MODULE);
-	ast_add_extension(context_name, 1, exten_name, 5, "", "",
+	res |= ast_add_extension(context_name, 1, exten_name, 5, "", "",
 			"Set", "ChannelToSpy=${URIDECODE(${EncodedChannel})}",
 			NULL, AST_MODULE);
-	ast_add_extension(context_name, 1, exten_name, 6, "", "",
+	res |= ast_add_extension(context_name, 1, exten_name, 6, "", "",
 			"ChanSpy", "${ChannelToSpy},qEB", NULL, AST_MODULE);
 
-	res = ast_add_extension(context_name, 1, beep_exten, 1, "", "",
+	res |= ast_add_extension(context_name, 1, beep_exten, 1, "", "",
 			"Answer", "", NULL, AST_MODULE);
 	res |= ast_add_extension(context_name, 1, beep_exten, 2, "", "",
 			"Playback", "beep", NULL, AST_MODULE);
 
-	res = ast_custom_function_register_escalating(&hook_function, AST_CFE_BOTH);
+	res |= ast_custom_function_register_escalating(&hook_function, AST_CFE_BOTH);
 
-	return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
+	if (res) {
+		unload_module();
+		return AST_MODULE_LOAD_DECLINE;
+	}
+	return AST_MODULE_LOAD_SUCCESS;
 }
 
 int AST_OPTIONAL_API_NAME(ast_beep_start)(struct ast_channel *chan,

-- 
To view, visit https://gerrit.asterisk.org/10439
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: I280fc6e8ba2a7ee2588ca01d870eebaf74b4ffe6
Gerrit-Change-Number: 10439
Gerrit-PatchSet: 1
Gerrit-Owner: Richard Mudgett <rmudgett at digium.com>
Gerrit-Reviewer: Corey Farrell <git at cfware.com>
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Jenkins2 (1000185)
Gerrit-Reviewer: Richard Mudgett <rmudgett at digium.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20181005/4d6808ef/attachment.html>


More information about the asterisk-code-review mailing list