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