gencache: fix an extra newline in a DEBUG message in gencache_iterate_fn()
[obnox/samba/samba-obnox.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,
291                    ctime(&timeout),
292                    (int)(timeout - time(NULL)), 
293                    timeout > time(NULL) ? "ahead" : "in the past"));
294
295         ret = tdb_store_bystring(
296                 cache_notrans, keystr,
297                 make_tdb_data((uint8_t *)val, talloc_array_length(val)),
298                 0);
299         TALLOC_FREE(val);
300
301         if (ret != 0) {
302                 return false;
303         }
304
305         /*
306          * Every 100 writes within a single process, stabilize the cache with
307          * a transaction. This is done to prevent a single transaction to
308          * become huge and chew lots of memory.
309          */
310         writecount += 1;
311         if (writecount > lp_parm_int(-1, "gencache", "stabilize_count", 100)) {
312                 gencache_stabilize();
313                 writecount = 0;
314                 goto done;
315         }
316
317         /*
318          * Every 5 minutes, call gencache_stabilize() to not let grow
319          * gencache_notrans.tdb too large.
320          */
321
322         last_stabilize = 0;
323         databuf = tdb_fetch_compat(cache_notrans, last_stabilize_key());
324         if ((databuf.dptr != NULL)
325             && (databuf.dptr[databuf.dsize-1] == '\0')) {
326                 last_stabilize = atoi((char *)databuf.dptr);
327                 SAFE_FREE(databuf.dptr);
328         }
329         if ((last_stabilize
330              + lp_parm_int(-1, "gencache", "stabilize_interval", 300))
331             < time(NULL)) {
332                 gencache_stabilize();
333         }
334
335 done:
336         return ret == 0;
337 }
338
339 /**
340  * Delete one entry from the cache file.
341  *
342  * @param keystr string that represents a key of this entry
343  *
344  * @retval true upon successful deletion
345  * @retval false in case of failure
346  **/
347
348 bool gencache_del(const char *keystr)
349 {
350         bool exists, was_expired;
351         bool ret = false;
352         DATA_BLOB value;
353
354         if (keystr == NULL) {
355                 return false;
356         }
357
358         if (!gencache_init()) return False;     
359
360         DEBUG(10, ("Deleting cache entry (key=[%s])\n", keystr));
361
362         /*
363          * We delete an element by setting its timeout to 0. This way we don't
364          * have to do a transaction on gencache.tdb every time we delete an
365          * element.
366          */
367
368         exists = gencache_get_data_blob(keystr, &value, NULL, &was_expired);
369
370         if (!exists && was_expired) {
371                 /*
372                  * gencache_get_data_blob has implicitly deleted this
373                  * entry, so we have to return success here.
374                  */
375                 return true;
376         }
377
378         if (exists) {
379                 data_blob_free(&value);
380                 ret = gencache_set(keystr, "", 0);
381         }
382         return ret;
383 }
384
385 static bool gencache_pull_timeout(char *val, time_t *pres, char **pendptr)
386 {
387         time_t res;
388         char *endptr;
389
390         if (val == NULL) {
391                 return false;
392         }
393
394         res = strtol(val, &endptr, 10);
395
396         if ((endptr == NULL) || (*endptr != '/')) {
397                 DEBUG(2, ("Invalid gencache data format: %s\n", val));
398                 return false;
399         }
400         if (pres != NULL) {
401                 *pres = res;
402         }
403         if (pendptr != NULL) {
404                 *pendptr = endptr;
405         }
406         return true;
407 }
408
409 struct gencache_parse_state {
410         void (*parser)(time_t timeout, DATA_BLOB blob, void *private_data);
411         void *private_data;
412 };
413
414 static int gencache_parse_fn(TDB_DATA key, TDB_DATA data, void *private_data)
415 {
416         struct gencache_parse_state *state;
417         DATA_BLOB blob;
418         time_t t;
419         char *endptr;
420         bool ret;
421
422         if (data.dptr == NULL) {
423                 return -1;
424         }
425         ret = gencache_pull_timeout((char *)data.dptr, &t, &endptr);
426         if (!ret) {
427                 return -1;
428         }
429         state = (struct gencache_parse_state *)private_data;
430         blob = data_blob_const(
431                 endptr+1, data.dsize - PTR_DIFF(endptr+1, data.dptr));
432         state->parser(t, blob, state->private_data);
433         return 0;
434 }
435
436 bool gencache_parse(const char *keystr,
437                     void (*parser)(time_t timeout, DATA_BLOB blob,
438                                    void *private_data),
439                     void *private_data)
440 {
441         struct gencache_parse_state state;
442         TDB_DATA key;
443         int ret;
444
445         if (keystr == NULL) {
446                 return false;
447         }
448         if (tdb_data_cmp(string_term_tdb_data(keystr),
449                          last_stabilize_key()) == 0) {
450                 return false;
451         }
452         if (!gencache_init()) {
453                 return false;
454         }
455
456         key = string_term_tdb_data(keystr);
457         state.parser = parser;
458         state.private_data = private_data;
459
460         ret = tdb_parse_record(cache_notrans, key, gencache_parse_fn, &state);
461         if (ret == 0) {
462                 return true;
463         }
464         ret = tdb_parse_record(cache, key, gencache_parse_fn, &state);
465         return (ret == 0);
466 }
467
468 struct gencache_get_data_blob_state {
469         DATA_BLOB *blob;
470         time_t timeout;
471         bool result;
472 };
473
474 static void gencache_get_data_blob_parser(time_t timeout, DATA_BLOB blob,
475                                           void *private_data)
476 {
477         struct gencache_get_data_blob_state *state =
478                 (struct gencache_get_data_blob_state *)private_data;
479
480         if (timeout == 0) {
481                 state->result = false;
482                 return;
483         }
484         state->timeout = timeout;
485
486         if (state->blob == NULL) {
487                 state->result = true;
488                 return;
489         }
490
491         *state->blob = data_blob(blob.data, blob.length);
492         if (state->blob->data == NULL) {
493                 state->result = false;
494                 return;
495         }
496         state->result = true;
497 }
498
499 /**
500  * Get existing entry from the cache file.
501  *
502  * @param keystr string that represents a key of this entry
503  * @param blob DATA_BLOB that is filled with entry's blob
504  * @param timeout pointer to a time_t that is filled with entry's
505  *        timeout
506  *
507  * @retval true when entry is successfuly fetched
508  * @retval False for failure
509  **/
510
511 bool gencache_get_data_blob(const char *keystr, DATA_BLOB *blob,
512                             time_t *timeout, bool *was_expired)
513 {
514         struct gencache_get_data_blob_state state;
515         bool expired = false;
516
517         state.result = false;
518         state.blob = blob;
519
520         if (!gencache_parse(keystr, gencache_get_data_blob_parser, &state)) {
521                 goto fail;
522         }
523         if (!state.result) {
524                 goto fail;
525         }
526         if (state.timeout <= time(NULL)) {
527                 /*
528                  * We're expired, delete the entry. We can't use gencache_del
529                  * here, because that uses gencache_get_data_blob for checking
530                  * the existence of a record. We know the thing exists and
531                  * directly store an empty value with 0 timeout.
532                  */
533                 gencache_set(keystr, "", 0);
534                 expired = true;
535                 goto fail;
536         }
537         if (timeout) {
538                 *timeout = state.timeout;
539         }
540
541         return True;
542
543 fail:
544         if (was_expired != NULL) {
545                 *was_expired = expired;
546         }
547         if (state.result && state.blob) {
548                 data_blob_free(state.blob);
549         }
550         return false;
551
552
553 struct stabilize_state {
554         bool written;
555         bool error;
556 };
557 static int stabilize_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA val,
558                         void *priv);
559
560 /**
561  * Stabilize gencache
562  *
563  * Migrate the clear-if-first gencache data to the stable,
564  * transaction-based gencache.tdb
565  */
566
567 bool gencache_stabilize(void)
568 {
569         struct stabilize_state state;
570         int res;
571         char *now;
572
573         if (!gencache_init()) {
574                 return false;
575         }
576
577         res = tdb_transaction_start_nonblock(cache);
578         if (res != 0) {
579                 if (tdb_error(cache) == TDB_ERR_NOLOCK)
580                 {
581                         /*
582                          * Someone else already does the stabilize,
583                          * this does not have to be done twice
584                          */
585                         return true;
586                 }
587
588                 DEBUG(10, ("Could not start transaction on gencache.tdb: "
589                            "%s\n", tdb_errorstr_compat(cache)));
590                 return false;
591         }
592         res = tdb_transaction_start(cache_notrans);
593         if (res != 0) {
594                 tdb_transaction_cancel(cache);
595                 DEBUG(10, ("Could not start transaction on "
596                            "gencache_notrans.tdb: %s\n",
597                            tdb_errorstr_compat(cache_notrans)));
598                 return false;
599         }
600
601         state.error = false;
602         state.written = false;
603
604         res = tdb_traverse(cache_notrans, stabilize_fn, &state);
605         if ((res < 0) || state.error) {
606                 tdb_transaction_cancel(cache_notrans);
607                 tdb_transaction_cancel(cache);
608                 return false;
609         }
610
611         if (!state.written) {
612                 tdb_transaction_cancel(cache_notrans);
613                 tdb_transaction_cancel(cache);
614                 return true;
615         }
616
617         res = tdb_transaction_commit(cache);
618         if (res != 0) {
619                 DEBUG(10, ("tdb_transaction_commit on gencache.tdb failed: "
620                            "%s\n", tdb_errorstr_compat(cache)));
621                 tdb_transaction_cancel(cache_notrans);
622                 return false;
623         }
624
625         res = tdb_transaction_commit(cache_notrans);
626         if (res != 0) {
627                 DEBUG(10, ("tdb_transaction_commit on gencache.tdb failed: "
628                            "%s\n", tdb_errorstr_compat(cache)));
629                 return false;
630         }
631
632         now = talloc_asprintf(talloc_tos(), "%d", (int)time(NULL));
633         if (now != NULL) {
634                 tdb_store(cache_notrans, last_stabilize_key(),
635                           string_term_tdb_data(now), 0);
636                 TALLOC_FREE(now);
637         }
638
639         return true;
640 }
641
642 static int stabilize_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA val,
643                         void *priv)
644 {
645         struct stabilize_state *state = (struct stabilize_state *)priv;
646         int res;
647         time_t timeout;
648
649         if (tdb_data_cmp(key, last_stabilize_key()) == 0) {
650                 return 0;
651         }
652
653         if (!gencache_pull_timeout((char *)val.dptr, &timeout, NULL)) {
654                 DEBUG(10, ("Ignoring invalid entry\n"));
655                 return 0;
656         }
657         if ((timeout < time(NULL)) || (val.dsize == 0)) {
658                 res = tdb_delete(cache, key);
659                 if ((res != 0) && (tdb_error(cache) == TDB_ERR_NOEXIST)) {
660                         res = 0;
661                 } else {
662                         state->written = true;
663                 }
664         } else {
665                 res = tdb_store(cache, key, val, 0);
666                 if (res == 0) {
667                         state->written = true;
668                 }
669         }
670
671         if (res != 0) {
672                 DEBUG(10, ("Transfer to gencache.tdb failed: %s\n",
673                            tdb_errorstr_compat(cache)));
674                 state->error = true;
675                 return -1;
676         }
677
678         if (tdb_delete(cache_notrans, key) != 0) {
679                 DEBUG(10, ("tdb_delete from gencache_notrans.tdb failed: "
680                            "%s\n", tdb_errorstr_compat(cache_notrans)));
681                 state->error = true;
682                 return -1;
683         }
684         return 0;
685 }
686
687 /**
688  * Get existing entry from the cache file.
689  *
690  * @param keystr string that represents a key of this entry
691  * @param valstr buffer that is allocated and filled with the entry value
692  *        buffer's disposing must be done outside
693  * @param timeout pointer to a time_t that is filled with entry's
694  *        timeout
695  *
696  * @retval true when entry is successfuly fetched
697  * @retval False for failure
698  **/
699
700 bool gencache_get(const char *keystr, char **value, time_t *ptimeout)
701 {
702         DATA_BLOB blob;
703         bool ret = False;
704
705         ret = gencache_get_data_blob(keystr, &blob, ptimeout, NULL);
706         if (!ret) {
707                 return false;
708         }
709         if ((blob.data == NULL) || (blob.length == 0)) {
710                 SAFE_FREE(blob.data);
711                 return false;
712         }
713         if (blob.data[blob.length-1] != '\0') {
714                 /* Not NULL terminated, can't be a string */
715                 SAFE_FREE(blob.data);
716                 return false;
717         }
718         if (value) {
719                 *value = SMB_STRDUP((char *)blob.data);
720                 data_blob_free(&blob);
721                 if (*value == NULL) {
722                         return false;
723                 }
724                 return true;
725         }
726         data_blob_free(&blob);
727         return true;
728 }
729
730 /**
731  * Set an entry in the cache file. If there's no such
732  * one, then add it.
733  *
734  * @param keystr string that represents a key of this entry
735  * @param value text representation value being cached
736  * @param timeout time when the value is expired
737  *
738  * @retval true when entry is successfuly stored
739  * @retval false on failure
740  **/
741
742 bool gencache_set(const char *keystr, const char *value, time_t timeout)
743 {
744         DATA_BLOB blob = data_blob_const(value, strlen(value)+1);
745         return gencache_set_data_blob(keystr, &blob, timeout);
746 }
747
748 struct gencache_iterate_blobs_state {
749         void (*fn)(const char *key, DATA_BLOB value,
750                    time_t timeout, void *private_data);
751         const char *pattern;
752         void *private_data;
753         bool in_persistent;
754 };
755
756 static int gencache_iterate_blobs_fn(struct tdb_context *tdb, TDB_DATA key,
757                                      TDB_DATA data, void *priv)
758 {
759         struct gencache_iterate_blobs_state *state =
760                 (struct gencache_iterate_blobs_state *)priv;
761         char *keystr;
762         char *free_key = NULL;
763         time_t timeout;
764         char *endptr;
765
766         if (tdb_data_cmp(key, last_stabilize_key()) == 0) {
767                 return 0;
768         }
769         if (state->in_persistent && tdb_exists(cache_notrans, key)) {
770                 return 0;
771         }
772
773         if (key.dptr[key.dsize-1] == '\0') {
774                 keystr = (char *)key.dptr;
775         } else {
776                 /* ensure 0-termination */
777                 keystr = SMB_STRNDUP((char *)key.dptr, key.dsize);
778                 free_key = keystr;
779         }
780
781         if (!gencache_pull_timeout((char *)data.dptr, &timeout, &endptr)) {
782                 goto done;
783         }
784         endptr += 1;
785
786         if (fnmatch(state->pattern, keystr, 0) != 0) {
787                 goto done;
788         }
789
790         DEBUG(10, ("Calling function with arguments "
791                    "(key=[%s], timeout=[%s])\n",
792                    keystr, timestring(talloc_tos(), timeout)));
793
794         state->fn(keystr,
795                   data_blob_const(endptr,
796                                   data.dsize - PTR_DIFF(endptr, data.dptr)),
797                   timeout, state->private_data);
798
799  done:
800         SAFE_FREE(free_key);
801         return 0;
802 }
803
804 void gencache_iterate_blobs(void (*fn)(const char *key, DATA_BLOB value,
805                                        time_t timeout, void *private_data),
806                             void *private_data, const char *pattern)
807 {
808         struct gencache_iterate_blobs_state state;
809
810         if ((fn == NULL) || (pattern == NULL) || !gencache_init()) {
811                 return;
812         }
813
814         DEBUG(5, ("Searching cache keys with pattern %s\n", pattern));
815
816         state.fn = fn;
817         state.pattern = pattern;
818         state.private_data = private_data;
819
820         state.in_persistent = false;
821         tdb_traverse(cache_notrans, gencache_iterate_blobs_fn, &state);
822
823         state.in_persistent = true;
824         tdb_traverse(cache, gencache_iterate_blobs_fn, &state);
825 }
826
827 /**
828  * Iterate through all entries which key matches to specified pattern
829  *
830  * @param fn pointer to the function that will be supplied with each single
831  *        matching cache entry (key, value and timeout) as an arguments
832  * @param data void pointer to an arbitrary data that is passed directly to the fn
833  *        function on each call
834  * @param keystr_pattern pattern the existing entries' keys are matched to
835  *
836  **/
837
838 struct gencache_iterate_state {
839         void (*fn)(const char *key, const char *value, time_t timeout,
840                    void *priv);
841         void *private_data;
842 };
843
844 static void gencache_iterate_fn(const char *key, DATA_BLOB value,
845                                 time_t timeout, void *private_data)
846 {
847         struct gencache_iterate_state *state =
848                 (struct gencache_iterate_state *)private_data;
849         char *valstr;
850         char *free_val = NULL;
851
852         if (value.data[value.length-1] == '\0') {
853                 valstr = (char *)value.data;
854         } else {
855                 /* ensure 0-termination */
856                 valstr = SMB_STRNDUP((char *)value.data, value.length);
857                 free_val = valstr;
858         }
859
860         DEBUG(10, ("Calling function with arguments "
861                    "(key=[%s], value=[%s], timeout=[%s])\n",
862                    key, valstr, timestring(talloc_tos(), timeout)));
863
864         state->fn(key, valstr, timeout, state->private_data);
865
866         SAFE_FREE(free_val);
867 }
868
869 void gencache_iterate(void (*fn)(const char *key, const char *value,
870                                  time_t timeout, void *dptr),
871                       void *private_data, const char *pattern)
872 {
873         struct gencache_iterate_state state;
874
875         if (fn == NULL) {
876                 return;
877         }
878         state.fn = fn;
879         state.private_data = private_data;
880         gencache_iterate_blobs(gencache_iterate_fn, &state, pattern);
881 }