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