[Asterisk-code-review] Allow command-line options to override asterisk.conf. (asterisk[master])

Corey Farrell asteriskteam at digium.com
Thu May 7 09:32:08 CDT 2015


Corey Farrell has uploaded a new change for review.

  https://gerrit.asterisk.org/392

Change subject: Allow command-line options to override asterisk.conf.
......................................................................

Allow command-line options to override asterisk.conf.

Previous versions of Asterisk processed command-line options before
processing asterisk.conf.  This meant that if an option was set in
asterisk.conf, it could not be overridden with the equivelent command
line option.  This change causes Asterisk to process the command-line
twice.  First it processes options that are needed to load asterisk.conf,
then it processes the remaining options after config is read.

ASTERISK-25042 #close
Reported by: Corey Farrell

Change-Id: I1450d45c15b4467274b871914d893ed4f6564cd7
---
M include/asterisk/options.h
M main/asterisk.c
2 files changed, 70 insertions(+), 38 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/92/392/1

diff --git a/include/asterisk/options.h b/include/asterisk/options.h
index a08b863..e2709f9 100644
--- a/include/asterisk/options.h
+++ b/include/asterisk/options.h
@@ -66,8 +66,6 @@
 	AST_OPT_FLAG_CACHE_RECORD_FILES = (1 << 13),
 	/*! Display timestamp in CLI verbose output */
 	AST_OPT_FLAG_TIMESTAMP = (1 << 14),
-	/*! Override config */
-	AST_OPT_FLAG_OVERRIDE_CONFIG = (1 << 15),
 	/*! Reconnect */
 	AST_OPT_FLAG_RECONNECT = (1 << 16),
 	/*! Transmit Silence during Record() and DTMF Generation */
@@ -119,7 +117,6 @@
 #define ast_opt_dump_core		ast_test_flag(&ast_options, AST_OPT_FLAG_DUMP_CORE)
 #define ast_opt_cache_record_files	ast_test_flag(&ast_options, AST_OPT_FLAG_CACHE_RECORD_FILES)
 #define ast_opt_timestamp		ast_test_flag(&ast_options, AST_OPT_FLAG_TIMESTAMP)
-#define ast_opt_override_config		ast_test_flag(&ast_options, AST_OPT_FLAG_OVERRIDE_CONFIG)
 #define ast_opt_reconnect		ast_test_flag(&ast_options, AST_OPT_FLAG_RECONNECT)
 #define ast_opt_transmit_silence	ast_test_flag(&ast_options, AST_OPT_FLAG_TRANSMIT_SILENCE)
 #define ast_opt_dont_warn		ast_test_flag(&ast_options, AST_OPT_FLAG_DONT_WARN)
diff --git a/main/asterisk.c b/main/asterisk.c
index 277604b..e241da0 100644
--- a/main/asterisk.c
+++ b/main/asterisk.c
@@ -3385,7 +3385,6 @@
 {
 	struct ast_config *cfg;
 	struct ast_variable *v;
-	char *config = DEFAULT_CONFIG_FILE;
 	char hostname[MAXHOSTNAMELEN] = "";
 	struct ast_flags config_flags = { CONFIG_FLAG_NOREALTIME };
 	struct {
@@ -3394,18 +3393,11 @@
 	} found = { 0, 0 };
 	/* Default to false for security */
 	int live_dangerously = 0;
+	int option_debug_new = 0;
+	int option_verbose_new = 0;
 
 	/* Set default value */
 	option_dtmfminduration = AST_MIN_DTMF_DURATION;
-
-	if (ast_opt_override_config) {
-		cfg = ast_config_load2(ast_config_AST_CONFIG_FILE, "" /* core, can't reload */, config_flags);
-		if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID) {
-			fprintf(stderr, "Unable to open specified master config file '%s', using built-in defaults\n", ast_config_AST_CONFIG_FILE);
-		}
-	} else {
-		cfg = ast_config_load2(config, "" /* core, can't reload */, config_flags);
-	}
 
 	/* init with buildtime config */
 	ast_copy_string(cfg_paths.config_dir, DEFAULT_CONFIG_DIR, sizeof(cfg_paths.config_dir));
