Fix build with multiarch disabled.
[glibc.git] / nscd / servicescache.c
blob2dd1cc5675e4f4a4b9826dfe6a8ad4f8e8d2d3f8
1 /* Cache handling for services lookup.
2 Copyright (C) 2007, 2008, 2009, 2011 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@drepper.com>, 2007.
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 <alloca.h>
21 #include <assert.h>
22 #include <errno.h>
23 #include <libintl.h>
24 #include <netdb.h>
25 #include <unistd.h>
26 #include <sys/mman.h>
27 #include <kernel-features.h>
29 #include "nscd.h"
30 #include "dbg_log.h"
33 /* This is the standard reply in case the service is disabled. */
34 static const serv_response_header disabled =
36 .version = NSCD_VERSION,
37 .found = -1,
38 .s_name_len = 0,
39 .s_proto_len = 0,
40 .s_aliases_cnt = 0,
41 .s_port = -1
44 /* This is the struct describing how to write this record. */
45 const struct iovec serv_iov_disabled =
47 .iov_base = (void *) &disabled,
48 .iov_len = sizeof (disabled)
52 /* This is the standard reply in case we haven't found the dataset. */
53 static const serv_response_header notfound =
55 .version = NSCD_VERSION,
56 .found = 0,
57 .s_name_len = 0,
58 .s_proto_len = 0,
59 .s_aliases_cnt = 0,
60 .s_port = -1
64 static time_t
65 cache_addserv (struct database_dyn *db, int fd, request_header *req,
66 const void *key, struct servent *serv, uid_t owner,
67 struct hashentry *const he, struct datahead *dh, int errval)
69 ssize_t total;
70 ssize_t written;
71 time_t t = time (NULL);
73 /* We allocate all data in one memory block: the iov vector,
74 the response header and the dataset itself. */
75 struct dataset
77 struct datahead head;
78 serv_response_header resp;
79 char strdata[0];
80 } *dataset;
82 assert (offsetof (struct dataset, resp) == offsetof (struct datahead, data));
84 time_t timeout = MAX_TIMEOUT_VALUE;
85 if (serv == NULL)
87 if (he != NULL && errval == EAGAIN)
89 /* If we have an old record available but cannot find one
90 now because the service is not available we keep the old
91 record and make sure it does not get removed. */
92 if (reload_count != UINT_MAX)
93 /* Do not reset the value if we never not reload the record. */
94 dh->nreloads = reload_count - 1;
96 /* Reload with the same time-to-live value. */
97 timeout = dh->timeout = t + db->postimeout;
99 written = total = 0;
101 else
103 /* We have no data. This means we send the standard reply for this
104 case. */
105 total = sizeof (notfound);
107 written = TEMP_FAILURE_RETRY (send (fd, &notfound, total,
108 MSG_NOSIGNAL));
110 dataset = mempool_alloc (db, sizeof (struct dataset) + req->key_len,
112 /* If we cannot permanently store the result, so be it. */
113 if (dataset != NULL)
115 dataset->head.allocsize = sizeof (struct dataset) + req->key_len;
116 dataset->head.recsize = total;
117 dataset->head.notfound = true;
118 dataset->head.nreloads = 0;
119 dataset->head.usable = true;
121 /* Compute the timeout time. */
122 timeout = dataset->head.timeout = t + db->negtimeout;
124 /* This is the reply. */
125 memcpy (&dataset->resp, &notfound, total);
127 /* Copy the key data. */
128 memcpy (dataset->strdata, key, req->key_len);
130 /* If necessary, we also propagate the data to disk. */
131 if (db->persistent)
133 // XXX async OK?
134 uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
135 msync ((void *) pval,
136 ((uintptr_t) dataset & pagesize_m1)
137 + sizeof (struct dataset) + req->key_len, MS_ASYNC);
140 (void) cache_add (req->type, &dataset->strdata, req->key_len,
141 &dataset->head, true, db, owner, he == NULL);
143 pthread_rwlock_unlock (&db->lock);
145 /* Mark the old entry as obsolete. */
146 if (dh != NULL)
147 dh->usable = false;
151 else
153 /* Determine the I/O structure. */
154 size_t s_name_len = strlen (serv->s_name) + 1;
155 size_t s_proto_len = strlen (serv->s_proto) + 1;
156 uint32_t *s_aliases_len;
157 size_t s_aliases_cnt;
158 char *aliases;
159 char *cp;
160 size_t cnt;
162 /* Determine the number of aliases. */
163 s_aliases_cnt = 0;
164 for (cnt = 0; serv->s_aliases[cnt] != NULL; ++cnt)
165 ++s_aliases_cnt;
166 /* Determine the length of all aliases. */
167 s_aliases_len = (uint32_t *) alloca (s_aliases_cnt * sizeof (uint32_t));
168 total = 0;
169 for (cnt = 0; cnt < s_aliases_cnt; ++cnt)
171 s_aliases_len[cnt] = strlen (serv->s_aliases[cnt]) + 1;
172 total += s_aliases_len[cnt];
175 total += (offsetof (struct dataset, strdata)
176 + s_name_len
177 + s_proto_len
178 + s_aliases_cnt * sizeof (uint32_t));
179 written = total;
181 /* If we refill the cache, first assume the reconrd did not
182 change. Allocate memory on the cache since it is likely
183 discarded anyway. If it turns out to be necessary to have a
184 new record we can still allocate real memory. */
185 bool alloca_used = false;
186 dataset = NULL;
188 if (he == NULL)
189 dataset = (struct dataset *) mempool_alloc (db, total + req->key_len,
192 if (dataset == NULL)
194 /* We cannot permanently add the result in the moment. But
195 we can provide the result as is. Store the data in some
196 temporary memory. */
197 dataset = (struct dataset *) alloca (total + req->key_len);
199 /* We cannot add this record to the permanent database. */
200 alloca_used = true;
203 dataset->head.allocsize = total + req->key_len;
204 dataset->head.recsize = total - offsetof (struct dataset, resp);
205 dataset->head.notfound = false;
206 dataset->head.nreloads = he == NULL ? 0 : (dh->nreloads + 1);
207 dataset->head.usable = true;
209 /* Compute the timeout time. */
210 timeout = dataset->head.timeout = t + db->postimeout;
212 dataset->resp.version = NSCD_VERSION;
213 dataset->resp.found = 1;
214 dataset->resp.s_name_len = s_name_len;
215 dataset->resp.s_proto_len = s_proto_len;
216 dataset->resp.s_port = serv->s_port;
217 dataset->resp.s_aliases_cnt = s_aliases_cnt;
219 cp = dataset->strdata;
221 cp = mempcpy (cp, serv->s_name, s_name_len);
222 cp = mempcpy (cp, serv->s_proto, s_proto_len);
223 cp = mempcpy (cp, s_aliases_len, s_aliases_cnt * sizeof (uint32_t));
225 /* Then the aliases. */
226 aliases = cp;
227 for (cnt = 0; cnt < s_aliases_cnt; ++cnt)
228 cp = mempcpy (cp, serv->s_aliases[cnt], s_aliases_len[cnt]);
230 assert (cp
231 == dataset->strdata + total - offsetof (struct dataset,
232 strdata));
234 char *key_copy = memcpy (cp, key, req->key_len);
236 /* Now we can determine whether on refill we have to create a new
237 record or not. */
238 if (he != NULL)
240 assert (fd == -1);
242 if (total + req->key_len == dh->allocsize
243 && total - offsetof (struct dataset, resp) == dh->recsize
244 && memcmp (&dataset->resp, dh->data,
245 dh->allocsize - offsetof (struct dataset, resp)) == 0)
247 /* The data has not changed. We will just bump the
248 timeout value. Note that the new record has been
249 allocated on the stack and need not be freed. */
250 dh->timeout = dataset->head.timeout;
251 ++dh->nreloads;
253 else
255 /* We have to create a new record. Just allocate
256 appropriate memory and copy it. */
257 struct dataset *newp
258 = (struct dataset *) mempool_alloc (db, total + req->key_len,
260 if (newp != NULL)
262 /* Adjust pointers into the memory block. */
263 aliases = (char *) newp + (aliases - (char *) dataset);
264 assert (key_copy != NULL);
265 key_copy = (char *) newp + (key_copy - (char *) dataset);
267 dataset = memcpy (newp, dataset, total + req->key_len);
268 alloca_used = false;
271 /* Mark the old record as obsolete. */
272 dh->usable = false;
275 else
277 /* We write the dataset before inserting it to the database
278 since while inserting this thread might block and so would
279 unnecessarily keep the receiver waiting. */
280 assert (fd != -1);
282 #ifdef HAVE_SENDFILE
283 if (__builtin_expect (db->mmap_used, 1) && !alloca_used)
285 assert (db->wr_fd != -1);
286 assert ((char *) &dataset->resp > (char *) db->data);
287 assert ((char *) &dataset->resp - (char *) db->head
288 + total
289 <= (sizeof (struct database_pers_head)
290 + db->head->module * sizeof (ref_t)
291 + db->head->data_size));
292 written = sendfileall (fd, db->wr_fd,
293 (char *) &dataset->resp
294 - (char *) db->head, total);
295 # ifndef __ASSUME_SENDFILE
296 if (written == -1 && errno == ENOSYS)
297 goto use_write;
298 # endif
300 else
301 # ifndef __ASSUME_SENDFILE
302 use_write:
303 # endif
304 #endif
305 written = writeall (fd, &dataset->resp, total);
308 /* Add the record to the database. But only if it has not been
309 stored on the stack. */
310 if (! alloca_used)
312 /* If necessary, we also propagate the data to disk. */
313 if (db->persistent)
315 // XXX async OK?
316 uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
317 msync ((void *) pval,
318 ((uintptr_t) dataset & pagesize_m1)
319 + total + req->key_len, MS_ASYNC);
322 (void) cache_add (req->type, key_copy, req->key_len,
323 &dataset->head, true, db, owner, he == NULL);
325 pthread_rwlock_unlock (&db->lock);
329 if (__builtin_expect (written != total, 0) && debug_level > 0)
331 char buf[256];
332 dbg_log (_("short write in %s: %s"), __FUNCTION__,
333 strerror_r (errno, buf, sizeof (buf)));
336 return timeout;
340 static int
341 lookup (int type, char *key, struct servent *resultbufp, char *buffer,
342 size_t buflen, struct servent **serv)
344 char *proto = strrchr (key, '/');
345 if (proto != NULL && proto != key)
347 key = strndupa (key, proto - key);
348 if (proto[1] == '\0')
349 proto = NULL;
350 else
351 ++proto;
354 if (type == GETSERVBYNAME)
355 return __getservbyname_r (key, proto, resultbufp, buffer, buflen, serv);
357 assert (type == GETSERVBYPORT);
358 return __getservbyport_r (atol (key), proto, resultbufp, buffer, buflen,
359 serv);
363 static time_t
364 addservbyX (struct database_dyn *db, int fd, request_header *req,
365 char *key, uid_t uid, struct hashentry *he, struct datahead *dh)
367 /* Search for the entry matching the key. Please note that we don't
368 look again in the table whether the dataset is now available. We
369 simply insert it. It does not matter if it is in there twice. The
370 pruning function only will look at the timestamp. */
371 size_t buflen = 1024;
372 char *buffer = (char *) alloca (buflen);
373 struct servent resultbuf;
374 struct servent *serv;
375 bool use_malloc = false;
376 int errval = 0;
378 if (__builtin_expect (debug_level > 0, 0))
380 if (he == NULL)
381 dbg_log (_("Haven't found \"%s\" in services cache!"), key);
382 else
383 dbg_log (_("Reloading \"%s\" in services cache!"), key);
386 while (lookup (req->type, key, &resultbuf, buffer, buflen, &serv) != 0
387 && (errval = errno) == ERANGE)
389 errno = 0;
391 if (__builtin_expect (buflen > 32768, 0))
393 char *old_buffer = buffer;
394 buflen *= 2;
395 buffer = (char *) realloc (use_malloc ? buffer : NULL, buflen);
396 if (buffer == NULL)
398 /* We ran out of memory. We cannot do anything but
399 sending a negative response. In reality this should
400 never happen. */
401 serv = NULL;
402 buffer = old_buffer;
404 /* We set the error to indicate this is (possibly) a
405 temporary error and that it does not mean the entry
406 is not available at all. */
407 errval = EAGAIN;
408 break;
410 use_malloc = true;
412 else
413 /* Allocate a new buffer on the stack. If possible combine it
414 with the previously allocated buffer. */
415 buffer = (char *) extend_alloca (buffer, buflen, 2 * buflen);
418 time_t timeout = cache_addserv (db, fd, req, key, serv, uid, he, dh, errval);
420 if (use_malloc)
421 free (buffer);
423 return timeout;
427 void
428 addservbyname (struct database_dyn *db, int fd, request_header *req,
429 void *key, uid_t uid)
431 addservbyX (db, fd, req, key, uid, NULL, NULL);
435 time_t
436 readdservbyname (struct database_dyn *db, struct hashentry *he,
437 struct datahead *dh)
439 request_header req =
441 .type = GETSERVBYNAME,
442 .key_len = he->len
445 return addservbyX (db, -1, &req, db->data + he->key, he->owner, he, dh);
449 void
450 addservbyport (struct database_dyn *db, int fd, request_header *req,
451 void *key, uid_t uid)
453 addservbyX (db, fd, req, key, uid, NULL, NULL);
457 time_t
458 readdservbyport (struct database_dyn *db, struct hashentry *he,
459 struct datahead *dh)
461 request_header req =
463 .type = GETSERVBYPORT,
464 .key_len = he->len
467 return addservbyX (db, -1, &req, db->data + he->key, he->owner, he, dh);