<sys/platform/x86.h>: Add LBR support
[glibc.git] / nscd / nscd_getai.c
blobc6c3bb628ca4a1f67395d2d36920f2423be0dcbe
1 /* Copyright (C) 2004-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
18 #include <assert.h>
19 #include <errno.h>
20 #include <netdb.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <not-cancel.h>
26 #include "nscd-client.h"
27 #include "nscd_proto.h"
30 /* Define in nscd_gethst_r.c. */
31 extern int __nss_not_use_nscd_hosts;
34 /* We use the mapping from nscd_gethst. */
35 libc_locked_map_ptr (extern, __hst_map_handle) attribute_hidden;
37 /* Defined in nscd_gethst_r.c. */
38 extern int __nss_have_localdomain attribute_hidden;
41 int
42 __nscd_getai (const char *key, struct nscd_ai_result **result, int *h_errnop)
44 if (__glibc_unlikely (__nss_have_localdomain >= 0))
46 if (__nss_have_localdomain == 0)
47 __nss_have_localdomain = getenv ("LOCALDOMAIN") != NULL ? 1 : -1;
48 if (__nss_have_localdomain > 0)
50 __nss_not_use_nscd_hosts = 1;
51 return -1;
55 size_t keylen = strlen (key) + 1;
56 int gc_cycle;
57 int nretries = 0;
59 /* If the mapping is available, try to search there instead of
60 communicating with the nscd. */
61 struct mapped_database *mapped;
62 mapped = __nscd_get_map_ref (GETFDHST, "hosts", &__hst_map_handle,
63 &gc_cycle);
65 retry:;
66 struct nscd_ai_result *resultbuf = NULL;
67 const char *recend = (const char *) ~UINTMAX_C (0);
68 char *respdata = NULL;
69 int retval = -1;
70 int sock = -1;
71 ai_response_header ai_resp;
73 if (mapped != NO_MAPPING)
75 struct datahead *found = __nscd_cache_search (GETAI, key, keylen,
76 mapped, sizeof ai_resp);
77 if (found != NULL)
79 respdata = (char *) (&found->data[0].aidata + 1);
80 ai_resp = found->data[0].aidata;
81 recend = (const char *) found->data + found->recsize;
82 /* Now check if we can trust ai_resp fields. If GC is
83 in progress, it can contain anything. */
84 if (mapped->head->gc_cycle != gc_cycle)
86 retval = -2;
87 goto out;
92 /* If we do not have the cache mapped, try to get the data over the
93 socket. */
94 if (respdata == NULL)
96 sock = __nscd_open_socket (key, keylen, GETAI, &ai_resp,
97 sizeof (ai_resp));
98 if (sock == -1)
100 /* nscd not running or wrong version. */
101 __nss_not_use_nscd_hosts = 1;
102 goto out;
106 if (ai_resp.found == 1)
108 size_t datalen = ai_resp.naddrs + ai_resp.addrslen + ai_resp.canonlen;
110 /* This check really only affects the case where the data
111 comes from the mapped cache. */
112 if (respdata + datalen > recend)
114 assert (sock == -1);
115 goto out;
118 /* Create result. */
119 resultbuf = (struct nscd_ai_result *) malloc (sizeof (*resultbuf)
120 + datalen);
121 if (resultbuf == NULL)
123 *h_errnop = NETDB_INTERNAL;
124 goto out_close;
127 /* Set up the data structure, including pointers. */
128 resultbuf->naddrs = ai_resp.naddrs;
129 resultbuf->addrs = (char *) (resultbuf + 1);
130 resultbuf->family = (uint8_t *) (resultbuf->addrs + ai_resp.addrslen);
131 if (ai_resp.canonlen != 0)
132 resultbuf->canon = (char *) (resultbuf->family + resultbuf->naddrs);
133 else
134 resultbuf->canon = NULL;
136 if (respdata == NULL)
138 /* Read the data from the socket. */
139 if ((size_t) __readall (sock, resultbuf + 1, datalen) == datalen)
141 retval = 0;
142 *result = resultbuf;
144 else
146 free (resultbuf);
147 *h_errnop = NETDB_INTERNAL;
150 else
152 /* Copy the data in the block. */
153 memcpy (resultbuf + 1, respdata, datalen);
155 /* Try to detect corrupt databases. */
156 if (resultbuf->canon != NULL
157 && resultbuf->canon[ai_resp.canonlen - 1] != '\0')
158 /* We cannot use the database. */
160 if (mapped->head->gc_cycle != gc_cycle)
161 retval = -2;
162 else
163 free (resultbuf);
164 goto out_close;
167 retval = 0;
168 *result = resultbuf;
171 else
173 if (__glibc_unlikely (ai_resp.found == -1))
175 /* The daemon does not cache this database. */
176 __nss_not_use_nscd_hosts = 1;
177 goto out_close;
180 /* Store the error number. */
181 *h_errnop = ai_resp.error;
183 /* Set errno to 0 to indicate no error, just no found record. */
184 __set_errno (0);
185 /* Even though we have not found anything, the result is zero. */
186 retval = 0;
189 out_close:
190 if (sock != -1)
191 __close_nocancel_nostatus (sock);
192 out:
193 if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0)
195 /* When we come here this means there has been a GC cycle while we
196 were looking for the data. This means the data might have been
197 inconsistent. Retry if possible. */
198 if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
200 /* nscd is just running gc now. Disable using the mapping. */
201 if (atomic_fetch_add_relaxed (&mapped->counter, -1) == 1)
202 __nscd_unmap (mapped);
203 mapped = NO_MAPPING;
206 if (retval != -1)
208 *result = NULL;
209 free (resultbuf);
210 goto retry;
214 return retval;