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