Removes colorful verb statements erroneously commited with r332760
[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         /* if congestion log is disabled, pass the buck to ast_cdr_failed */
792         if (!congestion) {
793                 ast_cdr_failed(cdr);
794         }
795
796         while (cdr && congestion) {
797                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
798                         chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
799
800                         if (ast_test_flag(cdr, AST_CDR_FLAG_POSTED)) {
801                                 ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
802                         }
803
804                         if (cdr->disposition < AST_CDR_CONGESTION) {
805                                 cdr->disposition = AST_CDR_CONGESTION;
806                         }
807                 }
808                 cdr = cdr->next;
809         }
810 }
811
812 /* everywhere ast_cdr_disposition is called, it will call ast_cdr_failed()
813    if ast_cdr_disposition returns a non-zero value */
814
815 int ast_cdr_disposition(struct ast_cdr *cdr, int cause)
816 {
817         int res = 0;
818
819         for (; cdr; cdr = cdr->next) {
820                 switch (cause) {  /* handle all the non failure, busy cases, return 0 not to set disposition,
821                                                         return -1 to set disposition to FAILED */
822                 case AST_CAUSE_BUSY:
823                         ast_cdr_busy(cdr);
824                         break;
825                 case AST_CAUSE_NO_ANSWER:
826                         ast_cdr_noanswer(cdr);
827                         break;
828                 case AST_CAUSE_NORMAL_CIRCUIT_CONGESTION:
829                         ast_cdr_congestion(cdr);
830                         break;
831                 case AST_CAUSE_NORMAL:
832                         break;
833                 default:
834                         res = -1;
835                 }
836         }
837         return res;
838 }
839
840 void ast_cdr_setdestchan(struct ast_cdr *cdr, const char *chann)
841 {
842         for (; cdr; cdr = cdr->next) {
843                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
844                         check_post(cdr);
845                         ast_copy_string(cdr->dstchannel, chann, sizeof(cdr->dstchannel));
846                 }
847         }
848 }
849
850 void ast_cdr_setapp(struct ast_cdr *cdr, const char *app, const char *data)
851 {
852
853         for (; cdr; cdr = cdr->next) {
854                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
855                         check_post(cdr);
856                         ast_copy_string(cdr->lastapp, S_OR(app, ""), sizeof(cdr->lastapp));
857                         ast_copy_string(cdr->lastdata, S_OR(data, ""), sizeof(cdr->lastdata));
858                 }
859         }
860 }
861
862 void ast_cdr_setanswer(struct ast_cdr *cdr, struct timeval t)
863 {
864
865         for (; cdr; cdr = cdr->next) {
866                 if (ast_test_flag(cdr, AST_CDR_FLAG_ANSLOCKED))
867                         continue;
868                 if (ast_test_flag(cdr, AST_CDR_FLAG_DONT_TOUCH) && ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
869                         continue;
870                 check_post(cdr);
871                 cdr->answer = t;
872         }
873 }
874
875 void ast_cdr_setdisposition(struct ast_cdr *cdr, long int disposition)
876 {
877
878         for (; cdr; cdr = cdr->next) {
879                 if (ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
880                         continue;
881                 check_post(cdr);
882                 cdr->disposition = disposition;
883         }
884 }
885
886 /* set cid info for one record */
887 static void set_one_cid(struct ast_cdr *cdr, struct ast_channel *c)
888 {
889         const char *num;
890
891         if (!cdr) {
892                 return;
893         }
894
895         /* Grab source from ANI or normal Caller*ID */
896         num = S_COR(c->caller.ani.number.valid, c->caller.ani.number.str,
897                 S_COR(c->caller.id.number.valid, c->caller.id.number.str, NULL));
898         ast_callerid_merge(cdr->clid, sizeof(cdr->clid),
899                 S_COR(c->caller.id.name.valid, c->caller.id.name.str, NULL), num, "");
900         ast_copy_string(cdr->src, S_OR(num, ""), sizeof(cdr->src));
901         ast_cdr_setvar(cdr, "dnid", S_OR(c->dialed.number.str, ""), 0);
902
903         if (c->caller.id.subaddress.valid) {
904                 ast_cdr_setvar(cdr, "callingsubaddr", S_OR(c->caller.id.subaddress.str, ""), 0);
905         }
906         if (c->dialed.subaddress.valid) {
907                 ast_cdr_setvar(cdr, "calledsubaddr", S_OR(c->dialed.subaddress.str, ""), 0);
908         }
909 }
910
911 int ast_cdr_setcid(struct ast_cdr *cdr, struct ast_channel *c)
912 {
913         for (; cdr; cdr = cdr->next) {
914                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
915                         set_one_cid(cdr, c);
916         }
917         return 0;
918 }
919
920 static int cdr_seq_inc(struct ast_cdr *cdr)
921 {
922         return (cdr->sequence = ast_atomic_fetchadd_int(&cdr_sequence, +1));
923 }
924
925 int ast_cdr_init(struct ast_cdr *cdr, struct ast_channel *c)
926 {
927         for ( ; cdr ; cdr = cdr->next) {
928                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
929                         ast_copy_string(cdr->channel, c->name, sizeof(cdr->channel));
930                         set_one_cid(cdr, c);
931                         cdr_seq_inc(cdr);
932
933                         cdr->disposition = (c->_state == AST_STATE_UP) ?  AST_CDR_ANSWERED : AST_CDR_NOANSWER;
934                         cdr->amaflags = c->amaflags ? c->amaflags :  ast_default_amaflags;
935                         ast_copy_string(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode));
936                         ast_copy_string(cdr->peeraccount, c->peeraccount, sizeof(cdr->peeraccount));
937                         /* Destination information */
938                         ast_copy_string(cdr->dst, S_OR(c->macroexten,c->exten), sizeof(cdr->dst));
939                         ast_copy_string(cdr->dcontext, S_OR(c->macrocontext,c->context), sizeof(cdr->dcontext));
940                         /* Unique call identifier */
941                         ast_copy_string(cdr->uniqueid, c->uniqueid, sizeof(cdr->uniqueid));
942                         /* Linked call identifier */
943                         ast_copy_string(cdr->linkedid, c->linkedid, sizeof(cdr->linkedid));
944                 }
945         }
946         return 0;
947 }
948
949 /* Three routines were "fixed" via 10668, and later shown that
950    users were depending on this behavior. ast_cdr_end,
951    ast_cdr_setvar and ast_cdr_answer are the three routines.
952    While most of the other routines would not touch
953    LOCKED cdr's, these three routines were designed to
954    operate on locked CDR's as a matter of course.
955    I now appreciate how this plays with the ForkCDR app,
956    which forms these cdr chains in the first place.
957    cdr_end is pretty key: all cdrs created are closed
958    together. They only vary by start time. Arithmetically,
959    users can calculate the subintervals they wish to track. */
960
961 void ast_cdr_end(struct ast_cdr *cdr)
962 {
963         for ( ; cdr ; cdr = cdr->next) {
964                 if (ast_test_flag(cdr, AST_CDR_FLAG_DONT_TOUCH) && ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
965                         continue;
966                 check_post(cdr);
967                 if (ast_tvzero(cdr->end))
968                         cdr->end = ast_tvnow();
969                 if (ast_tvzero(cdr->start)) {
970                         ast_log(LOG_WARNING, "CDR on channel '%s' has not started\n", S_OR(cdr->channel, "<unknown>"));
971                         cdr->disposition = AST_CDR_FAILED;
972                 } else
973                         cdr->duration = cdr->end.tv_sec - cdr->start.tv_sec;
974                 if (ast_tvzero(cdr->answer)) {
975                         if (cdr->disposition == AST_CDR_ANSWERED) {
976                                 ast_log(LOG_WARNING, "CDR on channel '%s' has no answer time but is 'ANSWERED'\n", S_OR(cdr->channel, "<unknown>"));
977                                 cdr->disposition = AST_CDR_FAILED;
978                         }
979                 } else {
980                         cdr->billsec = cdr->end.tv_sec - cdr->answer.tv_sec;
981                         if (ast_test_flag(&ast_options, AST_OPT_FLAG_INITIATED_SECONDS))
982                                 cdr->billsec += cdr->end.tv_usec > cdr->answer.tv_usec ? 1 : 0;
983                 }
984         }
985 }
986
987 char *ast_cdr_disp2str(int disposition)
988 {
989         switch (disposition) {
990         case AST_CDR_NULL:
991                 return "NO ANSWER"; /* by default, for backward compatibility */
992         case AST_CDR_NOANSWER:
993                 return "NO ANSWER";
994         case AST_CDR_FAILED:
995                 return "FAILED";
996         case AST_CDR_BUSY:
997                 return "BUSY";
998         case AST_CDR_ANSWERED:
999                 return "ANSWERED";
1000         case AST_CDR_CONGESTION:
1001                 return "CONGESTION";
1002         }
1003         return "UNKNOWN";
1004 }
1005
1006 /*! Converts AMA flag to printable string */
1007 char *ast_cdr_flags2str(int flag)
1008 {
1009         switch (flag) {
1010         case AST_CDR_OMIT:
1011                 return "OMIT";
1012         case AST_CDR_BILLING:
1013                 return "BILLING";
1014         case AST_CDR_DOCUMENTATION:
1015                 return "DOCUMENTATION";
1016         }
1017         return "Unknown";
1018 }
1019
1020 int ast_cdr_setaccount(struct ast_channel *chan, const char *account)
1021 {
1022         struct ast_cdr *cdr = chan->cdr;
1023         const char *old_acct = "";
1024
1025         if (!ast_strlen_zero(chan->accountcode)) {
1026                 old_acct = ast_strdupa(chan->accountcode);
1027         }
1028
1029         ast_string_field_set(chan, accountcode, account);
1030         for ( ; cdr ; cdr = cdr->next) {
1031                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1032                         ast_copy_string(cdr->accountcode, chan->accountcode, sizeof(cdr->accountcode));
1033                 }
1034         }
1035
1036         ast_manager_event(chan, EVENT_FLAG_CALL, "NewAccountCode",
1037                         "Channel: %s\r\n"
1038                         "Uniqueid: %s\r\n"
1039                         "AccountCode: %s\r\n"
1040                         "OldAccountCode: %s\r\n",
1041                         chan->name, chan->uniqueid, chan->accountcode, old_acct);
1042
1043         return 0;
1044 }
1045
1046 int ast_cdr_setpeeraccount(struct ast_channel *chan, const char *account)
1047 {
1048         struct ast_cdr *cdr = chan->cdr;
1049         const char *old_acct = "";
1050
1051         if (!ast_strlen_zero(chan->peeraccount)) {
1052                 old_acct = ast_strdupa(chan->peeraccount);
1053         }
1054
1055         ast_string_field_set(chan, peeraccount, account);
1056         for ( ; cdr ; cdr = cdr->next) {
1057                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1058                         ast_copy_string(cdr->peeraccount, chan->peeraccount, sizeof(cdr->peeraccount));
1059                 }
1060         }
1061
1062         ast_manager_event(chan, EVENT_FLAG_CALL, "NewPeerAccount",
1063                         "Channel: %s\r\n"
1064                         "Uniqueid: %s\r\n"
1065                         "PeerAccount: %s\r\n"
1066                         "OldPeerAccount: %s\r\n",
1067                         chan->name, chan->uniqueid, chan->peeraccount, old_acct);
1068
1069         return 0;
1070 }
1071
1072 int ast_cdr_setamaflags(struct ast_channel *chan, const char *flag)
1073 {
1074         struct ast_cdr *cdr;
1075         int newflag = ast_cdr_amaflags2int(flag);
1076         if (newflag) {
1077                 for (cdr = chan->cdr; cdr; cdr = cdr->next) {
1078                         if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1079                                 cdr->amaflags = newflag;
1080                         }
1081                 }
1082         }
1083
1084         return 0;
1085 }
1086
1087 int ast_cdr_setuserfield(struct ast_channel *chan, const char *userfield)
1088 {
1089         struct ast_cdr *cdr = chan->cdr;
1090
1091         for ( ; cdr ; cdr = cdr->next) {
1092                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
1093                         ast_copy_string(cdr->userfield, userfield, sizeof(cdr->userfield));
1094         }
1095
1096         return 0;
1097 }
1098
1099 int ast_cdr_appenduserfield(struct ast_channel *chan, const char *userfield)
1100 {
1101         struct ast_cdr *cdr = chan->cdr;
1102
1103         for ( ; cdr ; cdr = cdr->next) {
1104                 int len = strlen(cdr->userfield);
1105
1106                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
1107                         ast_copy_string(cdr->userfield + len, userfield, sizeof(cdr->userfield) - len);
1108         }
1109
1110         return 0;
1111 }
1112
1113 int ast_cdr_update(struct ast_channel *c)
1114 {
1115         struct ast_cdr *cdr = c->cdr;
1116
1117         for ( ; cdr ; cdr = cdr->next) {
1118                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1119                         set_one_cid(cdr, c);
1120
1121                         /* Copy account code et-al */
1122                         ast_copy_string(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode));
1123                         ast_copy_string(cdr->peeraccount, c->peeraccount, sizeof(cdr->peeraccount));
1124                         ast_copy_string(cdr->linkedid, c->linkedid, sizeof(cdr->linkedid));
1125
1126                         /* Destination information */ /* XXX privilege macro* ? */
1127                         ast_copy_string(cdr->dst, S_OR(c->macroexten, c->exten), sizeof(cdr->dst));
1128                         ast_copy_string(cdr->dcontext, S_OR(c->macrocontext, c->context), sizeof(cdr->dcontext));
1129                 }
1130         }
1131
1132         return 0;
1133 }
1134
1135 int ast_cdr_amaflags2int(const char *flag)
1136 {
1137         if (!strcasecmp(flag, "default"))
1138                 return 0;
1139         if (!strcasecmp(flag, "omit"))
1140                 return AST_CDR_OMIT;
1141         if (!strcasecmp(flag, "billing"))
1142                 return AST_CDR_BILLING;
1143         if (!strcasecmp(flag, "documentation"))
1144                 return AST_CDR_DOCUMENTATION;
1145         return -1;
1146 }
1147
1148 static void post_cdr(struct ast_cdr *cdr)
1149 {
1150         struct ast_cdr_beitem *i;
1151
1152         for ( ; cdr ; cdr = cdr->next) {
1153                 if (!unanswered && cdr->disposition < AST_CDR_ANSWERED && (ast_strlen_zero(cdr->channel) || ast_strlen_zero(cdr->dstchannel))) {
1154                         /* For people, who don't want to see unanswered single-channel events */
1155                         ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1156                         continue;
1157                 }
1158
1159                 /* don't post CDRs that are for dialed channels unless those
1160                  * channels were originated from asterisk (pbx_spool, manager,
1161                  * cli) */
1162                 if (ast_test_flag(cdr, AST_CDR_FLAG_DIALED) && !ast_test_flag(cdr, AST_CDR_FLAG_ORIGINATED)) {
1163                         ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1164                         continue;
1165                 }
1166
1167                 check_post(cdr);
1168                 ast_set_flag(cdr, AST_CDR_FLAG_POSTED);
1169                 if (ast_test_flag(cdr, AST_CDR_FLAG_POST_DISABLED))
1170                         continue;
1171                 AST_RWLIST_RDLOCK(&be_list);
1172                 AST_RWLIST_TRAVERSE(&be_list, i, list) {
1173                         i->be(cdr);
1174                 }
1175                 AST_RWLIST_UNLOCK(&be_list);
1176         }
1177 }
1178
1179 void ast_cdr_reset(struct ast_cdr *cdr, struct ast_flags *_flags)
1180 {
1181         struct ast_cdr *duplicate;
1182         struct ast_flags flags = { 0 };
1183
1184         if (_flags)
1185                 ast_copy_flags(&flags, _flags, AST_FLAGS_ALL);
1186
1187         for ( ; cdr ; cdr = cdr->next) {
1188                 /* Detach if post is requested */
1189                 if (ast_test_flag(&flags, AST_CDR_FLAG_LOCKED) || !ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1190                         if (ast_test_flag(&flags, AST_CDR_FLAG_POSTED)) {
1191                                 ast_cdr_end(cdr);
1192                                 if ((duplicate = ast_cdr_dup_unique_swap(cdr))) {
1193                                         ast_cdr_detach(duplicate);
1194                                 }
1195                                 ast_set_flag(cdr, AST_CDR_FLAG_POSTED);
1196                         }
1197
1198                         /* enable CDR only */
1199                         if (ast_test_flag(&flags, AST_CDR_FLAG_POST_ENABLE)) {
1200                                 ast_clear_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1201                                 continue;
1202                         }
1203
1204                         /* clear variables */
1205                         if (!ast_test_flag(&flags, AST_CDR_FLAG_KEEP_VARS)) {
1206                                 ast_cdr_free_vars(cdr, 0);
1207                         }
1208
1209                         /* Reset to initial state */
1210                         ast_clear_flag(cdr, AST_FLAGS_ALL);
1211                         memset(&cdr->start, 0, sizeof(cdr->start));
1212                         memset(&cdr->end, 0, sizeof(cdr->end));
1213                         memset(&cdr->answer, 0, sizeof(cdr->answer));
1214                         cdr->billsec = 0;
1215                         cdr->duration = 0;
1216                         ast_cdr_start(cdr);
1217                         cdr->disposition = AST_CDR_NOANSWER;
1218                 }
1219         }
1220 }
1221
1222 void ast_cdr_specialized_reset(struct ast_cdr *cdr, struct ast_flags *_flags)
1223 {
1224         struct ast_flags flags = { 0 };
1225
1226         if (_flags)
1227                 ast_copy_flags(&flags, _flags, AST_FLAGS_ALL);
1228
1229         /* Reset to initial state */
1230         if (ast_test_flag(cdr, AST_CDR_FLAG_POST_DISABLED)) { /* But do NOT lose the NoCDR() setting */
1231                 ast_clear_flag(cdr, AST_FLAGS_ALL);
1232                 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1233         } else {
1234                 ast_clear_flag(cdr, AST_FLAGS_ALL);
1235         }
1236
1237         memset(&cdr->start, 0, sizeof(cdr->start));
1238         memset(&cdr->end, 0, sizeof(cdr->end));
1239         memset(&cdr->answer, 0, sizeof(cdr->answer));
1240         cdr->billsec = 0;
1241         cdr->duration = 0;
1242         ast_cdr_start(cdr);
1243         cdr->disposition = AST_CDR_NULL;
1244 }
1245
1246 struct ast_cdr *ast_cdr_append(struct ast_cdr *cdr, struct ast_cdr *newcdr)
1247 {
1248         struct ast_cdr *ret;
1249
1250         if (cdr) {
1251                 ret = cdr;
1252
1253                 while (cdr->next)
1254                         cdr = cdr->next;
1255                 cdr->next = newcdr;
1256         } else {
1257                 ret = newcdr;
1258         }
1259
1260         return ret;
1261 }
1262
1263 /*! \note Don't call without cdr_batch_lock */
1264 static void reset_batch(void)
1265 {
1266         batch->size = 0;
1267         batch->head = NULL;
1268         batch->tail = NULL;
1269 }
1270
1271 /*! \note Don't call without cdr_batch_lock */
1272 static int init_batch(void)
1273 {
1274         /* This is the single meta-batch used to keep track of all CDRs during the entire life of the program */
1275         if (!(batch = ast_malloc(sizeof(*batch))))
1276                 return -1;
1277
1278         reset_batch();
1279
1280         return 0;
1281 }
1282
1283 static void *do_batch_backend_process(void *data)
1284 {
1285         struct ast_cdr_batch_item *processeditem;
1286         struct ast_cdr_batch_item *batchitem = data;
1287
1288         /* Push each CDR into storage mechanism(s) and free all the memory */
1289         while (batchitem) {
1290                 post_cdr(batchitem->cdr);
1291                 ast_cdr_free(batchitem->cdr);
1292                 processeditem = batchitem;
1293                 batchitem = batchitem->next;
1294                 ast_free(processeditem);
1295         }
1296
1297         return NULL;
1298 }
1299
1300 void ast_cdr_submit_batch(int do_shutdown)
1301 {
1302         struct ast_cdr_batch_item *oldbatchitems = NULL;
1303         pthread_t batch_post_thread = AST_PTHREADT_NULL;
1304
1305         /* if there's no batch, or no CDRs in the batch, then there's nothing to do */
1306         if (!batch || !batch->head)
1307                 return;
1308
1309         /* move the old CDRs aside, and prepare a new CDR batch */
1310         ast_mutex_lock(&cdr_batch_lock);
1311         oldbatchitems = batch->head;
1312         reset_batch();
1313         ast_mutex_unlock(&cdr_batch_lock);
1314
1315         /* if configured, spawn a new thread to post these CDRs,
1316            also try to save as much as possible if we are shutting down safely */
1317         if (batchscheduleronly || do_shutdown) {
1318                 ast_debug(1, "CDR single-threaded batch processing begins now\n");
1319                 do_batch_backend_process(oldbatchitems);
1320         } else {
1321                 if (ast_pthread_create_detached_background(&batch_post_thread, NULL, do_batch_backend_process, oldbatchitems)) {
1322                         ast_log(LOG_WARNING, "CDR processing thread could not detach, now trying in this thread\n");
1323                         do_batch_backend_process(oldbatchitems);
1324                 } else {
1325                         ast_debug(1, "CDR multi-threaded batch processing begins now\n");
1326                 }
1327         }
1328 }
1329
1330 static int submit_scheduled_batch(const void *data)
1331 {
1332         ast_cdr_submit_batch(0);
1333         /* manually reschedule from this point in time */
1334         cdr_sched = ast_sched_add(sched, batchtime * 1000, submit_scheduled_batch, NULL);
1335         /* returning zero so the scheduler does not automatically reschedule */
1336         return 0;
1337 }
1338
1339 static void submit_unscheduled_batch(void)
1340 {
1341         /* this is okay since we are not being called from within the scheduler */
1342         AST_SCHED_DEL(sched, cdr_sched);
1343         /* schedule the submission to occur ASAP (1 ms) */
1344         cdr_sched = ast_sched_add(sched, 1, submit_scheduled_batch, NULL);
1345         /* signal the do_cdr thread to wakeup early and do some work (that lazy thread ;) */
1346         ast_mutex_lock(&cdr_pending_lock);
1347         ast_cond_signal(&cdr_pending_cond);
1348         ast_mutex_unlock(&cdr_pending_lock);
1349 }
1350
1351 void ast_cdr_detach(struct ast_cdr *cdr)
1352 {
1353         struct ast_cdr_batch_item *newtail;
1354         int curr;
1355
1356         if (!cdr)
1357                 return;
1358
1359         /* maybe they disabled CDR stuff completely, so just drop it */
1360         if (!enabled) {
1361                 ast_debug(1, "Dropping CDR !\n");
1362                 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1363                 ast_cdr_free(cdr);
1364                 return;
1365         }
1366
1367         /* post stuff immediately if we are not in batch mode, this is legacy behaviour */
1368         if (!batchmode) {
1369                 post_cdr(cdr);
1370                 ast_cdr_free(cdr);
1371                 return;
1372         }
1373
1374         /* otherwise, each CDR gets put into a batch list (at the end) */
1375         ast_debug(1, "CDR detaching from this thread\n");
1376
1377         /* we'll need a new tail for every CDR */
1378         if (!(newtail = ast_calloc(1, sizeof(*newtail)))) {
1379                 post_cdr(cdr);
1380                 ast_cdr_free(cdr);
1381                 return;
1382         }
1383
1384         /* don't traverse a whole list (just keep track of the tail) */
1385         ast_mutex_lock(&cdr_batch_lock);
1386         if (!batch)
1387                 init_batch();
1388         if (!batch->head) {
1389                 /* new batch is empty, so point the head at the new tail */
1390                 batch->head = newtail;
1391         } else {
1392                 /* already got a batch with something in it, so just append a new tail */
1393                 batch->tail->next = newtail;
1394         }
1395         newtail->cdr = cdr;
1396         batch->tail = newtail;
1397         curr = batch->size++;
1398         ast_mutex_unlock(&cdr_batch_lock);
1399
1400         /* if we have enough stuff to post, then do it */
1401         if (curr >= (batchsize - 1))
1402                 submit_unscheduled_batch();
1403 }
1404
1405 static void *do_cdr(void *data)
1406 {
1407         struct timespec timeout;
1408         int schedms;
1409         int numevents = 0;
1410
1411         for (;;) {
1412                 struct timeval now;
1413                 schedms = ast_sched_wait(sched);
1414                 /* this shouldn't happen, but provide a 1 second default just in case */
1415                 if (schedms <= 0)
1416                         schedms = 1000;
1417                 now = ast_tvadd(ast_tvnow(), ast_samp2tv(schedms, 1000));
1418                 timeout.tv_sec = now.tv_sec;
1419                 timeout.tv_nsec = now.tv_usec * 1000;
1420                 /* prevent stuff from clobbering cdr_pending_cond, then wait on signals sent to it until the timeout expires */
1421                 ast_mutex_lock(&cdr_pending_lock);
1422                 ast_cond_timedwait(&cdr_pending_cond, &cdr_pending_lock, &timeout);
1423                 numevents = ast_sched_runq(sched);
1424                 ast_mutex_unlock(&cdr_pending_lock);
1425                 ast_debug(2, "Processed %d scheduled CDR batches from the run queue\n", numevents);
1426         }
1427
1428         return NULL;
1429 }
1430
1431 static char *handle_cli_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1432 {
1433         struct ast_cdr_beitem *beitem=NULL;
1434         int cnt=0;
1435         long nextbatchtime=0;
1436
1437         switch (cmd) {
1438         case CLI_INIT:
1439                 e->command = "cdr show status";
1440                 e->usage =
1441                         "Usage: cdr show status\n"
1442                         "       Displays the Call Detail Record engine system status.\n";
1443                 return NULL;
1444         case CLI_GENERATE:
1445                 return NULL;
1446         }
1447
1448         if (a->argc > 3)
1449                 return CLI_SHOWUSAGE;
1450
1451         ast_cli(a->fd, "\n");
1452         ast_cli(a->fd, "Call Detail Record (CDR) settings\n");
1453         ast_cli(a->fd, "----------------------------------\n");
1454         ast_cli(a->fd, "  Logging:                    %s\n", enabled ? "Enabled" : "Disabled");
1455         ast_cli(a->fd, "  Mode:                       %s\n", batchmode ? "Batch" : "Simple");
1456         if (enabled) {
1457                 ast_cli(a->fd, "  Log unanswered calls:       %s\n", unanswered ? "Yes" : "No");
1458                 ast_cli(a->fd, "  Log congestion:             %s\n\n", congestion ? "Yes" : "No");
1459                 if (batchmode) {
1460                         ast_cli(a->fd, "* Batch Mode Settings\n");
1461                         ast_cli(a->fd, "  -------------------\n");
1462                         if (batch)
1463                                 cnt = batch->size;
1464                         if (cdr_sched > -1)
1465                                 nextbatchtime = ast_sched_when(sched, cdr_sched);
1466                         ast_cli(a->fd, "  Safe shutdown:              %s\n", batchsafeshutdown ? "Enabled" : "Disabled");
1467                         ast_cli(a->fd, "  Threading model:            %s\n", batchscheduleronly ? "Scheduler only" : "Scheduler plus separate threads");
1468                         ast_cli(a->fd, "  Current batch size:         %d record%s\n", cnt, ESS(cnt));
1469                         ast_cli(a->fd, "  Maximum batch size:         %d record%s\n", batchsize, ESS(batchsize));
1470                         ast_cli(a->fd, "  Maximum batch time:         %d second%s\n", batchtime, ESS(batchtime));
1471                         ast_cli(a->fd, "  Next batch processing time: %ld second%s\n\n", nextbatchtime, ESS(nextbatchtime));
1472                 }
1473                 ast_cli(a->fd, "* Registered Backends\n");
1474                 ast_cli(a->fd, "  -------------------\n");
1475                 AST_RWLIST_RDLOCK(&be_list);
1476                 if (AST_RWLIST_EMPTY(&be_list)) {
1477                         ast_cli(a->fd, "    (none)\n");
1478                 } else {
1479                         AST_RWLIST_TRAVERSE(&be_list, beitem, list) {
1480                                 ast_cli(a->fd, "    %s\n", beitem->name);
1481                         }
1482                 }
1483                 AST_RWLIST_UNLOCK(&be_list);
1484                 ast_cli(a->fd, "\n");
1485         }
1486
1487         return CLI_SUCCESS;
1488 }
1489
1490 static char *handle_cli_submit(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1491 {
1492         switch (cmd) {
1493         case CLI_INIT:
1494                 e->command = "cdr submit";
1495                 e->usage =
1496                         "Usage: cdr submit\n"
1497                         "       Posts all pending batched CDR data to the configured CDR backend engine modules.\n";
1498                 return NULL;
1499         case CLI_GENERATE:
1500                 return NULL;
1501         }
1502         if (a->argc > 2)
1503                 return CLI_SHOWUSAGE;
1504
1505         submit_unscheduled_batch();
1506         ast_cli(a->fd, "Submitted CDRs to backend engines for processing.  This may take a while.\n");
1507
1508         return CLI_SUCCESS;
1509 }
1510
1511 static struct ast_cli_entry cli_submit = AST_CLI_DEFINE(handle_cli_submit, "Posts all pending batched CDR data");
1512 static struct ast_cli_entry cli_status = AST_CLI_DEFINE(handle_cli_status, "Display the CDR status");
1513
1514 static int do_reload(int reload)
1515 {
1516         struct ast_config *config;
1517         const char *enabled_value;
1518         const char *unanswered_value;
1519         const char *congestion_value;
1520         const char *batched_value;
1521         const char *scheduleronly_value;
1522         const char *batchsafeshutdown_value;
1523         const char *size_value;
1524         const char *time_value;
1525         const char *end_before_h_value;
1526         const char *initiatedseconds_value;
1527         int cfg_size;
1528         int cfg_time;
1529         int was_enabled;
1530         int was_batchmode;
1531         int res=0;
1532         struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
1533
1534         if ((config = ast_config_load2("cdr.conf", "cdr", config_flags)) == CONFIG_STATUS_FILEUNCHANGED) {
1535                 return 0;
1536         }
1537
1538         ast_mutex_lock(&cdr_batch_lock);
1539
1540         was_enabled = enabled;
1541         was_batchmode = batchmode;
1542
1543         batchsize = BATCH_SIZE_DEFAULT;
1544         batchtime = BATCH_TIME_DEFAULT;
1545         batchscheduleronly = BATCH_SCHEDULER_ONLY_DEFAULT;
1546         batchsafeshutdown = BATCH_SAFE_SHUTDOWN_DEFAULT;
1547         enabled = ENABLED_DEFAULT;
1548         batchmode = BATCHMODE_DEFAULT;
1549         unanswered = UNANSWERED_DEFAULT;
1550         congestion = CONGESTION_DEFAULT;
1551
1552         if (config == CONFIG_STATUS_FILEMISSING || config == CONFIG_STATUS_FILEINVALID) {
1553                 ast_mutex_unlock(&cdr_batch_lock);
1554                 return 0;
1555         }
1556
1557         /* don't run the next scheduled CDR posting while reloading */
1558         AST_SCHED_DEL(sched, cdr_sched);
1559
1560         if (config) {
1561                 if ((enabled_value = ast_variable_retrieve(config, "general", "enable"))) {
1562                         enabled = ast_true(enabled_value);
1563                 }
1564                 if ((unanswered_value = ast_variable_retrieve(config, "general", "unanswered"))) {
1565                         unanswered = ast_true(unanswered_value);
1566                 }
1567                 if ((congestion_value = ast_variable_retrieve(config, "general", "congestion"))) {
1568                         congestion = ast_true(congestion_value);
1569                 }
1570                 if ((batched_value = ast_variable_retrieve(config, "general", "batch"))) {
1571                         batchmode = ast_true(batched_value);
1572                 }
1573                 if ((scheduleronly_value = ast_variable_retrieve(config, "general", "scheduleronly"))) {
1574                         batchscheduleronly = ast_true(scheduleronly_value);
1575                 }
1576                 if ((batchsafeshutdown_value = ast_variable_retrieve(config, "general", "safeshutdown"))) {
1577                         batchsafeshutdown = ast_true(batchsafeshutdown_value);
1578                 }
1579                 if ((size_value = ast_variable_retrieve(config, "general", "size"))) {
1580                         if (sscanf(size_value, "%30d", &cfg_size) < 1)
1581                                 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", size_value);
1582                         else if (cfg_size < 0)
1583                                 ast_log(LOG_WARNING, "Invalid maximum batch size '%d' specified, using default\n", cfg_size);
1584                         else
1585                                 batchsize = cfg_size;
1586                 }
1587                 if ((time_value = ast_variable_retrieve(config, "general", "time"))) {
1588                         if (sscanf(time_value, "%30d", &cfg_time) < 1)
1589                                 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", time_value);
1590                         else if (cfg_time < 0)
1591                                 ast_log(LOG_WARNING, "Invalid maximum batch time '%d' specified, using default\n", cfg_time);
1592                         else
1593                                 batchtime = cfg_time;
1594                 }
1595                 if ((end_before_h_value = ast_variable_retrieve(config, "general", "endbeforehexten")))
1596                         ast_set2_flag(&ast_options, ast_true(end_before_h_value), AST_OPT_FLAG_END_CDR_BEFORE_H_EXTEN);
1597                 if ((initiatedseconds_value = ast_variable_retrieve(config, "general", "initiatedseconds")))
1598                         ast_set2_flag(&ast_options, ast_true(initiatedseconds_value), AST_OPT_FLAG_INITIATED_SECONDS);
1599         }
1600
1601         if (enabled && !batchmode) {
1602                 ast_log(LOG_NOTICE, "CDR simple logging enabled.\n");
1603         } else if (enabled && batchmode) {
1604                 cdr_sched = ast_sched_add(sched, batchtime * 1000, submit_scheduled_batch, NULL);
1605                 ast_log(LOG_NOTICE, "CDR batch mode logging enabled, first of either size %d or time %d seconds.\n", batchsize, batchtime);
1606         } else {
1607                 ast_log(LOG_NOTICE, "CDR logging disabled, data will be lost.\n");
1608         }
1609
1610         /* if this reload enabled the CDR batch mode, create the background thread
1611            if it does not exist */
1612         if (enabled && batchmode && (!was_enabled || !was_batchmode) && (cdr_thread == AST_PTHREADT_NULL)) {
1613                 ast_cond_init(&cdr_pending_cond, NULL);
1614                 if (ast_pthread_create_background(&cdr_thread, NULL, do_cdr, NULL) < 0) {
1615                         ast_log(LOG_ERROR, "Unable to start CDR thread.\n");
1616                         AST_SCHED_DEL(sched, cdr_sched);
1617                 } else {
1618                         ast_cli_register(&cli_submit);
1619                         ast_register_atexit(ast_cdr_engine_term);
1620                         res = 0;
1621                 }
1622         /* if this reload disabled the CDR and/or batch mode and there is a background thread,
1623            kill it */
1624         } else if (((!enabled && was_enabled) || (!batchmode && was_batchmode)) && (cdr_thread != AST_PTHREADT_NULL)) {
1625                 /* wake up the thread so it will exit */
1626                 pthread_cancel(cdr_thread);
1627                 pthread_kill(cdr_thread, SIGURG);
1628                 pthread_join(cdr_thread, NULL);
1629                 cdr_thread = AST_PTHREADT_NULL;
1630                 ast_cond_destroy(&cdr_pending_cond);
1631                 ast_cli_unregister(&cli_submit);
1632                 ast_unregister_atexit(ast_cdr_engine_term);
1633                 res = 0;
1634                 /* if leaving batch mode, then post the CDRs in the batch,
1635                    and don't reschedule, since we are stopping CDR logging */
1636                 if (!batchmode && was_batchmode) {
1637                         ast_cdr_engine_term();
1638                 }
1639         } else {
1640                 res = 0;
1641         }
1642
1643         ast_mutex_unlock(&cdr_batch_lock);
1644         ast_config_destroy(config);
1645         manager_event(EVENT_FLAG_SYSTEM, "Reload", "Module: CDR\r\nMessage: CDR subsystem reload requested\r\n");
1646
1647         return res;
1648 }
1649
1650 int ast_cdr_engine_init(void)
1651 {
1652         int res;
1653
1654         sched = ast_sched_context_create();
1655         if (!sched) {
1656                 ast_log(LOG_ERROR, "Unable to create schedule context.\n");
1657                 return -1;
1658         }
1659
1660         ast_cli_register(&cli_status);
1661
1662         res = do_reload(0);
1663         if (res) {
1664                 ast_mutex_lock(&cdr_batch_lock);
1665                 res = init_batch();
1666                 ast_mutex_unlock(&cdr_batch_lock);
1667         }
1668
1669         return res;
1670 }
1671
1672 /* \note This actually gets called a couple of times at shutdown.  Once, before we start
1673    hanging up channels, and then again, after the channel hangup timeout expires */
1674 void ast_cdr_engine_term(void)
1675 {
1676         ast_cdr_submit_batch(batchsafeshutdown);
1677 }
1678
1679 int ast_cdr_engine_reload(void)
1680 {
1681         return do_reload(1);
1682 }
1683
1684 int ast_cdr_data_add_structure(struct ast_data *tree, struct ast_cdr *cdr, int recur)
1685 {
1686         struct ast_cdr *tmpcdr;
1687         struct ast_data *level;
1688         struct ast_var_t *variables;
1689         const char *var, *val;
1690         int x = 1, i;
1691         char workspace[256];
1692         char *tmp;
1693
1694         if (!cdr) {
1695                 return -1;
1696         }
1697
1698         for (tmpcdr = cdr; tmpcdr; tmpcdr = (recur ? tmpcdr->next : NULL)) {
1699                 level = ast_data_add_node(tree, "level");
1700                 if (!level) {
1701                         continue;
1702                 }
1703
1704                 ast_data_add_int(level, "level_number", x);
1705
1706                 AST_LIST_TRAVERSE(&tmpcdr->varshead, variables, entries) {
1707                         if (variables && (var = ast_var_name(variables)) &&
1708                                         (val = ast_var_value(variables)) && !ast_strlen_zero(var)
1709                                         && !ast_strlen_zero(val)) {
1710                                 ast_data_add_str(level, var, val);
1711                         } else {
1712                                 break;
1713                         }
1714                 }
1715
1716                 for (i = 0; cdr_readonly_vars[i]; i++) {
1717                         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 */
1718                         ast_cdr_getvar(tmpcdr, cdr_readonly_vars[i], &tmp, workspace, sizeof(workspace), 0, 0);
1719                         if (!tmp) {
1720                                 continue;
1721                         }
1722                         ast_data_add_str(level, cdr_readonly_vars[i], tmp);
1723                 }
1724
1725                 x++;
1726         }
1727
1728         return 0;
1729 }
1730