ctdb-scripts: Organize global variables in nfs_ganesha_callout
[Samba.git] / source3 / lib / gencache.c
blob84d273e4f24aaf65d980a71b3b17bfb2729468e2
1 /*
2 Unix SMB/CIFS implementation.
4 Generic, persistent and shared between processes cache mechanism for use
5 by various parts of the Samba code
7 Copyright (C) Rafal Szczesniak 2002
8 Copyright (C) Volker Lendecke 2009
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.
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.
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/>.
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"
30 #undef DBGC_CLASS
31 #define DBGC_CLASS DBGC_TDB
33 #define CACHE_DATA_FMT "%12u/"
35 static struct tdb_wrap *cache;
36 static struct tdb_wrap *cache_notrans;
38 /**
39 * @file gencache.c
40 * @brief Generic, persistent and shared between processes cache mechanism
41 * for use by various parts of the Samba code
43 **/
46 /**
47 * Cache initialisation function. Opens cache tdb file or creates
48 * it if does not exist.
50 * @return true on successful initialisation of the cache or
51 * false on failure
52 **/
54 static bool gencache_init(void)
56 char* cache_fname = NULL;
57 int open_flags = O_RDWR|O_CREAT;
59 /* skip file open if it's already opened */
60 if (cache) {
61 return true;
64 cache_fname = cache_path("gencache.tdb");
65 if (cache_fname == NULL) {
66 return false;
69 DEBUG(5, ("Opening cache file at %s\n", cache_fname));
71 cache = tdb_wrap_open(NULL, cache_fname, 0,
72 TDB_DEFAULT|TDB_INCOMPATIBLE_HASH,
73 open_flags, 0644);
74 if (cache) {
75 int ret;
76 ret = tdb_check(cache->tdb, NULL, NULL);
77 if (ret != 0) {
78 TALLOC_FREE(cache);
81 * Retry with CLEAR_IF_FIRST.
83 * Warning: Converting this to dbwrap won't work
84 * directly. gencache.c does transactions on this tdb,
85 * and dbwrap forbids this for CLEAR_IF_FIRST
86 * databases. tdb does allow transactions on
87 * CLEAR_IF_FIRST databases, so lets use it here to
88 * clean up a broken database.
90 cache = tdb_wrap_open(NULL, cache_fname, 0,
91 TDB_DEFAULT|
92 TDB_INCOMPATIBLE_HASH|
93 TDB_CLEAR_IF_FIRST,
94 open_flags, 0644);
98 if (!cache && (errno == EACCES)) {
99 open_flags = O_RDONLY;
100 cache = tdb_wrap_open(NULL, cache_fname, 0,
101 TDB_DEFAULT|TDB_INCOMPATIBLE_HASH,
102 open_flags, 0644);
103 if (cache) {
104 DEBUG(5, ("gencache_init: Opening cache file %s read-only.\n", cache_fname));
107 TALLOC_FREE(cache_fname);
109 if (!cache) {
110 DEBUG(5, ("Attempt to open gencache.tdb has failed.\n"));
111 return false;
114 cache_fname = lock_path("gencache_notrans.tdb");
115 if (cache_fname == NULL) {
116 TALLOC_FREE(cache);
117 return false;
120 DEBUG(5, ("Opening cache file at %s\n", cache_fname));
122 cache_notrans = tdb_wrap_open(NULL, cache_fname, 0,
123 TDB_CLEAR_IF_FIRST|
124 TDB_INCOMPATIBLE_HASH|
125 TDB_NOSYNC|
126 TDB_MUTEX_LOCKING,
127 open_flags, 0644);
128 if (cache_notrans == NULL) {
129 DEBUG(5, ("Opening %s failed: %s\n", cache_fname,
130 strerror(errno)));
131 TALLOC_FREE(cache_fname);
132 TALLOC_FREE(cache);
133 return false;
135 TALLOC_FREE(cache_fname);
137 return true;
140 static TDB_DATA last_stabilize_key(void)
142 TDB_DATA result;
143 result.dptr = discard_const_p(uint8_t, "@LAST_STABILIZED");
144 result.dsize = 17;
145 return result;
148 struct gencache_have_val_state {
149 time_t new_timeout;
150 const DATA_BLOB *data;
151 bool gotit;
154 static void gencache_have_val_parser(time_t old_timeout, DATA_BLOB data,
155 void *private_data)
157 struct gencache_have_val_state *state =
158 (struct gencache_have_val_state *)private_data;
159 time_t now = time(NULL);
160 int cache_time_left, new_time_left, additional_time;
163 * Excuse the many variables, but these time calculations are
164 * confusing to me. We do not want to write to gencache with a
165 * possibly expensive transaction if we are about to write the same
166 * value, just extending the remaining timeout by less than 10%.
169 cache_time_left = old_timeout - now;
170 if (cache_time_left <= 0) {
172 * timed out, write new value
174 return;
177 new_time_left = state->new_timeout - now;
178 if (new_time_left <= 0) {
180 * Huh -- no new timeout?? Write it.
182 return;
185 if (new_time_left < cache_time_left) {
187 * Someone wants to shorten the timeout. Let it happen.
189 return;
193 * By how much does the new timeout extend the remaining cache time?
195 additional_time = new_time_left - cache_time_left;
197 if (additional_time * 10 < 0) {
199 * Integer overflow. We extend by so much that we have to write it.
201 return;
205 * The comparison below is essentially equivalent to
207 * new_time_left > cache_time_left * 1.10
209 * but without floating point calculations.
212 if (additional_time * 10 > cache_time_left) {
214 * We extend the cache timeout by more than 10%. Do it.
216 return;
220 * Now the more expensive data compare.
222 if (data_blob_cmp(state->data, &data) != 0) {
224 * Write a new value. Certainly do it.
226 return;
230 * Extending the timeout by less than 10% for the same cache value is
231 * not worth the trouble writing a value into gencache under a
232 * possibly expensive transaction.
234 state->gotit = true;
237 static bool gencache_have_val(const char *keystr, const DATA_BLOB *data,
238 time_t timeout)
240 struct gencache_have_val_state state;
242 state.new_timeout = timeout;
243 state.data = data;
244 state.gotit = false;
246 if (!gencache_parse(keystr, gencache_have_val_parser, &state)) {
247 return false;
249 return state.gotit;
252 static int last_stabilize_parser(TDB_DATA key, TDB_DATA data,
253 void *private_data)
255 time_t *last_stabilize = private_data;
257 if ((data.dsize != 0) && (data.dptr[data.dsize-1] == '\0')) {
258 *last_stabilize = atoi((char *)data.dptr);
260 return 0;
264 * Set an entry in the cache file. If there's no such
265 * one, then add it.
267 * @param keystr string that represents a key of this entry
268 * @param blob DATA_BLOB value being cached
269 * @param timeout time when the value is expired
271 * @retval true when entry is successfully stored
272 * @retval false on failure
275 bool gencache_set_data_blob(const char *keystr, const DATA_BLOB *blob,
276 time_t timeout)
278 int ret;
279 fstring hdr;
280 int hdr_len;
281 char* val;
282 time_t last_stabilize;
283 static int writecount;
285 if (tdb_data_cmp(string_term_tdb_data(keystr),
286 last_stabilize_key()) == 0) {
287 DEBUG(10, ("Can't store %s as a key\n", keystr));
288 return false;
291 if ((keystr == NULL) || (blob == NULL)) {
292 return false;
295 if (!gencache_init()) {
296 return false;
299 if ((timeout != 0) && gencache_have_val(keystr, blob, timeout)) {
300 DEBUG(10, ("Did not store value for %s, we already got it\n",
301 keystr));
302 return true;
305 hdr_len = fstr_sprintf(hdr, CACHE_DATA_FMT, (int)timeout);
307 if (hdr_len == -1) {
308 return false;
310 if ((blob->length + (size_t)hdr_len) < blob->length) {
311 return false;
314 val = talloc_array(talloc_tos(), char, hdr_len + blob->length);
315 if (val == NULL) {
316 return false;
319 memcpy(val, hdr, hdr_len);
320 memcpy(val+hdr_len, blob->data, blob->length);
322 DEBUG(10, ("Adding cache entry with key=[%s] and timeout="
323 "[%s] (%d seconds %s)\n", keystr,
324 timestring(talloc_tos(), timeout),
325 (int)(timeout - time(NULL)),
326 timeout > time(NULL) ? "ahead" : "in the past"));
328 ret = tdb_store_bystring(
329 cache_notrans->tdb, keystr,
330 make_tdb_data((uint8_t *)val, talloc_array_length(val)),
332 TALLOC_FREE(val);
334 if (ret != 0) {
335 return false;
339 * Every 100 writes within a single process, stabilize the cache with
340 * a transaction. This is done to prevent a single transaction to
341 * become huge and chew lots of memory.
343 writecount += 1;
344 if (writecount > lp_parm_int(-1, "gencache", "stabilize_count", 100)) {
345 gencache_stabilize();
346 writecount = 0;
347 goto done;
351 * Every 5 minutes, call gencache_stabilize() to not let grow
352 * gencache_notrans.tdb too large.
355 last_stabilize = 0;
357 tdb_parse_record(cache_notrans->tdb, last_stabilize_key(),
358 last_stabilize_parser, &last_stabilize);
360 if ((last_stabilize
361 + lp_parm_int(-1, "gencache", "stabilize_interval", 300))
362 < time(NULL)) {
363 gencache_stabilize();
366 done:
367 return ret == 0;
370 static void gencache_del_parser(time_t timeout, DATA_BLOB blob,
371 void *private_data)
373 if (timeout != 0) {
374 bool *exists = private_data;
375 *exists = true;
380 * Delete one entry from the cache file.
382 * @param keystr string that represents a key of this entry
384 * @retval true upon successful deletion
385 * @retval false in case of failure
388 bool gencache_del(const char *keystr)
390 TDB_DATA key = string_term_tdb_data(keystr);
391 bool exists = false;
392 bool result = false;
393 int ret;
395 if (keystr == NULL) {
396 return false;
399 if (!gencache_init()) {
400 return false;
403 DEBUG(10, ("Deleting cache entry (key=[%s])\n", keystr));
405 ret = tdb_chainlock(cache_notrans->tdb, key);
406 if (ret == -1) {
407 return false;
410 gencache_parse(keystr, gencache_del_parser, &exists);
412 if (exists) {
414 * We delete an element by setting its timeout to
415 * 0. This way we don't have to do a transaction on
416 * gencache.tdb every time we delete an element.
418 result = gencache_set(keystr, "", 0);
421 tdb_chainunlock(cache_notrans->tdb, key);
423 return result;
426 static bool gencache_pull_timeout(uint8_t *val, time_t *pres, char **payload)
428 time_t res;
429 char *endptr;
431 if (val == NULL) {
432 return false;
435 res = strtol((char *)val, &endptr, 10);
437 if ((endptr == NULL) || (*endptr != '/')) {
438 DEBUG(2, ("Invalid gencache data format: %s\n", (char *)val));
439 return false;
441 if (pres != NULL) {
442 *pres = res;
444 if (payload != NULL) {
445 *payload = endptr+1;
447 return true;
450 struct gencache_parse_state {
451 void (*parser)(time_t timeout, DATA_BLOB blob, void *private_data);
452 void *private_data;
453 bool copy_to_notrans;
456 static int gencache_parse_fn(TDB_DATA key, TDB_DATA data, void *private_data)
458 struct gencache_parse_state *state;
459 DATA_BLOB blob;
460 time_t t;
461 char *payload;
462 bool ret;
464 if (data.dptr == NULL) {
465 return -1;
467 ret = gencache_pull_timeout(data.dptr, &t, &payload);
468 if (!ret) {
469 return -1;
471 state = (struct gencache_parse_state *)private_data;
472 blob = data_blob_const(
473 payload, data.dsize - PTR_DIFF(payload, data.dptr));
474 state->parser(t, blob, state->private_data);
476 if (state->copy_to_notrans) {
477 tdb_store(cache_notrans->tdb, key, data, 0);
480 return 0;
483 bool gencache_parse(const char *keystr,
484 void (*parser)(time_t timeout, DATA_BLOB blob,
485 void *private_data),
486 void *private_data)
488 struct gencache_parse_state state;
489 TDB_DATA key = string_term_tdb_data(keystr);
490 int ret;
492 if (keystr == NULL) {
493 return false;
495 if (tdb_data_cmp(key, last_stabilize_key()) == 0) {
496 return false;
498 if (!gencache_init()) {
499 return false;
502 state.parser = parser;
503 state.private_data = private_data;
504 state.copy_to_notrans = false;
506 ret = tdb_chainlock(cache_notrans->tdb, key);
507 if (ret != 0) {
508 return false;
511 ret = tdb_parse_record(cache_notrans->tdb, key,
512 gencache_parse_fn, &state);
513 if (ret == 0) {
514 tdb_chainunlock(cache_notrans->tdb, key);
515 return true;
518 state.copy_to_notrans = true;
520 ret = tdb_parse_record(cache->tdb, key, gencache_parse_fn, &state);
522 if ((ret == -1) && (tdb_error(cache->tdb) == TDB_ERR_NOEXIST)) {
524 * The record does not exist. Set a delete-marker in
525 * gencache_notrans, so that we don't have to look at
526 * the fcntl-based cache again.
528 gencache_set(keystr, "", 0);
531 tdb_chainunlock(cache_notrans->tdb, key);
533 return (ret == 0);
536 struct gencache_get_data_blob_state {
537 TALLOC_CTX *mem_ctx;
538 DATA_BLOB *blob;
539 time_t timeout;
540 bool result;
543 static void gencache_get_data_blob_parser(time_t timeout, DATA_BLOB blob,
544 void *private_data)
546 struct gencache_get_data_blob_state *state =
547 (struct gencache_get_data_blob_state *)private_data;
549 if (timeout == 0) {
550 state->result = false;
551 return;
553 state->timeout = timeout;
555 if (state->blob == NULL) {
556 state->result = true;
557 return;
560 *state->blob = data_blob_talloc(state->mem_ctx, blob.data,
561 blob.length);
562 if (state->blob->data == NULL) {
563 state->result = false;
564 return;
566 state->result = true;
570 * Get existing entry from the cache file.
572 * @param keystr string that represents a key of this entry
573 * @param blob DATA_BLOB that is filled with entry's blob
574 * @param timeout pointer to a time_t that is filled with entry's
575 * timeout
577 * @retval true when entry is successfuly fetched
578 * @retval false for failure
581 bool gencache_get_data_blob(const char *keystr, TALLOC_CTX *mem_ctx,
582 DATA_BLOB *blob,
583 time_t *timeout, bool *was_expired)
585 struct gencache_get_data_blob_state state;
586 bool expired = false;
588 state.result = false;
589 state.mem_ctx = mem_ctx;
590 state.blob = blob;
592 if (!gencache_parse(keystr, gencache_get_data_blob_parser, &state)) {
593 goto fail;
595 if (!state.result) {
596 goto fail;
598 if (state.timeout <= time(NULL)) {
600 * We're expired, delete the entry. We can't use gencache_del
601 * here, because that uses gencache_get_data_blob for checking
602 * the existence of a record. We know the thing exists and
603 * directly store an empty value with 0 timeout.
605 gencache_set(keystr, "", 0);
606 expired = true;
607 goto fail;
609 if (timeout) {
610 *timeout = state.timeout;
613 return true;
615 fail:
616 if (was_expired != NULL) {
617 *was_expired = expired;
619 if (state.result && state.blob) {
620 data_blob_free(state.blob);
622 return false;
625 struct stabilize_state {
626 bool written;
628 static int stabilize_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA val,
629 void *priv);
631 static int wipe_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA val,
632 void *priv);
635 * Stabilize gencache
637 * Migrate the clear-if-first gencache data to the stable,
638 * transaction-based gencache.tdb
641 bool gencache_stabilize(void)
643 struct stabilize_state state;
644 int res;
645 char *now;
647 if (!gencache_init()) {
648 return false;
651 res = tdb_transaction_start_nonblock(cache->tdb);
652 if (res != 0) {
653 if (tdb_error(cache->tdb) == TDB_ERR_NOLOCK)
656 * Someone else already does the stabilize,
657 * this does not have to be done twice
659 return true;
662 DEBUG(10, ("Could not start transaction on gencache.tdb: "
663 "%s\n", tdb_errorstr(cache->tdb)));
664 return false;
667 res = tdb_lockall(cache_notrans->tdb);
668 if (res != 0) {
669 tdb_transaction_cancel(cache->tdb);
670 DEBUG(10, ("Could not get allrecord lock on "
671 "gencache_notrans.tdb: %s\n",
672 tdb_errorstr(cache_notrans->tdb)));
673 return false;
676 state.written = false;
678 res = tdb_traverse(cache_notrans->tdb, stabilize_fn, &state);
679 if (res < 0) {
680 tdb_unlockall(cache_notrans->tdb);
681 tdb_transaction_cancel(cache->tdb);
682 return false;
685 if (!state.written) {
686 tdb_unlockall(cache_notrans->tdb);
687 tdb_transaction_cancel(cache->tdb);
688 return true;
691 res = tdb_transaction_commit(cache->tdb);
692 if (res != 0) {
693 DEBUG(10, ("tdb_transaction_commit on gencache.tdb failed: "
694 "%s\n", tdb_errorstr(cache->tdb)));
695 tdb_unlockall(cache_notrans->tdb);
696 return false;
699 res = tdb_traverse(cache_notrans->tdb, wipe_fn, NULL);
700 if (res < 0) {
701 DEBUG(10, ("tdb_traverse with wipe_fn on gencache_notrans.tdb "
702 "failed: %s\n",
703 tdb_errorstr(cache_notrans->tdb)));
704 tdb_unlockall(cache_notrans->tdb);
705 return false;
708 res = tdb_unlockall(cache_notrans->tdb);
709 if (res != 0) {
710 DEBUG(10, ("tdb_unlockall on gencache.tdb failed: "
711 "%s\n", tdb_errorstr(cache->tdb)));
712 return false;
715 now = talloc_asprintf(talloc_tos(), "%d", (int)time(NULL));
716 if (now != NULL) {
717 tdb_store(cache_notrans->tdb, last_stabilize_key(),
718 string_term_tdb_data(now), 0);
719 TALLOC_FREE(now);
722 return true;
725 static int stabilize_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA val,
726 void *priv)
728 struct stabilize_state *state = (struct stabilize_state *)priv;
729 int res;
730 time_t timeout;
732 if (tdb_data_cmp(key, last_stabilize_key()) == 0) {
733 return 0;
736 if (!gencache_pull_timeout(val.dptr, &timeout, NULL)) {
737 DEBUG(10, ("Ignoring invalid entry\n"));
738 return 0;
740 if ((timeout < time(NULL)) || (val.dsize == 0)) {
741 res = tdb_delete(cache->tdb, key);
742 if (res == 0) {
743 state->written = true;
744 } else if (tdb_error(cache->tdb) == TDB_ERR_NOEXIST) {
745 res = 0;
747 } else {
748 res = tdb_store(cache->tdb, key, val, 0);
749 if (res == 0) {
750 state->written = true;
754 if (res != 0) {
755 DEBUG(10, ("Transfer to gencache.tdb failed: %s\n",
756 tdb_errorstr(cache->tdb)));
757 return -1;
760 return 0;
763 static int wipe_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA val,
764 void *priv)
766 int res;
767 bool ok;
768 time_t timeout;
770 res = tdb_data_cmp(key, last_stabilize_key());
771 if (res == 0) {
772 return 0;
775 ok = gencache_pull_timeout(val.dptr, &timeout, NULL);
776 if (!ok) {
777 DEBUG(10, ("Ignoring invalid entry\n"));
778 return 0;
781 res = tdb_delete(tdb, key);
782 if (res != 0) {
783 DEBUG(10, ("tdb_delete from gencache_notrans.tdb failed: "
784 "%s\n", tdb_errorstr(cache_notrans->tdb)));
785 return -1;
788 return 0;
793 * Get existing entry from the cache file.
795 * @param keystr string that represents a key of this entry
796 * @param valstr buffer that is allocated and filled with the entry value
797 * buffer's disposing must be done outside
798 * @param timeout pointer to a time_t that is filled with entry's
799 * timeout
801 * @retval true when entry is successfully fetched
802 * @retval false for failure
805 bool gencache_get(const char *keystr, TALLOC_CTX *mem_ctx, char **value,
806 time_t *ptimeout)
808 DATA_BLOB blob;
809 bool ret = false;
811 ret = gencache_get_data_blob(keystr, mem_ctx, &blob, ptimeout, NULL);
812 if (!ret) {
813 return false;
815 if ((blob.data == NULL) || (blob.length == 0)) {
816 data_blob_free(&blob);
817 return false;
819 if (blob.data[blob.length-1] != '\0') {
820 /* Not NULL terminated, can't be a string */
821 data_blob_free(&blob);
822 return false;
824 if (value) {
826 * talloc_move generates a type-punned warning here. As we
827 * leave the function immediately, do a simple talloc_steal.
829 *value = (char *)talloc_steal(mem_ctx, blob.data);
830 return true;
832 data_blob_free(&blob);
833 return true;
837 * Set an entry in the cache file. If there's no such
838 * one, then add it.
840 * @param keystr string that represents a key of this entry
841 * @param value text representation value being cached
842 * @param timeout time when the value is expired
844 * @retval true when entry is successfuly stored
845 * @retval false on failure
848 bool gencache_set(const char *keystr, const char *value, time_t timeout)
850 DATA_BLOB blob = data_blob_const(value, strlen(value)+1);
851 return gencache_set_data_blob(keystr, &blob, timeout);
854 struct gencache_iterate_blobs_state {
855 void (*fn)(const char *key, DATA_BLOB value,
856 time_t timeout, void *private_data);
857 const char *pattern;
858 void *private_data;
859 bool in_persistent;
862 static int gencache_iterate_blobs_fn(struct tdb_context *tdb, TDB_DATA key,
863 TDB_DATA data, void *priv)
865 struct gencache_iterate_blobs_state *state =
866 (struct gencache_iterate_blobs_state *)priv;
867 char *keystr;
868 char *free_key = NULL;
869 time_t timeout;
870 char *payload;
872 if (tdb_data_cmp(key, last_stabilize_key()) == 0) {
873 return 0;
875 if (state->in_persistent && tdb_exists(cache_notrans->tdb, key)) {
876 return 0;
879 if (key.dptr[key.dsize-1] == '\0') {
880 keystr = (char *)key.dptr;
881 } else {
882 /* ensure 0-termination */
883 keystr = talloc_strndup(talloc_tos(), (char *)key.dptr, key.dsize);
884 free_key = keystr;
885 if (keystr == NULL) {
886 goto done;
890 if (!gencache_pull_timeout(data.dptr, &timeout, &payload)) {
891 goto done;
894 if (timeout == 0) {
895 /* delete marker */
896 goto done;
899 if (fnmatch(state->pattern, keystr, 0) != 0) {
900 goto done;
903 DEBUG(10, ("Calling function with arguments "
904 "(key=[%s], timeout=[%s])\n",
905 keystr, timestring(talloc_tos(), timeout)));
907 state->fn(keystr,
908 data_blob_const(payload,
909 data.dsize - PTR_DIFF(payload, data.dptr)),
910 timeout, state->private_data);
912 done:
913 TALLOC_FREE(free_key);
914 return 0;
917 void gencache_iterate_blobs(void (*fn)(const char *key, DATA_BLOB value,
918 time_t timeout, void *private_data),
919 void *private_data, const char *pattern)
921 struct gencache_iterate_blobs_state state;
923 if ((fn == NULL) || (pattern == NULL) || !gencache_init()) {
924 return;
927 DEBUG(5, ("Searching cache keys with pattern %s\n", pattern));
929 state.fn = fn;
930 state.pattern = pattern;
931 state.private_data = private_data;
933 state.in_persistent = false;
934 tdb_traverse(cache_notrans->tdb, gencache_iterate_blobs_fn, &state);
936 state.in_persistent = true;
937 tdb_traverse(cache->tdb, gencache_iterate_blobs_fn, &state);
941 * Iterate through all entries which key matches to specified pattern
943 * @param fn pointer to the function that will be supplied with each single
944 * matching cache entry (key, value and timeout) as an arguments
945 * @param data void pointer to an arbitrary data that is passed directly to the fn
946 * function on each call
947 * @param keystr_pattern pattern the existing entries' keys are matched to
951 struct gencache_iterate_state {
952 void (*fn)(const char *key, const char *value, time_t timeout,
953 void *priv);
954 void *private_data;
957 static void gencache_iterate_fn(const char *key, DATA_BLOB value,
958 time_t timeout, void *private_data)
960 struct gencache_iterate_state *state =
961 (struct gencache_iterate_state *)private_data;
962 char *valstr;
963 char *free_val = NULL;
965 if (value.data[value.length-1] == '\0') {
966 valstr = (char *)value.data;
967 } else {
968 /* ensure 0-termination */
969 valstr = talloc_strndup(talloc_tos(), (char *)value.data, value.length);
970 free_val = valstr;
971 if (valstr == NULL) {
972 goto done;
976 DEBUG(10, ("Calling function with arguments "
977 "(key=[%s], value=[%s], timeout=[%s])\n",
978 key, valstr, timestring(talloc_tos(), timeout)));
980 state->fn(key, valstr, timeout, state->private_data);
982 done:
984 TALLOC_FREE(free_val);
987 void gencache_iterate(void (*fn)(const char *key, const char *value,
988 time_t timeout, void *dptr),
989 void *private_data, const char *pattern)
991 struct gencache_iterate_state state;
993 if (fn == NULL) {
994 return;
996 state.fn = fn;
997 state.private_data = private_data;
998 gencache_iterate_blobs(gencache_iterate_fn, &state, pattern);