1 /* Copyright (c) 1998, 1999, 2003-2005, 2006 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
, int fd
)
195 size_t cnt
= table
->head
->module
;
197 /* If this table is not actually used don't do anything. */
202 /* Reply to the INVALIDATE initiator. */
204 writeall (fd
, &resp
, sizeof (resp
));
209 /* This function can be called from the cleanup thread but also in
210 response to an invalidate command. Make sure only one thread is
211 running. When not serving INVALIDATE request, no need for the
212 second to wait around. */
215 if (pthread_mutex_trylock (&table
->prunelock
) != 0)
216 /* The work is already being done. */
220 pthread_mutex_lock (&table
->prunelock
);
222 /* If we check for the modification of the underlying file we invalidate
223 the entries also in this case. */
224 if (table
->check_file
)
228 if (stat64 (table
->filename
, &st
) < 0)
231 /* We cannot stat() the file, disable file checking if the
232 file does not exist. */
233 dbg_log (_("cannot stat() file `%s': %s"),
234 table
->filename
, strerror_r (errno
, buf
, sizeof (buf
)));
236 table
->check_file
= 0;
240 if (st
.st_mtime
!= table
->file_mtime
)
242 /* The file changed. Invalidate all entries. */
244 table
->file_mtime
= st
.st_mtime
;
249 /* We run through the table and find values which are not valid anymore.
251 Note that for the initial step, finding the entries to be removed,
252 we don't need to get any lock. It is at all timed assured that the
253 linked lists are set up correctly and that no second thread prunes
256 size_t first
= cnt
+ 1;
258 char *const data
= table
->data
;
261 if (__builtin_expect (debug_level
> 2, 0))
262 dbg_log (_("pruning %s cache; time %ld"),
263 dbnames
[table
- dbs
], (long int) now
);
267 ref_t run
= table
->head
->array
[--cnt
];
269 while (run
!= ENDREF
)
271 struct hashentry
*runp
= (struct hashentry
*) (data
+ run
);
272 struct datahead
*dh
= (struct datahead
*) (data
+ runp
->packet
);
274 /* Some debug support. */
275 if (__builtin_expect (debug_level
> 2, 0))
277 char buf
[INET6_ADDRSTRLEN
];
280 if (runp
->type
== GETHOSTBYADDR
|| runp
->type
== GETHOSTBYADDRv6
)
282 inet_ntop (runp
->type
== GETHOSTBYADDR
? AF_INET
: AF_INET6
,
283 data
+ runp
->key
, buf
, sizeof (buf
));
287 str
= data
+ runp
->key
;
289 dbg_log (_("considering %s entry \"%s\", timeout %" PRIu64
),
290 serv2str
[runp
->type
], str
, dh
->timeout
);
293 /* Check whether the entry timed out. */
294 if (dh
->timeout
< now
)
296 /* This hash bucket could contain entries which need to
300 first
= MIN (first
, cnt
);
301 last
= MAX (last
, cnt
);
303 /* We only have to look at the data of the first entries
304 since the count information is kept in the data part
309 /* At this point there are two choices: we reload the
310 value or we discard it. Do not change NRELOADS if
311 we never not reload the record. */
312 if ((reload_count
!= UINT_MAX
313 && __builtin_expect (dh
->nreloads
>= reload_count
, 0))
314 /* We always remove negative entries. */
316 /* Discard everything if the user explicitly
320 /* Remove the value. */
323 /* We definitely have some garbage entries now. */
328 /* Reload the value. We do this only for the
329 initially used key, not the additionally
330 added derived value. */
334 readdpwbyname (table
, runp
, dh
);
338 readdpwbyuid (table
, runp
, dh
);
342 readdgrbyname (table
, runp
, dh
);
346 readdgrbygid (table
, runp
, dh
);
350 readdhstbyname (table
, runp
, dh
);
353 case GETHOSTBYNAMEv6
:
354 readdhstbynamev6 (table
, runp
, dh
);
358 readdhstbyaddr (table
, runp
, dh
);
361 case GETHOSTBYADDRv6
:
362 readdhstbyaddrv6 (table
, runp
, dh
);
366 readdhstai (table
, runp
, dh
);
370 readdinitgroups (table
, runp
, dh
);
374 assert (! "should never happen");
377 /* If the entry has been replaced, we might need
393 /* Reply to the INVALIDATE initiator that the cache has been
396 writeall (fd
, &resp
, sizeof (resp
));
401 struct hashentry
*head
= NULL
;
403 /* Now we have to get the write lock since we are about to modify
405 if (__builtin_expect (pthread_rwlock_trywrlock (&table
->lock
) != 0, 0))
407 ++table
->head
->wrlockdelayed
;
408 pthread_rwlock_wrlock (&table
->lock
);
411 while (first
<= last
)
415 ref_t
*old
= &table
->head
->array
[first
];
416 ref_t run
= table
->head
->array
[first
];
418 while (run
!= ENDREF
)
420 struct hashentry
*runp
= (struct hashentry
*) (data
+ run
);
422 = (struct datahead
*) (data
+ runp
->packet
);
426 /* We need the list only for debugging but it is
427 more costly to avoid creating the list than
429 runp
->dellist
= head
;
432 /* No need for an atomic operation, we have the
434 --table
->head
->nentries
;
436 run
= *old
= runp
->next
;
450 pthread_rwlock_unlock (&table
->lock
);
452 /* Make sure the data is saved to disk. */
453 if (table
->persistent
)
455 data
+ table
->head
->first_free
- (char *) table
->head
,
458 /* One extra pass if we do debugging. */
459 if (__builtin_expect (debug_level
> 0, 0))
461 struct hashentry
*runp
= head
;
465 char buf
[INET6_ADDRSTRLEN
];
468 if (runp
->type
== GETHOSTBYADDR
|| runp
->type
== GETHOSTBYADDRv6
)
470 inet_ntop (runp
->type
== GETHOSTBYADDR
? AF_INET
: AF_INET6
,
471 data
+ runp
->key
, buf
, sizeof (buf
));
475 str
= data
+ runp
->key
;
477 dbg_log ("remove %s entry \"%s\"", serv2str
[runp
->type
], str
);
479 runp
= runp
->dellist
;
484 /* Run garbage collection if any entry has been removed or replaced. */
488 pthread_mutex_unlock (&table
->prunelock
);