cel_pgsql, cdr_pgsql, res_config_pgsql: Add PostgreSQL application_name support
[asterisk/asterisk.git] / cel / cel_pgsql.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2008
5  *
6  * Steve Murphy - adapted to CEL, from:
7  * Matthew D. Hardeman <mhardemn@papersoft.com>
8  * Adapted from the MySQL CDR logger originally by James Sharp
9  *
10  * Modified April, 2007; Dec, 2008
11  * Steve Murphy <murf@digium.com>
12
13  * Modified September 2003
14  * Matthew D. Hardeman <mhardemn@papersoft.com>
15  *
16  * See http://www.asterisk.org for more information about
17  * the Asterisk project. Please do not directly contact
18  * any of the maintainers of this project for assistance;
19  * the project provides a web site, mailing lists and IRC
20  * channels for your use.
21  *
22  * This program is free software, distributed under the terms of
23  * the GNU General Public License Version 2. See the LICENSE file
24  * at the top of the source tree.
25  */
26
27 /*! \file
28  *
29  * \brief PostgreSQL CEL logger
30  *
31  * \author Steve Murphy <murf@digium.com>
32  * PostgreSQL http://www.postgresql.org/
33  *
34  * See also
35  * \arg \ref Config_cel
36  * PostgreSQL http://www.postgresql.org/
37  * \ingroup cel_drivers
38  */
39
40 /*** MODULEINFO
41         <depend>pgsql</depend>
42         <support_level>extended</support_level>
43  ***/
44
45 #include "asterisk.h"
46
47 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
48
49 #include <libpq-fe.h>
50
51 #include "asterisk/config.h"
52 #include "asterisk/options.h"
53 #include "asterisk/channel.h"
54 #include "asterisk/cel.h"
55 #include "asterisk/module.h"
56 #include "asterisk/logger.h"
57 #include "asterisk.h"
58
59 #define DATE_FORMAT "%Y-%m-%d %T.%6q"
60
61 #define PGSQL_BACKEND_NAME "CEL PGSQL backend"
62
63 static char *config = "cel_pgsql.conf";
64
65 static char *pghostname;
66 static char *pgdbname;
67 static char *pgdbuser;
68 static char *pgpassword;
69 static char *pgappname;
70 static char *pgdbport;
71 static char *table;
72
73 static int connected = 0;
74 static int maxsize = 512, maxsize2 = 512;
75
76 /*! \brief show_user_def is off by default */
77 #define CEL_SHOW_USERDEF_DEFAULT        0
78
79 /*! TRUE if we should set the eventtype field to USER_DEFINED on user events. */
80 static unsigned char cel_show_user_def;
81
82 AST_MUTEX_DEFINE_STATIC(pgsql_lock);
83
84 static PGconn   *conn = NULL;
85 static PGresult *result = NULL;
86
87 struct columns {
88         char *name;
89         char *type;
90         int len;
91         unsigned int notnull:1;
92         unsigned int hasdefault:1;
93         AST_RWLIST_ENTRY(columns) list;
94 };
95
96 static AST_RWLIST_HEAD_STATIC(psql_columns, columns);
97
98 #define LENGTHEN_BUF1(size) \
99         do { \
100                 /* Lengthen buffer, if necessary */ \
101                 if (ast_str_strlen(sql) + size + 1 > ast_str_size(sql)) { \
102                         if (ast_str_make_space(&sql, ((ast_str_size(sql) + size + 3) / 512 + 1) * 512) != 0) { \
103                                 ast_log(LOG_ERROR, "Unable to allocate sufficient memory.  Insert CDR failed.\n"); \
104                                 ast_free(sql); \
105                                 ast_free(sql2); \
106                                 AST_RWLIST_UNLOCK(&psql_columns); \
107                                 return; \
108                         } \
109                 } \
110         } while (0)
111
112 #define LENGTHEN_BUF2(size) \
113         do { \
114                 if (ast_str_strlen(sql2) + size + 1 > ast_str_size(sql2)) { \
115                         if (ast_str_make_space(&sql2, ((ast_str_size(sql2) + size + 3) / 512 + 1) * 512) != 0) { \
116                                 ast_log(LOG_ERROR, "Unable to allocate sufficient memory.  Insert CDR failed.\n"); \
117                                 ast_free(sql); \
118                                 ast_free(sql2); \
119                                 AST_RWLIST_UNLOCK(&psql_columns); \
120                                 return; \
121                         } \
122                 } \
123         } while (0)
124
125 static void pgsql_reconnect(void)
126 {
127         struct ast_str *conn_info = ast_str_create(128);
128         if (!conn_info) {
129                 ast_log(LOG_ERROR, "Failed to allocate memory for connection string.\n");
130                 return;
131         }
132
133         if (conn) {
134                 PQfinish(conn);
135                 conn = NULL;
136         }
137
138         ast_str_set(&conn_info, 0, "host=%s port=%s dbname=%s user=%s",
139                 pghostname, pgdbport, pgdbname, pgdbuser);
140
141         if (!ast_strlen_zero(pgappname)) {
142                 ast_str_append(&conn_info, 0, " application_name=%s", pgappname);
143         }
144
145         if (!ast_strlen_zero(pgpassword)) {
146                 ast_str_append(&conn_info, 0, " password=%s", pgpassword);
147         }
148
149         conn = PQconnectdb(ast_str_buffer(conn_info));
150         ast_free(conn_info);
151 }
152
153
154 static void pgsql_log(struct ast_event *event)
155 {
156         struct ast_tm tm;
157         char timestr[128];
158         char *pgerror;
159         struct ast_cel_event_record record = {
160                 .version = AST_CEL_EVENT_RECORD_VERSION,
161         };
162
163         if (ast_cel_fill_record(event, &record)) {
164                 return;
165         }
166
167         ast_mutex_lock(&pgsql_lock);
168
169         ast_localtime(&record.event_time, &tm, NULL);
170         ast_strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
171
172         if ((!connected) && pghostname && pgdbuser && pgpassword && pgdbname) {
173                 pgsql_reconnect();
174                 if (PQstatus(conn) != CONNECTION_BAD) {
175                         connected = 1;
176                 } else {
177                         pgerror = PQerrorMessage(conn);
178                         ast_log(LOG_ERROR, "cel_pgsql: Unable to connect to database server %s.  Calls will not be logged!\n", pghostname);
179                         ast_log(LOG_ERROR, "cel_pgsql: Reason: %s\n", pgerror);
180                         PQfinish(conn);
181                         conn = NULL;
182                 }
183         }
184         if (connected) {
185                 struct columns *cur;
186                 struct ast_str *sql = ast_str_create(maxsize), *sql2 = ast_str_create(maxsize2);
187                 char buf[257], escapebuf[513];
188                 const char *value;
189                 int first = 1;
190
191                 if (!sql || !sql2) {
192                         goto ast_log_cleanup;
193                 }
194
195                 ast_str_set(&sql, 0, "INSERT INTO %s (", table);
196                 ast_str_set(&sql2, 0, " VALUES (");
197
198 #define SEP (first ? "" : ",")
199
200                 AST_RWLIST_RDLOCK(&psql_columns);
201                 AST_RWLIST_TRAVERSE(&psql_columns, cur, list) {
202                         LENGTHEN_BUF1(strlen(cur->name) + 2);
203                         ast_str_append(&sql, 0, "%s\"%s\"", first ? "" : ",", cur->name);
204
205                         if (strcmp(cur->name, "eventtime") == 0) {
206                                 if (strncmp(cur->type, "int", 3) == 0) {
207                                         LENGTHEN_BUF2(13);
208                                         ast_str_append(&sql2, 0, "%s%ld", SEP, (long) record.event_time.tv_sec);
209                                 } else if (strncmp(cur->type, "float", 5) == 0) {
210                                         LENGTHEN_BUF2(31);
211                                         ast_str_append(&sql2, 0, "%s%f",
212                                                 SEP,
213                                                 (double) record.event_time.tv_sec +
214                                                 (double) record.event_time.tv_usec / 1000000.0);
215                                 } else {
216                                         /* char, hopefully */
217                                         LENGTHEN_BUF2(31);
218                                         ast_localtime(&record.event_time, &tm, NULL);
219                                         ast_strftime(buf, sizeof(buf), DATE_FORMAT, &tm);
220                                         ast_str_append(&sql2, 0, "%s'%s'", SEP, buf);
221                                 }
222                         } else if (strcmp(cur->name, "eventtype") == 0) {
223                                 if (cur->type[0] == 'i') {
224                                         /* Get integer, no need to escape anything */
225                                         LENGTHEN_BUF2(5);
226                                         ast_str_append(&sql2, 0, "%s%d", SEP, (int) record.event_type);
227                                 } else if (strncmp(cur->type, "float", 5) == 0) {
228                                         LENGTHEN_BUF2(31);
229                                         ast_str_append(&sql2, 0, "%s%f", SEP, (double) record.event_type);
230                                 } else {
231                                         /* Char field, probably */
232                                         const char *event_name;
233
234                                         event_name = (!cel_show_user_def
235                                                 && record.event_type == AST_CEL_USER_DEFINED)
236                                                 ? record.user_defined_name : record.event_name;
237                                         LENGTHEN_BUF2(strlen(event_name) + 1);
238                                         ast_str_append(&sql2, 0, "%s'%s'", SEP, event_name);
239                                 }
240                         } else if (strcmp(cur->name, "amaflags") == 0) {
241                                 if (strncmp(cur->type, "int", 3) == 0) {
242                                         /* Integer, no need to escape anything */
243                                         LENGTHEN_BUF2(13);
244                                         ast_str_append(&sql2, 0, "%s%u", SEP, record.amaflag);
245                                 } else {
246                                         /* Although this is a char field, there are no special characters in the values for these fields */
247                                         LENGTHEN_BUF2(31);
248                                         ast_str_append(&sql2, 0, "%s'%u'", SEP, record.amaflag);
249                                 }
250                         } else {
251                                 /* Arbitrary field, could be anything */
252                                 if (strcmp(cur->name, "userdeftype") == 0) {
253                                         value = record.user_defined_name;
254                                 } else if (strcmp(cur->name, "cid_name") == 0) {
255                                         value = record.caller_id_name;
256                                 } else if (strcmp(cur->name, "cid_num") == 0) {
257                                         value = record.caller_id_num;
258                                 } else if (strcmp(cur->name, "cid_ani") == 0) {
259                                         value = record.caller_id_ani;
260                                 } else if (strcmp(cur->name, "cid_rdnis") == 0) {
261                                         value = record.caller_id_rdnis;
262                                 } else if (strcmp(cur->name, "cid_dnid") == 0) {
263                                         value = record.caller_id_dnid;
264                                 } else if (strcmp(cur->name, "exten") == 0) {
265                                         value = record.extension;
266                                 } else if (strcmp(cur->name, "context") == 0) {
267                                         value = record.context;
268                                 } else if (strcmp(cur->name, "channame") == 0) {
269                                         value = record.channel_name;
270                                 } else if (strcmp(cur->name, "appname") == 0) {
271                                         value = record.application_name;
272                                 } else if (strcmp(cur->name, "appdata") == 0) {
273                                         value = record.application_data;
274                                 } else if (strcmp(cur->name, "accountcode") == 0) {
275                                         value = record.account_code;
276                                 } else if (strcmp(cur->name, "peeraccount") == 0) {
277                                         value = record.peer_account;
278                                 } else if (strcmp(cur->name, "uniqueid") == 0) {
279                                         value = record.unique_id;
280                                 } else if (strcmp(cur->name, "linkedid") == 0) {
281                                         value = record.linked_id;
282                                 } else if (strcmp(cur->name, "userfield") == 0) {
283                                         value = record.user_field;
284                                 } else if (strcmp(cur->name, "peer") == 0) {
285                                         value = record.peer;
286                                 } else if (strcmp(cur->name, "extra") == 0) {
287                                         value = record.extra;
288                                 } else {
289                                         value = NULL;
290                                 }
291
292                                 if (value == NULL) {
293                                         ast_str_append(&sql2, 0, "%sDEFAULT", SEP);
294                                 } else if (strncmp(cur->type, "int", 3) == 0) {
295                                         long long whatever;
296                                         if (value && sscanf(value, "%30lld", &whatever) == 1) {
297                                                 LENGTHEN_BUF2(26);
298                                                 ast_str_append(&sql2, 0, "%s%lld", SEP, whatever);
299                                         } else {
300                                                 LENGTHEN_BUF2(2);
301                                                 ast_str_append(&sql2, 0, "%s0", SEP);
302                                         }
303                                 } else if (strncmp(cur->type, "float", 5) == 0) {
304                                         long double whatever;
305                                         if (value && sscanf(value, "%30Lf", &whatever) == 1) {
306                                                 LENGTHEN_BUF2(51);
307                                                 ast_str_append(&sql2, 0, "%s%30Lf", SEP, whatever);
308                                         } else {
309                                                 LENGTHEN_BUF2(2);
310                                                 ast_str_append(&sql2, 0, "%s0", SEP);
311                                         }
312                                         /* XXX Might want to handle dates, times, and other misc fields here XXX */
313                                 } else {
314                                         if (value) {
315                                                 PQescapeStringConn(conn, escapebuf, value, strlen(value), NULL);
316                                         } else {
317                                                 escapebuf[0] = '\0';
318                                         }
319                                         LENGTHEN_BUF2(strlen(escapebuf) + 3);
320                                         ast_str_append(&sql2, 0, "%s'%s'", SEP, escapebuf);
321                                 }
322                         }
323                         first = 0;
324                 }
325                 AST_RWLIST_UNLOCK(&psql_columns);
326                 LENGTHEN_BUF1(ast_str_strlen(sql2) + 2);
327                 ast_str_append(&sql, 0, ")%s)", ast_str_buffer(sql2));
328
329                 ast_debug(3, "Inserting a CEL record: [%s].\n", ast_str_buffer(sql));
330                 /* Test to be sure we're still connected... */
331                 /* If we're connected, and connection is working, good. */
332                 /* Otherwise, attempt reconnect.  If it fails... sorry... */
333                 if (PQstatus(conn) == CONNECTION_OK) {
334                         connected = 1;
335                 } else {
336                         ast_log(LOG_WARNING, "Connection was lost... attempting to reconnect.\n");
337                         PQreset(conn);
338                         if (PQstatus(conn) == CONNECTION_OK) {
339                                 ast_log(LOG_NOTICE, "Connection reestablished.\n");
340                                 connected = 1;
341                         } else {
342                                 pgerror = PQerrorMessage(conn);
343                                 ast_log(LOG_ERROR, "Unable to reconnect to database server %s. Calls will not be logged!\n", pghostname);
344                                 ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
345                                 PQfinish(conn);
346                                 conn = NULL;
347                                 connected = 0;
348                                 goto ast_log_cleanup;
349                         }
350                 }
351                 result = PQexec(conn, ast_str_buffer(sql));
352                 if (PQresultStatus(result) != PGRES_COMMAND_OK) {
353                         pgerror = PQresultErrorMessage(result);
354                         ast_log(LOG_WARNING, "Failed to insert call detail record into database!\n");
355                         ast_log(LOG_WARNING, "Reason: %s\n", pgerror);
356                         ast_log(LOG_WARNING, "Connection may have been lost... attempting to reconnect.\n");
357                         PQreset(conn);
358                         if (PQstatus(conn) == CONNECTION_OK) {
359                                 ast_log(LOG_NOTICE, "Connection reestablished.\n");
360                                 connected = 1;
361                                 PQclear(result);
362                                 result = PQexec(conn, ast_str_buffer(sql));
363                                 if (PQresultStatus(result) != PGRES_COMMAND_OK) {
364                                         pgerror = PQresultErrorMessage(result);
365                                         ast_log(LOG_ERROR, "HARD ERROR!  Attempted reconnection failed.  DROPPING CALL RECORD!\n");
366                                         ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
367                                 }
368                         }
369                         PQclear(result);
370                         goto ast_log_cleanup;
371                 }
372                 PQclear(result);
373
374 ast_log_cleanup:
375                 ast_free(sql);
376                 ast_free(sql2);
377         }
378
379         ast_mutex_unlock(&pgsql_lock);
380 }
381
382 static int my_unload_module(void)
383 {
384         struct columns *current;
385
386         ast_cel_backend_unregister(PGSQL_BACKEND_NAME);
387         AST_RWLIST_WRLOCK(&psql_columns);
388         if (conn) {
389                 PQfinish(conn);
390                 conn = NULL;
391         }
392         if (pghostname) {
393                 ast_free(pghostname);
394                 pghostname = NULL;
395         }
396         if (pgdbname) {
397                 ast_free(pgdbname);
398                 pgdbname = NULL;
399         }
400         if (pgdbuser) {
401                 ast_free(pgdbuser);
402                 pgdbuser = NULL;
403         }
404         if (pgpassword) {
405                 ast_free(pgpassword);
406                 pgpassword = NULL;
407         }
408         if (pgappname) {
409                 ast_free(pgappname);
410                 pgappname = NULL;
411         }
412         if (pgdbport) {
413                 ast_free(pgdbport);
414                 pgdbport = NULL;
415         }
416         if (table) {
417                 ast_free(table);
418                 table = NULL;
419         }
420         while ((current = AST_RWLIST_REMOVE_HEAD(&psql_columns, list))) {
421                 ast_free(current);
422         }
423         AST_RWLIST_UNLOCK(&psql_columns);
424         return 0;
425 }
426
427 static int unload_module(void)
428 {
429         return my_unload_module();
430 }
431
432 static int process_my_load_module(struct ast_config *cfg)
433 {
434         struct ast_variable *var;
435         char *pgerror;
436         const char *tmp;
437         PGresult *result;
438         struct columns *cur;
439
440         if (!(var = ast_variable_browse(cfg, "global"))) {
441                 ast_log(LOG_WARNING,"CEL pgsql config file missing global section.\n");
442                 return AST_MODULE_LOAD_DECLINE;
443         }
444         if (!(tmp = ast_variable_retrieve(cfg,"global","hostname"))) {
445                 ast_log(LOG_WARNING,"PostgreSQL server hostname not specified.  Assuming unix socket connection\n");
446                 tmp = "";       /* connect via UNIX-socket by default */
447         }
448         if (pghostname)
449                 ast_free(pghostname);
450         if (!(pghostname = ast_strdup(tmp))) {
451                 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying host info\n");
452                 return AST_MODULE_LOAD_DECLINE;
453         }
454         if (!(tmp = ast_variable_retrieve(cfg, "global", "dbname"))) {
455                 ast_log(LOG_WARNING,"PostgreSQL database not specified.  Assuming asterisk\n");
456                 tmp = "asteriskceldb";
457         }
458         if (pgdbname)
459                 ast_free(pgdbname);
460         if (!(pgdbname = ast_strdup(tmp))) {
461                 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying dbname info\n");
462                 return AST_MODULE_LOAD_DECLINE;
463         }
464         if (!(tmp = ast_variable_retrieve(cfg, "global", "user"))) {
465                 ast_log(LOG_WARNING,"PostgreSQL database user not specified.  Assuming asterisk\n");
466                 tmp = "asterisk";
467         }
468         if (pgdbuser)
469                 ast_free(pgdbuser);
470         if (!(pgdbuser = ast_strdup(tmp))) {
471                 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying user info\n");
472                 return AST_MODULE_LOAD_DECLINE;
473         }
474         if (!(tmp = ast_variable_retrieve(cfg, "global", "password"))) {
475                 ast_log(LOG_WARNING, "PostgreSQL database password not specified.  Assuming blank\n");
476                 tmp = "";
477         }
478         if (pgpassword)
479                 ast_free(pgpassword);
480         if (!(pgpassword = ast_strdup(tmp))) {
481                 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying password info\n");
482                 return AST_MODULE_LOAD_DECLINE;
483         }
484         if (!(tmp = ast_variable_retrieve(cfg, "global", "appname"))) {
485                 tmp = "";
486         }
487         if (pgappname) {
488                 ast_free(pgappname);
489         }
490         if (!(pgappname = ast_strdup(tmp))) {
491                 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying appname info\n");
492                 return AST_MODULE_LOAD_DECLINE;
493         }
494
495         if (!(tmp = ast_variable_retrieve(cfg,"global","port"))) {
496                 ast_log(LOG_WARNING,"PostgreSQL database port not specified.  Using default 5432.\n");
497                 tmp = "5432";
498         }
499         if (pgdbport)
500                 ast_free(pgdbport);
501         if (!(pgdbport = ast_strdup(tmp))) {
502                 ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying port info\n");
503                 return AST_MODULE_LOAD_DECLINE;
504         }
505         if (!(tmp = ast_variable_retrieve(cfg, "global", "table"))) {
506                 ast_log(LOG_WARNING,"CEL table not specified.  Assuming cel\n");
507                 tmp = "cel";
508         }
509         if (table)
510                 ast_free(table);
511         if (!(table = ast_strdup(tmp))) {
512                 return AST_MODULE_LOAD_DECLINE;
513         }
514         cel_show_user_def = CEL_SHOW_USERDEF_DEFAULT;
515         if ((tmp = ast_variable_retrieve(cfg, "global", "show_user_defined"))) {
516                 cel_show_user_def = ast_true(tmp) ? 1 : 0;
517         }
518         if (option_debug) {
519                 if (ast_strlen_zero(pghostname)) {
520                         ast_debug(3, "cel_pgsql: using default unix socket\n");
521                 } else {
522                         ast_debug(3, "cel_pgsql: got hostname of %s\n", pghostname);
523                 }
524                 ast_debug(3, "cel_pgsql: got port of %s\n", pgdbport);
525                 ast_debug(3, "cel_pgsql: got user of %s\n", pgdbuser);
526                 ast_debug(3, "cel_pgsql: got dbname of %s\n", pgdbname);
527                 ast_debug(3, "cel_pgsql: got password of %s\n", pgpassword);
528                 ast_debug(3, "cel_pgsql: got sql table name of %s\n", table);
529                 ast_debug(3, "cel_pgsql: got show_user_defined of %s\n",
530                         cel_show_user_def ? "Yes" : "No");
531         }
532
533         pgsql_reconnect();
534         if (PQstatus(conn) != CONNECTION_BAD) {
535                 char sqlcmd[512];
536                 char *fname, *ftype, *flen, *fnotnull, *fdef;
537                 char *tableptr;
538                 int i, rows;
539
540                 ast_debug(1, "Successfully connected to PostgreSQL database.\n");
541                 connected = 1;
542
543                 /* Remove any schema name from the table */
544                 if ((tableptr = strrchr(table, '.'))) {
545                         tableptr++;
546                 } else {
547                         tableptr = table;
548                 }
549
550                 /* Query the columns */
551                 snprintf(sqlcmd, sizeof(sqlcmd), "select a.attname, t.typname, a.attlen, a.attnotnull, d.adsrc from pg_class c, pg_type t, pg_attribute a left outer join pg_attrdef d on a.atthasdef and d.adrelid = a.attrelid and d.adnum = a.attnum where c.oid = a.attrelid and a.atttypid = t.oid and (a.attnum > 0) and c.relname = '%s' order by c.relname, attnum", tableptr);
552                 result = PQexec(conn, sqlcmd);
553                 if (PQresultStatus(result) != PGRES_TUPLES_OK) {
554                         pgerror = PQresultErrorMessage(result);
555                         ast_log(LOG_ERROR, "Failed to query database columns: %s\n", pgerror);
556                         PQclear(result);
557                         unload_module();
558                         return AST_MODULE_LOAD_DECLINE;
559                 }
560
561                 rows = PQntuples(result);
562                 for (i = 0; i < rows; i++) {
563                         fname = PQgetvalue(result, i, 0);
564                         ftype = PQgetvalue(result, i, 1);
565                         flen = PQgetvalue(result, i, 2);
566                         fnotnull = PQgetvalue(result, i, 3);
567                         fdef = PQgetvalue(result, i, 4);
568                         ast_verb(4, "Found column '%s' of type '%s'\n", fname, ftype);
569                         cur = ast_calloc(1, sizeof(*cur) + strlen(fname) + strlen(ftype) + 2);
570                         if (cur) {
571                                 sscanf(flen, "%30d", &cur->len);
572                                 cur->name = (char *)cur + sizeof(*cur);
573                                 cur->type = (char *)cur + sizeof(*cur) + strlen(fname) + 1;
574                                 strcpy(cur->name, fname);
575                                 strcpy(cur->type, ftype);
576                                 if (*fnotnull == 't') {
577                                         cur->notnull = 1;
578                                 } else {
579                                         cur->notnull = 0;
580                                 }
581                                 if (!ast_strlen_zero(fdef)) {
582                                         cur->hasdefault = 1;
583                                 } else {
584                                         cur->hasdefault = 0;
585                                 }
586                                 AST_RWLIST_INSERT_TAIL(&psql_columns, cur, list);
587                         }
588                 }
589                 PQclear(result);
590         } else {
591                 pgerror = PQerrorMessage(conn);
592                 ast_log(LOG_ERROR, "cel_pgsql: Unable to connect to database server %s.  CALLS WILL NOT BE LOGGED!!\n", pghostname);
593                 ast_log(LOG_ERROR, "cel_pgsql: Reason: %s\n", pgerror);
594                 connected = 0;
595                 PQfinish(conn);
596                 conn = NULL;
597         }
598         return AST_MODULE_LOAD_SUCCESS;
599 }
600
601 static int my_load_module(int reload)
602 {
603         struct ast_config *cfg;
604         struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
605
606         if ((cfg = ast_config_load(config, config_flags)) == NULL || cfg == CONFIG_STATUS_FILEINVALID) {
607                 ast_log(LOG_WARNING, "Unable to load config for PostgreSQL CEL's: %s\n", config);
608                 return AST_MODULE_LOAD_DECLINE;
609         } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
610                 return AST_MODULE_LOAD_SUCCESS;
611         }
612
613         if (reload) {
614                 my_unload_module();
615         }
616
617         process_my_load_module(cfg);
618         ast_config_destroy(cfg);
619
620         if (ast_cel_backend_register(PGSQL_BACKEND_NAME, pgsql_log)) {
621                 ast_log(LOG_WARNING, "Unable to subscribe to CEL events for pgsql\n");
622                 return AST_MODULE_LOAD_DECLINE;
623         }
624
625         return AST_MODULE_LOAD_SUCCESS;
626 }
627
628 static int load_module(void)
629 {
630         return my_load_module(0);
631 }
632
633 static int reload(void)
634 {
635         return my_load_module(1);
636 }
637
638 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PostgreSQL CEL Backend",
639         .load = load_module,
640         .unload = unload_module,
641         .reload = reload,
642         .load_pri = AST_MODPRI_CDR_DRIVER,
643 );