summaryrefslogtreecommitdiff
path: root/res/res_config_pgsql.c
diff options
context:
space:
mode:
authorMatthew Jordan <mjordan@digium.com>2014-07-16 13:55:36 +0000
committerMatthew Jordan <mjordan@digium.com>2014-07-16 13:55:36 +0000
commit03e9c598e5fe2274e0e171519c5ac52738c65b14 (patch)
treee532af6b6244b867b0f1470bc7cee7790b758a42 /res/res_config_pgsql.c
parentfee789dddbe0fe5791d42773e2293b693624e961 (diff)
cel_pgsql, cdr_pgsql, res_config_pgsql: Add PostgreSQL application_name support
This patch adds support for the PostgreSQL application_name connection setting. When the appropriate PostgreSQL module's configuration is set with an application name, the name will be passed to PostgreSQL on connection and displayed in the database's pg_stat_activity view, as well as in CSV logs. This aids in managing which applications/servers are connected to a PostgreSQL database, as well as tracing the activity of those connections. Review: https://reviewboard.asterisk.org/r/3591 ASTERISK-23737 #close Reported by: Gergely Domodi patches: pgsql_application_name.patch uploaded by Gergely Domodi (License 6610) git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@418755 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'res/res_config_pgsql.c')
-rw-r--r--res/res_config_pgsql.c36
1 files changed, 26 insertions, 10 deletions
diff --git a/res/res_config_pgsql.c b/res/res_config_pgsql.c
index b8ab994a7..3ee5e4e78 100644
--- a/res/res_config_pgsql.c
+++ b/res/res_config_pgsql.c
@@ -79,6 +79,7 @@ static char dbhost[MAX_DB_OPTION_SIZE] = "";
static char dbuser[MAX_DB_OPTION_SIZE] = "";
static char dbpass[MAX_DB_OPTION_SIZE] = "";
static char dbname[MAX_DB_OPTION_SIZE] = "";
+static char dbappname[MAX_DB_OPTION_SIZE] = "";
static char dbsock[MAX_DB_OPTION_SIZE] = "";
static int dbport = 5432;
static time_t connect_time = 0;
@@ -1436,6 +1437,12 @@ static int parse_config(int is_reload)
dbport = atoi(s);
}
+ if (!(s = ast_variable_retrieve(config, "general", "dbappname"))) {
+ dbappname[0] = '\0';
+ } else {
+ ast_copy_string(dbappname, s, sizeof(dbappname));
+ }
+
if (!ast_strlen_zero(dbhost)) {
/* No socket needed */
} else if (!(s = ast_variable_retrieve(config, "general", "dbsock"))) {
@@ -1499,18 +1506,27 @@ static int pgsql_reconnect(const char *database)
/* DB password can legitimately be 0-length */
if ((!pgsqlConn) && (!ast_strlen_zero(dbhost) || !ast_strlen_zero(dbsock)) && !ast_strlen_zero(dbuser) && !ast_strlen_zero(my_database)) {
- struct ast_str *connInfo = ast_str_create(128);
+ struct ast_str *conn_info = ast_str_create(128);
+
+ if (!conn_info) {
+ ast_log(LOG_ERROR, "PostgreSQL RealTime: Failed to allocate memory for connection string.\n");
+ return 0;
+ }
- ast_str_set(&connInfo, 0, "host=%s port=%d dbname=%s user=%s",
+ ast_str_set(&conn_info, 0, "host=%s port=%d dbname=%s user=%s",
S_OR(dbhost, dbsock), dbport, my_database, dbuser);
- if (!ast_strlen_zero(dbpass))
- ast_str_append(&connInfo, 0, " password=%s", dbpass);
-
- ast_debug(1, "%u connInfo=%s\n", (unsigned int)ast_str_size(connInfo), ast_str_buffer(connInfo));
- pgsqlConn = PQconnectdb(ast_str_buffer(connInfo));
- ast_debug(1, "%u connInfo=%s\n", (unsigned int)ast_str_size(connInfo), ast_str_buffer(connInfo));
- ast_free(connInfo);
- connInfo = NULL;
+
+ if (!ast_strlen_zero(dbappname)) {
+ ast_str_append(&conn_info, 0, " application_name=%s", dbappname);
+ }
+
+ if (!ast_strlen_zero(dbpass)) {
+ ast_str_append(&conn_info, 0, " password=%s", dbpass);
+ }
+
+ pgsqlConn = PQconnectdb(ast_str_buffer(conn_info));
+ ast_free(conn_info);
+ conn_info = NULL;
ast_debug(1, "pgsqlConn=%p\n", pgsqlConn);
if (pgsqlConn && PQstatus(pgsqlConn) == CONNECTION_OK) {