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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #define DBGC_CLASS DBGC_TDB
29 #define TIMEOUT_LEN 12
30 #define CACHE_DATA_FMT "%12u/%s"
31 #define READ_CACHE_DATA_FMT_TEMPLATE "%%12u/%%%us"
33 static TDB_CONTEXT
*cache
;
34 static BOOL cache_readonly
;
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 cache_readonly
= True
;
70 DEBUG(5, ("gencache_init: Opening cache file %s read-only.\n", cache_fname
));
75 DEBUG(5, ("Attempt to open gencache.tdb has failed.\n"));
83 * Cache shutdown function. Closes opened cache tdb file.
85 * @return true on successful closing the cache or
86 * false on failure during cache shutdown
89 BOOL
gencache_shutdown(void)
92 /* tdb_close routine returns -1 on error */
93 if (!cache
) return False
;
94 DEBUG(5, ("Closing cache file\n"));
95 ret
= tdb_close(cache
);
97 cache_readonly
= False
;
103 * Set an entry in the cache file. If there's no such
106 * @param keystr string that represents a key of this entry
107 * @param value text representation value being cached
108 * @param timeout time when the value is expired
110 * @retval true when entry is successfuly stored
111 * @retval false on failure
114 BOOL
gencache_set(const char *keystr
, const char *value
, time_t timeout
)
120 /* fail completely if get null pointers passed */
121 SMB_ASSERT(keystr
&& value
);
123 if (!gencache_init()) return False
;
125 if (cache_readonly
) {
129 asprintf(&valstr
, CACHE_DATA_FMT
, (int)timeout
, value
);
133 databuf
= string_term_tdb_data(valstr
);
134 DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
135 " %s (%d seconds %s)\n", keystr
, value
,ctime(&timeout
),
136 (int)(timeout
- time(NULL
)),
137 timeout
> time(NULL
) ? "ahead" : "in the past"));
139 ret
= tdb_store_bystring(cache
, keystr
, databuf
, 0);
146 * Delete one entry from the cache file.
148 * @param keystr string that represents a key of this entry
150 * @retval true upon successful deletion
151 * @retval false in case of failure
154 BOOL
gencache_del(const char *keystr
)
158 /* fail completely if get null pointers passed */
161 if (!gencache_init()) return False
;
163 if (cache_readonly
) {
167 DEBUG(10, ("Deleting cache entry (key = %s)\n", keystr
));
168 ret
= tdb_delete_bystring(cache
, keystr
);
175 * Get existing entry from the cache file.
177 * @param keystr string that represents a key of this entry
178 * @param valstr buffer that is allocated and filled with the entry value
179 * buffer's disposing must be done outside
180 * @param timeout pointer to a time_t that is filled with entry's
183 * @retval true when entry is successfuly fetched
184 * @retval False for failure
187 BOOL
gencache_get(const char *keystr
, char **valstr
, time_t *timeout
)
193 /* fail completely if get null pointers passed */
196 if (!gencache_init()) {
200 databuf
= tdb_fetch_bystring(cache
, keystr
);
202 if (databuf
.dptr
== NULL
) {
203 DEBUG(10, ("Cache entry with key = %s couldn't be found\n",
208 t
= strtol((const char *)databuf
.dptr
, &endptr
, 10);
210 if ((endptr
== NULL
) || (*endptr
!= '/')) {
211 DEBUG(2, ("Invalid gencache data format: %s\n", databuf
.dptr
));
212 SAFE_FREE(databuf
.dptr
);
216 DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, "
217 "timeout = %s", t
> time(NULL
) ? "valid" :
218 "expired", keystr
, endptr
+1, ctime(&t
)));
220 if (t
<= time(NULL
)) {
222 /* We're expired, delete the entry */
223 tdb_delete_bystring(cache
, keystr
);
225 SAFE_FREE(databuf
.dptr
);
230 *valstr
= SMB_STRDUP(endptr
+1);
231 if (*valstr
== NULL
) {
232 SAFE_FREE(databuf
.dptr
);
233 DEBUG(0, ("strdup failed\n"));
238 SAFE_FREE(databuf
.dptr
);
249 * Iterate through all entries which key matches to specified pattern
251 * @param fn pointer to the function that will be supplied with each single
252 * matching cache entry (key, value and timeout) as an arguments
253 * @param data void pointer to an arbitrary data that is passed directly to the fn
254 * function on each call
255 * @param keystr_pattern pattern the existing entries' keys are matched to
259 void gencache_iterate(void (*fn
)(const char* key
, const char *value
, time_t timeout
, void* dptr
),
260 void* data
, const char* keystr_pattern
)
262 TDB_LIST_NODE
*node
, *first_node
;
264 char *keystr
= NULL
, *valstr
= NULL
, *entry
= NULL
;
269 /* fail completely if get null pointers passed */
270 SMB_ASSERT(fn
&& keystr_pattern
);
272 if (!gencache_init()) return;
274 DEBUG(5, ("Searching cache keys with pattern %s\n", keystr_pattern
));
275 node
= tdb_search_keys(cache
, keystr_pattern
);
281 /* ensure null termination of the key string */
282 keystr
= SMB_STRNDUP((const char *)node
->node_key
.dptr
, node
->node_key
.dsize
);
288 * We don't use gencache_get function, because we need to iterate through
289 * all of the entries. Validity verification is up to fn routine.
291 databuf
= tdb_fetch(cache
, node
->node_key
);
292 if (!databuf
.dptr
|| databuf
.dsize
<= TIMEOUT_LEN
) {
293 SAFE_FREE(databuf
.dptr
);
298 entry
= SMB_STRNDUP((const char *)databuf
.dptr
, databuf
.dsize
);
300 SAFE_FREE(databuf
.dptr
);
305 SAFE_FREE(databuf
.dptr
);
307 valstr
= (char *)SMB_MALLOC(databuf
.dsize
+ 1 - TIMEOUT_LEN
);
314 asprintf(&fmt
, READ_CACHE_DATA_FMT_TEMPLATE
, (unsigned int)databuf
.dsize
- TIMEOUT_LEN
);
321 status
= sscanf(entry
, fmt
, &u
, valstr
);
325 DEBUG(0,("gencache_iterate: invalid return from sscanf %d\n",status
));
329 DEBUG(10, ("Calling function with arguments (key = %s, value = %s, timeout = %s)\n",
330 keystr
, valstr
, ctime(&timeout
)));
331 fn(keystr
, valstr
, timeout
, data
);
339 tdb_search_list_free(first_node
);
342 /********************************************************************
344 ********************************************************************/
346 int gencache_lock_entry( const char *key
)
348 if (!gencache_init())
351 return tdb_lock_bystring(cache
, key
);
354 /********************************************************************
356 ********************************************************************/
358 void gencache_unlock_entry( const char *key
)
360 if (!gencache_init())
363 tdb_unlock_bystring(cache
, key
);