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