Update copyright notices with scripts/update-copyrights
[jlayton/glibc.git] / nscd / servicescache.c
1 /* Cache handling for services lookup.
2    Copyright (C) 2007-2014 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Ulrich Drepper <drepper@drepper.com>, 2007.
5
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.
10
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.
15
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/>.  */
18
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 <stdint.h>
26 #include <sys/mman.h>
27 #include <kernel-features.h>
28
29 #include "nscd.h"
30 #include "dbg_log.h"
31
32
33 /* This is the standard reply in case the service is disabled.  */
34 static const serv_response_header disabled =
35 {
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
42 };
43
44 /* This is the struct describing how to write this record.  */
45 const struct iovec serv_iov_disabled =
46 {
47   .iov_base = (void *) &disabled,
48   .iov_len = sizeof (disabled)
49 };
50
51
52 /* This is the standard reply in case we haven't found the dataset.  */
53 static const serv_response_header notfound =
54 {
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
61 };
62
63
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)
68 {
69   bool all_written = true;
70   ssize_t total;
71   time_t t = time (NULL);
72
73   /* We allocate all data in one memory block: the iov vector,
74      the response header and the dataset itself.  */
75   struct dataset
76   {
77     struct datahead head;
78     serv_response_header resp;
79     char strdata[0];
80   } *dataset;
81
82   assert (offsetof (struct dataset, resp) == offsetof (struct datahead, data));
83
84   time_t timeout = MAX_TIMEOUT_VALUE;
85   if (serv == NULL)
86     {
87       if (he != NULL && errval == EAGAIN)
88         {
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;
95
96           /* Reload with the same time-to-live value.  */
97           timeout = dh->timeout = t + db->postimeout;
98
99           total = 0;
100         }
101       else
102         {
103           /* We have no data.  This means we send the standard reply for this
104              case.  */
105           total = sizeof (notfound);
106
107           if (fd != -1
108               && TEMP_FAILURE_RETRY (send (fd, &notfound, total,
109                                            MSG_NOSIGNAL)) != total)
110             all_written = false;
111
112           /* If we have a transient error or cannot permanently store
113              the result, so be it.  */
114           if (errval == EAGAIN || __builtin_expect (db->negtimeout == 0, 0))
115             {
116               /* Mark the old entry as obsolete.  */
117               if (dh != NULL)
118                 dh->usable = false;
119             }
120           else if ((dataset = mempool_alloc (db, (sizeof (struct dataset)
121                                                   + req->key_len), 1)) != NULL)
122             {
123               dataset->head.allocsize = sizeof (struct dataset) + req->key_len;
124               dataset->head.recsize = total;
125               dataset->head.notfound = true;
126               dataset->head.nreloads = 0;
127               dataset->head.usable = true;
128
129               /* Compute the timeout time.  */
130               timeout = dataset->head.timeout = t + db->negtimeout;
131
132               /* This is the reply.  */
133               memcpy (&dataset->resp, &notfound, total);
134
135               /* Copy the key data.  */
136               memcpy (dataset->strdata, key, req->key_len);
137
138               /* If necessary, we also propagate the data to disk.  */
139               if (db->persistent)
140                 {
141                   // XXX async OK?
142                   uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
143                   msync ((void *) pval,
144                          ((uintptr_t) dataset & pagesize_m1)
145                          + sizeof (struct dataset) + req->key_len, MS_ASYNC);
146                 }
147
148               (void) cache_add (req->type, &dataset->strdata, req->key_len,
149                                 &dataset->head, true, db, owner, he == NULL);
150
151               pthread_rwlock_unlock (&db->lock);
152
153               /* Mark the old entry as obsolete.  */
154               if (dh != NULL)
155                 dh->usable = false;
156             }
157         }
158     }
159   else
160     {
161       /* Determine the I/O structure.  */
162       size_t s_name_len = strlen (serv->s_name) + 1;
163       size_t s_proto_len = strlen (serv->s_proto) + 1;
164       uint32_t *s_aliases_len;
165       size_t s_aliases_cnt;
166       char *aliases;
167       char *cp;
168       size_t cnt;
169
170       /* Determine the number of aliases.  */
171       s_aliases_cnt = 0;
172       for (cnt = 0; serv->s_aliases[cnt] != NULL; ++cnt)
173         ++s_aliases_cnt;
174       /* Determine the length of all aliases.  */
175       s_aliases_len = (uint32_t *) alloca (s_aliases_cnt * sizeof (uint32_t));
176       total = 0;
177       for (cnt = 0; cnt < s_aliases_cnt; ++cnt)
178         {
179           s_aliases_len[cnt] = strlen (serv->s_aliases[cnt]) + 1;
180           total += s_aliases_len[cnt];
181         }
182
183       total += (offsetof (struct dataset, strdata)
184                 + s_name_len
185                 + s_proto_len
186                 + s_aliases_cnt * sizeof (uint32_t));
187
188       /* If we refill the cache, first assume the reconrd did not
189          change.  Allocate memory on the cache since it is likely
190          discarded anyway.  If it turns out to be necessary to have a
191          new record we can still allocate real memory.  */
192       bool alloca_used = false;
193       dataset = NULL;
194
195       if (he == NULL)
196         dataset = (struct dataset *) mempool_alloc (db, total + req->key_len,
197                                                     1);
198
199       if (dataset == NULL)
200         {
201           /* We cannot permanently add the result in the moment.  But
202              we can provide the result as is.  Store the data in some
203              temporary memory.  */
204           dataset = (struct dataset *) alloca (total + req->key_len);
205
206           /* We cannot add this record to the permanent database.  */
207           alloca_used = true;
208         }
209
210       dataset->head.allocsize = total + req->key_len;
211       dataset->head.recsize = total - offsetof (struct dataset, resp);
212       dataset->head.notfound = false;
213       dataset->head.nreloads = he == NULL ? 0 : (dh->nreloads + 1);
214       dataset->head.usable = true;
215
216       /* Compute the timeout time.  */
217       timeout = dataset->head.timeout = t + db->postimeout;
218
219       dataset->resp.version = NSCD_VERSION;
220       dataset->resp.found = 1;
221       dataset->resp.s_name_len = s_name_len;
222       dataset->resp.s_proto_len = s_proto_len;
223       dataset->resp.s_port = serv->s_port;
224       dataset->resp.s_aliases_cnt = s_aliases_cnt;
225
226       cp = dataset->strdata;
227
228       cp = mempcpy (cp, serv->s_name, s_name_len);
229       cp = mempcpy (cp, serv->s_proto, s_proto_len);
230       cp = mempcpy (cp, s_aliases_len, s_aliases_cnt * sizeof (uint32_t));
231
232       /* Then the aliases.  */
233       aliases = cp;
234       for (cnt = 0; cnt < s_aliases_cnt; ++cnt)
235         cp = mempcpy (cp, serv->s_aliases[cnt], s_aliases_len[cnt]);
236
237       assert (cp
238               == dataset->strdata + total - offsetof (struct dataset,
239                                                       strdata));
240
241       char *key_copy = memcpy (cp, key, req->key_len);
242
243       /* Now we can determine whether on refill we have to create a new
244          record or not.  */
245       if (he != NULL)
246         {
247           assert (fd == -1);
248
249           if (total + req->key_len == dh->allocsize
250               && total - offsetof (struct dataset, resp) == dh->recsize
251               && memcmp (&dataset->resp, dh->data,
252                          dh->allocsize - offsetof (struct dataset, resp)) == 0)
253             {
254               /* The data has not changed.  We will just bump the
255                  timeout value.  Note that the new record has been
256                  allocated on the stack and need not be freed.  */
257               dh->timeout = dataset->head.timeout;
258               ++dh->nreloads;
259             }
260           else
261             {
262               /* We have to create a new record.  Just allocate
263                  appropriate memory and copy it.  */
264               struct dataset *newp
265                 = (struct dataset *) mempool_alloc (db, total + req->key_len,
266                                                     1);
267               if (newp != NULL)
268                 {
269                   /* Adjust pointers into the memory block.  */
270                   aliases = (char *) newp + (aliases - (char *) dataset);
271                   assert (key_copy != NULL);
272                   key_copy = (char *) newp + (key_copy - (char *) dataset);
273
274                   dataset = memcpy (newp, dataset, total + req->key_len);
275                   alloca_used = false;
276                 }
277
278               /* Mark the old record as obsolete.  */
279               dh->usable = false;
280             }
281         }
282       else
283         {
284           /* We write the dataset before inserting it to the database
285              since while inserting this thread might block and so would
286              unnecessarily keep the receiver waiting.  */
287           assert (fd != -1);
288
289 #ifdef HAVE_SENDFILE
290           if (__builtin_expect (db->mmap_used, 1) && !alloca_used)
291             {
292               assert (db->wr_fd != -1);
293               assert ((char *) &dataset->resp > (char *) db->data);
294               assert ((char *) dataset - (char *) db->head
295                       + total
296                       <= (sizeof (struct database_pers_head)
297                           + db->head->module * sizeof (ref_t)
298                           + db->head->data_size));
299               ssize_t written = sendfileall (fd, db->wr_fd,
300                                              (char *) &dataset->resp
301                                              - (char *) db->head,
302                                              dataset->head.recsize);
303               if (written != dataset->head.recsize)
304                 {
305 # ifndef __ASSUME_SENDFILE
306                   if (written == -1 && errno == ENOSYS)
307                     goto use_write;
308 # endif
309                   all_written = false;
310                 }
311             }
312           else
313 # ifndef __ASSUME_SENDFILE
314           use_write:
315 # endif
316 #endif
317             if (writeall (fd, &dataset->resp, dataset->head.recsize)
318                 != dataset->head.recsize)
319               all_written = false;
320         }
321
322       /* Add the record to the database.  But only if it has not been
323          stored on the stack.  */
324       if (! alloca_used)
325         {
326           /* If necessary, we also propagate the data to disk.  */
327           if (db->persistent)
328             {
329               // XXX async OK?
330               uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
331               msync ((void *) pval,
332                      ((uintptr_t) dataset & pagesize_m1)
333                      + total + req->key_len, MS_ASYNC);
334             }
335
336           (void) cache_add (req->type, key_copy, req->key_len,
337                             &dataset->head, true, db, owner, he == NULL);
338
339           pthread_rwlock_unlock (&db->lock);
340         }
341     }
342
343   if (__builtin_expect (!all_written, 0) && debug_level > 0)
344     {
345       char buf[256];
346       dbg_log (_("short write in %s: %s"),  __FUNCTION__,
347                strerror_r (errno, buf, sizeof (buf)));
348     }
349
350   return timeout;
351 }
352
353
354 static int
355 lookup (int type, char *key, struct servent *resultbufp, char *buffer,
356         size_t buflen, struct servent **serv)
357 {
358   char *proto = strrchr (key, '/');
359   if (proto != NULL && proto != key)
360     {
361       key = strndupa (key, proto - key);
362       if (proto[1] == '\0')
363         proto = NULL;
364       else
365         ++proto;
366     }
367
368   if (type == GETSERVBYNAME)
369     return __getservbyname_r (key, proto, resultbufp, buffer, buflen, serv);
370
371   assert (type == GETSERVBYPORT);
372   return __getservbyport_r (atol (key), proto, resultbufp, buffer, buflen,
373                             serv);
374 }
375
376
377 static time_t
378 addservbyX (struct database_dyn *db, int fd, request_header *req,
379             char *key, uid_t uid, struct hashentry *he, struct datahead *dh)
380 {
381   /* Search for the entry matching the key.  Please note that we don't
382      look again in the table whether the dataset is now available.  We
383      simply insert it.  It does not matter if it is in there twice.  The
384      pruning function only will look at the timestamp.  */
385   size_t buflen = 1024;
386   char *buffer = (char *) alloca (buflen);
387   struct servent resultbuf;
388   struct servent *serv;
389   bool use_malloc = false;
390   int errval = 0;
391
392   if (__builtin_expect (debug_level > 0, 0))
393     {
394       if (he == NULL)
395         dbg_log (_("Haven't found \"%s\" in services cache!"), key);
396       else
397         dbg_log (_("Reloading \"%s\" in services cache!"), key);
398     }
399
400   while (lookup (req->type, key, &resultbuf, buffer, buflen, &serv) != 0
401          && (errval = errno) == ERANGE)
402     {
403       errno = 0;
404
405       if (__builtin_expect (buflen > 32768, 0))
406         {
407           char *old_buffer = buffer;
408           buflen *= 2;
409           buffer = (char *) realloc (use_malloc ? buffer : NULL, buflen);
410           if (buffer == NULL)
411             {
412               /* We ran out of memory.  We cannot do anything but
413                  sending a negative response.  In reality this should
414                  never happen.  */
415               serv = NULL;
416               buffer = old_buffer;
417
418               /* We set the error to indicate this is (possibly) a
419                  temporary error and that it does not mean the entry
420                  is not available at all.  */
421               errval = EAGAIN;
422               break;
423             }
424           use_malloc = true;
425         }
426       else
427         /* Allocate a new buffer on the stack.  If possible combine it
428            with the previously allocated buffer.  */
429         buffer = (char *) extend_alloca (buffer, buflen, 2 * buflen);
430     }
431
432   time_t timeout = cache_addserv (db, fd, req, key, serv, uid, he, dh, errval);
433
434   if (use_malloc)
435     free (buffer);
436
437   return timeout;
438 }
439
440
441 void
442 addservbyname (struct database_dyn *db, int fd, request_header *req,
443                void *key, uid_t uid)
444 {
445   addservbyX (db, fd, req, key, uid, NULL, NULL);
446 }
447
448
449 time_t
450 readdservbyname (struct database_dyn *db, struct hashentry *he,
451                  struct datahead *dh)
452 {
453   request_header req =
454     {
455       .type = GETSERVBYNAME,
456       .key_len = he->len
457     };
458
459   return addservbyX (db, -1, &req, db->data + he->key, he->owner, he, dh);
460 }
461
462
463 void
464 addservbyport (struct database_dyn *db, int fd, request_header *req,
465                void *key, uid_t uid)
466 {
467   addservbyX (db, fd, req, key, uid, NULL, NULL);
468 }
469
470
471 time_t
472 readdservbyport (struct database_dyn *db, struct hashentry *he,
473                  struct datahead *dh)
474 {
475   request_header req =
476     {
477       .type = GETSERVBYPORT,
478       .key_len = he->len
479     };
480
481   return addservbyX (db, -1, &req, db->data + he->key, he->owner, he, dh);
482 }