2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
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.
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.
21 * \brief Call Detail Record API
23 * \author Mark Spencer <markster@digium.com>
25 * \note Includes code and algorithms from the Zapata library.
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.
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
40 #include "asterisk/lock.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/cdr.h"
43 #include "asterisk/callerid.h"
44 #include "asterisk/manager.h"
45 #include "asterisk/causes.h"
46 #include "asterisk/linkedlists.h"
47 #include "asterisk/utils.h"
48 #include "asterisk/sched.h"
49 #include "asterisk/config.h"
50 #include "asterisk/cli.h"
51 #include "asterisk/stringfields.h"
52 #include "asterisk/data.h"
54 /*! Default AMA flag for billing records (CDR's) */
55 int ast_default_amaflags = AST_CDR_DOCUMENTATION;
56 char ast_default_accountcode[AST_MAX_ACCOUNT_CODE];
58 struct ast_cdr_beitem {
62 AST_RWLIST_ENTRY(ast_cdr_beitem) list;
65 static AST_RWLIST_HEAD_STATIC(be_list, ast_cdr_beitem);
67 struct ast_cdr_batch_item {
69 struct ast_cdr_batch_item *next;
72 static struct ast_cdr_batch {
74 struct ast_cdr_batch_item *head;
75 struct ast_cdr_batch_item *tail;
79 static int cdr_sequence = 0;
81 static int cdr_seq_inc(struct ast_cdr *cdr);
83 static struct sched_context *sched;
84 static int cdr_sched = -1;
85 static pthread_t cdr_thread = AST_PTHREADT_NULL;
87 #define BATCH_SIZE_DEFAULT 100
88 #define BATCH_TIME_DEFAULT 300
89 #define BATCH_SCHEDULER_ONLY_DEFAULT 0
90 #define BATCH_SAFE_SHUTDOWN_DEFAULT 1
92 static int enabled; /*! Is the CDR subsystem enabled ? */
93 static int unanswered;
97 static int batchscheduleronly;
98 static int batchsafeshutdown;
100 AST_MUTEX_DEFINE_STATIC(cdr_batch_lock);
102 /* these are used to wake up the CDR thread when there's work to do */
103 AST_MUTEX_DEFINE_STATIC(cdr_pending_lock);
104 static ast_cond_t cdr_pending_cond;
106 int check_cdr_enabled()
111 /*! Register a CDR driver. Each registered CDR driver generates a CDR
112 \return 0 on success, -1 on failure
114 int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be)
116 struct ast_cdr_beitem *i = NULL;
122 ast_log(LOG_WARNING, "CDR engine '%s' lacks backend\n", name);
126 AST_RWLIST_WRLOCK(&be_list);
127 AST_RWLIST_TRAVERSE(&be_list, i, list) {
128 if (!strcasecmp(name, i->name)) {
129 ast_log(LOG_WARNING, "Already have a CDR backend called '%s'\n", name);
130 AST_RWLIST_UNLOCK(&be_list);
135 if (!(i = ast_calloc(1, sizeof(*i))))
139 ast_copy_string(i->name, name, sizeof(i->name));
140 ast_copy_string(i->desc, desc, sizeof(i->desc));
142 AST_RWLIST_INSERT_HEAD(&be_list, i, list);
143 AST_RWLIST_UNLOCK(&be_list);
148 /*! unregister a CDR driver */
149 void ast_cdr_unregister(const char *name)
151 struct ast_cdr_beitem *i = NULL;
153 AST_RWLIST_WRLOCK(&be_list);
154 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&be_list, i, list) {
155 if (!strcasecmp(name, i->name)) {
156 AST_RWLIST_REMOVE_CURRENT(list);
160 AST_RWLIST_TRAVERSE_SAFE_END;
161 AST_RWLIST_UNLOCK(&be_list);
164 ast_verb(2, "Unregistered '%s' CDR backend\n", name);
169 int ast_cdr_isset_unanswered(void)
174 struct ast_cdr *ast_cdr_dup_unique(struct ast_cdr *cdr)
176 struct ast_cdr *newcdr = ast_cdr_dup(cdr);
184 struct ast_cdr *ast_cdr_dup_unique_swap(struct ast_cdr *cdr)
186 struct ast_cdr *newcdr = ast_cdr_dup(cdr);
194 /*! Duplicate a CDR record
195 \returns Pointer to new CDR record
197 struct ast_cdr *ast_cdr_dup(struct ast_cdr *cdr)
199 struct ast_cdr *newcdr;
201 if (!cdr) /* don't die if we get a null cdr pointer */
203 newcdr = ast_cdr_alloc();
207 memcpy(newcdr, cdr, sizeof(*newcdr));
208 /* The varshead is unusable, volatile even, after the memcpy so we take care of that here */
209 memset(&newcdr->varshead, 0, sizeof(newcdr->varshead));
210 ast_cdr_copy_vars(newcdr, cdr);
216 static const char *ast_cdr_getvar_internal(struct ast_cdr *cdr, const char *name, int recur)
218 if (ast_strlen_zero(name))
221 for (; cdr; cdr = recur ? cdr->next : NULL) {
222 struct ast_var_t *variables;
223 struct varshead *headp = &cdr->varshead;
224 AST_LIST_TRAVERSE(headp, variables, entries) {
225 if (!strcasecmp(name, ast_var_name(variables)))
226 return ast_var_value(variables);
233 static void cdr_get_tv(struct timeval when, const char *fmt, char *buf, int bufsize)
235 if (fmt == NULL) { /* raw mode */
236 snprintf(buf, bufsize, "%ld.%06ld", (long)when.tv_sec, (long)when.tv_usec);
241 ast_localtime(&when, &tm, NULL);
242 ast_strftime(buf, bufsize, fmt, &tm);
247 /*! CDR channel variable retrieval */
248 void ast_cdr_getvar(struct ast_cdr *cdr, const char *name, char **ret, char *workspace, int workspacelen, int recur, int raw)
250 const char *fmt = "%Y-%m-%d %T";
253 if (!cdr) /* don't die if the cdr is null */
257 /* special vars (the ones from the struct ast_cdr when requested by name)
258 I'd almost say we should convert all the stringed vals to vars */
260 if (!strcasecmp(name, "clid"))
261 ast_copy_string(workspace, cdr->clid, workspacelen);
262 else if (!strcasecmp(name, "src"))
263 ast_copy_string(workspace, cdr->src, workspacelen);
264 else if (!strcasecmp(name, "dst"))
265 ast_copy_string(workspace, cdr->dst, workspacelen);
266 else if (!strcasecmp(name, "dcontext"))
267 ast_copy_string(workspace, cdr->dcontext, workspacelen);
268 else if (!strcasecmp(name, "channel"))
269 ast_copy_string(workspace, cdr->channel, workspacelen);
270 else if (!strcasecmp(name, "dstchannel"))
271 ast_copy_string(workspace, cdr->dstchannel, workspacelen);
272 else if (!strcasecmp(name, "lastapp"))
273 ast_copy_string(workspace, cdr->lastapp, workspacelen);
274 else if (!strcasecmp(name, "lastdata"))
275 ast_copy_string(workspace, cdr->lastdata, workspacelen);
276 else if (!strcasecmp(name, "start"))
277 cdr_get_tv(cdr->start, raw ? NULL : fmt, workspace, workspacelen);
278 else if (!strcasecmp(name, "answer"))
279 cdr_get_tv(cdr->answer, raw ? NULL : fmt, workspace, workspacelen);
280 else if (!strcasecmp(name, "end"))
281 cdr_get_tv(cdr->end, raw ? NULL : fmt, workspace, workspacelen);
282 else if (!strcasecmp(name, "duration"))
283 snprintf(workspace, workspacelen, "%ld", cdr->duration ? cdr->duration : (long)ast_tvdiff_ms(ast_tvnow(), cdr->start) / 1000);
284 else if (!strcasecmp(name, "billsec"))
285 snprintf(workspace, workspacelen, "%ld", cdr->billsec || cdr->answer.tv_sec == 0 ? cdr->billsec : (long)ast_tvdiff_ms(ast_tvnow(), cdr->answer) / 1000);
286 else if (!strcasecmp(name, "disposition")) {
288 snprintf(workspace, workspacelen, "%ld", cdr->disposition);
290 ast_copy_string(workspace, ast_cdr_disp2str(cdr->disposition), workspacelen);
292 } else if (!strcasecmp(name, "amaflags")) {
294 snprintf(workspace, workspacelen, "%ld", cdr->amaflags);
296 ast_copy_string(workspace, ast_cdr_flags2str(cdr->amaflags), workspacelen);
298 } else if (!strcasecmp(name, "accountcode"))
299 ast_copy_string(workspace, cdr->accountcode, workspacelen);
300 else if (!strcasecmp(name, "peeraccount"))
301 ast_copy_string(workspace, cdr->peeraccount, workspacelen);
302 else if (!strcasecmp(name, "uniqueid"))
303 ast_copy_string(workspace, cdr->uniqueid, workspacelen);
304 else if (!strcasecmp(name, "linkedid"))
305 ast_copy_string(workspace, cdr->linkedid, workspacelen);
306 else if (!strcasecmp(name, "userfield"))
307 ast_copy_string(workspace, cdr->userfield, workspacelen);
308 else if (!strcasecmp(name, "sequence"))
309 snprintf(workspace, workspacelen, "%d", cdr->sequence);
310 else if ((varbuf = ast_cdr_getvar_internal(cdr, name, recur)))
311 ast_copy_string(workspace, varbuf, workspacelen);
315 if (!ast_strlen_zero(workspace))
319 /* readonly cdr variables */
320 static const char * const cdr_readonly_vars[] = { "clid", "src", "dst", "dcontext", "channel", "dstchannel",
321 "lastapp", "lastdata", "start", "answer", "end", "duration",
322 "billsec", "disposition", "amaflags", "accountcode", "uniqueid", "linkedid",
323 "userfield", "sequence", NULL };
324 /*! Set a CDR channel variable
325 \note You can't set the CDR variables that belong to the actual CDR record, like "billsec".
327 int ast_cdr_setvar(struct ast_cdr *cdr, const char *name, const char *value, int recur)
329 struct ast_var_t *newvariable;
330 struct varshead *headp;
333 for (x = 0; cdr_readonly_vars[x]; x++) {
334 if (!strcasecmp(name, cdr_readonly_vars[x])) {
335 ast_log(LOG_ERROR, "Attempt to set the '%s' read-only variable!.\n", name);
341 ast_log(LOG_ERROR, "Attempt to set a variable on a nonexistent CDR record.\n");
345 for (; cdr; cdr = recur ? cdr->next : NULL) {
346 if (ast_test_flag(cdr, AST_CDR_FLAG_DONT_TOUCH) && ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
348 headp = &cdr->varshead;
349 AST_LIST_TRAVERSE_SAFE_BEGIN(headp, newvariable, entries) {
350 if (!strcasecmp(ast_var_name(newvariable), name)) {
351 /* there is already such a variable, delete it */
352 AST_LIST_REMOVE_CURRENT(entries);
353 ast_var_delete(newvariable);
357 AST_LIST_TRAVERSE_SAFE_END;
360 newvariable = ast_var_assign(name, value);
361 AST_LIST_INSERT_HEAD(headp, newvariable, entries);
368 int ast_cdr_copy_vars(struct ast_cdr *to_cdr, struct ast_cdr *from_cdr)
370 struct ast_var_t *variables, *newvariable = NULL;
371 struct varshead *headpa, *headpb;
372 const char *var, *val;
375 if (!to_cdr || !from_cdr) /* don't die if one of the pointers is null */
378 headpa = &from_cdr->varshead;
379 headpb = &to_cdr->varshead;
381 AST_LIST_TRAVERSE(headpa,variables,entries) {
383 (var = ast_var_name(variables)) && (val = ast_var_value(variables)) &&
384 !ast_strlen_zero(var) && !ast_strlen_zero(val)) {
385 newvariable = ast_var_assign(var, val);
386 AST_LIST_INSERT_HEAD(headpb, newvariable, entries);
394 int ast_cdr_serialize_variables(struct ast_cdr *cdr, struct ast_str **buf, char delim, char sep, int recur)
396 struct ast_var_t *variables;
397 const char *var, *val;
400 int total = 0, x = 0, i;
404 for (; cdr; cdr = recur ? cdr->next : NULL) {
406 ast_str_append(buf, 0, "\n");
408 AST_LIST_TRAVERSE(&cdr->varshead, variables, entries) {
410 (var = ast_var_name(variables)) && (val = ast_var_value(variables)) &&
411 !ast_strlen_zero(var) && !ast_strlen_zero(val)) {
412 if (ast_str_append(buf, 0, "level %d: %s%c%s%c", x, var, delim, val, sep) < 0) {
413 ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
421 for (i = 0; cdr_readonly_vars[i]; i++) {
422 workspace[0] = 0; /* null out the workspace, because the cdr_get_tv() won't write anything if time is NULL, so you get old vals */
423 ast_cdr_getvar(cdr, cdr_readonly_vars[i], &tmp, workspace, sizeof(workspace), 0, 0);
427 if (ast_str_append(buf, 0, "level %d: %s%c%s%c", x, cdr_readonly_vars[i], delim, tmp, sep) < 0) {
428 ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
439 void ast_cdr_free_vars(struct ast_cdr *cdr, int recur)
442 /* clear variables */
443 for (; cdr; cdr = recur ? cdr->next : NULL) {
444 struct ast_var_t *vardata;
445 struct varshead *headp = &cdr->varshead;
446 while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
447 ast_var_delete(vardata);
451 /*! \brief print a warning if cdr already posted */
452 static void check_post(struct ast_cdr *cdr)
456 if (ast_test_flag(cdr, AST_CDR_FLAG_POSTED))
457 ast_log(LOG_NOTICE, "CDR on channel '%s' already posted\n", S_OR(cdr->channel, "<unknown>"));
460 void ast_cdr_free(struct ast_cdr *cdr)
464 struct ast_cdr *next = cdr->next;
466 ast_cdr_free_vars(cdr, 0);
472 /*! \brief the same as a cdr_free call, only with no checks; just get rid of it */
473 void ast_cdr_discard(struct ast_cdr *cdr)
476 struct ast_cdr *next = cdr->next;
478 ast_cdr_free_vars(cdr, 0);
484 struct ast_cdr *ast_cdr_alloc(void)
487 x = ast_calloc(1, sizeof(*x));
489 ast_log(LOG_ERROR,"Allocation Failure for a CDR!\n");
493 static void cdr_merge_vars(struct ast_cdr *to, struct ast_cdr *from)
495 struct ast_var_t *variablesfrom,*variablesto;
496 struct varshead *headpfrom = &to->varshead;
497 struct varshead *headpto = &from->varshead;
498 AST_LIST_TRAVERSE_SAFE_BEGIN(headpfrom, variablesfrom, entries) {
499 /* for every var in from, stick it in to */
500 const char *fromvarname, *fromvarval;
501 const char *tovarname = NULL, *tovarval = NULL;
502 fromvarname = ast_var_name(variablesfrom);
503 fromvarval = ast_var_value(variablesfrom);
506 /* now, quick see if that var is in the 'to' cdr already */
507 AST_LIST_TRAVERSE(headpto, variablesto, entries) {
509 /* now, quick see if that var is in the 'to' cdr already */
510 if ( strcasecmp(fromvarname, ast_var_name(variablesto)) == 0 ) {
511 tovarname = ast_var_name(variablesto);
512 tovarval = ast_var_value(variablesto);
516 if (tovarname && strcasecmp(fromvarval,tovarval) != 0) { /* this message here to see how irritating the userbase finds it */
517 ast_log(LOG_NOTICE, "Merging CDR's: variable %s value %s dropped in favor of value %s\n", tovarname, fromvarval, tovarval);
519 } else if (tovarname && strcasecmp(fromvarval,tovarval) == 0) /* if they are the same, the job is done */
522 /* rip this var out of the from cdr, and stick it in the to cdr */
523 AST_LIST_MOVE_CURRENT(headpto, entries);
525 AST_LIST_TRAVERSE_SAFE_END;
528 void ast_cdr_merge(struct ast_cdr *to, struct ast_cdr *from)
530 struct ast_cdr *zcdr;
531 struct ast_cdr *lto = NULL;
532 struct ast_cdr *lfrom = NULL;
533 int discard_from = 0;
538 /* don't merge into locked CDR's -- it's bad business */
539 if (ast_test_flag(to, AST_CDR_FLAG_LOCKED)) {
540 zcdr = to; /* safety valve? */
546 if (ast_test_flag(to, AST_CDR_FLAG_LOCKED)) {
547 ast_log(LOG_WARNING, "Merging into locked CDR... no choice.");
548 to = zcdr; /* safety-- if all there are is locked CDR's, then.... ?? */
553 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED)) {
554 struct ast_cdr *llfrom = NULL;
557 /* insert the from stuff after lto */
560 while (lfrom && lfrom->next) {
561 if (!lfrom->next->next)
565 /* rip off the last entry and put a copy of the to at the end */
569 /* save copy of the current *to cdr */
571 memcpy(&tcdr, to, sizeof(tcdr));
572 /* copy in the locked from cdr */
573 memcpy(to, from, sizeof(*to));
575 while (lfrom && lfrom->next) {
576 if (!lfrom->next->next)
581 /* rip off the last entry and put a copy of the to at the end */
583 to = to->next = ast_cdr_dup(&tcdr);
585 to = llfrom->next = ast_cdr_dup(&tcdr);
590 if (!ast_tvzero(from->start)) {
591 if (!ast_tvzero(to->start)) {
592 if (ast_tvcmp(to->start, from->start) > 0 ) {
593 to->start = from->start; /* use the earliest time */
594 from->start = ast_tv(0,0); /* we actively "steal" these values */
596 /* else nothing to do */
598 to->start = from->start;
599 from->start = ast_tv(0,0); /* we actively "steal" these values */
602 if (!ast_tvzero(from->answer)) {
603 if (!ast_tvzero(to->answer)) {
604 if (ast_tvcmp(to->answer, from->answer) > 0 ) {
605 to->answer = from->answer; /* use the earliest time */
606 from->answer = ast_tv(0,0); /* we actively "steal" these values */
608 /* we got the earliest answer time, so we'll settle for that? */
610 to->answer = from->answer;
611 from->answer = ast_tv(0,0); /* we actively "steal" these values */
614 if (!ast_tvzero(from->end)) {
615 if (!ast_tvzero(to->end)) {
616 if (ast_tvcmp(to->end, from->end) < 0 ) {
617 to->end = from->end; /* use the latest time */
618 from->end = ast_tv(0,0); /* we actively "steal" these values */
619 to->duration = to->end.tv_sec - to->start.tv_sec; /* don't forget to update the duration, billsec, when we set end */
620 to->billsec = ast_tvzero(to->answer) ? 0 : to->end.tv_sec - to->answer.tv_sec;
622 /* else, nothing to do */
625 from->end = ast_tv(0,0); /* we actively "steal" these values */
626 to->duration = to->end.tv_sec - to->start.tv_sec;
627 to->billsec = ast_tvzero(to->answer) ? 0 : to->end.tv_sec - to->answer.tv_sec;
630 if (to->disposition < from->disposition) {
631 to->disposition = from->disposition;
632 from->disposition = AST_CDR_NOANSWER;
634 if (ast_strlen_zero(to->lastapp) && !ast_strlen_zero(from->lastapp)) {
635 ast_copy_string(to->lastapp, from->lastapp, sizeof(to->lastapp));
636 from->lastapp[0] = 0; /* theft */
638 if (ast_strlen_zero(to->lastdata) && !ast_strlen_zero(from->lastdata)) {
639 ast_copy_string(to->lastdata, from->lastdata, sizeof(to->lastdata));
640 from->lastdata[0] = 0; /* theft */
642 if (ast_strlen_zero(to->dcontext) && !ast_strlen_zero(from->dcontext)) {
643 ast_copy_string(to->dcontext, from->dcontext, sizeof(to->dcontext));
644 from->dcontext[0] = 0; /* theft */
646 if (ast_strlen_zero(to->dstchannel) && !ast_strlen_zero(from->dstchannel)) {
647 ast_copy_string(to->dstchannel, from->dstchannel, sizeof(to->dstchannel));
648 from->dstchannel[0] = 0; /* theft */
650 if (!ast_strlen_zero(from->channel) && (ast_strlen_zero(to->channel) || !strncasecmp(from->channel, "Agent/", 6))) {
651 ast_copy_string(to->channel, from->channel, sizeof(to->channel));
652 from->channel[0] = 0; /* theft */
654 if (ast_strlen_zero(to->src) && !ast_strlen_zero(from->src)) {
655 ast_copy_string(to->src, from->src, sizeof(to->src));
656 from->src[0] = 0; /* theft */
658 if (ast_strlen_zero(to->clid) && !ast_strlen_zero(from->clid)) {
659 ast_copy_string(to->clid, from->clid, sizeof(to->clid));
660 from->clid[0] = 0; /* theft */
662 if (ast_strlen_zero(to->dst) && !ast_strlen_zero(from->dst)) {
663 ast_copy_string(to->dst, from->dst, sizeof(to->dst));
664 from->dst[0] = 0; /* theft */
667 to->amaflags = AST_CDR_DOCUMENTATION;
669 from->amaflags = AST_CDR_DOCUMENTATION; /* make sure both amaflags are set to something (DOC is default) */
670 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED) || (to->amaflags == AST_CDR_DOCUMENTATION && from->amaflags != AST_CDR_DOCUMENTATION)) {
671 to->amaflags = from->amaflags;
673 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED) || (ast_strlen_zero(to->accountcode) && !ast_strlen_zero(from->accountcode))) {
674 ast_copy_string(to->accountcode, from->accountcode, sizeof(to->accountcode));
676 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED) || (ast_strlen_zero(to->peeraccount) && !ast_strlen_zero(from->peeraccount))) {
677 ast_copy_string(to->peeraccount, from->peeraccount, sizeof(to->peeraccount));
679 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED) || (ast_strlen_zero(to->userfield) && !ast_strlen_zero(from->userfield))) {
680 ast_copy_string(to->userfield, from->userfield, sizeof(to->userfield));
682 /* flags, varsead, ? */
683 cdr_merge_vars(from, to);
685 if (ast_test_flag(from, AST_CDR_FLAG_KEEP_VARS))
686 ast_set_flag(to, AST_CDR_FLAG_KEEP_VARS);
687 if (ast_test_flag(from, AST_CDR_FLAG_POSTED))
688 ast_set_flag(to, AST_CDR_FLAG_POSTED);
689 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED))
690 ast_set_flag(to, AST_CDR_FLAG_LOCKED);
691 if (ast_test_flag(from, AST_CDR_FLAG_CHILD))
692 ast_set_flag(to, AST_CDR_FLAG_CHILD);
693 if (ast_test_flag(from, AST_CDR_FLAG_POST_DISABLED))
694 ast_set_flag(to, AST_CDR_FLAG_POST_DISABLED);
696 /* last, but not least, we need to merge any forked CDRs to the 'to' cdr */
698 /* just rip 'em off the 'from' and insert them on the 'to' */
700 from->next = zcdr->next;
702 /* zcdr is now ripped from the current list; */
703 ast_cdr_append(to, zcdr);
706 ast_cdr_discard(from);
709 void ast_cdr_start(struct ast_cdr *cdr)
713 for (; cdr; cdr = cdr->next) {
714 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
715 chan = S_OR(cdr->channel, "<unknown>");
717 cdr->start = ast_tvnow();
722 void ast_cdr_answer(struct ast_cdr *cdr)
725 for (; cdr; cdr = cdr->next) {
726 if (ast_test_flag(cdr, AST_CDR_FLAG_ANSLOCKED))
728 if (ast_test_flag(cdr, AST_CDR_FLAG_DONT_TOUCH) && ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
731 if (cdr->disposition < AST_CDR_ANSWERED)
732 cdr->disposition = AST_CDR_ANSWERED;
733 if (ast_tvzero(cdr->answer))
734 cdr->answer = ast_tvnow();
738 void ast_cdr_busy(struct ast_cdr *cdr)
741 for (; cdr; cdr = cdr->next) {
742 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
744 cdr->disposition = AST_CDR_BUSY;
749 void ast_cdr_failed(struct ast_cdr *cdr)
751 for (; cdr; cdr = cdr->next) {
753 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
755 if (cdr->disposition < AST_CDR_FAILED)
756 cdr->disposition = AST_CDR_FAILED;
761 void ast_cdr_noanswer(struct ast_cdr *cdr)
766 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
767 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
769 cdr->disposition = AST_CDR_NOANSWER;
775 /* everywhere ast_cdr_disposition is called, it will call ast_cdr_failed()
776 if ast_cdr_disposition returns a non-zero value */
778 int ast_cdr_disposition(struct ast_cdr *cdr, int cause)
782 for (; cdr; cdr = cdr->next) {
783 switch (cause) { /* handle all the non failure, busy cases, return 0 not to set disposition,
784 return -1 to set disposition to FAILED */
788 case AST_CAUSE_NO_ANSWER:
789 ast_cdr_noanswer(cdr);
791 case AST_CAUSE_NORMAL:
800 void ast_cdr_setdestchan(struct ast_cdr *cdr, const char *chann)
802 for (; cdr; cdr = cdr->next) {
803 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
805 ast_copy_string(cdr->dstchannel, chann, sizeof(cdr->dstchannel));
810 void ast_cdr_setapp(struct ast_cdr *cdr, const char *app, const char *data)
813 for (; cdr; cdr = cdr->next) {
814 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
816 ast_copy_string(cdr->lastapp, S_OR(app, ""), sizeof(cdr->lastapp));
817 ast_copy_string(cdr->lastdata, S_OR(data, ""), sizeof(cdr->lastdata));
822 void ast_cdr_setanswer(struct ast_cdr *cdr, struct timeval t)
825 for (; cdr; cdr = cdr->next) {
826 if (ast_test_flag(cdr, AST_CDR_FLAG_ANSLOCKED))
828 if (ast_test_flag(cdr, AST_CDR_FLAG_DONT_TOUCH) && ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
835 void ast_cdr_setdisposition(struct ast_cdr *cdr, long int disposition)
838 for (; cdr; cdr = cdr->next) {
839 if (ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
842 cdr->disposition = disposition;
846 /* set cid info for one record */
847 static void set_one_cid(struct ast_cdr *cdr, struct ast_channel *c)
849 /* Grab source from ANI or normal Caller*ID */
850 const char *num = S_OR(c->cid.cid_ani, c->cid.cid_num);
853 if (!ast_strlen_zero(c->cid.cid_name)) {
854 if (!ast_strlen_zero(num)) /* both name and number */
855 snprintf(cdr->clid, sizeof(cdr->clid), "\"%s\" <%s>", c->cid.cid_name, num);
857 ast_copy_string(cdr->clid, c->cid.cid_name, sizeof(cdr->clid));
858 } else if (!ast_strlen_zero(num)) { /* only number */
859 ast_copy_string(cdr->clid, num, sizeof(cdr->clid));
860 } else { /* nothing known */
863 ast_copy_string(cdr->src, S_OR(num, ""), sizeof(cdr->src));
864 ast_cdr_setvar(cdr, "dnid", S_OR(c->cid.cid_dnid, ""), 0);
866 if (c->cid.subaddress.valid) {
867 ast_cdr_setvar(cdr, "callingsubaddr", S_OR(c->cid.subaddress.str, ""), 0);
869 if (c->cid.dialed_subaddress.valid) {
870 ast_cdr_setvar(cdr, "calledsubaddr", S_OR(c->cid.dialed_subaddress.str, ""), 0);
874 int ast_cdr_setcid(struct ast_cdr *cdr, struct ast_channel *c)
876 for (; cdr; cdr = cdr->next) {
877 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
883 static int cdr_seq_inc(struct ast_cdr *cdr)
885 return (cdr->sequence = ast_atomic_fetchadd_int(&cdr_sequence, +1));
888 int ast_cdr_init(struct ast_cdr *cdr, struct ast_channel *c)
892 for ( ; cdr ; cdr = cdr->next) {
893 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
894 chan = S_OR(cdr->channel, "<unknown>");
895 ast_copy_string(cdr->channel, c->name, sizeof(cdr->channel));
899 cdr->disposition = (c->_state == AST_STATE_UP) ? AST_CDR_ANSWERED : AST_CDR_NOANSWER;
900 cdr->amaflags = c->amaflags ? c->amaflags : ast_default_amaflags;
901 ast_copy_string(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode));
902 ast_copy_string(cdr->peeraccount, c->peeraccount, sizeof(cdr->peeraccount));
903 /* Destination information */
904 ast_copy_string(cdr->dst, S_OR(c->macroexten,c->exten), sizeof(cdr->dst));
905 ast_copy_string(cdr->dcontext, S_OR(c->macrocontext,c->context), sizeof(cdr->dcontext));
906 /* Unique call identifier */
907 ast_copy_string(cdr->uniqueid, c->uniqueid, sizeof(cdr->uniqueid));
908 /* Linked call identifier */
909 ast_copy_string(cdr->linkedid, c->linkedid, sizeof(cdr->linkedid));
915 /* Three routines were "fixed" via 10668, and later shown that
916 users were depending on this behavior. ast_cdr_end,
917 ast_cdr_setvar and ast_cdr_answer are the three routines.
918 While most of the other routines would not touch
919 LOCKED cdr's, these three routines were designed to
920 operate on locked CDR's as a matter of course.
921 I now appreciate how this plays with the ForkCDR app,
922 which forms these cdr chains in the first place.
923 cdr_end is pretty key: all cdrs created are closed
924 together. They only vary by start time. Arithmetically,
925 users can calculate the subintervals they wish to track. */
927 void ast_cdr_end(struct ast_cdr *cdr)
929 for ( ; cdr ; cdr = cdr->next) {
930 if (ast_test_flag(cdr, AST_CDR_FLAG_DONT_TOUCH) && ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
933 if (ast_tvzero(cdr->end))
934 cdr->end = ast_tvnow();
935 if (ast_tvzero(cdr->start)) {
936 ast_log(LOG_WARNING, "CDR on channel '%s' has not started\n", S_OR(cdr->channel, "<unknown>"));
937 cdr->disposition = AST_CDR_FAILED;
939 cdr->duration = cdr->end.tv_sec - cdr->start.tv_sec;
940 if (ast_tvzero(cdr->answer)) {
941 if (cdr->disposition == AST_CDR_ANSWERED) {
942 ast_log(LOG_WARNING, "CDR on channel '%s' has no answer time but is 'ANSWERED'\n", S_OR(cdr->channel, "<unknown>"));
943 cdr->disposition = AST_CDR_FAILED;
946 cdr->billsec = cdr->end.tv_sec - cdr->answer.tv_sec;
947 if (ast_test_flag(&ast_options, AST_OPT_FLAG_INITIATED_SECONDS))
948 cdr->billsec += cdr->end.tv_usec > cdr->answer.tv_usec ? 1 : 0;
953 char *ast_cdr_disp2str(int disposition)
955 switch (disposition) {
957 return "NO ANSWER"; /* by default, for backward compatibility */
958 case AST_CDR_NOANSWER:
964 case AST_CDR_ANSWERED:
970 /*! Converts AMA flag to printable string */
971 char *ast_cdr_flags2str(int flag)
976 case AST_CDR_BILLING:
978 case AST_CDR_DOCUMENTATION:
979 return "DOCUMENTATION";
984 int ast_cdr_setaccount(struct ast_channel *chan, const char *account)
986 struct ast_cdr *cdr = chan->cdr;
987 const char *old_acct = "";
989 if (!ast_strlen_zero(chan->accountcode)) {
990 old_acct = ast_strdupa(chan->accountcode);
993 ast_string_field_set(chan, accountcode, account);
994 for ( ; cdr ; cdr = cdr->next) {
995 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
996 ast_copy_string(cdr->accountcode, chan->accountcode, sizeof(cdr->accountcode));
1000 ast_manager_event(chan, EVENT_FLAG_CALL, "NewAccountCode",
1003 "AccountCode: %s\r\n"
1004 "OldAccountCode: %s\r\n",
1005 chan->name, chan->uniqueid, chan->accountcode, old_acct);
1010 int ast_cdr_setpeeraccount(struct ast_channel *chan, const char *account)
1012 struct ast_cdr *cdr = chan->cdr;
1013 const char *old_acct = "";
1015 if (!ast_strlen_zero(chan->peeraccount)) {
1016 old_acct = ast_strdupa(chan->peeraccount);
1019 ast_string_field_set(chan, peeraccount, account);
1020 for ( ; cdr ; cdr = cdr->next) {
1021 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1022 ast_copy_string(cdr->peeraccount, chan->peeraccount, sizeof(cdr->peeraccount));
1026 ast_manager_event(chan, EVENT_FLAG_CALL, "NewPeerAccount",
1029 "PeerAccount: %s\r\n"
1030 "OldPeerAccount: %s\r\n",
1031 chan->name, chan->uniqueid, chan->peeraccount, old_acct);
1036 int ast_cdr_setamaflags(struct ast_channel *chan, const char *flag)
1038 struct ast_cdr *cdr;
1039 int newflag = ast_cdr_amaflags2int(flag);
1041 for (cdr = chan->cdr; cdr; cdr = cdr->next) {
1042 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1043 cdr->amaflags = newflag;
1051 int ast_cdr_setuserfield(struct ast_channel *chan, const char *userfield)
1053 struct ast_cdr *cdr = chan->cdr;
1055 for ( ; cdr ; cdr = cdr->next) {
1056 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
1057 ast_copy_string(cdr->userfield, userfield, sizeof(cdr->userfield));
1063 int ast_cdr_appenduserfield(struct ast_channel *chan, const char *userfield)
1065 struct ast_cdr *cdr = chan->cdr;
1067 for ( ; cdr ; cdr = cdr->next) {
1068 int len = strlen(cdr->userfield);
1070 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
1071 ast_copy_string(cdr->userfield + len, userfield, sizeof(cdr->userfield) - len);
1077 int ast_cdr_update(struct ast_channel *c)
1079 struct ast_cdr *cdr = c->cdr;
1081 for ( ; cdr ; cdr = cdr->next) {
1082 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1083 set_one_cid(cdr, c);
1085 /* Copy account code et-al */
1086 ast_copy_string(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode));
1087 ast_copy_string(cdr->peeraccount, c->peeraccount, sizeof(cdr->peeraccount));
1088 ast_copy_string(cdr->linkedid, c->linkedid, sizeof(cdr->linkedid));
1090 /* Destination information */ /* XXX privilege macro* ? */
1091 ast_copy_string(cdr->dst, S_OR(c->macroexten, c->exten), sizeof(cdr->dst));
1092 ast_copy_string(cdr->dcontext, S_OR(c->macrocontext, c->context), sizeof(cdr->dcontext));
1099 int ast_cdr_amaflags2int(const char *flag)
1101 if (!strcasecmp(flag, "default"))
1103 if (!strcasecmp(flag, "omit"))
1104 return AST_CDR_OMIT;
1105 if (!strcasecmp(flag, "billing"))
1106 return AST_CDR_BILLING;
1107 if (!strcasecmp(flag, "documentation"))
1108 return AST_CDR_DOCUMENTATION;
1112 static void post_cdr(struct ast_cdr *cdr)
1115 struct ast_cdr_beitem *i;
1117 for ( ; cdr ; cdr = cdr->next) {
1118 if (!unanswered && cdr->disposition < AST_CDR_ANSWERED && (ast_strlen_zero(cdr->channel) || ast_strlen_zero(cdr->dstchannel))) {
1119 /* For people, who don't want to see unanswered single-channel events */
1120 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1124 /* don't post CDRs that are for dialed channels unless those
1125 * channels were originated from asterisk (pbx_spool, manager,
1127 if (ast_test_flag(cdr, AST_CDR_FLAG_DIALED) && !ast_test_flag(cdr, AST_CDR_FLAG_ORIGINATED)) {
1128 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1132 chan = S_OR(cdr->channel, "<unknown>");
1134 ast_set_flag(cdr, AST_CDR_FLAG_POSTED);
1135 if (ast_test_flag(cdr, AST_CDR_FLAG_POST_DISABLED))
1137 AST_RWLIST_RDLOCK(&be_list);
1138 AST_RWLIST_TRAVERSE(&be_list, i, list) {
1141 AST_RWLIST_UNLOCK(&be_list);
1145 void ast_cdr_reset(struct ast_cdr *cdr, struct ast_flags *_flags)
1147 struct ast_cdr *duplicate;
1148 struct ast_flags flags = { 0 };
1151 ast_copy_flags(&flags, _flags, AST_FLAGS_ALL);
1153 for ( ; cdr ; cdr = cdr->next) {
1154 /* Detach if post is requested */
1155 if (ast_test_flag(&flags, AST_CDR_FLAG_LOCKED) || !ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1156 if (ast_test_flag(&flags, AST_CDR_FLAG_POSTED)) {
1158 if ((duplicate = ast_cdr_dup_unique_swap(cdr))) {
1159 ast_cdr_detach(duplicate);
1161 ast_set_flag(cdr, AST_CDR_FLAG_POSTED);
1164 /* enable CDR only */
1165 if (ast_test_flag(&flags, AST_CDR_FLAG_POST_ENABLE)) {
1166 ast_clear_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1170 /* clear variables */
1171 if (!ast_test_flag(&flags, AST_CDR_FLAG_KEEP_VARS)) {
1172 ast_cdr_free_vars(cdr, 0);
1175 /* Reset to initial state */
1176 ast_clear_flag(cdr, AST_FLAGS_ALL);
1177 memset(&cdr->start, 0, sizeof(cdr->start));
1178 memset(&cdr->end, 0, sizeof(cdr->end));
1179 memset(&cdr->answer, 0, sizeof(cdr->answer));
1183 cdr->disposition = AST_CDR_NOANSWER;
1188 void ast_cdr_specialized_reset(struct ast_cdr *cdr, struct ast_flags *_flags)
1190 struct ast_flags flags = { 0 };
1193 ast_copy_flags(&flags, _flags, AST_FLAGS_ALL);
1195 /* Reset to initial state */
1196 if (ast_test_flag(cdr, AST_CDR_FLAG_POST_DISABLED)) { /* But do NOT lose the NoCDR() setting */
1197 ast_clear_flag(cdr, AST_FLAGS_ALL);
1198 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1200 ast_clear_flag(cdr, AST_FLAGS_ALL);
1203 memset(&cdr->start, 0, sizeof(cdr->start));
1204 memset(&cdr->end, 0, sizeof(cdr->end));
1205 memset(&cdr->answer, 0, sizeof(cdr->answer));
1209 cdr->disposition = AST_CDR_NULL;
1212 struct ast_cdr *ast_cdr_append(struct ast_cdr *cdr, struct ast_cdr *newcdr)
1214 struct ast_cdr *ret;
1229 /*! \note Don't call without cdr_batch_lock */
1230 static void reset_batch(void)
1237 /*! \note Don't call without cdr_batch_lock */
1238 static int init_batch(void)
1240 /* This is the single meta-batch used to keep track of all CDRs during the entire life of the program */
1241 if (!(batch = ast_malloc(sizeof(*batch))))
1249 static void *do_batch_backend_process(void *data)
1251 struct ast_cdr_batch_item *processeditem;
1252 struct ast_cdr_batch_item *batchitem = data;
1254 /* Push each CDR into storage mechanism(s) and free all the memory */
1256 post_cdr(batchitem->cdr);
1257 ast_cdr_free(batchitem->cdr);
1258 processeditem = batchitem;
1259 batchitem = batchitem->next;
1260 ast_free(processeditem);
1266 void ast_cdr_submit_batch(int do_shutdown)
1268 struct ast_cdr_batch_item *oldbatchitems = NULL;
1269 pthread_t batch_post_thread = AST_PTHREADT_NULL;
1271 /* if there's no batch, or no CDRs in the batch, then there's nothing to do */
1272 if (!batch || !batch->head)
1275 /* move the old CDRs aside, and prepare a new CDR batch */
1276 ast_mutex_lock(&cdr_batch_lock);
1277 oldbatchitems = batch->head;
1279 ast_mutex_unlock(&cdr_batch_lock);
1281 /* if configured, spawn a new thread to post these CDRs,
1282 also try to save as much as possible if we are shutting down safely */
1283 if (batchscheduleronly || do_shutdown) {
1284 ast_debug(1, "CDR single-threaded batch processing begins now\n");
1285 do_batch_backend_process(oldbatchitems);
1287 if (ast_pthread_create_detached_background(&batch_post_thread, NULL, do_batch_backend_process, oldbatchitems)) {
1288 ast_log(LOG_WARNING, "CDR processing thread could not detach, now trying in this thread\n");
1289 do_batch_backend_process(oldbatchitems);
1291 ast_debug(1, "CDR multi-threaded batch processing begins now\n");
1296 static int submit_scheduled_batch(const void *data)
1298 ast_cdr_submit_batch(0);
1299 /* manually reschedule from this point in time */
1300 cdr_sched = ast_sched_add(sched, batchtime * 1000, submit_scheduled_batch, NULL);
1301 /* returning zero so the scheduler does not automatically reschedule */
1305 static void submit_unscheduled_batch(void)
1307 /* this is okay since we are not being called from within the scheduler */
1308 AST_SCHED_DEL(sched, cdr_sched);
1309 /* schedule the submission to occur ASAP (1 ms) */
1310 cdr_sched = ast_sched_add(sched, 1, submit_scheduled_batch, NULL);
1311 /* signal the do_cdr thread to wakeup early and do some work (that lazy thread ;) */
1312 ast_mutex_lock(&cdr_pending_lock);
1313 ast_cond_signal(&cdr_pending_cond);
1314 ast_mutex_unlock(&cdr_pending_lock);
1317 void ast_cdr_detach(struct ast_cdr *cdr)
1319 struct ast_cdr_batch_item *newtail;
1325 /* maybe they disabled CDR stuff completely, so just drop it */
1327 ast_debug(1, "Dropping CDR !\n");
1328 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1333 /* post stuff immediately if we are not in batch mode, this is legacy behaviour */
1340 /* otherwise, each CDR gets put into a batch list (at the end) */
1341 ast_debug(1, "CDR detaching from this thread\n");
1343 /* we'll need a new tail for every CDR */
1344 if (!(newtail = ast_calloc(1, sizeof(*newtail)))) {
1350 /* don't traverse a whole list (just keep track of the tail) */
1351 ast_mutex_lock(&cdr_batch_lock);
1355 /* new batch is empty, so point the head at the new tail */
1356 batch->head = newtail;
1358 /* already got a batch with something in it, so just append a new tail */
1359 batch->tail->next = newtail;
1362 batch->tail = newtail;
1363 curr = batch->size++;
1364 ast_mutex_unlock(&cdr_batch_lock);
1366 /* if we have enough stuff to post, then do it */
1367 if (curr >= (batchsize - 1))
1368 submit_unscheduled_batch();
1371 static void *do_cdr(void *data)
1373 struct timespec timeout;
1379 schedms = ast_sched_wait(sched);
1380 /* this shouldn't happen, but provide a 1 second default just in case */
1383 now = ast_tvadd(ast_tvnow(), ast_samp2tv(schedms, 1000));
1384 timeout.tv_sec = now.tv_sec;
1385 timeout.tv_nsec = now.tv_usec * 1000;
1386 /* prevent stuff from clobbering cdr_pending_cond, then wait on signals sent to it until the timeout expires */
1387 ast_mutex_lock(&cdr_pending_lock);
1388 ast_cond_timedwait(&cdr_pending_cond, &cdr_pending_lock, &timeout);
1389 numevents = ast_sched_runq(sched);
1390 ast_mutex_unlock(&cdr_pending_lock);
1391 ast_debug(2, "Processed %d scheduled CDR batches from the run queue\n", numevents);
1397 static char *handle_cli_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1399 struct ast_cdr_beitem *beitem=NULL;
1401 long nextbatchtime=0;
1405 e->command = "cdr show status";
1407 "Usage: cdr show status\n"
1408 " Displays the Call Detail Record engine system status.\n";
1415 return CLI_SHOWUSAGE;
1417 ast_cli(a->fd, "\n");
1418 ast_cli(a->fd, "Call Detail Record (CDR) settings\n");
1419 ast_cli(a->fd, "----------------------------------\n");
1420 ast_cli(a->fd, " Logging: %s\n", enabled ? "Enabled" : "Disabled");
1421 ast_cli(a->fd, " Mode: %s\n", batchmode ? "Batch" : "Simple");
1423 ast_cli(a->fd, " Log unanswered calls: %s\n\n", unanswered ? "Yes" : "No");
1425 ast_cli(a->fd, "* Batch Mode Settings\n");
1426 ast_cli(a->fd, " -------------------\n");
1430 nextbatchtime = ast_sched_when(sched, cdr_sched);
1431 ast_cli(a->fd, " Safe shutdown: %s\n", batchsafeshutdown ? "Enabled" : "Disabled");
1432 ast_cli(a->fd, " Threading model: %s\n", batchscheduleronly ? "Scheduler only" : "Scheduler plus separate threads");
1433 ast_cli(a->fd, " Current batch size: %d record%s\n", cnt, ESS(cnt));
1434 ast_cli(a->fd, " Maximum batch size: %d record%s\n", batchsize, ESS(batchsize));
1435 ast_cli(a->fd, " Maximum batch time: %d second%s\n", batchtime, ESS(batchtime));
1436 ast_cli(a->fd, " Next batch processing time: %ld second%s\n\n", nextbatchtime, ESS(nextbatchtime));
1438 ast_cli(a->fd, "* Registered Backends\n");
1439 ast_cli(a->fd, " -------------------\n");
1440 AST_RWLIST_RDLOCK(&be_list);
1441 if (AST_RWLIST_EMPTY(&be_list)) {
1442 ast_cli(a->fd, " (none)\n");
1444 AST_RWLIST_TRAVERSE(&be_list, beitem, list) {
1445 ast_cli(a->fd, " %s\n", beitem->name);
1448 AST_RWLIST_UNLOCK(&be_list);
1449 ast_cli(a->fd, "\n");
1455 static char *handle_cli_submit(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1459 e->command = "cdr submit";
1461 "Usage: cdr submit\n"
1462 " Posts all pending batched CDR data to the configured CDR backend engine modules.\n";
1468 return CLI_SHOWUSAGE;
1470 submit_unscheduled_batch();
1471 ast_cli(a->fd, "Submitted CDRs to backend engines for processing. This may take a while.\n");
1476 static struct ast_cli_entry cli_submit = AST_CLI_DEFINE(handle_cli_submit, "Posts all pending batched CDR data");
1477 static struct ast_cli_entry cli_status = AST_CLI_DEFINE(handle_cli_status, "Display the CDR status");
1479 static int do_reload(int reload)
1481 struct ast_config *config;
1482 const char *enabled_value;
1483 const char *unanswered_value;
1484 const char *batched_value;
1485 const char *scheduleronly_value;
1486 const char *batchsafeshutdown_value;
1487 const char *size_value;
1488 const char *time_value;
1489 const char *end_before_h_value;
1490 const char *initiatedseconds_value;
1496 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
1498 if ((config = ast_config_load2("cdr.conf", "cdr", config_flags)) == CONFIG_STATUS_FILEUNCHANGED)
1500 if (config == CONFIG_STATUS_FILEMISSING || config == CONFIG_STATUS_FILEUNCHANGED || config == CONFIG_STATUS_FILEINVALID) {
1504 ast_mutex_lock(&cdr_batch_lock);
1506 batchsize = BATCH_SIZE_DEFAULT;
1507 batchtime = BATCH_TIME_DEFAULT;
1508 batchscheduleronly = BATCH_SCHEDULER_ONLY_DEFAULT;
1509 batchsafeshutdown = BATCH_SAFE_SHUTDOWN_DEFAULT;
1510 was_enabled = enabled;
1511 was_batchmode = batchmode;
1515 /* don't run the next scheduled CDR posting while reloading */
1516 AST_SCHED_DEL(sched, cdr_sched);
1519 if ((enabled_value = ast_variable_retrieve(config, "general", "enable"))) {
1520 enabled = ast_true(enabled_value);
1522 if ((unanswered_value = ast_variable_retrieve(config, "general", "unanswered"))) {
1523 unanswered = ast_true(unanswered_value);
1525 if ((batched_value = ast_variable_retrieve(config, "general", "batch"))) {
1526 batchmode = ast_true(batched_value);
1528 if ((scheduleronly_value = ast_variable_retrieve(config, "general", "scheduleronly"))) {
1529 batchscheduleronly = ast_true(scheduleronly_value);
1531 if ((batchsafeshutdown_value = ast_variable_retrieve(config, "general", "safeshutdown"))) {
1532 batchsafeshutdown = ast_true(batchsafeshutdown_value);
1534 if ((size_value = ast_variable_retrieve(config, "general", "size"))) {
1535 if (sscanf(size_value, "%30d", &cfg_size) < 1)
1536 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", size_value);
1537 else if (cfg_size < 0)
1538 ast_log(LOG_WARNING, "Invalid maximum batch size '%d' specified, using default\n", cfg_size);
1540 batchsize = cfg_size;
1542 if ((time_value = ast_variable_retrieve(config, "general", "time"))) {
1543 if (sscanf(time_value, "%30d", &cfg_time) < 1)
1544 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", time_value);
1545 else if (cfg_time < 0)
1546 ast_log(LOG_WARNING, "Invalid maximum batch time '%d' specified, using default\n", cfg_time);
1548 batchtime = cfg_time;
1550 if ((end_before_h_value = ast_variable_retrieve(config, "general", "endbeforehexten")))
1551 ast_set2_flag(&ast_options, ast_true(end_before_h_value), AST_OPT_FLAG_END_CDR_BEFORE_H_EXTEN);
1552 if ((initiatedseconds_value = ast_variable_retrieve(config, "general", "initiatedseconds")))
1553 ast_set2_flag(&ast_options, ast_true(initiatedseconds_value), AST_OPT_FLAG_INITIATED_SECONDS);
1556 if (enabled && !batchmode) {
1557 ast_log(LOG_NOTICE, "CDR simple logging enabled.\n");
1558 } else if (enabled && batchmode) {
1559 cdr_sched = ast_sched_add(sched, batchtime * 1000, submit_scheduled_batch, NULL);
1560 ast_log(LOG_NOTICE, "CDR batch mode logging enabled, first of either size %d or time %d seconds.\n", batchsize, batchtime);
1562 ast_log(LOG_NOTICE, "CDR logging disabled, data will be lost.\n");
1565 /* if this reload enabled the CDR batch mode, create the background thread
1566 if it does not exist */
1567 if (enabled && batchmode && (!was_enabled || !was_batchmode) && (cdr_thread == AST_PTHREADT_NULL)) {
1568 ast_cond_init(&cdr_pending_cond, NULL);
1569 if (ast_pthread_create_background(&cdr_thread, NULL, do_cdr, NULL) < 0) {
1570 ast_log(LOG_ERROR, "Unable to start CDR thread.\n");
1571 AST_SCHED_DEL(sched, cdr_sched);
1573 ast_cli_register(&cli_submit);
1574 ast_register_atexit(ast_cdr_engine_term);
1577 /* if this reload disabled the CDR and/or batch mode and there is a background thread,
1579 } else if (((!enabled && was_enabled) || (!batchmode && was_batchmode)) && (cdr_thread != AST_PTHREADT_NULL)) {
1580 /* wake up the thread so it will exit */
1581 pthread_cancel(cdr_thread);
1582 pthread_kill(cdr_thread, SIGURG);
1583 pthread_join(cdr_thread, NULL);
1584 cdr_thread = AST_PTHREADT_NULL;
1585 ast_cond_destroy(&cdr_pending_cond);
1586 ast_cli_unregister(&cli_submit);
1587 ast_unregister_atexit(ast_cdr_engine_term);
1589 /* if leaving batch mode, then post the CDRs in the batch,
1590 and don't reschedule, since we are stopping CDR logging */
1591 if (!batchmode && was_batchmode) {
1592 ast_cdr_engine_term();
1598 ast_mutex_unlock(&cdr_batch_lock);
1599 ast_config_destroy(config);
1600 manager_event(EVENT_FLAG_SYSTEM, "Reload", "Module: CDR\r\nMessage: CDR subsystem reload requested\r\n");
1605 int ast_cdr_engine_init(void)
1609 sched = sched_context_create();
1611 ast_log(LOG_ERROR, "Unable to create schedule context.\n");
1615 ast_cli_register(&cli_status);
1619 ast_mutex_lock(&cdr_batch_lock);
1621 ast_mutex_unlock(&cdr_batch_lock);
1627 /* \note This actually gets called a couple of times at shutdown. Once, before we start
1628 hanging up channels, and then again, after the channel hangup timeout expires */
1629 void ast_cdr_engine_term(void)
1631 ast_cdr_submit_batch(batchsafeshutdown);
1634 int ast_cdr_engine_reload(void)
1636 return do_reload(1);
1639 int ast_cdr_data_add_structure(struct ast_data *tree, struct ast_cdr *cdr, int recur)
1641 struct ast_cdr *tmpcdr;
1642 struct ast_data *level;
1643 struct ast_var_t *variables;
1644 const char *var, *val;
1646 char workspace[256];
1653 for (tmpcdr = cdr; tmpcdr; tmpcdr = (recur ? tmpcdr->next : NULL)) {
1654 level = ast_data_add_node(tree, "level");
1659 ast_data_add_int(level, "level_number", x);
1661 AST_LIST_TRAVERSE(&tmpcdr->varshead, variables, entries) {
1662 if (variables && (var = ast_var_name(variables)) &&
1663 (val = ast_var_value(variables)) && !ast_strlen_zero(var)
1664 && !ast_strlen_zero(val)) {
1665 ast_data_add_str(level, var, val);
1671 for (i = 0; cdr_readonly_vars[i]; i++) {
1672 workspace[0] = 0; /* null out the workspace, because the cdr_get_tv() won't write anything if time is NULL, so you get old vals */
1673 ast_cdr_getvar(tmpcdr, cdr_readonly_vars[i], &tmp, workspace, sizeof(workspace), 0, 0);
1677 ast_data_add_str(level, cdr_readonly_vars[i], tmp);