bdb_lib.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475
  1. /*
  2. * $Id: bdb_lib.c 4585 2008-08-06 08:20:30Z klaus_darilion $
  3. *
  4. * db_berkeley module, portions of this code were templated using
  5. * the dbtext and postgres modules.
  6. * Copyright (C) 2007 Cisco Systems
  7. *
  8. * This file is part of SIP-router, a free SIP server.
  9. *
  10. * SIP-router is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * SIP-router is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * History:
  25. * --------
  26. * 2007-09-19 genesis (wiquan)
  27. */
  28. /*! \file
  29. * Berkeley DB : Library
  30. *
  31. * \ingroup database
  32. */
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include <sys/types.h>
  36. #include <dirent.h>
  37. #include <errno.h>
  38. #include "../../ut.h"
  39. #include "../../mem/mem.h"
  40. #include "../../dprint.h"
  41. #include "bdb_fld.h"
  42. #include "bdb_lib.h"
  43. static bdb_params_p _bdb_parms = NULL;
  44. /**
  45. *
  46. */
  47. int bdblib_init(bdb_params_p _p)
  48. {
  49. bdb_params_p dp = NULL;
  50. if (_bdb_parms != NULL)
  51. return 0;
  52. /*create default parms*/
  53. dp = (bdb_params_p) pkg_malloc( sizeof(bdb_params_t) );
  54. if (dp==NULL)
  55. {
  56. ERR("not enough private memory\n");
  57. return -1;
  58. }
  59. if(_p!=NULL)
  60. {
  61. dp->cache_size = _p->cache_size;
  62. dp->auto_reload = _p->auto_reload;
  63. dp->log_enable = _p->log_enable;
  64. dp->journal_roll_interval = _p->journal_roll_interval;
  65. } else {
  66. dp->cache_size = (4 * 1024 * 1024); //4Mb
  67. dp->auto_reload = 0;
  68. dp->log_enable = 0;
  69. dp->journal_roll_interval = 3600;
  70. }
  71. _bdb_parms = dp;
  72. return 0;
  73. }
  74. /**
  75. * close all DBs and then the DBENV; free all memory
  76. */
  77. int bdblib_destroy(void)
  78. {
  79. if(_bdb_parms) pkg_free(_bdb_parms);
  80. return 0;
  81. }
  82. /** closes the underlying Berkeley DB.
  83. assumes the lib data-structures are already initialzed;
  84. used to sync and reload the db file.
  85. */
  86. int bdblib_close(bdb_db_p _db_p, str *dirpath)
  87. {
  88. int rc;
  89. bdb_tcache_p _tbc;
  90. DB* _db = NULL;
  91. DB_ENV* _env = NULL;
  92. if (_db_p==NULL || dirpath==NULL)
  93. return -1;
  94. rc = 0;
  95. if (_db_p==NULL)
  96. {
  97. DBG("DB not found %.*s \n", dirpath->len, dirpath->s);
  98. return 1; /*table not found*/
  99. }
  100. _env = _db_p->dbenv;
  101. _tbc = _db_p->tables;
  102. DBG("ENV %.*s \n", _db_p->name.len, _db_p->name.s);
  103. if(dirpath->len == _db_p->name.len &&
  104. !strncasecmp(dirpath->s, _db_p->name.s, _db_p->name.len))
  105. {
  106. //close the whole dbenv
  107. DBG("ENV %.*s \n", dirpath->len, dirpath->s);
  108. while(_tbc)
  109. {
  110. if(_tbc->dtp)
  111. {
  112. _db = _tbc->dtp->db;
  113. if(_db)
  114. rc = _db->close(_db, 0);
  115. if(rc != 0)
  116. ERR("error closing %s\n", _tbc->dtp->name.s);
  117. _tbc->dtp->db = NULL;
  118. }
  119. _tbc = _tbc->next;
  120. }
  121. _env->close(_env, 0);
  122. _db_p->dbenv = NULL;
  123. return 0;
  124. }
  125. //close a particular db
  126. while(_tbc)
  127. {
  128. if(_tbc->dtp)
  129. {
  130. DBG("checking DB %.*s \n", _tbc->dtp->name.len, _tbc->dtp->name.s);
  131. if(_tbc->dtp->name.len == dirpath->len &&
  132. !strncasecmp(_tbc->dtp->name.s, dirpath->s, dirpath->len ))
  133. {
  134. DBG("DB %.*s \n", dirpath->len, dirpath->s);
  135. _db = _tbc->dtp->db;
  136. if(_db)
  137. rc = _db->close(_db, 0);
  138. if(rc != 0)
  139. ERR("error closing %s\n", _tbc->dtp->name.s);
  140. _tbc->dtp->db = NULL;
  141. return 0;
  142. }
  143. }
  144. _tbc = _tbc->next;
  145. }
  146. DBG("DB not found %.*s \n", dirpath->len, dirpath->s);
  147. return 1; /*table not found*/
  148. }
  149. /** opens the underlying Berkeley DB.
  150. assumes the lib data-structures are already initialzed;
  151. used to sync and reload the db file.
  152. */
  153. int bdblib_reopen(bdb_db_p _db_p, str *dirpath)
  154. {
  155. int rc, flags;
  156. bdb_tcache_p _tbc;
  157. DB* _db = NULL;
  158. DB_ENV* _env = NULL;
  159. rc = flags = 0;
  160. _tbc = NULL;
  161. if (_db_p==NULL || dirpath==NULL)
  162. return -1;
  163. if (_db_p)
  164. {
  165. DBG("bdb: DB not found %.*s \n", dirpath->len, dirpath->s);
  166. return 1; /*table not found*/
  167. }
  168. _env = _db_p->dbenv;
  169. _tbc = _db_p->tables;
  170. if(dirpath->len ==_db_p->name.len &&
  171. !strncasecmp(dirpath->s, _db_p->name.s, _db_p->name.len))
  172. {
  173. //open the whole dbenv
  174. DBG("-- bdblib_reopen ENV %.*s \n", dirpath->len, dirpath->s);
  175. if(!_db_p->dbenv)
  176. {
  177. rc = bdblib_create_dbenv(&_env, dirpath->s);
  178. _db_p->dbenv = _env;
  179. }
  180. if(rc!=0) return rc;
  181. _env = _db_p->dbenv;
  182. _tbc = _db_p->tables;
  183. while(_tbc)
  184. {
  185. if(_tbc->dtp)
  186. {
  187. if(!_tbc->dtp->db)
  188. {
  189. if ((rc = db_create(&_db, _env, 0)) != 0)
  190. {
  191. _env->err(_env, rc, "db_create");
  192. ERR("error in db_create, db error: %s.\n",
  193. db_strerror(rc));
  194. bdblib_recover(_tbc->dtp, rc);
  195. }
  196. }
  197. if ((rc = _db->open(_db, NULL, dirpath->s, NULL, DB_HASH,
  198. DB_CREATE, 0664)) != 0)
  199. {
  200. _db->dbenv->err(_env, rc, "DB->open: %s", dirpath->s);
  201. ERR("error in db_open: %s.\n",db_strerror(rc));
  202. bdblib_recover(_tbc->dtp, rc);
  203. }
  204. _tbc->dtp->db = _db;
  205. }
  206. _tbc = _tbc->next;
  207. }
  208. _env->close(_env, 0);
  209. return rc;
  210. }
  211. // open a particular db
  212. while(_tbc)
  213. {
  214. if(_tbc->dtp)
  215. {
  216. ERR("checking DB %.*s \n", _tbc->dtp->name.len, _tbc->dtp->name.s);
  217. if(_tbc->dtp->name.len == dirpath->len &&
  218. !strncasecmp(_tbc->dtp->name.s, dirpath->s, dirpath->len ))
  219. {
  220. ERR("DB %.*s \n", dirpath->len, dirpath->s);
  221. if(!_tbc->dtp->db)
  222. {
  223. if ((rc = db_create(&_db, _env, 0)) != 0)
  224. {
  225. _env->err(_env, rc, "db_create");
  226. ERR("error in db_create, db error: %s.\n",
  227. db_strerror(rc));
  228. bdblib_recover(_tbc->dtp, rc);
  229. }
  230. }
  231. if ((rc = _db->open(_db, NULL, dirpath->s, NULL, DB_HASH,
  232. DB_CREATE, 0664)) != 0)
  233. {
  234. _db->dbenv->err(_env, rc, "DB->open: %s", dirpath->s);
  235. ERR("bdb open: %s.\n",db_strerror(rc));
  236. bdblib_recover(_tbc->dtp, rc);
  237. }
  238. _tbc->dtp->db = _db;
  239. return rc;
  240. }
  241. }
  242. _tbc = _tbc->next;
  243. }
  244. DBG("DB not found %.*s \n", dirpath->len, dirpath->s);
  245. return 1; /*table not found*/
  246. }
  247. /**
  248. *
  249. */
  250. int bdblib_create_dbenv(DB_ENV **_dbenv, char* _home)
  251. {
  252. DB_ENV *env;
  253. char *progname;
  254. int rc, flags;
  255. progname = "kamailio";
  256. /* Create an environment and initialize it for additional error * reporting. */
  257. if ((rc = db_env_create(&env, 0)) != 0)
  258. {
  259. ERR("db_env_create failed! bdb error: %s.\n", db_strerror(rc));
  260. return (rc);
  261. }
  262. env->set_errpfx(env, progname);
  263. /* Specify the shared memory buffer pool cachesize */
  264. if ((rc = env->set_cachesize(env, 0, _bdb_parms->cache_size, 0)) != 0)
  265. {
  266. ERR("dbenv set_cachsize failed! bdb error: %s.\n", db_strerror(rc));
  267. env->err(env, rc, "set_cachesize");
  268. goto err;
  269. }
  270. /* Concurrent Data Store flags */
  271. flags = DB_CREATE |
  272. DB_INIT_CDB |
  273. DB_INIT_MPOOL |
  274. DB_THREAD;
  275. /* Transaction Data Store flags ; not supported yet */
  276. /*
  277. flags = DB_CREATE |
  278. DB_RECOVER |
  279. DB_INIT_LOG |
  280. DB_INIT_LOCK |
  281. DB_INIT_MPOOL |
  282. DB_THREAD |
  283. DB_INIT_TXN;
  284. */
  285. /* Open the environment */
  286. if ((rc = env->open(env, _home, flags, 0)) != 0)
  287. {
  288. ERR("dbenv is not initialized! bdb error: %s.\n",db_strerror(rc));
  289. env->err(env, rc, "environment open: %s", _home);
  290. goto err;
  291. }
  292. *_dbenv = env;
  293. return (0);
  294. err: (void)env->close(env, 0);
  295. return (rc);
  296. }
  297. /**
  298. */
  299. bdb_db_p bdblib_get_db(str *dirpath)
  300. {
  301. int rc;
  302. bdb_db_p _db_p=NULL;
  303. if(dirpath==0 || dirpath->s==NULL || dirpath->s[0]=='\0')
  304. return NULL;
  305. if(_bdb_parms==NULL)
  306. {
  307. ERR("bdb: cache is not initialized! Check if you loaded bdb "
  308. "before any other module that uses it.\n");
  309. return NULL;
  310. }
  311. if(!bdb_is_database(dirpath->s))
  312. {
  313. ERR("bdb: database [%.*s] does not exists!\n",
  314. dirpath->len , dirpath->s);
  315. return NULL;
  316. }
  317. _db_p = (bdb_db_p)pkg_malloc(sizeof(bdb_db_t));
  318. if(!_db_p)
  319. {
  320. ERR("no private memory for dbenv_t.\n");
  321. pkg_free(_db_p);
  322. return NULL;
  323. }
  324. _db_p->name.s = (char*)pkg_malloc(dirpath->len*sizeof(char));
  325. memcpy(_db_p->name.s, dirpath->s, dirpath->len);
  326. _db_p->name.len = dirpath->len;
  327. if ((rc = bdblib_create_dbenv(&(_db_p->dbenv), dirpath->s)) != 0)
  328. {
  329. ERR("bdblib_create_dbenv failed");
  330. pkg_free(_db_p->name.s);
  331. pkg_free(_db_p);
  332. return NULL;
  333. }
  334. _db_p->tables=NULL;
  335. return _db_p;
  336. }
  337. /**
  338. * look thru a linked list for the table. if dne, create a new one
  339. * and add to the list
  340. */
  341. bdb_tcache_p bdblib_get_table(bdb_db_t *_db, str *_s)
  342. {
  343. bdb_tcache_p _tbc = NULL;
  344. bdb_table_p _tp = NULL;
  345. if(!_db || !_s || !_s->s || _s->len<=0)
  346. return NULL;
  347. if(!_db->dbenv)
  348. {
  349. return NULL;
  350. }
  351. _tbc = _db->tables;
  352. while(_tbc)
  353. {
  354. if(_tbc->dtp)
  355. {
  356. if(_tbc->dtp->name.len == _s->len
  357. && !strncasecmp(_tbc->dtp->name.s, _s->s, _s->len ))
  358. {
  359. return _tbc;
  360. }
  361. }
  362. _tbc = _tbc->next;
  363. }
  364. _tbc = (bdb_tcache_p)pkg_malloc(sizeof(bdb_tcache_t));
  365. if(!_tbc)
  366. return NULL;
  367. _tp = bdblib_create_table(_db, _s);
  368. if(!_tp)
  369. {
  370. ERR("failed to create table.\n");
  371. pkg_free(_tbc);
  372. return NULL;
  373. }
  374. _tbc->dtp = _tp;
  375. if(_db->tables)
  376. (_db->tables)->prev = _tbc;
  377. _tbc->next = _db->tables;
  378. _db->tables = _tbc;
  379. return _tbc;
  380. }
  381. void bdblib_log(int op, bdb_db_p _db_p, bdb_table_p _tp, char* _msg, int len)
  382. {
  383. if(!_tp || !len) return;
  384. if(! _bdb_parms->log_enable) return;
  385. if (_tp->logflags == JLOG_NONE) return;
  386. if ((_tp->logflags & op) == op)
  387. { int op_len=7;
  388. char buf[MAX_ROW_SIZE + op_len];
  389. char *c;
  390. time_t now = time(NULL);
  391. if( _bdb_parms->journal_roll_interval)
  392. {
  393. if((_tp->t) && (now - _tp->t) > _bdb_parms->journal_roll_interval)
  394. { /*try to roll logfile*/
  395. if(bdblib_create_journal(_db_p, _tp))
  396. {
  397. ERR("Journaling has FAILED !\n");
  398. return;
  399. }
  400. }
  401. }
  402. c = buf;
  403. switch (op)
  404. {
  405. case JLOG_INSERT:
  406. strncpy(c, "INSERT|", op_len);
  407. break;
  408. case JLOG_UPDATE:
  409. strncpy(c, "UPDATE|", op_len);
  410. break;
  411. case JLOG_DELETE:
  412. strncpy(c, "DELETE|", op_len);
  413. break;
  414. }
  415. c += op_len;
  416. strncpy(c, _msg, len);
  417. c +=len;
  418. *c = '\n';
  419. c++;
  420. *c = '\0';
  421. if ((_tp->logflags & JLOG_STDOUT) == JLOG_STDOUT)
  422. puts(buf);
  423. if ((_tp->logflags & JLOG_SYSLOG) == JLOG_SYSLOG)
  424. syslog(LOG_LOCAL6, "%s", buf);
  425. if(_tp->fp)
  426. {
  427. if(!fputs(buf, _tp->fp) )
  428. fflush(_tp->fp);
  429. }
  430. }
  431. }
  432. /**
  433. * The function is called to create a handle to a db table.
  434. *
  435. * On startup, we do not create any of the db handles.
  436. * Instead it is done on first-use (lazy-initialized) to only create handles to
  437. * files (db) that we require.
  438. *
  439. * There is one db file per kamailio table (eg. acc), and they should exist
  440. * in your DB_PATH (refer to kamctlrc) directory.
  441. *
  442. * This function does _not_ create the underlying binary db tables.
  443. * Creating the tables MUST be manually performed before
  444. * kamailio startup by 'kamdbctl create'
  445. *
  446. * Function returns NULL on error, which will cause kamailio to exit.
  447. *
  448. */
  449. bdb_table_p bdblib_create_table(bdb_db_p _db, str *_s)
  450. {
  451. int rc,i,flags;
  452. DB *bdb = NULL;
  453. bdb_table_p tp = NULL;
  454. char tblname[MAX_TABLENAME_SIZE];
  455. if(!_db || !_db->dbenv)
  456. {
  457. ERR("no bdb_db_p or dbenv.\n");
  458. return NULL;
  459. }
  460. tp = (bdb_table_p)pkg_malloc(sizeof(bdb_table_t));
  461. if(!tp)
  462. {
  463. ERR("no private memory for bdb_table_t.\n");
  464. return NULL;
  465. }
  466. if ((rc = db_create(&bdb, _db->dbenv, 0)) != 0)
  467. {
  468. _db->dbenv->err(_db->dbenv, rc, "database create");
  469. ERR("error in db_create, bdb error: %s.\n",db_strerror(rc));
  470. pkg_free(tp);
  471. return NULL;
  472. }
  473. memset(tblname, 0, MAX_TABLENAME_SIZE);
  474. strncpy(tblname, _s->s, _s->len);
  475. flags = DB_THREAD;
  476. if ((rc = bdb->open(bdb, NULL, tblname, NULL, DB_HASH, flags, 0664)) != 0)
  477. {
  478. _db->dbenv->err(_db->dbenv, rc, "DB->open: %s", tblname);
  479. ERR("bdb open failed: %s.\n",db_strerror(rc));
  480. pkg_free(tp);
  481. return NULL;
  482. }
  483. tp->name.s = (char*)pkg_malloc(_s->len*sizeof(char));
  484. memcpy(tp->name.s, _s->s, _s->len);
  485. tp->name.len = _s->len;
  486. tp->db=bdb;
  487. tp->ncols=0;
  488. tp->nkeys=0;
  489. tp->ro=0; /*0=ReadWrite ; 1=ReadOnly*/
  490. tp->ino=0; /*inode*/
  491. tp->logflags=0; /*bitmap; 4=Delete, 2=Update, 1=Insert, 0=None*/
  492. tp->fp=0;
  493. tp->t=0;
  494. for(i=0;i<MAX_NUM_COLS;i++)
  495. tp->colp[i] = NULL;
  496. /*load metadata; seeded\db_loaded when database are created*/
  497. /*initialize columns with metadata*/
  498. rc = load_metadata_columns(tp);
  499. if(rc!=0)
  500. {
  501. ERR("FAILED to load METADATA COLS in table: %s.\n", tblname);
  502. goto error;
  503. }
  504. /*initialize columns default values from metadata*/
  505. rc = load_metadata_defaults(tp);
  506. if(rc!=0)
  507. {
  508. ERR("FAILED to load METADATA DEFAULTS in table: %s.\n", tblname);
  509. goto error;
  510. }
  511. rc = load_metadata_keys(tp);
  512. if(rc!=0)
  513. {
  514. ERR("FAILED to load METADATA KEYS in table: %s.\n", tblname);
  515. /*will have problems later figuring column types*/
  516. goto error;
  517. }
  518. /*opened RW by default; Query to set the RO flag */
  519. rc = load_metadata_readonly(tp);
  520. if(rc!=0)
  521. {
  522. INFO("No METADATA_READONLY in table: %s.\n", tblname);
  523. /*non-critical; table will default to READWRITE*/
  524. }
  525. if(tp->ro)
  526. {
  527. /*schema defines this table RO readonly*/
  528. if ((rc = bdb->close(bdb,0)) != 0)
  529. {
  530. _db->dbenv->err(_db->dbenv, rc, "DB->close: %s", tblname);
  531. ERR("bdb close: %s.\n",db_strerror(rc));
  532. goto error;
  533. }
  534. bdb = NULL;
  535. if ((rc = db_create(&bdb, _db->dbenv, 0)) != 0)
  536. {
  537. _db->dbenv->err(_db->dbenv, rc, "database create");
  538. ERR("error in db_create.\n");
  539. goto error;
  540. }
  541. flags = DB_THREAD | DB_RDONLY;
  542. if ((rc = bdb->open(bdb, NULL, tblname, NULL, DB_HASH, flags, 0664)) != 0)
  543. {
  544. _db->dbenv->err(_db->dbenv, rc, "DB->open: %s", tblname);
  545. ERR("bdb open: %s.\n",db_strerror(rc));
  546. goto error;
  547. }
  548. tp->db=bdb;
  549. }
  550. /* set the journaling flags; flags indicate which operations
  551. need to be journalled. (e.g possible to only journal INSERT.)
  552. */
  553. rc = load_metadata_logflags(tp);
  554. if(rc!=0)
  555. INFO("No METADATA_LOGFLAGS in table: %s.\n", tblname);
  556. if ((tp->logflags & JLOG_FILE) == JLOG_FILE)
  557. bdblib_create_journal(_db, tp);
  558. return tp;
  559. error:
  560. if(tp)
  561. {
  562. pkg_free(tp->name.s);
  563. pkg_free(tp);
  564. }
  565. return NULL;
  566. }
  567. int bdblib_create_journal(bdb_db_p _db_p, bdb_table_p _tp)
  568. {
  569. char *s;
  570. char fn[1024];
  571. char d[64];
  572. FILE *fp = NULL;
  573. struct tm *t;
  574. int bl;
  575. time_t tim = time(NULL);
  576. if(! _db_p || ! _tp) return -1;
  577. if(! _bdb_parms->log_enable) return 0;
  578. /* journal filename ; e.g. '/var/kamailio/db/location-YYYYMMDDhhmmss.jnl' */
  579. s=fn;
  580. strncpy(s, _db_p->name.s, _db_p->name.len);
  581. s+=_db_p->name.len;
  582. *s = '/';
  583. s++;
  584. strncpy(s, _tp->name.s, _tp->name.len);
  585. s+=_tp->name.len;
  586. t = localtime( &tim );
  587. bl=strftime(d,128,"-%Y%m%d%H%M%S.jnl",t);
  588. strncpy(s, d, bl);
  589. s+= bl;
  590. *s = 0;
  591. if(_tp->fp)
  592. { /* must be rolling. */
  593. if( fclose(_tp->fp) )
  594. { ERR("Failed to Close Log in table: %.*s .\n", _tp->name.len,
  595. _tp->name.s);
  596. return -1;
  597. }
  598. }
  599. if( (fp = fopen(fn, "w")) != NULL )
  600. {
  601. _tp->fp = fp;
  602. }
  603. else
  604. {
  605. ERR("Failed to Open Log in table: %.*s .\n",_tp->name.len, _tp->name.s);
  606. return -1;
  607. }
  608. _tp->t = tim;
  609. return 0;
  610. }
  611. int load_metadata_columns(bdb_table_p _tp)
  612. {
  613. int ret,n,len;
  614. char dbuf[MAX_ROW_SIZE];
  615. char *s = NULL;
  616. char cn[64], ct[16];
  617. DB *db = NULL;
  618. DBT key, data;
  619. bdb_col_p col;
  620. ret = n = len = 0;
  621. if(!_tp || !_tp->db)
  622. return -1;
  623. if(_tp->ncols!=0)
  624. return 0;
  625. db = _tp->db;
  626. memset(&key, 0, sizeof(DBT));
  627. memset(&data, 0, sizeof(DBT));
  628. memset(dbuf, 0, MAX_ROW_SIZE);
  629. key.data = METADATA_COLUMNS;
  630. key.size = strlen(METADATA_COLUMNS);
  631. /*memory for the result*/
  632. data.data = dbuf;
  633. data.ulen = MAX_ROW_SIZE;
  634. data.flags = DB_DBT_USERMEM;
  635. if ((ret = db->get(db, NULL, &key, &data, 0)) != 0)
  636. {
  637. db->err(db, ret, "load_metadata_columns DB->get failed");
  638. ERR("FAILED to find METADATA_COLUMNS in DB \n");
  639. return -1;
  640. }
  641. /* eg: dbuf = "bdb_table_name(str) bdb_table_version(int)" */
  642. s = strtok(dbuf, " ");
  643. while(s!=NULL && n<MAX_NUM_COLS)
  644. {
  645. /* eg: meta[0]=table_name meta[1]=str */
  646. sscanf(s,"%20[^(](%10[^)])[^\n]", cn, ct);
  647. /* create column*/
  648. col = (bdb_col_p) pkg_malloc(sizeof(bdb_col_t));
  649. if(!col)
  650. { ERR("out of private memory \n");
  651. return -1;
  652. }
  653. /* set name*/
  654. len = strlen( cn );
  655. col->name.s = (char*)pkg_malloc(len * sizeof(char));
  656. memcpy(col->name.s, cn, len );
  657. col->name.len = len;
  658. /*set column type*/
  659. if(strncmp(ct, "str", 3)==0)
  660. { col->type = DB_STR;
  661. }
  662. else if(strncmp(ct, "int", 3)==0)
  663. { col->type = DB_INT;
  664. }
  665. else if(strncmp(ct, "double", 6)==0)
  666. { col->type = DB_DOUBLE;
  667. }
  668. else if(strncmp(ct, "datetime", 8)==0)
  669. { col->type = DB_DATETIME;
  670. }
  671. else
  672. { col->type = DB_STR;
  673. }
  674. col->flag = 0;
  675. _tp->colp[n] = col;
  676. n++;
  677. _tp->ncols++;
  678. s=strtok(NULL, " ");
  679. }
  680. return 0;
  681. }
  682. int load_metadata_keys(bdb_table_p _tp)
  683. {
  684. int ret,n,ci;
  685. char dbuf[MAX_ROW_SIZE];
  686. char *s = NULL;
  687. DB *db = NULL;
  688. DBT key, data;
  689. ret = n = ci = 0;
  690. if(!_tp || !_tp->db)
  691. return -1;
  692. db = _tp->db;
  693. memset(&key, 0, sizeof(DBT));
  694. memset(&data, 0, sizeof(DBT));
  695. memset(dbuf, 0, MAX_ROW_SIZE);
  696. key.data = METADATA_KEY;
  697. key.size = strlen(METADATA_KEY);
  698. data.data = dbuf;
  699. data.ulen = MAX_ROW_SIZE;
  700. data.flags = DB_DBT_USERMEM;
  701. if ((ret = db->get(db, NULL, &key, &data, 0)) != 0)
  702. {
  703. db->err(db, ret, "load_metadata_keys DB->get failed");
  704. ERR("FAILED to find METADATA in table \n");
  705. return ret;
  706. }
  707. s = strtok(dbuf, " ");
  708. while(s!=NULL && n< _tp->ncols)
  709. { ret = sscanf(s,"%i", &ci);
  710. if(ret != 1) return -1;
  711. if( _tp->colp[ci] )
  712. { _tp->colp[ci]->flag = 1;
  713. _tp->nkeys++;
  714. }
  715. n++;
  716. s=strtok(NULL, " ");
  717. }
  718. return 0;
  719. }
  720. int load_metadata_readonly(bdb_table_p _tp)
  721. {
  722. int i, ret;
  723. char dbuf[MAX_ROW_SIZE];
  724. DB *db = NULL;
  725. DBT key, data;
  726. i = 0;
  727. if(!_tp || !_tp->db)
  728. return -1;
  729. db = _tp->db;
  730. memset(&key, 0, sizeof(DBT));
  731. memset(&data, 0, sizeof(DBT));
  732. memset(dbuf, 0, MAX_ROW_SIZE);
  733. key.data = METADATA_READONLY;
  734. key.size = strlen(METADATA_READONLY);
  735. data.data = dbuf;
  736. data.ulen = MAX_ROW_SIZE;
  737. data.flags = DB_DBT_USERMEM;
  738. if ((ret = db->get(db, NULL, &key, &data, 0)) != 0)
  739. { return ret;
  740. }
  741. if( 1 == sscanf(dbuf,"%i", &i) )
  742. _tp->ro=(i>0)?1:0;
  743. return 0;
  744. }
  745. int load_metadata_logflags(bdb_table_p _tp)
  746. {
  747. int i, ret;
  748. char dbuf[MAX_ROW_SIZE];
  749. DB *db = NULL;
  750. DBT key, data;
  751. i = 0;
  752. if(!_tp || !_tp->db)
  753. return -1;
  754. db = _tp->db;
  755. memset(&key, 0, sizeof(DBT));
  756. memset(&data, 0, sizeof(DBT));
  757. memset(dbuf, 0, MAX_ROW_SIZE);
  758. key.data = METADATA_LOGFLAGS;
  759. key.size = strlen(METADATA_LOGFLAGS);
  760. data.data = dbuf;
  761. data.ulen = MAX_ROW_SIZE;
  762. data.flags = DB_DBT_USERMEM;
  763. if ((ret = db->get(db, NULL, &key, &data, 0)) != 0)
  764. { return ret;
  765. }
  766. if( 1 == sscanf(dbuf,"%i", &i) )
  767. _tp->logflags=i;
  768. return 0;
  769. }
  770. int load_metadata_defaults(bdb_table_p _tp)
  771. {
  772. int ret,n,len;
  773. char dbuf[MAX_ROW_SIZE];
  774. char *s = NULL;
  775. char cv[64];
  776. DB *db = NULL;
  777. DBT key, data;
  778. bdb_col_p col;
  779. ret = n = len = 0;
  780. if(!_tp || !_tp->db)
  781. return -1;
  782. db = _tp->db;
  783. memset(&key, 0, sizeof(DBT));
  784. memset(&data, 0, sizeof(DBT));
  785. memset(dbuf, 0, MAX_ROW_SIZE);
  786. key.data = METADATA_DEFAULTS;
  787. key.size = strlen(METADATA_DEFAULTS);
  788. /*memory for the result*/
  789. data.data = dbuf;
  790. data.ulen = MAX_ROW_SIZE;
  791. data.flags = DB_DBT_USERMEM;
  792. if ((ret = db->get(db, NULL, &key, &data, 0)) != 0)
  793. {
  794. /*no defaults in DB; make some up.*/
  795. for(n=0; n<_tp->ncols; n++)
  796. {
  797. col = _tp->colp[n];
  798. if( col )
  799. { /*set all columns default value to 'NULL' */
  800. len = strlen("NULL");
  801. col->dv.s = (char*)pkg_malloc(len * sizeof(char));
  802. memcpy(col->dv.s, "NULL", len);
  803. col->dv.len = len;
  804. }
  805. }
  806. return 0;
  807. }
  808. /* use the defaults provided*/
  809. s = strtok(dbuf, DELIM);
  810. while(s!=NULL && n< _tp->ncols)
  811. { ret = sscanf(s,"%s", cv);
  812. if(ret != 1) return -1;
  813. col = _tp->colp[n];
  814. if( col )
  815. { /*set column default*/
  816. len = strlen(s);
  817. col->dv.s = (char*)pkg_malloc(len * sizeof(char));
  818. memcpy(col->dv.s, cv, len);
  819. col->dv.len = len;
  820. }
  821. n++;
  822. s=strtok(NULL, DELIM);
  823. }
  824. return 0;
  825. }
  826. inline int bdb_int2str(int _v, char* _s, int* _l)
  827. {
  828. int ret;
  829. if ((!_s) || (!_l) || (!*_l)) {
  830. ERR("Invalid parameter value\n");
  831. return -1;
  832. }
  833. ret = snprintf(_s, *_l, "%-d", _v);
  834. if (ret < 0 || ret >= *_l) {
  835. ERR("Error in snprintf\n");
  836. return -1;
  837. }
  838. *_l = ret;
  839. return 0;
  840. }
  841. inline int bdb_double2str(double _v, char* _s, int* _l)
  842. {
  843. int ret;
  844. if ((!_s) || (!_l) || (!*_l)) {
  845. ERR("Invalid parameter value\n");
  846. return -1;
  847. }
  848. ret = snprintf(_s, *_l, "%-10.2f", _v);
  849. if (ret < 0 || ret >= *_l) {
  850. ERR("Error in snprintf\n");
  851. return -1;
  852. }
  853. *_l = ret;
  854. return 0;
  855. }
  856. inline int bdb_time2str(time_t _v, char* _s, int* _l)
  857. {
  858. struct tm* t;
  859. int l;
  860. if ((!_s) || (!_l) || (*_l < 2)) {
  861. ERR("Invalid parameter value\n");
  862. return -1;
  863. }
  864. *_s++ = '\'';
  865. /* Convert time_t structure to format accepted by the database */
  866. t = localtime(&_v);
  867. l = strftime(_s, *_l -1, "%Y-%m-%d %H:%M:%S", t);
  868. if (l == 0) {
  869. ERR("Error during time conversion\n");
  870. /* the value of _s is now unspecified */
  871. _s = NULL;
  872. _l = 0;
  873. return -1;
  874. }
  875. *_l = l;
  876. *(_s + l) = '\'';
  877. *_l = l + 2;
  878. return 0;
  879. }
  880. /*
  881. * Used when converting result from a query
  882. */
  883. int bdb_val2str(db_fld_t *fld, char *sout, int *slen)
  884. {
  885. int l;
  886. db_fld_val_t *val;
  887. if (fld->flags&DB_NULL)
  888. {
  889. *slen = snprintf(sout, *slen, "NULL");
  890. return 0;
  891. }
  892. val = &(fld->v);
  893. switch(fld->type)
  894. {
  895. case DB_INT:
  896. if (bdb_int2str(val->int4, sout, slen) < 0) {
  897. ERR("Error while converting int to string\n");
  898. return -2;
  899. } else {
  900. DBG("Converted int to string\n");
  901. return 0;
  902. }
  903. break;
  904. case DB_BITMAP:
  905. if (bdb_int2str(val->bitmap, sout, slen) < 0) {
  906. ERR("Error while converting bitmap to string\n");
  907. return -3;
  908. } else {
  909. DBG("Converted bitmap to string\n");
  910. return 0;
  911. }
  912. break;
  913. case DB_DOUBLE:
  914. if (bdb_double2str(val->dbl, sout, slen) < 0) {
  915. ERR("Error while converting double to string\n");
  916. return -3;
  917. } else {
  918. DBG("Converted double to string\n");
  919. return 0;
  920. }
  921. break;
  922. case DB_CSTR:
  923. l = strlen(val->cstr);
  924. if (*slen < l )
  925. {
  926. ERR("Destination buffer too short for string\n");
  927. return -4;
  928. } else {
  929. DBG("Converted string to string\n");
  930. strncpy(sout, val->cstr , l);
  931. sout[l] = 0;
  932. *slen = l;
  933. return 0;
  934. }
  935. break;
  936. case DB_STR:
  937. l = val->lstr.len;
  938. if (*slen < l)
  939. {
  940. ERR("Destination buffer too short for str\n");
  941. return -5;
  942. } else {
  943. DBG("Converted str to string\n");
  944. strncpy(sout, val->lstr.s , val->lstr.len);
  945. *slen = val->lstr.len;
  946. return 0;
  947. }
  948. break;
  949. case DB_DATETIME:
  950. if (bdb_time2str(val->time, sout, slen) < 0) {
  951. ERR("Error while converting time_t to string\n");
  952. return -6;
  953. } else {
  954. DBG("Converted time_t to string\n");
  955. return 0;
  956. }
  957. break;
  958. case DB_BLOB:
  959. l = val->blob.len;
  960. if (*slen < l)
  961. {
  962. ERR("Destination buffer too short for blob\n");
  963. return -7;
  964. } else {
  965. DBG("Converting BLOB [%s]\n", sout);
  966. memcpy(sout, val->blob.s , val->blob.len);
  967. *slen = l;
  968. return 0;
  969. }
  970. break;
  971. default:
  972. DBG("Unknown data type\n");
  973. return -8;
  974. }
  975. }
  976. /*creates a composite key _k of length _klen from n values of _v;
  977. provide your own initialized memory for target _k and _klen;
  978. resulting value: _k = "KEY1 | KEY2"
  979. ko = key only
  980. */
  981. int bdblib_valtochar(bdb_table_p tp, db_fld_t *fld, int fld_count, char *kout,
  982. int *klen, int ktype)
  983. {
  984. char *p;
  985. static char sk[MAX_ROW_SIZE]; // subkey(sk) val
  986. char* delim = DELIM;
  987. char* cNULL = "NULL";
  988. int len, total, sum;
  989. int i, j, k;
  990. bdb_fld_t *f;
  991. p = kout;
  992. len = sum = total = 0;
  993. i = j = k = 0;
  994. if(tp==NULL) return -1;
  995. if(fld==NULL || fld_count<1) return -1;
  996. if(kout==NULL || klen==NULL ) return -1;
  997. if( *klen < 1) return -1;
  998. memset(sk, 0, MAX_ROW_SIZE);
  999. total = *klen;
  1000. *klen = 0; //sum
  1001. /*
  1002. schema has specified keys
  1003. verify all schema keys are provided
  1004. use 'NULL' for those that are missing.
  1005. */
  1006. for(i=0; i<tp->ncols; i++)
  1007. { /* i indexes columns in schema order */
  1008. if(ktype)
  1009. { /* keymode; skip over non-key columns */
  1010. if(tp->colp[i]->flag==0)
  1011. continue;
  1012. }
  1013. for(j=0; j<fld_count; j++)
  1014. {
  1015. f = DB_GET_PAYLOAD(fld + j);
  1016. /*
  1017. j indexes the columns provided in _k
  1018. which may be less than the total required by
  1019. the schema. the app does not know the order
  1020. of the columns in our schema!
  1021. */
  1022. k = f->col_pos;
  1023. /*
  1024. * k index will remap back to our schema order; like i
  1025. */
  1026. if(i == k)
  1027. {
  1028. /*
  1029. KEY was provided; append to buffer;
  1030. _k[j] contains a key, but its a key that
  1031. corresponds to column k of our schema.
  1032. now we know its a match, and we dont need
  1033. index k for anything else
  1034. */
  1035. len = total - sum;
  1036. if ( bdb_val2str((fld+j), sk, &len) != 0)
  1037. {
  1038. ERR("Destination buffer too short for subval %s\n",sk);
  1039. return -4;
  1040. }
  1041. sum += len;
  1042. if(sum > total)
  1043. {
  1044. ERR("Destination buffer too short for subval %s\n",sk);
  1045. return -5;
  1046. }
  1047. strncpy(p, sk, len);
  1048. p += len;
  1049. *klen = sum;
  1050. sum += DELIM_LEN;
  1051. if(sum > total)
  1052. {
  1053. ERR("Destination buffer too short for delim \n");
  1054. return -5;
  1055. }
  1056. /* append delim */
  1057. strncpy(p, delim, DELIM_LEN);
  1058. p += DELIM_LEN;
  1059. *klen = sum;
  1060. /* take us out of inner for loop
  1061. and at the end of the outer loop
  1062. to look for our next schema key
  1063. */
  1064. goto next;
  1065. }
  1066. }
  1067. /*
  1068. NO KEY provided; use the column default value (dv)
  1069. i.e _tp->colp[i]->dv
  1070. */
  1071. len = tp->colp[i]->dv.len;
  1072. sum += len;
  1073. if(sum > total)
  1074. {
  1075. ERR("Destination buffer too short for subval %s\n",cNULL);
  1076. return -5;
  1077. }
  1078. strncpy(p, tp->colp[i]->dv.s, len);
  1079. p += len;
  1080. *klen = sum;
  1081. sum += DELIM_LEN;
  1082. if(sum > total)
  1083. {
  1084. ERR("Destination buffer too short for delim \n");
  1085. return -5;
  1086. }
  1087. strncpy(p, delim, DELIM_LEN);
  1088. p += DELIM_LEN;
  1089. *klen = sum;
  1090. next:
  1091. continue;
  1092. }
  1093. return 0;
  1094. }
  1095. /**
  1096. *
  1097. */
  1098. int bdb_db_free(bdb_db_p _dbp)
  1099. {
  1100. bdb_tcache_p _tbc = NULL, _tbc0=NULL;
  1101. if(!_dbp)
  1102. return -1;
  1103. _tbc = _dbp->tables;
  1104. while(_tbc)
  1105. {
  1106. _tbc0 = _tbc->next;
  1107. bdb_tcache_free(_tbc);
  1108. _tbc = _tbc0;
  1109. }
  1110. if(_dbp->dbenv)
  1111. _dbp->dbenv->close(_dbp->dbenv, 0);
  1112. if(_dbp->name.s)
  1113. pkg_free(_dbp->name.s);
  1114. pkg_free(_dbp);
  1115. return 0;
  1116. }
  1117. /**
  1118. *
  1119. */
  1120. int bdb_tcache_free(bdb_tcache_p _tbc)
  1121. {
  1122. if(!_tbc)
  1123. return -1;
  1124. /*while ??!? */
  1125. if(_tbc->dtp)
  1126. bdb_table_free(_tbc->dtp);
  1127. pkg_free(_tbc);
  1128. return 0;
  1129. }
  1130. /**
  1131. * close DB (sync data to disk) and free mem
  1132. */
  1133. int bdb_table_free(bdb_table_p _tp)
  1134. { int i;
  1135. if(!_tp)
  1136. return -1;
  1137. if(_tp->db)
  1138. _tp->db->close(_tp->db, 0);
  1139. if(_tp->fp)
  1140. fclose(_tp->fp);
  1141. if(_tp->name.s)
  1142. pkg_free(_tp->name.s);
  1143. for(i=0;i<_tp->ncols;i++)
  1144. { if(_tp->colp[i])
  1145. { pkg_free(_tp->colp[i]->name.s);
  1146. pkg_free(_tp->colp[i]->dv.s);
  1147. pkg_free(_tp->colp[i]);
  1148. }
  1149. }
  1150. pkg_free(_tp);
  1151. return 0;
  1152. }
  1153. int bdblib_recover(bdb_table_p _tp, int _rc)
  1154. {
  1155. switch(_rc)
  1156. {
  1157. case DB_LOCK_DEADLOCK:
  1158. ERR("DB_LOCK_DEADLOCK detected !!\n");
  1159. case DB_RUNRECOVERY:
  1160. ERR("DB_RUNRECOVERY detected !! \n");
  1161. bdblib_destroy();
  1162. exit(1);
  1163. break;
  1164. }
  1165. return 0;
  1166. }
  1167. /**
  1168. *
  1169. */
  1170. int bdb_is_database(char *dirpath)
  1171. {
  1172. DIR *dirp = NULL;
  1173. if(dirpath==NULL || dirpath[0]=='\0')
  1174. return 0;
  1175. dirp = opendir(dirpath);
  1176. if(dirp==NULL)
  1177. return 0;
  1178. closedir(dirp);
  1179. return 1;
  1180. }
  1181. int bdb_get_colpos(bdb_table_t *tp, char *name)
  1182. {
  1183. str s;
  1184. int i;
  1185. if(tp==NULL || name==NULL)
  1186. {
  1187. ERR("bdb: bad parameters\n");
  1188. return -1;
  1189. }
  1190. s.s = name;
  1191. s.len = strlen(name);
  1192. for(i=0; i<tp->ncols; i++) {
  1193. if(tp->colp[i]->name.len == s.len
  1194. && !strncasecmp(s.s, tp->colp[i]->name.s, s.len))
  1195. return i;
  1196. }
  1197. return -1;
  1198. }
  1199. int bdb_str2time(char *s, time_t *v)
  1200. {
  1201. struct tm time;
  1202. if ((!s) || (!v)) {
  1203. ERR("bdb:invalid parameter value\n");
  1204. return -1;
  1205. }
  1206. memset(&time, '\0', sizeof(struct tm));
  1207. //if (strptime(s, "%Y-%m-%d %H:%M:%S", &time) == NULL) {
  1208. // ERR("Error during time conversion\n");
  1209. // return -1;
  1210. //}
  1211. time.tm_isdst = -1;
  1212. *v = mktime(&time);
  1213. return 0;
  1214. }
  1215. int bdb_str2double(char *s, double *v)
  1216. {
  1217. if ((!s) || (!v)) {
  1218. ERR("Invalid parameter value\n");
  1219. return -1;
  1220. }
  1221. *v = atof(s);
  1222. return 0;
  1223. }
  1224. int bdb_str2int(char *s, int *v)
  1225. {
  1226. long tmp;
  1227. if (!s || !v) {
  1228. ERR("Invalid parameter value\n");
  1229. return -1;
  1230. }
  1231. tmp = strtoul(s, 0, 10);
  1232. if ((tmp == ULONG_MAX && errno == ERANGE) ||
  1233. (tmp < INT_MIN) || (tmp > UINT_MAX)) {
  1234. ERR("Value out of range\n");
  1235. return -1;
  1236. }
  1237. *v = (int)tmp;
  1238. return 0;
  1239. }