Enable SSE2 memset for AMD'supcoming Orochi processor.
[glibc.git] / nscd / initgrcache.c
blob8cb6cf197a198d1afe55e8051aea6d34ba60587f
1 /* Cache handling for host lookup.
2 Copyright (C) 2004, 2005, 2006, 2008, 2009, 2011 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@redhat.com>, 2004.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; version 2 of the License, or
9 (at your option) any later version.
11 This program 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
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 #include <assert.h>
21 #include <errno.h>
22 #include <grp.h>
23 #include <libintl.h>
24 #include <string.h>
25 #include <time.h>
26 #include <unistd.h>
27 #include <sys/mman.h>
29 #include "dbg_log.h"
30 #include "nscd.h"
31 #ifdef HAVE_SENDFILE
32 # include <kernel-features.h>
33 #endif
35 #include "../nss/nsswitch.h"
38 /* Type of the lookup function. */
39 typedef enum nss_status (*initgroups_dyn_function) (const char *, gid_t,
40 long int *, long int *,
41 gid_t **, long int, int *);
44 static const initgr_response_header notfound =
46 .version = NSCD_VERSION,
47 .found = 0,
48 .ngrps = 0
52 #include "../grp/compat-initgroups.c"
55 static time_t
56 addinitgroupsX (struct database_dyn *db, int fd, request_header *req,
57 void *key, uid_t uid, struct hashentry *const he,
58 struct datahead *dh)
60 /* Search for the entry matching the key. Please note that we don't
61 look again in the table whether the dataset is now available. We
62 simply insert it. It does not matter if it is in there twice. The
63 pruning function only will look at the timestamp. */
66 /* We allocate all data in one memory block: the iov vector,
67 the response header and the dataset itself. */
68 struct dataset
70 struct datahead head;
71 initgr_response_header resp;
72 char strdata[0];
73 } *dataset = NULL;
75 if (__builtin_expect (debug_level > 0, 0))
77 if (he == NULL)
78 dbg_log (_("Haven't found \"%s\" in group cache!"), (char *) key);
79 else
80 dbg_log (_("Reloading \"%s\" in group cache!"), (char *) key);
83 static service_user *group_database;
84 service_user *nip = NULL;
85 int no_more;
87 if (group_database != NULL)
89 nip = group_database;
90 no_more = 0;
92 else
93 no_more = __nss_database_lookup ("group", NULL,
94 "compat [NOTFOUND=return] files", &nip);
96 /* We always use sysconf even if NGROUPS_MAX is defined. That way, the
97 limit can be raised in the kernel configuration without having to
98 recompile libc. */
99 long int limit = __sysconf (_SC_NGROUPS_MAX);
101 long int size;
102 if (limit > 0)
103 /* We limit the size of the intially allocated array. */
104 size = MIN (limit, 64);
105 else
106 /* No fixed limit on groups. Pick a starting buffer size. */
107 size = 16;
109 long int start = 0;
110 bool all_tryagain = true;
111 bool any_success = false;
113 /* This is temporary memory, we need not (and 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 any_success |= status == NSS_STATUS_SUCCESS;
165 if (status != NSS_STATUS_SUCCESS
166 && nss_next_action (nip, status) == NSS_ACTION_RETURN)
167 break;
169 if (nip->next == NULL)
170 no_more = -1;
171 else
172 nip = nip->next;
175 ssize_t total;
176 ssize_t written;
177 time_t timeout;
178 out:
179 timeout = MAX_TIMEOUT_VALUE;
180 if (!any_success)
182 /* Nothing found. Create a negative result record. */
183 written = total = sizeof (notfound);
185 if (he != NULL && all_tryagain)
187 /* If we have an old record available but cannot find one now
188 because the service is not available we keep the old record
189 and make sure it does not get removed. */
190 if (reload_count != UINT_MAX && dh->nreloads == reload_count)
191 /* Do not reset the value if we never not reload the record. */
192 dh->nreloads = reload_count - 1;
194 /* Reload with the same time-to-live value. */
195 timeout = dh->timeout = time (NULL) + db->postimeout;
197 else
199 /* We have no data. This means we send the standard reply for this
200 case. */
201 if (fd != -1)
202 written = TEMP_FAILURE_RETRY (send (fd, &notfound, total,
203 MSG_NOSIGNAL));
205 dataset = mempool_alloc (db, sizeof (struct dataset) + req->key_len,
207 /* If we cannot permanently store the result, so be it. */
208 if (dataset != NULL)
210 dataset->head.allocsize = sizeof (struct dataset) + req->key_len;
211 dataset->head.recsize = total;
212 dataset->head.notfound = true;
213 dataset->head.nreloads = 0;
214 dataset->head.usable = true;
216 /* Compute the timeout time. */
217 timeout = dataset->head.timeout = time (NULL) + db->negtimeout;
219 /* This is the reply. */
220 memcpy (&dataset->resp, &notfound, total);
222 /* Copy the key data. */
223 char *key_copy = memcpy (dataset->strdata, key, req->key_len);
225 /* If necessary, we also propagate the data to disk. */
226 if (db->persistent)
228 // XXX async OK?
229 uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
230 msync ((void *) pval,
231 ((uintptr_t) dataset & pagesize_m1)
232 + sizeof (struct dataset) + req->key_len, MS_ASYNC);
235 (void) cache_add (req->type, key_copy, req->key_len,
236 &dataset->head, true, db, uid, he == NULL);
238 pthread_rwlock_unlock (&db->lock);
240 /* Mark the old entry as obsolete. */
241 if (dh != NULL)
242 dh->usable = false;
246 else
249 written = total = (offsetof (struct dataset, strdata)
250 + start * sizeof (int32_t));
252 /* If we refill the cache, first assume the reconrd did not
253 change. Allocate memory on the cache since it is likely
254 discarded anyway. If it turns out to be necessary to have a
255 new record we can still allocate real memory. */
256 bool alloca_used = false;
257 dataset = NULL;
259 if (he == NULL)
260 dataset = (struct dataset *) mempool_alloc (db, total + req->key_len,
263 if (dataset == NULL)
265 /* We cannot permanently add the result in the moment. But
266 we can provide the result as is. Store the data in some
267 temporary memory. */
268 dataset = (struct dataset *) alloca (total + req->key_len);
270 /* We cannot add this record to the permanent database. */
271 alloca_used = true;
274 dataset->head.allocsize = total + req->key_len;
275 dataset->head.recsize = total - offsetof (struct dataset, resp);
276 dataset->head.notfound = false;
277 dataset->head.nreloads = he == NULL ? 0 : (dh->nreloads + 1);
278 dataset->head.usable = true;
280 /* Compute the timeout time. */
281 timeout = dataset->head.timeout = time (NULL) + db->postimeout;
283 dataset->resp.version = NSCD_VERSION;
284 dataset->resp.found = 1;
285 dataset->resp.ngrps = start;
287 char *cp = dataset->strdata;
289 /* Copy the GID values. If the size of the types match this is
290 very simple. */
291 if (sizeof (gid_t) == sizeof (int32_t))
292 cp = mempcpy (cp, groups, start * sizeof (gid_t));
293 else
295 gid_t *gcp = (gid_t *) cp;
297 for (int i = 0; i < start; ++i)
298 *gcp++ = groups[i];
300 cp = (char *) gcp;
303 /* Finally the user name. */
304 memcpy (cp, key, req->key_len);
306 assert (cp == dataset->strdata + total - offsetof (struct dataset,
307 strdata));
309 /* Now we can determine whether on refill we have to create a new
310 record or not. */
311 if (he != NULL)
313 assert (fd == -1);
315 if (total + req->key_len == dh->allocsize
316 && total - offsetof (struct dataset, resp) == dh->recsize
317 && memcmp (&dataset->resp, dh->data,
318 dh->allocsize - offsetof (struct dataset, resp)) == 0)
320 /* The data has not changed. We will just bump the
321 timeout value. Note that the new record has been
322 allocated on the stack and need not be freed. */
323 dh->timeout = dataset->head.timeout;
324 ++dh->nreloads;
326 else
328 /* We have to create a new record. Just allocate
329 appropriate memory and copy it. */
330 struct dataset *newp
331 = (struct dataset *) mempool_alloc (db, total + req->key_len,
333 if (newp != NULL)
335 /* Adjust pointer into the memory block. */
336 cp = (char *) newp + (cp - (char *) dataset);
338 dataset = memcpy (newp, dataset, total + req->key_len);
339 alloca_used = false;
342 /* Mark the old record as obsolete. */
343 dh->usable = false;
346 else
348 /* We write the dataset before inserting it to the database
349 since while inserting this thread might block and so would
350 unnecessarily let the receiver wait. */
351 assert (fd != -1);
353 #ifdef HAVE_SENDFILE
354 if (__builtin_expect (db->mmap_used, 1) && !alloca_used)
356 assert (db->wr_fd != -1);
357 assert ((char *) &dataset->resp > (char *) db->data);
358 assert ((char *) dataset - (char *) db->head
359 + total
360 <= (sizeof (struct database_pers_head)
361 + db->head->module * sizeof (ref_t)
362 + db->head->data_size));
363 written = sendfileall (fd, db->wr_fd,
364 (char *) &dataset->resp
365 - (char *) db->head, dataset->head.recsize);
366 # ifndef __ASSUME_SENDFILE
367 if (written == -1 && errno == ENOSYS)
368 goto use_write;
369 # endif
371 else
372 # ifndef __ASSUME_SENDFILE
373 use_write:
374 # endif
375 #endif
376 written = writeall (fd, &dataset->resp, dataset->head.recsize);
380 /* Add the record to the database. But only if it has not been
381 stored on the stack. */
382 if (! alloca_used)
384 /* If necessary, we also propagate the data to disk. */
385 if (db->persistent)
387 // XXX async OK?
388 uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
389 msync ((void *) pval,
390 ((uintptr_t) dataset & pagesize_m1) + total +
391 req->key_len, MS_ASYNC);
394 (void) cache_add (INITGROUPS, cp, req->key_len, &dataset->head, true,
395 db, uid, he == NULL);
397 pthread_rwlock_unlock (&db->lock);
401 free (groups);
403 if (__builtin_expect (written != total, 0) && debug_level > 0)
405 char buf[256];
406 dbg_log (_("short write in %s: %s"), __FUNCTION__,
407 strerror_r (errno, buf, sizeof (buf)));
410 return timeout;
414 void
415 addinitgroups (struct database_dyn *db, int fd, request_header *req, void *key,
416 uid_t uid)
418 addinitgroupsX (db, fd, req, key, uid, NULL, NULL);
422 time_t
423 readdinitgroups (struct database_dyn *db, struct hashentry *he,
424 struct datahead *dh)
426 request_header req =
428 .type = INITGROUPS,
429 .key_len = he->len
432 return addinitgroupsX (db, -1, &req, db->data + he->key, he->owner, he, dh);