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