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
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
;
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;
58 size_t keylen
= strlen (key
) + 1;
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
,
69 struct nscd_ai_result
*resultbuf
= NULL
;
70 const char *recend
= (const char *) ~UINTMAX_C (0);
71 char *respdata
= NULL
;
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
);
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
)
95 /* If we do not have the cache mapped, try to get the data over the
99 sock
= __nscd_open_socket (key
, keylen
, GETAI
, &ai_resp
,
103 /* nscd not running or wrong version. */
104 __nss_not_use_nscd_hosts
= 1;
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
)
122 resultbuf
= (struct nscd_ai_result
*) malloc (sizeof (*resultbuf
)
124 if (resultbuf
== NULL
)
126 *h_errnop
= NETDB_INTERNAL
;
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
);
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
)
150 *h_errnop
= NETDB_INTERNAL
;
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
)
176 if (__builtin_expect (ai_resp
.found
== -1, 0))
178 /* The daemon does not cache this database. */
179 __nss_not_use_nscd_hosts
= 1;
183 /* Store the error number. */
184 *h_errnop
= ai_resp
.error
;
186 /* Set errno to 0 to indicate no error, just no found record. */
188 /* Even though we have not found anything, the result is zero. */
194 close_not_cancel_no_status (sock
);
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
);