Merged revisions 44378 via svnmerge from
[asterisk/asterisk.git] / main / cdr.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*! \file
20  *
21  * \brief Call Detail Record API 
22  *
23  * \author Mark Spencer <markster@digium.com>
24  * 
25  * \note Includes code and algorithms from the Zapata library.
26  *
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.
31  */
32
33
34 #include "asterisk.h"
35
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37
38 #include <unistd.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <stdio.h>
42 #include <signal.h>
43
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"
57
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] = "";
61
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;
67 };
68
69 static AST_LIST_HEAD_STATIC(be_list, ast_cdr_beitem);
70
71 struct ast_cdr_batch_item {
72         struct ast_cdr *cdr;
73         struct ast_cdr_batch_item *next;
74 };
75
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;
81
82 static struct sched_context *sched;
83 static int cdr_sched = -1;
84 static pthread_t cdr_thread = AST_PTHREADT_NULL;
85
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
90
91 static int enabled;
92 static int batchmode;
93 static int batchsize;
94 static int batchtime;
95 static int batchscheduleronly;
96 static int batchsafeshutdown;
97
98 AST_MUTEX_DEFINE_STATIC(cdr_batch_lock);
99
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;
103
104
105 /*! Register a CDR driver. Each registered CDR driver generates a CDR 
106         \return 0 on success, -1 on failure 
107 */
108 int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be)
109 {
110         struct ast_cdr_beitem *i;
111
112         if (!name)
113                 return -1;
114         if (!be) {
115                 ast_log(LOG_WARNING, "CDR engine '%s' lacks backend\n", name);
116                 return -1;
117         }
118
119         AST_LIST_LOCK(&be_list);
120         AST_LIST_TRAVERSE(&be_list, i, list) {
121                 if (!strcasecmp(name, i->name))
122                         break;
123         }
124         AST_LIST_UNLOCK(&be_list);
125
126         if (i) {
127                 ast_log(LOG_WARNING, "Already have a CDR backend called '%s'\n", name);
128                 return -1;
129         }
130
131         if (!(i = ast_calloc(1, sizeof(*i))))   
132                 return -1;
133
134         i->be = be;
135         ast_copy_string(i->name, name, sizeof(i->name));
136         ast_copy_string(i->desc, desc, sizeof(i->desc));
137
138         AST_LIST_LOCK(&be_list);
139         AST_LIST_INSERT_HEAD(&be_list, i, list);
140         AST_LIST_UNLOCK(&be_list);
141
142         return 0;
143 }
144
145 /*! unregister a CDR driver */
146 void ast_cdr_unregister(const char *name)
147 {
148         struct ast_cdr_beitem *i = NULL;
149
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);
156                         free(i);
157                         break;
158                 }
159         }
160         AST_LIST_TRAVERSE_SAFE_END;
161         AST_LIST_UNLOCK(&be_list);
162 }
163
164 /*! Duplicate a CDR record 
165         \returns Pointer to new CDR record
166 */
167 struct ast_cdr *ast_cdr_dup(struct ast_cdr *cdr) 
168 {
169         struct ast_cdr *newcdr = ast_cdr_alloc();
170
171         if (!newcdr)
172                 return NULL;
173
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);
178         newcdr->next = NULL;
179
180         return newcdr;
181 }
182
183 static const char *ast_cdr_getvar_internal(struct ast_cdr *cdr, const char *name, int recur) 
184 {
185         if (ast_strlen_zero(name))
186                 return NULL;
187
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);
194                 }
195         }
196
197         return NULL;
198 }
199
200 static void cdr_get_tv(struct timeval tv, const char *fmt, char *buf, int bufsize)
201 {
202         if (fmt == NULL) {      /* raw mode */
203                 snprintf(buf, bufsize, "%ld.%06ld", (long)tv.tv_sec, (long)tv.tv_usec);
204         } else {  
205                 time_t t = tv.tv_sec;
206                 if (t) {
207                         struct tm tm;
208                         localtime_r(&t, &tm);
209                         strftime(buf, bufsize, fmt, &tm);
210                 }
211         }
212 }
213
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) 
216 {
217         const char *fmt = "%Y-%m-%d %T";
218         const char *varbuf;
219
220         *ret = NULL;
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 */
223
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")) {
251                 if (raw) {
252                         snprintf(workspace, workspacelen, "%ld", cdr->disposition);
253                 } else {
254                         ast_copy_string(workspace, ast_cdr_disp2str(cdr->disposition), workspacelen);
255                 }
256         } else if (!strcasecmp(name, "amaflags")) {
257                 if (raw) {
258                         snprintf(workspace, workspacelen, "%ld", cdr->amaflags);
259                 } else {
260                         ast_copy_string(workspace, ast_cdr_flags2str(cdr->amaflags), workspacelen);
261                 }
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);
270         else
271                 workspace[0] = '\0';
272
273         if (!ast_strlen_zero(workspace))
274                 *ret = workspace;
275 }
276
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",
281                                     "userfield", NULL };
282 /*! Set a CDR channel variable 
283         \note You can't set the CDR variables that belong to the actual CDR record, like "billsec".
284 */
285 int ast_cdr_setvar(struct ast_cdr *cdr, const char *name, const char *value, int recur) 
286 {
287         struct ast_var_t *newvariable;
288         struct varshead *headp;
289         int x;
290         
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);
294                         return -1;
295                 }
296         }
297
298         if (!cdr) {
299                 ast_log(LOG_ERROR, "Attempt to set a variable on a nonexistent CDR record.\n");
300                 return -1;
301         }
302
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);
310                                 break;
311                         }
312                 }
313                 AST_LIST_TRAVERSE_SAFE_END;
314
315                 if (value) {
316                         newvariable = ast_var_assign(name, value);
317                         AST_LIST_INSERT_HEAD(headp, newvariable, entries);
318                 }
319         }
320
321         return 0;
322 }
323
324 int ast_cdr_copy_vars(struct ast_cdr *to_cdr, struct ast_cdr *from_cdr)
325 {
326         struct ast_var_t *variables, *newvariable = NULL;
327         struct varshead *headpa, *headpb;
328         const char *var, *val;
329         int x = 0;
330
331         headpa = &from_cdr->varshead;
332         headpb = &to_cdr->varshead;
333
334         AST_LIST_TRAVERSE(headpa,variables,entries) {
335                 if (variables &&
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);
340                         x++;
341                 }
342         }
343
344         return x;
345 }
346
347 int ast_cdr_serialize_variables(struct ast_cdr *cdr, char *buf, size_t size, char delim, char sep, int recur) 
348 {
349         struct ast_var_t *variables;
350         const char *var, *val;
351         char *tmp;
352         char workspace[256];
353         int total = 0, x = 0, i;
354
355         memset(buf, 0, size);
356
357         for (; cdr; cdr = recur ? cdr->next : NULL) {
358                 if (++x > 1)
359                         ast_build_string(&buf, &size, "\n");
360
361                 AST_LIST_TRAVERSE(&cdr->varshead, variables, entries) {
362                         if (variables &&
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");
367                                         break;
368                                 } else
369                                         total++;
370                         } else 
371                                 break;
372                 }
373
374                 for (i = 0; cdr_readonly_vars[i]; i++) {
375                         ast_cdr_getvar(cdr, cdr_readonly_vars[i], &tmp, workspace, sizeof(workspace), 0, 0);
376                         if (!tmp)
377                                 continue;
378                         
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");
381                                 break;
382                         } else
383                                 total++;
384                 }
385         }
386
387         return total;
388 }
389
390
391 void ast_cdr_free_vars(struct ast_cdr *cdr, int recur)
392 {
393
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);
400         }
401 }
402
403 /*! \brief  print a warning if cdr already posted */
404 static void check_post(struct ast_cdr *cdr)
405 {
406         if (ast_test_flag(cdr, AST_CDR_FLAG_POSTED))
407                 ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", S_OR(cdr->channel, "<unknown>"));
408 }
409
410 /*! \brief  print a warning if cdr already started */
411 static void check_start(struct ast_cdr *cdr)
412 {
413         if (!ast_tvzero(cdr->start))
414                 ast_log(LOG_WARNING, "CDR on channel '%s' already started\n", S_OR(cdr->channel, "<unknown>"));
415 }
416
417 void ast_cdr_free(struct ast_cdr *cdr)
418 {
419
420         while (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_WARNING, "CDR on channel '%s' not posted\n", chan);
425                 if (ast_tvzero(cdr->end))
426                         ast_log(LOG_WARNING, "CDR on channel '%s' lacks end\n", chan);
427                 if (ast_tvzero(cdr->start))
428                         ast_log(LOG_WARNING, "CDR on channel '%s' lacks start\n", chan);
429
430                 ast_cdr_free_vars(cdr, 0);
431                 free(cdr);
432                 cdr = next;
433         }
434 }
435
436 struct ast_cdr *ast_cdr_alloc(void)
437 {
438         return ast_calloc(1, sizeof(struct ast_cdr));
439 }
440
441 void ast_cdr_start(struct ast_cdr *cdr)
442 {
443         char *chan; 
444
445         for (; cdr; cdr = cdr->next) {
446                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
447                         chan = S_OR(cdr->channel, "<unknown>");
448                         check_post(cdr);
449                         check_start(cdr);
450                         cdr->start = ast_tvnow();
451                 }
452         }
453 }
454
455 void ast_cdr_answer(struct ast_cdr *cdr)
456 {
457
458         for (; cdr; cdr = cdr->next) {
459                 check_post(cdr);
460                 if (cdr->disposition < AST_CDR_ANSWERED)
461                         cdr->disposition = AST_CDR_ANSWERED;
462                 if (ast_tvzero(cdr->answer))
463                         cdr->answer = ast_tvnow();
464         }
465 }
466
467 void ast_cdr_busy(struct ast_cdr *cdr)
468 {
469
470         for (; cdr; cdr = cdr->next) {
471                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
472                         check_post(cdr);
473                         if (cdr->disposition < AST_CDR_BUSY)
474                                 cdr->disposition = AST_CDR_BUSY;
475                 }
476         }
477 }
478
479 void ast_cdr_failed(struct ast_cdr *cdr)
480 {
481         for (; cdr; cdr = cdr->next) {
482                 check_post(cdr);
483                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
484                         if (cdr->disposition < AST_CDR_FAILED)
485                                 cdr->disposition = AST_CDR_FAILED;
486                 }
487         }
488 }
489
490 int ast_cdr_disposition(struct ast_cdr *cdr, int cause)
491 {
492         int res = 0;
493
494         for (; cdr; cdr = cdr->next) {
495                 switch(cause) {
496                 case AST_CAUSE_BUSY:
497                         ast_cdr_busy(cdr);
498                         break;
499                 case AST_CAUSE_FAILURE:
500                         ast_cdr_failed(cdr);
501                         break;
502                 case AST_CAUSE_NORMAL:
503                         break;
504                 case AST_CAUSE_NOTDEFINED:
505                         res = -1;
506                         break;
507                 default:
508                         res = -1;
509                         ast_log(LOG_WARNING, "Cause not handled\n");
510                 }
511         }
512         return res;
513 }
514
515 void ast_cdr_setdestchan(struct ast_cdr *cdr, const char *chann)
516 {
517         for (; cdr; cdr = cdr->next) {
518                 check_post(cdr);
519                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
520                         ast_copy_string(cdr->dstchannel, chann, sizeof(cdr->dstchannel));
521         }
522 }
523
524 void ast_cdr_setapp(struct ast_cdr *cdr, char *app, char *data)
525 {
526
527         for (; cdr; cdr = cdr->next) {
528                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
529                         check_post(cdr);
530                         if (!app)
531                                 app = "";
532                         ast_copy_string(cdr->lastapp, app, sizeof(cdr->lastapp));
533                         if (!data)
534                                 data = "";
535                         ast_copy_string(cdr->lastdata, data, sizeof(cdr->lastdata));
536                 }
537         }
538 }
539
540 /* set cid info for one record */
541 static void set_one_cid(struct ast_cdr *cdr, struct ast_channel *c)
542 {
543         /* Grab source from ANI or normal Caller*ID */
544         const char *num = S_OR(c->cid.cid_ani, c->cid.cid_num);
545         
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);
549                 else                            /* only name */
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 */
554                 cdr->clid[0] = '\0';
555         }
556         ast_copy_string(cdr->src, S_OR(num, ""), sizeof(cdr->src));
557
558 }
559 int ast_cdr_setcid(struct ast_cdr *cdr, struct ast_channel *c)
560 {
561         for (; cdr; cdr = cdr->next) {
562                 if (ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
563                         set_one_cid(cdr, c);
564         }
565         return 0;
566 }
567
568 int ast_cdr_init(struct ast_cdr *cdr, struct ast_channel *c)
569 {
570         char *chan;
571
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));
578                         set_one_cid(cdr, c);
579
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));
588                 }
589         }
590         return 0;
591 }
592
593 void ast_cdr_end(struct ast_cdr *cdr)
594 {
595         for ( ; cdr ; cdr = cdr->next) {
596                 check_post(cdr);
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;
602                 } else
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;
605         }
606 }
607
608 char *ast_cdr_disp2str(int disposition)
609 {
610         switch (disposition) {
611         case AST_CDR_NOANSWER:
612                 return "NO ANSWER";
613         case AST_CDR_FAILED:
614                 return "FAILED";                
615         case AST_CDR_BUSY:
616                 return "BUSY";          
617         case AST_CDR_ANSWERED:
618                 return "ANSWERED";
619         }
620         return "UNKNOWN";
621 }
622
623 /*! Converts AMA flag to printable string */
624 char *ast_cdr_flags2str(int flag)
625 {
626         switch(flag) {
627         case AST_CDR_OMIT:
628                 return "OMIT";
629         case AST_CDR_BILLING:
630                 return "BILLING";
631         case AST_CDR_DOCUMENTATION:
632                 return "DOCUMENTATION";
633         }
634         return "Unknown";
635 }
636
637 int ast_cdr_setaccount(struct ast_channel *chan, const char *account)
638 {
639         struct ast_cdr *cdr = chan->cdr;
640
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));
645         }
646         return 0;
647 }
648
649 int ast_cdr_setamaflags(struct ast_channel *chan, const char *flag)
650 {
651         struct ast_cdr *cdr;
652         int newflag = ast_cdr_amaflags2int(flag);
653         if (newflag) {
654                 for (cdr = chan->cdr; cdr; cdr = cdr->next)
655                         cdr->amaflags = newflag;
656         }
657
658         return 0;
659 }
660
661 int ast_cdr_setuserfield(struct ast_channel *chan, const char *userfield)
662 {
663         struct ast_cdr *cdr = chan->cdr;
664
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));
668         }
669
670         return 0;
671 }
672
673 int ast_cdr_appenduserfield(struct ast_channel *chan, const char *userfield)
674 {
675         struct ast_cdr *cdr = chan->cdr;
676
677         for ( ; cdr ; cdr = cdr->next) {
678                 int len = strlen(cdr->userfield);
679
680                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
681                         strncpy(cdr->userfield+len, userfield, sizeof(cdr->userfield) - len - 1);
682         }
683
684         return 0;
685 }
686
687 int ast_cdr_update(struct ast_channel *c)
688 {
689         struct ast_cdr *cdr = c->cdr;
690
691         for ( ; cdr ; cdr = cdr->next) {
692                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
693                         set_one_cid(cdr, c);
694
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));
700                 }
701         }
702
703         return 0;
704 }
705
706 int ast_cdr_amaflags2int(const char *flag)
707 {
708         if (!strcasecmp(flag, "default"))
709                 return 0;
710         if (!strcasecmp(flag, "omit"))
711                 return AST_CDR_OMIT;
712         if (!strcasecmp(flag, "billing"))
713                 return AST_CDR_BILLING;
714         if (!strcasecmp(flag, "documentation"))
715                 return AST_CDR_DOCUMENTATION;
716         return -1;
717 }
718
719 static void post_cdr(struct ast_cdr *cdr)
720 {
721         char *chan;
722         struct ast_cdr_beitem *i;
723
724         for ( ; cdr ; cdr = cdr->next) {
725                 chan = S_OR(cdr->channel, "<unknown>");
726                 check_post(cdr);
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) {
734                         i->be(cdr);
735                 }
736                 AST_LIST_UNLOCK(&be_list);
737         }
738 }
739
740 void ast_cdr_reset(struct ast_cdr *cdr, struct ast_flags *_flags)
741 {
742         struct ast_cdr *dup;
743         struct ast_flags flags = { 0 };
744
745         if (_flags)
746                 ast_copy_flags(&flags, _flags, AST_FLAGS_ALL);
747
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)) {
752                                 ast_cdr_end(cdr);
753                                 if ((dup = ast_cdr_dup(cdr))) {
754                                         ast_cdr_detach(dup);
755                                 }
756                                 ast_set_flag(cdr, AST_CDR_FLAG_POSTED);
757                         }
758
759                         /* clear variables */
760                         if (!ast_test_flag(&flags, AST_CDR_FLAG_KEEP_VARS)) {
761                                 ast_cdr_free_vars(cdr, 0);
762                         }
763
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));
769                         cdr->billsec = 0;
770                         cdr->duration = 0;
771                         ast_cdr_start(cdr);
772                         cdr->disposition = AST_CDR_NOANSWER;
773                 }
774         }
775 }
776
777 struct ast_cdr *ast_cdr_append(struct ast_cdr *cdr, struct ast_cdr *newcdr) 
778 {
779         struct ast_cdr *ret;
780
781         if (cdr) {
782                 ret = cdr;
783
784                 while (cdr->next)
785                         cdr = cdr->next;
786                 cdr->next = newcdr;
787         } else {
788                 ret = newcdr;
789         }
790
791         return ret;
792 }
793
794 /*! \note Don't call without cdr_batch_lock */
795 static void reset_batch(void)
796 {
797         batch->size = 0;
798         batch->head = NULL;
799         batch->tail = NULL;
800 }
801
802 /*! \note Don't call without cdr_batch_lock */
803 static int init_batch(void)
804 {
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))))
807                 return -1;
808
809         reset_batch();
810
811         return 0;
812 }
813
814 static void *do_batch_backend_process(void *data)
815 {
816         struct ast_cdr_batch_item *processeditem;
817         struct ast_cdr_batch_item *batchitem = data;
818
819         /* Push each CDR into storage mechanism(s) and free all the memory */
820         while (batchitem) {
821                 post_cdr(batchitem->cdr);
822                 ast_cdr_free(batchitem->cdr);
823                 processeditem = batchitem;
824                 batchitem = batchitem->next;
825                 free(processeditem);
826         }
827
828         return NULL;
829 }
830
831 void ast_cdr_submit_batch(int shutdown)
832 {
833         struct ast_cdr_batch_item *oldbatchitems = NULL;
834         pthread_attr_t attr;
835         pthread_t batch_post_thread = AST_PTHREADT_NULL;
836
837         /* if there's no batch, or no CDRs in the batch, then there's nothing to do */
838         if (!batch || !batch->head)
839                 return;
840
841         /* move the old CDRs aside, and prepare a new CDR batch */
842         ast_mutex_lock(&cdr_batch_lock);
843         oldbatchitems = batch->head;
844         reset_batch();
845         ast_mutex_unlock(&cdr_batch_lock);
846
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) {
850                 if (option_debug)
851                         ast_log(LOG_DEBUG, "CDR single-threaded batch processing begins now\n");
852                 do_batch_backend_process(oldbatchitems);
853         } else {
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);
859                 } else {
860                         if (option_debug)
861                                 ast_log(LOG_DEBUG, "CDR multi-threaded batch processing begins now\n");
862                 }
863         }
864 }
865
866 static int submit_scheduled_batch(void *data)
867 {
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 */
872         return 0;
873 }
874
875 static void submit_unscheduled_batch(void)
876 {
877         /* this is okay since we are not being called from within the scheduler */
878         if (cdr_sched > -1)
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);
886 }
887
888 void ast_cdr_detach(struct ast_cdr *cdr)
889 {
890         struct ast_cdr_batch_item *newtail;
891         int curr;
892
893         /* maybe they disabled CDR stuff completely, so just drop it */
894         if (!enabled) {
895                 if (option_debug)
896                         ast_log(LOG_DEBUG, "Dropping CDR !\n");
897                 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
898                 ast_cdr_free(cdr);
899                 return;
900         }
901
902         /* post stuff immediately if we are not in batch mode, this is legacy behaviour */
903         if (!batchmode) {
904                 post_cdr(cdr);
905                 ast_cdr_free(cdr);
906                 return;
907         }
908
909         /* otherwise, each CDR gets put into a batch list (at the end) */
910         if (option_debug)
911                 ast_log(LOG_DEBUG, "CDR detaching from this thread\n");
912
913         /* we'll need a new tail for every CDR */
914         if (!(newtail = ast_calloc(1, sizeof(*newtail)))) {
915                 post_cdr(cdr);
916                 ast_cdr_free(cdr);
917                 return;
918         }
919
920         /* don't traverse a whole list (just keep track of the tail) */
921         ast_mutex_lock(&cdr_batch_lock);
922         if (!batch)
923                 init_batch();
924         if (!batch->head) {
925                 /* new batch is empty, so point the head at the new tail */
926                 batch->head = newtail;
927         } else {
928                 /* already got a batch with something in it, so just append a new tail */
929                 batch->tail->next = newtail;
930         }
931         newtail->cdr = cdr;
932         batch->tail = newtail;
933         curr = batch->size++;
934         ast_mutex_unlock(&cdr_batch_lock);
935
936         /* if we have enough stuff to post, then do it */
937         if (curr >= (batchsize - 1))
938                 submit_unscheduled_batch();
939 }
940
941 static void *do_cdr(void *data)
942 {
943         struct timespec timeout;
944         int schedms;
945         int numevents = 0;
946
947         for(;;) {
948                 struct timeval now;
949                 schedms = ast_sched_wait(sched);
950                 /* this shouldn't happen, but provide a 1 second default just in case */
951                 if (schedms <= 0)
952                         schedms = 1000;
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);
963         }
964
965         return NULL;
966 }
967
968 static int handle_cli_status(int fd, int argc, char *argv[])
969 {
970         struct ast_cdr_beitem *beitem=NULL;
971         int cnt=0;
972         long nextbatchtime=0;
973
974         if (argc > 2)
975                 return RESULT_SHOWUSAGE;
976
977         ast_cli(fd, "CDR logging: %s\n", enabled ? "enabled" : "disabled");
978         ast_cli(fd, "CDR mode: %s\n", batchmode ? "batch" : "simple");
979         if (enabled) {
980                 if (batchmode) {
981                         if (batch)
982                                 cnt = batch->size;
983                         if (cdr_sched > -1)
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" : "");
991                 }
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);
995                 }
996                 AST_LIST_UNLOCK(&be_list);
997         }
998
999         return 0;
1000 }
1001
1002 static int handle_cli_submit(int fd, int argc, char *argv[])
1003 {
1004         if (argc > 2)
1005                 return RESULT_SHOWUSAGE;
1006
1007         submit_unscheduled_batch();
1008         ast_cli(fd, "Submitted CDRs to backend engines for processing.  This may take a while.\n");
1009
1010         return 0;
1011 }
1012
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"
1018 };
1019
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"
1025 };
1026
1027 static int do_reload(void)
1028 {
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;
1037         int cfg_size;
1038         int cfg_time;
1039         int was_enabled;
1040         int was_batchmode;
1041         int res=0;
1042
1043         ast_mutex_lock(&cdr_batch_lock);
1044
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;
1051         enabled = 1;
1052         batchmode = 0;
1053
1054         /* don't run the next scheduled CDR posting while reloading */
1055         if (cdr_sched > -1)
1056                 ast_sched_del(sched, cdr_sched);
1057
1058         if ((config = ast_config_load("cdr.conf"))) {
1059                 if ((enabled_value = ast_variable_retrieve(config, "general", "enable"))) {
1060                         enabled = ast_true(enabled_value);
1061                 }
1062                 if ((batched_value = ast_variable_retrieve(config, "general", "batch"))) {
1063                         batchmode = ast_true(batched_value);
1064                 }
1065                 if ((scheduleronly_value = ast_variable_retrieve(config, "general", "scheduleronly"))) {
1066                         batchscheduleronly = ast_true(scheduleronly_value);
1067                 }
1068                 if ((batchsafeshutdown_value = ast_variable_retrieve(config, "general", "safeshutdown"))) {
1069                         batchsafeshutdown = ast_true(batchsafeshutdown_value);
1070                 }
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);
1076                         else
1077                                 batchsize = cfg_size;
1078                 }
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);
1084                         else
1085                                 batchtime = cfg_time;
1086                 }
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);
1089         }
1090
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);
1096         } else {
1097                 ast_log(LOG_NOTICE, "CDR logging disabled, data will be lost.\n");
1098         }
1099
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);
1107                 } else {
1108                         ast_cli_register(&cli_submit);
1109                         ast_register_atexit(ast_cdr_engine_term);
1110                         res = 0;
1111                 }
1112         /* if this reload disabled the CDR and/or batch mode and there is a background thread,
1113            kill it */
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);
1123                 res = 0;
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();
1128                 }
1129         } else {
1130                 res = 0;
1131         }
1132
1133         ast_mutex_unlock(&cdr_batch_lock);
1134         ast_config_destroy(config);
1135
1136         return res;
1137 }
1138
1139 int ast_cdr_engine_init(void)
1140 {
1141         int res;
1142
1143         sched = sched_context_create();
1144         if (!sched) {
1145                 ast_log(LOG_ERROR, "Unable to create schedule context.\n");
1146                 return -1;
1147         }
1148
1149         ast_cli_register(&cli_status);
1150
1151         res = do_reload();
1152         if (res) {
1153                 ast_mutex_lock(&cdr_batch_lock);
1154                 res = init_batch();
1155                 ast_mutex_unlock(&cdr_batch_lock);
1156         }
1157
1158         return res;
1159 }
1160
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)
1164 {
1165         ast_cdr_submit_batch(batchsafeshutdown);
1166 }
1167
1168 int ast_cdr_engine_reload(void)
1169 {
1170         return do_reload();
1171 }
1172