* sysdeps/hppa/bits/setjmp.h: Add _BITS_SETJMP_H preprocessor
[glibc.git] / nscd / initgrcache.c
blob23d5d59a11039357c63bad10b9898c5361ed8bf5
1 /* Cache handling for host lookup.
2 Copyright (C) 2004, 2005 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>
30 #include "dbg_log.h"
31 #include "nscd.h"
32 #ifdef HAVE_SENDFILE
33 # include <kernel-features.h>
34 #endif
36 #include "../nss/nsswitch.h"
39 /* Type of the lookup function. */
40 typedef enum nss_status (*initgroups_dyn_function) (const char *, gid_t,
41 long int *, long int *,
42 gid_t **, long int, int *);
45 static const initgr_response_header notfound =
47 .version = NSCD_VERSION,
48 .found = 0,
49 .ngrps = 0
53 #include "../grp/compat-initgroups.c"
56 static void
57 addinitgroupsX (struct database_dyn *db, int fd, request_header *req,
58 void *key, uid_t uid, struct hashentry *he,
59 struct datahead *dh)
61 /* Search for the entry matching the key. Please note that we don't
62 look again in the table whether the dataset is now available. We
63 simply insert it. It does not matter if it is in there twice. The
64 pruning function only will look at the timestamp. */
67 /* We allocate all data in one memory block: the iov vector,
68 the response header and the dataset itself. */
69 struct dataset
71 struct datahead head;
72 initgr_response_header resp;
73 char strdata[0];
74 } *dataset = NULL;
76 if (__builtin_expect (debug_level > 0, 0))
78 if (he == NULL)
79 dbg_log (_("Haven't found \"%s\" in group cache!"), (char *) key);
80 else
81 dbg_log (_("Reloading \"%s\" in group cache!"), (char *) key);
84 static service_user *group_database;
85 service_user *nip = NULL;
86 int no_more;
88 if (group_database != NULL)
90 nip = group_database;
91 no_more = 0;
93 else
94 no_more = __nss_database_lookup ("group", NULL,
95 "compat [NOTFOUND=return] files", &nip);
97 /* We always use sysconf even if NGROUPS_MAX is defined. That way, the
98 limit can be raised in the kernel configuration without having to
99 recompile libc. */
100 long int limit = __sysconf (_SC_NGROUPS_MAX);
102 long int size;
103 if (limit > 0)
104 /* We limit the size of the intially allocated array. */
105 size = MIN (limit, 64);
106 else
107 /* No fixed limit on groups. Pick a starting buffer size. */
108 size = 16;
110 long int start = 0;
111 bool all_tryagain = true;
113 /* This is temporary memory, we need not (ad must not) call
114 mempool_alloc. */
115 // XXX This really should use alloca. need to change the backends.
116 gid_t *groups = (gid_t *) malloc (size * sizeof (gid_t));
117 if (__builtin_expect (groups == NULL, 0))
118 /* No more memory. */
119 goto out;
121 /* Nothing added yet. */
122 while (! no_more)
124 long int prev_start = start;
125 enum nss_status status;
126 initgroups_dyn_function fct;
127 fct = __nss_lookup_function (nip, "initgroups_dyn");
129 if (fct == NULL)
131 status = compat_call (nip, key, -1, &start, &size, &groups,
132 limit, &errno);
134 if (nss_next_action (nip, NSS_STATUS_UNAVAIL) != NSS_ACTION_CONTINUE)
135 break;
137 else
138 status = DL_CALL_FCT (fct, (key, -1, &start, &size, &groups,
139 limit, &errno));
141 /* Remove duplicates. */
142 long int cnt = prev_start;
143 while (cnt < start)
145 long int inner;
146 for (inner = 0; inner < prev_start; ++inner)
147 if (groups[inner] == groups[cnt])
148 break;
150 if (inner < prev_start)
151 groups[cnt] = groups[--start];
152 else
153 ++cnt;
156 if (status != NSS_STATUS_TRYAGAIN)
157 all_tryagain = false;
159 /* This is really only for debugging. */
160 if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_RETURN)
161 __libc_fatal ("illegal status in internal_getgrouplist");
163 if (status != NSS_STATUS_SUCCESS
164 && nss_next_action (nip, status) == NSS_ACTION_RETURN)
165 break;
167 if (nip->next == NULL)
168 no_more = -1;
169 else
170 nip = nip->next;
173 ssize_t total;
174 ssize_t written;
175 out:
176 if (start == 0)
178 /* Nothing found. Create a negative result record. */
179 written = total = sizeof (notfound);
181 if (he != NULL && all_tryagain)
183 /* If we have an old record available but cannot find one now
184 because the service is not available we keep the old record
185 and make sure it does not get removed. */
186 if (reload_count != UINT_MAX && dh->nreloads == reload_count)
187 /* Do not reset the value if we never not reload the record. */
188 dh->nreloads = reload_count - 1;
190 else
192 /* We have no data. This means we send the standard reply for this
193 case. */
194 if (fd != -1)
195 written = TEMP_FAILURE_RETRY (send (fd, &notfound, total,
196 MSG_NOSIGNAL));
198 dataset = mempool_alloc (db, sizeof (struct dataset) + req->key_len);
199 /* If we cannot permanently store the result, so be it. */
200 if (dataset != NULL)
202 dataset->head.allocsize = sizeof (struct dataset) + req->key_len;
203 dataset->head.recsize = total;
204 dataset->head.notfound = true;
205 dataset->head.nreloads = 0;
206 dataset->head.usable = true;
208 /* Compute the timeout time. */
209 dataset->head.timeout = time (NULL) + db->negtimeout;
211 /* This is the reply. */
212 memcpy (&dataset->resp, &notfound, total);
214 /* Copy the key data. */
215 char *key_copy = memcpy (dataset->strdata, key, req->key_len);
217 /* If necessary, we also propagate the data to disk. */
218 if (db->persistent)
220 // XXX async OK?
221 uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
222 msync ((void *) pval,
223 ((uintptr_t) dataset & pagesize_m1)
224 + sizeof (struct dataset) + req->key_len, MS_ASYNC);
227 /* Now get the lock to safely insert the records. */
228 pthread_rwlock_rdlock (&db->lock);
230 if (cache_add (req->type, key_copy, req->key_len,
231 &dataset->head, true, db, uid) < 0)
232 /* Ensure the data can be recovered. */
233 dataset->head.usable = false;
235 pthread_rwlock_unlock (&db->lock);
237 /* Mark the old entry as obsolete. */
238 if (dh != NULL)
239 dh->usable = false;
241 else
242 ++db->head->addfailed;
245 else
248 written = total = sizeof (struct dataset) + start * sizeof (int32_t);
250 /* If we refill the cache, first assume the reconrd did not
251 change. Allocate memory on the cache since it is likely
252 discarded anyway. If it turns out to be necessary to have a
253 new record we can still allocate real memory. */
254 bool alloca_used = false;
255 dataset = NULL;
257 if (he == NULL)
259 dataset = (struct dataset *) mempool_alloc (db,
260 total + req->key_len);
261 if (dataset == NULL)
262 ++db->head->addfailed;
265 if (dataset == NULL)
267 /* We cannot permanently add the result in the moment. But
268 we can provide the result as is. Store the data in some
269 temporary memory. */
270 dataset = (struct dataset *) alloca (total + req->key_len);
272 /* We cannot add this record to the permanent database. */
273 alloca_used = true;
276 dataset->head.allocsize = total + req->key_len;
277 dataset->head.recsize = total - offsetof (struct dataset, resp);
278 dataset->head.notfound = false;
279 dataset->head.nreloads = he == NULL ? 0 : (dh->nreloads + 1);
280 dataset->head.usable = true;
282 /* Compute the timeout time. */
283 dataset->head.timeout = time (NULL) + db->postimeout;
285 dataset->resp.version = NSCD_VERSION;
286 dataset->resp.found = 1;
287 dataset->resp.ngrps = start;
289 char *cp = dataset->strdata;
291 /* Copy the GID values. If the size of the types match this is
292 very simple. */
293 if (sizeof (gid_t) == sizeof (int32_t))
294 cp = mempcpy (cp, groups, start * sizeof (gid_t));
295 else
297 gid_t *gcp = (gid_t *) cp;
299 for (int i = 0; i < start; ++i)
300 *gcp++ = groups[i];
302 cp = (char *) gcp;
305 /* Finally the user name. */
306 memcpy (cp, key, req->key_len);
308 /* Now we can determine whether on refill we have to create a new
309 record or not. */
310 if (he != NULL)
312 assert (fd == -1);
314 if (total + req->key_len == dh->allocsize
315 && total - offsetof (struct dataset, resp) == dh->recsize
316 && memcmp (&dataset->resp, dh->data,
317 dh->allocsize - offsetof (struct dataset, resp)) == 0)
319 /* The data has not changed. We will just bump the
320 timeout value. Note that the new record has been
321 allocated on the stack and need not be freed. */
322 dh->timeout = dataset->head.timeout;
323 ++dh->nreloads;
325 else
327 /* We have to create a new record. Just allocate
328 appropriate memory and copy it. */
329 struct dataset *newp
330 = (struct dataset *) mempool_alloc (db, total + req->key_len);
331 if (newp != NULL)
333 /* Adjust pointer into the memory block. */
334 cp = (char *) newp + (cp - (char *) dataset);
336 dataset = memcpy (newp, dataset, total + req->key_len);
337 alloca_used = false;
340 /* Mark the old record as obsolete. */
341 dh->usable = false;
344 else
346 /* We write the dataset before inserting it to the database
347 since while inserting this thread might block and so would
348 unnecessarily let the receiver wait. */
349 assert (fd != -1);
351 #ifdef HAVE_SENDFILE
352 if (__builtin_expect (db->mmap_used, 1))
354 assert (db->wr_fd != -1);
355 assert ((char *) &dataset->resp > (char *) db->data);
356 assert ((char *) &dataset->resp - (char *) db->head
357 + total
358 <= (sizeof (struct database_pers_head)
359 + db->head->module * sizeof (ref_t)
360 + db->head->data_size));
361 written = sendfileall (fd, db->wr_fd,
362 (char *) &dataset->resp
363 - (char *) db->head, total);
364 # ifndef __ASSUME_SENDFILE
365 if (written == -1 && errno == ENOSYS)
366 goto use_write;
367 # endif
369 else
370 # ifndef __ASSUME_SENDFILE
371 use_write:
372 # endif
373 #endif
374 written = writeall (fd, &dataset->resp, total);
378 /* Add the record to the database. But only if it has not been
379 stored on the stack. */
380 if (! alloca_used)
382 /* If necessary, we also propagate the data to disk. */
383 if (db->persistent)
385 // XXX async OK?
386 uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
387 msync ((void *) pval,
388 ((uintptr_t) dataset & pagesize_m1) + total +
389 req->key_len, MS_ASYNC);
392 /* Now get the lock to safely insert the records. */
393 pthread_rwlock_rdlock (&db->lock);
395 if (cache_add (INITGROUPS, cp, req->key_len, &dataset->head, true,
396 db, uid) < 0)
397 /* Could not allocate memory. Make sure the data gets
398 discarded. */
399 dataset->head.usable = false;
401 pthread_rwlock_unlock (&db->lock);
405 free (groups);
407 if (__builtin_expect (written != total, 0) && debug_level > 0)
409 char buf[256];
410 dbg_log (_("short write in %s: %s"), __FUNCTION__,
411 strerror_r (errno, buf, sizeof (buf)));
416 void
417 addinitgroups (struct database_dyn *db, int fd, request_header *req, void *key,
418 uid_t uid)
420 addinitgroupsX (db, fd, req, key, uid, NULL, NULL);
424 void
425 readdinitgroups (struct database_dyn *db, struct hashentry *he,
426 struct datahead *dh)
428 request_header req =
430 .type = INITGROUPS,
431 .key_len = he->len
434 addinitgroupsX (db, -1, &req, db->data + he->key, he->owner, he, dh);