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
8 Copyright (C) Volker Lendecke 2009
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.
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.
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/>.
27 #define DBGC_CLASS DBGC_TDB
29 #define TIMEOUT_LEN 12
30 #define CACHE_DATA_FMT "%12u/"
31 #define READ_CACHE_DATA_FMT_TEMPLATE "%%12u/%%%us"
32 #define BLOB_TYPE "DATA_BLOB"
33 #define BLOB_TYPE_LEN 9
35 static struct tdb_context
*cache
;
36 static struct tdb_context
*cache_notrans
;
40 * @brief Generic, persistent and shared between processes cache mechanism
41 * for use by various parts of the Samba code
47 * Cache initialisation function. Opens cache tdb file or creates
48 * it if does not exist.
50 * @return true on successful initialisation of the cache or
54 static bool gencache_init(void)
56 char* cache_fname
= NULL
;
57 int open_flags
= O_RDWR
|O_CREAT
;
59 /* skip file open if it's already opened */
60 if (cache
) return True
;
62 cache_fname
= lock_path("gencache.tdb");
64 DEBUG(5, ("Opening cache file at %s\n", cache_fname
));
66 cache
= tdb_open_log(cache_fname
, 0, TDB_DEFAULT
, open_flags
, 0644);
68 if (!cache
&& (errno
== EACCES
)) {
69 open_flags
= O_RDONLY
;
70 cache
= tdb_open_log(cache_fname
, 0, TDB_DEFAULT
, open_flags
,
73 DEBUG(5, ("gencache_init: Opening cache file %s read-only.\n", cache_fname
));
78 DEBUG(5, ("Attempt to open gencache.tdb has failed.\n"));
82 cache_fname
= lock_path("gencache_notrans.tdb");
84 DEBUG(5, ("Opening cache file at %s\n", cache_fname
));
86 cache_notrans
= tdb_open_log(cache_fname
, 0, TDB_CLEAR_IF_FIRST
,
88 if (cache_notrans
== NULL
) {
89 DEBUG(5, ("Opening %s failed: %s\n", cache_fname
,
98 static TDB_DATA
last_stabilize_key(void)
101 result
.dptr
= (uint8_t *)"@LAST_STABILIZED";
107 * Set an entry in the cache file. If there's no such
110 * @param keystr string that represents a key of this entry
111 * @param blob DATA_BLOB value being cached
112 * @param timeout time when the value is expired
114 * @retval true when entry is successfuly stored
115 * @retval false on failure
118 bool gencache_set_data_blob(const char *keystr
, const DATA_BLOB
*blob
,
124 time_t last_stabilize
;
125 static int writecount
;
127 if (tdb_data_cmp(string_term_tdb_data(keystr
),
128 last_stabilize_key()) == 0) {
129 DEBUG(10, ("Can't store %s as a key\n", keystr
));
133 if ((keystr
== NULL
) || (blob
== NULL
)) {
137 if (!gencache_init()) return False
;
139 val
= talloc_asprintf(talloc_tos(), CACHE_DATA_FMT
, (int)timeout
);
143 val
= talloc_realloc(NULL
, val
, char, talloc_array_length(val
)-1);
147 val
= (char *)talloc_append_blob(NULL
, val
, *blob
);
152 DEBUG(10, ("Adding cache entry with key = %s and timeout ="
153 " %s (%d seconds %s)\n", keystr
, ctime(&timeout
),
154 (int)(timeout
- time(NULL
)),
155 timeout
> time(NULL
) ? "ahead" : "in the past"));
157 ret
= tdb_store_bystring(
158 cache_notrans
, keystr
,
159 make_tdb_data((uint8_t *)val
, talloc_array_length(val
)),
168 * Every 100 writes within a single process, stabilize the cache with
169 * a transaction. This is done to prevent a single transaction to
170 * become huge and chew lots of memory.
173 if (writecount
> lp_parm_int(-1, "gencache", "stabilize_count", 100)) {
174 gencache_stabilize();
180 * Every 5 minutes, call gencache_stabilize() to not let grow
181 * gencache_notrans.tdb too large.
185 databuf
= tdb_fetch(cache_notrans
, last_stabilize_key());
186 if ((databuf
.dptr
!= NULL
)
187 && (databuf
.dptr
[databuf
.dsize
-1] == '\0')) {
188 last_stabilize
= atoi((char *)databuf
.dptr
);
189 SAFE_FREE(databuf
.dptr
);
192 + lp_parm_int(-1, "gencache", "stabilize_interval", 300))
194 gencache_stabilize();
202 * Delete one entry from the cache file.
204 * @param keystr string that represents a key of this entry
206 * @retval true upon successful deletion
207 * @retval false in case of failure
210 bool gencache_del(const char *keystr
)
212 bool exists
, was_expired
;
216 if (keystr
== NULL
) {
220 if (!gencache_init()) return False
;
222 DEBUG(10, ("Deleting cache entry (key = %s)\n", keystr
));
225 * We delete an element by setting its timeout to 0. This way we don't
226 * have to do a transaction on gencache.tdb every time we delete an
230 exists
= gencache_get_data_blob(keystr
, &value
, NULL
, &was_expired
);
232 if (!exists
&& was_expired
) {
234 * gencache_get_data_blob has implicitly deleted this
235 * entry, so we have to return success here.
241 data_blob_free(&value
);
242 ret
= gencache_set(keystr
, "", 0);
247 static bool gencache_pull_timeout(char *val
, time_t *pres
, char **pendptr
)
252 res
= strtol(val
, &endptr
, 10);
254 if ((endptr
== NULL
) || (*endptr
!= '/')) {
255 DEBUG(2, ("Invalid gencache data format: %s\n", val
));
261 if (pendptr
!= NULL
) {
268 * Get existing entry from the cache file.
270 * @param keystr string that represents a key of this entry
271 * @param blob DATA_BLOB that is filled with entry's blob
272 * @param timeout pointer to a time_t that is filled with entry's
275 * @retval true when entry is successfuly fetched
276 * @retval False for failure
279 bool gencache_get_data_blob(const char *keystr
, DATA_BLOB
*blob
,
280 time_t *timeout
, bool *was_expired
)
285 bool expired
= false;
287 if (keystr
== NULL
) {
291 if (tdb_data_cmp(string_term_tdb_data(keystr
),
292 last_stabilize_key()) == 0) {
293 DEBUG(10, ("Can't get %s as a key\n", keystr
));
297 if (!gencache_init()) {
301 databuf
= tdb_fetch_bystring(cache_notrans
, keystr
);
303 if (databuf
.dptr
== NULL
) {
304 databuf
= tdb_fetch_bystring(cache
, keystr
);
307 if (databuf
.dptr
== NULL
) {
308 DEBUG(10, ("Cache entry with key = %s couldn't be found \n",
313 if (!gencache_pull_timeout((char *)databuf
.dptr
, &t
, &endptr
)) {
314 SAFE_FREE(databuf
.dptr
);
318 DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, "
319 "timeout = %s", t
> time(NULL
) ? "valid" :
320 "expired", keystr
, endptr
+1, ctime(&t
)));
324 SAFE_FREE(databuf
.dptr
);
328 if (t
<= time(NULL
)) {
331 * We're expired, delete the entry. We can't use gencache_del
332 * here, because that uses gencache_get_data_blob for checking
333 * the existence of a record. We know the thing exists and
334 * directly store an empty value with 0 timeout.
336 gencache_set(keystr
, "", 0);
338 SAFE_FREE(databuf
.dptr
);
347 databuf
.dsize
- PTR_DIFF(endptr
+1, databuf
.dptr
));
348 if (blob
->data
== NULL
) {
349 SAFE_FREE(databuf
.dptr
);
350 DEBUG(0, ("memdup failed\n"));
355 SAFE_FREE(databuf
.dptr
);
364 if (was_expired
!= NULL
) {
365 *was_expired
= expired
;
370 struct stabilize_state
{
374 static int stabilize_fn(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA val
,
380 * Migrate the clear-if-first gencache data to the stable,
381 * transaction-based gencache.tdb
384 bool gencache_stabilize(void)
386 struct stabilize_state state
;
390 if (!gencache_init()) {
394 res
= tdb_transaction_start(cache
);
396 DEBUG(10, ("Could not start transaction on gencache.tdb: "
397 "%s\n", tdb_errorstr(cache
)));
400 res
= tdb_transaction_start(cache_notrans
);
402 tdb_transaction_cancel(cache
);
403 DEBUG(10, ("Could not start transaction on "
404 "gencache_notrans.tdb: %s\n",
405 tdb_errorstr(cache_notrans
)));
410 state
.written
= false;
412 res
= tdb_traverse(cache_notrans
, stabilize_fn
, &state
);
413 if ((res
== -1) || state
.error
) {
414 if ((tdb_transaction_cancel(cache_notrans
) == -1)
415 || (tdb_transaction_cancel(cache
) == -1)) {
416 smb_panic("tdb_transaction_cancel failed\n");
421 if (!state
.written
) {
422 if ((tdb_transaction_cancel(cache_notrans
) == -1)
423 || (tdb_transaction_cancel(cache
) == -1)) {
424 smb_panic("tdb_transaction_cancel failed\n");
429 res
= tdb_transaction_commit(cache
);
431 DEBUG(10, ("tdb_transaction_commit on gencache.tdb failed: "
432 "%s\n", tdb_errorstr(cache
)));
433 if (tdb_transaction_cancel(cache_notrans
) == -1) {
434 smb_panic("tdb_transaction_cancel failed\n");
439 res
= tdb_transaction_commit(cache_notrans
);
441 DEBUG(10, ("tdb_transaction_commit on gencache.tdb failed: "
442 "%s\n", tdb_errorstr(cache
)));
446 now
= talloc_asprintf(talloc_tos(), "%d", (int)time(NULL
));
448 tdb_store(cache_notrans
, last_stabilize_key(),
449 string_term_tdb_data(now
), 0);
456 static int stabilize_fn(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA val
,
459 struct stabilize_state
*state
= (struct stabilize_state
*)priv
;
463 if (tdb_data_cmp(key
, last_stabilize_key()) == 0) {
467 if (!gencache_pull_timeout((char *)val
.dptr
, &timeout
, NULL
)) {
468 DEBUG(10, ("Ignoring invalid entry\n"));
471 if ((timeout
< time(NULL
)) || (val
.dsize
== 0)) {
472 res
= tdb_delete(cache
, key
);
473 if ((res
== -1) && (tdb_error(cache
) == TDB_ERR_NOEXIST
)) {
476 state
->written
= true;
479 res
= tdb_store(cache
, key
, val
, 0);
481 state
->written
= true;
486 DEBUG(10, ("Transfer to gencache.tdb failed: %s\n",
487 tdb_errorstr(cache
)));
492 if (tdb_delete(cache_notrans
, key
) == -1) {
493 DEBUG(10, ("tdb_delete from gencache_notrans.tdb failed: "
494 "%s\n", tdb_errorstr(cache_notrans
)));
502 * Get existing entry from the cache file.
504 * @param keystr string that represents a key of this entry
505 * @param valstr buffer that is allocated and filled with the entry value
506 * buffer's disposing must be done outside
507 * @param timeout pointer to a time_t that is filled with entry's
510 * @retval true when entry is successfuly fetched
511 * @retval False for failure
514 bool gencache_get(const char *keystr
, char **value
, time_t *ptimeout
)
519 ret
= gencache_get_data_blob(keystr
, &blob
, ptimeout
, NULL
);
523 if ((blob
.data
== NULL
) || (blob
.length
== 0)) {
524 SAFE_FREE(blob
.data
);
527 if (blob
.data
[blob
.length
-1] != '\0') {
528 /* Not NULL terminated, can't be a string */
529 SAFE_FREE(blob
.data
);
532 *value
= SMB_STRDUP((char *)blob
.data
);
533 data_blob_free(&blob
);
534 if (*value
== NULL
) {
541 * Set an entry in the cache file. If there's no such
544 * @param keystr string that represents a key of this entry
545 * @param value text representation value being cached
546 * @param timeout time when the value is expired
548 * @retval true when entry is successfuly stored
549 * @retval false on failure
552 bool gencache_set(const char *keystr
, const char *value
, time_t timeout
)
554 DATA_BLOB blob
= data_blob_const(value
, strlen(value
)+1);
555 return gencache_set_data_blob(keystr
, &blob
, timeout
);
559 * Iterate through all entries which key matches to specified pattern
561 * @param fn pointer to the function that will be supplied with each single
562 * matching cache entry (key, value and timeout) as an arguments
563 * @param data void pointer to an arbitrary data that is passed directly to the fn
564 * function on each call
565 * @param keystr_pattern pattern the existing entries' keys are matched to
569 struct gencache_iterate_state
{
570 void (*fn
)(const char *key
, const char *value
, time_t timeout
,
577 static int gencache_iterate_fn(struct tdb_context
*tdb
, TDB_DATA key
,
578 TDB_DATA value
, void *priv
)
580 struct gencache_iterate_state
*state
=
581 (struct gencache_iterate_state
*)priv
;
583 char *free_key
= NULL
;
585 char *free_val
= NULL
;
590 if (tdb_data_cmp(key
, last_stabilize_key()) == 0) {
594 if (state
->in_persistent
&& tdb_exists(cache_notrans
, key
)) {
598 if (key
.dptr
[key
.dsize
-1] == '\0') {
599 keystr
= (char *)key
.dptr
;
601 /* ensure 0-termination */
602 keystr
= SMB_STRNDUP((char *)key
.dptr
, key
.dsize
);
606 if ((value
.dptr
== NULL
) || (value
.dsize
<= TIMEOUT_LEN
)) {
610 if (fnmatch(state
->pattern
, keystr
, 0) != 0) {
614 if (value
.dptr
[value
.dsize
-1] == '\0') {
615 valstr
= (char *)value
.dptr
;
617 /* ensure 0-termination */
618 valstr
= SMB_STRNDUP((char *)value
.dptr
, value
.dsize
);
622 u
= strtoul(valstr
, &timeout_endp
, 10);
624 if ((*timeout_endp
!= '/') || ((timeout_endp
-valstr
) != TIMEOUT_LEN
)) {
631 DEBUG(10, ("Calling function with arguments "
632 "(key = %s, value = %s, timeout = %s)\n",
633 keystr
, timeout_endp
, ctime(&timeout
)));
634 state
->fn(keystr
, timeout_endp
, timeout
, state
->priv
);
642 void gencache_iterate(void (*fn
)(const char* key
, const char *value
, time_t timeout
, void* dptr
),
643 void* data
, const char* keystr_pattern
)
645 struct gencache_iterate_state state
;
647 if ((fn
== NULL
) || (keystr_pattern
== NULL
)) {
651 if (!gencache_init()) return;
653 DEBUG(5, ("Searching cache keys with pattern %s\n", keystr_pattern
));
656 state
.pattern
= keystr_pattern
;
659 state
.in_persistent
= false;
660 tdb_traverse(cache_notrans
, gencache_iterate_fn
, &state
);
662 state
.in_persistent
= true;
663 tdb_traverse(cache
, gencache_iterate_fn
, &state
);