Update.
[glibc.git] / nscd / cache.c
blob4ab83db6e2d52845a1f769098ff6ec6ba0dbb923
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>
21 #include <errno.h>
22 #include <error.h>
23 #include <limits.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <rpcsvc/nis.h>
27 #include <sys/param.h>
28 #include <sys/stat.h>
29 #include <sys/uio.h>
31 #include "nscd.h"
32 #include "dbg_log.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. */
39 struct hashentry *
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];
47 while (work != NULL)
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)
54 ++table->neghit;
55 else
56 ++table->poshit;
58 return work;
61 work = work->next;
64 return NULL;
67 /* Add a new entry to the cache. The return value is zero if the function
68 call was successful.
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. */
76 void
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));
84 if (newp == NULL)
85 error (EXIT_FAILURE, errno, _("while allocating hash table entry"));
87 newp->type = type;
88 newp->len = len;
89 newp->key = key;
90 newp->data = data;
91 newp->timeout = t;
92 newp->packet = packet;
93 newp->total = total;
95 newp->last = last;
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)
105 ++table->negmiss;
106 else if (last)
107 ++table->posmiss;
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
121 data. */
122 void
123 prune_cache (struct database *table, time_t now)
125 size_t cnt = table->module;
126 int mark[cnt];
127 int anything = 0;
128 size_t first = cnt + 1;
129 size_t last = 0;
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)
135 struct stat st;
137 if (stat (table->filename, &st) < 0)
139 char buf[128];
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;
145 else
147 if (st.st_mtime != table->file_mtime)
148 /* The file changed. Invalidate all entries. */
149 now = LONG_MAX;
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
158 the cache. */
161 struct hashentry *runp = table->array[--cnt];
163 mark[cnt] = 0;
165 while (runp != NULL)
167 if (runp->timeout < now)
169 ++mark[cnt];
170 anything = 1;
171 first = MIN (first, cnt);
172 last = MAX (last, cnt);
174 runp = runp->next;
177 while (cnt > 0);
179 if (anything)
181 struct hashentry *head = NULL;
183 /* Now we have to get the write lock since we are about to modify
184 the table. */
185 pthread_rwlock_wrlock (&table->lock);
187 while (first <= last)
189 if (mark[first] > 0)
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)
199 break;
202 runp = table->array[first];
203 while (mark[first] > 0)
205 if (runp->next->timeout < now)
207 runp->next->dellist = head;
208 head = runp->next;
209 runp->next = head->next;
210 --mark[first];
212 else
213 runp = runp->next;
216 ++first;
219 /* It's all done. */
220 pthread_rwlock_unlock (&table->lock);
222 /* And another run to free the data. */
225 struct hashentry *old = head;
227 if (debug_level > 0)
228 dbg_log ("remove %s entry \"%s\"",
229 serv2str[old->type],
230 old->last
231 ? old->key : old->data == (void *) -1 ? old->key : "???");
233 /* Free the data structures. */
234 if (old->data == (void *) -1)
235 free (old->key);
236 else if (old->last)
237 free (old->data);
239 head = head->dellist;
241 free (old);
243 while (head != NULL);