Add a massive set of changes for converting to use the ast_debug() macro.
[asterisk/asterisk.git] / res / res_config_pgsql.c
index cf38386..821f32c 100644 (file)
  * \arg http://www.postgresql.org
  */
 
-#include <stdlib.h>
-#include <string.h>
-#include <libpq-fe.h>                  /* PostgreSQL */
+/*** MODULEINFO
+       <depend>pgsql</depend>
+ ***/
 
 #include "asterisk.h"
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <libpq-fe.h>                  /* PostgreSQL */
+
 #include "asterisk/file.h"
 #include "asterisk/logger.h"
 #include "asterisk/channel.h"
@@ -61,17 +66,15 @@ static int parse_config(void);
 static int pgsql_reconnect(const char *database);
 static int realtime_pgsql_status(int fd, int argc, char **argv);
 
-LOCAL_USER_DECL;
-
-static char cli_realtime_pgsql_status_usage[] =
+static const char cli_realtime_pgsql_status_usage[] =
        "Usage: realtime pgsql status\n"
        "       Shows connection information for the Postgresql RealTime driver\n";
 
-static struct ast_cli_entry cli_realtime_pgsql_status = {
-        { "realtime", "pgsql", "status", NULL }, realtime_pgsql_status,
-        "Shows connection information for the Postgresql RealTime driver",
-        cli_realtime_pgsql_status_usage, NULL
- };
+static struct ast_cli_entry cli_realtime[] = {
+       { { "realtime", "pgsql", "status", NULL },
+       realtime_pgsql_status, "Shows connection information for the Postgresql RealTime driver",
+       cli_realtime_pgsql_status_usage },
+};
 
 static struct ast_variable *realtime_pgsql(const char *database, const char *table, va_list ap)
 {
@@ -129,9 +132,8 @@ static struct ast_variable *realtime_pgsql(const char *database, const char *tab
        if (!(result = PQexec(pgsqlConn, sql))) {
                ast_log(LOG_WARNING,
                                "Postgresql RealTime: Failed to query database. Check debug for more info.\n");
-               ast_log(LOG_DEBUG, "Postgresql RealTime: Query: %s\n", sql);
-               ast_log(LOG_DEBUG, "Postgresql RealTime: Query Failed because: %s\n",
-                               PQerrorMessage(pgsqlConn));
+               ast_debug(1, "Postgresql RealTime: Query: %s\n", sql);
+               ast_debug(1, "Postgresql RealTime: Query Failed because: %s\n", PQerrorMessage(pgsqlConn));
                ast_mutex_unlock(&pgsql_lock);
                return NULL;
        } else {
@@ -141,15 +143,15 @@ static struct ast_variable *realtime_pgsql(const char *database, const char *tab
                        && result_status != PGRES_NONFATAL_ERROR) {
                        ast_log(LOG_WARNING,
                                        "Postgresql RealTime: Failed to query database. Check debug for more info.\n");
-                       ast_log(LOG_DEBUG, "Postgresql RealTime: Query: %s\n", sql);
-                       ast_log(LOG_DEBUG, "Postgresql RealTime: Query Failed because: %s (%s)\n",
-                                       PQresultErrorMessage(result), PQresStatus(result_status));
+                       ast_debug(1, "Postgresql RealTime: Query: %s\n", sql);
+                       ast_debug(1, "Postgresql RealTime: Query Failed because: %s (%s)\n",
+                                               PQresultErrorMessage(result), PQresStatus(result_status));
                        ast_mutex_unlock(&pgsql_lock);
                        return NULL;
                }
        }
 
-       ast_log(LOG_DEBUG, "1Postgresql RealTime: Result=%p Query: %s\n", result, sql);
+       ast_debug(1, "1Postgresql RealTime: Result=%p Query: %s\n", result, sql);
 
        if ((num_rows = PQntuples(result)) > 0) {
                int i = 0;
@@ -157,7 +159,7 @@ static struct ast_variable *realtime_pgsql(const char *database, const char *tab
                int numFields = PQnfields(result);
                char **fieldnames = NULL;
 
-               ast_log(LOG_DEBUG, "Postgresql RealTime: Found %d rows.\n", num_rows);
+               ast_debug(1, "Postgresql RealTime: Found %d rows.\n", num_rows);
 
                if (!(fieldnames = ast_calloc(1, numFields * sizeof(char *)))) {
                        ast_mutex_unlock(&pgsql_lock);
@@ -171,7 +173,7 @@ static struct ast_variable *realtime_pgsql(const char *database, const char *tab
                                stringp = PQgetvalue(result, rowIndex, i);
                                while (stringp) {
                                        chunk = strsep(&stringp, ";");
-                                       if (chunk && !ast_strlen_zero(ast_strip(chunk))) {
+                                       if (!ast_strlen_zero(ast_strip(chunk))) {
                                                if (prev) {
                                                        prev->next = ast_variable_new(fieldnames[i], chunk);
                                                        if (prev->next) {
@@ -184,7 +186,7 @@ static struct ast_variable *realtime_pgsql(const char *database, const char *tab
                                }
                        }
                }
-               free(fieldnames);
+               ast_free(fieldnames);
        } else {
                ast_log(LOG_WARNING,
                                "Postgresql RealTime: Could not find any rows in table %s.\n", table);
@@ -235,7 +237,7 @@ static struct ast_config *realtime_multi_pgsql(const char *database, const char
        }
 
        initfield = ast_strdupa(newparam);
-       if (initfield && (op = strchr(initfield, ' '))) {
+       if ((op = strchr(initfield, ' '))) {
                *op = '\0';
        }
 
@@ -275,9 +277,8 @@ static struct ast_config *realtime_multi_pgsql(const char *database, const char
        if (!(result = PQexec(pgsqlConn, sql))) {
                ast_log(LOG_WARNING,
                                "Postgresql RealTime: Failed to query database. Check debug for more info.\n");
-               ast_log(LOG_DEBUG, "Postgresql RealTime: Query: %s\n", sql);
-               ast_log(LOG_DEBUG, "Postgresql RealTime: Query Failed because: %s\n",
-                               PQerrorMessage(pgsqlConn));
+               ast_debug(1, "Postgresql RealTime: Query: %s\n", sql);
+               ast_debug(1, "Postgresql RealTime: Query Failed because: %s\n", PQerrorMessage(pgsqlConn));
                ast_mutex_unlock(&pgsql_lock);
                return NULL;
        } else {
@@ -287,15 +288,15 @@ static struct ast_config *realtime_multi_pgsql(const char *database, const char
                        && result_status != PGRES_NONFATAL_ERROR) {
                        ast_log(LOG_WARNING,
                                        "Postgresql RealTime: Failed to query database. Check debug for more info.\n");
-                       ast_log(LOG_DEBUG, "Postgresql RealTime: Query: %s\n", sql);
-                       ast_log(LOG_DEBUG, "Postgresql RealTime: Query Failed because: %s (%s)\n",
-                                       PQresultErrorMessage(result), PQresStatus(result_status));
+                       ast_debug(1, "Postgresql RealTime: Query: %s\n", sql);
+                       ast_debug(1, "Postgresql RealTime: Query Failed because: %s (%s)\n",
+                                               PQresultErrorMessage(result), PQresStatus(result_status));
                        ast_mutex_unlock(&pgsql_lock);
                        return NULL;
                }
        }
 
-       ast_log(LOG_DEBUG, "2Postgresql RealTime: Result=%p Query: %s\n", result, sql);
+       ast_debug(1, "2Postgresql RealTime: Result=%p Query: %s\n", result, sql);
 
        if ((num_rows = PQntuples(result)) > 0) {
                int numFields = PQnfields(result);
@@ -303,7 +304,7 @@ static struct ast_config *realtime_multi_pgsql(const char *database, const char
                int rowIndex = 0;
                char **fieldnames = NULL;
 
-               ast_log(LOG_DEBUG, "Postgresql RealTime: Found %d rows.\n", num_rows);
+               ast_debug(1, "Postgresql RealTime: Found %d rows.\n", num_rows);
 
                if (!(fieldnames = ast_calloc(1, numFields * sizeof(char *)))) {
                        ast_mutex_unlock(&pgsql_lock);
@@ -321,7 +322,7 @@ static struct ast_config *realtime_multi_pgsql(const char *database, const char
                                stringp = PQgetvalue(result, rowIndex, i);
                                while (stringp) {
                                        chunk = strsep(&stringp, ";");
-                                       if (chunk && !ast_strlen_zero(ast_strip(chunk))) {
+                                       if (!ast_strlen_zero(ast_strip(chunk))) {
                                                if (initfield && !strcmp(initfield, fieldnames[i])) {
                                                        ast_category_rename(cat, chunk);
                                                }
@@ -332,7 +333,7 @@ static struct ast_config *realtime_multi_pgsql(const char *database, const char
                        }
                        ast_category_append(cfg, cat);
                }
-               free(fieldnames);
+               ast_free(fieldnames);
        } else {
                ast_log(LOG_WARNING,
                                "Postgresql RealTime: Could not find any rows in table %s.\n", table);
@@ -383,7 +384,7 @@ static int update_pgsql(const char *database, const char *table, const char *key
        snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " WHERE %s = '%s'", keyfield,
                         lookup);
 
-       ast_log(LOG_DEBUG, "Postgresql RealTime: Update SQL: %s\n", sql);
+       ast_debug(1, "Postgresql RealTime: Update SQL: %s\n", sql);
 
        /* We now have our complete statement; Lets connect to the server and execute it. */
        ast_mutex_lock(&pgsql_lock);
@@ -395,9 +396,8 @@ static int update_pgsql(const char *database, const char *table, const char *key
        if (!(result = PQexec(pgsqlConn, sql))) {
                ast_log(LOG_WARNING,
                                "Postgresql RealTime: Failed to query database. Check debug for more info.\n");
-               ast_log(LOG_DEBUG, "Postgresql RealTime: Query: %s\n", sql);
-               ast_log(LOG_DEBUG, "Postgresql RealTime: Query Failed because: %s\n",
-                               PQerrorMessage(pgsqlConn));
+               ast_debug(1, "Postgresql RealTime: Query: %s\n", sql);
+               ast_debug(1, "Postgresql RealTime: Query Failed because: %s\n", PQerrorMessage(pgsqlConn));
                ast_mutex_unlock(&pgsql_lock);
                return -1;
        } else {
@@ -407,9 +407,9 @@ static int update_pgsql(const char *database, const char *table, const char *key
                        && result_status != PGRES_NONFATAL_ERROR) {
                        ast_log(LOG_WARNING,
                                        "Postgresql RealTime: Failed to query database. Check debug for more info.\n");
-                       ast_log(LOG_DEBUG, "Postgresql RealTime: Query: %s\n", sql);
-                       ast_log(LOG_DEBUG, "Postgresql RealTime: Query Failed because: %s (%s)\n",
-                                       PQresultErrorMessage(result), PQresStatus(result_status));
+                       ast_debug(1, "Postgresql RealTime: Query: %s\n", sql);
+                       ast_debug(1, "Postgresql RealTime: Query Failed because: %s (%s)\n",
+                                               PQresultErrorMessage(result), PQresStatus(result_status));
                        ast_mutex_unlock(&pgsql_lock);
                        return -1;
                }
@@ -418,8 +418,7 @@ static int update_pgsql(const char *database, const char *table, const char *key
        numrows = atoi(PQcmdTuples(result));
        ast_mutex_unlock(&pgsql_lock);
 
-       ast_log(LOG_DEBUG, "Postgresql RealTime: Updated %d rows on table: %s\n", numrows,
-                       table);
+       ast_debug(1, "Postgresql RealTime: Updated %d rows on table: %s\n", numrows, table);
 
        /* From http://dev.pgsql.com/doc/pgsql/en/pgsql-affected-rows.html
         * An integer greater than zero indicates the number of rows affected
@@ -434,13 +433,16 @@ static int update_pgsql(const char *database, const char *table, const char *key
 }
 
 static struct ast_config *config_pgsql(const char *database, const char *table,
-                                          const char *file, struct ast_config *cfg)
+                                          const char *file, struct ast_config *cfg,
+                                          int withcomments)
 {
        PGresult *result = NULL;
        long num_rows;
        struct ast_variable *new_v;
        struct ast_category *cur_cat = NULL;
-       char sql[250] = "";
+       char sqlbuf[1024] = "";
+       char *sql;
+       size_t sqlleft = sizeof(sqlbuf);
        char last[80] = "";
        int last_cat_metric = 0;
 
@@ -451,11 +453,11 @@ static struct ast_config *config_pgsql(const char *database, const char *table,
                return NULL;
        }
 
-       snprintf(sql, sizeof(sql),
-                        "SELECT category, var_name, var_val, cat_metric FROM %s WHERE filename='%s' and commented=0 ORDER BY filename, cat_metric desc, var_metric asc, category, var_name, var_val, id",
-                        table, file);
+       ast_build_string(&sql, &sqlleft, "SELECT category, var_name, var_val, cat_metric FROM %s ", table);
+       ast_build_string(&sql, &sqlleft, "WHERE filename='%s' and commented=0", file);
+       ast_build_string(&sql, &sqlleft, "ORDER BY cat_metric DESC, var_metric ASC, category, var_name ");
 
-       ast_log(LOG_DEBUG, "Postgresql RealTime: Static SQL: %s\n", sql);
+       ast_debug(1, "Postgresql RealTime: Static SQL: %s\n", sqlbuf);
 
        /* We now have our complete statement; Lets connect to the server and execute it. */
        ast_mutex_lock(&pgsql_lock);
@@ -464,12 +466,11 @@ static struct ast_config *config_pgsql(const char *database, const char *table,
                return NULL;
        }
 
-       if (!(result = PQexec(pgsqlConn, sql))) {
+       if (!(result = PQexec(pgsqlConn, sqlbuf))) {
                ast_log(LOG_WARNING,
                                "Postgresql RealTime: Failed to query database. Check debug for more info.\n");
-               ast_log(LOG_DEBUG, "Postgresql RealTime: Query: %s\n", sql);
-               ast_log(LOG_DEBUG, "Postgresql RealTime: Query Failed because: %s\n",
-                               PQerrorMessage(pgsqlConn));
+               ast_debug(1, "Postgresql RealTime: Query: %s\n", sql);
+               ast_debug(1, "Postgresql RealTime: Query Failed because: %s\n", PQerrorMessage(pgsqlConn));
                ast_mutex_unlock(&pgsql_lock);
                return NULL;
        } else {
@@ -479,29 +480,18 @@ static struct ast_config *config_pgsql(const char *database, const char *table,
                        && result_status != PGRES_NONFATAL_ERROR) {
                        ast_log(LOG_WARNING,
                                        "Postgresql RealTime: Failed to query database. Check debug for more info.\n");
-                       ast_log(LOG_DEBUG, "Postgresql RealTime: Query: %s\n", sql);
-                       ast_log(LOG_DEBUG, "Postgresql RealTime: Query Failed because: %s (%s)\n",
-                                       PQresultErrorMessage(result), PQresStatus(result_status));
+                       ast_debug(1, "Postgresql RealTime: Query: %s\n", sql);
+                       ast_debug(1, "Postgresql RealTime: Query Failed because: %s (%s)\n",
+                                               PQresultErrorMessage(result), PQresStatus(result_status));
                        ast_mutex_unlock(&pgsql_lock);
                        return NULL;
                }
        }
 
        if ((num_rows = PQntuples(result)) > 0) {
-               int numFields = PQnfields(result);
-               int i = 0;
                int rowIndex = 0;
-               char **fieldnames = NULL;
 
-               ast_log(LOG_DEBUG, "Postgresql RealTime: Found %ld rows.\n", num_rows);
-
-               if (!(fieldnames = ast_calloc(1, numFields * sizeof(char *)))) {
-                       ast_mutex_unlock(&pgsql_lock);
-                       PQclear(result);
-                       return NULL;
-               }
-               for (i = 0; i < numFields; i++)
-                       fieldnames[i] = PQfname(result, i);
+               ast_debug(1, "Postgresql RealTime: Found %ld rows.\n", num_rows);
 
                for (rowIndex = 0; rowIndex < num_rows; rowIndex++) {
                        char *field_category = PQgetvalue(result, rowIndex, 0);
@@ -509,7 +499,7 @@ static struct ast_config *config_pgsql(const char *database, const char *table,
                        char *field_var_val = PQgetvalue(result, rowIndex, 2);
                        char *field_cat_metric = PQgetvalue(result, rowIndex, 3);
                        if (!strcmp(field_var_name, "#include")) {
-                               if (!ast_config_internal_load(field_var_val, cfg)) {
+                               if (!ast_config_internal_load(field_var_val, cfg, 0)) {
                                        PQclear(result);
                                        ast_mutex_unlock(&pgsql_lock);
                                        return NULL;
@@ -547,31 +537,31 @@ static struct ast_config_engine pgsql_engine = {
        .update_func = update_pgsql
 };
 
-int load_module(void)
+static int load_module(void)
 {
-       parse_config();
+       if(!parse_config())
+               return AST_MODULE_LOAD_DECLINE;
 
        ast_mutex_lock(&pgsql_lock);
 
        if (!pgsql_reconnect(NULL)) {
                ast_log(LOG_WARNING,
                                "Postgresql RealTime: Couldn't establish connection. Check debug.\n");
-               ast_log(LOG_DEBUG, "Postgresql RealTime: Cannot Connect: %s\n",
-                               PQerrorMessage(pgsqlConn));
+               ast_debug(1, "Postgresql RealTime: Cannot Connect: %s\n", PQerrorMessage(pgsqlConn));
        }
 
        ast_config_engine_register(&pgsql_engine);
        if (option_verbose) {
                ast_verbose("Postgresql RealTime driver loaded.\n");
        }
-       ast_cli_register(&cli_realtime_pgsql_status);
+       ast_cli_register_multiple(cli_realtime, sizeof(cli_realtime) / sizeof(struct ast_cli_entry));
 
        ast_mutex_unlock(&pgsql_lock);
 
        return 0;
 }
 
-int unload_module(void)
+static int unload_module(void)
 {
        /* Aquire control before doing anything to the module itself. */
        ast_mutex_lock(&pgsql_lock);
@@ -580,13 +570,13 @@ int unload_module(void)
                PQfinish(pgsqlConn);
                pgsqlConn = NULL;
        };
-       ast_cli_unregister(&cli_realtime_pgsql_status);
+       ast_cli_unregister_multiple(cli_realtime, sizeof(cli_realtime) / sizeof(struct ast_cli_entry));
        ast_config_engine_deregister(&pgsql_engine);
        if (option_verbose) {
                ast_verbose("Postgresql RealTime unloaded.\n");
        }
 
-       STANDARD_HANGUP_LOCALUSERS;
+       ast_module_user_hangup_all();
 
        /* Unlock so something else can destroy the lock. */
        ast_mutex_unlock(&pgsql_lock);
@@ -594,7 +584,7 @@ int unload_module(void)
        return 0;
 }
 
-int reload(void)
+static int reload(void)
 {
        /* Aquire control before doing anything to the module itself. */
        ast_mutex_lock(&pgsql_lock);
@@ -608,8 +598,7 @@ int reload(void)
        if (!pgsql_reconnect(NULL)) {
                ast_log(LOG_WARNING,
                                "Postgresql RealTime: Couldn't establish connection. Check debug.\n");
-               ast_log(LOG_DEBUG, "Postgresql RealTime: Cannot Connect: %s\n",
-                               PQerrorMessage(pgsqlConn));
+               ast_debug(1, "Postgresql RealTime: Cannot Connect: %s\n", PQerrorMessage(pgsqlConn));
        }
 
        ast_verbose(VERBOSE_PREFIX_2 "Postgresql RealTime reloaded.\n");
@@ -620,97 +609,79 @@ int reload(void)
        return 0;
 }
 
-int parse_config(void)
+static int parse_config(void)
 {
        struct ast_config *config;
-       char *s;
+       const char *s;
 
        config = ast_config_load(RES_CONFIG_PGSQL_CONF);
 
-       if (config) {
-               if (!(s = ast_variable_retrieve(config, "general", "dbuser"))) {
-                       ast_log(LOG_WARNING,
-                                       "Postgresql RealTime: No database user found, using 'asterisk' as default.\n");
-                       strcpy(dbuser, "asterisk");
-               } else {
-                       ast_copy_string(dbuser, s, sizeof(dbuser));
-               }
-
-               if (!(s = ast_variable_retrieve(config, "general", "dbpass"))) {
-                       ast_log(LOG_WARNING,
-                                       "Postgresql RealTime: No database password found, using 'asterisk' as default.\n");
-                       strcpy(dbpass, "asterisk");
-               } else {
-                       ast_copy_string(dbpass, s, sizeof(dbpass));
-               }
-
-               if (!(s = ast_variable_retrieve(config, "general", "dbhost"))) {
-                       ast_log(LOG_WARNING,
-                                       "Postgresql RealTime: No database host found, using localhost via socket.\n");
-                       dbhost[0] = '\0';
-               } else {
-                       ast_copy_string(dbhost, s, sizeof(dbhost));
-               }
-
-               if (!(s = ast_variable_retrieve(config, "general", "dbname"))) {
-                       ast_log(LOG_WARNING,
-                                       "Postgresql RealTime: No database name found, using 'asterisk' as default.\n");
-                       strcpy(dbname, "asterisk");
-               } else {
-                       ast_copy_string(dbname, s, sizeof(dbname));
-               }
-
-               if (!(s = ast_variable_retrieve(config, "general", "dbport"))) {
-                       ast_log(LOG_WARNING,
-                                       "Postgresql RealTime: No database port found, using 5432 as default.\n");
-                       dbport = 5432;
-               } else {
-                       dbport = atoi(s);
-               }
+       if (!config) {
+               ast_log(LOG_WARNING, "Unable to load config %s\n",RES_CONFIG_PGSQL_CONF);
+               return 0;
+       }
+       if (!(s = ast_variable_retrieve(config, "general", "dbuser"))) {
+               ast_log(LOG_WARNING,
+                               "Postgresql RealTime: No database user found, using 'asterisk' as default.\n");
+               strcpy(dbuser, "asterisk");
+       } else {
+               ast_copy_string(dbuser, s, sizeof(dbuser));
+       }
 
-               if (dbhost && !(s = ast_variable_retrieve(config, "general", "dbsock"))) {
-                       ast_log(LOG_WARNING,
-                                       "Postgresql RealTime: No database socket found, using '/tmp/pgsql.sock' as default.\n");
-                       strcpy(dbsock, "/tmp/pgsql.sock");
-               } else {
-                       ast_copy_string(dbsock, s, sizeof(dbsock));
-               }
+       if (!(s = ast_variable_retrieve(config, "general", "dbpass"))) {
+               ast_log(LOG_WARNING,
+                               "Postgresql RealTime: No database password found, using 'asterisk' as default.\n");
+               strcpy(dbpass, "asterisk");
+       } else {
+               ast_copy_string(dbpass, s, sizeof(dbpass));
        }
-       ast_config_destroy(config);
 
-       if (dbhost) {
-               ast_log(LOG_DEBUG, "Postgresql RealTime Host: %s\n", dbhost);
-               ast_log(LOG_DEBUG, "Postgresql RealTime Port: %i\n", dbport);
+       if (!(s = ast_variable_retrieve(config, "general", "dbhost"))) {
+               ast_log(LOG_WARNING,
+                               "Postgresql RealTime: No database host found, using localhost via socket.\n");
+               dbhost[0] = '\0';
        } else {
-               ast_log(LOG_DEBUG, "Postgresql RealTime Socket: %s\n", dbsock);
+               ast_copy_string(dbhost, s, sizeof(dbhost));
        }
-       ast_log(LOG_DEBUG, "Postgresql RealTime User: %s\n", dbuser);
-       ast_log(LOG_DEBUG, "Postgresql RealTime Password: %s\n", dbpass);
-       ast_log(LOG_DEBUG, "Postgresql RealTime DBName: %s\n", dbname);
 
-       return 1;
-}
+       if (!(s = ast_variable_retrieve(config, "general", "dbname"))) {
+               ast_log(LOG_WARNING,
+                               "Postgresql RealTime: No database name found, using 'asterisk' as default.\n");
+               strcpy(dbname, "asterisk");
+       } else {
+               ast_copy_string(dbname, s, sizeof(dbname));
+       }
 
-const char *description(void)
-{
-       return "Postgresql RealTime Configuration Driver";
+       if (!(s = ast_variable_retrieve(config, "general", "dbport"))) {
+               ast_log(LOG_WARNING,
+                               "Postgresql RealTime: No database port found, using 5432 as default.\n");
+               dbport = 5432;
+       } else {
+               dbport = atoi(s);
+       }
 
-}
+       if (dbhost && !(s = ast_variable_retrieve(config, "general", "dbsock"))) {
+               ast_log(LOG_WARNING,
+                               "Postgresql RealTime: No database socket found, using '/tmp/pgsql.sock' as default.\n");
+               strcpy(dbsock, "/tmp/pgsql.sock");
+       } else {
+               ast_copy_string(dbsock, s, sizeof(dbsock));
+       }
+       ast_config_destroy(config);
 
-int usecount(void)
-{
-       /* Try and get a lock. If unsuccessful, than that means another thread is using the pgsql object. */
-       if (ast_mutex_trylock(&pgsql_lock)) {
-               ast_log(LOG_DEBUG, "Postgresql RealTime: Module usage count is 1.\n");
-               return 1;
+       if (option_debug) {
+               if (dbhost) {
+                       ast_debug(1, "Postgresql RealTime Host: %s\n", dbhost);
+                       ast_debug(1, "Postgresql RealTime Port: %i\n", dbport);
+               } else {
+                       ast_debug(1, "Postgresql RealTime Socket: %s\n", dbsock);
+               }
+               ast_debug(1, "Postgresql RealTime User: %s\n", dbuser);
+               ast_debug(1, "Postgresql RealTime Password: %s\n", dbpass);
+               ast_debug(1, "Postgresql RealTime DBName: %s\n", dbname);
        }
-       ast_mutex_unlock(&pgsql_lock);
-       return 0;
-}
 
-const char *key(void)
-{
-       return ASTERISK_GPL_KEY;
+       return 1;
 }
 
 static int pgsql_reconnect(const char *database)
@@ -738,26 +709,25 @@ static int pgsql_reconnect(const char *database)
                
                sprintf(connInfo, "host=%s port=%d dbname=%s user=%s password=%s",
                                        dbhost, dbport, my_database, dbuser, dbpass);
-               ast_log(LOG_DEBUG, "%u connInfo=%s\n", size, connInfo);
+               ast_debug(1, "%u connInfo=%s\n", size, connInfo);
                pgsqlConn = PQconnectdb(connInfo);
-               ast_log(LOG_DEBUG, "%u connInfo=%s\n", size, connInfo);
-               free(connInfo);
+               ast_debug(1, "%u connInfo=%s\n", size, connInfo);
+               ast_free(connInfo);
                connInfo = NULL;
-               ast_log(LOG_DEBUG, "pgsqlConn=%p\n", pgsqlConn);
-               if (pgsqlConn) {
-                       ast_log(LOG_DEBUG, "Postgresql RealTime: Successfully connected to database.\n");
+               ast_debug(1, "pgsqlConn=%p\n", pgsqlConn);
+               if (pgsqlConn && PQstatus(pgsqlConn) == CONNECTION_OK) {
+                       ast_debug(1, "Postgresql RealTime: Successfully connected to database.\n");
                        connect_time = time(NULL);
                        return 1;
                } else {
                        ast_log(LOG_ERROR,
                                        "Postgresql RealTime: Failed to connect database server %s on %s. Check debug for more info.\n",
                                        dbname, dbhost);
-                       ast_log(LOG_DEBUG, "Postgresql RealTime: Cannot Connect: %s\n",
-                                       PQresultErrorMessage(NULL));
+                       ast_debug(1, "Postgresql RealTime: Cannot Connect: %s\n", PQresultErrorMessage(NULL));
                        return 0;
                }
        } else {
-               ast_log(LOG_DEBUG, "Postgresql RealTime: Everything is fine.\n");
+               ast_debug(1, "Postgresql RealTime: Everything is fine.\n");
                return 1;
        }
 }
@@ -803,3 +773,10 @@ static int realtime_pgsql_status(int fd, int argc, char **argv)
                return RESULT_FAILURE;
        }
 }
+
+/* needs usecount semantics defined */
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "PostgreSQL RealTime Configuration Driver",
+               .load = load_module,
+               .unload = unload_module,
+               .reload = reload
+              );