LoongArch: Remove support code for old linker in start.S
[glibc.git] / nscd / nscd_initgroups.c
bloba8643f578995981e04ea44ebba4606a29884a788
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 <grp.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 /* We use the same mapping as in nscd_getgr. */
31 libc_locked_map_ptr (extern, __gr_map_handle) attribute_hidden;
34 int
35 __nscd_getgrouplist (const char *user, gid_t group, long int *size,
36 gid_t **groupsp, long int limit)
38 size_t userlen = strlen (user) + 1;
39 int gc_cycle;
40 int nretries = 0;
42 /* If the mapping is available, try to search there instead of
43 communicating with the nscd. */
44 struct mapped_database *mapped;
45 mapped = __nscd_get_map_ref (GETFDGR, "group", &__gr_map_handle, &gc_cycle);
47 retry:;
48 char *respdata = NULL;
49 int retval = -1;
50 int sock = -1;
51 initgr_response_header initgr_resp;
53 if (mapped != NO_MAPPING)
55 struct datahead *found = __nscd_cache_search (INITGROUPS, user,
56 userlen, mapped,
57 sizeof initgr_resp);
58 if (found != NULL)
60 respdata = (char *) (&found->data[0].initgrdata + 1);
61 initgr_resp = found->data[0].initgrdata;
62 char *recend = (char *) found->data + found->recsize;
64 /* Now check if we can trust initgr_resp fields. If GC is
65 in progress, it can contain anything. */
66 if (mapped->head->gc_cycle != gc_cycle)
68 retval = -2;
69 goto out;
72 if (respdata + initgr_resp.ngrps * sizeof (int32_t) > recend)
73 goto out;
77 /* If we do not have the cache mapped, try to get the data over the
78 socket. */
79 if (respdata == NULL)
81 sock = __nscd_open_socket (user, userlen, INITGROUPS, &initgr_resp,
82 sizeof (initgr_resp));
83 if (sock == -1)
85 /* nscd not running or wrong version. */
86 __nss_not_use_nscd_group = 1;
87 goto out;
91 if (initgr_resp.found == 1)
93 /* The following code assumes that gid_t and int32_t are the
94 same size. This is the case for al existing implementation.
95 If this should change some code needs to be added which
96 doesn't use memcpy but instead copies each array element one
97 by one. */
98 assert (sizeof (int32_t) == sizeof (gid_t));
99 assert (initgr_resp.ngrps >= 0);
101 /* Make sure we have enough room. We always count GROUP in even
102 though we might not end up adding it. */
103 if (*size < initgr_resp.ngrps + 1)
105 gid_t *newp = realloc (*groupsp,
106 (initgr_resp.ngrps + 1) * sizeof (gid_t));
107 if (newp == NULL)
108 /* We cannot increase the buffer size. */
109 goto out_close;
111 *groupsp = newp;
112 *size = initgr_resp.ngrps + 1;
115 if (respdata == NULL)
117 /* Read the data from the socket. */
118 if ((size_t) __readall (sock, *groupsp, initgr_resp.ngrps
119 * sizeof (gid_t))
120 == initgr_resp.ngrps * sizeof (gid_t))
121 retval = initgr_resp.ngrps;
123 else
125 /* Just copy the data. */
126 retval = initgr_resp.ngrps;
127 memcpy (*groupsp, respdata, retval * sizeof (gid_t));
130 else
132 if (__glibc_unlikely (initgr_resp.found == -1))
134 /* The daemon does not cache this database. */
135 __nss_not_use_nscd_group = 1;
136 goto out_close;
139 /* No group found yet. */
140 retval = 0;
142 assert (*size >= 1);
145 /* Check whether GROUP is part of the mix. If not, add it. */
146 if (retval >= 0)
148 int cnt;
149 for (cnt = 0; cnt < retval; ++cnt)
150 if ((*groupsp)[cnt] == group)
151 break;
153 if (cnt == retval)
154 (*groupsp)[retval++] = group;
157 out_close:
158 if (sock != -1)
159 __close_nocancel_nostatus (sock);
160 out:
161 if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0)
163 /* When we come here this means there has been a GC cycle while we
164 were looking for the data. This means the data might have been
165 inconsistent. Retry if possible. */
166 if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
168 /* nscd is just running gc now. Disable using the mapping. */
169 if (atomic_fetch_add_relaxed (&mapped->counter, -1) == 1)
170 __nscd_unmap (mapped);
171 mapped = NO_MAPPING;
174 if (retval != -1)
175 goto retry;
178 return retval;