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. */
31 /* This is the standard reply in case the service is disabled. */
32 static const serv_response_header disabled
=
34 .version
= NSCD_VERSION
,
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
,
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
)
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. */
76 serv_response_header resp
;
80 assert (offsetof (struct dataset
, resp
) == offsetof (struct datahead
, data
));
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;
97 /* We have no data. This means we send the standard reply for this
99 total
= sizeof (notfound
);
101 written
= TEMP_FAILURE_RETRY (send (fd
, ¬found
, total
,
104 dataset
= mempool_alloc (db
, sizeof (struct dataset
) + req
->key_len
);
105 /* If we cannot permanently store the result, so be it. */
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
, ¬found
, total
);
120 /* Copy the key data. */
121 memcpy (dataset
->strdata
, key
, req
->key_len
);
123 /* If necessary, we also propagate the data to disk. */
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. */
148 ++db
->head
->addfailed
;
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
;
162 /* Determine the number of aliases. */
164 for (cnt
= 0; serv
->s_aliases
[cnt
] != NULL
; ++cnt
)
166 /* Determine the length of all aliases. */
167 s_aliases_len
= (uint32_t *) alloca (s_aliases_cnt
* sizeof (uint32_t));
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
)
178 + s_aliases_cnt
* sizeof (uint32_t));
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;
190 dataset
= (struct dataset
*) mempool_alloc (db
,
191 total
+ req
->key_len
);
193 ++db
->head
->addfailed
;
198 /* We cannot permanently add the result in the moment. But
199 we can provide the result as is. Store the data in some
201 dataset
= (struct dataset
*) alloca (total
+ req
->key_len
);
203 /* We cannot add this record to the permanent database. */
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. */
231 for (cnt
= 0; cnt
< s_aliases_cnt
; ++cnt
)
232 cp
= mempcpy (cp
, serv
->s_aliases
[cnt
], s_aliases_len
[cnt
]);
235 == dataset
->strdata
+ total
- offsetof (struct dataset
,
238 char *key_copy
= memcpy (cp
, key
, req
->key_len
);
240 /* Now we can determine whether on refill we have to create a new
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
;
259 /* We have to create a new record. Just allocate
260 appropriate memory and copy it. */
262 = (struct dataset
*) mempool_alloc (db
, total
+ req
->key_len
);
265 /* Adjust pointers into the memory block. */
266 aliases
= (char *) newp
+ (aliases
- (char *) dataset
);
267 if (key_copy
!= NULL
)
268 key_copy
= (char *) newp
+ (key_copy
- (char *) dataset
);
270 dataset
= memcpy (newp
, dataset
, total
+ req
->key_len
);
274 /* Mark the old record as obsolete. */
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. */
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
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
)
304 # ifndef __ASSUME_SENDFILE
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. */
315 /* If necessary, we also propagate the data to disk. */
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)
341 dbg_log (_("short write in %s: %s"), __FUNCTION__
,
342 strerror_r (errno
, buf
, sizeof (buf
)));
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')
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
,
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;
385 if (__builtin_expect (debug_level
> 0, 0))
388 dbg_log (_("Haven't found \"%s\" in services cache!"), key
);
390 dbg_log (_("Reloading \"%s\" in services cache!"), key
);
393 while (lookup (req
->type
, key
, &resultbuf
, buffer
, buflen
, &serv
) != 0
394 && (errval
= errno
) == ERANGE
)
398 if (__builtin_expect (buflen
> 32768, 0))
400 char *old_buffer
= buffer
;
402 buffer
= (char *) realloc (use_malloc
? buffer
: NULL
, buflen
);
405 /* We ran out of memory. We cannot do anything but
406 sending a negative response. In reality this should
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. */
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
);
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
);
441 readdservbyname (struct database_dyn
*db
, struct hashentry
*he
,
446 .type
= GETSERVBYNAME
,
450 addservbyX (db
, -1, &req
, db
->data
+ he
->key
, he
->owner
, he
, dh
);
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
);
463 readdservbyport (struct database_dyn
*db
, struct hashentry
*he
,
468 .type
= GETSERVBYPORT
,
472 addservbyX (db
, -1, &req
, db
->data
+ he
->key
, he
->owner
, he
, dh
);