cache.c revision b120e39deac3e9c323cdc7801ed2313f8ebbb744
/*
* Copyright (C) 1999 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 INTERNET SOFTWARE CONSORTIUM DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
* CONSORTIUM 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.2 1999/12/02 22:35:29 gson Exp $ */
#include <config.h>
#include <limits.h>
#include <isc/assertions.h>
#include <dns/dbiterator.h>
/***
*** 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. */
struct cache_cleaner {
int cleaning_interval; /* The cleaning-interval from
named.conf, in seconds. */
itself to reschedule */
int increment; /* Number of names to
clean in one increment */
};
/*
* The actual cache object.
*/
struct dns_cache {
/* Unlocked */
unsigned int magic;
/* Locked by 'lock'. */
unsigned int references;
unsigned int live_tasks;
/* Locked by 'filelock'. */
char * filename;
/* Access to the on-disk cache file is also locked by 'filelock'. */
};
/***
*** Functions
***/
static dns_result_t
static void
static void
static void
{
return (ISC_R_NOMEMORY);
if (iresult != ISC_R_SUCCESS) {
"isc_mutex_init() failed: %s",
goto fail;
}
cache->live_tasks = 0;
if (dresult != ISC_R_SUCCESS)
goto fail;
return (ISC_R_SUCCESS);
fail:
return (dresult);
}
static void
}
}
}
void
cache->references++;
}
void
cache->references--;
if (cache->references == 0) {
if (cache->live_tasks == 0)
}
if (free_cache)
}
void
}
#ifdef NOTYET
{
return (ISC_R_NOMEMORY);
return (ISC_R_SUCCESS);
}
return (ISC_R_SUCCESS);
/* XXX handle TTLs in a way appropriate for the cache */
return (dresult);
}
/* XXX to be written */
return (ISC_R_NOTIMPLEMENTED);
}
#endif
/*
* Initialize the cache cleaner object at *cleaner.
* Space for the object must be allocated by the caller.
*/
static dns_result_t
{
if (dresult != ISC_R_SUCCESS)
goto fail;
if (iresult != ISC_R_SUCCESS) {
"isc_task_create() failed: %s",
goto cleanup_dbiterator;
}
/* XXX get this from the configuration file */
if (iresult != ISC_R_SUCCESS) {
"isc_timer_create() failed: %s",
goto cleanup_task;
}
cleaner, sizeof(isc_event_t));
goto cleanup_timer;
}
}
return (ISC_R_SUCCESS);
fail:
return (dresult);
}
/*
* Try to clean the next n_names domain names.
*/
static dns_result_t
/*
* When starting or restarting from the idle state,
* position the iterator at the beginning of the cache.
*/
"begin cache cleaning");
if (dresult == ISC_R_NOMORE) {
/*
* We have an empty database, but that's OK.
*/
goto idle;
}
if (dresult != ISC_R_SUCCESS) {
"cache cleaner: dns_dbiterator_first() "
goto idle;
}
}
while (n_names-- > 0) {
(dns_name_t *) NULL);
if (dresult != ISC_R_SUCCESS) {
"cache cleaner: dns_dbiterator_current() "
goto idle;
}
/* Check TTLs, mark expired rdatasets stale. */
/* This is where the actual freeing takes place. */
/* Step to the next node */
if (dresult == ISC_R_NOMORE) {
/* We have successfully cleaned the whole cache. */
goto idle;
}
if (dresult != ISC_R_SUCCESS) {
"cache cleaner: dns_dbiterator_next() "
goto idle;
}
}
/* We have successfully performed a cleaning increment. */
goto done;
idle:
"end cache cleaning");
done:
"cache cleaner: dns_dbiterator_pause() "
return (ISC_R_UNEXPECTED);
}
return (return_result);
}
/*
* This is run once for every cache-cleaning-interval as defined in named.conf.
*/
static void
} else {
"cache cleaner did not finish "
"in one cleaning-interval");
}
}
/*
* Do incremental cleaning.
*/
static void
/*
* The return value from do_some_cleaning() is ignored because it
* does its own error reporting.
*/
/* Went idle. Save the event for later reuse. */
} else {
/* Still busy. Reschedule. */
}
}
/*
* Do immediate cleaning.
*/
if (dresult != ISC_R_SUCCESS)
return dresult;
while (dresult == ISC_R_SUCCESS) {
(dns_name_t *) NULL);
if (dresult != ISC_R_SUCCESS)
break;
/* Check TTLs, mark expired rdatasets stale. */
/* This is where the actual freeing takes place. */
}
if (dresult == ISC_R_NOMORE)
return dresult;
}
/*
* The cleaner task is shutting down; do the necessary cleanup.
*/
static void
cache->live_tasks--;
if (should_free)
}