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