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/>. */
27 #include <scratch_buffer.h>
33 # include <kernel-features.h>
36 #include "../nss/nsswitch.h"
38 #ifdef LINK_OBSOLETE_NSL
39 # define DEFAULT_CONFIG "compat [NOTFOUND=return] files"
41 # define DEFAULT_CONFIG "files"
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
,
58 #include "../grp/compat-initgroups.c"
62 addinitgroupsX (struct database_dyn
*db
, int fd
, request_header
*req
,
63 void *key
, uid_t uid
, struct hashentry
*const he
,
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. */
77 initgr_response_header resp
;
81 if (__glibc_unlikely (debug_level
> 0))
84 dbg_log (_("Haven't found \"%s\" in group cache!"), (char *) key
);
86 dbg_log (_("Reloading \"%s\" in group cache!"), (char *) key
);
89 static service_user
*group_database
;
93 if (group_database
== NULL
)
94 no_more
= __nss_database_lookup ("group", NULL
, DEFAULT_CONFIG
,
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
103 long int limit
= __sysconf (_SC_NGROUPS_MAX
);
107 /* We limit the size of the intially allocated array. */
108 size
= MIN (limit
, 64);
110 /* No fixed limit on groups. Pick a starting buffer size. */
114 bool all_tryagain
= true;
115 bool any_success
= false;
117 /* This is temporary memory, we need not (and must not) call
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. */
125 /* Nothing added yet. */
128 long int prev_start
= start
;
129 enum nss_status status
;
130 initgroups_dyn_function fct
;
131 fct
= __nss_lookup_function (nip
, "initgroups_dyn");
135 status
= compat_call (nip
, key
, -1, &start
, &size
, &groups
,
138 if (nss_next_action (nip
, NSS_STATUS_UNAVAIL
) != NSS_ACTION_CONTINUE
)
142 status
= DL_CALL_FCT (fct
, (key
, -1, &start
, &size
, &groups
,
145 /* Remove duplicates. */
146 long int cnt
= prev_start
;
150 for (inner
= 0; inner
< prev_start
; ++inner
)
151 if (groups
[inner
] == groups
[cnt
])
154 if (inner
< prev_start
)
155 groups
[cnt
] = groups
[--start
];
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
)
173 if (nip
->next
== NULL
)
184 timeout
= MAX_TIMEOUT_VALUE
;
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
;
204 /* We have no data. This means we send the standard reply for this
207 && TEMP_FAILURE_RETRY (send (fd
, ¬found
, total
,
208 MSG_NOSIGNAL
)) != total
)
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. */
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
,
227 /* This is the reply. */
228 memcpy (&dataset
->resp
, ¬found
, 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. */
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. */
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;
267 dataset
= (struct dataset
*) mempool_alloc (db
, total
+ req
->key_len
,
272 /* We cannot permanently add the result in the moment. But
273 we can provide the result as is. Store the data in some
275 dataset
= (struct dataset
*) alloca (total
+ req
->key_len
);
277 /* We cannot add this record to the permanent database. */
281 timeout
= datahead_init_pos (&dataset
->head
, total
+ req
->key_len
,
282 total
- offsetof (struct dataset
, resp
),
283 he
== NULL
? 0 : dh
->nreloads
+ 1,
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
294 if (sizeof (gid_t
) == sizeof (int32_t))
295 cp
= mempcpy (cp
, groups
, start
* sizeof (gid_t
));
298 gid_t
*gcp
= (gid_t
*) cp
;
300 for (int i
= 0; i
< start
; ++i
)
306 /* Finally the user name. */
307 memcpy (cp
, key
, req
->key_len
);
309 assert (cp
== dataset
->strdata
+ total
- offsetof (struct dataset
,
312 /* Now we can determine whether on refill we have to create a new
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
;
331 /* We have to create a new record. Just allocate
332 appropriate memory and copy it. */
334 = (struct dataset
*) mempool_alloc (db
, total
+ req
->key_len
,
338 /* Adjust pointer into the memory block. */
339 cp
= (char *) newp
+ (cp
- (char *) dataset
);
341 dataset
= memcpy (newp
, dataset
, total
+ req
->key_len
);
345 /* Mark the old record as obsolete. */
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. */
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
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
369 dataset
->head
.recsize
);
370 if (written
!= dataset
->head
.recsize
)
372 # ifndef __ASSUME_SENDFILE
373 if (written
== -1 && errno
== ENOSYS
)
380 # ifndef __ASSUME_SENDFILE
384 if (writeall (fd
, &dataset
->resp
, dataset
->head
.recsize
)
385 != dataset
->head
.recsize
)
390 /* Add the record to the database. But only if it has not been
391 stored on the stack. */
394 /* If necessary, we also propagate the data to disk. */
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
);
413 if (__builtin_expect (!all_written
, 0) && debug_level
> 0)
416 dbg_log (_("short write in %s: %s"), __FUNCTION__
,
417 strerror_r (errno
, buf
, sizeof (buf
)));
425 addinitgroups (struct database_dyn
*db
, int fd
, request_header
*req
, void *key
,
428 addinitgroupsX (db
, fd
, req
, key
, uid
, NULL
, NULL
);
433 readdinitgroups (struct database_dyn
*db
, struct hashentry
*he
,
442 return addinitgroupsX (db
, -1, &req
, db
->data
+ he
->key
, he
->owner
, he
, dh
);