From 1e315f6cb505c706e319ba53643573872356d835 Mon Sep 17 00:00:00 2001 From: Tzafrir Cohen Date: Thu, 11 Jan 2018 14:37:49 +0200 Subject: cdr_mysql: check early for missing configs ASTERISK-27572 Change-Id: Id5126f525fb255574bbf44878059a8d1890843fc --- addons/cdr_mysql.c | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/addons/cdr_mysql.c b/addons/cdr_mysql.c index bd31664c8..766c1fc0a 100644 --- a/addons/cdr_mysql.c +++ b/addons/cdr_mysql.c @@ -185,7 +185,7 @@ static int mysql_log(struct ast_cdr *cdr) ast_mutex_lock(&mysql_lock); db_reconnect: - if ((!connected) && (hostname || dbsock) && dbuser && password && dbname && dbtable ) { + if (!connected) { /* Attempt to connect */ mysql_init(&mysql); /* Add option to quickly timeout the connection */ @@ -581,6 +581,37 @@ static int my_connect_db(struct ast_config *cfg) return AST_MODULE_LOAD_SUCCESS; } +static const char * my_global_var(ast_config *cfg, const char *var_name) +{ + return ast_variable_retrieve(cfg, "global", variable); +} + + +const char[] required_global_cfgs[] = { + "dbuser", "dbpassword", "dbname", "dbtable" +}; + +static int is_valid_cfg(ast_config *cfg) +{ + int i; + + if (! (my_global_var(cfg, "hostname") || my_global_var(cfg, "dbsock"))) { + ast_log(LOG_ERROR, "Either 'hostname' or 'dbsock' must be set\n"); + return 0; + } + + for (i = 0; i < sizeof(required_global_cfgs); i++) { + if (!my_global_var(cfg, required_global_cfgs[i])) { + ast_log(LOG_ERROR, "'%s' must be set\n", + required_global_cfgs[i]); + return 0; + } + + } + + return 1; +} + static int my_load_module(int reload) { int res; @@ -605,6 +636,10 @@ static int my_load_module(int reload) return AST_MODULE_LOAD_DECLINE; } + if (!is_valid_cfg(cfg)) { + return AST_MODULE_LOAD_DECLINE; + } + if (reload) { AST_RWLIST_WRLOCK(&columns); my_unload_module(1); -- cgit v1.2.3