From cdca2ad1c4ca396e130a1091063c3aeedbb06cd8 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Wed, 11 Sep 2002 14:07:21 +0000 Subject: added gencache implementation from mimir - thanks! (This used to be commit 05a202c287f5daeb1ccbaf9479aa93e7928e93db) --- source3/lib/gencache.c | 319 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 319 insertions(+) create mode 100644 source3/lib/gencache.c (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c new file mode 100644 index 0000000000..9e2009ad4a --- /dev/null +++ b/source3/lib/gencache.c @@ -0,0 +1,319 @@ +/* + Unix SMB/CIFS implementation. + + Generic, persistent and shared between processes cache mechanism for use + by various parts of the Samba code + + Copyright (C) Rafal Szczesniak 2002 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include "includes.h" + +#undef DBGC_CLASS +#define DBGC_CLASS DBGC_TDB + +#define TIMEOUT_LEN 12 +#define CACHE_DATA_FMT "%12u/%s" + +static TDB_CONTEXT *cache; + +/** + * @file gencache.c + * @brief Generic, persistent and shared between processes cache mechanism + * for use by various parts of the Samba code + * + **/ + + +/** + * Cache initialisation function. Opens cache tdb file or creates + * it if does not exist. + * + * @return true on successful initialisation of the cache or + * false on failure + **/ + +BOOL gencache_init(void) +{ + char* cache_fname = NULL; + + /* skip file open if it's already opened */ + if (cache) return True; + + asprintf(&cache_fname, "%s/%s", lp_lockdir(), "gencache.tdb"); + if (cache_fname) + DEBUG(5, ("Opening cache file at %s\n", cache_fname)); + else { + DEBUG(0, ("Filename allocation failed.\n")); + return False; + } + + cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT, + O_RDWR|O_CREAT, 0644); + + SAFE_FREE(cache_fname); + if (!cache) { + DEBUG(0, ("Attempt to open the cache file has failed.\n")); + return False; + } + return True; +} + + +/** + * Cache shutdown function. Closes opened cache tdb file. + * + * @return true on successful closing the cache or + * false on failure during cache shutdown + **/ + +BOOL gencache_shutdown(void) +{ + /* tdb_close routine returns 0 on successful close */ + if (!cache) return False; + DEBUG(5, ("Closing cache file\n")); + return tdb_close(cache) ? False : True; +} + + +/** + * Add one entry to the cache file. + * (it part of tridge's proposed API) + * + * @param key string that represents a key of this entry + * @param value text representation value being cached + * @param timeout time when the value is expired + * + * @return true when entry is successfuly stored or + * false on the attempt's failure + **/ + +BOOL gencache_add(const char *keystr, const char *value, time_t timeout) +{ + int ret; + TDB_DATA keybuf, databuf; + char* valstr = NULL; + + /* fail completely if get null pointers passed */ + SMB_ASSERT(keystr && value); + + if (!gencache_init()) return False; + + asprintf(&valstr, CACHE_DATA_FMT, (int)timeout, value); + keybuf.dptr = strdup(keystr); + keybuf.dsize = strlen(keystr); + databuf.dptr = strdup(valstr); + databuf.dsize = strlen(valstr); + DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout \ + = %s (%d seconds %s)\n", keybuf.dptr, value, ctime(&timeout), + (int)(timeout - time(NULL)), timeout > time(NULL) ? "ahead" : "in the past")); + + ret = tdb_store(cache, keybuf, databuf, TDB_INSERT); + SAFE_FREE(valstr); + SAFE_FREE(keybuf.dptr); + SAFE_FREE(databuf.dptr); + + return ret == 0 ? True : False; +} + + +/** + * Set existing entry to the cache file. + * (it part of tridge's proposed API) + * + * @param key string that represents a key of this entry + * @param value text representation value being cached + * @param timeout time when the value is expired + * + * @return true when entry is successfuly set or + * false on the attempt's failure + **/ + +BOOL gencache_set(const char *keystr, const char *valstr, time_t timeout) +{ + int ret = -1; + TDB_DATA keybuf, databuf; + char *old_valstr, *datastr; + time_t old_timeout; + + /* fail completely if get null pointers passed */ + SMB_ASSERT(keystr && valstr); + + if (!gencache_init()) return False; + + /* + * Check whether entry exists in the cache + * Don't verify gencache_get exit code, since the entry may be expired + */ + gencache_get(keystr, &old_valstr, &old_timeout); + + if (!(old_valstr && old_timeout)) return False; + + DEBUG(10, ("Setting cache entry with key = %s; old value = %s and old timeout \ + = %s\n", keystr, old_valstr, ctime(&old_timeout))); + + asprintf(&datastr, CACHE_DATA_FMT, (int)timeout, valstr); + keybuf.dptr = strdup(keystr); + keybuf.dsize = strlen(keystr); + databuf.dptr = strdup(datastr); + databuf.dsize = strlen(datastr); + DEBUGADD(10, ("New value = %s, new timeout = %s (%d seconds %s)", valstr, + ctime(&timeout), (int)(timeout - time(NULL)), + timeout > time(NULL) ? "ahead" : "in the past")); + + + ret = tdb_store(cache, keybuf, databuf, TDB_REPLACE); + + SAFE_FREE(datastr); + SAFE_FREE(old_valstr); + SAFE_FREE(keybuf.dptr); + SAFE_FREE(databuf.dptr); + + return ret == 0 ? True : False; +} + + +/** + * Delete one entry from the cache file. + * (it part of tridge's proposed API) + * + * @param key string that represents a key of this entry + * + * @return true upon successful deletion or + * false in case of failure + **/ + +BOOL gencache_del(const char *keystr) +{ + int ret; + TDB_DATA keybuf; + + /* fail completely if get null pointers passed */ + SMB_ASSERT(keystr); + + if (!gencache_init()) return False; + + keybuf.dptr = strdup(keystr); + keybuf.dsize = strlen(keystr); + DEBUG(10, ("Deleting cache entry (key = %s)\n", keystr)); + ret = tdb_delete(cache, keybuf); + + SAFE_FREE(keybuf.dptr); + return ret == 0 ? True : False; +} + + +/** + * Get existing entry from the cache file. + * (it part of tridge's proposed API) + * + * @param key string that represents a key of this entry + * @param value buffer that is allocated and filled with the entry value + * buffer's disposing is done outside + * @param timeout pointer to a time_t that is filled with entry's + * timeout + * + * @return true when entry is successfuly fetched or + * false on the failure + **/ + +BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout) +{ + TDB_DATA keybuf, databuf; + + /* fail completely if get null pointers passed */ + SMB_ASSERT(keystr && valstr && timeout); + + if (!gencache_init()) return False; + + keybuf.dptr = strdup(keystr); + keybuf.dsize = strlen(keystr); + databuf = tdb_fetch(cache, keybuf); + + if (databuf.dptr) { + char* entry_buf = strndup(databuf.dptr, databuf.dsize); + *valstr = (char*)malloc(sizeof(char) * (databuf.dsize - TIMEOUT_LEN)); + + sscanf(entry_buf, CACHE_DATA_FMT, (int*)timeout, *valstr); + SAFE_FREE(entry_buf); + + DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, timeout = %s\n", + *timeout > time(NULL) ? "valid" : "expired", keystr, *valstr, + ctime(timeout))); + return *timeout > time(NULL); + } else { + *valstr = NULL; + timeout = NULL; + DEBUG(10, ("Cache entry with key = %s couldn't be found\n", keystr)); + return False; + } +} + + +/** + * Iterate through all entries which key matches to specified pattern + * + * @param fn pointer to the function that will be supplied with each single + * matching cache entry (key, value and timeout) as an arguments + * @param keystr_pattern pattern the existing entries' keys are matched to + * + **/ + +void gencache_iterate(void (*fn)(const char* key, const char *value, time_t timeout), + const char* keystr_pattern) +{ + TDB_LIST_NODE *node, *first_node; + TDB_DATA databuf; + char *keystr = NULL, *valstr = NULL, *entry = NULL; + time_t timeout = 0; + + /* fail completely if get null pointers passed */ + SMB_ASSERT(fn && keystr_pattern); + + if (!gencache_init()) return; + + DEBUG(5, ("Searching cache keys with pattern %s", keystr_pattern)); + node = tdb_search_keys(cache, keystr_pattern); + first_node = node; + + while (node) { + /* ensure null termination of the key string */ + node->node_key.dptr[node->node_key.dsize] = '\0'; + keystr = node->node_key.dptr; + + /* + * We don't use gencache_get function, because we need to iterate through + * all of the entries. Validity verification is up to fn routine. + */ + databuf = tdb_fetch(cache, node->node_key); + entry = strndup(databuf.dptr, databuf.dsize); + valstr = (char*)malloc(sizeof(char) * (databuf.dsize - TIMEOUT_LEN)); + sscanf(entry, CACHE_DATA_FMT, (int*)(&timeout), valstr); + + DEBUG(10, ("Calling function with arguments (key = %s, value = %s, timeout = %s)\n", + keystr, valstr, ctime(&timeout))); + fn(keystr, valstr, timeout); + + SAFE_FREE(valstr); + SAFE_FREE(entry); + node = node->next; + } + + tdb_search_list_free(first_node); +} + + -- cgit From de474974ea25df7738dd175126e3f1de0df47ea6 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Sat, 23 Nov 2002 02:52:36 +0000 Subject: Lots of fixes for error paths where tdb_fetch() data need freeing. Found via a post from Arcady Chernyak . Jeremy. (This used to be commit 5d5762d1787db4392d2dff16024097c638b2d494) --- source3/lib/gencache.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index 9e2009ad4a..a872f1331c 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -238,16 +238,18 @@ BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout) /* fail completely if get null pointers passed */ SMB_ASSERT(keystr && valstr && timeout); - if (!gencache_init()) return False; + if (!gencache_init()) + return False; keybuf.dptr = strdup(keystr); keybuf.dsize = strlen(keystr); databuf = tdb_fetch(cache, keybuf); - if (databuf.dptr) { + if (databuf.dptr && databuf.dsize > TIMEOUT_LEN) { char* entry_buf = strndup(databuf.dptr, databuf.dsize); *valstr = (char*)malloc(sizeof(char) * (databuf.dsize - TIMEOUT_LEN)); + SAFE_FREE(databuf.dptr); sscanf(entry_buf, CACHE_DATA_FMT, (int*)timeout, *valstr); SAFE_FREE(entry_buf); @@ -256,6 +258,7 @@ BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout) ctime(timeout))); return *timeout > time(NULL); } else { + SAFE_FREE(databuf.dptr); *valstr = NULL; timeout = NULL; DEBUG(10, ("Cache entry with key = %s couldn't be found\n", keystr)); @@ -300,7 +303,12 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time * all of the entries. Validity verification is up to fn routine. */ databuf = tdb_fetch(cache, node->node_key); + if (!databuf.dptr || databuf.dsize <= TIMEOUT_LEN) { + SAFE_FREE(databuf.dptr); + continue; + } entry = strndup(databuf.dptr, databuf.dsize); + SAFE_FREE(databuf.dptr); valstr = (char*)malloc(sizeof(char) * (databuf.dsize - TIMEOUT_LEN)); sscanf(entry, CACHE_DATA_FMT, (int*)(&timeout), valstr); @@ -315,5 +323,3 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time tdb_search_list_free(first_node); } - - -- cgit From 863e9ca2c640ce7a94acf81cff7408edc6f64e01 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Sat, 4 Jan 2003 08:48:15 +0000 Subject: Merge from HEAD - mimir's new gencache based namecache code. Andrew Bartlett (This used to be commit f79324f730c400342f445c931b0d75ff756d7cc7) --- source3/lib/gencache.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index a872f1331c..a7df7c95db 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -91,8 +91,8 @@ BOOL gencache_shutdown(void) /** - * Add one entry to the cache file. - * (it part of tridge's proposed API) + * Set an entry in the cache file. If there's no such + * one, then add it. * * @param key string that represents a key of this entry * @param value text representation value being cached @@ -102,7 +102,7 @@ BOOL gencache_shutdown(void) * false on the attempt's failure **/ -BOOL gencache_add(const char *keystr, const char *value, time_t timeout) +BOOL gencache_set(const char *keystr, const char *value, time_t timeout) { int ret; TDB_DATA keybuf, databuf; @@ -122,7 +122,7 @@ BOOL gencache_add(const char *keystr, const char *value, time_t timeout) = %s (%d seconds %s)\n", keybuf.dptr, value, ctime(&timeout), (int)(timeout - time(NULL)), timeout > time(NULL) ? "ahead" : "in the past")); - ret = tdb_store(cache, keybuf, databuf, TDB_INSERT); + ret = tdb_store(cache, keybuf, databuf, 0); SAFE_FREE(valstr); SAFE_FREE(keybuf.dptr); SAFE_FREE(databuf.dptr); @@ -133,7 +133,6 @@ BOOL gencache_add(const char *keystr, const char *value, time_t timeout) /** * Set existing entry to the cache file. - * (it part of tridge's proposed API) * * @param key string that represents a key of this entry * @param value text representation value being cached @@ -143,7 +142,7 @@ BOOL gencache_add(const char *keystr, const char *value, time_t timeout) * false on the attempt's failure **/ -BOOL gencache_set(const char *keystr, const char *valstr, time_t timeout) +BOOL gencache_set_only(const char *keystr, const char *valstr, time_t timeout) { int ret = -1; TDB_DATA keybuf, databuf; @@ -189,7 +188,6 @@ BOOL gencache_set(const char *keystr, const char *valstr, time_t timeout) /** * Delete one entry from the cache file. - * (it part of tridge's proposed API) * * @param key string that represents a key of this entry * @@ -219,11 +217,10 @@ BOOL gencache_del(const char *keystr) /** * Get existing entry from the cache file. - * (it part of tridge's proposed API) * * @param key string that represents a key of this entry * @param value buffer that is allocated and filled with the entry value - * buffer's disposing is done outside + * buffer's disposing must be done outside * @param timeout pointer to a time_t that is filled with entry's * timeout * @@ -272,12 +269,14 @@ BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout) * * @param fn pointer to the function that will be supplied with each single * matching cache entry (key, value and timeout) as an arguments + * @param data void pointer to an arbitrary data that is passed directly to the fn + * function on each call * @param keystr_pattern pattern the existing entries' keys are matched to * **/ -void gencache_iterate(void (*fn)(const char* key, const char *value, time_t timeout), - const char* keystr_pattern) +void gencache_iterate(void (*fn)(const char* key, const char *value, time_t timeout, void* dptr), + void* data, const char* keystr_pattern) { TDB_LIST_NODE *node, *first_node; TDB_DATA databuf; @@ -289,7 +288,7 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time if (!gencache_init()) return; - DEBUG(5, ("Searching cache keys with pattern %s", keystr_pattern)); + DEBUG(5, ("Searching cache keys with pattern %s\n", keystr_pattern)); node = tdb_search_keys(cache, keystr_pattern); first_node = node; @@ -314,7 +313,7 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time DEBUG(10, ("Calling function with arguments (key = %s, value = %s, timeout = %s)\n", keystr, valstr, ctime(&timeout))); - fn(keystr, valstr, timeout); + fn(keystr, valstr, timeout, data); SAFE_FREE(valstr); SAFE_FREE(entry); -- cgit From aff13f53afe54506cdb679b355c7f9a3505c8736 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Sat, 4 Jan 2003 09:08:47 +0000 Subject: Merge from HEAD - whitespace :-) (This used to be commit 5fc90b6cf438480c9fcf8d01f0a319a52c5914cc) --- source3/lib/gencache.c | 1 + 1 file changed, 1 insertion(+) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index a7df7c95db..d0748456f9 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -322,3 +322,4 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time tdb_search_list_free(first_node); } + -- cgit From 7cc3cf9410dedcf242254ff017c28cabb86276c6 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Fri, 24 Jan 2003 21:20:31 +0000 Subject: masking an assert error in Tim's wins server checking code; needs a proper fix after I talk to tpot (This used to be commit 607bc0b4fca38640c6b5c730aa4119e2aa9006fc) --- source3/lib/gencache.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index d0748456f9..3ff67b9918 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -233,7 +233,9 @@ BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout) TDB_DATA keybuf, databuf; /* fail completely if get null pointers passed */ +#if 0 /* JERRY */ SMB_ASSERT(keystr && valstr && timeout); +#endif if (!gencache_init()) return False; @@ -242,6 +244,15 @@ BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout) keybuf.dsize = strlen(keystr); databuf = tdb_fetch(cache, keybuf); + /* special case for tpot */ + if ( !valstr && !timeout ) { + BOOL result = False; + + result = (databuf.dptr == NULL); + SAFE_FREE(databuf.dptr); + return result; + } + if (databuf.dptr && databuf.dsize > TIMEOUT_LEN) { char* entry_buf = strndup(databuf.dptr, databuf.dsize); *valstr = (char*)malloc(sizeof(char) * (databuf.dsize - TIMEOUT_LEN)); -- cgit From f911f03a7db94635bf78d2c9b4748700af757bb5 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Fri, 24 Jan 2003 21:25:12 +0000 Subject: sync with tpot's change to gencache_get() in HEAD (This used to be commit 305f167db2a34cdf3dd6378954e815ce34111232) --- source3/lib/gencache.c | 53 +++++++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 22 deletions(-) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index 3ff67b9918..a844d8c014 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -233,9 +233,7 @@ BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout) TDB_DATA keybuf, databuf; /* fail completely if get null pointers passed */ -#if 0 /* JERRY */ - SMB_ASSERT(keystr && valstr && timeout); -#endif + SMB_ASSERT(keystr); if (!gencache_init()) return False; @@ -244,32 +242,44 @@ BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout) keybuf.dsize = strlen(keystr); databuf = tdb_fetch(cache, keybuf); - /* special case for tpot */ - if ( !valstr && !timeout ) { - BOOL result = False; - - result = (databuf.dptr == NULL); - SAFE_FREE(databuf.dptr); - return result; - } - if (databuf.dptr && databuf.dsize > TIMEOUT_LEN) { char* entry_buf = strndup(databuf.dptr, databuf.dsize); - *valstr = (char*)malloc(sizeof(char) * (databuf.dsize - TIMEOUT_LEN)); + char *v; + time_t t; + + v = (char*)malloc(sizeof(char) * + (databuf.dsize - TIMEOUT_LEN)); SAFE_FREE(databuf.dptr); - sscanf(entry_buf, CACHE_DATA_FMT, (int*)timeout, *valstr); + sscanf(entry_buf, CACHE_DATA_FMT, (int*)&t, v); SAFE_FREE(entry_buf); - DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, timeout = %s\n", - *timeout > time(NULL) ? "valid" : "expired", keystr, *valstr, - ctime(timeout))); - return *timeout > time(NULL); + DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, " + "timeout = %s\n", t > time(NULL) ? "valid" : + "expired", keystr, v, ctime(&t))); + + if (valstr) + *valstr = v; + else + SAFE_FREE(v); + + if (timeout) + *timeout = t; + + return t > time(NULL); + } else { SAFE_FREE(databuf.dptr); - *valstr = NULL; - timeout = NULL; - DEBUG(10, ("Cache entry with key = %s couldn't be found\n", keystr)); + + if (valstr) + *valstr = NULL; + + if (timeout) + timeout = NULL; + + DEBUG(10, ("Cache entry with key = %s couldn't be found\n", + keystr)); + return False; } } @@ -333,4 +343,3 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time tdb_search_list_free(first_node); } - -- cgit From a68de8f42c305d4e77df2072dcd9ae470d32efbf Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Wed, 12 Feb 2003 01:20:56 +0000 Subject: Merge of nicer error message for gencache open error from HEAD. (This used to be commit 23ca54000514d400c81950edb556ef4308a88253) --- source3/lib/gencache.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index a844d8c014..2c356d24e8 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -67,7 +67,7 @@ BOOL gencache_init(void) SAFE_FREE(cache_fname); if (!cache) { - DEBUG(0, ("Attempt to open the cache file has failed.\n")); + DEBUG(5, ("Attempt to open gencache.tdb has failed.\n")); return False; } return True; -- cgit From aa1a248336776079ba305744d35f529bd17d2e94 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Wed, 19 Feb 2003 22:50:29 +0000 Subject: Merge cleanups of return values. (This used to be commit bae354a20c07458722b5193911e7eaaf9bfbaeb7) --- source3/lib/gencache.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index 2c356d24e8..7260e477b8 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -83,10 +83,10 @@ BOOL gencache_init(void) BOOL gencache_shutdown(void) { - /* tdb_close routine returns 0 on successful close */ + /* tdb_close routine returns -1 on error */ if (!cache) return False; DEBUG(5, ("Closing cache file\n")); - return tdb_close(cache) ? False : True; + return tdb_close(cache) != -1; } @@ -127,7 +127,7 @@ BOOL gencache_set(const char *keystr, const char *value, time_t timeout) SAFE_FREE(keybuf.dptr); SAFE_FREE(databuf.dptr); - return ret == 0 ? True : False; + return ret == 0; } @@ -182,7 +182,7 @@ BOOL gencache_set_only(const char *keystr, const char *valstr, time_t timeout) SAFE_FREE(keybuf.dptr); SAFE_FREE(databuf.dptr); - return ret == 0 ? True : False; + return ret == 0; } @@ -211,7 +211,7 @@ BOOL gencache_del(const char *keystr) ret = tdb_delete(cache, keybuf); SAFE_FREE(keybuf.dptr); - return ret == 0 ? True : False; + return ret == 0; } -- cgit From 1f499a79f5468e87d26b60ffe3aa375b91cadbef Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Sat, 22 Mar 2003 13:47:42 +0000 Subject: (merge from HEAD) Small clenaup patches: - safe_string.h - don't assume that __FUNCTION__ is available - process.c - use new workaround from safe_string.h for the same - util.c - Show how many bytes we smb_panic()ed trying to smb_xmalloc() - gencache.c - Keep valgrind quiet by always null terminating. - clistr.c - Add copyright - srvstr.h - move srvstr_push into a .c file again, as a real function. - srvstr.c - revive, with 'safe' checked srvstr_push - loadparm.c - set a default for the display charset. - connection.c - use safe_strcpy() Andrew Bartlett (This used to be commit c91e76bddbe1244ddc8d12b092eba875834029ac) --- source3/lib/gencache.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index 7260e477b8..6a66ce95b9 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -115,9 +115,9 @@ BOOL gencache_set(const char *keystr, const char *value, time_t timeout) asprintf(&valstr, CACHE_DATA_FMT, (int)timeout, value); keybuf.dptr = strdup(keystr); - keybuf.dsize = strlen(keystr); + keybuf.dsize = strlen(keystr)+1; databuf.dptr = strdup(valstr); - databuf.dsize = strlen(valstr); + databuf.dsize = strlen(valstr)+1; DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout \ = %s (%d seconds %s)\n", keybuf.dptr, value, ctime(&timeout), (int)(timeout - time(NULL)), timeout > time(NULL) ? "ahead" : "in the past")); @@ -167,9 +167,9 @@ BOOL gencache_set_only(const char *keystr, const char *valstr, time_t timeout) asprintf(&datastr, CACHE_DATA_FMT, (int)timeout, valstr); keybuf.dptr = strdup(keystr); - keybuf.dsize = strlen(keystr); + keybuf.dsize = strlen(keystr)+1; databuf.dptr = strdup(datastr); - databuf.dsize = strlen(datastr); + databuf.dsize = strlen(datastr)+1; DEBUGADD(10, ("New value = %s, new timeout = %s (%d seconds %s)", valstr, ctime(&timeout), (int)(timeout - time(NULL)), timeout > time(NULL) ? "ahead" : "in the past")); @@ -206,7 +206,7 @@ BOOL gencache_del(const char *keystr) if (!gencache_init()) return False; keybuf.dptr = strdup(keystr); - keybuf.dsize = strlen(keystr); + keybuf.dsize = strlen(keystr)+1; DEBUG(10, ("Deleting cache entry (key = %s)\n", keystr)); ret = tdb_delete(cache, keybuf); @@ -239,7 +239,7 @@ BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout) return False; keybuf.dptr = strdup(keystr); - keybuf.dsize = strlen(keystr); + keybuf.dsize = strlen(keystr)+1; databuf = tdb_fetch(cache, keybuf); if (databuf.dptr && databuf.dsize > TIMEOUT_LEN) { -- cgit From 79f3265893a60c9109b02407d15d13f18925c751 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Sat, 22 Mar 2003 23:32:50 +0000 Subject: (merge from HEAD) Valgrind found some memory leaks! (This used to be commit 8315b9c3119dde62aeb72ad5e20f63aee89abd0b) --- source3/lib/gencache.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index 6a66ce95b9..baec0e3b37 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -114,6 +114,9 @@ BOOL gencache_set(const char *keystr, const char *value, time_t timeout) if (!gencache_init()) return False; asprintf(&valstr, CACHE_DATA_FMT, (int)timeout, value); + if (!valstr) + return False; + keybuf.dptr = strdup(keystr); keybuf.dsize = strlen(keystr)+1; databuf.dptr = strdup(valstr); @@ -241,6 +244,7 @@ BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout) keybuf.dptr = strdup(keystr); keybuf.dsize = strlen(keystr)+1; databuf = tdb_fetch(cache, keybuf); + SAFE_FREE(keybuf.dptr); if (databuf.dptr && databuf.dsize > TIMEOUT_LEN) { char* entry_buf = strndup(databuf.dptr, databuf.dsize); -- cgit From 564cd8cc2d86093f686c555629476986cf317f03 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Mon, 14 Apr 2003 02:18:10 +0000 Subject: Merge of doxygen updates from HEAD. (This used to be commit 6d7baad38fbdf0921c1d5eb59004c65cfe110975) --- source3/lib/gencache.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index baec0e3b37..40b4d1390d 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -94,12 +94,12 @@ BOOL gencache_shutdown(void) * Set an entry in the cache file. If there's no such * one, then add it. * - * @param key string that represents a key of this entry + * @param keystr string that represents a key of this entry * @param value text representation value being cached * @param timeout time when the value is expired * - * @return true when entry is successfuly stored or - * false on the attempt's failure + * @retval true when entry is successfuly stored + * @retval false on failure **/ BOOL gencache_set(const char *keystr, const char *value, time_t timeout) @@ -137,12 +137,12 @@ BOOL gencache_set(const char *keystr, const char *value, time_t timeout) /** * Set existing entry to the cache file. * - * @param key string that represents a key of this entry - * @param value text representation value being cached + * @param keystr string that represents a key of this entry + * @param valstr text representation value being cached * @param timeout time when the value is expired * - * @return true when entry is successfuly set or - * false on the attempt's failure + * @retval true when entry is successfuly set + * @retval false on failure **/ BOOL gencache_set_only(const char *keystr, const char *valstr, time_t timeout) @@ -192,10 +192,10 @@ BOOL gencache_set_only(const char *keystr, const char *valstr, time_t timeout) /** * Delete one entry from the cache file. * - * @param key string that represents a key of this entry + * @param keystr string that represents a key of this entry * - * @return true upon successful deletion or - * false in case of failure + * @retval true upon successful deletion + * @retval false in case of failure **/ BOOL gencache_del(const char *keystr) @@ -221,14 +221,14 @@ BOOL gencache_del(const char *keystr) /** * Get existing entry from the cache file. * - * @param key string that represents a key of this entry - * @param value buffer that is allocated and filled with the entry value + * @param keystr string that represents a key of this entry + * @param valstr buffer that is allocated and filled with the entry value * buffer's disposing must be done outside * @param timeout pointer to a time_t that is filled with entry's * timeout * - * @return true when entry is successfuly fetched or - * false on the failure + * @retval true when entry is successfuly fetched + * @retval False for failure **/ BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout) -- cgit From db6ce132e360a42ea6843c81429be194662fce39 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Tue, 1 Jul 2003 03:49:41 +0000 Subject: * fix the trustdom_cache to work when winbindd is not running. smbd will update the trustdom_cache periodically after locking the timestamp key (This used to be commit 7bc4b65b91f98271089335cc301146d5f0c76c3a) --- source3/lib/gencache.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index 40b4d1390d..58e44e3f0b 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -347,3 +347,24 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time tdb_search_list_free(first_node); } + +/******************************************************************** + lock a key +********************************************************************/ + +int gencache_lock_entry( const char *key ) +{ + return tdb_lock_bystring(cache, key, 0); +} + +/******************************************************************** + unlock a key +********************************************************************/ + +void gencache_unlock_entry( const char *key ) +{ + tdb_unlock_bystring(cache, key); + return; +} + + -- cgit From 2b0662b33a4f104ae9f900a2984ae2f07dcc3c45 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Tue, 1 Jul 2003 12:03:00 +0000 Subject: Fix a segfault found by metze & valgrind... Don't overwrite past the end of a string. Volker (This used to be commit f036368efdcbe576552ea85a78e5e6199a2b2c6d) --- source3/lib/gencache.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index 58e44e3f0b..f3740e3e12 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -319,9 +319,8 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time while (node) { /* ensure null termination of the key string */ - node->node_key.dptr[node->node_key.dsize] = '\0'; - keystr = node->node_key.dptr; - + keystr = strndup(node->node_key.dptr, node->node_key.dsize); + /* * We don't use gencache_get function, because we need to iterate through * all of the entries. Validity verification is up to fn routine. @@ -329,6 +328,8 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time databuf = tdb_fetch(cache, node->node_key); if (!databuf.dptr || databuf.dsize <= TIMEOUT_LEN) { SAFE_FREE(databuf.dptr); + SAFE_FREE(keystr); + node = node->next; continue; } entry = strndup(databuf.dptr, databuf.dsize); @@ -342,6 +343,7 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time SAFE_FREE(valstr); SAFE_FREE(entry); + SAFE_FREE(keystr); node = node->next; } -- cgit From b78dd91e68d6273d1fcd3470b1c3c333047fb186 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Tue, 1 Jul 2003 12:40:52 +0000 Subject: Fix two memory leaks. tdb_search_keys allocates space for the key strings. Running 'net cache list' or secrets_get_trusted_domains through valgrind gives a *huge* amount of invalid reads of one byte beyond the indicated string length in libc's strncpy. Annoying... Volker (This used to be commit 0f8933ae778064ff58cdc832ce52c843631435bb) --- source3/lib/gencache.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index f3740e3e12..b94e35eff4 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -329,6 +329,7 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time if (!databuf.dptr || databuf.dsize <= TIMEOUT_LEN) { SAFE_FREE(databuf.dptr); SAFE_FREE(keystr); + SAFE_FREE(node->node_key.dptr); node = node->next; continue; } @@ -344,6 +345,7 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time SAFE_FREE(valstr); SAFE_FREE(entry); SAFE_FREE(keystr); + SAFE_FREE(node->node_key.dptr); node = node->next; } -- cgit From f13e48e2ee205036983fb5c2abe28b4bfaa31856 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Tue, 1 Jul 2003 13:04:50 +0000 Subject: Different fix for memleak just committed. This belongs into tdb_search_list_free. Volker (This used to be commit 0f3822c8e71426983b960ad49511efa8707159f9) --- source3/lib/gencache.c | 2 -- 1 file changed, 2 deletions(-) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index b94e35eff4..f3740e3e12 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -329,7 +329,6 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time if (!databuf.dptr || databuf.dsize <= TIMEOUT_LEN) { SAFE_FREE(databuf.dptr); SAFE_FREE(keystr); - SAFE_FREE(node->node_key.dptr); node = node->next; continue; } @@ -345,7 +344,6 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time SAFE_FREE(valstr); SAFE_FREE(entry); SAFE_FREE(keystr); - SAFE_FREE(node->node_key.dptr); node = node->next; } -- cgit From 5660c449efd83231b9b965a24663ecc7aafa25e9 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Mon, 8 Sep 2003 01:28:48 +0000 Subject: Reformat debug. (This used to be commit 3aab7a3bc7920b6e479700c176ac7a7b80262f42) --- source3/lib/gencache.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index f3740e3e12..da8808af16 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -121,10 +121,11 @@ BOOL gencache_set(const char *keystr, const char *value, time_t timeout) keybuf.dsize = strlen(keystr)+1; databuf.dptr = strdup(valstr); databuf.dsize = strlen(valstr)+1; - DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout \ - = %s (%d seconds %s)\n", keybuf.dptr, value, ctime(&timeout), - (int)(timeout - time(NULL)), timeout > time(NULL) ? "ahead" : "in the past")); - + DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout =" + " %s (%d seconds %s)\n", keybuf.dptr, value,ctime(&timeout), + (int)(timeout - time(NULL)), + timeout > time(NULL) ? "ahead" : "in the past")); + ret = tdb_store(cache, keybuf, databuf, 0); SAFE_FREE(valstr); SAFE_FREE(keybuf.dptr); -- cgit From b86830a7cd2e0233e34358f829fa186233194976 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Mon, 5 Jan 2004 04:15:55 +0000 Subject: Always call the auto-init funciton - this avoids tdb segfaulting under us if we failed to open it earlier. Andrew Bartlett (This used to be commit 379368b0bec1f57cc5302b274362ce2f1df0fd9d) --- source3/lib/gencache.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index da8808af16..39e727c24f 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -357,6 +357,9 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time int gencache_lock_entry( const char *key ) { + if (!gencache_init()) + return -1; + return tdb_lock_bystring(cache, key, 0); } @@ -366,6 +369,9 @@ int gencache_lock_entry( const char *key ) void gencache_unlock_entry( const char *key ) { + if (!gencache_init()) + return; + tdb_unlock_bystring(cache, key); return; } -- cgit From acf9d61421faa6c0055d57fdee7db300dc5431aa Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 7 Dec 2004 18:25:53 +0000 Subject: r4088: Get medieval on our ass about malloc.... :-). Take control of all our allocation functions so we can funnel through some well known functions. Should help greatly with malloc checking. HEAD patch to follow. Jeremy. (This used to be commit 620f2e608f70ba92f032720c031283d295c5c06a) --- source3/lib/gencache.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index 39e727c24f..f2e267c9d4 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -117,9 +117,9 @@ BOOL gencache_set(const char *keystr, const char *value, time_t timeout) if (!valstr) return False; - keybuf.dptr = strdup(keystr); + keybuf.dptr = SMB_STRDUP(keystr); keybuf.dsize = strlen(keystr)+1; - databuf.dptr = strdup(valstr); + databuf.dptr = SMB_STRDUP(valstr); databuf.dsize = strlen(valstr)+1; DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout =" " %s (%d seconds %s)\n", keybuf.dptr, value,ctime(&timeout), @@ -170,9 +170,9 @@ BOOL gencache_set_only(const char *keystr, const char *valstr, time_t timeout) = %s\n", keystr, old_valstr, ctime(&old_timeout))); asprintf(&datastr, CACHE_DATA_FMT, (int)timeout, valstr); - keybuf.dptr = strdup(keystr); + keybuf.dptr = SMB_STRDUP(keystr); keybuf.dsize = strlen(keystr)+1; - databuf.dptr = strdup(datastr); + databuf.dptr = SMB_STRDUP(datastr); databuf.dsize = strlen(datastr)+1; DEBUGADD(10, ("New value = %s, new timeout = %s (%d seconds %s)", valstr, ctime(&timeout), (int)(timeout - time(NULL)), @@ -209,7 +209,7 @@ BOOL gencache_del(const char *keystr) if (!gencache_init()) return False; - keybuf.dptr = strdup(keystr); + keybuf.dptr = SMB_STRDUP(keystr); keybuf.dsize = strlen(keystr)+1; DEBUG(10, ("Deleting cache entry (key = %s)\n", keystr)); ret = tdb_delete(cache, keybuf); @@ -242,18 +242,17 @@ BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout) if (!gencache_init()) return False; - keybuf.dptr = strdup(keystr); + keybuf.dptr = SMB_STRDUP(keystr); keybuf.dsize = strlen(keystr)+1; databuf = tdb_fetch(cache, keybuf); SAFE_FREE(keybuf.dptr); if (databuf.dptr && databuf.dsize > TIMEOUT_LEN) { - char* entry_buf = strndup(databuf.dptr, databuf.dsize); + char* entry_buf = SMB_STRNDUP(databuf.dptr, databuf.dsize); char *v; time_t t; - v = (char*)malloc(sizeof(char) * - (databuf.dsize - TIMEOUT_LEN)); + v = SMB_MALLOC(databuf.dsize - TIMEOUT_LEN); SAFE_FREE(databuf.dptr); sscanf(entry_buf, CACHE_DATA_FMT, (int*)&t, v); @@ -320,7 +319,7 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time while (node) { /* ensure null termination of the key string */ - keystr = strndup(node->node_key.dptr, node->node_key.dsize); + keystr = SMB_STRNDUP(node->node_key.dptr, node->node_key.dsize); /* * We don't use gencache_get function, because we need to iterate through @@ -333,9 +332,9 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time node = node->next; continue; } - entry = strndup(databuf.dptr, databuf.dsize); + entry = SMB_STRNDUP(databuf.dptr, databuf.dsize); SAFE_FREE(databuf.dptr); - valstr = (char*)malloc(sizeof(char) * (databuf.dsize - TIMEOUT_LEN)); + valstr = SMB_MALLOC(databuf.dsize - TIMEOUT_LEN); sscanf(entry, CACHE_DATA_FMT, (int*)(&timeout), valstr); DEBUG(10, ("Calling function with arguments (key = %s, value = %s, timeout = %s)\n", -- cgit From 54abd2aa66069e6baf7769c496f46d9dba18db39 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Fri, 30 Sep 2005 17:13:37 +0000 Subject: r10656: BIG merge from trunk. Features not copied over * \PIPE\unixinfo * winbindd's {group,alias}membership new functions * winbindd's lookupsids() functionality * swat (trunk changes to be reverted as per discussion with Deryck) (This used to be commit 939c3cb5d78e3a2236209b296aa8aba8bdce32d3) --- source3/lib/gencache.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index f2e267c9d4..85599c92d3 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -251,11 +251,17 @@ BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout) char* entry_buf = SMB_STRNDUP(databuf.dptr, databuf.dsize); char *v; time_t t; + unsigned u; + int status; v = SMB_MALLOC(databuf.dsize - TIMEOUT_LEN); SAFE_FREE(databuf.dptr); - sscanf(entry_buf, CACHE_DATA_FMT, (int*)&t, v); + status = sscanf(entry_buf, CACHE_DATA_FMT, &u, v); + if ( status != 2 ) { + DEBUG(0, ("gencache_get: Invalid return %d from sscanf\n", status )); + } + t = u; SAFE_FREE(entry_buf); DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, " @@ -307,6 +313,8 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time TDB_DATA databuf; char *keystr = NULL, *valstr = NULL, *entry = NULL; time_t timeout = 0; + int status; + unsigned u; /* fail completely if get null pointers passed */ SMB_ASSERT(fn && keystr_pattern); @@ -335,7 +343,11 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time entry = SMB_STRNDUP(databuf.dptr, databuf.dsize); SAFE_FREE(databuf.dptr); valstr = SMB_MALLOC(databuf.dsize - TIMEOUT_LEN); - sscanf(entry, CACHE_DATA_FMT, (int*)(&timeout), valstr); + status = sscanf(entry, CACHE_DATA_FMT, &u, valstr); + if ( status != 2 ) { + DEBUG(0,("gencache_iterate: invalid return from sscanf %d\n",status)); + } + timeout = u; DEBUG(10, ("Calling function with arguments (key = %s, value = %s, timeout = %s)\n", keystr, valstr, ctime(&timeout))); -- cgit From aa363846d6c26869a780b1ad1ad2e3b3588a7387 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Fri, 13 Jan 2006 18:45:30 +0000 Subject: r12912: patch from Tony Mountifield for BUG 3327 (fix bad access to gencache.tdb after fork() in smbmount (This used to be commit 68399ce04ca4509d51950d2d7b1ed817e82bf17c) --- source3/lib/gencache.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index 85599c92d3..fd44616270 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -83,10 +83,13 @@ BOOL gencache_init(void) BOOL gencache_shutdown(void) { + int ret; /* tdb_close routine returns -1 on error */ if (!cache) return False; DEBUG(5, ("Closing cache file\n")); - return tdb_close(cache) != -1; + ret = tdb_close(cache); + cache = NULL; + return ret != -1; } -- cgit From 855e02f1649992f05b685be96dfff4a9140170e9 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Fri, 3 Feb 2006 21:19:24 +0000 Subject: r13310: first round of server affinity patches for winbindd & net ads join (This used to be commit 6c3480f9aecc061660ad5c06347b8f1d3e11a330) --- source3/lib/gencache.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index fd44616270..89badcd4f9 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -268,7 +268,7 @@ BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout) SAFE_FREE(entry_buf); DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, " - "timeout = %s\n", t > time(NULL) ? "valid" : + "timeout = %s", t > time(NULL) ? "valid" : "expired", keystr, v, ctime(&t))); if (valstr) @@ -281,20 +281,18 @@ BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout) return t > time(NULL); - } else { - SAFE_FREE(databuf.dptr); + } - if (valstr) - *valstr = NULL; + SAFE_FREE(databuf.dptr); - if (timeout) - timeout = NULL; + if (valstr) + *valstr = NULL; + if (timeout) + timeout = NULL; - DEBUG(10, ("Cache entry with key = %s couldn't be found\n", - keystr)); + DEBUG(10, ("Cache entry with key = %s couldn't be found\n", keystr)); - return False; - } + return False; } -- cgit From e17302200c138eec7df504a7f4b2bde46073a810 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Mon, 17 Apr 2006 11:49:06 +0000 Subject: r15101: Little step towards getting Samba4 tdb into 3: tdb_lock_bystring does not have the timeout argument in Samba4. Add a new routine tdb_lock_bystring_with_timeout. Volker (This used to be commit b9c6e3f55602fa505859a4b2cd137b74105d685f) --- source3/lib/gencache.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index 89badcd4f9..6725ed4c12 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -372,7 +372,7 @@ int gencache_lock_entry( const char *key ) if (!gencache_init()) return -1; - return tdb_lock_bystring(cache, key, 0); + return tdb_lock_bystring(cache, key); } /******************************************************************** -- cgit From 835bfbb8ac6a6239299a22a9e79643674a0a75e2 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 15 Jun 2006 21:03:40 +0000 Subject: r16267: Fix Klocwork #401, #402 - ensure format specifier limited. Fix memleak in printing gencache contents. Jeremy. (This used to be commit 81731e1f68cdf4af80733338238aeae0a7d108c0) --- source3/lib/gencache.c | 74 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 59 insertions(+), 15 deletions(-) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index 6725ed4c12..defc7ca206 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -28,6 +28,7 @@ #define TIMEOUT_LEN 12 #define CACHE_DATA_FMT "%12u/%s" +#define READ_CACHE_DATA_FMT_TEMPLATE "%%12u/%%%us" static TDB_CONTEXT *cache; @@ -242,8 +243,9 @@ BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout) /* fail completely if get null pointers passed */ SMB_ASSERT(keystr); - if (!gencache_init()) + if (!gencache_init()) { return False; + } keybuf.dptr = SMB_STRDUP(keystr); keybuf.dsize = strlen(keystr)+1; @@ -256,13 +258,26 @@ BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout) time_t t; unsigned u; int status; + char *fmt; + + v = SMB_MALLOC(databuf.dsize + 1 - TIMEOUT_LEN); + if (!v) { + return False; + } - v = SMB_MALLOC(databuf.dsize - TIMEOUT_LEN); - SAFE_FREE(databuf.dptr); - status = sscanf(entry_buf, CACHE_DATA_FMT, &u, v); + + asprintf(&fmt, READ_CACHE_DATA_FMT_TEMPLATE, (unsigned int)databuf.dsize - TIMEOUT_LEN); + if (!fmt) { + SAFE_FREE(v); + return False; + } + + status = sscanf(entry_buf, fmt, &u, v); + SAFE_FREE(fmt); + if ( status != 2 ) { - DEBUG(0, ("gencache_get: Invalid return %d from sscanf\n", status )); + DEBUG(0, ("gencache_get: Invalid return %d from sscanf\n", status )); } t = u; SAFE_FREE(entry_buf); @@ -271,13 +286,15 @@ BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout) "timeout = %s", t > time(NULL) ? "valid" : "expired", keystr, v, ctime(&t))); - if (valstr) + if (valstr) { *valstr = v; - else + } else { SAFE_FREE(v); + } - if (timeout) + if (timeout) { *timeout = t; + } return t > time(NULL); @@ -285,17 +302,17 @@ BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout) SAFE_FREE(databuf.dptr); - if (valstr) + if (valstr) { *valstr = NULL; - if (timeout) + } + if (timeout) { timeout = NULL; + } DEBUG(10, ("Cache entry with key = %s couldn't be found\n", keystr)); - return False; } - /** * Iterate through all entries which key matches to specified pattern * @@ -327,8 +344,13 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time first_node = node; while (node) { + char *fmt; + /* ensure null termination of the key string */ keystr = SMB_STRNDUP(node->node_key.dptr, node->node_key.dsize); + if (!keystr) { + return; + } /* * We don't use gencache_get function, because we need to iterate through @@ -342,11 +364,33 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time continue; } entry = SMB_STRNDUP(databuf.dptr, databuf.dsize); + if (!entry) { + SAFE_FREE(databuf.dptr); + SAFE_FREE(keystr); + return; + } + SAFE_FREE(databuf.dptr); - valstr = SMB_MALLOC(databuf.dsize - TIMEOUT_LEN); - status = sscanf(entry, CACHE_DATA_FMT, &u, valstr); + + valstr = SMB_MALLOC(databuf.dsize + 1 - TIMEOUT_LEN); + if (!valstr) { + SAFE_FREE(entry); + SAFE_FREE(keystr); + return; + } + + asprintf(&fmt, READ_CACHE_DATA_FMT_TEMPLATE, (unsigned int)databuf.dsize - TIMEOUT_LEN); + if (!fmt) { + SAFE_FREE(valstr); + SAFE_FREE(entry); + SAFE_FREE(keystr); + return; + } + status = sscanf(entry, fmt, &u, valstr); + SAFE_FREE(fmt); + if ( status != 2 ) { - DEBUG(0,("gencache_iterate: invalid return from sscanf %d\n",status)); + DEBUG(0,("gencache_iterate: invalid return from sscanf %d\n",status)); } timeout = u; -- cgit From 366a2d498d115503067f306a1891ec2063baaeb8 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Fri, 16 Jun 2006 12:30:39 +0000 Subject: r16290: Fix Coverity bugs 298 and 298 (This used to be commit b96808bb623c01f2515fdbdede8b4e9edebff23b) --- source3/lib/gencache.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index defc7ca206..561a019429 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -349,7 +349,7 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time /* ensure null termination of the key string */ keystr = SMB_STRNDUP(node->node_key.dptr, node->node_key.dsize); if (!keystr) { - return; + break; } /* @@ -367,7 +367,7 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time if (!entry) { SAFE_FREE(databuf.dptr); SAFE_FREE(keystr); - return; + break; } SAFE_FREE(databuf.dptr); @@ -376,7 +376,7 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time if (!valstr) { SAFE_FREE(entry); SAFE_FREE(keystr); - return; + break; } asprintf(&fmt, READ_CACHE_DATA_FMT_TEMPLATE, (unsigned int)databuf.dsize - TIMEOUT_LEN); @@ -384,7 +384,7 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time SAFE_FREE(valstr); SAFE_FREE(entry); SAFE_FREE(keystr); - return; + break; } status = sscanf(entry, fmt, &u, valstr); SAFE_FREE(fmt); -- cgit From 1cf1e648feed823244731eef5f56bd34e15cb045 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Mon, 31 Jul 2006 04:30:55 +0000 Subject: r17334: Some C++ warnings (This used to be commit 8ae7ed1f3cecbb5285313d17b5f9511e2e622f0b) --- source3/lib/gencache.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index 561a019429..d4582b34f9 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -260,7 +260,7 @@ BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout) int status; char *fmt; - v = SMB_MALLOC(databuf.dsize + 1 - TIMEOUT_LEN); + v = (char *)SMB_MALLOC(databuf.dsize + 1 - TIMEOUT_LEN); if (!v) { return False; } @@ -372,7 +372,7 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time SAFE_FREE(databuf.dptr); - valstr = SMB_MALLOC(databuf.dsize + 1 - TIMEOUT_LEN); + valstr = (char *)SMB_MALLOC(databuf.dsize + 1 - TIMEOUT_LEN); if (!valstr) { SAFE_FREE(entry); SAFE_FREE(keystr); -- cgit From 2abab7ee6d04a62017d99578c274244a1cdd27b2 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 30 Aug 2006 04:40:03 +0000 Subject: r17928: Implement the basic store for CLDAP sitename support when looking up DC's. On every CLDAP call store the returned client sitename (if present, delete store if not) in gencache with infinate timeout. On AD DNS DC lookup, try looking for sitename DC's first, only try generic if sitename DNS lookup failed. I still haven't figured out yet how to ensure we fetch the sitename with a CLDAP query before doing the generic DC list lookup. This code is difficult to understand. I'll do some experiments and backtraces tomorrow to try and work out where to force a CLDAP site query first. Jeremy. (This used to be commit ab3f0c5b1e9c5fd192c5514cbe9451b938f9cd5d) --- source3/lib/gencache.c | 2 -- 1 file changed, 2 deletions(-) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index d4582b34f9..fe038011d8 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -431,5 +431,3 @@ void gencache_unlock_entry( const char *key ) tdb_unlock_bystring(cache, key); return; } - - -- cgit From 96c72e2f8152f2b9e006a392e6e8d1006b9cdd2c Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Sat, 9 Sep 2006 21:05:51 +0000 Subject: r18310: Add a little test for some gencache routines Remove unused gencache_set_only Use CONST_DISCARD instead of SMB_STRDUP Volker (This used to be commit 651e7e44e2e56eab81c5fe708f33e6d3918a39f9) --- source3/lib/gencache.c | 74 +++++--------------------------------------------- 1 file changed, 7 insertions(+), 67 deletions(-) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index fe038011d8..a9900fd4d8 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -56,13 +56,13 @@ BOOL gencache_init(void) if (cache) return True; asprintf(&cache_fname, "%s/%s", lp_lockdir(), "gencache.tdb"); - if (cache_fname) - DEBUG(5, ("Opening cache file at %s\n", cache_fname)); - else { + if (cache_fname == NULL) { DEBUG(0, ("Filename allocation failed.\n")); return False; } + DEBUG(5, ("Opening cache file at %s\n", cache_fname)); + cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644); @@ -121,9 +121,9 @@ BOOL gencache_set(const char *keystr, const char *value, time_t timeout) if (!valstr) return False; - keybuf.dptr = SMB_STRDUP(keystr); + keybuf.dptr = CONST_DISCARD(char *, keystr); keybuf.dsize = strlen(keystr)+1; - databuf.dptr = SMB_STRDUP(valstr); + databuf.dptr = valstr; databuf.dsize = strlen(valstr)+1; DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout =" " %s (%d seconds %s)\n", keybuf.dptr, value,ctime(&timeout), @@ -132,67 +132,9 @@ BOOL gencache_set(const char *keystr, const char *value, time_t timeout) ret = tdb_store(cache, keybuf, databuf, 0); SAFE_FREE(valstr); - SAFE_FREE(keybuf.dptr); - SAFE_FREE(databuf.dptr); - - return ret == 0; -} - - -/** - * Set existing entry to the cache file. - * - * @param keystr string that represents a key of this entry - * @param valstr text representation value being cached - * @param timeout time when the value is expired - * - * @retval true when entry is successfuly set - * @retval false on failure - **/ - -BOOL gencache_set_only(const char *keystr, const char *valstr, time_t timeout) -{ - int ret = -1; - TDB_DATA keybuf, databuf; - char *old_valstr, *datastr; - time_t old_timeout; - - /* fail completely if get null pointers passed */ - SMB_ASSERT(keystr && valstr); - - if (!gencache_init()) return False; - - /* - * Check whether entry exists in the cache - * Don't verify gencache_get exit code, since the entry may be expired - */ - gencache_get(keystr, &old_valstr, &old_timeout); - - if (!(old_valstr && old_timeout)) return False; - - DEBUG(10, ("Setting cache entry with key = %s; old value = %s and old timeout \ - = %s\n", keystr, old_valstr, ctime(&old_timeout))); - - asprintf(&datastr, CACHE_DATA_FMT, (int)timeout, valstr); - keybuf.dptr = SMB_STRDUP(keystr); - keybuf.dsize = strlen(keystr)+1; - databuf.dptr = SMB_STRDUP(datastr); - databuf.dsize = strlen(datastr)+1; - DEBUGADD(10, ("New value = %s, new timeout = %s (%d seconds %s)", valstr, - ctime(&timeout), (int)(timeout - time(NULL)), - timeout > time(NULL) ? "ahead" : "in the past")); - - - ret = tdb_store(cache, keybuf, databuf, TDB_REPLACE); - - SAFE_FREE(datastr); - SAFE_FREE(old_valstr); - SAFE_FREE(keybuf.dptr); - SAFE_FREE(databuf.dptr); return ret == 0; } - /** * Delete one entry from the cache file. @@ -213,12 +155,11 @@ BOOL gencache_del(const char *keystr) if (!gencache_init()) return False; - keybuf.dptr = SMB_STRDUP(keystr); + keybuf.dptr = CONST_DISCARD(char *, keystr); keybuf.dsize = strlen(keystr)+1; DEBUG(10, ("Deleting cache entry (key = %s)\n", keystr)); ret = tdb_delete(cache, keybuf); - SAFE_FREE(keybuf.dptr); return ret == 0; } @@ -247,10 +188,9 @@ BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout) return False; } - keybuf.dptr = SMB_STRDUP(keystr); + keybuf.dptr = CONST_DISCARD(char *, keystr); keybuf.dsize = strlen(keystr)+1; databuf = tdb_fetch(cache, keybuf); - SAFE_FREE(keybuf.dptr); if (databuf.dptr && databuf.dsize > TIMEOUT_LEN) { char* entry_buf = SMB_STRNDUP(databuf.dptr, databuf.dsize); -- cgit From cfefd8fd789b6512cad418a791c274faf54f11da Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Sat, 9 Sep 2006 21:31:56 +0000 Subject: r18311: Simplify gencache_get by using strtol instead of sscanf (This used to be commit f6497adac674f9e5089a2e54ead07596e568a936) --- source3/lib/gencache.c | 78 ++++++++++++++++++-------------------------------- 1 file changed, 28 insertions(+), 50 deletions(-) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index a9900fd4d8..871d1d1d80 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -180,6 +180,8 @@ BOOL gencache_del(const char *keystr) BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout) { TDB_DATA keybuf, databuf; + time_t t; + char *endptr; /* fail completely if get null pointers passed */ SMB_ASSERT(keystr); @@ -191,67 +193,43 @@ BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout) keybuf.dptr = CONST_DISCARD(char *, keystr); keybuf.dsize = strlen(keystr)+1; databuf = tdb_fetch(cache, keybuf); - - if (databuf.dptr && databuf.dsize > TIMEOUT_LEN) { - char* entry_buf = SMB_STRNDUP(databuf.dptr, databuf.dsize); - char *v; - time_t t; - unsigned u; - int status; - char *fmt; - - v = (char *)SMB_MALLOC(databuf.dsize + 1 - TIMEOUT_LEN); - if (!v) { - return False; - } - SAFE_FREE(databuf.dptr); - - asprintf(&fmt, READ_CACHE_DATA_FMT_TEMPLATE, (unsigned int)databuf.dsize - TIMEOUT_LEN); - if (!fmt) { - SAFE_FREE(v); - return False; - } - - status = sscanf(entry_buf, fmt, &u, v); - SAFE_FREE(fmt); + if (databuf.dptr == NULL) { + DEBUG(10, ("Cache entry with key = %s couldn't be found\n", + keystr)); + return False; + } - if ( status != 2 ) { - DEBUG(0, ("gencache_get: Invalid return %d from sscanf\n", status )); - } - t = u; - SAFE_FREE(entry_buf); + t = strtol(databuf.dptr, &endptr, 10); - DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, " - "timeout = %s", t > time(NULL) ? "valid" : - "expired", keystr, v, ctime(&t))); + if ((endptr == NULL) || (*endptr != '/')) { + DEBUG(2, ("Invalid gencache data format: %s\n", databuf.dptr)); + SAFE_FREE(databuf.dptr); + return False; + } - if (valstr) { - *valstr = v; - } else { - SAFE_FREE(v); - } + DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, " + "timeout = %s", t > time(NULL) ? "valid" : + "expired", keystr, endptr+1, ctime(&t))); - if (timeout) { - *timeout = t; + if (valstr) { + *valstr = SMB_STRDUP(endptr+1); + if (*valstr == NULL) { + SAFE_FREE(databuf.dptr); + DEBUG(0, ("strdup failed\n")); + return False; } - - return t > time(NULL); - - } - + } + SAFE_FREE(databuf.dptr); - if (valstr) { - *valstr = NULL; - } if (timeout) { - timeout = NULL; + *timeout = t; } - DEBUG(10, ("Cache entry with key = %s couldn't be found\n", keystr)); - return False; -} + return t > time(NULL); +} + /** * Iterate through all entries which key matches to specified pattern -- cgit From eab60e2bb13e0197f3771ab6d60da48a72fb311d Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Sat, 9 Sep 2006 21:40:47 +0000 Subject: r18312: Change gencache_get slightly: Delete expired keys, and only strdup the value if a valid entry was found. The newer calls got the latter one wrong, change the older calls. Volker (This used to be commit 554e68887bc84510690226c9b07a872e7a282abe) --- source3/lib/gencache.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index 871d1d1d80..dc5f32d66e 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -212,6 +212,15 @@ BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout) "timeout = %s", t > time(NULL) ? "valid" : "expired", keystr, endptr+1, ctime(&t))); + if (t <= time(NULL)) { + + /* We're expired, delete the entry */ + tdb_delete(cache, keybuf); + + SAFE_FREE(databuf.dptr); + return False; + } + if (valstr) { *valstr = SMB_STRDUP(endptr+1); if (*valstr == NULL) { @@ -227,7 +236,7 @@ BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout) *timeout = t; } - return t > time(NULL); + return True; } -- cgit From b96aae779bdbd96677aef58d205282605046a8a6 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 2 Oct 2006 23:34:03 +0000 Subject: r19054: Callers of gencache may not have the rights to open read/write. Allow them to fallback to read-only. Jeremy (This used to be commit ec526e1b882e3ade23f90c5e3d637c72b6839da5) --- source3/lib/gencache.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index dc5f32d66e..95ec47b697 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -31,6 +31,7 @@ #define READ_CACHE_DATA_FMT_TEMPLATE "%%12u/%%%us" static TDB_CONTEXT *cache; +static BOOL cache_readonly; /** * @file gencache.c @@ -66,6 +67,14 @@ BOOL gencache_init(void) cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644); + if (!cache && (errno == EACCES)) { + cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT, O_RDONLY, 0644); + if (cache) { + cache_readonly = True; + DEBUG(5, ("gencache_init: Opening cache file %s read-only.\n", cache_fname)); + } + } + SAFE_FREE(cache_fname); if (!cache) { DEBUG(5, ("Attempt to open gencache.tdb has failed.\n")); @@ -90,6 +99,7 @@ BOOL gencache_shutdown(void) DEBUG(5, ("Closing cache file\n")); ret = tdb_close(cache); cache = NULL; + cache_readonly = False; return ret != -1; } @@ -117,6 +127,10 @@ BOOL gencache_set(const char *keystr, const char *value, time_t timeout) if (!gencache_init()) return False; + if (cache_readonly) { + return False; + } + asprintf(&valstr, CACHE_DATA_FMT, (int)timeout, value); if (!valstr) return False; @@ -155,6 +169,10 @@ BOOL gencache_del(const char *keystr) if (!gencache_init()) return False; + if (cache_readonly) { + return False; + } + keybuf.dptr = CONST_DISCARD(char *, keystr); keybuf.dsize = strlen(keystr)+1; DEBUG(10, ("Deleting cache entry (key = %s)\n", keystr)); -- cgit From 9034063617d42ecc1cd32249cd6e1fa46921c51a Mon Sep 17 00:00:00 2001 From: Günther Deschner Date: Thu, 19 Oct 2006 15:43:25 +0000 Subject: r19414: gencache is getting really important now, make sure that lp_lockdir always exists so that the gencache.tdb can get created there. Guenther (This used to be commit e5ed286125d1f4b8b28bf143f987102071fd0de2) --- source3/lib/gencache.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index 95ec47b697..1448d5a638 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -56,7 +56,7 @@ BOOL gencache_init(void) /* skip file open if it's already opened */ if (cache) return True; - asprintf(&cache_fname, "%s/%s", lp_lockdir(), "gencache.tdb"); + asprintf(&cache_fname, "%s/%s", lock_path("gencache.tdb")); if (cache_fname == NULL) { DEBUG(0, ("Filename allocation failed.\n")); return False; -- cgit From 1e86361080ed7d7b4f74dd962ea0c060f1d4e6f9 Mon Sep 17 00:00:00 2001 From: Günther Deschner Date: Thu, 19 Oct 2006 15:47:19 +0000 Subject: r19415: oh la la, always compile before commit, I'm very sorry. Guenther (This used to be commit bdd2e0361ce53a4f10fca767f734991797e7f927) --- source3/lib/gencache.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index 1448d5a638..c58642553c 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -56,11 +56,7 @@ BOOL gencache_init(void) /* skip file open if it's already opened */ if (cache) return True; - asprintf(&cache_fname, "%s/%s", lock_path("gencache.tdb")); - if (cache_fname == NULL) { - DEBUG(0, ("Filename allocation failed.\n")); - return False; - } + cache_fname = lock_path("gencache.tdb"); DEBUG(5, ("Opening cache file at %s\n", cache_fname)); @@ -75,7 +71,6 @@ BOOL gencache_init(void) } } - SAFE_FREE(cache_fname); if (!cache) { DEBUG(5, ("Attempt to open gencache.tdb has failed.\n")); return False; -- cgit From 8960af95585cca9312e6df2f9214fcad8cff73c7 Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Tue, 27 Mar 2007 09:59:32 +0000 Subject: r21976: make use of tdb_*_bystring() and string_term_tdb_data() in lib/ to avoid creating the TDB_DATA struct from strings "by hand" metze (This used to be commit c22b86595a502eb48c9d0038faee8a9ee41b8438) --- source3/lib/gencache.c | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index c58642553c..d6f5584c12 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -114,7 +114,7 @@ BOOL gencache_shutdown(void) BOOL gencache_set(const char *keystr, const char *value, time_t timeout) { int ret; - TDB_DATA keybuf, databuf; + TDB_DATA databuf; char* valstr = NULL; /* fail completely if get null pointers passed */ @@ -130,16 +130,13 @@ BOOL gencache_set(const char *keystr, const char *value, time_t timeout) if (!valstr) return False; - keybuf.dptr = CONST_DISCARD(char *, keystr); - keybuf.dsize = strlen(keystr)+1; - databuf.dptr = valstr; - databuf.dsize = strlen(valstr)+1; + databuf = string_term_tdb_data(valstr); DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout =" - " %s (%d seconds %s)\n", keybuf.dptr, value,ctime(&timeout), + " %s (%d seconds %s)\n", keystr, value,ctime(&timeout), (int)(timeout - time(NULL)), timeout > time(NULL) ? "ahead" : "in the past")); - ret = tdb_store(cache, keybuf, databuf, 0); + ret = tdb_store_bystring(cache, keystr, databuf, 0); SAFE_FREE(valstr); return ret == 0; @@ -157,7 +154,6 @@ BOOL gencache_set(const char *keystr, const char *value, time_t timeout) BOOL gencache_del(const char *keystr) { int ret; - TDB_DATA keybuf; /* fail completely if get null pointers passed */ SMB_ASSERT(keystr); @@ -168,10 +164,8 @@ BOOL gencache_del(const char *keystr) return False; } - keybuf.dptr = CONST_DISCARD(char *, keystr); - keybuf.dsize = strlen(keystr)+1; DEBUG(10, ("Deleting cache entry (key = %s)\n", keystr)); - ret = tdb_delete(cache, keybuf); + ret = tdb_delete_bystring(cache, keystr); return ret == 0; } @@ -192,7 +186,7 @@ BOOL gencache_del(const char *keystr) BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout) { - TDB_DATA keybuf, databuf; + TDB_DATA databuf; time_t t; char *endptr; @@ -202,10 +196,8 @@ BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout) if (!gencache_init()) { return False; } - - keybuf.dptr = CONST_DISCARD(char *, keystr); - keybuf.dsize = strlen(keystr)+1; - databuf = tdb_fetch(cache, keybuf); + + databuf = tdb_fetch_bystring(cache, keystr); if (databuf.dptr == NULL) { DEBUG(10, ("Cache entry with key = %s couldn't be found\n", @@ -228,7 +220,7 @@ BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout) if (t <= time(NULL)) { /* We're expired, delete the entry */ - tdb_delete(cache, keybuf); + tdb_delete_bystring(cache, keystr); SAFE_FREE(databuf.dptr); return False; -- cgit From bc2b6436d0f5f3e9ffdfaeb7f1b32996a83d5478 Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Thu, 29 Mar 2007 09:35:51 +0000 Subject: r22009: change TDB_DATA from char * to unsigned char * and fix all compiler warnings in the users metze (This used to be commit 3a28443079c141a6ce8182c65b56ca210e34f37f) --- source3/lib/gencache.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index d6f5584c12..af60ee1ff4 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -205,7 +205,7 @@ BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout) return False; } - t = strtol(databuf.dptr, &endptr, 10); + t = strtol((const char *)databuf.dptr, &endptr, 10); if ((endptr == NULL) || (*endptr != '/')) { DEBUG(2, ("Invalid gencache data format: %s\n", databuf.dptr)); @@ -279,7 +279,7 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time char *fmt; /* ensure null termination of the key string */ - keystr = SMB_STRNDUP(node->node_key.dptr, node->node_key.dsize); + keystr = SMB_STRNDUP((const char *)node->node_key.dptr, node->node_key.dsize); if (!keystr) { break; } @@ -295,7 +295,7 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time node = node->next; continue; } - entry = SMB_STRNDUP(databuf.dptr, databuf.dsize); + entry = SMB_STRNDUP((const char *)databuf.dptr, databuf.dsize); if (!entry) { SAFE_FREE(databuf.dptr); SAFE_FREE(keystr); -- cgit From d824b98f80ba186030cbb70b3a1e5daf80469ecd Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 9 Jul 2007 19:25:36 +0000 Subject: r23779: Change from v2 or later to v3 or later. Jeremy. (This used to be commit 407e6e695b8366369b7c76af1ff76869b45347b3) --- source3/lib/gencache.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index af60ee1ff4..5bb29e096b 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -8,7 +8,7 @@ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or + the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, -- cgit From 5e54558c6dea67b56bbfaba5698f3a434d3dffb6 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Tue, 10 Jul 2007 00:52:41 +0000 Subject: r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text (This used to be commit b0132e94fc5fef936aa766fb99a306b3628e9f07) --- source3/lib/gencache.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index 5bb29e096b..c58546da9c 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -17,8 +17,7 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program. If not, see . */ #include "includes.h" -- cgit From 2af963792566797ac9edcfb528198b82a2518a18 Mon Sep 17 00:00:00 2001 From: Günther Deschner Date: Tue, 28 Aug 2007 12:40:01 +0000 Subject: r24733: Add support for storing DATA_BLOBs in gencache.tdb (including torturetest). Mimir, please have a look. DATA_BLOBs will now just show up as "DATA_BLOB" values with "net cache list". Guenther (This used to be commit b8ad546d041a2a8cc85c7db8eba4d2d3b97df1a8) --- source3/lib/gencache.c | 157 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index c58546da9c..1ee720cdfd 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -28,6 +28,8 @@ #define TIMEOUT_LEN 12 #define CACHE_DATA_FMT "%12u/%s" #define READ_CACHE_DATA_FMT_TEMPLATE "%%12u/%%%us" +#define BLOB_TYPE "DATA_BLOB" +#define BLOB_TYPE_LEN 9 static TDB_CONTEXT *cache; static BOOL cache_readonly; @@ -243,6 +245,161 @@ BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout) return True; } +/** + * Get existing entry from the cache file. + * + * @param keystr string that represents a key of this entry + * @param blob DATA_BLOB that is filled with entry's blob + * @param expired pointer to a BOOL that indicates whether the entry is expired + * + * @retval true when entry is successfuly fetched + * @retval False for failure + **/ + +BOOL gencache_get_data_blob(const char *keystr, DATA_BLOB *blob, BOOL *expired) +{ + TDB_DATA databuf; + time_t t; + char *blob_type; + unsigned char *buf = NULL; + BOOL ret = False; + fstring valstr; + int buflen = 0, len = 0, blob_len = 0; + unsigned char *blob_buf = NULL; + + /* fail completely if get null pointers passed */ + SMB_ASSERT(keystr); + + if (!gencache_init()) { + return False; + } + + databuf = tdb_fetch_bystring(cache, keystr); + if (!databuf.dptr) { + DEBUG(10,("Cache entry with key = %s couldn't be found\n", + keystr)); + return False; + } + + buf = (unsigned char *)databuf.dptr; + buflen = databuf.dsize; + + len += tdb_unpack(buf+len, buflen-len, "fB", + &valstr, + &blob_len, &blob_buf); + if (len == -1) { + goto out; + } + + t = strtol(valstr, &blob_type, 10); + + if (strcmp(blob_type+1, BLOB_TYPE) != 0) { + goto out; + } + + DEBUG(10,("Returning %s cache entry: key = %s, " + "timeout = %s", t > time(NULL) ? "valid" : + "expired", keystr, ctime(&t))); + + if (t <= time(NULL)) { + /* We're expired */ + if (expired) { + *expired = True; + } + } + + if (blob) { + *blob = data_blob(blob_buf, blob_len); + if (!blob->data) { + goto out; + } + } + + ret = True; + out: + SAFE_FREE(blob_buf); + SAFE_FREE(databuf.dptr); + + return ret; +} + +/** + * Set an entry in the cache file. If there's no such + * one, then add it. + * + * @param keystr string that represents a key of this entry + * @param blob DATA_BLOB value being cached + * @param timeout time when the value is expired + * + * @retval true when entry is successfuly stored + * @retval false on failure + **/ + +BOOL gencache_set_data_blob(const char *keystr, DATA_BLOB *blob, time_t timeout) +{ + BOOL ret = False; + int tdb_ret; + TDB_DATA databuf; + char *valstr = NULL; + unsigned char *buf = NULL; + int len = 0, buflen = 0; + + /* fail completely if get null pointers passed */ + SMB_ASSERT(keystr && blob); + + if (!gencache_init()) { + return False; + } + + if (cache_readonly) { + return False; + } + + asprintf(&valstr, "%12u/%s", (int)timeout, BLOB_TYPE); + if (!valstr) { + return False; + } + + again: + len = 0; + + len += tdb_pack(buf+len, buflen-len, "fB", + valstr, + blob->length, blob->data); + + if (len == -1) { + goto out; + } + + if (buflen < len) { + SAFE_FREE(buf); + buf = SMB_MALLOC_ARRAY(unsigned char, len); + if (!buf) { + goto out; + } + buflen = len; + goto again; + } + + databuf = make_tdb_data(buf, len); + + DEBUG(10,("Adding cache entry with key = %s; " + "blob size = %d and timeout = %s" + "(%d seconds %s)\n", keystr, (int)databuf.dsize, + ctime(&timeout), (int)(timeout - time(NULL)), + timeout > time(NULL) ? "ahead" : "in the past")); + + tdb_ret = tdb_store_bystring(cache, keystr, databuf, 0); + if (tdb_ret == 0) { + ret = True; + } + + out: + SAFE_FREE(valstr); + SAFE_FREE(buf); + + return ret; +} /** * Iterate through all entries which key matches to specified pattern -- cgit From 30191d1a5704ad2b158386b511558972d539ce47 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 18 Oct 2007 17:40:25 -0700 Subject: RIP BOOL. Convert BOOL -> bool. I found a few interesting bugs in various places whilst doing this (places that assumed BOOL == int). I also need to fix the Samba4 pidl generation (next checkin). Jeremy. (This used to be commit f35a266b3cbb3e5fa6a86be60f34fe340a3ca71f) --- source3/lib/gencache.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index 1ee720cdfd..a50e5d01fa 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -32,7 +32,7 @@ #define BLOB_TYPE_LEN 9 static TDB_CONTEXT *cache; -static BOOL cache_readonly; +static bool cache_readonly; /** * @file gencache.c @@ -50,7 +50,7 @@ static BOOL cache_readonly; * false on failure **/ -BOOL gencache_init(void) +bool gencache_init(void) { char* cache_fname = NULL; @@ -87,7 +87,7 @@ BOOL gencache_init(void) * false on failure during cache shutdown **/ -BOOL gencache_shutdown(void) +bool gencache_shutdown(void) { int ret; /* tdb_close routine returns -1 on error */ @@ -112,7 +112,7 @@ BOOL gencache_shutdown(void) * @retval false on failure **/ -BOOL gencache_set(const char *keystr, const char *value, time_t timeout) +bool gencache_set(const char *keystr, const char *value, time_t timeout) { int ret; TDB_DATA databuf; @@ -152,7 +152,7 @@ BOOL gencache_set(const char *keystr, const char *value, time_t timeout) * @retval false in case of failure **/ -BOOL gencache_del(const char *keystr) +bool gencache_del(const char *keystr) { int ret; @@ -185,7 +185,7 @@ BOOL gencache_del(const char *keystr) * @retval False for failure **/ -BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout) +bool gencache_get(const char *keystr, char **valstr, time_t *timeout) { TDB_DATA databuf; time_t t; @@ -250,19 +250,19 @@ BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout) * * @param keystr string that represents a key of this entry * @param blob DATA_BLOB that is filled with entry's blob - * @param expired pointer to a BOOL that indicates whether the entry is expired + * @param expired pointer to a bool that indicates whether the entry is expired * * @retval true when entry is successfuly fetched * @retval False for failure **/ -BOOL gencache_get_data_blob(const char *keystr, DATA_BLOB *blob, BOOL *expired) +bool gencache_get_data_blob(const char *keystr, DATA_BLOB *blob, bool *expired) { TDB_DATA databuf; time_t t; char *blob_type; unsigned char *buf = NULL; - BOOL ret = False; + bool ret = False; fstring valstr; int buflen = 0, len = 0, blob_len = 0; unsigned char *blob_buf = NULL; @@ -335,9 +335,9 @@ BOOL gencache_get_data_blob(const char *keystr, DATA_BLOB *blob, BOOL *expired) * @retval false on failure **/ -BOOL gencache_set_data_blob(const char *keystr, DATA_BLOB *blob, time_t timeout) +bool gencache_set_data_blob(const char *keystr, DATA_BLOB *blob, time_t timeout) { - BOOL ret = False; + bool ret = False; int tdb_ret; TDB_DATA databuf; char *valstr = NULL; -- cgit From 3ba59d461665a16cf87b991a8135821208457f67 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Fri, 28 Dec 2007 17:09:57 +0100 Subject: don't store cache_readonly in gencache tdb won't allow us to write anyway (This used to be commit 069cd6d63a61065be7926230235e198c456d38ae) --- source3/lib/gencache.c | 15 --------------- 1 file changed, 15 deletions(-) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index a50e5d01fa..663385cfe3 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -32,7 +32,6 @@ #define BLOB_TYPE_LEN 9 static TDB_CONTEXT *cache; -static bool cache_readonly; /** * @file gencache.c @@ -67,7 +66,6 @@ bool gencache_init(void) if (!cache && (errno == EACCES)) { cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT, O_RDONLY, 0644); if (cache) { - cache_readonly = True; DEBUG(5, ("gencache_init: Opening cache file %s read-only.\n", cache_fname)); } } @@ -95,7 +93,6 @@ bool gencache_shutdown(void) DEBUG(5, ("Closing cache file\n")); ret = tdb_close(cache); cache = NULL; - cache_readonly = False; return ret != -1; } @@ -123,10 +120,6 @@ bool gencache_set(const char *keystr, const char *value, time_t timeout) if (!gencache_init()) return False; - if (cache_readonly) { - return False; - } - asprintf(&valstr, CACHE_DATA_FMT, (int)timeout, value); if (!valstr) return False; @@ -161,10 +154,6 @@ bool gencache_del(const char *keystr) if (!gencache_init()) return False; - if (cache_readonly) { - return False; - } - DEBUG(10, ("Deleting cache entry (key = %s)\n", keystr)); ret = tdb_delete_bystring(cache, keystr); @@ -351,10 +340,6 @@ bool gencache_set_data_blob(const char *keystr, DATA_BLOB *blob, time_t timeout) return False; } - if (cache_readonly) { - return False; - } - asprintf(&valstr, "%12u/%s", (int)timeout, BLOB_TYPE); if (!valstr) { return False; -- cgit From 317639287886181edf08ccecad1b324e4cc55d0b Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Mon, 25 Feb 2008 15:24:49 +0100 Subject: Fix some warnings warning: ignoring return value of 'asprintf', declared with attribute warn_unused_result (This used to be commit ad37b7b0aee265a3e4d8b7552610f4b9a105434d) --- source3/lib/gencache.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index 663385cfe3..6131269adb 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -120,9 +120,9 @@ bool gencache_set(const char *keystr, const char *value, time_t timeout) if (!gencache_init()) return False; - asprintf(&valstr, CACHE_DATA_FMT, (int)timeout, value); - if (!valstr) + if (asprintf(&valstr, CACHE_DATA_FMT, (int)timeout, value) == -1) { return False; + } databuf = string_term_tdb_data(valstr); DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout =" @@ -340,8 +340,7 @@ bool gencache_set_data_blob(const char *keystr, DATA_BLOB *blob, time_t timeout) return False; } - asprintf(&valstr, "%12u/%s", (int)timeout, BLOB_TYPE); - if (!valstr) { + if (asprintf(&valstr, "%12u/%s", (int)timeout, BLOB_TYPE) == -1) { return False; } @@ -452,8 +451,9 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time break; } - asprintf(&fmt, READ_CACHE_DATA_FMT_TEMPLATE, (unsigned int)databuf.dsize - TIMEOUT_LEN); - if (!fmt) { + if (asprintf(&fmt, READ_CACHE_DATA_FMT_TEMPLATE, + (unsigned int)databuf.dsize - TIMEOUT_LEN) + == -1) { SAFE_FREE(valstr); SAFE_FREE(entry); SAFE_FREE(keystr); -- cgit From 077f24e51e26ea2e6ebafc257d9387f301ad9e44 Mon Sep 17 00:00:00 2001 From: Günther Deschner Date: Wed, 7 May 2008 21:01:46 +0200 Subject: gencache: add some const. Guenther (This used to be commit ec9f8c4cf67c82f4665ed51e4fd0181f5f147ea0) --- source3/lib/gencache.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index 6131269adb..b773f83c58 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -324,7 +324,7 @@ bool gencache_get_data_blob(const char *keystr, DATA_BLOB *blob, bool *expired) * @retval false on failure **/ -bool gencache_set_data_blob(const char *keystr, DATA_BLOB *blob, time_t timeout) +bool gencache_set_data_blob(const char *keystr, const DATA_BLOB *blob, time_t timeout) { bool ret = False; int tdb_ret; -- cgit From c4503f5658282169f38ac87fd2d82a9b67273037 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Thu, 3 Jul 2008 15:58:37 +0200 Subject: Return timed out entries from gencache_get if timeout param != NULL net cache get was the only one interested in the timeout. That single caller can take care of the timeout itself then. With this API change idmap_cache.c can be converted to gencache. (This used to be commit 2954b2be563149380e1fae7fe088b98d6cbd42e7) --- source3/lib/gencache.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index b773f83c58..1b4342a62b 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -166,15 +166,16 @@ bool gencache_del(const char *keystr) * * @param keystr string that represents a key of this entry * @param valstr buffer that is allocated and filled with the entry value - * buffer's disposing must be done outside - * @param timeout pointer to a time_t that is filled with entry's - * timeout + * buffer's disposing must be done outside + * @param timeout If == NULL, the caller is not interested in timed out + * entries. If != NULL, return the timeout timestamp, the + * caller must figure out itself if this entry is timed out. * * @retval true when entry is successfuly fetched * @retval False for failure **/ -bool gencache_get(const char *keystr, char **valstr, time_t *timeout) +bool gencache_get(const char *keystr, char **valstr, time_t *ptimeout) { TDB_DATA databuf; time_t t; @@ -207,9 +208,13 @@ bool gencache_get(const char *keystr, char **valstr, time_t *timeout) "timeout = %s", t > time(NULL) ? "valid" : "expired", keystr, endptr+1, ctime(&t))); - if (t <= time(NULL)) { + if ((t <= time(NULL)) && (ptimeout == NULL)) { + + /* + * The entry is expired, and the caller isn't interested in + * timed out ones. Delete it. + */ - /* We're expired, delete the entry */ tdb_delete_bystring(cache, keystr); SAFE_FREE(databuf.dptr); @@ -224,15 +229,15 @@ bool gencache_get(const char *keystr, char **valstr, time_t *timeout) return False; } } - + SAFE_FREE(databuf.dptr); - if (timeout) { - *timeout = t; + if (ptimeout) { + *ptimeout = t; } return True; -} +} /** * Get existing entry from the cache file. -- cgit From 352b5c18579c9610c15e5c24e012e60cd972b95a Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Thu, 3 Jul 2008 16:24:27 +0200 Subject: Remove gencache_[un]lock_entry MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Günther agreed that it might be unnecessary in dsgetdcname_cache_store() :-) (This used to be commit 7a5a575ffe5196caecedc93970a25abfbe6f8059) --- source3/lib/gencache.c | 25 ------------------------- 1 file changed, 25 deletions(-) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index 1b4342a62b..a55b2ab9ba 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -484,28 +484,3 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time tdb_search_list_free(first_node); } - -/******************************************************************** - lock a key -********************************************************************/ - -int gencache_lock_entry( const char *key ) -{ - if (!gencache_init()) - return -1; - - return tdb_lock_bystring(cache, key); -} - -/******************************************************************** - unlock a key -********************************************************************/ - -void gencache_unlock_entry( const char *key ) -{ - if (!gencache_init()) - return; - - tdb_unlock_bystring(cache, key); - return; -} -- cgit From d670d0a09bec3b6900421df17fc9d959545ee953 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Fri, 11 Jul 2008 17:44:09 +0200 Subject: Revert "Remove gencache_[un]lock_entry" This reverts commit 7a5a575ffe5196caecedc93970a25abfbe6f8059. (This used to be commit 62e444dd50ae974c2ab9a553cdf7f188a8f2c538) --- source3/lib/gencache.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index a55b2ab9ba..1b4342a62b 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -484,3 +484,28 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time tdb_search_list_free(first_node); } + +/******************************************************************** + lock a key +********************************************************************/ + +int gencache_lock_entry( const char *key ) +{ + if (!gencache_init()) + return -1; + + return tdb_lock_bystring(cache, key); +} + +/******************************************************************** + unlock a key +********************************************************************/ + +void gencache_unlock_entry( const char *key ) +{ + if (!gencache_init()) + return; + + tdb_unlock_bystring(cache, key); + return; +} -- cgit From cc78ea5d09f2d8e338f0626fb1215f06ee7e1bbe Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Fri, 11 Jul 2008 17:44:15 +0200 Subject: Revert "Return timed out entries from gencache_get if timeout param != NULL" This reverts commit 2954b2be563149380e1fae7fe088b98d6cbd42e7. (This used to be commit 77ab2fb306a7ad59447a3e1591c2af03447e09c5) --- source3/lib/gencache.c | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) (limited to 'source3/lib/gencache.c') diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c index 1b4342a62b..b773f83c58 100644 --- a/source3/lib/gencache.c +++ b/source3/lib/gencache.c @@ -166,16 +166,15 @@ bool gencache_del(const char *keystr) * * @param keystr string that represents a key of this entry * @param valstr buffer that is allocated and filled with the entry value - * buffer's disposing must be done outside - * @param timeout If == NULL, the caller is not interested in timed out - * entries. If != NULL, return the timeout timestamp, the - * caller must figure out itself if this entry is timed out. + * buffer's disposing must be done outside + * @param timeout pointer to a time_t that is filled with entry's + * timeout * * @retval true when entry is successfuly fetched * @retval False for failure **/ -bool gencache_get(const char *keystr, char **valstr, time_t *ptimeout) +bool gencache_get(const char *keystr, char **valstr, time_t *timeout) { TDB_DATA databuf; time_t t; @@ -208,13 +207,9 @@ bool gencache_get(const char *keystr, char **valstr, time_t *ptimeout) "timeout = %s", t > time(NULL) ? "valid" : "expired", keystr, endptr+1, ctime(&t))); - if ((t <= time(NULL)) && (ptimeout == NULL)) { - - /* - * The entry is expired, and the caller isn't interested in - * timed out ones. Delete it. - */ + if (t <= time(NULL)) { + /* We're expired, delete the entry */ tdb_delete_bystring(cache, keystr); SAFE_FREE(databuf.dptr); @@ -229,15 +224,15 @@ bool gencache_get(const char *keystr, char **valstr, time_t *ptimeout) return False; } } - + SAFE_FREE(databuf.dptr); - if (ptimeout) { - *ptimeout = t; + if (timeout) { + *timeout = t; } return True; -} +} /** * Get existing entry from the cache file. -- cgit