From f0e246e271f84c6fe960a3c36703a56d1067431c Mon Sep 17 00:00:00 2001 From: Brian Wellington Date: Fri, 12 Jan 2001 22:22:17 +0000 Subject: [PATCH] Fully implement the cachefile option, which allows persistent caching. This removes some unused code in view.c and uncomments some code in cache.c. This still isn't really usable, since the trust level of cached data is not persistent, so all data in the persistent cache will be promoted to "ultimate" trust on reload. --- bin/named/server.c | 43 +++++++++++------------------ lib/dns/cache.c | 56 +++++++++++++++++++++++++++++--------- lib/dns/include/dns/view.h | 19 +------------ lib/dns/view.c | 18 ++---------- 4 files changed, 62 insertions(+), 74 deletions(-) diff --git a/bin/named/server.c b/bin/named/server.c index 007dacaae0..07826fd043 100644 --- a/bin/named/server.c +++ b/bin/named/server.c @@ -15,7 +15,7 @@ * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -/* $Id: server.c,v 1.280 2001/01/12 00:37:11 bwelling Exp $ */ +/* $Id: server.c,v 1.281 2001/01/12 22:22:14 bwelling Exp $ */ #include @@ -417,6 +417,8 @@ configure_view(dns_view_t *view, dns_c_ctx_t *cctx, dns_c_view_t *cview, dns_dispatch_t *dispatch4 = NULL; dns_dispatch_t *dispatch6 = NULL; in_port_t port; + isc_boolean_t reused_cache = ISC_FALSE; + char *cachefile = NULL; REQUIRE(DNS_VIEW_VALID(view)); @@ -460,6 +462,7 @@ configure_view(dns_view_t *view, dns_c_ctx_t *cctx, dns_c_view_t *cview, isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER, ISC_LOG_DEBUG(3), "reusing existing cache"); + reused_cache = ISC_TRUE; dns_cache_attach(pview->cache, &cache); dns_view_detach(&pview); } else { @@ -469,6 +472,18 @@ configure_view(dns_view_t *view, dns_c_ctx_t *cctx, dns_c_view_t *cview, } dns_view_setcache(view, cache); + if (cview != NULL) + result = dns_c_view_getcachefile(cview, &cachefile); + else + result = dns_c_ctx_getcachefile(cctx, &cachefile); + if (result != ISC_R_SUCCESS && result != ISC_R_NOTFOUND) + goto cleanup; + if (cachefile != NULL) { + dns_cache_setfilename(cache, cachefile); + if (!reused_cache) + CHECK(dns_cache_load(cache)); + } + result = ISC_R_NOTFOUND; if (cview != NULL) result = dns_c_view_getcleaninterval(cview, @@ -706,32 +721,6 @@ configure_view(dns_view_t *view, dns_c_ctx_t *cctx, dns_c_view_t *cview, val = 7 * 24 * 3600; view->maxncachettl = val; } - { - char *cachefile = NULL, *p = NULL; - if (cview != NULL) - result = dns_c_view_getcachefile(cview, &cachefile); - else - result = dns_c_ctx_getcachefile(cctx, &cachefile); - if (result != ISC_R_SUCCESS && result != ISC_R_NOTFOUND) - goto cleanup; - if (cachefile != NULL) { - p = isc_mem_strdup(view->mctx, cachefile); - if (p == NULL) { - result = ISC_R_NOMEMORY; - goto cleanup; - } - } - if (view->cachefile != NULL) - isc_mem_free(view->mctx, view->cachefile); - view->cachefile = p; - if (view->cachefile != NULL) { - isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, - NS_LOGMODULE_SERVER, ISC_LOG_DEBUG(1), - "loading cache '%s'", view->cachefile); - /* DNS_R_SEENINCLUDE should be impossible here. */ - CHECK(dns_db_load(view->cachedb, view->cachefile)); - } - } result = ISC_R_SUCCESS; diff --git a/lib/dns/cache.c b/lib/dns/cache.c index ace74625cf..8d09a0b71f 100644 --- a/lib/dns/cache.c +++ b/lib/dns/cache.c @@ -15,7 +15,7 @@ * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -/* $Id: cache.c,v 1.32 2001/01/09 21:50:40 bwelling Exp $ */ +/* $Id: cache.c,v 1.33 2001/01/12 22:22:15 bwelling Exp $ */ #include @@ -30,6 +30,7 @@ #include #include #include +#include #include #define CACHE_MAGIC 0x24242424U /* $$$$. */ @@ -151,6 +152,15 @@ dns_cache_create(isc_mem_t *mctx, isc_taskmgr_t *taskmgr, goto cleanup_mem; } + result = isc_mutex_init(&cache->filelock); + if (result != ISC_R_SUCCESS) { + UNEXPECTED_ERROR(__FILE__, __LINE__, + "isc_mutex_init() failed: %s", + dns_result_totext(result)); + result = ISC_R_UNEXPECTED; + goto cleanup_lock; + } + cache->references = 1; cache->live_tasks = 0; cache->rdclass = rdclass; @@ -160,7 +170,7 @@ dns_cache_create(isc_mem_t *mctx, isc_taskmgr_t *taskmgr, dns_dbtype_cache, rdclass, db_argc, db_argv, &cache->db); if (result != ISC_R_SUCCESS) - goto cleanup_mutex; + goto cleanup_filelock; cache->filename = NULL; @@ -176,7 +186,9 @@ dns_cache_create(isc_mem_t *mctx, isc_taskmgr_t *taskmgr, cleanup_db: dns_db_detach(&cache->db); - cleanup_mutex: + cleanup_filelock: + DESTROYLOCK(&cache->filelock); + cleanup_lock: DESTROYLOCK(&cache->lock); cleanup_mem: isc_mem_put(mctx, cache, sizeof *cache); @@ -214,6 +226,7 @@ cache_free(dns_cache_t *cache) { dns_db_detach(&cache->db); DESTROYLOCK(&cache->lock); + DESTROYLOCK(&cache->filelock); cache->magic = 0; mctx = cache->mctx; isc_mem_put(cache->mctx, cache, sizeof *cache); @@ -253,6 +266,12 @@ dns_cache_detach(dns_cache_t **cachep) { UNLOCK(&cache->lock); *cachep = NULL; if (free_cache) { + /* + * When the cache is shut down, dump it to a file if one is + * specified. + */ + dns_cache_dump(cache); + /* XXXRTH This is not locked! */ if (cache->live_tasks > 0) isc_task_shutdown(cache->cleaner.task); @@ -271,12 +290,14 @@ dns_cache_attachdb(dns_cache_t *cache, dns_db_t **dbp) { UNLOCK(&cache->lock); } -#ifdef NOTYET - -/* ARGSUSED */ isc_result_t -dns_cache_setfilename(dns_cache_t *cahce, char *filename) { - char *newname = isc_mem_strdup(filename); +dns_cache_setfilename(dns_cache_t *cache, char *filename) { + char *newname; + + REQUIRE(VALID_CACHE(cache)); + REQUIRE(filename != NULL); + + newname = isc_mem_strdup(cache->mctx, filename); if (newname == NULL) return (ISC_R_NOMEMORY); LOCK(&cache->filelock); @@ -290,10 +311,12 @@ dns_cache_setfilename(dns_cache_t *cahce, char *filename) { isc_result_t dns_cache_load(dns_cache_t *cache) { isc_result_t result; + + REQUIRE(VALID_CACHE(cache)); + if (cache->filename == NULL) return (ISC_R_SUCCESS); LOCK(&cache->filelock); - /* XXX handle TTLs in a way appropriate for the cache */ result = dns_db_load(cache->db, cache->filename); UNLOCK(&cache->filelock); return (result); @@ -301,11 +324,18 @@ dns_cache_load(dns_cache_t *cache) { isc_result_t dns_cache_dump(dns_cache_t *cache) { - /* XXX to be written */ - return (ISC_R_NOTIMPLEMENTED); -} + isc_result_t result; -#endif + REQUIRE(VALID_CACHE(cache)); + + LOCK(&cache->filelock); + if (cache->filename == NULL) + return (ISC_R_SUCCESS); + result = dns_master_dump(cache->mctx, cache->db, NULL, + &dns_master_style_cache, cache->filename); + UNLOCK(&cache->filelock); + return (result); +} void dns_cache_setcleaninginterval(dns_cache_t *cache, unsigned int t) { diff --git a/lib/dns/include/dns/view.h b/lib/dns/include/dns/view.h index 693d5c0b05..0fe1bdf09f 100644 --- a/lib/dns/include/dns/view.h +++ b/lib/dns/include/dns/view.h @@ -15,7 +15,7 @@ * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -/* $Id: view.h,v 1.63 2001/01/09 21:53:40 bwelling Exp $ */ +/* $Id: view.h,v 1.64 2001/01/12 22:22:17 bwelling Exp $ */ #ifndef DNS_VIEW_H #define DNS_VIEW_H 1 @@ -115,7 +115,6 @@ struct dns_view { dns_ttl_t maxcachettl; dns_ttl_t maxncachettl; in_port_t dstport; - char * cachefile; /* * Configurable data for server use only, @@ -644,21 +643,6 @@ dns_view_dialup(dns_view_t *view); * Perform dialup-time maintenance on the zones of 'view'. */ -isc_result_t -dns_view_dumpcache(dns_view_t *view); -/* - * Dump the view's cache to the the view's cache file. - * - * Requires: - * - * 'view' is valid. - * - * Returns: - * ISC_R_SUCCESS The cache was successfully dumped. - * ISC_R_IGNORE No cachefile was specified. - * others An error occurred (see dns_master_dump) - */ - isc_result_t dns_view_dumpdbtostream(dns_view_t *view, FILE *fp); /* @@ -678,7 +662,6 @@ dns_view_dumpdbtostream(dns_view_t *view, FILE *fp); * * Returns: * ISC_R_SUCCESS The cache was successfully dumped. - * ISC_R_IGNORE No cachefile was specified. * others An error occurred (see dns_master_dump) */ diff --git a/lib/dns/view.c b/lib/dns/view.c index a2f12dd109..f485b83193 100644 --- a/lib/dns/view.c +++ b/lib/dns/view.c @@ -15,7 +15,7 @@ * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -/* $Id: view.c,v 1.91 2001/01/09 21:51:44 bwelling Exp $ */ +/* $Id: view.c,v 1.92 2001/01/12 22:22:16 bwelling Exp $ */ #include @@ -164,7 +164,6 @@ dns_view_create(isc_mem_t *mctx, dns_rdataclass_t rdclass, view->maxcachettl = 7 * 24 * 3600; view->maxncachettl = 3 * 3600; view->dstport = 53; - view->cachefile = NULL; result = dns_peerlist_new(view->mctx, &view->peers); if (result != ISC_R_SUCCESS) @@ -256,8 +255,6 @@ destroy(dns_view_t *view) { dns_acl_detach(&view->v6synthesisacl); if (view->sortlist != NULL) dns_acl_detach(&view->sortlist); - if (view->cachefile != NULL) - isc_mem_free(view->mctx, view->cachefile); dns_keytable_detach(&view->trustedkeys); dns_keytable_detach(&view->secroots); dns_fwdtable_destroy(&view->fwdtable); @@ -1100,17 +1097,6 @@ dns_view_checksig(dns_view_t *view, isc_buffer_t *source, dns_message_t *msg) { view->dynamickeys)); } -isc_result_t -dns_view_dumpcache(dns_view_t *view) { - REQUIRE(DNS_VIEW_VALID(view)); - - if (view->cachefile == NULL) - return (ISC_R_IGNORE); - return (dns_master_dump(view->mctx, view->cachedb, NULL, - &dns_master_style_default, view->cachefile)); - -} - isc_result_t dns_view_dumpdbtostream(dns_view_t *view, FILE *fp) { isc_result_t result; @@ -1119,7 +1105,7 @@ dns_view_dumpdbtostream(dns_view_t *view, FILE *fp) { (void)fprintf(fp, ";\n; Cache dump of view '%s'\n;\n", view->name); result = dns_master_dumptostream(view->mctx, view->cachedb, NULL, - &dns_master_style_explicitttl, fp); + &dns_master_style_cache, fp); if (result != ISC_R_SUCCESS) return (result); #ifdef notyet /* clean up adb dump format first */