ruleparam_db.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. #include "ruleparam_db.h"
  2. extern int commhub_fd;
  3. struct message_record log_msg;
  4. int ruleparam_db_init( ruleparam_db_ctx *ctx )
  5. {
  6. ctx->n = 0;
  7. ctx->head = NULL;
  8. ctx->sync_counter = 0;
  9. ctx->sync_every_n = 100;
  10. ctx->n_filename_history = 4;
  11. ctx->pos_filename_history = 0;
  12. ctx->seq = 0;
  13. return 0;
  14. }
  15. static int _chomp(char *s)
  16. {
  17. int n;
  18. if (!s)
  19. {
  20. return -1;
  21. }
  22. n = strlen(s);
  23. if (n==0)
  24. return 0;
  25. if (s[n-1] == '\n')
  26. {
  27. s[n-1] = '\0';
  28. return n-1;
  29. }
  30. return n;
  31. }
  32. // In the consistency check, we allocate our own ruleparam_db, so we needed to expose
  33. // this function.
  34. //
  35. passdb_slim_ruleparam *_alloc_ruleparam_db_nod(int id, char *name, char *param)
  36. {
  37. passdb_slim_ruleparam *nod;
  38. nod = (passdb_slim_ruleparam *)malloc(sizeof(passdb_slim_ruleparam));
  39. if (!nod)
  40. {
  41. fprintf(stderr, "_alloc_ruelparam_db_nod malloc failed: %s line %i\n", __FILE__, __LINE__ );
  42. return NULL;
  43. }
  44. nod->id = id;
  45. strncpy( nod->name, name, RULENAME_LEN);
  46. strncpy( nod->param, param, PARAM_LEN);
  47. nod->name[RULENAME_LEN-1] = '\0';
  48. nod->param[PARAM_LEN-1] = '\0';
  49. nod->reference_count = 0;
  50. nod->next = NULL;
  51. return nod;
  52. }
  53. ruleparam_db_ctx * ruleparam_db_alloc(void)
  54. {
  55. ruleparam_db_ctx *ctx;
  56. ctx = (ruleparam_db_ctx *)malloc(sizeof(ruleparam_db_ctx));
  57. if (!ctx)
  58. {
  59. fprintf(stderr, "ruleparam_db_alloc: malloc failed: %s line %i\n", __FILE__, __LINE__ );
  60. return NULL;
  61. }
  62. ctx->n_filename_history = 4;
  63. ctx->pos_filename_history = 0;
  64. ruleparam_db_init(ctx);
  65. return ctx;
  66. }
  67. int ruleparam_db_load( ruleparam_db_ctx **ctx, char *db_fn )
  68. {
  69. FILE *fp;
  70. passdb_slim_ruleparam *nod, *prev=NULL;
  71. ruleparam_db_ctx *tctx = NULL;
  72. FILE *seq_fp;
  73. char db_seq_fn[1024];
  74. char buf[1024];
  75. char name[RULENAME_LEN];
  76. char param[PARAM_LEN];
  77. char *chp, *chp_next;
  78. char *token;
  79. int r = -1;
  80. int id;
  81. int line_no = 0;
  82. int k;
  83. unsigned long long seq=0;
  84. long long int tseq;
  85. int rulecount=-1;
  86. tctx = (ruleparam_db_ctx *)malloc(sizeof(ruleparam_db_ctx));
  87. CHECK_ALLOC_FAIL( (tctx) );
  88. ruleparam_db_init( tctx );
  89. // Read in sequence number from file.
  90. // Keep going if file does not exist.
  91. //
  92. snprintf(db_seq_fn, 128, "%s.seq", db_fn );
  93. seq_fp = fopen( db_seq_fn, "r");
  94. if (!seq_fp)
  95. {
  96. perror( db_seq_fn );
  97. }
  98. else
  99. {
  100. fgets( buf, 1024, seq_fp );
  101. tseq = atoll( buf );
  102. if (tseq > 0)
  103. {
  104. seq = (unsigned long long)tseq;
  105. }
  106. fclose(seq_fp);
  107. }
  108. fp = fopen(db_fn, "r");
  109. if (!fp)
  110. {
  111. perror(db_fn);
  112. if (tctx)
  113. free(tctx);
  114. return -1;
  115. }
  116. while (fgets(buf, 1024, fp ))
  117. {
  118. line_no++;
  119. chp = buf;
  120. if (!chp)
  121. break;
  122. if (!(*chp))
  123. continue;
  124. token = chp;
  125. // skip initial whitespace
  126. //
  127. while ( (*token) && (*token == ' ') )
  128. token++;
  129. // skip comment
  130. //
  131. if (token[0] == '#')
  132. {
  133. /*
  134. if (strncmp( token, "#seq: ", strlen("#seq: ")) == 0)
  135. {
  136. tseq = atoll( token + strlen("#seq: ") );
  137. if (tseq > 0)
  138. {
  139. seq = (unsigned long long)tseq;
  140. }
  141. }
  142. else
  143. */
  144. if (strncmp( token, "#rulecount: ", strlen("#rulecount: ")) == 0)
  145. {
  146. rulecount = atoi( token + strlen("#rulecount: ") );
  147. }
  148. continue;
  149. }
  150. // skip if empty line
  151. //
  152. if (*token == '\n') continue;
  153. // FIRST TOKEN (id)
  154. //find next token (tab)
  155. chp_next = strchr( chp, '\t' );
  156. if (!chp_next) { goto _cleanup; }
  157. *chp_next = '\0';
  158. // and process
  159. k = _chomp(token);
  160. if (k<0) { goto _cleanup; }
  161. if (k==0) continue;
  162. id = atoi(token);
  163. // SECOND TOKEN (rule)
  164. // find next token (tab)
  165. token = chp_next+1;
  166. chp_next = strchr( token, '\t' );
  167. if ( !chp_next ) { goto _cleanup; }
  168. *chp_next = '\0';
  169. _chomp(token);
  170. // and process
  171. strncpy(name, token, RULENAME_LEN);
  172. name[RULENAME_LEN-1] = '\0';
  173. // THIRD TOKEN (param)
  174. // NOTE: fourth token assummed to exist, even though we don't read it
  175. // in. By convention, the fourth token is the reference count.
  176. //
  177. // find next token (tab)
  178. token = chp_next+1;
  179. chp_next = strchr( token, '\t' );
  180. if ( !chp_next ) { goto _cleanup; }
  181. *chp_next = '\0';
  182. _chomp(token);
  183. // and process
  184. strncpy(param, token, PARAM_LEN);
  185. param[PARAM_LEN-1] = '\0';
  186. nod = _alloc_ruleparam_db_nod( id, name, param );
  187. if (tctx->head) prev->next = nod;
  188. else tctx->head = nod;
  189. prev = nod;
  190. tctx->n++;
  191. }
  192. if (tctx->n != rulecount)
  193. {
  194. fprintf(stderr, "WARNING: ruleparam_db_load: read rule count (%i) != reported rule count (%i)\n", tctx->n, rulecount);
  195. }
  196. r = 0;
  197. tctx->db_filename = strdup( db_fn );
  198. tctx->seq = seq;
  199. *ctx = tctx;
  200. return 0;
  201. _cleanup:
  202. fprintf(stderr, "ERROR: ruleparam_db_load: FAIL on line %i\n", line_no);
  203. if (tctx)
  204. {
  205. nod = tctx->head;
  206. while (nod)
  207. {
  208. prev = nod;
  209. nod = nod->next;
  210. free(prev);
  211. }
  212. free(tctx);
  213. }
  214. fclose(fp);
  215. return -1;
  216. }
  217. int _copy_file( char *src_fn, char *dst_fn )
  218. {
  219. FILE *src_fp, *dst_fp;
  220. char buf[1024];
  221. int read_len=0, write_len=0;
  222. int ret=0;
  223. if ( !(src_fp = fopen(src_fn, "r")) )
  224. {
  225. perror(src_fn);
  226. return errno;
  227. }
  228. if ( !(dst_fp = fopen(dst_fn, "w")) )
  229. {
  230. fclose(src_fp);
  231. perror(dst_fn);
  232. return errno;
  233. }
  234. while (!feof(src_fp))
  235. {
  236. read_len = fread(buf, 1, sizeof(buf), src_fp);
  237. if (read_len <= 0)
  238. {
  239. if (ferror(src_fp) != 0)
  240. {
  241. perror(src_fn);
  242. ret = errno;
  243. goto _copy_file_cleanup;
  244. }
  245. continue;
  246. }
  247. while ( (write_len = fwrite(buf, 1, read_len, dst_fp)) != read_len )
  248. {
  249. perror(dst_fn);
  250. ret = errno;
  251. goto _copy_file_cleanup;
  252. }
  253. }
  254. _copy_file_cleanup:
  255. fclose(src_fp);
  256. fclose(dst_fp);
  257. return ret;
  258. }
  259. int ruleparam_db_seq_save( ruleparam_db_ctx *ctx )
  260. {
  261. FILE *seq_fp;
  262. char tmp_fn[512];
  263. char seq_fn[512];
  264. // After ruleparam.db has been synchronized, finally
  265. // write out sequence number in 'ruleparam.db.seq' file.
  266. //
  267. snprintf(tmp_fn, 512, "%s.seq.tmp", ctx->db_filename );
  268. snprintf(seq_fn, 512, "%s.seq", ctx->db_filename );
  269. seq_fp = fopen( tmp_fn, "w" );
  270. if (seq_fp)
  271. {
  272. fprintf(seq_fp, "%llu", ctx->seq);
  273. fclose(seq_fp);
  274. sync();
  275. rename( tmp_fn, seq_fn );
  276. sync();
  277. }
  278. else
  279. {
  280. perror( tmp_fn );
  281. return -1;
  282. }
  283. return 0;
  284. }
  285. int ruleparam_db_save( ruleparam_db_ctx *ctx )
  286. {
  287. FILE *fp;
  288. passdb_slim_ruleparam *nod;
  289. struct tm tm_tim;
  290. struct timeval timval;
  291. char str_tim[64];
  292. char tmp_fn[1024];
  293. char log_fn[1024];
  294. int fn_num = 0;
  295. int rule_count=0;
  296. if ( ctx->n_filename_history > 1 )
  297. {
  298. if (ctx->pos_filename_history < 0)
  299. {
  300. ctx->pos_filename_history = 0;
  301. }
  302. else
  303. {
  304. ctx->pos_filename_history++;
  305. ctx->pos_filename_history %= ctx->n_filename_history;
  306. }
  307. fn_num = ctx->pos_filename_history;
  308. }
  309. sprintf(tmp_fn, "%s.%i.tmp", ctx->db_filename, fn_num);
  310. sprintf(log_fn, "%s.%i", ctx->db_filename, fn_num);
  311. gettimeofday(&timval, NULL);
  312. localtime_r(&(timval.tv_sec), &tm_tim);
  313. asctime_r(&tm_tim, str_tim);
  314. fp = fopen(tmp_fn, "w");
  315. if (!fp)
  316. {
  317. perror(tmp_fn);
  318. format_log_message(&log_msg, LOGLEVEL_ERROR, "Ruleparam dabase open error (%s)", tmp_fn);
  319. if (commhub_fd >= 0)
  320. {
  321. send_message(commhub_fd, &log_msg);
  322. }
  323. return -1;
  324. }
  325. fprintf(fp, "# %s", str_tim);
  326. fprintf(fp, "# Tabs are a necessary field delimiter.\n");
  327. fprintf(fp, "#\n# InternalID,RuleName,RuleParam,ReferenceCount\n");
  328. fprintf(fp, "# Note: The reference count in this file is only used for debugging purposes.\n");
  329. fprintf(fp, "#seq: %llu\n", ctx->seq);
  330. fprintf(fp, "#\n#\n");
  331. nod = ctx->head;
  332. while (nod)
  333. {
  334. fprintf(fp, "%i\t%s\t%s\t%i\n", nod->id, nod->name, nod->param, nod->reference_count);
  335. nod = nod->next;
  336. rule_count++;
  337. }
  338. fprintf(fp, "#rulecount: %i\n", rule_count);
  339. fclose(fp);
  340. sync();
  341. if (_copy_file( tmp_fn, log_fn ) < 0)
  342. {
  343. format_log_message(&log_msg, LOGLEVEL_ERROR, "Ruleparam dabase copy error (%s -> %s)", tmp_fn, log_fn);
  344. if (commhub_fd >= 0)
  345. {
  346. send_message(commhub_fd, &log_msg);
  347. }
  348. return errno;
  349. }
  350. sync();
  351. rename( tmp_fn, ctx->db_filename );
  352. sync();
  353. ruleparam_db_seq_save( ctx );
  354. return 0;
  355. }
  356. int format_new_ruleparamdb( char *fn )
  357. {
  358. ruleparam_db_ctx ctx;
  359. ruleparam_db_init(&ctx);
  360. //return ruleparam_db_save( &ctx, fn );
  361. return ruleparam_db_save( &ctx );
  362. }
  363. int ruleparam_db_add( ruleparam_db_ctx *ctx, char *name, char *param)
  364. {
  365. int id;
  366. passdb_slim_ruleparam *prev = NULL, *nod ;
  367. if (ctx->n == RULEPARAM_DB_MAX)
  368. return RULEPARAM_DB_FULL;
  369. id = ruleparam_db_find( ctx, name, param );
  370. if (id >= 0)
  371. return RULEPARAM_DB_DUP;
  372. id = ruleparam_db_find_free_id( ctx );
  373. if (id < 0)
  374. return id;
  375. nod = _alloc_ruleparam_db_nod( id, name, param);
  376. ctx->n++;
  377. if (!(ctx->head))
  378. {
  379. ctx->head = nod;
  380. return 0;
  381. }
  382. prev = ctx->head;
  383. while(prev->next) prev = prev->next;
  384. prev->next = nod;
  385. return id;
  386. }
  387. int ruleparam_db_remove( ruleparam_db_ctx *ctx, int id)
  388. {
  389. passdb_slim_ruleparam *prev = NULL, *nod ;
  390. nod = ctx->head;
  391. while (nod)
  392. {
  393. if (nod->id == id)
  394. {
  395. if (prev) prev->next = nod->next;
  396. else ctx->head = nod->next;
  397. free(nod);
  398. ctx->n--;
  399. return 0;
  400. }
  401. prev = nod;
  402. nod = nod->next;
  403. }
  404. return RULEPARAM_DB_NOT_FOUND;
  405. }
  406. // Return id of found rule record.
  407. // RULEPARAM_DB_NOT_FOUND if none found.
  408. //
  409. int ruleparam_db_find( ruleparam_db_ctx *ctx, char *name, char *param )
  410. {
  411. passdb_slim_ruleparam *nod;
  412. nod = ctx->head;
  413. while (nod)
  414. {
  415. if ( (strncmp( nod->name, name, RULENAME_LEN ) == 0) &&
  416. (strncmp( nod->param, param, PARAM_LEN ) == 0) )
  417. return nod->id;
  418. nod = nod->next;
  419. }
  420. return RULEPARAM_DB_NOT_FOUND;
  421. }
  422. // Return reference count of rule give as (name,param) string pair
  423. //
  424. int ruleparam_db_reference_count( ruleparam_db_ctx *ctx, char *name, char *param )
  425. {
  426. passdb_slim_ruleparam *nod;
  427. nod = ctx->head;
  428. while (nod)
  429. {
  430. if ( (strncmp( nod->name, name, RULENAME_LEN ) == 0) &&
  431. (strncmp( nod->param, param, PARAM_LEN ) == 0) )
  432. return nod->reference_count;
  433. nod = nod->next;
  434. }
  435. return RULEPARAM_DB_NOT_FOUND;
  436. }
  437. // Populate name and param from rule id, if found
  438. //
  439. int ruleparam_db_get( char *name, char *param, ruleparam_db_ctx *ctx, int id )
  440. {
  441. passdb_slim_ruleparam *nod;
  442. nod = ctx->head;
  443. while (nod)
  444. {
  445. if (nod->id == id)
  446. {
  447. memcpy( name, nod->name, RULENAME_LEN );
  448. memcpy( param, nod->param, PARAM_LEN );
  449. name[RULENAME_LEN-1] = '\0';
  450. name[PARAM_LEN-1] = '\0';
  451. return 0;
  452. }
  453. nod = nod->next;
  454. }
  455. return RULEPARAM_DB_NOT_FOUND;
  456. }
  457. static int _icmp( const void *a, const void *b)
  458. {
  459. return (*((int *)a)) - (*((int *)b)) ;
  460. }
  461. // Take all id's, put them into an array.
  462. // Sort it.
  463. // Find the first non colliding entry starting
  464. // at 0.
  465. //
  466. int ruleparam_db_find_free_id( ruleparam_db_ctx *ctx )
  467. {
  468. int i, n, pos=0;
  469. int *p;
  470. passdb_slim_ruleparam *nod;
  471. n = ctx->n;
  472. if (n == RULEPARAM_DB_MAX)
  473. return RULEPARAM_DB_FULL;
  474. if (n == 0)
  475. return 0;
  476. p = (int *)malloc(sizeof(int)*n);
  477. CHECK_ALLOC_FAIL( (p) );
  478. nod = ctx->head;
  479. while (nod)
  480. {
  481. p[pos++] = nod->id;
  482. nod = nod->next;
  483. }
  484. qsort(p, n, sizeof(int), _icmp);
  485. i=0;
  486. pos=0;
  487. while ( (i<n) && (pos < RULEPARAM_DB_MAX) )
  488. {
  489. if (p[i] == pos)
  490. {
  491. i++;
  492. pos++;
  493. }
  494. else break;
  495. }
  496. free(p);
  497. return pos;
  498. }
  499. void ruleparam_db_dump(ruleparam_db_ctx *ctx)
  500. {
  501. passdb_slim_ruleparam *nod;
  502. printf("n %i\n", ctx->n);
  503. nod = ctx->head;
  504. while (nod)
  505. {
  506. printf(" (%i) '%s' '%s'\n", nod->id, nod->name, nod->param );
  507. nod = nod->next;
  508. }
  509. printf("\n");
  510. }
  511. passdb_slim_ruleparam *ruleparam_db_get_node( ruleparam_db_ctx *ctx, int id )
  512. {
  513. passdb_slim_ruleparam *nod;
  514. nod = ctx->head;
  515. while (nod)
  516. {
  517. if (nod->id == id )
  518. return nod;
  519. nod = nod->next;
  520. }
  521. return NULL;
  522. }
  523. int ruleparam_db_update( ruleparam_db_ctx *ctx, char *name, char *param, int delta_ref_count )
  524. {
  525. int id;
  526. int r;
  527. passdb_slim_ruleparam *nod = NULL;
  528. id = ruleparam_db_find( ctx, name, param );
  529. // If it's not found, create it and update reference count
  530. //
  531. if ( id == RULEPARAM_DB_NOT_FOUND )
  532. {
  533. if (delta_ref_count < 0)
  534. {
  535. goto _ruleparam_db_update_sanity_error;
  536. }
  537. id = ruleparam_db_add( ctx, name, param );
  538. if (id < 0)
  539. return id;
  540. nod = ruleparam_db_get_node( ctx, id );
  541. nod->reference_count += delta_ref_count;
  542. //ruleparam_db_save( ctx, ctx->db_filename );
  543. ruleparam_db_save( ctx );
  544. return id;
  545. }
  546. // Rule record was found, update reference count
  547. //
  548. nod = ruleparam_db_get_node( ctx, id );
  549. nod->reference_count += delta_ref_count;
  550. // check for sanity
  551. //
  552. if (nod->reference_count < 0)
  553. {
  554. goto _ruleparam_db_update_sanity_error;
  555. }
  556. // simply return if there are more references to this record
  557. //
  558. if (nod->reference_count > 0)
  559. {
  560. return id;
  561. }
  562. // otherwise we need to remove this rule record
  563. //
  564. r = ruleparam_db_remove( ctx, id );
  565. if (r < 0)
  566. goto _ruleparam_db_update_sanity_error;
  567. //ruleparam_db_save( ctx, ctx->db_filename );
  568. ruleparam_db_save( ctx );
  569. return r;
  570. _ruleparam_db_update_sanity_error:
  571. fprintf(stderr, "ERROR: sanity! ruleparam_db_update: id %i (%s,%s), delta_ref_count: %i, [ nod(%p).reference_count:%i ] \n",
  572. id, name, param, delta_ref_count,
  573. nod, nod ? nod->reference_count : 0 );
  574. fprintf(stdout, "ERROR: sanity! ruleparam_db_update: id %i (%s,%s), delta_ref_count: %i, [ nod(%p).reference_count:%i ] \n",
  575. id, name, param, delta_ref_count,
  576. nod, nod ? nod->reference_count : 0 );
  577. return RULEPARAM_DB_SANITY;
  578. }
  579. // Remove entries that have zero references
  580. //
  581. int ruleparam_db_clean (ruleparam_db_ctx *ctx)
  582. {
  583. passdb_slim_ruleparam *nod;
  584. passdb_slim_ruleparam *prv = NULL;
  585. passdb_slim_ruleparam *nex ;
  586. nod = ctx->head;
  587. while (nod)
  588. {
  589. nex = nod->next;
  590. if (nod->reference_count == 0)
  591. {
  592. if (prv) prv->next = nex;
  593. else ctx->head = nex;
  594. free(nod);
  595. ctx->n--;
  596. }
  597. else
  598. {
  599. prv = nod;
  600. }
  601. nod = nex;
  602. }
  603. if (ctx->n == 0)
  604. ctx->head = NULL;
  605. return 0;
  606. }
  607. int ruleparam_db_consistency_check( ruleparam_db_ctx *ctx )
  608. {
  609. int n=0;
  610. passdb_slim_ruleparam *nod;
  611. if (!ctx)
  612. {
  613. printf("ruleparam_db_consistenc_check: error, ctx is null\n");
  614. return 0;
  615. }
  616. if ((ctx->n > 0) && (!(ctx->head)))
  617. {
  618. printf("ruleparam_db_consistenc_check: error, ctx->n > 0 (%i) but ctx->head is null \n", ctx->n);
  619. }
  620. nod = ctx->head;
  621. while(nod)
  622. {
  623. if (nod->name[0] == '\0')
  624. {
  625. printf("ruleparam_db_consistenc_check: error, node id %i (pos %i) name is empty\n", nod->id, n);
  626. return 0;
  627. }
  628. if (nod->reference_count <= 0)
  629. {
  630. printf("ruleparam_db_consistenc_check: error, node id %i (pos %i) (%s,%s) reference count (%i) <= 0 \n", nod->id, n, nod->name, nod->param, nod->reference_count);
  631. return 0;
  632. }
  633. n++;
  634. nod = nod->next;
  635. }
  636. if (n != ctx->n)
  637. {
  638. printf("ruleparam_db_consistenc_check: error, ctx->n (%i) != list length (%i) \n", ctx->n, n);
  639. return 0;
  640. }
  641. return 1;
  642. }
  643. int ruleparam_db_debug_dump( ruleparam_db_ctx *ctx)
  644. {
  645. int count=0;
  646. passdb_slim_ruleparam *nod;
  647. printf("ruleparam_db_ctx:\n");
  648. printf(" n: %i\n", ctx->n);
  649. printf(" db_filename: %s\n", ctx->db_filename);
  650. nod = ctx->head;
  651. while (nod)
  652. {
  653. printf(" {%i} id: %i, reference_count: %i, name: %s, param: %s\n",
  654. count++, nod->id, nod->reference_count, nod->name, nod->param );
  655. nod = nod->next;
  656. }
  657. printf("\n");
  658. return 0;
  659. }
  660. int ruleparam_db_free( ruleparam_db_ctx *ctx )
  661. {
  662. passdb_slim_ruleparam *nod, *prev = NULL;
  663. nod = ctx->head;
  664. while (nod)
  665. {
  666. prev = nod;
  667. nod = nod->next;
  668. free(prev);
  669. }
  670. if (ctx->db_filename)
  671. free(ctx->db_filename);
  672. free(ctx);
  673. return 0;
  674. }
  675. // Periodically refresh ruleparam_db, mostly for debugging purposes
  676. // so we can see a somewhat recent snapshot of reference counts.
  677. //
  678. // ruleparam updates through the 'ruleparam_db_update' function
  679. // will automatically write to the ruleparam_db file if a deletion
  680. // or insertion happened.
  681. //
  682. int ruleparam_db_rate_limited_sync( ruleparam_db_ctx *ctx )
  683. {
  684. int retval = 0;
  685. if (ctx->sync_every_n < 1)
  686. {
  687. ctx->sync_counter++;
  688. return retval;
  689. }
  690. if ((ctx->sync_counter % ctx->sync_every_n) == 0)
  691. {
  692. ruleparam_db_save( ctx );
  693. retval = 1;
  694. }
  695. // Else do a sequence number save regardless
  696. //
  697. else
  698. {
  699. ruleparam_db_seq_save( ctx );
  700. sync();
  701. }
  702. ctx->sync_counter++;
  703. return retval;
  704. }
  705. /*
  706. int main(int argc, char **argv)
  707. {
  708. int id=-1, k;
  709. char name[64], param[64];
  710. name[0] = '\0';
  711. param[0] = '\0';
  712. ruleparam_db_ctx *ctx;
  713. ruleparam_db_load(&ctx, RULEPARAM_DB_FILE);
  714. ruleparam_db_dump(ctx);
  715. ruleparam_db_add( ctx, "myrule", "myparam" );
  716. id = ruleparam_db_add( ctx, "myrule0", "myparam" );
  717. ruleparam_db_add( ctx, "myrule", "myparam0" );
  718. ruleparam_db_add( ctx, "myrule0", "myparam0" );
  719. ruleparam_db_dump(ctx);
  720. printf("removing %i\n", id);
  721. printf("\n\n");
  722. ruleparam_db_remove( ctx, id );
  723. ruleparam_db_dump(ctx);
  724. ruleparam_db_save(ctx, RULEPARAM_DB_FILE);
  725. id = ruleparam_db_find(ctx, "myrule0", "myparam");
  726. printf("got %i\n", id);
  727. k = ruleparam_db_get(name, param, ctx, id);
  728. printf(" (%i) %s %s\n", k, name, param);
  729. }
  730. */