CJK locales: Add fullwidth yYnN to yesexpr and noexpr [BZ #15261]
[glibc.git] / nscd / initgrcache.c
blob4114fbe4e3b3614bad475882e40d52c0b12e8b3e
1 /* Cache handling for host lookup.
2 Copyright (C) 2004-2017 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, see <http://www.gnu.org/licenses/>. */
19 #include <assert.h>
20 #include <errno.h>
21 #include <grp.h>
22 #include <libintl.h>
23 #include <string.h>
24 #include <time.h>
25 #include <unistd.h>
26 #include <sys/mman.h>
27 #include <scratch_buffer.h>
28 #include <config.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"
38 #ifdef LINK_OBSOLETE_NSL
39 # define DEFAULT_CONFIG "compat [NOTFOUND=return] files"
40 #else
41 # define DEFAULT_CONFIG "files"
42 #endif
44 /* Type of the lookup function. */
45 typedef enum nss_status (*initgroups_dyn_function) (const char *, gid_t,
46 long int *, long int *,
47 gid_t **, long int, int *);
50 static const initgr_response_header notfound =
52 .version = NSCD_VERSION,
53 .found = 0,
54 .ngrps = 0
58 #include "../grp/compat-initgroups.c"
61 static time_t
62 addinitgroupsX (struct database_dyn *db, int fd, request_header *req,
63 void *key, uid_t uid, struct hashentry *const he,
64 struct datahead *dh)
66 /* Search for the entry matching the key. Please note that we don't
67 look again in the table whether the dataset is now available. We
68 simply insert it. It does not matter if it is in there twice. The
69 pruning function only will look at the timestamp. */
72 /* We allocate all data in one memory block: the iov vector,
73 the response header and the dataset itself. */
74 struct dataset
76 struct datahead head;
77 initgr_response_header resp;
78 char strdata[0];
79 } *dataset = NULL;
81 if (__glibc_unlikely (debug_level > 0))
83 if (he == NULL)
84 dbg_log (_("Haven't found \"%s\" in group cache!"), (char *) key);
85 else
86 dbg_log (_("Reloading \"%s\" in group cache!"), (char *) key);
89 static service_user *group_database;
90 service_user *nip;
91 int no_more;
93 if (group_database == NULL)
94 no_more = __nss_database_lookup ("group", NULL, DEFAULT_CONFIG,
95 &group_database);
96 else
97 no_more = 0;
98 nip = group_database;
100 /* We always use sysconf even if NGROUPS_MAX is defined. That way, the
101 limit can be raised in the kernel configuration without having to
102 recompile libc. */
103 long int limit = __sysconf (_SC_NGROUPS_MAX);
105 long int size;
106 if (limit > 0)
107 /* We limit the size of the intially allocated array. */
108 size = MIN (limit, 64);
109 else
110 /* No fixed limit on groups. Pick a starting buffer size. */
111 size = 16;
113 long int start = 0;
114 bool all_tryagain = true;
115 bool any_success = false;
117 /* This is temporary memory, we need not (and must not) call
118 mempool_alloc. */
119 // XXX This really should use alloca. need to change the backends.
120 gid_t *groups = (gid_t *) malloc (size * sizeof (gid_t));
121 if (__glibc_unlikely (groups == NULL))
122 /* No more memory. */
123 goto out;
125 /* Nothing added yet. */
126 while (! no_more)
128 long int prev_start = start;
129 enum nss_status status;
130 initgroups_dyn_function fct;
131 fct = __nss_lookup_function (nip, "initgroups_dyn");
133 if (fct == NULL)
135 status = compat_call (nip, key, -1, &start, &size, &groups,
136 limit, &errno);
138 if (nss_next_action (nip, NSS_STATUS_UNAVAIL) != NSS_ACTION_CONTINUE)
139 break;
141 else
142 status = DL_CALL_FCT (fct, (key, -1, &start, &size, &groups,
143 limit, &errno));
145 /* Remove duplicates. */
146 long int cnt = prev_start;
147 while (cnt < start)
149 long int inner;
150 for (inner = 0; inner < prev_start; ++inner)
151 if (groups[inner] == groups[cnt])
152 break;
154 if (inner < prev_start)
155 groups[cnt] = groups[--start];
156 else
157 ++cnt;
160 if (status != NSS_STATUS_TRYAGAIN)
161 all_tryagain = false;
163 /* This is really only for debugging. */
164 if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_RETURN)
165 __libc_fatal ("illegal status in internal_getgrouplist");
167 any_success |= status == NSS_STATUS_SUCCESS;
169 if (status != NSS_STATUS_SUCCESS
170 && nss_next_action (nip, status) == NSS_ACTION_RETURN)
171 break;
173 if (nip->next == NULL)
174 no_more = -1;
175 else
176 nip = nip->next;
179 bool all_written;
180 ssize_t total;
181 time_t timeout;
182 out:
183 all_written = true;
184 timeout = MAX_TIMEOUT_VALUE;
185 if (!any_success)
187 /* Nothing found. Create a negative result record. */
188 total = sizeof (notfound);
190 if (he != NULL && all_tryagain)
192 /* If we have an old record available but cannot find one now
193 because the service is not available we keep the old record
194 and make sure it does not get removed. */
195 if (reload_count != UINT_MAX && dh->nreloads == reload_count)
196 /* Do not reset the value if we never not reload the record. */
197 dh->nreloads = reload_count - 1;
199 /* Reload with the same time-to-live value. */
200 timeout = dh->timeout = time (NULL) + db->postimeout;
202 else
204 /* We have no data. This means we send the standard reply for this
205 case. */
206 if (fd != -1
207 && TEMP_FAILURE_RETRY (send (fd, &notfound, total,
208 MSG_NOSIGNAL)) != total)
209 all_written = false;
211 /* If we have a transient error or cannot permanently store
212 the result, so be it. */
213 if (all_tryagain || __builtin_expect (db->negtimeout == 0, 0))
215 /* Mark the old entry as obsolete. */
216 if (dh != NULL)
217 dh->usable = false;
219 else if ((dataset = mempool_alloc (db, (sizeof (struct dataset)
220 + req->key_len), 1)) != NULL)
222 timeout = datahead_init_neg (&dataset->head,
223 (sizeof (struct dataset)
224 + req->key_len), total,
225 db->negtimeout);
227 /* This is the reply. */
228 memcpy (&dataset->resp, &notfound, total);
230 /* Copy the key data. */
231 char *key_copy = memcpy (dataset->strdata, key, req->key_len);
233 /* If necessary, we also propagate the data to disk. */
234 if (db->persistent)
236 // XXX async OK?
237 uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
238 msync ((void *) pval,
239 ((uintptr_t) dataset & pagesize_m1)
240 + sizeof (struct dataset) + req->key_len, MS_ASYNC);
243 (void) cache_add (req->type, key_copy, req->key_len,
244 &dataset->head, true, db, uid, he == NULL);
246 pthread_rwlock_unlock (&db->lock);
248 /* Mark the old entry as obsolete. */
249 if (dh != NULL)
250 dh->usable = false;
254 else
257 total = offsetof (struct dataset, strdata) + start * sizeof (int32_t);
259 /* If we refill the cache, first assume the reconrd did not
260 change. Allocate memory on the cache since it is likely
261 discarded anyway. If it turns out to be necessary to have a
262 new record we can still allocate real memory. */
263 bool alloca_used = false;
264 dataset = NULL;
266 if (he == NULL)
267 dataset = (struct dataset *) mempool_alloc (db, total + req->key_len,
270 if (dataset == NULL)
272 /* We cannot permanently add the result in the moment. But
273 we can provide the result as is. Store the data in some
274 temporary memory. */
275 dataset = (struct dataset *) alloca (total + req->key_len);
277 /* We cannot add this record to the permanent database. */
278 alloca_used = true;
281 timeout = datahead_init_pos (&dataset->head, total + req->key_len,
282 total - offsetof (struct dataset, resp),
283 he == NULL ? 0 : dh->nreloads + 1,
284 db->postimeout);
286 dataset->resp.version = NSCD_VERSION;
287 dataset->resp.found = 1;
288 dataset->resp.ngrps = start;
290 char *cp = dataset->strdata;
292 /* Copy the GID values. If the size of the types match this is
293 very simple. */
294 if (sizeof (gid_t) == sizeof (int32_t))
295 cp = mempcpy (cp, groups, start * sizeof (gid_t));
296 else
298 gid_t *gcp = (gid_t *) cp;
300 for (int i = 0; i < start; ++i)
301 *gcp++ = groups[i];
303 cp = (char *) gcp;
306 /* Finally the user name. */
307 memcpy (cp, key, req->key_len);
309 assert (cp == dataset->strdata + total - offsetof (struct dataset,
310 strdata));
312 /* Now we can determine whether on refill we have to create a new
313 record or not. */
314 if (he != NULL)
316 assert (fd == -1);
318 if (total + req->key_len == dh->allocsize
319 && total - offsetof (struct dataset, resp) == dh->recsize
320 && memcmp (&dataset->resp, dh->data,
321 dh->allocsize - offsetof (struct dataset, resp)) == 0)
323 /* The data has not changed. We will just bump the
324 timeout value. Note that the new record has been
325 allocated on the stack and need not be freed. */
326 dh->timeout = dataset->head.timeout;
327 ++dh->nreloads;
329 else
331 /* We have to create a new record. Just allocate
332 appropriate memory and copy it. */
333 struct dataset *newp
334 = (struct dataset *) mempool_alloc (db, total + req->key_len,
336 if (newp != NULL)
338 /* Adjust pointer into the memory block. */
339 cp = (char *) newp + (cp - (char *) dataset);
341 dataset = memcpy (newp, dataset, total + req->key_len);
342 alloca_used = false;
345 /* Mark the old record as obsolete. */
346 dh->usable = false;
349 else
351 /* We write the dataset before inserting it to the database
352 since while inserting this thread might block and so would
353 unnecessarily let the receiver wait. */
354 assert (fd != -1);
356 #ifdef HAVE_SENDFILE
357 if (__builtin_expect (db->mmap_used, 1) && !alloca_used)
359 assert (db->wr_fd != -1);
360 assert ((char *) &dataset->resp > (char *) db->data);
361 assert ((char *) dataset - (char *) db->head
362 + total
363 <= (sizeof (struct database_pers_head)
364 + db->head->module * sizeof (ref_t)
365 + db->head->data_size));
366 ssize_t written = sendfileall (fd, db->wr_fd,
367 (char *) &dataset->resp
368 - (char *) db->head,
369 dataset->head.recsize);
370 if (written != dataset->head.recsize)
372 # ifndef __ASSUME_SENDFILE
373 if (written == -1 && errno == ENOSYS)
374 goto use_write;
375 # endif
376 all_written = false;
379 else
380 # ifndef __ASSUME_SENDFILE
381 use_write:
382 # endif
383 #endif
384 if (writeall (fd, &dataset->resp, dataset->head.recsize)
385 != dataset->head.recsize)
386 all_written = false;
390 /* Add the record to the database. But only if it has not been
391 stored on the stack. */
392 if (! alloca_used)
394 /* If necessary, we also propagate the data to disk. */
395 if (db->persistent)
397 // XXX async OK?
398 uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
399 msync ((void *) pval,
400 ((uintptr_t) dataset & pagesize_m1) + total +
401 req->key_len, MS_ASYNC);
404 (void) cache_add (INITGROUPS, cp, req->key_len, &dataset->head, true,
405 db, uid, he == NULL);
407 pthread_rwlock_unlock (&db->lock);
411 free (groups);
413 if (__builtin_expect (!all_written, 0) && debug_level > 0)
415 char buf[256];
416 dbg_log (_("short write in %s: %s"), __FUNCTION__,
417 strerror_r (errno, buf, sizeof (buf)));
420 return timeout;
424 void
425 addinitgroups (struct database_dyn *db, int fd, request_header *req, void *key,
426 uid_t uid)
428 addinitgroupsX (db, fd, req, key, uid, NULL, NULL);
432 time_t
433 readdinitgroups (struct database_dyn *db, struct hashentry *he,
434 struct datahead *dh)
436 request_header req =
438 .type = INITGROUPS,
439 .key_len = he->len
442 return addinitgroupsX (db, -1, &req, db->data + he->key, he->owner, he, dh);