1 /* Cache handling for services lookup.
2 Copyright (C) 2007-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; version 2 of the License, or
8 (at your option) any later version.
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, see <https://www.gnu.org/licenses/>. */
25 #include <kernel-features.h>
26 #include <scratch_buffer.h>
32 /* This is the standard reply in case the service is disabled. */
33 static const serv_response_header disabled
=
35 .version
= NSCD_VERSION
,
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
,
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 bool all_written
= true;
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. */
77 serv_response_header resp
;
81 assert (offsetof (struct dataset
, resp
) == offsetof (struct datahead
, data
));
83 time_t timeout
= MAX_TIMEOUT_VALUE
;
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
;
102 /* We have no data. This means we send the standard reply for this
104 total
= sizeof (notfound
);
107 && TEMP_FAILURE_RETRY (send (fd
, ¬found
, total
,
108 MSG_NOSIGNAL
)) != total
)
111 /* If we have a transient error or cannot permanently store
112 the result, so be it. */
113 if (errval
== EAGAIN
|| __builtin_expect (db
->negtimeout
== 0, 0))
115 /* Mark the old entry as obsolete. */
119 else if ((dataset
= mempool_alloc (db
, (sizeof (struct dataset
)
120 + req
->key_len
), 1)) != NULL
)
122 timeout
= datahead_init_neg (&dataset
->head
,
123 (sizeof (struct dataset
)
124 + req
->key_len
), total
,
127 /* This is the reply. */
128 memcpy (&dataset
->resp
, ¬found
, total
);
130 /* Copy the key data. */
131 memcpy (dataset
->strdata
, key
, req
->key_len
);
133 /* If necessary, we also propagate the data to disk. */
137 uintptr_t pval
= (uintptr_t) dataset
& ~pagesize_m1
;
138 msync ((void *) pval
,
139 ((uintptr_t) dataset
& pagesize_m1
)
140 + sizeof (struct dataset
) + req
->key_len
, MS_ASYNC
);
143 (void) cache_add (req
->type
, &dataset
->strdata
, req
->key_len
,
144 &dataset
->head
, true, db
, owner
, he
== NULL
);
146 pthread_rwlock_unlock (&db
->lock
);
148 /* Mark the old entry as obsolete. */
156 /* Determine the I/O structure. */
157 size_t s_name_len
= strlen (serv
->s_name
) + 1;
158 size_t s_proto_len
= strlen (serv
->s_proto
) + 1;
159 uint32_t *s_aliases_len
;
160 size_t s_aliases_cnt
;
165 /* Determine the number of aliases. */
167 for (cnt
= 0; serv
->s_aliases
[cnt
] != NULL
; ++cnt
)
169 /* Determine the length of all aliases. */
170 s_aliases_len
= (uint32_t *) alloca (s_aliases_cnt
* sizeof (uint32_t));
172 for (cnt
= 0; cnt
< s_aliases_cnt
; ++cnt
)
174 s_aliases_len
[cnt
] = strlen (serv
->s_aliases
[cnt
]) + 1;
175 total
+= s_aliases_len
[cnt
];
178 total
+= (offsetof (struct dataset
, strdata
)
181 + s_aliases_cnt
* sizeof (uint32_t));
183 /* If we refill the cache, first assume the reconrd did not
184 change. Allocate memory on the cache since it is likely
185 discarded anyway. If it turns out to be necessary to have a
186 new record we can still allocate real memory. */
187 bool alloca_used
= false;
191 dataset
= (struct dataset
*) mempool_alloc (db
, total
+ req
->key_len
,
196 /* We cannot permanently add the result in the moment. But
197 we can provide the result as is. Store the data in some
199 dataset
= (struct dataset
*) alloca (total
+ req
->key_len
);
201 /* We cannot add this record to the permanent database. */
205 timeout
= datahead_init_pos (&dataset
->head
, total
+ req
->key_len
,
206 total
- offsetof (struct dataset
, resp
),
207 he
== NULL
? 0 : dh
->nreloads
+ 1,
210 dataset
->resp
.version
= NSCD_VERSION
;
211 dataset
->resp
.found
= 1;
212 dataset
->resp
.s_name_len
= s_name_len
;
213 dataset
->resp
.s_proto_len
= s_proto_len
;
214 dataset
->resp
.s_port
= serv
->s_port
;
215 dataset
->resp
.s_aliases_cnt
= s_aliases_cnt
;
217 cp
= dataset
->strdata
;
219 cp
= mempcpy (cp
, serv
->s_name
, s_name_len
);
220 cp
= mempcpy (cp
, serv
->s_proto
, s_proto_len
);
221 cp
= mempcpy (cp
, s_aliases_len
, s_aliases_cnt
* sizeof (uint32_t));
223 /* Then the aliases. */
225 for (cnt
= 0; cnt
< s_aliases_cnt
; ++cnt
)
226 cp
= mempcpy (cp
, serv
->s_aliases
[cnt
], s_aliases_len
[cnt
]);
229 == dataset
->strdata
+ total
- offsetof (struct dataset
,
232 char *key_copy
= memcpy (cp
, key
, req
->key_len
);
234 /* Now we can determine whether on refill we have to create a new
240 if (total
+ req
->key_len
== dh
->allocsize
241 && total
- offsetof (struct dataset
, resp
) == dh
->recsize
242 && memcmp (&dataset
->resp
, dh
->data
,
243 dh
->allocsize
- offsetof (struct dataset
, resp
)) == 0)
245 /* The data has not changed. We will just bump the
246 timeout value. Note that the new record has been
247 allocated on the stack and need not be freed. */
248 dh
->timeout
= dataset
->head
.timeout
;
253 /* We have to create a new record. Just allocate
254 appropriate memory and copy it. */
256 = (struct dataset
*) mempool_alloc (db
, total
+ req
->key_len
,
260 /* Adjust pointers into the memory block. */
261 aliases
= (char *) newp
+ (aliases
- (char *) dataset
);
262 assert (key_copy
!= NULL
);
263 key_copy
= (char *) newp
+ (key_copy
- (char *) dataset
);
265 dataset
= memcpy (newp
, dataset
, total
+ req
->key_len
);
269 /* Mark the old record as obsolete. */
275 /* We write the dataset before inserting it to the database
276 since while inserting this thread might block and so would
277 unnecessarily keep the receiver waiting. */
280 if (writeall (fd
, &dataset
->resp
, dataset
->head
.recsize
)
281 != dataset
->head
.recsize
)
285 /* Add the record to the database. But only if it has not been
286 stored on the stack. */
289 /* If necessary, we also propagate the data to disk. */
293 uintptr_t pval
= (uintptr_t) dataset
& ~pagesize_m1
;
294 msync ((void *) pval
,
295 ((uintptr_t) dataset
& pagesize_m1
)
296 + total
+ req
->key_len
, MS_ASYNC
);
299 (void) cache_add (req
->type
, key_copy
, req
->key_len
,
300 &dataset
->head
, true, db
, owner
, he
== NULL
);
302 pthread_rwlock_unlock (&db
->lock
);
306 if (__builtin_expect (!all_written
, 0) && debug_level
> 0)
309 dbg_log (_("short write in %s: %s"), __FUNCTION__
,
310 strerror_r (errno
, buf
, sizeof (buf
)));
318 lookup (int type
, char *key
, struct servent
*resultbufp
, char *buffer
,
319 size_t buflen
, struct servent
**serv
)
321 char *proto
= strrchr (key
, '/');
322 if (proto
!= NULL
&& proto
!= key
)
324 key
= strndupa (key
, proto
- key
);
325 if (proto
[1] == '\0')
331 if (type
== GETSERVBYNAME
)
332 return __getservbyname_r (key
, proto
, resultbufp
, buffer
, buflen
, serv
);
334 assert (type
== GETSERVBYPORT
);
335 return __getservbyport_r (atol (key
), proto
, resultbufp
, buffer
, buflen
,
341 addservbyX (struct database_dyn
*db
, int fd
, request_header
*req
,
342 char *key
, uid_t uid
, struct hashentry
*he
, struct datahead
*dh
)
344 /* Search for the entry matching the key. Please note that we don't
345 look again in the table whether the dataset is now available. We
346 simply insert it. It does not matter if it is in there twice. The
347 pruning function only will look at the timestamp. */
348 struct servent resultbuf
;
349 struct servent
*serv
;
351 struct scratch_buffer tmpbuf
;
352 scratch_buffer_init (&tmpbuf
);
354 if (__glibc_unlikely (debug_level
> 0))
357 dbg_log (_("Haven't found \"%s\" in services cache!"), key
);
359 dbg_log (_("Reloading \"%s\" in services cache!"), key
);
362 while (lookup (req
->type
, key
, &resultbuf
,
363 tmpbuf
.data
, tmpbuf
.length
, &serv
) != 0
364 && (errval
= errno
) == ERANGE
)
365 if (!scratch_buffer_grow (&tmpbuf
))
367 /* We ran out of memory. We cannot do anything but sending a
368 negative response. In reality this should never
371 /* We set the error to indicate this is (possibly) a temporary
372 error and that it does not mean the entry is not available
378 time_t timeout
= cache_addserv (db
, fd
, req
, key
, serv
, uid
, he
, dh
, errval
);
379 scratch_buffer_free (&tmpbuf
);
385 addservbyname (struct database_dyn
*db
, int fd
, request_header
*req
,
386 void *key
, uid_t uid
)
388 addservbyX (db
, fd
, req
, key
, uid
, NULL
, NULL
);
393 readdservbyname (struct database_dyn
*db
, struct hashentry
*he
,
398 .type
= GETSERVBYNAME
,
402 return addservbyX (db
, -1, &req
, db
->data
+ he
->key
, he
->owner
, he
, dh
);
407 addservbyport (struct database_dyn
*db
, int fd
, request_header
*req
,
408 void *key
, uid_t uid
)
410 addservbyX (db
, fd
, req
, key
, uid
, NULL
, NULL
);
415 readdservbyport (struct database_dyn
*db
, struct hashentry
*he
,
420 .type
= GETSERVBYPORT
,
424 return addservbyX (db
, -1, &req
, db
->data
+ he
->key
, he
->owner
, he
, dh
);