Remove 'Contributed' line, glibc is not using them anymore.
[glibc.git] / nscd / servicescache.c
blob0e7520de248daffb29c0260eabee215ab0294fd1
1 /* Cache handling for services lookup.
2 Copyright (C) 2007, 2008, 2009, 2011, 2012 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, see <http://www.gnu.org/licenses/>. */
19 #include <alloca.h>
20 #include <assert.h>
21 #include <errno.h>
22 #include <libintl.h>
23 #include <netdb.h>
24 #include <unistd.h>
25 #include <sys/mman.h>
26 #include <kernel-features.h>
28 #include "nscd.h"
29 #include "dbg_log.h"
32 /* This is the standard reply in case the service is disabled. */
33 static const serv_response_header disabled =
35 .version = NSCD_VERSION,
36 .found = -1,
37 .s_name_len = 0,
38 .s_proto_len = 0,
39 .s_aliases_cnt = 0,
40 .s_port = -1
43 /* This is the struct describing how to write this record. */
44 const struct iovec serv_iov_disabled =
46 .iov_base = (void *) &disabled,
47 .iov_len = sizeof (disabled)
51 /* This is the standard reply in case we haven't found the dataset. */
52 static const serv_response_header notfound =
54 .version = NSCD_VERSION,
55 .found = 0,
56 .s_name_len = 0,
57 .s_proto_len = 0,
58 .s_aliases_cnt = 0,
59 .s_port = -1
63 static time_t
64 cache_addserv (struct database_dyn *db, int fd, request_header *req,
65 const void *key, struct servent *serv, uid_t owner,
66 struct hashentry *const he, struct datahead *dh, int errval)
68 ssize_t total;
69 ssize_t written;
70 time_t t = time (NULL);
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 serv_response_header resp;
78 char strdata[0];
79 } *dataset;
81 assert (offsetof (struct dataset, resp) == offsetof (struct datahead, data));
83 time_t timeout = MAX_TIMEOUT_VALUE;
84 if (serv == NULL)
86 if (he != NULL && errval == EAGAIN)
88 /* If we have an old record available but cannot find one
89 now because the service is not available we keep the old
90 record and make sure it does not get removed. */
91 if (reload_count != UINT_MAX)
92 /* Do not reset the value if we never not reload the record. */
93 dh->nreloads = reload_count - 1;
95 /* Reload with the same time-to-live value. */
96 timeout = dh->timeout = t + db->postimeout;
98 written = total = 0;
100 else
102 /* We have no data. This means we send the standard reply for this
103 case. */
104 written = total = sizeof (notfound);
106 if (fd != -1)
107 written = TEMP_FAILURE_RETRY (send (fd, &notfound, total,
108 MSG_NOSIGNAL));
110 /* If we have a transient error or cannot permanently store
111 the result, so be it. */
112 if (errval == EAGAIN || __builtin_expect (db->negtimeout == 0, 0))
114 /* Mark the old entry as obsolete. */
115 if (dh != NULL)
116 dh->usable = false;
118 else if ((dataset = mempool_alloc (db, (sizeof (struct dataset)
119 + req->key_len), 1)) != NULL)
121 dataset->head.allocsize = sizeof (struct dataset) + req->key_len;
122 dataset->head.recsize = total;
123 dataset->head.notfound = true;
124 dataset->head.nreloads = 0;
125 dataset->head.usable = true;
127 /* Compute the timeout time. */
128 timeout = dataset->head.timeout = t + db->negtimeout;
130 /* This is the reply. */
131 memcpy (&dataset->resp, &notfound, total);
133 /* Copy the key data. */
134 memcpy (dataset->strdata, key, req->key_len);
136 /* If necessary, we also propagate the data to disk. */
137 if (db->persistent)
139 // XXX async OK?
140 uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
141 msync ((void *) pval,
142 ((uintptr_t) dataset & pagesize_m1)
143 + sizeof (struct dataset) + req->key_len, MS_ASYNC);
146 (void) cache_add (req->type, &dataset->strdata, req->key_len,
147 &dataset->head, true, db, owner, he == NULL);
149 pthread_rwlock_unlock (&db->lock);
151 /* Mark the old entry as obsolete. */
152 if (dh != NULL)
153 dh->usable = false;
157 else
159 /* Determine the I/O structure. */
160 size_t s_name_len = strlen (serv->s_name) + 1;
161 size_t s_proto_len = strlen (serv->s_proto) + 1;
162 uint32_t *s_aliases_len;
163 size_t s_aliases_cnt;
164 char *aliases;
165 char *cp;
166 size_t cnt;
168 /* Determine the number of aliases. */
169 s_aliases_cnt = 0;
170 for (cnt = 0; serv->s_aliases[cnt] != NULL; ++cnt)
171 ++s_aliases_cnt;
172 /* Determine the length of all aliases. */
173 s_aliases_len = (uint32_t *) alloca (s_aliases_cnt * sizeof (uint32_t));
174 total = 0;
175 for (cnt = 0; cnt < s_aliases_cnt; ++cnt)
177 s_aliases_len[cnt] = strlen (serv->s_aliases[cnt]) + 1;
178 total += s_aliases_len[cnt];
181 total += (offsetof (struct dataset, strdata)
182 + s_name_len
183 + s_proto_len
184 + s_aliases_cnt * sizeof (uint32_t));
185 written = total;
187 /* If we refill the cache, first assume the reconrd did not
188 change. Allocate memory on the cache since it is likely
189 discarded anyway. If it turns out to be necessary to have a
190 new record we can still allocate real memory. */
191 bool alloca_used = false;
192 dataset = NULL;
194 if (he == NULL)
195 dataset = (struct dataset *) mempool_alloc (db, total + req->key_len,
198 if (dataset == NULL)
200 /* We cannot permanently add the result in the moment. But
201 we can provide the result as is. Store the data in some
202 temporary memory. */
203 dataset = (struct dataset *) alloca (total + req->key_len);
205 /* We cannot add this record to the permanent database. */
206 alloca_used = true;
209 dataset->head.allocsize = total + req->key_len;
210 dataset->head.recsize = total - offsetof (struct dataset, resp);
211 dataset->head.notfound = false;
212 dataset->head.nreloads = he == NULL ? 0 : (dh->nreloads + 1);
213 dataset->head.usable = true;
215 /* Compute the timeout time. */
216 timeout = dataset->head.timeout = t + db->postimeout;
218 dataset->resp.version = NSCD_VERSION;
219 dataset->resp.found = 1;
220 dataset->resp.s_name_len = s_name_len;
221 dataset->resp.s_proto_len = s_proto_len;
222 dataset->resp.s_port = serv->s_port;
223 dataset->resp.s_aliases_cnt = s_aliases_cnt;
225 cp = dataset->strdata;
227 cp = mempcpy (cp, serv->s_name, s_name_len);
228 cp = mempcpy (cp, serv->s_proto, s_proto_len);
229 cp = mempcpy (cp, s_aliases_len, s_aliases_cnt * sizeof (uint32_t));
231 /* Then the aliases. */
232 aliases = cp;
233 for (cnt = 0; cnt < s_aliases_cnt; ++cnt)
234 cp = mempcpy (cp, serv->s_aliases[cnt], s_aliases_len[cnt]);
236 assert (cp
237 == dataset->strdata + total - offsetof (struct dataset,
238 strdata));
240 char *key_copy = memcpy (cp, key, req->key_len);
242 /* Now we can determine whether on refill we have to create a new
243 record or not. */
244 if (he != NULL)
246 assert (fd == -1);
248 if (total + req->key_len == dh->allocsize
249 && total - offsetof (struct dataset, resp) == dh->recsize
250 && memcmp (&dataset->resp, dh->data,
251 dh->allocsize - offsetof (struct dataset, resp)) == 0)
253 /* The data has not changed. We will just bump the
254 timeout value. Note that the new record has been
255 allocated on the stack and need not be freed. */
256 dh->timeout = dataset->head.timeout;
257 ++dh->nreloads;
259 else
261 /* We have to create a new record. Just allocate
262 appropriate memory and copy it. */
263 struct dataset *newp
264 = (struct dataset *) mempool_alloc (db, total + req->key_len,
266 if (newp != NULL)
268 /* Adjust pointers into the memory block. */
269 aliases = (char *) newp + (aliases - (char *) dataset);
270 assert (key_copy != NULL);
271 key_copy = (char *) newp + (key_copy - (char *) dataset);
273 dataset = memcpy (newp, dataset, total + req->key_len);
274 alloca_used = false;
277 /* Mark the old record as obsolete. */
278 dh->usable = false;
281 else
283 /* We write the dataset before inserting it to the database
284 since while inserting this thread might block and so would
285 unnecessarily keep the receiver waiting. */
286 assert (fd != -1);
288 #ifdef HAVE_SENDFILE
289 if (__builtin_expect (db->mmap_used, 1) && !alloca_used)
291 assert (db->wr_fd != -1);
292 assert ((char *) &dataset->resp > (char *) db->data);
293 assert ((char *) &dataset->resp - (char *) db->head
294 + total
295 <= (sizeof (struct database_pers_head)
296 + db->head->module * sizeof (ref_t)
297 + db->head->data_size));
298 written = sendfileall (fd, db->wr_fd,
299 (char *) &dataset->resp
300 - (char *) db->head, total);
301 # ifndef __ASSUME_SENDFILE
302 if (written == -1 && errno == ENOSYS)
303 goto use_write;
304 # endif
306 else
307 # ifndef __ASSUME_SENDFILE
308 use_write:
309 # endif
310 #endif
311 written = writeall (fd, &dataset->resp, total);
314 /* Add the record to the database. But only if it has not been
315 stored on the stack. */
316 if (! alloca_used)
318 /* If necessary, we also propagate the data to disk. */
319 if (db->persistent)
321 // XXX async OK?
322 uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
323 msync ((void *) pval,
324 ((uintptr_t) dataset & pagesize_m1)
325 + total + req->key_len, MS_ASYNC);
328 (void) cache_add (req->type, key_copy, req->key_len,
329 &dataset->head, true, db, owner, he == NULL);
331 pthread_rwlock_unlock (&db->lock);
335 if (__builtin_expect (written != total, 0) && debug_level > 0)
337 char buf[256];
338 dbg_log (_("short write in %s: %s"), __FUNCTION__,
339 strerror_r (errno, buf, sizeof (buf)));
342 return timeout;
346 static int
347 lookup (int type, char *key, struct servent *resultbufp, char *buffer,
348 size_t buflen, struct servent **serv)
350 char *proto = strrchr (key, '/');
351 if (proto != NULL && proto != key)
353 key = strndupa (key, proto - key);
354 if (proto[1] == '\0')
355 proto = NULL;
356 else
357 ++proto;
360 if (type == GETSERVBYNAME)
361 return __getservbyname_r (key, proto, resultbufp, buffer, buflen, serv);
363 assert (type == GETSERVBYPORT);
364 return __getservbyport_r (atol (key), proto, resultbufp, buffer, buflen,
365 serv);
369 static time_t
370 addservbyX (struct database_dyn *db, int fd, request_header *req,
371 char *key, uid_t uid, struct hashentry *he, struct datahead *dh)
373 /* Search for the entry matching the key. Please note that we don't
374 look again in the table whether the dataset is now available. We
375 simply insert it. It does not matter if it is in there twice. The
376 pruning function only will look at the timestamp. */
377 size_t buflen = 1024;
378 char *buffer = (char *) alloca (buflen);
379 struct servent resultbuf;
380 struct servent *serv;
381 bool use_malloc = false;
382 int errval = 0;
384 if (__builtin_expect (debug_level > 0, 0))
386 if (he == NULL)
387 dbg_log (_("Haven't found \"%s\" in services cache!"), key);
388 else
389 dbg_log (_("Reloading \"%s\" in services cache!"), key);
392 while (lookup (req->type, key, &resultbuf, buffer, buflen, &serv) != 0
393 && (errval = errno) == ERANGE)
395 errno = 0;
397 if (__builtin_expect (buflen > 32768, 0))
399 char *old_buffer = buffer;
400 buflen *= 2;
401 buffer = (char *) realloc (use_malloc ? buffer : NULL, buflen);
402 if (buffer == NULL)
404 /* We ran out of memory. We cannot do anything but
405 sending a negative response. In reality this should
406 never happen. */
407 serv = NULL;
408 buffer = old_buffer;
410 /* We set the error to indicate this is (possibly) a
411 temporary error and that it does not mean the entry
412 is not available at all. */
413 errval = EAGAIN;
414 break;
416 use_malloc = true;
418 else
419 /* Allocate a new buffer on the stack. If possible combine it
420 with the previously allocated buffer. */
421 buffer = (char *) extend_alloca (buffer, buflen, 2 * buflen);
424 time_t timeout = cache_addserv (db, fd, req, key, serv, uid, he, dh, errval);
426 if (use_malloc)
427 free (buffer);
429 return timeout;
433 void
434 addservbyname (struct database_dyn *db, int fd, request_header *req,
435 void *key, uid_t uid)
437 addservbyX (db, fd, req, key, uid, NULL, NULL);
441 time_t
442 readdservbyname (struct database_dyn *db, struct hashentry *he,
443 struct datahead *dh)
445 request_header req =
447 .type = GETSERVBYNAME,
448 .key_len = he->len
451 return addservbyX (db, -1, &req, db->data + he->key, he->owner, he, dh);
455 void
456 addservbyport (struct database_dyn *db, int fd, request_header *req,
457 void *key, uid_t uid)
459 addservbyX (db, fd, req, key, uid, NULL, NULL);
463 time_t
464 readdservbyport (struct database_dyn *db, struct hashentry *he,
465 struct datahead *dh)
467 request_header req =
469 .type = GETSERVBYPORT,
470 .key_len = he->len
473 return addservbyX (db, -1, &req, db->data + he->key, he->owner, he, dh);