Index: /trunk/cdr/cdr_pgsql.c =================================================================== --- /trunk/cdr/cdr_pgsql.c (revision 415300) +++ /trunk/cdr/cdr_pgsql.c (working copy) @@ -63,7 +63,7 @@ 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 = NULL, *pgdbname = NULL, *pgdbuser = NULL, *pgpassword = NULL, *pgdbappname = NULL, *pgdbport = NULL, *table = NULL, *encoding = NULL, *tz = NULL; static int connected = 0; static int maxsize = 512, maxsize2 = 512; static time_t connect_time = 0; @@ -183,7 +183,27 @@ ast_mutex_lock(&pgsql_lock); if ((!connected) && pghostname && pgdbuser && pgpassword && pgdbname) { - conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword); + struct ast_str *connInfo = ast_str_create(128); + + ast_str_set(&connInfo, 0, "host=%s port=%s dbname=%s user=%s", + pghostname, pgdbport, pgdbname, pgdbuser); + + if (!ast_strlen_zero(pgdbappname)) { + ast_str_append(&connInfo, 0, " application_name=%s", pgdbappname); + } + + if (!ast_strlen_zero(pgpassword)) { + ast_str_append(&connInfo, 0, " password=%s", pgpassword); + } + + ast_debug(1, "%u connInfo=%s\n", (unsigned int)ast_str_size(connInfo), ast_str_buffer(connInfo)); + conn = 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; + + ast_debug(1, "conn=%p\n", conn); + if (PQstatus(conn) != CONNECTION_BAD) { connected = 1; connect_time = time(NULL); @@ -447,6 +467,7 @@ ast_free(pgdbname); ast_free(pgdbuser); ast_free(pgpassword); + ast_free(pgdbappname); ast_free(pgdbport); ast_free(table); ast_free(encoding); @@ -519,6 +540,18 @@ return -1; } + if (!(tmp = ast_variable_retrieve(cfg, "global", "appname"))) { + ast_log(LOG_WARNING, "PostgreSQL database application name not specified.\n"); + tmp = ""; + } + ast_free(pgdbappname); + if (!(pgdbappname = 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 +623,34 @@ 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", pgdbappname); 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); + + struct ast_str *connInfo = ast_str_create(128); + + ast_str_set(&connInfo, 0, "host=%s port=%s dbname=%s user=%s", + pghostname, pgdbport, pgdbname, pgdbuser); + + if (!ast_strlen_zero(pgdbappname)) { + ast_str_append(&connInfo, 0, " application_name=%s", pgdbappname); + } + + if (!ast_strlen_zero(pgpassword)) { + ast_str_append(&connInfo, 0, " password=%s", pgpassword); + } + + ast_debug(1, "%u connInfo=%s\n", (unsigned int)ast_str_size(connInfo), ast_str_buffer(connInfo)); + conn = 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; + + ast_debug(1, "conn=%p\n", conn); + if (PQstatus(conn) != CONNECTION_BAD) { char sqlcmd[768]; char *fname, *ftype, *flen, *fnotnull, *fdef; Index: /trunk/res/res_config_pgsql.c =================================================================== --- /trunk/res/res_config_pgsql.c (revision 415300) +++ /trunk/res/res_config_pgsql.c (working copy) @@ -79,6 +79,7 @@ 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 @@ dbport = atoi(s); } + if (!(s = ast_variable_retrieve(config, "general", "dbappname"))) { + strcpy(dbappname, ""); + } 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"))) { @@ -1503,6 +1510,11 @@ ast_str_set(&connInfo, 0, "host=%s port=%d dbname=%s user=%s", S_OR(dbhost, dbsock), dbport, my_database, dbuser); + + if (!ast_strlen_zero(dbappname)) { + ast_str_append(&connInfo, 0, " application_name=%s", dbappname); + } + if (!ast_strlen_zero(dbpass)) ast_str_append(&connInfo, 0, " password=%s", dbpass);