One more bug in ldconfig -r handling.
[glibc.git] / nscd / nscd_getai.c
blobd1c5cd14e97414fb9043374b62779c95103cf499
1 /* Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@redhat.com>, 2004.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library 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 GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <assert.h>
22 #include <errno.h>
23 #include <netdb.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <not-cancel.h>
29 #include "nscd-client.h"
30 #include "nscd_proto.h"
33 /* Define in nscd_gethst_r.c. */
34 extern int __nss_not_use_nscd_hosts;
37 /* We use the mapping from nscd_gethst. */
38 libc_locked_map_ptr (extern, __hst_map_handle) attribute_hidden;
40 /* Defined in nscd_gethst_r.c. */
41 extern int __nss_have_localdomain attribute_hidden;
44 int
45 __nscd_getai (const char *key, struct nscd_ai_result **result, int *h_errnop)
47 if (__builtin_expect (__nss_have_localdomain >= 0, 0))
49 if (__nss_have_localdomain == 0)
50 __nss_have_localdomain = getenv ("LOCALDOMAIN") != NULL ? 1 : -1;
51 if (__nss_have_localdomain > 0)
53 __nss_not_use_nscd_hosts = 1;
54 return -1;
58 size_t keylen = strlen (key) + 1;
59 int gc_cycle;
60 int nretries = 0;
62 /* If the mapping is available, try to search there instead of
63 communicating with the nscd. */
64 struct mapped_database *mapped;
65 mapped = __nscd_get_map_ref (GETFDHST, "hosts", &__hst_map_handle,
66 &gc_cycle);
68 retry:;
69 struct nscd_ai_result *resultbuf = NULL;
70 const char *recend = (const char *) ~UINTMAX_C (0);
71 char *respdata = NULL;
72 int retval = -1;
73 int sock = -1;
74 ai_response_header ai_resp;
76 if (mapped != NO_MAPPING)
78 struct datahead *found = __nscd_cache_search (GETAI, key, keylen,
79 mapped, sizeof ai_resp);
80 if (found != NULL)
82 respdata = (char *) (&found->data[0].aidata + 1);
83 ai_resp = found->data[0].aidata;
84 recend = (const char *) found->data + found->recsize;
85 /* Now check if we can trust ai_resp fields. If GC is
86 in progress, it can contain anything. */
87 if (mapped->head->gc_cycle != gc_cycle)
89 retval = -2;
90 goto out;
95 /* If we do not have the cache mapped, try to get the data over the
96 socket. */
97 if (respdata == NULL)
99 sock = __nscd_open_socket (key, keylen, GETAI, &ai_resp,
100 sizeof (ai_resp));
101 if (sock == -1)
103 /* nscd not running or wrong version. */
104 __nss_not_use_nscd_hosts = 1;
105 goto out;
109 if (ai_resp.found == 1)
111 size_t datalen = ai_resp.naddrs + ai_resp.addrslen + ai_resp.canonlen;
113 /* This check really only affects the case where the data
114 comes from the mapped cache. */
115 if (respdata + datalen > recend)
117 assert (sock == -1);
118 goto out;
121 /* Create result. */
122 resultbuf = (struct nscd_ai_result *) malloc (sizeof (*resultbuf)
123 + datalen);
124 if (resultbuf == NULL)
126 *h_errnop = NETDB_INTERNAL;
127 goto out_close;
130 /* Set up the data structure, including pointers. */
131 resultbuf->naddrs = ai_resp.naddrs;
132 resultbuf->addrs = (char *) (resultbuf + 1);
133 resultbuf->family = (uint8_t *) (resultbuf->addrs + ai_resp.addrslen);
134 if (ai_resp.canonlen != 0)
135 resultbuf->canon = (char *) (resultbuf->family + resultbuf->naddrs);
136 else
137 resultbuf->canon = NULL;
139 if (respdata == NULL)
141 /* Read the data from the socket. */
142 if ((size_t) __readall (sock, resultbuf + 1, datalen) == datalen)
144 retval = 0;
145 *result = resultbuf;
147 else
149 free (resultbuf);
150 *h_errnop = NETDB_INTERNAL;
153 else
155 /* Copy the data in the block. */
156 memcpy (resultbuf + 1, respdata, datalen);
158 /* Try to detect corrupt databases. */
159 if (resultbuf->canon != NULL
160 && resultbuf->canon[ai_resp.canonlen - 1] != '\0')
161 /* We cannot use the database. */
163 if (mapped->head->gc_cycle != gc_cycle)
164 retval = -2;
165 else
166 free (resultbuf);
167 goto out_close;
170 retval = 0;
171 *result = resultbuf;
174 else
176 if (__builtin_expect (ai_resp.found == -1, 0))
178 /* The daemon does not cache this database. */
179 __nss_not_use_nscd_hosts = 1;
180 goto out_close;
183 /* Store the error number. */
184 *h_errnop = ai_resp.error;
186 /* Set errno to 0 to indicate no error, just no found record. */
187 __set_errno (0);
188 /* Even though we have not found anything, the result is zero. */
189 retval = 0;
192 out_close:
193 if (sock != -1)
194 close_not_cancel_no_status (sock);
195 out:
196 if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0)
198 /* When we come here this means there has been a GC cycle while we
199 were looking for the data. This means the data might have been
200 inconsistent. Retry if possible. */
201 if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
203 /* nscd is just running gc now. Disable using the mapping. */
204 if (atomic_decrement_val (&mapped->counter) == 0)
205 __nscd_unmap (mapped);
206 mapped = NO_MAPPING;
209 if (retval != -1)
211 *result = NULL;
212 free (resultbuf);
213 goto retry;
217 return retval;