[Asterisk-code-review] cdr mysql: check early for missing configs (asterisk[master])

Tzafrir Cohen asteriskteam at digium.com
Thu May 3 10:57:52 CDT 2018


Tzafrir Cohen has uploaded this change for review. ( https://gerrit.asterisk.org/8921


Change subject: cdr_mysql: check early for missing configs
......................................................................

cdr_mysql: check early for missing configs

ASTERISK-27572

Change-Id: Id5126f525fb255574bbf44878059a8d1890843fc
---
M addons/cdr_mysql.c
1 file changed, 36 insertions(+), 1 deletion(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/21/8921/1

diff --git a/addons/cdr_mysql.c b/addons/cdr_mysql.c
index bd31664..766c1fc 100644
--- a/addons/cdr_mysql.c
+++ b/addons/cdr_mysql.c
@@ -185,7 +185,7 @@
 	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 @@
 	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 @@
 		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);

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

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: Id5126f525fb255574bbf44878059a8d1890843fc
Gerrit-Change-Number: 8921
Gerrit-PatchSet: 1
Gerrit-Owner: Tzafrir Cohen <tzafrir.cohen at xorcom.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20180503/27911846/attachment.html>


More information about the asterisk-code-review mailing list