Skip to content
Snippets Groups Projects
Commit a9414a6e authored by Mark Spencer's avatar Mark Spencer
Browse files

Special NULL case for mysql (bug #49)

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@1337 65c4cc65-6c06-0410-ace0-fbb531ad65f3
parent ae886f5c
Branches
Tags
No related merge requests found
...@@ -5,6 +5,9 @@ ...@@ -5,6 +5,9 @@
* *
* James Sharp <jsharp@psychoses.org> * James Sharp <jsharp@psychoses.org>
* *
* Modified August 2003
* Tilghman Lesher <asterisk__cdr__cdr_mysql__200308@the-tilghman.com>
*
* This program is free software, distributed under the terms of * This program is free software, distributed under the terms of
* the GNU General Public License. * the GNU General Public License.
* *
...@@ -35,6 +38,7 @@ static char *desc = "MySQL CDR Backend"; ...@@ -35,6 +38,7 @@ static char *desc = "MySQL CDR Backend";
static char *name = "mysql"; static char *name = "mysql";
static char *config = "cdr_mysql.conf"; static char *config = "cdr_mysql.conf";
static char *hostname = NULL, *dbname = NULL, *dbuser = NULL, *password = NULL; static char *hostname = NULL, *dbname = NULL, *dbuser = NULL, *password = NULL;
static int hostname_alloc = 0, dbname_alloc = 0, dbuser_alloc = 0, password_alloc = 0;
static int connected = 0; static int connected = 0;
static ast_mutex_t mysql_lock = AST_MUTEX_INITIALIZER; static ast_mutex_t mysql_lock = AST_MUTEX_INITIALIZER;
...@@ -63,7 +67,7 @@ static int mysql_log(struct ast_cdr *cdr) ...@@ -63,7 +67,7 @@ static int mysql_log(struct ast_cdr *cdr)
if (mysql_real_connect(&mysql, hostname, dbuser, password, dbname, 0, NULL, 0)) { if (mysql_real_connect(&mysql, hostname, dbuser, password, dbname, 0, NULL, 0)) {
connected = 1; connected = 1;
} else { } else {
ast_log(LOG_ERROR, "cdr_mysql: cannot connect to database %s. Call will not be logged\n", hostname); ast_log(LOG_ERROR, "cdr_mysql: cannot connect to database server %s. Call will not be logged\n", hostname);
} }
} else { } else {
/* Long connection - ping the server */ /* Long connection - ping the server */
...@@ -110,21 +114,25 @@ int unload_module(void) ...@@ -110,21 +114,25 @@ int unload_module(void)
mysql_close(&mysql); mysql_close(&mysql);
connected = 0; connected = 0;
} }
if (hostname) { if (hostname && hostname_alloc) {
free(hostname); free(hostname);
hostname = NULL; hostname = NULL;
hostname_alloc = 0;
} }
if (dbname) { if (dbname && dbname_alloc) {
free(dbname); free(dbname);
dbname = NULL; dbname = NULL;
dbname_alloc = 0;
} }
if (dbuser) { if (dbuser && dbuser_alloc) {
free(dbuser); free(dbuser);
dbuser = NULL; dbuser = NULL;
dbuser_alloc = 0;
} }
if (password) { if (password && password_alloc) {
free(password); free(password);
password = NULL; password = NULL;
password_alloc = 0;
} }
ast_cdr_unregister(name); ast_cdr_unregister(name);
return 0; return 0;
...@@ -150,51 +158,63 @@ int load_module(void) ...@@ -150,51 +158,63 @@ int load_module(void)
} }
tmp = ast_variable_retrieve(cfg,"global","hostname"); tmp = ast_variable_retrieve(cfg,"global","hostname");
hostname = malloc(strlen(tmp) + 1); if (tmp) {
if ((tmp != NULL) && (hostname != NULL)) { hostname = malloc(strlen(tmp) + 1);
strcpy(hostname,tmp); if (hostname != NULL) {
} else if (tmp == NULL) { hostname_alloc = 1;
ast_log(LOG_ERROR,"Database server hostname not specified.\n"); strcpy(hostname,tmp);
return -1; } else {
ast_log(LOG_ERROR,"Out of memory error.\n");
return -1;
}
} else { } else {
ast_log(LOG_ERROR,"Out of memory error.\n"); ast_log(LOG_WARNING,"MySQL server hostname not specified. Assuming localhost");
return -1; hostname = "localhost";
} }
tmp = ast_variable_retrieve(cfg,"global","dbname"); tmp = ast_variable_retrieve(cfg,"global","dbname");
dbname = malloc(strlen(tmp) + 1); if (tmp) {
if ((tmp != NULL) && (dbname != NULL)) { dbname = malloc(strlen(tmp) + 1);
strcpy(dbname,tmp); if (dbname != NULL) {
} else if (tmp == NULL) { dbname_alloc = 1;
ast_log(LOG_ERROR,"Database dbname not specified.\n"); strcpy(dbname,tmp);
return -1; } else {
ast_log(LOG_ERROR,"Out of memory error.\n");
return -1;
}
} else { } else {
ast_log(LOG_ERROR,"Out of memory error.\n"); ast_log(LOG_WARNING,"MySQL database not specified. Assuming asteriskcdrdb\n");
return -1; dbname = "asteriskcdrdb";
} }
tmp = ast_variable_retrieve(cfg,"global","user"); tmp = ast_variable_retrieve(cfg,"global","user");
dbuser = malloc(strlen(tmp) + 1); if (tmp) {
if ((tmp != NULL) && (dbuser != NULL)) { dbuser = malloc(strlen(tmp) + 1);
strcpy(dbuser,tmp); if (dbuser != NULL) {
} else if (tmp == NULL) { dbuser_alloc = 1;
ast_log(LOG_ERROR,"Database dbuser not specified.\n"); strcpy(dbuser,tmp);
return -1; } else {
ast_log(LOG_ERROR,"Out of memory error.\n");
return -1;
}
} else { } else {
ast_log(LOG_ERROR,"Out of memory error.\n"); ast_log(LOG_WARNING,"MySQL database user not specified. Assuming root\n");
return -1; dbuser = "root";
} }
tmp = ast_variable_retrieve(cfg,"global","password"); tmp = ast_variable_retrieve(cfg,"global","password");
password = malloc(strlen(tmp) + 1); if (tmp) {
if ((tmp != NULL) && (password != NULL)) { password = malloc(strlen(tmp) + 1);
strcpy(password,tmp); if (password != NULL) {
} else if (tmp == NULL) { password_alloc = 1;
ast_log(LOG_ERROR,"Database password not specified.\n"); strcpy(password,tmp);
return -1; } else {
ast_log(LOG_ERROR,"Out of memory error.\n");
return -1;
}
} else { } else {
ast_log(LOG_ERROR,"Out of memory error.\n"); ast_log(LOG_WARNING,"MySQL database password not specified. Assuming blank\n");
return -1; password = "";
} }
ast_destroy(cfg); ast_destroy(cfg);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment