servicescache.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /* Cache handling for services lookup.
  2. Copyright (C) 2007-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, see <https://www.gnu.org/licenses/>. */
  14. #include <assert.h>
  15. #include <errno.h>
  16. #include <libintl.h>
  17. #include <netdb.h>
  18. #include <unistd.h>
  19. #include <stdint.h>
  20. #include <sys/mman.h>
  21. #include <kernel-features.h>
  22. #include <scratch_buffer.h>
  23. #include "nscd.h"
  24. #include "dbg_log.h"
  25. /* This is the standard reply in case the service is disabled. */
  26. static const serv_response_header disabled =
  27. {
  28. .version = NSCD_VERSION,
  29. .found = -1,
  30. .s_name_len = 0,
  31. .s_proto_len = 0,
  32. .s_aliases_cnt = 0,
  33. .s_port = -1
  34. };
  35. /* This is the struct describing how to write this record. */
  36. const struct iovec serv_iov_disabled =
  37. {
  38. .iov_base = (void *) &disabled,
  39. .iov_len = sizeof (disabled)
  40. };
  41. /* This is the standard reply in case we haven't found the dataset. */
  42. static const serv_response_header notfound =
  43. {
  44. .version = NSCD_VERSION,
  45. .found = 0,
  46. .s_name_len = 0,
  47. .s_proto_len = 0,
  48. .s_aliases_cnt = 0,
  49. .s_port = -1
  50. };
  51. static time_t
  52. cache_addserv (struct database_dyn *db, int fd, request_header *req,
  53. const void *key, struct servent *serv, uid_t owner,
  54. struct hashentry *const he, struct datahead *dh, int errval)
  55. {
  56. bool all_written = true;
  57. ssize_t total;
  58. time_t t = time (NULL);
  59. /* We allocate all data in one memory block: the iov vector,
  60. the response header and the dataset itself. */
  61. struct dataset
  62. {
  63. struct datahead head;
  64. serv_response_header resp;
  65. char strdata[0];
  66. } *dataset;
  67. assert (offsetof (struct dataset, resp) == offsetof (struct datahead, data));
  68. time_t timeout = MAX_TIMEOUT_VALUE;
  69. if (serv == NULL)
  70. {
  71. if (he != NULL && errval == EAGAIN)
  72. {
  73. /* If we have an old record available but cannot find one
  74. now because the service is not available we keep the old
  75. record and make sure it does not get removed. */
  76. if (reload_count != UINT_MAX)
  77. /* Do not reset the value if we never not reload the record. */
  78. dh->nreloads = reload_count - 1;
  79. /* Reload with the same time-to-live value. */
  80. timeout = dh->timeout = t + db->postimeout;
  81. total = 0;
  82. }
  83. else
  84. {
  85. /* We have no data. This means we send the standard reply for this
  86. case. */
  87. total = sizeof (notfound);
  88. if (fd != -1
  89. && TEMP_FAILURE_RETRY (send (fd, &notfound, total,
  90. MSG_NOSIGNAL)) != total)
  91. all_written = false;
  92. /* If we have a transient error or cannot permanently store
  93. the result, so be it. */
  94. if (errval == EAGAIN || __builtin_expect (db->negtimeout == 0, 0))
  95. {
  96. /* Mark the old entry as obsolete. */
  97. if (dh != NULL)
  98. dh->usable = false;
  99. }
  100. else if ((dataset = mempool_alloc (db, (sizeof (struct dataset)
  101. + req->key_len), 1)) != NULL)
  102. {
  103. timeout = datahead_init_neg (&dataset->head,
  104. (sizeof (struct dataset)
  105. + req->key_len), total,
  106. db->negtimeout);
  107. /* This is the reply. */
  108. memcpy (&dataset->resp, &notfound, total);
  109. /* Copy the key data. */
  110. memcpy (dataset->strdata, key, req->key_len);
  111. /* If necessary, we also propagate the data to disk. */
  112. if (db->persistent)
  113. {
  114. // XXX async OK?
  115. uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
  116. msync ((void *) pval,
  117. ((uintptr_t) dataset & pagesize_m1)
  118. + sizeof (struct dataset) + req->key_len, MS_ASYNC);
  119. }
  120. (void) cache_add (req->type, &dataset->strdata, req->key_len,
  121. &dataset->head, true, db, owner, he == NULL);
  122. pthread_rwlock_unlock (&db->lock);
  123. /* Mark the old entry as obsolete. */
  124. if (dh != NULL)
  125. dh->usable = false;
  126. }
  127. }
  128. }
  129. else
  130. {
  131. /* Determine the I/O structure. */
  132. size_t s_name_len = strlen (serv->s_name) + 1;
  133. size_t s_proto_len = strlen (serv->s_proto) + 1;
  134. uint32_t *s_aliases_len;
  135. size_t s_aliases_cnt;
  136. char *aliases;
  137. char *cp;
  138. size_t cnt;
  139. /* Determine the number of aliases. */
  140. s_aliases_cnt = 0;
  141. for (cnt = 0; serv->s_aliases[cnt] != NULL; ++cnt)
  142. ++s_aliases_cnt;
  143. /* Determine the length of all aliases. */
  144. s_aliases_len = (uint32_t *) alloca (s_aliases_cnt * sizeof (uint32_t));
  145. total = 0;
  146. for (cnt = 0; cnt < s_aliases_cnt; ++cnt)
  147. {
  148. s_aliases_len[cnt] = strlen (serv->s_aliases[cnt]) + 1;
  149. total += s_aliases_len[cnt];
  150. }
  151. total += (offsetof (struct dataset, strdata)
  152. + s_name_len
  153. + s_proto_len
  154. + s_aliases_cnt * sizeof (uint32_t));
  155. /* If we refill the cache, first assume the reconrd did not
  156. change. Allocate memory on the cache since it is likely
  157. discarded anyway. If it turns out to be necessary to have a
  158. new record we can still allocate real memory. */
  159. bool alloca_used = false;
  160. dataset = NULL;
  161. if (he == NULL)
  162. dataset = (struct dataset *) mempool_alloc (db, total + req->key_len,
  163. 1);
  164. if (dataset == NULL)
  165. {
  166. /* We cannot permanently add the result in the moment. But
  167. we can provide the result as is. Store the data in some
  168. temporary memory. */
  169. dataset = (struct dataset *) alloca (total + req->key_len);
  170. /* We cannot add this record to the permanent database. */
  171. alloca_used = true;
  172. }
  173. timeout = datahead_init_pos (&dataset->head, total + req->key_len,
  174. total - offsetof (struct dataset, resp),
  175. he == NULL ? 0 : dh->nreloads + 1,
  176. db->postimeout);
  177. dataset->resp.version = NSCD_VERSION;
  178. dataset->resp.found = 1;
  179. dataset->resp.s_name_len = s_name_len;
  180. dataset->resp.s_proto_len = s_proto_len;
  181. dataset->resp.s_port = serv->s_port;
  182. dataset->resp.s_aliases_cnt = s_aliases_cnt;
  183. cp = dataset->strdata;
  184. cp = mempcpy (cp, serv->s_name, s_name_len);
  185. cp = mempcpy (cp, serv->s_proto, s_proto_len);
  186. cp = mempcpy (cp, s_aliases_len, s_aliases_cnt * sizeof (uint32_t));
  187. /* Then the aliases. */
  188. aliases = cp;
  189. for (cnt = 0; cnt < s_aliases_cnt; ++cnt)
  190. cp = mempcpy (cp, serv->s_aliases[cnt], s_aliases_len[cnt]);
  191. assert (cp
  192. == dataset->strdata + total - offsetof (struct dataset,
  193. strdata));
  194. char *key_copy = memcpy (cp, key, req->key_len);
  195. /* Now we can determine whether on refill we have to create a new
  196. record or not. */
  197. if (he != NULL)
  198. {
  199. assert (fd == -1);
  200. if (total + req->key_len == dh->allocsize
  201. && total - offsetof (struct dataset, resp) == dh->recsize
  202. && memcmp (&dataset->resp, dh->data,
  203. dh->allocsize - offsetof (struct dataset, resp)) == 0)
  204. {
  205. /* The data has not changed. We will just bump the
  206. timeout value. Note that the new record has been
  207. allocated on the stack and need not be freed. */
  208. dh->timeout = dataset->head.timeout;
  209. ++dh->nreloads;
  210. }
  211. else
  212. {
  213. /* We have to create a new record. Just allocate
  214. appropriate memory and copy it. */
  215. struct dataset *newp
  216. = (struct dataset *) mempool_alloc (db, total + req->key_len,
  217. 1);
  218. if (newp != NULL)
  219. {
  220. /* Adjust pointers into the memory block. */
  221. aliases = (char *) newp + (aliases - (char *) dataset);
  222. assert (key_copy != NULL);
  223. key_copy = (char *) newp + (key_copy - (char *) dataset);
  224. dataset = memcpy (newp, dataset, total + req->key_len);
  225. alloca_used = false;
  226. }
  227. /* Mark the old record as obsolete. */
  228. dh->usable = false;
  229. }
  230. }
  231. else
  232. {
  233. /* We write the dataset before inserting it to the database
  234. since while inserting this thread might block and so would
  235. unnecessarily keep the receiver waiting. */
  236. assert (fd != -1);
  237. if (writeall (fd, &dataset->resp, dataset->head.recsize)
  238. != dataset->head.recsize)
  239. all_written = false;
  240. }
  241. /* Add the record to the database. But only if it has not been
  242. stored on the stack. */
  243. if (! alloca_used)
  244. {
  245. /* If necessary, we also propagate the data to disk. */
  246. if (db->persistent)
  247. {
  248. // XXX async OK?
  249. uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
  250. msync ((void *) pval,
  251. ((uintptr_t) dataset & pagesize_m1)
  252. + total + req->key_len, MS_ASYNC);
  253. }
  254. (void) cache_add (req->type, key_copy, req->key_len,
  255. &dataset->head, true, db, owner, he == NULL);
  256. pthread_rwlock_unlock (&db->lock);
  257. }
  258. }
  259. if (__builtin_expect (!all_written, 0) && debug_level > 0)
  260. {
  261. char buf[256];
  262. dbg_log (_("short write in %s: %s"), __FUNCTION__,
  263. strerror_r (errno, buf, sizeof (buf)));
  264. }
  265. return timeout;
  266. }
  267. static int
  268. lookup (int type, char *key, struct servent *resultbufp, char *buffer,
  269. size_t buflen, struct servent **serv)
  270. {
  271. char *proto = strrchr (key, '/');
  272. if (proto != NULL && proto != key)
  273. {
  274. key = strndupa (key, proto - key);
  275. if (proto[1] == '\0')
  276. proto = NULL;
  277. else
  278. ++proto;
  279. }
  280. if (type == GETSERVBYNAME)
  281. return __getservbyname_r (key, proto, resultbufp, buffer, buflen, serv);
  282. assert (type == GETSERVBYPORT);
  283. return __getservbyport_r (atol (key), proto, resultbufp, buffer, buflen,
  284. serv);
  285. }
  286. static time_t
  287. addservbyX (struct database_dyn *db, int fd, request_header *req,
  288. char *key, uid_t uid, struct hashentry *he, struct datahead *dh)
  289. {
  290. /* Search for the entry matching the key. Please note that we don't
  291. look again in the table whether the dataset is now available. We
  292. simply insert it. It does not matter if it is in there twice. The
  293. pruning function only will look at the timestamp. */
  294. struct servent resultbuf;
  295. struct servent *serv;
  296. int errval = 0;
  297. struct scratch_buffer tmpbuf;
  298. scratch_buffer_init (&tmpbuf);
  299. if (__glibc_unlikely (debug_level > 0))
  300. {
  301. if (he == NULL)
  302. dbg_log (_("Haven't found \"%s\" in services cache!"), key);
  303. else
  304. dbg_log (_("Reloading \"%s\" in services cache!"), key);
  305. }
  306. while (lookup (req->type, key, &resultbuf,
  307. tmpbuf.data, tmpbuf.length, &serv) != 0
  308. && (errval = errno) == ERANGE)
  309. if (!scratch_buffer_grow (&tmpbuf))
  310. {
  311. /* We ran out of memory. We cannot do anything but sending a
  312. negative response. In reality this should never
  313. happen. */
  314. serv = NULL;
  315. /* We set the error to indicate this is (possibly) a temporary
  316. error and that it does not mean the entry is not available
  317. at all. */
  318. errval = EAGAIN;
  319. break;
  320. }
  321. time_t timeout = cache_addserv (db, fd, req, key, serv, uid, he, dh, errval);
  322. scratch_buffer_free (&tmpbuf);
  323. return timeout;
  324. }
  325. void
  326. addservbyname (struct database_dyn *db, int fd, request_header *req,
  327. void *key, uid_t uid)
  328. {
  329. addservbyX (db, fd, req, key, uid, NULL, NULL);
  330. }
  331. time_t
  332. readdservbyname (struct database_dyn *db, struct hashentry *he,
  333. struct datahead *dh)
  334. {
  335. request_header req =
  336. {
  337. .type = GETSERVBYNAME,
  338. .key_len = he->len
  339. };
  340. return addservbyX (db, -1, &req, db->data + he->key, he->owner, he, dh);
  341. }
  342. void
  343. addservbyport (struct database_dyn *db, int fd, request_header *req,
  344. void *key, uid_t uid)
  345. {
  346. addservbyX (db, fd, req, key, uid, NULL, NULL);
  347. }
  348. time_t
  349. readdservbyport (struct database_dyn *db, struct hashentry *he,
  350. struct datahead *dh)
  351. {
  352. request_header req =
  353. {
  354. .type = GETSERVBYPORT,
  355. .key_len = he->len
  356. };
  357. return addservbyX (db, -1, &req, db->data + he->key, he->owner, he, dh);
  358. }