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$")
44 #include "asterisk/lock.h"
45 #include "asterisk/channel.h"
46 #include "asterisk/cdr.h"
47 #include "asterisk/logger.h"
48 #include "asterisk/callerid.h"
49 #include "asterisk/causes.h"
50 #include "asterisk/options.h"
51 #include "asterisk/linkedlists.h"
52 #include "asterisk/utils.h"
53 #include "asterisk/sched.h"
54 #include "asterisk/config.h"
55 #include "asterisk/cli.h"
56 #include "asterisk/stringfields.h"
58 /*! Default AMA flag for billing records (CDR's) */
59 int ast_default_amaflags
= AST_CDR_DOCUMENTATION
;
60 char ast_default_accountcode
[AST_MAX_ACCOUNT_CODE
];
62 struct ast_cdr_beitem
{
66 AST_LIST_ENTRY(ast_cdr_beitem
) list
;
69 static AST_LIST_HEAD_STATIC(be_list
, ast_cdr_beitem
);
71 struct ast_cdr_batch_item
{
73 struct ast_cdr_batch_item
*next
;
76 static struct ast_cdr_batch
{
78 struct ast_cdr_batch_item
*head
;
79 struct ast_cdr_batch_item
*tail
;
82 static struct sched_context
*sched
;
83 static int cdr_sched
= -1;
84 static pthread_t cdr_thread
= AST_PTHREADT_NULL
;
86 #define BATCH_SIZE_DEFAULT 100
87 #define BATCH_TIME_DEFAULT 300
88 #define BATCH_SCHEDULER_ONLY_DEFAULT 0
89 #define BATCH_SAFE_SHUTDOWN_DEFAULT 1
95 static int batchscheduleronly
;
96 static int batchsafeshutdown
;
98 AST_MUTEX_DEFINE_STATIC(cdr_batch_lock
);
100 /* these are used to wake up the CDR thread when there's work to do */
101 AST_MUTEX_DEFINE_STATIC(cdr_pending_lock
);
102 static ast_cond_t cdr_pending_cond
;
105 /*! Register a CDR driver. Each registered CDR driver generates a CDR
106 \return 0 on success, -1 on failure
108 int ast_cdr_register(const char *name
, const char *desc
, ast_cdrbe be
)
110 struct ast_cdr_beitem
*i
;
115 ast_log(LOG_WARNING
, "CDR engine '%s' lacks backend\n", name
);
119 AST_LIST_LOCK(&be_list
);
120 AST_LIST_TRAVERSE(&be_list
, i
, list
) {
121 if (!strcasecmp(name
, i
->name
))
124 AST_LIST_UNLOCK(&be_list
);
127 ast_log(LOG_WARNING
, "Already have a CDR backend called '%s'\n", name
);
131 if (!(i
= ast_calloc(1, 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(const 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
= ast_cdr_alloc();
174 memcpy(newcdr
, cdr
, sizeof(*newcdr
));
175 /* The varshead is unusable, volatile even, after the memcpy so we take care of that here */
176 memset(&newcdr
->varshead
, 0, sizeof(newcdr
->varshead
));
177 ast_cdr_copy_vars(newcdr
, cdr
);
183 static const char *ast_cdr_getvar_internal(struct ast_cdr
*cdr
, const char *name
, int recur
)
185 if (ast_strlen_zero(name
))
188 for (; cdr
; cdr
= recur
? cdr
->next
: NULL
) {
189 struct ast_var_t
*variables
;
190 struct varshead
*headp
= &cdr
->varshead
;
191 AST_LIST_TRAVERSE(headp
, variables
, entries
) {
192 if (!strcasecmp(name
, ast_var_name(variables
)))
193 return ast_var_value(variables
);
200 static void cdr_get_tv(struct timeval tv
, const char *fmt
, char *buf
, int bufsize
)
202 if (fmt
== NULL
) { /* raw mode */
203 snprintf(buf
, bufsize
, "%ld.%06ld", (long)tv
.tv_sec
, (long)tv
.tv_usec
);
205 time_t t
= tv
.tv_sec
;
208 localtime_r(&t
, &tm
);
209 strftime(buf
, bufsize
, fmt
, &tm
);
214 /*! CDR channel variable retrieval */
215 void ast_cdr_getvar(struct ast_cdr
*cdr
, const char *name
, char **ret
, char *workspace
, int workspacelen
, int recur
, int raw
)
217 const char *fmt
= "%Y-%m-%d %T";
221 /* special vars (the ones from the struct ast_cdr when requested by name)
222 I'd almost say we should convert all the stringed vals to vars */
224 if (!strcasecmp(name
, "clid"))
225 ast_copy_string(workspace
, cdr
->clid
, workspacelen
);
226 else if (!strcasecmp(name
, "src"))
227 ast_copy_string(workspace
, cdr
->src
, workspacelen
);
228 else if (!strcasecmp(name
, "dst"))
229 ast_copy_string(workspace
, cdr
->dst
, workspacelen
);
230 else if (!strcasecmp(name
, "dcontext"))
231 ast_copy_string(workspace
, cdr
->dcontext
, workspacelen
);
232 else if (!strcasecmp(name
, "channel"))
233 ast_copy_string(workspace
, cdr
->channel
, workspacelen
);
234 else if (!strcasecmp(name
, "dstchannel"))
235 ast_copy_string(workspace
, cdr
->dstchannel
, workspacelen
);
236 else if (!strcasecmp(name
, "lastapp"))
237 ast_copy_string(workspace
, cdr
->lastapp
, workspacelen
);
238 else if (!strcasecmp(name
, "lastdata"))
239 ast_copy_string(workspace
, cdr
->lastdata
, workspacelen
);
240 else if (!strcasecmp(name
, "start"))
241 cdr_get_tv(cdr
->start
, raw
? NULL
: fmt
, workspace
, workspacelen
);
242 else if (!strcasecmp(name
, "answer"))
243 cdr_get_tv(cdr
->answer
, raw
? NULL
: fmt
, workspace
, workspacelen
);
244 else if (!strcasecmp(name
, "end"))
245 cdr_get_tv(cdr
->end
, raw
? NULL
: fmt
, workspace
, workspacelen
);
246 else if (!strcasecmp(name
, "duration"))
247 snprintf(workspace
, workspacelen
, "%ld", cdr
->duration
);
248 else if (!strcasecmp(name
, "billsec"))
249 snprintf(workspace
, workspacelen
, "%ld", cdr
->billsec
);
250 else if (!strcasecmp(name
, "disposition")) {
252 snprintf(workspace
, workspacelen
, "%ld", cdr
->disposition
);
254 ast_copy_string(workspace
, ast_cdr_disp2str(cdr
->disposition
), workspacelen
);
256 } else if (!strcasecmp(name
, "amaflags")) {
258 snprintf(workspace
, workspacelen
, "%ld", cdr
->amaflags
);
260 ast_copy_string(workspace
, ast_cdr_flags2str(cdr
->amaflags
), workspacelen
);
262 } else if (!strcasecmp(name
, "accountcode"))
263 ast_copy_string(workspace
, cdr
->accountcode
, workspacelen
);
264 else if (!strcasecmp(name
, "uniqueid"))
265 ast_copy_string(workspace
, cdr
->uniqueid
, workspacelen
);
266 else if (!strcasecmp(name
, "userfield"))
267 ast_copy_string(workspace
, cdr
->userfield
, workspacelen
);
268 else if ((varbuf
= ast_cdr_getvar_internal(cdr
, name
, recur
)))
269 ast_copy_string(workspace
, varbuf
, workspacelen
);
273 if (!ast_strlen_zero(workspace
))
277 /* readonly cdr variables */
278 static const char *cdr_readonly_vars
[] = { "clid", "src", "dst", "dcontext", "channel", "dstchannel",
279 "lastapp", "lastdata", "start", "answer", "end", "duration",
280 "billsec", "disposition", "amaflags", "accountcode", "uniqueid",
282 /*! Set a CDR channel variable
283 \note You can't set the CDR variables that belong to the actual CDR record, like "billsec".
285 int ast_cdr_setvar(struct ast_cdr
*cdr
, const char *name
, const char *value
, int recur
)
287 struct ast_var_t
*newvariable
;
288 struct varshead
*headp
;
291 for(x
= 0; cdr_readonly_vars
[x
]; x
++) {
292 if (!strcasecmp(name
, cdr_readonly_vars
[x
])) {
293 ast_log(LOG_ERROR
, "Attempt to set the '%s' read-only variable!.\n", name
);
299 ast_log(LOG_ERROR
, "Attempt to set a variable on a nonexistent CDR record.\n");
303 for (; cdr
; cdr
= recur
? cdr
->next
: NULL
) {
304 headp
= &cdr
->varshead
;
305 AST_LIST_TRAVERSE_SAFE_BEGIN(headp
, newvariable
, entries
) {
306 if (!strcasecmp(ast_var_name(newvariable
), name
)) {
307 /* there is already such a variable, delete it */
308 AST_LIST_REMOVE_CURRENT(headp
, entries
);
309 ast_var_delete(newvariable
);
313 AST_LIST_TRAVERSE_SAFE_END
;
316 newvariable
= ast_var_assign(name
, value
);
317 AST_LIST_INSERT_HEAD(headp
, newvariable
, entries
);
324 int ast_cdr_copy_vars(struct ast_cdr
*to_cdr
, struct ast_cdr
*from_cdr
)
326 struct ast_var_t
*variables
, *newvariable
= NULL
;
327 struct varshead
*headpa
, *headpb
;
328 const char *var
, *val
;
331 headpa
= &from_cdr
->varshead
;
332 headpb
= &to_cdr
->varshead
;
334 AST_LIST_TRAVERSE(headpa
,variables
,entries
) {
336 (var
= ast_var_name(variables
)) && (val
= ast_var_value(variables
)) &&
337 !ast_strlen_zero(var
) && !ast_strlen_zero(val
)) {
338 newvariable
= ast_var_assign(var
, val
);
339 AST_LIST_INSERT_HEAD(headpb
, newvariable
, entries
);
347 int ast_cdr_serialize_variables(struct ast_cdr
*cdr
, char *buf
, size_t size
, char delim
, char sep
, int recur
)
349 struct ast_var_t
*variables
;
350 const char *var
, *val
;
353 int total
= 0, x
= 0, i
;
355 memset(buf
, 0, size
);
357 for (; cdr
; cdr
= recur
? cdr
->next
: NULL
) {
359 ast_build_string(&buf
, &size
, "\n");
361 AST_LIST_TRAVERSE(&cdr
->varshead
, variables
, entries
) {
363 (var
= ast_var_name(variables
)) && (val
= ast_var_value(variables
)) &&
364 !ast_strlen_zero(var
) && !ast_strlen_zero(val
)) {
365 if (ast_build_string(&buf
, &size
, "level %d: %s%c%s%c", x
, var
, delim
, val
, sep
)) {
366 ast_log(LOG_ERROR
, "Data Buffer Size Exceeded!\n");
374 for (i
= 0; cdr_readonly_vars
[i
]; i
++) {
375 ast_cdr_getvar(cdr
, cdr_readonly_vars
[i
], &tmp
, workspace
, sizeof(workspace
), 0, 0);
379 if (ast_build_string(&buf
, &size
, "level %d: %s%c%s%c", x
, cdr_readonly_vars
[i
], delim
, tmp
, sep
)) {
380 ast_log(LOG_ERROR
, "Data Buffer Size Exceeded!\n");
391 void ast_cdr_free_vars(struct ast_cdr
*cdr
, int recur
)
394 /* clear variables */
395 for (; cdr
; cdr
= recur
? cdr
->next
: NULL
) {
396 struct ast_var_t
*vardata
;
397 struct varshead
*headp
= &cdr
->varshead
;
398 while ((vardata
= AST_LIST_REMOVE_HEAD(headp
, entries
)))
399 ast_var_delete(vardata
);
403 /*! \brief print a warning if cdr already posted */
404 static void check_post(struct ast_cdr
*cdr
)
406 if (ast_test_flag(cdr
, AST_CDR_FLAG_POSTED
))
407 ast_log(LOG_NOTICE
, "CDR on channel '%s' already posted\n", S_OR(cdr
->channel
, "<unknown>"));
410 /*! \brief print a warning if cdr already started */
411 static void check_start(struct ast_cdr
*cdr
)
413 if (!ast_tvzero(cdr
->start
))
414 ast_log(LOG_NOTICE
, "CDR on channel '%s' already started\n", S_OR(cdr
->channel
, "<unknown>"));
417 void ast_cdr_free(struct ast_cdr
*cdr
)
421 struct ast_cdr
*next
= cdr
->next
;
422 char *chan
= S_OR(cdr
->channel
, "<unknown>");
423 if (!ast_test_flag(cdr
, AST_CDR_FLAG_POSTED
) && !ast_test_flag(cdr
, AST_CDR_FLAG_POST_DISABLED
))
424 ast_log(LOG_NOTICE
, "CDR on channel '%s' not posted\n", chan
);
425 if (ast_tvzero(cdr
->end
))
426 ast_log(LOG_NOTICE
, "CDR on channel '%s' lacks end\n", chan
);
427 if (ast_tvzero(cdr
->start
))
428 ast_log(LOG_NOTICE
, "CDR on channel '%s' lacks start\n", chan
);
430 ast_cdr_free_vars(cdr
, 0);
436 struct ast_cdr
*ast_cdr_alloc(void)
438 return ast_calloc(1, sizeof(struct ast_cdr
));
441 void ast_cdr_start(struct ast_cdr
*cdr
)
445 for (; cdr
; cdr
= cdr
->next
) {
446 if (!ast_test_flag(cdr
, AST_CDR_FLAG_LOCKED
)) {
447 chan
= S_OR(cdr
->channel
, "<unknown>");
450 cdr
->start
= ast_tvnow();
455 void ast_cdr_answer(struct ast_cdr
*cdr
)
458 for (; cdr
; cdr
= cdr
->next
) {
460 if (cdr
->disposition
< AST_CDR_ANSWERED
)
461 cdr
->disposition
= AST_CDR_ANSWERED
;
462 if (ast_tvzero(cdr
->answer
))
463 cdr
->answer
= ast_tvnow();
467 void ast_cdr_busy(struct ast_cdr
*cdr
)
470 for (; cdr
; cdr
= cdr
->next
) {
471 if (!ast_test_flag(cdr
, AST_CDR_FLAG_LOCKED
)) {
473 if (cdr
->disposition
< AST_CDR_BUSY
)
474 cdr
->disposition
= AST_CDR_BUSY
;
479 void ast_cdr_failed(struct ast_cdr
*cdr
)
481 for (; cdr
; cdr
= cdr
->next
) {
483 if (!ast_test_flag(cdr
, AST_CDR_FLAG_LOCKED
)) {
484 if (cdr
->disposition
< AST_CDR_FAILED
)
485 cdr
->disposition
= AST_CDR_FAILED
;
490 int ast_cdr_disposition(struct ast_cdr
*cdr
, int cause
)
494 for (; cdr
; cdr
= cdr
->next
) {
499 case AST_CAUSE_FAILURE
:
502 case AST_CAUSE_NORMAL
:
504 case AST_CAUSE_NOTDEFINED
:
509 ast_log(LOG_WARNING
, "Cause not handled\n");
515 void ast_cdr_setdestchan(struct ast_cdr
*cdr
, const char *chann
)
517 for (; cdr
; cdr
= cdr
->next
) {
519 if (!ast_test_flag(cdr
, AST_CDR_FLAG_LOCKED
))
520 ast_copy_string(cdr
->dstchannel
, chann
, sizeof(cdr
->dstchannel
));
524 void ast_cdr_setapp(struct ast_cdr
*cdr
, char *app
, char *data
)
527 for (; cdr
; cdr
= cdr
->next
) {
528 if (!ast_test_flag(cdr
, AST_CDR_FLAG_LOCKED
)) {
532 ast_copy_string(cdr
->lastapp
, app
, sizeof(cdr
->lastapp
));
535 ast_copy_string(cdr
->lastdata
, data
, sizeof(cdr
->lastdata
));
540 /* set cid info for one record */
541 static void set_one_cid(struct ast_cdr
*cdr
, struct ast_channel
*c
)
543 /* Grab source from ANI or normal Caller*ID */
544 const char *num
= S_OR(c
->cid
.cid_ani
, c
->cid
.cid_num
);
546 if (!ast_strlen_zero(c
->cid
.cid_name
)) {
547 if (!ast_strlen_zero(num
)) /* both name and number */
548 snprintf(cdr
->clid
, sizeof(cdr
->clid
), "\"%s\" <%s>", c
->cid
.cid_name
, num
);
550 ast_copy_string(cdr
->clid
, c
->cid
.cid_name
, sizeof(cdr
->clid
));
551 } else if (!ast_strlen_zero(num
)) { /* only number */
552 ast_copy_string(cdr
->clid
, num
, sizeof(cdr
->clid
));
553 } else { /* nothing known */
556 ast_copy_string(cdr
->src
, S_OR(num
, ""), sizeof(cdr
->src
));
559 int ast_cdr_setcid(struct ast_cdr
*cdr
, struct ast_channel
*c
)
561 for (; cdr
; cdr
= cdr
->next
) {
562 if (ast_test_flag(cdr
, AST_CDR_FLAG_LOCKED
))
568 int ast_cdr_init(struct ast_cdr
*cdr
, struct ast_channel
*c
)
572 for ( ; cdr
; cdr
= cdr
->next
) {
573 if (!ast_test_flag(cdr
, AST_CDR_FLAG_LOCKED
)) {
574 chan
= S_OR(cdr
->channel
, "<unknown>");
575 if (!ast_strlen_zero(cdr
->channel
))
576 ast_log(LOG_WARNING
, "CDR already initialized on '%s'\n", chan
);
577 ast_copy_string(cdr
->channel
, c
->name
, sizeof(cdr
->channel
));
580 cdr
->disposition
= (c
->_state
== AST_STATE_UP
) ? AST_CDR_ANSWERED
: AST_CDR_NOANSWER
;
581 cdr
->amaflags
= c
->amaflags
? c
->amaflags
: ast_default_amaflags
;
582 ast_copy_string(cdr
->accountcode
, c
->accountcode
, sizeof(cdr
->accountcode
));
583 /* Destination information */
584 ast_copy_string(cdr
->dst
, c
->exten
, sizeof(cdr
->dst
));
585 ast_copy_string(cdr
->dcontext
, c
->context
, sizeof(cdr
->dcontext
));
586 /* Unique call identifier */
587 ast_copy_string(cdr
->uniqueid
, c
->uniqueid
, sizeof(cdr
->uniqueid
));
593 void ast_cdr_end(struct ast_cdr
*cdr
)
595 for ( ; cdr
; cdr
= cdr
->next
) {
597 if (ast_tvzero(cdr
->end
))
598 cdr
->end
= ast_tvnow();
599 if (ast_tvzero(cdr
->start
)) {
600 ast_log(LOG_WARNING
, "CDR on channel '%s' has not started\n", S_OR(cdr
->channel
, "<unknown>"));
601 cdr
->disposition
= AST_CDR_FAILED
;
603 cdr
->duration
= cdr
->end
.tv_sec
- cdr
->start
.tv_sec
;
604 cdr
->billsec
= ast_tvzero(cdr
->answer
) ? 0 : cdr
->end
.tv_sec
- cdr
->answer
.tv_sec
;
608 char *ast_cdr_disp2str(int disposition
)
610 switch (disposition
) {
611 case AST_CDR_NOANSWER
:
617 case AST_CDR_ANSWERED
:
623 /*! Converts AMA flag to printable string */
624 char *ast_cdr_flags2str(int flag
)
629 case AST_CDR_BILLING
:
631 case AST_CDR_DOCUMENTATION
:
632 return "DOCUMENTATION";
637 int ast_cdr_setaccount(struct ast_channel
*chan
, const char *account
)
639 struct ast_cdr
*cdr
= chan
->cdr
;
641 ast_string_field_set(chan
, accountcode
, account
);
642 for ( ; cdr
; cdr
= cdr
->next
) {
643 if (!ast_test_flag(cdr
, AST_CDR_FLAG_LOCKED
))
644 ast_copy_string(cdr
->accountcode
, chan
->accountcode
, sizeof(cdr
->accountcode
));
649 int ast_cdr_setamaflags(struct ast_channel
*chan
, const char *flag
)
652 int newflag
= ast_cdr_amaflags2int(flag
);
654 for (cdr
= chan
->cdr
; cdr
; cdr
= cdr
->next
)
655 cdr
->amaflags
= newflag
;
661 int ast_cdr_setuserfield(struct ast_channel
*chan
, const char *userfield
)
663 struct ast_cdr
*cdr
= chan
->cdr
;
665 for ( ; cdr
; cdr
= cdr
->next
) {
666 if (!ast_test_flag(cdr
, AST_CDR_FLAG_LOCKED
))
667 ast_copy_string(cdr
->userfield
, userfield
, sizeof(cdr
->userfield
));
673 int ast_cdr_appenduserfield(struct ast_channel
*chan
, const char *userfield
)
675 struct ast_cdr
*cdr
= chan
->cdr
;
677 for ( ; cdr
; cdr
= cdr
->next
) {
678 int len
= strlen(cdr
->userfield
);
680 if (!ast_test_flag(cdr
, AST_CDR_FLAG_LOCKED
))
681 ast_copy_string(cdr
->userfield
+ len
, userfield
, sizeof(cdr
->userfield
) - len
);
687 int ast_cdr_update(struct ast_channel
*c
)
689 struct ast_cdr
*cdr
= c
->cdr
;
691 for ( ; cdr
; cdr
= cdr
->next
) {
692 if (!ast_test_flag(cdr
, AST_CDR_FLAG_LOCKED
)) {
695 /* Copy account code et-al */
696 ast_copy_string(cdr
->accountcode
, c
->accountcode
, sizeof(cdr
->accountcode
));
697 /* Destination information */ /* XXX privilege macro* ? */
698 ast_copy_string(cdr
->dst
, S_OR(c
->macroexten
, c
->exten
), sizeof(cdr
->dst
));
699 ast_copy_string(cdr
->dcontext
, S_OR(c
->macrocontext
, c
->context
), sizeof(cdr
->dcontext
));
706 int ast_cdr_amaflags2int(const char *flag
)
708 if (!strcasecmp(flag
, "default"))
710 if (!strcasecmp(flag
, "omit"))
712 if (!strcasecmp(flag
, "billing"))
713 return AST_CDR_BILLING
;
714 if (!strcasecmp(flag
, "documentation"))
715 return AST_CDR_DOCUMENTATION
;
719 static void post_cdr(struct ast_cdr
*cdr
)
722 struct ast_cdr_beitem
*i
;
724 for ( ; cdr
; cdr
= cdr
->next
) {
725 chan
= S_OR(cdr
->channel
, "<unknown>");
727 if (ast_tvzero(cdr
->end
))
728 ast_log(LOG_WARNING
, "CDR on channel '%s' lacks end\n", chan
);
729 if (ast_tvzero(cdr
->start
))
730 ast_log(LOG_WARNING
, "CDR on channel '%s' lacks start\n", chan
);
731 ast_set_flag(cdr
, AST_CDR_FLAG_POSTED
);
732 AST_LIST_LOCK(&be_list
);
733 AST_LIST_TRAVERSE(&be_list
, i
, list
) {
736 AST_LIST_UNLOCK(&be_list
);
740 void ast_cdr_reset(struct ast_cdr
*cdr
, struct ast_flags
*_flags
)
743 struct ast_flags flags
= { 0 };
746 ast_copy_flags(&flags
, _flags
, AST_FLAGS_ALL
);
748 for ( ; cdr
; cdr
= cdr
->next
) {
749 /* Detach if post is requested */
750 if (ast_test_flag(&flags
, AST_CDR_FLAG_LOCKED
) || !ast_test_flag(cdr
, AST_CDR_FLAG_LOCKED
)) {
751 if (ast_test_flag(&flags
, AST_CDR_FLAG_POSTED
)) {
753 if ((dup
= ast_cdr_dup(cdr
))) {
756 ast_set_flag(cdr
, AST_CDR_FLAG_POSTED
);
759 /* clear variables */
760 if (!ast_test_flag(&flags
, AST_CDR_FLAG_KEEP_VARS
)) {
761 ast_cdr_free_vars(cdr
, 0);
764 /* Reset to initial state */
765 ast_clear_flag(cdr
, AST_FLAGS_ALL
);
766 memset(&cdr
->start
, 0, sizeof(cdr
->start
));
767 memset(&cdr
->end
, 0, sizeof(cdr
->end
));
768 memset(&cdr
->answer
, 0, sizeof(cdr
->answer
));
772 cdr
->disposition
= AST_CDR_NOANSWER
;
777 struct ast_cdr
*ast_cdr_append(struct ast_cdr
*cdr
, struct ast_cdr
*newcdr
)
794 /*! \note Don't call without cdr_batch_lock */
795 static void reset_batch(void)
802 /*! \note Don't call without cdr_batch_lock */
803 static int init_batch(void)
805 /* This is the single meta-batch used to keep track of all CDRs during the entire life of the program */
806 if (!(batch
= ast_malloc(sizeof(*batch
))))
814 static void *do_batch_backend_process(void *data
)
816 struct ast_cdr_batch_item
*processeditem
;
817 struct ast_cdr_batch_item
*batchitem
= data
;
819 /* Push each CDR into storage mechanism(s) and free all the memory */
821 post_cdr(batchitem
->cdr
);
822 ast_cdr_free(batchitem
->cdr
);
823 processeditem
= batchitem
;
824 batchitem
= batchitem
->next
;
831 void ast_cdr_submit_batch(int shutdown
)
833 struct ast_cdr_batch_item
*oldbatchitems
= NULL
;
835 pthread_t batch_post_thread
= AST_PTHREADT_NULL
;
837 /* if there's no batch, or no CDRs in the batch, then there's nothing to do */
838 if (!batch
|| !batch
->head
)
841 /* move the old CDRs aside, and prepare a new CDR batch */
842 ast_mutex_lock(&cdr_batch_lock
);
843 oldbatchitems
= batch
->head
;
845 ast_mutex_unlock(&cdr_batch_lock
);
847 /* if configured, spawn a new thread to post these CDRs,
848 also try to save as much as possible if we are shutting down safely */
849 if (batchscheduleronly
|| shutdown
) {
851 ast_log(LOG_DEBUG
, "CDR single-threaded batch processing begins now\n");
852 do_batch_backend_process(oldbatchitems
);
854 pthread_attr_init(&attr
);
855 pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
856 if (ast_pthread_create_background(&batch_post_thread
, &attr
, do_batch_backend_process
, oldbatchitems
)) {
857 ast_log(LOG_WARNING
, "CDR processing thread could not detach, now trying in this thread\n");
858 do_batch_backend_process(oldbatchitems
);
861 ast_log(LOG_DEBUG
, "CDR multi-threaded batch processing begins now\n");
866 static int submit_scheduled_batch(void *data
)
868 ast_cdr_submit_batch(0);
869 /* manually reschedule from this point in time */
870 cdr_sched
= ast_sched_add(sched
, batchtime
* 1000, submit_scheduled_batch
, NULL
);
871 /* returning zero so the scheduler does not automatically reschedule */
875 static void submit_unscheduled_batch(void)
877 /* this is okay since we are not being called from within the scheduler */
879 ast_sched_del(sched
, cdr_sched
);
880 /* schedule the submission to occur ASAP (1 ms) */
881 cdr_sched
= ast_sched_add(sched
, 1, submit_scheduled_batch
, NULL
);
882 /* signal the do_cdr thread to wakeup early and do some work (that lazy thread ;) */
883 ast_mutex_lock(&cdr_pending_lock
);
884 ast_cond_signal(&cdr_pending_cond
);
885 ast_mutex_unlock(&cdr_pending_lock
);
888 void ast_cdr_detach(struct ast_cdr
*cdr
)
890 struct ast_cdr_batch_item
*newtail
;
893 /* maybe they disabled CDR stuff completely, so just drop it */
896 ast_log(LOG_DEBUG
, "Dropping CDR !\n");
897 ast_set_flag(cdr
, AST_CDR_FLAG_POST_DISABLED
);
902 /* post stuff immediately if we are not in batch mode, this is legacy behaviour */
909 /* otherwise, each CDR gets put into a batch list (at the end) */
911 ast_log(LOG_DEBUG
, "CDR detaching from this thread\n");
913 /* we'll need a new tail for every CDR */
914 if (!(newtail
= ast_calloc(1, sizeof(*newtail
)))) {
920 /* don't traverse a whole list (just keep track of the tail) */
921 ast_mutex_lock(&cdr_batch_lock
);
925 /* new batch is empty, so point the head at the new tail */
926 batch
->head
= newtail
;
928 /* already got a batch with something in it, so just append a new tail */
929 batch
->tail
->next
= newtail
;
932 batch
->tail
= newtail
;
933 curr
= batch
->size
++;
934 ast_mutex_unlock(&cdr_batch_lock
);
936 /* if we have enough stuff to post, then do it */
937 if (curr
>= (batchsize
- 1))
938 submit_unscheduled_batch();
941 static void *do_cdr(void *data
)
943 struct timespec timeout
;
949 schedms
= ast_sched_wait(sched
);
950 /* this shouldn't happen, but provide a 1 second default just in case */
953 now
= ast_tvadd(ast_tvnow(), ast_samp2tv(schedms
, 1000));
954 timeout
.tv_sec
= now
.tv_sec
;
955 timeout
.tv_nsec
= now
.tv_usec
* 1000;
956 /* prevent stuff from clobbering cdr_pending_cond, then wait on signals sent to it until the timeout expires */
957 ast_mutex_lock(&cdr_pending_lock
);
958 ast_cond_timedwait(&cdr_pending_cond
, &cdr_pending_lock
, &timeout
);
959 numevents
= ast_sched_runq(sched
);
960 ast_mutex_unlock(&cdr_pending_lock
);
961 if (option_debug
> 1)
962 ast_log(LOG_DEBUG
, "Processed %d scheduled CDR batches from the run queue\n", numevents
);
968 static int handle_cli_status(int fd
, int argc
, char *argv
[])
970 struct ast_cdr_beitem
*beitem
=NULL
;
972 long nextbatchtime
=0;
975 return RESULT_SHOWUSAGE
;
977 ast_cli(fd
, "CDR logging: %s\n", enabled
? "enabled" : "disabled");
978 ast_cli(fd
, "CDR mode: %s\n", batchmode
? "batch" : "simple");
984 nextbatchtime
= ast_sched_when(sched
, cdr_sched
);
985 ast_cli(fd
, "CDR safe shut down: %s\n", batchsafeshutdown
? "enabled" : "disabled");
986 ast_cli(fd
, "CDR batch threading model: %s\n", batchscheduleronly
? "scheduler only" : "scheduler plus separate threads");
987 ast_cli(fd
, "CDR current batch size: %d record%s\n", cnt
, (cnt
!= 1) ? "s" : "");
988 ast_cli(fd
, "CDR maximum batch size: %d record%s\n", batchsize
, (batchsize
!= 1) ? "s" : "");
989 ast_cli(fd
, "CDR maximum batch time: %d second%s\n", batchtime
, (batchtime
!= 1) ? "s" : "");
990 ast_cli(fd
, "CDR next scheduled batch processing time: %ld second%s\n", nextbatchtime
, (nextbatchtime
!= 1) ? "s" : "");
992 AST_LIST_LOCK(&be_list
);
993 AST_LIST_TRAVERSE(&be_list
, beitem
, list
) {
994 ast_cli(fd
, "CDR registered backend: %s\n", beitem
->name
);
996 AST_LIST_UNLOCK(&be_list
);
1002 static int handle_cli_submit(int fd
, int argc
, char *argv
[])
1005 return RESULT_SHOWUSAGE
;
1007 submit_unscheduled_batch();
1008 ast_cli(fd
, "Submitted CDRs to backend engines for processing. This may take a while.\n");
1013 static struct ast_cli_entry cli_submit
= {
1014 { "cdr", "submit", NULL
},
1015 handle_cli_submit
, "Posts all pending batched CDR data",
1016 "Usage: cdr submit\n"
1017 " Posts all pending batched CDR data to the configured CDR backend engine modules.\n"
1020 static struct ast_cli_entry cli_status
= {
1021 { "cdr", "status", NULL
},
1022 handle_cli_status
, "Display the CDR status",
1023 "Usage: cdr status\n"
1024 " Displays the Call Detail Record engine system status.\n"
1027 static int do_reload(void)
1029 struct ast_config
*config
;
1030 const char *enabled_value
;
1031 const char *batched_value
;
1032 const char *scheduleronly_value
;
1033 const char *batchsafeshutdown_value
;
1034 const char *size_value
;
1035 const char *time_value
;
1036 const char *end_before_h_value
;
1043 ast_mutex_lock(&cdr_batch_lock
);
1045 batchsize
= BATCH_SIZE_DEFAULT
;
1046 batchtime
= BATCH_TIME_DEFAULT
;
1047 batchscheduleronly
= BATCH_SCHEDULER_ONLY_DEFAULT
;
1048 batchsafeshutdown
= BATCH_SAFE_SHUTDOWN_DEFAULT
;
1049 was_enabled
= enabled
;
1050 was_batchmode
= batchmode
;
1054 /* don't run the next scheduled CDR posting while reloading */
1056 ast_sched_del(sched
, cdr_sched
);
1058 if ((config
= ast_config_load("cdr.conf"))) {
1059 if ((enabled_value
= ast_variable_retrieve(config
, "general", "enable"))) {
1060 enabled
= ast_true(enabled_value
);
1062 if ((batched_value
= ast_variable_retrieve(config
, "general", "batch"))) {
1063 batchmode
= ast_true(batched_value
);
1065 if ((scheduleronly_value
= ast_variable_retrieve(config
, "general", "scheduleronly"))) {
1066 batchscheduleronly
= ast_true(scheduleronly_value
);
1068 if ((batchsafeshutdown_value
= ast_variable_retrieve(config
, "general", "safeshutdown"))) {
1069 batchsafeshutdown
= ast_true(batchsafeshutdown_value
);
1071 if ((size_value
= ast_variable_retrieve(config
, "general", "size"))) {
1072 if (sscanf(size_value
, "%d", &cfg_size
) < 1)
1073 ast_log(LOG_WARNING
, "Unable to convert '%s' to a numeric value.\n", size_value
);
1074 else if (size_value
< 0)
1075 ast_log(LOG_WARNING
, "Invalid maximum batch size '%d' specified, using default\n", cfg_size
);
1077 batchsize
= cfg_size
;
1079 if ((time_value
= ast_variable_retrieve(config
, "general", "time"))) {
1080 if (sscanf(time_value
, "%d", &cfg_time
) < 1)
1081 ast_log(LOG_WARNING
, "Unable to convert '%s' to a numeric value.\n", time_value
);
1082 else if (time_value
< 0)
1083 ast_log(LOG_WARNING
, "Invalid maximum batch time '%d' specified, using default\n", cfg_time
);
1085 batchtime
= cfg_time
;
1087 if ((end_before_h_value
= ast_variable_retrieve(config
, "general", "endbeforehexten")))
1088 ast_set2_flag(&ast_options
, ast_true(end_before_h_value
), AST_OPT_FLAG_END_CDR_BEFORE_H_EXTEN
);
1091 if (enabled
&& !batchmode
) {
1092 ast_log(LOG_NOTICE
, "CDR simple logging enabled.\n");
1093 } else if (enabled
&& batchmode
) {
1094 cdr_sched
= ast_sched_add(sched
, batchtime
* 1000, submit_scheduled_batch
, NULL
);
1095 ast_log(LOG_NOTICE
, "CDR batch mode logging enabled, first of either size %d or time %d seconds.\n", batchsize
, batchtime
);
1097 ast_log(LOG_NOTICE
, "CDR logging disabled, data will be lost.\n");
1100 /* if this reload enabled the CDR batch mode, create the background thread
1101 if it does not exist */
1102 if (enabled
&& batchmode
&& (!was_enabled
|| !was_batchmode
) && (cdr_thread
== AST_PTHREADT_NULL
)) {
1103 ast_cond_init(&cdr_pending_cond
, NULL
);
1104 if (ast_pthread_create_background(&cdr_thread
, NULL
, do_cdr
, NULL
) < 0) {
1105 ast_log(LOG_ERROR
, "Unable to start CDR thread.\n");
1106 ast_sched_del(sched
, cdr_sched
);
1108 ast_cli_register(&cli_submit
);
1109 ast_register_atexit(ast_cdr_engine_term
);
1112 /* if this reload disabled the CDR and/or batch mode and there is a background thread,
1114 } else if (((!enabled
&& was_enabled
) || (!batchmode
&& was_batchmode
)) && (cdr_thread
!= AST_PTHREADT_NULL
)) {
1115 /* wake up the thread so it will exit */
1116 pthread_cancel(cdr_thread
);
1117 pthread_kill(cdr_thread
, SIGURG
);
1118 pthread_join(cdr_thread
, NULL
);
1119 cdr_thread
= AST_PTHREADT_NULL
;
1120 ast_cond_destroy(&cdr_pending_cond
);
1121 ast_cli_unregister(&cli_submit
);
1122 ast_unregister_atexit(ast_cdr_engine_term
);
1124 /* if leaving batch mode, then post the CDRs in the batch,
1125 and don't reschedule, since we are stopping CDR logging */
1126 if (!batchmode
&& was_batchmode
) {
1127 ast_cdr_engine_term();
1133 ast_mutex_unlock(&cdr_batch_lock
);
1134 ast_config_destroy(config
);
1139 int ast_cdr_engine_init(void)
1143 sched
= sched_context_create();
1145 ast_log(LOG_ERROR
, "Unable to create schedule context.\n");
1149 ast_cli_register(&cli_status
);
1153 ast_mutex_lock(&cdr_batch_lock
);
1155 ast_mutex_unlock(&cdr_batch_lock
);
1161 /* \note This actually gets called a couple of times at shutdown. Once, before we start
1162 hanging up channels, and then again, after the channel hangup timeout expires */
1163 void ast_cdr_engine_term(void)
1165 ast_cdr_submit_batch(batchsafeshutdown
);
1168 int ast_cdr_engine_reload(void)