when a PRI call must be moved to a different B channel at the request of the other...
[asterisk-bristuff.git] / main / cdr.c
blobee0086444ad5b384b73bd9838acc6ab00bc0a039
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 int ast_cdr_log_unanswered(void)
108 return unanswered;
111 /*! Register a CDR driver. Each registered CDR driver generates a CDR
112 \return 0 on success, -1 on failure
114 int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be)
116 struct ast_cdr_beitem *i;
118 if (!name)
119 return -1;
120 if (!be) {
121 ast_log(LOG_WARNING, "CDR engine '%s' lacks backend\n", name);
122 return -1;
125 AST_LIST_LOCK(&be_list);
126 AST_LIST_TRAVERSE(&be_list, i, list) {
127 if (!strcasecmp(name, i->name))
128 break;
130 AST_LIST_UNLOCK(&be_list);
132 if (i) {
133 ast_log(LOG_WARNING, "Already have a CDR backend called '%s'\n", name);
134 return -1;
137 if (!(i = ast_calloc(1, sizeof(*i))))
138 return -1;
140 i->be = be;
141 ast_copy_string(i->name, name, sizeof(i->name));
142 ast_copy_string(i->desc, desc, sizeof(i->desc));
144 AST_LIST_LOCK(&be_list);
145 AST_LIST_INSERT_HEAD(&be_list, i, list);
146 AST_LIST_UNLOCK(&be_list);
148 return 0;
151 /*! unregister a CDR driver */
152 void ast_cdr_unregister(const char *name)
154 struct ast_cdr_beitem *i = NULL;
156 AST_LIST_LOCK(&be_list);
157 AST_LIST_TRAVERSE_SAFE_BEGIN(&be_list, i, list) {
158 if (!strcasecmp(name, i->name)) {
159 AST_LIST_REMOVE_CURRENT(&be_list, list);
160 if (option_verbose > 1)
161 ast_verbose(VERBOSE_PREFIX_2 "Unregistered '%s' CDR backend\n", name);
162 free(i);
163 break;
166 AST_LIST_TRAVERSE_SAFE_END;
167 AST_LIST_UNLOCK(&be_list);
170 /*! Duplicate a CDR record
171 \returns Pointer to new CDR record
173 struct ast_cdr *ast_cdr_dup(struct ast_cdr *cdr)
175 struct ast_cdr *newcdr;
177 if (!cdr) /* don't die if we get a null cdr pointer */
178 return NULL;
179 newcdr = ast_cdr_alloc();
180 if (!newcdr)
181 return NULL;
183 memcpy(newcdr, cdr, sizeof(*newcdr));
184 /* The varshead is unusable, volatile even, after the memcpy so we take care of that here */
185 memset(&newcdr->varshead, 0, sizeof(newcdr->varshead));
186 ast_cdr_copy_vars(newcdr, cdr);
187 newcdr->next = NULL;
189 return newcdr;
192 static const char *ast_cdr_getvar_internal(struct ast_cdr *cdr, const char *name, int recur)
194 if (ast_strlen_zero(name))
195 return NULL;
197 for (; cdr; cdr = recur ? cdr->next : NULL) {
198 struct ast_var_t *variables;
199 struct varshead *headp = &cdr->varshead;
200 AST_LIST_TRAVERSE(headp, variables, entries) {
201 if (!strcasecmp(name, ast_var_name(variables)))
202 return ast_var_value(variables);
206 return NULL;
209 static void cdr_get_tv(struct timeval tv, const char *fmt, char *buf, int bufsize)
211 if (fmt == NULL) { /* raw mode */
212 snprintf(buf, bufsize, "%ld.%06ld", (long)tv.tv_sec, (long)tv.tv_usec);
213 } else {
214 time_t t = tv.tv_sec;
215 if (t) {
216 struct tm tm;
218 ast_localtime(&t, &tm, NULL);
219 strftime(buf, bufsize, fmt, &tm);
224 /*! CDR channel variable retrieval */
225 void ast_cdr_getvar(struct ast_cdr *cdr, const char *name, char **ret, char *workspace, int workspacelen, int recur, int raw)
227 const char *fmt = "%Y-%m-%d %T";
228 const char *varbuf;
230 if (!cdr) /* don't die if the cdr is null */
231 return;
233 *ret = NULL;
234 /* special vars (the ones from the struct ast_cdr when requested by name)
235 I'd almost say we should convert all the stringed vals to vars */
237 if (!strcasecmp(name, "clid"))
238 ast_copy_string(workspace, cdr->clid, workspacelen);
239 else if (!strcasecmp(name, "src"))
240 ast_copy_string(workspace, cdr->src, workspacelen);
241 else if (!strcasecmp(name, "dst"))
242 ast_copy_string(workspace, cdr->dst, workspacelen);
243 else if (!strcasecmp(name, "dcontext"))
244 ast_copy_string(workspace, cdr->dcontext, workspacelen);
245 else if (!strcasecmp(name, "channel"))
246 ast_copy_string(workspace, cdr->channel, workspacelen);
247 else if (!strcasecmp(name, "dstchannel"))
248 ast_copy_string(workspace, cdr->dstchannel, workspacelen);
249 else if (!strcasecmp(name, "lastapp"))
250 ast_copy_string(workspace, cdr->lastapp, workspacelen);
251 else if (!strcasecmp(name, "lastdata"))
252 ast_copy_string(workspace, cdr->lastdata, workspacelen);
253 else if (!strcasecmp(name, "start"))
254 cdr_get_tv(cdr->start, raw ? NULL : fmt, workspace, workspacelen);
255 else if (!strcasecmp(name, "answer"))
256 cdr_get_tv(cdr->answer, raw ? NULL : fmt, workspace, workspacelen);
257 else if (!strcasecmp(name, "end"))
258 cdr_get_tv(cdr->end, raw ? NULL : fmt, workspace, workspacelen);
259 else if (!strcasecmp(name, "duration"))
260 snprintf(workspace, workspacelen, "%ld", cdr->duration);
261 else if (!strcasecmp(name, "billsec"))
262 snprintf(workspace, workspacelen, "%ld", cdr->billsec);
263 else if (!strcasecmp(name, "disposition")) {
264 if (raw) {
265 snprintf(workspace, workspacelen, "%ld", cdr->disposition);
266 } else {
267 ast_copy_string(workspace, ast_cdr_disp2str(cdr->disposition), workspacelen);
269 } else if (!strcasecmp(name, "amaflags")) {
270 if (raw) {
271 snprintf(workspace, workspacelen, "%ld", cdr->amaflags);
272 } else {
273 ast_copy_string(workspace, ast_cdr_flags2str(cdr->amaflags), workspacelen);
275 } else if (!strcasecmp(name, "accountcode"))
276 ast_copy_string(workspace, cdr->accountcode, workspacelen);
277 else if (!strcasecmp(name, "uniqueid"))
278 ast_copy_string(workspace, cdr->uniqueid, workspacelen);
279 else if (!strcasecmp(name, "userfield"))
280 ast_copy_string(workspace, cdr->userfield, workspacelen);
281 else if ((varbuf = ast_cdr_getvar_internal(cdr, name, recur)))
282 ast_copy_string(workspace, varbuf, workspacelen);
283 else
284 workspace[0] = '\0';
286 if (!ast_strlen_zero(workspace))
287 *ret = workspace;
290 /* readonly cdr variables */
291 static const char *cdr_readonly_vars[] = { "clid", "src", "dst", "dcontext", "channel", "dstchannel",
292 "lastapp", "lastdata", "start", "answer", "end", "duration",
293 "billsec", "disposition", "amaflags", "accountcode", "uniqueid",
294 "userfield", NULL };
295 /*! Set a CDR channel variable
296 \note You can't set the CDR variables that belong to the actual CDR record, like "billsec".
298 int ast_cdr_setvar(struct ast_cdr *cdr, const char *name, const char *value, int recur)
300 struct ast_var_t *newvariable;
301 struct varshead *headp;
302 int x;
304 if (!cdr) /* don't die if the cdr is null */
305 return -1;
307 for(x = 0; cdr_readonly_vars[x]; x++) {
308 if (!strcasecmp(name, cdr_readonly_vars[x])) {
309 ast_log(LOG_ERROR, "Attempt to set the '%s' read-only variable!.\n", name);
310 return -1;
314 if (!cdr) {
315 ast_log(LOG_ERROR, "Attempt to set a variable on a nonexistent CDR record.\n");
316 return -1;
319 for (; cdr; cdr = recur ? cdr->next : NULL) {
320 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
321 headp = &cdr->varshead;
322 AST_LIST_TRAVERSE_SAFE_BEGIN(headp, newvariable, entries) {
323 if (!strcasecmp(ast_var_name(newvariable), name)) {
324 /* there is already such a variable, delete it */
325 AST_LIST_REMOVE_CURRENT(headp, entries);
326 ast_var_delete(newvariable);
327 break;
330 AST_LIST_TRAVERSE_SAFE_END;
332 if (value) {
333 newvariable = ast_var_assign(name, value);
334 AST_LIST_INSERT_HEAD(headp, newvariable, entries);
339 return 0;
342 int ast_cdr_copy_vars(struct ast_cdr *to_cdr, struct ast_cdr *from_cdr)
344 struct ast_var_t *variables, *newvariable = NULL;
345 struct varshead *headpa, *headpb;
346 const char *var, *val;
347 int x = 0;
349 if (!to_cdr || !from_cdr) /* don't die if one of the pointers is null */
350 return 0;
352 headpa = &from_cdr->varshead;
353 headpb = &to_cdr->varshead;
355 AST_LIST_TRAVERSE(headpa,variables,entries) {
356 if (variables &&
357 (var = ast_var_name(variables)) && (val = ast_var_value(variables)) &&
358 !ast_strlen_zero(var) && !ast_strlen_zero(val)) {
359 newvariable = ast_var_assign(var, val);
360 AST_LIST_INSERT_HEAD(headpb, newvariable, entries);
361 x++;
365 return x;
368 int ast_cdr_serialize_variables(struct ast_cdr *cdr, char *buf, size_t size, char delim, char sep, int recur)
370 struct ast_var_t *variables;
371 const char *var, *val;
372 char *tmp;
373 char workspace[256];
374 int total = 0, x = 0, i;
376 memset(buf, 0, size);
378 for (; cdr; cdr = recur ? cdr->next : NULL) {
379 if (++x > 1)
380 ast_build_string(&buf, &size, "\n");
382 AST_LIST_TRAVERSE(&cdr->varshead, variables, entries) {
383 if (variables &&
384 (var = ast_var_name(variables)) && (val = ast_var_value(variables)) &&
385 !ast_strlen_zero(var) && !ast_strlen_zero(val)) {
386 if (ast_build_string(&buf, &size, "level %d: %s%c%s%c", x, var, delim, val, sep)) {
387 ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
388 break;
389 } else
390 total++;
391 } else
392 break;
395 for (i = 0; cdr_readonly_vars[i]; i++) {
396 ast_cdr_getvar(cdr, cdr_readonly_vars[i], &tmp, workspace, sizeof(workspace), 0, 0);
397 if (!tmp)
398 continue;
400 if (ast_build_string(&buf, &size, "level %d: %s%c%s%c", x, cdr_readonly_vars[i], delim, tmp, sep)) {
401 ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
402 break;
403 } else
404 total++;
408 return total;
412 void ast_cdr_free_vars(struct ast_cdr *cdr, int recur)
415 /* clear variables */
416 for (; cdr; cdr = recur ? cdr->next : NULL) {
417 struct ast_var_t *vardata;
418 struct varshead *headp = &cdr->varshead;
419 while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
420 ast_var_delete(vardata);
424 /*! \brief print a warning if cdr already posted */
425 static void check_post(struct ast_cdr *cdr)
427 if (!cdr)
428 return;
429 if (ast_test_flag(cdr, AST_CDR_FLAG_POSTED))
430 ast_log(LOG_NOTICE, "CDR on channel '%s' already posted\n", S_OR(cdr->channel, "<unknown>"));
433 void ast_cdr_free(struct ast_cdr *cdr)
436 while (cdr) {
437 struct ast_cdr *next = cdr->next;
438 char *chan = S_OR(cdr->channel, "<unknown>");
439 if (!ast_test_flag(cdr, AST_CDR_FLAG_POSTED) && !ast_test_flag(cdr, AST_CDR_FLAG_POST_DISABLED))
440 ast_log(LOG_NOTICE, "CDR on channel '%s' not posted\n", chan);
441 if (ast_tvzero(cdr->end))
442 ast_log(LOG_NOTICE, "CDR on channel '%s' lacks end\n", chan);
443 if (ast_tvzero(cdr->start))
444 ast_log(LOG_NOTICE, "CDR on channel '%s' lacks start\n", chan);
446 ast_cdr_free_vars(cdr, 0);
447 free(cdr);
448 cdr = next;
452 /*! \brief the same as a cdr_free call, only with no checks; just get rid of it */
453 void ast_cdr_discard(struct ast_cdr *cdr)
455 while (cdr) {
456 struct ast_cdr *next = cdr->next;
458 ast_cdr_free_vars(cdr, 0);
459 free(cdr);
460 cdr = next;
464 struct ast_cdr *ast_cdr_alloc(void)
466 struct ast_cdr *x = ast_calloc(1, sizeof(struct ast_cdr));
467 if (!x)
468 ast_log(LOG_ERROR,"Allocation Failure for a CDR!\n");
469 return x;
472 static void cdr_merge_vars(struct ast_cdr *to, struct ast_cdr *from)
474 struct ast_var_t *variablesfrom,*variablesto;
475 struct varshead *headpfrom = &to->varshead;
476 struct varshead *headpto = &from->varshead;
477 AST_LIST_TRAVERSE_SAFE_BEGIN(headpfrom, variablesfrom, entries) {
478 /* for every var in from, stick it in to */
479 const char *fromvarname = NULL, *fromvarval = NULL;
480 const char *tovarname = NULL, *tovarval = NULL;
481 fromvarname = ast_var_name(variablesfrom);
482 fromvarval = ast_var_value(variablesfrom);
483 tovarname = 0;
485 /* now, quick see if that var is in the 'to' cdr already */
486 AST_LIST_TRAVERSE(headpto, variablesto, entries) {
488 /* now, quick see if that var is in the 'to' cdr already */
489 if ( strcasecmp(fromvarname, ast_var_name(variablesto)) == 0 ) {
490 tovarname = ast_var_name(variablesto);
491 tovarval = ast_var_value(variablesto);
492 break;
495 if (tovarname && strcasecmp(fromvarval,tovarval) != 0) { /* this message here to see how irritating the userbase finds it */
496 ast_log(LOG_NOTICE, "Merging CDR's: variable %s value %s dropped in favor of value %s\n", tovarname, fromvarval, tovarval);
497 continue;
498 } else if (tovarname && strcasecmp(fromvarval,tovarval) == 0) /* if they are the same, the job is done */
499 continue;
501 /*rip this var out of the from cdr, and stick it in the to cdr */
502 AST_LIST_REMOVE_CURRENT(headpfrom, entries);
503 AST_LIST_INSERT_HEAD(headpto, variablesfrom, entries);
505 AST_LIST_TRAVERSE_SAFE_END;
508 void ast_cdr_merge(struct ast_cdr *to, struct ast_cdr *from)
510 struct ast_cdr *zcdr;
511 struct ast_cdr *lto = NULL;
512 struct ast_cdr *lfrom = NULL;
513 int discard_from = 0;
515 if (!to || !from)
516 return;
518 /* don't merge into locked CDR's -- it's bad business */
519 if (ast_test_flag(to, AST_CDR_FLAG_LOCKED)) {
520 zcdr = to; /* safety valve? */
521 while (to->next) {
522 lto = to;
523 to = to->next;
526 if (ast_test_flag(to, AST_CDR_FLAG_LOCKED)) {
527 ast_log(LOG_WARNING, "Merging into locked CDR... no choice.");
528 to = zcdr; /* safety-- if all there are is locked CDR's, then.... ?? */
529 lto = NULL;
533 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED)) {
534 discard_from = 1;
535 if (lto) {
536 struct ast_cdr *llfrom = NULL;
537 /* insert the from stuff after lto */
538 lto->next = from;
539 lfrom = from;
540 while (lfrom && lfrom->next) {
541 if (!lfrom->next->next)
542 llfrom = lfrom;
543 lfrom = lfrom->next;
545 /* rip off the last entry and put a copy of the to at the end */
546 llfrom->next = to;
547 from = lfrom;
548 } else {
549 /* save copy of the current *to cdr */
550 struct ast_cdr tcdr;
551 struct ast_cdr *llfrom = NULL;
552 memcpy(&tcdr, to, sizeof(tcdr));
553 /* copy in the locked from cdr */
554 memcpy(to, from, sizeof(*to));
555 lfrom = from;
556 while (lfrom && lfrom->next) {
557 if (!lfrom->next->next)
558 llfrom = lfrom;
559 lfrom = lfrom->next;
561 from->next = NULL;
562 /* rip off the last entry and put a copy of the to at the end */
563 if (llfrom == from)
564 to = to->next = ast_cdr_dup(&tcdr);
565 else
566 to = llfrom->next = ast_cdr_dup(&tcdr);
567 from = lfrom;
571 if (!ast_tvzero(from->start)) {
572 if (!ast_tvzero(to->start)) {
573 if (ast_tvcmp(to->start, from->start) > 0 ) {
574 to->start = from->start; /* use the earliest time */
575 from->start = ast_tv(0,0); /* we actively "steal" these values */
577 /* else nothing to do */
578 } else {
579 to->start = from->start;
580 from->start = ast_tv(0,0); /* we actively "steal" these values */
583 if (!ast_tvzero(from->answer)) {
584 if (!ast_tvzero(to->answer)) {
585 if (ast_tvcmp(to->answer, from->answer) > 0 ) {
586 to->answer = from->answer; /* use the earliest time */
587 from->answer = ast_tv(0,0); /* we actively "steal" these values */
589 /* we got the earliest answer time, so we'll settle for that? */
590 } else {
591 to->answer = from->answer;
592 from->answer = ast_tv(0,0); /* we actively "steal" these values */
595 if (!ast_tvzero(from->end)) {
596 if (!ast_tvzero(to->end)) {
597 if (ast_tvcmp(to->end, from->end) < 0 ) {
598 to->end = from->end; /* use the latest time */
599 from->end = ast_tv(0,0); /* we actively "steal" these values */
600 to->duration = to->end.tv_sec - to->start.tv_sec; /* don't forget to update the duration, billsec, when we set end */
601 to->billsec = ast_tvzero(to->answer) ? 0 : to->end.tv_sec - to->answer.tv_sec;
603 /* else, nothing to do */
604 } else {
605 to->end = from->end;
606 from->end = ast_tv(0,0); /* we actively "steal" these values */
607 to->duration = to->end.tv_sec - to->start.tv_sec;
608 to->billsec = ast_tvzero(to->answer) ? 0 : to->end.tv_sec - to->answer.tv_sec;
611 if (to->disposition < from->disposition) {
612 to->disposition = from->disposition;
613 from->disposition = AST_CDR_NOANSWER;
615 if (ast_strlen_zero(to->lastapp) && !ast_strlen_zero(from->lastapp)) {
616 ast_copy_string(to->lastapp, from->lastapp, sizeof(to->lastapp));
617 from->lastapp[0] = 0; /* theft */
619 if (ast_strlen_zero(to->lastdata) && !ast_strlen_zero(from->lastdata)) {
620 ast_copy_string(to->lastdata, from->lastdata, sizeof(to->lastdata));
621 from->lastdata[0] = 0; /* theft */
623 if (ast_strlen_zero(to->dcontext) && !ast_strlen_zero(from->dcontext)) {
624 ast_copy_string(to->dcontext, from->dcontext, sizeof(to->dcontext));
625 from->dcontext[0] = 0; /* theft */
627 if (ast_strlen_zero(to->dstchannel) && !ast_strlen_zero(from->dstchannel)) {
628 ast_copy_string(to->dstchannel, from->dstchannel, sizeof(to->dstchannel));
629 from->dstchannel[0] = 0; /* theft */
631 if (!ast_strlen_zero(from->channel) && (ast_strlen_zero(to->channel) || !strncasecmp(from->channel, "Agent/", 6))) {
632 ast_copy_string(to->channel, from->channel, sizeof(to->channel));
633 from->channel[0] = 0; /* theft */
635 if (ast_strlen_zero(to->src) && !ast_strlen_zero(from->src)) {
636 ast_copy_string(to->src, from->src, sizeof(to->src));
637 from->src[0] = 0; /* theft */
639 if (ast_strlen_zero(to->clid) && !ast_strlen_zero(from->clid)) {
640 ast_copy_string(to->clid, from->clid, sizeof(to->clid));
641 from->clid[0] = 0; /* theft */
643 if (ast_strlen_zero(to->dst) && !ast_strlen_zero(from->dst)) {
644 ast_copy_string(to->dst, from->dst, sizeof(to->dst));
645 from->dst[0] = 0; /* theft */
647 if (!to->amaflags)
648 to->amaflags = AST_CDR_DOCUMENTATION;
649 if (!from->amaflags)
650 from->amaflags = AST_CDR_DOCUMENTATION; /* make sure both amaflags are set to something (DOC is default) */
651 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED) || (to->amaflags == AST_CDR_DOCUMENTATION && from->amaflags != AST_CDR_DOCUMENTATION)) {
652 to->amaflags = from->amaflags;
654 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED) || (ast_strlen_zero(to->accountcode) && !ast_strlen_zero(from->accountcode))) {
655 ast_copy_string(to->accountcode, from->accountcode, sizeof(to->accountcode));
657 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED) || (ast_strlen_zero(to->userfield) && !ast_strlen_zero(from->userfield))) {
658 ast_copy_string(to->userfield, from->userfield, sizeof(to->userfield));
660 /* flags, varsead, ? */
661 cdr_merge_vars(from, to);
663 if (ast_test_flag(from, AST_CDR_FLAG_KEEP_VARS))
664 ast_set_flag(to, AST_CDR_FLAG_KEEP_VARS);
665 if (ast_test_flag(from, AST_CDR_FLAG_POSTED))
666 ast_set_flag(to, AST_CDR_FLAG_POSTED);
667 if (ast_test_flag(from, AST_CDR_FLAG_LOCKED))
668 ast_set_flag(to, AST_CDR_FLAG_LOCKED);
669 if (ast_test_flag(from, AST_CDR_FLAG_CHILD))
670 ast_set_flag(to, AST_CDR_FLAG_CHILD);
671 if (ast_test_flag(from, AST_CDR_FLAG_POST_DISABLED))
672 ast_set_flag(to, AST_CDR_FLAG_POST_DISABLED);
674 /* last, but not least, we need to merge any forked CDRs to the 'to' cdr */
675 while (from->next) {
676 /* just rip 'em off the 'from' and insert them on the 'to' */
677 zcdr = from->next;
678 from->next = zcdr->next;
679 zcdr->next = NULL;
680 /* zcdr is now ripped from the current list; */
681 ast_cdr_append(to, zcdr);
683 if (discard_from)
684 ast_cdr_discard(from);
687 void ast_cdr_start(struct ast_cdr *cdr)
689 char *chan;
691 for (; cdr; cdr = cdr->next) {
692 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
693 chan = S_OR(cdr->channel, "<unknown>");
694 check_post(cdr);
695 cdr->start = ast_tvnow();
700 void ast_cdr_answer(struct ast_cdr *cdr)
703 for (; cdr; cdr = cdr->next) {
704 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
705 check_post(cdr);
706 if (cdr->disposition < AST_CDR_ANSWERED)
707 cdr->disposition = AST_CDR_ANSWERED;
708 if (ast_tvzero(cdr->answer))
709 cdr->answer = ast_tvnow();
714 void ast_cdr_busy(struct ast_cdr *cdr)
717 for (; cdr; cdr = cdr->next) {
718 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
719 check_post(cdr);
720 if (cdr->disposition < AST_CDR_BUSY)
721 cdr->disposition = AST_CDR_BUSY;
726 void ast_cdr_failed(struct ast_cdr *cdr)
728 for (; cdr; cdr = cdr->next) {
729 check_post(cdr);
730 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
731 if (cdr->disposition < AST_CDR_FAILED)
732 cdr->disposition = AST_CDR_FAILED;
737 void ast_cdr_noanswer(struct ast_cdr *cdr)
739 char *chan;
741 while (cdr) {
742 chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
743 if (ast_test_flag(cdr, AST_CDR_FLAG_POSTED))
744 ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
745 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
746 if (cdr->disposition < AST_CDR_NOANSWER)
747 cdr->disposition = AST_CDR_NOANSWER;
749 cdr = cdr->next;
753 /* everywhere ast_cdr_disposition is called, it will call ast_cdr_failed()
754 if ast_cdr_disposition returns a non-zero value */
756 int ast_cdr_disposition(struct ast_cdr *cdr, int cause)
758 int res = 0;
760 for (; cdr; cdr = cdr->next) {
761 switch(cause) { /* handle all the non failure, busy cases, return 0 not to set disposition,
762 return -1 to set disposition to FAILED */
763 case AST_CAUSE_BUSY:
764 ast_cdr_busy(cdr);
765 break;
766 case AST_CAUSE_NORMAL:
767 break;
768 default:
769 res = -1;
772 return res;
775 void ast_cdr_setdestchan(struct ast_cdr *cdr, const char *chann)
777 for (; cdr; cdr = cdr->next) {
778 check_post(cdr);
779 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
780 ast_copy_string(cdr->dstchannel, chann, sizeof(cdr->dstchannel));
784 void ast_cdr_setapp(struct ast_cdr *cdr, char *app, char *data)
787 for (; cdr; cdr = cdr->next) {
788 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
789 check_post(cdr);
790 ast_copy_string(cdr->lastapp, S_OR(app, ""), sizeof(cdr->lastapp));
791 ast_copy_string(cdr->lastdata, S_OR(data, ""), sizeof(cdr->lastdata));
796 /* set cid info for one record */
797 static void set_one_cid(struct ast_cdr *cdr, struct ast_channel *c)
799 /* Grab source from ANI or normal Caller*ID */
800 const char *num = S_OR(c->cid.cid_ani, c->cid.cid_num);
801 if (!cdr)
802 return;
803 if (!ast_strlen_zero(c->cid.cid_name)) {
804 if (!ast_strlen_zero(num)) /* both name and number */
805 snprintf(cdr->clid, sizeof(cdr->clid), "\"%s\" <%s>", c->cid.cid_name, num);
806 else /* only name */
807 ast_copy_string(cdr->clid, c->cid.cid_name, sizeof(cdr->clid));
808 } else if (!ast_strlen_zero(num)) { /* only number */
809 ast_copy_string(cdr->clid, num, sizeof(cdr->clid));
810 } else { /* nothing known */
811 cdr->clid[0] = '\0';
813 ast_copy_string(cdr->src, S_OR(num, ""), sizeof(cdr->src));
816 int ast_cdr_setcid(struct ast_cdr *cdr, struct ast_channel *c)
818 for (; cdr; cdr = cdr->next) {
819 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
820 set_one_cid(cdr, c);
822 return 0;
825 int ast_cdr_init(struct ast_cdr *cdr, struct ast_channel *c)
827 char *chan;
829 for ( ; cdr ; cdr = cdr->next) {
830 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
831 chan = S_OR(cdr->channel, "<unknown>");
832 ast_copy_string(cdr->channel, c->name, sizeof(cdr->channel));
833 set_one_cid(cdr, c);
835 cdr->disposition = (c->_state == AST_STATE_UP) ? AST_CDR_ANSWERED : AST_CDR_NULL;
836 cdr->amaflags = c->amaflags ? c->amaflags : ast_default_amaflags;
837 ast_copy_string(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode));
838 /* Destination information */
839 ast_copy_string(cdr->dst, S_OR(c->macroexten,c->exten), sizeof(cdr->dst));
840 ast_copy_string(cdr->dcontext, S_OR(c->macrocontext,c->context), sizeof(cdr->dcontext));
841 /* Unique call identifier */
842 ast_copy_string(cdr->uniqueid, c->uniqueid, sizeof(cdr->uniqueid));
845 return 0;
848 void ast_cdr_end(struct ast_cdr *cdr)
850 for ( ; cdr ; cdr = cdr->next) {
851 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
852 check_post(cdr);
853 if (ast_tvzero(cdr->end))
854 cdr->end = ast_tvnow();
855 if (ast_tvzero(cdr->start)) {
856 ast_log(LOG_WARNING, "CDR on channel '%s' has not started\n", S_OR(cdr->channel, "<unknown>"));
857 cdr->disposition = AST_CDR_FAILED;
858 } else
859 cdr->duration = cdr->end.tv_sec - cdr->start.tv_sec;
860 cdr->billsec = ast_tvzero(cdr->answer) ? 0 : cdr->end.tv_sec - cdr->answer.tv_sec;
865 char *ast_cdr_disp2str(int disposition)
867 switch (disposition) {
868 case AST_CDR_NULL:
869 return "NO ANSWER"; /* by default, for backward compatibility */
870 case AST_CDR_NOANSWER:
871 return "NO ANSWER";
872 case AST_CDR_FAILED:
873 return "FAILED";
874 case AST_CDR_BUSY:
875 return "BUSY";
876 case AST_CDR_ANSWERED:
877 return "ANSWERED";
879 return "UNKNOWN";
882 /*! Converts AMA flag to printable string */
883 char *ast_cdr_flags2str(int flag)
885 switch(flag) {
886 case AST_CDR_OMIT:
887 return "OMIT";
888 case AST_CDR_BILLING:
889 return "BILLING";
890 case AST_CDR_DOCUMENTATION:
891 return "DOCUMENTATION";
893 return "Unknown";
896 int ast_cdr_setaccount(struct ast_channel *chan, const char *account)
898 struct ast_cdr *cdr = chan->cdr;
900 ast_string_field_set(chan, accountcode, account);
901 for ( ; cdr ; cdr = cdr->next) {
902 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
903 ast_copy_string(cdr->accountcode, chan->accountcode, sizeof(cdr->accountcode));
906 return 0;
909 int ast_cdr_setamaflags(struct ast_channel *chan, const char *flag)
911 struct ast_cdr *cdr;
912 int newflag = ast_cdr_amaflags2int(flag);
913 if (newflag) {
914 for (cdr = chan->cdr; cdr; cdr = cdr->next) {
915 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
916 cdr->amaflags = newflag;
921 return 0;
924 int ast_cdr_setuserfield(struct ast_channel *chan, const char *userfield)
926 struct ast_cdr *cdr = chan->cdr;
928 for ( ; cdr ; cdr = cdr->next) {
929 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
930 ast_copy_string(cdr->userfield, userfield, sizeof(cdr->userfield));
933 return 0;
936 int ast_cdr_appenduserfield(struct ast_channel *chan, const char *userfield)
938 struct ast_cdr *cdr = chan->cdr;
940 for ( ; cdr ; cdr = cdr->next) {
941 int len = strlen(cdr->userfield);
943 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
944 ast_copy_string(cdr->userfield + len, userfield, sizeof(cdr->userfield) - len);
947 return 0;
950 int ast_cdr_update(struct ast_channel *c)
952 struct ast_cdr *cdr = c->cdr;
954 for ( ; cdr ; cdr = cdr->next) {
955 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
956 set_one_cid(cdr, c);
958 /* Copy account code et-al */
959 ast_copy_string(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode));
961 /* Destination information */ /* XXX privilege macro* ? */
962 ast_copy_string(cdr->dst, S_OR(c->macroexten, c->exten), sizeof(cdr->dst));
963 ast_copy_string(cdr->dcontext, S_OR(c->macrocontext, c->context), sizeof(cdr->dcontext));
967 return 0;
970 int ast_cdr_amaflags2int(const char *flag)
972 if (!strcasecmp(flag, "default"))
973 return 0;
974 if (!strcasecmp(flag, "omit"))
975 return AST_CDR_OMIT;
976 if (!strcasecmp(flag, "billing"))
977 return AST_CDR_BILLING;
978 if (!strcasecmp(flag, "documentation"))
979 return AST_CDR_DOCUMENTATION;
980 return -1;
983 static void post_cdr(struct ast_cdr *cdr)
985 char *chan;
986 struct ast_cdr_beitem *i;
988 for ( ; cdr ; cdr = cdr->next) {
989 chan = S_OR(cdr->channel, "<unknown>");
990 check_post(cdr);
991 if (ast_tvzero(cdr->end))
992 ast_log(LOG_WARNING, "CDR on channel '%s' lacks end\n", chan);
993 if (ast_tvzero(cdr->start))
994 ast_log(LOG_WARNING, "CDR on channel '%s' lacks start\n", chan);
995 ast_set_flag(cdr, AST_CDR_FLAG_POSTED);
996 if (ast_test_flag(cdr, AST_CDR_FLAG_POST_DISABLED))
997 continue;
998 AST_LIST_LOCK(&be_list);
999 AST_LIST_TRAVERSE(&be_list, i, list) {
1000 i->be(cdr);
1002 AST_LIST_UNLOCK(&be_list);
1006 void ast_cdr_reset(struct ast_cdr *cdr, struct ast_flags *_flags)
1008 struct ast_cdr *dup;
1009 struct ast_flags flags = { 0 };
1011 if (_flags)
1012 ast_copy_flags(&flags, _flags, AST_FLAGS_ALL);
1014 for ( ; cdr ; cdr = cdr->next) {
1015 /* Detach if post is requested */
1016 if (ast_test_flag(&flags, AST_CDR_FLAG_LOCKED) || !ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1017 if (ast_test_flag(&flags, AST_CDR_FLAG_POSTED)) {
1018 ast_cdr_end(cdr);
1019 if ((dup = ast_cdr_dup(cdr))) {
1020 ast_cdr_detach(dup);
1022 ast_set_flag(cdr, AST_CDR_FLAG_POSTED);
1025 /* clear variables */
1026 if (!ast_test_flag(&flags, AST_CDR_FLAG_KEEP_VARS)) {
1027 ast_cdr_free_vars(cdr, 0);
1030 /* Reset to initial state */
1031 ast_clear_flag(cdr, AST_FLAGS_ALL);
1032 memset(&cdr->start, 0, sizeof(cdr->start));
1033 memset(&cdr->end, 0, sizeof(cdr->end));
1034 memset(&cdr->answer, 0, sizeof(cdr->answer));
1035 cdr->billsec = 0;
1036 cdr->duration = 0;
1037 ast_cdr_start(cdr);
1038 cdr->disposition = AST_CDR_NULL;
1043 struct ast_cdr *ast_cdr_append(struct ast_cdr *cdr, struct ast_cdr *newcdr)
1045 struct ast_cdr *ret;
1047 if (cdr) {
1048 ret = cdr;
1050 while (cdr->next)
1051 cdr = cdr->next;
1052 cdr->next = newcdr;
1053 } else {
1054 ret = newcdr;
1057 return ret;
1060 /*! \note Don't call without cdr_batch_lock */
1061 static void reset_batch(void)
1063 batch->size = 0;
1064 batch->head = NULL;
1065 batch->tail = NULL;
1068 /*! \note Don't call without cdr_batch_lock */
1069 static int init_batch(void)
1071 /* This is the single meta-batch used to keep track of all CDRs during the entire life of the program */
1072 if (!(batch = ast_malloc(sizeof(*batch))))
1073 return -1;
1075 reset_batch();
1077 return 0;
1080 static void *do_batch_backend_process(void *data)
1082 struct ast_cdr_batch_item *processeditem;
1083 struct ast_cdr_batch_item *batchitem = data;
1085 /* Push each CDR into storage mechanism(s) and free all the memory */
1086 while (batchitem) {
1087 post_cdr(batchitem->cdr);
1088 ast_cdr_free(batchitem->cdr);
1089 processeditem = batchitem;
1090 batchitem = batchitem->next;
1091 free(processeditem);
1094 return NULL;
1097 void ast_cdr_submit_batch(int shutdown)
1099 struct ast_cdr_batch_item *oldbatchitems = NULL;
1100 pthread_attr_t attr;
1101 pthread_t batch_post_thread = AST_PTHREADT_NULL;
1103 /* if there's no batch, or no CDRs in the batch, then there's nothing to do */
1104 if (!batch || !batch->head)
1105 return;
1107 /* move the old CDRs aside, and prepare a new CDR batch */
1108 ast_mutex_lock(&cdr_batch_lock);
1109 oldbatchitems = batch->head;
1110 reset_batch();
1111 ast_mutex_unlock(&cdr_batch_lock);
1113 /* if configured, spawn a new thread to post these CDRs,
1114 also try to save as much as possible if we are shutting down safely */
1115 if (batchscheduleronly || shutdown) {
1116 if (option_debug)
1117 ast_log(LOG_DEBUG, "CDR single-threaded batch processing begins now\n");
1118 do_batch_backend_process(oldbatchitems);
1119 } else {
1120 pthread_attr_init(&attr);
1121 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1122 if (ast_pthread_create_background(&batch_post_thread, &attr, do_batch_backend_process, oldbatchitems)) {
1123 ast_log(LOG_WARNING, "CDR processing thread could not detach, now trying in this thread\n");
1124 do_batch_backend_process(oldbatchitems);
1125 } else {
1126 if (option_debug)
1127 ast_log(LOG_DEBUG, "CDR multi-threaded batch processing begins now\n");
1129 pthread_attr_destroy(&attr);
1133 static int submit_scheduled_batch(const void *data)
1135 ast_cdr_submit_batch(0);
1136 /* manually reschedule from this point in time */
1137 cdr_sched = ast_sched_add(sched, batchtime * 1000, submit_scheduled_batch, NULL);
1138 /* returning zero so the scheduler does not automatically reschedule */
1139 return 0;
1142 static void submit_unscheduled_batch(void)
1144 /* this is okay since we are not being called from within the scheduler */
1145 AST_SCHED_DEL(sched, cdr_sched);
1146 /* schedule the submission to occur ASAP (1 ms) */
1147 cdr_sched = ast_sched_add(sched, 1, submit_scheduled_batch, NULL);
1148 /* signal the do_cdr thread to wakeup early and do some work (that lazy thread ;) */
1149 ast_mutex_lock(&cdr_pending_lock);
1150 ast_cond_signal(&cdr_pending_cond);
1151 ast_mutex_unlock(&cdr_pending_lock);
1154 void ast_cdr_detach(struct ast_cdr *cdr)
1156 struct ast_cdr_batch_item *newtail;
1157 int curr;
1159 if (!cdr)
1160 return;
1162 /* maybe they disabled CDR stuff completely, so just drop it */
1163 if (!enabled) {
1164 if (option_debug)
1165 ast_log(LOG_DEBUG, "Dropping CDR !\n");
1166 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1167 ast_cdr_free(cdr);
1168 return;
1171 /* post stuff immediately if we are not in batch mode, this is legacy behaviour */
1172 if (!batchmode) {
1173 post_cdr(cdr);
1174 ast_cdr_free(cdr);
1175 return;
1178 /* otherwise, each CDR gets put into a batch list (at the end) */
1179 if (option_debug)
1180 ast_log(LOG_DEBUG, "CDR detaching from this thread\n");
1182 /* we'll need a new tail for every CDR */
1183 if (!(newtail = ast_calloc(1, sizeof(*newtail)))) {
1184 post_cdr(cdr);
1185 ast_cdr_free(cdr);
1186 return;
1189 /* don't traverse a whole list (just keep track of the tail) */
1190 ast_mutex_lock(&cdr_batch_lock);
1191 if (!batch)
1192 init_batch();
1193 if (!batch->head) {
1194 /* new batch is empty, so point the head at the new tail */
1195 batch->head = newtail;
1196 } else {
1197 /* already got a batch with something in it, so just append a new tail */
1198 batch->tail->next = newtail;
1200 newtail->cdr = cdr;
1201 batch->tail = newtail;
1202 curr = batch->size++;
1203 ast_mutex_unlock(&cdr_batch_lock);
1205 /* if we have enough stuff to post, then do it */
1206 if (curr >= (batchsize - 1))
1207 submit_unscheduled_batch();
1210 static void *do_cdr(void *data)
1212 struct timespec timeout;
1213 int schedms;
1214 int numevents = 0;
1216 for(;;) {
1217 struct timeval now;
1218 schedms = ast_sched_wait(sched);
1219 /* this shouldn't happen, but provide a 1 second default just in case */
1220 if (schedms <= 0)
1221 schedms = 1000;
1222 now = ast_tvadd(ast_tvnow(), ast_samp2tv(schedms, 1000));
1223 timeout.tv_sec = now.tv_sec;
1224 timeout.tv_nsec = now.tv_usec * 1000;
1225 /* prevent stuff from clobbering cdr_pending_cond, then wait on signals sent to it until the timeout expires */
1226 ast_mutex_lock(&cdr_pending_lock);
1227 ast_cond_timedwait(&cdr_pending_cond, &cdr_pending_lock, &timeout);
1228 numevents = ast_sched_runq(sched);
1229 ast_mutex_unlock(&cdr_pending_lock);
1230 if (option_debug > 1)
1231 ast_log(LOG_DEBUG, "Processed %d scheduled CDR batches from the run queue\n", numevents);
1234 return NULL;
1237 static int handle_cli_status(int fd, int argc, char *argv[])
1239 struct ast_cdr_beitem *beitem=NULL;
1240 int cnt=0;
1241 long nextbatchtime=0;
1243 if (argc > 2)
1244 return RESULT_SHOWUSAGE;
1246 ast_cli(fd, "CDR logging: %s\n", enabled ? "enabled" : "disabled");
1247 ast_cli(fd, "CDR mode: %s\n", batchmode ? "batch" : "simple");
1248 if (enabled) {
1249 ast_cli(fd, "CDR output unanswered calls: %s\n", unanswered ? "yes" : "no");
1250 if (batchmode) {
1251 if (batch)
1252 cnt = batch->size;
1253 if (cdr_sched > -1)
1254 nextbatchtime = ast_sched_when(sched, cdr_sched);
1255 ast_cli(fd, "CDR safe shut down: %s\n", batchsafeshutdown ? "enabled" : "disabled");
1256 ast_cli(fd, "CDR batch threading model: %s\n", batchscheduleronly ? "scheduler only" : "scheduler plus separate threads");
1257 ast_cli(fd, "CDR current batch size: %d record%s\n", cnt, (cnt != 1) ? "s" : "");
1258 ast_cli(fd, "CDR maximum batch size: %d record%s\n", batchsize, (batchsize != 1) ? "s" : "");
1259 ast_cli(fd, "CDR maximum batch time: %d second%s\n", batchtime, (batchtime != 1) ? "s" : "");
1260 ast_cli(fd, "CDR next scheduled batch processing time: %ld second%s\n", nextbatchtime, (nextbatchtime != 1) ? "s" : "");
1262 AST_LIST_LOCK(&be_list);
1263 AST_LIST_TRAVERSE(&be_list, beitem, list) {
1264 ast_cli(fd, "CDR registered backend: %s\n", beitem->name);
1266 AST_LIST_UNLOCK(&be_list);
1269 return 0;
1272 static int handle_cli_submit(int fd, int argc, char *argv[])
1274 if (argc > 2)
1275 return RESULT_SHOWUSAGE;
1277 submit_unscheduled_batch();
1278 ast_cli(fd, "Submitted CDRs to backend engines for processing. This may take a while.\n");
1280 return 0;
1283 static struct ast_cli_entry cli_submit = {
1284 { "cdr", "submit", NULL },
1285 handle_cli_submit, "Posts all pending batched CDR data",
1286 "Usage: cdr submit\n"
1287 " Posts all pending batched CDR data to the configured CDR backend engine modules.\n"
1290 static struct ast_cli_entry cli_status = {
1291 { "cdr", "status", NULL },
1292 handle_cli_status, "Display the CDR status",
1293 "Usage: cdr status\n"
1294 " Displays the Call Detail Record engine system status.\n"
1297 static int do_reload(void)
1299 struct ast_config *config;
1300 const char *enabled_value;
1301 const char *unanswered_value;
1302 const char *batched_value;
1303 const char *scheduleronly_value;
1304 const char *batchsafeshutdown_value;
1305 const char *size_value;
1306 const char *time_value;
1307 const char *end_before_h_value;
1308 int cfg_size;
1309 int cfg_time;
1310 int was_enabled;
1311 int was_batchmode;
1312 int res=0;
1314 ast_mutex_lock(&cdr_batch_lock);
1316 batchsize = BATCH_SIZE_DEFAULT;
1317 batchtime = BATCH_TIME_DEFAULT;
1318 batchscheduleronly = BATCH_SCHEDULER_ONLY_DEFAULT;
1319 batchsafeshutdown = BATCH_SAFE_SHUTDOWN_DEFAULT;
1320 was_enabled = enabled;
1321 was_batchmode = batchmode;
1322 enabled = 1;
1323 batchmode = 0;
1325 /* don't run the next scheduled CDR posting while reloading */
1326 AST_SCHED_DEL(sched, cdr_sched);
1328 if ((config = ast_config_load("cdr.conf"))) {
1329 if ((enabled_value = ast_variable_retrieve(config, "general", "enable"))) {
1330 enabled = ast_true(enabled_value);
1332 if ((unanswered_value = ast_variable_retrieve(config, "general", "unanswered"))) {
1333 unanswered = ast_true(unanswered_value);
1335 if ((batched_value = ast_variable_retrieve(config, "general", "batch"))) {
1336 batchmode = ast_true(batched_value);
1338 if ((scheduleronly_value = ast_variable_retrieve(config, "general", "scheduleronly"))) {
1339 batchscheduleronly = ast_true(scheduleronly_value);
1341 if ((batchsafeshutdown_value = ast_variable_retrieve(config, "general", "safeshutdown"))) {
1342 batchsafeshutdown = ast_true(batchsafeshutdown_value);
1344 if ((size_value = ast_variable_retrieve(config, "general", "size"))) {
1345 if (sscanf(size_value, "%d", &cfg_size) < 1)
1346 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", size_value);
1347 else if (size_value < 0)
1348 ast_log(LOG_WARNING, "Invalid maximum batch size '%d' specified, using default\n", cfg_size);
1349 else
1350 batchsize = cfg_size;
1352 if ((time_value = ast_variable_retrieve(config, "general", "time"))) {
1353 if (sscanf(time_value, "%d", &cfg_time) < 1)
1354 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", time_value);
1355 else if (time_value < 0)
1356 ast_log(LOG_WARNING, "Invalid maximum batch time '%d' specified, using default\n", cfg_time);
1357 else
1358 batchtime = cfg_time;
1360 if ((end_before_h_value = ast_variable_retrieve(config, "general", "endbeforehexten")))
1361 ast_set2_flag(&ast_options, ast_true(end_before_h_value), AST_OPT_FLAG_END_CDR_BEFORE_H_EXTEN);
1364 if (enabled && !batchmode) {
1365 ast_log(LOG_NOTICE, "CDR simple logging enabled.\n");
1366 } else if (enabled && batchmode) {
1367 cdr_sched = ast_sched_add(sched, batchtime * 1000, submit_scheduled_batch, NULL);
1368 ast_log(LOG_NOTICE, "CDR batch mode logging enabled, first of either size %d or time %d seconds.\n", batchsize, batchtime);
1369 } else {
1370 ast_log(LOG_NOTICE, "CDR logging disabled, data will be lost.\n");
1373 /* if this reload enabled the CDR batch mode, create the background thread
1374 if it does not exist */
1375 if (enabled && batchmode && (!was_enabled || !was_batchmode) && (cdr_thread == AST_PTHREADT_NULL)) {
1376 ast_cond_init(&cdr_pending_cond, NULL);
1377 if (ast_pthread_create_background(&cdr_thread, NULL, do_cdr, NULL) < 0) {
1378 ast_log(LOG_ERROR, "Unable to start CDR thread.\n");
1379 AST_SCHED_DEL(sched, cdr_sched);
1380 } else {
1381 ast_cli_register(&cli_submit);
1382 ast_register_atexit(ast_cdr_engine_term);
1383 res = 0;
1385 /* if this reload disabled the CDR and/or batch mode and there is a background thread,
1386 kill it */
1387 } else if (((!enabled && was_enabled) || (!batchmode && was_batchmode)) && (cdr_thread != AST_PTHREADT_NULL)) {
1388 /* wake up the thread so it will exit */
1389 pthread_cancel(cdr_thread);
1390 pthread_kill(cdr_thread, SIGURG);
1391 pthread_join(cdr_thread, NULL);
1392 cdr_thread = AST_PTHREADT_NULL;
1393 ast_cond_destroy(&cdr_pending_cond);
1394 ast_cli_unregister(&cli_submit);
1395 ast_unregister_atexit(ast_cdr_engine_term);
1396 res = 0;
1397 /* if leaving batch mode, then post the CDRs in the batch,
1398 and don't reschedule, since we are stopping CDR logging */
1399 if (!batchmode && was_batchmode) {
1400 ast_cdr_engine_term();
1402 } else {
1403 res = 0;
1406 ast_mutex_unlock(&cdr_batch_lock);
1407 ast_config_destroy(config);
1409 return res;
1412 int ast_cdr_engine_init(void)
1414 int res;
1416 sched = sched_context_create();
1417 if (!sched) {
1418 ast_log(LOG_ERROR, "Unable to create schedule context.\n");
1419 return -1;
1422 ast_cli_register(&cli_status);
1424 res = do_reload();
1425 if (res) {
1426 ast_mutex_lock(&cdr_batch_lock);
1427 res = init_batch();
1428 ast_mutex_unlock(&cdr_batch_lock);
1431 return res;
1434 /* \note This actually gets called a couple of times at shutdown. Once, before we start
1435 hanging up channels, and then again, after the channel hangup timeout expires */
1436 void ast_cdr_engine_term(void)
1438 ast_cdr_submit_batch(batchsafeshutdown);
1441 int ast_cdr_engine_reload(void)
1443 return do_reload();