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