2004-07-22 09:47:28 +08:00
|
|
|
/* Copyright (c) 1998, 1999, 2003, 2004 Free Software Foundation, Inc.
|
1998-10-18 23:16:22 +08:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
2001-07-06 12:58:11 +08:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
1998-10-18 23:16:22 +08:00
|
|
|
|
|
|
|
The GNU C Library 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
|
2001-07-06 12:58:11 +08:00
|
|
|
Lesser General Public License for more details.
|
1998-10-18 23:16:22 +08:00
|
|
|
|
2001-07-06 12:58:11 +08:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with the GNU C Library; if not, write to the Free
|
|
|
|
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
02111-1307 USA. */
|
1998-10-18 23:16:22 +08:00
|
|
|
|
2004-08-27 02:35:05 +08:00
|
|
|
#include <assert.h>
|
2003-03-20 15:19:17 +08:00
|
|
|
#include <atomic.h>
|
1998-10-18 23:16:22 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <error.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1999-06-19 17:58:37 +08:00
|
|
|
#include <libintl.h>
|
1999-02-07 20:50:11 +08:00
|
|
|
#include <arpa/inet.h>
|
1998-10-18 23:16:22 +08:00
|
|
|
#include <rpcsvc/nis.h>
|
2004-08-27 02:35:05 +08:00
|
|
|
#include <sys/mman.h>
|
1998-10-18 23:16:22 +08:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
|
|
|
|
#include "nscd.h"
|
|
|
|
#include "dbg_log.h"
|
|
|
|
|
2004-08-27 02:35:05 +08:00
|
|
|
|
|
|
|
/* Number of times a value is reloaded without being used. UINT_MAX
|
|
|
|
means unlimited. */
|
|
|
|
unsigned int reload_count = DEFAULT_RELOAD_LIMIT;
|
|
|
|
|
|
|
|
|
1998-10-18 23:16:22 +08:00
|
|
|
/* Search the cache for a matching entry and return it when found. If
|
|
|
|
this fails search the negative cache and return (void *) -1 if this
|
|
|
|
search was successful. Otherwise return NULL.
|
|
|
|
|
|
|
|
This function must be called with the read-lock held. */
|
2004-08-27 02:35:05 +08:00
|
|
|
struct datahead *
|
|
|
|
cache_search (request_type type, void *key, size_t len,
|
|
|
|
struct database_dyn *table, uid_t owner)
|
1998-10-18 23:16:22 +08:00
|
|
|
{
|
2004-08-27 02:35:05 +08:00
|
|
|
unsigned long int hash = __nis_hash (key, len) % table->head->module;
|
1998-10-18 23:16:22 +08:00
|
|
|
|
2004-08-27 02:35:05 +08:00
|
|
|
unsigned long int nsearched = 0;
|
|
|
|
struct datahead *result = NULL;
|
1998-10-18 23:16:22 +08:00
|
|
|
|
2004-08-27 02:35:05 +08:00
|
|
|
ref_t work = table->head->array[hash];
|
|
|
|
while (work != ENDREF)
|
1998-10-18 23:16:22 +08:00
|
|
|
{
|
2003-04-26 12:15:50 +08:00
|
|
|
++nsearched;
|
|
|
|
|
2004-08-27 02:35:05 +08:00
|
|
|
struct hashentry *here = (struct hashentry *) (table->data + work);
|
|
|
|
|
|
|
|
if (type == here->type && len == here->len
|
|
|
|
&& memcmp (key, table->data + here->key, len) == 0
|
|
|
|
&& here->owner == owner)
|
1998-10-18 23:16:22 +08:00
|
|
|
{
|
|
|
|
/* We found the entry. Increment the appropriate counter. */
|
2004-08-27 02:35:05 +08:00
|
|
|
struct datahead *dh
|
|
|
|
= (struct datahead *) (table->data + here->packet);
|
|
|
|
|
|
|
|
/* See whether we must ignore the entry. */
|
|
|
|
if (dh->usable)
|
|
|
|
{
|
|
|
|
/* We do not synchronize the memory here. The statistics
|
|
|
|
data is not crucial, we synchronize only once in a while
|
|
|
|
in the cleanup threads. */
|
|
|
|
if (dh->notfound)
|
|
|
|
++table->head->neghit;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++table->head->poshit;
|
|
|
|
|
|
|
|
if (dh->nreloads != 0)
|
|
|
|
dh->nreloads = 0;
|
|
|
|
}
|
1998-10-18 23:16:22 +08:00
|
|
|
|
2004-08-27 02:35:05 +08:00
|
|
|
result = dh;
|
|
|
|
break;
|
|
|
|
}
|
1998-10-18 23:16:22 +08:00
|
|
|
}
|
|
|
|
|
2004-08-27 02:35:05 +08:00
|
|
|
work = here->next;
|
1998-10-18 23:16:22 +08:00
|
|
|
}
|
|
|
|
|
2004-08-27 02:35:05 +08:00
|
|
|
if (nsearched > table->head->maxnsearched)
|
|
|
|
table->head->maxnsearched = nsearched;
|
2003-04-26 12:15:50 +08:00
|
|
|
|
2004-08-27 02:35:05 +08:00
|
|
|
return result;
|
1998-10-18 23:16:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add a new entry to the cache. The return value is zero if the function
|
|
|
|
call was successful.
|
|
|
|
|
|
|
|
This function must be called with the read-lock held.
|
|
|
|
|
|
|
|
We modify the table but we nevertheless only acquire a read-lock.
|
|
|
|
This is ok since we use operations which would be safe even without
|
|
|
|
locking, given that the `prune_cache' function never runs. Using
|
|
|
|
the readlock reduces the chance of conflicts. */
|
2004-08-27 02:35:05 +08:00
|
|
|
int
|
|
|
|
cache_add (int type, const void *key, size_t len, struct datahead *packet,
|
|
|
|
bool first, struct database_dyn *table,
|
|
|
|
uid_t owner)
|
1998-10-18 23:16:22 +08:00
|
|
|
{
|
2004-08-27 02:35:05 +08:00
|
|
|
if (__builtin_expect (debug_level >= 2, 0))
|
2004-09-05 15:19:46 +08:00
|
|
|
{
|
|
|
|
const char *str;
|
|
|
|
char buf[INET6_ADDRSTRLEN + 1];
|
|
|
|
if (type == GETHOSTBYADDR || type == GETHOSTBYADDRv6)
|
|
|
|
str = inet_ntop (type == GETHOSTBYADDR ? AF_INET : AF_INET6,
|
|
|
|
key, buf, sizeof (buf));
|
|
|
|
else
|
|
|
|
str = key;
|
|
|
|
|
|
|
|
dbg_log (_("add new entry \"%s\" of type %s for %s to cache%s"),
|
|
|
|
str, serv2str[type], dbnames[table - dbs],
|
|
|
|
first ? " (first)" : "");
|
|
|
|
}
|
2004-08-27 02:35:05 +08:00
|
|
|
|
|
|
|
unsigned long int hash = __nis_hash (key, len) % table->head->module;
|
1998-10-18 23:16:22 +08:00
|
|
|
struct hashentry *newp;
|
|
|
|
|
2004-08-27 02:35:05 +08:00
|
|
|
newp = mempool_alloc (table, sizeof (struct hashentry));
|
|
|
|
/* If we cannot allocate memory, just do not do anything. */
|
1998-10-18 23:16:22 +08:00
|
|
|
if (newp == NULL)
|
2004-08-27 02:35:05 +08:00
|
|
|
return -1;
|
1998-10-18 23:16:22 +08:00
|
|
|
|
|
|
|
newp->type = type;
|
2004-08-27 02:35:05 +08:00
|
|
|
newp->first = first;
|
1998-10-18 23:16:22 +08:00
|
|
|
newp->len = len;
|
2004-08-27 02:35:05 +08:00
|
|
|
newp->key = (char *) key - table->data;
|
|
|
|
assert (newp->key + newp->len <= table->head->first_free);
|
1999-06-12 04:58:21 +08:00
|
|
|
newp->owner = owner;
|
2004-08-27 02:35:05 +08:00
|
|
|
newp->packet = (char *) packet - table->data;
|
1998-10-18 23:16:22 +08:00
|
|
|
|
|
|
|
/* Put the new entry in the first position. */
|
|
|
|
do
|
2004-08-27 02:35:05 +08:00
|
|
|
newp->next = table->head->array[hash];
|
|
|
|
while (atomic_compare_and_exchange_bool_acq (&table->head->array[hash],
|
|
|
|
(ref_t) ((char *) newp
|
|
|
|
- table->data),
|
|
|
|
(ref_t) newp->next));
|
1998-10-18 23:16:22 +08:00
|
|
|
|
|
|
|
/* Update the statistics. */
|
2004-08-27 02:35:05 +08:00
|
|
|
if (packet->notfound)
|
|
|
|
++table->head->negmiss;
|
|
|
|
else if (first)
|
|
|
|
++table->head->posmiss;
|
|
|
|
|
|
|
|
/* We depend on this value being correct and at least as high as the
|
|
|
|
real number of entries. */
|
|
|
|
atomic_increment (&table->head->nentries);
|
|
|
|
|
|
|
|
/* It does not matter that we are not loading the just increment
|
|
|
|
value, this is just for statistics. */
|
|
|
|
unsigned long int nentries = table->head->nentries;
|
|
|
|
if (nentries > table->head->maxnentries)
|
|
|
|
table->head->maxnentries = nentries;
|
|
|
|
|
|
|
|
return 0;
|
1998-10-18 23:16:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Walk through the table and remove all entries which lifetime ended.
|
|
|
|
|
|
|
|
We have a problem here. To actually remove the entries we must get
|
|
|
|
the write-lock. But since we want to keep the time we have the
|
|
|
|
lock as short as possible we cannot simply acquire the lock when we
|
|
|
|
start looking for timedout entries.
|
|
|
|
|
|
|
|
Therefore we do it in two stages: first we look for entries which
|
|
|
|
must be invalidated and remember them. Then we get the lock and
|
|
|
|
actually remove them. This is complicated by the way we have to
|
|
|
|
free the data structures since some hash table entries share the same
|
1998-10-19 23:22:14 +08:00
|
|
|
data. */
|
1998-10-18 23:16:22 +08:00
|
|
|
void
|
2004-08-27 02:35:05 +08:00
|
|
|
prune_cache (struct database_dyn *table, time_t now)
|
1998-10-18 23:16:22 +08:00
|
|
|
{
|
2004-08-27 02:35:05 +08:00
|
|
|
size_t cnt = table->head->module;
|
1998-10-18 23:16:22 +08:00
|
|
|
|
1999-04-15 16:43:35 +08:00
|
|
|
/* If this table is not actually used don't do anything. */
|
|
|
|
if (cnt == 0)
|
|
|
|
return;
|
|
|
|
|
1998-10-18 23:16:22 +08:00
|
|
|
/* If we check for the modification of the underlying file we invalidate
|
|
|
|
the entries also in this case. */
|
|
|
|
if (table->check_file)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (stat (table->filename, &st) < 0)
|
|
|
|
{
|
|
|
|
char buf[128];
|
1999-04-16 17:38:29 +08:00
|
|
|
/* We cannot stat() the file, disable file checking if the
|
|
|
|
file does not exist. */
|
1998-10-18 23:16:22 +08:00
|
|
|
dbg_log (_("cannot stat() file `%s': %s"),
|
|
|
|
table->filename, strerror_r (errno, buf, sizeof (buf)));
|
1999-04-16 17:38:29 +08:00
|
|
|
if (errno == ENOENT)
|
|
|
|
table->check_file = 0;
|
1998-10-18 23:16:22 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (st.st_mtime != table->file_mtime)
|
1999-04-16 17:09:54 +08:00
|
|
|
{
|
|
|
|
/* The file changed. Invalidate all entries. */
|
|
|
|
now = LONG_MAX;
|
1999-04-16 17:38:29 +08:00
|
|
|
table->file_mtime = st.st_mtime;
|
1999-04-16 17:09:54 +08:00
|
|
|
}
|
1998-10-18 23:16:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We run through the table and find values which are not valid anymore.
|
|
|
|
|
2003-04-26 12:15:50 +08:00
|
|
|
Note that for the initial step, finding the entries to be removed,
|
|
|
|
we don't need to get any lock. It is at all timed assured that the
|
|
|
|
linked lists are set up correctly and that no second thread prunes
|
|
|
|
the cache. */
|
2004-08-27 02:35:05 +08:00
|
|
|
bool mark[cnt];
|
|
|
|
size_t first = cnt + 1;
|
|
|
|
size_t last = 0;
|
|
|
|
char *const data = table->data;
|
|
|
|
bool any = false;
|
|
|
|
|
1998-10-18 23:16:22 +08:00
|
|
|
do
|
|
|
|
{
|
2004-08-27 02:35:05 +08:00
|
|
|
ref_t run = table->head->array[--cnt];
|
1998-10-18 23:16:22 +08:00
|
|
|
|
2004-08-27 02:35:05 +08:00
|
|
|
while (run != ENDREF)
|
1998-10-18 23:16:22 +08:00
|
|
|
{
|
2004-08-27 02:35:05 +08:00
|
|
|
struct hashentry *runp = (struct hashentry *) (data + run);
|
|
|
|
struct datahead *dh = (struct datahead *) (data + runp->packet);
|
|
|
|
|
|
|
|
/* Check whether the entry timed out. */
|
|
|
|
if (dh->timeout < now)
|
1998-10-18 23:16:22 +08:00
|
|
|
{
|
2004-08-27 02:35:05 +08:00
|
|
|
/* This hash bucket could contain entries which need to
|
|
|
|
be looked at. */
|
|
|
|
mark[cnt] = true;
|
|
|
|
|
1998-10-18 23:16:22 +08:00
|
|
|
first = MIN (first, cnt);
|
|
|
|
last = MAX (last, cnt);
|
2004-08-27 02:35:05 +08:00
|
|
|
|
|
|
|
/* We only have to look at the data of the first entries
|
|
|
|
since the count information is kept in the data part
|
|
|
|
which is shared. */
|
|
|
|
if (runp->first)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* At this point there are two choices: we reload the
|
|
|
|
value or we discard it. Do not change NRELOADS if
|
|
|
|
we never not reload the record. */
|
|
|
|
if ((reload_count != UINT_MAX
|
|
|
|
&& __builtin_expect (dh->nreloads >= reload_count, 0))
|
|
|
|
/* We always remove negative entries. */
|
|
|
|
|| dh->notfound
|
|
|
|
/* Discard everything if the user explicitly
|
|
|
|
requests it. */
|
|
|
|
|| now == LONG_MAX)
|
|
|
|
{
|
|
|
|
/* Remove the value. */
|
|
|
|
dh->usable = false;
|
|
|
|
|
|
|
|
/* We definitely have some garbage entries now. */
|
|
|
|
any = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Reload the value. We do this only for the
|
|
|
|
initially used key, not the additionally
|
|
|
|
added derived value. */
|
|
|
|
switch (runp->type)
|
|
|
|
{
|
|
|
|
case GETPWBYNAME:
|
|
|
|
readdpwbyname (table, runp, dh);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GETPWBYUID:
|
|
|
|
readdpwbyuid (table, runp, dh);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GETGRBYNAME:
|
|
|
|
readdgrbyname (table, runp, dh);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GETGRBYGID:
|
|
|
|
readdgrbygid (table, runp, dh);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GETHOSTBYNAME:
|
|
|
|
readdhstbyname (table, runp, dh);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GETHOSTBYNAMEv6:
|
|
|
|
readdhstbynamev6 (table, runp, dh);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GETHOSTBYADDR:
|
|
|
|
readdhstbyaddr (table, runp, dh);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GETHOSTBYADDRv6:
|
|
|
|
readdhstbyaddrv6 (table, runp, dh);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert (! "should never happen");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the entry has been replaced, we might need
|
|
|
|
cleanup. */
|
|
|
|
any |= !dh->usable;
|
|
|
|
}
|
|
|
|
}
|
1998-10-18 23:16:22 +08:00
|
|
|
}
|
2004-08-27 02:35:05 +08:00
|
|
|
else
|
|
|
|
assert (dh->usable);
|
|
|
|
|
|
|
|
run = runp->next;
|
1998-10-18 23:16:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
while (cnt > 0);
|
|
|
|
|
2004-08-27 02:35:05 +08:00
|
|
|
if (first <= last)
|
1998-10-18 23:16:22 +08:00
|
|
|
{
|
|
|
|
struct hashentry *head = NULL;
|
|
|
|
|
|
|
|
/* Now we have to get the write lock since we are about to modify
|
|
|
|
the table. */
|
2003-04-26 12:15:50 +08:00
|
|
|
if (__builtin_expect (pthread_rwlock_trywrlock (&table->lock) != 0, 0))
|
|
|
|
{
|
2004-08-27 02:35:05 +08:00
|
|
|
++table->head->wrlockdelayed;
|
2003-04-26 12:15:50 +08:00
|
|
|
pthread_rwlock_wrlock (&table->lock);
|
|
|
|
}
|
1998-10-18 23:16:22 +08:00
|
|
|
|
|
|
|
while (first <= last)
|
|
|
|
{
|
2004-08-27 02:35:05 +08:00
|
|
|
if (mark[first])
|
1998-10-18 23:16:22 +08:00
|
|
|
{
|
2004-08-27 02:35:05 +08:00
|
|
|
ref_t *old = &table->head->array[first];
|
|
|
|
ref_t run = table->head->array[first];
|
1998-10-18 23:16:22 +08:00
|
|
|
|
2004-08-27 02:35:05 +08:00
|
|
|
while (run != ENDREF)
|
1998-10-18 23:16:22 +08:00
|
|
|
{
|
2004-08-27 02:35:05 +08:00
|
|
|
struct hashentry *runp = (struct hashentry *) (data + run);
|
|
|
|
struct datahead *dh
|
|
|
|
= (struct datahead *) (data + runp->packet);
|
1998-10-18 23:16:22 +08:00
|
|
|
|
2004-08-27 02:35:05 +08:00
|
|
|
if (! dh->usable)
|
1998-10-18 23:16:22 +08:00
|
|
|
{
|
2004-08-27 02:35:05 +08:00
|
|
|
/* We need the list only for debugging but it is
|
|
|
|
more costly to avoid creating the list than
|
|
|
|
doing it. */
|
|
|
|
runp->dellist = head;
|
|
|
|
head = runp;
|
|
|
|
|
|
|
|
/* No need for an atomic operation, we have the
|
|
|
|
write lock. */
|
|
|
|
--table->head->nentries;
|
|
|
|
|
|
|
|
run = *old = runp->next;
|
1998-10-18 23:16:22 +08:00
|
|
|
}
|
|
|
|
else
|
2004-08-27 02:35:05 +08:00
|
|
|
{
|
|
|
|
old = &runp->next;
|
|
|
|
run = runp->next;
|
|
|
|
}
|
1998-10-18 23:16:22 +08:00
|
|
|
}
|
|
|
|
}
|
2004-08-27 02:35:05 +08:00
|
|
|
|
1998-10-18 23:16:22 +08:00
|
|
|
++first;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* It's all done. */
|
|
|
|
pthread_rwlock_unlock (&table->lock);
|
|
|
|
|
2004-08-27 02:35:05 +08:00
|
|
|
/* Make sure the data is saved to disk. */
|
|
|
|
if (table->persistent)
|
|
|
|
msync (table->head,
|
|
|
|
table->data + table->head->first_free - (char *) table->head,
|
|
|
|
MS_ASYNC);
|
|
|
|
|
2003-04-26 12:15:50 +08:00
|
|
|
/* One extra pass if we do debugging. */
|
|
|
|
if (__builtin_expect (debug_level > 0, 0))
|
1998-10-18 23:16:22 +08:00
|
|
|
{
|
2003-04-26 12:15:50 +08:00
|
|
|
struct hashentry *runp = head;
|
1998-10-18 23:16:22 +08:00
|
|
|
|
2003-04-26 12:15:50 +08:00
|
|
|
while (runp != NULL)
|
1999-02-07 08:06:12 +08:00
|
|
|
{
|
|
|
|
char buf[INET6_ADDRSTRLEN];
|
|
|
|
const char *str;
|
|
|
|
|
2003-04-26 12:15:50 +08:00
|
|
|
if (runp->type == GETHOSTBYADDR || runp->type == GETHOSTBYADDRv6)
|
1999-02-07 08:06:12 +08:00
|
|
|
{
|
2003-04-26 12:15:50 +08:00
|
|
|
inet_ntop (runp->type == GETHOSTBYADDR ? AF_INET : AF_INET6,
|
2004-08-27 02:35:05 +08:00
|
|
|
table->data + runp->key, buf, sizeof (buf));
|
1999-02-07 08:06:12 +08:00
|
|
|
str = buf;
|
|
|
|
}
|
|
|
|
else
|
2004-08-27 02:35:05 +08:00
|
|
|
str = table->data + runp->key;
|
1999-02-07 08:06:12 +08:00
|
|
|
|
2003-04-26 12:15:50 +08:00
|
|
|
dbg_log ("remove %s entry \"%s\"", serv2str[runp->type], str);
|
|
|
|
|
2004-07-22 09:47:28 +08:00
|
|
|
runp = runp->dellist;
|
1999-02-07 08:06:12 +08:00
|
|
|
}
|
2003-04-26 12:15:50 +08:00
|
|
|
}
|
1998-10-18 23:16:22 +08:00
|
|
|
}
|
2004-08-27 02:35:05 +08:00
|
|
|
|
|
|
|
/* Run garbage collection if any entry has been removed or replaced. */
|
|
|
|
if (any)
|
|
|
|
gc (table);
|
1998-10-18 23:16:22 +08:00
|
|
|
}
|