@@ -3432,8 +3424,10 @@
 
 	ast_set_default_eid(&ast_eid_default);
 
+	cfg = ast_config_load2(ast_config_AST_CONFIG_FILE, "" /* core, can't reload */, config_flags);
 	/* no asterisk.conf? no problem, use buildtime config! */
 	if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID) {
+		fprintf(stderr, "Unable to open specified master config file '%s', using built-in defaults\n", ast_config_AST_CONFIG_FILE);
 		return;
 	}
 
@@ -3487,7 +3481,7 @@
 	for (v = ast_variable_browse(cfg, "options"); v; v = v->next) {
 		/* verbose level (-v at startup) */
 		if (!strcasecmp(v->name, "verbose")) {
-			option_verbose = atoi(v->value);
+			option_verbose_new = atoi(v->value);
 		/* whether or not to force timestamping in CLI verbose output. (-T at startup) */
 		} else if (!strcasecmp(v->name, "timestamp")) {
 			ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_TIMESTAMP);
@@ -3496,9 +3490,9 @@
 			ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_EXEC_INCLUDES);
 		/* debug level (-d at startup) */
 		} else if (!strcasecmp(v->name, "debug")) {
-			option_debug = 0;
-			if (sscanf(v->value, "%30d", &option_debug) != 1) {
-				option_debug = ast_true(v->value);
+			option_debug_new = 0;
+			if (sscanf(v->value, "%30d", &option_debug_new) != 1) {
+				option_debug_new = ast_true(v->value);
 			}
 		} else if (!strcasecmp(v->name, "refdebug")) {
 			ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_REF_DEBUG);
@@ -3647,6 +3641,9 @@
 		pbx_live_dangerously(live_dangerously);
 	}
 
+	option_debug += option_debug_new;
+	option_verbose += option_verbose_new;
+
 	ast_config_destroy(cfg);
 }
 
@@ -3787,9 +3784,9 @@
 	int isroot = 1, rundir_exists = 0;
 	char *buf;
 	const char *runuser = NULL, *rungroup = NULL;
-	char *remotesock = NULL;
 	int moduleresult;         /*!< Result from the module load subsystem */
 	struct rlimit l;
