Add option for logging congested calls as CONGESTION instead of NO_ANSWER in CDR
[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 <signal.h>
39
40 #include "asterisk/lock.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/cdr.h"
43 #include "asterisk/callerid.h"
44 #include "asterisk/manager.h"
45 #include "asterisk/causes.h"
46 #include "asterisk/linkedlists.h"
47 #include "asterisk/utils.h"
48 #include "asterisk/sched.h"
49 #include "asterisk/config.h"
50 #include "asterisk/cli.h"
51 #include "asterisk/stringfields.h"
52 #include "asterisk/data.h"
53
54 /*! Default AMA flag for billing records (CDR's) */
55 int ast_default_amaflags = AST_CDR_DOCUMENTATION;
56 char ast_default_accountcode[AST_MAX_ACCOUNT_CODE];
57
58 struct ast_cdr_beitem {
59         char name[20];
60         char desc[80];
61         ast_cdrbe be;
62         AST_RWLIST_ENTRY(ast_cdr_beitem) list;
63 };
64
65 static AST_RWLIST_HEAD_STATIC(be_list, ast_cdr_beitem);
66
67 struct ast_cdr_batch_item {
68         struct ast_cdr *cdr;
69         struct ast_cdr_batch_item *next;
70 };
71
72 static struct ast_cdr_batch {
73         int size;
74         struct ast_cdr_batch_item *head;
75         struct ast_cdr_batch_item *tail;
76 } *batch = NULL;
77
78
79 static int cdr_sequence =  0;
80
81 static int cdr_seq_inc(struct ast_cdr *cdr);
82
83 static struct ast_sched_context *sched;
84 static int cdr_sched = -1;
85 static pthread_t cdr_thread = AST_PTHREADT_NULL;
86
87 static int enabled;
88 static const int ENABLED_DEFAULT = 1;
89
90 static int batchmode;
91 static const int BATCHMODE_DEFAULT = 0;
92
93 static int unanswered;
94 static const int UNANSWERED_DEFAULT = 0;
95
96 static int congestion;
97 static const int CONGESTION_DEFAULT = 0;
98
99 static int batchsize;
100 static const int BATCH_SIZE_DEFAULT = 100;
101
102 static int batchtime;
103 static const int BATCH_TIME_DEFAULT = 300;
104
105 static int batchscheduleronly;
106 static const int BATCH_SCHEDULER_ONLY_DEFAULT = 0;
107
108 static int batchsafeshutdown;
109 static const int BATCH_SAFE_SHUTDOWN_DEFAULT = 1;
110
111 AST_MUTEX_DEFINE_STATIC(cdr_batch_lock);
112
113 /* these are used to wake up the CDR thread when there's work to do */
114 AST_MUTEX_DEFINE_STATIC(cdr_pending_lock);
115 static ast_cond_t cdr_pending_cond;
116
117 int check_cdr_enabled(void)
118 {
119         return enabled;
120 }
121
122 /*!
123  * \brief Register a CDR driver. Each registered CDR driver generates a CDR
124  * \retval 0 on success.
125  * \retval -1 on error
126  */
127 int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be)
128 {
129         struct ast_cdr_beitem *i = NULL;
130
131         if (!name)
132                 return -1;
133
134         if (!be) {
135                 ast_log(LOG_WARNING, "CDR engine '%s' lacks backend\n", name);
136                 return -1;
137         }
138
139         AST_RWLIST_WRLOCK(&be_list);
140         AST_RWLIST_TRAVERSE(&be_list, i, list) {
141                 if (!strcasecmp(name, i->name)) {
142                         ast_log(LOG_WARNING, "Already have a CDR backend called '%s'\n", name);
143                         AST_RWLIST_UNLOCK(&be_list);
144                         return -1;
145                 }
146         }
147
148         if (!(i = ast_calloc(1, sizeof(*i))))
149                 return -1;
150
151         i->be = be;
152         ast_copy_string(i->name, name, sizeof(i->name));
153         ast_copy_string(i->desc, desc, sizeof(i->desc));
154
155         AST_RWLIST_INSERT_HEAD(&be_list, i, list);
156         AST_RWLIST_UNLOCK(&be_list);
157
158         return 0;
159 }
160
161 /*! unregister a CDR driver */
162 void ast_cdr_unregister(const char *name)
163 {
164         struct ast_cdr_beitem *i = NULL;
165
166         AST_RWLIST_WRLOCK(&be_list);
167         AST_RWLIST_TRAVERSE_SAFE_BEGIN(&be_list, i, list) {
168                 if (!strcasecmp(name, i->name)) {
169                         AST_RWLIST_REMOVE_CURRENT(list);
170                         break;
171                 }
172         }
173         AST_RWLIST_TRAVERSE_SAFE_END;
174         AST_RWLIST_UNLOCK(&be_list);
175
176         if (i) {
177                 ast_verb(2, "Unregistered '%s' CDR backend\n", name);
178                 ast_free(i);
179         }
180 }
181
182 int ast_cdr_isset_unanswered(void)
183 {
184         return unanswered;
185 }
186
187 int ast_cdr_isset_congestion(void)
188 {
189         return congestion;
190 }
191
192 struct ast_cdr *ast_cdr_dup_unique(struct ast_cdr *cdr)
193 {
194         struct ast_cdr *newcdr = ast_cdr_dup(cdr);
195         if (!newcdr)
196                 return NULL;
197
198         cdr_seq_inc(newcdr);
199         return newcdr;
200 }
201
202 struct ast_cdr *ast_cdr_dup_unique_swap(struct ast_cdr *cdr)
203 {
204         struct ast_cdr *newcdr = ast_cdr_dup(cdr);
205         if (!newcdr)
206                 return NULL;
207
208         cdr_seq_inc(cdr);
209         return newcdr;
210 }
211
212 /*! Duplicate a CDR record
213         \returns Pointer to new CDR record
214 */
215 struct ast_cdr *ast_cdr_dup(struct ast_cdr *cdr)
216 {
217         struct ast_cdr *newcdr;
218
219         if (!cdr) /* don't die if we get a null cdr pointer */
220                 return NULL;
221         newcdr = ast_cdr_alloc();
222         if (!newcdr)
223                 return NULL;
224
225         memcpy(newcdr, cdr, sizeof(*newcdr));
226         /* The varshead is unusable, volatile even, after the memcpy so we take care of that here */
227         memset(&newcdr->varshead, 0, sizeof(newcdr->varshead));
228         ast_cdr_copy_vars(newcdr, cdr);
229         newcdr->next = NULL;
230
231         return newcdr;
232 }
233
234 static const char *ast_cdr_getvar_internal(struct ast_cdr *cdr, const char *name, int recur)
235 {
236         if (ast_strlen_zero(name))
237                 return NULL;
238
239         for (; cdr; cdr = recur ? cdr->next : NULL) {
240                 struct ast_var_t *variables;
241                 struct varshead *headp = &cdr->varshead;
242                 AST_LIST_TRAVERSE(headp, variables, entries) {
243                         if (!strcasecmp(name, ast_var_name(variables)))
244                                 return ast_var_value(variables);
245                 }
246         }
247
248         return NULL;
249 }
250
251 static void cdr_get_tv(struct timeval when, const char *fmt, char *buf, int bufsize)
252 {
253         if (fmt == NULL) {      /* raw mode */
254                 snprintf(buf, bufsize, "%ld.%06ld", (long)when.tv_sec, (long)when.tv_usec);
255         } else {
256                 if (when.tv_sec) {
257                         struct ast_tm tm;
258
259                         ast_localtime(&when, &tm, NULL);
260                         ast_strftime(buf, bufsize, fmt, &tm);
261                 }
262         }
263 }
264
265 /*! CDR channel variable retrieval */
266 void ast_cdr_getvar(struct ast_cdr *cdr, const char *name, char **ret, char *workspace, int workspacelen, int recur, int raw)
267 {
268         const char *fmt = "%Y-%m-%d %T";
269         const char *varbuf;
270
271         if (!cdr)  /* don't die if the cdr is null */
272                 return;
273
274         *ret = NULL;
275         /* special vars (the ones from the struct ast_cdr when requested by name)
276            I'd almost say we should convert all the stringed vals to vars */
277
278         if (!strcasecmp(name, "clid"))
279                 ast_copy_string(workspace, cdr->clid, workspacelen);
280         else if (!strcasecmp(name, "src"))
281                 ast_copy_string(workspace, cdr->src, workspacelen);
282         else if (!strcasecmp(name, "dst"))
283                 ast_copy_string(workspace, cdr->dst, workspacelen);
284         else if (!strcasecmp(name, "dcontext"))
285                 ast_copy_string(workspace, cdr->dcontext, workspacelen);
286         else if (!strcasecmp(name, "channel"))
287                 ast_copy_string(workspace, cdr->channel, workspacelen);
288         else if (!strcasecmp(name, "dstchannel"))
289                 ast_copy_string(workspace, cdr->dstchannel, workspacelen);
290         else if (!strcasecmp(name, "lastapp"))
291                 ast_copy_string(workspace, cdr->lastapp, workspacelen);
292         else if (!strcasecmp(name, "lastdata"))
293                 ast_copy_string(workspace, cdr->lastdata, workspacelen);
294         else if (!strcasecmp(name, "start"))
295                 cdr_get_tv(cdr->start, raw ? NULL : fmt, workspace, workspacelen);
296         else if (!strcasecmp(name, "answer"))
297                 cdr_get_tv(cdr->answer, raw ? NULL : fmt, workspace, workspacelen);
298         else if (!strcasecmp(name, "end"))
299                 cdr_get_tv(cdr->end, raw ? NULL : fmt, workspace, workspacelen);
300         else if (!strcasecmp(name, "duration"))
301                 snprintf(workspace, workspacelen, "%ld", cdr->duration ? cdr->duration : (long)ast_tvdiff_ms(ast_tvnow(), cdr->start) / 1000);
302         else if (!strcasecmp(name, "billsec"))
303                 snprintf(workspace, workspacelen, "%ld", cdr->billsec || cdr->answer.tv_sec == 0 ? cdr->billsec : (long)ast_tvdiff_ms(ast_tvnow(), cdr->answer) / 1000);
304         else if (!strcasecmp(name, "disposition")) {
305                 if (raw) {
306                         snprintf(workspace, workspacelen, "%ld", cdr->disposition);
307                 } else {
308                         ast_copy_string(workspace, ast_cdr_disp2str(cdr->disposition), workspacelen);
309                 }
310         } else if (!strcasecmp(name, "amaflags")) {
311                 if (raw) {
312                         snprintf(workspace, workspacelen, "%ld", cdr->amaflags);
313                 } else {
314                         ast_copy_string(workspace, ast_cdr_flags2str(cdr->amaflags), workspacelen);
315                 }
316         } else if (!strcasecmp(name, "accountcode"))
317                 ast_copy_string(workspace, cdr->accountcode, workspacelen);
318         else if (!strcasecmp(name, "peeraccount"))
319                 ast_copy_string(workspace, cdr->peeraccount, workspacelen);
320         else if (!strcasecmp(name, "uniqueid"))
321                 ast_copy_string(workspace, cdr->uniqueid, workspacelen);
322         else if (!strcasecmp(name, "linkedid"))
323                 ast_copy_string(workspace, cdr->linkedid, workspacelen);
324         else if (!strcasecmp(name, "userfield"))
325                 ast_copy_string(workspace, cdr->userfield, workspacelen);
326         else if (!strcasecmp(name, "sequence"))
327                 snprintf(workspace, workspacelen, "%d", cdr->sequence);
328         else if ((varbuf = ast_cdr_getvar_internal(cdr, name, recur)))
329                 ast_copy_string(workspace, varbuf, workspacelen);
330         else
331                 workspace[0] = '\0';
332
333         if (!ast_strlen_zero(workspace))
334                 *ret = workspace;
335 }
336
337 /* readonly cdr variables */
338 static const char * const cdr_readonly_vars[] = { "clid", "src", "dst", "dcontext", "channel", "dstchannel",
339                                                   "lastapp", "lastdata", "start", "answer", "end", "duration",
340                                                   "billsec", "disposition", "amaflags", "accountcode", "uniqueid", "linkedid",
341                                                   "userfield", "sequence", NULL };
342 /*! Set a CDR channel variable
343         \note You can't set the CDR variables that belong to the actual CDR record, like "billsec".
344 */
345 int ast_cdr_setvar(struct ast_cdr *cdr, const char *name, const char *value, int recur)
346 {
347         struct ast_var_t *newvariable;
348         struct varshead *headp;
349         int x;
350
351         for (x = 0; cdr_readonly_vars[x]; x++) {
352                 if (!strcasecmp(name, cdr_readonly_vars[x])) {
353                         ast_log(LOG_ERROR, "Attempt to set the '%s' read-only variable!.\n", name);
354                         return -1;
355                 }
356         }
357
358         if (!cdr) {
359                 ast_log(LOG_ERROR, "Attempt to set a variable on a nonexistent CDR record.\n");
360                 return -1;
361         }
362
363         for (; cdr; cdr = recur ? cdr->next : NULL) {
364                 if (ast_test_flag(cdr, AST_CDR_FLAG_DONT_TOUCH) && ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
365                         continue;
366                 headp = &cdr->varshead;
367                 AST_LIST_TRAVERSE_SAFE_BEGIN(headp, newvariable, entries) {
368                         if (!strcasecmp(ast_var_name(newvariable), name)) {
369                                 /* there is already such a variable, delete it */
370                                 AST_LIST_REMOVE_CURRENT(entries);
371                                 ast_var_delete(newvariable);
372                                 break;
373                         }
374                 }
375                 AST_LIST_TRAVERSE_SAFE_END;
376
377                 if (value) {
378                         newvariable = ast_var_assign(name, value);
379                         AST_LIST_INSERT_HEAD(headp, newvariable, entries);
380                 }
381         }
382
383         return 0;
384 }
385
386 int ast_cdr_copy_vars(struct ast_cdr *to_cdr, struct ast_cdr *from_cdr)
387 {
388         struct ast_var_t *variables, *newvariable = NULL;
389         struct varshead *headpa, *headpb;
390         const char *var, *val;
391         int x = 0;
392
393         if (!to_cdr || !from_cdr) /* don't die if one of the pointers is null */
394                 return 0;
395
396         headpa = &from_cdr->varshead;
397         headpb = &to_cdr->varshead;
398
399         AST_LIST_TRAVERSE(headpa,variables,entries) {
400                 if (variables &&
401                     (var = ast_var_name(variables)) && (val = ast_var_value(variables)) &&
402                     !ast_strlen_zero(var) && !ast_strlen_zero(val)) {
403                         newvariable = ast_var_assign(var, val);
404                         AST_LIST_INSERT_HEAD(headpb, newvariable, entries);
405                         x++;
406                 }
407         }
408
409         return x;
410 }
411
412 int ast_cdr_serialize_variables(struct ast_cdr *cdr, struct ast_str **buf, char delim, char sep, int recur)
413 {
414         struct ast_var_t *variables;
415         const char *var;
416         char *tmp;
417         char workspace[256];
418         int total = 0, x = 0, i;
419
420         ast_str_reset(*buf);
421
422         for (; cdr; cdr = recur ? cdr->next : NULL) {
423                 if (++x > 1)
424                         ast_str_append(buf, 0, "\n");
425
426                 AST_LIST_TRAVERSE(&cdr->varshead, variables, entries) {
427                         if (!(var = ast_var_name(variables))) {
428                                 continue;
429                         }
430
431                         if (ast_str_append(buf, 0, "level %d: %s%c%s%c", x, var, delim, S_OR(ast_var_value(variables), ""), sep) < 0) {
432                                 ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
433                                 break;
434                         }
435
436                         total++;
437                 }
438
439                 for (i = 0; cdr_readonly_vars[i]; i++) {
440                         workspace[0] = 0; /* null out the workspace, because the cdr_get_tv() won't write anything if time is NULL, so you get old vals */
441                         ast_cdr_getvar(cdr, cdr_readonly_vars[i], &tmp, workspace, sizeof(workspace), 0, 0);
442                         if (!tmp)
443                                 continue;
444
445                         if (ast_str_append(buf, 0, "level %d: %s%c%s%c", x, cdr_readonly_vars[i], delim, tmp, sep) < 0) {
446                                 ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
447                                 break;
448                         } else
449                                 total++;
450                 }
451         }
452
453         return total;
454 }
455
456
457 void ast_cdr_free_vars(struct ast_cdr *cdr, int recur)
458 {
459
460         /* clear variables */
461         for (; cdr; cdr = recur ? cdr->next : NULL) {
462                 struct ast_var_t *vardata;
463                 struct varshead *headp = &cdr->varshead;
464                 while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
465                         ast_var_delete(vardata);
466         }
467 }
468
469 /*! \brief  print a warning if cdr already posted */
470 static void check_post(struct ast_cdr *cdr)
471 {
472         if (!cdr)
473                 return;
474         if (ast_test_flag(cdr, AST_CDR_FLAG_POSTED))
475                 ast_log(LOG_NOTICE, "CDR on channel '%s' already posted\n", S_OR(cdr->channel, "<unknown>"));
476 }
477
478 void ast_cdr_free(struct ast_cdr *cdr)
479 {
480
481         while (cdr) {
482                 struct ast_cdr *next = cdr->next;
483
484                 ast_cdr_free_vars(cdr, 0);
485                 ast_free(cdr);
486                 cdr = next;
487         }
488 }
489
490 /*! \brief the same as a cdr_free call, only with no checks; just get rid of it */
491 void ast_cdr_discard(struct ast_cdr *cdr)
492 {
493         while (cdr) {
494                 struct ast_cdr *next = cdr->next;
495
496                 ast_cdr_free_vars(cdr, 0);
497                 ast_free(cdr);
498                 cdr = next;
499         }
500 }
501
502 struct ast_cdr *ast_cdr_alloc(void)
503 {
504         struct ast_cdr *x;
505         x = ast_calloc(1, sizeof(*x));
506         if (!x)
507                 ast_log(LOG_ERROR,"Allocation Failure for a CDR!\n");
508         return x;
509 }
510
511 static void cdr_merge_vars(struct ast_cdr *to, struct ast_cdr *from)
512 {
513         struct ast_var_t *variablesfrom,*variablesto;
514         struct varshead *headpfrom = &to->varshead;
515         struct varshead *headpto = &from->varshead;
516         AST_LIST_TRAVERSE_SAFE_BEGIN(headpfrom, variablesfrom, entries) {
517                 /* for every var in from, stick it in to */
518                 const char *fromvarname, *fromvarval;
519                 const char *tovarname = NULL, *tovarval = NULL;
520                 fromvarname = ast_var_name(variablesfrom);
521                 fromvarval = ast_var_value(variablesfrom);
522                 tovarname = 0;
523
524                 /* now, quick see if that var is in the 'to' cdr already */
525                 AST_LIST_TRAVERSE(headpto, variablesto, entries) {
526
527                         /* now, quick see if that var is in the 'to' cdr already */
528                         if ( strcasecmp(fromvarname, ast_var_name(variablesto)) == 0 ) {
529                                 tovarname = ast_var_name(variablesto);
530                                 tovarval = ast_var_value(variablesto);
531                                 break;
532                         }
533                 }
534                 if (tovarname && strcasecmp(fromvarval,tovarval) != 0) {  /* this message here to see how irritating the userbase finds it */
535                         ast_log(LOG_NOTICE, "Merging CDR's: variable %s value %s dropped in favor of value %s\n", tovarname, fromvarval, tovarval);
536                         continue;
537                 } else if (tovarname && strcasecmp(fromvarval,tovarval) == 0) /* if they are the same, the job is done */
538                         continue;
539
540                 /* rip this var out of the from cdr, and stick it in the to cdr */
541                 AST_LIST_MOVE_CURRENT(headpto, entries);
542         }
543         AST_LIST_TRAVERSE_SAFE_END;
544 }
545
546 void ast_cdr_merge(struct ast_cdr *to, struct ast_cdr *from)
547 {
548         struct ast_cdr *zcdr;
549         struct ast_cdr *lto = NULL;
550         struct ast_cdr *lfrom = NULL;
551         int discard_from = 0;
552
553         if (!to || !from)
554                 return;
555
556         /* don't merge into locked CDR's -- it's bad business */
557         if (ast_test_flag(to, AST_CDR_FLAG_LOCKED)) {
558                 zcdr = to; /* safety valve? */
559                 while (to->next) {
560                         lto = to;
561                         to = to->next;
562                 }
563
564                 if (ast_test_flag(to, AST_CDR_FLAG_LOCKED)) {
565                         ast_log(LOG_WARNING, "Merging into locked CDR... no choice.");
566                         to = zcdr; /* safety-- if all there are is locked CDR's, then.... ?? */
567                         lto = NULL;
568                 }
569         }
570
571         if (ast_test_flag(from, AST_CDR_FLAG_LOCKED)) {
572                 struct ast_cdr *llfrom = NULL;
573                 discard_from = 1;
574                 if (lto) {
575                         /* insert the from stuff after lto */
576                         lto->next = from;
577                         lfrom = from;
578                         while (lfrom && lfrom->next) {
579                                 if (!lfrom->next->next)
580                                         llfrom = lfrom;
581                                 lfrom = lfrom->next;
582                         }
583                         /* rip off the last entry and put a copy of the to at the end */
584                         llfrom->next = to;
585                         from = lfrom;
586                 } else {
587                         /* save copy of the current *to cdr */
588                         struct ast_cdr tcdr;
589                         memcpy(&tcdr, to, sizeof(tcdr));
590                         /* copy in the locked from cdr */
591                         memcpy(to, from, sizeof(*to));
592                         lfrom = from;
593                         while (lfrom && lfrom->next) {
594                                 if (!lfrom->next->next)
595                                         llfrom = lfrom;
596                                 lfrom = lfrom->next;
597                         }
598                         from->next = NULL;
599                         /* rip off the last entry and put a copy of the to at the end */
600                         if (llfrom == from)
601                                 to = to->next = ast_cdr_dup(&tcdr);
602                         else
603                                 to = llfrom->next = ast_cdr_dup(&tcdr);
604                         from = lfrom;
605                 }
606         }
607
608         if (!ast_tvzero(from->start)) {
609                 if (!ast_tvzero(to->start)) {
610                         if (ast_tvcmp(to->start, from->start) > 0 ) {
611                                 to->start = from->start; /* use the earliest time */
612                                 from->start = ast_tv(0,0); /* we actively "steal" these values */
613                         }
614                         /* else nothing to do */
615                 } else {
616                         to->start = from->start;
617                         from->start = ast_tv(0,0); /* we actively "steal" these values */
618                 }
619         }
620         if (!ast_tvzero(from->answer)) {
621                 if (!ast_tvzero(to->answer)) {
622                         if (ast_tvcmp(to->answer, from->answer) > 0 ) {
623                                 to->answer = from->answer; /* use the earliest time */
624                                 from->answer = ast_tv(0,0); /* we actively "steal" these values */
625                         }
626                         /* we got the earliest answer time, so we'll settle for that? */
627                 } else {
628                         to->answer = from->answer;
629                         from->answer = ast_tv(0,0); /* we actively "steal" these values */
630                 }
631         }
632         if (!ast_tvzero(from->end)) {
633                 if (!ast_tvzero(to->end)) {
634                         if (ast_tvcmp(to->end, from->end) < 0 ) {
635                                 to->end = from->end; /* use the latest time */
636                                 from->end = ast_tv(0,0); /* we actively "steal" these values */
637                                 to->duration = to->end.tv_sec - to->start.tv_sec;  /* don't forget to update the duration, billsec, when we set end */
638                                 to->billsec = ast_tvzero(to->answer) ? 0 : to->end.tv_sec - to->answer.tv_sec;
639                         }
640                         /* else, nothing to do */
641                 } else {
642                         to->end = from->end;
643                         from->end = ast_tv(0,0); /* we actively "steal" these values */
644                         to->duration = to->end.tv_sec - to->start.tv_sec;
645                         to->billsec = ast_tvzero(to->answer) ? 0 : to->end.tv_sec - to->answer.tv_sec;
646                 }
647         }
648         if (to->disposition < from->disposition) {
649                 to->disposition = from->disposition;
650                 from->disposition = AST_CDR_NOANSWER;
651         }
652         if (ast_strlen_zero(to->lastapp) && !ast_strlen_zero(from->lastapp)) {
653                 ast_copy_string(to->lastapp, from->lastapp, sizeof(to->lastapp));
654                 from->lastapp[0] = 0; /* theft */
655         }
656         if (ast_strlen_zero(to->lastdata) && !ast_strlen_zero(from->lastdata)) {
657                 ast_copy_string(to->lastdata, from->lastdata, sizeof(to->lastdata));
658                 from->lastdata[0] = 0; /* theft */
659         }
660         if (ast_strlen_zero(to->dcontext) && !ast_strlen_zero(from->dcontext)) {
661                 ast_copy_string(to->dcontext, from->dcontext, sizeof(to->dcontext));
662                 from->dcontext[0] = 0; /* theft */
663         }
664         if (ast_strlen_zero(to->dstchannel) && !ast_strlen_zero(from->dstchannel)) {
665                 ast_copy_string(to->dstchannel, from->dstchannel, sizeof(to->dstchannel));
666                 from->dstchannel[0] = 0; /* theft */
667         }
668         if (!ast_strlen_zero(from->channel) && (ast_strlen_zero(to->channel) || !strncasecmp(from->channel, "Agent/", 6))) {
669                 ast_copy_string(to->channel, from->channel, sizeof(to->channel));
670                 from->channel[0] = 0; /* theft */
671         }
672         if (ast_strlen_zero(to->src) && !ast_strlen_zero(from->src)) {
673                 ast_copy_string(to->src, from->src, sizeof(to->src));
674                 from->src[0] = 0; /* theft */
675         }
676         if (ast_strlen_zero(to->clid) && !ast_strlen_zero(from->clid)) {
677                 ast_copy_string(to->clid, from->clid, sizeof(to->clid));
678                 from->clid[0] = 0; /* theft */
679         }
680         if (ast_strlen_zero(to->dst) && !ast_strlen_zero(from->dst)) {
681                 ast_copy_string(to->dst, from->dst, sizeof(to->dst));
682                 from->dst[0] = 0; /* theft */
683         }
684         if (!to->amaflags)
685                 to->amaflags = AST_CDR_DOCUMENTATION;
686         if (!from->amaflags)
687                 from->amaflags = AST_CDR_DOCUMENTATION; /* make sure both amaflags are set to something (DOC is default) */
688         if (ast_test_flag(from, AST_CDR_FLAG_LOCKED) || (to->amaflags == AST_CDR_DOCUMENTATION && from->amaflags != AST_CDR_DOCUMENTATION)) {
689                 to->amaflags = from->amaflags;
690         }
691         if (ast_test_flag(from, AST_CDR_FLAG_LOCKED) || (ast_strlen_zero(to->accountcode) && !ast_strlen_zero(from->accountcode))) {
692                 ast_copy_string(to->accountcode, from->accountcode, sizeof(to->accountcode));
693         }
694         if (ast_test_flag(from, AST_CDR_FLAG_LOCKED) || (ast_strlen_zero(to->peeraccount) && !ast_strlen_zero(from->peeraccount))) {
695                 ast_copy_string(to->peeraccount, from->peeraccount, sizeof(to->peeraccount));
696         }
697         if (ast_test_flag(from, AST_CDR_FLAG_LOCKED) || (ast_strlen_zero(to->userfield) && !ast_strlen_zero(from->userfield))) {
698                 ast_copy_string(to->userfield, from->userfield, sizeof(to->userfield));
699         }
700         /* flags, varsead, ? */
701         cdr_merge_vars(from, to);
702
703         if (ast_test_flag(from, AST_CDR_FLAG_KEEP_VARS))
704                 ast_set_flag(to, AST_CDR_FLAG_KEEP_VARS);
705         if (ast_test_flag(from, AST_CDR_FLAG_POSTED))
706                 ast_set_flag(to, AST_CDR_FLAG_POSTED);
707         if (ast_test_flag(from, AST_CDR_FLAG_LOCKED))
708                 ast_set_flag(to, AST_CDR_FLAG_LOCKED);
709         if (ast_test_flag(from, AST_CDR_FLAG_CHILD))
710                 ast_set_flag(to, AST_CDR_FLAG_CHILD);
711         if (ast_test_flag(from, AST_CDR_FLAG_POST_DISABLED))
712                 ast_set_flag(to, AST_CDR_FLAG_POST_DISABLED);
713
714         /* last, but not least, we need to merge any forked CDRs to the 'to' cdr */
715         while (from->next) {
716                 /* just rip 'em off the 'from' and insert them on the 'to' */
717                 zcdr = from->next;
718                 from->next = zcdr->next;
719                 zcdr->next = NULL;
720                 /* zcdr is now ripped from the current list; */
721                 ast_cdr_append(to, zcdr);
722         }
723         if (discard_from)
724                 ast_cdr_discard(from);
725 }
726
727 void ast_cdr_start(struct ast_cdr *cdr)
728 {
729         for (; cdr; cdr = cdr->next) {
730                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
731                         check_post(cdr);
732                         cdr->start = ast_tvnow();
733                 }
734         }
735 }
736
737 void ast_cdr_answer(struct ast_cdr *cdr)
738 {
739
740         for (; cdr; cdr = cdr->next) {
741                 if (ast_test_flag(cdr, AST_CDR_FLAG_ANSLOCKED))
742                         continue;
743                 if (ast_test_flag(cdr, AST_CDR_FLAG_DONT_TOUCH) && ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
744                         continue;
745                 check_post(cdr);
746                 if (cdr->disposition < AST_CDR_ANSWERED)
747                         cdr->disposition = AST_CDR_ANSWERED;
748                 if (ast_tvzero(cdr->answer))
749                         cdr->answer = ast_tvnow();
750         }
751 }
752
753 void ast_cdr_busy(struct ast_cdr *cdr)
754 {
755
756         for (; cdr; cdr = cdr->next) {
757                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
758                         check_post(cdr);
759                         cdr->disposition = AST_CDR_BUSY;
760                 }
761         }
762 }
763
764 void ast_cdr_failed(struct ast_cdr *cdr)
765 {
766         for (; cdr; cdr = cdr->next) {
767                 check_post(cdr);
768                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
769                         check_post(cdr);
770                         if (cdr->disposition < AST_CDR_FAILED)
771                                 cdr->disposition = AST_CDR_FAILED;
772                 }
773         }
774 }
775
776 void ast_cdr_noanswer(struct ast_cdr *cdr)
777 {
778         while (cdr) {
779                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
780                         check_post(cdr);
781                         cdr->disposition = AST_CDR_NOANSWER;
782                 }
783                 cdr = cdr->next;
784         }
785 }
786
787 void ast_cdr_congestion(struct ast_cdr *cdr)
788 {
789         char *chan;
790
791         ast_verb (1, "congestion value: %d\n  INYOURFACE", congestion);
792
793
794         /* if congestion log is disabled, pass the buck to ast_cdr_failed */
795         if (!congestion) {
796                 ast_cdr_failed(cdr);
797         }
798
799         while (cdr && congestion) {
800                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
801                         chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
802
803                         if (ast_test_flag(cdr, AST_CDR_FLAG_POSTED)) {
804                                 ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
805                         }
806
807                         if (cdr->disposition < AST_CDR_CONGESTION) {
808                                 cdr->disposition = AST_CDR_CONGESTION;
809                         }
810                 }
811                 cdr = cdr->next;
812         }
813 }
814
815 /* everywhere ast_cdr_disposition is called, it will call ast_cdr_failed()
816    if ast_cdr_disposition returns a non-zero value */
817
818 int ast_cdr_disposition(struct ast_cdr *cdr, int cause)
819 {
820         int res = 0;
821
822         for (; cdr; cdr = cdr->next) {
823                 switch (cause) {  /* handle all the non failure, busy cases, return 0 not to set disposition,
824                                                         return -1 to set disposition to FAILED */
825                 case AST_CAUSE_BUSY:
826                         ast_cdr_busy(cdr);
827                         break;
828                 case AST_CAUSE_NO_ANSWER:
829                         ast_cdr_noanswer(cdr);
830                         break;
831                 case AST_CAUSE_NORMAL_CIRCUIT_CONGESTION:
832                         ast_cdr_congestion(cdr);
833                         break;
834                 case AST_CAUSE_NORMAL:
835                         break;
836                 default:
837                         res = -1;
838                 }
839         }
840         return res;
841 }
842
843 void ast_cdr_setdestchan(struct ast_cdr *cdr, const char *chann)
844 {
845         for (; cdr; cdr = cdr->next) {
846                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
847                         check_post(cdr);
848                         ast_copy_string(cdr->dstchannel, chann, sizeof(cdr->dstchannel));
849                 }
850         }
851 }
852
853 void ast_cdr_setapp(struct ast_cdr *cdr, const char *app, const char *data)
854 {
855
856         for (; cdr; cdr = cdr->next) {
857                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
858                         check_post(cdr);
859                         ast_copy_string(cdr->lastapp, S_OR(app, ""), sizeof(cdr->lastapp));
860                         ast_copy_string(cdr->lastdata, S_OR(data, ""), sizeof(cdr->lastdata));
861                 }
862         }
863 }
864
865 void ast_cdr_setanswer(struct ast_cdr *cdr, struct timeval t)
866 {
867
868         for (; cdr; cdr = cdr->next) {
869                 if (ast_test_flag(cdr, AST_CDR_FLAG_ANSLOCKED))
870                         continue;
871                 if (ast_test_flag(cdr, AST_CDR_FLAG_DONT_TOUCH) && ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
872                         continue;
873                 check_post(cdr);
874                 cdr->answer = t;
875         }
876 }
877
878 void ast_cdr_setdisposition(struct ast_cdr *cdr, long int disposition)
879 {
880
881         for (; cdr; cdr = cdr->next) {
882                 if (ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
883                         continue;
884                 check_post(cdr);
885                 cdr->disposition = disposition;
886         }
887 }
888
889 /* set cid info for one record */
890 static void set_one_cid(struct ast_cdr *cdr, struct ast_channel *c)
891 {
892         const char *num;
893
894         if (!cdr) {
895                 return;
896         }
897
898         /* Grab source from ANI or normal Caller*ID */
899         num = S_COR(c->caller.ani.number.valid, c->caller.ani.number.str,
900                 S_COR(c->caller.id.number.valid, c->caller.id.number.str, NULL));
901         ast_callerid_merge(cdr->clid, sizeof(cdr->clid),
902                 S_COR(c->caller.id.name.valid, c->caller.id.name.str, NULL), num, "");
903         ast_copy_string(cdr->src, S_OR(num, ""), sizeof(cdr->src));
904         ast_cdr_setvar(cdr, "dnid", S_OR(c->dialed.number.str, ""), 0);
905
906         if (c->caller.id.subaddress.valid) {
907                 ast_cdr_setvar(cdr, "callingsubaddr", S_OR(c->caller.id.subaddress.str, ""), 0);
908         }
909         if (c->dialed.subaddress.valid) {
910                 ast_cdr_setvar(cdr, "calledsubaddr", S_OR(c->dialed.subaddress.str, ""), 0);
911         }
912 }
913
914 int ast_cdr_setcid(struct ast_cdr *cdr, struct ast_channel *c)
915 {
916         for (; cdr; cdr = cdr->next) {
917                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
918                         set_one_cid(cdr, c);
919         }
920         return 0;
921 }
922
923 static int cdr_seq_inc(struct ast_cdr *cdr)
924 {
925         return (cdr->sequence = ast_atomic_fetchadd_int(&cdr_sequence, +1));
926 }
927
928 int ast_cdr_init(struct ast_cdr *cdr, struct ast_channel *c)
929 {
930         for ( ; cdr ; cdr = cdr->next) {
931                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
932                         ast_copy_string(cdr->channel, c->name, sizeof(cdr->channel));
933                         set_one_cid(cdr, c);
934                         cdr_seq_inc(cdr);
935
936                         cdr->disposition = (c->_state == AST_STATE_UP) ?  AST_CDR_ANSWERED : AST_CDR_NOANSWER;
937                         cdr->amaflags = c->amaflags ? c->amaflags :  ast_default_amaflags;
938                         ast_copy_string(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode));
939                         ast_copy_string(cdr->peeraccount, c->peeraccount, sizeof(cdr->peeraccount));
940                         /* Destination information */
941                         ast_copy_string(cdr->dst, S_OR(c->macroexten,c->exten), sizeof(cdr->dst));
942                         ast_copy_string(cdr->dcontext, S_OR(c->macrocontext,c->context), sizeof(cdr->dcontext));
943                         /* Unique call identifier */
944                         ast_copy_string(cdr->uniqueid, c->uniqueid, sizeof(cdr->uniqueid));
945                         /* Linked call identifier */
946                         ast_copy_string(cdr->linkedid, c->linkedid, sizeof(cdr->linkedid));
947                 }
948         }
949         return 0;
950 }
951
952 /* Three routines were "fixed" via 10668, and later shown that
953    users were depending on this behavior. ast_cdr_end,
954    ast_cdr_setvar and ast_cdr_answer are the three routines.
955    While most of the other routines would not touch
956    LOCKED cdr's, these three routines were designed to
957    operate on locked CDR's as a matter of course.
958    I now appreciate how this plays with the ForkCDR app,
959    which forms these cdr chains in the first place.
960    cdr_end is pretty key: all cdrs created are closed
961    together. They only vary by start time. Arithmetically,
962    users can calculate the subintervals they wish to track. */
963
964 void ast_cdr_end(struct ast_cdr *cdr)
965 {
966         for ( ; cdr ; cdr = cdr->next) {
967                 if (ast_test_flag(cdr, AST_CDR_FLAG_DONT_TOUCH) && ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
968                         continue;
969                 check_post(cdr);
970                 if (ast_tvzero(cdr->end))
971                         cdr->end = ast_tvnow();
972                 if (ast_tvzero(cdr->start)) {
973                         ast_log(LOG_WARNING, "CDR on channel '%s' has not started\n", S_OR(cdr->channel, "<unknown>"));
974                         cdr->disposition = AST_CDR_FAILED;
975                 } else
976                         cdr->duration = cdr->end.tv_sec - cdr->start.tv_sec;
977                 if (ast_tvzero(cdr->answer)) {
978                         if (cdr->disposition == AST_CDR_ANSWERED) {
979                                 ast_log(LOG_WARNING, "CDR on channel '%s' has no answer time but is 'ANSWERED'\n", S_OR(cdr->channel, "<unknown>"));
980                                 cdr->disposition = AST_CDR_FAILED;
981                         }
982                 } else {
983                         cdr->billsec = cdr->end.tv_sec - cdr->answer.tv_sec;
984                         if (ast_test_flag(&ast_options, AST_OPT_FLAG_INITIATED_SECONDS))
985                                 cdr->billsec += cdr->end.tv_usec > cdr->answer.tv_usec ? 1 : 0;
986                 }
987         }
988 }
989
990 char *ast_cdr_disp2str(int disposition)
991 {
992         switch (disposition) {
993         case AST_CDR_NULL:
994                 return "NO ANSWER"; /* by default, for backward compatibility */
995         case AST_CDR_NOANSWER:
996                 return "NO ANSWER";
997         case AST_CDR_FAILED:
998                 return "FAILED";
999         case AST_CDR_BUSY:
1000                 return "BUSY";
1001         case AST_CDR_ANSWERED:
1002                 return "ANSWERED";
1003         case AST_CDR_CONGESTION:
1004                 return "CONGESTION";
1005         }
1006         return "UNKNOWN";
1007 }
1008
1009 /*! Converts AMA flag to printable string */
1010 char *ast_cdr_flags2str(int flag)
1011 {
1012         switch (flag) {
1013         case AST_CDR_OMIT:
1014                 return "OMIT";
1015         case AST_CDR_BILLING:
1016                 return "BILLING";
1017         case AST_CDR_DOCUMENTATION:
1018                 return "DOCUMENTATION";
1019         }
1020         return "Unknown";
1021 }
1022
1023 int ast_cdr_setaccount(struct ast_channel *chan, const char *account)
1024 {
1025         struct ast_cdr *cdr = chan->cdr;
1026         const char *old_acct = "";
1027
1028         if (!ast_strlen_zero(chan->accountcode)) {
1029                 old_acct = ast_strdupa(chan->accountcode);
1030         }
1031
1032         ast_string_field_set(chan, accountcode, account);
1033         for ( ; cdr ; cdr = cdr->next) {
1034                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1035                         ast_copy_string(cdr->accountcode, chan->accountcode, sizeof(cdr->accountcode));
1036                 }
1037         }
1038
1039         ast_manager_event(chan, EVENT_FLAG_CALL, "NewAccountCode",
1040                         "Channel: %s\r\n"
1041                         "Uniqueid: %s\r\n"
1042                         "AccountCode: %s\r\n"
1043                         "OldAccountCode: %s\r\n",
1044                         chan->name, chan->uniqueid, chan->accountcode, old_acct);
1045
1046         return 0;
1047 }
1048
1049 int ast_cdr_setpeeraccount(struct ast_channel *chan, const char *account)
1050 {
1051         struct ast_cdr *cdr = chan->cdr;
1052         const char *old_acct = "";
1053
1054         if (!ast_strlen_zero(chan->peeraccount)) {
1055                 old_acct = ast_strdupa(chan->peeraccount);
1056         }
1057
1058         ast_string_field_set(chan, peeraccount, account);
1059         for ( ; cdr ; cdr = cdr->next) {
1060                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1061                         ast_copy_string(cdr->peeraccount, chan->peeraccount, sizeof(cdr->peeraccount));
1062                 }
1063         }
1064
1065         ast_manager_event(chan, EVENT_FLAG_CALL, "NewPeerAccount",
1066                         "Channel: %s\r\n"
1067                         "Uniqueid: %s\r\n"
1068                         "PeerAccount: %s\r\n"
1069                         "OldPeerAccount: %s\r\n",
1070                         chan->name, chan->uniqueid, chan->peeraccount, old_acct);
1071
1072         return 0;
1073 }
1074
1075 int ast_cdr_setamaflags(struct ast_channel *chan, const char *flag)
1076 {
1077         struct ast_cdr *cdr;
1078         int newflag = ast_cdr_amaflags2int(flag);
1079         if (newflag) {
1080                 for (cdr = chan->cdr; cdr; cdr = cdr->next) {
1081                         if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1082                                 cdr->amaflags = newflag;
1083                         }
1084                 }
1085         }
1086
1087         return 0;
1088 }
1089
1090 int ast_cdr_setuserfield(struct ast_channel *chan, const char *userfield)
1091 {
1092         struct ast_cdr *cdr = chan->cdr;
1093
1094         for ( ; cdr ; cdr = cdr->next) {
1095                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
1096                         ast_copy_string(cdr->userfield, userfield, sizeof(cdr->userfield));
1097         }
1098
1099         return 0;
1100 }
1101
1102 int ast_cdr_appenduserfield(struct ast_channel *chan, const char *userfield)
1103 {
1104         struct ast_cdr *cdr = chan->cdr;
1105
1106         for ( ; cdr ; cdr = cdr->next) {
1107                 int len = strlen(cdr->userfield);
1108
1109                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
1110                         ast_copy_string(cdr->userfield + len, userfield, sizeof(cdr->userfield) - len);
1111         }
1112
1113         return 0;
1114 }
1115
1116 int ast_cdr_update(struct ast_channel *c)
1117 {
1118         struct ast_cdr *cdr = c->cdr;
1119
1120         for ( ; cdr ; cdr = cdr->next) {
1121                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1122                         set_one_cid(cdr, c);
1123
1124                         /* Copy account code et-al */
1125                         ast_copy_string(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode));
1126                         ast_copy_string(cdr->peeraccount, c->peeraccount, sizeof(cdr->peeraccount));
1127                         ast_copy_string(cdr->linkedid, c->linkedid, sizeof(cdr->linkedid));
1128
1129                         /* Destination information */ /* XXX privilege macro* ? */
1130                         ast_copy_string(cdr->dst, S_OR(c->macroexten, c->exten), sizeof(cdr->dst));
1131                         ast_copy_string(cdr->dcontext, S_OR(c->macrocontext, c->context), sizeof(cdr->dcontext));
1132                 }
1133         }
1134
1135         return 0;
1136 }
1137
1138 int ast_cdr_amaflags2int(const char *flag)
1139 {
1140         if (!strcasecmp(flag, "default"))
1141                 return 0;
1142         if (!strcasecmp(flag, "omit"))
1143                 return AST_CDR_OMIT;
1144         if (!strcasecmp(flag, "billing"))
1145                 return AST_CDR_BILLING;
1146         if (!strcasecmp(flag, "documentation"))
1147                 return AST_CDR_DOCUMENTATION;
1148         return -1;
1149 }
1150
1151 static void post_cdr(struct ast_cdr *cdr)
1152 {
1153         struct ast_cdr_beitem *i;
1154
1155         for ( ; cdr ; cdr = cdr->next) {
1156                 if (!unanswered && cdr->disposition < AST_CDR_ANSWERED && (ast_strlen_zero(cdr->channel) || ast_strlen_zero(cdr->dstchannel))) {
1157                         /* For people, who don't want to see unanswered single-channel events */
1158                         ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1159                         continue;
1160                 }
1161
1162                 /* don't post CDRs that are for dialed channels unless those
1163                  * channels were originated from asterisk (pbx_spool, manager,
1164                  * cli) */
1165                 if (ast_test_flag(cdr, AST_CDR_FLAG_DIALED) && !ast_test_flag(cdr, AST_CDR_FLAG_ORIGINATED)) {
1166                         ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1167                         continue;
1168                 }
1169
1170                 check_post(cdr);
1171                 ast_set_flag(cdr, AST_CDR_FLAG_POSTED);
1172                 if (ast_test_flag(cdr, AST_CDR_FLAG_POST_DISABLED))
1173                         continue;
1174                 AST_RWLIST_RDLOCK(&be_list);
1175                 AST_RWLIST_TRAVERSE(&be_list, i, list) {
1176                         i->be(cdr);
1177                 }
1178                 AST_RWLIST_UNLOCK(&be_list);
1179         }
1180 }
1181
1182 void ast_cdr_reset(struct ast_cdr *cdr, struct ast_flags *_flags)
1183 {
1184         struct ast_cdr *duplicate;
1185         struct ast_flags flags = { 0 };
1186
1187         if (_flags)
1188                 ast_copy_flags(&flags, _flags, AST_FLAGS_ALL);
1189
1190         for ( ; cdr ; cdr = cdr->next) {
1191                 /* Detach if post is requested */
1192                 if (ast_test_flag(&flags, AST_CDR_FLAG_LOCKED) || !ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1193                         if (ast_test_flag(&flags, AST_CDR_FLAG_POSTED)) {
1194                                 ast_cdr_end(cdr);
1195                                 if ((duplicate = ast_cdr_dup_unique_swap(cdr))) {
1196                                         ast_cdr_detach(duplicate);
1197                                 }
1198                                 ast_set_flag(cdr, AST_CDR_FLAG_POSTED);
1199                         }
1200
1201                         /* enable CDR only */
1202                         if (ast_test_flag(&flags, AST_CDR_FLAG_POST_ENABLE)) {
1203                                 ast_clear_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1204                                 continue;
1205                         }
1206
1207                         /* clear variables */
1208                         if (!ast_test_flag(&flags, AST_CDR_FLAG_KEEP_VARS)) {
1209                                 ast_cdr_free_vars(cdr, 0);
1210                         }
1211
1212                         /* Reset to initial state */
1213                         ast_clear_flag(cdr, AST_FLAGS_ALL);
1214                         memset(&cdr->start, 0, sizeof(cdr->start));
1215                         memset(&cdr->end, 0, sizeof(cdr->end));
1216                         memset(&cdr->answer, 0, sizeof(cdr->answer));
1217                         cdr->billsec = 0;
1218                         cdr->duration = 0;
1219                         ast_cdr_start(cdr);
1220                         cdr->disposition = AST_CDR_NOANSWER;
1221                 }
1222         }
1223 }
1224
1225 void ast_cdr_specialized_reset(struct ast_cdr *cdr, struct ast_flags *_flags)
1226 {
1227         struct ast_flags flags = { 0 };
1228
1229         if (_flags)
1230                 ast_copy_flags(&flags, _flags, AST_FLAGS_ALL);
1231
1232         /* Reset to initial state */
1233         if (ast_test_flag(cdr, AST_CDR_FLAG_POST_DISABLED)) { /* But do NOT lose the NoCDR() setting */
1234                 ast_clear_flag(cdr, AST_FLAGS_ALL);
1235                 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1236         } else {
1237                 ast_clear_flag(cdr, AST_FLAGS_ALL);
1238         }
1239
1240         memset(&cdr->start, 0, sizeof(cdr->start));
1241         memset(&cdr->end, 0, sizeof(cdr->end));
1242         memset(&cdr->answer, 0, sizeof(cdr->answer));
1243         cdr->billsec = 0;
1244         cdr->duration = 0;
1245         ast_cdr_start(cdr);
1246         cdr->disposition = AST_CDR_NULL;
1247 }
1248
1249 struct ast_cdr *ast_cdr_append(struct ast_cdr *cdr, struct ast_cdr *newcdr)
1250 {
1251         struct ast_cdr *ret;
1252
1253         if (cdr) {
1254                 ret = cdr;
1255
1256                 while (cdr->next)
1257                         cdr = cdr->next;
1258                 cdr->next = newcdr;
1259         } else {
1260                 ret = newcdr;
1261         }
1262
1263         return ret;
1264 }
1265
1266 /*! \note Don't call without cdr_batch_lock */
1267 static void reset_batch(void)
1268 {
1269         batch->size = 0;
1270         batch->head = NULL;
1271         batch->tail = NULL;
1272 }
1273
1274 /*! \note Don't call without cdr_batch_lock */
1275 static int init_batch(void)
1276 {
1277         /* This is the single meta-batch used to keep track of all CDRs during the entire life of the program */
1278         if (!(batch = ast_malloc(sizeof(*batch))))
1279                 return -1;
1280
1281         reset_batch();
1282
1283         return 0;
1284 }
1285
1286 static void *do_batch_backend_process(void *data)
1287 {
1288         struct ast_cdr_batch_item *processeditem;
1289         struct ast_cdr_batch_item *batchitem = data;
1290
1291         /* Push each CDR into storage mechanism(s) and free all the memory */
1292         while (batchitem) {
1293                 post_cdr(batchitem->cdr);
1294                 ast_cdr_free(batchitem->cdr);
1295                 processeditem = batchitem;
1296                 batchitem = batchitem->next;
1297                 ast_free(processeditem);
1298         }
1299
1300         return NULL;
1301 }
1302
1303 void ast_cdr_submit_batch(int do_shutdown)
1304 {
1305         struct ast_cdr_batch_item *oldbatchitems = NULL;
1306         pthread_t batch_post_thread = AST_PTHREADT_NULL;
1307
1308         /* if there's no batch, or no CDRs in the batch, then there's nothing to do */
1309         if (!batch || !batch->head)
1310                 return;
1311
1312         /* move the old CDRs aside, and prepare a new CDR batch */
1313         ast_mutex_lock(&cdr_batch_lock);
1314         oldbatchitems = batch->head;
1315         reset_batch();
1316         ast_mutex_unlock(&cdr_batch_lock);
1317
1318         /* if configured, spawn a new thread to post these CDRs,
1319            also try to save as much as possible if we are shutting down safely */
1320         if (batchscheduleronly || do_shutdown) {
1321                 ast_debug(1, "CDR single-threaded batch processing begins now\n");
1322                 do_batch_backend_process(oldbatchitems);
1323         } else {
1324                 if (ast_pthread_create_detached_background(&batch_post_thread, NULL, do_batch_backend_process, oldbatchitems)) {
1325                         ast_log(LOG_WARNING, "CDR processing thread could not detach, now trying in this thread\n");
1326                         do_batch_backend_process(oldbatchitems);
1327                 } else {
1328                         ast_debug(1, "CDR multi-threaded batch processing begins now\n");
1329                 }
1330         }
1331 }
1332
1333 static int submit_scheduled_batch(const void *data)
1334 {
1335         ast_cdr_submit_batch(0);
1336         /* manually reschedule from this point in time */
1337         cdr_sched = ast_sched_add(sched, batchtime * 1000, submit_scheduled_batch, NULL);
1338         /* returning zero so the scheduler does not automatically reschedule */
1339         return 0;
1340 }
1341
1342 static void submit_unscheduled_batch(void)
1343 {
1344         /* this is okay since we are not being called from within the scheduler */
1345         AST_SCHED_DEL(sched, cdr_sched);
1346         /* schedule the submission to occur ASAP (1 ms) */
1347         cdr_sched = ast_sched_add(sched, 1, submit_scheduled_batch, NULL);
1348         /* signal the do_cdr thread to wakeup early and do some work (that lazy thread ;) */
1349         ast_mutex_lock(&cdr_pending_lock);
1350         ast_cond_signal(&cdr_pending_cond);
1351         ast_mutex_unlock(&cdr_pending_lock);
1352 }
1353
1354 void ast_cdr_detach(struct ast_cdr *cdr)
1355 {
1356         struct ast_cdr_batch_item *newtail;
1357         int curr;
1358
1359         if (!cdr)
1360                 return;
1361
1362         /* maybe they disabled CDR stuff completely, so just drop it */
1363         if (!enabled) {
1364                 ast_debug(1, "Dropping CDR !\n");
1365                 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1366                 ast_cdr_free(cdr);
1367                 return;
1368         }
1369
1370         /* post stuff immediately if we are not in batch mode, this is legacy behaviour */
1371         if (!batchmode) {
1372                 post_cdr(cdr);
1373                 ast_cdr_free(cdr);
1374                 return;
1375         }
1376
1377         /* otherwise, each CDR gets put into a batch list (at the end) */
1378         ast_debug(1, "CDR detaching from this thread\n");
1379
1380         /* we'll need a new tail for every CDR */
1381         if (!(newtail = ast_calloc(1, sizeof(*newtail)))) {
1382                 post_cdr(cdr);
1383                 ast_cdr_free(cdr);
1384                 return;
1385         }
1386
1387         /* don't traverse a whole list (just keep track of the tail) */
1388         ast_mutex_lock(&cdr_batch_lock);
1389         if (!batch)
1390                 init_batch();
1391         if (!batch->head) {
1392                 /* new batch is empty, so point the head at the new tail */
1393                 batch->head = newtail;
1394         } else {
1395                 /* already got a batch with something in it, so just append a new tail */
1396                 batch->tail->next = newtail;
1397         }
1398         newtail->cdr = cdr;
1399         batch->tail = newtail;
1400         curr = batch->size++;
1401         ast_mutex_unlock(&cdr_batch_lock);
1402
1403         /* if we have enough stuff to post, then do it */
1404         if (curr >= (batchsize - 1))
1405                 submit_unscheduled_batch();
1406 }
1407
1408 static void *do_cdr(void *data)
1409 {
1410         struct timespec timeout;
1411         int schedms;
1412         int numevents = 0;
1413
1414         for (;;) {
1415                 struct timeval now;
1416                 schedms = ast_sched_wait(sched);
1417                 /* this shouldn't happen, but provide a 1 second default just in case */
1418                 if (schedms <= 0)
1419                         schedms = 1000;
1420                 now = ast_tvadd(ast_tvnow(), ast_samp2tv(schedms, 1000));
1421                 timeout.tv_sec = now.tv_sec;
1422                 timeout.tv_nsec = now.tv_usec * 1000;
1423                 /* prevent stuff from clobbering cdr_pending_cond, then wait on signals sent to it until the timeout expires */
1424                 ast_mutex_lock(&cdr_pending_lock);
1425                 ast_cond_timedwait(&cdr_pending_cond, &cdr_pending_lock, &timeout);
1426                 numevents = ast_sched_runq(sched);
1427                 ast_mutex_unlock(&cdr_pending_lock);
1428                 ast_debug(2, "Processed %d scheduled CDR batches from the run queue\n", numevents);
1429         }
1430
1431         return NULL;
1432 }
1433
1434 static char *handle_cli_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1435 {
1436         struct ast_cdr_beitem *beitem=NULL;
1437         int cnt=0;
1438         long nextbatchtime=0;
1439
1440         switch (cmd) {
1441         case CLI_INIT:
1442                 e->command = "cdr show status";
1443                 e->usage =
1444                         "Usage: cdr show status\n"
1445                         "       Displays the Call Detail Record engine system status.\n";
1446                 return NULL;
1447         case CLI_GENERATE:
1448                 return NULL;
1449         }
1450
1451         if (a->argc > 3)
1452                 return CLI_SHOWUSAGE;
1453
1454         ast_cli(a->fd, "\n");
1455         ast_cli(a->fd, "Call Detail Record (CDR) settings\n");
1456         ast_cli(a->fd, "----------------------------------\n");
1457         ast_cli(a->fd, "  Logging:                    %s\n", enabled ? "Enabled" : "Disabled");
1458         ast_cli(a->fd, "  Mode:                       %s\n", batchmode ? "Batch" : "Simple");
1459         if (enabled) {
1460                 ast_cli(a->fd, "  Log unanswered calls:       %s\n", unanswered ? "Yes" : "No");
1461                 ast_cli(a->fd, "  Log congestion:             %s\n\n", congestion ? "Yes" : "No");
1462                 if (batchmode) {
1463                         ast_cli(a->fd, "* Batch Mode Settings\n");
1464                         ast_cli(a->fd, "  -------------------\n");
1465                         if (batch)
1466                                 cnt = batch->size;
1467                         if (cdr_sched > -1)
1468                                 nextbatchtime = ast_sched_when(sched, cdr_sched);
1469                         ast_cli(a->fd, "  Safe shutdown:              %s\n", batchsafeshutdown ? "Enabled" : "Disabled");
1470                         ast_cli(a->fd, "  Threading model:            %s\n", batchscheduleronly ? "Scheduler only" : "Scheduler plus separate threads");
1471                         ast_cli(a->fd, "  Current batch size:         %d record%s\n", cnt, ESS(cnt));
1472                         ast_cli(a->fd, "  Maximum batch size:         %d record%s\n", batchsize, ESS(batchsize));
1473                         ast_cli(a->fd, "  Maximum batch time:         %d second%s\n", batchtime, ESS(batchtime));
1474                         ast_cli(a->fd, "  Next batch processing time: %ld second%s\n\n", nextbatchtime, ESS(nextbatchtime));
1475                 }
1476                 ast_cli(a->fd, "* Registered Backends\n");
1477                 ast_cli(a->fd, "  -------------------\n");
1478                 AST_RWLIST_RDLOCK(&be_list);
1479                 if (AST_RWLIST_EMPTY(&be_list)) {
1480                         ast_cli(a->fd, "    (none)\n");
1481                 } else {
1482                         AST_RWLIST_TRAVERSE(&be_list, beitem, list) {
1483                                 ast_cli(a->fd, "    %s\n", beitem->name);
1484                         }
1485                 }
1486                 AST_RWLIST_UNLOCK(&be_list);
1487                 ast_cli(a->fd, "\n");
1488         }
1489
1490         return CLI_SUCCESS;
1491 }
1492
1493 static char *handle_cli_submit(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1494 {
1495         switch (cmd) {
1496         case CLI_INIT:
1497                 e->command = "cdr submit";
1498                 e->usage =
1499                         "Usage: cdr submit\n"
1500                         "       Posts all pending batched CDR data to the configured CDR backend engine modules.\n";
1501                 return NULL;
1502         case CLI_GENERATE:
1503                 return NULL;
1504         }
1505         if (a->argc > 2)
1506                 return CLI_SHOWUSAGE;
1507
1508         submit_unscheduled_batch();
1509         ast_cli(a->fd, "Submitted CDRs to backend engines for processing.  This may take a while.\n");
1510
1511         return CLI_SUCCESS;
1512 }
1513
1514 static struct ast_cli_entry cli_submit = AST_CLI_DEFINE(handle_cli_submit, "Posts all pending batched CDR data");
1515 static struct ast_cli_entry cli_status = AST_CLI_DEFINE(handle_cli_status, "Display the CDR status");
1516
1517 static int do_reload(int reload)
1518 {
1519         struct ast_config *config;
1520         const char *enabled_value;
1521         const char *unanswered_value;
1522         const char *congestion_value;
1523         const char *batched_value;
1524         const char *scheduleronly_value;
1525         const char *batchsafeshutdown_value;
1526         const char *size_value;
1527         const char *time_value;
1528         const char *end_before_h_value;
1529         const char *initiatedseconds_value;
1530         int cfg_size;
1531         int cfg_time;
1532         int was_enabled;
1533         int was_batchmode;
1534         int res=0;
1535         struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
1536
1537         if ((config = ast_config_load2("cdr.conf", "cdr", config_flags)) == CONFIG_STATUS_FILEUNCHANGED) {
1538                 return 0;
1539         }
1540
1541         ast_mutex_lock(&cdr_batch_lock);
1542
1543         was_enabled = enabled;
1544         was_batchmode = batchmode;
1545
1546         batchsize = BATCH_SIZE_DEFAULT;
1547         batchtime = BATCH_TIME_DEFAULT;
1548         batchscheduleronly = BATCH_SCHEDULER_ONLY_DEFAULT;
1549         batchsafeshutdown = BATCH_SAFE_SHUTDOWN_DEFAULT;
1550         enabled = ENABLED_DEFAULT;
1551         batchmode = BATCHMODE_DEFAULT;
1552         unanswered = UNANSWERED_DEFAULT;
1553         congestion = CONGESTION_DEFAULT;
1554
1555         if (config == CONFIG_STATUS_FILEMISSING || config == CONFIG_STATUS_FILEINVALID) {
1556                 ast_mutex_unlock(&cdr_batch_lock);
1557                 return 0;
1558         }
1559
1560         /* don't run the next scheduled CDR posting while reloading */
1561         AST_SCHED_DEL(sched, cdr_sched);
1562
1563         if (config) {
1564                 if ((enabled_value = ast_variable_retrieve(config, "general", "enable"))) {
1565                         enabled = ast_true(enabled_value);
1566                 }
1567                 if ((unanswered_value = ast_variable_retrieve(config, "general", "unanswered"))) {
1568                         unanswered = ast_true(unanswered_value);
1569                 }
1570                 if ((congestion_value = ast_variable_retrieve(config, "general", "congestion"))) {
1571                         ast_verb(1, "INTHEFACEPUNCH!\n");
1572                         congestion = ast_true(congestion_value);
1573                 }
1574                 if ((batched_value = ast_variable_retrieve(config, "general", "batch"))) {
1575                         batchmode = ast_true(batched_value);
1576                 }
1577                 if ((scheduleronly_value = ast_variable_retrieve(config, "general", "scheduleronly"))) {
1578                         batchscheduleronly = ast_true(scheduleronly_value);
1579                 }
1580                 if ((batchsafeshutdown_value = ast_variable_retrieve(config, "general", "safeshutdown"))) {
1581                         batchsafeshutdown = ast_true(batchsafeshutdown_value);
1582                 }
1583                 if ((size_value = ast_variable_retrieve(config, "general", "size"))) {
1584                         if (sscanf(size_value, "%30d", &cfg_size) < 1)
1585                                 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", size_value);
1586                         else if (cfg_size < 0)
1587                                 ast_log(LOG_WARNING, "Invalid maximum batch size '%d' specified, using default\n", cfg_size);
1588                         else
1589                                 batchsize = cfg_size;
1590                 }
1591                 if ((time_value = ast_variable_retrieve(config, "general", "time"))) {
1592                         if (sscanf(time_value, "%30d", &cfg_time) < 1)
1593                                 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", time_value);
1594                         else if (cfg_time < 0)
1595                                 ast_log(LOG_WARNING, "Invalid maximum batch time '%d' specified, using default\n", cfg_time);
1596                         else
1597                                 batchtime = cfg_time;
1598                 }
1599                 if ((end_before_h_value = ast_variable_retrieve(config, "general", "endbeforehexten")))
1600                         ast_set2_flag(&ast_options, ast_true(end_before_h_value), AST_OPT_FLAG_END_CDR_BEFORE_H_EXTEN);
1601                 if ((initiatedseconds_value = ast_variable_retrieve(config, "general", "initiatedseconds")))
1602                         ast_set2_flag(&ast_options, ast_true(initiatedseconds_value), AST_OPT_FLAG_INITIATED_SECONDS);
1603         }
1604
1605         if (enabled && !batchmode) {
1606                 ast_log(LOG_NOTICE, "CDR simple logging enabled.\n");
1607         } else if (enabled && batchmode) {
1608                 cdr_sched = ast_sched_add(sched, batchtime * 1000, submit_scheduled_batch, NULL);
1609                 ast_log(LOG_NOTICE, "CDR batch mode logging enabled, first of either size %d or time %d seconds.\n", batchsize, batchtime);
1610         } else {
1611                 ast_log(LOG_NOTICE, "CDR logging disabled, data will be lost.\n");
1612         }
1613
1614         /* if this reload enabled the CDR batch mode, create the background thread
1615            if it does not exist */
1616         if (enabled && batchmode && (!was_enabled || !was_batchmode) && (cdr_thread == AST_PTHREADT_NULL)) {
1617                 ast_cond_init(&cdr_pending_cond, NULL);
1618                 if (ast_pthread_create_background(&cdr_thread, NULL, do_cdr, NULL) < 0) {
1619                         ast_log(LOG_ERROR, "Unable to start CDR thread.\n");
1620                         AST_SCHED_DEL(sched, cdr_sched);
1621                 } else {
1622                         ast_cli_register(&cli_submit);
1623                         ast_register_atexit(ast_cdr_engine_term);
1624                         res = 0;
1625                 }
1626         /* if this reload disabled the CDR and/or batch mode and there is a background thread,
1627            kill it */
1628         } else if (((!enabled && was_enabled) || (!batchmode && was_batchmode)) && (cdr_thread != AST_PTHREADT_NULL)) {
1629                 /* wake up the thread so it will exit */
1630                 pthread_cancel(cdr_thread);
1631                 pthread_kill(cdr_thread, SIGURG);
1632                 pthread_join(cdr_thread, NULL);
1633                 cdr_thread = AST_PTHREADT_NULL;
1634                 ast_cond_destroy(&cdr_pending_cond);
1635                 ast_cli_unregister(&cli_submit);
1636                 ast_unregister_atexit(ast_cdr_engine_term);
1637                 res = 0;
1638                 /* if leaving batch mode, then post the CDRs in the batch,
1639                    and don't reschedule, since we are stopping CDR logging */
1640                 if (!batchmode && was_batchmode) {
1641                         ast_cdr_engine_term();
1642                 }
1643         } else {
1644                 res = 0;
1645         }
1646
1647         ast_mutex_unlock(&cdr_batch_lock);
1648         ast_config_destroy(config);
1649         manager_event(EVENT_FLAG_SYSTEM, "Reload", "Module: CDR\r\nMessage: CDR subsystem reload requested\r\n");
1650
1651         return res;
1652 }
1653
1654 int ast_cdr_engine_init(void)
1655 {
1656         int res;
1657
1658         sched = ast_sched_context_create();
1659         if (!sched) {
1660                 ast_log(LOG_ERROR, "Unable to create schedule context.\n");
1661                 return -1;
1662         }
1663
1664         ast_cli_register(&cli_status);
1665
1666         res = do_reload(0);
1667         if (res) {
1668                 ast_mutex_lock(&cdr_batch_lock);
1669                 res = init_batch();
1670                 ast_mutex_unlock(&cdr_batch_lock);
1671         }
1672
1673         return res;
1674 }
1675
1676 /* \note This actually gets called a couple of times at shutdown.  Once, before we start
1677    hanging up channels, and then again, after the channel hangup timeout expires */
1678 void ast_cdr_engine_term(void)
1679 {
1680         ast_cdr_submit_batch(batchsafeshutdown);
1681 }
1682
1683 int ast_cdr_engine_reload(void)
1684 {
1685         return do_reload(1);
1686 }
1687
1688 int ast_cdr_data_add_structure(struct ast_data *tree, struct ast_cdr *cdr, int recur)
1689 {
1690         struct ast_cdr *tmpcdr;
1691         struct ast_data *level;
1692         struct ast_var_t *variables;
1693         const char *var, *val;
1694         int x = 1, i;
1695         char workspace[256];
1696         char *tmp;
1697
1698         if (!cdr) {
1699                 return -1;
1700         }
1701
1702         for (tmpcdr = cdr; tmpcdr; tmpcdr = (recur ? tmpcdr->next : NULL)) {
1703                 level = ast_data_add_node(tree, "level");
1704                 if (!level) {
1705                         continue;
1706                 }
1707
1708                 ast_data_add_int(level, "level_number", x);
1709
1710                 AST_LIST_TRAVERSE(&tmpcdr->varshead, variables, entries) {
1711                         if (variables && (var = ast_var_name(variables)) &&
1712                                         (val = ast_var_value(variables)) && !ast_strlen_zero(var)
1713                                         && !ast_strlen_zero(val)) {
1714                                 ast_data_add_str(level, var, val);
1715                         } else {
1716                                 break;
1717                         }
1718                 }
1719
1720                 for (i = 0; cdr_readonly_vars[i]; i++) {
1721                         workspace[0] = 0; /* null out the workspace, because the cdr_get_tv() won't write anything if time is NULL, so you get old vals */
1722                         ast_cdr_getvar(tmpcdr, cdr_readonly_vars[i], &tmp, workspace, sizeof(workspace), 0, 0);
1723                         if (!tmp) {
1724                                 continue;
1725                         }
1726                         ast_data_add_str(level, cdr_readonly_vars[i], tmp);
1727                 }
1728
1729                 x++;
1730         }
1731
1732         return 0;
1733 }
1734