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