1 /* Cache handling for services lookup.
2 Copyright (C) 2007, 2008, 2009 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. */
27 #include <kernel-features.h>
33 /* This is the standard reply in case the service is disabled. */
34 static const serv_response_header disabled
=
36 .version
= NSCD_VERSION
,
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
,
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
)
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. */
78 serv_response_header resp
;
82 assert (offsetof (struct dataset
, resp
) == offsetof (struct datahead
, data
));
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;
99 /* We have no data. This means we send the standard reply for this
101 total
= sizeof (notfound
);
103 written
= TEMP_FAILURE_RETRY (send (fd
, ¬found
, total
,
106 dataset
= mempool_alloc (db
, sizeof (struct dataset
) + req
->key_len
,
108 /* If we cannot permanently store the result, so be it. */
111 dataset
->head
.allocsize
= sizeof (struct dataset
) + req
->key_len
;
112 dataset
->head
.recsize
= total
;
113 dataset
->head
.notfound
= true;
114 dataset
->head
.nreloads
= 0;
115 dataset
->head
.usable
= true;
117 /* Compute the timeout time. */
118 dataset
->head
.timeout
= t
+ db
->negtimeout
;
120 /* This is the reply. */
121 memcpy (&dataset
->resp
, ¬found
, total
);
123 /* Copy the key data. */
124 memcpy (dataset
->strdata
, key
, req
->key_len
);
126 /* If necessary, we also propagate the data to disk. */
130 uintptr_t pval
= (uintptr_t) dataset
& ~pagesize_m1
;
131 msync ((void *) pval
,
132 ((uintptr_t) dataset
& pagesize_m1
)
133 + sizeof (struct dataset
) + req
->key_len
, MS_ASYNC
);
136 (void) cache_add (req
->type
, &dataset
->strdata
, req
->key_len
,
137 &dataset
->head
, true, db
, owner
, he
== NULL
);
139 pthread_rwlock_unlock (&db
->lock
);
141 /* Mark the old entry as obsolete. */
149 /* Determine the I/O structure. */
150 size_t s_name_len
= strlen (serv
->s_name
) + 1;
151 size_t s_proto_len
= strlen (serv
->s_proto
) + 1;
152 uint32_t *s_aliases_len
;
153 size_t s_aliases_cnt
;
158 /* Determine the number of aliases. */
160 for (cnt
= 0; serv
->s_aliases
[cnt
] != NULL
; ++cnt
)
162 /* Determine the length of all aliases. */
163 s_aliases_len
= (uint32_t *) alloca (s_aliases_cnt
* sizeof (uint32_t));
165 for (cnt
= 0; cnt
< s_aliases_cnt
; ++cnt
)
167 s_aliases_len
[cnt
] = strlen (serv
->s_aliases
[cnt
]) + 1;
168 total
+= s_aliases_len
[cnt
];
171 total
+= (offsetof (struct dataset
, strdata
)
174 + s_aliases_cnt
* sizeof (uint32_t));
177 /* If we refill the cache, first assume the reconrd did not
178 change. Allocate memory on the cache since it is likely
179 discarded anyway. If it turns out to be necessary to have a
180 new record we can still allocate real memory. */
181 bool alloca_used
= false;
185 dataset
= (struct dataset
*) mempool_alloc (db
, total
+ req
->key_len
,
190 /* We cannot permanently add the result in the moment. But
191 we can provide the result as is. Store the data in some
193 dataset
= (struct dataset
*) alloca (total
+ req
->key_len
);
195 /* We cannot add this record to the permanent database. */
199 dataset
->head
.allocsize
= total
+ req
->key_len
;
200 dataset
->head
.recsize
= total
- offsetof (struct dataset
, resp
);
201 dataset
->head
.notfound
= false;
202 dataset
->head
.nreloads
= he
== NULL
? 0 : (dh
->nreloads
+ 1);
203 dataset
->head
.usable
= true;
205 /* Compute the timeout time. */
206 dataset
->head
.timeout
= t
+ db
->postimeout
;
208 dataset
->resp
.version
= NSCD_VERSION
;
209 dataset
->resp
.found
= 1;
210 dataset
->resp
.s_name_len
= s_name_len
;
211 dataset
->resp
.s_proto_len
= s_proto_len
;
212 dataset
->resp
.s_port
= serv
->s_port
;
213 dataset
->resp
.s_aliases_cnt
= s_aliases_cnt
;
215 cp
= dataset
->strdata
;
217 cp
= mempcpy (cp
, serv
->s_name
, s_name_len
);
218 cp
= mempcpy (cp
, serv
->s_proto
, s_proto_len
);
219 cp
= mempcpy (cp
, s_aliases_len
, s_aliases_cnt
* sizeof (uint32_t));
221 /* Then the aliases. */
223 for (cnt
= 0; cnt
< s_aliases_cnt
; ++cnt
)
224 cp
= mempcpy (cp
, serv
->s_aliases
[cnt
], s_aliases_len
[cnt
]);
227 == dataset
->strdata
+ total
- offsetof (struct dataset
,
230 char *key_copy
= memcpy (cp
, key
, req
->key_len
);
232 /* Now we can determine whether on refill we have to create a new
238 if (total
+ req
->key_len
== dh
->allocsize
239 && total
- offsetof (struct dataset
, resp
) == dh
->recsize
240 && memcmp (&dataset
->resp
, dh
->data
,
241 dh
->allocsize
- offsetof (struct dataset
, resp
)) == 0)
243 /* The data has not changed. We will just bump the
244 timeout value. Note that the new record has been
245 allocated on the stack and need not be freed. */
246 dh
->timeout
= dataset
->head
.timeout
;
251 /* We have to create a new record. Just allocate
252 appropriate memory and copy it. */
254 = (struct dataset
*) mempool_alloc (db
, total
+ req
->key_len
,
258 /* Adjust pointers into the memory block. */
259 aliases
= (char *) newp
+ (aliases
- (char *) dataset
);
260 assert (key_copy
!= NULL
);
261 key_copy
= (char *) newp
+ (key_copy
- (char *) dataset
);
263 dataset
= memcpy (newp
, dataset
, total
+ req
->key_len
);
267 /* Mark the old record as obsolete. */
273 /* We write the dataset before inserting it to the database
274 since while inserting this thread might block and so would
275 unnecessarily keep the receiver waiting. */
279 if (__builtin_expect (db
->mmap_used
, 1) && !alloca_used
)
281 assert (db
->wr_fd
!= -1);
282 assert ((char *) &dataset
->resp
> (char *) db
->data
);
283 assert ((char *) &dataset
->resp
- (char *) db
->head
285 <= (sizeof (struct database_pers_head
)
286 + db
->head
->module
* sizeof (ref_t
)
287 + db
->head
->data_size
));
288 written
= sendfileall (fd
, db
->wr_fd
,
289 (char *) &dataset
->resp
290 - (char *) db
->head
, total
);
291 # ifndef __ASSUME_SENDFILE
292 if (written
== -1 && errno
== ENOSYS
)
297 # ifndef __ASSUME_SENDFILE
301 written
= writeall (fd
, &dataset
->resp
, total
);
304 /* Add the record to the database. But only if it has not been
305 stored on the stack. */
308 /* If necessary, we also propagate the data to disk. */
312 uintptr_t pval
= (uintptr_t) dataset
& ~pagesize_m1
;
313 msync ((void *) pval
,
314 ((uintptr_t) dataset
& pagesize_m1
)
315 + total
+ req
->key_len
, MS_ASYNC
);
318 (void) cache_add (req
->type
, key_copy
, req
->key_len
,
319 &dataset
->head
, true, db
, owner
, he
== NULL
);
321 pthread_rwlock_unlock (&db
->lock
);
325 if (__builtin_expect (written
!= total
, 0) && debug_level
> 0)
328 dbg_log (_("short write in %s: %s"), __FUNCTION__
,
329 strerror_r (errno
, buf
, sizeof (buf
)));
335 lookup (int type
, char *key
, struct servent
*resultbufp
, char *buffer
,
336 size_t buflen
, struct servent
**serv
)
338 char *proto
= strrchr (key
, '/');
339 if (proto
!= NULL
&& proto
!= key
)
341 key
= strndupa (key
, proto
- key
);
342 if (proto
[1] == '\0')
348 if (type
== GETSERVBYNAME
)
349 return __getservbyname_r (key
, proto
, resultbufp
, buffer
, buflen
, serv
);
351 assert (type
== GETSERVBYPORT
);
352 return __getservbyport_r (atol (key
), proto
, resultbufp
, buffer
, buflen
,
358 addservbyX (struct database_dyn
*db
, int fd
, request_header
*req
,
359 char *key
, uid_t uid
, struct hashentry
*he
, struct datahead
*dh
)
361 /* Search for the entry matching the key. Please note that we don't
362 look again in the table whether the dataset is now available. We
363 simply insert it. It does not matter if it is in there twice. The
364 pruning function only will look at the timestamp. */
365 size_t buflen
= 1024;
366 char *buffer
= (char *) alloca (buflen
);
367 struct servent resultbuf
;
368 struct servent
*serv
;
369 bool use_malloc
= false;
372 if (__builtin_expect (debug_level
> 0, 0))
375 dbg_log (_("Haven't found \"%s\" in services cache!"), key
);
377 dbg_log (_("Reloading \"%s\" in services cache!"), key
);
380 while (lookup (req
->type
, key
, &resultbuf
, buffer
, buflen
, &serv
) != 0
381 && (errval
= errno
) == ERANGE
)
385 if (__builtin_expect (buflen
> 32768, 0))
387 char *old_buffer
= buffer
;
389 buffer
= (char *) realloc (use_malloc
? buffer
: NULL
, buflen
);
392 /* We ran out of memory. We cannot do anything but
393 sending a negative response. In reality this should
398 /* We set the error to indicate this is (possibly) a
399 temporary error and that it does not mean the entry
400 is not available at all. */
407 /* Allocate a new buffer on the stack. If possible combine it
408 with the previously allocated buffer. */
409 buffer
= (char *) extend_alloca (buffer
, buflen
, 2 * buflen
);
412 cache_addserv (db
, fd
, req
, key
, serv
, uid
, he
, dh
, errval
);
420 addservbyname (struct database_dyn
*db
, int fd
, request_header
*req
,
421 void *key
, uid_t uid
)
423 addservbyX (db
, fd
, req
, key
, uid
, NULL
, NULL
);
428 readdservbyname (struct database_dyn
*db
, struct hashentry
*he
,
433 .type
= GETSERVBYNAME
,
437 addservbyX (db
, -1, &req
, db
->data
+ he
->key
, he
->owner
, he
, dh
);
442 addservbyport (struct database_dyn
*db
, int fd
, request_header
*req
,
443 void *key
, uid_t uid
)
445 addservbyX (db
, fd
, req
, key
, uid
, NULL
, NULL
);
450 readdservbyport (struct database_dyn
*db
, struct hashentry
*he
,
455 .type
= GETSERVBYPORT
,
459 addservbyX (db
, -1, &req
, db
->data
+ he
->key
, he
->owner
, he
, dh
);