New CLI command "Core show settings" to list some core settings
[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;
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;             /*! Is the CDR subsystem 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 int check_cdr_enabled()
105 {
106         return enabled;
107 }
108
109 /*! Register a CDR driver. Each registered CDR driver generates a CDR 
110         \return 0 on success, -1 on failure 
111 */
112 int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be)
113 {
114         struct ast_cdr_beitem *i;
115
116         if (!name)
117                 return -1;
118         if (!be) {
119                 ast_log(LOG_WARNING, "CDR engine '%s' lacks backend\n", name);
120                 return -1;
121         }
122
123         AST_LIST_LOCK(&be_list);
124         AST_LIST_TRAVERSE(&be_list, i, list) {
125                 if (!strcasecmp(name, i->name))
126                         break;
127         }
128         AST_LIST_UNLOCK(&be_list);
129
130         if (i) {
131                 ast_log(LOG_WARNING, "Already have a CDR backend called '%s'\n", name);
132                 return -1;
133         }
134
135         if (!(i = ast_calloc(1, sizeof(*i))))   
136                 return -1;
137
138         i->be = be;
139         ast_copy_string(i->name, name, sizeof(i->name));
140         ast_copy_string(i->desc, desc, sizeof(i->desc));
141
142         AST_LIST_LOCK(&be_list);
143         AST_LIST_INSERT_HEAD(&be_list, i, list);
144         AST_LIST_UNLOCK(&be_list);
145
146         return 0;
147 }
148
149 /*! unregister a CDR driver */
150 void ast_cdr_unregister(const char *name)
151 {
152         struct ast_cdr_beitem *i = NULL;
153
154         AST_LIST_LOCK(&be_list);
155         AST_LIST_TRAVERSE_SAFE_BEGIN(&be_list, i, list) {
156                 if (!strcasecmp(name, i->name)) {
157                         AST_LIST_REMOVE_CURRENT(&be_list, list);
158                         if (option_verbose > 1)
159                                 ast_verbose(VERBOSE_PREFIX_2 "Unregistered '%s' CDR backend\n", name);
160                         free(i);
161                         break;
162                 }
163         }
164         AST_LIST_TRAVERSE_SAFE_END;
165         AST_LIST_UNLOCK(&be_list);
166 }
167
168 /*! Duplicate a CDR record 
169         \returns Pointer to new CDR record
170 */
171 struct ast_cdr *ast_cdr_dup(struct ast_cdr *cdr) 
172 {
173         struct ast_cdr *newcdr = ast_cdr_alloc();
174
175         if (!newcdr)
176                 return NULL;
177
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;
183
184         return newcdr;
185 }
186
187 static const char *ast_cdr_getvar_internal(struct ast_cdr *cdr, const char *name, int recur) 
188 {
189         if (ast_strlen_zero(name))
190                 return NULL;
191
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);
198                 }
199         }
200
201         return NULL;
202 }
203
204 static void cdr_get_tv(struct timeval tv, const char *fmt, char *buf, int bufsize)
205 {
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;
212                         localtime_r(&t, &tm);
213                         strftime(buf, bufsize, fmt, &tm);
214                 }
215         }
216 }
217
218 /*! CDR channel variable retrieval */
219 void ast_cdr_getvar(struct ast_cdr *cdr, const char *name, char **ret, char *workspace, int workspacelen, int recur, int raw) 
220 {
221         const char *fmt = "%Y-%m-%d %T";
222         const char *varbuf;
223
224         *ret = NULL;
225         /* special vars (the ones from the struct ast_cdr when requested by name) 
226            I'd almost say we should convert all the stringed vals to vars */
227
228         if (!strcasecmp(name, "clid"))
229                 ast_copy_string(workspace, cdr->clid, workspacelen);
230         else if (!strcasecmp(name, "src"))
231                 ast_copy_string(workspace, cdr->src, workspacelen);
232         else if (!strcasecmp(name, "dst"))
233                 ast_copy_string(workspace, cdr->dst, workspacelen);
234         else if (!strcasecmp(name, "dcontext"))
235                 ast_copy_string(workspace, cdr->dcontext, workspacelen);
236         else if (!strcasecmp(name, "channel"))
237                 ast_copy_string(workspace, cdr->channel, workspacelen);
238         else if (!strcasecmp(name, "dstchannel"))
239                 ast_copy_string(workspace, cdr->dstchannel, workspacelen);
240         else if (!strcasecmp(name, "lastapp"))
241                 ast_copy_string(workspace, cdr->lastapp, workspacelen);
242         else if (!strcasecmp(name, "lastdata"))
243                 ast_copy_string(workspace, cdr->lastdata, workspacelen);
244         else if (!strcasecmp(name, "start"))
245                 cdr_get_tv(cdr->start, raw ? NULL : fmt, workspace, workspacelen);
246         else if (!strcasecmp(name, "answer"))
247                 cdr_get_tv(cdr->answer, raw ? NULL : fmt, workspace, workspacelen);
248         else if (!strcasecmp(name, "end"))
249                 cdr_get_tv(cdr->end, raw ? NULL : fmt, workspace, workspacelen);
250         else if (!strcasecmp(name, "duration"))
251                 snprintf(workspace, workspacelen, "%ld", cdr->duration);
252         else if (!strcasecmp(name, "billsec"))
253                 snprintf(workspace, workspacelen, "%ld", cdr->billsec);
254         else if (!strcasecmp(name, "disposition")) {
255                 if (raw) {
256                         snprintf(workspace, workspacelen, "%ld", cdr->disposition);
257                 } else {
258                         ast_copy_string(workspace, ast_cdr_disp2str(cdr->disposition), workspacelen);
259                 }
260         } else if (!strcasecmp(name, "amaflags")) {
261                 if (raw) {
262                         snprintf(workspace, workspacelen, "%ld", cdr->amaflags);
263                 } else {
264                         ast_copy_string(workspace, ast_cdr_flags2str(cdr->amaflags), workspacelen);
265                 }
266         } else if (!strcasecmp(name, "accountcode"))
267                 ast_copy_string(workspace, cdr->accountcode, workspacelen);
268         else if (!strcasecmp(name, "uniqueid"))
269                 ast_copy_string(workspace, cdr->uniqueid, workspacelen);
270         else if (!strcasecmp(name, "userfield"))
271                 ast_copy_string(workspace, cdr->userfield, workspacelen);
272         else if ((varbuf = ast_cdr_getvar_internal(cdr, name, recur)))
273                 ast_copy_string(workspace, varbuf, workspacelen);
274         else
275                 workspace[0] = '\0';
276
277         if (!ast_strlen_zero(workspace))
278                 *ret = workspace;
279 }
280
281 /* readonly cdr variables */
282 static  const char *cdr_readonly_vars[] = { "clid", "src", "dst", "dcontext", "channel", "dstchannel",
283                                     "lastapp", "lastdata", "start", "answer", "end", "duration",
284                                     "billsec", "disposition", "amaflags", "accountcode", "uniqueid",
285                                     "userfield", NULL };
286 /*! Set a CDR channel variable 
287         \note You can't set the CDR variables that belong to the actual CDR record, like "billsec".
288 */
289 int ast_cdr_setvar(struct ast_cdr *cdr, const char *name, const char *value, int recur) 
290 {
291         struct ast_var_t *newvariable;
292         struct varshead *headp;
293         int x;
294         
295         for (x = 0; cdr_readonly_vars[x]; x++) {
296                 if (!strcasecmp(name, cdr_readonly_vars[x])) {
297                         ast_log(LOG_ERROR, "Attempt to set the '%s' read-only variable!.\n", name);
298                         return -1;
299                 }
300         }
301
302         if (!cdr) {
303                 ast_log(LOG_ERROR, "Attempt to set a variable on a nonexistent CDR record.\n");
304                 return -1;
305         }
306
307         for (; cdr; cdr = recur ? cdr->next : NULL) {
308                 headp = &cdr->varshead;
309                 AST_LIST_TRAVERSE_SAFE_BEGIN(headp, newvariable, entries) {
310                         if (!strcasecmp(ast_var_name(newvariable), name)) {
311                                 /* there is already such a variable, delete it */
312                                 AST_LIST_REMOVE_CURRENT(headp, entries);
313                                 ast_var_delete(newvariable);
314                                 break;
315                         }
316                 }
317                 AST_LIST_TRAVERSE_SAFE_END;
318
319                 if (value) {
320                         newvariable = ast_var_assign(name, value);
321                         AST_LIST_INSERT_HEAD(headp, newvariable, entries);
322                 }
323         }
324
325         return 0;
326 }
327
328 int ast_cdr_copy_vars(struct ast_cdr *to_cdr, struct ast_cdr *from_cdr)
329 {
330         struct ast_var_t *variables, *newvariable = NULL;
331         struct varshead *headpa, *headpb;
332         const char *var, *val;
333         int x = 0;
334
335         headpa = &from_cdr->varshead;
336         headpb = &to_cdr->varshead;
337
338         AST_LIST_TRAVERSE(headpa,variables,entries) {
339                 if (variables &&
340                     (var = ast_var_name(variables)) && (val = ast_var_value(variables)) &&
341                     !ast_strlen_zero(var) && !ast_strlen_zero(val)) {
342                         newvariable = ast_var_assign(var, val);
343                         AST_LIST_INSERT_HEAD(headpb, newvariable, entries);
344                         x++;
345                 }
346         }
347
348         return x;
349 }
350
351 int ast_cdr_serialize_variables(struct ast_cdr *cdr, struct ast_str **buf, char delim, char sep, int recur) 
352 {
353         struct ast_var_t *variables;
354         const char *var, *val;
355         char *tmp;
356         char workspace[256];
357         int total = 0, x = 0, i;
358
359         (*buf)->used = 0;
360         (*buf)->str[0] = '\0';
361
362         for (; cdr; cdr = recur ? cdr->next : NULL) {
363                 if (++x > 1)
364                         ast_str_append(buf, 0, "\n");
365
366                 AST_LIST_TRAVERSE(&cdr->varshead, variables, entries) {
367                         if (variables &&
368                             (var = ast_var_name(variables)) && (val = ast_var_value(variables)) &&
369                             !ast_strlen_zero(var) && !ast_strlen_zero(val)) {
370                                 if (ast_str_append(buf, 0, "level %d: %s%c%s%c", x, var, delim, val, sep) < 0) {
371                                         ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
372                                         break;
373                                 } else
374                                         total++;
375                         } else 
376                                 break;
377                 }
378
379                 for (i = 0; cdr_readonly_vars[i]; i++) {
380                         ast_cdr_getvar(cdr, cdr_readonly_vars[i], &tmp, workspace, sizeof(workspace), 0, 0);
381                         if (!tmp)
382                                 continue;
383                         
384                         if (ast_str_append(buf, 0, "level %d: %s%c%s%c", x, cdr_readonly_vars[i], delim, tmp, sep) < 0) {
385                                 ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
386                                 break;
387                         } else
388                                 total++;
389                 }
390         }
391
392         return total;
393 }
394
395
396 void ast_cdr_free_vars(struct ast_cdr *cdr, int recur)
397 {
398
399         /* clear variables */
400         for (; cdr; cdr = recur ? cdr->next : NULL) {
401                 struct ast_var_t *vardata;
402                 struct varshead *headp = &cdr->varshead;
403                 while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
404                         ast_var_delete(vardata);
405         }
406 }
407
408 /*! \brief  print a warning if cdr already posted */
409 static void check_post(struct ast_cdr *cdr)
410 {
411         if (ast_test_flag(cdr, AST_CDR_FLAG_POSTED))
412                 ast_log(LOG_NOTICE, "CDR on channel '%s' already posted\n", S_OR(cdr->channel, "<unknown>"));
413 }
414
415 /*! \brief  print a warning if cdr already started */
416 static void check_start(struct ast_cdr *cdr)
417 {
418         if (!ast_tvzero(cdr->start))
419                 ast_log(LOG_NOTICE, "CDR on channel '%s' already started\n", S_OR(cdr->channel, "<unknown>"));
420 }
421
422 void ast_cdr_free(struct ast_cdr *cdr)
423 {
424
425         while (cdr) {
426                 struct ast_cdr *next = cdr->next;
427                 char *chan = S_OR(cdr->channel, "<unknown>");
428                 if (!ast_test_flag(cdr, AST_CDR_FLAG_POSTED) && !ast_test_flag(cdr, AST_CDR_FLAG_POST_DISABLED))
429                         ast_log(LOG_NOTICE, "CDR on channel '%s' not posted\n", chan);
430                 if (ast_tvzero(cdr->end))
431                         ast_log(LOG_NOTICE, "CDR on channel '%s' lacks end\n", chan);
432                 if (ast_tvzero(cdr->start))
433                         ast_log(LOG_NOTICE, "CDR on channel '%s' lacks start\n", chan);
434
435                 ast_cdr_free_vars(cdr, 0);
436                 free(cdr);
437                 cdr = next;
438         }
439 }
440
441 struct ast_cdr *ast_cdr_alloc(void)
442 {
443         return ast_calloc(1, sizeof(struct ast_cdr));
444 }
445
446 void ast_cdr_start(struct ast_cdr *cdr)
447 {
448         char *chan; 
449
450         for (; cdr; cdr = cdr->next) {
451                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
452                         chan = S_OR(cdr->channel, "<unknown>");
453                         check_post(cdr);
454                         check_start(cdr);
455                         cdr->start = ast_tvnow();
456                 }
457         }
458 }
459
460 void ast_cdr_answer(struct ast_cdr *cdr)
461 {
462
463         for (; cdr; cdr = cdr->next) {
464                 check_post(cdr);
465                 if (cdr->disposition < AST_CDR_ANSWERED)
466                         cdr->disposition = AST_CDR_ANSWERED;
467                 if (ast_tvzero(cdr->answer))
468                         cdr->answer = ast_tvnow();
469         }
470 }
471
472 void ast_cdr_busy(struct ast_cdr *cdr)
473 {
474
475         for (; cdr; cdr = cdr->next) {
476                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
477                         check_post(cdr);
478                         if (cdr->disposition < AST_CDR_BUSY)
479                                 cdr->disposition = AST_CDR_BUSY;
480                 }
481         }
482 }
483
484 void ast_cdr_failed(struct ast_cdr *cdr)
485 {
486         for (; cdr; cdr = cdr->next) {
487                 check_post(cdr);
488                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
489                         if (cdr->disposition < AST_CDR_FAILED)
490                                 cdr->disposition = AST_CDR_FAILED;
491                 }
492         }
493 }
494
495 int ast_cdr_disposition(struct ast_cdr *cdr, int cause)
496 {
497         int res = 0;
498
499         for (; cdr; cdr = cdr->next) {
500                 switch (cause) {
501                 case AST_CAUSE_BUSY:
502                         ast_cdr_busy(cdr);
503                         break;
504                 case AST_CAUSE_FAILURE:
505                         ast_cdr_failed(cdr);
506                         break;
507                 case AST_CAUSE_NORMAL:
508                         break;
509                 case AST_CAUSE_NOTDEFINED:
510                         res = -1;
511                         break;
512                 default:
513                         res = -1;
514                         ast_log(LOG_WARNING, "Cause not handled\n");
515                 }
516         }
517         return res;
518 }
519
520 void ast_cdr_setdestchan(struct ast_cdr *cdr, const char *chann)
521 {
522         for (; cdr; cdr = cdr->next) {
523                 check_post(cdr);
524                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
525                         ast_copy_string(cdr->dstchannel, chann, sizeof(cdr->dstchannel));
526         }
527 }
528
529 void ast_cdr_setapp(struct ast_cdr *cdr, char *app, char *data)
530 {
531
532         for (; cdr; cdr = cdr->next) {
533                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
534                         check_post(cdr);
535                         if (!app)
536                                 app = "";
537                         ast_copy_string(cdr->lastapp, app, sizeof(cdr->lastapp));
538                         if (!data)
539                                 data = "";
540                         ast_copy_string(cdr->lastdata, data, sizeof(cdr->lastdata));
541                 }
542         }
543 }
544
545 /* set cid info for one record */
546 static void set_one_cid(struct ast_cdr *cdr, struct ast_channel *c)
547 {
548         /* Grab source from ANI or normal Caller*ID */
549         const char *num = S_OR(c->cid.cid_ani, c->cid.cid_num);
550         
551         if (!ast_strlen_zero(c->cid.cid_name)) {
552                 if (!ast_strlen_zero(num))      /* both name and number */
553                         snprintf(cdr->clid, sizeof(cdr->clid), "\"%s\" <%s>", c->cid.cid_name, num);
554                 else                            /* only name */
555                         ast_copy_string(cdr->clid, c->cid.cid_name, sizeof(cdr->clid));
556         } else if (!ast_strlen_zero(num)) {     /* only number */
557                 ast_copy_string(cdr->clid, num, sizeof(cdr->clid));
558         } else {                                /* nothing known */
559                 cdr->clid[0] = '\0';
560         }
561         ast_copy_string(cdr->src, S_OR(num, ""), sizeof(cdr->src));
562
563 }
564 int ast_cdr_setcid(struct ast_cdr *cdr, struct ast_channel *c)
565 {
566         for (; cdr; cdr = cdr->next) {
567                 if (ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
568                         set_one_cid(cdr, c);
569         }
570         return 0;
571 }
572
573 int ast_cdr_init(struct ast_cdr *cdr, struct ast_channel *c)
574 {
575         char *chan;
576
577         for ( ; cdr ; cdr = cdr->next) {
578                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
579                         chan = S_OR(cdr->channel, "<unknown>");
580                         if (!ast_strlen_zero(cdr->channel)) 
581                                 ast_log(LOG_WARNING, "CDR already initialized on '%s'\n", chan); 
582                         ast_copy_string(cdr->channel, c->name, sizeof(cdr->channel));
583                         set_one_cid(cdr, c);
584
585                         cdr->disposition = (c->_state == AST_STATE_UP) ?  AST_CDR_ANSWERED : AST_CDR_NOANSWER;
586                         cdr->amaflags = c->amaflags ? c->amaflags :  ast_default_amaflags;
587                         ast_copy_string(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode));
588                         /* Destination information */
589                         ast_copy_string(cdr->dst, c->exten, sizeof(cdr->dst));
590                         ast_copy_string(cdr->dcontext, c->context, sizeof(cdr->dcontext));
591                         /* Unique call identifier */
592                         ast_copy_string(cdr->uniqueid, c->uniqueid, sizeof(cdr->uniqueid));
593                 }
594         }
595         return 0;
596 }
597
598 void ast_cdr_end(struct ast_cdr *cdr)
599 {
600         for ( ; cdr ; cdr = cdr->next) {
601                 check_post(cdr);
602                 if (ast_tvzero(cdr->end))
603                         cdr->end = ast_tvnow();
604                 if (ast_tvzero(cdr->start)) {
605                         ast_log(LOG_WARNING, "CDR on channel '%s' has not started\n", S_OR(cdr->channel, "<unknown>"));
606                         cdr->disposition = AST_CDR_FAILED;
607                 } else
608                         cdr->duration = cdr->end.tv_sec - cdr->start.tv_sec;
609                 cdr->billsec = ast_tvzero(cdr->answer) ? 0 : cdr->end.tv_sec - cdr->answer.tv_sec;
610         }
611 }
612
613 char *ast_cdr_disp2str(int disposition)
614 {
615         switch (disposition) {
616         case AST_CDR_NOANSWER:
617                 return "NO ANSWER";
618         case AST_CDR_FAILED:
619                 return "FAILED";                
620         case AST_CDR_BUSY:
621                 return "BUSY";          
622         case AST_CDR_ANSWERED:
623                 return "ANSWERED";
624         }
625         return "UNKNOWN";
626 }
627
628 /*! Converts AMA flag to printable string */
629 char *ast_cdr_flags2str(int flag)
630 {
631         switch (flag) {
632         case AST_CDR_OMIT:
633                 return "OMIT";
634         case AST_CDR_BILLING:
635                 return "BILLING";
636         case AST_CDR_DOCUMENTATION:
637                 return "DOCUMENTATION";
638         }
639         return "Unknown";
640 }
641
642 int ast_cdr_setaccount(struct ast_channel *chan, const char *account)
643 {
644         struct ast_cdr *cdr = chan->cdr;
645
646         ast_string_field_set(chan, accountcode, account);
647         for ( ; cdr ; cdr = cdr->next) {
648                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
649                         ast_copy_string(cdr->accountcode, chan->accountcode, sizeof(cdr->accountcode));
650         }
651         return 0;
652 }
653
654 int ast_cdr_setamaflags(struct ast_channel *chan, const char *flag)
655 {
656         struct ast_cdr *cdr;
657         int newflag = ast_cdr_amaflags2int(flag);
658         if (newflag) {
659                 for (cdr = chan->cdr; cdr; cdr = cdr->next)
660                         cdr->amaflags = newflag;
661         }
662
663         return 0;
664 }
665
666 int ast_cdr_setuserfield(struct ast_channel *chan, const char *userfield)
667 {
668         struct ast_cdr *cdr = chan->cdr;
669
670         for ( ; cdr ; cdr = cdr->next) {
671                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) 
672                         ast_copy_string(cdr->userfield, userfield, sizeof(cdr->userfield));
673         }
674
675         return 0;
676 }
677
678 int ast_cdr_appenduserfield(struct ast_channel *chan, const char *userfield)
679 {
680         struct ast_cdr *cdr = chan->cdr;
681
682         for ( ; cdr ; cdr = cdr->next) {
683                 int len = strlen(cdr->userfield);
684
685                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
686                         ast_copy_string(cdr->userfield + len, userfield, sizeof(cdr->userfield) - len);
687         }
688
689         return 0;
690 }
691
692 int ast_cdr_update(struct ast_channel *c)
693 {
694         struct ast_cdr *cdr = c->cdr;
695
696         for ( ; cdr ; cdr = cdr->next) {
697                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
698                         set_one_cid(cdr, c);
699
700                         /* Copy account code et-al */   
701                         ast_copy_string(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode));
702                         /* Destination information */ /* XXX privilege macro* ? */
703                         ast_copy_string(cdr->dst, S_OR(c->macroexten, c->exten), sizeof(cdr->dst));
704                         ast_copy_string(cdr->dcontext, S_OR(c->macrocontext, c->context), sizeof(cdr->dcontext));
705                 }
706         }
707
708         return 0;
709 }
710
711 int ast_cdr_amaflags2int(const char *flag)
712 {
713         if (!strcasecmp(flag, "default"))
714                 return 0;
715         if (!strcasecmp(flag, "omit"))
716                 return AST_CDR_OMIT;
717         if (!strcasecmp(flag, "billing"))
718                 return AST_CDR_BILLING;
719         if (!strcasecmp(flag, "documentation"))
720                 return AST_CDR_DOCUMENTATION;
721         return -1;
722 }
723
724 static void post_cdr(struct ast_cdr *cdr)
725 {
726         char *chan;
727         struct ast_cdr_beitem *i;
728
729         for ( ; cdr ; cdr = cdr->next) {
730                 chan = S_OR(cdr->channel, "<unknown>");
731                 check_post(cdr);
732                 if (ast_tvzero(cdr->end))
733                         ast_log(LOG_WARNING, "CDR on channel '%s' lacks end\n", chan);
734                 if (ast_tvzero(cdr->start))
735                         ast_log(LOG_WARNING, "CDR on channel '%s' lacks start\n", chan);
736                 ast_set_flag(cdr, AST_CDR_FLAG_POSTED);
737                 AST_LIST_LOCK(&be_list);
738                 AST_LIST_TRAVERSE(&be_list, i, list) {
739                         i->be(cdr);
740                 }
741                 AST_LIST_UNLOCK(&be_list);
742         }
743 }
744
745 void ast_cdr_reset(struct ast_cdr *cdr, struct ast_flags *_flags)
746 {
747         struct ast_cdr *dup;
748         struct ast_flags flags = { 0 };
749
750         if (_flags)
751                 ast_copy_flags(&flags, _flags, AST_FLAGS_ALL);
752
753         for ( ; cdr ; cdr = cdr->next) {
754                 /* Detach if post is requested */
755                 if (ast_test_flag(&flags, AST_CDR_FLAG_LOCKED) || !ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
756                         if (ast_test_flag(&flags, AST_CDR_FLAG_POSTED)) {
757                                 ast_cdr_end(cdr);
758                                 if ((dup = ast_cdr_dup(cdr))) {
759                                         ast_cdr_detach(dup);
760                                 }
761                                 ast_set_flag(cdr, AST_CDR_FLAG_POSTED);
762                         }
763
764                         /* clear variables */
765                         if (!ast_test_flag(&flags, AST_CDR_FLAG_KEEP_VARS)) {
766                                 ast_cdr_free_vars(cdr, 0);
767                         }
768
769                         /* Reset to initial state */
770                         ast_clear_flag(cdr, AST_FLAGS_ALL);     
771                         memset(&cdr->start, 0, sizeof(cdr->start));
772                         memset(&cdr->end, 0, sizeof(cdr->end));
773                         memset(&cdr->answer, 0, sizeof(cdr->answer));
774                         cdr->billsec = 0;
775                         cdr->duration = 0;
776                         ast_cdr_start(cdr);
777                         cdr->disposition = AST_CDR_NOANSWER;
778                 }
779         }
780 }
781
782 struct ast_cdr *ast_cdr_append(struct ast_cdr *cdr, struct ast_cdr *newcdr) 
783 {
784         struct ast_cdr *ret;
785
786         if (cdr) {
787                 ret = cdr;
788
789                 while (cdr->next)
790                         cdr = cdr->next;
791                 cdr->next = newcdr;
792         } else {
793                 ret = newcdr;
794         }
795
796         return ret;
797 }
798
799 /*! \note Don't call without cdr_batch_lock */
800 static void reset_batch(void)
801 {
802         batch->size = 0;
803         batch->head = NULL;
804         batch->tail = NULL;
805 }
806
807 /*! \note Don't call without cdr_batch_lock */
808 static int init_batch(void)
809 {
810         /* This is the single meta-batch used to keep track of all CDRs during the entire life of the program */
811         if (!(batch = ast_malloc(sizeof(*batch))))
812                 return -1;
813
814         reset_batch();
815
816         return 0;
817 }
818
819 static void *do_batch_backend_process(void *data)
820 {
821         struct ast_cdr_batch_item *processeditem;
822         struct ast_cdr_batch_item *batchitem = data;
823
824         /* Push each CDR into storage mechanism(s) and free all the memory */
825         while (batchitem) {
826                 post_cdr(batchitem->cdr);
827                 ast_cdr_free(batchitem->cdr);
828                 processeditem = batchitem;
829                 batchitem = batchitem->next;
830                 free(processeditem);
831         }
832
833         return NULL;
834 }
835
836 void ast_cdr_submit_batch(int shutdown)
837 {
838         struct ast_cdr_batch_item *oldbatchitems = NULL;
839         pthread_attr_t attr;
840         pthread_t batch_post_thread = AST_PTHREADT_NULL;
841
842         /* if there's no batch, or no CDRs in the batch, then there's nothing to do */
843         if (!batch || !batch->head)
844                 return;
845
846         /* move the old CDRs aside, and prepare a new CDR batch */
847         ast_mutex_lock(&cdr_batch_lock);
848         oldbatchitems = batch->head;
849         reset_batch();
850         ast_mutex_unlock(&cdr_batch_lock);
851
852         /* if configured, spawn a new thread to post these CDRs,
853            also try to save as much as possible if we are shutting down safely */
854         if (batchscheduleronly || shutdown) {
855                 if (option_debug)
856                         ast_log(LOG_DEBUG, "CDR single-threaded batch processing begins now\n");
857                 do_batch_backend_process(oldbatchitems);
858         } else {
859                 pthread_attr_init(&attr);
860                 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
861                 if (ast_pthread_create_background(&batch_post_thread, &attr, do_batch_backend_process, oldbatchitems)) {
862                         ast_log(LOG_WARNING, "CDR processing thread could not detach, now trying in this thread\n");
863                         do_batch_backend_process(oldbatchitems);
864                 } else {
865                         if (option_debug)
866                                 ast_log(LOG_DEBUG, "CDR multi-threaded batch processing begins now\n");
867                 }
868                 pthread_attr_destroy(&attr);
869         }
870 }
871
872 static int submit_scheduled_batch(void *data)
873 {
874         ast_cdr_submit_batch(0);
875         /* manually reschedule from this point in time */
876         cdr_sched = ast_sched_add(sched, batchtime * 1000, submit_scheduled_batch, NULL);
877         /* returning zero so the scheduler does not automatically reschedule */
878         return 0;
879 }
880
881 static void submit_unscheduled_batch(void)
882 {
883         /* this is okay since we are not being called from within the scheduler */
884         if (cdr_sched > -1)
885                 ast_sched_del(sched, cdr_sched);
886         /* schedule the submission to occur ASAP (1 ms) */
887         cdr_sched = ast_sched_add(sched, 1, submit_scheduled_batch, NULL);
888         /* signal the do_cdr thread to wakeup early and do some work (that lazy thread ;) */
889         ast_mutex_lock(&cdr_pending_lock);
890         ast_cond_signal(&cdr_pending_cond);
891         ast_mutex_unlock(&cdr_pending_lock);
892 }
893
894 void ast_cdr_detach(struct ast_cdr *cdr)
895 {
896         struct ast_cdr_batch_item *newtail;
897         int curr;
898
899         /* maybe they disabled CDR stuff completely, so just drop it */
900         if (!enabled) {
901                 if (option_debug)
902                         ast_log(LOG_DEBUG, "Dropping CDR !\n");
903                 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
904                 ast_cdr_free(cdr);
905                 return;
906         }
907
908         /* post stuff immediately if we are not in batch mode, this is legacy behaviour */
909         if (!batchmode) {
910                 post_cdr(cdr);
911                 ast_cdr_free(cdr);
912                 return;
913         }
914
915         /* otherwise, each CDR gets put into a batch list (at the end) */
916         if (option_debug)
917                 ast_log(LOG_DEBUG, "CDR detaching from this thread\n");
918
919         /* we'll need a new tail for every CDR */
920         if (!(newtail = ast_calloc(1, sizeof(*newtail)))) {
921                 post_cdr(cdr);
922                 ast_cdr_free(cdr);
923                 return;
924         }
925
926         /* don't traverse a whole list (just keep track of the tail) */
927         ast_mutex_lock(&cdr_batch_lock);
928         if (!batch)
929                 init_batch();
930         if (!batch->head) {
931                 /* new batch is empty, so point the head at the new tail */
932                 batch->head = newtail;
933         } else {
934                 /* already got a batch with something in it, so just append a new tail */
935                 batch->tail->next = newtail;
936         }
937         newtail->cdr = cdr;
938         batch->tail = newtail;
939         curr = batch->size++;
940         ast_mutex_unlock(&cdr_batch_lock);
941
942         /* if we have enough stuff to post, then do it */
943         if (curr >= (batchsize - 1))
944                 submit_unscheduled_batch();
945 }
946
947 static void *do_cdr(void *data)
948 {
949         struct timespec timeout;
950         int schedms;
951         int numevents = 0;
952
953         for (;;) {
954                 struct timeval now;
955                 schedms = ast_sched_wait(sched);
956                 /* this shouldn't happen, but provide a 1 second default just in case */
957                 if (schedms <= 0)
958                         schedms = 1000;
959                 now = ast_tvadd(ast_tvnow(), ast_samp2tv(schedms, 1000));
960                 timeout.tv_sec = now.tv_sec;
961                 timeout.tv_nsec = now.tv_usec * 1000;
962                 /* prevent stuff from clobbering cdr_pending_cond, then wait on signals sent to it until the timeout expires */
963                 ast_mutex_lock(&cdr_pending_lock);
964                 ast_cond_timedwait(&cdr_pending_cond, &cdr_pending_lock, &timeout);
965                 numevents = ast_sched_runq(sched);
966                 ast_mutex_unlock(&cdr_pending_lock);
967                 if (option_debug > 1)
968                         ast_log(LOG_DEBUG, "Processed %d scheduled CDR batches from the run queue\n", numevents);
969         }
970
971         return NULL;
972 }
973
974 static int handle_cli_status(int fd, int argc, char *argv[])
975 {
976         struct ast_cdr_beitem *beitem=NULL;
977         int cnt=0;
978         long nextbatchtime=0;
979
980         if (argc > 2)
981                 return RESULT_SHOWUSAGE;
982
983         ast_cli(fd, "CDR logging: %s\n", enabled ? "enabled" : "disabled");
984         ast_cli(fd, "CDR mode: %s\n", batchmode ? "batch" : "simple");
985         if (enabled) {
986                 if (batchmode) {
987                         if (batch)
988                                 cnt = batch->size;
989                         if (cdr_sched > -1)
990                                 nextbatchtime = ast_sched_when(sched, cdr_sched);
991                         ast_cli(fd, "CDR safe shut down: %s\n", batchsafeshutdown ? "enabled" : "disabled");
992                         ast_cli(fd, "CDR batch threading model: %s\n", batchscheduleronly ? "scheduler only" : "scheduler plus separate threads");
993                         ast_cli(fd, "CDR current batch size: %d record%s\n", cnt, ESS(cnt));
994                         ast_cli(fd, "CDR maximum batch size: %d record%s\n", batchsize, ESS(batchsize));
995                         ast_cli(fd, "CDR maximum batch time: %d second%s\n", batchtime, ESS(batchtime));
996                         ast_cli(fd, "CDR next scheduled batch processing time: %ld second%s\n", nextbatchtime, ESS(nextbatchtime));
997                 }
998                 AST_LIST_LOCK(&be_list);
999                 AST_LIST_TRAVERSE(&be_list, beitem, list) {
1000                         ast_cli(fd, "CDR registered backend: %s\n", beitem->name);
1001                 }
1002                 AST_LIST_UNLOCK(&be_list);
1003         }
1004
1005         return 0;
1006 }
1007
1008 static int handle_cli_submit(int fd, int argc, char *argv[])
1009 {
1010         if (argc > 2)
1011                 return RESULT_SHOWUSAGE;
1012
1013         submit_unscheduled_batch();
1014         ast_cli(fd, "Submitted CDRs to backend engines for processing.  This may take a while.\n");
1015
1016         return 0;
1017 }
1018
1019 static struct ast_cli_entry cli_submit = {
1020         { "cdr", "submit", NULL },
1021         handle_cli_submit, "Posts all pending batched CDR data",
1022         "Usage: cdr submit\n"
1023         "       Posts all pending batched CDR data to the configured CDR backend engine modules.\n"
1024 };
1025
1026 static struct ast_cli_entry cli_status = {
1027         { "cdr", "status", NULL },
1028         handle_cli_status, "Display the CDR status",
1029         "Usage: cdr status\n"
1030         "       Displays the Call Detail Record engine system status.\n"
1031 };
1032
1033 static int do_reload(void)
1034 {
1035         struct ast_config *config;
1036         const char *enabled_value;
1037         const char *batched_value;
1038         const char *scheduleronly_value;
1039         const char *batchsafeshutdown_value;
1040         const char *size_value;
1041         const char *time_value;
1042         const char *end_before_h_value;
1043         int cfg_size;
1044         int cfg_time;
1045         int was_enabled;
1046         int was_batchmode;
1047         int res=0;
1048
1049         ast_mutex_lock(&cdr_batch_lock);
1050
1051         batchsize = BATCH_SIZE_DEFAULT;
1052         batchtime = BATCH_TIME_DEFAULT;
1053         batchscheduleronly = BATCH_SCHEDULER_ONLY_DEFAULT;
1054         batchsafeshutdown = BATCH_SAFE_SHUTDOWN_DEFAULT;
1055         was_enabled = enabled;
1056         was_batchmode = batchmode;
1057         enabled = 1;
1058         batchmode = 0;
1059
1060         /* don't run the next scheduled CDR posting while reloading */
1061         if (cdr_sched > -1)
1062                 ast_sched_del(sched, cdr_sched);
1063
1064         if ((config = ast_config_load("cdr.conf"))) {
1065                 if ((enabled_value = ast_variable_retrieve(config, "general", "enable"))) {
1066                         enabled = ast_true(enabled_value);
1067                 }
1068                 if ((batched_value = ast_variable_retrieve(config, "general", "batch"))) {
1069                         batchmode = ast_true(batched_value);
1070                 }
1071                 if ((scheduleronly_value = ast_variable_retrieve(config, "general", "scheduleronly"))) {
1072                         batchscheduleronly = ast_true(scheduleronly_value);
1073                 }
1074                 if ((batchsafeshutdown_value = ast_variable_retrieve(config, "general", "safeshutdown"))) {
1075                         batchsafeshutdown = ast_true(batchsafeshutdown_value);
1076                 }
1077                 if ((size_value = ast_variable_retrieve(config, "general", "size"))) {
1078                         if (sscanf(size_value, "%d", &cfg_size) < 1)
1079                                 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", size_value);
1080                         else if (size_value < 0)
1081                                 ast_log(LOG_WARNING, "Invalid maximum batch size '%d' specified, using default\n", cfg_size);
1082                         else
1083                                 batchsize = cfg_size;
1084                 }
1085                 if ((time_value = ast_variable_retrieve(config, "general", "time"))) {
1086                         if (sscanf(time_value, "%d", &cfg_time) < 1)
1087                                 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", time_value);
1088                         else if (time_value < 0)
1089                                 ast_log(LOG_WARNING, "Invalid maximum batch time '%d' specified, using default\n", cfg_time);
1090                         else
1091                                 batchtime = cfg_time;
1092                 }
1093                 if ((end_before_h_value = ast_variable_retrieve(config, "general", "endbeforehexten")))
1094                         ast_set2_flag(&ast_options, ast_true(end_before_h_value), AST_OPT_FLAG_END_CDR_BEFORE_H_EXTEN);
1095         }
1096
1097         if (enabled && !batchmode) {
1098                 ast_log(LOG_NOTICE, "CDR simple logging enabled.\n");
1099         } else if (enabled && batchmode) {
1100                 cdr_sched = ast_sched_add(sched, batchtime * 1000, submit_scheduled_batch, NULL);
1101                 ast_log(LOG_NOTICE, "CDR batch mode logging enabled, first of either size %d or time %d seconds.\n", batchsize, batchtime);
1102         } else {
1103                 ast_log(LOG_NOTICE, "CDR logging disabled, data will be lost.\n");
1104         }
1105
1106         /* if this reload enabled the CDR batch mode, create the background thread
1107            if it does not exist */
1108         if (enabled && batchmode && (!was_enabled || !was_batchmode) && (cdr_thread == AST_PTHREADT_NULL)) {
1109                 ast_cond_init(&cdr_pending_cond, NULL);
1110                 if (ast_pthread_create_background(&cdr_thread, NULL, do_cdr, NULL) < 0) {
1111                         ast_log(LOG_ERROR, "Unable to start CDR thread.\n");
1112                         ast_sched_del(sched, cdr_sched);
1113                 } else {
1114                         ast_cli_register(&cli_submit);
1115                         ast_register_atexit(ast_cdr_engine_term);
1116                         res = 0;
1117                 }
1118         /* if this reload disabled the CDR and/or batch mode and there is a background thread,
1119            kill it */
1120         } else if (((!enabled && was_enabled) || (!batchmode && was_batchmode)) && (cdr_thread != AST_PTHREADT_NULL)) {
1121                 /* wake up the thread so it will exit */
1122                 pthread_cancel(cdr_thread);
1123                 pthread_kill(cdr_thread, SIGURG);
1124                 pthread_join(cdr_thread, NULL);
1125                 cdr_thread = AST_PTHREADT_NULL;
1126                 ast_cond_destroy(&cdr_pending_cond);
1127                 ast_cli_unregister(&cli_submit);
1128                 ast_unregister_atexit(ast_cdr_engine_term);
1129                 res = 0;
1130                 /* if leaving batch mode, then post the CDRs in the batch,
1131                    and don't reschedule, since we are stopping CDR logging */
1132                 if (!batchmode && was_batchmode) {
1133                         ast_cdr_engine_term();
1134                 }
1135         } else {
1136                 res = 0;
1137         }
1138
1139         ast_mutex_unlock(&cdr_batch_lock);
1140         ast_config_destroy(config);
1141
1142         return res;
1143 }
1144
1145 int ast_cdr_engine_init(void)
1146 {
1147         int res;
1148
1149         sched = sched_context_create();
1150         if (!sched) {
1151                 ast_log(LOG_ERROR, "Unable to create schedule context.\n");
1152                 return -1;
1153         }
1154
1155         ast_cli_register(&cli_status);
1156
1157         res = do_reload();
1158         if (res) {
1159                 ast_mutex_lock(&cdr_batch_lock);
1160                 res = init_batch();
1161                 ast_mutex_unlock(&cdr_batch_lock);
1162         }
1163
1164         return res;
1165 }
1166
1167 /* \note This actually gets called a couple of times at shutdown.  Once, before we start
1168    hanging up channels, and then again, after the channel hangup timeout expires */
1169 void ast_cdr_engine_term(void)
1170 {
1171         ast_cdr_submit_batch(batchsafeshutdown);
1172 }
1173
1174 int ast_cdr_engine_reload(void)
1175 {
1176         return do_reload();
1177 }
1178