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."));