[BZ #4514]
[glibc.git] / nscd / hstcache.c
blobad2e323eace61a724662fb3a98a1f0ebad862e65
1 /* Cache handling for host lookup.
2 Copyright (C) 1998-2005, 2006, 2007 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License version 2 as
8 published by the Free Software Foundation.
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. */
19 #include <alloca.h>
20 #include <assert.h>
21 #include <errno.h>
22 #include <error.h>
23 #include <libintl.h>
24 #include <netdb.h>
25 #include <stdbool.h>
26 #include <stddef.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <time.h>
31 #include <unistd.h>
32 #include <arpa/inet.h>
33 #include <arpa/nameser.h>
34 #include <sys/mman.h>
35 #include <stackinfo.h>
37 #include "nscd.h"
38 #include "dbg_log.h"
39 #ifdef HAVE_SENDFILE
40 # include <kernel-features.h>
41 #endif
44 /* This is the standard reply in case the service is disabled. */
45 static const hst_response_header disabled =
47 .version = NSCD_VERSION,
48 .found = -1,
49 .h_name_len = 0,
50 .h_aliases_cnt = 0,
51 .h_addrtype = -1,
52 .h_length = -1,
53 .h_addr_list_cnt = 0,
54 .error = NETDB_INTERNAL
57 /* This is the struct describing how to write this record. */
58 const struct iovec hst_iov_disabled =
60 .iov_base = (void *) &disabled,
61 .iov_len = sizeof (disabled)
65 /* This is the standard reply in case we haven't found the dataset. */
66 static const hst_response_header notfound =
68 .version = NSCD_VERSION,
69 .found = 0,
70 .h_name_len = 0,
71 .h_aliases_cnt = 0,
72 .h_addrtype = -1,
73 .h_length = -1,
74 .h_addr_list_cnt = 0,
75 .error = HOST_NOT_FOUND
79 static void
80 cache_addhst (struct database_dyn *db, int fd, request_header *req,
81 const void *key, struct hostent *hst, uid_t owner,
82 struct hashentry *he, struct datahead *dh, int errval)
84 ssize_t total;
85 ssize_t written;
86 time_t t = time (NULL);
88 /* We allocate all data in one memory block: the iov vector,
89 the response header and the dataset itself. */
90 struct dataset
92 struct datahead head;
93 hst_response_header resp;
94 char strdata[0];
95 } *dataset;
97 assert (offsetof (struct dataset, resp) == offsetof (struct datahead, data));
99 if (hst == NULL)
101 if (he != NULL && errval == EAGAIN)
103 /* If we have an old record available but cannot find one
104 now because the service is not available we keep the old
105 record and make sure it does not get removed. */
106 if (reload_count != UINT_MAX)
107 /* Do not reset the value if we never not reload the record. */
108 dh->nreloads = reload_count - 1;
110 written = total = 0;
112 else
114 /* We have no data. This means we send the standard reply for this
115 case. */
116 written = total = sizeof (notfound);
118 if (fd != -1)
119 written = TEMP_FAILURE_RETRY (send (fd, &notfound, total,
120 MSG_NOSIGNAL));
122 dataset = mempool_alloc (db, sizeof (struct dataset) + req->key_len);
123 /* If we cannot permanently store the result, so be it. */
124 if (dataset != NULL)
126 dataset->head.allocsize = sizeof (struct dataset) + req->key_len;
127 dataset->head.recsize = total;
128 dataset->head.notfound = true;
129 dataset->head.nreloads = 0;
130 dataset->head.usable = true;
132 /* Compute the timeout time. */
133 dataset->head.timeout = t + db->negtimeout;
135 /* This is the reply. */
136 memcpy (&dataset->resp, &notfound, total);
138 /* Copy the key data. */
139 memcpy (dataset->strdata, key, req->key_len);
141 /* If necessary, we also propagate the data to disk. */
142 if (db->persistent)
144 // XXX async OK?
145 uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
146 msync ((void *) pval,
147 ((uintptr_t) dataset & pagesize_m1)
148 + sizeof (struct dataset) + req->key_len, MS_ASYNC);
151 /* Now get the lock to safely insert the records. */
152 pthread_rwlock_rdlock (&db->lock);
154 if (cache_add (req->type, &dataset->strdata, req->key_len,
155 &dataset->head, true, db, owner) < 0)
156 /* Ensure the data can be recovered. */
157 dataset->head.usable = false;
159 pthread_rwlock_unlock (&db->lock);
161 /* Mark the old entry as obsolete. */
162 if (dh != NULL)
163 dh->usable = false;
165 else
166 ++db->head->addfailed;
169 else
171 /* Determine the I/O structure. */
172 size_t h_name_len = strlen (hst->h_name) + 1;
173 size_t h_aliases_cnt;
174 uint32_t *h_aliases_len;
175 size_t h_addr_list_cnt;
176 int addr_list_type;
177 char *addresses;
178 char *aliases;
179 char *key_copy = NULL;
180 char *cp;
181 size_t cnt;
183 /* Determine the number of aliases. */
184 h_aliases_cnt = 0;
185 for (cnt = 0; hst->h_aliases[cnt] != NULL; ++cnt)
186 ++h_aliases_cnt;
187 /* Determine the length of all aliases. */
188 h_aliases_len = (uint32_t *) alloca (h_aliases_cnt * sizeof (uint32_t));
189 total = 0;
190 for (cnt = 0; cnt < h_aliases_cnt; ++cnt)
192 h_aliases_len[cnt] = strlen (hst->h_aliases[cnt]) + 1;
193 total += h_aliases_len[cnt];
196 /* Determine the number of addresses. */
197 h_addr_list_cnt = 0;
198 for (cnt = 0; hst->h_addr_list[cnt]; ++cnt)
199 ++h_addr_list_cnt;
201 if (h_addr_list_cnt == 0)
202 /* Invalid entry. */
203 return;
205 total += (sizeof (struct dataset)
206 + h_name_len
207 + h_aliases_cnt * sizeof (uint32_t)
208 + h_addr_list_cnt * hst->h_length);
209 written = total;
211 /* If we refill the cache, first assume the reconrd did not
212 change. Allocate memory on the cache since it is likely
213 discarded anyway. If it turns out to be necessary to have a
214 new record we can still allocate real memory. */
215 bool alloca_used = false;
216 dataset = NULL;
218 /* If the record contains more than one IP address (used for
219 load balancing etc) don't cache the entry. This is something
220 the current cache handling cannot handle and it is more than
221 questionable whether it is worthwhile complicating the cache
222 handling just for handling such a special case. */
223 if (he == NULL && hst->h_addr_list[1] == NULL)
225 dataset = (struct dataset *) mempool_alloc (db,
226 total + req->key_len);
227 if (dataset == NULL)
228 ++db->head->addfailed;
231 if (dataset == NULL)
233 /* We cannot permanently add the result in the moment. But
234 we can provide the result as is. Store the data in some
235 temporary memory. */
236 dataset = (struct dataset *) alloca (total + req->key_len);
238 /* We cannot add this record to the permanent database. */
239 alloca_used = true;
242 dataset->head.allocsize = total + req->key_len;
243 dataset->head.recsize = total - offsetof (struct dataset, resp);
244 dataset->head.notfound = false;
245 dataset->head.nreloads = he == NULL ? 0 : (dh->nreloads + 1);
246 dataset->head.usable = true;
248 /* Compute the timeout time. */
249 dataset->head.timeout = t + db->postimeout;
251 dataset->resp.version = NSCD_VERSION;
252 dataset->resp.found = 1;
253 dataset->resp.h_name_len = h_name_len;
254 dataset->resp.h_aliases_cnt = h_aliases_cnt;
255 dataset->resp.h_addrtype = hst->h_addrtype;
256 dataset->resp.h_length = hst->h_length;
257 dataset->resp.h_addr_list_cnt = h_addr_list_cnt;
258 dataset->resp.error = NETDB_SUCCESS;
260 cp = dataset->strdata;
262 cp = mempcpy (cp, hst->h_name, h_name_len);
263 cp = mempcpy (cp, h_aliases_len, h_aliases_cnt * sizeof (uint32_t));
265 /* The normal addresses first. */
266 addresses = cp;
267 for (cnt = 0; cnt < h_addr_list_cnt; ++cnt)
268 cp = mempcpy (cp, hst->h_addr_list[cnt], hst->h_length);
270 /* Then the aliases. */
271 aliases = cp;
272 for (cnt = 0; cnt < h_aliases_cnt; ++cnt)
273 cp = mempcpy (cp, hst->h_aliases[cnt], h_aliases_len[cnt]);
275 assert (cp
276 == dataset->strdata + total - offsetof (struct dataset,
277 strdata));
279 /* If we are adding a GETHOSTBYNAME{,v6} entry we must be prepared
280 that the answer we get from the NSS does not contain the key
281 itself. This is the case if the resolver is used and the name
282 is extended by the domainnames from /etc/resolv.conf. Therefore
283 we explicitly add the name here. */
284 key_copy = memcpy (cp, key, req->key_len);
286 /* Now we can determine whether on refill we have to create a new
287 record or not. */
288 if (he != NULL)
290 assert (fd == -1);
292 if (total + req->key_len == dh->allocsize
293 && total - offsetof (struct dataset, resp) == dh->recsize
294 && memcmp (&dataset->resp, dh->data,
295 dh->allocsize - offsetof (struct dataset, resp)) == 0)
297 /* The data has not changed. We will just bump the
298 timeout value. Note that the new record has been
299 allocated on the stack and need not be freed. */
300 dh->timeout = dataset->head.timeout;
301 ++dh->nreloads;
303 else
305 /* We have to create a new record. Just allocate
306 appropriate memory and copy it. */
307 struct dataset *newp
308 = (struct dataset *) mempool_alloc (db, total + req->key_len);
309 if (newp != NULL)
311 /* Adjust pointers into the memory block. */
312 addresses = (char *) newp + (addresses - (char *) dataset);
313 aliases = (char *) newp + (aliases - (char *) dataset);
314 assert (key_copy != NULL);
315 key_copy = (char *) newp + (key_copy - (char *) dataset);
317 dataset = memcpy (newp, dataset, total + req->key_len);
318 alloca_used = false;
321 /* Mark the old record as obsolete. */
322 dh->usable = false;
325 else
327 /* We write the dataset before inserting it to the database
328 since while inserting this thread might block and so would
329 unnecessarily keep the receiver waiting. */
330 assert (fd != -1);
332 #ifdef HAVE_SENDFILE
333 if (__builtin_expect (db->mmap_used, 1) && !alloca_used)
335 assert (db->wr_fd != -1);
336 assert ((char *) &dataset->resp > (char *) db->data);
337 assert ((char *) &dataset->resp - (char *) db->head
338 + total
339 <= (sizeof (struct database_pers_head)
340 + db->head->module * sizeof (ref_t)
341 + db->head->data_size));
342 written = sendfileall (fd, db->wr_fd,
343 (char *) &dataset->resp
344 - (char *) db->head, total);
345 # ifndef __ASSUME_SENDFILE
346 if (written == -1 && errno == ENOSYS)
347 goto use_write;
348 # endif
350 else
351 # ifndef __ASSUME_SENDFILE
352 use_write:
353 # endif
354 #endif
355 written = writeall (fd, &dataset->resp, total);
358 /* Add the record to the database. But only if it has not been
359 stored on the stack.
361 If the record contains more than one IP address (used for
362 load balancing etc) don't cache the entry. This is something
363 the current cache handling cannot handle and it is more than
364 questionable whether it is worthwhile complicating the cache
365 handling just for handling such a special case. */
366 if (! alloca_used)
368 /* If necessary, we also propagate the data to disk. */
369 if (db->persistent)
371 // XXX async OK?
372 uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
373 msync ((void *) pval,
374 ((uintptr_t) dataset & pagesize_m1)
375 + total + req->key_len, MS_ASYNC);
378 addr_list_type = (hst->h_length == NS_INADDRSZ
379 ? GETHOSTBYADDR : GETHOSTBYADDRv6);
381 /* Now get the lock to safely insert the records. */
382 pthread_rwlock_rdlock (&db->lock);
384 /* NB: the following code is really complicated. It has
385 seemlingly duplicated code paths which do the same. The
386 problem is that we always must add the hash table entry
387 with the FIRST flag set first. Otherwise we get dangling
388 pointers in case memory allocation fails. */
389 assert (hst->h_addr_list[1] == NULL);
391 /* Avoid adding names if more than one address is available. See
392 above for more info. */
393 assert (req->type == GETHOSTBYNAME
394 || req->type == GETHOSTBYNAMEv6
395 || req->type == GETHOSTBYADDR
396 || req->type == GETHOSTBYADDRv6);
398 if (cache_add (req->type, key_copy, req->key_len,
399 &dataset->head, true, db, owner) < 0)
400 /* Could not allocate memory. Make sure the
401 data gets discarded. */
402 dataset->head.usable = false;
404 pthread_rwlock_unlock (&db->lock);
408 if (__builtin_expect (written != total, 0) && debug_level > 0)
410 char buf[256];
411 dbg_log (_("short write in %s: %s"), __FUNCTION__,
412 strerror_r (errno, buf, sizeof (buf)));
417 static int
418 lookup (int type, void *key, struct hostent *resultbufp, char *buffer,
419 size_t buflen, struct hostent **hst)
421 if (type == GETHOSTBYNAME)
422 return __gethostbyname2_r (key, AF_INET, resultbufp, buffer, buflen, hst,
423 &h_errno);
424 if (type == GETHOSTBYNAMEv6)
425 return __gethostbyname2_r (key, AF_INET6, resultbufp, buffer, buflen, hst,
426 &h_errno);
427 if (type == GETHOSTBYADDR)
428 return __gethostbyaddr_r (key, NS_INADDRSZ, AF_INET, resultbufp, buffer,
429 buflen, hst, &h_errno);
430 return __gethostbyaddr_r (key, NS_IN6ADDRSZ, AF_INET6, resultbufp, buffer,
431 buflen, hst, &h_errno);
435 static void
436 addhstbyX (struct database_dyn *db, int fd, request_header *req,
437 void *key, uid_t uid, struct hashentry *he, struct datahead *dh)
439 /* Search for the entry matching the key. Please note that we don't
440 look again in the table whether the dataset is now available. We
441 simply insert it. It does not matter if it is in there twice. The
442 pruning function only will look at the timestamp. */
443 int buflen = 1024;
444 char *buffer = (char *) alloca (buflen);
445 struct hostent resultbuf;
446 struct hostent *hst;
447 bool use_malloc = false;
448 int errval = 0;
450 if (__builtin_expect (debug_level > 0, 0))
452 const char *str;
453 char buf[INET6_ADDRSTRLEN + 1];
454 if (req->type == GETHOSTBYNAME || req->type == GETHOSTBYNAMEv6)
455 str = key;
456 else
457 str = inet_ntop (req->type == GETHOSTBYADDR ? AF_INET : AF_INET6,
458 key, buf, sizeof (buf));
460 if (he == NULL)
461 dbg_log (_("Haven't found \"%s\" in hosts cache!"), (char *) str);
462 else
463 dbg_log (_("Reloading \"%s\" in hosts cache!"), (char *) str);
466 while (lookup (req->type, key, &resultbuf, buffer, buflen, &hst) != 0
467 && h_errno == NETDB_INTERNAL
468 && (errval = errno) == ERANGE)
470 errno = 0;
472 if (__builtin_expect (buflen > 32768, 0))
474 char *old_buffer = buffer;
475 buflen *= 2;
476 buffer = (char *) realloc (use_malloc ? buffer : NULL, buflen);
477 if (buffer == NULL)
479 /* We ran out of memory. We cannot do anything but
480 sending a negative response. In reality this should
481 never happen. */
482 hst = NULL;
483 buffer = old_buffer;
485 /* We set the error to indicate this is (possibly) a
486 temporary error and that it does not mean the entry
487 is not available at all. */
488 errval = EAGAIN;
489 break;
491 use_malloc = true;
493 else
494 /* Allocate a new buffer on the stack. If possible combine it
495 with the previously allocated buffer. */
496 buffer = (char *) extend_alloca (buffer, buflen, 2 * buflen);
499 cache_addhst (db, fd, req, key, hst, uid, he, dh,
500 h_errno == TRY_AGAIN ? errval : 0);
502 if (use_malloc)
503 free (buffer);
507 void
508 addhstbyname (struct database_dyn *db, int fd, request_header *req,
509 void *key, uid_t uid)
511 addhstbyX (db, fd, req, key, uid, NULL, NULL);
515 void
516 readdhstbyname (struct database_dyn *db, struct hashentry *he,
517 struct datahead *dh)
519 request_header req =
521 .type = GETHOSTBYNAME,
522 .key_len = he->len
525 addhstbyX (db, -1, &req, db->data + he->key, he->owner, he, dh);
529 void
530 addhstbyaddr (struct database_dyn *db, int fd, request_header *req,
531 void *key, uid_t uid)
533 addhstbyX (db, fd, req, key, uid, NULL, NULL);
537 void
538 readdhstbyaddr (struct database_dyn *db, struct hashentry *he,
539 struct datahead *dh)
541 request_header req =
543 .type = GETHOSTBYADDR,
544 .key_len = he->len
547 addhstbyX (db, -1, &req, db->data + he->key, he->owner, he, dh);
551 void
552 addhstbynamev6 (struct database_dyn *db, int fd, request_header *req,
553 void *key, uid_t uid)
555 addhstbyX (db, fd, req, key, uid, NULL, NULL);
559 void
560 readdhstbynamev6 (struct database_dyn *db, struct hashentry *he,
561 struct datahead *dh)
563 request_header req =
565 .type = GETHOSTBYNAMEv6,
566 .key_len = he->len
569 addhstbyX (db, -1, &req, db->data + he->key, he->owner, he, dh);
573 void
574 addhstbyaddrv6 (struct database_dyn *db, int fd, request_header *req,
575 void *key, uid_t uid)
577 addhstbyX (db, fd, req, key, uid, NULL, NULL);
581 void
582 readdhstbyaddrv6 (struct database_dyn *db, struct hashentry *he,
583 struct datahead *dh)
585 request_header req =
587 .type = GETHOSTBYADDRv6,
588 .key_len = he->len
591 addhstbyX (db, -1, &req, db->data + he->key, he->owner, he, dh);