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