cdr.c 49 KB

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