2.8-2
[glibc.git] / nscd / aicache.c
blob3de84821a40abaae7926191242cadc34faa0225c
1 /* Cache handling for host lookup.
2 Copyright (C) 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@redhat.com>, 2004.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 #include <assert.h>
21 #include <errno.h>
22 #include <libintl.h>
23 #include <netdb.h>
24 #include <string.h>
25 #include <time.h>
26 #include <unistd.h>
27 #include <sys/mman.h>
29 #include "dbg_log.h"
30 #include "nscd.h"
31 #ifdef HAVE_SENDFILE
32 # include <kernel-features.h>
33 #endif
36 typedef enum nss_status (*nss_gethostbyname3_r)
37 (const char *name, int af, struct hostent *host,
38 char *buffer, size_t buflen, int *errnop,
39 int *h_errnop, int32_t *, char **);
40 typedef enum nss_status (*nss_getcanonname_r)
41 (const char *name, char *buffer, size_t buflen, char **result,
42 int *errnop, int *h_errnop);
45 static const ai_response_header notfound =
47 .version = NSCD_VERSION,
48 .found = 0,
49 .naddrs = 0,
50 .addrslen = 0,
51 .canonlen = 0,
52 .error = 0
56 static void
57 addhstaiX (struct database_dyn *db, int fd, request_header *req,
58 void *key, uid_t uid, struct hashentry *he, struct datahead *dh)
60 /* Search for the entry matching the key. Please note that we don't
61 look again in the table whether the dataset is now available. We
62 simply insert it. It does not matter if it is in there twice. The
63 pruning function only will look at the timestamp. */
65 /* We allocate all data in one memory block: the iov vector,
66 the response header and the dataset itself. */
67 struct dataset
69 struct datahead head;
70 ai_response_header resp;
71 char strdata[0];
72 } *dataset = NULL;
74 if (__builtin_expect (debug_level > 0, 0))
76 if (he == NULL)
77 dbg_log (_("Haven't found \"%s\" in hosts cache!"), (char *) key);
78 else
79 dbg_log (_("Reloading \"%s\" in hosts cache!"), (char *) key);
82 static service_user *hosts_database;
83 service_user *nip = NULL;
84 int no_more;
85 int rc6 = 0;
86 int rc4 = 0;
87 int herrno = 0;
89 if (hosts_database != NULL)
91 nip = hosts_database;
92 no_more = 0;
94 else
95 no_more = __nss_database_lookup ("hosts", NULL,
96 "dns [!UNAVAIL=return] files", &nip);
98 if (__res_maybe_init (&_res, 0) == -1)
99 no_more = 1;
101 /* If we are looking for both IPv4 and IPv6 address we don't want
102 the lookup functions to automatically promote IPv4 addresses to
103 IPv6 addresses. Currently this is decided by setting the
104 RES_USE_INET6 bit in _res.options. */
105 int old_res_options = _res.options;
106 _res.options &= ~RES_USE_INET6;
108 size_t tmpbuf6len = 512;
109 char *tmpbuf6 = alloca (tmpbuf6len);
110 size_t tmpbuf4len = 0;
111 char *tmpbuf4 = NULL;
112 char *canon = NULL;
113 int32_t ttl = INT32_MAX;
114 ssize_t total = 0;
115 char *key_copy = NULL;
116 bool alloca_used = false;
118 while (!no_more)
120 int status[2] = { NSS_STATUS_UNAVAIL, NSS_STATUS_UNAVAIL };
122 /* Prefer the function which also returns the TTL and canonical name. */
123 nss_gethostbyname3_r fct = __nss_lookup_function (nip,
124 "gethostbyname3_r");
125 if (fct == NULL)
126 fct = __nss_lookup_function (nip, "gethostbyname2_r");
128 if (fct != NULL)
130 struct hostent th[2];
132 /* Collect IPv6 information first. */
133 while (1)
135 rc6 = 0;
136 status[0] = DL_CALL_FCT (fct, (key, AF_INET6, &th[0], tmpbuf6,
137 tmpbuf6len, &rc6, &herrno,
138 &ttl, &canon));
139 if (rc6 != ERANGE || herrno != NETDB_INTERNAL)
140 break;
141 tmpbuf6 = extend_alloca (tmpbuf6, tmpbuf6len, 2 * tmpbuf6len);
144 if (rc6 != 0 && herrno == NETDB_INTERNAL)
145 goto out;
147 /* If the IPv6 lookup has been successful do not use the
148 buffer used in that lookup, use a new one. */
149 if (status[0] == NSS_STATUS_SUCCESS && rc6 == 0)
151 tmpbuf4len = 512;
152 tmpbuf4 = alloca (tmpbuf4len);
154 else
156 tmpbuf4len = tmpbuf6len;
157 tmpbuf4 = tmpbuf6;
160 /* Next collect IPv4 information. */
161 while (1)
163 rc4 = 0;
164 status[1] = DL_CALL_FCT (fct, (key, AF_INET, &th[1], tmpbuf4,
165 tmpbuf4len, &rc4, &herrno,
166 ttl == INT32_MAX ? &ttl : NULL,
167 canon == NULL ? &canon : NULL));
168 if (rc4 != ERANGE || herrno != NETDB_INTERNAL)
169 break;
170 tmpbuf4 = extend_alloca (tmpbuf4, tmpbuf4len, 2 * tmpbuf4len);
173 if (rc4 != 0 && herrno == NETDB_INTERNAL)
174 goto out;
176 if (status[0] == NSS_STATUS_SUCCESS
177 || status[1] == NSS_STATUS_SUCCESS)
179 /* We found the data. Count the addresses and the size. */
180 int naddrs = 0;
181 size_t addrslen = 0;
182 for (int j = 0; j < 2; ++j)
183 if (status[j] == NSS_STATUS_SUCCESS)
184 for (int i = 0; th[j].h_addr_list[i] != NULL; ++i)
186 ++naddrs;
187 addrslen += th[j].h_length;
190 if (canon == NULL)
192 /* Determine the canonical name. */
193 nss_getcanonname_r cfct;
194 cfct = __nss_lookup_function (nip, "getcanonname_r");
195 if (cfct != NULL)
197 const size_t max_fqdn_len = 256;
198 char *buf = alloca (max_fqdn_len);
199 char *s;
200 int rc;
202 if (DL_CALL_FCT (cfct, (key, buf, max_fqdn_len, &s, &rc,
203 &herrno)) == NSS_STATUS_SUCCESS)
204 canon = s;
205 else
206 /* Set to name now to avoid using gethostbyaddr. */
207 canon = key;
209 else
211 struct hostent *he = NULL;
212 int herrno;
213 struct hostent he_mem;
214 void *addr;
215 size_t addrlen;
216 int addrfamily;
218 if (status[1] == NSS_STATUS_SUCCESS)
220 addr = th[1].h_addr_list[0];
221 addrlen = sizeof (struct in_addr);
222 addrfamily = AF_INET;
224 else
226 addr = th[0].h_addr_list[0];
227 addrlen = sizeof (struct in6_addr);
228 addrfamily = AF_INET6;
231 size_t tmpbuflen = 512;
232 char *tmpbuf = alloca (tmpbuflen);
233 int rc;
234 while (1)
236 rc = __gethostbyaddr2_r (addr, addrlen, addrfamily,
237 &he_mem, tmpbuf, tmpbuflen,
238 &he, &herrno, NULL);
239 if (rc != ERANGE || herrno != NETDB_INTERNAL)
240 break;
241 tmpbuf = extend_alloca (tmpbuf, tmpbuflen,
242 tmpbuflen * 2);
245 if (rc == 0)
247 if (he != NULL)
248 canon = he->h_name;
249 else
250 canon = key;
254 size_t canonlen = canon == NULL ? 0 : (strlen (canon) + 1);
256 total = sizeof (*dataset) + naddrs + addrslen + canonlen;
258 /* Now we can allocate the data structure. If the TTL
259 of the entry is reported as zero do not cache the
260 entry at all. */
261 if (ttl != 0 && he == NULL)
263 dataset = (struct dataset *) mempool_alloc (db,
264 total
265 + req->key_len,
266 IDX_result_data);
267 if (dataset == NULL)
268 ++db->head->addfailed;
271 if (dataset == NULL)
273 /* We cannot permanently add the result in the moment. But
274 we can provide the result as is. Store the data in some
275 temporary memory. */
276 dataset = (struct dataset *) alloca (total + req->key_len);
278 /* We cannot add this record to the permanent database. */
279 alloca_used = true;
282 dataset->head.allocsize = total + req->key_len;
283 dataset->head.recsize = total - offsetof (struct dataset, resp);
284 dataset->head.notfound = false;
285 dataset->head.nreloads = he == NULL ? 0 : (dh->nreloads + 1);
286 dataset->head.usable = true;
288 /* Compute the timeout time. */
289 dataset->head.timeout = time (NULL) + (ttl == INT32_MAX
290 ? db->postimeout : ttl);
292 dataset->resp.version = NSCD_VERSION;
293 dataset->resp.found = 1;
294 dataset->resp.naddrs = naddrs;
295 dataset->resp.addrslen = addrslen;
296 dataset->resp.canonlen = canonlen;
297 dataset->resp.error = NETDB_SUCCESS;
299 char *addrs = (char *) (&dataset->resp + 1);
300 uint8_t *family = (uint8_t *) (addrs + addrslen);
302 for (int j = 0; j < 2; ++j)
303 if (status[j] == NSS_STATUS_SUCCESS)
304 for (int i = 0; th[j].h_addr_list[i] != NULL; ++i)
306 addrs = mempcpy (addrs, th[j].h_addr_list[i],
307 th[j].h_length);
308 *family++ = th[j].h_addrtype;
311 void *cp = family;
312 if (canon != NULL)
313 cp = mempcpy (cp, canon, canonlen);
315 key_copy = memcpy (cp, key, req->key_len);
317 /* Now we can determine whether on refill we have to
318 create a new record or not. */
319 if (he != NULL)
321 assert (fd == -1);
323 if (total + req->key_len == dh->allocsize
324 && total - offsetof (struct dataset, resp) == dh->recsize
325 && memcmp (&dataset->resp, dh->data,
326 dh->allocsize
327 - offsetof (struct dataset, resp)) == 0)
329 /* The data has not changed. We will just bump the
330 timeout value. Note that the new record has been
331 allocated on the stack and need not be freed. */
332 dh->timeout = dataset->head.timeout;
333 ++dh->nreloads;
335 else
337 /* We have to create a new record. Just allocate
338 appropriate memory and copy it. */
339 struct dataset *newp
340 = (struct dataset *) mempool_alloc (db,
341 total
342 + req->key_len,
343 IDX_result_data);
344 if (__builtin_expect (newp != NULL, 1))
346 /* Adjust pointer into the memory block. */
347 key_copy = (char *) newp + (key_copy
348 - (char *) dataset);
350 dataset = memcpy (newp, dataset,
351 total + req->key_len);
352 alloca_used = false;
354 else
355 ++db->head->addfailed;
357 /* Mark the old record as obsolete. */
358 dh->usable = false;
361 else
363 /* We write the dataset before inserting it to the
364 database since while inserting this thread might
365 block and so would unnecessarily let the receiver
366 wait. */
367 assert (fd != -1);
369 #ifdef HAVE_SENDFILE
370 if (__builtin_expect (db->mmap_used, 1) && !alloca_used)
372 assert (db->wr_fd != -1);
373 assert ((char *) &dataset->resp > (char *) db->data);
374 assert ((char *) &dataset->resp - (char *) db->head
375 + total
376 <= (sizeof (struct database_pers_head)
377 + db->head->module * sizeof (ref_t)
378 + db->head->data_size));
379 ssize_t written;
380 written = sendfileall (fd, db->wr_fd,
381 (char *) &dataset->resp
382 - (char *) db->head, total);
383 # ifndef __ASSUME_SENDFILE
384 if (written == -1 && errno == ENOSYS)
385 goto use_write;
386 # endif
388 else
389 # ifndef __ASSUME_SENDFILE
390 use_write:
391 # endif
392 #endif
393 writeall (fd, &dataset->resp, total);
396 goto out;
401 if (nss_next_action (nip, status[1]) == NSS_ACTION_RETURN)
402 break;
404 if (nip->next == NULL)
405 no_more = -1;
406 else
407 nip = nip->next;
410 /* No result found. Create a negative result record. */
411 if (he != NULL && rc4 == EAGAIN)
413 /* If we have an old record available but cannot find one now
414 because the service is not available we keep the old record
415 and make sure it does not get removed. */
416 if (reload_count != UINT_MAX && dh->nreloads == reload_count)
417 /* Do not reset the value if we never not reload the record. */
418 dh->nreloads = reload_count - 1;
420 else
422 /* We have no data. This means we send the standard reply for
423 this case. */
424 total = sizeof (notfound);
426 if (fd != -1)
427 TEMP_FAILURE_RETRY (send (fd, &notfound, total, MSG_NOSIGNAL));
429 dataset = mempool_alloc (db, sizeof (struct dataset) + req->key_len,
430 IDX_result_data);
431 /* If we cannot permanently store the result, so be it. */
432 if (dataset != NULL)
434 dataset->head.allocsize = sizeof (struct dataset) + req->key_len;
435 dataset->head.recsize = total;
436 dataset->head.notfound = true;
437 dataset->head.nreloads = 0;
438 dataset->head.usable = true;
440 /* Compute the timeout time. */
441 dataset->head.timeout = time (NULL) + db->negtimeout;
443 /* This is the reply. */
444 memcpy (&dataset->resp, &notfound, total);
446 /* Copy the key data. */
447 key_copy = memcpy (dataset->strdata, key, req->key_len);
449 else
450 ++db->head->addfailed;
453 out:
454 _res.options = old_res_options;
456 if (dataset != NULL && !alloca_used)
458 /* If necessary, we also propagate the data to disk. */
459 if (db->persistent)
461 // XXX async OK?
462 uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
463 msync ((void *) pval,
464 ((uintptr_t) dataset & pagesize_m1) + total + req->key_len,
465 MS_ASYNC);
468 /* Now get the lock to safely insert the records. */
469 pthread_rwlock_rdlock (&db->lock);
471 if (cache_add (req->type, key_copy, req->key_len, &dataset->head, true,
472 db, uid) < 0)
473 /* Ensure the data can be recovered. */
474 dataset->head.usable = false;
476 pthread_rwlock_unlock (&db->lock);
478 /* Mark the old entry as obsolete. */
479 if (dh != NULL)
480 dh->usable = false;
485 void
486 addhstai (struct database_dyn *db, int fd, request_header *req, void *key,
487 uid_t uid)
489 addhstaiX (db, fd, req, key, uid, NULL, NULL);
493 void
494 readdhstai (struct database_dyn *db, struct hashentry *he, struct datahead *dh)
496 request_header req =
498 .type = GETAI,
499 .key_len = he->len
502 addhstaiX (db, -1, &req, db->data + he->key, he->owner, he, dh);