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 TDB_CONTEXT
*cache
;
35 static BOOL cache_readonly
;
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 BOOL
gencache_init(void)
55 char* cache_fname
= NULL
;
57 /* skip file open if it's already opened */
58 if (cache
) return True
;
60 cache_fname
= lock_path("gencache.tdb");
62 DEBUG(5, ("Opening cache file at %s\n", cache_fname
));
64 cache
= tdb_open_log(cache_fname
, 0, TDB_DEFAULT
,
65 O_RDWR
|O_CREAT
, 0644);
67 if (!cache
&& (errno
== EACCES
)) {
68 cache
= tdb_open_log(cache_fname
, 0, TDB_DEFAULT
, O_RDONLY
, 0644);
70 cache_readonly
= True
;
71 DEBUG(5, ("gencache_init: Opening cache file %s read-only.\n", cache_fname
));
76 DEBUG(5, ("Attempt to open gencache.tdb has failed.\n"));
84 * Cache shutdown function. Closes opened cache tdb file.
86 * @return true on successful closing the cache or
87 * false on failure during cache shutdown
90 BOOL
gencache_shutdown(void)
93 /* tdb_close routine returns -1 on error */
94 if (!cache
) return False
;
95 DEBUG(5, ("Closing cache file\n"));
96 ret
= tdb_close(cache
);
98 cache_readonly
= False
;
104 * Set an entry in the cache file. If there's no such
107 * @param keystr string that represents a key of this entry
108 * @param value text representation value being cached
109 * @param timeout time when the value is expired
111 * @retval true when entry is successfuly stored
112 * @retval false on failure
115 BOOL
gencache_set(const char *keystr
, const char *value
, time_t timeout
)
121 /* fail completely if get null pointers passed */
122 SMB_ASSERT(keystr
&& value
);
124 if (!gencache_init()) return False
;
126 if (cache_readonly
) {
130 asprintf(&valstr
, CACHE_DATA_FMT
, (int)timeout
, value
);
134 databuf
= string_term_tdb_data(valstr
);
135 DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
136 " %s (%d seconds %s)\n", keystr
, value
,ctime(&timeout
),
137 (int)(timeout
- time(NULL
)),
138 timeout
> time(NULL
) ? "ahead" : "in the past"));
140 ret
= tdb_store_bystring(cache
, keystr
, databuf
, 0);
147 * Delete one entry from the cache file.
149 * @param keystr string that represents a key of this entry
151 * @retval true upon successful deletion
152 * @retval false in case of failure
155 BOOL
gencache_del(const char *keystr
)
159 /* fail completely if get null pointers passed */
162 if (!gencache_init()) return False
;
164 if (cache_readonly
) {
168 DEBUG(10, ("Deleting cache entry (key = %s)\n", keystr
));
169 ret
= tdb_delete_bystring(cache
, keystr
);
176 * Get existing entry from the cache file.
178 * @param keystr string that represents a key of this entry
179 * @param valstr buffer that is allocated and filled with the entry value
180 * buffer's disposing must be done outside
181 * @param timeout pointer to a time_t that is filled with entry's
184 * @retval true when entry is successfuly fetched
185 * @retval False for failure
188 BOOL
gencache_get(const char *keystr
, char **valstr
, time_t *timeout
)
194 /* fail completely if get null pointers passed */
197 if (!gencache_init()) {
201 databuf
= tdb_fetch_bystring(cache
, keystr
);
203 if (databuf
.dptr
== NULL
) {
204 DEBUG(10, ("Cache entry with key = %s couldn't be found\n",
209 t
= strtol((const char *)databuf
.dptr
, &endptr
, 10);
211 if ((endptr
== NULL
) || (*endptr
!= '/')) {
212 DEBUG(2, ("Invalid gencache data format: %s\n", databuf
.dptr
));
213 SAFE_FREE(databuf
.dptr
);
217 DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, "
218 "timeout = %s", t
> time(NULL
) ? "valid" :
219 "expired", keystr
, endptr
+1, ctime(&t
)));
221 if (t
<= time(NULL
)) {
223 /* We're expired, delete the entry */
224 tdb_delete_bystring(cache
, keystr
);
226 SAFE_FREE(databuf
.dptr
);
231 *valstr
= SMB_STRDUP(endptr
+1);
232 if (*valstr
== NULL
) {
233 SAFE_FREE(databuf
.dptr
);
234 DEBUG(0, ("strdup failed\n"));
239 SAFE_FREE(databuf
.dptr
);
249 * Get existing entry from the cache file.
251 * @param keystr string that represents a key of this entry
252 * @param blob DATA_BLOB that is filled with entry's blob
253 * @param expired pointer to a BOOL that indicates whether the entry is expired
255 * @retval true when entry is successfuly fetched
256 * @retval False for failure
259 BOOL
gencache_get_data_blob(const char *keystr
, DATA_BLOB
*blob
, BOOL
*expired
)
264 unsigned char *buf
= NULL
;
267 int buflen
= 0, len
= 0, blob_len
= 0;
268 unsigned char *blob_buf
= NULL
;
270 /* fail completely if get null pointers passed */
273 if (!gencache_init()) {
277 databuf
= tdb_fetch_bystring(cache
, keystr
);
279 DEBUG(10,("Cache entry with key = %s couldn't be found\n",
284 buf
= (unsigned char *)databuf
.dptr
;
285 buflen
= databuf
.dsize
;
287 len
+= tdb_unpack(buf
+len
, buflen
-len
, "fB",
289 &blob_len
, &blob_buf
);
294 t
= strtol(valstr
, &blob_type
, 10);
296 if (strcmp(blob_type
+1, BLOB_TYPE
) != 0) {
300 DEBUG(10,("Returning %s cache entry: key = %s, "
301 "timeout = %s", t
> time(NULL
) ? "valid" :
302 "expired", keystr
, ctime(&t
)));
304 if (t
<= time(NULL
)) {
312 *blob
= data_blob(blob_buf
, blob_len
);
321 SAFE_FREE(databuf
.dptr
);
327 * Set an entry in the cache file. If there's no such
330 * @param keystr string that represents a key of this entry
331 * @param blob DATA_BLOB value being cached
332 * @param timeout time when the value is expired
334 * @retval true when entry is successfuly stored
335 * @retval false on failure
338 BOOL
gencache_set_data_blob(const char *keystr
, DATA_BLOB
*blob
, time_t timeout
)
344 unsigned char *buf
= NULL
;
345 int len
= 0, buflen
= 0;
347 /* fail completely if get null pointers passed */
348 SMB_ASSERT(keystr
&& blob
);
350 if (!gencache_init()) {
354 if (cache_readonly
) {
358 asprintf(&valstr
, "%12u/%s", (int)timeout
, BLOB_TYPE
);
366 len
+= tdb_pack(buf
+len
, buflen
-len
, "fB",
368 blob
->length
, blob
->data
);
376 buf
= SMB_MALLOC_ARRAY(unsigned char, len
);
384 databuf
= make_tdb_data(buf
, len
);
386 DEBUG(10,("Adding cache entry with key = %s; "
387 "blob size = %d and timeout = %s"
388 "(%d seconds %s)\n", keystr
, (int)databuf
.dsize
,
389 ctime(&timeout
), (int)(timeout
- time(NULL
)),
390 timeout
> time(NULL
) ? "ahead" : "in the past"));
392 tdb_ret
= tdb_store_bystring(cache
, keystr
, databuf
, 0);
405 * Iterate through all entries which key matches to specified pattern
407 * @param fn pointer to the function that will be supplied with each single
408 * matching cache entry (key, value and timeout) as an arguments
409 * @param data void pointer to an arbitrary data that is passed directly to the fn
410 * function on each call
411 * @param keystr_pattern pattern the existing entries' keys are matched to
415 void gencache_iterate(void (*fn
)(const char* key
, const char *value
, time_t timeout
, void* dptr
),
416 void* data
, const char* keystr_pattern
)
418 TDB_LIST_NODE
*node
, *first_node
;
420 char *keystr
= NULL
, *valstr
= NULL
, *entry
= NULL
;
425 /* fail completely if get null pointers passed */
426 SMB_ASSERT(fn
&& keystr_pattern
);
428 if (!gencache_init()) return;
430 DEBUG(5, ("Searching cache keys with pattern %s\n", keystr_pattern
));
431 node
= tdb_search_keys(cache
, keystr_pattern
);
437 /* ensure null termination of the key string */
438 keystr
= SMB_STRNDUP((const char *)node
->node_key
.dptr
, node
->node_key
.dsize
);
444 * We don't use gencache_get function, because we need to iterate through
445 * all of the entries. Validity verification is up to fn routine.
447 databuf
= tdb_fetch(cache
, node
->node_key
);
448 if (!databuf
.dptr
|| databuf
.dsize
<= TIMEOUT_LEN
) {
449 SAFE_FREE(databuf
.dptr
);
454 entry
= SMB_STRNDUP((const char *)databuf
.dptr
, databuf
.dsize
);
456 SAFE_FREE(databuf
.dptr
);
461 SAFE_FREE(databuf
.dptr
);
463 valstr
= (char *)SMB_MALLOC(databuf
.dsize
+ 1 - TIMEOUT_LEN
);
470 asprintf(&fmt
, READ_CACHE_DATA_FMT_TEMPLATE
, (unsigned int)databuf
.dsize
- TIMEOUT_LEN
);
477 status
= sscanf(entry
, fmt
, &u
, valstr
);
481 DEBUG(0,("gencache_iterate: invalid return from sscanf %d\n",status
));
485 DEBUG(10, ("Calling function with arguments (key = %s, value = %s, timeout = %s)\n",
486 keystr
, valstr
, ctime(&timeout
)));
487 fn(keystr
, valstr
, timeout
, data
);
495 tdb_search_list_free(first_node
);
498 /********************************************************************
500 ********************************************************************/
502 int gencache_lock_entry( const char *key
)
504 if (!gencache_init())
507 return tdb_lock_bystring(cache
, key
);
510 /********************************************************************
512 ********************************************************************/
514 void gencache_unlock_entry( const char *key
)
516 if (!gencache_init())
519 tdb_unlock_bystring(cache
, key
);