1 /* Copyright (c) 1998, 1999, 2003, 2004, 2005 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
27 #include <arpa/inet.h>
28 #include <rpcsvc/nis.h>
30 #include <sys/param.h>
38 /* Number of times a value is reloaded without being used. UINT_MAX
40 unsigned int reload_count
= DEFAULT_RELOAD_LIMIT
;
43 /* Search the cache for a matching entry and return it when found. If
44 this fails search the negative cache and return (void *) -1 if this
45 search was successful. Otherwise return NULL.
47 This function must be called with the read-lock held. */
49 cache_search (request_type type
, void *key
, size_t len
,
50 struct database_dyn
*table
, uid_t owner
)
52 unsigned long int hash
= __nis_hash (key
, len
) % table
->head
->module
;
54 unsigned long int nsearched
= 0;
55 struct datahead
*result
= NULL
;
57 ref_t work
= table
->head
->array
[hash
];
58 while (work
!= ENDREF
)
62 struct hashentry
*here
= (struct hashentry
*) (table
->data
+ work
);
64 if (type
== here
->type
&& len
== here
->len
65 && memcmp (key
, table
->data
+ here
->key
, len
) == 0
66 && here
->owner
== owner
)
68 /* We found the entry. Increment the appropriate counter. */
70 = (struct datahead
*) (table
->data
+ here
->packet
);
72 /* See whether we must ignore the entry. */
75 /* We do not synchronize the memory here. The statistics
76 data is not crucial, we synchronize only once in a while
77 in the cleanup threads. */
79 ++table
->head
->neghit
;
82 ++table
->head
->poshit
;
84 if (dh
->nreloads
!= 0)
96 if (nsearched
> table
->head
->maxnsearched
)
97 table
->head
->maxnsearched
= nsearched
;
102 /* Add a new entry to the cache. The return value is zero if the function
105 This function must be called with the read-lock held.
107 We modify the table but we nevertheless only acquire a read-lock.
108 This is ok since we use operations which would be safe even without
109 locking, given that the `prune_cache' function never runs. Using
110 the readlock reduces the chance of conflicts. */
112 cache_add (int type
, const void *key
, size_t len
, struct datahead
*packet
,
113 bool first
, struct database_dyn
*table
,
116 if (__builtin_expect (debug_level
>= 2, 0))
119 char buf
[INET6_ADDRSTRLEN
+ 1];
120 if (type
== GETHOSTBYADDR
|| type
== GETHOSTBYADDRv6
)
121 str
= inet_ntop (type
== GETHOSTBYADDR
? AF_INET
: AF_INET6
,
122 key
, buf
, sizeof (buf
));
126 dbg_log (_("add new entry \"%s\" of type %s for %s to cache%s"),
127 str
, serv2str
[type
], dbnames
[table
- dbs
],
128 first
? " (first)" : "");
131 unsigned long int hash
= __nis_hash (key
, len
) % table
->head
->module
;
132 struct hashentry
*newp
;
134 newp
= mempool_alloc (table
, sizeof (struct hashentry
));
135 /* If we cannot allocate memory, just do not do anything. */
142 newp
->key
= (char *) key
- table
->data
;
143 assert (newp
->key
+ newp
->len
<= table
->head
->first_free
);
145 newp
->packet
= (char *) packet
- table
->data
;
147 /* Put the new entry in the first position. */
149 newp
->next
= table
->head
->array
[hash
];
150 while (atomic_compare_and_exchange_bool_acq (&table
->head
->array
[hash
],
151 (ref_t
) ((char *) newp
153 (ref_t
) newp
->next
));
155 /* Update the statistics. */
156 if (packet
->notfound
)
157 ++table
->head
->negmiss
;
159 ++table
->head
->posmiss
;
161 /* We depend on this value being correct and at least as high as the
162 real number of entries. */
163 atomic_increment (&table
->head
->nentries
);
165 /* It does not matter that we are not loading the just increment
166 value, this is just for statistics. */
167 unsigned long int nentries
= table
->head
->nentries
;
168 if (nentries
> table
->head
->maxnentries
)
169 table
->head
->maxnentries
= nentries
;
171 if (table
->persistent
)
173 msync ((void *) table
->head
,
174 (char *) &table
->head
->array
[hash
] - (char *) table
->head
175 + sizeof (ref_t
), MS_ASYNC
);
180 /* Walk through the table and remove all entries which lifetime ended.
182 We have a problem here. To actually remove the entries we must get
183 the write-lock. But since we want to keep the time we have the
184 lock as short as possible we cannot simply acquire the lock when we
185 start looking for timedout entries.
187 Therefore we do it in two stages: first we look for entries which
188 must be invalidated and remember them. Then we get the lock and
189 actually remove them. This is complicated by the way we have to
190 free the data structures since some hash table entries share the same
193 prune_cache (struct database_dyn
*table
, time_t now
)
195 size_t cnt
= table
->head
->module
;
197 /* If this table is not actually used don't do anything. */
201 /* If we check for the modification of the underlying file we invalidate
202 the entries also in this case. */
203 if (table
->check_file
)
207 if (stat64 (table
->filename
, &st
) < 0)
210 /* We cannot stat() the file, disable file checking if the
211 file does not exist. */
212 dbg_log (_("cannot stat() file `%s': %s"),
213 table
->filename
, strerror_r (errno
, buf
, sizeof (buf
)));
215 table
->check_file
= 0;
219 if (st
.st_mtime
!= table
->file_mtime
)
221 /* The file changed. Invalidate all entries. */
223 table
->file_mtime
= st
.st_mtime
;
228 /* We run through the table and find values which are not valid anymore.
230 Note that for the initial step, finding the entries to be removed,
231 we don't need to get any lock. It is at all timed assured that the
232 linked lists are set up correctly and that no second thread prunes
235 size_t first
= cnt
+ 1;
237 char *const data
= table
->data
;
240 if (__builtin_expect (debug_level
> 2, 0))
241 dbg_log (_("pruning %s cache; time %ld"),
242 dbnames
[table
- dbs
], (long int) now
);
246 ref_t run
= table
->head
->array
[--cnt
];
248 while (run
!= ENDREF
)
250 struct hashentry
*runp
= (struct hashentry
*) (data
+ run
);
251 struct datahead
*dh
= (struct datahead
*) (data
+ runp
->packet
);
253 /* Some debug support. */
254 if (__builtin_expect (debug_level
> 2, 0))
256 char buf
[INET6_ADDRSTRLEN
];
259 if (runp
->type
== GETHOSTBYADDR
|| runp
->type
== GETHOSTBYADDRv6
)
261 inet_ntop (runp
->type
== GETHOSTBYADDR
? AF_INET
: AF_INET6
,
262 data
+ runp
->key
, buf
, sizeof (buf
));
266 str
= data
+ runp
->key
;
268 dbg_log (_("considering %s entry \"%s\", timeout %" PRIu64
),
269 serv2str
[runp
->type
], str
, dh
->timeout
);
272 /* Check whether the entry timed out. */
273 if (dh
->timeout
< now
)
275 /* This hash bucket could contain entries which need to
279 first
= MIN (first
, cnt
);
280 last
= MAX (last
, cnt
);
282 /* We only have to look at the data of the first entries
283 since the count information is kept in the data part
288 /* At this point there are two choices: we reload the
289 value or we discard it. Do not change NRELOADS if
290 we never not reload the record. */
291 if ((reload_count
!= UINT_MAX
292 && __builtin_expect (dh
->nreloads
>= reload_count
, 0))
293 /* We always remove negative entries. */
295 /* Discard everything if the user explicitly
299 /* Remove the value. */
302 /* We definitely have some garbage entries now. */
307 /* Reload the value. We do this only for the
308 initially used key, not the additionally
309 added derived value. */
313 readdpwbyname (table
, runp
, dh
);
317 readdpwbyuid (table
, runp
, dh
);
321 readdgrbyname (table
, runp
, dh
);
325 readdgrbygid (table
, runp
, dh
);
329 readdhstbyname (table
, runp
, dh
);
332 case GETHOSTBYNAMEv6
:
333 readdhstbynamev6 (table
, runp
, dh
);
337 readdhstbyaddr (table
, runp
, dh
);
340 case GETHOSTBYADDRv6
:
341 readdhstbyaddrv6 (table
, runp
, dh
);
345 readdhstai (table
, runp
, dh
);
349 readdinitgroups (table
, runp
, dh
);
353 assert (! "should never happen");
356 /* If the entry has been replaced, we might need
372 struct hashentry
*head
= NULL
;
374 /* Now we have to get the write lock since we are about to modify
376 if (__builtin_expect (pthread_rwlock_trywrlock (&table
->lock
) != 0, 0))
378 ++table
->head
->wrlockdelayed
;
379 pthread_rwlock_wrlock (&table
->lock
);
382 while (first
<= last
)
386 ref_t
*old
= &table
->head
->array
[first
];
387 ref_t run
= table
->head
->array
[first
];
389 while (run
!= ENDREF
)
391 struct hashentry
*runp
= (struct hashentry
*) (data
+ run
);
393 = (struct datahead
*) (data
+ runp
->packet
);
397 /* We need the list only for debugging but it is
398 more costly to avoid creating the list than
400 runp
->dellist
= head
;
403 /* No need for an atomic operation, we have the
405 --table
->head
->nentries
;
407 run
= *old
= runp
->next
;
421 pthread_rwlock_unlock (&table
->lock
);
423 /* Make sure the data is saved to disk. */
424 if (table
->persistent
)
426 data
+ table
->head
->first_free
- (char *) table
->head
,
429 /* One extra pass if we do debugging. */
430 if (__builtin_expect (debug_level
> 0, 0))
432 struct hashentry
*runp
= head
;
436 char buf
[INET6_ADDRSTRLEN
];
439 if (runp
->type
== GETHOSTBYADDR
|| runp
->type
== GETHOSTBYADDRv6
)
441 inet_ntop (runp
->type
== GETHOSTBYADDR
? AF_INET
: AF_INET6
,
442 data
+ runp
->key
, buf
, sizeof (buf
));
446 str
= data
+ runp
->key
;
448 dbg_log ("remove %s entry \"%s\"", serv2str
[runp
->type
], str
);
450 runp
= runp
->dellist
;
455 /* Run garbage collection if any entry has been removed or replaced. */