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