From ade69af6d9ea753e7fb85d964fbedeeff0369e04 Mon Sep 17 00:00:00 2001 From: George Joseph Date: Thu, 10 Apr 2025 07:59:34 -0600 Subject: [PATCH] asterisk.c: Add "pre-init" and "pre-module" capability to cli.conf. Commands in the "[startup_commands]" section of cli.conf have historically run after all core and module initialization has been completed and just before "Asterisk Ready" is printed on the console. This meant that if you wanted to debug initialization of a specific module, your only option was to turn on debug for everything by setting "debug" in asterisk.conf. This commit introduces options to allow you to run CLI commands earlier in the asterisk startup process. A command with a value of "pre-init" will run just after logger initialization but before most core, and all module, initialization. A command with a value of "pre-module" will run just after all core initialization but before all module initialization. A command with a value of "fully-booted" (or "yes" for backwards compatibility) will run as they always have been...after all initialization and just before "Asterisk Ready" is printed on the console. This means you could do this... ``` [startup_commands] core set debug 3 res_pjsip.so = pre-module core set debug 0 res_pjsip.so = fully-booted ``` This would turn debugging on for res_pjsip.so to catch any module initialization debug messages then turn it off again after the module is loaded. UserNote: In cli.conf, you can now define startup commands that run before core initialization and before module initialization. --- configs/samples/cli.conf.sample | 18 +++++++++++++++++- main/asterisk.c | 24 +++++++++++++++++++++--- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/configs/samples/cli.conf.sample b/configs/samples/cli.conf.sample index f6a546ac82..409c23412b 100644 --- a/configs/samples/cli.conf.sample +++ b/configs/samples/cli.conf.sample @@ -7,5 +7,21 @@ ; Any commands listed in this section will get automatically executed ; when Asterisk starts as a daemon or foreground process (-c). ; +; Commands with a value of "pre-init" will run just after the logger +; is initialized but before all other core and module initialization. +; +; Commands with a value of "pre-module" will run just after core +; initialization is done but before module initialization. +; +; These two values can be used for things like enabling debugging on +; specific modules before they're loaded so you can see debug messages +; generated during their initialization. Of course you can't run +; commands from modules that haven't loaded yet. + +; Commands with a value of "yes" or "fully-booted" will run after all +; core and module initialization is completed and just before +; "Asterisk Ready" is printed on the console. +; +;core set debug 3 channels.c = pre-init +;core set debug 3 res_pjsip.so = pre-module ;core set verbose 3 = yes -;core set debug 1 = yes diff --git a/main/asterisk.c b/main/asterisk.c index 9b57e097a0..b0f8a14311 100644 --- a/main/asterisk.c +++ b/main/asterisk.c @@ -3528,8 +3528,20 @@ static void canary_exit(void) } } +enum startup_commands_phase { + STARTUP_COMMANDS_PRE_INIT = 0, + STARTUP_COMMANDS_PRE_MODULE, + STARTUP_COMMANDS_FULLY_BOOTED +}; + +static const char *startup_commands_phase_str[] = { + "pre-init", + "pre-module", + "fully-booted,yes,true,y,t,1,on" +}; + /* Execute CLI commands on startup. Run by main() thread. */ -static void run_startup_commands(void) +static void run_startup_commands(enum startup_commands_phase phase) { int fd; struct ast_config *cfg; @@ -3549,8 +3561,10 @@ static void run_startup_commands(void) } for (v = ast_variable_browse(cfg, "startup_commands"); v; v = v->next) { - if (ast_true(v->value)) + char *value = ast_str_to_lower(ast_strdupa(v->value)); + if (ast_in_delimited_string(value, startup_commands_phase_str[phase], ',')) { ast_cli_command(fd, v->name); + } } close(fd); @@ -4269,6 +4283,8 @@ static void asterisk_daemon(int isroot, const char *runuser, const char *rungrou threadstorage_init(); check_init(init_logger(), "Logger"); + run_startup_commands(STARTUP_COMMANDS_PRE_INIT); + check_init(ast_rtp_engine_init(), "RTP Engine"); ast_autoservice_init(); @@ -4303,6 +4319,8 @@ static void asterisk_daemon(int isroot, const char *runuser, const char *rungrou check_init(ast_local_init(), "Local Proxy Channel Driver"); check_init(ast_refer_init(), "Refer API"); + run_startup_commands(STARTUP_COMMANDS_PRE_MODULE); + /* We should avoid most config loads before this point as they can't use realtime. */ check_init(load_modules(), "Module"); @@ -4340,7 +4358,7 @@ static void asterisk_daemon(int isroot, const char *runuser, const char *rungrou ast_cli_register_multiple(cli_asterisk, ARRAY_LEN(cli_asterisk)); ast_register_cleanup(main_atexit); - run_startup_commands(); + run_startup_commands(STARTUP_COMMANDS_FULLY_BOOTED); ast_sd_notify("READY=1"); ast_verb(0, COLORIZE_FMT "\n", COLORIZE(COLOR_BRGREEN, 0, "Asterisk Ready."));