source: vendor/current/source3/lib/gencache.c

Last change on this file was 989, checked in by Silvan Scherrer, 9 years ago

Samba Server: update vendor to version 4.4.7

File size: 22.8 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3
4 Generic, persistent and shared between processes cache mechanism for use
5 by various parts of the Samba code
6
7 Copyright (C) Rafal Szczesniak 2002
8 Copyright (C) Volker Lendecke 2009
9
10 This program 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 3 of the License, or
13 (at your option) any later version.
14
15 This program 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, see <http://www.gnu.org/licenses/>.
22*/
23
24#include "includes.h"
25#include "system/filesys.h"
26#include "system/glob.h"
27#include "util_tdb.h"
28#include "tdb_wrap/tdb_wrap.h"
29#include "../lib/util/memcache.h"
30
31#undef DBGC_CLASS
32#define DBGC_CLASS DBGC_TDB
33
34#define TIMEOUT_LEN 12
35#define CACHE_DATA_FMT "%12u/"
36#define READ_CACHE_DATA_FMT_TEMPLATE "%%12u/%%%us"
37#define BLOB_TYPE "DATA_BLOB"
38#define BLOB_TYPE_LEN 9
39
40static struct tdb_wrap *cache;
41static struct tdb_wrap *cache_notrans;
42static int cache_notrans_seqnum;
43
44/**
45 * @file gencache.c
46 * @brief Generic, persistent and shared between processes cache mechanism
47 * for use by various parts of the Samba code
48 *
49 **/
50
51
52/**
53 * Cache initialisation function. Opens cache tdb file or creates
54 * it if does not exist.
55 *
56 * @return true on successful initialisation of the cache or
57 * false on failure
58 **/
59
60static bool gencache_init(void)
61{
62 char* cache_fname = NULL;
63 int open_flags = O_RDWR|O_CREAT;
64
65 /* skip file open if it's already opened */
66 if (cache) {
67 return true;
68 }
69
70 cache_fname = cache_path("gencache.tdb");
71 if (cache_fname == NULL) {
72 return false;
73 }
74
75 DEBUG(5, ("Opening cache file at %s\n", cache_fname));
76
77 cache = tdb_wrap_open(NULL, cache_fname, 0,
78 TDB_DEFAULT|TDB_INCOMPATIBLE_HASH,
79 open_flags, 0644);
80 if (cache) {
81 int ret;
82 ret = tdb_check(cache->tdb, NULL, NULL);
83 if (ret != 0) {
84 TALLOC_FREE(cache);
85
86 /*
87 * Retry with CLEAR_IF_FIRST.
88 *
89 * Warning: Converting this to dbwrap won't work
90 * directly. gencache.c does transactions on this tdb,
91 * and dbwrap forbids this for CLEAR_IF_FIRST
92 * databases. tdb does allow transactions on
93 * CLEAR_IF_FIRST databases, so lets use it here to
94 * clean up a broken database.
95 */
96 cache = tdb_wrap_open(NULL, cache_fname, 0,
97 TDB_DEFAULT|
98 TDB_INCOMPATIBLE_HASH|
99 TDB_CLEAR_IF_FIRST,
100 open_flags, 0644);
101 }
102 }
103
104 if (!cache && (errno == EACCES)) {
105 open_flags = O_RDONLY;
106 cache = tdb_wrap_open(NULL, cache_fname, 0,
107 TDB_DEFAULT|TDB_INCOMPATIBLE_HASH,
108 open_flags, 0644);
109 if (cache) {
110 DEBUG(5, ("gencache_init: Opening cache file %s read-only.\n", cache_fname));
111 }
112 }
113 TALLOC_FREE(cache_fname);
114
115 if (!cache) {
116 DEBUG(5, ("Attempt to open gencache.tdb has failed.\n"));
117 return false;
118 }
119
120 cache_fname = lock_path("gencache_notrans.tdb");
121 if (cache_fname == NULL) {
122 TALLOC_FREE(cache);
123 return false;
124 }
125
126 DEBUG(5, ("Opening cache file at %s\n", cache_fname));
127
128 cache_notrans = tdb_wrap_open(NULL, cache_fname, 0,
129 TDB_CLEAR_IF_FIRST|
130 TDB_INCOMPATIBLE_HASH|
131 TDB_SEQNUM|
132 TDB_NOSYNC|
133 TDB_MUTEX_LOCKING,
134 open_flags, 0644);
135 if (cache_notrans == NULL) {
136 DEBUG(5, ("Opening %s failed: %s\n", cache_fname,
137 strerror(errno)));
138 TALLOC_FREE(cache_fname);
139 TALLOC_FREE(cache);
140 return false;
141 }
142 TALLOC_FREE(cache_fname);
143
144 return true;
145}
146
147static TDB_DATA last_stabilize_key(void)
148{
149 TDB_DATA result;
150 result.dptr = discard_const_p(uint8_t, "@LAST_STABILIZED");
151 result.dsize = 17;
152 return result;
153}
154
155struct gencache_have_val_state {
156 time_t new_timeout;
157 const DATA_BLOB *data;
158 bool gotit;
159};
160
161static void gencache_have_val_parser(time_t old_timeout, DATA_BLOB data,
162 void *private_data)
163{
164 struct gencache_have_val_state *state =
165 (struct gencache_have_val_state *)private_data;
166 time_t now = time(NULL);
167 int cache_time_left, new_time_left, additional_time;
168
169 /*
170 * Excuse the many variables, but these time calculations are
171 * confusing to me. We do not want to write to gencache with a
172 * possibly expensive transaction if we are about to write the same
173 * value, just extending the remaining timeout by less than 10%.
174 */
175
176 cache_time_left = old_timeout - now;
177 if (cache_time_left <= 0) {
178 /*
179 * timed out, write new value
180 */
181 return;
182 }
183
184 new_time_left = state->new_timeout - now;
185 if (new_time_left <= 0) {
186 /*
187 * Huh -- no new timeout?? Write it.
188 */
189 return;
190 }
191
192 if (new_time_left < cache_time_left) {
193 /*
194 * Someone wants to shorten the timeout. Let it happen.
195 */
196 return;
197 }
198
199 /*
200 * By how much does the new timeout extend the remaining cache time?
201 */
202 additional_time = new_time_left - cache_time_left;
203
204 if (additional_time * 10 < 0) {
205 /*
206 * Integer overflow. We extend by so much that we have to write it.
207 */
208 return;
209 }
210
211 /*
212 * The comparison below is essentially equivalent to
213 *
214 * new_time_left > cache_time_left * 1.10
215 *
216 * but without floating point calculations.
217 */
218
219 if (additional_time * 10 > cache_time_left) {
220 /*
221 * We extend the cache timeout by more than 10%. Do it.
222 */
223 return;
224 }
225
226 /*
227 * Now the more expensive data compare.
228 */
229 if (data_blob_cmp(state->data, &data) != 0) {
230 /*
231 * Write a new value. Certainly do it.
232 */
233 return;
234 }
235
236 /*
237 * Extending the timeout by less than 10% for the same cache value is
238 * not worth the trouble writing a value into gencache under a
239 * possibly expensive transaction.
240 */
241 state->gotit = true;
242}
243
244static bool gencache_have_val(const char *keystr, const DATA_BLOB *data,
245 time_t timeout)
246{
247 struct gencache_have_val_state state;
248
249 state.new_timeout = timeout;
250 state.data = data;
251 state.gotit = false;
252
253 if (!gencache_parse(keystr, gencache_have_val_parser, &state)) {
254 return false;
255 }
256 return state.gotit;
257}
258
259static int last_stabilize_parser(TDB_DATA key, TDB_DATA data,
260 void *private_data)
261{
262 time_t *last_stabilize = private_data;
263
264 if ((data.dsize != 0) && (data.dptr[data.dsize-1] == '\0')) {
265 *last_stabilize = atoi((char *)data.dptr);
266 }
267 return 0;
268}
269
270/**
271 * Set an entry in the cache file. If there's no such
272 * one, then add it.
273 *
274 * @param keystr string that represents a key of this entry
275 * @param blob DATA_BLOB value being cached
276 * @param timeout time when the value is expired
277 *
278 * @retval true when entry is successfully stored
279 * @retval false on failure
280 **/
281
282bool gencache_set_data_blob(const char *keystr, const DATA_BLOB *blob,
283 time_t timeout)
284{
285 int ret;
286 fstring hdr;
287 int hdr_len;
288 char* val;
289 time_t last_stabilize;
290 static int writecount;
291
292 if (tdb_data_cmp(string_term_tdb_data(keystr),
293 last_stabilize_key()) == 0) {
294 DEBUG(10, ("Can't store %s as a key\n", keystr));
295 return false;
296 }
297
298 if ((keystr == NULL) || (blob == NULL)) {
299 return false;
300 }
301
302 if (!gencache_init()) {
303 return false;
304 }
305
306 if (gencache_have_val(keystr, blob, timeout)) {
307 DEBUG(10, ("Did not store value for %s, we already got it\n",
308 keystr));
309 return true;
310 }
311
312 hdr_len = fstr_sprintf(hdr, CACHE_DATA_FMT, (int)timeout);
313
314 if (hdr_len == -1) {
315 return false;
316 }
317 if ((blob->length + (size_t)hdr_len) < blob->length) {
318 return false;
319 }
320
321 val = talloc_array(talloc_tos(), char, hdr_len + blob->length);
322 if (val == NULL) {
323 return false;
324 }
325
326 memcpy(val, hdr, hdr_len);
327 memcpy(val+hdr_len, blob->data, blob->length);
328
329 DEBUG(10, ("Adding cache entry with key=[%s] and timeout="
330 "[%s] (%d seconds %s)\n", keystr,
331 timestring(talloc_tos(), timeout),
332 (int)(timeout - time(NULL)),
333 timeout > time(NULL) ? "ahead" : "in the past"));
334
335 ret = tdb_store_bystring(
336 cache_notrans->tdb, keystr,
337 make_tdb_data((uint8_t *)val, talloc_array_length(val)),
338 0);
339 TALLOC_FREE(val);
340
341 if (ret != 0) {
342 return false;
343 }
344
345 /*
346 * Every 100 writes within a single process, stabilize the cache with
347 * a transaction. This is done to prevent a single transaction to
348 * become huge and chew lots of memory.
349 */
350 writecount += 1;
351 if (writecount > lp_parm_int(-1, "gencache", "stabilize_count", 100)) {
352 gencache_stabilize();
353 writecount = 0;
354 goto done;
355 }
356
357 /*
358 * Every 5 minutes, call gencache_stabilize() to not let grow
359 * gencache_notrans.tdb too large.
360 */
361
362 last_stabilize = 0;
363
364 tdb_parse_record(cache_notrans->tdb, last_stabilize_key(),
365 last_stabilize_parser, &last_stabilize);
366
367 if ((last_stabilize
368 + lp_parm_int(-1, "gencache", "stabilize_interval", 300))
369 < time(NULL)) {
370 gencache_stabilize();
371 }
372
373done:
374 return ret == 0;
375}
376
377/**
378 * Delete one entry from the cache file.
379 *
380 * @param keystr string that represents a key of this entry
381 *
382 * @retval true upon successful deletion
383 * @retval false in case of failure
384 **/
385
386bool gencache_del(const char *keystr)
387{
388 bool exists, was_expired;
389 bool ret = false;
390 DATA_BLOB value;
391
392 if (keystr == NULL) {
393 return false;
394 }
395
396 if (!gencache_init()) {
397 return false;
398 }
399
400 DEBUG(10, ("Deleting cache entry (key=[%s])\n", keystr));
401
402 /*
403 * We delete an element by setting its timeout to 0. This way we don't
404 * have to do a transaction on gencache.tdb every time we delete an
405 * element.
406 */
407
408 exists = gencache_get_data_blob(keystr, NULL, &value, NULL,
409 &was_expired);
410
411 if (!exists && was_expired) {
412 /*
413 * gencache_get_data_blob has implicitly deleted this
414 * entry, so we have to return success here.
415 */
416 return true;
417 }
418
419 if (exists) {
420 data_blob_free(&value);
421 ret = gencache_set(keystr, "", 0);
422 }
423 return ret;
424}
425
426static bool gencache_pull_timeout(char *val, time_t *pres, char **pendptr)
427{
428 time_t res;
429 char *endptr;
430
431 if (val == NULL) {
432 return false;
433 }
434
435 res = strtol(val, &endptr, 10);
436
437 if ((endptr == NULL) || (*endptr != '/')) {
438 DEBUG(2, ("Invalid gencache data format: %s\n", val));
439 return false;
440 }
441 if (pres != NULL) {
442 *pres = res;
443 }
444 if (pendptr != NULL) {
445 *pendptr = endptr;
446 }
447 return true;
448}
449
450struct gencache_parse_state {
451 void (*parser)(time_t timeout, DATA_BLOB blob, void *private_data);
452 void *private_data;
453 bool is_memcache;
454};
455
456static int gencache_parse_fn(TDB_DATA key, TDB_DATA data, void *private_data)
457{
458 struct gencache_parse_state *state;
459 DATA_BLOB blob;
460 time_t t;
461 char *endptr;
462 bool ret;
463
464 if (data.dptr == NULL) {
465 return -1;
466 }
467 ret = gencache_pull_timeout((char *)data.dptr, &t, &endptr);
468 if (!ret) {
469 return -1;
470 }
471 state = (struct gencache_parse_state *)private_data;
472 blob = data_blob_const(
473 endptr+1, data.dsize - PTR_DIFF(endptr+1, data.dptr));
474 state->parser(t, blob, state->private_data);
475
476 if (!state->is_memcache) {
477 memcache_add(NULL, GENCACHE_RAM,
478 data_blob_const(key.dptr, key.dsize),
479 data_blob_const(data.dptr, data.dsize));
480 }
481
482 return 0;
483}
484
485bool gencache_parse(const char *keystr,
486 void (*parser)(time_t timeout, DATA_BLOB blob,
487 void *private_data),
488 void *private_data)
489{
490 struct gencache_parse_state state;
491 TDB_DATA key = string_term_tdb_data(keystr);
492 DATA_BLOB memcache_val;
493 int ret;
494
495 if (keystr == NULL) {
496 return false;
497 }
498 if (tdb_data_cmp(key, last_stabilize_key()) == 0) {
499 return false;
500 }
501 if (!gencache_init()) {
502 return false;
503 }
504
505 state.parser = parser;
506 state.private_data = private_data;
507
508 if (memcache_lookup(NULL, GENCACHE_RAM,
509 data_blob_const(key.dptr, key.dsize),
510 &memcache_val)) {
511 /*
512 * Make sure that nobody has changed the gencache behind our
513 * back.
514 */
515 int current_seqnum = tdb_get_seqnum(cache_notrans->tdb);
516 if (current_seqnum == cache_notrans_seqnum) {
517 /*
518 * Ok, our memcache is still current, use it without
519 * going to the tdb files.
520 */
521 state.is_memcache = true;
522 gencache_parse_fn(key, make_tdb_data(memcache_val.data,
523 memcache_val.length),
524 &state);
525 return true;
526 }
527 memcache_flush(NULL, GENCACHE_RAM);
528 cache_notrans_seqnum = current_seqnum;
529 }
530
531 state.is_memcache = false;
532
533 ret = tdb_parse_record(cache_notrans->tdb, key,
534 gencache_parse_fn, &state);
535 if (ret == 0) {
536 return true;
537 }
538 ret = tdb_parse_record(cache->tdb, key, gencache_parse_fn, &state);
539 return (ret == 0);
540}
541
542struct gencache_get_data_blob_state {
543 TALLOC_CTX *mem_ctx;
544 DATA_BLOB *blob;
545 time_t timeout;
546 bool result;
547};
548
549static void gencache_get_data_blob_parser(time_t timeout, DATA_BLOB blob,
550 void *private_data)
551{
552 struct gencache_get_data_blob_state *state =
553 (struct gencache_get_data_blob_state *)private_data;
554
555 if (timeout == 0) {
556 state->result = false;
557 return;
558 }
559 state->timeout = timeout;
560
561 if (state->blob == NULL) {
562 state->result = true;
563 return;
564 }
565
566 *state->blob = data_blob_talloc(state->mem_ctx, blob.data,
567 blob.length);
568 if (state->blob->data == NULL) {
569 state->result = false;
570 return;
571 }
572 state->result = true;
573}
574
575/**
576 * Get existing entry from the cache file.
577 *
578 * @param keystr string that represents a key of this entry
579 * @param blob DATA_BLOB that is filled with entry's blob
580 * @param timeout pointer to a time_t that is filled with entry's
581 * timeout
582 *
583 * @retval true when entry is successfuly fetched
584 * @retval false for failure
585 **/
586
587bool gencache_get_data_blob(const char *keystr, TALLOC_CTX *mem_ctx,
588 DATA_BLOB *blob,
589 time_t *timeout, bool *was_expired)
590{
591 struct gencache_get_data_blob_state state;
592 bool expired = false;
593
594 state.result = false;
595 state.mem_ctx = mem_ctx;
596 state.blob = blob;
597
598 if (!gencache_parse(keystr, gencache_get_data_blob_parser, &state)) {
599 goto fail;
600 }
601 if (!state.result) {
602 goto fail;
603 }
604 if (state.timeout <= time(NULL)) {
605 /*
606 * We're expired, delete the entry. We can't use gencache_del
607 * here, because that uses gencache_get_data_blob for checking
608 * the existence of a record. We know the thing exists and
609 * directly store an empty value with 0 timeout.
610 */
611 gencache_set(keystr, "", 0);
612 expired = true;
613 goto fail;
614 }
615 if (timeout) {
616 *timeout = state.timeout;
617 }
618
619 return true;
620
621fail:
622 if (was_expired != NULL) {
623 *was_expired = expired;
624 }
625 if (state.result && state.blob) {
626 data_blob_free(state.blob);
627 }
628 return false;
629}
630
631struct stabilize_state {
632 bool written;
633};
634static int stabilize_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA val,
635 void *priv);
636
637static int wipe_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA val,
638 void *priv);
639
640/**
641 * Stabilize gencache
642 *
643 * Migrate the clear-if-first gencache data to the stable,
644 * transaction-based gencache.tdb
645 */
646
647bool gencache_stabilize(void)
648{
649 struct stabilize_state state;
650 int res;
651 char *now;
652
653 if (!gencache_init()) {
654 return false;
655 }
656
657 res = tdb_transaction_start_nonblock(cache->tdb);
658 if (res != 0) {
659 if (tdb_error(cache->tdb) == TDB_ERR_NOLOCK)
660 {
661 /*
662 * Someone else already does the stabilize,
663 * this does not have to be done twice
664 */
665 return true;
666 }
667
668 DEBUG(10, ("Could not start transaction on gencache.tdb: "
669 "%s\n", tdb_errorstr(cache->tdb)));
670 return false;
671 }
672
673 res = tdb_lockall_nonblock(cache_notrans->tdb);
674 if (res != 0) {
675 tdb_transaction_cancel(cache->tdb);
676 DEBUG(10, ("Could not get allrecord lock on "
677 "gencache_notrans.tdb: %s\n",
678 tdb_errorstr(cache_notrans->tdb)));
679 return false;
680 }
681
682 state.written = false;
683
684 res = tdb_traverse(cache_notrans->tdb, stabilize_fn, &state);
685 if (res < 0) {
686 tdb_unlockall(cache_notrans->tdb);
687 tdb_transaction_cancel(cache->tdb);
688 return false;
689 }
690
691 if (!state.written) {
692 tdb_unlockall(cache_notrans->tdb);
693 tdb_transaction_cancel(cache->tdb);
694 return true;
695 }
696
697 res = tdb_transaction_commit(cache->tdb);
698 if (res != 0) {
699 DEBUG(10, ("tdb_transaction_commit on gencache.tdb failed: "
700 "%s\n", tdb_errorstr(cache->tdb)));
701 tdb_unlockall(cache_notrans->tdb);
702 return false;
703 }
704
705 res = tdb_traverse(cache_notrans->tdb, wipe_fn, NULL);
706 if (res < 0) {
707 DEBUG(10, ("tdb_traverse with wipe_fn on gencache_notrans.tdb "
708 "failed: %s\n",
709 tdb_errorstr(cache_notrans->tdb)));
710 tdb_unlockall(cache_notrans->tdb);
711 return false;
712 }
713
714 res = tdb_unlockall(cache_notrans->tdb);
715 if (res != 0) {
716 DEBUG(10, ("tdb_unlockall on gencache.tdb failed: "
717 "%s\n", tdb_errorstr(cache->tdb)));
718 return false;
719 }
720
721 now = talloc_asprintf(talloc_tos(), "%d", (int)time(NULL));
722 if (now != NULL) {
723 tdb_store(cache_notrans->tdb, last_stabilize_key(),
724 string_term_tdb_data(now), 0);
725 TALLOC_FREE(now);
726 }
727
728 return true;
729}
730
731static int stabilize_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA val,
732 void *priv)
733{
734 struct stabilize_state *state = (struct stabilize_state *)priv;
735 int res;
736 time_t timeout;
737
738 if (tdb_data_cmp(key, last_stabilize_key()) == 0) {
739 return 0;
740 }
741
742 if (!gencache_pull_timeout((char *)val.dptr, &timeout, NULL)) {
743 DEBUG(10, ("Ignoring invalid entry\n"));
744 return 0;
745 }
746 if ((timeout < time(NULL)) || (val.dsize == 0)) {
747 res = tdb_delete(cache->tdb, key);
748 if (res == 0) {
749 state->written = true;
750 } else if (tdb_error(cache->tdb) == TDB_ERR_NOEXIST) {
751 res = 0;
752 }
753 } else {
754 res = tdb_store(cache->tdb, key, val, 0);
755 if (res == 0) {
756 state->written = true;
757 }
758 }
759
760 if (res != 0) {
761 DEBUG(10, ("Transfer to gencache.tdb failed: %s\n",
762 tdb_errorstr(cache->tdb)));
763 return -1;
764 }
765
766 return 0;
767}
768
769static int wipe_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA val,
770 void *priv)
771{
772 int res;
773 bool ok;
774 time_t timeout;
775
776 res = tdb_data_cmp(key, last_stabilize_key());
777 if (res == 0) {
778 return 0;
779 }
780
781 ok = gencache_pull_timeout((char *)val.dptr, &timeout, NULL);
782 if (!ok) {
783 DEBUG(10, ("Ignoring invalid entry\n"));
784 return 0;
785 }
786
787 res = tdb_delete(tdb, key);
788 if (res != 0) {
789 DEBUG(10, ("tdb_delete from gencache_notrans.tdb failed: "
790 "%s\n", tdb_errorstr(cache_notrans->tdb)));
791 return -1;
792 }
793
794 return 0;
795}
796
797
798/**
799 * Get existing entry from the cache file.
800 *
801 * @param keystr string that represents a key of this entry
802 * @param valstr buffer that is allocated and filled with the entry value
803 * buffer's disposing must be done outside
804 * @param timeout pointer to a time_t that is filled with entry's
805 * timeout
806 *
807 * @retval true when entry is successfuly fetched
808 * @retval false for failure
809 **/
810
811bool gencache_get(const char *keystr, TALLOC_CTX *mem_ctx, char **value,
812 time_t *ptimeout)
813{
814 DATA_BLOB blob;
815 bool ret = false;
816
817 ret = gencache_get_data_blob(keystr, mem_ctx, &blob, ptimeout, NULL);
818 if (!ret) {
819 return false;
820 }
821 if ((blob.data == NULL) || (blob.length == 0)) {
822 data_blob_free(&blob);
823 return false;
824 }
825 if (blob.data[blob.length-1] != '\0') {
826 /* Not NULL terminated, can't be a string */
827 data_blob_free(&blob);
828 return false;
829 }
830 if (value) {
831 /*
832 * talloc_move generates a type-punned warning here. As we
833 * leave the function immediately, do a simple talloc_steal.
834 */
835 *value = (char *)talloc_steal(mem_ctx, blob.data);
836 return true;
837 }
838 data_blob_free(&blob);
839 return true;
840}
841
842/**
843 * Set an entry in the cache file. If there's no such
844 * one, then add it.
845 *
846 * @param keystr string that represents a key of this entry
847 * @param value text representation value being cached
848 * @param timeout time when the value is expired
849 *
850 * @retval true when entry is successfuly stored
851 * @retval false on failure
852 **/
853
854bool gencache_set(const char *keystr, const char *value, time_t timeout)
855{
856 DATA_BLOB blob = data_blob_const(value, strlen(value)+1);
857 return gencache_set_data_blob(keystr, &blob, timeout);
858}
859
860struct gencache_iterate_blobs_state {
861 void (*fn)(const char *key, DATA_BLOB value,
862 time_t timeout, void *private_data);
863 const char *pattern;
864 void *private_data;
865 bool in_persistent;
866};
867
868static int gencache_iterate_blobs_fn(struct tdb_context *tdb, TDB_DATA key,
869 TDB_DATA data, void *priv)
870{
871 struct gencache_iterate_blobs_state *state =
872 (struct gencache_iterate_blobs_state *)priv;
873 char *keystr;
874 char *free_key = NULL;
875 time_t timeout;
876 char *endptr;
877
878 if (tdb_data_cmp(key, last_stabilize_key()) == 0) {
879 return 0;
880 }
881 if (state->in_persistent && tdb_exists(cache_notrans->tdb, key)) {
882 return 0;
883 }
884
885 if (key.dptr[key.dsize-1] == '\0') {
886 keystr = (char *)key.dptr;
887 } else {
888 /* ensure 0-termination */
889 keystr = talloc_strndup(talloc_tos(), (char *)key.dptr, key.dsize);
890 free_key = keystr;
891 if (keystr == NULL) {
892 goto done;
893 }
894 }
895
896 if (!gencache_pull_timeout((char *)data.dptr, &timeout, &endptr)) {
897 goto done;
898 }
899 endptr += 1;
900
901 if (fnmatch(state->pattern, keystr, 0) != 0) {
902 goto done;
903 }
904
905 DEBUG(10, ("Calling function with arguments "
906 "(key=[%s], timeout=[%s])\n",
907 keystr, timestring(talloc_tos(), timeout)));
908
909 state->fn(keystr,
910 data_blob_const(endptr,
911 data.dsize - PTR_DIFF(endptr, data.dptr)),
912 timeout, state->private_data);
913
914 done:
915 TALLOC_FREE(free_key);
916 return 0;
917}
918
919void gencache_iterate_blobs(void (*fn)(const char *key, DATA_BLOB value,
920 time_t timeout, void *private_data),
921 void *private_data, const char *pattern)
922{
923 struct gencache_iterate_blobs_state state;
924
925 if ((fn == NULL) || (pattern == NULL) || !gencache_init()) {
926 return;
927 }
928
929 DEBUG(5, ("Searching cache keys with pattern %s\n", pattern));
930
931 state.fn = fn;
932 state.pattern = pattern;
933 state.private_data = private_data;
934
935 state.in_persistent = false;
936 tdb_traverse(cache_notrans->tdb, gencache_iterate_blobs_fn, &state);
937
938 state.in_persistent = true;
939 tdb_traverse(cache->tdb, gencache_iterate_blobs_fn, &state);
940}
941
942/**
943 * Iterate through all entries which key matches to specified pattern
944 *
945 * @param fn pointer to the function that will be supplied with each single
946 * matching cache entry (key, value and timeout) as an arguments
947 * @param data void pointer to an arbitrary data that is passed directly to the fn
948 * function on each call
949 * @param keystr_pattern pattern the existing entries' keys are matched to
950 *
951 **/
952
953struct gencache_iterate_state {
954 void (*fn)(const char *key, const char *value, time_t timeout,
955 void *priv);
956 void *private_data;
957};
958
959static void gencache_iterate_fn(const char *key, DATA_BLOB value,
960 time_t timeout, void *private_data)
961{
962 struct gencache_iterate_state *state =
963 (struct gencache_iterate_state *)private_data;
964 char *valstr;
965 char *free_val = NULL;
966
967 if (value.data[value.length-1] == '\0') {
968 valstr = (char *)value.data;
969 } else {
970 /* ensure 0-termination */
971 valstr = talloc_strndup(talloc_tos(), (char *)value.data, value.length);
972 free_val = valstr;
973 if (valstr == NULL) {
974 goto done;
975 }
976 }
977
978 DEBUG(10, ("Calling function with arguments "
979 "(key=[%s], value=[%s], timeout=[%s])\n",
980 key, valstr, timestring(talloc_tos(), timeout)));
981
982 state->fn(key, valstr, timeout, state->private_data);
983
984 done:
985
986 TALLOC_FREE(free_val);
987}
988
989void gencache_iterate(void (*fn)(const char *key, const char *value,
990 time_t timeout, void *dptr),
991 void *private_data, const char *pattern)
992{
993 struct gencache_iterate_state state;
994
995 if (fn == NULL) {
996 return;
997 }
998 state.fn = fn;
999 state.private_data = private_data;
1000 gencache_iterate_blobs(gencache_iterate_fn, &state, pattern);
1001}
Note: See TracBrowser for help on using the repository browser.