[Asterisk-code-review] cli: Prevent assertions on startup from bad ao2 refs. (asterisk[16])

Friendly Automation asteriskteam at digium.com
Fri Sep 9 20:41:44 CDT 2022


Friendly Automation has submitted this change. ( https://gerrit.asterisk.org/c/asterisk/+/19084 )

Change subject: cli: Prevent assertions on startup from bad ao2 refs.
......................................................................

cli: Prevent assertions on startup from bad ao2 refs.

If "core show channels" is run before startup has completed, it
is possible for bad ao2 refs to occur because the system is not
yet fully initialized. This will lead to an assertion failing.

To prevent this, initialization of CLI builtins is moved to be
later along in the main load sequence. Core CLI commands are
loaded at the same time, but channel-related commands are loaded
later on.

ASTERISK-29846 #close

Change-Id: If6b3cde802876bd738c1b4cf2683bea6ddc615b6
---
M include/asterisk/_private.h
M main/asterisk.c
M main/cli.c
3 files changed, 43 insertions(+), 23 deletions(-)

Approvals:
  George Joseph: Looks good to me, approved
  Friendly Automation: Approved for Submit




diff --git a/include/asterisk/_private.h b/include/asterisk/_private.h
index 4f81421..65e403f 100644
--- a/include/asterisk/_private.h
+++ b/include/asterisk/_private.h
@@ -37,6 +37,7 @@
 int astdb_init(void);			/*!< Provided by db.c */
 int ast_channels_init(void);		/*!< Provided by channel.c */
 void ast_builtins_init(void);		/*!< Provided by cli.c */
+void ast_cli_channels_init(void);	/*!< Provided by cli.c */
 int ast_cli_perms_init(int reload);	/*!< Provided by cli.c */
 void dnsmgr_start_refresh(void);	/*!< Provided by dnsmgr.c */
 int ast_dns_system_resolver_init(void); /*!< Provided by dns_system_resolver.c */
diff --git a/main/asterisk.c b/main/asterisk.c
index 9bc4c85..9b9fb35 100644
--- a/main/asterisk.c
+++ b/main/asterisk.c
@@ -4266,6 +4266,7 @@
 
 	/* loads the cli_permissions.conf file needed to implement cli restrictions. */
 	ast_cli_perms_init(0);
+	ast_cli_channels_init(); /* Not always safe to access CLI commands until startup is complete. */
 
 	ast_stun_init();
 
diff --git a/main/cli.c b/main/cli.c
index fb4dbc5..5237945 100644
--- a/main/cli.c
+++ b/main/cli.c
@@ -2025,50 +2025,27 @@
 static struct ast_cli_entry cli_cli[] = {
 	AST_CLI_DEFINE(handle_commandmatchesarray, "Returns command matches array"),
 
-	AST_CLI_DEFINE(handle_nodebugchan_deprecated, "Disable debugging on channel(s)"),
-
-	AST_CLI_DEFINE(handle_chanlist, "Display information on channels"),
-
-	AST_CLI_DEFINE(handle_showcalls, "Display information on calls"),
-
-	AST_CLI_DEFINE(handle_showchan, "Display information on a specific channel"),
-
-	AST_CLI_DEFINE(handle_core_set_debug_channel, "Enable/disable debugging on a channel"),
-
 	AST_CLI_DEFINE(handle_debug_category, "Enable/disable debugging categories"),
 
 	AST_CLI_DEFINE(handle_debug, "Set level of debug chattiness"),
 	AST_CLI_DEFINE(handle_trace, "Set level of trace chattiness"),
 	AST_CLI_DEFINE(handle_verbose, "Set level of verbose chattiness"),
 
-	AST_CLI_DEFINE(group_show_channels, "Display active channels with group(s)"),
-
 	AST_CLI_DEFINE(handle_help, "Display help list, or specific help on a command"),
-
 	AST_CLI_DEFINE(handle_logger_mute, "Toggle logging output to a console"),
 
 	AST_CLI_DEFINE(handle_modlist, "List modules and info"),
-
 	AST_CLI_DEFINE(handle_load, "Load a module by name"),
-
 	AST_CLI_DEFINE(handle_reload, "Reload configuration for a module"),
-
 	AST_CLI_DEFINE(handle_core_reload, "Global reload"),
-
 	AST_CLI_DEFINE(handle_unload, "Unload a module by name"),
-
 	AST_CLI_DEFINE(handle_refresh, "Completely unloads and loads a module by name"),
 
 	AST_CLI_DEFINE(handle_showuptime, "Show uptime information"),
 
-	AST_CLI_DEFINE(handle_softhangup, "Request a hangup on a given channel"),
-
 	AST_CLI_DEFINE(handle_cli_reload_permissions, "Reload CLI permissions config"),
-
 	AST_CLI_DEFINE(handle_cli_show_permissions, "Show CLI permissions"),
-
 	AST_CLI_DEFINE(handle_cli_check_permissions, "Try a permissions config for a user"),
-
 	AST_CLI_DEFINE(handle_cli_wait_fullybooted, "Wait for Asterisk to be fully booted"),
 
 #ifdef HAVE_MALLOC_TRIM
@@ -2077,6 +2054,16 @@
 
 };
 
+static struct ast_cli_entry cli_channels_cli[] = {
+	AST_CLI_DEFINE(handle_nodebugchan_deprecated, "Disable debugging on channel(s)"),
+	AST_CLI_DEFINE(handle_chanlist, "Display information on channels"),
+	AST_CLI_DEFINE(handle_showcalls, "Display information on calls"),
+	AST_CLI_DEFINE(handle_showchan, "Display information on a specific channel"),
+	AST_CLI_DEFINE(handle_core_set_debug_channel, "Enable/disable debugging on a channel"),
+	AST_CLI_DEFINE(group_show_channels, "Display active channels with group(s)"),
+	AST_CLI_DEFINE(handle_softhangup, "Request a hangup on a given channel"),
+};
+
 /*!
  * Some regexp characters in cli arguments are reserved and used as separators.
  */
@@ -2248,6 +2235,11 @@
 	ast_cli_unregister_multiple(cli_cli, ARRAY_LEN(cli_cli));
 }
 
+static void cli_channels_shutdown(void)
+{
+	ast_cli_unregister_multiple(cli_channels_cli, ARRAY_LEN(cli_channels_cli));
+}
+
 /*! \brief initialize the _full_cmd string in * each of the builtins. */
 void ast_builtins_init(void)
 {
@@ -2256,6 +2248,12 @@
 	ast_register_cleanup(cli_shutdown);
 }
 
+void ast_cli_channels_init(void)
+{
+	ast_cli_register_multiple(cli_channels_cli, ARRAY_LEN(cli_channels_cli));
+	ast_register_cleanup(cli_channels_shutdown);
+}
+
 /*!
  * match a word in the CLI entry.
  * returns -1 on mismatch, 0 on match of an optional word,

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

Gerrit-Project: asterisk
Gerrit-Branch: 16
Gerrit-Change-Id: If6b3cde802876bd738c1b4cf2683bea6ddc615b6
Gerrit-Change-Number: 19084
Gerrit-PatchSet: 2
Gerrit-Owner: N A <mail at interlinked.x10host.com>
Gerrit-Reviewer: Friendly Automation
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20220910/14020606/attachment-0001.html>


More information about the asterisk-code-review mailing list