remove the dlfcn compatibility stuff, because no platforms that Asterisk currently...
[asterisk-bristuff.git] / main / cdr.c
blob3afe07aad6efeafc7d5fd73c97b5b2a678306103
1 /*
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.
19 /*! \file
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.
34 #include "asterisk.h"
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
38 #include <unistd.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <stdio.h>
42 #include <signal.h>
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 {
63 char name[20];
64 char desc[80];
65 ast_cdrbe be;
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 {
72 struct ast_cdr *cdr;
73 struct ast_cdr_batch_item *next;
76 static struct ast_cdr_batch {
77 int size;
78 struct ast_cdr_batch_item *head;
79 struct ast_cdr_batch_item *tail;
80 } *batch = NULL;
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
91 static int enabled; /*! Is the CDR subsystem enabled ? */
92 static int unanswered;
93 static int batchmode;
94 static int batchsize;
95 static int batchtime;
96 static int batchscheduleronly;
97 static int batchsafeshutdown;
99 AST_MUTEX_DEFINE_STATIC(cdr_batch_lock);
101 /* these are used to wake up the CDR thread when there's work to do */
102 AST_MUTEX_DEFINE_STATIC(cdr_pending_lock);
103 static ast_cond_t cdr_pending_cond;
106 /*! Register a CDR driver. Each registered CDR driver generates a CDR
107 \return 0 on success, -1 on failure
109 int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be)
111 struct ast_cdr_beitem *i;
113 if (!name)
114 return -1;
115 if (!be) {
116 ast_log(LOG_WARNING, "CDR engine '%s' lacks backend\n", name);
117 return -1;
120 AST_LIST_LOCK(&be_list);
121 AST_LIST_TRAVERSE(&be_list, i, list) {
122 if (!strcasecmp(name, i->name))
123 break;
125 AST_LIST_UNLOCK(&be_list);
127 if (i) {
128 ast_log(LOG_WARNING, "Already have a CDR backend called '%s'\n", name);
129 return -1;
132 if (!(i = ast_calloc(1, sizeof(*i))))
133 return -1;
135 i->be = be;
136 ast_copy_string(i->name, name, sizeof(i->name));
137 ast_copy_string(i->desc, desc, sizeof(i->desc));
139 AST_LIST_LOCK(&be_list);
140 AST_LIST_INSERT_HEAD(&be_list, i, list);
141 AST_LIST_UNLOCK(&be_list);
143 return 0;
146 /*! unregister a CDR driver */
147 void ast_cdr_unregister(const char *name)
149 struct ast_cdr_beitem *i = NULL;
151 AST_LIST_LOCK(&be_list);
152 AST_LIST_TRAVERSE_SAFE_BEGIN(&be_list, i, list) {
153 if (!strcasecmp(name, i->name)) {
154 AST_LIST_REMOVE_CURRENT(&be_list, list);
155 if (option_verbose > 1)
156 ast_verbose(VERBOSE_PREFIX_2 "Unregistered '%s' CDR backend\n", name);
157 free(i);
158 break;
161 AST_LIST_TRAVERSE_SAFE_END;
162 AST_LIST_UNLOCK(&be_list);
165 /*! Duplicate a CDR record
166 \returns Pointer to new CDR record
168 struct ast_cdr *ast_cdr_dup(struct ast_cdr *cdr)
170 struct ast_cdr *newcdr;
172 if (!cdr) /* don't die if we get a null cdr pointer */
173 return NULL;
174 newcdr = ast_cdr_alloc();
175 if (!newcdr)
176 return NULL;
178 memcpy(newcdr, cdr, sizeof(*newcdr));
179 /* The varshead is unusable, volatile even, after the memcpy so we take care of that here */
180 memset(&newcdr->varshead, 0, sizeof(newcdr->varshead));
181 ast_cdr_copy_vars(newcdr, cdr);
182 newcdr->next = NULL;
184 return newcdr;
187 static const char *ast_cdr_getvar_internal(struct ast_cdr *cdr, const char *name, int recur)
189 if (ast_strlen_zero(name))
190 return NULL;
192 for (; cdr; cdr = recur ? cdr->next : NULL) {
193 struct ast_var_t *variables;
194 struct varshead *headp = &cdr->varshead;
195 AST_LIST_TRAVERSE(headp, variables, entries) {
196 if (!strcasecmp(name, ast_var_name(variables)))
197 return ast_var_value(variables);
201 return NULL;
204 static void cdr_get_tv(struct timeval tv, const char *fmt, char *buf, int bufsize)
206 if (fmt == NULL) { /* raw mode */
207 snprintf(buf, bufsize, "%ld.%06ld", (long)tv.tv_sec, (long)tv.tv_usec);
208 } else {
209 time_t t = tv.tv_sec;
210 if (t) {
211 struct tm tm;
213 ast_localtime(&t, &tm, NULL);
214 strftime(buf, bufsize, fmt, &tm);
219 /*! CDR channel variable retrieval */
220 void ast_cdr_getvar(struct ast_cdr *cdr, const char *name, char **ret, char *workspace, int workspacelen, int recur, int raw)
222 const char *fmt = "%Y-%m-%d %T";
223 const char *varbuf;
225 if (!cdr) /* don't die if the cdr is null */
226 return;
228 *ret = NULL;
229 /* special vars (the ones from the struct ast_cdr when requested by name)
230 I'd almost say we should convert all the stringed vals to vars */
232 if (!strcasecmp(name, "clid"))
233 ast_copy_string(workspace, cdr->clid, workspacelen);
234 else if (!strcasecmp(name, "src"))
235 ast_copy_string(workspace, cdr->src, workspacelen);
236 else if (!strcasecmp(name, "dst"))
237 ast_copy_string(workspace, cdr->dst, workspacelen);
238 else if (!strcasecmp(name, "dcontext"))
239 ast_copy_string(workspace, cdr->dcontext, workspacelen);
240 else if (!strcasecmp(name, "channel"))
241 ast_copy_string(workspace, cdr->channel, workspacelen);
242 else if (!strcasecmp(name, "dstchannel"))
243 ast_copy_string(workspace, cdr->dstchannel, workspacelen);
244 else if (!strcasecmp(name, "lastapp"))
245 ast_copy_string(workspace, cdr->lastapp, workspacelen);
246 else if (!strcasecmp(name, "lastdata"))
247 ast_copy_string(workspace, cdr->lastdata, workspacelen);
248 else if (!strcasecmp(name, "start"))
249 cdr_get_tv(cdr->start, raw ? NULL : fmt, workspace, workspacelen);
250 else if (!strcasecmp(name, "answer"))
251 cdr_get_tv(cdr->answer, raw ? NULL : fmt, workspace, workspacelen);
252 else if (!strcasecmp(name, "end"))
253 cdr_get_tv(cdr->end, raw ? NULL : fmt, workspace, workspacelen);
254 else if (!strcasecmp(name, "duration"))
255 snprintf(workspace, workspacelen, "%ld", cdr->duration);
256 else if (!strcasecmp(name, "billsec"))
257 snprintf(workspace, workspacelen, "%ld", cdr->billsec);
258 else if (!strcasecmp(name, "disposition")) {
259 if (raw) {
260 snprintf(workspace, workspacelen, "%ld", cdr->disposition);
261 } else {
262 ast_copy_string(workspace, ast_cdr_disp2str(cdr->disposition), workspacelen);
264 } else if (!strcasecmp(name, "amaflags")) {
265 if (raw) {
266 snprintf(workspace, workspacelen, "%ld", cdr->amaflags);
267 } else {
268 ast_copy_string(workspace, ast_cdr_flags2str(cdr->amaflags), workspacelen);
270 } else if (!strcasecmp(name, "accountcode"))
271 ast_copy_string(workspace, cdr->accountcode, workspacelen);
272 else if (!strcasecmp(name, "uniqueid"))
273 ast_copy_string(workspace, cdr->uniqueid, workspacelen);
274 else if (!strcasecmp(name, "userfield"))
275 ast_copy_string(workspace, cdr->userfield, workspacelen);
276 else if ((varbuf = ast_cdr_getvar_internal(cdr, name, recur)))
277 ast_copy_string(workspace, varbuf, workspacelen);
278 else
279 workspace[0] = '\0';
281 if (!ast_strlen_zero(workspace))
282 *ret = workspace;
285 /* readonly cdr variables */
286 static const char *cdr_readonly_vars[] = { "clid", "src", "dst", "dcontext", "channel", "dstchannel",
287 "lastapp", "lastdata", "start", "answer", "end", "duration",
288 "billsec", "disposition", "amaflags", "accountcode", "uniqueid",
289 "userfield", NULL };
290 /*! Set a CDR channel variable
291 \note You can't set the CDR variables that belong to the actual CDR record, like "billsec".
293 int ast_cdr_setvar(struct ast_cdr *cdr, const char *name, const char *value, int recur)
295 struct ast_var_t *newvariable;
296 struct varshead *headp;
297 int x;
299 if (!cdr) /* don't die if the cdr is null */
300 return -1;
302 for(x = 0; cdr_readonly_vars[x]; x++) {
303 if (!strcasecmp(name, cdr_readonly_vars[x])) {
304 ast_log(LOG_ERROR, "Attempt to set the '%s' read-only variable!.\n", name);
305 return -1;
309 if (!cdr) {
310 ast_log(LOG_ERROR, "Attempt to set a variable on a nonexistent CDR record.\n");
311 return -1;
314 for (; cdr; cdr = recur ? cdr->next : NULL) {
315 if (ast_test_flag(cdr, AST_CDR_FLAG_DONT_TOUCH) && ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
316 continue;
317 headp = &cdr->varshead;
318 AST_LIST_TRAVERSE_SAFE_BEGIN(headp, newvariable, entries) {
319 if (!strcasecmp(ast_var_name(newvariable), name)) {
320 /* there is already such a variable, delete it */
321 AST_LIST_REMOVE_CURRENT(headp, entries);
322 ast_var_delete(newvariable);
323 break;
326 AST_LIST_TRAVERSE_SAFE_END;
328 if (value) {
329 newvariable = ast_var_assign(name, value);
330 AST_LIST_INSERT_HEAD(headp, newvariable, entries);
334 return 0;
337 int ast_cdr_copy_vars(struct ast_cdr *to_cdr, struct ast_cdr *from_cdr)
339 struct ast_var_t *variables, *newvariable = NULL;
340 struct varshead *headpa, *headpb;
341 const char *var, *val;
342 int x = 0;
344 if (!to_cdr || !from_cdr) /* don't die if one of the pointers is null */
345 return 0;
347 headpa = &from_cdr->varshead;
348 headpb = &to_cdr->varshead;
350 AST_LIST_TRAVERSE(headpa,variables,entries) {
351 if (variables &&
352 (var = ast_var_name(variables)) && (val = ast_var_value(variables)) &&
353 !ast_strlen_zero(var) && !ast_strlen_zero(val)) {
354 newvariable = ast_var_assign(var, val);
355 AST_LIST_INSERT_HEAD(headpb, newvariable, entries);
356 x++;
360 return x;
363 int ast_cdr_serialize_variables(struct ast_cdr *cdr, char *buf, size_t size, char delim, char sep, int recur)
365 struct ast_var_t *variables;
366 const char *var, *val;
367 char *tmp;
368 char workspace[256];
369 int total = 0, x = 0, i;
371 memset(buf, 0, size);
373 for (; cdr; cdr = recur ? cdr->next : NULL) {
374 if (++x > 1)
375 ast_build_string(&buf, &size, "\n");
377 AST_LIST_TRAVERSE(&cdr->varshead, variables, entries) {
378 if (variables &&
379 (var = ast_var_name(variables)) && (val = ast_var_value(variables)) &&
380 !ast_strlen_zero(var) && !ast_strlen_zero(val)) {
381 if (ast_build_string(&buf, &size, "level %d: %s%c%s%c", x, var, delim, val, sep)) {
382 ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
383 break;
384 } else
385 total++;
386 } else
387 break;
390 for (i = 0; cdr_readonly_vars[i]; i++) {
391 workspace[0] = 0; /* null out the workspace, because the cdr_get_tv() won't write anything if time is NULL, so you get old vals */
392 ast_cdr_getvar(cdr, cdr_readonly_vars[i], &tmp, workspace, sizeof(workspace), 0, 0);
393 if (!tmp)
394 continue;
396 if (ast_build_string(&buf, &size, "level %d: %s%c%s%c", x, cdr_readonly_vars[i], delim, tmp, sep)) {
397 ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
398 break;
399 } else
400 total++;
404 return total;
408 void ast_cdr_free_vars(struct ast_cdr *cdr, int recur)
411 /* clear variables */
412 for (; cdr; cdr = recur ? cdr->next : NULL) {
413 struct ast_var_t *vardata;
414 struct varshead *headp = &cdr->varshead;
415 while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
416 ast_var_delete(vardata);
420 /*! \brief print a warning if cdr already posted */
421 static void check_post(struct ast_cdr *cdr)
423 if (!cdr)
424 return;
425 if (ast_test_flag(cdr, AST_CDR_FLAG_POSTED))
426 ast_log(LOG_NOTICE, "CDR on channel '%s' already posted\n", S_OR(cdr->channel, "<unknown>"));
429 void ast_cdr_free(struct ast_cdr *cdr)
432 while (cdr) {
433 struct ast_cdr *next = cdr->next;
434 char *chan = S_OR(cdr->channel, "<unknown>");
435 if (!ast_test_flag(cdr, AST_CDR_FLAG_POSTED) && !ast_test_flag(cdr, AST_CDR_FLAG_POST_DISABLED))
436 ast_log(LOG_NOTICE, "CDR on channel '%s' not posted\n", chan);
437 if (ast_tvzero(cdr->end))
438 ast_log(LOG_NOTICE, "CDR on channel '%s' lacks end\n", chan);
439 if (ast_tvzero(cdr->start))
440 ast_log(LOG_NOTICE, "CDR on channel '%s' lacks start\n", chan);
442 ast_cdr_free_vars(cdr, 0);
443 free(cdr);
444 cdr = next;
448 /*! \brief the same as a cdr_free call, only with no checks; just get rid of it */
449 void ast_cdr_discard(struct ast_cdr *cdr)
451 while (cdr) {
452 struct ast_cdr *next = cdr->next;
454 ast_cdr_free_vars(cdr, 0);
455 free(cdr);
456 cdr = next;
460 struct ast_cdr *ast_cdr_alloc(void)
462 struct ast_cdr *x = ast_calloc(1, sizeof(struct ast_cdr));
463 if (!x)
464 ast_log(LOG_ERROR,"Allocation Failure for a CDR!\n");
465 return x;
468 static void cdr_merge_vars(struct ast_cdr *to, struct ast_cdr *from)
470 struct ast_var_t *variablesfrom,*variablesto;
471 struct varshead *headpfrom = &to->varshead;
472 struct varshead *headpto = &from->varshead;
473 AST_LIST_TRAVERSE_SAFE_BEGIN(headpfrom, variablesfrom, entries) {
474 /* for every var in from, stick it in to */
475 const char *fromvarname = NULL, *fromvarval = NULL;
476 const char *tovarname = NULL, *tovarval = NULL;
477 fromvarname = ast_var_name(variablesfrom);
478 fromvarval = ast_var_value(variablesfrom);
479 tovarname = 0;
481 /* now, quick see if that var is in the 'to' cdr already */
482 AST_LIST_TRAVERSE(headpto, variablesto, entries) {
484 /* now, quick see if that var is in the 'to' cdr already */
485 if ( strcasecmp(fromvarname, ast_var_name(variablesto)) == 0 ) {
486 tovarname = ast_var_name(variablesto);
487 tovarval = ast_var_value(variablesto);
488 break;
491 if (tovarname && strcasecmp(fromvarval,tovarval) != 0) { /* this message here to see how irritating the userbase finds it */
492 ast_log(LOG_NOTICE, "Merging CDR's: variable %s value %s dropped in favor of value %s\n", tovarname, fromvarval, tovarval);
493 continue;
494 } else if (tovarname && strcasecmp(fromvarval,tovarval) == 0) /* if they are the same, the job is done */
495 continue;
497 /*rip this var out of the from cdr, and stick it in the to cdr */
498 AST_LIST_REMOVE_CURRENT(headpfrom, entries);
499 AST_LIST_INSERT_HEAD(headpto, variablesfrom, entries);
501 AST_LIST_TRAVERSE_SAFE_END;
504 void ast_cdr_merge(struct ast_cdr *to, struct ast_cdr *from)
506 struct ast_cdr *zcdr;
507 struct ast_cdr *lto = NULL;
508 struct ast_cdr *lfrom = NULL;
509 int discard_from = 0;
511 if (!to || !from)
512 return;
514 /* don't merge into locked CDR's -- it's bad business */
515 if (ast_test_flag(to, AST_CDR_FLAG_LOCKED)) {
516 zcdr = to; /* safety valve? */
517 while (to->next) {
518 lto = to;
519 to = to->next;
522 if (ast_test_flag(to, AST_CDR_FLAG_LOCKED)) {
523 ast_log(LOG_WARNING, "Merging into locked CDR... no choice.");
524 to = zcdr; /* safety-- if all there are is locked CDR's, then.... ?? */
525 lto = NULL;
529 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED)) {
530 discard_from = 1;
531 if (lto) {
532 struct ast_cdr *llfrom = NULL;
533 /* insert the from stuff after lto */
534 lto->next = from;
535 lfrom = from;
536 while (lfrom && lfrom->next) {
537 if (!lfrom->next->next)
538 llfrom = lfrom;
539 lfrom = lfrom->next;
541 /* rip off the last entry and put a copy of the to at the end */
542 llfrom->next = to;
543 from = lfrom;
544 } else {
545 /* save copy of the current *to cdr */
546 struct ast_cdr tcdr;
547 struct ast_cdr *llfrom = NULL;
548 memcpy(&tcdr, to, sizeof(tcdr));
549 /* copy in the locked from cdr */
550 memcpy(to, from, sizeof(*to));
551 lfrom = from;
552 while (lfrom && lfrom->next) {
553 if (!lfrom->next->next)
554 llfrom = lfrom;
555 lfrom = lfrom->next;
557 from->next = NULL;
558 /* rip off the last entry and put a copy of the to at the end */
559 if (llfrom == from)
560 to = to->next = ast_cdr_dup(&tcdr);
561 else
562 to = llfrom->next = ast_cdr_dup(&tcdr);
563 from = lfrom;
567 if (!ast_tvzero(from->start)) {
568 if (!ast_tvzero(to->start)) {
569 if (ast_tvcmp(to->start, from->start) > 0 ) {
570 to->start = from->start; /* use the earliest time */
571 from->start = ast_tv(0,0); /* we actively "steal" these values */
573 /* else nothing to do */
574 } else {
575 to->start = from->start;
576 from->start = ast_tv(0,0); /* we actively "steal" these values */
579 if (!ast_tvzero(from->answer)) {
580 if (!ast_tvzero(to->answer)) {
581 if (ast_tvcmp(to->answer, from->answer) > 0 ) {
582 to->answer = from->answer; /* use the earliest time */
583 from->answer = ast_tv(0,0); /* we actively "steal" these values */
585 /* we got the earliest answer time, so we'll settle for that? */
586 } else {
587 to->answer = from->answer;
588 from->answer = ast_tv(0,0); /* we actively "steal" these values */
591 if (!ast_tvzero(from->end)) {
592 if (!ast_tvzero(to->end)) {
593 if (ast_tvcmp(to->end, from->end) < 0 ) {
594 to->end = from->end; /* use the latest time */
595 from->end = ast_tv(0,0); /* we actively "steal" these values */
596 to->duration = to->end.tv_sec - to->start.tv_sec; /* don't forget to update the duration, billsec, when we set end */
597 to->billsec = ast_tvzero(to->answer) ? 0 : to->end.tv_sec - to->answer.tv_sec;
599 /* else, nothing to do */
600 } else {
601 to->end = from->end;
602 from->end = ast_tv(0,0); /* we actively "steal" these values */
603 to->duration = to->end.tv_sec - to->start.tv_sec;
604 to->billsec = ast_tvzero(to->answer) ? 0 : to->end.tv_sec - to->answer.tv_sec;
607 if (to->disposition < from->disposition) {
608 to->disposition = from->disposition;
609 from->disposition = AST_CDR_NOANSWER;
611 if (ast_strlen_zero(to->lastapp) && !ast_strlen_zero(from->lastapp)) {
612 ast_copy_string(to->lastapp, from->lastapp, sizeof(to->lastapp));
613 from->lastapp[0] = 0; /* theft */
615 if (ast_strlen_zero(to->lastdata) && !ast_strlen_zero(from->lastdata)) {
616 ast_copy_string(to->lastdata, from->lastdata, sizeof(to->lastdata));
617 from->lastdata[0] = 0; /* theft */
619 if (ast_strlen_zero(to->dcontext) && !ast_strlen_zero(from->dcontext)) {
620 ast_copy_string(to->dcontext, from->dcontext, sizeof(to->dcontext));
621 from->dcontext[0] = 0; /* theft */
623 if (ast_strlen_zero(to->dstchannel) && !ast_strlen_zero(from->dstchannel)) {
624 ast_copy_string(to->dstchannel, from->dstchannel, sizeof(to->dstchannel));
625 from->dstchannel[0] = 0; /* theft */
627 if (!ast_strlen_zero(from->channel) && (ast_strlen_zero(to->channel) || !strncasecmp(from->channel, "Agent/", 6))) {
628 ast_copy_string(to->channel, from->channel, sizeof(to->channel));
629 from->channel[0] = 0; /* theft */
631 if (ast_strlen_zero(to->src) && !ast_strlen_zero(from->src)) {
632 ast_copy_string(to->src, from->src, sizeof(to->src));
633 from->src[0] = 0; /* theft */
635 if (ast_strlen_zero(to->clid) && !ast_strlen_zero(from->clid)) {
636 ast_copy_string(to->clid, from->clid, sizeof(to->clid));
637 from->clid[0] = 0; /* theft */
639 if (ast_strlen_zero(to->dst) && !ast_strlen_zero(from->dst)) {
640 ast_copy_string(to->dst, from->dst, sizeof(to->dst));
641 from->dst[0] = 0; /* theft */
643 if (!to->amaflags)
644 to->amaflags = AST_CDR_DOCUMENTATION;
645 if (!from->amaflags)
646 from->amaflags = AST_CDR_DOCUMENTATION; /* make sure both amaflags are set to something (DOC is default) */
647 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED) || (to->amaflags == AST_CDR_DOCUMENTATION && from->amaflags != AST_CDR_DOCUMENTATION)) {
648 to->amaflags = from->amaflags;
650 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED) || (ast_strlen_zero(to->accountcode) && !ast_strlen_zero(from->accountcode))) {
651 ast_copy_string(to->accountcode, from->accountcode, sizeof(to->accountcode));
653 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED) || (ast_strlen_zero(to->userfield) && !ast_strlen_zero(from->userfield))) {
654 ast_copy_string(to->userfield, from->userfield, sizeof(to->userfield));
656 /* flags, varsead, ? */
657 cdr_merge_vars(from, to);
659 if (ast_test_flag(from, AST_CDR_FLAG_KEEP_VARS))
660 ast_set_flag(to, AST_CDR_FLAG_KEEP_VARS);
661 if (ast_test_flag(from, AST_CDR_FLAG_POSTED))
662 ast_set_flag(to, AST_CDR_FLAG_POSTED);
663 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED))
664 ast_set_flag(to, AST_CDR_FLAG_LOCKED);
665 if (ast_test_flag(from, AST_CDR_FLAG_CHILD))
666 ast_set_flag(to, AST_CDR_FLAG_CHILD);
667 if (ast_test_flag(from, AST_CDR_FLAG_POST_DISABLED))
668 ast_set_flag(to, AST_CDR_FLAG_POST_DISABLED);
670 /* last, but not least, we need to merge any forked CDRs to the 'to' cdr */
671 while (from->next) {
672 /* just rip 'em off the 'from' and insert them on the 'to' */
673 zcdr = from->next;
674 from->next = zcdr->next;
675 zcdr->next = NULL;
676 /* zcdr is now ripped from the current list; */
677 ast_cdr_append(to, zcdr);
679 if (discard_from)
680 ast_cdr_discard(from);
683 void ast_cdr_start(struct ast_cdr *cdr)
685 char *chan;
687 for (; cdr; cdr = cdr->next) {
688 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
689 chan = S_OR(cdr->channel, "<unknown>");
690 check_post(cdr);
691 cdr->start = ast_tvnow();
696 void ast_cdr_answer(struct ast_cdr *cdr)
699 for (; cdr; cdr = cdr->next) {
700 if (ast_test_flag(cdr, AST_CDR_FLAG_ANSLOCKED))
701 continue;
702 if (ast_test_flag(cdr, AST_CDR_FLAG_DONT_TOUCH) && ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
703 continue;
704 check_post(cdr);
705 if (cdr->disposition < AST_CDR_ANSWERED)
706 cdr->disposition = AST_CDR_ANSWERED;
707 if (ast_tvzero(cdr->answer))
708 cdr->answer = ast_tvnow();
712 void ast_cdr_busy(struct ast_cdr *cdr)
715 for (; cdr; cdr = cdr->next) {
716 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
717 check_post(cdr);
718 if (cdr->disposition < AST_CDR_BUSY)
719 cdr->disposition = AST_CDR_BUSY;
724 void ast_cdr_failed(struct ast_cdr *cdr)
726 for (; cdr; cdr = cdr->next) {
727 check_post(cdr);
728 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
729 if (cdr->disposition < AST_CDR_FAILED)
730 cdr->disposition = AST_CDR_FAILED;
735 void ast_cdr_noanswer(struct ast_cdr *cdr)
737 char *chan;
739 while (cdr) {
740 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
741 if (ast_test_flag(cdr, AST_CDR_FLAG_POSTED))
742 ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
743 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
744 if (cdr->disposition < AST_CDR_NOANSWER)
745 cdr->disposition = AST_CDR_NOANSWER;
747 cdr = cdr->next;
751 /* everywhere ast_cdr_disposition is called, it will call ast_cdr_failed()
752 if ast_cdr_disposition returns a non-zero value */
754 int ast_cdr_disposition(struct ast_cdr *cdr, int cause)
756 int res = 0;
758 for (; cdr; cdr = cdr->next) {
759 switch(cause) { /* handle all the non failure, busy cases, return 0 not to set disposition,
760 return -1 to set disposition to FAILED */
761 case AST_CAUSE_BUSY:
762 ast_cdr_busy(cdr);
763 break;
764 case AST_CAUSE_NORMAL:
765 break;
766 default:
767 res = -1;
770 return res;
773 void ast_cdr_setdestchan(struct ast_cdr *cdr, const char *chann)
775 for (; cdr; cdr = cdr->next) {
776 check_post(cdr);
777 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
778 ast_copy_string(cdr->dstchannel, chann, sizeof(cdr->dstchannel));
782 void ast_cdr_setapp(struct ast_cdr *cdr, char *app, char *data)
785 for (; cdr; cdr = cdr->next) {
786 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
787 check_post(cdr);
788 ast_copy_string(cdr->lastapp, S_OR(app, ""), sizeof(cdr->lastapp));
789 ast_copy_string(cdr->lastdata, S_OR(data, ""), sizeof(cdr->lastdata));
794 /* set cid info for one record */
795 static void set_one_cid(struct ast_cdr *cdr, struct ast_channel *c)
797 /* Grab source from ANI or normal Caller*ID */
798 const char *num = S_OR(c->cid.cid_ani, c->cid.cid_num);
799 if (!cdr)
800 return;
801 if (!ast_strlen_zero(c->cid.cid_name)) {
802 if (!ast_strlen_zero(num)) /* both name and number */
803 snprintf(cdr->clid, sizeof(cdr->clid), "\"%s\" <%s>", c->cid.cid_name, num);
804 else /* only name */
805 ast_copy_string(cdr->clid, c->cid.cid_name, sizeof(cdr->clid));
806 } else if (!ast_strlen_zero(num)) { /* only number */
807 ast_copy_string(cdr->clid, num, sizeof(cdr->clid));
808 } else { /* nothing known */
809 cdr->clid[0] = '\0';
811 ast_copy_string(cdr->src, S_OR(num, ""), sizeof(cdr->src));
814 int ast_cdr_setcid(struct ast_cdr *cdr, struct ast_channel *c)
816 for (; cdr; cdr = cdr->next) {
817 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
818 set_one_cid(cdr, c);
820 return 0;
823 int ast_cdr_init(struct ast_cdr *cdr, struct ast_channel *c)
825 char *chan;
827 for ( ; cdr ; cdr = cdr->next) {
828 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
829 chan = S_OR(cdr->channel, "<unknown>");
830 ast_copy_string(cdr->channel, c->name, sizeof(cdr->channel));
831 set_one_cid(cdr, c);
833 cdr->disposition = (c->_state == AST_STATE_UP) ? AST_CDR_ANSWERED : AST_CDR_NULL;
834 cdr->amaflags = c->amaflags ? c->amaflags : ast_default_amaflags;
835 ast_copy_string(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode));
836 /* Destination information */
837 ast_copy_string(cdr->dst, S_OR(c->macroexten,c->exten), sizeof(cdr->dst));
838 ast_copy_string(cdr->dcontext, S_OR(c->macrocontext,c->context), sizeof(cdr->dcontext));
839 /* Unique call identifier */
840 ast_copy_string(cdr->uniqueid, c->uniqueid, sizeof(cdr->uniqueid));
843 return 0;
846 /* Three routines were "fixed" via 10668, and later shown that
847 users were depending on this behavior. ast_cdr_end,
848 ast_cdr_setvar and ast_cdr_answer are the three routines.
849 While most of the other routines would not touch
850 LOCKED cdr's, these three routines were designed to
851 operate on locked CDR's as a matter of course.
852 I now appreciate how this plays with the ForkCDR app,
853 which forms these cdr chains in the first place.
854 cdr_end is pretty key: all cdrs created are closed
855 together. They only vary by start time. Arithmetically,
856 users can calculate the subintervals they wish to track. */
858 void ast_cdr_end(struct ast_cdr *cdr)
860 for ( ; cdr ; cdr = cdr->next) {
861 if (ast_test_flag(cdr, AST_CDR_FLAG_DONT_TOUCH) && ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
862 continue;
863 check_post(cdr);
864 if (ast_tvzero(cdr->end))
865 cdr->end = ast_tvnow();
866 if (ast_tvzero(cdr->start)) {
867 ast_log(LOG_WARNING, "CDR on channel '%s' has not started\n", S_OR(cdr->channel, "<unknown>"));
868 cdr->disposition = AST_CDR_FAILED;
869 } else
870 cdr->duration = cdr->end.tv_sec - cdr->start.tv_sec;
871 if (ast_tvzero(cdr->answer)) {
872 if (cdr->disposition == AST_CDR_ANSWERED) {
873 ast_log(LOG_WARNING, "CDR on channel '%s' has no answer time but is 'ANSWERED'\n", S_OR(cdr->channel, "<unknown>"));
874 cdr->disposition = AST_CDR_FAILED;
876 } else
877 cdr->billsec = cdr->end.tv_sec - cdr->answer.tv_sec;
881 char *ast_cdr_disp2str(int disposition)
883 switch (disposition) {
884 case AST_CDR_NULL:
885 return "NO ANSWER"; /* by default, for backward compatibility */
886 case AST_CDR_NOANSWER:
887 return "NO ANSWER";
888 case AST_CDR_FAILED:
889 return "FAILED";
890 case AST_CDR_BUSY:
891 return "BUSY";
892 case AST_CDR_ANSWERED:
893 return "ANSWERED";
895 return "UNKNOWN";
898 /*! Converts AMA flag to printable string */
899 char *ast_cdr_flags2str(int flag)
901 switch(flag) {
902 case AST_CDR_OMIT:
903 return "OMIT";
904 case AST_CDR_BILLING:
905 return "BILLING";
906 case AST_CDR_DOCUMENTATION:
907 return "DOCUMENTATION";
909 return "Unknown";
912 int ast_cdr_setaccount(struct ast_channel *chan, const char *account)
914 struct ast_cdr *cdr = chan->cdr;
916 ast_string_field_set(chan, accountcode, account);
917 for ( ; cdr ; cdr = cdr->next) {
918 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
919 ast_copy_string(cdr->accountcode, chan->accountcode, sizeof(cdr->accountcode));
922 return 0;
925 int ast_cdr_setamaflags(struct ast_channel *chan, const char *flag)
927 struct ast_cdr *cdr;
928 int newflag = ast_cdr_amaflags2int(flag);
929 if (newflag) {
930 for (cdr = chan->cdr; cdr; cdr = cdr->next) {
931 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
932 cdr->amaflags = newflag;
937 return 0;
940 int ast_cdr_setuserfield(struct ast_channel *chan, const char *userfield)
942 struct ast_cdr *cdr = chan->cdr;
944 for ( ; cdr ; cdr = cdr->next) {
945 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
946 ast_copy_string(cdr->userfield, userfield, sizeof(cdr->userfield));
949 return 0;
952 int ast_cdr_appenduserfield(struct ast_channel *chan, const char *userfield)
954 struct ast_cdr *cdr = chan->cdr;
956 for ( ; cdr ; cdr = cdr->next) {
957 int len = strlen(cdr->userfield);
959 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
960 ast_copy_string(cdr->userfield + len, userfield, sizeof(cdr->userfield) - len);
963 return 0;
966 int ast_cdr_update(struct ast_channel *c)
968 struct ast_cdr *cdr = c->cdr;
970 for ( ; cdr ; cdr = cdr->next) {
971 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
972 set_one_cid(cdr, c);
974 /* Copy account code et-al */
975 ast_copy_string(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode));
977 /* Destination information */ /* XXX privilege macro* ? */
978 ast_copy_string(cdr->dst, S_OR(c->macroexten, c->exten), sizeof(cdr->dst));
979 ast_copy_string(cdr->dcontext, S_OR(c->macrocontext, c->context), sizeof(cdr->dcontext));
983 return 0;
986 int ast_cdr_amaflags2int(const char *flag)
988 if (!strcasecmp(flag, "default"))
989 return 0;
990 if (!strcasecmp(flag, "omit"))
991 return AST_CDR_OMIT;
992 if (!strcasecmp(flag, "billing"))
993 return AST_CDR_BILLING;
994 if (!strcasecmp(flag, "documentation"))
995 return AST_CDR_DOCUMENTATION;
996 return -1;
999 static void post_cdr(struct ast_cdr *cdr)
1001 char *chan;
1002 struct ast_cdr_beitem *i;
1004 for ( ; cdr ; cdr = cdr->next) {
1005 if (!unanswered && cdr->disposition < AST_CDR_ANSWERED && (ast_strlen_zero(cdr->channel) || ast_strlen_zero(cdr->dstchannel))) {
1006 /* For people, who don't want to see unanswered single-channel events */
1007 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1008 continue;
1011 chan = S_OR(cdr->channel, "<unknown>");
1012 check_post(cdr);
1013 if (ast_tvzero(cdr->end))
1014 ast_log(LOG_WARNING, "CDR on channel '%s' lacks end\n", chan);
1015 if (ast_tvzero(cdr->start))
1016 ast_log(LOG_WARNING, "CDR on channel '%s' lacks start\n", chan);
1017 ast_set_flag(cdr, AST_CDR_FLAG_POSTED);
1018 if (ast_test_flag(cdr, AST_CDR_FLAG_POST_DISABLED))
1019 continue;
1020 AST_LIST_LOCK(&be_list);
1021 AST_LIST_TRAVERSE(&be_list, i, list) {
1022 i->be(cdr);
1024 AST_LIST_UNLOCK(&be_list);
1028 void ast_cdr_reset(struct ast_cdr *cdr, struct ast_flags *_flags)
1030 struct ast_cdr *dup;
1031 struct ast_flags flags = { 0 };
1033 if (_flags)
1034 ast_copy_flags(&flags, _flags, AST_FLAGS_ALL);
1036 for ( ; cdr ; cdr = cdr->next) {
1037 /* Detach if post is requested */
1038 if (ast_test_flag(&flags, AST_CDR_FLAG_LOCKED) || !ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1039 if (ast_test_flag(&flags, AST_CDR_FLAG_POSTED)) {
1040 ast_cdr_end(cdr);
1041 if ((dup = ast_cdr_dup(cdr))) {
1042 ast_cdr_detach(dup);
1044 ast_set_flag(cdr, AST_CDR_FLAG_POSTED);
1047 /* clear variables */
1048 if (!ast_test_flag(&flags, AST_CDR_FLAG_KEEP_VARS)) {
1049 ast_cdr_free_vars(cdr, 0);
1052 /* Reset to initial state */
1053 ast_clear_flag(cdr, AST_FLAGS_ALL);
1054 memset(&cdr->start, 0, sizeof(cdr->start));
1055 memset(&cdr->end, 0, sizeof(cdr->end));
1056 memset(&cdr->answer, 0, sizeof(cdr->answer));
1057 cdr->billsec = 0;
1058 cdr->duration = 0;
1059 ast_cdr_start(cdr);
1060 cdr->disposition = AST_CDR_NULL;
1065 void ast_cdr_specialized_reset(struct ast_cdr *cdr, struct ast_flags *_flags)
1067 struct ast_flags flags = { 0 };
1069 if (_flags)
1070 ast_copy_flags(&flags, _flags, AST_FLAGS_ALL);
1072 if (_flags)
1073 ast_copy_flags(&flags, _flags, AST_FLAGS_ALL);
1075 /* Reset to initial state */
1076 ast_clear_flag(cdr, AST_FLAGS_ALL);
1077 memset(&cdr->start, 0, sizeof(cdr->start));
1078 memset(&cdr->end, 0, sizeof(cdr->end));
1079 memset(&cdr->answer, 0, sizeof(cdr->answer));
1080 cdr->billsec = 0;
1081 cdr->duration = 0;
1082 ast_cdr_start(cdr);
1083 cdr->disposition = AST_CDR_NULL;
1086 struct ast_cdr *ast_cdr_append(struct ast_cdr *cdr, struct ast_cdr *newcdr)
1088 struct ast_cdr *ret;
1090 if (cdr) {
1091 ret = cdr;
1093 while (cdr->next)
1094 cdr = cdr->next;
1095 cdr->next = newcdr;
1096 } else {
1097 ret = newcdr;
1100 return ret;
1103 /*! \note Don't call without cdr_batch_lock */
1104 static void reset_batch(void)
1106 batch->size = 0;
1107 batch->head = NULL;
1108 batch->tail = NULL;
1111 /*! \note Don't call without cdr_batch_lock */
1112 static int init_batch(void)
1114 /* This is the single meta-batch used to keep track of all CDRs during the entire life of the program */
1115 if (!(batch = ast_malloc(sizeof(*batch))))
1116 return -1;
1118 reset_batch();
1120 return 0;
1123 static void *do_batch_backend_process(void *data)
1125 struct ast_cdr_batch_item *processeditem;
1126 struct ast_cdr_batch_item *batchitem = data;
1128 /* Push each CDR into storage mechanism(s) and free all the memory */
1129 while (batchitem) {
1130 post_cdr(batchitem->cdr);
1131 ast_cdr_free(batchitem->cdr);
1132 processeditem = batchitem;
1133 batchitem = batchitem->next;
1134 free(processeditem);
1137 return NULL;
1140 void ast_cdr_submit_batch(int shutdown)
1142 struct ast_cdr_batch_item *oldbatchitems = NULL;
1143 pthread_attr_t attr;
1144 pthread_t batch_post_thread = AST_PTHREADT_NULL;
1146 /* if there's no batch, or no CDRs in the batch, then there's nothing to do */
1147 if (!batch || !batch->head)
1148 return;
1150 /* move the old CDRs aside, and prepare a new CDR batch */
1151 ast_mutex_lock(&cdr_batch_lock);
1152 oldbatchitems = batch->head;
1153 reset_batch();
1154 ast_mutex_unlock(&cdr_batch_lock);
1156 /* if configured, spawn a new thread to post these CDRs,
1157 also try to save as much as possible if we are shutting down safely */
1158 if (batchscheduleronly || shutdown) {
1159 if (option_debug)
1160 ast_log(LOG_DEBUG, "CDR single-threaded batch processing begins now\n");
1161 do_batch_backend_process(oldbatchitems);
1162 } else {
1163 pthread_attr_init(&attr);
1164 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1165 if (ast_pthread_create_background(&batch_post_thread, &attr, do_batch_backend_process, oldbatchitems)) {
1166 ast_log(LOG_WARNING, "CDR processing thread could not detach, now trying in this thread\n");
1167 do_batch_backend_process(oldbatchitems);
1168 } else {
1169 if (option_debug)
1170 ast_log(LOG_DEBUG, "CDR multi-threaded batch processing begins now\n");
1172 pthread_attr_destroy(&attr);
1176 static int submit_scheduled_batch(const void *data)
1178 ast_cdr_submit_batch(0);
1179 /* manually reschedule from this point in time */
1180 cdr_sched = ast_sched_add(sched, batchtime * 1000, submit_scheduled_batch, NULL);
1181 /* returning zero so the scheduler does not automatically reschedule */
1182 return 0;
1185 static void submit_unscheduled_batch(void)
1187 /* this is okay since we are not being called from within the scheduler */
1188 AST_SCHED_DEL(sched, cdr_sched);
1189 /* schedule the submission to occur ASAP (1 ms) */
1190 cdr_sched = ast_sched_add(sched, 1, submit_scheduled_batch, NULL);
1191 /* signal the do_cdr thread to wakeup early and do some work (that lazy thread ;) */
1192 ast_mutex_lock(&cdr_pending_lock);
1193 ast_cond_signal(&cdr_pending_cond);
1194 ast_mutex_unlock(&cdr_pending_lock);
1197 void ast_cdr_detach(struct ast_cdr *cdr)
1199 struct ast_cdr_batch_item *newtail;
1200 int curr;
1202 if (!cdr)
1203 return;
1205 /* maybe they disabled CDR stuff completely, so just drop it */
1206 if (!enabled) {
1207 if (option_debug)
1208 ast_log(LOG_DEBUG, "Dropping CDR !\n");
1209 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1210 ast_cdr_free(cdr);
1211 return;
1214 /* post stuff immediately if we are not in batch mode, this is legacy behaviour */
1215 if (!batchmode) {
1216 post_cdr(cdr);
1217 ast_cdr_free(cdr);
1218 return;
1221 /* otherwise, each CDR gets put into a batch list (at the end) */
1222 if (option_debug)
1223 ast_log(LOG_DEBUG, "CDR detaching from this thread\n");
1225 /* we'll need a new tail for every CDR */
1226 if (!(newtail = ast_calloc(1, sizeof(*newtail)))) {
1227 post_cdr(cdr);
1228 ast_cdr_free(cdr);
1229 return;
1232 /* don't traverse a whole list (just keep track of the tail) */
1233 ast_mutex_lock(&cdr_batch_lock);
1234 if (!batch)
1235 init_batch();
1236 if (!batch->head) {
1237 /* new batch is empty, so point the head at the new tail */
1238 batch->head = newtail;
1239 } else {
1240 /* already got a batch with something in it, so just append a new tail */
1241 batch->tail->next = newtail;
1243 newtail->cdr = cdr;
1244 batch->tail = newtail;
1245 curr = batch->size++;
1246 ast_mutex_unlock(&cdr_batch_lock);
1248 /* if we have enough stuff to post, then do it */
1249 if (curr >= (batchsize - 1))
1250 submit_unscheduled_batch();
1253 static void *do_cdr(void *data)
1255 struct timespec timeout;
1256 int schedms;
1257 int numevents = 0;
1259 for(;;) {
1260 struct timeval now;
1261 schedms = ast_sched_wait(sched);
1262 /* this shouldn't happen, but provide a 1 second default just in case */
1263 if (schedms <= 0)
1264 schedms = 1000;
1265 now = ast_tvadd(ast_tvnow(), ast_samp2tv(schedms, 1000));
1266 timeout.tv_sec = now.tv_sec;
1267 timeout.tv_nsec = now.tv_usec * 1000;
1268 /* prevent stuff from clobbering cdr_pending_cond, then wait on signals sent to it until the timeout expires */
1269 ast_mutex_lock(&cdr_pending_lock);
1270 ast_cond_timedwait(&cdr_pending_cond, &cdr_pending_lock, &timeout);
1271 numevents = ast_sched_runq(sched);
1272 ast_mutex_unlock(&cdr_pending_lock);
1273 if (option_debug > 1)
1274 ast_log(LOG_DEBUG, "Processed %d scheduled CDR batches from the run queue\n", numevents);
1277 return NULL;
1280 static int handle_cli_status(int fd, int argc, char *argv[])
1282 struct ast_cdr_beitem *beitem=NULL;
1283 int cnt=0;
1284 long nextbatchtime=0;
1286 if (argc > 2)
1287 return RESULT_SHOWUSAGE;
1289 ast_cli(fd, "CDR logging: %s\n", enabled ? "enabled" : "disabled");
1290 ast_cli(fd, "CDR mode: %s\n", batchmode ? "batch" : "simple");
1291 if (enabled) {
1292 ast_cli(fd, "CDR output unanswered calls: %s\n", unanswered ? "yes" : "no");
1293 if (batchmode) {
1294 if (batch)
1295 cnt = batch->size;
1296 if (cdr_sched > -1)
1297 nextbatchtime = ast_sched_when(sched, cdr_sched);
1298 ast_cli(fd, "CDR safe shut down: %s\n", batchsafeshutdown ? "enabled" : "disabled");
1299 ast_cli(fd, "CDR batch threading model: %s\n", batchscheduleronly ? "scheduler only" : "scheduler plus separate threads");
1300 ast_cli(fd, "CDR current batch size: %d record%s\n", cnt, (cnt != 1) ? "s" : "");
1301 ast_cli(fd, "CDR maximum batch size: %d record%s\n", batchsize, (batchsize != 1) ? "s" : "");
1302 ast_cli(fd, "CDR maximum batch time: %d second%s\n", batchtime, (batchtime != 1) ? "s" : "");
1303 ast_cli(fd, "CDR next scheduled batch processing time: %ld second%s\n", nextbatchtime, (nextbatchtime != 1) ? "s" : "");
1305 AST_LIST_LOCK(&be_list);
1306 AST_LIST_TRAVERSE(&be_list, beitem, list) {
1307 ast_cli(fd, "CDR registered backend: %s\n", beitem->name);
1309 AST_LIST_UNLOCK(&be_list);
1312 return 0;
1315 static int handle_cli_submit(int fd, int argc, char *argv[])
1317 if (argc > 2)
1318 return RESULT_SHOWUSAGE;
1320 submit_unscheduled_batch();
1321 ast_cli(fd, "Submitted CDRs to backend engines for processing. This may take a while.\n");
1323 return 0;
1326 static struct ast_cli_entry cli_submit = {
1327 { "cdr", "submit", NULL },
1328 handle_cli_submit, "Posts all pending batched CDR data",
1329 "Usage: cdr submit\n"
1330 " Posts all pending batched CDR data to the configured CDR backend engine modules.\n"
1333 static struct ast_cli_entry cli_status = {
1334 { "cdr", "status", NULL },
1335 handle_cli_status, "Display the CDR status",
1336 "Usage: cdr status\n"
1337 " Displays the Call Detail Record engine system status.\n"
1340 static int do_reload(void)
1342 struct ast_config *config;
1343 const char *enabled_value;
1344 const char *unanswered_value;
1345 const char *batched_value;
1346 const char *scheduleronly_value;
1347 const char *batchsafeshutdown_value;
1348 const char *size_value;
1349 const char *time_value;
1350 const char *end_before_h_value;
1351 int cfg_size;
1352 int cfg_time;
1353 int was_enabled;
1354 int was_batchmode;
1355 int res=0;
1357 ast_mutex_lock(&cdr_batch_lock);
1359 batchsize = BATCH_SIZE_DEFAULT;
1360 batchtime = BATCH_TIME_DEFAULT;
1361 batchscheduleronly = BATCH_SCHEDULER_ONLY_DEFAULT;
1362 batchsafeshutdown = BATCH_SAFE_SHUTDOWN_DEFAULT;
1363 was_enabled = enabled;
1364 was_batchmode = batchmode;
1365 enabled = 1;
1366 batchmode = 0;
1368 /* don't run the next scheduled CDR posting while reloading */
1369 AST_SCHED_DEL(sched, cdr_sched);
1371 if ((config = ast_config_load("cdr.conf"))) {
1372 if ((enabled_value = ast_variable_retrieve(config, "general", "enable"))) {
1373 enabled = ast_true(enabled_value);
1375 if ((unanswered_value = ast_variable_retrieve(config, "general", "unanswered"))) {
1376 unanswered = ast_true(unanswered_value);
1378 if ((batched_value = ast_variable_retrieve(config, "general", "batch"))) {
1379 batchmode = ast_true(batched_value);
1381 if ((scheduleronly_value = ast_variable_retrieve(config, "general", "scheduleronly"))) {
1382 batchscheduleronly = ast_true(scheduleronly_value);
1384 if ((batchsafeshutdown_value = ast_variable_retrieve(config, "general", "safeshutdown"))) {
1385 batchsafeshutdown = ast_true(batchsafeshutdown_value);
1387 if ((size_value = ast_variable_retrieve(config, "general", "size"))) {
1388 if (sscanf(size_value, "%d", &cfg_size) < 1)
1389 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", size_value);
1390 else if (size_value < 0)
1391 ast_log(LOG_WARNING, "Invalid maximum batch size '%d' specified, using default\n", cfg_size);
1392 else
1393 batchsize = cfg_size;
1395 if ((time_value = ast_variable_retrieve(config, "general", "time"))) {
1396 if (sscanf(time_value, "%d", &cfg_time) < 1)
1397 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", time_value);
1398 else if (time_value < 0)
1399 ast_log(LOG_WARNING, "Invalid maximum batch time '%d' specified, using default\n", cfg_time);
1400 else
1401 batchtime = cfg_time;
1403 if ((end_before_h_value = ast_variable_retrieve(config, "general", "endbeforehexten")))
1404 ast_set2_flag(&ast_options, ast_true(end_before_h_value), AST_OPT_FLAG_END_CDR_BEFORE_H_EXTEN);
1407 if (enabled && !batchmode) {
1408 ast_log(LOG_NOTICE, "CDR simple logging enabled.\n");
1409 } else if (enabled && batchmode) {
1410 cdr_sched = ast_sched_add(sched, batchtime * 1000, submit_scheduled_batch, NULL);
1411 ast_log(LOG_NOTICE, "CDR batch mode logging enabled, first of either size %d or time %d seconds.\n", batchsize, batchtime);
1412 } else {
1413 ast_log(LOG_NOTICE, "CDR logging disabled, data will be lost.\n");
1416 /* if this reload enabled the CDR batch mode, create the background thread
1417 if it does not exist */
1418 if (enabled && batchmode && (!was_enabled || !was_batchmode) && (cdr_thread == AST_PTHREADT_NULL)) {
1419 ast_cond_init(&cdr_pending_cond, NULL);
1420 if (ast_pthread_create_background(&cdr_thread, NULL, do_cdr, NULL) < 0) {
1421 ast_log(LOG_ERROR, "Unable to start CDR thread.\n");
1422 AST_SCHED_DEL(sched, cdr_sched);
1423 } else {
1424 ast_cli_register(&cli_submit);
1425 ast_register_atexit(ast_cdr_engine_term);
1426 res = 0;
1428 /* if this reload disabled the CDR and/or batch mode and there is a background thread,
1429 kill it */
1430 } else if (((!enabled && was_enabled) || (!batchmode && was_batchmode)) && (cdr_thread != AST_PTHREADT_NULL)) {
1431 /* wake up the thread so it will exit */
1432 pthread_cancel(cdr_thread);
1433 pthread_kill(cdr_thread, SIGURG);
1434 pthread_join(cdr_thread, NULL);
1435 cdr_thread = AST_PTHREADT_NULL;
1436 ast_cond_destroy(&cdr_pending_cond);
1437 ast_cli_unregister(&cli_submit);
1438 ast_unregister_atexit(ast_cdr_engine_term);
1439 res = 0;
1440 /* if leaving batch mode, then post the CDRs in the batch,
1441 and don't reschedule, since we are stopping CDR logging */
1442 if (!batchmode && was_batchmode) {
1443 ast_cdr_engine_term();
1445 } else {
1446 res = 0;
1449 ast_mutex_unlock(&cdr_batch_lock);
1450 ast_config_destroy(config);
1452 return res;
1455 int ast_cdr_engine_init(void)
1457 int res;
1459 sched = sched_context_create();
1460 if (!sched) {
1461 ast_log(LOG_ERROR, "Unable to create schedule context.\n");
1462 return -1;
1465 ast_cli_register(&cli_status);
1467 res = do_reload();
1468 if (res) {
1469 ast_mutex_lock(&cdr_batch_lock);
1470 res = init_batch();
1471 ast_mutex_unlock(&cdr_batch_lock);
1474 return res;
1477 /* \note This actually gets called a couple of times at shutdown. Once, before we start
1478 hanging up channels, and then again, after the channel hangup timeout expires */
1479 void ast_cdr_engine_term(void)
1481 ast_cdr_submit_batch(batchsafeshutdown);
1484 int ast_cdr_engine_reload(void)
1486 return do_reload();