7a86063de5d77ae4ed8a01208c3174dc4836639b
[metze/samba/wip.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 CLEAR_IF_FIRST\n",
85                                   cache_fname));
86                         cache = tdb_open_log(cache_fname, 0, TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH, open_flags, 0644);
87                         if (cache) {
88                                 tdb_close(cache);
89                                 cache = NULL;
90                                 goto again;
91                         }
92                 }
93         }
94
95         if (!cache && (errno == EACCES)) {
96                 open_flags = O_RDONLY;
97                 cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT|TDB_INCOMPATIBLE_HASH, open_flags,
98                                      0644);
99                 if (cache) {
100                         DEBUG(5, ("gencache_init: Opening cache file %s read-only.\n", cache_fname));
101                 }
102         }
103
104         if (!cache) {
105                 DEBUG(5, ("Attempt to open gencache.tdb has failed.\n"));
106                 return False;
107         }
108
109         cache_fname = lock_path("gencache_notrans.tdb");
110
111         DEBUG(5, ("Opening cache file at %s\n", cache_fname));
112
113         cache_notrans = tdb_open_log(cache_fname, 0, TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
114                                      open_flags, 0644);
115         if (cache_notrans == NULL) {
116                 DEBUG(5, ("Opening %s failed: %s\n", cache_fname,
117                           strerror(errno)));
118                 tdb_close(cache);
119                 cache = NULL;
120                 return false;
121         }
122
123         return True;
124 }
125
126 static TDB_DATA last_stabilize_key(void)
127 {
128         TDB_DATA result;
129         result.dptr = discard_const_p(uint8_t, "@LAST_STABILIZED");
130         result.dsize = 17;
131         return result;
132 }
133
134 /**
135  * Set an entry in the cache file. If there's no such
136  * one, then add it.
137  *
138  * @param keystr string that represents a key of this entry
139  * @param blob DATA_BLOB value being cached
140  * @param timeout time when the value is expired
141  *
142  * @retval true when entry is successfuly stored
143  * @retval false on failure
144  **/
145
146 bool gencache_set_data_blob(const char *keystr, const DATA_BLOB *blob,
147                             time_t timeout)
148 {
149         int ret;
150         TDB_DATA databuf;
151         char* val;
152         time_t last_stabilize;
153         static int writecount;
154
155         if (tdb_data_cmp(string_term_tdb_data(keystr),
156                          last_stabilize_key()) == 0) {
157                 DEBUG(10, ("Can't store %s as a key\n", keystr));
158                 return false;
159         }
160
161         if ((keystr == NULL) || (blob == NULL)) {
162                 return false;
163         }
164
165         if (!gencache_init()) return False;
166
167         val = talloc_asprintf(talloc_tos(), CACHE_DATA_FMT, (int)timeout);
168         if (val == NULL) {
169                 return False;
170         }
171         val = talloc_realloc(NULL, val, char, talloc_array_length(val)-1);
172         if (val == NULL) {
173                 return false;
174         }
175         val = (char *)talloc_append_blob(NULL, val, *blob);
176         if (val == NULL) {
177                 return false;
178         }
179
180         DEBUG(10, ("Adding cache entry with key = %s and timeout ="
181                    " %s (%d seconds %s)\n", keystr, ctime(&timeout),
182                    (int)(timeout - time(NULL)), 
183                    timeout > time(NULL) ? "ahead" : "in the past"));
184
185         ret = tdb_store_bystring(
186                 cache_notrans, keystr,
187                 make_tdb_data((uint8_t *)val, talloc_array_length(val)),
188                 0);
189         TALLOC_FREE(val);
190
191         if (ret != 0) {
192                 return false;
193         }
194
195         /*
196          * Every 100 writes within a single process, stabilize the cache with
197          * a transaction. This is done to prevent a single transaction to
198          * become huge and chew lots of memory.
199          */
200         writecount += 1;
201         if (writecount > lp_parm_int(-1, "gencache", "stabilize_count", 100)) {
202                 gencache_stabilize();
203                 writecount = 0;
204                 goto done;
205         }
206
207         /*
208          * Every 5 minutes, call gencache_stabilize() to not let grow
209          * gencache_notrans.tdb too large.
210          */
211
212         last_stabilize = 0;
213         databuf = tdb_fetch(cache_notrans, last_stabilize_key());
214         if ((databuf.dptr != NULL)
215             && (databuf.dptr[databuf.dsize-1] == '\0')) {
216                 last_stabilize = atoi((char *)databuf.dptr);
217                 SAFE_FREE(databuf.dptr);
218         }
219         if ((last_stabilize
220              + lp_parm_int(-1, "gencache", "stabilize_interval", 300))
221             < time(NULL)) {
222                 gencache_stabilize();
223         }
224
225 done:
226         return ret == 0;
227 }
228
229 /**
230  * Delete one entry from the cache file.
231  *
232  * @param keystr string that represents a key of this entry
233  *
234  * @retval true upon successful deletion
235  * @retval false in case of failure
236  **/
237
238 bool gencache_del(const char *keystr)
239 {
240         bool exists, was_expired;
241         bool ret = false;
242         DATA_BLOB value;
243
244         if (keystr == NULL) {
245                 return false;
246         }
247
248         if (!gencache_init()) return False;     
249
250         DEBUG(10, ("Deleting cache entry (key = %s)\n", keystr));
251
252         /*
253          * We delete an element by setting its timeout to 0. This way we don't
254          * have to do a transaction on gencache.tdb every time we delete an
255          * element.
256          */
257
258         exists = gencache_get_data_blob(keystr, &value, NULL, &was_expired);
259
260         if (!exists && was_expired) {
261                 /*
262                  * gencache_get_data_blob has implicitly deleted this
263                  * entry, so we have to return success here.
264                  */
265                 return true;
266         }
267
268         if (exists) {
269                 data_blob_free(&value);
270                 ret = gencache_set(keystr, "", 0);
271         }
272         return ret;
273 }
274
275 static bool gencache_pull_timeout(char *val, time_t *pres, char **pendptr)
276 {
277         time_t res;
278         char *endptr;
279
280         if (val == NULL) {
281                 return false;
282         }
283
284         res = strtol(val, &endptr, 10);
285
286         if ((endptr == NULL) || (*endptr != '/')) {
287                 DEBUG(2, ("Invalid gencache data format: %s\n", val));
288                 return false;
289         }
290         if (pres != NULL) {
291                 *pres = res;
292         }
293         if (pendptr != NULL) {
294                 *pendptr = endptr;
295         }
296         return true;
297 }
298
299 struct gencache_parse_state {
300         void (*parser)(time_t timeout, DATA_BLOB blob, void *private_data);
301         void *private_data;
302 };
303
304 static int gencache_parse_fn(TDB_DATA key, TDB_DATA data, void *private_data)
305 {
306         struct gencache_parse_state *state;
307         DATA_BLOB blob;
308         time_t t;
309         char *endptr;
310         bool ret;
311
312         if (data.dptr == NULL) {
313                 return -1;
314         }
315         ret = gencache_pull_timeout((char *)data.dptr, &t, &endptr);
316         if (!ret) {
317                 return -1;
318         }
319         state = (struct gencache_parse_state *)private_data;
320         blob = data_blob_const(
321                 endptr+1, data.dsize - PTR_DIFF(endptr+1, data.dptr));
322         state->parser(t, blob, state->private_data);
323         return 0;
324 }
325
326 bool gencache_parse(const char *keystr,
327                     void (*parser)(time_t timeout, DATA_BLOB blob,
328                                    void *private_data),
329                     void *private_data)
330 {
331         struct gencache_parse_state state;
332         TDB_DATA key;
333         int ret;
334
335         if (keystr == NULL) {
336                 return false;
337         }
338         if (tdb_data_cmp(string_term_tdb_data(keystr),
339                          last_stabilize_key()) == 0) {
340                 return false;
341         }
342         if (!gencache_init()) {
343                 return false;
344         }
345
346         key = string_term_tdb_data(keystr);
347         state.parser = parser;
348         state.private_data = private_data;
349
350         ret = tdb_parse_record(cache_notrans, key, gencache_parse_fn, &state);
351         if (ret != -1) {
352                 return true;
353         }
354         ret = tdb_parse_record(cache, key, gencache_parse_fn, &state);
355         return (ret != -1);
356 }
357
358 struct gencache_get_data_blob_state {
359         DATA_BLOB *blob;
360         time_t timeout;
361         bool result;
362 };
363
364 static void gencache_get_data_blob_parser(time_t timeout, DATA_BLOB blob,
365                                           void *private_data)
366 {
367         struct gencache_get_data_blob_state *state =
368                 (struct gencache_get_data_blob_state *)private_data;
369
370         if (timeout == 0) {
371                 state->result = false;
372                 return;
373         }
374         state->timeout = timeout;
375
376         if (state->blob == NULL) {
377                 state->result = true;
378                 return;
379         }
380
381         *state->blob = data_blob(blob.data, blob.length);
382         if (state->blob->data == NULL) {
383                 state->result = false;
384                 return;
385         }
386         state->result = true;
387 }
388
389 /**
390  * Get existing entry from the cache file.
391  *
392  * @param keystr string that represents a key of this entry
393  * @param blob DATA_BLOB that is filled with entry's blob
394  * @param timeout pointer to a time_t that is filled with entry's
395  *        timeout
396  *
397  * @retval true when entry is successfuly fetched
398  * @retval False for failure
399  **/
400
401 bool gencache_get_data_blob(const char *keystr, DATA_BLOB *blob,
402                             time_t *timeout, bool *was_expired)
403 {
404         struct gencache_get_data_blob_state state;
405         bool expired = false;
406
407         state.result = false;
408         state.blob = blob;
409
410         if (!gencache_parse(keystr, gencache_get_data_blob_parser, &state)) {
411                 goto fail;
412         }
413         if (!state.result) {
414                 goto fail;
415         }
416         if (state.timeout <= time(NULL)) {
417                 /*
418                  * We're expired, delete the entry. We can't use gencache_del
419                  * here, because that uses gencache_get_data_blob for checking
420                  * the existence of a record. We know the thing exists and
421                  * directly store an empty value with 0 timeout.
422                  */
423                 gencache_set(keystr, "", 0);
424                 expired = true;
425                 goto fail;
426         }
427         if (timeout) {
428                 *timeout = state.timeout;
429         }
430
431         return True;
432
433 fail:
434         if (was_expired != NULL) {
435                 *was_expired = expired;
436         }
437         if (state.result && state.blob) {
438                 data_blob_free(state.blob);
439         }
440         return false;
441
442
443 struct stabilize_state {
444         bool written;
445         bool error;
446 };
447 static int stabilize_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA val,
448                         void *priv);
449
450 /**
451  * Stabilize gencache
452  *
453  * Migrate the clear-if-first gencache data to the stable,
454  * transaction-based gencache.tdb
455  */
456
457 bool gencache_stabilize(void)
458 {
459         struct stabilize_state state;
460         int res;
461         char *now;
462
463         if (!gencache_init()) {
464                 return false;
465         }
466
467         res = tdb_transaction_start_nonblock(cache);
468         if (res == -1) {
469
470                 if (tdb_error(cache) == TDB_ERR_NOLOCK) {
471                         /*
472                          * Someone else already does the stabilize,
473                          * this does not have to be done twice
474                          */
475                         return true;
476                 }
477
478                 DEBUG(10, ("Could not start transaction on gencache.tdb: "
479                            "%s\n", tdb_errorstr(cache)));
480                 return false;
481         }
482         res = tdb_transaction_start(cache_notrans);
483         if (res == -1) {
484                 tdb_transaction_cancel(cache);
485                 DEBUG(10, ("Could not start transaction on "
486                            "gencache_notrans.tdb: %s\n",
487                            tdb_errorstr(cache_notrans)));
488                 return false;
489         }
490
491         state.error = false;
492         state.written = false;
493
494         res = tdb_traverse(cache_notrans, stabilize_fn, &state);
495         if ((res == -1) || state.error) {
496                 if ((tdb_transaction_cancel(cache_notrans) == -1)
497                     || (tdb_transaction_cancel(cache) == -1)) {
498                         smb_panic("tdb_transaction_cancel failed\n");
499                 }
500                 return false;
501         }
502
503         if (!state.written) {
504                 if ((tdb_transaction_cancel(cache_notrans) == -1)
505                     || (tdb_transaction_cancel(cache) == -1)) {
506                         smb_panic("tdb_transaction_cancel failed\n");
507                 }
508                 return true;
509         }
510
511         res = tdb_transaction_commit(cache);
512         if (res == -1) {
513                 DEBUG(10, ("tdb_transaction_commit on gencache.tdb failed: "
514                            "%s\n", tdb_errorstr(cache)));
515                 if (tdb_transaction_cancel(cache_notrans) == -1) {
516                         smb_panic("tdb_transaction_cancel failed\n");
517                 }
518                 return false;
519         }
520
521         res = tdb_transaction_commit(cache_notrans);
522         if (res == -1) {
523                 DEBUG(10, ("tdb_transaction_commit on gencache.tdb failed: "
524                            "%s\n", tdb_errorstr(cache)));
525                 return false;
526         }
527
528         now = talloc_asprintf(talloc_tos(), "%d", (int)time(NULL));
529         if (now != NULL) {
530                 tdb_store(cache_notrans, last_stabilize_key(),
531                           string_term_tdb_data(now), 0);
532                 TALLOC_FREE(now);
533         }
534
535         return true;
536 }
537
538 static int stabilize_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA val,
539                         void *priv)
540 {
541         struct stabilize_state *state = (struct stabilize_state *)priv;
542         int res;
543         time_t timeout;
544
545         if (tdb_data_cmp(key, last_stabilize_key()) == 0) {
546                 return 0;
547         }
548
549         if (!gencache_pull_timeout((char *)val.dptr, &timeout, NULL)) {
550                 DEBUG(10, ("Ignoring invalid entry\n"));
551                 return 0;
552         }
553         if ((timeout < time(NULL)) || (val.dsize == 0)) {
554                 res = tdb_delete(cache, key);
555                 if ((res == -1) && (tdb_error(cache) == TDB_ERR_NOEXIST)) {
556                         res = 0;
557                 } else {
558                         state->written = true;
559                 }
560         } else {
561                 res = tdb_store(cache, key, val, 0);
562                 if (res == 0) {
563                         state->written = true;
564                 }
565         }
566
567         if (res == -1) {
568                 DEBUG(10, ("Transfer to gencache.tdb failed: %s\n",
569                            tdb_errorstr(cache)));
570                 state->error = true;
571                 return -1;
572         }
573
574         if (tdb_delete(cache_notrans, key) == -1) {
575                 DEBUG(10, ("tdb_delete from gencache_notrans.tdb failed: "
576                            "%s\n", tdb_errorstr(cache_notrans)));
577                 state->error = true;
578                 return -1;
579         }
580         return 0;
581 }
582
583 /**
584  * Get existing entry from the cache file.
585  *
586  * @param keystr string that represents a key of this entry
587  * @param valstr buffer that is allocated and filled with the entry value
588  *        buffer's disposing must be done outside
589  * @param timeout pointer to a time_t that is filled with entry's
590  *        timeout
591  *
592  * @retval true when entry is successfuly fetched
593  * @retval False for failure
594  **/
595
596 bool gencache_get(const char *keystr, char **value, time_t *ptimeout)
597 {
598         DATA_BLOB blob;
599         bool ret = False;
600
601         ret = gencache_get_data_blob(keystr, &blob, ptimeout, NULL);
602         if (!ret) {
603                 return false;
604         }
605         if ((blob.data == NULL) || (blob.length == 0)) {
606                 SAFE_FREE(blob.data);
607                 return false;
608         }
609         if (blob.data[blob.length-1] != '\0') {
610                 /* Not NULL terminated, can't be a string */
611                 SAFE_FREE(blob.data);
612                 return false;
613         }
614         if (value) {
615                 *value = SMB_STRDUP((char *)blob.data);
616                 data_blob_free(&blob);
617                 if (*value == NULL) {
618                         return false;
619                 }
620                 return true;
621         }
622         data_blob_free(&blob);
623         return true;
624 }
625
626 /**
627  * Set an entry in the cache file. If there's no such
628  * one, then add it.
629  *
630  * @param keystr string that represents a key of this entry
631  * @param value text representation value being cached
632  * @param timeout time when the value is expired
633  *
634  * @retval true when entry is successfuly stored
635  * @retval false on failure
636  **/
637
638 bool gencache_set(const char *keystr, const char *value, time_t timeout)
639 {
640         DATA_BLOB blob = data_blob_const(value, strlen(value)+1);
641         return gencache_set_data_blob(keystr, &blob, timeout);
642 }
643
644 struct gencache_iterate_blobs_state {
645         void (*fn)(const char *key, DATA_BLOB value,
646                    time_t timeout, void *private_data);
647         const char *pattern;
648         void *private_data;
649         bool in_persistent;
650 };
651
652 static int gencache_iterate_blobs_fn(struct tdb_context *tdb, TDB_DATA key,
653                                      TDB_DATA data, void *priv)
654 {
655         struct gencache_iterate_blobs_state *state =
656                 (struct gencache_iterate_blobs_state *)priv;
657         char *keystr;
658         char *free_key = NULL;
659         time_t timeout;
660         char *endptr;
661
662         if (tdb_data_cmp(key, last_stabilize_key()) == 0) {
663                 return 0;
664         }
665         if (state->in_persistent && tdb_exists(cache_notrans, key)) {
666                 return 0;
667         }
668
669         if (key.dptr[key.dsize-1] == '\0') {
670                 keystr = (char *)key.dptr;
671         } else {
672                 /* ensure 0-termination */
673                 keystr = SMB_STRNDUP((char *)key.dptr, key.dsize);
674                 free_key = keystr;
675         }
676
677         if (!gencache_pull_timeout((char *)data.dptr, &timeout, &endptr)) {
678                 goto done;
679         }
680         endptr += 1;
681
682         if (fnmatch(state->pattern, keystr, 0) != 0) {
683                 goto done;
684         }
685
686         DEBUG(10, ("Calling function with arguments (key=%s, timeout=%s)\n",
687                    keystr, ctime(&timeout)));
688
689         state->fn(keystr,
690                   data_blob_const(endptr,
691                                   data.dsize - PTR_DIFF(endptr, data.dptr)),
692                   timeout, state->private_data);
693
694  done:
695         SAFE_FREE(free_key);
696         return 0;
697 }
698
699 void gencache_iterate_blobs(void (*fn)(const char *key, DATA_BLOB value,
700                                        time_t timeout, void *private_data),
701                             void *private_data, const char *pattern)
702 {
703         struct gencache_iterate_blobs_state state;
704
705         if ((fn == NULL) || (pattern == NULL) || !gencache_init()) {
706                 return;
707         }
708
709         DEBUG(5, ("Searching cache keys with pattern %s\n", pattern));
710
711         state.fn = fn;
712         state.pattern = pattern;
713         state.private_data = private_data;
714
715         state.in_persistent = false;
716         tdb_traverse(cache_notrans, gencache_iterate_blobs_fn, &state);
717
718         state.in_persistent = true;
719         tdb_traverse(cache, gencache_iterate_blobs_fn, &state);
720 }
721
722 /**
723  * Iterate through all entries which key matches to specified pattern
724  *
725  * @param fn pointer to the function that will be supplied with each single
726  *        matching cache entry (key, value and timeout) as an arguments
727  * @param data void pointer to an arbitrary data that is passed directly to the fn
728  *        function on each call
729  * @param keystr_pattern pattern the existing entries' keys are matched to
730  *
731  **/
732
733 struct gencache_iterate_state {
734         void (*fn)(const char *key, const char *value, time_t timeout,
735                    void *priv);
736         void *private_data;
737 };
738
739 static void gencache_iterate_fn(const char *key, DATA_BLOB value,
740                                 time_t timeout, void *private_data)
741 {
742         struct gencache_iterate_state *state =
743                 (struct gencache_iterate_state *)private_data;
744         char *valstr;
745         char *free_val = NULL;
746
747         if (value.data[value.length-1] == '\0') {
748                 valstr = (char *)value.data;
749         } else {
750                 /* ensure 0-termination */
751                 valstr = SMB_STRNDUP((char *)value.data, value.length);
752                 free_val = valstr;
753         }
754
755         DEBUG(10, ("Calling function with arguments "
756                    "(key = %s, value = %s, timeout = %s)\n",
757                    key, valstr, ctime(&timeout)));
758
759         state->fn(key, valstr, timeout, state->private_data);
760
761         SAFE_FREE(free_val);
762 }
763
764 void gencache_iterate(void (*fn)(const char *key, const char *value,
765                                  time_t timeout, void *dptr),
766                       void *private_data, const char *pattern)
767 {
768         struct gencache_iterate_state state;
769
770         if (fn == NULL) {
771                 return;
772         }
773         state.fn = fn;
774         state.private_data = private_data;
775         gencache_iterate_blobs(gencache_iterate_fn, &state, pattern);
776 }