1 | /* Copyright (C) 1999-2019 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. |
3 | Contributed by Andreas Jaeger <aj@suse.de>, 1999. |
4 | |
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. |
9 | |
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. |
14 | |
15 | You should have received a copy of the GNU General Public License |
16 | along with this program; if not, see <http://www.gnu.org/licenses/>. */ |
17 | |
18 | #include <errno.h> |
19 | #include <error.h> |
20 | #include <dirent.h> |
21 | #include <inttypes.h> |
22 | #include <libgen.h> |
23 | #include <libintl.h> |
24 | #include <stdio.h> |
25 | #include <stdlib.h> |
26 | #include <string.h> |
27 | #include <unistd.h> |
28 | #include <stdint.h> |
29 | #include <sys/fcntl.h> |
30 | #include <sys/mman.h> |
31 | #include <sys/stat.h> |
32 | #include <sys/types.h> |
33 | |
34 | #include <ldconfig.h> |
35 | #include <dl-cache.h> |
36 | |
37 | struct cache_entry |
38 | { |
39 | char *lib; /* Library name. */ |
40 | char *path; /* Path to find library. */ |
41 | int flags; /* Flags to indicate kind of library. */ |
42 | unsigned int osversion; /* Required OS version. */ |
43 | uint64_t hwcap; /* Important hardware capabilities. */ |
44 | int bits_hwcap; /* Number of bits set in hwcap. */ |
45 | struct cache_entry *next; /* Next entry in list. */ |
46 | }; |
47 | |
48 | /* List of all cache entries. */ |
49 | static struct cache_entry *entries; |
50 | |
51 | static const char *flag_descr[] = |
52 | { "libc4" , "ELF" , "libc5" , "libc6" }; |
53 | |
54 | /* Print a single entry. */ |
55 | static void |
56 | print_entry (const char *lib, int flag, unsigned int osversion, |
57 | uint64_t hwcap, const char *key) |
58 | { |
59 | printf ("\t%s (" , lib); |
60 | switch (flag & FLAG_TYPE_MASK) |
61 | { |
62 | case FLAG_LIBC4: |
63 | case FLAG_ELF: |
64 | case FLAG_ELF_LIBC5: |
65 | case FLAG_ELF_LIBC6: |
66 | fputs (flag_descr[flag & FLAG_TYPE_MASK], stdout); |
67 | break; |
68 | default: |
69 | fputs (_("unknown" ), stdout); |
70 | break; |
71 | } |
72 | switch (flag & FLAG_REQUIRED_MASK) |
73 | { |
74 | case FLAG_SPARC_LIB64: |
75 | fputs (",64bit" , stdout); |
76 | break; |
77 | case FLAG_IA64_LIB64: |
78 | fputs (",IA-64" , stdout); |
79 | break; |
80 | case FLAG_X8664_LIB64: |
81 | fputs (",x86-64" , stdout); |
82 | break; |
83 | case FLAG_S390_LIB64: |
84 | fputs (",64bit" , stdout); |
85 | break; |
86 | case FLAG_POWERPC_LIB64: |
87 | fputs (",64bit" , stdout); |
88 | break; |
89 | case FLAG_MIPS64_LIBN32: |
90 | fputs (",N32" , stdout); |
91 | break; |
92 | case FLAG_MIPS64_LIBN64: |
93 | fputs (",64bit" , stdout); |
94 | break; |
95 | case FLAG_X8664_LIBX32: |
96 | fputs (",x32" , stdout); |
97 | break; |
98 | case FLAG_ARM_LIBHF: |
99 | fputs (",hard-float" , stdout); |
100 | break; |
101 | case FLAG_AARCH64_LIB64: |
102 | fputs (",AArch64" , stdout); |
103 | break; |
104 | /* Uses the ARM soft-float ABI. */ |
105 | case FLAG_ARM_LIBSF: |
106 | fputs (",soft-float" , stdout); |
107 | break; |
108 | case FLAG_MIPS_LIB32_NAN2008: |
109 | fputs (",nan2008" , stdout); |
110 | break; |
111 | case FLAG_MIPS64_LIBN32_NAN2008: |
112 | fputs (",N32,nan2008" , stdout); |
113 | break; |
114 | case FLAG_MIPS64_LIBN64_NAN2008: |
115 | fputs (",64bit,nan2008" , stdout); |
116 | break; |
117 | case FLAG_RISCV_FLOAT_ABI_SOFT: |
118 | fputs (",soft-float" , stdout); |
119 | break; |
120 | case FLAG_RISCV_FLOAT_ABI_DOUBLE: |
121 | fputs (",double-float" , stdout); |
122 | break; |
123 | case 0: |
124 | break; |
125 | default: |
126 | printf (",%d" , flag & FLAG_REQUIRED_MASK); |
127 | break; |
128 | } |
129 | if (hwcap != 0) |
130 | printf (", hwcap: %#.16" PRIx64, hwcap); |
131 | if (osversion != 0) |
132 | { |
133 | static const char *const abi_tag_os[] = |
134 | { |
135 | [0] = "Linux" , |
136 | [1] = "Hurd" , |
137 | [2] = "Solaris" , |
138 | [3] = "FreeBSD" , |
139 | [4] = "kNetBSD" , |
140 | [5] = "Syllable" , |
141 | [6] = N_("Unknown OS" ) |
142 | }; |
143 | #define MAXTAG (sizeof abi_tag_os / sizeof abi_tag_os[0] - 1) |
144 | unsigned int os = osversion >> 24; |
145 | |
146 | printf (_(", OS ABI: %s %d.%d.%d" ), |
147 | _(abi_tag_os[os > MAXTAG ? MAXTAG : os]), |
148 | (osversion >> 16) & 0xff, |
149 | (osversion >> 8) & 0xff, |
150 | osversion & 0xff); |
151 | } |
152 | printf (") => %s\n" , key); |
153 | } |
154 | |
155 | |
156 | /* Print the whole cache file, if a file contains the new cache format |
157 | hidden in the old one, print the contents of the new format. */ |
158 | void |
159 | print_cache (const char *cache_name) |
160 | { |
161 | int fd = open (cache_name, O_RDONLY); |
162 | if (fd < 0) |
163 | error (EXIT_FAILURE, errno, _("Can't open cache file %s\n" ), cache_name); |
164 | |
165 | struct stat64 st; |
166 | if (fstat64 (fd, &st) < 0 |
167 | /* No need to map the file if it is empty. */ |
168 | || st.st_size == 0) |
169 | { |
170 | close (fd); |
171 | return; |
172 | } |
173 | |
174 | struct cache_file *cache |
175 | = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
176 | if (cache == MAP_FAILED) |
177 | error (EXIT_FAILURE, errno, _("mmap of cache file failed.\n" )); |
178 | |
179 | size_t cache_size = st.st_size; |
180 | if (cache_size < sizeof (struct cache_file)) |
181 | error (EXIT_FAILURE, 0, _("File is not a cache file.\n" )); |
182 | |
183 | struct cache_file_new *cache_new = NULL; |
184 | const char *cache_data; |
185 | int format = 0; |
186 | |
187 | if (memcmp (cache->magic, CACHEMAGIC, sizeof CACHEMAGIC - 1)) |
188 | { |
189 | /* This can only be the new format without the old one. */ |
190 | cache_new = (struct cache_file_new *) cache; |
191 | |
192 | if (memcmp (cache_new->magic, CACHEMAGIC_NEW, sizeof CACHEMAGIC_NEW - 1) |
193 | || memcmp (cache_new->version, CACHE_VERSION, |
194 | sizeof CACHE_VERSION - 1)) |
195 | error (EXIT_FAILURE, 0, _("File is not a cache file.\n" )); |
196 | format = 1; |
197 | /* This is where the strings start. */ |
198 | cache_data = (const char *) cache_new; |
199 | } |
200 | else |
201 | { |
202 | /* Check for corruption, avoiding overflow. */ |
203 | if ((cache_size - sizeof (struct cache_file)) / sizeof (struct file_entry) |
204 | < cache->nlibs) |
205 | error (EXIT_FAILURE, 0, _("File is not a cache file.\n" )); |
206 | |
207 | size_t offset = ALIGN_CACHE (sizeof (struct cache_file) |
208 | + (cache->nlibs |
209 | * sizeof (struct file_entry))); |
210 | /* This is where the strings start. */ |
211 | cache_data = (const char *) &cache->libs[cache->nlibs]; |
212 | |
213 | /* Check for a new cache embedded in the old format. */ |
214 | if (cache_size > |
215 | (offset + sizeof (struct cache_file_new))) |
216 | { |
217 | |
218 | cache_new = (struct cache_file_new *) ((void *)cache + offset); |
219 | |
220 | if (memcmp (cache_new->magic, CACHEMAGIC_NEW, |
221 | sizeof CACHEMAGIC_NEW - 1) == 0 |
222 | && memcmp (cache_new->version, CACHE_VERSION, |
223 | sizeof CACHE_VERSION - 1) == 0) |
224 | { |
225 | cache_data = (const char *) cache_new; |
226 | format = 1; |
227 | } |
228 | } |
229 | } |
230 | |
231 | if (format == 0) |
232 | { |
233 | printf (_("%d libs found in cache `%s'\n" ), cache->nlibs, cache_name); |
234 | |
235 | /* Print everything. */ |
236 | for (unsigned int i = 0; i < cache->nlibs; i++) |
237 | print_entry (cache_data + cache->libs[i].key, |
238 | cache->libs[i].flags, 0, 0, |
239 | cache_data + cache->libs[i].value); |
240 | } |
241 | else if (format == 1) |
242 | { |
243 | printf (_("%d libs found in cache `%s'\n" ), |
244 | cache_new->nlibs, cache_name); |
245 | |
246 | /* Print everything. */ |
247 | for (unsigned int i = 0; i < cache_new->nlibs; i++) |
248 | print_entry (cache_data + cache_new->libs[i].key, |
249 | cache_new->libs[i].flags, |
250 | cache_new->libs[i].osversion, |
251 | cache_new->libs[i].hwcap, |
252 | cache_data + cache_new->libs[i].value); |
253 | } |
254 | /* Cleanup. */ |
255 | munmap (cache, cache_size); |
256 | close (fd); |
257 | } |
258 | |
259 | /* Initialize cache data structures. */ |
260 | void |
261 | init_cache (void) |
262 | { |
263 | entries = NULL; |
264 | } |
265 | |
266 | static int |
267 | compare (const struct cache_entry *e1, const struct cache_entry *e2) |
268 | { |
269 | /* We need to swap entries here to get the correct sort order. */ |
270 | int res = _dl_cache_libcmp (e2->lib, e1->lib); |
271 | if (res == 0) |
272 | { |
273 | if (e1->flags < e2->flags) |
274 | return 1; |
275 | else if (e1->flags > e2->flags) |
276 | return -1; |
277 | /* Sort by most specific hwcap. */ |
278 | else if (e2->bits_hwcap > e1->bits_hwcap) |
279 | return 1; |
280 | else if (e2->bits_hwcap < e1->bits_hwcap) |
281 | return -1; |
282 | else if (e2->hwcap > e1->hwcap) |
283 | return 1; |
284 | else if (e2->hwcap < e1->hwcap) |
285 | return -1; |
286 | if (e2->osversion > e1->osversion) |
287 | return 1; |
288 | if (e2->osversion < e1->osversion) |
289 | return -1; |
290 | } |
291 | return res; |
292 | } |
293 | |
294 | /* Save the contents of the cache. */ |
295 | void |
296 | save_cache (const char *cache_name) |
297 | { |
298 | /* The cache entries are sorted already, save them in this order. */ |
299 | |
300 | /* Count the length of all strings. */ |
301 | /* The old format doesn't contain hwcap entries and doesn't contain |
302 | libraries in subdirectories with hwcaps entries. Count therefore |
303 | also all entries with hwcap == 0. */ |
304 | size_t total_strlen = 0; |
305 | struct cache_entry *entry; |
306 | /* Number of cache entries. */ |
307 | int cache_entry_count = 0; |
308 | /* Number of normal cache entries. */ |
309 | int cache_entry_old_count = 0; |
310 | |
311 | for (entry = entries; entry != NULL; entry = entry->next) |
312 | { |
313 | /* Account the final NULs. */ |
314 | total_strlen += strlen (entry->lib) + strlen (entry->path) + 2; |
315 | ++cache_entry_count; |
316 | if (entry->hwcap == 0) |
317 | ++cache_entry_old_count; |
318 | } |
319 | |
320 | /* Create the on disk cache structure. */ |
321 | struct cache_file *file_entries = NULL; |
322 | size_t file_entries_size = 0; |
323 | |
324 | if (opt_format != 2) |
325 | { |
326 | /* struct cache_file_new is 64-bit aligned on some arches while |
327 | only 32-bit aligned on other arches. Duplicate last old |
328 | cache entry so that new cache in ld.so.cache can be used by |
329 | both. */ |
330 | if (opt_format != 0) |
331 | cache_entry_old_count = (cache_entry_old_count + 1) & ~1; |
332 | |
333 | /* And the list of all entries in the old format. */ |
334 | file_entries_size = sizeof (struct cache_file) |
335 | + cache_entry_old_count * sizeof (struct file_entry); |
336 | file_entries = xmalloc (file_entries_size); |
337 | |
338 | /* Fill in the header. */ |
339 | memset (file_entries, '\0', sizeof (struct cache_file)); |
340 | memcpy (file_entries->magic, CACHEMAGIC, sizeof CACHEMAGIC - 1); |
341 | |
342 | file_entries->nlibs = cache_entry_old_count; |
343 | } |
344 | |
345 | struct cache_file_new *file_entries_new = NULL; |
346 | size_t file_entries_new_size = 0; |
347 | |
348 | if (opt_format != 0) |
349 | { |
350 | /* And the list of all entries in the new format. */ |
351 | file_entries_new_size = sizeof (struct cache_file_new) |
352 | + cache_entry_count * sizeof (struct file_entry_new); |
353 | file_entries_new = xmalloc (file_entries_new_size); |
354 | |
355 | /* Fill in the header. */ |
356 | memset (file_entries_new, '\0', sizeof (struct cache_file_new)); |
357 | memcpy (file_entries_new->magic, CACHEMAGIC_NEW, |
358 | sizeof CACHEMAGIC_NEW - 1); |
359 | memcpy (file_entries_new->version, CACHE_VERSION, |
360 | sizeof CACHE_VERSION - 1); |
361 | |
362 | file_entries_new->nlibs = cache_entry_count; |
363 | file_entries_new->len_strings = total_strlen; |
364 | } |
365 | |
366 | /* Pad for alignment of cache_file_new. */ |
367 | size_t pad = ALIGN_CACHE (file_entries_size) - file_entries_size; |
368 | |
369 | /* If we have both formats, we hide the new format in the strings |
370 | table, we have to adjust all string indices for this so that |
371 | old libc5/glibc 2 dynamic linkers just ignore them. */ |
372 | unsigned int str_offset; |
373 | if (opt_format != 0) |
374 | str_offset = file_entries_new_size; |
375 | else |
376 | str_offset = 0; |
377 | |
378 | /* An array for all strings. */ |
379 | char *strings = xmalloc (total_strlen); |
380 | char *str = strings; |
381 | int idx_old; |
382 | int idx_new; |
383 | |
384 | for (idx_old = 0, idx_new = 0, entry = entries; entry != NULL; |
385 | entry = entry->next, ++idx_new) |
386 | { |
387 | /* First the library. */ |
388 | if (opt_format != 2 && entry->hwcap == 0) |
389 | { |
390 | file_entries->libs[idx_old].flags = entry->flags; |
391 | /* XXX: Actually we can optimize here and remove duplicates. */ |
392 | file_entries->libs[idx_old].key = str_offset + pad; |
393 | } |
394 | if (opt_format != 0) |
395 | { |
396 | /* We could subtract file_entries_new_size from str_offset - |
397 | not doing so makes the code easier, the string table |
398 | always begins at the beginning of the new cache |
399 | struct. */ |
400 | file_entries_new->libs[idx_new].flags = entry->flags; |
401 | file_entries_new->libs[idx_new].osversion = entry->osversion; |
402 | file_entries_new->libs[idx_new].hwcap = entry->hwcap; |
403 | file_entries_new->libs[idx_new].key = str_offset; |
404 | } |
405 | |
406 | size_t len = strlen (entry->lib) + 1; |
407 | str = mempcpy (str, entry->lib, len); |
408 | str_offset += len; |
409 | /* Then the path. */ |
410 | if (opt_format != 2 && entry->hwcap == 0) |
411 | file_entries->libs[idx_old].value = str_offset + pad; |
412 | if (opt_format != 0) |
413 | file_entries_new->libs[idx_new].value = str_offset; |
414 | len = strlen (entry->path) + 1; |
415 | str = mempcpy (str, entry->path, len); |
416 | str_offset += len; |
417 | /* Ignore entries with hwcap for old format. */ |
418 | if (entry->hwcap == 0) |
419 | ++idx_old; |
420 | } |
421 | |
422 | /* Duplicate last old cache entry if needed. */ |
423 | if (opt_format != 2 |
424 | && idx_old < cache_entry_old_count) |
425 | file_entries->libs[idx_old] = file_entries->libs[idx_old - 1]; |
426 | |
427 | /* Write out the cache. */ |
428 | |
429 | /* Write cache first to a temporary file and rename it later. */ |
430 | char *temp_name = xmalloc (strlen (cache_name) + 2); |
431 | sprintf (temp_name, "%s~" , cache_name); |
432 | |
433 | /* Create file. */ |
434 | int fd = open (temp_name, O_CREAT|O_WRONLY|O_TRUNC|O_NOFOLLOW, |
435 | S_IRUSR|S_IWUSR); |
436 | if (fd < 0) |
437 | error (EXIT_FAILURE, errno, _("Can't create temporary cache file %s" ), |
438 | temp_name); |
439 | |
440 | /* Write contents. */ |
441 | if (opt_format != 2) |
442 | { |
443 | if (write (fd, file_entries, file_entries_size) |
444 | != (ssize_t) file_entries_size) |
445 | error (EXIT_FAILURE, errno, _("Writing of cache data failed" )); |
446 | } |
447 | if (opt_format != 0) |
448 | { |
449 | /* Align cache. */ |
450 | if (opt_format != 2) |
451 | { |
452 | char zero[pad]; |
453 | memset (zero, '\0', pad); |
454 | if (write (fd, zero, pad) != (ssize_t) pad) |
455 | error (EXIT_FAILURE, errno, _("Writing of cache data failed" )); |
456 | } |
457 | if (write (fd, file_entries_new, file_entries_new_size) |
458 | != (ssize_t) file_entries_new_size) |
459 | error (EXIT_FAILURE, errno, _("Writing of cache data failed" )); |
460 | } |
461 | |
462 | if (write (fd, strings, total_strlen) != (ssize_t) total_strlen) |
463 | error (EXIT_FAILURE, errno, _("Writing of cache data failed" )); |
464 | |
465 | /* Make sure user can always read cache file */ |
466 | if (chmod (temp_name, S_IROTH|S_IRGRP|S_IRUSR|S_IWUSR)) |
467 | error (EXIT_FAILURE, errno, |
468 | _("Changing access rights of %s to %#o failed" ), temp_name, |
469 | S_IROTH|S_IRGRP|S_IRUSR|S_IWUSR); |
470 | |
471 | /* Make sure that data is written to disk. */ |
472 | if (fsync (fd) != 0 || close (fd) != 0) |
473 | error (EXIT_FAILURE, errno, _("Writing of cache data failed" )); |
474 | |
475 | /* Move temporary to its final location. */ |
476 | if (rename (temp_name, cache_name)) |
477 | error (EXIT_FAILURE, errno, _("Renaming of %s to %s failed" ), temp_name, |
478 | cache_name); |
479 | |
480 | /* Free all allocated memory. */ |
481 | free (file_entries_new); |
482 | free (file_entries); |
483 | free (strings); |
484 | |
485 | while (entries) |
486 | { |
487 | entry = entries; |
488 | entries = entries->next; |
489 | free (entry); |
490 | } |
491 | } |
492 | |
493 | |
494 | /* Add one library to the cache. */ |
495 | void |
496 | add_to_cache (const char *path, const char *lib, int flags, |
497 | unsigned int osversion, uint64_t hwcap) |
498 | { |
499 | size_t liblen = strlen (lib) + 1; |
500 | size_t len = liblen + strlen (path) + 1; |
501 | struct cache_entry *new_entry |
502 | = xmalloc (sizeof (struct cache_entry) + liblen + len); |
503 | |
504 | new_entry->lib = memcpy ((char *) (new_entry + 1), lib, liblen); |
505 | new_entry->path = new_entry->lib + liblen; |
506 | snprintf (new_entry->path, len, "%s/%s" , path, lib); |
507 | new_entry->flags = flags; |
508 | new_entry->osversion = osversion; |
509 | new_entry->hwcap = hwcap; |
510 | new_entry->bits_hwcap = 0; |
511 | |
512 | /* Count the number of bits set in the masked value. */ |
513 | for (size_t i = 0; |
514 | (~((1ULL << i) - 1) & hwcap) != 0 && i < 8 * sizeof (hwcap); ++i) |
515 | if ((hwcap & (1ULL << i)) != 0) |
516 | ++new_entry->bits_hwcap; |
517 | |
518 | |
519 | /* Keep the list sorted - search for right place to insert. */ |
520 | struct cache_entry *ptr = entries; |
521 | struct cache_entry *prev = entries; |
522 | while (ptr != NULL) |
523 | { |
524 | if (compare (ptr, new_entry) > 0) |
525 | break; |
526 | prev = ptr; |
527 | ptr = ptr->next; |
528 | } |
529 | /* Is this the first entry? */ |
530 | if (ptr == entries) |
531 | { |
532 | new_entry->next = entries; |
533 | entries = new_entry; |
534 | } |
535 | else |
536 | { |
537 | new_entry->next = prev->next; |
538 | prev->next = new_entry; |
539 | } |
540 | } |
541 | |
542 | |
543 | /* Auxiliary cache. */ |
544 | |
545 | struct aux_cache_entry_id |
546 | { |
547 | uint64_t ino; |
548 | uint64_t ctime; |
549 | uint64_t size; |
550 | uint64_t dev; |
551 | }; |
552 | |
553 | struct aux_cache_entry |
554 | { |
555 | struct aux_cache_entry_id id; |
556 | int flags; |
557 | unsigned int osversion; |
558 | int used; |
559 | char *soname; |
560 | struct aux_cache_entry *next; |
561 | }; |
562 | |
563 | #define AUX_CACHEMAGIC "glibc-ld.so.auxcache-1.0" |
564 | |
565 | struct aux_cache_file_entry |
566 | { |
567 | struct aux_cache_entry_id id; /* Unique id of entry. */ |
568 | int32_t flags; /* This is 1 for an ELF library. */ |
569 | uint32_t soname; /* String table indice. */ |
570 | uint32_t osversion; /* Required OS version. */ |
571 | int32_t pad; |
572 | }; |
573 | |
574 | /* ldconfig maintains an auxiliary cache file that allows |
575 | only reading those libraries that have changed since the last iteration. |
576 | For this for each library some information is cached in the auxiliary |
577 | cache. */ |
578 | struct aux_cache_file |
579 | { |
580 | char magic[sizeof AUX_CACHEMAGIC - 1]; |
581 | uint32_t nlibs; /* Number of entries. */ |
582 | uint32_t len_strings; /* Size of string table. */ |
583 | struct aux_cache_file_entry libs[0]; /* Entries describing libraries. */ |
584 | /* After this the string table of size len_strings is found. */ |
585 | }; |
586 | |
587 | static const unsigned int primes[] = |
588 | { |
589 | 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071, 262139, |
590 | 524287, 1048573, 2097143, 4194301, 8388593, 16777213, 33554393, |
591 | 67108859, 134217689, 268435399, 536870909, 1073741789, 2147483647 |
592 | }; |
593 | |
594 | static size_t aux_hash_size; |
595 | static struct aux_cache_entry **aux_hash; |
596 | |
597 | /* Simplistic hash function for aux_cache_entry_id. */ |
598 | static unsigned int |
599 | aux_cache_entry_id_hash (struct aux_cache_entry_id *id) |
600 | { |
601 | uint64_t ret = ((id->ino * 11 + id->ctime) * 11 + id->size) * 11 + id->dev; |
602 | return ret ^ (ret >> 32); |
603 | } |
604 | |
605 | static size_t nextprime (size_t x) |
606 | { |
607 | for (unsigned int i = 0; i < sizeof (primes) / sizeof (primes[0]); ++i) |
608 | if (primes[i] >= x) |
609 | return primes[i]; |
610 | return x; |
611 | } |
612 | |
613 | void |
614 | init_aux_cache (void) |
615 | { |
616 | aux_hash_size = primes[3]; |
617 | aux_hash = xcalloc (aux_hash_size, sizeof (struct aux_cache_entry *)); |
618 | } |
619 | |
620 | int |
621 | search_aux_cache (struct stat64 *stat_buf, int *flags, |
622 | unsigned int *osversion, char **soname) |
623 | { |
624 | struct aux_cache_entry_id id; |
625 | id.ino = (uint64_t) stat_buf->st_ino; |
626 | id.ctime = (uint64_t) stat_buf->st_ctime; |
627 | id.size = (uint64_t) stat_buf->st_size; |
628 | id.dev = (uint64_t) stat_buf->st_dev; |
629 | |
630 | unsigned int hash = aux_cache_entry_id_hash (&id); |
631 | struct aux_cache_entry *entry; |
632 | for (entry = aux_hash[hash % aux_hash_size]; entry; entry = entry->next) |
633 | if (id.ino == entry->id.ino |
634 | && id.ctime == entry->id.ctime |
635 | && id.size == entry->id.size |
636 | && id.dev == entry->id.dev) |
637 | { |
638 | *flags = entry->flags; |
639 | *osversion = entry->osversion; |
640 | if (entry->soname != NULL) |
641 | *soname = xstrdup (entry->soname); |
642 | else |
643 | *soname = NULL; |
644 | entry->used = 1; |
645 | return 1; |
646 | } |
647 | |
648 | return 0; |
649 | } |
650 | |
651 | static void |
652 | insert_to_aux_cache (struct aux_cache_entry_id *id, int flags, |
653 | unsigned int osversion, const char *soname, int used) |
654 | { |
655 | size_t hash = aux_cache_entry_id_hash (id) % aux_hash_size; |
656 | struct aux_cache_entry *entry; |
657 | for (entry = aux_hash[hash]; entry; entry = entry->next) |
658 | if (id->ino == entry->id.ino |
659 | && id->ctime == entry->id.ctime |
660 | && id->size == entry->id.size |
661 | && id->dev == entry->id.dev) |
662 | abort (); |
663 | |
664 | size_t len = soname ? strlen (soname) + 1 : 0; |
665 | entry = xmalloc (sizeof (struct aux_cache_entry) + len); |
666 | entry->id = *id; |
667 | entry->flags = flags; |
668 | entry->osversion = osversion; |
669 | entry->used = used; |
670 | if (soname != NULL) |
671 | entry->soname = memcpy ((char *) (entry + 1), soname, len); |
672 | else |
673 | entry->soname = NULL; |
674 | entry->next = aux_hash[hash]; |
675 | aux_hash[hash] = entry; |
676 | } |
677 | |
678 | void |
679 | add_to_aux_cache (struct stat64 *stat_buf, int flags, |
680 | unsigned int osversion, const char *soname) |
681 | { |
682 | struct aux_cache_entry_id id; |
683 | id.ino = (uint64_t) stat_buf->st_ino; |
684 | id.ctime = (uint64_t) stat_buf->st_ctime; |
685 | id.size = (uint64_t) stat_buf->st_size; |
686 | id.dev = (uint64_t) stat_buf->st_dev; |
687 | insert_to_aux_cache (&id, flags, osversion, soname, 1); |
688 | } |
689 | |
690 | /* Load auxiliary cache to search for unchanged entries. */ |
691 | void |
692 | load_aux_cache (const char *aux_cache_name) |
693 | { |
694 | int fd = open (aux_cache_name, O_RDONLY); |
695 | if (fd < 0) |
696 | { |
697 | init_aux_cache (); |
698 | return; |
699 | } |
700 | |
701 | struct stat64 st; |
702 | if (fstat64 (fd, &st) < 0 || st.st_size < sizeof (struct aux_cache_file)) |
703 | { |
704 | close (fd); |
705 | init_aux_cache (); |
706 | return; |
707 | } |
708 | |
709 | size_t aux_cache_size = st.st_size; |
710 | struct aux_cache_file *aux_cache |
711 | = mmap (NULL, aux_cache_size, PROT_READ, MAP_PRIVATE, fd, 0); |
712 | if (aux_cache == MAP_FAILED |
713 | || aux_cache_size < sizeof (struct aux_cache_file) |
714 | || memcmp (aux_cache->magic, AUX_CACHEMAGIC, sizeof AUX_CACHEMAGIC - 1) |
715 | || aux_cache_size != (sizeof(struct aux_cache_file) + |
716 | aux_cache->nlibs * sizeof(struct aux_cache_file_entry) + |
717 | aux_cache->len_strings)) |
718 | { |
719 | close (fd); |
720 | init_aux_cache (); |
721 | return; |
722 | } |
723 | |
724 | aux_hash_size = nextprime (aux_cache->nlibs); |
725 | aux_hash = xcalloc (aux_hash_size, sizeof (struct aux_cache_entry *)); |
726 | |
727 | const char *aux_cache_data |
728 | = (const char *) &aux_cache->libs[aux_cache->nlibs]; |
729 | for (unsigned int i = 0; i < aux_cache->nlibs; ++i) |
730 | insert_to_aux_cache (&aux_cache->libs[i].id, |
731 | aux_cache->libs[i].flags, |
732 | aux_cache->libs[i].osversion, |
733 | aux_cache->libs[i].soname == 0 |
734 | ? NULL : aux_cache_data + aux_cache->libs[i].soname, |
735 | 0); |
736 | |
737 | munmap (aux_cache, aux_cache_size); |
738 | close (fd); |
739 | } |
740 | |
741 | /* Save the contents of the auxiliary cache. */ |
742 | void |
743 | save_aux_cache (const char *aux_cache_name) |
744 | { |
745 | /* Count the length of all sonames. We start with empty string. */ |
746 | size_t total_strlen = 1; |
747 | /* Number of cache entries. */ |
748 | int cache_entry_count = 0; |
749 | |
750 | for (size_t i = 0; i < aux_hash_size; ++i) |
751 | for (struct aux_cache_entry *entry = aux_hash[i]; |
752 | entry != NULL; entry = entry->next) |
753 | if (entry->used) |
754 | { |
755 | ++cache_entry_count; |
756 | if (entry->soname != NULL) |
757 | total_strlen += strlen (entry->soname) + 1; |
758 | } |
759 | |
760 | /* Auxiliary cache. */ |
761 | size_t file_entries_size |
762 | = sizeof (struct aux_cache_file) |
763 | + cache_entry_count * sizeof (struct aux_cache_file_entry); |
764 | struct aux_cache_file *file_entries |
765 | = xmalloc (file_entries_size + total_strlen); |
766 | |
767 | /* Fill in the header of the auxiliary cache. */ |
768 | memset (file_entries, '\0', sizeof (struct aux_cache_file)); |
769 | memcpy (file_entries->magic, AUX_CACHEMAGIC, sizeof AUX_CACHEMAGIC - 1); |
770 | |
771 | file_entries->nlibs = cache_entry_count; |
772 | file_entries->len_strings = total_strlen; |
773 | |
774 | /* Initial String offset for auxiliary cache is always after the |
775 | special empty string. */ |
776 | unsigned int str_offset = 1; |
777 | |
778 | /* An array for all strings. */ |
779 | char *str = (char *) file_entries + file_entries_size; |
780 | *str++ = '\0'; |
781 | |
782 | size_t idx = 0; |
783 | for (size_t i = 0; i < aux_hash_size; ++i) |
784 | for (struct aux_cache_entry *entry = aux_hash[i]; |
785 | entry != NULL; entry = entry->next) |
786 | if (entry->used) |
787 | { |
788 | file_entries->libs[idx].id = entry->id; |
789 | file_entries->libs[idx].flags = entry->flags; |
790 | if (entry->soname == NULL) |
791 | file_entries->libs[idx].soname = 0; |
792 | else |
793 | { |
794 | file_entries->libs[idx].soname = str_offset; |
795 | |
796 | size_t len = strlen (entry->soname) + 1; |
797 | str = mempcpy (str, entry->soname, len); |
798 | str_offset += len; |
799 | } |
800 | file_entries->libs[idx].osversion = entry->osversion; |
801 | file_entries->libs[idx++].pad = 0; |
802 | } |
803 | |
804 | /* Write out auxiliary cache file. */ |
805 | /* Write auxiliary cache first to a temporary file and rename it later. */ |
806 | |
807 | char *temp_name = xmalloc (strlen (aux_cache_name) + 2); |
808 | sprintf (temp_name, "%s~" , aux_cache_name); |
809 | |
810 | /* Check that directory exists and create if needed. */ |
811 | char *dir = strdupa (aux_cache_name); |
812 | dir = dirname (dir); |
813 | |
814 | struct stat64 st; |
815 | if (stat64 (dir, &st) < 0) |
816 | { |
817 | if (mkdir (dir, 0700) < 0) |
818 | goto out_fail; |
819 | } |
820 | |
821 | /* Create file. */ |
822 | int fd = open (temp_name, O_CREAT|O_WRONLY|O_TRUNC|O_NOFOLLOW, |
823 | S_IRUSR|S_IWUSR); |
824 | if (fd < 0) |
825 | goto out_fail; |
826 | |
827 | if (write (fd, file_entries, file_entries_size + total_strlen) |
828 | != (ssize_t) (file_entries_size + total_strlen) |
829 | || fdatasync (fd) != 0 |
830 | || close (fd) != 0) |
831 | { |
832 | unlink (temp_name); |
833 | goto out_fail; |
834 | } |
835 | |
836 | /* Move temporary to its final location. */ |
837 | if (rename (temp_name, aux_cache_name)) |
838 | unlink (temp_name); |
839 | |
840 | out_fail: |
841 | /* Free allocated memory. */ |
842 | free (temp_name); |
843 | free (file_entries); |
844 | } |
845 | |