Update.
[glibc.git] / nscd / initgrcache.c
blob124b4bae626bc04fec25104f9a5d0eaf8fe3362c
1 /* Cache handling for host lookup.
2 Copyright (C) 2004 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@redhat.com>, 2004.
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 <assert.h>
22 #include <errno.h>
23 #include <grp.h>
24 #include <libintl.h>
25 #include <string.h>
26 #include <time.h>
27 #include <unistd.h>
28 #include <sys/mman.h>
29 #include <dbg_log.h>
30 #include <nscd.h>
32 #include "../nss/nsswitch.h"
35 /* Type of the lookup function. */
36 typedef enum nss_status (*initgroups_dyn_function) (const char *, gid_t,
37 long int *, long int *,
38 gid_t **, long int, int *);
41 static const initgr_response_header notfound =
43 .version = NSCD_VERSION,
44 .found = 0,
45 .ngrps = 0
49 #include "../grp/compat-initgroups.c"
52 static void
53 addinitgroupsX (struct database_dyn *db, int fd, request_header *req,
54 void *key, uid_t uid, struct hashentry *he,
55 struct datahead *dh)
57 /* Search for the entry matching the key. Please note that we don't
58 look again in the table whether the dataset is now available. We
59 simply insert it. It does not matter if it is in there twice. The
60 pruning function only will look at the timestamp. */
63 /* We allocate all data in one memory block: the iov vector,
64 the response header and the dataset itself. */
65 struct dataset
67 struct datahead head;
68 initgr_response_header resp;
69 char strdata[0];
70 } *dataset = NULL;
72 if (__builtin_expect (debug_level > 0, 0))
74 if (he == NULL)
75 dbg_log (_("Haven't found \"%s\" in group cache!"), (char *) key);
76 else
77 dbg_log (_("Reloading \"%s\" in group cache!"), (char *) key);
80 static service_user *group_database;
81 service_user *nip = NULL;
82 int no_more;
84 if (group_database != NULL)
86 nip = group_database;
87 no_more = 0;
89 else
90 no_more = __nss_database_lookup ("group", NULL,
91 "compat [NOTFOUND=return] files", &nip);
93 /* We always use sysconf even if NGROUPS_MAX is defined. That way, the
94 limit can be raised in the kernel configuration without having to
95 recompile libc. */
96 long int limit = __sysconf (_SC_NGROUPS_MAX);
98 long int size;
99 if (limit > 0)
100 /* We limit the size of the intially allocated array. */
101 size = MIN (limit, 64);
102 else
103 /* No fixed limit on groups. Pick a starting buffer size. */
104 size = 16;
106 long int start = 0;
107 bool all_tryagain = true;
109 /* This is temporary memory, we need not (ad must not) call
110 mempool_alloc. */
111 // XXX This really should use alloca. need to change the backends.
112 gid_t *groups = (gid_t *) malloc (size * sizeof (gid_t));
113 if (__builtin_expect (groups == NULL, 0))
114 /* No more memory. */
115 goto out;
117 /* Nothing added yet. */
118 while (! no_more)
120 enum nss_status status;
121 initgroups_dyn_function fct;
122 fct = __nss_lookup_function (nip, "initgroups_dyn");
124 if (fct == NULL)
126 status = compat_call (nip, key, -1, &start, &size, &groups,
127 limit, &errno);
129 if (nss_next_action (nip, NSS_STATUS_UNAVAIL) != NSS_ACTION_CONTINUE)
130 break;
132 else
133 status = DL_CALL_FCT (fct, (key, -1, &start, &size, &groups,
134 limit, &errno));
136 if (status != NSS_STATUS_TRYAGAIN)
137 all_tryagain = false;
139 /* This is really only for debugging. */
140 if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_RETURN)
141 __libc_fatal ("illegal status in internal_getgrouplist");
143 if (status != NSS_STATUS_SUCCESS
144 && nss_next_action (nip, status) == NSS_ACTION_RETURN)
145 break;
147 if (nip->next == NULL)
148 no_more = -1;
149 else
150 nip = nip->next;
153 ssize_t total;
154 ssize_t written;
155 out:
156 if (start == 0)
158 /* Nothing found. Create a negative result record. */
159 written = total = sizeof (notfound);
161 if (he != NULL && all_tryagain)
163 /* If we have an old record available but cannot find one now
164 because the service is not available we keep the old record
165 and make sure it does not get removed. */
166 if (reload_count != UINT_MAX && dh->nreloads == reload_count)
167 /* Do not reset the value if we never not reload the record. */
168 dh->nreloads = reload_count - 1;
170 else
172 /* We have no data. This means we send the standard reply for this
173 case. */
174 if (fd != -1)
175 written = TEMP_FAILURE_RETRY (write (fd, &notfound, total));
177 dataset = mempool_alloc (db, sizeof (struct dataset) + req->key_len);
178 /* If we cannot permanently store the result, so be it. */
179 if (dataset != NULL)
181 dataset->head.allocsize = sizeof (struct dataset) + req->key_len;
182 dataset->head.recsize = total;
183 dataset->head.notfound = true;
184 dataset->head.nreloads = 0;
185 dataset->head.usable = true;
187 /* Compute the timeout time. */
188 dataset->head.timeout = time (NULL) + db->negtimeout;
190 /* This is the reply. */
191 memcpy (&dataset->resp, &notfound, total);
193 /* Copy the key data. */
194 char *key_copy = memcpy (dataset->strdata, key, req->key_len);
196 /* If necessary, we also propagate the data to disk. */
197 if (db->persistent)
199 // XXX async OK?
200 uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
201 msync ((void *) pval,
202 ((uintptr_t) dataset & pagesize_m1)
203 + sizeof (struct dataset) + req->key_len, MS_ASYNC);
206 /* Now get the lock to safely insert the records. */
207 pthread_rwlock_rdlock (&db->lock);
209 if (cache_add (req->type, key_copy, req->key_len,
210 &dataset->head, true, db, uid) < 0)
211 /* Ensure the data can be recovered. */
212 dataset->head.usable = false;
214 pthread_rwlock_unlock (&db->lock);
216 /* Mark the old entry as obsolete. */
217 if (dh != NULL)
218 dh->usable = false;
220 else
221 ++db->head->addfailed;
224 else
227 written = total = sizeof (struct dataset) + start * sizeof (int32_t);
229 /* If we refill the cache, first assume the reconrd did not
230 change. Allocate memory on the cache since it is likely
231 discarded anyway. If it turns out to be necessary to have a
232 new record we can still allocate real memory. */
233 bool alloca_used = false;
234 dataset = NULL;
236 if (he == NULL)
238 dataset = (struct dataset *) mempool_alloc (db,
239 total + req->key_len);
240 if (dataset == NULL)
241 ++db->head->addfailed;
244 if (dataset == NULL)
246 /* We cannot permanently add the result in the moment. But
247 we can provide the result as is. Store the data in some
248 temporary memory. */
249 dataset = (struct dataset *) alloca (total + req->key_len);
251 /* We cannot add this record to the permanent database. */
252 alloca_used = true;
255 dataset->head.allocsize = total + req->key_len;
256 dataset->head.recsize = total - offsetof (struct dataset, resp);
257 dataset->head.notfound = false;
258 dataset->head.nreloads = he == NULL ? 0 : (dh->nreloads + 1);
259 dataset->head.usable = true;
261 /* Compute the timeout time. */
262 dataset->head.timeout = time (NULL) + db->postimeout;
264 dataset->resp.version = NSCD_VERSION;
265 dataset->resp.found = 1;
266 dataset->resp.ngrps = start;
268 char *cp = dataset->strdata;
270 /* Copy the GID values. If the size of the types match this is
271 very simple. */
272 if (sizeof (gid_t) == sizeof (int32_t))
273 cp = mempcpy (cp, groups, start * sizeof (gid_t));
274 else
276 gid_t *gcp = (gid_t *) cp;
278 for (int i = 0; i < start; ++i)
279 *gcp++ = groups[i];
281 cp = (char *) gcp;
284 /* Finally the user name. */
285 memcpy (cp, key, req->key_len);
287 /* Now we can determine whether on refill we have to create a new
288 record or not. */
289 if (he != NULL)
291 assert (fd == -1);
293 if (total + req->key_len == dh->allocsize
294 && total - offsetof (struct dataset, resp) == dh->recsize
295 && memcmp (&dataset->resp, dh->data,
296 dh->allocsize - offsetof (struct dataset, resp)) == 0)
298 /* The data has not changed. We will just bump the
299 timeout value. Note that the new record has been
300 allocated on the stack and need not be freed. */
301 dh->timeout = dataset->head.timeout;
302 ++dh->nreloads;
304 else
306 /* We have to create a new record. Just allocate
307 appropriate memory and copy it. */
308 struct dataset *newp
309 = (struct dataset *) mempool_alloc (db, total + req->key_len);
310 if (newp != NULL)
312 /* Adjust pointer into the memory block. */
313 cp = (char *) newp + (cp - (char *) dataset);
315 dataset = memcpy (newp, dataset, total + req->key_len);
316 alloca_used = false;
319 /* Mark the old record as obsolete. */
320 dh->usable = false;
323 else
325 /* We write the dataset before inserting it to the database
326 since while inserting this thread might block and so would
327 unnecessarily let the receiver wait. */
328 assert (fd != -1);
330 written = TEMP_FAILURE_RETRY (write (fd, &dataset->resp, total));
334 /* Add the record to the database. But only if it has not been
335 stored on the stack. */
336 if (! alloca_used)
338 /* If necessary, we also propagate the data to disk. */
339 if (db->persistent)
341 // XXX async OK?
342 uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
343 msync ((void *) pval,
344 ((uintptr_t) dataset & pagesize_m1) + total +
345 req->key_len, MS_ASYNC);
348 /* Now get the lock to safely insert the records. */
349 pthread_rwlock_rdlock (&db->lock);
351 if (cache_add (INITGROUPS, cp, req->key_len, &dataset->head, true,
352 db, uid) < 0)
353 /* Could not allocate memory. Make sure the data gets
354 discarded. */
355 dataset->head.usable = false;
357 pthread_rwlock_unlock (&db->lock);
361 free (groups);
363 if (__builtin_expect (written != total, 0) && debug_level > 0)
365 char buf[256];
366 dbg_log (_("short write in %s: %s"), __FUNCTION__,
367 strerror_r (errno, buf, sizeof (buf)));
372 void
373 addinitgroups (struct database_dyn *db, int fd, request_header *req, void *key,
374 uid_t uid)
376 addinitgroupsX (db, fd, req, key, uid, NULL, NULL);
380 void
381 readdinitgroups (struct database_dyn *db, struct hashentry *he,
382 struct datahead *dh)
384 request_header req =
386 .type = INITGROUPS,
387 .key_len = he->len
390 addinitgroupsX (db, -1, &req, db->data + he->key, he->owner, he, dh);