cdr.c 51 KB

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