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