summaryrefslogtreecommitdiff
path: root/cdr
diff options
context:
space:
mode:
Diffstat (limited to 'cdr')
-rw-r--r--cdr/cdr_pgsql.c68
1 files changed, 63 insertions, 5 deletions
diff --git a/cdr/cdr_pgsql.c b/cdr/cdr_pgsql.c
index 8322da9b5..815bb6a7d 100644
--- a/cdr/cdr_pgsql.c
+++ b/cdr/cdr_pgsql.c
@@ -63,7 +63,17 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
static const char name[] = "pgsql";
static const char config[] = "cdr_pgsql.conf";
-static char *pghostname = NULL, *pgdbname = NULL, *pgdbuser = NULL, *pgpassword = NULL, *pgdbport = NULL, *table = NULL, *encoding = NULL, *tz = NULL;
+
+static char *pghostname;
+static char *pgdbname;
+static char *pgdbuser;
+static char *pgpassword;
+static char *pgappname;
+static char *pgdbport;
+static char *table;
+static char *encoding;
+static char *tz;
+
static int connected = 0;
static int maxsize = 512, maxsize2 = 512;
static time_t connect_time = 0;
@@ -174,6 +184,34 @@ static char *handle_cdr_pgsql_status(struct ast_cli_entry *e, int cmd, struct as
return CLI_SUCCESS;
}
+static void pgsql_reconnect(void)
+{
+ struct ast_str *conn_info = ast_str_create(128);
+ if (!conn_info) {
+ ast_log(LOG_ERROR, "Failed to allocate memory for connection string.\n");
+ return;
+ }
+
+ if (conn) {
+ PQfinish(conn);
+ conn = NULL;
+ }
+
+ ast_str_set(&conn_info, 0, "host=%s port=%s dbname=%s user=%s",
+ pghostname, pgdbport, pgdbname, pgdbuser);
+
+ if (!ast_strlen_zero(pgappname)) {
+ ast_str_append(&conn_info, 0, " application_name=%s", pgappname);
+ }
+
+ if (!ast_strlen_zero(pgpassword)) {
+ ast_str_append(&conn_info, 0, " password=%s", pgpassword);
+ }
+
+ conn = PQconnectdb(ast_str_buffer(conn_info));
+ ast_free(conn_info);
+}
+
static int pgsql_log(struct ast_cdr *cdr)
{
struct ast_tm tm;
@@ -183,7 +221,8 @@ static int pgsql_log(struct ast_cdr *cdr)
ast_mutex_lock(&pgsql_lock);
if ((!connected) && pghostname && pgdbuser && pgpassword && pgdbname) {
- conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
+ pgsql_reconnect();
+
if (PQstatus(conn) != CONNECTION_BAD) {
connected = 1;
connect_time = time(NULL);
@@ -441,12 +480,15 @@ static int unload_module(void)
ast_cli_unregister_multiple(cdr_pgsql_status_cli, ARRAY_LEN(cdr_pgsql_status_cli));
- PQfinish(conn);
-
+ if (conn) {
+ PQfinish(conn);
+ conn = NULL;
+ }
ast_free(pghostname);
ast_free(pgdbname);
ast_free(pgdbuser);
ast_free(pgpassword);
+ ast_free(pgappname);
ast_free(pgdbport);
ast_free(table);
ast_free(encoding);
@@ -519,6 +561,18 @@ static int config_module(int reload)
return -1;
}
+ if (!(tmp = ast_variable_retrieve(cfg, "global", "appname"))) {
+ tmp = "";
+ }
+
+ ast_free(pgappname);
+ if (!(pgappname = ast_strdup(tmp))) {
+ ast_config_destroy(cfg);
+ ast_mutex_unlock(&pgsql_lock);
+ return -1;
+ }
+
+
if (!(tmp = ast_variable_retrieve(cfg, "global", "password"))) {
ast_log(LOG_WARNING, "PostgreSQL database password not specified. Assuming blank\n");
tmp = "";
@@ -590,12 +644,14 @@ static int config_module(int reload)
ast_debug(1, "got user of %s\n", pgdbuser);
ast_debug(1, "got dbname of %s\n", pgdbname);
ast_debug(1, "got password of %s\n", pgpassword);
+ ast_debug(1, "got application name of %s\n", pgappname);
ast_debug(1, "got sql table name of %s\n", table);
ast_debug(1, "got encoding of %s\n", encoding);
ast_debug(1, "got timezone of %s\n", tz);
}
- conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
+ pgsql_reconnect();
+
if (PQstatus(conn) != CONNECTION_BAD) {
char sqlcmd[768];
char *fname, *ftype, *flen, *fnotnull, *fdef;
@@ -719,6 +775,8 @@ static int config_module(int reload)
ast_log(LOG_ERROR, "Unable to connect to database server %s. CALLS WILL NOT BE LOGGED!!\n", pghostname);
ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
connected = 0;
+ PQfinish(conn);
+ conn = NULL;
}
ast_config_destroy(cfg);