1 /* Copyright (c) 1998, 1999, 2003-2007, 2008 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 as published
7 by the Free Software Foundation; version 2 of the License, or
8 (at your option) any later version.
10 This program 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 #include <arpa/inet.h>
29 #include <rpcsvc/nis.h>
31 #include <sys/param.h>
39 /* Wrapper functions with error checking for standard functions. */
40 extern void *xcalloc (size_t n
, size_t s
);
43 /* Number of times a value is reloaded without being used. UINT_MAX
45 unsigned int reload_count
= DEFAULT_RELOAD_LIMIT
;
48 static void (*const readdfcts
[LASTREQ
]) (struct database_dyn
*,
52 [GETPWBYNAME
] = readdpwbyname
,
53 [GETPWBYUID
] = readdpwbyuid
,
54 [GETGRBYNAME
] = readdgrbyname
,
55 [GETGRBYGID
] = readdgrbygid
,
56 [GETHOSTBYNAME
] = readdhstbyname
,
57 [GETHOSTBYNAMEv6
] = readdhstbynamev6
,
58 [GETHOSTBYADDR
] = readdhstbyaddr
,
59 [GETHOSTBYADDRv6
] = readdhstbyaddrv6
,
61 [INITGROUPS
] = readdinitgroups
,
62 [GETSERVBYNAME
] = readdservbyname
,
63 [GETSERVBYPORT
] = readdservbyport
67 /* Search the cache for a matching entry and return it when found. If
68 this fails search the negative cache and return (void *) -1 if this
69 search was successful. Otherwise return NULL.
71 This function must be called with the read-lock held. */
73 cache_search (request_type type
, void *key
, size_t len
,
74 struct database_dyn
*table
, uid_t owner
)
76 unsigned long int hash
= __nis_hash (key
, len
) % table
->head
->module
;
78 unsigned long int nsearched
= 0;
79 struct datahead
*result
= NULL
;
81 ref_t work
= table
->head
->array
[hash
];
82 while (work
!= ENDREF
)
86 struct hashentry
*here
= (struct hashentry
*) (table
->data
+ work
);
88 if (type
== here
->type
&& len
== here
->len
89 && memcmp (key
, table
->data
+ here
->key
, len
) == 0
90 && here
->owner
== owner
)
92 /* We found the entry. Increment the appropriate counter. */
94 = (struct datahead
*) (table
->data
+ here
->packet
);
96 /* See whether we must ignore the entry. */
99 /* We do not synchronize the memory here. The statistics
100 data is not crucial, we synchronize only once in a while
101 in the cleanup threads. */
103 ++table
->head
->neghit
;
106 ++table
->head
->poshit
;
108 if (dh
->nreloads
!= 0)
120 if (nsearched
> table
->head
->maxnsearched
)
121 table
->head
->maxnsearched
= nsearched
;
126 /* Add a new entry to the cache. The return value is zero if the function
129 This function must be called with the read-lock held.
131 We modify the table but we nevertheless only acquire a read-lock.
132 This is ok since we use operations which would be safe even without
133 locking, given that the `prune_cache' function never runs. Using
134 the readlock reduces the chance of conflicts. */
136 cache_add (int type
, const void *key
, size_t len
, struct datahead
*packet
,
137 bool first
, struct database_dyn
*table
,
140 if (__builtin_expect (debug_level
>= 2, 0))
143 char buf
[INET6_ADDRSTRLEN
+ 1];
144 if (type
== GETHOSTBYADDR
|| type
== GETHOSTBYADDRv6
)
145 str
= inet_ntop (type
== GETHOSTBYADDR
? AF_INET
: AF_INET6
,
146 key
, buf
, sizeof (buf
));
150 dbg_log (_("add new entry \"%s\" of type %s for %s to cache%s"),
151 str
, serv2str
[type
], dbnames
[table
- dbs
],
152 first
? _(" (first)") : "");
155 unsigned long int hash
= __nis_hash (key
, len
) % table
->head
->module
;
156 struct hashentry
*newp
;
158 newp
= mempool_alloc (table
, sizeof (struct hashentry
), IDX_record_data
);
159 /* If we cannot allocate memory, just do not do anything. */
162 ++table
->head
->addfailed
;
164 /* Mark the in-flight memory as unused. */
165 for (enum in_flight idx
= 0; idx
< IDX_record_data
; ++idx
)
166 mem_in_flight
.block
[idx
].dbidx
= -1;
174 newp
->key
= (char *) key
- table
->data
;
175 assert (newp
->key
+ newp
->len
<= table
->head
->first_free
);
177 newp
->packet
= (char *) packet
- table
->data
;
179 /* Put the new entry in the first position. */
181 newp
->next
= table
->head
->array
[hash
];
182 while (atomic_compare_and_exchange_bool_acq (&table
->head
->array
[hash
],
183 (ref_t
) ((char *) newp
185 (ref_t
) newp
->next
));
187 /* Update the statistics. */
188 if (packet
->notfound
)
189 ++table
->head
->negmiss
;
191 ++table
->head
->posmiss
;
193 /* We depend on this value being correct and at least as high as the
194 real number of entries. */
195 atomic_increment (&table
->head
->nentries
);
197 /* It does not matter that we are not loading the just increment
198 value, this is just for statistics. */
199 unsigned long int nentries
= table
->head
->nentries
;
200 if (nentries
> table
->head
->maxnentries
)
201 table
->head
->maxnentries
= nentries
;
203 if (table
->persistent
)
205 msync ((void *) table
->head
,
206 (char *) &table
->head
->array
[hash
] - (char *) table
->head
207 + sizeof (ref_t
), MS_ASYNC
);
209 /* Perhaps the prune thread for the data is not running in a long
210 time. Wake it if necessary. */
211 time_t next_wakeup
= table
->wakeup_time
;
212 while (next_wakeup
+ CACHE_PRUNE_INTERVAL
> packet
->timeout
)
213 if (atomic_compare_and_exchange_bool_acq (&table
->wakeup_time
,
217 pthread_cond_signal (&table
->prune_cond
);
221 next_wakeup
= table
->wakeup_time
;
223 /* Mark the in-flight memory as unused. */
224 for (enum in_flight idx
= 0; idx
< IDX_last
; ++idx
)
225 mem_in_flight
.block
[idx
].dbidx
= -1;
230 /* Walk through the table and remove all entries which lifetime ended.
232 We have a problem here. To actually remove the entries we must get
233 the write-lock. But since we want to keep the time we have the
234 lock as short as possible we cannot simply acquire the lock when we
235 start looking for timedout entries.
237 Therefore we do it in two stages: first we look for entries which
238 must be invalidated and remember them. Then we get the lock and
239 actually remove them. This is complicated by the way we have to
240 free the data structures since some hash table entries share the same
243 prune_cache (struct database_dyn
*table
, time_t now
, int fd
)
245 size_t cnt
= table
->head
->module
;
247 /* If this table is not actually used don't do anything. */
252 /* Reply to the INVALIDATE initiator. */
254 writeall (fd
, &resp
, sizeof (resp
));
257 /* No need to do this again anytime soon. */
261 /* If we check for the modification of the underlying file we invalidate
262 the entries also in this case. */
263 if (table
->check_file
&& now
!= LONG_MAX
)
267 if (stat64 (table
->filename
, &st
) < 0)
270 /* We cannot stat() the file, disable file checking if the
271 file does not exist. */
272 dbg_log (_("cannot stat() file `%s': %s"),
273 table
->filename
, strerror_r (errno
, buf
, sizeof (buf
)));
275 table
->check_file
= 0;
279 if (st
.st_mtime
!= table
->file_mtime
)
281 /* The file changed. Invalidate all entries. */
283 table
->file_mtime
= st
.st_mtime
;
288 /* We run through the table and find values which are not valid anymore.
290 Note that for the initial step, finding the entries to be removed,
291 we don't need to get any lock. It is at all timed assured that the
292 linked lists are set up correctly and that no second thread prunes
295 size_t memory_needed
= cnt
* sizeof (bool);
296 bool mark_use_alloca
;
297 if (__builtin_expect (memory_needed
<= MAX_STACK_USE
, 1))
299 mark
= alloca (cnt
* sizeof (bool));
300 memset (mark
, '\0', memory_needed
);
301 mark_use_alloca
= true;
305 mark
= xcalloc (1, memory_needed
);
306 mark_use_alloca
= false;
308 size_t first
= cnt
+ 1;
310 char *const data
= table
->data
;
313 if (__builtin_expect (debug_level
> 2, 0))
314 dbg_log (_("pruning %s cache; time %ld"),
315 dbnames
[table
- dbs
], (long int) now
);
317 #define NO_TIMEOUT LONG_MAX
318 time_t next_timeout
= NO_TIMEOUT
;
321 ref_t run
= table
->head
->array
[--cnt
];
323 while (run
!= ENDREF
)
325 struct hashentry
*runp
= (struct hashentry
*) (data
+ run
);
326 struct datahead
*dh
= (struct datahead
*) (data
+ runp
->packet
);
328 /* Some debug support. */
329 if (__builtin_expect (debug_level
> 2, 0))
331 char buf
[INET6_ADDRSTRLEN
];
334 if (runp
->type
== GETHOSTBYADDR
|| runp
->type
== GETHOSTBYADDRv6
)
336 inet_ntop (runp
->type
== GETHOSTBYADDR
? AF_INET
: AF_INET6
,
337 data
+ runp
->key
, buf
, sizeof (buf
));
341 str
= data
+ runp
->key
;
343 dbg_log (_("considering %s entry \"%s\", timeout %" PRIu64
),
344 serv2str
[runp
->type
], str
, dh
->timeout
);
347 /* Check whether the entry timed out. */
348 if (dh
->timeout
< now
)
350 /* This hash bucket could contain entries which need to
354 first
= MIN (first
, cnt
);
355 last
= MAX (last
, cnt
);
357 /* We only have to look at the data of the first entries
358 since the count information is kept in the data part
363 /* At this point there are two choices: we reload the
364 value or we discard it. Do not change NRELOADS if
365 we never not reload the record. */
366 if ((reload_count
!= UINT_MAX
367 && __builtin_expect (dh
->nreloads
>= reload_count
, 0))
368 /* We always remove negative entries. */
370 /* Discard everything if the user explicitly
374 /* Remove the value. */
377 /* We definitely have some garbage entries now. */
382 /* Reload the value. We do this only for the
383 initially used key, not the additionally
384 added derived value. */
385 assert (runp
->type
< LASTREQ
386 && readdfcts
[runp
->type
] != NULL
);
388 readdfcts
[runp
->type
] (table
, runp
, dh
);
390 /* If the entry has been replaced, we might need
399 next_timeout
= MIN (next_timeout
, dh
->timeout
);
407 if (__builtin_expect (fd
!= -1, 0))
409 /* Reply to the INVALIDATE initiator that the cache has been
412 writeall (fd
, &resp
, sizeof (resp
));
417 struct hashentry
*head
= NULL
;
419 /* Now we have to get the write lock since we are about to modify
421 if (__builtin_expect (pthread_rwlock_trywrlock (&table
->lock
) != 0, 0))
423 ++table
->head
->wrlockdelayed
;
424 pthread_rwlock_wrlock (&table
->lock
);
427 while (first
<= last
)
431 ref_t
*old
= &table
->head
->array
[first
];
432 ref_t run
= table
->head
->array
[first
];
434 while (run
!= ENDREF
)
436 struct hashentry
*runp
= (struct hashentry
*) (data
+ run
);
438 = (struct datahead
*) (data
+ runp
->packet
);
442 /* We need the list only for debugging but it is
443 more costly to avoid creating the list than
445 runp
->dellist
= head
;
448 /* No need for an atomic operation, we have the
450 --table
->head
->nentries
;
452 run
= *old
= runp
->next
;
466 pthread_rwlock_unlock (&table
->lock
);
468 /* Make sure the data is saved to disk. */
469 if (table
->persistent
)
471 data
+ table
->head
->first_free
- (char *) table
->head
,
474 /* One extra pass if we do debugging. */
475 if (__builtin_expect (debug_level
> 0, 0))
477 struct hashentry
*runp
= head
;
481 char buf
[INET6_ADDRSTRLEN
];
484 if (runp
->type
== GETHOSTBYADDR
|| runp
->type
== GETHOSTBYADDRv6
)
486 inet_ntop (runp
->type
== GETHOSTBYADDR
? AF_INET
: AF_INET6
,
487 data
+ runp
->key
, buf
, sizeof (buf
));
491 str
= data
+ runp
->key
;
493 dbg_log ("remove %s entry \"%s\"", serv2str
[runp
->type
], str
);
495 runp
= runp
->dellist
;
500 if (__builtin_expect (! mark_use_alloca
, 0))
503 /* Run garbage collection if any entry has been removed or replaced. */
507 /* If there is no entry in the database and we therefore have no new
508 timeout value, tell the caller to wake up in 24 hours. */
509 return next_timeout
== NO_TIMEOUT
? 24 * 60 * 60 : next_timeout
- now
;