* sysdeps/ia64/fpu/fesetround.c (fesetround): Return 0 on success
[glibc/pb-stable.git] / nscd / servicescache.c
blob8c3a9516bab20ee7dea4cd320d30a50097a78a01
1 /* Cache handling for services lookup.
2 Copyright (C) 2007 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 version 2 as
8 published by the Free Software Foundation.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
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>
27 #include "nscd.h"
28 #include "dbg_log.h"
31 /* This is the standard reply in case the service is disabled. */
32 static const serv_response_header disabled =
34 .version = NSCD_VERSION,
35 .found = -1,
36 .s_name_len = 0,
37 .s_proto_len = 0,
38 .s_aliases_cnt = 0,
39 .s_port = -1
42 /* This is the struct describing how to write this record. */
43 const struct iovec serv_iov_disabled =
45 .iov_base = (void *) &disabled,
46 .iov_len = sizeof (disabled)
50 /* This is the standard reply in case we haven't found the dataset. */
51 static const serv_response_header notfound =
53 .version = NSCD_VERSION,
54 .found = 0,
55 .s_name_len = 0,
56 .s_proto_len = 0,
57 .s_aliases_cnt = 0,
58 .s_port = -1
62 static void
63 cache_addserv (struct database_dyn *db, int fd, request_header *req,
64 const void *key, struct servent *serv, uid_t owner,
65 struct hashentry *he, struct datahead *dh, int errval)
67 ssize_t total;
68 ssize_t written;
69 time_t t = time (NULL);
71 /* We allocate all data in one memory block: the iov vector,
72 the response header and the dataset itself. */
73 struct dataset
75 struct datahead head;
76 serv_response_header resp;
77 char strdata[0];
78 } *dataset;
80 assert (offsetof (struct dataset, resp) == offsetof (struct datahead, data));
82 if (serv == NULL)
84 if (he != NULL && errval == EAGAIN)
86 /* If we have an old record available but cannot find one
87 now because the service is not available we keep the old
88 record and make sure it does not get removed. */
89 if (reload_count != UINT_MAX)
90 /* Do not reset the value if we never not reload the record. */
91 dh->nreloads = reload_count - 1;
93 written = total = 0;
95 else
97 /* We have no data. This means we send the standard reply for this
98 case. */
99 total = sizeof (notfound);
101 written = TEMP_FAILURE_RETRY (send (fd, &notfound, total,
102 MSG_NOSIGNAL));
104 dataset = mempool_alloc (db, sizeof (struct dataset) + req->key_len);
105 /* If we cannot permanently store the result, so be it. */
106 if (dataset != NULL)
108 dataset->head.allocsize = sizeof (struct dataset) + req->key_len;
109 dataset->head.recsize = total;
110 dataset->head.notfound = true;
111 dataset->head.nreloads = 0;
112 dataset->head.usable = true;
114 /* Compute the timeout time. */
115 dataset->head.timeout = t + db->negtimeout;
117 /* This is the reply. */
118 memcpy (&dataset->resp, &notfound, total);
120 /* Copy the key data. */
121 memcpy (dataset->strdata, key, req->key_len);
123 /* If necessary, we also propagate the data to disk. */
124 if (db->persistent)
126 // XXX async OK?
127 uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
128 msync ((void *) pval,
129 ((uintptr_t) dataset & pagesize_m1)
130 + sizeof (struct dataset) + req->key_len, MS_ASYNC);
133 /* Now get the lock to safely insert the records. */
134 pthread_rwlock_rdlock (&db->lock);
136 if (cache_add (req->type, &dataset->strdata, req->key_len,
137 &dataset->head, true, db, owner) < 0)
138 /* Ensure the data can be recovered. */
139 dataset->head.usable = false;
141 pthread_rwlock_unlock (&db->lock);
143 /* Mark the old entry as obsolete. */
144 if (dh != NULL)
145 dh->usable = false;
147 else
148 ++db->head->addfailed;
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 += (sizeof (struct dataset)
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)
190 dataset = (struct dataset *) mempool_alloc (db,
191 total + req->key_len);
192 if (dataset == NULL)
193 ++db->head->addfailed;
196 if (dataset == NULL)
198 /* We cannot permanently add the result in the moment. But
199 we can provide the result as is. Store the data in some
200 temporary memory. */
201 dataset = (struct dataset *) alloca (total + req->key_len);
203 /* We cannot add this record to the permanent database. */
204 alloca_used = true;
207 dataset->head.allocsize = total + req->key_len;
208 dataset->head.recsize = total - offsetof (struct dataset, resp);
209 dataset->head.notfound = false;
210 dataset->head.nreloads = he == NULL ? 0 : (dh->nreloads + 1);
211 dataset->head.usable = true;
213 /* Compute the timeout time. */
214 dataset->head.timeout = t + db->postimeout;
216 dataset->resp.version = NSCD_VERSION;
217 dataset->resp.found = 1;
218 dataset->resp.s_name_len = s_name_len;
219 dataset->resp.s_proto_len = s_proto_len;
220 dataset->resp.s_port = serv->s_port;
221 dataset->resp.s_aliases_cnt = s_aliases_cnt;
223 cp = dataset->strdata;
225 cp = mempcpy (cp, serv->s_name, s_name_len);
226 cp = mempcpy (cp, serv->s_proto, s_proto_len);
227 cp = mempcpy (cp, s_aliases_len, s_aliases_cnt * sizeof (uint32_t));
229 /* Then the aliases. */
230 aliases = cp;
231 for (cnt = 0; cnt < s_aliases_cnt; ++cnt)
232 cp = mempcpy (cp, serv->s_aliases[cnt], s_aliases_len[cnt]);
234 assert (cp
235 == dataset->strdata + total - offsetof (struct dataset,
236 strdata));
238 char *key_copy = memcpy (cp, key, req->key_len);
240 /* Now we can determine whether on refill we have to create a new
241 record or not. */
242 if (he != NULL)
244 assert (fd == -1);
246 if (total + req->key_len == dh->allocsize
247 && total - offsetof (struct dataset, resp) == dh->recsize
248 && memcmp (&dataset->resp, dh->data,
249 dh->allocsize - offsetof (struct dataset, resp)) == 0)
251 /* The data has not changed. We will just bump the
252 timeout value. Note that the new record has been
253 allocated on the stack and need not be freed. */
254 dh->timeout = dataset->head.timeout;
255 ++dh->nreloads;
257 else
259 /* We have to create a new record. Just allocate
260 appropriate memory and copy it. */
261 struct dataset *newp
262 = (struct dataset *) mempool_alloc (db, total + req->key_len);
263 if (newp != NULL)
265 /* Adjust pointers into the memory block. */
266 aliases = (char *) newp + (aliases - (char *) dataset);
267 assert (key_copy != NULL);
268 key_copy = (char *) newp + (key_copy - (char *) dataset);
270 dataset = memcpy (newp, dataset, total + req->key_len);
271 alloca_used = false;
274 /* Mark the old record as obsolete. */
275 dh->usable = false;
278 else
280 /* We write the dataset before inserting it to the database
281 since while inserting this thread might block and so would
282 unnecessarily keep the receiver waiting. */
283 assert (fd != -1);
285 #ifdef HAVE_SENDFILE
286 if (__builtin_expect (db->mmap_used, 1) && !alloca_used)
288 assert (db->wr_fd != -1);
289 assert ((char *) &dataset->resp > (char *) db->data);
290 assert ((char *) &dataset->resp - (char *) db->head
291 + total
292 <= (sizeof (struct database_pers_head)
293 + db->head->module * sizeof (ref_t)
294 + db->head->data_size));
295 written = sendfileall (fd, db->wr_fd,
296 (char *) &dataset->resp
297 - (char *) db->head, total);
298 # ifndef __ASSUME_SENDFILE
299 if (written == -1 && errno == ENOSYS)
300 goto use_write;
301 # endif
303 else
304 # ifndef __ASSUME_SENDFILE
305 use_write:
306 # endif
307 #endif
308 written = writeall (fd, &dataset->resp, total);
311 /* Add the record to the database. But only if it has not been
312 stored on the stack. */
313 if (! alloca_used)
315 /* If necessary, we also propagate the data to disk. */
316 if (db->persistent)
318 // XXX async OK?
319 uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
320 msync ((void *) pval,
321 ((uintptr_t) dataset & pagesize_m1)
322 + total + req->key_len, MS_ASYNC);
325 /* Now get the lock to safely insert the records. */
326 pthread_rwlock_rdlock (&db->lock);
328 if (cache_add (req->type, key_copy, req->key_len,
329 &dataset->head, true, db, owner) < 0)
330 /* Could not allocate memory. Make sure the
331 data gets discarded. */
332 dataset->head.usable = false;
334 pthread_rwlock_unlock (&db->lock);
338 if (__builtin_expect (written != total, 0) && debug_level > 0)
340 char buf[256];
341 dbg_log (_("short write in %s: %s"), __FUNCTION__,
342 strerror_r (errno, buf, sizeof (buf)));
347 static int
348 lookup (int type, char *key, struct servent *resultbufp, char *buffer,
349 size_t buflen, struct servent **serv)
351 char *proto = strrchr (key, '/');
352 if (proto != NULL && proto != key)
354 key = strndupa (key, proto - key);
355 if (proto[1] == '\0')
356 proto = NULL;
357 else
358 ++proto;
361 if (type == GETSERVBYNAME)
362 return __getservbyname_r (key, proto, resultbufp, buffer, buflen, serv);
364 assert (type == GETSERVBYPORT);
365 return __getservbyport_r (atol (key), proto, resultbufp, buffer, buflen,
366 serv);
370 static void
371 addservbyX (struct database_dyn *db, int fd, request_header *req,
372 char *key, uid_t uid, struct hashentry *he, struct datahead *dh)
374 /* Search for the entry matching the key. Please note that we don't
375 look again in the table whether the dataset is now available. We
376 simply insert it. It does not matter if it is in there twice. The
377 pruning function only will look at the timestamp. */
378 size_t buflen = 1024;
379 char *buffer = (char *) alloca (buflen);
380 struct servent resultbuf;
381 struct servent *serv;
382 bool use_malloc = false;
383 int errval = 0;
385 if (__builtin_expect (debug_level > 0, 0))
387 if (he == NULL)
388 dbg_log (_("Haven't found \"%s\" in services cache!"), key);
389 else
390 dbg_log (_("Reloading \"%s\" in services cache!"), key);
393 while (lookup (req->type, key, &resultbuf, buffer, buflen, &serv) != 0
394 && (errval = errno) == ERANGE)
396 errno = 0;
398 if (__builtin_expect (buflen > 32768, 0))
400 char *old_buffer = buffer;
401 buflen *= 2;
402 buffer = (char *) realloc (use_malloc ? buffer : NULL, buflen);
403 if (buffer == NULL)
405 /* We ran out of memory. We cannot do anything but
406 sending a negative response. In reality this should
407 never happen. */
408 serv = NULL;
409 buffer = old_buffer;
411 /* We set the error to indicate this is (possibly) a
412 temporary error and that it does not mean the entry
413 is not available at all. */
414 errval = EAGAIN;
415 break;
417 use_malloc = true;
419 else
420 /* Allocate a new buffer on the stack. If possible combine it
421 with the previously allocated buffer. */
422 buffer = (char *) extend_alloca (buffer, buflen, 2 * buflen);
425 cache_addserv (db, fd, req, key, serv, uid, he, dh, errval);
427 if (use_malloc)
428 free (buffer);
432 void
433 addservbyname (struct database_dyn *db, int fd, request_header *req,
434 void *key, uid_t uid)
436 addservbyX (db, fd, req, key, uid, NULL, NULL);
440 void
441 readdservbyname (struct database_dyn *db, struct hashentry *he,
442 struct datahead *dh)
444 request_header req =
446 .type = GETSERVBYNAME,
447 .key_len = he->len
450 addservbyX (db, -1, &req, db->data + he->key, he->owner, he, dh);
454 void
455 addservbyport (struct database_dyn *db, int fd, request_header *req,
456 void *key, uid_t uid)
458 addservbyX (db, fd, req, key, uid, NULL, NULL);
462 void
463 readdservbyport (struct database_dyn *db, struct hashentry *he,
464 struct datahead *dh)
466 request_header req =
468 .type = GETSERVBYPORT,
469 .key_len = he->len
472 addservbyX (db, -1, &req, db->data + he->key, he->owner, he, dh);