Expand the caller ANI field to an ast_party_id
[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         const char *num;
850
851         if (!cdr) {
852                 return;
853         }
854
855         /* Grab source from ANI or normal Caller*ID */
856         num = S_COR(c->caller.ani.number.valid, c->caller.ani.number.str,
857                 S_COR(c->caller.id.number.valid, c->caller.id.number.str, NULL));
858         ast_callerid_merge(cdr->clid, sizeof(cdr->clid),
859                 S_COR(c->caller.id.name.valid, c->caller.id.name.str, NULL), num, "");
860         ast_copy_string(cdr->src, S_OR(num, ""), sizeof(cdr->src));
861         ast_cdr_setvar(cdr, "dnid", S_OR(c->dialed.number.str, ""), 0);
862
863         if (c->caller.id.subaddress.valid) {
864                 ast_cdr_setvar(cdr, "callingsubaddr", S_OR(c->caller.id.subaddress.str, ""), 0);
865         }
866         if (c->dialed.subaddress.valid) {
867                 ast_cdr_setvar(cdr, "calledsubaddr", S_OR(c->dialed.subaddress.str, ""), 0);
868         }
869 }
870
871 int ast_cdr_setcid(struct ast_cdr *cdr, struct ast_channel *c)
872 {
873         for (; cdr; cdr = cdr->next) {
874                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
875                         set_one_cid(cdr, c);
876         }
877         return 0;
878 }
879
880 static int cdr_seq_inc(struct ast_cdr *cdr)
881 {
882         return (cdr->sequence = ast_atomic_fetchadd_int(&cdr_sequence, +1));
883 }
884
885 int ast_cdr_init(struct ast_cdr *cdr, struct ast_channel *c)
886 {
887         char *chan;
888
889         for ( ; cdr ; cdr = cdr->next) {
890                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
891                         chan = S_OR(cdr->channel, "<unknown>");
892                         ast_copy_string(cdr->channel, c->name, sizeof(cdr->channel));
893                         set_one_cid(cdr, c);
894                         cdr_seq_inc(cdr);
895
896                         cdr->disposition = (c->_state == AST_STATE_UP) ?  AST_CDR_ANSWERED : AST_CDR_NOANSWER;
897                         cdr->amaflags = c->amaflags ? c->amaflags :  ast_default_amaflags;
898                         ast_copy_string(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode));
899                         ast_copy_string(cdr->peeraccount, c->peeraccount, sizeof(cdr->peeraccount));
900                         /* Destination information */
901                         ast_copy_string(cdr->dst, S_OR(c->macroexten,c->exten), sizeof(cdr->dst));
902                         ast_copy_string(cdr->dcontext, S_OR(c->macrocontext,c->context), sizeof(cdr->dcontext));
903                         /* Unique call identifier */
904                         ast_copy_string(cdr->uniqueid, c->uniqueid, sizeof(cdr->uniqueid));
905                         /* Linked call identifier */
906                         ast_copy_string(cdr->linkedid, c->linkedid, sizeof(cdr->linkedid));
907                 }
908         }
909         return 0;
910 }
911
912 /* Three routines were "fixed" via 10668, and later shown that
913    users were depending on this behavior. ast_cdr_end,
914    ast_cdr_setvar and ast_cdr_answer are the three routines.
915    While most of the other routines would not touch
916    LOCKED cdr's, these three routines were designed to
917    operate on locked CDR's as a matter of course.
918    I now appreciate how this plays with the ForkCDR app,
919    which forms these cdr chains in the first place.
920    cdr_end is pretty key: all cdrs created are closed
921    together. They only vary by start time. Arithmetically,
922    users can calculate the subintervals they wish to track. */
923
924 void ast_cdr_end(struct ast_cdr *cdr)
925 {
926         for ( ; cdr ; cdr = cdr->next) {
927                 if (ast_test_flag(cdr, AST_CDR_FLAG_DONT_TOUCH) && ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
928                         continue;
929                 check_post(cdr);
930                 if (ast_tvzero(cdr->end))
931                         cdr->end = ast_tvnow();
932                 if (ast_tvzero(cdr->start)) {
933                         ast_log(LOG_WARNING, "CDR on channel '%s' has not started\n", S_OR(cdr->channel, "<unknown>"));
934                         cdr->disposition = AST_CDR_FAILED;
935                 } else
936                         cdr->duration = cdr->end.tv_sec - cdr->start.tv_sec;
937                 if (ast_tvzero(cdr->answer)) {
938                         if (cdr->disposition == AST_CDR_ANSWERED) {
939                                 ast_log(LOG_WARNING, "CDR on channel '%s' has no answer time but is 'ANSWERED'\n", S_OR(cdr->channel, "<unknown>"));
940                                 cdr->disposition = AST_CDR_FAILED;
941                         }
942                 } else {
943                         cdr->billsec = cdr->end.tv_sec - cdr->answer.tv_sec;
944                         if (ast_test_flag(&ast_options, AST_OPT_FLAG_INITIATED_SECONDS))
945                                 cdr->billsec += cdr->end.tv_usec > cdr->answer.tv_usec ? 1 : 0;
946                 }
947         }
948 }
949
950 char *ast_cdr_disp2str(int disposition)
951 {
952         switch (disposition) {
953         case AST_CDR_NULL:
954                 return "NO ANSWER"; /* by default, for backward compatibility */
955         case AST_CDR_NOANSWER:
956                 return "NO ANSWER";
957         case AST_CDR_FAILED:
958                 return "FAILED";
959         case AST_CDR_BUSY:
960                 return "BUSY";
961         case AST_CDR_ANSWERED:
962                 return "ANSWERED";
963         }
964         return "UNKNOWN";
965 }
966
967 /*! Converts AMA flag to printable string */
968 char *ast_cdr_flags2str(int flag)
969 {
970         switch (flag) {
971         case AST_CDR_OMIT:
972                 return "OMIT";
973         case AST_CDR_BILLING:
974                 return "BILLING";
975         case AST_CDR_DOCUMENTATION:
976                 return "DOCUMENTATION";
977         }
978         return "Unknown";
979 }
980
981 int ast_cdr_setaccount(struct ast_channel *chan, const char *account)
982 {
983         struct ast_cdr *cdr = chan->cdr;
984         const char *old_acct = "";
985
986         if (!ast_strlen_zero(chan->accountcode)) {
987                 old_acct = ast_strdupa(chan->accountcode);
988         }
989
990         ast_string_field_set(chan, accountcode, account);
991         for ( ; cdr ; cdr = cdr->next) {
992                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
993                         ast_copy_string(cdr->accountcode, chan->accountcode, sizeof(cdr->accountcode));
994                 }
995         }
996
997         ast_manager_event(chan, EVENT_FLAG_CALL, "NewAccountCode",
998                         "Channel: %s\r\n"
999                         "Uniqueid: %s\r\n"
1000                         "AccountCode: %s\r\n"
1001                         "OldAccountCode: %s\r\n",
1002                         chan->name, chan->uniqueid, chan->accountcode, old_acct);
1003
1004         return 0;
1005 }
1006
1007 int ast_cdr_setpeeraccount(struct ast_channel *chan, const char *account)
1008 {
1009         struct ast_cdr *cdr = chan->cdr;
1010         const char *old_acct = "";
1011
1012         if (!ast_strlen_zero(chan->peeraccount)) {
1013                 old_acct = ast_strdupa(chan->peeraccount);
1014         }
1015
1016         ast_string_field_set(chan, peeraccount, account);
1017         for ( ; cdr ; cdr = cdr->next) {
1018                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1019                         ast_copy_string(cdr->peeraccount, chan->peeraccount, sizeof(cdr->peeraccount));
1020                 }
1021         }
1022
1023         ast_manager_event(chan, EVENT_FLAG_CALL, "NewPeerAccount",
1024                         "Channel: %s\r\n"
1025                         "Uniqueid: %s\r\n"
1026                         "PeerAccount: %s\r\n"
1027                         "OldPeerAccount: %s\r\n",
1028                         chan->name, chan->uniqueid, chan->peeraccount, old_acct);
1029
1030         return 0;
1031 }
1032
1033 int ast_cdr_setamaflags(struct ast_channel *chan, const char *flag)
1034 {
1035         struct ast_cdr *cdr;
1036         int newflag = ast_cdr_amaflags2int(flag);
1037         if (newflag) {
1038                 for (cdr = chan->cdr; cdr; cdr = cdr->next) {
1039                         if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1040                                 cdr->amaflags = newflag;
1041                         }
1042                 }
1043         }
1044
1045         return 0;
1046 }
1047
1048 int ast_cdr_setuserfield(struct ast_channel *chan, const char *userfield)
1049 {
1050         struct ast_cdr *cdr = chan->cdr;
1051
1052         for ( ; cdr ; cdr = cdr->next) {
1053                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
1054                         ast_copy_string(cdr->userfield, userfield, sizeof(cdr->userfield));
1055         }
1056
1057         return 0;
1058 }
1059
1060 int ast_cdr_appenduserfield(struct ast_channel *chan, const char *userfield)
1061 {
1062         struct ast_cdr *cdr = chan->cdr;
1063
1064         for ( ; cdr ; cdr = cdr->next) {
1065                 int len = strlen(cdr->userfield);
1066
1067                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
1068                         ast_copy_string(cdr->userfield + len, userfield, sizeof(cdr->userfield) - len);
1069         }
1070
1071         return 0;
1072 }
1073
1074 int ast_cdr_update(struct ast_channel *c)
1075 {
1076         struct ast_cdr *cdr = c->cdr;
1077
1078         for ( ; cdr ; cdr = cdr->next) {
1079                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1080                         set_one_cid(cdr, c);
1081
1082                         /* Copy account code et-al */
1083                         ast_copy_string(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode));
1084                         ast_copy_string(cdr->peeraccount, c->peeraccount, sizeof(cdr->peeraccount));
1085                         ast_copy_string(cdr->linkedid, c->linkedid, sizeof(cdr->linkedid));
1086
1087                         /* Destination information */ /* XXX privilege macro* ? */
1088                         ast_copy_string(cdr->dst, S_OR(c->macroexten, c->exten), sizeof(cdr->dst));
1089                         ast_copy_string(cdr->dcontext, S_OR(c->macrocontext, c->context), sizeof(cdr->dcontext));
1090                 }
1091         }
1092
1093         return 0;
1094 }
1095
1096 int ast_cdr_amaflags2int(const char *flag)
1097 {
1098         if (!strcasecmp(flag, "default"))
1099                 return 0;
1100         if (!strcasecmp(flag, "omit"))
1101                 return AST_CDR_OMIT;
1102         if (!strcasecmp(flag, "billing"))
1103                 return AST_CDR_BILLING;
1104         if (!strcasecmp(flag, "documentation"))
1105                 return AST_CDR_DOCUMENTATION;
1106         return -1;
1107 }
1108
1109 static void post_cdr(struct ast_cdr *cdr)
1110 {
1111         char *chan;
1112         struct ast_cdr_beitem *i;
1113
1114         for ( ; cdr ; cdr = cdr->next) {
1115                 if (!unanswered && cdr->disposition < AST_CDR_ANSWERED && (ast_strlen_zero(cdr->channel) || ast_strlen_zero(cdr->dstchannel))) {
1116                         /* For people, who don't want to see unanswered single-channel events */
1117                         ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1118                         continue;
1119                 }
1120
1121                 /* don't post CDRs that are for dialed channels unless those
1122                  * channels were originated from asterisk (pbx_spool, manager,
1123                  * cli) */
1124                 if (ast_test_flag(cdr, AST_CDR_FLAG_DIALED) && !ast_test_flag(cdr, AST_CDR_FLAG_ORIGINATED)) {
1125                         ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1126                         continue;
1127                 }
1128
1129                 chan = S_OR(cdr->channel, "<unknown>");
1130                 check_post(cdr);
1131                 ast_set_flag(cdr, AST_CDR_FLAG_POSTED);
1132                 if (ast_test_flag(cdr, AST_CDR_FLAG_POST_DISABLED))
1133                         continue;
1134                 AST_RWLIST_RDLOCK(&be_list);
1135                 AST_RWLIST_TRAVERSE(&be_list, i, list) {
1136                         i->be(cdr);
1137                 }
1138                 AST_RWLIST_UNLOCK(&be_list);
1139         }
1140 }
1141
1142 void ast_cdr_reset(struct ast_cdr *cdr, struct ast_flags *_flags)
1143 {
1144         struct ast_cdr *duplicate;
1145         struct ast_flags flags = { 0 };
1146
1147         if (_flags)
1148                 ast_copy_flags(&flags, _flags, AST_FLAGS_ALL);
1149
1150         for ( ; cdr ; cdr = cdr->next) {
1151                 /* Detach if post is requested */
1152                 if (ast_test_flag(&flags, AST_CDR_FLAG_LOCKED) || !ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1153                         if (ast_test_flag(&flags, AST_CDR_FLAG_POSTED)) {
1154                                 ast_cdr_end(cdr);
1155                                 if ((duplicate = ast_cdr_dup_unique_swap(cdr))) {
1156                                         ast_cdr_detach(duplicate);
1157                                 }
1158                                 ast_set_flag(cdr, AST_CDR_FLAG_POSTED);
1159                         }
1160
1161                         /* enable CDR only */
1162                         if (ast_test_flag(&flags, AST_CDR_FLAG_POST_ENABLE)) {
1163                                 ast_clear_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1164                                 continue;
1165                         }
1166
1167                         /* clear variables */
1168                         if (!ast_test_flag(&flags, AST_CDR_FLAG_KEEP_VARS)) {
1169                                 ast_cdr_free_vars(cdr, 0);
1170                         }
1171
1172                         /* Reset to initial state */
1173                         ast_clear_flag(cdr, AST_FLAGS_ALL);
1174                         memset(&cdr->start, 0, sizeof(cdr->start));
1175                         memset(&cdr->end, 0, sizeof(cdr->end));
1176                         memset(&cdr->answer, 0, sizeof(cdr->answer));
1177                         cdr->billsec = 0;
1178                         cdr->duration = 0;
1179                         ast_cdr_start(cdr);
1180                         cdr->disposition = AST_CDR_NOANSWER;
1181                 }
1182         }
1183 }
1184
1185 void ast_cdr_specialized_reset(struct ast_cdr *cdr, struct ast_flags *_flags)
1186 {
1187         struct ast_flags flags = { 0 };
1188
1189         if (_flags)
1190                 ast_copy_flags(&flags, _flags, AST_FLAGS_ALL);
1191
1192         /* Reset to initial state */
1193         if (ast_test_flag(cdr, AST_CDR_FLAG_POST_DISABLED)) { /* But do NOT lose the NoCDR() setting */
1194                 ast_clear_flag(cdr, AST_FLAGS_ALL);
1195                 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1196         } else {
1197                 ast_clear_flag(cdr, AST_FLAGS_ALL);
1198         }
1199
1200         memset(&cdr->start, 0, sizeof(cdr->start));
1201         memset(&cdr->end, 0, sizeof(cdr->end));
1202         memset(&cdr->answer, 0, sizeof(cdr->answer));
1203         cdr->billsec = 0;
1204         cdr->duration = 0;
1205         ast_cdr_start(cdr);
1206         cdr->disposition = AST_CDR_NULL;
1207 }
1208
1209 struct ast_cdr *ast_cdr_append(struct ast_cdr *cdr, struct ast_cdr *newcdr)
1210 {
1211         struct ast_cdr *ret;
1212
1213         if (cdr) {
1214                 ret = cdr;
1215
1216                 while (cdr->next)
1217                         cdr = cdr->next;
1218                 cdr->next = newcdr;
1219         } else {
1220                 ret = newcdr;
1221         }
1222
1223         return ret;
1224 }
1225
1226 /*! \note Don't call without cdr_batch_lock */
1227 static void reset_batch(void)
1228 {
1229         batch->size = 0;
1230         batch->head = NULL;
1231         batch->tail = NULL;
1232 }
1233
1234 /*! \note Don't call without cdr_batch_lock */
1235 static int init_batch(void)
1236 {
1237         /* This is the single meta-batch used to keep track of all CDRs during the entire life of the program */
1238         if (!(batch = ast_malloc(sizeof(*batch))))
1239                 return -1;
1240
1241         reset_batch();
1242
1243         return 0;
1244 }
1245
1246 static void *do_batch_backend_process(void *data)
1247 {
1248         struct ast_cdr_batch_item *processeditem;
1249         struct ast_cdr_batch_item *batchitem = data;
1250
1251         /* Push each CDR into storage mechanism(s) and free all the memory */
1252         while (batchitem) {
1253                 post_cdr(batchitem->cdr);
1254                 ast_cdr_free(batchitem->cdr);
1255                 processeditem = batchitem;
1256                 batchitem = batchitem->next;
1257                 ast_free(processeditem);
1258         }
1259
1260         return NULL;
1261 }
1262
1263 void ast_cdr_submit_batch(int do_shutdown)
1264 {
1265         struct ast_cdr_batch_item *oldbatchitems = NULL;
1266         pthread_t batch_post_thread = AST_PTHREADT_NULL;
1267
1268         /* if there's no batch, or no CDRs in the batch, then there's nothing to do */
1269         if (!batch || !batch->head)
1270                 return;
1271
1272         /* move the old CDRs aside, and prepare a new CDR batch */
1273         ast_mutex_lock(&cdr_batch_lock);
1274         oldbatchitems = batch->head;
1275         reset_batch();
1276         ast_mutex_unlock(&cdr_batch_lock);
1277
1278         /* if configured, spawn a new thread to post these CDRs,
1279            also try to save as much as possible if we are shutting down safely */
1280         if (batchscheduleronly || do_shutdown) {
1281                 ast_debug(1, "CDR single-threaded batch processing begins now\n");
1282                 do_batch_backend_process(oldbatchitems);
1283         } else {
1284                 if (ast_pthread_create_detached_background(&batch_post_thread, NULL, do_batch_backend_process, oldbatchitems)) {
1285                         ast_log(LOG_WARNING, "CDR processing thread could not detach, now trying in this thread\n");
1286                         do_batch_backend_process(oldbatchitems);
1287                 } else {
1288                         ast_debug(1, "CDR multi-threaded batch processing begins now\n");
1289                 }
1290         }
1291 }
1292
1293 static int submit_scheduled_batch(const void *data)
1294 {
1295         ast_cdr_submit_batch(0);
1296         /* manually reschedule from this point in time */
1297         cdr_sched = ast_sched_add(sched, batchtime * 1000, submit_scheduled_batch, NULL);
1298         /* returning zero so the scheduler does not automatically reschedule */
1299         return 0;
1300 }
1301
1302 static void submit_unscheduled_batch(void)
1303 {
1304         /* this is okay since we are not being called from within the scheduler */
1305         AST_SCHED_DEL(sched, cdr_sched);
1306         /* schedule the submission to occur ASAP (1 ms) */
1307         cdr_sched = ast_sched_add(sched, 1, submit_scheduled_batch, NULL);
1308         /* signal the do_cdr thread to wakeup early and do some work (that lazy thread ;) */
1309         ast_mutex_lock(&cdr_pending_lock);
1310         ast_cond_signal(&cdr_pending_cond);
1311         ast_mutex_unlock(&cdr_pending_lock);
1312 }
1313
1314 void ast_cdr_detach(struct ast_cdr *cdr)
1315 {
1316         struct ast_cdr_batch_item *newtail;
1317         int curr;
1318
1319         if (!cdr)
1320                 return;
1321
1322         /* maybe they disabled CDR stuff completely, so just drop it */
1323         if (!enabled) {
1324                 ast_debug(1, "Dropping CDR !\n");
1325                 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1326                 ast_cdr_free(cdr);
1327                 return;
1328         }
1329
1330         /* post stuff immediately if we are not in batch mode, this is legacy behaviour */
1331         if (!batchmode) {
1332                 post_cdr(cdr);
1333                 ast_cdr_free(cdr);
1334                 return;
1335         }
1336
1337         /* otherwise, each CDR gets put into a batch list (at the end) */
1338         ast_debug(1, "CDR detaching from this thread\n");
1339
1340         /* we'll need a new tail for every CDR */
1341         if (!(newtail = ast_calloc(1, sizeof(*newtail)))) {
1342                 post_cdr(cdr);
1343                 ast_cdr_free(cdr);
1344                 return;
1345         }
1346
1347         /* don't traverse a whole list (just keep track of the tail) */
1348         ast_mutex_lock(&cdr_batch_lock);
1349         if (!batch)
1350                 init_batch();
1351         if (!batch->head) {
1352                 /* new batch is empty, so point the head at the new tail */
1353                 batch->head = newtail;
1354         } else {
1355                 /* already got a batch with something in it, so just append a new tail */
1356                 batch->tail->next = newtail;
1357         }
1358         newtail->cdr = cdr;
1359         batch->tail = newtail;
1360         curr = batch->size++;
1361         ast_mutex_unlock(&cdr_batch_lock);
1362
1363         /* if we have enough stuff to post, then do it */
1364         if (curr >= (batchsize - 1))
1365                 submit_unscheduled_batch();
1366 }
1367
1368 static void *do_cdr(void *data)
1369 {
1370         struct timespec timeout;
1371         int schedms;
1372         int numevents = 0;
1373
1374         for (;;) {
1375                 struct timeval now;
1376                 schedms = ast_sched_wait(sched);
1377                 /* this shouldn't happen, but provide a 1 second default just in case */
1378                 if (schedms <= 0)
1379                         schedms = 1000;
1380                 now = ast_tvadd(ast_tvnow(), ast_samp2tv(schedms, 1000));
1381                 timeout.tv_sec = now.tv_sec;
1382                 timeout.tv_nsec = now.tv_usec * 1000;
1383                 /* prevent stuff from clobbering cdr_pending_cond, then wait on signals sent to it until the timeout expires */
1384                 ast_mutex_lock(&cdr_pending_lock);
1385                 ast_cond_timedwait(&cdr_pending_cond, &cdr_pending_lock, &timeout);
1386                 numevents = ast_sched_runq(sched);
1387                 ast_mutex_unlock(&cdr_pending_lock);
1388                 ast_debug(2, "Processed %d scheduled CDR batches from the run queue\n", numevents);
1389         }
1390
1391         return NULL;
1392 }
1393
1394 static char *handle_cli_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1395 {
1396         struct ast_cdr_beitem *beitem=NULL;
1397         int cnt=0;
1398         long nextbatchtime=0;
1399
1400         switch (cmd) {
1401         case CLI_INIT:
1402                 e->command = "cdr show status";
1403                 e->usage =
1404                         "Usage: cdr show status\n"
1405                         "       Displays the Call Detail Record engine system status.\n";
1406                 return NULL;
1407         case CLI_GENERATE:
1408                 return NULL;
1409         }
1410
1411         if (a->argc > 3)
1412                 return CLI_SHOWUSAGE;
1413
1414         ast_cli(a->fd, "\n");
1415         ast_cli(a->fd, "Call Detail Record (CDR) settings\n");
1416         ast_cli(a->fd, "----------------------------------\n");
1417         ast_cli(a->fd, "  Logging:                    %s\n", enabled ? "Enabled" : "Disabled");
1418         ast_cli(a->fd, "  Mode:                       %s\n", batchmode ? "Batch" : "Simple");
1419         if (enabled) {
1420                 ast_cli(a->fd, "  Log unanswered calls:       %s\n\n", unanswered ? "Yes" : "No");
1421                 if (batchmode) {
1422                         ast_cli(a->fd, "* Batch Mode Settings\n");
1423                         ast_cli(a->fd, "  -------------------\n");
1424                         if (batch)
1425                                 cnt = batch->size;
1426                         if (cdr_sched > -1)
1427                                 nextbatchtime = ast_sched_when(sched, cdr_sched);
1428                         ast_cli(a->fd, "  Safe shutdown:              %s\n", batchsafeshutdown ? "Enabled" : "Disabled");
1429                         ast_cli(a->fd, "  Threading model:            %s\n", batchscheduleronly ? "Scheduler only" : "Scheduler plus separate threads");
1430                         ast_cli(a->fd, "  Current batch size:         %d record%s\n", cnt, ESS(cnt));
1431                         ast_cli(a->fd, "  Maximum batch size:         %d record%s\n", batchsize, ESS(batchsize));
1432                         ast_cli(a->fd, "  Maximum batch time:         %d second%s\n", batchtime, ESS(batchtime));
1433                         ast_cli(a->fd, "  Next batch processing time: %ld second%s\n\n", nextbatchtime, ESS(nextbatchtime));
1434                 }
1435                 ast_cli(a->fd, "* Registered Backends\n");
1436                 ast_cli(a->fd, "  -------------------\n");
1437                 AST_RWLIST_RDLOCK(&be_list);
1438                 if (AST_RWLIST_EMPTY(&be_list)) {
1439                         ast_cli(a->fd, "    (none)\n");
1440                 } else {
1441                         AST_RWLIST_TRAVERSE(&be_list, beitem, list) {
1442                                 ast_cli(a->fd, "    %s\n", beitem->name);
1443                         }
1444                 }
1445                 AST_RWLIST_UNLOCK(&be_list);
1446                 ast_cli(a->fd, "\n");
1447         }
1448
1449         return CLI_SUCCESS;
1450 }
1451
1452 static char *handle_cli_submit(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1453 {
1454         switch (cmd) {
1455         case CLI_INIT:
1456                 e->command = "cdr submit";
1457                 e->usage =
1458                         "Usage: cdr submit\n"
1459                         "       Posts all pending batched CDR data to the configured CDR backend engine modules.\n";
1460                 return NULL;
1461         case CLI_GENERATE:
1462                 return NULL;
1463         }
1464         if (a->argc > 2)
1465                 return CLI_SHOWUSAGE;
1466
1467         submit_unscheduled_batch();
1468         ast_cli(a->fd, "Submitted CDRs to backend engines for processing.  This may take a while.\n");
1469
1470         return CLI_SUCCESS;
1471 }
1472
1473 static struct ast_cli_entry cli_submit = AST_CLI_DEFINE(handle_cli_submit, "Posts all pending batched CDR data");
1474 static struct ast_cli_entry cli_status = AST_CLI_DEFINE(handle_cli_status, "Display the CDR status");
1475
1476 static int do_reload(int reload)
1477 {
1478         struct ast_config *config;
1479         const char *enabled_value;
1480         const char *unanswered_value;
1481         const char *batched_value;
1482         const char *scheduleronly_value;
1483         const char *batchsafeshutdown_value;
1484         const char *size_value;
1485         const char *time_value;
1486         const char *end_before_h_value;
1487         const char *initiatedseconds_value;
1488         int cfg_size;
1489         int cfg_time;
1490         int was_enabled;
1491         int was_batchmode;
1492         int res=0;
1493         struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
1494
1495         if ((config = ast_config_load2("cdr.conf", "cdr", config_flags)) == CONFIG_STATUS_FILEUNCHANGED)
1496                 return 0;
1497         if (config == CONFIG_STATUS_FILEMISSING || config == CONFIG_STATUS_FILEUNCHANGED || config == CONFIG_STATUS_FILEINVALID) {
1498                 return 0;
1499         }
1500
1501         ast_mutex_lock(&cdr_batch_lock);
1502
1503         batchsize = BATCH_SIZE_DEFAULT;
1504         batchtime = BATCH_TIME_DEFAULT;
1505         batchscheduleronly = BATCH_SCHEDULER_ONLY_DEFAULT;
1506         batchsafeshutdown = BATCH_SAFE_SHUTDOWN_DEFAULT;
1507         was_enabled = enabled;
1508         was_batchmode = batchmode;
1509         enabled = 1;
1510         batchmode = 0;
1511
1512         /* don't run the next scheduled CDR posting while reloading */
1513         AST_SCHED_DEL(sched, cdr_sched);
1514
1515         if (config) {
1516                 if ((enabled_value = ast_variable_retrieve(config, "general", "enable"))) {
1517                         enabled = ast_true(enabled_value);
1518                 }
1519                 if ((unanswered_value = ast_variable_retrieve(config, "general", "unanswered"))) {
1520                         unanswered = ast_true(unanswered_value);
1521                 }
1522                 if ((batched_value = ast_variable_retrieve(config, "general", "batch"))) {
1523                         batchmode = ast_true(batched_value);
1524                 }
1525                 if ((scheduleronly_value = ast_variable_retrieve(config, "general", "scheduleronly"))) {
1526                         batchscheduleronly = ast_true(scheduleronly_value);
1527                 }
1528                 if ((batchsafeshutdown_value = ast_variable_retrieve(config, "general", "safeshutdown"))) {
1529                         batchsafeshutdown = ast_true(batchsafeshutdown_value);
1530                 }
1531                 if ((size_value = ast_variable_retrieve(config, "general", "size"))) {
1532                         if (sscanf(size_value, "%30d", &cfg_size) < 1)
1533                                 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", size_value);
1534                         else if (cfg_size < 0)
1535                                 ast_log(LOG_WARNING, "Invalid maximum batch size '%d' specified, using default\n", cfg_size);
1536                         else
1537                                 batchsize = cfg_size;
1538                 }
1539                 if ((time_value = ast_variable_retrieve(config, "general", "time"))) {
1540                         if (sscanf(time_value, "%30d", &cfg_time) < 1)
1541                                 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", time_value);
1542                         else if (cfg_time < 0)
1543                                 ast_log(LOG_WARNING, "Invalid maximum batch time '%d' specified, using default\n", cfg_time);
1544                         else
1545                                 batchtime = cfg_time;
1546                 }
1547                 if ((end_before_h_value = ast_variable_retrieve(config, "general", "endbeforehexten")))
1548                         ast_set2_flag(&ast_options, ast_true(end_before_h_value), AST_OPT_FLAG_END_CDR_BEFORE_H_EXTEN);
1549                 if ((initiatedseconds_value = ast_variable_retrieve(config, "general", "initiatedseconds")))
1550                         ast_set2_flag(&ast_options, ast_true(initiatedseconds_value), AST_OPT_FLAG_INITIATED_SECONDS);
1551         }
1552
1553         if (enabled && !batchmode) {
1554                 ast_log(LOG_NOTICE, "CDR simple logging enabled.\n");
1555         } else if (enabled && batchmode) {
1556                 cdr_sched = ast_sched_add(sched, batchtime * 1000, submit_scheduled_batch, NULL);
1557                 ast_log(LOG_NOTICE, "CDR batch mode logging enabled, first of either size %d or time %d seconds.\n", batchsize, batchtime);
1558         } else {
1559                 ast_log(LOG_NOTICE, "CDR logging disabled, data will be lost.\n");
1560         }
1561
1562         /* if this reload enabled the CDR batch mode, create the background thread
1563            if it does not exist */
1564         if (enabled && batchmode && (!was_enabled || !was_batchmode) && (cdr_thread == AST_PTHREADT_NULL)) {
1565                 ast_cond_init(&cdr_pending_cond, NULL);
1566                 if (ast_pthread_create_background(&cdr_thread, NULL, do_cdr, NULL) < 0) {
1567                         ast_log(LOG_ERROR, "Unable to start CDR thread.\n");
1568                         AST_SCHED_DEL(sched, cdr_sched);
1569                 } else {
1570                         ast_cli_register(&cli_submit);
1571                         ast_register_atexit(ast_cdr_engine_term);
1572                         res = 0;
1573                 }
1574         /* if this reload disabled the CDR and/or batch mode and there is a background thread,
1575            kill it */
1576         } else if (((!enabled && was_enabled) || (!batchmode && was_batchmode)) && (cdr_thread != AST_PTHREADT_NULL)) {
1577                 /* wake up the thread so it will exit */
1578                 pthread_cancel(cdr_thread);
1579                 pthread_kill(cdr_thread, SIGURG);
1580                 pthread_join(cdr_thread, NULL);
1581                 cdr_thread = AST_PTHREADT_NULL;
1582                 ast_cond_destroy(&cdr_pending_cond);
1583                 ast_cli_unregister(&cli_submit);
1584                 ast_unregister_atexit(ast_cdr_engine_term);
1585                 res = 0;
1586                 /* if leaving batch mode, then post the CDRs in the batch,
1587                    and don't reschedule, since we are stopping CDR logging */
1588                 if (!batchmode && was_batchmode) {
1589                         ast_cdr_engine_term();
1590                 }
1591         } else {
1592                 res = 0;
1593         }
1594
1595         ast_mutex_unlock(&cdr_batch_lock);
1596         ast_config_destroy(config);
1597         manager_event(EVENT_FLAG_SYSTEM, "Reload", "Module: CDR\r\nMessage: CDR subsystem reload requested\r\n");
1598
1599         return res;
1600 }
1601
1602 int ast_cdr_engine_init(void)
1603 {
1604         int res;
1605
1606         sched = sched_context_create();
1607         if (!sched) {
1608                 ast_log(LOG_ERROR, "Unable to create schedule context.\n");
1609                 return -1;
1610         }
1611
1612         ast_cli_register(&cli_status);
1613
1614         res = do_reload(0);
1615         if (res) {
1616                 ast_mutex_lock(&cdr_batch_lock);
1617                 res = init_batch();
1618                 ast_mutex_unlock(&cdr_batch_lock);
1619         }
1620
1621         return res;
1622 }
1623
1624 /* \note This actually gets called a couple of times at shutdown.  Once, before we start
1625    hanging up channels, and then again, after the channel hangup timeout expires */
1626 void ast_cdr_engine_term(void)
1627 {
1628         ast_cdr_submit_batch(batchsafeshutdown);
1629 }
1630
1631 int ast_cdr_engine_reload(void)
1632 {
1633         return do_reload(1);
1634 }
1635
1636 int ast_cdr_data_add_structure(struct ast_data *tree, struct ast_cdr *cdr, int recur)
1637 {
1638         struct ast_cdr *tmpcdr;
1639         struct ast_data *level;
1640         struct ast_var_t *variables;
1641         const char *var, *val;
1642         int x = 1, i;
1643         char workspace[256];
1644         char *tmp;
1645
1646         if (!cdr) {
1647                 return -1;
1648         }
1649
1650         for (tmpcdr = cdr; tmpcdr; tmpcdr = (recur ? tmpcdr->next : NULL)) {
1651                 level = ast_data_add_node(tree, "level");
1652                 if (!level) {
1653                         continue;
1654                 }
1655
1656                 ast_data_add_int(level, "level_number", x);
1657
1658                 AST_LIST_TRAVERSE(&tmpcdr->varshead, variables, entries) {
1659                         if (variables && (var = ast_var_name(variables)) &&
1660                                         (val = ast_var_value(variables)) && !ast_strlen_zero(var)
1661                                         && !ast_strlen_zero(val)) {
1662                                 ast_data_add_str(level, var, val);
1663                         } else {
1664                                 break;
1665                         }
1666                 }
1667
1668                 for (i = 0; cdr_readonly_vars[i]; i++) {
1669                         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 */
1670                         ast_cdr_getvar(tmpcdr, cdr_readonly_vars[i], &tmp, workspace, sizeof(workspace), 0, 0);
1671                         if (!tmp) {
1672                                 continue;
1673                         }
1674                         ast_data_add_str(level, cdr_readonly_vars[i], tmp);
1675                 }
1676
1677                 x++;
1678         }
1679
1680         return 0;
1681 }
1682