comment that for callerid field, NULL and "" should be considered
[asterisk/asterisk.git] / cdr.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Call Detail Record API 
22  *
23  * \author Mark Spencer <markster@digium.com>
24  * 
25  * \note Includes code and algorithms from the Zapata library.
26  *
27  * \note We do a lot of checking here in the CDR code to try to be sure we don't ever let a CDR slip
28  * through our fingers somehow.  If someone allocates a CDR, it must be completely handled normally
29  * or a WARNING shall be logged, so that we can best keep track of any escape condition where the CDR
30  * isn't properly generated and posted.
31  */
32
33
34 #include <unistd.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <stdio.h>
38 #include <signal.h>
39
40 #include "asterisk.h"
41
42 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
43
44 #include "asterisk/lock.h"
45 #include "asterisk/channel.h"
46 #include "asterisk/cdr.h"
47 #include "asterisk/logger.h"
48 #include "asterisk/callerid.h"
49 #include "asterisk/causes.h"
50 #include "asterisk/options.h"
51 #include "asterisk/linkedlists.h"
52 #include "asterisk/utils.h"
53 #include "asterisk/sched.h"
54 #include "asterisk/config.h"
55 #include "asterisk/cli.h"
56 // XXX #include "asterisk/module.h"
57 #include "asterisk/stringfields.h"
58
59 /*! Default AMA flag for billing records (CDR's) */
60 int ast_default_amaflags = AST_CDR_DOCUMENTATION;
61 char ast_default_accountcode[AST_MAX_ACCOUNT_CODE] = "";
62
63 struct ast_cdr_beitem {
64         char name[20];
65         char desc[80];
66         ast_cdrbe be;
67         AST_LIST_ENTRY(ast_cdr_beitem) list;
68 };
69
70 static AST_LIST_HEAD_STATIC(be_list, ast_cdr_beitem);
71
72 struct ast_cdr_batch_item {
73         struct ast_cdr *cdr;
74         struct ast_cdr_batch_item *next;
75 };
76
77 static struct ast_cdr_batch {
78         int size;
79         struct ast_cdr_batch_item *head;
80         struct ast_cdr_batch_item *tail;
81 } *batch = NULL;
82
83 static struct sched_context *sched;
84 static int cdr_sched = -1;
85 static pthread_t cdr_thread = AST_PTHREADT_NULL;
86
87 #define BATCH_SIZE_DEFAULT 100
88 #define BATCH_TIME_DEFAULT 300
89 #define BATCH_SCHEDULER_ONLY_DEFAULT 0
90 #define BATCH_SAFE_SHUTDOWN_DEFAULT 1
91
92 static int enabled;
93 static int batchmode;
94 static int batchsize;
95 static int batchtime;
96 static int batchscheduleronly;
97 static int batchsafeshutdown;
98
99 AST_MUTEX_DEFINE_STATIC(cdr_batch_lock);
100
101 /* these are used to wake up the CDR thread when there's work to do */
102 AST_MUTEX_DEFINE_STATIC(cdr_pending_lock);
103 static ast_cond_t cdr_pending_cond;
104
105
106 /*! Register a CDR driver. Each registered CDR driver generates a CDR 
107         \return 0 on success, -1 on failure 
108 */
109 int ast_cdr_register(char *name, char *desc, ast_cdrbe be)
110 {
111         struct ast_cdr_beitem *i;
112
113         if (!name)
114                 return -1;
115         if (!be) {
116                 ast_log(LOG_WARNING, "CDR engine '%s' lacks backend\n", name);
117                 return -1;
118         }
119
120         AST_LIST_LOCK(&be_list);
121         AST_LIST_TRAVERSE(&be_list, i, list) {
122                 if (!strcasecmp(name, i->name))
123                         break;
124         }
125         AST_LIST_UNLOCK(&be_list);
126
127         if (i) {
128                 ast_log(LOG_WARNING, "Already have a CDR backend called '%s'\n", name);
129                 return -1;
130         }
131
132         if (!(i = ast_calloc(1, sizeof(*i))))   
133                 return -1;
134
135         i->be = be;
136         ast_copy_string(i->name, name, sizeof(i->name));
137         ast_copy_string(i->desc, desc, sizeof(i->desc));
138
139         AST_LIST_LOCK(&be_list);
140         AST_LIST_INSERT_HEAD(&be_list, i, list);
141         AST_LIST_UNLOCK(&be_list);
142
143         return 0;
144 }
145
146 /*! unregister a CDR driver */
147 void ast_cdr_unregister(char *name)
148 {
149         struct ast_cdr_beitem *i = NULL;
150
151         AST_LIST_LOCK(&be_list);
152         AST_LIST_TRAVERSE_SAFE_BEGIN(&be_list, i, list) {
153                 if (!strcasecmp(name, i->name)) {
154                         AST_LIST_REMOVE_CURRENT(&be_list, list);
155                         if (option_verbose > 1)
156                                 ast_verbose(VERBOSE_PREFIX_2 "Unregistered '%s' CDR backend\n", name);
157                         free(i);
158                         break;
159                 }
160         }
161         AST_LIST_TRAVERSE_SAFE_END;
162         AST_LIST_UNLOCK(&be_list);
163 }
164
165 /*! Duplicate a CDR record 
166         \returns Pointer to new CDR record
167 */
168 struct ast_cdr *ast_cdr_dup(struct ast_cdr *cdr) 
169 {
170         struct ast_cdr *newcdr = ast_cdr_alloc();
171
172         if (!newcdr)
173                 return NULL;
174
175         memcpy(newcdr, cdr, sizeof(*newcdr));
176         /* The varshead is unusable, volatile even, after the memcpy so we take care of that here */
177         memset(&newcdr->varshead, 0, sizeof(newcdr->varshead));
178         ast_cdr_copy_vars(newcdr, cdr);
179         newcdr->next = NULL;
180
181         return newcdr;
182 }
183
184 static const char *ast_cdr_getvar_internal(struct ast_cdr *cdr, const char *name, int recur) 
185 {
186         if (ast_strlen_zero(name))
187                 return NULL;
188
189         for (; cdr; cdr = recur ? cdr->next : NULL) {
190                 struct ast_var_t *variables;
191                 struct varshead *headp = &cdr->varshead;
192                 AST_LIST_TRAVERSE(headp, variables, entries) {
193                         if (!strcasecmp(name, ast_var_name(variables)))
194                                 return ast_var_value(variables);
195                 }
196         }
197
198         return NULL;
199 }
200
201 static void cdr_get_tv(struct timeval tv, const char *fmt, char *buf, int bufsize)
202 {
203         if (fmt == NULL) {      /* raw mode */
204                 snprintf(buf, bufsize, "%ld.%06ld", (long)tv.tv_sec, (long)tv.tv_usec);
205         } else {  
206                 time_t t = tv.tv_sec;
207                 if (t) {
208                         struct tm tm;
209                         localtime_r(&t, &tm);
210                         strftime(buf, bufsize, fmt, &tm);
211                 }
212         }
213 }
214
215 /*! CDR channel variable retrieval */
216 void ast_cdr_getvar(struct ast_cdr *cdr, const char *name, char **ret, char *workspace, int workspacelen, int recur, int raw) 
217 {
218         const char *fmt = "%Y-%m-%d %T";
219         const char *varbuf;
220
221         *ret = NULL;
222         /* special vars (the ones from the struct ast_cdr when requested by name) 
223            I'd almost say we should convert all the stringed vals to vars */
224
225         if (!strcasecmp(name, "clid"))
226                 ast_copy_string(workspace, cdr->clid, workspacelen);
227         else if (!strcasecmp(name, "src"))
228                 ast_copy_string(workspace, cdr->src, workspacelen);
229         else if (!strcasecmp(name, "dst"))
230                 ast_copy_string(workspace, cdr->dst, workspacelen);
231         else if (!strcasecmp(name, "dcontext"))
232                 ast_copy_string(workspace, cdr->dcontext, workspacelen);
233         else if (!strcasecmp(name, "channel"))
234                 ast_copy_string(workspace, cdr->channel, workspacelen);
235         else if (!strcasecmp(name, "dstchannel"))
236                 ast_copy_string(workspace, cdr->dstchannel, workspacelen);
237         else if (!strcasecmp(name, "lastapp"))
238                 ast_copy_string(workspace, cdr->lastapp, workspacelen);
239         else if (!strcasecmp(name, "lastdata"))
240                 ast_copy_string(workspace, cdr->lastdata, workspacelen);
241         else if (!strcasecmp(name, "start"))
242                 cdr_get_tv(cdr->start, raw ? NULL : fmt, workspace, workspacelen);
243         else if (!strcasecmp(name, "answer"))
244                 cdr_get_tv(cdr->answer, raw ? NULL : fmt, workspace, workspacelen);
245         else if (!strcasecmp(name, "end"))
246                 cdr_get_tv(cdr->end, raw ? NULL : fmt, workspace, workspacelen);
247         else if (!strcasecmp(name, "duration"))
248                 snprintf(workspace, workspacelen, "%ld", cdr->duration);
249         else if (!strcasecmp(name, "billsec"))
250                 snprintf(workspace, workspacelen, "%ld", cdr->billsec);
251         else if (!strcasecmp(name, "disposition")) {
252                 if (raw) {
253                         snprintf(workspace, workspacelen, "%ld", cdr->disposition);
254                 } else {
255                         ast_copy_string(workspace, ast_cdr_disp2str(cdr->disposition), workspacelen);
256                 }
257         } else if (!strcasecmp(name, "amaflags")) {
258                 if (raw) {
259                         snprintf(workspace, workspacelen, "%ld", cdr->amaflags);
260                 } else {
261                         ast_copy_string(workspace, ast_cdr_flags2str(cdr->amaflags), workspacelen);
262                 }
263         } else if (!strcasecmp(name, "accountcode"))
264                 ast_copy_string(workspace, cdr->accountcode, workspacelen);
265         else if (!strcasecmp(name, "uniqueid"))
266                 ast_copy_string(workspace, cdr->uniqueid, workspacelen);
267         else if (!strcasecmp(name, "userfield"))
268                 ast_copy_string(workspace, cdr->userfield, workspacelen);
269         else if ((varbuf = ast_cdr_getvar_internal(cdr, name, recur)))
270                 ast_copy_string(workspace, varbuf, workspacelen);
271
272         if (!ast_strlen_zero(workspace))
273                 *ret = workspace;
274 }
275
276 /* readonly cdr variables */
277 static  const char *cdr_readonly_vars[] = { "clid", "src", "dst", "dcontext", "channel", "dstchannel",
278                                     "lastapp", "lastdata", "start", "answer", "end", "duration",
279                                     "billsec", "disposition", "amaflags", "accountcode", "uniqueid",
280                                     "userfield", NULL };
281 /*! Set a CDR channel variable 
282         \note You can't set the CDR variables that belong to the actual CDR record, like "billsec".
283 */
284 int ast_cdr_setvar(struct ast_cdr *cdr, const char *name, const char *value, int recur) 
285 {
286         struct ast_var_t *newvariable;
287         struct varshead *headp;
288         int x;
289         
290         for(x = 0; cdr_readonly_vars[x]; x++) {
291                 if (!strcasecmp(name, cdr_readonly_vars[x])) {
292                         ast_log(LOG_ERROR, "Attempt to set a read-only variable!.\n");
293                         return -1;
294                 }
295         }
296
297         if (!cdr) {
298                 ast_log(LOG_ERROR, "Attempt to set a variable on a nonexistent CDR record.\n");
299                 return -1;
300         }
301
302         for (; cdr; cdr = recur ? cdr->next : NULL) {
303                 headp = &cdr->varshead;
304                 AST_LIST_TRAVERSE_SAFE_BEGIN(headp, newvariable, entries) {
305                         if (!strcasecmp(ast_var_name(newvariable), name)) {
306                                 /* there is already such a variable, delete it */
307                                 AST_LIST_REMOVE_CURRENT(headp, entries);
308                                 ast_var_delete(newvariable);
309                                 break;
310                         }
311                 }
312                 AST_LIST_TRAVERSE_SAFE_END;
313
314                 if (value) {
315                         newvariable = ast_var_assign(name, value);
316                         AST_LIST_INSERT_HEAD(headp, newvariable, entries);
317                 }
318         }
319
320         return 0;
321 }
322
323 int ast_cdr_copy_vars(struct ast_cdr *to_cdr, struct ast_cdr *from_cdr)
324 {
325         struct ast_var_t *variables, *newvariable = NULL;
326         struct varshead *headpa, *headpb;
327         const char *var, *val;
328         int x = 0;
329
330         headpa = &from_cdr->varshead;
331         headpb = &to_cdr->varshead;
332
333         AST_LIST_TRAVERSE(headpa,variables,entries) {
334                 if (variables &&
335                     (var = ast_var_name(variables)) && (val = ast_var_value(variables)) &&
336                     !ast_strlen_zero(var) && !ast_strlen_zero(val)) {
337                         newvariable = ast_var_assign(var, val);
338                         AST_LIST_INSERT_HEAD(headpb, newvariable, entries);
339                         x++;
340                 }
341         }
342
343         return x;
344 }
345
346 int ast_cdr_serialize_variables(struct ast_cdr *cdr, char *buf, size_t size, char delim, char sep, int recur) 
347 {
348         struct ast_var_t *variables;
349         const char *var, *val;
350         char *tmp;
351         char workspace[256];
352         int total = 0, x = 0, i;
353
354         memset(buf, 0, size);
355
356         for (; cdr; cdr = recur ? cdr->next : NULL) {
357                 if (++x > 1)
358                         ast_build_string(&buf, &size, "\n");
359
360                 AST_LIST_TRAVERSE(&cdr->varshead, variables, entries) {
361                         if (variables &&
362                             (var = ast_var_name(variables)) && (val = ast_var_value(variables)) &&
363                             !ast_strlen_zero(var) && !ast_strlen_zero(val)) {
364                                 if (ast_build_string(&buf, &size, "level %d: %s%c%s%c", x, var, delim, val, sep)) {
365                                         ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
366                                         break;
367                                 } else
368                                         total++;
369                         } else 
370                                 break;
371                 }
372
373                 for (i = 0; cdr_readonly_vars[i]; i++) {
374                         ast_cdr_getvar(cdr, cdr_readonly_vars[i], &tmp, workspace, sizeof(workspace), 0, 0);
375                         if (!tmp)
376                                 continue;
377                         
378                         if (ast_build_string(&buf, &size, "level %d: %s%c%s%c", x, cdr_readonly_vars[i], delim, tmp, sep)) {
379                                 ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
380                                 break;
381                         } else
382                                 total++;
383                 }
384         }
385
386         return total;
387 }
388
389
390 void ast_cdr_free_vars(struct ast_cdr *cdr, int recur)
391 {
392
393         /* clear variables */
394         for (; cdr; cdr = recur ? cdr->next : NULL) {
395                 struct ast_var_t *vardata;
396                 struct varshead *headp = &cdr->varshead;
397                 while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
398                         ast_var_delete(vardata);
399         }
400 }
401
402 /*! \brief  print a warning if cdr already posted */
403 static void check_post(struct ast_cdr *cdr)
404 {
405         if (ast_test_flag(cdr, AST_CDR_FLAG_POSTED))
406                 ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", S_OR(cdr->channel, "<unknown>"));
407 }
408
409 /*! \brief  print a warning if cdr already started */
410 static void check_start(struct ast_cdr *cdr)
411 {
412         if (!ast_tvzero(cdr->start))
413                 ast_log(LOG_WARNING, "CDR on channel '%s' already started\n", S_OR(cdr->channel, "<unknown>"));
414 }
415
416 void ast_cdr_free(struct ast_cdr *cdr)
417 {
418
419         while (cdr) {
420                 struct ast_cdr *next = cdr->next;
421                 char *chan = S_OR(cdr->channel, "<unknown>");
422                 if (!ast_test_flag(cdr, AST_CDR_FLAG_POSTED) && !ast_test_flag(cdr, AST_CDR_FLAG_POST_DISABLED))
423                         ast_log(LOG_WARNING, "CDR on channel '%s' not posted\n", chan);
424                 if (ast_tvzero(cdr->end))
425                         ast_log(LOG_WARNING, "CDR on channel '%s' lacks end\n", chan);
426                 if (ast_tvzero(cdr->start))
427                         ast_log(LOG_WARNING, "CDR on channel '%s' lacks start\n", chan);
428
429                 ast_cdr_free_vars(cdr, 0);
430                 free(cdr);
431                 cdr = next;
432         }
433 }
434
435 struct ast_cdr *ast_cdr_alloc(void)
436 {
437         return ast_calloc(1, sizeof(struct ast_cdr));
438 }
439
440 void ast_cdr_start(struct ast_cdr *cdr)
441 {
442         char *chan; 
443
444         for (; cdr; cdr = cdr->next) {
445                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
446                         chan = S_OR(cdr->channel, "<unknown>");
447                         check_post(cdr);
448                         check_start(cdr);
449                         cdr->start = ast_tvnow();
450                 }
451         }
452 }
453
454 void ast_cdr_answer(struct ast_cdr *cdr)
455 {
456
457         for (; cdr; cdr = cdr->next) {
458                 check_post(cdr);
459                 if (cdr->disposition < AST_CDR_ANSWERED)
460                         cdr->disposition = AST_CDR_ANSWERED;
461                 if (ast_tvzero(cdr->answer))
462                         cdr->answer = ast_tvnow();
463         }
464 }
465
466 void ast_cdr_busy(struct ast_cdr *cdr)
467 {
468
469         for (; cdr; cdr = cdr->next) {
470                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
471                         check_post(cdr);
472                         if (cdr->disposition < AST_CDR_BUSY)
473                                 cdr->disposition = AST_CDR_BUSY;
474                 }
475         }
476 }
477
478 void ast_cdr_failed(struct ast_cdr *cdr)
479 {
480         for (; cdr; cdr = cdr->next) {
481                 check_post(cdr);
482                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
483                         if (cdr->disposition < AST_CDR_FAILED)
484                                 cdr->disposition = AST_CDR_FAILED;
485                 }
486         }
487 }
488
489 int ast_cdr_disposition(struct ast_cdr *cdr, int cause)
490 {
491         int res = 0;
492
493         for (; cdr; cdr = cdr->next) {
494                 switch(cause) {
495                 case AST_CAUSE_BUSY:
496                         ast_cdr_busy(cdr);
497                         break;
498                 case AST_CAUSE_FAILURE:
499                         ast_cdr_failed(cdr);
500                         break;
501                 case AST_CAUSE_NORMAL:
502                         break;
503                 case AST_CAUSE_NOTDEFINED:
504                         res = -1;
505                         break;
506                 default:
507                         res = -1;
508                         ast_log(LOG_WARNING, "Cause not handled\n");
509                 }
510         }
511         return res;
512 }
513
514 void ast_cdr_setdestchan(struct ast_cdr *cdr, const char *chann)
515 {
516         for (; cdr; cdr = cdr->next) {
517                 check_post(cdr);
518                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
519                         ast_copy_string(cdr->dstchannel, chann, sizeof(cdr->dstchannel));
520         }
521 }
522
523 void ast_cdr_setapp(struct ast_cdr *cdr, char *app, char *data)
524 {
525
526         for (; cdr; cdr = cdr->next) {
527                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
528                         check_post(cdr);
529                         if (!app)
530                                 app = "";
531                         ast_copy_string(cdr->lastapp, app, sizeof(cdr->lastapp));
532                         if (!data)
533                                 data = "";
534                         ast_copy_string(cdr->lastdata, data, sizeof(cdr->lastdata));
535                 }
536         }
537 }
538
539 int ast_cdr_setcid(struct ast_cdr *cdr, struct ast_channel *c)
540 {
541         char tmp[AST_MAX_EXTENSION] = "";
542
543         for (; cdr; cdr = cdr->next) {
544                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
545                         /* Grab source from ANI or normal Caller*ID */
546                         char *num = c->cid.cid_ani ? c->cid.cid_ani : c->cid.cid_num; /* XXX ast_strlen_zero ? */
547                         
548                         if (c->cid.cid_name && num)
549                                 snprintf(tmp, sizeof(tmp), "\"%s\" <%s>", c->cid.cid_name, num);
550                         else if (c->cid.cid_name)
551                                 ast_copy_string(tmp, c->cid.cid_name, sizeof(tmp));
552                         else if (num)
553                                 ast_copy_string(tmp, num, sizeof(tmp));
554                         ast_copy_string(cdr->clid, tmp, sizeof(cdr->clid));
555                         ast_copy_string(cdr->src, num ? num : "", sizeof(cdr->src));
556                 }
557         }
558
559         return 0;
560 }
561
562
563 int ast_cdr_init(struct ast_cdr *cdr, struct ast_channel *c)
564 {
565         char *chan;
566         char *num;
567         char tmp[AST_MAX_EXTENSION] = "";
568
569         for ( ; cdr ; cdr = cdr->next) {
570                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
571                         chan = S_OR(cdr->channel, "<unknown>");
572                         if (!ast_strlen_zero(cdr->channel)) 
573                                 ast_log(LOG_WARNING, "CDR already initialized on '%s'\n", chan); 
574                         ast_copy_string(cdr->channel, c->name, sizeof(cdr->channel));
575                         /* Grab source from ANI or normal Caller*ID */
576                         num = c->cid.cid_ani ? c->cid.cid_ani : c->cid.cid_num;
577                         
578                         if (c->cid.cid_name && num)
579                                 snprintf(tmp, sizeof(tmp), "\"%s\" <%s>", c->cid.cid_name, num);
580                         else if (c->cid.cid_name)
581                                 ast_copy_string(tmp, c->cid.cid_name, sizeof(tmp));
582                         else if (num)
583                                 ast_copy_string(tmp, num, sizeof(tmp));
584                         ast_copy_string(cdr->clid, tmp, sizeof(cdr->clid));
585                         ast_copy_string(cdr->src, num ? num : "", sizeof(cdr->src));
586
587                         cdr->disposition = (c->_state == AST_STATE_UP) ?  AST_CDR_ANSWERED : AST_CDR_NOANSWER;
588                         cdr->amaflags = c->amaflags ? c->amaflags :  ast_default_amaflags;
589                         ast_copy_string(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode));
590                         /* Destination information */
591                         ast_copy_string(cdr->dst, c->exten, sizeof(cdr->dst));
592                         ast_copy_string(cdr->dcontext, c->context, sizeof(cdr->dcontext));
593                         /* Unique call identifier */
594                         ast_copy_string(cdr->uniqueid, c->uniqueid, sizeof(cdr->uniqueid));
595                 }
596         }
597         return 0;
598 }
599
600 void ast_cdr_end(struct ast_cdr *cdr)
601 {
602         for ( ; cdr ; cdr = cdr->next) {
603                 check_post(cdr);
604                 if (ast_tvzero(cdr->end))
605                         cdr->end = ast_tvnow();
606                 if (ast_tvzero(cdr->start)) {
607                         ast_log(LOG_WARNING, "CDR on channel '%s' has not started\n", S_OR(cdr->channel, "<unknown>"));
608                         cdr->disposition = AST_CDR_FAILED;
609                 } else
610                         cdr->duration = cdr->end.tv_sec - cdr->start.tv_sec;
611                 if (!ast_tvzero(cdr->answer))
612                         cdr->billsec = cdr->end.tv_sec - cdr->answer.tv_sec;
613                 else
614                         cdr->billsec = 0;
615         }
616 }
617
618 char *ast_cdr_disp2str(int disposition)
619 {
620         switch (disposition) {
621         case AST_CDR_NOANSWER:
622                 return "NO ANSWER";
623         case AST_CDR_FAILED:
624                 return "FAILED";                
625         case AST_CDR_BUSY:
626                 return "BUSY";          
627         case AST_CDR_ANSWERED:
628                 return "ANSWERED";
629         }
630         return "UNKNOWN";
631 }
632
633 /*! Converts AMA flag to printable string */
634 char *ast_cdr_flags2str(int flag)
635 {
636         switch(flag) {
637         case AST_CDR_OMIT:
638                 return "OMIT";
639         case AST_CDR_BILLING:
640                 return "BILLING";
641         case AST_CDR_DOCUMENTATION:
642                 return "DOCUMENTATION";
643         }
644         return "Unknown";
645 }
646
647 int ast_cdr_setaccount(struct ast_channel *chan, const char *account)
648 {
649         struct ast_cdr *cdr = chan->cdr;
650
651         ast_string_field_set(chan, accountcode, account);
652         for ( ; cdr ; cdr = cdr->next) {
653                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
654                         ast_copy_string(cdr->accountcode, chan->accountcode, sizeof(cdr->accountcode));
655         }
656         return 0;
657 }
658
659 int ast_cdr_setamaflags(struct ast_channel *chan, const char *flag)
660 {
661         struct ast_cdr *cdr;
662         int newflag = ast_cdr_amaflags2int(flag);
663         if (newflag) {
664                 for (cdr = chan->cdr; cdr; cdr = cdr->next)
665                         cdr->amaflags = newflag;
666         }
667
668         return 0;
669 }
670
671 int ast_cdr_setuserfield(struct ast_channel *chan, const char *userfield)
672 {
673         struct ast_cdr *cdr = chan->cdr;
674
675         for ( ; cdr ; cdr = cdr->next) {
676                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) 
677                         ast_copy_string(cdr->userfield, userfield, sizeof(cdr->userfield));
678         }
679
680         return 0;
681 }
682
683 int ast_cdr_appenduserfield(struct ast_channel *chan, const char *userfield)
684 {
685         struct ast_cdr *cdr = chan->cdr;
686
687         for ( ; cdr ; cdr = cdr->next) {
688                 int len = strlen(cdr->userfield);
689
690                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
691                         strncpy(cdr->userfield+len, userfield, sizeof(cdr->userfield) - len - 1);
692         }
693
694         return 0;
695 }
696
697 int ast_cdr_update(struct ast_channel *c)
698 {
699         struct ast_cdr *cdr = c->cdr;
700         char *num;
701         char tmp[AST_MAX_EXTENSION] = "";
702
703         for ( ; cdr ; cdr = cdr->next) {
704                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
705                         num = c->cid.cid_ani ? c->cid.cid_ani : c->cid.cid_num;
706                         
707                         if (c->cid.cid_name && num)
708                                 snprintf(tmp, sizeof(tmp), "\"%s\" <%s>", c->cid.cid_name, num);
709                         else if (c->cid.cid_name)
710                                 ast_copy_string(tmp, c->cid.cid_name, sizeof(tmp));
711                         else if (num)
712                                 ast_copy_string(tmp, num, sizeof(tmp));
713                         ast_copy_string(cdr->clid, tmp, sizeof(cdr->clid));
714                         ast_copy_string(cdr->src, num ? num : "", sizeof(cdr->src));
715
716                         /* Copy account code et-al */   
717                         ast_copy_string(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode));
718                         /* Destination information */ /* XXX privilege macro* ? */
719                         ast_copy_string(cdr->dst, S_OR(c->macroexten, c->exten), sizeof(cdr->dst));
720                         ast_copy_string(cdr->dcontext, S_OR(c->macrocontext, c->context), sizeof(cdr->dcontext));
721                 }
722         }
723
724         return 0;
725 }
726
727 int ast_cdr_amaflags2int(const char *flag)
728 {
729         if (!strcasecmp(flag, "default"))
730                 return 0;
731         if (!strcasecmp(flag, "omit"))
732                 return AST_CDR_OMIT;
733         if (!strcasecmp(flag, "billing"))
734                 return AST_CDR_BILLING;
735         if (!strcasecmp(flag, "documentation"))
736                 return AST_CDR_DOCUMENTATION;
737         return -1;
738 }
739
740 static void post_cdr(struct ast_cdr *cdr)
741 {
742         char *chan;
743         struct ast_cdr_beitem *i;
744
745         for ( ; cdr ; cdr = cdr->next) {
746                 chan = S_OR(cdr->channel, "<unknown>");
747                 check_post(cdr);
748                 if (ast_tvzero(cdr->end))
749                         ast_log(LOG_WARNING, "CDR on channel '%s' lacks end\n", chan);
750                 if (ast_tvzero(cdr->start))
751                         ast_log(LOG_WARNING, "CDR on channel '%s' lacks start\n", chan);
752                 ast_set_flag(cdr, AST_CDR_FLAG_POSTED);
753                 AST_LIST_LOCK(&be_list);
754                 AST_LIST_TRAVERSE(&be_list, i, list) {
755                         i->be(cdr);
756                 }
757                 AST_LIST_UNLOCK(&be_list);
758         }
759 }
760
761 void ast_cdr_reset(struct ast_cdr *cdr, struct ast_flags *_flags)
762 {
763         struct ast_cdr *dup;
764         struct ast_flags flags = { 0 };
765
766         if (_flags)
767                 ast_copy_flags(&flags, _flags, AST_FLAGS_ALL);
768
769         for ( ; cdr ; cdr = cdr->next) {
770                 /* Detach if post is requested */
771                 if (ast_test_flag(&flags, AST_CDR_FLAG_LOCKED) || !ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
772                         if (ast_test_flag(&flags, AST_CDR_FLAG_POSTED)) {
773                                 ast_cdr_end(cdr);
774                                 if ((dup = ast_cdr_dup(cdr))) {
775                                         ast_cdr_detach(dup);
776                                 }
777                                 ast_set_flag(cdr, AST_CDR_FLAG_POSTED);
778                         }
779
780                         /* clear variables */
781                         if (!ast_test_flag(&flags, AST_CDR_FLAG_KEEP_VARS)) {
782                                 ast_cdr_free_vars(cdr, 0);
783                         }
784
785                         /* Reset to initial state */
786                         ast_clear_flag(cdr, AST_FLAGS_ALL);     
787                         memset(&cdr->start, 0, sizeof(cdr->start));
788                         memset(&cdr->end, 0, sizeof(cdr->end));
789                         memset(&cdr->answer, 0, sizeof(cdr->answer));
790                         cdr->billsec = 0;
791                         cdr->duration = 0;
792                         ast_cdr_start(cdr);
793                         cdr->disposition = AST_CDR_NOANSWER;
794                 }
795         }
796 }
797
798 struct ast_cdr *ast_cdr_append(struct ast_cdr *cdr, struct ast_cdr *newcdr) 
799 {
800         struct ast_cdr *ret;
801
802         if (cdr) {
803                 ret = cdr;
804
805                 while (cdr->next)
806                         cdr = cdr->next;
807                 cdr->next = newcdr;
808         } else {
809                 ret = newcdr;
810         }
811
812         return ret;
813 }
814
815 /*! \note Don't call without cdr_batch_lock */
816 static void reset_batch(void)
817 {
818         batch->size = 0;
819         batch->head = NULL;
820         batch->tail = NULL;
821 }
822
823 /*! \note Don't call without cdr_batch_lock */
824 static int init_batch(void)
825 {
826         /* This is the single meta-batch used to keep track of all CDRs during the entire life of the program */
827         if (!(batch = ast_malloc(sizeof(*batch))))
828                 return -1;
829
830         reset_batch();
831
832         return 0;
833 }
834
835 static void *do_batch_backend_process(void *data)
836 {
837         struct ast_cdr_batch_item *processeditem;
838         struct ast_cdr_batch_item *batchitem = data;
839
840         /* Push each CDR into storage mechanism(s) and free all the memory */
841         while (batchitem) {
842                 post_cdr(batchitem->cdr);
843                 ast_cdr_free(batchitem->cdr);
844                 processeditem = batchitem;
845                 batchitem = batchitem->next;
846                 free(processeditem);
847         }
848
849         return NULL;
850 }
851
852 void ast_cdr_submit_batch(int shutdown)
853 {
854         struct ast_cdr_batch_item *oldbatchitems = NULL;
855         pthread_attr_t attr;
856         pthread_t batch_post_thread = AST_PTHREADT_NULL;
857
858         /* if there's no batch, or no CDRs in the batch, then there's nothing to do */
859         if (!batch || !batch->head)
860                 return;
861
862         /* move the old CDRs aside, and prepare a new CDR batch */
863         ast_mutex_lock(&cdr_batch_lock);
864         oldbatchitems = batch->head;
865         reset_batch();
866         ast_mutex_unlock(&cdr_batch_lock);
867
868         /* if configured, spawn a new thread to post these CDRs,
869            also try to save as much as possible if we are shutting down safely */
870         if (batchscheduleronly || shutdown) {
871                 if (option_debug)
872                         ast_log(LOG_DEBUG, "CDR single-threaded batch processing begins now\n");
873                 do_batch_backend_process(oldbatchitems);
874         } else {
875                 pthread_attr_init(&attr);
876                 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
877                 if (ast_pthread_create(&batch_post_thread, &attr, do_batch_backend_process, oldbatchitems)) {
878                         ast_log(LOG_WARNING, "CDR processing thread could not detach, now trying in this thread\n");
879                         do_batch_backend_process(oldbatchitems);
880                 } else {
881                         if (option_debug)
882                                 ast_log(LOG_DEBUG, "CDR multi-threaded batch processing begins now\n");
883                 }
884         }
885 }
886
887 static int submit_scheduled_batch(void *data)
888 {
889         ast_cdr_submit_batch(0);
890         /* manually reschedule from this point in time */
891         cdr_sched = ast_sched_add(sched, batchtime * 1000, submit_scheduled_batch, NULL);
892         /* returning zero so the scheduler does not automatically reschedule */
893         return 0;
894 }
895
896 static void submit_unscheduled_batch(void)
897 {
898         /* this is okay since we are not being called from within the scheduler */
899         if (cdr_sched > -1)
900                 ast_sched_del(sched, cdr_sched);
901         /* schedule the submission to occur ASAP (1 ms) */
902         cdr_sched = ast_sched_add(sched, 1, submit_scheduled_batch, NULL);
903         /* signal the do_cdr thread to wakeup early and do some work (that lazy thread ;) */
904         ast_mutex_lock(&cdr_pending_lock);
905         ast_cond_signal(&cdr_pending_cond);
906         ast_mutex_unlock(&cdr_pending_lock);
907 }
908
909 void ast_cdr_detach(struct ast_cdr *cdr)
910 {
911         struct ast_cdr_batch_item *newtail;
912         int curr;
913
914         /* maybe they disabled CDR stuff completely, so just drop it */
915         if (!enabled) {
916                 if (option_debug)
917                         ast_log(LOG_DEBUG, "Dropping CDR !\n");
918                 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
919                 ast_cdr_free(cdr);
920                 return;
921         }
922
923         /* post stuff immediately if we are not in batch mode, this is legacy behaviour */
924         if (!batchmode) {
925                 post_cdr(cdr);
926                 ast_cdr_free(cdr);
927                 return;
928         }
929
930         /* otherwise, each CDR gets put into a batch list (at the end) */
931         if (option_debug)
932                 ast_log(LOG_DEBUG, "CDR detaching from this thread\n");
933
934         /* we'll need a new tail for every CDR */
935         if (!(newtail = ast_calloc(1, sizeof(*newtail)))) {
936                 post_cdr(cdr);
937                 ast_cdr_free(cdr);
938                 return;
939         }
940
941         /* don't traverse a whole list (just keep track of the tail) */
942         ast_mutex_lock(&cdr_batch_lock);
943         if (!batch)
944                 init_batch();
945         if (!batch->head) {
946                 /* new batch is empty, so point the head at the new tail */
947                 batch->head = newtail;
948         } else {
949                 /* already got a batch with something in it, so just append a new tail */
950                 batch->tail->next = newtail;
951         }
952         newtail->cdr = cdr;
953         batch->tail = newtail;
954         curr = batch->size++;
955         ast_mutex_unlock(&cdr_batch_lock);
956
957         /* if we have enough stuff to post, then do it */
958         if (curr >= (batchsize - 1))
959                 submit_unscheduled_batch();
960 }
961
962 static void *do_cdr(void *data)
963 {
964         struct timespec timeout;
965         int schedms;
966         int numevents = 0;
967
968         for(;;) {
969                 struct timeval now = ast_tvnow();
970                 schedms = ast_sched_wait(sched);
971                 /* this shouldn't happen, but provide a 1 second default just in case */
972                 if (schedms <= 0)
973                         schedms = 1000;
974                 timeout.tv_sec = now.tv_sec + (schedms / 1000);
975                 timeout.tv_nsec = (now.tv_usec * 1000) + ((schedms % 1000) * 1000);
976                 /* prevent stuff from clobbering cdr_pending_cond, then wait on signals sent to it until the timeout expires */
977                 ast_mutex_lock(&cdr_pending_lock);
978                 ast_cond_timedwait(&cdr_pending_cond, &cdr_pending_lock, &timeout);
979                 numevents = ast_sched_runq(sched);
980                 ast_mutex_unlock(&cdr_pending_lock);
981                 if (option_debug > 1)
982                         ast_log(LOG_DEBUG, "Processed %d scheduled CDR batches from the run queue\n", numevents);
983         }
984
985         return NULL;
986 }
987
988 static int handle_cli_status(int fd, int argc, char *argv[])
989 {
990         struct ast_cdr_beitem *beitem=NULL;
991         int cnt=0;
992         long nextbatchtime=0;
993
994         if (argc > 2)
995                 return RESULT_SHOWUSAGE;
996
997         ast_cli(fd, "CDR logging: %s\n", enabled ? "enabled" : "disabled");
998         ast_cli(fd, "CDR mode: %s\n", batchmode ? "batch" : "simple");
999         if (enabled) {
1000                 if (batchmode) {
1001                         if (batch)
1002                                 cnt = batch->size;
1003                         if (cdr_sched > -1)
1004                                 nextbatchtime = ast_sched_when(sched, cdr_sched);
1005                         ast_cli(fd, "CDR safe shut down: %s\n", batchsafeshutdown ? "enabled" : "disabled");
1006                         ast_cli(fd, "CDR batch threading model: %s\n", batchscheduleronly ? "scheduler only" : "scheduler plus separate threads");
1007                         ast_cli(fd, "CDR current batch size: %d record%s\n", cnt, (cnt != 1) ? "s" : "");
1008                         ast_cli(fd, "CDR maximum batch size: %d record%s\n", batchsize, (batchsize != 1) ? "s" : "");
1009                         ast_cli(fd, "CDR maximum batch time: %d second%s\n", batchtime, (batchtime != 1) ? "s" : "");
1010                         ast_cli(fd, "CDR next scheduled batch processing time: %ld second%s\n", nextbatchtime, (nextbatchtime != 1) ? "s" : "");
1011                 }
1012                 AST_LIST_LOCK(&be_list);
1013                 AST_LIST_TRAVERSE(&be_list, beitem, list) {
1014                         ast_cli(fd, "CDR registered backend: %s\n", beitem->name);
1015                 }
1016                 AST_LIST_UNLOCK(&be_list);
1017         }
1018
1019         return 0;
1020 }
1021
1022 static int handle_cli_submit(int fd, int argc, char *argv[])
1023 {
1024         if (argc > 2)
1025                 return RESULT_SHOWUSAGE;
1026
1027         submit_unscheduled_batch();
1028         ast_cli(fd, "Submitted CDRs to backend engines for processing.  This may take a while.\n");
1029
1030         return 0;
1031 }
1032
1033 static struct ast_cli_entry cli_submit = {
1034         .cmda = { "cdr", "submit", NULL },
1035         .handler = handle_cli_submit,
1036         .summary = "Posts all pending batched CDR data",
1037         .usage =
1038         "Usage: cdr submit\n"
1039         "       Posts all pending batched CDR data to the configured CDR backend engine modules.\n"
1040 };
1041
1042 static struct ast_cli_entry cli_status = {
1043         .cmda = { "cdr", "status", NULL },
1044         .handler = handle_cli_status,
1045         .summary = "Display the CDR status",
1046         .usage =
1047         "Usage: cdr status\n"
1048         "       Displays the Call Detail Record engine system status.\n"
1049 };
1050
1051 static int do_reload(void)
1052 {
1053         struct ast_config *config;
1054         const char *enabled_value;
1055         const char *batched_value;
1056         const char *scheduleronly_value;
1057         const char *batchsafeshutdown_value;
1058         const char *size_value;
1059         const char *time_value;
1060         const char *end_before_h_value;
1061         int cfg_size;
1062         int cfg_time;
1063         int was_enabled;
1064         int was_batchmode;
1065         int res=0;
1066
1067         ast_mutex_lock(&cdr_batch_lock);
1068
1069         batchsize = BATCH_SIZE_DEFAULT;
1070         batchtime = BATCH_TIME_DEFAULT;
1071         batchscheduleronly = BATCH_SCHEDULER_ONLY_DEFAULT;
1072         batchsafeshutdown = BATCH_SAFE_SHUTDOWN_DEFAULT;
1073         was_enabled = enabled;
1074         was_batchmode = batchmode;
1075         enabled = 1;
1076         batchmode = 0;
1077
1078         /* don't run the next scheduled CDR posting while reloading */
1079         if (cdr_sched > -1)
1080                 ast_sched_del(sched, cdr_sched);
1081
1082         if ((config = ast_config_load("cdr.conf"))) {
1083                 if ((enabled_value = ast_variable_retrieve(config, "general", "enable"))) {
1084                         enabled = ast_true(enabled_value);
1085                 }
1086                 if ((batched_value = ast_variable_retrieve(config, "general", "batch"))) {
1087                         batchmode = ast_true(batched_value);
1088                 }
1089                 if ((scheduleronly_value = ast_variable_retrieve(config, "general", "scheduleronly"))) {
1090                         batchscheduleronly = ast_true(scheduleronly_value);
1091                 }
1092                 if ((batchsafeshutdown_value = ast_variable_retrieve(config, "general", "safeshutdown"))) {
1093                         batchsafeshutdown = ast_true(batchsafeshutdown_value);
1094                 }
1095                 if ((size_value = ast_variable_retrieve(config, "general", "size"))) {
1096                         if (sscanf(size_value, "%d", &cfg_size) < 1)
1097                                 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", size_value);
1098                         else if (size_value < 0)
1099                                 ast_log(LOG_WARNING, "Invalid maximum batch size '%d' specified, using default\n", cfg_size);
1100                         else
1101                                 batchsize = cfg_size;
1102                 }
1103                 if ((time_value = ast_variable_retrieve(config, "general", "time"))) {
1104                         if (sscanf(time_value, "%d", &cfg_time) < 1)
1105                                 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", time_value);
1106                         else if (time_value < 0)
1107                                 ast_log(LOG_WARNING, "Invalid maximum batch time '%d' specified, using default\n", cfg_time);
1108                         else
1109                                 batchtime = cfg_time;
1110                 }
1111                 if ((end_before_h_value = ast_variable_retrieve(config, "general", "endbeforehexten")))
1112                         ast_set2_flag(&ast_options, ast_true(end_before_h_value), AST_OPT_END_CDR_BEFORE_H_EXTEN);
1113         }
1114
1115         if (enabled && !batchmode) {
1116                 ast_log(LOG_NOTICE, "CDR simple logging enabled.\n");
1117         } else if (enabled && batchmode) {
1118                 cdr_sched = ast_sched_add(sched, batchtime * 1000, submit_scheduled_batch, NULL);
1119                 ast_log(LOG_NOTICE, "CDR batch mode logging enabled, first of either size %d or time %d seconds.\n", batchsize, batchtime);
1120         } else {
1121                 ast_log(LOG_NOTICE, "CDR logging disabled, data will be lost.\n");
1122         }
1123
1124         /* if this reload enabled the CDR batch mode, create the background thread
1125            if it does not exist */
1126         if (enabled && batchmode && (!was_enabled || !was_batchmode) && (cdr_thread == AST_PTHREADT_NULL)) {
1127                 ast_cond_init(&cdr_pending_cond, NULL);
1128                 if (ast_pthread_create(&cdr_thread, NULL, do_cdr, NULL) < 0) {
1129                         ast_log(LOG_ERROR, "Unable to start CDR thread.\n");
1130                         ast_sched_del(sched, cdr_sched);
1131                 } else {
1132                         ast_cli_register(&cli_submit);
1133                         ast_register_atexit(ast_cdr_engine_term);
1134                         res = 0;
1135                 }
1136         /* if this reload disabled the CDR and/or batch mode and there is a background thread,
1137            kill it */
1138         } else if (((!enabled && was_enabled) || (!batchmode && was_batchmode)) && (cdr_thread != AST_PTHREADT_NULL)) {
1139                 /* wake up the thread so it will exit */
1140                 pthread_cancel(cdr_thread);
1141                 pthread_kill(cdr_thread, SIGURG);
1142                 pthread_join(cdr_thread, NULL);
1143                 cdr_thread = AST_PTHREADT_NULL;
1144                 ast_cond_destroy(&cdr_pending_cond);
1145                 ast_cli_unregister(&cli_submit);
1146                 ast_unregister_atexit(ast_cdr_engine_term);
1147                 res = 0;
1148                 /* if leaving batch mode, then post the CDRs in the batch,
1149                    and don't reschedule, since we are stopping CDR logging */
1150                 if (!batchmode && was_batchmode) {
1151                         ast_cdr_engine_term();
1152                 }
1153         } else {
1154                 res = 0;
1155         }
1156
1157         ast_mutex_unlock(&cdr_batch_lock);
1158         ast_config_destroy(config);
1159
1160         return res;
1161 }
1162
1163 int ast_cdr_engine_init(void)
1164 {
1165         int res;
1166
1167         sched = sched_context_create();
1168         if (!sched) {
1169                 ast_log(LOG_ERROR, "Unable to create schedule context.\n");
1170                 return -1;
1171         }
1172
1173         ast_cli_register(&cli_status);
1174
1175         res = do_reload();
1176         if (res) {
1177                 ast_mutex_lock(&cdr_batch_lock);
1178                 res = init_batch();
1179                 ast_mutex_unlock(&cdr_batch_lock);
1180         }
1181
1182         return res;
1183 }
1184
1185 /* \note This actually gets called a couple of times at shutdown.  Once, before we start
1186    hanging up channels, and then again, after the channel hangup timeout expires */
1187 void ast_cdr_engine_term(void)
1188 {
1189         ast_cdr_submit_batch(batchsafeshutdown);
1190 }
1191
1192 int ast_cdr_engine_reload(void)
1193 {
1194         return do_reload();
1195 }
1196