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