Add the missing "; \".
[glibc.git] / nscd / nscd_getserv_r.c
blobdce41654821e9bcfc20b6a10f576325952abda2a
1 /* Copyright (C) 2007, 2009 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2007.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <string.h>
22 #include <not-cancel.h>
23 #include <stdio-common/_itoa.h>
25 #include "nscd-client.h"
26 #include "nscd_proto.h"
29 int __nss_not_use_nscd_services;
32 static int nscd_getserv_r (const char *crit, size_t critlen, const char *proto,
33 request_type type, struct servent *resultbuf,
34 char *buf, size_t buflen, struct servent **result);
37 int
38 __nscd_getservbyname_r (const char *name, const char *proto,
39 struct servent *result_buf, char *buf, size_t buflen,
40 struct servent **result)
42 return nscd_getserv_r (name, strlen (name), proto, GETSERVBYNAME, result_buf,
43 buf, buflen, result);
47 int
48 __nscd_getservbyport_r (int port, const char *proto,
49 struct servent *result_buf, char *buf, size_t buflen,
50 struct servent **result)
52 char portstr[3 * sizeof (int) + 2];
53 portstr[sizeof (portstr) - 1] = '\0';
54 char *cp = _itoa_word (port, portstr + sizeof (portstr) - 1, 10, 0);
56 return nscd_getserv_r (cp, portstr + sizeof (portstr) - cp, proto,
57 GETSERVBYPORT, result_buf, buf, buflen, result);
61 libc_locked_map_ptr (, __serv_map_handle) attribute_hidden;
62 /* Note that we only free the structure if necessary. The memory
63 mapping is not removed since it is not visible to the malloc
64 handling. */
65 libc_freeres_fn (serv_map_free)
67 if (__serv_map_handle.mapped != NO_MAPPING)
69 void *p = __serv_map_handle.mapped;
70 __serv_map_handle.mapped = NO_MAPPING;
71 free (p);
76 static int
77 nscd_getserv_r (const char *crit, size_t critlen, const char *proto,
78 request_type type, struct servent *resultbuf,
79 char *buf, size_t buflen, struct servent **result)
81 int gc_cycle;
82 int nretries = 0;
84 /* If the mapping is available, try to search there instead of
85 communicating with the nscd. */
86 struct mapped_database *mapped;
87 mapped = __nscd_get_map_ref (GETFDSERV, "services", &__serv_map_handle,
88 &gc_cycle);
89 size_t protolen = proto == NULL ? 0 : strlen (proto);
90 size_t keylen = critlen + 1 + protolen + 1;
91 char *key = alloca (keylen);
92 memcpy (__mempcpy (__mempcpy (key, crit, critlen),
93 "/", 1), proto ?: "", protolen + 1);
95 retry:;
96 const char *s_name = NULL;
97 const char *s_proto = NULL;
98 const uint32_t *aliases_len = NULL;
99 const char *aliases_list = NULL;
100 int retval = -1;
101 const char *recend = (const char *) ~UINTMAX_C (0);
102 int sock = -1;
103 serv_response_header serv_resp;
105 if (mapped != NO_MAPPING)
107 struct datahead *found = __nscd_cache_search (type, key, keylen, mapped,
108 sizeof serv_resp);
110 if (found != NULL)
112 s_name = (char *) (&found->data[0].servdata + 1);
113 serv_resp = found->data[0].servdata;
114 s_proto = s_name + serv_resp.s_name_len;
115 aliases_len = (uint32_t *) (s_proto + serv_resp.s_proto_len);
116 aliases_list = ((char *) aliases_len
117 + serv_resp.s_aliases_cnt * sizeof (uint32_t));
118 recend = (const char *) found->data + found->recsize;
119 /* Now check if we can trust serv_resp fields. If GC is
120 in progress, it can contain anything. */
121 if (mapped->head->gc_cycle != gc_cycle)
123 retval = -2;
124 goto out;
126 if (__builtin_expect ((const char *) aliases_len
127 + serv_resp.s_aliases_cnt * sizeof (uint32_t)
128 > recend, 0))
129 goto out;
131 #ifndef _STRING_ARCH_unaligned
132 /* The aliases_len array in the mapped database might very
133 well be unaligned. We will access it word-wise so on
134 platforms which do not tolerate unaligned accesses we
135 need to make an aligned copy. */
136 if (((uintptr_t) aliases_len & (__alignof__ (*aliases_len) - 1))
137 != 0)
139 uint32_t *tmp = alloca (serv_resp.s_aliases_cnt
140 * sizeof (uint32_t));
141 aliases_len = memcpy (tmp, aliases_len,
142 serv_resp.s_aliases_cnt
143 * sizeof (uint32_t));
145 #endif
149 if (s_name == NULL)
151 sock = __nscd_open_socket (key, keylen, type, &serv_resp,
152 sizeof (serv_resp));
153 if (sock == -1)
155 __nss_not_use_nscd_services = 1;
156 goto out;
160 /* No value found so far. */
161 *result = NULL;
163 if (__builtin_expect (serv_resp.found == -1, 0))
165 /* The daemon does not cache this database. */
166 __nss_not_use_nscd_services = 1;
167 goto out_close;
170 if (serv_resp.found == 1)
172 char *cp = buf;
173 uintptr_t align1;
174 uintptr_t align2;
175 size_t total_len;
176 ssize_t cnt;
177 int n;
179 /* A first check whether the buffer is sufficiently large is possible. */
180 /* Now allocate the buffer the array for the group members. We must
181 align the pointer and the base of the h_addr_list pointers. */
182 align1 = ((__alignof__ (char *) - (cp - ((char *) 0)))
183 & (__alignof__ (char *) - 1));
184 align2 = ((__alignof__ (char *) - ((cp + align1 + serv_resp.s_name_len
185 + serv_resp.s_proto_len)
186 - ((char *) 0)))
187 & (__alignof__ (char *) - 1));
188 if (buflen < (align1 + serv_resp.s_name_len + serv_resp.s_proto_len
189 + align2
190 + (serv_resp.s_aliases_cnt + 1) * sizeof (char *)))
192 no_room:
193 __set_errno (ERANGE);
194 retval = ERANGE;
195 goto out_close;
197 cp += align1;
199 /* Prepare the result as far as we can. */
200 resultbuf->s_aliases = (char **) cp;
201 cp += (serv_resp.s_aliases_cnt + 1) * sizeof (char *);
203 resultbuf->s_name = cp;
204 cp += serv_resp.s_name_len;
205 resultbuf->s_proto = cp;
206 cp += serv_resp.s_proto_len + align2;
207 resultbuf->s_port = serv_resp.s_port;
209 if (s_name == NULL)
211 struct iovec vec[2];
213 vec[0].iov_base = resultbuf->s_name;
214 vec[0].iov_len = serv_resp.s_name_len + serv_resp.s_proto_len;
215 total_len = vec[0].iov_len;
216 n = 1;
218 if (serv_resp.s_aliases_cnt > 0)
220 aliases_len = alloca (serv_resp.s_aliases_cnt
221 * sizeof (uint32_t));
222 vec[n].iov_base = (void *) aliases_len;
223 vec[n].iov_len = serv_resp.s_aliases_cnt * sizeof (uint32_t);
225 total_len += serv_resp.s_aliases_cnt * sizeof (uint32_t);
226 ++n;
229 if ((size_t) __readvall (sock, vec, n) != total_len)
230 goto out_close;
232 else
233 memcpy (resultbuf->s_name, s_name,
234 serv_resp.s_name_len + serv_resp.s_proto_len);
236 /* Now we also can read the aliases. */
237 total_len = 0;
238 for (cnt = 0; cnt < serv_resp.s_aliases_cnt; ++cnt)
240 resultbuf->s_aliases[cnt] = cp;
241 cp += aliases_len[cnt];
242 total_len += aliases_len[cnt];
244 resultbuf->s_aliases[cnt] = NULL;
246 if (__builtin_expect ((const char *) aliases_list + total_len > recend,
249 /* aliases_len array might contain garbage during nscd GC cycle,
250 retry rather than fail in that case. */
251 if (aliases_list != NULL && mapped->head->gc_cycle != gc_cycle)
252 retval = -2;
253 goto out_close;
256 /* See whether this would exceed the buffer capacity. */
257 if (__builtin_expect (cp > buf + buflen, 0))
259 /* aliases_len array might contain garbage during nscd GC cycle,
260 retry rather than fail in that case. */
261 if (aliases_list != NULL && mapped->head->gc_cycle != gc_cycle)
263 retval = -2;
264 goto out_close;
266 goto no_room;
269 /* And finally read the aliases. */
270 if (aliases_list == NULL)
272 if (total_len == 0
273 || ((size_t) __readall (sock, resultbuf->s_aliases[0], total_len)
274 == total_len))
276 retval = 0;
277 *result = resultbuf;
280 else
282 memcpy (resultbuf->s_aliases[0], aliases_list, total_len);
284 /* Try to detect corrupt databases. */
285 if (resultbuf->s_name[serv_resp.s_name_len - 1] != '\0'
286 || resultbuf->s_proto[serv_resp.s_proto_len - 1] != '\0'
287 || ({for (cnt = 0; cnt < serv_resp.s_aliases_cnt; ++cnt)
288 if (resultbuf->s_aliases[cnt][aliases_len[cnt] - 1]
289 != '\0')
290 break;
291 cnt < serv_resp.s_aliases_cnt; }))
293 /* We cannot use the database. */
294 if (mapped->head->gc_cycle != gc_cycle)
295 retval = -2;
296 goto out_close;
299 retval = 0;
300 *result = resultbuf;
303 else
305 /* Set errno to 0 to indicate no error, just no found record. */
306 __set_errno (0);
307 /* Even though we have not found anything, the result is zero. */
308 retval = 0;
311 out_close:
312 if (sock != -1)
313 close_not_cancel_no_status (sock);
314 out:
315 if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0)
317 /* When we come here this means there has been a GC cycle while we
318 were looking for the data. This means the data might have been
319 inconsistent. Retry if possible. */
320 if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
322 /* nscd is just running gc now. Disable using the mapping. */
323 if (atomic_decrement_val (&mapped->counter) == 0)
324 __nscd_unmap (mapped);
325 mapped = NO_MAPPING;
328 if (retval != -1)
329 goto retry;
332 return retval;