Add the missing "; \".
[glibc.git] / nscd / nscd_initgroups.c
blob5ff60c080c0d43f4be8d2ce3a3fdc578ce234f87
1 /* Copyright (C) 2004, 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2004.
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 <assert.h>
21 #include <errno.h>
22 #include <grp.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <not-cancel.h>
28 #include "nscd-client.h"
29 #include "nscd_proto.h"
32 /* We use the same mapping as in nscd_getgr. */
33 libc_locked_map_ptr (extern, __gr_map_handle) attribute_hidden;
36 int
37 __nscd_getgrouplist (const char *user, gid_t group, long int *size,
38 gid_t **groupsp, long int limit)
40 size_t userlen = strlen (user) + 1;
41 int gc_cycle;
42 int nretries = 0;
44 /* If the mapping is available, try to search there instead of
45 communicating with the nscd. */
46 struct mapped_database *mapped;
47 mapped = __nscd_get_map_ref (GETFDGR, "group", &__gr_map_handle, &gc_cycle);
49 retry:;
50 char *respdata = NULL;
51 int retval = -1;
52 int sock = -1;
53 initgr_response_header initgr_resp;
55 if (mapped != NO_MAPPING)
57 struct datahead *found = __nscd_cache_search (INITGROUPS, user,
58 userlen, mapped,
59 sizeof initgr_resp);
60 if (found != NULL)
62 respdata = (char *) (&found->data[0].initgrdata + 1);
63 initgr_resp = found->data[0].initgrdata;
64 char *recend = (char *) found->data + found->recsize;
66 /* Now check if we can trust initgr_resp fields. If GC is
67 in progress, it can contain anything. */
68 if (mapped->head->gc_cycle != gc_cycle)
70 retval = -2;
71 goto out;
74 if (respdata + initgr_resp.ngrps * sizeof (int32_t) > recend)
75 goto out;
79 /* If we do not have the cache mapped, try to get the data over the
80 socket. */
81 if (respdata == NULL)
83 sock = __nscd_open_socket (user, userlen, INITGROUPS, &initgr_resp,
84 sizeof (initgr_resp));
85 if (sock == -1)
87 /* nscd not running or wrong version. */
88 __nss_not_use_nscd_group = 1;
89 goto out;
93 if (initgr_resp.found == 1)
95 /* The following code assumes that gid_t and int32_t are the
96 same size. This is the case for al existing implementation.
97 If this should change some code needs to be added which
98 doesn't use memcpy but instead copies each array element one
99 by one. */
100 assert (sizeof (int32_t) == sizeof (gid_t));
101 assert (initgr_resp.ngrps >= 0);
103 /* Make sure we have enough room. We always count GROUP in even
104 though we might not end up adding it. */
105 if (*size < initgr_resp.ngrps + 1)
107 gid_t *newp = realloc (*groupsp,
108 (initgr_resp.ngrps + 1) * sizeof (gid_t));
109 if (newp == NULL)
110 /* We cannot increase the buffer size. */
111 goto out_close;
113 *groupsp = newp;
114 *size = initgr_resp.ngrps + 1;
117 if (respdata == NULL)
119 /* Read the data from the socket. */
120 if ((size_t) __readall (sock, *groupsp, initgr_resp.ngrps
121 * sizeof (gid_t))
122 == initgr_resp.ngrps * sizeof (gid_t))
123 retval = initgr_resp.ngrps;
125 else
127 /* Just copy the data. */
128 retval = initgr_resp.ngrps;
129 memcpy (*groupsp, respdata, retval * sizeof (gid_t));
132 else
134 if (__builtin_expect (initgr_resp.found == -1, 0))
136 /* The daemon does not cache this database. */
137 __nss_not_use_nscd_group = 1;
138 goto out_close;
141 /* No group found yet. */
142 retval = 0;
144 assert (*size >= 1);
147 /* Check whether GROUP is part of the mix. If not, add it. */
148 if (retval >= 0)
150 int cnt;
151 for (cnt = 0; cnt < retval; ++cnt)
152 if ((*groupsp)[cnt] == group)
153 break;
155 if (cnt == retval)
156 (*groupsp)[retval++] = group;
159 out_close:
160 if (sock != -1)
161 close_not_cancel_no_status (sock);
162 out:
163 if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0)
165 /* When we come here this means there has been a GC cycle while we
166 were looking for the data. This means the data might have been
167 inconsistent. Retry if possible. */
168 if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
170 /* nscd is just running gc now. Disable using the mapping. */
171 if (atomic_decrement_val (&mapped->counter) == 0)
172 __nscd_unmap (mapped);
173 mapped = NO_MAPPING;
176 if (retval != -1)
177 goto retry;
180 return retval;