Memory leak
[asterisk/asterisk.git] / cdr / cdr_pgsql.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2003 - 2006
5  *
6  * Matthew D. Hardeman <mhardemn@papersoft.com> 
7  * Adapted from the MySQL CDR logger originally by James Sharp 
8  *
9  * Modified September 2003
10  * Matthew D. Hardeman <mhardemn@papersoft.com>
11  *
12  * See http://www.asterisk.org for more information about
13  * the Asterisk project. Please do not directly contact
14  * any of the maintainers of this project for assistance;
15  * the project provides a web site, mailing lists and IRC
16  * channels for your use.
17  *
18  * This program is free software, distributed under the terms of
19  * the GNU General Public License Version 2. See the LICENSE file
20  * at the top of the source tree.
21  */
22
23 /*! \file
24  *
25  * \brief PostgreSQL CDR logger 
26  * 
27  * \author Matthew D. Hardeman <mhardemn@papersoft.com> 
28  * \extref PostgreSQL http://www.postgresql.org/
29  *
30  * See also
31  * \arg \ref Config_cdr
32  * \arg http://www.postgresql.org/
33  * \ingroup cdr_drivers
34  */
35
36 /*** MODULEINFO
37         <depend>pgsql</depend>
38  ***/
39
40 #include "asterisk.h"
41
42 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
43
44 #include <time.h>
45
46 #include <libpq-fe.h>
47
48 #include "asterisk/config.h"
49 #include "asterisk/channel.h"
50 #include "asterisk/cdr.h"
51 #include "asterisk/module.h"
52
53 #define DATE_FORMAT "'%Y-%m-%d %T'"
54
55 static char *name = "pgsql";
56 static char *config = "cdr_pgsql.conf";
57 static char *pghostname = NULL, *pgdbname = NULL, *pgdbuser = NULL, *pgpassword = NULL, *pgdbport = NULL, *table = NULL;
58 static int connected = 0;
59 static int maxsize = 512, maxsize2 = 512;
60
61 AST_MUTEX_DEFINE_STATIC(pgsql_lock);
62
63 static PGconn   *conn = NULL;
64
65 struct columns {
66         char *name;
67         char *type;
68         int len;
69         unsigned int notnull:1;
70         unsigned int hasdefault:1;
71         AST_RWLIST_ENTRY(columns) list;
72 };
73
74 static AST_RWLIST_HEAD_STATIC(psql_columns, columns);
75
76 #define LENGTHEN_BUF1(size)                                                                                                             \
77                         do {                                                                                                                            \
78                                 /* Lengthen buffer, if necessary */                                                             \
79                                 if (sql->used + size + 1 > sql->len) {  \
80                                         if (ast_str_make_space(&sql, ((sql->len + size + 1) / 512 + 1) * 512) != 0) {   \
81                                                 ast_log(LOG_ERROR, "Unable to allocate sufficient memory.  Insert CDR failed.\n"); \
82                                                 ast_free(sql);                                                                                  \
83                                                 ast_free(sql2);                                                                                 \
84                                                 AST_RWLIST_UNLOCK(&psql_columns);                                               \
85                                                 return -1;                                                                                              \
86                                         }                                                                                                                       \
87                                 }                                                                                                                               \
88                         } while (0)
89
90 #define LENGTHEN_BUF2(size)                                                                                                             \
91                         do {                                                                                                                            \
92                                 if (sql2->used + size + 1 > sql2->len) {                                \
93                                         if (ast_str_make_space(&sql2, ((sql2->len + 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 -1;                                                                                              \
99                                         }                                                                                                                       \
100                                 }                                                                                                                               \
101                         } while (0)
102
103 static int pgsql_log(struct ast_cdr *cdr)
104 {
105         struct ast_tm tm;
106         char *pgerror;
107         PGresult *result;
108
109         ast_mutex_lock(&pgsql_lock);
110
111         if ((!connected) && pghostname && pgdbuser && pgpassword && pgdbname) {
112                 conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
113                 if (PQstatus(conn) != CONNECTION_BAD) {
114                         connected = 1;
115                 } else {
116                         pgerror = PQerrorMessage(conn);
117                         ast_log(LOG_ERROR, "Unable to connect to database server %s.  Calls will not be logged!\n", pghostname);
118                         ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
119                         PQfinish(conn);
120                         conn = NULL;
121                 }
122         }
123
124         if (connected) {
125                 struct columns *cur;
126                 struct ast_str *sql = ast_str_create(maxsize), *sql2 = ast_str_create(maxsize2);
127                 char buf[257], escapebuf[513], *value;
128   
129                 if (!sql || !sql2) {
130                         if (sql) {
131                                 ast_free(sql);
132                         }
133                         if (sql2) {
134                                 ast_free(sql2);
135                         }
136                         return -1;
137                 }
138
139                 ast_str_set(&sql, 0, "INSERT INTO %s (", table);
140                 ast_str_set(&sql2, 0, " VALUES (");
141   
142                 AST_RWLIST_RDLOCK(&psql_columns);
143                 AST_RWLIST_TRAVERSE(&psql_columns, cur, list) {
144                         /* For fields not set, simply skip them */
145                         ast_cdr_getvar(cdr, cur->name, &value, buf, sizeof(buf), 0, 0);
146                         if (strcmp(cur->name, "calldate") == 0 && !value) {
147                                 ast_cdr_getvar(cdr, "start", &value, buf, sizeof(buf), 0, 0);
148                         }
149                         if (!value) {
150                                 if (cur->notnull && !cur->hasdefault) {
151                                         /* Field is NOT NULL (but no default), must include it anyway */
152                                         LENGTHEN_BUF1(strlen(cur->name) + 2);
153                                         ast_str_append(&sql, 0, "\"%s\",", cur->name);
154                                         LENGTHEN_BUF2(3);
155                                         ast_str_append(&sql2, 0, "'',");
156                                 }
157                                 continue;
158                         }
159                         
160                         LENGTHEN_BUF1(strlen(cur->name) + 2);
161                         ast_str_append(&sql, 0, "\"%s\",", cur->name);
162
163                         if (strcmp(cur->name, "start") == 0 || strcmp(cur->name, "calldate") == 0) {
164                                 if (strncmp(cur->type, "int", 3) == 0) {
165                                         LENGTHEN_BUF2(12);
166                                         ast_str_append(&sql2, 0, "%ld", cdr->start.tv_sec);
167                                 } else if (strncmp(cur->type, "float", 5) == 0) {
168                                         LENGTHEN_BUF2(30);
169                                         ast_str_append(&sql2, 0, "%f", (double)cdr->start.tv_sec + (double)cdr->start.tv_usec / 1000000.0);
170                                 } else {
171                                         /* char, hopefully */
172                                         LENGTHEN_BUF2(30);
173                                         ast_localtime(&cdr->start, &tm, NULL);
174                                         ast_strftime(buf, sizeof(buf), DATE_FORMAT, &tm);
175                                         ast_str_append(&sql2, 0, "%s", buf);
176                                 }
177                         } else if (strcmp(cur->name, "answer") == 0) {
178                                 if (strncmp(cur->type, "int", 3) == 0) {
179                                         LENGTHEN_BUF2(12);
180                                         ast_str_append(&sql2, 0, "%ld", cdr->answer.tv_sec);
181                                 } else if (strncmp(cur->type, "float", 5) == 0) {
182                                         LENGTHEN_BUF2(30);
183                                         ast_str_append(&sql2, 0, "%f", (double)cdr->answer.tv_sec + (double)cdr->answer.tv_usec / 1000000.0);
184                                 } else {
185                                         /* char, hopefully */
186                                         LENGTHEN_BUF2(30);
187                                         ast_localtime(&cdr->start, &tm, NULL);
188                                         ast_strftime(buf, sizeof(buf), DATE_FORMAT, &tm);
189                                         ast_str_append(&sql2, 0, "%s", buf);
190                                 }
191                         } else if (strcmp(cur->name, "end") == 0) {
192                                 if (strncmp(cur->type, "int", 3) == 0) {
193                                         LENGTHEN_BUF2(12);
194                                         ast_str_append(&sql2, 0, "%ld", cdr->end.tv_sec);
195                                 } else if (strncmp(cur->type, "float", 5) == 0) {
196                                         LENGTHEN_BUF2(30);
197                                         ast_str_append(&sql2, 0, "%f", (double)cdr->end.tv_sec + (double)cdr->end.tv_usec / 1000000.0);
198                                 } else {
199                                         /* char, hopefully */
200                                         LENGTHEN_BUF2(30);
201                                         ast_localtime(&cdr->end, &tm, NULL);
202                                         ast_strftime(buf, sizeof(buf), DATE_FORMAT, &tm);
203                                         ast_str_append(&sql2, 0, "%s", buf);
204                                 }
205                         } else if (strcmp(cur->name, "duration") == 0 || strcmp(cur->name, "billsec") == 0) {
206                                 if (cur->type[0] == 'i') {
207                                         /* Get integer, no need to escape anything */
208                                         ast_cdr_getvar(cdr, cur->name, &value, buf, sizeof(buf), 0, 0);
209                                         LENGTHEN_BUF2(12);
210                                         ast_str_append(&sql2, 0, "%s", value);
211                                 } else if (strncmp(cur->type, "float", 5) == 0) {
212                                         struct timeval *when = cur->name[0] == 'd' ? &cdr->start : &cdr->answer;
213                                         LENGTHEN_BUF2(30);
214                                         ast_str_append(&sql2, 0, "%f", (double)cdr->end.tv_sec - when->tv_sec + cdr->end.tv_usec / 1000000.0 - when->tv_usec / 1000000.0);
215                                 } else {
216                                         /* Char field, probably */
217                                         struct timeval *when = cur->name[0] == 'd' ? &cdr->start : &cdr->answer;
218                                         LENGTHEN_BUF2(30);
219                                         ast_str_append(&sql2, 0, "'%f'", (double)cdr->end.tv_sec - when->tv_sec + cdr->end.tv_usec / 1000000.0 - when->tv_usec / 1000000.0);
220                                 }
221                         } else if (strcmp(cur->name, "disposition") == 0 || strcmp(cur->name, "amaflags") == 0) {
222                                 if (strncmp(cur->type, "int", 3) == 0) {
223                                         /* Integer, no need to escape anything */
224                                         ast_cdr_getvar(cdr, cur->name, &value, buf, sizeof(buf), 0, 1);
225                                         LENGTHEN_BUF2(12);
226                                         ast_str_append(&sql2, 0, "%s", value);
227                                 } else {
228                                         /* Although this is a char field, there are no special characters in the values for these fields */
229                                         ast_cdr_getvar(cdr, cur->name, &value, buf, sizeof(buf), 0, 0);
230                                         LENGTHEN_BUF2(30);
231                                         ast_str_append(&sql2, 0, "'%s'", value);
232                                 }
233                         } else {
234                                 /* Arbitrary field, could be anything */
235                                 ast_cdr_getvar(cdr, cur->name, &value, buf, sizeof(buf), 0, 0);
236                                 if (strncmp(cur->type, "int", 3) == 0) {
237                                         long long whatever;
238                                         if (value && sscanf(value, "%lld", &whatever) == 1) {
239                                                 LENGTHEN_BUF2(25);
240                                                 ast_str_append(&sql2, 0, "%lld", whatever);
241                                         } else {
242                                                 LENGTHEN_BUF2(1);
243                                                 ast_str_append(&sql2, 0, "0");
244                                         }
245                                 } else if (strncmp(cur->type, "float", 5) == 0) {
246                                         long double whatever;
247                                         if (value && sscanf(value, "%Lf", &whatever) == 1) {
248                                                 LENGTHEN_BUF2(50);
249                                                 ast_str_append(&sql2, 0, "%30Lf", whatever);
250                                         } else {
251                                                 LENGTHEN_BUF2(1);
252                                                 ast_str_append(&sql2, 0, "0");
253                                         }
254                                 /* XXX Might want to handle dates, times, and other misc fields here XXX */
255                                 } else {
256                                         if (value)
257                                                 PQescapeStringConn(conn, escapebuf, value, strlen(value), NULL);
258                                         else
259                                                 escapebuf[0] = '\0';
260                                         LENGTHEN_BUF2(strlen(escapebuf) + 2);
261                                         ast_str_append(&sql2, 0, "'%s'", escapebuf);
262                                 }
263                         }
264                         LENGTHEN_BUF2(1);
265                         ast_str_append(&sql2, 0, ",");
266                 }
267                 AST_RWLIST_UNLOCK(&psql_columns);
268                 LENGTHEN_BUF1(sql2->len);
269                 sql->str[sql->len - 1] = ')';
270                 sql2->str[sql2->len - 1] = ')';
271                 ast_str_append(&sql, 0, "%s", sql2->str);
272                 ast_verb(11, "[%s]\n", sql->str);
273
274                 ast_debug(2, "inserting a CDR record.\n");
275
276                 /* Test to be sure we're still connected... */
277                 /* If we're connected, and connection is working, good. */
278                 /* Otherwise, attempt reconnect.  If it fails... sorry... */
279                 if (PQstatus(conn) == CONNECTION_OK) {
280                         connected = 1;
281                 } else {
282                         ast_log(LOG_ERROR, "Connection was lost... attempting to reconnect.\n");
283                         PQreset(conn);
284                         if (PQstatus(conn) == CONNECTION_OK) {
285                                 ast_log(LOG_ERROR, "Connection reestablished.\n");
286                                 connected = 1;
287                         } else {
288                                 pgerror = PQerrorMessage(conn);
289                                 ast_log(LOG_ERROR, "Unable to reconnect to database server %s. Calls will not be logged!\n", pghostname);
290                                 ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
291                                 PQfinish(conn);
292                                 conn = NULL;
293                                 connected = 0;
294                                 ast_mutex_unlock(&pgsql_lock);
295                                 ast_free(sql);
296                                 ast_free(sql2);
297                                 return -1;
298                         }
299                 }
300                 result = PQexec(conn, sql->str);
301                 if (PQresultStatus(result) != PGRES_COMMAND_OK) {
302                         pgerror = PQresultErrorMessage(result);
303                         ast_log(LOG_ERROR, "Failed to insert call detail record into database!\n");
304                         ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
305                         ast_log(LOG_ERROR, "Connection may have been lost... attempting to reconnect.\n");
306                         PQreset(conn);
307                         if (PQstatus(conn) == CONNECTION_OK) {
308                                 ast_log(LOG_ERROR, "Connection reestablished.\n");
309                                 connected = 1;
310                                 PQclear(result);
311                                 result = PQexec(conn, sql->str);
312                                 if (PQresultStatus(result) != PGRES_COMMAND_OK) {
313                                         pgerror = PQresultErrorMessage(result);
314                                         ast_log(LOG_ERROR, "HARD ERROR!  Attempted reconnection failed.  DROPPING CALL RECORD!\n");
315                                         ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
316                                 }
317                         }
318                         ast_mutex_unlock(&pgsql_lock);
319                         PQclear(result);
320                         ast_free(sql);
321                         ast_free(sql2);
322                         return -1;
323                 }
324                 PQclear(result);
325                 ast_free(sql);
326                 ast_free(sql2);
327         }
328         ast_mutex_unlock(&pgsql_lock);
329         return 0;
330 }
331
332 static int unload_module(void)
333 {
334         struct columns *current;
335         ast_cdr_unregister(name);
336
337         /* Give all threads time to finish */
338         usleep(1);
339         PQfinish(conn);
340
341         if (pghostname)
342                 ast_free(pghostname);
343         if (pgdbname)
344                 ast_free(pgdbname);
345         if (pgdbuser)
346                 ast_free(pgdbuser);
347         if (pgpassword)
348                 ast_free(pgpassword);
349         if (pgdbport)
350                 ast_free(pgdbport);
351         if (table)
352                 ast_free(table);
353
354         AST_RWLIST_WRLOCK(&psql_columns);
355         while ((current = AST_RWLIST_REMOVE_HEAD(&psql_columns, list))) {
356                 ast_free(current);
357         }
358         AST_RWLIST_UNLOCK(&psql_columns);
359
360         return 0;
361 }
362
363 static int config_module(int reload)
364 {
365         struct ast_variable *var;
366         char *pgerror;
367         struct columns *cur;
368         PGresult *result;
369         const char *tmp;
370         struct ast_config *cfg;
371         struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
372
373         if ((cfg = ast_config_load(config, config_flags)) == NULL) {
374                 ast_log(LOG_WARNING, "Unable to load config for PostgreSQL CDR's: %s\n", config);
375                 return -1;
376         } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
377                 return 0;
378
379         if (!(var = ast_variable_browse(cfg, "global"))) {
380                 ast_config_destroy(cfg);
381                 return 0;
382         }
383
384         if (!(tmp = ast_variable_retrieve(cfg, "global", "hostname"))) {
385                 ast_log(LOG_WARNING, "PostgreSQL server hostname not specified.  Assuming unix socket connection\n");
386                 tmp = "";       /* connect via UNIX-socket by default */
387         }
388
389         if (pghostname)
390                 ast_free(pghostname);
391         if (!(pghostname = ast_strdup(tmp))) {
392                 ast_config_destroy(cfg);
393                 return -1;
394         }
395
396         if (!(tmp = ast_variable_retrieve(cfg, "global", "dbname"))) {
397                 ast_log(LOG_WARNING, "PostgreSQL database not specified.  Assuming asterisk\n");
398                 tmp = "asteriskcdrdb";
399         }
400
401         if (pgdbname)
402                 ast_free(pgdbname);
403         if (!(pgdbname = ast_strdup(tmp))) {
404                 ast_config_destroy(cfg);
405                 return -1;
406         }
407
408         if (!(tmp = ast_variable_retrieve(cfg, "global", "user"))) {
409                 ast_log(LOG_WARNING, "PostgreSQL database user not specified.  Assuming asterisk\n");
410                 tmp = "asterisk";
411         }
412
413         if (pgdbuser)
414                 ast_free(pgdbuser);
415         if (!(pgdbuser = ast_strdup(tmp))) {
416                 ast_config_destroy(cfg);
417                 return -1;
418         }
419
420         if (!(tmp = ast_variable_retrieve(cfg, "global", "password"))) {
421                 ast_log(LOG_WARNING, "PostgreSQL database password not specified.  Assuming blank\n");
422                 tmp = "";
423         }
424
425         if (pgpassword)
426                 ast_free(pgpassword);
427         if (!(pgpassword = ast_strdup(tmp))) {
428                 ast_config_destroy(cfg);
429                 return -1;
430         }
431
432         if (!(tmp = ast_variable_retrieve(cfg, "global", "port"))) {
433                 ast_log(LOG_WARNING, "PostgreSQL database port not specified.  Using default 5432.\n");
434                 tmp = "5432";
435         }
436
437         if (pgdbport)
438                 ast_free(pgdbport);
439         if (!(pgdbport = ast_strdup(tmp))) {
440                 ast_config_destroy(cfg);
441                 return -1;
442         }
443
444         if (!(tmp = ast_variable_retrieve(cfg, "global", "table"))) {
445                 ast_log(LOG_WARNING, "CDR table not specified.  Assuming cdr\n");
446                 tmp = "cdr";
447         }
448
449         if (table)
450                 ast_free(table);
451         if (!(table = ast_strdup(tmp))) {
452                 ast_config_destroy(cfg);
453                 return -1;
454         }
455
456         if (option_debug) {
457                 if (ast_strlen_zero(pghostname)) {
458                         ast_debug(1, "using default unix socket\n");
459                 } else {
460                         ast_debug(1, "got hostname of %s\n", pghostname);
461                 }
462                 ast_debug(1, "got port of %s\n", pgdbport);
463                 ast_debug(1, "got user of %s\n", pgdbuser);
464                 ast_debug(1, "got dbname of %s\n", pgdbname);
465                 ast_debug(1, "got password of %s\n", pgpassword);
466                 ast_debug(1, "got sql table name of %s\n", table);
467         }
468
469         conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
470         if (PQstatus(conn) != CONNECTION_BAD) {
471                 char sqlcmd[512];
472                 char *fname, *ftype, *flen, *fnotnull, *fdef;
473                 int i, rows;
474                 ast_debug(1, "Successfully connected to PostgreSQL database.\n");
475                 connected = 1;
476
477                 /* Query the columns */
478                 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", table);
479                 result = PQexec(conn, sqlcmd);
480                 if (PQresultStatus(result) != PGRES_TUPLES_OK) {
481                         pgerror = PQresultErrorMessage(result);
482                         ast_log(LOG_ERROR, "Failed to query database columns: %s\n", pgerror);
483                         PQclear(result);
484                         unload_module();
485                         return AST_MODULE_LOAD_DECLINE;
486                 }
487
488                 rows = PQntuples(result);
489                 for (i = 0; i < rows; i++) {
490                         fname = PQgetvalue(result, i, 0);
491                         ftype = PQgetvalue(result, i, 1);
492                         flen = PQgetvalue(result, i, 2);
493                         fnotnull = PQgetvalue(result, i, 3);
494                         fdef = PQgetvalue(result, i, 4);
495                         ast_verb(4, "Found column '%s' of type '%s'\n", fname, ftype);
496                         cur = ast_calloc(1, sizeof(*cur) + strlen(fname) + strlen(ftype) + 2);
497                         if (cur) {
498                                 sscanf(flen, "%d", &cur->len);
499                                 cur->name = (char *)cur + sizeof(*cur);
500                                 cur->type = (char *)cur + sizeof(*cur) + strlen(fname) + 1;
501                                 strcpy(cur->name, fname);
502                                 strcpy(cur->type, ftype);
503                                 if (*fnotnull == 't') {
504                                         cur->notnull = 1;
505                                 } else {
506                                         cur->notnull = 0;
507                                 }
508                                 if (!ast_strlen_zero(fdef)) {
509                                         cur->hasdefault = 1;
510                                 } else {
511                                         cur->hasdefault = 0;
512                                 }
513                                 AST_RWLIST_INSERT_TAIL(&psql_columns, cur, list);
514                         }
515                 }
516                 PQclear(result);
517         } else {
518                 pgerror = PQerrorMessage(conn);
519                 ast_log(LOG_ERROR, "Unable to connect to database server %s.  CALLS WILL NOT BE LOGGED!!\n", pghostname);
520                 ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
521                 connected = 0;
522         }
523
524         ast_config_destroy(cfg);
525
526         return ast_cdr_register(name, ast_module_info->description, pgsql_log);
527 }
528
529 static int load_module(void)
530 {
531         return config_module(0) ? AST_MODULE_LOAD_DECLINE : 0;
532 }
533
534 static int reload(void)
535 {
536         return config_module(1);
537 }
538
539 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "PostgreSQL CDR Backend",
540                 .load = load_module,
541                 .unload = unload_module,
542                 .reload = reload,
543                );