Add the missing "; \".
[glibc.git] / nscd / nscd_getgr_r.c
blobc2d204c3c801952da6f3375bbbb2e674bb33497d
1 /* Copyright (C) 1998-2000, 2002-2005, 2006, 2007, 2009
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Thorsten Kukuk <kukuk@uni-paderborn.de>, 1998.
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 <alloca.h>
22 #include <assert.h>
23 #include <errno.h>
24 #include <grp.h>
25 #include <stdint.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <sys/mman.h>
31 #include <sys/socket.h>
32 #include <sys/uio.h>
33 #include <sys/un.h>
34 #include <not-cancel.h>
35 #include <stdio-common/_itoa.h>
37 #include "nscd-client.h"
38 #include "nscd_proto.h"
40 int __nss_not_use_nscd_group;
42 static int nscd_getgr_r (const char *key, size_t keylen, request_type type,
43 struct group *resultbuf, char *buffer,
44 size_t buflen, struct group **result)
45 internal_function;
48 int
49 __nscd_getgrnam_r (const char *name, struct group *resultbuf, char *buffer,
50 size_t buflen, struct group **result)
52 return nscd_getgr_r (name, strlen (name) + 1, GETGRBYNAME, resultbuf,
53 buffer, buflen, result);
57 int
58 __nscd_getgrgid_r (gid_t gid, struct group *resultbuf, char *buffer,
59 size_t buflen, struct group **result)
61 char buf[3 * sizeof (gid_t)];
62 buf[sizeof (buf) - 1] = '\0';
63 char *cp = _itoa_word (gid, buf + sizeof (buf) - 1, 10, 0);
65 return nscd_getgr_r (cp, buf + sizeof (buf) - cp, GETGRBYGID, resultbuf,
66 buffer, buflen, result);
70 libc_locked_map_ptr (,__gr_map_handle) attribute_hidden;
71 /* Note that we only free the structure if necessary. The memory
72 mapping is not removed since it is not visible to the malloc
73 handling. */
74 libc_freeres_fn (gr_map_free)
76 if (__gr_map_handle.mapped != NO_MAPPING)
78 void *p = __gr_map_handle.mapped;
79 __gr_map_handle.mapped = NO_MAPPING;
80 free (p);
85 static int
86 internal_function
87 nscd_getgr_r (const char *key, size_t keylen, request_type type,
88 struct group *resultbuf, char *buffer, size_t buflen,
89 struct group **result)
91 int gc_cycle;
92 int nretries = 0;
93 const uint32_t *len = NULL;
94 size_t lensize = 0;
96 /* If the mapping is available, try to search there instead of
97 communicating with the nscd. */
98 struct mapped_database *mapped = __nscd_get_map_ref (GETFDGR, "group",
99 &__gr_map_handle,
100 &gc_cycle);
101 retry:;
102 const char *gr_name = NULL;
103 size_t gr_name_len = 0;
104 int retval = -1;
105 const char *recend = (const char *) ~UINTMAX_C (0);
106 gr_response_header gr_resp;
108 if (mapped != NO_MAPPING)
110 struct datahead *found = __nscd_cache_search (type, key, keylen, mapped,
111 sizeof gr_resp);
112 if (found != NULL)
114 len = (const uint32_t *) (&found->data[0].grdata + 1);
115 gr_resp = found->data[0].grdata;
116 gr_name = ((const char *) len
117 + gr_resp.gr_mem_cnt * sizeof (uint32_t));
118 gr_name_len = gr_resp.gr_name_len + gr_resp.gr_passwd_len;
119 recend = (const char *) found->data + found->recsize;
120 /* Now check if we can trust gr_resp fields. If GC is
121 in progress, it can contain anything. */
122 if (mapped->head->gc_cycle != gc_cycle)
124 retval = -2;
125 goto out;
128 /* The alignment is always sufficient, unless GC is in progress. */
129 assert (((uintptr_t) len & (__alignof__ (*len) - 1)) == 0);
133 int sock = -1;
134 if (gr_name == NULL)
136 sock = __nscd_open_socket (key, keylen, type, &gr_resp,
137 sizeof (gr_resp));
138 if (sock == -1)
140 __nss_not_use_nscd_group = 1;
141 goto out;
145 /* No value found so far. */
146 *result = NULL;
148 if (__builtin_expect (gr_resp.found == -1, 0))
150 /* The daemon does not cache this database. */
151 __nss_not_use_nscd_group = 1;
152 goto out_close;
155 if (gr_resp.found == 1)
157 struct iovec vec[2];
158 char *p = buffer;
159 size_t total_len;
160 uintptr_t align;
161 nscd_ssize_t cnt;
163 /* Now allocate the buffer the array for the group members. We must
164 align the pointer. */
165 align = ((__alignof__ (char *) - (p - ((char *) 0)))
166 & (__alignof__ (char *) - 1));
167 total_len = (align + (1 + gr_resp.gr_mem_cnt) * sizeof (char *)
168 + gr_resp.gr_name_len + gr_resp.gr_passwd_len);
169 if (__builtin_expect (buflen < total_len, 0))
171 no_room:
172 __set_errno (ERANGE);
173 retval = ERANGE;
174 goto out_close;
176 buflen -= total_len;
178 p += align;
179 resultbuf->gr_mem = (char **) p;
180 p += (1 + gr_resp.gr_mem_cnt) * sizeof (char *);
182 /* Set pointers for strings. */
183 resultbuf->gr_name = p;
184 p += gr_resp.gr_name_len;
185 resultbuf->gr_passwd = p;
186 p += gr_resp.gr_passwd_len;
188 /* Fill in what we know now. */
189 resultbuf->gr_gid = gr_resp.gr_gid;
191 /* Read the length information, group name, and password. */
192 if (gr_name == NULL)
194 /* Handle a simple, usual case: no group members. */
195 if (__builtin_expect (gr_resp.gr_mem_cnt == 0, 1))
197 size_t n = gr_resp.gr_name_len + gr_resp.gr_passwd_len;
198 if (__builtin_expect (__readall (sock, resultbuf->gr_name, n)
199 != (ssize_t) n, 0))
200 goto out_close;
202 else
204 /* Allocate array to store lengths. */
205 if (lensize == 0)
207 lensize = gr_resp.gr_mem_cnt * sizeof (uint32_t);
208 len = (uint32_t *) alloca (lensize);
210 else if (gr_resp.gr_mem_cnt * sizeof (uint32_t) > lensize)
211 len = extend_alloca (len, lensize,
212 gr_resp.gr_mem_cnt * sizeof (uint32_t));
214 vec[0].iov_base = (void *) len;
215 vec[0].iov_len = gr_resp.gr_mem_cnt * sizeof (uint32_t);
216 vec[1].iov_base = resultbuf->gr_name;
217 vec[1].iov_len = gr_resp.gr_name_len + gr_resp.gr_passwd_len;
218 total_len = vec[0].iov_len + vec[1].iov_len;
220 /* Get this data. */
221 size_t n = __readvall (sock, vec, 2);
222 if (__builtin_expect (n != total_len, 0))
223 goto out_close;
226 else
227 /* We already have the data. Just copy the group name and
228 password. */
229 memcpy (resultbuf->gr_name, gr_name,
230 gr_resp.gr_name_len + gr_resp.gr_passwd_len);
232 /* Clear the terminating entry. */
233 resultbuf->gr_mem[gr_resp.gr_mem_cnt] = NULL;
235 /* Prepare reading the group members. */
236 total_len = 0;
237 for (cnt = 0; cnt < gr_resp.gr_mem_cnt; ++cnt)
239 resultbuf->gr_mem[cnt] = p;
240 total_len += len[cnt];
241 p += len[cnt];
244 if (__builtin_expect (gr_name + gr_name_len + total_len > recend, 0))
246 /* len array might contain garbage during nscd GC cycle,
247 retry rather than fail in that case. */
248 if (gr_name != NULL && mapped->head->gc_cycle != gc_cycle)
249 retval = -2;
250 goto out_close;
252 if (__builtin_expect (total_len > buflen, 0))
254 /* len array might contain garbage during nscd GC cycle,
255 retry rather than fail in that case. */
256 if (gr_name != NULL && mapped->head->gc_cycle != gc_cycle)
258 retval = -2;
259 goto out_close;
261 else
262 goto no_room;
265 retval = 0;
267 /* If there are no group members TOTAL_LEN is zero. */
268 if (gr_name == NULL)
270 if (total_len > 0
271 && __builtin_expect (__readall (sock, resultbuf->gr_mem[0],
272 total_len) != total_len, 0))
274 /* The `errno' to some value != ERANGE. */
275 __set_errno (ENOENT);
276 retval = ENOENT;
278 else
279 *result = resultbuf;
281 else
283 /* Copy the group member names. */
284 memcpy (resultbuf->gr_mem[0], gr_name + gr_name_len, total_len);
286 /* Try to detect corrupt databases. */
287 if (resultbuf->gr_name[gr_name_len - 1] != '\0'
288 || resultbuf->gr_passwd[gr_resp.gr_passwd_len - 1] != '\0'
289 || ({for (cnt = 0; cnt < gr_resp.gr_mem_cnt; ++cnt)
290 if (resultbuf->gr_mem[cnt][len[cnt] - 1] != '\0')
291 break;
292 cnt < gr_resp.gr_mem_cnt; }))
294 /* We cannot use the database. */
295 retval = mapped->head->gc_cycle != gc_cycle ? -2 : -1;
296 goto out_close;
299 *result = resultbuf;
302 else
304 /* Set errno to 0 to indicate no error, just no found record. */
305 __set_errno (0);
306 /* Even though we have not found anything, the result is zero. */
307 retval = 0;
310 out_close:
311 if (sock != -1)
312 close_not_cancel_no_status (sock);
313 out:
314 if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0)
316 /* When we come here this means there has been a GC cycle while we
317 were looking for the data. This means the data might have been
318 inconsistent. Retry if possible. */
319 if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
321 /* nscd is just running gc now. Disable using the mapping. */
322 if (atomic_decrement_val (&mapped->counter) == 0)
323 __nscd_unmap (mapped);
324 mapped = NO_MAPPING;
327 if (retval != -1)
328 goto retry;
331 return retval;