2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, 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 * Includes code and algorithms from the Zapata library.
25 * \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
26 * through our fingers somehow. If someone allocates a CDR, it must be completely handled normally
27 * or a WARNING shall be logged, so that we can best keep track of any escape condition where the CDR
28 * isn't properly generated and posted.
40 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
42 #include "asterisk/lock.h"
43 #include "asterisk/channel.h"
44 #include "asterisk/cdr.h"
45 #include "asterisk/logger.h"
46 #include "asterisk/callerid.h"
47 #include "asterisk/causes.h"
48 #include "asterisk/options.h"
49 #include "asterisk/linkedlists.h"
50 #include "asterisk/utils.h"
51 #include "asterisk/sched.h"
52 #include "asterisk/config.h"
53 #include "asterisk/cli.h"
54 #include "asterisk/module.h"
56 /*! Default AMA flag for billing records (CDR's) */
57 int ast_default_amaflags = AST_CDR_DOCUMENTATION;
58 char ast_default_accountcode[AST_MAX_ACCOUNT_CODE] = "";
60 struct ast_cdr_beitem {
64 AST_LIST_ENTRY(ast_cdr_beitem) list;
67 static AST_LIST_HEAD_STATIC(be_list, ast_cdr_beitem);
69 struct ast_cdr_batch_item {
71 struct ast_cdr_batch_item *next;
74 static struct ast_cdr_batch {
76 struct ast_cdr_batch_item *head;
77 struct ast_cdr_batch_item *tail;
80 static struct sched_context *sched;
81 static int cdr_sched = -1;
82 static pthread_t cdr_thread = AST_PTHREADT_NULL;
84 #define BATCH_SIZE_DEFAULT 100
85 #define BATCH_TIME_DEFAULT 300
86 #define BATCH_SCHEDULER_ONLY_DEFAULT 0
87 #define BATCH_SAFE_SHUTDOWN_DEFAULT 1
93 static int batchscheduleronly;
94 static int batchsafeshutdown;
96 AST_MUTEX_DEFINE_STATIC(cdr_batch_lock);
98 /* these are used to wake up the CDR thread when there's work to do */
99 AST_MUTEX_DEFINE_STATIC(cdr_pending_lock);
100 static ast_cond_t cdr_pending_cond;
103 /*! Register a CDR driver. Each registered CDR driver generates a CDR
104 \return 0 on success, -1 on failure
106 int ast_cdr_register(char *name, char *desc, ast_cdrbe be)
108 struct ast_cdr_beitem *i;
113 ast_log(LOG_WARNING, "CDR engine '%s' lacks backend\n", name);
117 AST_LIST_LOCK(&be_list);
118 AST_LIST_TRAVERSE(&be_list, i, list) {
119 if (!strcasecmp(name, i->name))
122 AST_LIST_UNLOCK(&be_list);
125 ast_log(LOG_WARNING, "Already have a CDR backend called '%s'\n", name);
129 i = malloc(sizeof(*i));
133 memset(i, 0, sizeof(*i));
135 ast_copy_string(i->name, name, sizeof(i->name));
136 ast_copy_string(i->desc, desc, sizeof(i->desc));
138 AST_LIST_LOCK(&be_list);
139 AST_LIST_INSERT_HEAD(&be_list, i, list);
140 AST_LIST_UNLOCK(&be_list);
145 /*! unregister a CDR driver */
146 void ast_cdr_unregister(char *name)
148 struct ast_cdr_beitem *i = NULL;
150 AST_LIST_LOCK(&be_list);
151 AST_LIST_TRAVERSE_SAFE_BEGIN(&be_list, i, list) {
152 if (!strcasecmp(name, i->name)) {
153 AST_LIST_REMOVE_CURRENT(&be_list, list);
154 if (option_verbose > 1)
155 ast_verbose(VERBOSE_PREFIX_2 "Unregistered '%s' CDR backend\n", name);
160 AST_LIST_TRAVERSE_SAFE_END;
161 AST_LIST_UNLOCK(&be_list);
164 /*! Duplicate a CDR record
165 \returns Pointer to new CDR record
167 struct ast_cdr *ast_cdr_dup(struct ast_cdr *cdr)
169 struct ast_cdr *newcdr;
171 if (!(newcdr = ast_cdr_alloc())) {
172 ast_log(LOG_ERROR, "Memory Error!\n");
176 memcpy(newcdr, cdr, sizeof(*newcdr));
177 /* The varshead is unusable, volatile even, after the memcpy so we take care of that here */
178 memset(&newcdr->varshead, 0, sizeof(newcdr->varshead));
179 ast_cdr_copy_vars(newcdr, cdr);
185 static const char *ast_cdr_getvar_internal(struct ast_cdr *cdr, const char *name, int recur)
187 struct ast_var_t *variables;
188 struct varshead *headp;
190 if (ast_strlen_zero(name))
194 headp = &cdr->varshead;
195 AST_LIST_TRAVERSE(headp, variables, entries) {
196 if (!strcasecmp(name, ast_var_name(variables)))
197 return ast_var_value(variables);
207 /*! CDR channel variable retrieval */
208 void ast_cdr_getvar(struct ast_cdr *cdr, const char *name, char **ret, char *workspace, int workspacelen, int recur)
212 const char *fmt = "%Y-%m-%d %T";
216 /* special vars (the ones from the struct ast_cdr when requested by name)
217 I'd almost say we should convert all the stringed vals to vars */
219 if (!strcasecmp(name, "clid"))
220 ast_copy_string(workspace, cdr->clid, workspacelen);
221 else if (!strcasecmp(name, "src"))
222 ast_copy_string(workspace, cdr->src, workspacelen);
223 else if (!strcasecmp(name, "dst"))
224 ast_copy_string(workspace, cdr->dst, workspacelen);
225 else if (!strcasecmp(name, "dcontext"))
226 ast_copy_string(workspace, cdr->dcontext, workspacelen);
227 else if (!strcasecmp(name, "channel"))
228 ast_copy_string(workspace, cdr->channel, workspacelen);
229 else if (!strcasecmp(name, "dstchannel"))
230 ast_copy_string(workspace, cdr->dstchannel, workspacelen);
231 else if (!strcasecmp(name, "lastapp"))
232 ast_copy_string(workspace, cdr->lastapp, workspacelen);
233 else if (!strcasecmp(name, "lastdata"))
234 ast_copy_string(workspace, cdr->lastdata, workspacelen);
235 else if (!strcasecmp(name, "start")) {
236 t = cdr->start.tv_sec;
238 localtime_r(&t, &tm);
239 strftime(workspace, workspacelen, fmt, &tm);
241 } else if (!strcasecmp(name, "answer")) {
242 t = cdr->answer.tv_sec;
244 localtime_r(&t, &tm);
245 strftime(workspace, workspacelen, fmt, &tm);
247 } else if (!strcasecmp(name, "end")) {
250 localtime_r(&t, &tm);
251 strftime(workspace, workspacelen, fmt, &tm);
253 } else if (!strcasecmp(name, "duration"))
254 snprintf(workspace, workspacelen, "%d", cdr->duration);
255 else if (!strcasecmp(name, "billsec"))
256 snprintf(workspace, workspacelen, "%d", cdr->billsec);
257 else if (!strcasecmp(name, "disposition"))
258 ast_copy_string(workspace, ast_cdr_disp2str(cdr->disposition), workspacelen);
259 else if (!strcasecmp(name, "amaflags"))
260 ast_copy_string(workspace, ast_cdr_flags2str(cdr->amaflags), workspacelen);
261 else if (!strcasecmp(name, "accountcode"))
262 ast_copy_string(workspace, cdr->accountcode, workspacelen);
263 else if (!strcasecmp(name, "uniqueid"))
264 ast_copy_string(workspace, cdr->uniqueid, workspacelen);
265 else if (!strcasecmp(name, "userfield"))
266 ast_copy_string(workspace, cdr->userfield, workspacelen);
267 else if ((varbuf = ast_cdr_getvar_internal(cdr, name, recur)))
268 ast_copy_string(workspace, varbuf, workspacelen);
270 if (!ast_strlen_zero(workspace))
274 /*! Set a CDR channel variable
275 \note You can't set the CDR variables that belong to the actual CDR record, like "billsec".
277 int ast_cdr_setvar(struct ast_cdr *cdr, const char *name, const char *value, int recur)
279 struct ast_var_t *newvariable;
280 struct varshead *headp;
281 const char *read_only[] = { "clid", "src", "dst", "dcontext", "channel", "dstchannel",
282 "lastapp", "lastdata", "start", "answer", "end", "duration",
283 "billsec", "disposition", "amaflags", "accountcode", "uniqueid",
287 for(x = 0; read_only[x]; x++) {
288 if (!strcasecmp(name, read_only[x])) {
289 ast_log(LOG_ERROR, "Attempt to set a read-only variable!.\n");
295 ast_log(LOG_ERROR, "Attempt to set a variable on a nonexistent CDR record.\n");
300 headp = &cdr->varshead;
301 AST_LIST_TRAVERSE_SAFE_BEGIN(headp, newvariable, entries) {
302 if (!strcasecmp(ast_var_name(newvariable), name)) {
303 /* there is already such a variable, delete it */
304 AST_LIST_REMOVE_CURRENT(headp, entries);
305 ast_var_delete(newvariable);
309 AST_LIST_TRAVERSE_SAFE_END;
312 newvariable = ast_var_assign(name, value);
313 AST_LIST_INSERT_HEAD(headp, newvariable, entries);
326 int ast_cdr_copy_vars(struct ast_cdr *to_cdr, struct ast_cdr *from_cdr)
328 struct ast_var_t *variables, *newvariable = NULL;
329 struct varshead *headpa, *headpb;
333 headpa = &from_cdr->varshead;
334 headpb = &to_cdr->varshead;
336 AST_LIST_TRAVERSE(headpa,variables,entries) {
338 (var = ast_var_name(variables)) && (val = ast_var_value(variables)) &&
339 !ast_strlen_zero(var) && !ast_strlen_zero(val)) {
340 newvariable = ast_var_assign(var, val);
341 AST_LIST_INSERT_HEAD(headpb, newvariable, entries);
349 int ast_cdr_serialize_variables(struct ast_cdr *cdr, char *buf, size_t size, char delim, char sep, int recur)
351 struct ast_var_t *variables;
355 int total = 0, x = 0, i;
356 const char *cdrcols[] = {
377 memset(buf, 0, size);
379 for (; cdr; cdr = recur ? cdr->next : NULL) {
381 ast_build_string(&buf, &size, "\n");
383 AST_LIST_TRAVERSE(&cdr->varshead, variables, entries) {
385 (var = ast_var_name(variables)) && (val = ast_var_value(variables)) &&
386 !ast_strlen_zero(var) && !ast_strlen_zero(val)) {
387 if (ast_build_string(&buf, &size, "level %d: %s%c%s%c", x, var, delim, val, sep)) {
388 ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
396 for (i = 0; i < (sizeof(cdrcols) / sizeof(cdrcols[0])); i++) {
397 ast_cdr_getvar(cdr, cdrcols[i], &tmp, workspace, sizeof(workspace), 0);
401 if (ast_build_string(&buf, &size, "level %d: %s%c%s%c", x, cdrcols[i], delim, tmp, sep)) {
402 ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
413 void ast_cdr_free_vars(struct ast_cdr *cdr, int recur)
415 struct varshead *headp;
416 struct ast_var_t *vardata;
418 /* clear variables */
420 headp = &cdr->varshead;
421 while (!AST_LIST_EMPTY(headp)) {
422 vardata = AST_LIST_REMOVE_HEAD(headp, entries);
423 ast_var_delete(vardata);
434 void ast_cdr_free(struct ast_cdr *cdr)
437 struct ast_cdr *next;
441 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
442 if (!ast_test_flag(cdr, AST_CDR_FLAG_POSTED) && !ast_test_flag(cdr, AST_CDR_FLAG_POST_DISABLED))
443 ast_log(LOG_WARNING, "CDR on channel '%s' not posted\n", chan);
444 if (ast_tvzero(cdr->end))
445 ast_log(LOG_WARNING, "CDR on channel '%s' lacks end\n", chan);
446 if (ast_tvzero(cdr->start))
447 ast_log(LOG_WARNING, "CDR on channel '%s' lacks start\n", chan);
449 ast_cdr_free_vars(cdr, 0);
455 struct ast_cdr *ast_cdr_alloc(void)
459 cdr = malloc(sizeof(*cdr));
461 memset(cdr, 0, sizeof(*cdr));
466 void ast_cdr_start(struct ast_cdr *cdr)
471 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
472 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
473 if (ast_test_flag(cdr, AST_CDR_FLAG_POSTED))
474 ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
475 if (!ast_tvzero(cdr->start))
476 ast_log(LOG_WARNING, "CDR on channel '%s' already started\n", chan);
477 cdr->start = ast_tvnow();
483 void ast_cdr_answer(struct ast_cdr *cdr)
488 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
489 if (ast_test_flag(cdr, AST_CDR_FLAG_POSTED))
490 ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
491 if (cdr->disposition < AST_CDR_ANSWERED)
492 cdr->disposition = AST_CDR_ANSWERED;
493 if (ast_tvzero(cdr->answer))
494 cdr->answer = ast_tvnow();
499 void ast_cdr_busy(struct ast_cdr *cdr)
504 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
505 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
506 if (ast_test_flag(cdr, AST_CDR_FLAG_POSTED))
507 ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
508 if (cdr->disposition < AST_CDR_BUSY)
509 cdr->disposition = AST_CDR_BUSY;
515 void ast_cdr_failed(struct ast_cdr *cdr)
520 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
521 if (ast_test_flag(cdr, AST_CDR_FLAG_POSTED))
522 ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
523 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
524 cdr->disposition = AST_CDR_FAILED;
529 int ast_cdr_disposition(struct ast_cdr *cdr, int cause)
538 case AST_CAUSE_FAILURE:
541 case AST_CAUSE_NORMAL:
543 case AST_CAUSE_NOTDEFINED:
548 ast_log(LOG_WARNING, "Cause not handled\n");
555 void ast_cdr_setdestchan(struct ast_cdr *cdr, char *chann)
560 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
561 if (ast_test_flag(cdr, AST_CDR_FLAG_POSTED))
562 ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
563 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
564 ast_copy_string(cdr->dstchannel, chann, sizeof(cdr->dstchannel));
569 void ast_cdr_setapp(struct ast_cdr *cdr, char *app, char *data)
574 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
575 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
576 if (ast_test_flag(cdr, AST_CDR_FLAG_POSTED))
577 ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
580 ast_copy_string(cdr->lastapp, app, sizeof(cdr->lastapp));
583 ast_copy_string(cdr->lastdata, data, sizeof(cdr->lastdata));
589 int ast_cdr_setcid(struct ast_cdr *cdr, struct ast_channel *c)
591 char tmp[AST_MAX_EXTENSION] = "";
595 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
596 /* Grab source from ANI or normal Caller*ID */
597 num = c->cid.cid_ani ? c->cid.cid_ani : c->cid.cid_num;
599 if (c->cid.cid_name && num)
600 snprintf(tmp, sizeof(tmp), "\"%s\" <%s>", c->cid.cid_name, num);
601 else if (c->cid.cid_name)
602 ast_copy_string(tmp, c->cid.cid_name, sizeof(tmp));
604 ast_copy_string(tmp, num, sizeof(tmp));
605 ast_copy_string(cdr->clid, tmp, sizeof(cdr->clid));
606 ast_copy_string(cdr->src, num ? num : "", sizeof(cdr->src));
615 int ast_cdr_init(struct ast_cdr *cdr, struct ast_channel *c)
619 char tmp[AST_MAX_EXTENSION] = "";
622 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
623 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
624 if (!ast_strlen_zero(cdr->channel))
625 ast_log(LOG_WARNING, "CDR already initialized on '%s'\n", chan);
626 ast_copy_string(cdr->channel, c->name, sizeof(cdr->channel));
627 /* Grab source from ANI or normal Caller*ID */
628 num = c->cid.cid_ani ? c->cid.cid_ani : c->cid.cid_num;
630 if (c->cid.cid_name && num)
631 snprintf(tmp, sizeof(tmp), "\"%s\" <%s>", c->cid.cid_name, num);
632 else if (c->cid.cid_name)
633 ast_copy_string(tmp, c->cid.cid_name, sizeof(tmp));
635 ast_copy_string(tmp, num, sizeof(tmp));
636 ast_copy_string(cdr->clid, tmp, sizeof(cdr->clid));
637 ast_copy_string(cdr->src, num ? num : "", sizeof(cdr->src));
639 cdr->disposition = (c->_state == AST_STATE_UP) ? AST_CDR_ANSWERED : AST_CDR_NOANSWER;
640 cdr->amaflags = c->amaflags ? c->amaflags : ast_default_amaflags;
641 ast_copy_string(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode));
642 /* Destination information */
643 ast_copy_string(cdr->dst, c->exten, sizeof(cdr->dst));
644 ast_copy_string(cdr->dcontext, c->context, sizeof(cdr->dcontext));
645 /* Unique call identifier */
646 ast_copy_string(cdr->uniqueid, c->uniqueid, sizeof(cdr->uniqueid));
653 void ast_cdr_end(struct ast_cdr *cdr)
658 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
659 if (ast_test_flag(cdr, AST_CDR_FLAG_POSTED))
660 ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
661 if (ast_tvzero(cdr->start))
662 ast_log(LOG_WARNING, "CDR on channel '%s' has not started\n", chan);
663 if (ast_tvzero(cdr->end))
664 cdr->end = ast_tvnow();
669 char *ast_cdr_disp2str(int disposition)
671 switch (disposition) {
672 case AST_CDR_NOANSWER:
678 case AST_CDR_ANSWERED:
684 /*! Converts AMA flag to printable string */
685 char *ast_cdr_flags2str(int flag)
690 case AST_CDR_BILLING:
692 case AST_CDR_DOCUMENTATION:
693 return "DOCUMENTATION";
698 int ast_cdr_setaccount(struct ast_channel *chan, const char *account)
700 struct ast_cdr *cdr = chan->cdr;
702 ast_copy_string(chan->accountcode, account, sizeof(chan->accountcode));
704 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
705 ast_copy_string(cdr->accountcode, chan->accountcode, sizeof(cdr->accountcode));
711 int ast_cdr_setamaflags(struct ast_channel *chan, const char *flag)
713 struct ast_cdr *cdr = chan->cdr;
716 newflag = ast_cdr_amaflags2int(flag);
718 cdr->amaflags = newflag;
723 int ast_cdr_setuserfield(struct ast_channel *chan, const char *userfield)
725 struct ast_cdr *cdr = chan->cdr;
728 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
729 ast_copy_string(cdr->userfield, userfield, sizeof(cdr->userfield));
736 int ast_cdr_appenduserfield(struct ast_channel *chan, const char *userfield)
738 struct ast_cdr *cdr = chan->cdr;
741 int len = strlen(cdr->userfield);
743 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
744 strncpy(cdr->userfield+len, userfield, sizeof(cdr->userfield) - len - 1);
752 int ast_cdr_update(struct ast_channel *c)
754 struct ast_cdr *cdr = c->cdr;
756 char tmp[AST_MAX_EXTENSION] = "";
759 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
760 num = c->cid.cid_ani ? c->cid.cid_ani : c->cid.cid_num;
762 if (c->cid.cid_name && num)
763 snprintf(tmp, sizeof(tmp), "\"%s\" <%s>", c->cid.cid_name, num);
764 else if (c->cid.cid_name)
765 ast_copy_string(tmp, c->cid.cid_name, sizeof(tmp));
767 ast_copy_string(tmp, num, sizeof(tmp));
768 ast_copy_string(cdr->clid, tmp, sizeof(cdr->clid));
769 ast_copy_string(cdr->src, num ? num : "", sizeof(cdr->src));
771 /* Copy account code et-al */
772 ast_copy_string(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode));
773 /* Destination information */
774 ast_copy_string(cdr->dst, (ast_strlen_zero(c->macroexten)) ? c->exten : c->macroexten, sizeof(cdr->dst));
775 ast_copy_string(cdr->dcontext, (ast_strlen_zero(c->macrocontext)) ? c->context : c->macrocontext, sizeof(cdr->dcontext));
783 int ast_cdr_amaflags2int(const char *flag)
785 if (!strcasecmp(flag, "default"))
787 if (!strcasecmp(flag, "omit"))
789 if (!strcasecmp(flag, "billing"))
790 return AST_CDR_BILLING;
791 if (!strcasecmp(flag, "documentation"))
792 return AST_CDR_DOCUMENTATION;
796 static void post_cdr(struct ast_cdr *cdr)
799 struct ast_cdr_beitem *i;
802 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
803 if (ast_test_flag(cdr, AST_CDR_FLAG_POSTED))
804 ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
805 if (ast_tvzero(cdr->end))
806 ast_log(LOG_WARNING, "CDR on channel '%s' lacks end\n", chan);
807 if (ast_tvzero(cdr->start))
808 ast_log(LOG_WARNING, "CDR on channel '%s' lacks start\n", chan);
809 cdr->duration = cdr->end.tv_sec - cdr->start.tv_sec + (cdr->end.tv_usec - cdr->start.tv_usec) / 1000000;
810 if (!ast_tvzero(cdr->answer))
811 cdr->billsec = cdr->end.tv_sec - cdr->answer.tv_sec + (cdr->end.tv_usec - cdr->answer.tv_usec) / 1000000;
814 ast_set_flag(cdr, AST_CDR_FLAG_POSTED);
815 AST_LIST_LOCK(&be_list);
816 AST_LIST_TRAVERSE(&be_list, i, list) {
819 AST_LIST_UNLOCK(&be_list);
824 void ast_cdr_reset(struct ast_cdr *cdr, struct ast_flags *_flags)
827 struct ast_flags flags = { 0 };
830 ast_copy_flags(&flags, _flags, AST_FLAGS_ALL);
833 /* Detach if post is requested */
834 if (ast_test_flag(&flags, AST_CDR_FLAG_LOCKED) || !ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
835 if (ast_test_flag(&flags, AST_CDR_FLAG_POSTED)) {
837 if ((dup = ast_cdr_dup(cdr))) {
840 ast_set_flag(cdr, AST_CDR_FLAG_POSTED);
843 /* clear variables */
844 if (!ast_test_flag(&flags, AST_CDR_FLAG_KEEP_VARS)) {
845 ast_cdr_free_vars(cdr, 0);
848 /* Reset to initial state */
849 ast_clear_flag(cdr, AST_FLAGS_ALL);
850 memset(&cdr->start, 0, sizeof(cdr->start));
851 memset(&cdr->end, 0, sizeof(cdr->end));
852 memset(&cdr->answer, 0, sizeof(cdr->answer));
856 cdr->disposition = AST_CDR_NOANSWER;
863 struct ast_cdr *ast_cdr_append(struct ast_cdr *cdr, struct ast_cdr *newcdr)
880 /*! \note Don't call without cdr_batch_lock */
881 static void reset_batch(void)
888 /*! \note Don't call without cdr_batch_lock */
889 static int init_batch(void)
891 /* This is the single meta-batch used to keep track of all CDRs during the entire life of the program */
892 batch = malloc(sizeof(*batch));
894 ast_log(LOG_WARNING, "CDR: out of memory while trying to handle batched records, data will most likely be lost\n");
903 static void *do_batch_backend_process(void *data)
905 struct ast_cdr_batch_item *processeditem;
906 struct ast_cdr_batch_item *batchitem = data;
908 /* Push each CDR into storage mechanism(s) and free all the memory */
910 post_cdr(batchitem->cdr);
911 ast_cdr_free(batchitem->cdr);
912 processeditem = batchitem;
913 batchitem = batchitem->next;
920 void ast_cdr_submit_batch(int shutdown)
922 struct ast_cdr_batch_item *oldbatchitems = NULL;
924 pthread_t batch_post_thread = AST_PTHREADT_NULL;
926 /* if there's no batch, or no CDRs in the batch, then there's nothing to do */
927 if (!batch || !batch->head)
930 /* move the old CDRs aside, and prepare a new CDR batch */
931 ast_mutex_lock(&cdr_batch_lock);
932 oldbatchitems = batch->head;
934 ast_mutex_unlock(&cdr_batch_lock);
936 /* if configured, spawn a new thread to post these CDRs,
937 also try to save as much as possible if we are shutting down safely */
938 if (batchscheduleronly || shutdown) {
940 ast_log(LOG_DEBUG, "CDR single-threaded batch processing begins now\n");
941 do_batch_backend_process(oldbatchitems);
943 pthread_attr_init(&attr);
944 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
945 if (ast_pthread_create(&batch_post_thread, &attr, do_batch_backend_process, oldbatchitems)) {
946 ast_log(LOG_WARNING, "CDR processing thread could not detach, now trying in this thread\n");
947 do_batch_backend_process(oldbatchitems);
950 ast_log(LOG_DEBUG, "CDR multi-threaded batch processing begins now\n");
955 static int submit_scheduled_batch(void *data)
957 ast_cdr_submit_batch(0);
958 /* manually reschedule from this point in time */
959 cdr_sched = ast_sched_add(sched, batchtime * 1000, submit_scheduled_batch, NULL);
960 /* returning zero so the scheduler does not automatically reschedule */
964 static void submit_unscheduled_batch(void)
966 /* this is okay since we are not being called from within the scheduler */
968 ast_sched_del(sched, cdr_sched);
969 /* schedule the submission to occur ASAP (1 ms) */
970 cdr_sched = ast_sched_add(sched, 1, submit_scheduled_batch, NULL);
971 /* signal the do_cdr thread to wakeup early and do some work (that lazy thread ;) */
972 ast_mutex_lock(&cdr_pending_lock);
973 ast_cond_signal(&cdr_pending_cond);
974 ast_mutex_unlock(&cdr_pending_lock);
977 void ast_cdr_detach(struct ast_cdr *cdr)
979 struct ast_cdr_batch_item *newtail;
982 /* maybe they disabled CDR stuff completely, so just drop it */
985 ast_log(LOG_DEBUG, "Dropping CDR !\n");
986 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
991 /* post stuff immediately if we are not in batch mode, this is legacy behaviour */
998 /* otherwise, each CDR gets put into a batch list (at the end) */
1000 ast_log(LOG_DEBUG, "CDR detaching from this thread\n");
1002 /* we'll need a new tail for every CDR */
1003 newtail = malloc(sizeof(*newtail));
1005 ast_log(LOG_WARNING, "CDR: out of memory while trying to detach, will try in this thread instead\n");
1010 memset(newtail, 0, sizeof(*newtail));
1012 /* don't traverse a whole list (just keep track of the tail) */
1013 ast_mutex_lock(&cdr_batch_lock);
1017 /* new batch is empty, so point the head at the new tail */
1018 batch->head = newtail;
1020 /* already got a batch with something in it, so just append a new tail */
1021 batch->tail->next = newtail;
1024 batch->tail = newtail;
1025 curr = batch->size++;
1026 ast_mutex_unlock(&cdr_batch_lock);
1028 /* if we have enough stuff to post, then do it */
1029 if (curr >= (batchsize - 1))
1030 submit_unscheduled_batch();
1033 static void *do_cdr(void *data)
1035 struct timespec timeout;
1040 struct timeval now = ast_tvnow();
1041 schedms = ast_sched_wait(sched);
1042 /* this shouldn't happen, but provide a 1 second default just in case */
1045 timeout.tv_sec = now.tv_sec + (schedms / 1000);
1046 timeout.tv_nsec = (now.tv_usec * 1000) + ((schedms % 1000) * 1000);
1047 /* prevent stuff from clobbering cdr_pending_cond, then wait on signals sent to it until the timeout expires */
1048 ast_mutex_lock(&cdr_pending_lock);
1049 ast_cond_timedwait(&cdr_pending_cond, &cdr_pending_lock, &timeout);
1050 numevents = ast_sched_runq(sched);
1051 ast_mutex_unlock(&cdr_pending_lock);
1052 if (option_debug > 1)
1053 ast_log(LOG_DEBUG, "Processed %d scheduled CDR batches from the run queue\n", numevents);
1059 static int handle_cli_status(int fd, int argc, char *argv[])
1061 struct ast_cdr_beitem *beitem=NULL;
1063 long nextbatchtime=0;
1066 return RESULT_SHOWUSAGE;
1068 ast_cli(fd, "CDR logging: %s\n", enabled ? "enabled" : "disabled");
1069 ast_cli(fd, "CDR mode: %s\n", batchmode ? "batch" : "simple");
1075 nextbatchtime = ast_sched_when(sched, cdr_sched);
1076 ast_cli(fd, "CDR safe shut down: %s\n", batchsafeshutdown ? "enabled" : "disabled");
1077 ast_cli(fd, "CDR batch threading model: %s\n", batchscheduleronly ? "scheduler only" : "scheduler plus separate threads");
1078 ast_cli(fd, "CDR current batch size: %d record%s\n", cnt, (cnt != 1) ? "s" : "");
1079 ast_cli(fd, "CDR maximum batch size: %d record%s\n", batchsize, (batchsize != 1) ? "s" : "");
1080 ast_cli(fd, "CDR maximum batch time: %d second%s\n", batchtime, (batchtime != 1) ? "s" : "");
1081 ast_cli(fd, "CDR next scheduled batch processing time: %ld second%s\n", nextbatchtime, (nextbatchtime != 1) ? "s" : "");
1083 AST_LIST_LOCK(&be_list);
1084 AST_LIST_TRAVERSE(&be_list, beitem, list) {
1085 ast_cli(fd, "CDR registered backend: %s\n", beitem->name);
1087 AST_LIST_UNLOCK(&be_list);
1093 static int handle_cli_submit(int fd, int argc, char *argv[])
1096 return RESULT_SHOWUSAGE;
1098 submit_unscheduled_batch();
1099 ast_cli(fd, "Submitted CDRs to backend engines for processing. This may take a while.\n");
1104 static struct ast_cli_entry cli_submit = {
1105 .cmda = { "cdr", "submit", NULL },
1106 .handler = handle_cli_submit,
1107 .summary = "Posts all pending batched CDR data",
1109 "Usage: cdr submit\n"
1110 " Posts all pending batched CDR data to the configured CDR backend engine modules.\n"
1113 static struct ast_cli_entry cli_status = {
1114 .cmda = { "cdr", "status", NULL },
1115 .handler = handle_cli_status,
1116 .summary = "Display the CDR status",
1118 "Usage: cdr status\n"
1119 " Displays the Call Detail Record engine system status.\n"
1122 static int do_reload(void)
1124 struct ast_config *config;
1125 const char *enabled_value;
1126 const char *batched_value;
1127 const char *scheduleronly_value;
1128 const char *batchsafeshutdown_value;
1129 const char *size_value;
1130 const char *time_value;
1136 pthread_attr_t attr;
1138 ast_mutex_lock(&cdr_batch_lock);
1140 batchsize = BATCH_SIZE_DEFAULT;
1141 batchtime = BATCH_TIME_DEFAULT;
1142 batchscheduleronly = BATCH_SCHEDULER_ONLY_DEFAULT;
1143 batchsafeshutdown = BATCH_SAFE_SHUTDOWN_DEFAULT;
1144 was_enabled = enabled;
1145 was_batchmode = batchmode;
1149 /* don't run the next scheduled CDR posting while reloading */
1151 ast_sched_del(sched, cdr_sched);
1153 if ((config = ast_config_load("cdr.conf"))) {
1154 if ((enabled_value = ast_variable_retrieve(config, "general", "enable"))) {
1155 enabled = ast_true(enabled_value);
1157 if ((batched_value = ast_variable_retrieve(config, "general", "batch"))) {
1158 batchmode = ast_true(batched_value);
1160 if ((scheduleronly_value = ast_variable_retrieve(config, "general", "scheduleronly"))) {
1161 batchscheduleronly = ast_true(scheduleronly_value);
1163 if ((batchsafeshutdown_value = ast_variable_retrieve(config, "general", "safeshutdown"))) {
1164 batchsafeshutdown = ast_true(batchsafeshutdown_value);
1166 if ((size_value = ast_variable_retrieve(config, "general", "size"))) {
1167 if (sscanf(size_value, "%d", &cfg_size) < 1)
1168 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", size_value);
1169 else if (size_value < 0)
1170 ast_log(LOG_WARNING, "Invalid maximum batch size '%d' specified, using default\n", cfg_size);
1172 batchsize = cfg_size;
1174 if ((time_value = ast_variable_retrieve(config, "general", "time"))) {
1175 if (sscanf(time_value, "%d", &cfg_time) < 1)
1176 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", time_value);
1177 else if (time_value < 0)
1178 ast_log(LOG_WARNING, "Invalid maximum batch time '%d' specified, using default\n", cfg_time);
1180 batchtime = cfg_time;
1184 if (enabled && !batchmode) {
1185 ast_log(LOG_NOTICE, "CDR simple logging enabled.\n");
1186 } else if (enabled && batchmode) {
1187 cdr_sched = ast_sched_add(sched, batchtime * 1000, submit_scheduled_batch, NULL);
1188 ast_log(LOG_NOTICE, "CDR batch mode logging enabled, first of either size %d or time %d seconds.\n", batchsize, batchtime);
1190 ast_log(LOG_NOTICE, "CDR logging disabled, data will be lost.\n");
1193 /* if this reload enabled the CDR batch mode, create the background thread
1194 if it does not exist */
1195 if (enabled && batchmode && (!was_enabled || !was_batchmode) && (cdr_thread == AST_PTHREADT_NULL)) {
1196 ast_cond_init(&cdr_pending_cond, NULL);
1197 pthread_attr_init(&attr);
1198 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1199 if (ast_pthread_create(&cdr_thread, &attr, do_cdr, NULL) < 0) {
1200 ast_log(LOG_ERROR, "Unable to start CDR thread.\n");
1201 ast_sched_del(sched, cdr_sched);
1203 ast_cli_register(&cli_submit);
1204 ast_register_atexit(ast_cdr_engine_term);
1207 /* if this reload disabled the CDR and/or batch mode and there is a background thread,
1209 } else if (((!enabled && was_enabled) || (!batchmode && was_batchmode)) && (cdr_thread != AST_PTHREADT_NULL)) {
1210 /* wake up the thread so it will exit */
1211 pthread_cancel(cdr_thread);
1212 pthread_kill(cdr_thread, SIGURG);
1213 pthread_join(cdr_thread, NULL);
1214 cdr_thread = AST_PTHREADT_NULL;
1215 ast_cond_destroy(&cdr_pending_cond);
1216 ast_cli_unregister(&cli_submit);
1217 ast_unregister_atexit(ast_cdr_engine_term);
1219 /* if leaving batch mode, then post the CDRs in the batch,
1220 and don't reschedule, since we are stopping CDR logging */
1221 if (!batchmode && was_batchmode) {
1222 ast_cdr_engine_term();
1228 ast_mutex_unlock(&cdr_batch_lock);
1229 ast_config_destroy(config);
1234 int ast_cdr_engine_init(void)
1238 sched = sched_context_create();
1240 ast_log(LOG_ERROR, "Unable to create schedule context.\n");
1244 ast_cli_register(&cli_status);
1248 ast_mutex_lock(&cdr_batch_lock);
1250 ast_mutex_unlock(&cdr_batch_lock);
1256 /* \note This actually gets called a couple of times at shutdown. Once, before we start
1257 hanging up channels, and then again, after the channel hangup timeout expires */
1258 void ast_cdr_engine_term(void)
1260 ast_cdr_submit_batch(batchsafeshutdown);
1263 void ast_cdr_engine_reload(void)