1 /* Copyright (c) 1998-2014 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, see <http://www.gnu.org/licenses/>. */
27 #include <arpa/inet.h>
28 #include <rpcsvc/nis.h>
30 #include <sys/param.h>
38 /* Wrapper functions with error checking for standard functions. */
39 extern void *xcalloc (size_t n
, size_t s
);
42 /* Number of times a value is reloaded without being used. UINT_MAX
44 unsigned int reload_count
= DEFAULT_RELOAD_LIMIT
;
47 static time_t (*const readdfcts
[LASTREQ
]) (struct database_dyn
*,
51 [GETPWBYNAME
] = readdpwbyname
,
52 [GETPWBYUID
] = readdpwbyuid
,
53 [GETGRBYNAME
] = readdgrbyname
,
54 [GETGRBYGID
] = readdgrbygid
,
55 [GETHOSTBYNAME
] = readdhstbyname
,
56 [GETHOSTBYNAMEv6
] = readdhstbynamev6
,
57 [GETHOSTBYADDR
] = readdhstbyaddr
,
58 [GETHOSTBYADDRv6
] = readdhstbyaddrv6
,
60 [INITGROUPS
] = readdinitgroups
,
61 [GETSERVBYNAME
] = readdservbyname
,
62 [GETSERVBYPORT
] = readdservbyport
,
63 [GETNETGRENT
] = readdgetnetgrent
,
64 [INNETGR
] = readdinnetgr
68 /* Search the cache for a matching entry and return it when found. If
69 this fails search the negative cache and return (void *) -1 if this
70 search was successful. Otherwise return NULL.
72 This function must be called with the read-lock held. */
74 cache_search (request_type type
, const void *key
, size_t len
,
75 struct database_dyn
*table
, uid_t owner
)
77 unsigned long int hash
= __nis_hash (key
, len
) % table
->head
->module
;
79 unsigned long int nsearched
= 0;
80 struct datahead
*result
= NULL
;
82 ref_t work
= table
->head
->array
[hash
];
83 while (work
!= ENDREF
)
87 struct hashentry
*here
= (struct hashentry
*) (table
->data
+ work
);
89 if (type
== here
->type
&& len
== here
->len
90 && memcmp (key
, table
->data
+ here
->key
, len
) == 0
91 && here
->owner
== owner
)
93 /* We found the entry. Increment the appropriate counter. */
95 = (struct datahead
*) (table
->data
+ here
->packet
);
97 /* See whether we must ignore the entry. */
100 /* We do not synchronize the memory here. The statistics
101 data is not crucial, we synchronize only once in a while
102 in the cleanup threads. */
104 ++table
->head
->neghit
;
107 ++table
->head
->poshit
;
109 if (dh
->nreloads
!= 0)
121 if (nsearched
> table
->head
->maxnsearched
)
122 table
->head
->maxnsearched
= nsearched
;
127 /* Add a new entry to the cache. The return value is zero if the function
130 This function must be called with the read-lock held.
132 We modify the table but we nevertheless only acquire a read-lock.
133 This is ok since we use operations which would be safe even without
134 locking, given that the `prune_cache' function never runs. Using
135 the readlock reduces the chance of conflicts. */
137 cache_add (int type
, const void *key
, size_t len
, struct datahead
*packet
,
138 bool first
, struct database_dyn
*table
,
139 uid_t owner
, bool prune_wakeup
)
141 if (__glibc_unlikely (debug_level
>= 2))
144 char buf
[INET6_ADDRSTRLEN
+ 1];
145 if (type
== GETHOSTBYADDR
|| type
== GETHOSTBYADDRv6
)
146 str
= inet_ntop (type
== GETHOSTBYADDR
? AF_INET
: AF_INET6
,
147 key
, buf
, sizeof (buf
));
151 dbg_log (_("add new entry \"%s\" of type %s for %s to cache%s"),
152 str
, serv2str
[type
], dbnames
[table
- dbs
],
153 first
? _(" (first)") : "");
156 unsigned long int hash
= __nis_hash (key
, len
) % table
->head
->module
;
157 struct hashentry
*newp
;
159 newp
= mempool_alloc (table
, sizeof (struct hashentry
), 0);
160 /* If we cannot allocate memory, just do not do anything. */
163 /* If necessary mark the entry as unusable so that lookups will
166 packet
->usable
= false;
174 newp
->key
= (char *) key
- table
->data
;
175 assert (newp
->key
+ newp
->len
<= table
->head
->first_free
);
177 newp
->packet
= (char *) packet
- table
->data
;
178 assert ((newp
->packet
& BLOCK_ALIGN_M1
) == 0);
180 /* Put the new entry in the first position. */
182 newp
->next
= table
->head
->array
[hash
];
183 while (atomic_compare_and_exchange_bool_rel (&table
->head
->array
[hash
],
184 (ref_t
) ((char *) newp
186 (ref_t
) newp
->next
));
188 /* Update the statistics. */
189 if (packet
->notfound
)
190 ++table
->head
->negmiss
;
192 ++table
->head
->posmiss
;
194 /* We depend on this value being correct and at least as high as the
195 real number of entries. */
196 atomic_increment (&table
->head
->nentries
);
198 /* It does not matter that we are not loading the just increment
199 value, this is just for statistics. */
200 unsigned long int nentries
= table
->head
->nentries
;
201 if (nentries
> table
->head
->maxnentries
)
202 table
->head
->maxnentries
= nentries
;
204 if (table
->persistent
)
206 msync ((void *) table
->head
,
207 (char *) &table
->head
->array
[hash
] - (char *) table
->head
208 + sizeof (ref_t
), MS_ASYNC
);
210 /* We do not have to worry about the pruning thread if we are
211 re-adding the data since this is done by the pruning thread. We
212 also do not have to do anything in case this is not the first
213 time the data is entered since different data heads all have the
215 if (first
&& prune_wakeup
)
217 /* Perhaps the prune thread for the table is not running in a long
218 time. Wake it if necessary. */
219 pthread_mutex_lock (&table
->prune_lock
);
220 time_t next_wakeup
= table
->wakeup_time
;
221 bool do_wakeup
= false;
222 if (next_wakeup
> packet
->timeout
+ CACHE_PRUNE_INTERVAL
)
224 table
->wakeup_time
= packet
->timeout
;
227 pthread_mutex_unlock (&table
->prune_lock
);
229 pthread_cond_signal (&table
->prune_cond
);
235 /* Walk through the table and remove all entries which lifetime ended.
237 We have a problem here. To actually remove the entries we must get
238 the write-lock. But since we want to keep the time we have the
239 lock as short as possible we cannot simply acquire the lock when we
240 start looking for timedout entries.
242 Therefore we do it in two stages: first we look for entries which
243 must be invalidated and remember them. Then we get the lock and
244 actually remove them. This is complicated by the way we have to
245 free the data structures since some hash table entries share the same
248 prune_cache (struct database_dyn
*table
, time_t now
, int fd
)
250 size_t cnt
= table
->head
->module
;
252 /* If this table is not actually used don't do anything. */
257 /* Reply to the INVALIDATE initiator. */
259 writeall (fd
, &resp
, sizeof (resp
));
262 /* No need to do this again anytime soon. */
266 /* If we check for the modification of the underlying file we invalidate
267 the entries also in this case. */
268 if (table
->check_file
&& now
!= LONG_MAX
)
270 struct traced_file
*runp
= table
->traced_files
;
275 if (runp
->inotify_descr
== -1)
280 if (stat64 (runp
->fname
, &st
) < 0)
283 /* We cannot stat() the file, disable file checking if the
284 file does not exist. */
285 dbg_log (_("cannot stat() file `%s': %s"),
286 runp
->fname
, strerror_r (errno
, buf
, sizeof (buf
)));
288 table
->check_file
= 0;
292 if (st
.st_mtime
!= table
->file_mtime
)
294 /* The file changed. Invalidate all entries. */
296 table
->file_mtime
= st
.st_mtime
;
305 /* We run through the table and find values which are not valid anymore.
307 Note that for the initial step, finding the entries to be removed,
308 we don't need to get any lock. It is at all timed assured that the
309 linked lists are set up correctly and that no second thread prunes
312 size_t memory_needed
= cnt
* sizeof (bool);
313 bool mark_use_alloca
;
314 if (__glibc_likely (memory_needed
<= MAX_STACK_USE
))
316 mark
= alloca (cnt
* sizeof (bool));
317 memset (mark
, '\0', memory_needed
);
318 mark_use_alloca
= true;
322 mark
= xcalloc (1, memory_needed
);
323 mark_use_alloca
= false;
325 size_t first
= cnt
+ 1;
327 char *const data
= table
->data
;
330 if (__glibc_unlikely (debug_level
> 2))
331 dbg_log (_("pruning %s cache; time %ld"),
332 dbnames
[table
- dbs
], (long int) now
);
334 #define NO_TIMEOUT LONG_MAX
335 time_t next_timeout
= NO_TIMEOUT
;
338 ref_t run
= table
->head
->array
[--cnt
];
340 while (run
!= ENDREF
)
342 struct hashentry
*runp
= (struct hashentry
*) (data
+ run
);
343 struct datahead
*dh
= (struct datahead
*) (data
+ runp
->packet
);
345 /* Some debug support. */
346 if (__glibc_unlikely (debug_level
> 2))
348 char buf
[INET6_ADDRSTRLEN
];
351 if (runp
->type
== GETHOSTBYADDR
|| runp
->type
== GETHOSTBYADDRv6
)
353 inet_ntop (runp
->type
== GETHOSTBYADDR
? AF_INET
: AF_INET6
,
354 data
+ runp
->key
, buf
, sizeof (buf
));
358 str
= data
+ runp
->key
;
360 dbg_log (_("considering %s entry \"%s\", timeout %" PRIu64
),
361 serv2str
[runp
->type
], str
, dh
->timeout
);
364 /* Check whether the entry timed out. */
365 if (dh
->timeout
< now
)
367 /* This hash bucket could contain entries which need to
371 first
= MIN (first
, cnt
);
372 last
= MAX (last
, cnt
);
374 /* We only have to look at the data of the first entries
375 since the count information is kept in the data part
380 /* At this point there are two choices: we reload the
381 value or we discard it. Do not change NRELOADS if
382 we never not reload the record. */
383 if ((reload_count
!= UINT_MAX
384 && __builtin_expect (dh
->nreloads
>= reload_count
, 0))
385 /* We always remove negative entries. */
387 /* Discard everything if the user explicitly
391 /* Remove the value. */
394 /* We definitely have some garbage entries now. */
399 /* Reload the value. We do this only for the
400 initially used key, not the additionally
401 added derived value. */
402 assert (runp
->type
< LASTREQ
403 && readdfcts
[runp
->type
] != NULL
);
405 time_t timeout
= readdfcts
[runp
->type
] (table
, runp
, dh
);
406 next_timeout
= MIN (next_timeout
, timeout
);
408 /* If the entry has been replaced, we might need
417 next_timeout
= MIN (next_timeout
, dh
->timeout
);
425 if (__glibc_unlikely (fd
!= -1))
427 /* Reply to the INVALIDATE initiator that the cache has been
430 writeall (fd
, &resp
, sizeof (resp
));
435 struct hashentry
*head
= NULL
;
437 /* Now we have to get the write lock since we are about to modify
439 if (__glibc_unlikely (pthread_rwlock_trywrlock (&table
->lock
) != 0))
441 ++table
->head
->wrlockdelayed
;
442 pthread_rwlock_wrlock (&table
->lock
);
445 while (first
<= last
)
449 ref_t
*old
= &table
->head
->array
[first
];
450 ref_t run
= table
->head
->array
[first
];
452 assert (run
!= ENDREF
);
455 struct hashentry
*runp
= (struct hashentry
*) (data
+ run
);
457 = (struct datahead
*) (data
+ runp
->packet
);
461 /* We need the list only for debugging but it is
462 more costly to avoid creating the list than
464 runp
->dellist
= head
;
467 /* No need for an atomic operation, we have the
469 --table
->head
->nentries
;
471 run
= *old
= runp
->next
;
479 while (run
!= ENDREF
);
486 pthread_rwlock_unlock (&table
->lock
);
488 /* Make sure the data is saved to disk. */
489 if (table
->persistent
)
491 data
+ table
->head
->first_free
- (char *) table
->head
,
494 /* One extra pass if we do debugging. */
495 if (__glibc_unlikely (debug_level
> 0))
497 struct hashentry
*runp
= head
;
501 char buf
[INET6_ADDRSTRLEN
];
504 if (runp
->type
== GETHOSTBYADDR
|| runp
->type
== GETHOSTBYADDRv6
)
506 inet_ntop (runp
->type
== GETHOSTBYADDR
? AF_INET
: AF_INET6
,
507 data
+ runp
->key
, buf
, sizeof (buf
));
511 str
= data
+ runp
->key
;
513 dbg_log ("remove %s entry \"%s\"", serv2str
[runp
->type
], str
);
515 runp
= runp
->dellist
;
520 if (__glibc_unlikely (! mark_use_alloca
))
523 /* Run garbage collection if any entry has been removed or replaced. */
527 /* If there is no entry in the database and we therefore have no new
528 timeout value, tell the caller to wake up in 24 hours. */
529 return next_timeout
== NO_TIMEOUT
? 24 * 60 * 60 : next_timeout
- now
;