⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cache.c

📁 bind 9.3结合mysql数据库
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC") * Copyright (C) 1999-2003  Internet Software Consortium. * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR * PERFORMANCE OF THIS SOFTWARE. *//* $Id: cache.c,v 1.45.2.4.8.7 2004/03/08 02:07:52 marka Exp $ */#include <config.h>#include <isc/mem.h>#include <isc/task.h>#include <isc/time.h>#include <isc/timer.h>#include <isc/util.h>#include <dns/cache.h>#include <dns/db.h>#include <dns/dbiterator.h>#include <dns/events.h>#include <dns/log.h>#include <dns/masterdump.h>#include <dns/rdata.h>#include <dns/rdataset.h>#include <dns/rdatasetiter.h>#include <dns/result.h>#define CACHE_MAGIC		ISC_MAGIC('$', '$', '$', '$')#define VALID_CACHE(cache)	ISC_MAGIC_VALID(cache, CACHE_MAGIC)/* * The following two variables control incremental cleaning. * MINSIZE is how many bytes is the floor for dns_cache_setcachesize(). * CLEANERINCREMENT is how many nodes are examined in one pass. */#define DNS_CACHE_MINSIZE 		2097152	/* Bytes.  2097152 = 2 MB */#define DNS_CACHE_CLEANERINCREMENT	1000	/* Number of nodes. *//*** ***	Types ***//* * A cache_cleaner_t encapsulsates the state of the periodic * cache cleaning. */typedef struct cache_cleaner cache_cleaner_t;typedef enum {	cleaner_s_idle,	/* Waiting for cleaning-interval to expire. */	cleaner_s_busy,	/* Currently cleaning. */	cleaner_s_done  /* Freed enough memory after being overmem. */} cleaner_state_t;/* * Convenience macros for comprehensive assertion checking. */#define CLEANER_IDLE(c) ((c)->state == cleaner_s_idle && \			 (c)->iterator == NULL && \			 (c)->resched_event != NULL)#define CLEANER_BUSY(c) ((c)->state == cleaner_s_busy && \			 (c)->iterator != NULL && \			 (c)->resched_event == NULL)/* * Accesses to a cache cleaner object are synchronized through * task/event serialization, or locked from the cache object. */struct cache_cleaner {	isc_mutex_t	lock;	/*	 * Locks overmem_event, overmem.  Note: never allocate memory	 * while holding this lock - that could lead to deadlock since	 * the lock is take by water() which is called from the memory	 * allocator.	 */	dns_cache_t	*cache;	isc_task_t 	*task;	unsigned int	cleaning_interval; /* The cleaning-interval from					      named.conf, in seconds. */	isc_timer_t 	*cleaning_timer;	isc_event_t	*resched_event;	/* Sent by cleaner task to					   itself to reschedule */	isc_event_t	*overmem_event;	dns_dbiterator_t *iterator;	int 		 increment;	/* Number of names to					   clean in one increment */	cleaner_state_t  state;		/* Idle/Busy. */	isc_boolean_t	 overmem;	/* The cache is in an overmem state. */};/* * The actual cache object. */struct dns_cache {	/* Unlocked. */	unsigned int		magic;	isc_mutex_t		lock;	isc_mutex_t		filelock;	isc_mem_t		*mctx;	/* Locked by 'lock'. */	int			references;	int			live_tasks;	dns_rdataclass_t	rdclass;	dns_db_t		*db;	cache_cleaner_t		cleaner;	char			*db_type;	int			db_argc;	char			**db_argv;	/* Locked by 'filelock'. */	char *			filename;	/* Access to the on-disk cache file is also locked by 'filelock'. */};/*** ***	Functions ***/static isc_result_tcache_cleaner_init(dns_cache_t *cache, isc_taskmgr_t *taskmgr,		   isc_timermgr_t *timermgr, cache_cleaner_t *cleaner);static voidcleaning_timer_action(isc_task_t *task, isc_event_t *event);static voidincremental_cleaning_action(isc_task_t *task, isc_event_t *event);static voidcleaner_shutdown_action(isc_task_t *task, isc_event_t *event);static voidovermem_cleaning_action(isc_task_t *task, isc_event_t *event);static inline isc_result_tcache_create_db(dns_cache_t *cache, dns_db_t **db) {	return (dns_db_create(cache->mctx, cache->db_type, dns_rootname,			      dns_dbtype_cache, cache->rdclass,			      cache->db_argc, cache->db_argv, db));}isc_result_tdns_cache_create(isc_mem_t *mctx, isc_taskmgr_t *taskmgr,		 isc_timermgr_t *timermgr, dns_rdataclass_t rdclass,		 const char *db_type, unsigned int db_argc, char **db_argv,		 dns_cache_t **cachep){	isc_result_t result;	dns_cache_t *cache;	int i;	REQUIRE(cachep != NULL);	REQUIRE(*cachep == NULL);	REQUIRE(mctx != NULL);	cache = isc_mem_get(mctx, sizeof(*cache));	if (cache == NULL)		return (ISC_R_NOMEMORY);	cache->mctx = NULL;	isc_mem_attach(mctx, &cache->mctx);	result = isc_mutex_init(&cache->lock);	if (result != ISC_R_SUCCESS) {		UNEXPECTED_ERROR(__FILE__, __LINE__,				 "isc_mutex_init() failed: %s",				 dns_result_totext(result));		result = ISC_R_UNEXPECTED;		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;	cache->db_type = isc_mem_strdup(mctx, db_type);	if (cache->db_type == NULL) {		result = ISC_R_NOMEMORY;		goto cleanup_filelock;	}	cache->db_argc = db_argc;	if (cache->db_argc == 0)		cache->db_argv = NULL;	else {		cache->db_argv = isc_mem_get(mctx,					     cache->db_argc * sizeof(char *));		if (cache->db_argv == NULL) {			result = ISC_R_NOMEMORY;			goto cleanup_dbtype;		}		for (i = 0; i < cache->db_argc; i++)			cache->db_argv[i] = NULL;		for (i = 0; i < cache->db_argc; i++) {			cache->db_argv[i] = isc_mem_strdup(mctx, db_argv[i]);			if (cache->db_argv[i] == NULL) {				result = ISC_R_NOMEMORY;				goto cleanup_dbargv;			}		}	}	cache->db = NULL;	result = cache_create_db(cache, &cache->db);	if (result != ISC_R_SUCCESS)		goto cleanup_dbargv;	cache->filename = NULL;	cache->magic = CACHE_MAGIC;	result = cache_cleaner_init(cache, taskmgr, timermgr, &cache->cleaner);	if (result != ISC_R_SUCCESS)		goto cleanup_db;	*cachep = cache;	return (ISC_R_SUCCESS); cleanup_db:	dns_db_detach(&cache->db); cleanup_dbargv:	for (i = 0; i < cache->db_argc; i++)		if (cache->db_argv[i] != NULL)			isc_mem_free(mctx, cache->db_argv[i]);	if (cache->db_argv != NULL)		isc_mem_put(mctx, cache->db_argv,			    cache->db_argc * sizeof(char *)); cleanup_dbtype:	isc_mem_free(mctx, cache->db_type); cleanup_filelock:	DESTROYLOCK(&cache->filelock); cleanup_lock:	DESTROYLOCK(&cache->lock); cleanup_mem:	isc_mem_put(mctx, cache, sizeof(*cache));	isc_mem_detach(&mctx);	return (result);}static voidcache_free(dns_cache_t *cache) {	isc_mem_t *mctx;	int i;	REQUIRE(VALID_CACHE(cache));	REQUIRE(cache->references == 0);	isc_mem_setwater(cache->mctx, NULL, NULL, 0, 0);	if (cache->cleaner.task != NULL)		isc_task_detach(&cache->cleaner.task);	if (cache->cleaner.overmem_event != NULL)		isc_event_free(&cache->cleaner.overmem_event);	if (cache->cleaner.resched_event != NULL)		isc_event_free(&cache->cleaner.resched_event);	if (cache->cleaner.iterator != NULL)		dns_dbiterator_destroy(&cache->cleaner.iterator);	DESTROYLOCK(&cache->cleaner.lock);	if (cache->filename) {		isc_mem_free(cache->mctx, cache->filename);		cache->filename = NULL;	}	if (cache->db != NULL)		dns_db_detach(&cache->db);	if (cache->db_argv != NULL) {		for (i = 0; i < cache->db_argc; i++)			if (cache->db_argv[i] != NULL)				isc_mem_free(cache->mctx, cache->db_argv[i]);		isc_mem_put(cache->mctx, cache->db_argv,			    cache->db_argc * sizeof(char *));	}	if (cache->db_type != NULL)		isc_mem_free(cache->mctx, cache->db_type);	DESTROYLOCK(&cache->lock);	DESTROYLOCK(&cache->filelock);	cache->magic = 0;	mctx = cache->mctx;	isc_mem_put(cache->mctx, cache, sizeof(*cache));	isc_mem_detach(&mctx);}voiddns_cache_attach(dns_cache_t *cache, dns_cache_t **targetp) {	REQUIRE(VALID_CACHE(cache));	REQUIRE(targetp != NULL && *targetp == NULL);	LOCK(&cache->lock);	cache->references++;	UNLOCK(&cache->lock);	*targetp = cache;}voiddns_cache_detach(dns_cache_t **cachep) {	dns_cache_t *cache;	isc_boolean_t free_cache = ISC_FALSE;	REQUIRE(cachep != NULL);	cache = *cachep;	REQUIRE(VALID_CACHE(cache));	LOCK(&cache->lock);	REQUIRE(cache->references > 0);	cache->references--;	if (cache->references == 0) {		cache->cleaner.overmem = ISC_FALSE;		free_cache = ISC_TRUE;	}	*cachep = NULL;	if (free_cache) {		/*		 * When the cache is shut down, dump it to a file if one is		 * specified.		 */		isc_result_t result = dns_cache_dump(cache);		if (result != ISC_R_SUCCESS)			isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,				      DNS_LOGMODULE_CACHE, ISC_LOG_WARNING,				      "error dumping cache: %s ",				      isc_result_totext(result));		/*		 * If the cleaner task exists, let it free the cache.		 */		if (cache->live_tasks > 0) {			isc_task_shutdown(cache->cleaner.task);			free_cache = ISC_FALSE;		}	}	UNLOCK(&cache->lock);	if (free_cache)		cache_free(cache);}voiddns_cache_attachdb(dns_cache_t *cache, dns_db_t **dbp) {	REQUIRE(VALID_CACHE(cache));	REQUIRE(dbp != NULL && *dbp == NULL);	REQUIRE(cache->db != NULL);	LOCK(&cache->lock);	dns_db_attach(cache->db, dbp);	UNLOCK(&cache->lock);}isc_result_tdns_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);	if (cache->filename)		isc_mem_free(cache->mctx, cache->filename);	cache->filename = newname;	UNLOCK(&cache->filelock);	return (ISC_R_SUCCESS);}isc_result_tdns_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);	result = dns_db_load(cache->db, cache->filename);	UNLOCK(&cache->filelock);	return (result);}isc_result_tdns_cache_dump(dns_cache_t *cache) {	isc_result_t result;	REQUIRE(VALID_CACHE(cache));	if (cache->filename == NULL)		return (ISC_R_SUCCESS);	LOCK(&cache->filelock);	result = dns_master_dump(cache->mctx, cache->db, NULL,				 &dns_master_style_cache, cache->filename);	UNLOCK(&cache->filelock);	return (result);}voiddns_cache_setcleaninginterval(dns_cache_t *cache, unsigned int t) {	isc_interval_t interval;	isc_result_t result;	LOCK(&cache->lock);	/*	 * It may be the case that the cache has already shut down.	 * If so, it has no timer.	 */	if (cache->cleaner.cleaning_timer == NULL)		goto unlock;	cache->cleaner.cleaning_interval = t;	if (t == 0) {		result = isc_timer_reset(cache->cleaner.cleaning_timer,					 isc_timertype_inactive,					 NULL, NULL, ISC_TRUE);	} else {		isc_interval_set(&interval, cache->cleaner.cleaning_interval,				 0);		result = isc_timer_reset(cache->cleaner.cleaning_timer,					 isc_timertype_ticker,					 NULL, &interval, ISC_FALSE);	}	if (result != ISC_R_SUCCESS)			isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,			      DNS_LOGMODULE_CACHE, ISC_LOG_WARNING,			      "could not set cache cleaning interval: %s",			      isc_result_totext(result)); unlock:	UNLOCK(&cache->lock);}/* * Initialize the cache cleaner object at *cleaner. * Space for the object must be allocated by the caller. */static isc_result_tcache_cleaner_init(dns_cache_t *cache, isc_taskmgr_t *taskmgr,		   isc_timermgr_t *timermgr, cache_cleaner_t *cleaner){	isc_result_t result;	result = isc_mutex_init(&cleaner->lock);	if (result != ISC_R_SUCCESS) {		UNEXPECTED_ERROR(__FILE__, __LINE__,				 "isc_mutex_init() failed: %s",				 dns_result_totext(result));		result = ISC_R_UNEXPECTED;		goto fail;	}	cleaner->increment = DNS_CACHE_CLEANERINCREMENT;	cleaner->state = cleaner_s_idle;	cleaner->cache = cache;	cleaner->iterator = NULL;	cleaner->overmem = ISC_FALSE;	cleaner->task = NULL;	cleaner->cleaning_timer = NULL;	cleaner->resched_event = NULL;	cleaner->overmem_event = NULL;	if (taskmgr != NULL && timermgr != NULL) {		result = isc_task_create(taskmgr, 1, &cleaner->task);		if (result != ISC_R_SUCCESS) {			UNEXPECTED_ERROR(__FILE__, __LINE__,					 "isc_task_create() failed: %s",					 dns_result_totext(result));			result = ISC_R_UNEXPECTED;			goto cleanup;		}		cleaner->cache->live_tasks++;		isc_task_setname(cleaner->task, "cachecleaner", cleaner);		result = isc_task_onshutdown(cleaner->task,					     cleaner_shutdown_action, cache);		if (result != ISC_R_SUCCESS) {			UNEXPECTED_ERROR(__FILE__, __LINE__,					 "cache cleaner: "					 "isc_task_onshutdown() failed: %s",					 dns_result_totext(result));			goto cleanup;		}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -