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/%s"
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 value text representation 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(const char *keystr, const char *value, time_t timeout)
122 time_t last_stabilize;
124 if (tdb_data_cmp(string_term_tdb_data(keystr),
125 last_stabilize_key()) == 0) {
126 DEBUG(10, ("Can't store %s as a key\n", keystr));
130 if ((keystr == NULL) || (value == NULL)) {
134 if (!gencache_init()) return False;
136 if (asprintf(&valstr, CACHE_DATA_FMT, (int)timeout, value) == -1) {
140 databuf = string_term_tdb_data(valstr);
141 DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
142 " %s (%d seconds %s)\n", keystr, value,ctime(&timeout),
143 (int)(timeout - time(NULL)),
144 timeout > time(NULL) ? "ahead" : "in the past"));
146 ret = tdb_store_bystring(cache_notrans, keystr, databuf, 0);
154 * Every 5 minutes, call gencache_stabilize() to not let grow
155 * gencache_notrans.tdb too large.
159 databuf = tdb_fetch(cache_notrans, last_stabilize_key());
160 if ((databuf.dptr != NULL)
161 && (databuf.dptr[databuf.dsize-1] == '\0')) {
162 last_stabilize = atoi((char *)databuf.dptr);
163 SAFE_FREE(databuf.dptr);
166 + lp_parm_int(-1, "gencache", "stabilize_interval", 300))
168 gencache_stabilize();
175 * Delete one entry from the cache file.
177 * @param keystr string that represents a key of this entry
179 * @retval true upon successful deletion
180 * @retval false in case of failure
183 bool gencache_del(const char *keystr)
189 if (keystr == NULL) {
193 if (!gencache_init()) return False;
195 DEBUG(10, ("Deleting cache entry (key = %s)\n", keystr));
197 if (tdb_lock_bystring(cache_notrans, keystr) == -1) {
198 DEBUG(5, ("Could not lock key for %s\n", keystr));
203 * We delete an element by setting its timeout to 0. This way we don't
204 * have to do a transaction on gencache.tdb every time we delete an
208 exists = gencache_get(keystr, &value, NULL);
211 ret = gencache_set(keystr, "", 0);
213 tdb_unlock_bystring(cache_notrans, keystr);
217 static bool gencache_pull_timeout(char *val, time_t *pres, char **pendptr)
222 res = strtol(val, &endptr, 10);
224 if ((endptr == NULL) || (*endptr != '/')) {
225 DEBUG(2, ("Invalid gencache data format: %s\n", val));
231 if (pendptr != NULL) {
238 * Get existing entry from the cache file.
240 * @param keystr string that represents a key of this entry
241 * @param valstr buffer that is allocated and filled with the entry value
242 * buffer's disposing must be done outside
243 * @param timeout pointer to a time_t that is filled with entry's
246 * @retval true when entry is successfuly fetched
247 * @retval False for failure
250 bool gencache_get(const char *keystr, char **valstr, time_t *timeout)
256 if (keystr == NULL) {
260 if (tdb_data_cmp(string_term_tdb_data(keystr),
261 last_stabilize_key()) == 0) {
262 DEBUG(10, ("Can't get %s as a key\n", keystr));
266 if (!gencache_init()) {
270 databuf = tdb_fetch_bystring(cache_notrans, keystr);
272 if (databuf.dptr == NULL) {
273 databuf = tdb_fetch_bystring(cache, keystr);
276 if (databuf.dptr == NULL) {
277 DEBUG(10, ("Cache entry with key = %s couldn't be found \n",
282 if (!gencache_pull_timeout((char *)databuf.dptr, &t, &endptr)) {
283 SAFE_FREE(databuf.dptr);
287 DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, "
288 "timeout = %s", t > time(NULL) ? "valid" :
289 "expired", keystr, endptr+1, ctime(&t)));
293 SAFE_FREE(databuf.dptr);
297 if (t <= time(NULL)) {
300 * We're expired, delete the entry. We can't use gencache_del
301 * here, because that uses gencache_get_data_blob for checking
302 * the existence of a record. We know the thing exists and
303 * directly store an empty value with 0 timeout.
305 gencache_set(keystr, "", 0);
307 SAFE_FREE(databuf.dptr);
312 *valstr = SMB_STRDUP(endptr+1);
313 if (*valstr == NULL) {
314 SAFE_FREE(databuf.dptr);
315 DEBUG(0, ("strdup failed\n"));
320 SAFE_FREE(databuf.dptr);
329 struct stabilize_state {
333 static int stabilize_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA val,
339 * Migrate the clear-if-first gencache data to the stable,
340 * transaction-based gencache.tdb
343 bool gencache_stabilize(void)
345 struct stabilize_state state;
349 if (!gencache_init()) {
353 res = tdb_transaction_start(cache);
355 DEBUG(10, ("Could not start transaction on gencache.tdb: "
356 "%s\n", tdb_errorstr(cache)));
359 res = tdb_transaction_start(cache_notrans);
361 tdb_transaction_cancel(cache);
362 DEBUG(10, ("Could not start transaction on "
363 "gencache_notrans.tdb: %s\n",
364 tdb_errorstr(cache_notrans)));
369 state.written = false;
371 res = tdb_traverse(cache_notrans, stabilize_fn, &state);
372 if ((res == -1) || state.error) {
373 if ((tdb_transaction_cancel(cache_notrans) == -1)
374 || (tdb_transaction_cancel(cache) == -1)) {
375 smb_panic("tdb_transaction_cancel failed\n");
380 if (!state.written) {
381 if ((tdb_transaction_cancel(cache_notrans) == -1)
382 || (tdb_transaction_cancel(cache) == -1)) {
383 smb_panic("tdb_transaction_cancel failed\n");
388 res = tdb_transaction_commit(cache);
390 DEBUG(10, ("tdb_transaction_commit on gencache.tdb failed: "
391 "%s\n", tdb_errorstr(cache)));
392 if (tdb_transaction_cancel(cache_notrans) == -1) {
393 smb_panic("tdb_transaction_cancel failed\n");
398 res = tdb_transaction_commit(cache_notrans);
400 DEBUG(10, ("tdb_transaction_commit on gencache.tdb failed: "
401 "%s\n", tdb_errorstr(cache)));
405 now = talloc_asprintf(talloc_tos(), "%d", (int)time(NULL));
407 tdb_store(cache_notrans, last_stabilize_key(),
408 string_term_tdb_data(now), 0);
415 static int stabilize_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA val,
418 struct stabilize_state *state = (struct stabilize_state *)priv;
422 if (tdb_data_cmp(key, last_stabilize_key()) == 0) {
426 if (!gencache_pull_timeout((char *)val.dptr, &timeout, NULL)) {
427 DEBUG(10, ("Ignoring invalid entry\n"));
430 if ((timeout < time(NULL)) || (val.dsize == 0)) {
431 res = tdb_delete(cache, key);
432 if ((res == -1) && (tdb_error(cache) == TDB_ERR_NOEXIST)) {
435 state->written = true;
438 res = tdb_store(cache, key, val, 0);
440 state->written = true;
445 DEBUG(10, ("Transfer to gencache.tdb failed: %s\n",
446 tdb_errorstr(cache)));
451 if (tdb_delete(cache_notrans, key) == -1) {
452 DEBUG(10, ("tdb_delete from gencache_notrans.tdb failed: "
453 "%s\n", tdb_errorstr(cache_notrans)));
461 * Get existing entry from the cache file.
463 * @param keystr string that represents a key of this entry
464 * @param blob DATA_BLOB that is filled with entry's blob
465 * @param expired pointer to a bool that indicates whether the entry is expired
467 * @retval true when entry is successfuly fetched
468 * @retval False for failure
471 bool gencache_get_data_blob(const char *keystr, DATA_BLOB *blob, bool *expired)
476 unsigned char *buf = NULL;
479 int buflen = 0, len = 0, blob_len = 0;
480 unsigned char *blob_buf = NULL;
482 if (keystr == NULL) {
486 if (!gencache_init()) {
490 databuf = tdb_fetch_bystring(cache, keystr);
492 DEBUG(10,("Cache entry with key = %s couldn't be found\n",
497 buf = (unsigned char *)databuf.dptr;
498 buflen = databuf.dsize;
500 len += tdb_unpack(buf+len, buflen-len, "fB",
502 &blob_len, &blob_buf);
507 t = strtol(valstr, &blob_type, 10);
509 if (strcmp(blob_type+1, BLOB_TYPE) != 0) {
513 DEBUG(10,("Returning %s cache entry: key = %s, "
514 "timeout = %s", t > time(NULL) ? "valid" :
515 "expired", keystr, ctime(&t)));
517 if (t <= time(NULL)) {
525 *blob = data_blob(blob_buf, blob_len);
534 SAFE_FREE(databuf.dptr);
540 * Set an entry in the cache file. If there's no such
543 * @param keystr string that represents a key of this entry
544 * @param blob DATA_BLOB value being cached
545 * @param timeout time when the value is expired
547 * @retval true when entry is successfuly stored
548 * @retval false on failure
551 bool gencache_set_data_blob(const char *keystr, const DATA_BLOB *blob, time_t timeout)
557 unsigned char *buf = NULL;
558 int len = 0, buflen = 0;
560 if ((keystr == NULL) || (blob == NULL)) {
564 if (!gencache_init()) {
568 if (asprintf(&valstr, "%12u/%s", (int)timeout, BLOB_TYPE) == -1) {
575 len += tdb_pack(buf+len, buflen-len, "fB",
577 blob->length, blob->data);
585 buf = SMB_MALLOC_ARRAY(unsigned char, len);
593 databuf = make_tdb_data(buf, len);
595 DEBUG(10,("Adding cache entry with key = %s; "
596 "blob size = %d and timeout = %s"
597 "(%d seconds %s)\n", keystr, (int)databuf.dsize,
598 ctime(&timeout), (int)(timeout - time(NULL)),
599 timeout > time(NULL) ? "ahead" : "in the past"));
601 tdb_ret = tdb_store_bystring(cache, keystr, databuf, 0);
614 * Iterate through all entries which key matches to specified pattern
616 * @param fn pointer to the function that will be supplied with each single
617 * matching cache entry (key, value and timeout) as an arguments
618 * @param data void pointer to an arbitrary data that is passed directly to the fn
619 * function on each call
620 * @param keystr_pattern pattern the existing entries' keys are matched to
624 struct gencache_iterate_state {
625 void (*fn)(const char *key, const char *value, time_t timeout,
632 static int gencache_iterate_fn(struct tdb_context *tdb, TDB_DATA key,
633 TDB_DATA value, void *priv)
635 struct gencache_iterate_state *state =
636 (struct gencache_iterate_state *)priv;
638 char *free_key = NULL;
640 char *free_val = NULL;
645 if (tdb_data_cmp(key, last_stabilize_key()) == 0) {
649 if (state->in_persistent && tdb_exists(cache_notrans, key)) {
653 if (key.dptr[key.dsize-1] == '\0') {
654 keystr = (char *)key.dptr;
656 /* ensure 0-termination */
657 keystr = SMB_STRNDUP((char *)key.dptr, key.dsize);
661 if ((value.dptr == NULL) || (value.dsize <= TIMEOUT_LEN)) {
665 if (fnmatch(state->pattern, keystr, 0) != 0) {
669 if (value.dptr[value.dsize-1] == '\0') {
670 valstr = (char *)value.dptr;
672 /* ensure 0-termination */
673 valstr = SMB_STRNDUP((char *)value.dptr, value.dsize);
677 u = strtoul(valstr, &timeout_endp, 10);
679 if ((*timeout_endp != '/') || ((timeout_endp-valstr) != TIMEOUT_LEN)) {
686 DEBUG(10, ("Calling function with arguments "
687 "(key = %s, value = %s, timeout = %s)\n",
688 keystr, timeout_endp, ctime(&timeout)));
689 state->fn(keystr, timeout_endp, timeout, state->priv);
697 void gencache_iterate(void (*fn)(const char* key, const char *value, time_t timeout, void* dptr),
698 void* data, const char* keystr_pattern)
700 struct gencache_iterate_state state;
702 if ((fn == NULL) || (keystr_pattern == NULL)) {
706 if (!gencache_init()) return;
708 DEBUG(5, ("Searching cache keys with pattern %s\n", keystr_pattern));
711 state.pattern = keystr_pattern;
714 state.in_persistent = false;
715 tdb_traverse(cache_notrans, gencache_iterate_fn, &state);
717 state.in_persistent = true;
718 tdb_traverse(cache, gencache_iterate_fn, &state);