1 /* Copyright (c) 1998 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <atomicity.h>
26 #include <rpcsvc/nis.h>
27 #include <sys/param.h>
34 /* Search the cache for a matching entry and return it when found. If
35 this fails search the negative cache and return (void *) -1 if this
36 search was successful. Otherwise return NULL.
38 This function must be called with the read-lock held. */
40 cache_search (int type, void *key, size_t len, struct database *table)
42 unsigned long int hash = __nis_hash (key, len) % table->module;
43 struct hashentry *work;
45 work = table->array[hash];
49 if (type == work->type
50 && len == work->len && memcmp (key, work->key, len) == 0)
52 /* We found the entry. Increment the appropriate counter. */
53 if (work->data == (void *) -1)
67 /* Add a new entry to the cache. The return value is zero if the function
70 This function must be called with the read-lock held.
72 We modify the table but we nevertheless only acquire a read-lock.
73 This is ok since we use operations which would be safe even without
74 locking, given that the `prune_cache' function never runs. Using
75 the readlock reduces the chance of conflicts. */
77 cache_add (int type, void *key, size_t len, const void *packet, size_t total,
78 void *data, int last, time_t t, struct database *table)
80 unsigned long int hash = __nis_hash (key, len) % table->module;
81 struct hashentry *newp;
83 newp = malloc (sizeof (struct hashentry));
85 error (EXIT_FAILURE, errno, _("while allocating hash table entry"));
92 newp->packet = packet;
97 /* Put the new entry in the first position. */
99 newp->next = table->array[hash];
100 while (! compare_and_swap ((volatile long int *) &table->array[hash],
101 (long int) newp->next, (long int) newp));
103 /* Update the statistics. */
104 if (data == (void *) -1)
110 /* Walk through the table and remove all entries which lifetime ended.
112 We have a problem here. To actually remove the entries we must get
113 the write-lock. But since we want to keep the time we have the
114 lock as short as possible we cannot simply acquire the lock when we
115 start looking for timedout entries.
117 Therefore we do it in two stages: first we look for entries which
118 must be invalidated and remember them. Then we get the lock and
119 actually remove them. This is complicated by the way we have to
120 free the data structures since some hash table entries share the same
123 prune_cache (struct database *table, time_t now)
125 size_t cnt = table->module;
128 size_t first = cnt + 1;
131 /* If we check for the modification of the underlying file we invalidate
132 the entries also in this case. */
133 if (table->check_file)
137 if (stat (table->filename, &st) < 0)
140 /* We cannot stat() the file, disable file checking. */
141 dbg_log (_("cannot stat() file `%s': %s"),
142 table->filename, strerror_r (errno, buf, sizeof (buf)));
143 table->check_file = 0;
147 if (st.st_mtime != table->file_mtime)
148 /* The file changed. Invalidate all entries. */
153 /* We run through the table and find values which are not valid anymore.
155 Note that for the initial step, finding the entries to be removed,
156 we don't need to get any lock. It is at all timed assured that the
157 linked lists are set up correctly and that no second thread prunes
161 struct hashentry *runp = table->array[--cnt];
167 if (runp->timeout < now)
171 first = MIN (first, cnt);
172 last = MAX (last, cnt);
181 struct hashentry *head = NULL;
183 /* Now we have to get the write lock since we are about to modify
185 pthread_rwlock_wrlock (&table->lock);
187 while (first <= last)
191 struct hashentry *runp;
193 while (table->array[first]->timeout < now)
195 table->array[first]->dellist = head;
196 head = table->array[first];
197 table->array[first] = head->next;
198 if (--mark[first] == 0)
202 runp = table->array[first];
203 while (mark[first] > 0)
205 if (runp->next->timeout < now)
207 runp->next->dellist = head;
209 runp->next = head->next;
220 pthread_rwlock_unlock (&table->lock);
222 /* And another run to free the data. */
225 struct hashentry *old = head;
229 char buf[INET6_ADDRSTRLEN];
232 if ((old->type == GETHOSTBYADDR || old->type == GETHOSTBYADDRv6)
233 && (old->last || old->data == (void *) -1))
235 inet_ntop (old->type == GETHOSTBYADDR ? AF_INET : AF_INET6,
236 old->key, buf, sizeof (buf));
240 str = old->last ? old->key : (old->data == (void *) -1
243 dbg_log ("remove %s entry \"%s\"", serv2str[old->type], str);
246 /* Free the data structures. */
247 if (old->data == (void *) -1)
252 head = head->dellist;
256 while (head != NULL);