+	static const char *getopt_settings = "BC:cde:FfG:ghIiL:M:mnpqRrs:TtU:VvWXx:";
 
 	/* Remember original args for restart */
 	if (argc > ARRAY_LEN(_argv) - 1) {
@@ -3813,11 +3810,56 @@
 
 	if (getenv("HOME"))
 		snprintf(filename, sizeof(filename), "%s/.asterisk_history", getenv("HOME"));
+
+	/* Set config file to default before checking arguments for override. */
+	ast_copy_string(cfg_paths.config_file, DEFAULT_CONFIG_FILE, sizeof(cfg_paths.config_file));
+
+	/* Process command-line options that effect asterisk.conf load. */
+	while ((c = getopt(argc, argv, getopt_settings)) != -1) {
+		switch (c) {
+		case 'X':
+			ast_set_flag(&ast_options, AST_OPT_FLAG_EXEC_INCLUDES);
+			break;
+		case 'C':
+			ast_copy_string(cfg_paths.config_file, optarg, sizeof(cfg_paths.config_file));
+			break;
+		case 'd':
+			option_debug++;
+			break;
+		case 'h':
+			show_cli_help();
+			exit(0);
+		case 'R':
+		case 'r':
+		case 'x':
+			/* ast_opt_remote is checked during config load. */
+			ast_set_flag(&ast_options, AST_OPT_FLAG_REMOTE);
+			break;
+		case 'V':
+			show_version();
+			exit(0);
+		case 'v':
+			option_verbose++;
+			break;
+		case '?':
+			exit(1);
+		}
+	}
+
+	/* Initialize env so it is available if #exec is used in asterisk.conf. */
+	env_init();
+
+	ast_readconfig();
+
+	/* Update env to include any systemname that was set. */
+	env_init();
+
 	/*! \brief Check for options
 	 *
 	 * \todo Document these options
 	 */
-	while ((c = getopt(argc, argv, "BC:cde:FfG:ghIiL:M:mnpqRrs:TtU:VvWXx:")) != -1) {
+	optind = 0;
+	while ((c = getopt(argc, argv, getopt_settings)) != -1) {
 		/*!\note Please keep the ordering here to alphabetical, capital letters
 		 * first.  This will make it easier in the future to select unused
 		 * option flags for new features. */
@@ -3827,18 +3869,17 @@
 			ast_clear_flag(&ast_options, AST_OPT_FLAG_LIGHT_BACKGROUND);
 			break;
 		case 'X':
-			ast_set_flag(&ast_options, AST_OPT_FLAG_EXEC_INCLUDES);
+			/* The command-line -X option enables #exec before asterisk.conf.  If it is
+			 * reset to no in asterisk.conf, that applies to the rest of the system. */
 			break;
 		case 'C':
-			ast_copy_string(cfg_paths.config_file, optarg, sizeof(cfg_paths.config_file));
-			ast_set_flag(&ast_options, AST_OPT_FLAG_OVERRIDE_CONFIG);
+			/* already processed. */
 			break;
 		case 'c':
 			ast_set_flag(&ast_options, AST_OPT_FLAG_NO_FORK | AST_OPT_FLAG_CONSOLE);
 			break;
 		case 'd':
-			option_debug++;
-			ast_set_flag(&ast_options, AST_OPT_FLAG_NO_FORK);
+			/* already processed. */
 			break;
 #if defined(HAVE_SYSINFO)
 		case 'e':
@@ -3862,8 +3903,8 @@
 			ast_set_flag(&ast_options, AST_OPT_FLAG_DUMP_CORE);
 			break;
 		case 'h':
-			show_cli_help();
-			exit(0);
+			/* already processed. */
+			break;
 		case 'I':
 			fprintf(stderr,
 				"NOTICE: The -I option is no longer needed.\n"
@@ -3901,7 +3942,9 @@
 			ast_set_flag(&ast_options, AST_OPT_FLAG_NO_FORK | AST_OPT_FLAG_REMOTE);
 			break;
 		case 's':
-			remotesock = ast_strdupa(optarg);
+			if (ast_opt_remote) {
+				ast_copy_string((char *) cfg_paths.socket_path, optarg, sizeof(cfg_paths.socket_path));
+			}
 			break;
 		case 'T':
 			ast_set_flag(&ast_options, AST_OPT_FLAG_TIMESTAMP);
@@ -3913,11 +3956,8 @@
 			runuser = ast_strdupa(optarg);
 			break;
 		case 'V':
-			show_version();
-			exit(0);
 		case 'v':
-			option_verbose++;
-			ast_set_flag(&ast_options, AST_OPT_FLAG_NO_FORK);
+			/* already processed. */
 			break;
 		case 'W': /* White background */
 			ast_set_flag(&ast_options, AST_OPT_FLAG_LIGHT_BACKGROUND);
@@ -3931,7 +3971,8 @@
 			xarg = ast_strdupa(optarg);
 			break;
 		case '?':
-			exit(1);
+			/* already processed. */
+			break;
 		}
 	}
 
@@ -3944,12 +3985,6 @@
 			argv[x] = argv[0] + 10;
 		}
 	}
-
-	ast_readconfig();
-	env_init();
-
-	if (ast_opt_remote && remotesock != NULL)
-		ast_copy_string((char *) cfg_paths.socket_path, remotesock, sizeof(cfg_paths.socket_path));
 
 	if (!ast_language_is_prefix && !ast_opt_remote) {
 		fprintf(stderr, "The 'languageprefix' option in asterisk.conf is deprecated; in a future release it will be removed, and your sound files will need to be organized in the 'new style' language layout.\n");

-- 
To view, visit https://gerrit.asterisk.org/392
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I1450d45c15b4467274b871914d893ed4f6564cd7
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Owner: Corey Farrell <git at cfware.com>



More information about the asterisk-code-review mailing list