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