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
;
38 * @brief Generic, persistent and shared between processes cache mechanism
39 * for use by various parts of the Samba code
45 * Cache initialisation function. Opens cache tdb file or creates
46 * it if does not exist.
48 * @return true on successful initialisation of the cache or
52 bool gencache_init(void)
54 char* cache_fname
= NULL
;
56 /* skip file open if it's already opened */
57 if (cache
) return True
;
59 cache_fname
= lock_path("gencache.tdb");
61 DEBUG(5, ("Opening cache file at %s\n", cache_fname
));
63 cache
= tdb_open_log(cache_fname
, 0, TDB_DEFAULT
,
64 O_RDWR
|O_CREAT
, 0644);
66 if (!cache
&& (errno
== EACCES
)) {
67 cache
= tdb_open_log(cache_fname
, 0, TDB_DEFAULT
, O_RDONLY
, 0644);
69 DEBUG(5, ("gencache_init: Opening cache file %s read-only.\n", cache_fname
));
74 DEBUG(5, ("Attempt to open gencache.tdb has failed.\n"));
82 * Cache shutdown function. Closes opened cache tdb file.
84 * @return true on successful closing the cache or
85 * false on failure during cache shutdown
88 bool gencache_shutdown(void)
91 /* tdb_close routine returns -1 on error */
92 if (!cache
) return False
;
93 DEBUG(5, ("Closing cache file\n"));
94 ret
= tdb_close(cache
);
101 * Set an entry in the cache file. If there's no such
104 * @param keystr string that represents a key of this entry
105 * @param value text representation value being cached
106 * @param timeout time when the value is expired
108 * @retval true when entry is successfuly stored
109 * @retval false on failure
112 bool gencache_set(const char *keystr
, const char *value
, time_t timeout
)
118 /* fail completely if get null pointers passed */
119 SMB_ASSERT(keystr
&& value
);
121 if (!gencache_init()) return False
;
123 if (asprintf(&valstr
, CACHE_DATA_FMT
, (int)timeout
, value
) == -1) {
127 databuf
= string_term_tdb_data(valstr
);
128 DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
129 " %s (%d seconds %s)\n", keystr
, value
,ctime(&timeout
),
130 (int)(timeout
- time(NULL
)),
131 timeout
> time(NULL
) ? "ahead" : "in the past"));
133 ret
= tdb_store_bystring(cache
, keystr
, databuf
, 0);
140 * Delete one entry from the cache file.
142 * @param keystr string that represents a key of this entry
144 * @retval true upon successful deletion
145 * @retval false in case of failure
148 bool gencache_del(const char *keystr
)
152 /* fail completely if get null pointers passed */
155 if (!gencache_init()) return False
;
157 DEBUG(10, ("Deleting cache entry (key = %s)\n", keystr
));
158 ret
= tdb_delete_bystring(cache
, keystr
);
165 * Get existing entry from the cache file.
167 * @param keystr string that represents a key of this entry
168 * @param valstr buffer that is allocated and filled with the entry value
169 * buffer's disposing must be done outside
170 * @param timeout pointer to a time_t that is filled with entry's
173 * @retval true when entry is successfuly fetched
174 * @retval False for failure
177 bool gencache_get(const char *keystr
, char **valstr
, time_t *timeout
)
183 /* fail completely if get null pointers passed */
186 if (!gencache_init()) {
190 databuf
= tdb_fetch_bystring(cache
, keystr
);
192 if (databuf
.dptr
== NULL
) {
193 DEBUG(10, ("Cache entry with key = %s couldn't be found\n",
198 t
= strtol((const char *)databuf
.dptr
, &endptr
, 10);
200 if ((endptr
== NULL
) || (*endptr
!= '/')) {
201 DEBUG(2, ("Invalid gencache data format: %s\n", databuf
.dptr
));
202 SAFE_FREE(databuf
.dptr
);
206 DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, "
207 "timeout = %s", t
> time(NULL
) ? "valid" :
208 "expired", keystr
, endptr
+1, ctime(&t
)));
210 if (t
<= time(NULL
)) {
212 /* We're expired, delete the entry */
213 tdb_delete_bystring(cache
, keystr
);
215 SAFE_FREE(databuf
.dptr
);
220 *valstr
= SMB_STRDUP(endptr
+1);
221 if (*valstr
== NULL
) {
222 SAFE_FREE(databuf
.dptr
);
223 DEBUG(0, ("strdup failed\n"));
228 SAFE_FREE(databuf
.dptr
);
238 * Get existing entry from the cache file.
240 * @param keystr string that represents a key of this entry
241 * @param blob DATA_BLOB that is filled with entry's blob
242 * @param expired pointer to a bool that indicates whether the entry is expired
244 * @retval true when entry is successfuly fetched
245 * @retval False for failure
248 bool gencache_get_data_blob(const char *keystr
, DATA_BLOB
*blob
, bool *expired
)
253 unsigned char *buf
= NULL
;
256 int buflen
= 0, len
= 0, blob_len
= 0;
257 unsigned char *blob_buf
= NULL
;
259 /* fail completely if get null pointers passed */
262 if (!gencache_init()) {
266 databuf
= tdb_fetch_bystring(cache
, keystr
);
268 DEBUG(10,("Cache entry with key = %s couldn't be found\n",
273 buf
= (unsigned char *)databuf
.dptr
;
274 buflen
= databuf
.dsize
;
276 len
+= tdb_unpack(buf
+len
, buflen
-len
, "fB",
278 &blob_len
, &blob_buf
);
283 t
= strtol(valstr
, &blob_type
, 10);
285 if (strcmp(blob_type
+1, BLOB_TYPE
) != 0) {
289 DEBUG(10,("Returning %s cache entry: key = %s, "
290 "timeout = %s", t
> time(NULL
) ? "valid" :
291 "expired", keystr
, ctime(&t
)));
293 if (t
<= time(NULL
)) {
301 *blob
= data_blob(blob_buf
, blob_len
);
310 SAFE_FREE(databuf
.dptr
);
316 * Set an entry in the cache file. If there's no such
319 * @param keystr string that represents a key of this entry
320 * @param blob DATA_BLOB value being cached
321 * @param timeout time when the value is expired
323 * @retval true when entry is successfuly stored
324 * @retval false on failure
327 bool gencache_set_data_blob(const char *keystr
, DATA_BLOB
*blob
, time_t timeout
)
333 unsigned char *buf
= NULL
;
334 int len
= 0, buflen
= 0;
336 /* fail completely if get null pointers passed */
337 SMB_ASSERT(keystr
&& blob
);
339 if (!gencache_init()) {
343 if (asprintf(&valstr
, "%12u/%s", (int)timeout
, BLOB_TYPE
) == -1) {
350 len
+= tdb_pack(buf
+len
, buflen
-len
, "fB",
352 blob
->length
, blob
->data
);
360 buf
= SMB_MALLOC_ARRAY(unsigned char, len
);
368 databuf
= make_tdb_data(buf
, len
);
370 DEBUG(10,("Adding cache entry with key = %s; "
371 "blob size = %d and timeout = %s"
372 "(%d seconds %s)\n", keystr
, (int)databuf
.dsize
,
373 ctime(&timeout
), (int)(timeout
- time(NULL
)),
374 timeout
> time(NULL
) ? "ahead" : "in the past"));
376 tdb_ret
= tdb_store_bystring(cache
, keystr
, databuf
, 0);
389 * Iterate through all entries which key matches to specified pattern
391 * @param fn pointer to the function that will be supplied with each single
392 * matching cache entry (key, value and timeout) as an arguments
393 * @param data void pointer to an arbitrary data that is passed directly to the fn
394 * function on each call
395 * @param keystr_pattern pattern the existing entries' keys are matched to
399 void gencache_iterate(void (*fn
)(const char* key
, const char *value
, time_t timeout
, void* dptr
),
400 void* data
, const char* keystr_pattern
)
402 TDB_LIST_NODE
*node
, *first_node
;
404 char *keystr
= NULL
, *valstr
= NULL
, *entry
= NULL
;
409 /* fail completely if get null pointers passed */
410 SMB_ASSERT(fn
&& keystr_pattern
);
412 if (!gencache_init()) return;
414 DEBUG(5, ("Searching cache keys with pattern %s\n", keystr_pattern
));
415 node
= tdb_search_keys(cache
, keystr_pattern
);
421 /* ensure null termination of the key string */
422 keystr
= SMB_STRNDUP((const char *)node
->node_key
.dptr
, node
->node_key
.dsize
);
428 * We don't use gencache_get function, because we need to iterate through
429 * all of the entries. Validity verification is up to fn routine.
431 databuf
= tdb_fetch(cache
, node
->node_key
);
432 if (!databuf
.dptr
|| databuf
.dsize
<= TIMEOUT_LEN
) {
433 SAFE_FREE(databuf
.dptr
);
438 entry
= SMB_STRNDUP((const char *)databuf
.dptr
, databuf
.dsize
);
440 SAFE_FREE(databuf
.dptr
);
445 SAFE_FREE(databuf
.dptr
);
447 valstr
= (char *)SMB_MALLOC(databuf
.dsize
+ 1 - TIMEOUT_LEN
);
454 if (asprintf(&fmt
, READ_CACHE_DATA_FMT_TEMPLATE
,
455 (unsigned int)databuf
.dsize
- TIMEOUT_LEN
)
462 status
= sscanf(entry
, fmt
, &u
, valstr
);
466 DEBUG(0,("gencache_iterate: invalid return from sscanf %d\n",status
));
470 DEBUG(10, ("Calling function with arguments (key = %s, value = %s, timeout = %s)\n",
471 keystr
, valstr
, ctime(&timeout
)));
472 fn(keystr
, valstr
, timeout
, data
);
480 tdb_search_list_free(first_node
);
483 /********************************************************************
485 ********************************************************************/
487 int gencache_lock_entry( const char *key
)
489 if (!gencache_init())
492 return tdb_lock_bystring(cache
, key
);
495 /********************************************************************
497 ********************************************************************/
499 void gencache_unlock_entry( const char *key
)
501 if (!gencache_init())
504 tdb_unlock_bystring(cache
, key
);