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