]> git.ipfire.org Git - thirdparty/glibc.git/blame - nscd/initgrcache.c
Fix mutex pretty printer test and pretty printer output.
[thirdparty/glibc.git] / nscd / initgrcache.c
CommitLineData
f7e7a396 1/* Cache handling for host lookup.
bfff8b1b 2 Copyright (C) 2004-2017 Free Software Foundation, Inc.
f7e7a396
UD
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@redhat.com>, 2004.
5
43bc8ac6 6 This program is free software; you can redistribute it and/or modify
2e2efe65
RM
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.
f7e7a396 10
43bc8ac6 11 This program is distributed in the hope that it will be useful,
f7e7a396 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
43bc8ac6
UD
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
f7e7a396 15
43bc8ac6 16 You should have received a copy of the GNU General Public License
59ba27a6 17 along with this program; if not, see <http://www.gnu.org/licenses/>. */
f7e7a396
UD
18
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>
866ba63b 27#include <scratch_buffer.h>
eac10791
UD
28
29#include "dbg_log.h"
30#include "nscd.h"
31#ifdef HAVE_SENDFILE
32# include <kernel-features.h>
33#endif
f7e7a396
UD
34
35#include "../nss/nsswitch.h"
36
37
38/* Type of the lookup function. */
39typedef enum nss_status (*initgroups_dyn_function) (const char *, gid_t,
40 long int *, long int *,
41 gid_t **, long int, int *);
42
43
44static const initgr_response_header notfound =
45{
46 .version = NSCD_VERSION,
47 .found = 0,
48 .ngrps = 0
49};
50
51
52#include "../grp/compat-initgroups.c"
53
54
a4c7ea7b 55static time_t
f7e7a396 56addinitgroupsX (struct database_dyn *db, int fd, request_header *req,
20e498bd 57 void *key, uid_t uid, struct hashentry *const he,
f7e7a396
UD
58 struct datahead *dh)
59{
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. */
64
65
66 /* We allocate all data in one memory block: the iov vector,
67 the response header and the dataset itself. */
68 struct dataset
69 {
70 struct datahead head;
71 initgr_response_header resp;
72 char strdata[0];
73 } *dataset = NULL;
74
a1ffb40e 75 if (__glibc_unlikely (debug_level > 0))
f7e7a396
UD
76 {
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);
81 }
82
83 static service_user *group_database;
b2179107 84 service_user *nip;
f7e7a396
UD
85 int no_more;
86
b2179107 87 if (group_database == NULL)
f7e7a396 88 no_more = __nss_database_lookup ("group", NULL,
b2179107
AS
89 "compat [NOTFOUND=return] files",
90 &group_database);
91 else
92 no_more = 0;
93 nip = group_database;
f7e7a396
UD
94
95 /* We always use sysconf even if NGROUPS_MAX is defined. That way, the
96 limit can be raised in the kernel configuration without having to
97 recompile libc. */
98 long int limit = __sysconf (_SC_NGROUPS_MAX);
99
100 long int size;
101 if (limit > 0)
102 /* We limit the size of the intially allocated array. */
103 size = MIN (limit, 64);
104 else
105 /* No fixed limit on groups. Pick a starting buffer size. */
106 size = 16;
107
108 long int start = 0;
109 bool all_tryagain = true;
cd248c3f 110 bool any_success = false;
f7e7a396 111
e8667ddc 112 /* This is temporary memory, we need not (and must not) call
f7e7a396
UD
113 mempool_alloc. */
114 // XXX This really should use alloca. need to change the backends.
115 gid_t *groups = (gid_t *) malloc (size * sizeof (gid_t));
a1ffb40e 116 if (__glibc_unlikely (groups == NULL))
f7e7a396
UD
117 /* No more memory. */
118 goto out;
119
120 /* Nothing added yet. */
121 while (! no_more)
122 {
695c4370 123 long int prev_start = start;
f7e7a396
UD
124 enum nss_status status;
125 initgroups_dyn_function fct;
126 fct = __nss_lookup_function (nip, "initgroups_dyn");
127
128 if (fct == NULL)
129 {
130 status = compat_call (nip, key, -1, &start, &size, &groups,
131 limit, &errno);
132
133 if (nss_next_action (nip, NSS_STATUS_UNAVAIL) != NSS_ACTION_CONTINUE)
134 break;
135 }
136 else
137 status = DL_CALL_FCT (fct, (key, -1, &start, &size, &groups,
138 limit, &errno));
139
695c4370
UD
140 /* Remove duplicates. */
141 long int cnt = prev_start;
142 while (cnt < start)
143 {
144 long int inner;
145 for (inner = 0; inner < prev_start; ++inner)
146 if (groups[inner] == groups[cnt])
147 break;
148
149 if (inner < prev_start)
150 groups[cnt] = groups[--start];
151 else
152 ++cnt;
153 }
154
f7e7a396
UD
155 if (status != NSS_STATUS_TRYAGAIN)
156 all_tryagain = false;
157
158 /* This is really only for debugging. */
159 if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_RETURN)
160 __libc_fatal ("illegal status in internal_getgrouplist");
161
fbbc73b3
UD
162 any_success |= status == NSS_STATUS_SUCCESS;
163
f7e7a396
UD
164 if (status != NSS_STATUS_SUCCESS
165 && nss_next_action (nip, status) == NSS_ACTION_RETURN)
166 break;
167
168 if (nip->next == NULL)
169 no_more = -1;
170 else
171 nip = nip->next;
172 }
173
306dfba9 174 bool all_written;
f7e7a396 175 ssize_t total;
a4c7ea7b 176 time_t timeout;
f7e7a396 177 out:
306dfba9 178 all_written = true;
a4c7ea7b 179 timeout = MAX_TIMEOUT_VALUE;
fbbc73b3 180 if (!any_success)
f7e7a396
UD
181 {
182 /* Nothing found. Create a negative result record. */
306dfba9 183 total = sizeof (notfound);
f7e7a396
UD
184
185 if (he != NULL && all_tryagain)
186 {
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;
a4c7ea7b
UD
193
194 /* Reload with the same time-to-live value. */
195 timeout = dh->timeout = time (NULL) + db->postimeout;
f7e7a396
UD
196 }
197 else
198 {
199 /* We have no data. This means we send the standard reply for this
200 case. */
306dfba9
AS
201 if (fd != -1
202 && TEMP_FAILURE_RETRY (send (fd, &notfound, total,
203 MSG_NOSIGNAL)) != total)
204 all_written = false;
f7e7a396 205
3e1aa84e
UD
206 /* If we have a transient error or cannot permanently store
207 the result, so be it. */
208 if (all_tryagain || __builtin_expect (db->negtimeout == 0, 0))
445b4a53
TK
209 {
210 /* Mark the old entry as obsolete. */
211 if (dh != NULL)
212 dh->usable = false;
213 }
99231d9a
UD
214 else if ((dataset = mempool_alloc (db, (sizeof (struct dataset)
215 + req->key_len), 1)) != NULL)
f7e7a396 216 {
1cdeb237
SP
217 timeout = datahead_init_neg (&dataset->head,
218 (sizeof (struct dataset)
219 + req->key_len), total,
220 db->negtimeout);
f7e7a396
UD
221
222 /* This is the reply. */
223 memcpy (&dataset->resp, &notfound, total);
224
225 /* Copy the key data. */
226 char *key_copy = memcpy (dataset->strdata, key, req->key_len);
227
228 /* If necessary, we also propagate the data to disk. */
229 if (db->persistent)
230 {
231 // XXX async OK?
232 uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
233 msync ((void *) pval,
234 ((uintptr_t) dataset & pagesize_m1)
235 + sizeof (struct dataset) + req->key_len, MS_ASYNC);
236 }
237
7e71e55f 238 (void) cache_add (req->type, key_copy, req->key_len,
528741cb 239 &dataset->head, true, db, uid, he == NULL);
f7e7a396 240
00ebd7ed
UD
241 pthread_rwlock_unlock (&db->lock);
242
f7e7a396
UD
243 /* Mark the old entry as obsolete. */
244 if (dh != NULL)
245 dh->usable = false;
246 }
f7e7a396
UD
247 }
248 }
249 else
250 {
251
306dfba9 252 total = offsetof (struct dataset, strdata) + start * sizeof (int32_t);
f7e7a396
UD
253
254 /* If we refill the cache, first assume the reconrd did not
255 change. Allocate memory on the cache since it is likely
256 discarded anyway. If it turns out to be necessary to have a
257 new record we can still allocate real memory. */
258 bool alloca_used = false;
259 dataset = NULL;
260
261 if (he == NULL)
20e498bd
UD
262 dataset = (struct dataset *) mempool_alloc (db, total + req->key_len,
263 1);
f7e7a396
UD
264
265 if (dataset == NULL)
266 {
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);
271
272 /* We cannot add this record to the permanent database. */
273 alloca_used = true;
274 }
275
1cdeb237
SP
276 timeout = datahead_init_pos (&dataset->head, total + req->key_len,
277 total - offsetof (struct dataset, resp),
278 he == NULL ? 0 : dh->nreloads + 1,
279 db->postimeout);
f7e7a396
UD
280
281 dataset->resp.version = NSCD_VERSION;
282 dataset->resp.found = 1;
283 dataset->resp.ngrps = start;
284
285 char *cp = dataset->strdata;
286
287 /* Copy the GID values. If the size of the types match this is
288 very simple. */
289 if (sizeof (gid_t) == sizeof (int32_t))
290 cp = mempcpy (cp, groups, start * sizeof (gid_t));
291 else
292 {
293 gid_t *gcp = (gid_t *) cp;
294
295 for (int i = 0; i < start; ++i)
296 *gcp++ = groups[i];
297
298 cp = (char *) gcp;
299 }
300
301 /* Finally the user name. */
302 memcpy (cp, key, req->key_len);
303
5a337776
UD
304 assert (cp == dataset->strdata + total - offsetof (struct dataset,
305 strdata));
306
f7e7a396
UD
307 /* Now we can determine whether on refill we have to create a new
308 record or not. */
309 if (he != NULL)
310 {
311 assert (fd == -1);
312
313 if (total + req->key_len == dh->allocsize
314 && total - offsetof (struct dataset, resp) == dh->recsize
315 && memcmp (&dataset->resp, dh->data,
316 dh->allocsize - offsetof (struct dataset, resp)) == 0)
317 {
318 /* The data has not changed. We will just bump the
319 timeout value. Note that the new record has been
320 allocated on the stack and need not be freed. */
321 dh->timeout = dataset->head.timeout;
322 ++dh->nreloads;
323 }
324 else
325 {
326 /* We have to create a new record. Just allocate
327 appropriate memory and copy it. */
328 struct dataset *newp
c52137d3 329 = (struct dataset *) mempool_alloc (db, total + req->key_len,
20e498bd 330 1);
f7e7a396
UD
331 if (newp != NULL)
332 {
333 /* Adjust pointer into the memory block. */
334 cp = (char *) newp + (cp - (char *) dataset);
335
336 dataset = memcpy (newp, dataset, total + req->key_len);
337 alloca_used = false;
338 }
339
340 /* Mark the old record as obsolete. */
341 dh->usable = false;
342 }
343 }
344 else
345 {
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);
350
eac10791 351#ifdef HAVE_SENDFILE
74158740 352 if (__builtin_expect (db->mmap_used, 1) && !alloca_used)
eac10791
UD
353 {
354 assert (db->wr_fd != -1);
355 assert ((char *) &dataset->resp > (char *) db->data);
ea547a1a 356 assert ((char *) dataset - (char *) db->head
eac10791
UD
357 + total
358 <= (sizeof (struct database_pers_head)
359 + db->head->module * sizeof (ref_t)
360 + db->head->data_size));
306dfba9
AS
361 ssize_t written = sendfileall (fd, db->wr_fd,
362 (char *) &dataset->resp
363 - (char *) db->head,
364 dataset->head.recsize);
365 if (written != dataset->head.recsize)
366 {
eac10791 367# ifndef __ASSUME_SENDFILE
306dfba9
AS
368 if (written == -1 && errno == ENOSYS)
369 goto use_write;
eac10791 370# endif
306dfba9
AS
371 all_written = false;
372 }
eac10791
UD
373 }
374 else
375# ifndef __ASSUME_SENDFILE
376 use_write:
377# endif
378#endif
306dfba9
AS
379 if (writeall (fd, &dataset->resp, dataset->head.recsize)
380 != dataset->head.recsize)
381 all_written = false;
f7e7a396
UD
382 }
383
384
385 /* Add the record to the database. But only if it has not been
386 stored on the stack. */
387 if (! alloca_used)
388 {
389 /* If necessary, we also propagate the data to disk. */
390 if (db->persistent)
391 {
392 // XXX async OK?
393 uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
394 msync ((void *) pval,
395 ((uintptr_t) dataset & pagesize_m1) + total +
396 req->key_len, MS_ASYNC);
397 }
398
7e71e55f 399 (void) cache_add (INITGROUPS, cp, req->key_len, &dataset->head, true,
528741cb 400 db, uid, he == NULL);
00ebd7ed
UD
401
402 pthread_rwlock_unlock (&db->lock);
f7e7a396
UD
403 }
404 }
405
406 free (groups);
407
306dfba9 408 if (__builtin_expect (!all_written, 0) && debug_level > 0)
f7e7a396
UD
409 {
410 char buf[256];
411 dbg_log (_("short write in %s: %s"), __FUNCTION__,
412 strerror_r (errno, buf, sizeof (buf)));
413 }
a4c7ea7b
UD
414
415 return timeout;
f7e7a396
UD
416}
417
418
419void
420addinitgroups (struct database_dyn *db, int fd, request_header *req, void *key,
421 uid_t uid)
422{
423 addinitgroupsX (db, fd, req, key, uid, NULL, NULL);
424}
425
426
a4c7ea7b 427time_t
f7e7a396
UD
428readdinitgroups (struct database_dyn *db, struct hashentry *he,
429 struct datahead *dh)
430{
431 request_header req =
432 {
433 .type = INITGROUPS,
434 .key_len = he->len
435 };
436
a4c7ea7b 437 return addinitgroupsX (db, -1, &req, db->data + he->key, he->owner, he, dh);
f7e7a396 438}