Add some additional documentation for core AMI events
[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 /*** MODULEINFO
35         <support_level>core</support_level>
36  ***/
37
38 #include "asterisk.h"
39
40 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
41
42 #include <signal.h>
43
44 #include "asterisk/lock.h"
45 #include "asterisk/channel.h"
46 #include "asterisk/cdr.h"
47 #include "asterisk/callerid.h"
48 #include "asterisk/manager.h"
49 #include "asterisk/causes.h"
50 #include "asterisk/linkedlists.h"
51 #include "asterisk/utils.h"
52 #include "asterisk/sched.h"
53 #include "asterisk/config.h"
54 #include "asterisk/cli.h"
55 #include "asterisk/stringfields.h"
56 #include "asterisk/data.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_RWLIST_ENTRY(ast_cdr_beitem) list;
67 };
68
69 static AST_RWLIST_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
83 static int cdr_sequence =  0;
84
85 static int cdr_seq_inc(struct ast_cdr *cdr);
86
87 static struct ast_sched_context *sched;
88 static int cdr_sched = -1;
89 static pthread_t cdr_thread = AST_PTHREADT_NULL;
90
91 static int enabled;
92 static const int ENABLED_DEFAULT = 1;
93
94 static int batchmode;
95 static const int BATCHMODE_DEFAULT = 0;
96
97 static int unanswered;
98 static const int UNANSWERED_DEFAULT = 0;
99
100 static int congestion;
101 static const int CONGESTION_DEFAULT = 0;
102
103 static int batchsize;
104 static const int BATCH_SIZE_DEFAULT = 100;
105
106 static int batchtime;
107 static const int BATCH_TIME_DEFAULT = 300;
108
109 static int batchscheduleronly;
110 static const int BATCH_SCHEDULER_ONLY_DEFAULT = 0;
111
112 static int batchsafeshutdown;
113 static const int BATCH_SAFE_SHUTDOWN_DEFAULT = 1;
114
115 AST_MUTEX_DEFINE_STATIC(cdr_batch_lock);
116
117 /* these are used to wake up the CDR thread when there's work to do */
118 AST_MUTEX_DEFINE_STATIC(cdr_pending_lock);
119 static ast_cond_t cdr_pending_cond;
120
121 int check_cdr_enabled(void)
122 {
123         return enabled;
124 }
125
126 /*!
127  * \brief Register a CDR driver. Each registered CDR driver generates a CDR
128  * \retval 0 on success.
129  * \retval -1 on error
130  */
131 int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be)
132 {
133         struct ast_cdr_beitem *i = NULL;
134
135         if (!name)
136                 return -1;
137
138         if (!be) {
139                 ast_log(LOG_WARNING, "CDR engine '%s' lacks backend\n", name);
140                 return -1;
141         }
142
143         AST_RWLIST_WRLOCK(&be_list);
144         AST_RWLIST_TRAVERSE(&be_list, i, list) {
145                 if (!strcasecmp(name, i->name)) {
146                         ast_log(LOG_WARNING, "Already have a CDR backend called '%s'\n", name);
147                         AST_RWLIST_UNLOCK(&be_list);
148                         return -1;
149                 }
150         }
151
152         if (!(i = ast_calloc(1, sizeof(*i))))
153                 return -1;
154
155         i->be = be;
156         ast_copy_string(i->name, name, sizeof(i->name));
157         ast_copy_string(i->desc, desc, sizeof(i->desc));
158
159         AST_RWLIST_INSERT_HEAD(&be_list, i, list);
160         AST_RWLIST_UNLOCK(&be_list);
161
162         return 0;
163 }
164
165 /*! unregister a CDR driver */
166 void ast_cdr_unregister(const char *name)
167 {
168         struct ast_cdr_beitem *i = NULL;
169
170         AST_RWLIST_WRLOCK(&be_list);
171         AST_RWLIST_TRAVERSE_SAFE_BEGIN(&be_list, i, list) {
172                 if (!strcasecmp(name, i->name)) {
173                         AST_RWLIST_REMOVE_CURRENT(list);
174                         break;
175                 }
176         }
177         AST_RWLIST_TRAVERSE_SAFE_END;
178         AST_RWLIST_UNLOCK(&be_list);
179
180         if (i) {
181                 ast_verb(2, "Unregistered '%s' CDR backend\n", name);
182                 ast_free(i);
183         }
184 }
185
186 int ast_cdr_isset_unanswered(void)
187 {
188         return unanswered;
189 }
190
191 int ast_cdr_isset_congestion(void)
192 {
193         return congestion;
194 }
195
196 struct ast_cdr *ast_cdr_dup_unique(struct ast_cdr *cdr)
197 {
198         struct ast_cdr *newcdr = ast_cdr_dup(cdr);
199         if (!newcdr)
200                 return NULL;
201
202         cdr_seq_inc(newcdr);
203         return newcdr;
204 }
205
206 struct ast_cdr *ast_cdr_dup_unique_swap(struct ast_cdr *cdr)
207 {
208         struct ast_cdr *newcdr = ast_cdr_dup(cdr);
209         if (!newcdr)
210                 return NULL;
211
212         cdr_seq_inc(cdr);
213         return newcdr;
214 }
215
216 /*! Duplicate a CDR record
217         \returns Pointer to new CDR record
218 */
219 struct ast_cdr *ast_cdr_dup(struct ast_cdr *cdr)
220 {
221         struct ast_cdr *newcdr;
222
223         if (!cdr) /* don't die if we get a null cdr pointer */
224                 return NULL;
225         newcdr = ast_cdr_alloc();
226         if (!newcdr)
227                 return NULL;
228
229         memcpy(newcdr, cdr, sizeof(*newcdr));
230         /* The varshead is unusable, volatile even, after the memcpy so we take care of that here */
231         memset(&newcdr->varshead, 0, sizeof(newcdr->varshead));
232         ast_cdr_copy_vars(newcdr, cdr);
233         newcdr->next = NULL;
234
235         return newcdr;
236 }
237
238 static const char *ast_cdr_getvar_internal(struct ast_cdr *cdr, const char *name, int recur)
239 {
240         if (ast_strlen_zero(name))
241                 return NULL;
242
243         for (; cdr; cdr = recur ? cdr->next : NULL) {
244                 struct ast_var_t *variables;
245                 struct varshead *headp = &cdr->varshead;
246                 AST_LIST_TRAVERSE(headp, variables, entries) {
247                         if (!strcasecmp(name, ast_var_name(variables)))
248                                 return ast_var_value(variables);
249                 }
250         }
251
252         return NULL;
253 }
254
255 static void cdr_get_tv(struct timeval when, const char *fmt, char *buf, int bufsize)
256 {
257         if (fmt == NULL) {      /* raw mode */
258                 snprintf(buf, bufsize, "%ld.%06ld", (long)when.tv_sec, (long)when.tv_usec);
259         } else {
260                 if (when.tv_sec) {
261                         struct ast_tm tm;
262
263                         ast_localtime(&when, &tm, NULL);
264                         ast_strftime(buf, bufsize, fmt, &tm);
265                 }
266         }
267 }
268
269 /*! CDR channel variable retrieval */
270 void ast_cdr_getvar(struct ast_cdr *cdr, const char *name, char **ret, char *workspace, int workspacelen, int recur, int raw)
271 {
272         const char *fmt = "%Y-%m-%d %T";
273         const char *varbuf;
274
275         if (!cdr)  /* don't die if the cdr is null */
276                 return;
277
278         *ret = NULL;
279         /* special vars (the ones from the struct ast_cdr when requested by name)
280            I'd almost say we should convert all the stringed vals to vars */
281
282         if (!strcasecmp(name, "clid"))
283                 ast_copy_string(workspace, cdr->clid, workspacelen);
284         else if (!strcasecmp(name, "src"))
285                 ast_copy_string(workspace, cdr->src, workspacelen);
286         else if (!strcasecmp(name, "dst"))
287                 ast_copy_string(workspace, cdr->dst, workspacelen);
288         else if (!strcasecmp(name, "dcontext"))
289                 ast_copy_string(workspace, cdr->dcontext, workspacelen);
290         else if (!strcasecmp(name, "channel"))
291                 ast_copy_string(workspace, cdr->channel, workspacelen);
292         else if (!strcasecmp(name, "dstchannel"))
293                 ast_copy_string(workspace, cdr->dstchannel, workspacelen);
294         else if (!strcasecmp(name, "lastapp"))
295                 ast_copy_string(workspace, cdr->lastapp, workspacelen);
296         else if (!strcasecmp(name, "lastdata"))
297                 ast_copy_string(workspace, cdr->lastdata, workspacelen);
298         else if (!strcasecmp(name, "start"))
299                 cdr_get_tv(cdr->start, raw ? NULL : fmt, workspace, workspacelen);
300         else if (!strcasecmp(name, "answer"))
301                 cdr_get_tv(cdr->answer, raw ? NULL : fmt, workspace, workspacelen);
302         else if (!strcasecmp(name, "end"))
303                 cdr_get_tv(cdr->end, raw ? NULL : fmt, workspace, workspacelen);
304         else if (!strcasecmp(name, "duration")) {
305                 snprintf(workspace, workspacelen, "%ld", cdr->end.tv_sec != 0 ? cdr->duration : (long)ast_tvdiff_ms(ast_tvnow(), cdr->start) / 1000);
306         } else if (!strcasecmp(name, "billsec"))
307                 snprintf(workspace, workspacelen, "%ld", cdr->billsec || cdr->answer.tv_sec == 0 ? cdr->billsec : (long)ast_tvdiff_ms(ast_tvnow(), cdr->answer) / 1000);
308         else if (!strcasecmp(name, "disposition")) {
309                 if (raw) {
310                         snprintf(workspace, workspacelen, "%ld", cdr->disposition);
311                 } else {
312                         ast_copy_string(workspace, ast_cdr_disp2str(cdr->disposition), workspacelen);
313                 }
314         } else if (!strcasecmp(name, "amaflags")) {
315                 if (raw) {
316                         snprintf(workspace, workspacelen, "%ld", cdr->amaflags);
317                 } else {
318                         ast_copy_string(workspace, ast_cdr_flags2str(cdr->amaflags), workspacelen);
319                 }
320         } else if (!strcasecmp(name, "accountcode"))
321                 ast_copy_string(workspace, cdr->accountcode, workspacelen);
322         else if (!strcasecmp(name, "peeraccount"))
323                 ast_copy_string(workspace, cdr->peeraccount, workspacelen);
324         else if (!strcasecmp(name, "uniqueid"))
325                 ast_copy_string(workspace, cdr->uniqueid, workspacelen);
326         else if (!strcasecmp(name, "linkedid"))
327                 ast_copy_string(workspace, cdr->linkedid, workspacelen);
328         else if (!strcasecmp(name, "userfield"))
329                 ast_copy_string(workspace, cdr->userfield, workspacelen);
330         else if (!strcasecmp(name, "sequence"))
331                 snprintf(workspace, workspacelen, "%d", cdr->sequence);
332         else if ((varbuf = ast_cdr_getvar_internal(cdr, name, recur)))
333                 ast_copy_string(workspace, varbuf, workspacelen);
334         else
335                 workspace[0] = '\0';
336
337         if (!ast_strlen_zero(workspace))
338                 *ret = workspace;
339 }
340
341 /* readonly cdr variables */
342 static const char * const cdr_readonly_vars[] = { "clid", "src", "dst", "dcontext", "channel", "dstchannel",
343                                                   "lastapp", "lastdata", "start", "answer", "end", "duration",
344                                                   "billsec", "disposition", "amaflags", "accountcode", "uniqueid", "linkedid",
345                                                   "userfield", "sequence", NULL };
346 /*! Set a CDR channel variable
347         \note You can't set the CDR variables that belong to the actual CDR record, like "billsec".
348 */
349 int ast_cdr_setvar(struct ast_cdr *cdr, const char *name, const char *value, int recur)
350 {
351         struct ast_var_t *newvariable;
352         struct varshead *headp;
353         int x;
354
355         for (x = 0; cdr_readonly_vars[x]; x++) {
356                 if (!strcasecmp(name, cdr_readonly_vars[x])) {
357                         ast_log(LOG_ERROR, "Attempt to set the '%s' read-only variable!.\n", name);
358                         return -1;
359                 }
360         }
361
362         if (!cdr) {
363                 ast_log(LOG_ERROR, "Attempt to set a variable on a nonexistent CDR record.\n");
364                 return -1;
365         }
366
367         for (; cdr; cdr = recur ? cdr->next : NULL) {
368                 if (ast_test_flag(cdr, AST_CDR_FLAG_DONT_TOUCH) && ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
369                         continue;
370                 headp = &cdr->varshead;
371                 AST_LIST_TRAVERSE_SAFE_BEGIN(headp, newvariable, entries) {
372                         if (!strcasecmp(ast_var_name(newvariable), name)) {
373                                 /* there is already such a variable, delete it */
374                                 AST_LIST_REMOVE_CURRENT(entries);
375                                 ast_var_delete(newvariable);
376                                 break;
377                         }
378                 }
379                 AST_LIST_TRAVERSE_SAFE_END;
380
381                 if (value) {
382                         newvariable = ast_var_assign(name, value);
383                         AST_LIST_INSERT_HEAD(headp, newvariable, entries);
384                 }
385         }
386
387         return 0;
388 }
389
390 int ast_cdr_copy_vars(struct ast_cdr *to_cdr, struct ast_cdr *from_cdr)
391 {
392         struct ast_var_t *variables, *newvariable = NULL;
393         struct varshead *headpa, *headpb;
394         const char *var, *val;
395         int x = 0;
396
397         if (!to_cdr || !from_cdr) /* don't die if one of the pointers is null */
398                 return 0;
399
400         headpa = &from_cdr->varshead;
401         headpb = &to_cdr->varshead;
402
403         AST_LIST_TRAVERSE(headpa,variables,entries) {
404                 if (variables &&
405                     (var = ast_var_name(variables)) && (val = ast_var_value(variables)) &&
406                     !ast_strlen_zero(var) && !ast_strlen_zero(val)) {
407                         newvariable = ast_var_assign(var, val);
408                         AST_LIST_INSERT_HEAD(headpb, newvariable, entries);
409                         x++;
410                 }
411         }
412
413         return x;
414 }
415
416 int ast_cdr_serialize_variables(struct ast_cdr *cdr, struct ast_str **buf, char delim, char sep, int recur)
417 {
418         struct ast_var_t *variables;
419         const char *var;
420         char *tmp;
421         char workspace[256];
422         int total = 0, x = 0, i;
423
424         ast_str_reset(*buf);
425
426         for (; cdr; cdr = recur ? cdr->next : NULL) {
427                 if (++x > 1)
428                         ast_str_append(buf, 0, "\n");
429
430                 AST_LIST_TRAVERSE(&cdr->varshead, variables, entries) {
431                         if (!(var = ast_var_name(variables))) {
432                                 continue;
433                         }
434
435                         if (ast_str_append(buf, 0, "level %d: %s%c%s%c", x, var, delim, S_OR(ast_var_value(variables), ""), sep) < 0) {
436                                 ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
437                                 break;
438                         }
439
440                         total++;
441                 }
442
443                 for (i = 0; cdr_readonly_vars[i]; i++) {
444                         workspace[0] = 0; /* null out the workspace, because the cdr_get_tv() won't write anything if time is NULL, so you get old vals */
445                         ast_cdr_getvar(cdr, cdr_readonly_vars[i], &tmp, workspace, sizeof(workspace), 0, 0);
446                         if (!tmp)
447                                 continue;
448
449                         if (ast_str_append(buf, 0, "level %d: %s%c%s%c", x, cdr_readonly_vars[i], delim, tmp, sep) < 0) {
450                                 ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
451                                 break;
452                         } else
453                                 total++;
454                 }
455         }
456
457         return total;
458 }
459
460
461 void ast_cdr_free_vars(struct ast_cdr *cdr, int recur)
462 {
463
464         /* clear variables */
465         for (; cdr; cdr = recur ? cdr->next : NULL) {
466                 struct ast_var_t *vardata;
467                 struct varshead *headp = &cdr->varshead;
468                 while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
469                         ast_var_delete(vardata);
470         }
471 }
472
473 /*! \brief  print a warning if cdr already posted */
474 static void check_post(struct ast_cdr *cdr)
475 {
476         if (!cdr)
477                 return;
478         if (ast_test_flag(cdr, AST_CDR_FLAG_POSTED))
479                 ast_log(LOG_NOTICE, "CDR on channel '%s' already posted\n", S_OR(cdr->channel, "<unknown>"));
480 }
481
482 void ast_cdr_free(struct ast_cdr *cdr)
483 {
484
485         while (cdr) {
486                 struct ast_cdr *next = cdr->next;
487
488                 ast_cdr_free_vars(cdr, 0);
489                 ast_free(cdr);
490                 cdr = next;
491         }
492 }
493
494 /*! \brief the same as a cdr_free call, only with no checks; just get rid of it */
495 void ast_cdr_discard(struct ast_cdr *cdr)
496 {
497         while (cdr) {
498                 struct ast_cdr *next = cdr->next;
499
500                 ast_cdr_free_vars(cdr, 0);
501                 ast_free(cdr);
502                 cdr = next;
503         }
504 }
505
506 struct ast_cdr *ast_cdr_alloc(void)
507 {
508         struct ast_cdr *x;
509         x = ast_calloc(1, sizeof(*x));
510         if (!x)
511                 ast_log(LOG_ERROR,"Allocation Failure for a CDR!\n");
512         return x;
513 }
514
515 static void cdr_merge_vars(struct ast_cdr *to, struct ast_cdr *from)
516 {
517         struct ast_var_t *variablesfrom,*variablesto;
518         struct varshead *headpfrom = &to->varshead;
519         struct varshead *headpto = &from->varshead;
520         AST_LIST_TRAVERSE_SAFE_BEGIN(headpfrom, variablesfrom, entries) {
521                 /* for every var in from, stick it in to */
522                 const char *fromvarname, *fromvarval;
523                 const char *tovarname = NULL, *tovarval = NULL;
524                 fromvarname = ast_var_name(variablesfrom);
525                 fromvarval = ast_var_value(variablesfrom);
526                 tovarname = 0;
527
528                 /* now, quick see if that var is in the 'to' cdr already */
529                 AST_LIST_TRAVERSE(headpto, variablesto, entries) {
530
531                         /* now, quick see if that var is in the 'to' cdr already */
532                         if ( strcasecmp(fromvarname, ast_var_name(variablesto)) == 0 ) {
533                                 tovarname = ast_var_name(variablesto);
534                                 tovarval = ast_var_value(variablesto);
535                                 break;
536                         }
537                 }
538                 if (tovarname && strcasecmp(fromvarval,tovarval) != 0) {  /* this message here to see how irritating the userbase finds it */
539                         ast_log(LOG_NOTICE, "Merging CDR's: variable %s value %s dropped in favor of value %s\n", tovarname, fromvarval, tovarval);
540                         continue;
541                 } else if (tovarname && strcasecmp(fromvarval,tovarval) == 0) /* if they are the same, the job is done */
542                         continue;
543
544                 /* rip this var out of the from cdr, and stick it in the to cdr */
545                 AST_LIST_MOVE_CURRENT(headpto, entries);
546         }
547         AST_LIST_TRAVERSE_SAFE_END;
548 }
549
550 void ast_cdr_merge(struct ast_cdr *to, struct ast_cdr *from)
551 {
552         struct ast_cdr *zcdr;
553         struct ast_cdr *lto = NULL;
554         struct ast_cdr *lfrom = NULL;
555         int discard_from = 0;
556
557         if (!to || !from)
558                 return;
559
560         /* don't merge into locked CDR's -- it's bad business */
561         if (ast_test_flag(to, AST_CDR_FLAG_LOCKED)) {
562                 zcdr = to; /* safety valve? */
563                 while (to->next) {
564                         lto = to;
565                         to = to->next;
566                 }
567
568                 if (ast_test_flag(to, AST_CDR_FLAG_LOCKED)) {
569                         ast_log(LOG_WARNING, "Merging into locked CDR... no choice.\n");
570                         to = zcdr; /* safety-- if all there are is locked CDR's, then.... ?? */
571                         lto = NULL;
572                 }
573         }
574
575         if (ast_test_flag(from, AST_CDR_FLAG_LOCKED)) {
576                 struct ast_cdr *llfrom = NULL;
577                 discard_from = 1;
578                 if (lto) {
579                         /* insert the from stuff after lto */
580                         lto->next = from;
581                         lfrom = from;
582                         while (lfrom && lfrom->next) {
583                                 if (!lfrom->next->next)
584                                         llfrom = lfrom;
585                                 lfrom = lfrom->next;
586                         }
587                         /* rip off the last entry and put a copy of the to at the end */
588                         if (llfrom) {
589                                 llfrom->next = to;
590                         }
591                         from = lfrom;
592                 } else {
593                         /* save copy of the current *to cdr */
594                         struct ast_cdr tcdr;
595                         memcpy(&tcdr, to, sizeof(tcdr));
596                         /* copy in the locked from cdr */
597                         memcpy(to, from, sizeof(*to));
598                         lfrom = from;
599                         while (lfrom && lfrom->next) {
600                                 if (!lfrom->next->next)
601                                         llfrom = lfrom;
602                                 lfrom = lfrom->next;
603                         }
604                         from->next = NULL;
605                         /* rip off the last entry and put a copy of the to at the end */
606                         if (llfrom == from) {
607                                 to = to->next = ast_cdr_dup(&tcdr);
608                         } else if (llfrom) {
609                                 to = llfrom->next = ast_cdr_dup(&tcdr);
610                         }
611                         from = lfrom;
612                 }
613         }
614
615         if (!ast_tvzero(from->start)) {
616                 if (!ast_tvzero(to->start)) {
617                         if (ast_tvcmp(to->start, from->start) > 0 ) {
618                                 to->start = from->start; /* use the earliest time */
619                                 from->start = ast_tv(0,0); /* we actively "steal" these values */
620                         }
621                         /* else nothing to do */
622                 } else {
623                         to->start = from->start;
624                         from->start = ast_tv(0,0); /* we actively "steal" these values */
625                 }
626         }
627         if (!ast_tvzero(from->answer)) {
628                 if (!ast_tvzero(to->answer)) {
629                         if (ast_tvcmp(to->answer, from->answer) > 0 ) {
630                                 to->answer = from->answer; /* use the earliest time */
631                                 from->answer = ast_tv(0,0); /* we actively "steal" these values */
632                         }
633                         /* we got the earliest answer time, so we'll settle for that? */
634                 } else {
635                         to->answer = from->answer;
636                         from->answer = ast_tv(0,0); /* we actively "steal" these values */
637                 }
638         }
639         if (!ast_tvzero(from->end)) {
640                 if (!ast_tvzero(to->end)) {
641                         if (ast_tvcmp(to->end, from->end) < 0 ) {
642                                 to->end = from->end; /* use the latest time */
643                                 from->end = ast_tv(0,0); /* we actively "steal" these values */
644                                 to->duration = to->end.tv_sec - to->start.tv_sec;  /* don't forget to update the duration, billsec, when we set end */
645                                 to->billsec = ast_tvzero(to->answer) ? 0 : to->end.tv_sec - to->answer.tv_sec;
646                         }
647                         /* else, nothing to do */
648                 } else {
649                         to->end = from->end;
650                         from->end = ast_tv(0,0); /* we actively "steal" these values */
651                         to->duration = to->end.tv_sec - to->start.tv_sec;
652                         to->billsec = ast_tvzero(to->answer) ? 0 : to->end.tv_sec - to->answer.tv_sec;
653                 }
654         }
655         if (to->disposition < from->disposition) {
656                 to->disposition = from->disposition;
657                 from->disposition = AST_CDR_NOANSWER;
658         }
659         if (ast_strlen_zero(to->lastapp) && !ast_strlen_zero(from->lastapp)) {
660                 ast_copy_string(to->lastapp, from->lastapp, sizeof(to->lastapp));
661                 from->lastapp[0] = 0; /* theft */
662         }
663         if (ast_strlen_zero(to->lastdata) && !ast_strlen_zero(from->lastdata)) {
664                 ast_copy_string(to->lastdata, from->lastdata, sizeof(to->lastdata));
665                 from->lastdata[0] = 0; /* theft */
666         }
667         if (ast_strlen_zero(to->dcontext) && !ast_strlen_zero(from->dcontext)) {
668                 ast_copy_string(to->dcontext, from->dcontext, sizeof(to->dcontext));
669                 from->dcontext[0] = 0; /* theft */
670         }
671         if (ast_strlen_zero(to->dstchannel) && !ast_strlen_zero(from->dstchannel)) {
672                 ast_copy_string(to->dstchannel, from->dstchannel, sizeof(to->dstchannel));
673                 from->dstchannel[0] = 0; /* theft */
674         }
675         if (!ast_strlen_zero(from->channel) && (ast_strlen_zero(to->channel) || !strncasecmp(from->channel, "Agent/", 6))) {
676                 ast_copy_string(to->channel, from->channel, sizeof(to->channel));
677                 from->channel[0] = 0; /* theft */
678         }
679         if (ast_strlen_zero(to->src) && !ast_strlen_zero(from->src)) {
680                 ast_copy_string(to->src, from->src, sizeof(to->src));
681                 from->src[0] = 0; /* theft */
682         }
683         if (ast_strlen_zero(to->clid) && !ast_strlen_zero(from->clid)) {
684                 ast_copy_string(to->clid, from->clid, sizeof(to->clid));
685                 from->clid[0] = 0; /* theft */
686         }
687         if (ast_strlen_zero(to->dst) && !ast_strlen_zero(from->dst)) {
688                 ast_copy_string(to->dst, from->dst, sizeof(to->dst));
689                 from->dst[0] = 0; /* theft */
690         }
691         if (!to->amaflags)
692                 to->amaflags = AST_CDR_DOCUMENTATION;
693         if (!from->amaflags)
694                 from->amaflags = AST_CDR_DOCUMENTATION; /* make sure both amaflags are set to something (DOC is default) */
695         if (ast_test_flag(from, AST_CDR_FLAG_LOCKED) || (to->amaflags == AST_CDR_DOCUMENTATION && from->amaflags != AST_CDR_DOCUMENTATION)) {
696                 to->amaflags = from->amaflags;
697         }
698         if (ast_test_flag(from, AST_CDR_FLAG_LOCKED) || (ast_strlen_zero(to->accountcode) && !ast_strlen_zero(from->accountcode))) {
699                 ast_copy_string(to->accountcode, from->accountcode, sizeof(to->accountcode));
700         }
701         if (ast_test_flag(from, AST_CDR_FLAG_LOCKED) || (ast_strlen_zero(to->peeraccount) && !ast_strlen_zero(from->peeraccount))) {
702                 ast_copy_string(to->peeraccount, from->peeraccount, sizeof(to->peeraccount));
703         }
704         if (ast_test_flag(from, AST_CDR_FLAG_LOCKED) || (ast_strlen_zero(to->userfield) && !ast_strlen_zero(from->userfield))) {
705                 ast_copy_string(to->userfield, from->userfield, sizeof(to->userfield));
706         }
707         /* flags, varsead, ? */
708         cdr_merge_vars(from, to);
709
710         if (ast_test_flag(from, AST_CDR_FLAG_KEEP_VARS))
711                 ast_set_flag(to, AST_CDR_FLAG_KEEP_VARS);
712         if (ast_test_flag(from, AST_CDR_FLAG_POSTED))
713                 ast_set_flag(to, AST_CDR_FLAG_POSTED);
714         if (ast_test_flag(from, AST_CDR_FLAG_LOCKED))
715                 ast_set_flag(to, AST_CDR_FLAG_LOCKED);
716         if (ast_test_flag(from, AST_CDR_FLAG_CHILD))
717                 ast_set_flag(to, AST_CDR_FLAG_CHILD);
718         if (ast_test_flag(from, AST_CDR_FLAG_POST_DISABLED))
719                 ast_set_flag(to, AST_CDR_FLAG_POST_DISABLED);
720
721         /* last, but not least, we need to merge any forked CDRs to the 'to' cdr */
722         while (from->next) {
723                 /* just rip 'em off the 'from' and insert them on the 'to' */
724                 zcdr = from->next;
725                 from->next = zcdr->next;
726                 zcdr->next = NULL;
727                 /* zcdr is now ripped from the current list; */
728                 ast_cdr_append(to, zcdr);
729         }
730         if (discard_from)
731                 ast_cdr_discard(from);
732 }
733
734 void ast_cdr_start(struct ast_cdr *cdr)
735 {
736         for (; cdr; cdr = cdr->next) {
737                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
738                         check_post(cdr);
739                         cdr->start = ast_tvnow();
740                 }
741         }
742 }
743
744 void ast_cdr_answer(struct ast_cdr *cdr)
745 {
746
747         for (; cdr; cdr = cdr->next) {
748                 if (ast_test_flag(cdr, AST_CDR_FLAG_ANSLOCKED))
749                         continue;
750                 if (ast_test_flag(cdr, AST_CDR_FLAG_DONT_TOUCH) && ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
751                         continue;
752                 check_post(cdr);
753                 if (cdr->disposition < AST_CDR_ANSWERED)
754                         cdr->disposition = AST_CDR_ANSWERED;
755                 if (ast_tvzero(cdr->answer))
756                         cdr->answer = ast_tvnow();
757         }
758 }
759
760 void ast_cdr_busy(struct ast_cdr *cdr)
761 {
762
763         for (; cdr; cdr = cdr->next) {
764                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
765                         check_post(cdr);
766                         cdr->disposition = AST_CDR_BUSY;
767                 }
768         }
769 }
770
771 void ast_cdr_failed(struct ast_cdr *cdr)
772 {
773         for (; cdr; cdr = cdr->next) {
774                 check_post(cdr);
775                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
776                         check_post(cdr);
777                         if (cdr->disposition < AST_CDR_FAILED)
778                                 cdr->disposition = AST_CDR_FAILED;
779                 }
780         }
781 }
782
783 void ast_cdr_noanswer(struct ast_cdr *cdr)
784 {
785         while (cdr) {
786                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
787                         check_post(cdr);
788                         cdr->disposition = AST_CDR_NOANSWER;
789                 }
790                 cdr = cdr->next;
791         }
792 }
793
794 void ast_cdr_congestion(struct ast_cdr *cdr)
795 {
796         char *chan;
797
798         /* if congestion log is disabled, pass the buck to ast_cdr_failed */
799         if (!congestion) {
800                 ast_cdr_failed(cdr);
801         }
802
803         while (cdr && congestion) {
804                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
805                         chan = !ast_strlen_zero(cdr->channel) ? cdr->channel : "<unknown>";
806
807                         if (ast_test_flag(cdr, AST_CDR_FLAG_POSTED)) {
808                                 ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
809                         }
810
811                         if (cdr->disposition < AST_CDR_CONGESTION) {
812                                 cdr->disposition = AST_CDR_CONGESTION;
813                         }
814                 }
815                 cdr = cdr->next;
816         }
817 }
818
819 /* everywhere ast_cdr_disposition is called, it will call ast_cdr_failed()
820    if ast_cdr_disposition returns a non-zero value */
821
822 int ast_cdr_disposition(struct ast_cdr *cdr, int cause)
823 {
824         int res = 0;
825
826         for (; cdr; cdr = cdr->next) {
827                 switch (cause) {  /* handle all the non failure, busy cases, return 0 not to set disposition,
828                                                         return -1 to set disposition to FAILED */
829                 case AST_CAUSE_BUSY:
830                         ast_cdr_busy(cdr);
831                         break;
832                 case AST_CAUSE_NO_ANSWER:
833                         ast_cdr_noanswer(cdr);
834                         break;
835                 case AST_CAUSE_NORMAL_CIRCUIT_CONGESTION:
836                         ast_cdr_congestion(cdr);
837                         break;
838                 case AST_CAUSE_NORMAL:
839                         break;
840                 default:
841                         res = -1;
842                 }
843         }
844         return res;
845 }
846
847 void ast_cdr_setdestchan(struct ast_cdr *cdr, const char *chann)
848 {
849         for (; cdr; cdr = cdr->next) {
850                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
851                         check_post(cdr);
852                         ast_copy_string(cdr->dstchannel, chann, sizeof(cdr->dstchannel));
853                 }
854         }
855 }
856
857 void ast_cdr_setapp(struct ast_cdr *cdr, const char *app, const char *data)
858 {
859
860         for (; cdr; cdr = cdr->next) {
861                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
862                         check_post(cdr);
863                         ast_copy_string(cdr->lastapp, S_OR(app, ""), sizeof(cdr->lastapp));
864                         ast_copy_string(cdr->lastdata, S_OR(data, ""), sizeof(cdr->lastdata));
865                 }
866         }
867 }
868
869 void ast_cdr_setanswer(struct ast_cdr *cdr, struct timeval t)
870 {
871
872         for (; cdr; cdr = cdr->next) {
873                 if (ast_test_flag(cdr, AST_CDR_FLAG_ANSLOCKED))
874                         continue;
875                 if (ast_test_flag(cdr, AST_CDR_FLAG_DONT_TOUCH) && ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
876                         continue;
877                 check_post(cdr);
878                 cdr->answer = t;
879         }
880 }
881
882 void ast_cdr_setdisposition(struct ast_cdr *cdr, long int disposition)
883 {
884
885         for (; cdr; cdr = cdr->next) {
886                 if (ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
887                         continue;
888                 check_post(cdr);
889                 cdr->disposition = disposition;
890         }
891 }
892
893 /* set cid info for one record */
894 static void set_one_cid(struct ast_cdr *cdr, struct ast_channel *c)
895 {
896         const char *num;
897
898         if (!cdr) {
899                 return;
900         }
901
902         /* Grab source from ANI or normal Caller*ID */
903         num = S_COR(ast_channel_caller(c)->ani.number.valid, ast_channel_caller(c)->ani.number.str,
904                 S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL));
905         ast_callerid_merge(cdr->clid, sizeof(cdr->clid),
906                 S_COR(ast_channel_caller(c)->id.name.valid, ast_channel_caller(c)->id.name.str, NULL), num, "");
907         ast_copy_string(cdr->src, S_OR(num, ""), sizeof(cdr->src));
908         ast_cdr_setvar(cdr, "dnid", S_OR(ast_channel_dialed(c)->number.str, ""), 0);
909
910         if (ast_channel_caller(c)->id.subaddress.valid) {
911                 ast_cdr_setvar(cdr, "callingsubaddr", S_OR(ast_channel_caller(c)->id.subaddress.str, ""), 0);
912         }
913         if (ast_channel_dialed(c)->subaddress.valid) {
914                 ast_cdr_setvar(cdr, "calledsubaddr", S_OR(ast_channel_dialed(c)->subaddress.str, ""), 0);
915         }
916 }
917
918 int ast_cdr_setcid(struct ast_cdr *cdr, struct ast_channel *c)
919 {
920         for (; cdr; cdr = cdr->next) {
921                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
922                         set_one_cid(cdr, c);
923         }
924         return 0;
925 }
926
927 static int cdr_seq_inc(struct ast_cdr *cdr)
928 {
929         return (cdr->sequence = ast_atomic_fetchadd_int(&cdr_sequence, +1));
930 }
931
932 int ast_cdr_init(struct ast_cdr *cdr, struct ast_channel *c)
933 {
934         for ( ; cdr ; cdr = cdr->next) {
935                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
936                         ast_copy_string(cdr->channel, ast_channel_name(c), sizeof(cdr->channel));
937                         set_one_cid(cdr, c);
938                         cdr_seq_inc(cdr);
939
940                         cdr->disposition = (ast_channel_state(c) == AST_STATE_UP) ?  AST_CDR_ANSWERED : AST_CDR_NOANSWER;
941                         cdr->amaflags = ast_channel_amaflags(c) ? ast_channel_amaflags(c) :  ast_default_amaflags;
942                         ast_copy_string(cdr->accountcode, ast_channel_accountcode(c), sizeof(cdr->accountcode));
943                         ast_copy_string(cdr->peeraccount, ast_channel_peeraccount(c), sizeof(cdr->peeraccount));
944                         /* Destination information */
945                         ast_copy_string(cdr->dst, S_OR(ast_channel_macroexten(c),ast_channel_exten(c)), sizeof(cdr->dst));
946                         ast_copy_string(cdr->dcontext, S_OR(ast_channel_macrocontext(c),ast_channel_context(c)), sizeof(cdr->dcontext));
947                         /* Unique call identifier */
948                         ast_copy_string(cdr->uniqueid, ast_channel_uniqueid(c), sizeof(cdr->uniqueid));
949                         /* Linked call identifier */
950                         ast_copy_string(cdr->linkedid, ast_channel_linkedid(c), sizeof(cdr->linkedid));
951                 }
952         }
953         return 0;
954 }
955
956 /* Three routines were "fixed" via 10668, and later shown that
957    users were depending on this behavior. ast_cdr_end,
958    ast_cdr_setvar and ast_cdr_answer are the three routines.
959    While most of the other routines would not touch
960    LOCKED cdr's, these three routines were designed to
961    operate on locked CDR's as a matter of course.
962    I now appreciate how this plays with the ForkCDR app,
963    which forms these cdr chains in the first place.
964    cdr_end is pretty key: all cdrs created are closed
965    together. They only vary by start time. Arithmetically,
966    users can calculate the subintervals they wish to track. */
967
968 void ast_cdr_end(struct ast_cdr *cdr)
969 {
970         for ( ; cdr ; cdr = cdr->next) {
971                 if (ast_test_flag(cdr, AST_CDR_FLAG_DONT_TOUCH) && ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
972                         continue;
973                 check_post(cdr);
974                 if (ast_tvzero(cdr->end))
975                         cdr->end = ast_tvnow();
976                 if (ast_tvzero(cdr->start)) {
977                         ast_log(LOG_WARNING, "CDR on channel '%s' has not started\n", S_OR(cdr->channel, "<unknown>"));
978                         cdr->disposition = AST_CDR_FAILED;
979                 } else
980                         cdr->duration = cdr->end.tv_sec - cdr->start.tv_sec;
981                 if (ast_tvzero(cdr->answer)) {
982                         if (cdr->disposition == AST_CDR_ANSWERED) {
983                                 ast_log(LOG_WARNING, "CDR on channel '%s' has no answer time but is 'ANSWERED'\n", S_OR(cdr->channel, "<unknown>"));
984                                 cdr->disposition = AST_CDR_FAILED;
985                         }
986                 } else {
987                         cdr->billsec = cdr->end.tv_sec - cdr->answer.tv_sec;
988                         if (ast_test_flag(&ast_options, AST_OPT_FLAG_INITIATED_SECONDS))
989                                 cdr->billsec += cdr->end.tv_usec > cdr->answer.tv_usec ? 1 : 0;
990                 }
991         }
992 }
993
994 char *ast_cdr_disp2str(int disposition)
995 {
996         switch (disposition) {
997         case AST_CDR_NULL:
998                 return "NO ANSWER"; /* by default, for backward compatibility */
999         case AST_CDR_NOANSWER:
1000                 return "NO ANSWER";
1001         case AST_CDR_FAILED:
1002                 return "FAILED";
1003         case AST_CDR_BUSY:
1004                 return "BUSY";
1005         case AST_CDR_ANSWERED:
1006                 return "ANSWERED";
1007         case AST_CDR_CONGESTION:
1008                 return "CONGESTION";
1009         }
1010         return "UNKNOWN";
1011 }
1012
1013 /*! Converts AMA flag to printable string */
1014 char *ast_cdr_flags2str(int flag)
1015 {
1016         switch (flag) {
1017         case AST_CDR_OMIT:
1018                 return "OMIT";
1019         case AST_CDR_BILLING:
1020                 return "BILLING";
1021         case AST_CDR_DOCUMENTATION:
1022                 return "DOCUMENTATION";
1023         }
1024         return "Unknown";
1025 }
1026
1027 int ast_cdr_setaccount(struct ast_channel *chan, const char *account)
1028 {
1029         struct ast_cdr *cdr = ast_channel_cdr(chan);
1030         const char *old_acct = "";
1031
1032         if (!ast_strlen_zero(ast_channel_accountcode(chan))) {
1033                 old_acct = ast_strdupa(ast_channel_accountcode(chan));
1034         }
1035
1036         ast_channel_accountcode_set(chan, account);
1037         for ( ; cdr ; cdr = cdr->next) {
1038                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1039                         ast_copy_string(cdr->accountcode, ast_channel_accountcode(chan), sizeof(cdr->accountcode));
1040                 }
1041         }
1042         /*** DOCUMENTATION
1043                 <managerEventInstance>
1044                         <synopsis>Raised when a CDR's AccountCode is changed.</synopsis>
1045                 </managerEventInstance>
1046         ***/
1047         ast_manager_event(chan, EVENT_FLAG_CALL, "NewAccountCode",
1048                         "Channel: %s\r\n"
1049                         "Uniqueid: %s\r\n"
1050                         "AccountCode: %s\r\n"
1051                         "OldAccountCode: %s\r\n",
1052                         ast_channel_name(chan), ast_channel_uniqueid(chan), ast_channel_accountcode(chan), old_acct);
1053
1054         return 0;
1055 }
1056
1057 int ast_cdr_setpeeraccount(struct ast_channel *chan, const char *account)
1058 {
1059         struct ast_cdr *cdr = ast_channel_cdr(chan);
1060         const char *old_acct = "";
1061
1062         if (!ast_strlen_zero(ast_channel_peeraccount(chan))) {
1063                 old_acct = ast_strdupa(ast_channel_peeraccount(chan));
1064         }
1065
1066         ast_channel_peeraccount_set(chan, account);
1067         for ( ; cdr ; cdr = cdr->next) {
1068                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1069                         ast_copy_string(cdr->peeraccount, ast_channel_peeraccount(chan), sizeof(cdr->peeraccount));
1070                 }
1071         }
1072         /*** DOCUMENTATION
1073                 <managerEventInstance>
1074                         <synopsis>Raised when a CDR's PeerAccount is changed.</synopsis>
1075                 </managerEventInstance>
1076         ***/
1077         ast_manager_event(chan, EVENT_FLAG_CALL, "NewPeerAccount",
1078                         "Channel: %s\r\n"
1079                         "Uniqueid: %s\r\n"
1080                         "PeerAccount: %s\r\n"
1081                         "OldPeerAccount: %s\r\n",
1082                         ast_channel_name(chan), ast_channel_uniqueid(chan), ast_channel_peeraccount(chan), old_acct);
1083
1084         return 0;
1085 }
1086
1087 int ast_cdr_setamaflags(struct ast_channel *chan, const char *flag)
1088 {
1089         struct ast_cdr *cdr;
1090         int newflag = ast_cdr_amaflags2int(flag);
1091         if (newflag) {
1092                 for (cdr = ast_channel_cdr(chan); cdr; cdr = cdr->next) {
1093                         if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1094                                 cdr->amaflags = newflag;
1095                         }
1096                 }
1097         }
1098
1099         return 0;
1100 }
1101
1102 int ast_cdr_setuserfield(struct ast_channel *chan, const char *userfield)
1103 {
1104         struct ast_cdr *cdr = ast_channel_cdr(chan);
1105
1106         for ( ; cdr ; cdr = cdr->next) {
1107                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
1108                         ast_copy_string(cdr->userfield, userfield, sizeof(cdr->userfield));
1109         }
1110
1111         return 0;
1112 }
1113
1114 int ast_cdr_appenduserfield(struct ast_channel *chan, const char *userfield)
1115 {
1116         struct ast_cdr *cdr = ast_channel_cdr(chan);
1117
1118         for ( ; cdr ; cdr = cdr->next) {
1119                 int len = strlen(cdr->userfield);
1120
1121                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED))
1122                         ast_copy_string(cdr->userfield + len, userfield, sizeof(cdr->userfield) - len);
1123         }
1124
1125         return 0;
1126 }
1127
1128 int ast_cdr_update(struct ast_channel *c)
1129 {
1130         struct ast_cdr *cdr = ast_channel_cdr(c);
1131
1132         for ( ; cdr ; cdr = cdr->next) {
1133                 if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1134                         set_one_cid(cdr, c);
1135
1136                         /* Copy account code et-al */
1137                         ast_copy_string(cdr->accountcode, ast_channel_accountcode(c), sizeof(cdr->accountcode));
1138                         ast_copy_string(cdr->peeraccount, ast_channel_peeraccount(c), sizeof(cdr->peeraccount));
1139                         ast_copy_string(cdr->linkedid, ast_channel_linkedid(c), sizeof(cdr->linkedid));
1140
1141                         /* Destination information */ /* XXX privilege macro* ? */
1142                         ast_copy_string(cdr->dst, S_OR(ast_channel_macroexten(c), ast_channel_exten(c)), sizeof(cdr->dst));
1143                         ast_copy_string(cdr->dcontext, S_OR(ast_channel_macrocontext(c), ast_channel_context(c)), sizeof(cdr->dcontext));
1144                 }
1145         }
1146
1147         return 0;
1148 }
1149
1150 int ast_cdr_amaflags2int(const char *flag)
1151 {
1152         if (!strcasecmp(flag, "default"))
1153                 return 0;
1154         if (!strcasecmp(flag, "omit"))
1155                 return AST_CDR_OMIT;
1156         if (!strcasecmp(flag, "billing"))
1157                 return AST_CDR_BILLING;
1158         if (!strcasecmp(flag, "documentation"))
1159                 return AST_CDR_DOCUMENTATION;
1160         return -1;
1161 }
1162
1163 static void post_cdr(struct ast_cdr *cdr)
1164 {
1165         struct ast_cdr_beitem *i;
1166
1167         for ( ; cdr ; cdr = cdr->next) {
1168                 if (!unanswered && cdr->disposition < AST_CDR_ANSWERED && (ast_strlen_zero(cdr->channel) || ast_strlen_zero(cdr->dstchannel))) {
1169                         /* For people, who don't want to see unanswered single-channel events */
1170                         ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1171                         continue;
1172                 }
1173
1174                 /* don't post CDRs that are for dialed channels unless those
1175                  * channels were originated from asterisk (pbx_spool, manager,
1176                  * cli) */
1177                 if (ast_test_flag(cdr, AST_CDR_FLAG_DIALED) && !ast_test_flag(cdr, AST_CDR_FLAG_ORIGINATED)) {
1178                         ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1179                         continue;
1180                 }
1181
1182                 check_post(cdr);
1183                 ast_set_flag(cdr, AST_CDR_FLAG_POSTED);
1184                 if (ast_test_flag(cdr, AST_CDR_FLAG_POST_DISABLED))
1185                         continue;
1186                 AST_RWLIST_RDLOCK(&be_list);
1187                 AST_RWLIST_TRAVERSE(&be_list, i, list) {
1188                         i->be(cdr);
1189                 }
1190                 AST_RWLIST_UNLOCK(&be_list);
1191         }
1192 }
1193
1194 void ast_cdr_reset(struct ast_cdr *cdr, struct ast_flags *_flags)
1195 {
1196         struct ast_cdr *duplicate;
1197         struct ast_flags flags = { 0 };
1198
1199         if (_flags)
1200                 ast_copy_flags(&flags, _flags, AST_FLAGS_ALL);
1201
1202         for ( ; cdr ; cdr = cdr->next) {
1203                 /* Detach if post is requested */
1204                 if (ast_test_flag(&flags, AST_CDR_FLAG_LOCKED) || !ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
1205                         if (ast_test_flag(&flags, AST_CDR_FLAG_POSTED)) {
1206                                 ast_cdr_end(cdr);
1207                                 if ((duplicate = ast_cdr_dup_unique_swap(cdr))) {
1208                                         ast_cdr_detach(duplicate);
1209                                 }
1210                                 ast_set_flag(cdr, AST_CDR_FLAG_POSTED);
1211                         }
1212
1213                         /* enable CDR only */
1214                         if (ast_test_flag(&flags, AST_CDR_FLAG_POST_ENABLE)) {
1215                                 ast_clear_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1216                                 continue;
1217                         }
1218
1219                         /* clear variables */
1220                         if (!ast_test_flag(&flags, AST_CDR_FLAG_KEEP_VARS)) {
1221                                 ast_cdr_free_vars(cdr, 0);
1222                         }
1223
1224                         /* Reset to initial state */
1225                         ast_clear_flag(cdr, AST_FLAGS_ALL);
1226                         memset(&cdr->start, 0, sizeof(cdr->start));
1227                         memset(&cdr->end, 0, sizeof(cdr->end));
1228                         memset(&cdr->answer, 0, sizeof(cdr->answer));
1229                         cdr->billsec = 0;
1230                         cdr->duration = 0;
1231                         ast_cdr_start(cdr);
1232                         cdr->disposition = AST_CDR_NOANSWER;
1233                 }
1234         }
1235 }
1236
1237 void ast_cdr_specialized_reset(struct ast_cdr *cdr, struct ast_flags *_flags)
1238 {
1239         struct ast_flags flags = { 0 };
1240
1241         if (_flags)
1242                 ast_copy_flags(&flags, _flags, AST_FLAGS_ALL);
1243
1244         /* Reset to initial state */
1245         if (ast_test_flag(cdr, AST_CDR_FLAG_POST_DISABLED)) { /* But do NOT lose the NoCDR() setting */
1246                 ast_clear_flag(cdr, AST_FLAGS_ALL);
1247                 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1248         } else {
1249                 ast_clear_flag(cdr, AST_FLAGS_ALL);
1250         }
1251
1252         memset(&cdr->start, 0, sizeof(cdr->start));
1253         memset(&cdr->end, 0, sizeof(cdr->end));
1254         memset(&cdr->answer, 0, sizeof(cdr->answer));
1255         cdr->billsec = 0;
1256         cdr->duration = 0;
1257         ast_cdr_start(cdr);
1258         cdr->disposition = AST_CDR_NULL;
1259 }
1260
1261 struct ast_cdr *ast_cdr_append(struct ast_cdr *cdr, struct ast_cdr *newcdr)
1262 {
1263         struct ast_cdr *ret;
1264
1265         if (cdr) {
1266                 ret = cdr;
1267
1268                 while (cdr->next)
1269                         cdr = cdr->next;
1270                 cdr->next = newcdr;
1271         } else {
1272                 ret = newcdr;
1273         }
1274
1275         return ret;
1276 }
1277
1278 /*! \note Don't call without cdr_batch_lock */
1279 static void reset_batch(void)
1280 {
1281         batch->size = 0;
1282         batch->head = NULL;
1283         batch->tail = NULL;
1284 }
1285
1286 /*! \note Don't call without cdr_batch_lock */
1287 static int init_batch(void)
1288 {
1289         /* This is the single meta-batch used to keep track of all CDRs during the entire life of the program */
1290         if (!(batch = ast_malloc(sizeof(*batch))))
1291                 return -1;
1292
1293         reset_batch();
1294
1295         return 0;
1296 }
1297
1298 static void *do_batch_backend_process(void *data)
1299 {
1300         struct ast_cdr_batch_item *processeditem;
1301         struct ast_cdr_batch_item *batchitem = data;
1302
1303         /* Push each CDR into storage mechanism(s) and free all the memory */
1304         while (batchitem) {
1305                 post_cdr(batchitem->cdr);
1306                 ast_cdr_free(batchitem->cdr);
1307                 processeditem = batchitem;
1308                 batchitem = batchitem->next;
1309                 ast_free(processeditem);
1310         }
1311
1312         return NULL;
1313 }
1314
1315 void ast_cdr_submit_batch(int do_shutdown)
1316 {
1317         struct ast_cdr_batch_item *oldbatchitems = NULL;
1318         pthread_t batch_post_thread = AST_PTHREADT_NULL;
1319
1320         /* if there's no batch, or no CDRs in the batch, then there's nothing to do */
1321         if (!batch || !batch->head)
1322                 return;
1323
1324         /* move the old CDRs aside, and prepare a new CDR batch */
1325         ast_mutex_lock(&cdr_batch_lock);
1326         oldbatchitems = batch->head;
1327         reset_batch();
1328         ast_mutex_unlock(&cdr_batch_lock);
1329
1330         /* if configured, spawn a new thread to post these CDRs,
1331            also try to save as much as possible if we are shutting down safely */
1332         if (batchscheduleronly || do_shutdown) {
1333                 ast_debug(1, "CDR single-threaded batch processing begins now\n");
1334                 do_batch_backend_process(oldbatchitems);
1335         } else {
1336                 if (ast_pthread_create_detached_background(&batch_post_thread, NULL, do_batch_backend_process, oldbatchitems)) {
1337                         ast_log(LOG_WARNING, "CDR processing thread could not detach, now trying in this thread\n");
1338                         do_batch_backend_process(oldbatchitems);
1339                 } else {
1340                         ast_debug(1, "CDR multi-threaded batch processing begins now\n");
1341                 }
1342         }
1343 }
1344
1345 static int submit_scheduled_batch(const void *data)
1346 {
1347         ast_cdr_submit_batch(0);
1348         /* manually reschedule from this point in time */
1349         cdr_sched = ast_sched_add(sched, batchtime * 1000, submit_scheduled_batch, NULL);
1350         /* returning zero so the scheduler does not automatically reschedule */
1351         return 0;
1352 }
1353
1354 static void submit_unscheduled_batch(void)
1355 {
1356         /* this is okay since we are not being called from within the scheduler */
1357         AST_SCHED_DEL(sched, cdr_sched);
1358         /* schedule the submission to occur ASAP (1 ms) */
1359         cdr_sched = ast_sched_add(sched, 1, submit_scheduled_batch, NULL);
1360         /* signal the do_cdr thread to wakeup early and do some work (that lazy thread ;) */
1361         ast_mutex_lock(&cdr_pending_lock);
1362         ast_cond_signal(&cdr_pending_cond);
1363         ast_mutex_unlock(&cdr_pending_lock);
1364 }
1365
1366 void ast_cdr_detach(struct ast_cdr *cdr)
1367 {
1368         struct ast_cdr_batch_item *newtail;
1369         int curr;
1370
1371         if (!cdr)
1372                 return;
1373
1374         /* maybe they disabled CDR stuff completely, so just drop it */
1375         if (!enabled) {
1376                 ast_debug(1, "Dropping CDR !\n");
1377                 ast_set_flag(cdr, AST_CDR_FLAG_POST_DISABLED);
1378                 ast_cdr_free(cdr);
1379                 return;
1380         }
1381
1382         /* post stuff immediately if we are not in batch mode, this is legacy behaviour */
1383         if (!batchmode) {
1384                 post_cdr(cdr);
1385                 ast_cdr_free(cdr);
1386                 return;
1387         }
1388
1389         /* otherwise, each CDR gets put into a batch list (at the end) */
1390         ast_debug(1, "CDR detaching from this thread\n");
1391
1392         /* we'll need a new tail for every CDR */
1393         if (!(newtail = ast_calloc(1, sizeof(*newtail)))) {
1394                 post_cdr(cdr);
1395                 ast_cdr_free(cdr);
1396                 return;
1397         }
1398
1399         /* don't traverse a whole list (just keep track of the tail) */
1400         ast_mutex_lock(&cdr_batch_lock);
1401         if (!batch)
1402                 init_batch();
1403         if (!batch->head) {
1404                 /* new batch is empty, so point the head at the new tail */
1405                 batch->head = newtail;
1406         } else {
1407                 /* already got a batch with something in it, so just append a new tail */
1408                 batch->tail->next = newtail;
1409         }
1410         newtail->cdr = cdr;
1411         batch->tail = newtail;
1412         curr = batch->size++;
1413         ast_mutex_unlock(&cdr_batch_lock);
1414
1415         /* if we have enough stuff to post, then do it */
1416         if (curr >= (batchsize - 1))
1417                 submit_unscheduled_batch();
1418 }
1419
1420 static void *do_cdr(void *data)
1421 {
1422         struct timespec timeout;
1423         int schedms;
1424         int numevents = 0;
1425
1426         for (;;) {
1427                 struct timeval now;
1428                 schedms = ast_sched_wait(sched);
1429                 /* this shouldn't happen, but provide a 1 second default just in case */
1430                 if (schedms <= 0)
1431                         schedms = 1000;
1432                 now = ast_tvadd(ast_tvnow(), ast_samp2tv(schedms, 1000));
1433                 timeout.tv_sec = now.tv_sec;
1434                 timeout.tv_nsec = now.tv_usec * 1000;
1435                 /* prevent stuff from clobbering cdr_pending_cond, then wait on signals sent to it until the timeout expires */
1436                 ast_mutex_lock(&cdr_pending_lock);
1437                 ast_cond_timedwait(&cdr_pending_cond, &cdr_pending_lock, &timeout);
1438                 numevents = ast_sched_runq(sched);
1439                 ast_mutex_unlock(&cdr_pending_lock);
1440                 ast_debug(2, "Processed %d scheduled CDR batches from the run queue\n", numevents);
1441         }
1442
1443         return NULL;
1444 }
1445
1446 static char *handle_cli_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1447 {
1448         struct ast_cdr_beitem *beitem=NULL;
1449         int cnt=0;
1450         long nextbatchtime=0;
1451
1452         switch (cmd) {
1453         case CLI_INIT:
1454                 e->command = "cdr show status";
1455                 e->usage =
1456                         "Usage: cdr show status\n"
1457                         "       Displays the Call Detail Record engine system status.\n";
1458                 return NULL;
1459         case CLI_GENERATE:
1460                 return NULL;
1461         }
1462
1463         if (a->argc > 3)
1464                 return CLI_SHOWUSAGE;
1465
1466         ast_cli(a->fd, "\n");
1467         ast_cli(a->fd, "Call Detail Record (CDR) settings\n");
1468         ast_cli(a->fd, "----------------------------------\n");
1469         ast_cli(a->fd, "  Logging:                    %s\n", enabled ? "Enabled" : "Disabled");
1470         ast_cli(a->fd, "  Mode:                       %s\n", batchmode ? "Batch" : "Simple");
1471         if (enabled) {
1472                 ast_cli(a->fd, "  Log unanswered calls:       %s\n", unanswered ? "Yes" : "No");
1473                 ast_cli(a->fd, "  Log congestion:             %s\n\n", congestion ? "Yes" : "No");
1474                 if (batchmode) {
1475                         ast_cli(a->fd, "* Batch Mode Settings\n");
1476                         ast_cli(a->fd, "  -------------------\n");
1477                         if (batch)
1478                                 cnt = batch->size;
1479                         if (cdr_sched > -1)
1480                                 nextbatchtime = ast_sched_when(sched, cdr_sched);
1481                         ast_cli(a->fd, "  Safe shutdown:              %s\n", batchsafeshutdown ? "Enabled" : "Disabled");
1482                         ast_cli(a->fd, "  Threading model:            %s\n", batchscheduleronly ? "Scheduler only" : "Scheduler plus separate threads");
1483                         ast_cli(a->fd, "  Current batch size:         %d record%s\n", cnt, ESS(cnt));
1484                         ast_cli(a->fd, "  Maximum batch size:         %d record%s\n", batchsize, ESS(batchsize));
1485                         ast_cli(a->fd, "  Maximum batch time:         %d second%s\n", batchtime, ESS(batchtime));
1486                         ast_cli(a->fd, "  Next batch processing time: %ld second%s\n\n", nextbatchtime, ESS(nextbatchtime));
1487                 }
1488                 ast_cli(a->fd, "* Registered Backends\n");
1489                 ast_cli(a->fd, "  -------------------\n");
1490                 AST_RWLIST_RDLOCK(&be_list);
1491                 if (AST_RWLIST_EMPTY(&be_list)) {
1492                         ast_cli(a->fd, "    (none)\n");
1493                 } else {
1494                         AST_RWLIST_TRAVERSE(&be_list, beitem, list) {
1495                                 ast_cli(a->fd, "    %s\n", beitem->name);
1496                         }
1497                 }
1498                 AST_RWLIST_UNLOCK(&be_list);
1499                 ast_cli(a->fd, "\n");
1500         }
1501
1502         return CLI_SUCCESS;
1503 }
1504
1505 static char *handle_cli_submit(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1506 {
1507         switch (cmd) {
1508         case CLI_INIT:
1509                 e->command = "cdr submit";
1510                 e->usage =
1511                         "Usage: cdr submit\n"
1512                         "       Posts all pending batched CDR data to the configured CDR backend engine modules.\n";
1513                 return NULL;
1514         case CLI_GENERATE:
1515                 return NULL;
1516         }
1517         if (a->argc > 2)
1518                 return CLI_SHOWUSAGE;
1519
1520         submit_unscheduled_batch();
1521         ast_cli(a->fd, "Submitted CDRs to backend engines for processing.  This may take a while.\n");
1522
1523         return CLI_SUCCESS;
1524 }
1525
1526 static struct ast_cli_entry cli_submit = AST_CLI_DEFINE(handle_cli_submit, "Posts all pending batched CDR data");
1527 static struct ast_cli_entry cli_status = AST_CLI_DEFINE(handle_cli_status, "Display the CDR status");
1528
1529 static int do_reload(int reload)
1530 {
1531         struct ast_config *config;
1532         struct ast_variable *v;
1533         int cfg_size;
1534         int cfg_time;
1535         int was_enabled;
1536         int was_batchmode;
1537         int res = 0;
1538         struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
1539
1540         if ((config = ast_config_load2("cdr.conf", "cdr", config_flags)) == CONFIG_STATUS_FILEUNCHANGED) {
1541                 return 0;
1542         }
1543
1544         ast_mutex_lock(&cdr_batch_lock);
1545
1546         was_enabled = enabled;
1547         was_batchmode = batchmode;
1548
1549         batchsize = BATCH_SIZE_DEFAULT;
1550         batchtime = BATCH_TIME_DEFAULT;
1551         batchscheduleronly = BATCH_SCHEDULER_ONLY_DEFAULT;
1552         batchsafeshutdown = BATCH_SAFE_SHUTDOWN_DEFAULT;
1553         enabled = ENABLED_DEFAULT;
1554         batchmode = BATCHMODE_DEFAULT;
1555         unanswered = UNANSWERED_DEFAULT;
1556         congestion = CONGESTION_DEFAULT;
1557
1558         if (config == CONFIG_STATUS_FILEMISSING || config == CONFIG_STATUS_FILEINVALID) {
1559                 ast_mutex_unlock(&cdr_batch_lock);
1560                 return 0;
1561         }
1562
1563         /* don't run the next scheduled CDR posting while reloading */
1564         AST_SCHED_DEL(sched, cdr_sched);
1565
1566         for (v = ast_variable_browse(config, "general"); v; v = v->next) {
1567                 if (!strcasecmp(v->name, "enable")) {
1568                         enabled = ast_true(v->value);
1569                 } else if (!strcasecmp(v->name, "unanswered")) {
1570                         unanswered = ast_true(v->value);
1571                 } else if (!strcasecmp(v->name, "congestion")) {
1572                         congestion = ast_true(v->value);
1573                 } else if (!strcasecmp(v->name, "batch")) {
1574                         batchmode = ast_true(v->value);
1575                 } else if (!strcasecmp(v->name, "scheduleronly")) {
1576                         batchscheduleronly = ast_true(v->value);
1577                 } else if (!strcasecmp(v->name, "safeshutdown")) {
1578                         batchsafeshutdown = ast_true(v->value);
1579                 } else if (!strcasecmp(v->name, "size")) {
1580                         if (sscanf(v->value, "%30d", &cfg_size) < 1) {
1581                                 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", v->value);
1582                         } else if (cfg_size < 0) {
1583                                 ast_log(LOG_WARNING, "Invalid maximum batch size '%d' specified, using default\n", cfg_size);
1584                         } else {
1585                                 batchsize = cfg_size;
1586                         }
1587                 } else if (!strcasecmp(v->name, "time")) {
1588                         if (sscanf(v->value, "%30d", &cfg_time) < 1) {
1589                                 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", v->value);
1590                         } else if (cfg_time < 0) {
1591                                 ast_log(LOG_WARNING, "Invalid maximum batch time '%d' specified, using default\n", cfg_time);
1592                         } else {
1593                                 batchtime = cfg_time;
1594                         }
1595                 } else if (!strcasecmp(v->name, "endbeforehexten")) {
1596                         ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_END_CDR_BEFORE_H_EXTEN);
1597                 } else if (!strcasecmp(v->name, "initiatedseconds")) {
1598                         ast_set2_flag(&ast_options, ast_true(v->value), AST_OPT_FLAG_INITIATED_SECONDS);
1599                 }
1600         }
1601
1602         if (enabled && !batchmode) {
1603                 ast_log(LOG_NOTICE, "CDR simple logging enabled.\n");
1604         } else if (enabled && batchmode) {
1605                 cdr_sched = ast_sched_add(sched, batchtime * 1000, submit_scheduled_batch, NULL);
1606                 ast_log(LOG_NOTICE, "CDR batch mode logging enabled, first of either size %d or time %d seconds.\n", batchsize, batchtime);
1607         } else {
1608                 ast_log(LOG_NOTICE, "CDR logging disabled, data will be lost.\n");
1609         }
1610
1611         /* if this reload enabled the CDR batch mode, create the background thread
1612            if it does not exist */
1613         if (enabled && batchmode && (!was_enabled || !was_batchmode) && (cdr_thread == AST_PTHREADT_NULL)) {
1614                 ast_cond_init(&cdr_pending_cond, NULL);
1615                 if (ast_pthread_create_background(&cdr_thread, NULL, do_cdr, NULL) < 0) {
1616                         ast_log(LOG_ERROR, "Unable to start CDR thread.\n");
1617                         AST_SCHED_DEL(sched, cdr_sched);
1618                 } else {
1619                         ast_cli_register(&cli_submit);
1620                         ast_register_atexit(ast_cdr_engine_term);
1621                         res = 0;
1622                 }
1623         /* if this reload disabled the CDR and/or batch mode and there is a background thread,
1624            kill it */
1625         } else if (((!enabled && was_enabled) || (!batchmode && was_batchmode)) && (cdr_thread != AST_PTHREADT_NULL)) {
1626                 /* wake up the thread so it will exit */
1627                 pthread_cancel(cdr_thread);
1628                 pthread_kill(cdr_thread, SIGURG);
1629                 pthread_join(cdr_thread, NULL);
1630                 cdr_thread = AST_PTHREADT_NULL;
1631                 ast_cond_destroy(&cdr_pending_cond);
1632                 ast_cli_unregister(&cli_submit);
1633                 ast_unregister_atexit(ast_cdr_engine_term);
1634                 res = 0;
1635                 /* if leaving batch mode, then post the CDRs in the batch,
1636                    and don't reschedule, since we are stopping CDR logging */
1637                 if (!batchmode && was_batchmode) {
1638                         ast_cdr_engine_term();
1639                 }
1640         } else {
1641                 res = 0;
1642         }
1643
1644         ast_mutex_unlock(&cdr_batch_lock);
1645         ast_config_destroy(config);
1646         manager_event(EVENT_FLAG_SYSTEM, "Reload", "Module: CDR\r\nMessage: CDR subsystem reload requested\r\n");
1647
1648         return res;
1649 }
1650
1651 int ast_cdr_engine_init(void)
1652 {
1653         int res;
1654
1655         sched = ast_sched_context_create();
1656         if (!sched) {
1657                 ast_log(LOG_ERROR, "Unable to create schedule context.\n");
1658                 return -1;
1659         }
1660
1661         ast_cli_register(&cli_status);
1662
1663         res = do_reload(0);
1664         if (res) {
1665                 ast_mutex_lock(&cdr_batch_lock);
1666                 res = init_batch();
1667                 ast_mutex_unlock(&cdr_batch_lock);
1668         }
1669
1670         return res;
1671 }
1672
1673 /* \note This actually gets called a couple of times at shutdown.  Once, before we start
1674    hanging up channels, and then again, after the channel hangup timeout expires */
1675 void ast_cdr_engine_term(void)
1676 {
1677         ast_cdr_submit_batch(batchsafeshutdown);
1678 }
1679
1680 int ast_cdr_engine_reload(void)
1681 {
1682         return do_reload(1);
1683 }
1684
1685 int ast_cdr_data_add_structure(struct ast_data *tree, struct ast_cdr *cdr, int recur)
1686 {
1687         struct ast_cdr *tmpcdr;
1688         struct ast_data *level;
1689         struct ast_var_t *variables;
1690         const char *var, *val;
1691         int x = 1, i;
1692         char workspace[256];
1693         char *tmp;
1694
1695         if (!cdr) {
1696                 return -1;
1697         }
1698
1699         for (tmpcdr = cdr; tmpcdr; tmpcdr = (recur ? tmpcdr->next : NULL)) {
1700                 level = ast_data_add_node(tree, "level");
1701                 if (!level) {
1702                         continue;
1703                 }
1704
1705                 ast_data_add_int(level, "level_number", x);
1706
1707                 AST_LIST_TRAVERSE(&tmpcdr->varshead, variables, entries) {
1708                         if (variables && (var = ast_var_name(variables)) &&
1709                                         (val = ast_var_value(variables)) && !ast_strlen_zero(var)
1710                                         && !ast_strlen_zero(val)) {
1711                                 ast_data_add_str(level, var, val);
1712                         } else {
1713                                 break;
1714                         }
1715                 }
1716
1717                 for (i = 0; cdr_readonly_vars[i]; i++) {
1718                         workspace[0] = 0; /* null out the workspace, because the cdr_get_tv() won't write anything if time is NULL, so you get old vals */
1719                         ast_cdr_getvar(tmpcdr, cdr_readonly_vars[i], &tmp, workspace, sizeof(workspace), 0, 0);
1720                         if (!tmp) {
1721                                 continue;
1722                         }
1723                         ast_data_add_str(level, cdr_readonly_vars[i], tmp);
1724                 }
1725
1726                 x++;
1727         }
1728
1729         return 0;
1730 }
1731