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
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #define DBGC_CLASS DBGC_TDB
28 #define TIMEOUT_LEN 12
29 #define CACHE_DATA_FMT "%12u/"
30 #define READ_CACHE_DATA_FMT_TEMPLATE "%%12u/%%%us"
31 #define BLOB_TYPE "DATA_BLOB"
32 #define BLOB_TYPE_LEN 9
34 static struct tdb_context *cache;
35 static struct tdb_context *cache_notrans;
39 * @brief Generic, persistent and shared between processes cache mechanism
40 * for use by various parts of the Samba code
46 * Cache initialisation function. Opens cache tdb file or creates
47 * it if does not exist.
49 * @return true on successful initialisation of the cache or
53 static bool gencache_init(void)
55 char* cache_fname = NULL;
56 int open_flags = O_RDWR|O_CREAT;
58 /* skip file open if it's already opened */
59 if (cache) return True;
61 cache_fname = lock_path("gencache.tdb");
63 DEBUG(5, ("Opening cache file at %s\n", cache_fname));
65 cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT, open_flags, 0644);
67 if (!cache && (errno == EACCES)) {
68 open_flags = O_RDONLY;
69 cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT, open_flags,
72 DEBUG(5, ("gencache_init: Opening cache file %s read-only.\n", cache_fname));
77 DEBUG(5, ("Attempt to open gencache.tdb has failed.\n"));
81 cache_fname = lock_path("gencache_notrans.tdb");
83 DEBUG(5, ("Opening cache file at %s\n", cache_fname));
85 cache_notrans = tdb_open_log(cache_fname, 0, TDB_CLEAR_IF_FIRST,
87 if (cache_notrans == NULL) {
88 DEBUG(5, ("Opening %s failed: %s\n", cache_fname,
97 static TDB_DATA last_stabilize_key(void)
100 result.dptr = (uint8_t *)"@LAST_STABILIZED";
106 * Set an entry in the cache file. If there's no such
109 * @param keystr string that represents a key of this entry
110 * @param blob DATA_BLOB value being cached
111 * @param timeout time when the value is expired
113 * @retval true when entry is successfuly stored
114 * @retval false on failure
117 bool gencache_set_data_blob(const char *keystr, const DATA_BLOB *blob,
123 time_t last_stabilize;
124 static int writecount;
126 if (tdb_data_cmp(string_term_tdb_data(keystr),
127 last_stabilize_key()) == 0) {
128 DEBUG(10, ("Can't store %s as a key\n", keystr));
132 if ((keystr == NULL) || (blob == NULL)) {
136 if (!gencache_init()) return False;
138 val = talloc_asprintf(talloc_tos(), CACHE_DATA_FMT, (int)timeout);
142 val = talloc_realloc(NULL, val, char, talloc_array_length(val)-1);
146 val = (char *)talloc_append_blob(NULL, val, *blob);
151 DEBUG(10, ("Adding cache entry with key = %s and timeout ="
152 " %s (%d seconds %s)\n", keystr, ctime(&timeout),
153 (int)(timeout - time(NULL)),
154 timeout > time(NULL) ? "ahead" : "in the past"));
156 ret = tdb_store_bystring(
157 cache_notrans, keystr,
158 make_tdb_data((uint8_t *)val, talloc_array_length(val)),
167 * Every 100 writes within a single process, stabilize the cache with
168 * a transaction. This is done to prevent a single transaction to
169 * become huge and chew lots of memory.
172 if (writecount > lp_parm_int(-1, "gencache", "stabilize_count", 100)) {
173 gencache_stabilize();
179 * Every 5 minutes, call gencache_stabilize() to not let grow
180 * gencache_notrans.tdb too large.
184 databuf = tdb_fetch(cache_notrans, last_stabilize_key());
185 if ((databuf.dptr != NULL)
186 && (databuf.dptr[databuf.dsize-1] == '\0')) {
187 last_stabilize = atoi((char *)databuf.dptr);
188 SAFE_FREE(databuf.dptr);
191 + lp_parm_int(-1, "gencache", "stabilize_interval", 300))
193 gencache_stabilize();
201 * Delete one entry from the cache file.
203 * @param keystr string that represents a key of this entry
205 * @retval true upon successful deletion
206 * @retval false in case of failure
209 bool gencache_del(const char *keystr)
215 if (keystr == NULL) {
219 if (!gencache_init()) return False;
221 DEBUG(10, ("Deleting cache entry (key = %s)\n", keystr));
223 if (tdb_lock_bystring(cache_notrans, keystr) == -1) {
224 DEBUG(5, ("Could not lock key for %s\n", keystr));
229 * We delete an element by setting its timeout to 0. This way we don't
230 * have to do a transaction on gencache.tdb every time we delete an
234 exists = gencache_get(keystr, &value, NULL);
237 ret = gencache_set(keystr, "", 0);
239 tdb_unlock_bystring(cache_notrans, keystr);
243 static bool gencache_pull_timeout(char *val, time_t *pres, char **pendptr)
248 res = strtol(val, &endptr, 10);
250 if ((endptr == NULL) || (*endptr != '/')) {
251 DEBUG(2, ("Invalid gencache data format: %s\n", val));
257 if (pendptr != NULL) {
264 * Get existing entry from the cache file.
266 * @param keystr string that represents a key of this entry
267 * @param blob DATA_BLOB that is filled with entry's blob
268 * @param timeout pointer to a time_t that is filled with entry's
271 * @retval true when entry is successfuly fetched
272 * @retval False for failure
275 bool gencache_get_data_blob(const char *keystr, DATA_BLOB *blob,
276 time_t *timeout, bool *was_expired)
281 bool expired = false;
283 if (keystr == NULL) {
287 if (tdb_data_cmp(string_term_tdb_data(keystr),
288 last_stabilize_key()) == 0) {
289 DEBUG(10, ("Can't get %s as a key\n", keystr));
293 if (!gencache_init()) {
297 databuf = tdb_fetch_bystring(cache_notrans, keystr);
299 if (databuf.dptr == NULL) {
300 databuf = tdb_fetch_bystring(cache, keystr);
303 if (databuf.dptr == NULL) {
304 DEBUG(10, ("Cache entry with key = %s couldn't be found \n",
309 if (!gencache_pull_timeout((char *)databuf.dptr, &t, &endptr)) {
310 SAFE_FREE(databuf.dptr);
314 DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, "
315 "timeout = %s", t > time(NULL) ? "valid" :
316 "expired", keystr, endptr+1, ctime(&t)));
320 SAFE_FREE(databuf.dptr);
324 if (t <= time(NULL)) {
327 * We're expired, delete the entry. We can't use gencache_del
328 * here, because that uses gencache_get_data_blob for checking
329 * the existence of a record. We know the thing exists and
330 * directly store an empty value with 0 timeout.
332 gencache_set(keystr, "", 0);
334 SAFE_FREE(databuf.dptr);
343 databuf.dsize - PTR_DIFF(endptr+1, databuf.dptr));
344 if (blob->data == NULL) {
345 SAFE_FREE(databuf.dptr);
346 DEBUG(0, ("memdup failed\n"));
351 SAFE_FREE(databuf.dptr);
360 if (was_expired != NULL) {
361 *was_expired = expired;
366 struct stabilize_state {
370 static int stabilize_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA val,
376 * Migrate the clear-if-first gencache data to the stable,
377 * transaction-based gencache.tdb
380 bool gencache_stabilize(void)
382 struct stabilize_state state;
386 if (!gencache_init()) {
390 res = tdb_transaction_start(cache);
392 DEBUG(10, ("Could not start transaction on gencache.tdb: "
393 "%s\n", tdb_errorstr(cache)));
396 res = tdb_transaction_start(cache_notrans);
398 tdb_transaction_cancel(cache);
399 DEBUG(10, ("Could not start transaction on "
400 "gencache_notrans.tdb: %s\n",
401 tdb_errorstr(cache_notrans)));
406 state.written = false;
408 res = tdb_traverse(cache_notrans, stabilize_fn, &state);
409 if ((res == -1) || state.error) {
410 if ((tdb_transaction_cancel(cache_notrans) == -1)
411 || (tdb_transaction_cancel(cache) == -1)) {
412 smb_panic("tdb_transaction_cancel failed\n");
417 if (!state.written) {
418 if ((tdb_transaction_cancel(cache_notrans) == -1)
419 || (tdb_transaction_cancel(cache) == -1)) {
420 smb_panic("tdb_transaction_cancel failed\n");
425 res = tdb_transaction_commit(cache);
427 DEBUG(10, ("tdb_transaction_commit on gencache.tdb failed: "
428 "%s\n", tdb_errorstr(cache)));
429 if (tdb_transaction_cancel(cache_notrans) == -1) {
430 smb_panic("tdb_transaction_cancel failed\n");
435 res = tdb_transaction_commit(cache_notrans);
437 DEBUG(10, ("tdb_transaction_commit on gencache.tdb failed: "
438 "%s\n", tdb_errorstr(cache)));
442 now = talloc_asprintf(talloc_tos(), "%d", (int)time(NULL));
444 tdb_store(cache_notrans, last_stabilize_key(),
445 string_term_tdb_data(now), 0);
452 static int stabilize_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA val,
455 struct stabilize_state *state = (struct stabilize_state *)priv;
459 if (tdb_data_cmp(key, last_stabilize_key()) == 0) {
463 if (!gencache_pull_timeout((char *)val.dptr, &timeout, NULL)) {
464 DEBUG(10, ("Ignoring invalid entry\n"));
467 if ((timeout < time(NULL)) || (val.dsize == 0)) {
468 res = tdb_delete(cache, key);
469 if ((res == -1) && (tdb_error(cache) == TDB_ERR_NOEXIST)) {
472 state->written = true;
475 res = tdb_store(cache, key, val, 0);
477 state->written = true;
482 DEBUG(10, ("Transfer to gencache.tdb failed: %s\n",
483 tdb_errorstr(cache)));
488 if (tdb_delete(cache_notrans, key) == -1) {
489 DEBUG(10, ("tdb_delete from gencache_notrans.tdb failed: "
490 "%s\n", tdb_errorstr(cache_notrans)));
498 * Get existing entry from the cache file.
500 * @param keystr string that represents a key of this entry
501 * @param valstr buffer that is allocated and filled with the entry value
502 * buffer's disposing must be done outside
503 * @param timeout pointer to a time_t that is filled with entry's
506 * @retval true when entry is successfuly fetched
507 * @retval False for failure
510 bool gencache_get(const char *keystr, char **value, time_t *ptimeout)
515 ret = gencache_get_data_blob(keystr, &blob, ptimeout, NULL);
519 if ((blob.data == NULL) || (blob.length == 0)) {
520 SAFE_FREE(blob.data);
523 if (blob.data[blob.length-1] != '\0') {
524 /* Not NULL terminated, can't be a string */
525 SAFE_FREE(blob.data);
528 *value = SMB_STRDUP((char *)blob.data);
529 data_blob_free(&blob);
530 if (*value == NULL) {
537 * Set an entry in the cache file. If there's no such
540 * @param keystr string that represents a key of this entry
541 * @param value text representation value being cached
542 * @param timeout time when the value is expired
544 * @retval true when entry is successfuly stored
545 * @retval false on failure
548 bool gencache_set(const char *keystr, const char *value, time_t timeout)
550 DATA_BLOB blob = data_blob_const(value, strlen(value)+1);
551 return gencache_set_data_blob(keystr, &blob, timeout);
555 * Iterate through all entries which key matches to specified pattern
557 * @param fn pointer to the function that will be supplied with each single
558 * matching cache entry (key, value and timeout) as an arguments
559 * @param data void pointer to an arbitrary data that is passed directly to the fn
560 * function on each call
561 * @param keystr_pattern pattern the existing entries' keys are matched to
565 struct gencache_iterate_state {
566 void (*fn)(const char *key, const char *value, time_t timeout,
573 static int gencache_iterate_fn(struct tdb_context *tdb, TDB_DATA key,
574 TDB_DATA value, void *priv)
576 struct gencache_iterate_state *state =
577 (struct gencache_iterate_state *)priv;
579 char *free_key = NULL;
581 char *free_val = NULL;
586 if (tdb_data_cmp(key, last_stabilize_key()) == 0) {
590 if (state->in_persistent && tdb_exists(cache_notrans, key)) {
594 if (key.dptr[key.dsize-1] == '\0') {
595 keystr = (char *)key.dptr;
597 /* ensure 0-termination */
598 keystr = SMB_STRNDUP((char *)key.dptr, key.dsize);
602 if ((value.dptr == NULL) || (value.dsize <= TIMEOUT_LEN)) {
606 if (fnmatch(state->pattern, keystr, 0) != 0) {
610 if (value.dptr[value.dsize-1] == '\0') {
611 valstr = (char *)value.dptr;
613 /* ensure 0-termination */
614 valstr = SMB_STRNDUP((char *)value.dptr, value.dsize);
618 u = strtoul(valstr, &timeout_endp, 10);
620 if ((*timeout_endp != '/') || ((timeout_endp-valstr) != TIMEOUT_LEN)) {
627 DEBUG(10, ("Calling function with arguments "
628 "(key = %s, value = %s, timeout = %s)\n",
629 keystr, timeout_endp, ctime(&timeout)));
630 state->fn(keystr, timeout_endp, timeout, state->priv);
638 void gencache_iterate(void (*fn)(const char* key, const char *value, time_t timeout, void* dptr),
639 void* data, const char* keystr_pattern)
641 struct gencache_iterate_state state;
643 if ((fn == NULL) || (keystr_pattern == NULL)) {
647 if (!gencache_init()) return;
649 DEBUG(5, ("Searching cache keys with pattern %s\n", keystr_pattern));
652 state.pattern = keystr_pattern;
655 state.in_persistent = false;
656 tdb_traverse(cache_notrans, gencache_iterate_fn, &state);
658 state.in_persistent = true;
659 tdb_traverse(cache, gencache_iterate_fn, &state);