include/string.h: Also redirect calls if not inlined in libpthread
[glibc.git] / elf / ldconfig.c
blob1432187e9b3a14fae9679b59d7b4b9a744b0e51e
1 /* Copyright (C) 1999-2025 Free Software Foundation, Inc.
2 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.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <https://www.gnu.org/licenses/>. */
17 #define PROCINFO_CLASS static
18 #include <assert.h>
19 #include <alloca.h>
20 #include <argp.h>
21 #include <dirent.h>
22 #include <elf.h>
23 #include <error.h>
24 #include <errno.h>
25 #include <inttypes.h>
26 #include <libintl.h>
27 #include <locale.h>
28 #include <stdbool.h>
29 #include <stdio.h>
30 #include <stdio_ext.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <stdint.h>
35 #include <sys/fcntl.h>
36 #include <sys/mman.h>
37 #include <sys/stat.h>
38 #include <sys/types.h>
39 #include <glob.h>
40 #include <libgen.h>
42 #include <ldconfig.h>
43 #include <endswith.h>
44 #include <dl-cache.h>
45 #include <dl-hwcaps.h>
46 #include <dl-is_dso.h>
48 #include <dl-procinfo.h>
50 #ifndef LD_SO_CONF
51 # define LD_SO_CONF SYSCONFDIR "/ld.so.conf"
52 #endif
54 /* Get libc version number. */
55 #include <version.h>
57 #define PACKAGE _libc_intl_domainname
59 /* List of directories to handle. */
60 struct dir_entry
62 char *path;
63 int flag;
64 ino64_t ino;
65 dev_t dev;
66 const char *from_file;
67 int from_line;
69 /* Non-NULL for subdirectories under a glibc-hwcaps subdirectory. */
70 struct glibc_hwcaps_subdirectory *hwcaps;
72 struct dir_entry *next;
75 /* The list is unsorted, contains no duplicates. Entries are added at
76 the end. */
77 static struct dir_entry *dir_entries;
79 /* Flags for different options. */
80 /* Print Cache. */
81 static int opt_print_cache;
83 /* Be verbose. */
84 int opt_verbose;
86 /* Format to support. */
87 enum opt_format opt_format = opt_format_new;
89 /* Build cache. */
90 static int opt_build_cache = 1;
92 /* Enable symbolic link processing. If set, create or update symbolic
93 links, and remove stale symbolic links. */
94 static int opt_link = 1;
96 /* Only process directories specified on the command line. */
97 static int opt_only_cline;
99 /* Path to root for chroot. */
100 static char *opt_chroot;
102 /* Manually link given shared libraries. */
103 static int opt_manual_link;
105 /* Should we ignore an old auxiliary cache file? */
106 static int opt_ignore_aux_cache;
108 /* Cache file to use. */
109 static char *cache_file;
111 /* Configuration file. */
112 static const char *config_file;
114 /* Name and version of program. */
115 static void print_version (FILE *stream, struct argp_state *state);
116 void (*argp_program_version_hook) (FILE *, struct argp_state *)
117 = print_version;
119 /* Function to print some extra text in the help message. */
120 static char *more_help (int key, const char *text, void *input);
122 /* Definitions of arguments for argp functions. */
123 static const struct argp_option options[] =
125 { "print-cache", 'p', NULL, 0, N_("Print cache"), 0},
126 { "verbose", 'v', NULL, 0, N_("Generate verbose messages"), 0},
127 { NULL, 'N', NULL, 0, N_("Don't build cache"), 0},
128 { NULL, 'X', NULL, 0, N_("Don't update symbolic links"), 0},
129 { NULL, 'r', N_("ROOT"), 0, N_("Change to and use ROOT as root directory"), 0},
130 { NULL, 'C', N_("CACHE"), 0, N_("Use CACHE as cache file"), 0},
131 { NULL, 'f', N_("CONF"), 0, N_("Use CONF as configuration file"), 0},
132 { NULL, 'n', NULL, 0, N_("Only process directories specified on the command line. Don't build cache."), 0},
133 { NULL, 'l', NULL, 0, N_("Manually link individual libraries."), 0},
134 { "format", 'c', N_("FORMAT"), 0, N_("Format to use: new (default), old, or compat"), 0},
135 { "ignore-aux-cache", 'i', NULL, 0, N_("Ignore auxiliary cache file"), 0},
136 { NULL, 0, NULL, 0, NULL, 0 }
139 #define PROCINFO_CLASS static
140 #include <dl-procinfo.c>
142 /* Short description of program. */
143 static const char doc[] = N_("Configure Dynamic Linker Run Time Bindings.");
145 /* Prototype for option handler. */
146 static error_t parse_opt (int key, char *arg, struct argp_state *state);
148 /* Data structure to communicate with argp functions. */
149 static struct argp argp =
151 options, parse_opt, NULL, doc, NULL, more_help, NULL
154 /* Handle program arguments. */
155 static error_t
156 parse_opt (int key, char *arg, struct argp_state *state)
158 switch (key)
160 case 'C':
161 cache_file = arg;
162 /* Ignore auxiliary cache since we use non-standard cache. */
163 opt_ignore_aux_cache = 1;
164 break;
165 case 'f':
166 config_file = arg;
167 break;
168 case 'i':
169 opt_ignore_aux_cache = 1;
170 break;
171 case 'l':
172 opt_manual_link = 1;
173 break;
174 case 'N':
175 opt_build_cache = 0;
176 break;
177 case 'n':
178 opt_build_cache = 0;
179 opt_only_cline = 1;
180 break;
181 case 'p':
182 opt_print_cache = 1;
183 break;
184 case 'r':
185 opt_chroot = arg;
186 break;
187 case 'v':
188 opt_verbose = 1;
189 break;
190 case 'X':
191 opt_link = 0;
192 break;
193 case 'c':
194 if (strcmp (arg, "old") == 0)
195 opt_format = opt_format_old;
196 else if (strcmp (arg, "compat") == 0)
197 opt_format = opt_format_compat;
198 else if (strcmp (arg, "new") == 0)
199 opt_format = opt_format_new;
200 break;
201 default:
202 return ARGP_ERR_UNKNOWN;
205 return 0;
208 /* Print bug-reporting information in the help message. */
209 static char *
210 more_help (int key, const char *text, void *input)
212 char *tp = NULL;
213 switch (key)
215 case ARGP_KEY_HELP_EXTRA:
216 /* We print some extra information. */
217 if (asprintf (&tp, gettext ("\
218 For bug reporting instructions, please see:\n\
219 %s.\n"), REPORT_BUGS_TO) < 0)
220 return NULL;
221 return tp;
222 default:
223 break;
225 return (char *) text;
228 /* Print the version information. */
229 static void
230 print_version (FILE *stream, struct argp_state *state)
232 fprintf (stream, "ldconfig %s%s\n", PKGVERSION, VERSION);
233 fprintf (stream, gettext ("\
234 Copyright (C) %s Free Software Foundation, Inc.\n\
235 This is free software; see the source for copying conditions. There is NO\n\
236 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
237 "), "2024");
238 fprintf (stream, gettext ("Written by %s.\n"),
239 "Andreas Jaeger");
242 /* Allocate a new subdirectory with full path PATH under ENTRY, using
243 inode data from *ST. */
244 static struct dir_entry *
245 new_sub_entry (const struct dir_entry *entry, const char *path,
246 const struct stat *st)
248 struct dir_entry *new_entry = xmalloc (sizeof (struct dir_entry));
249 new_entry->from_file = entry->from_file;
250 new_entry->from_line = entry->from_line;
251 new_entry->path = xstrdup (path);
252 new_entry->flag = entry->flag;
253 new_entry->hwcaps = NULL;
254 new_entry->next = NULL;
255 new_entry->ino = st->st_ino;
256 new_entry->dev = st->st_dev;
257 return new_entry;
260 /* Add a single directory entry. Return true if the directory is
261 actually added (because it is not a duplicate). */
262 static bool
263 add_single_dir (struct dir_entry *entry, int verbose)
265 struct dir_entry *ptr, *prev;
266 bool added = true;
268 ptr = dir_entries;
269 prev = ptr;
270 while (ptr != NULL)
272 /* Check for duplicates. */
273 if (ptr->ino == entry->ino && ptr->dev == entry->dev)
275 if (opt_verbose && verbose)
277 error (0, 0, _("Path `%s' given more than once"), entry->path);
278 fprintf (stderr, _("(from %s:%d and %s:%d)\n"),
279 entry->from_file, entry->from_line,
280 ptr->from_file, ptr->from_line);
282 /* Use the newer information. */
283 ptr->flag = entry->flag;
284 free (entry->path);
285 free (entry);
286 added = false;
287 break;
289 prev = ptr;
290 ptr = ptr->next;
292 /* Is this the first entry? */
293 if (ptr == NULL && dir_entries == NULL)
294 dir_entries = entry;
295 else if (ptr == NULL)
296 prev->next = entry;
297 return added;
300 /* Check if PATH contains a "glibc-hwcaps" subdirectory. If so, queue
301 its subdirectories for glibc-hwcaps processing. */
302 static void
303 add_glibc_hwcaps_subdirectories (struct dir_entry *entry, const char *path)
305 /* glibc-hwcaps subdirectories do not nest. */
306 assert (entry->hwcaps == NULL);
308 char *glibc_hwcaps;
309 if (asprintf (&glibc_hwcaps, "%s/" GLIBC_HWCAPS_SUBDIRECTORY, path) < 0)
310 error (EXIT_FAILURE, errno, _("Could not form glibc-hwcaps path"));
312 DIR *dir = opendir (glibc_hwcaps);
313 if (dir != NULL)
315 while (true)
317 errno = 0;
318 struct dirent64 *e = readdir64 (dir);
319 if (e == NULL)
321 if (errno == 0)
322 break;
323 else
324 error (EXIT_FAILURE, errno, _("Listing directory %s"), path);
327 /* Ignore hidden subdirectories, including "." and "..", and
328 regular files. File names containing a ':' cannot be
329 looked up by the dynamic loader, so skip those as
330 well. */
331 if (e->d_name[0] == '.' || e->d_type == DT_REG
332 || strchr (e->d_name, ':') != NULL)
333 continue;
335 /* See if this entry eventually resolves to a directory. */
336 struct stat st;
337 if (fstatat (dirfd (dir), e->d_name, &st, 0) < 0)
338 /* Ignore unreadable entries. */
339 continue;
341 if (S_ISDIR (st.st_mode))
343 /* This is a directory, so it needs to be scanned for
344 libraries, associated with the hwcaps implied by the
345 subdirectory name. */
346 char *new_path;
347 if (asprintf (&new_path, "%s/" GLIBC_HWCAPS_SUBDIRECTORY "/%s",
348 /* Use non-canonicalized path here. */
349 entry->path, e->d_name) < 0)
350 error (EXIT_FAILURE, errno,
351 _("Could not form glibc-hwcaps path"));
352 struct dir_entry *new_entry = new_sub_entry (entry, new_path,
353 &st);
354 free (new_path);
355 new_entry->hwcaps = new_glibc_hwcaps_subdirectory (e->d_name);
356 add_single_dir (new_entry, 0);
360 closedir (dir);
363 free (glibc_hwcaps);
366 /* Add one directory to the list of directories to process. */
367 static void
368 add_dir_1 (const char *line, const char *from_file, int from_line)
370 unsigned int i;
371 struct dir_entry *entry = xmalloc (sizeof (struct dir_entry));
372 entry->hwcaps = NULL;
373 entry->next = NULL;
375 entry->from_file = strdup (from_file);
376 entry->from_line = from_line;
378 entry->path = xstrdup (line);
379 entry->flag = FLAG_ELF_LIBC6;
381 /* Canonify path: for now only remove leading and trailing
382 whitespace and the trailing slashes. */
383 i = strlen (entry->path);
385 while (i > 0 && isspace (entry->path[i - 1]))
386 entry->path[--i] = '\0';
388 while (i > 0 && entry->path[i - 1] == '/')
389 entry->path[--i] = '\0';
391 if (i == 0)
393 free (entry->path);
394 free (entry);
395 return;
398 char *path = entry->path;
399 if (opt_chroot != NULL)
400 path = chroot_canon (opt_chroot, path);
402 struct stat stat_buf;
403 if (path == NULL || stat (path, &stat_buf))
405 if (opt_verbose)
406 error (0, errno, _("Can't stat %s"), entry->path);
407 free (entry->path);
408 free (entry);
410 else
412 entry->ino = stat_buf.st_ino;
413 entry->dev = stat_buf.st_dev;
415 if (add_single_dir (entry, 1))
416 /* Add glibc-hwcaps subdirectories if present. */
417 add_glibc_hwcaps_subdirectories (entry, path);
420 if (opt_chroot != NULL)
421 free (path);
424 static void
425 add_dir (const char *line)
427 add_dir_1 (line, "<builtin>", 0);
430 static int
431 chroot_stat (const char *real_path, const char *path, struct stat *st)
433 int ret;
434 char *canon_path;
436 if (!opt_chroot)
437 return stat (real_path, st);
439 ret = lstat (real_path, st);
440 if (ret || !S_ISLNK (st->st_mode))
441 return ret;
443 canon_path = chroot_canon (opt_chroot, path);
444 if (canon_path == NULL)
445 return -1;
447 ret = stat (canon_path, st);
448 free (canon_path);
449 return ret;
452 /* Create a symbolic link from soname to libname in directory path. */
453 static void
454 create_links (const char *real_path, const char *path, const char *libname,
455 const char *soname)
457 char *full_libname, *full_soname;
458 char *real_full_libname, *real_full_soname;
459 struct stat stat_lib, stat_so, lstat_so;
460 int do_link = 1;
461 int do_remove = 1;
462 /* XXX: The logics in this function should be simplified. */
464 /* Get complete path. */
465 full_libname = alloca (strlen (path) + strlen (libname) + 2);
466 full_soname = alloca (strlen (path) + strlen (soname) + 2);
467 sprintf (full_libname, "%s/%s", path, libname);
468 sprintf (full_soname, "%s/%s", path, soname);
469 if (opt_chroot != NULL)
471 real_full_libname = alloca (strlen (real_path) + strlen (libname) + 2);
472 real_full_soname = alloca (strlen (real_path) + strlen (soname) + 2);
473 sprintf (real_full_libname, "%s/%s", real_path, libname);
474 sprintf (real_full_soname, "%s/%s", real_path, soname);
476 else
478 real_full_libname = full_libname;
479 real_full_soname = full_soname;
482 /* Does soname already exist and point to the right library? */
483 if (chroot_stat (real_full_soname, full_soname, &stat_so) == 0)
485 if (chroot_stat (real_full_libname, full_libname, &stat_lib))
487 error (0, 0, _("Can't stat %s\n"), full_libname);
488 return;
490 if (stat_lib.st_dev == stat_so.st_dev
491 && stat_lib.st_ino == stat_so.st_ino)
492 /* Link is already correct. */
493 do_link = 0;
494 else if (lstat (full_soname, &lstat_so) == 0
495 && !S_ISLNK (lstat_so.st_mode))
497 error (0, 0, _("%s is not a symbolic link\n"), full_soname);
498 do_link = 0;
499 do_remove = 0;
502 else if (lstat (real_full_soname, &lstat_so) != 0
503 || !S_ISLNK (lstat_so.st_mode))
504 /* Unless it is a stale symlink, there is no need to remove. */
505 do_remove = 0;
507 if (opt_verbose)
508 printf ("\t%s -> %s", soname, libname);
510 if (do_link && opt_link)
512 /* Remove old link. */
513 if (do_remove)
514 if (unlink (real_full_soname))
516 error (0, 0, _("Can't unlink %s"), full_soname);
517 do_link = 0;
519 /* Create symbolic link. */
520 if (do_link && symlink (libname, real_full_soname))
522 error (0, 0, _("Can't link %s to %s"), full_soname, libname);
523 do_link = 0;
525 if (opt_verbose)
527 if (do_link)
528 fputs (_(" (changed)\n"), stdout);
529 else
530 fputs (_(" (SKIPPED)\n"), stdout);
533 else if (opt_verbose)
534 fputs ("\n", stdout);
537 /* Manually link the given library. */
538 static void
539 manual_link (char *library)
541 char *path;
542 char *real_path;
543 char *real_library;
544 char *libname;
545 char *soname;
546 struct stat stat_buf;
547 int flag;
548 unsigned int isa_level;
550 /* Prepare arguments for create_links call. Split library name in
551 directory and filename first. Since path is allocated, we've got
552 to be careful to free at the end. */
553 path = xstrdup (library);
554 libname = strrchr (path, '/');
556 if (libname)
558 /* Successfully split names. Check if path is just "/" to avoid
559 an empty path. */
560 if (libname == path)
562 libname = library + 1;
563 path = xrealloc (path, 2);
564 strcpy (path, "/");
566 else
568 *libname = '\0';
569 ++libname;
572 else
574 /* There's no path, construct one. */
575 libname = library;
576 path = xrealloc (path, 2);
577 strcpy (path, ".");
580 if (opt_chroot != NULL)
582 real_path = chroot_canon (opt_chroot, path);
583 if (real_path == NULL)
585 error (0, errno, _("Can't find %s"), path);
586 free (path);
587 return;
589 real_library = alloca (strlen (real_path) + strlen (libname) + 2);
590 sprintf (real_library, "%s/%s", real_path, libname);
592 else
594 real_path = path;
595 real_library = library;
598 /* Do some sanity checks first. */
599 if (lstat (real_library, &stat_buf))
601 error (0, errno, _("Cannot lstat %s"), library);
602 goto out;
604 /* We don't want links here! */
605 else if (!S_ISREG (stat_buf.st_mode))
607 error (0, 0, _("Ignored file %s since it is not a regular file."),
608 library);
609 goto out;
612 if (process_file (real_library, library, libname, &flag, &isa_level, &soname,
613 0, &stat_buf))
615 error (0, 0, _("No link created since soname could not be found for %s"),
616 library);
617 goto out;
619 if (soname == NULL)
620 soname = xstrdup (libname);
621 create_links (real_path, path, libname, soname);
622 free (soname);
623 out:
624 if (path != real_path)
625 free (real_path);
626 free (path);
630 /* Read a whole directory and search for libraries.
631 The purpose is two-fold:
632 - search for libraries which will be added to the cache
633 - create symbolic links to the soname for each library
635 This has to be done separately for each directory.
637 To keep track of which libraries to add to the cache and which
638 links to create, we save a list of all libraries.
640 The algorithm is basically:
641 for all libraries in the directory do
642 get soname of library
643 if soname is already in list
644 if new library is newer, replace entry
645 otherwise ignore this library
646 otherwise add library to list
648 For example, if the two libraries libxy.so.1.1 and libxy.so.1.2
649 exist and both have the same soname, e.g. libxy.so, a symbolic link
650 is created from libxy.so.1.2 (the newer one) to libxy.so.
651 libxy.so.1.2 and libxy.so are added to the cache - but not
652 libxy.so.1.1. */
654 /* Information for one library. */
655 struct dlib_entry
657 char *name;
658 char *soname;
659 int flag;
660 int is_link;
661 unsigned int isa_level;
662 struct dlib_entry *next;
665 /* Skip some temporary DSO files. These files may be partially written
666 and lead to ldconfig crashes when examined. */
667 static bool
668 skip_dso_based_on_name (const char *name, size_t len)
670 /* Skip temporary files created by the prelink program. Files with
671 names like these are never really DSOs we want to look at. */
672 if (len >= sizeof (".#prelink#") - 1)
674 if (endswithn (name, len, ".#prelink#"))
675 return true;
676 if (len >= sizeof (".#prelink#.XXXXXX") - 1
677 && memcmp (name + len - sizeof (".#prelink#.XXXXXX")
678 + 1, ".#prelink#.", sizeof (".#prelink#.") - 1) == 0)
679 return true;
681 /* Skip temporary files created by RPM. */
682 if (memchr (name, ';', len) != NULL)
683 return true;
684 /* Skip temporary files created by dpkg. */
685 if (endswithn (name, len, ".dpkg-new")
686 || endswithn (name, len, ".dpkg-tmp"))
687 return true;
688 return false;
691 static void
692 search_dir (const struct dir_entry *entry)
694 if (opt_verbose)
696 if (entry->hwcaps == NULL)
697 printf ("%s:", entry->path);
698 else
699 printf ("%s: (hwcap: \"%s\")", entry->path,
700 glibc_hwcaps_subdirectory_name (entry->hwcaps));
701 printf (_(" (from %s:%d)\n"), entry->from_file, entry->from_line);
704 char *dir_name;
705 char *real_file_name;
706 char *file_name;
707 if (opt_chroot != NULL)
708 dir_name = chroot_canon (opt_chroot, entry->path);
709 else
710 dir_name = entry->path;
712 DIR *dir;
713 if (dir_name == NULL || (dir = opendir (dir_name)) == NULL)
715 if (opt_verbose)
716 error (0, errno, _("Can't open directory %s"), entry->path);
717 if (opt_chroot != NULL)
718 free (dir_name);
719 return;
722 struct dirent64 *direntry;
723 struct dlib_entry *dlibs = NULL;
724 while ((direntry = readdir64 (dir)) != NULL)
726 int flag;
727 /* We only look at links and regular files. */
728 if (direntry->d_type != DT_UNKNOWN
729 && direntry->d_type != DT_LNK
730 && direntry->d_type != DT_REG
731 && direntry->d_type != DT_DIR)
732 continue;
733 /* Does this file look like a shared library? The dynamic linker
734 is also considered as shared library. */
735 if (!_dl_is_dso (direntry->d_name)
736 && (direntry->d_type == DT_REG || entry->hwcaps == NULL))
737 continue;
739 size_t len = strlen (direntry->d_name);
740 if (skip_dso_based_on_name (direntry->d_name, len))
741 continue;
742 if (asprintf (&file_name, "%s/%s", entry->path, direntry->d_name) < 0)
743 error (EXIT_FAILURE, errno, _("Could not form library path"));
744 if (opt_chroot != NULL)
746 if (asprintf (&real_file_name, "%s/%s",
747 dir_name, direntry->d_name) < 0)
748 error (EXIT_FAILURE, errno, _("Could not form library path"));
750 else
751 real_file_name = xstrdup (file_name);
753 struct stat lstat_buf;
754 /* We optimize and try to do the lstat call only if needed. */
755 if (direntry->d_type != DT_UNKNOWN)
756 lstat_buf.st_mode = DTTOIF (direntry->d_type);
757 else
758 if (__glibc_unlikely (lstat (real_file_name, &lstat_buf)))
760 error (0, errno, _("Cannot lstat %s"), file_name);
761 goto next;
764 struct stat stat_buf;
765 int is_link = S_ISLNK (lstat_buf.st_mode);
766 if (is_link)
768 /* In case of symlink, we check if the symlink refers to
769 a directory. */
770 char *target_name = real_file_name;
771 if (opt_chroot != NULL)
773 target_name = chroot_canon (opt_chroot, file_name);
774 if (target_name == NULL)
776 if (strstr (file_name, ".so") == NULL)
777 error (0, 0, _("Input file %s not found.\n"), file_name);
778 goto next;
781 if (__glibc_unlikely (stat (target_name, &stat_buf)))
783 if (opt_verbose)
784 error (0, errno, _("Cannot stat %s"), file_name);
786 /* Remove stale symlinks. */
787 if (opt_link && strstr (direntry->d_name, ".so."))
788 unlink (real_file_name);
790 if (opt_chroot != NULL)
791 free (target_name);
793 goto next;
796 if (opt_chroot != NULL)
797 free (target_name);
799 /* lstat_buf is later stored, update contents. */
800 lstat_buf.st_dev = stat_buf.st_dev;
801 lstat_buf.st_ino = stat_buf.st_ino;
802 lstat_buf.st_size = stat_buf.st_size;
803 lstat_buf.st_ctime = stat_buf.st_ctime;
805 else if (!S_ISREG (lstat_buf.st_mode))
806 goto next;
808 char *real_name;
809 if (opt_chroot != NULL && is_link)
811 real_name = chroot_canon (opt_chroot, file_name);
812 if (real_name == NULL)
814 if (strstr (file_name, ".so") == NULL)
815 error (0, 0, _("Input file %s not found.\n"), file_name);
816 goto next;
819 else
820 real_name = real_file_name;
822 /* Call lstat if not done yet. */
823 if (!is_link
824 && direntry->d_type != DT_UNKNOWN
825 && __builtin_expect (lstat (real_file_name, &lstat_buf), 0))
827 error (0, errno, _("Cannot lstat %s"), file_name);
828 goto next;
831 /* First search whether the auxiliary cache contains this
832 library already and it's not changed. */
833 char *soname;
834 unsigned int isa_level;
835 if (!search_aux_cache (&lstat_buf, &flag, &isa_level, &soname))
837 if (process_file (real_name, file_name, direntry->d_name, &flag,
838 &isa_level, &soname, is_link, &lstat_buf))
840 if (real_name != real_file_name)
841 free (real_name);
842 goto next;
844 else if (opt_build_cache)
845 add_to_aux_cache (&lstat_buf, flag, isa_level, soname);
848 if (soname == NULL)
849 soname = xstrdup (direntry->d_name);
851 /* A link may just point to itself. */
852 if (is_link)
854 /* If the path the link points to isn't its soname or it is not
855 the .so symlink for ld(1), we treat it as a normal file.
857 You should always do this:
859 libfoo.so -> SONAME -> Arbitrary package-chosen name.
861 e.g. libfoo.so -> libfoo.so.1 -> libfooimp.so.9.99.
862 Given a SONAME of libfoo.so.1.
864 You should *never* do this:
866 libfoo.so -> libfooimp.so.9.99
868 If you do, and your SONAME is libfoo.so.1, then libfoo.so
869 fails to point at the SONAME. In that case ldconfig may consider
870 libfoo.so as another implementation of SONAME and will create
871 symlinks against it causing problems when you try to upgrade
872 or downgrade. The problems will arise because ldconfig will,
873 depending on directory ordering, creat symlinks against libfoo.so
874 e.g. libfoo.so.1.2 -> libfoo.so, but when libfoo.so is removed
875 (typically by the removal of a development package not required
876 for the runtime) it will break the libfoo.so.1.2 symlink and the
877 application will fail to start. */
878 const char *real_base_name = basename (real_file_name);
880 if (strcmp (real_base_name, soname) != 0)
882 len = strlen (real_base_name);
883 if (len < strlen (".so")
884 || strcmp (real_base_name + len - strlen (".so"), ".so") != 0
885 || strncmp (real_base_name, soname, len) != 0)
886 is_link = 0;
890 if (real_name != real_file_name)
891 free (real_name);
893 if (is_link)
895 free (soname);
896 soname = xstrdup (direntry->d_name);
899 /* Some sanity checks to print warnings. */
900 if (opt_verbose)
902 if (flag == FLAG_ELF_LIBC6 && entry->flag != FLAG_ELF_LIBC6)
903 error (0, 0, _("libc6 library %s in wrong directory"), file_name);
906 /* Add library to list. */
907 struct dlib_entry *dlib_ptr;
908 for (dlib_ptr = dlibs; dlib_ptr != NULL; dlib_ptr = dlib_ptr->next)
910 /* Is soname already in list? */
911 if (strcmp (dlib_ptr->soname, soname) == 0)
913 /* Prefer a file to a link, otherwise check which one
914 is newer. */
915 if ((!is_link && dlib_ptr->is_link)
916 || (is_link == dlib_ptr->is_link
917 && _dl_cache_libcmp (dlib_ptr->name, direntry->d_name) < 0))
919 /* It's newer - add it. */
920 /* Flag should be the same - sanity check. */
921 if (dlib_ptr->flag != flag)
922 error (0, 0, _("libraries %s and %s in directory %s have same soname but different type."),
923 dlib_ptr->name, direntry->d_name, entry->path);
924 free (dlib_ptr->name);
925 dlib_ptr->name = xstrdup (direntry->d_name);
926 dlib_ptr->is_link = is_link;
927 dlib_ptr->isa_level = isa_level;
929 /* Don't add this library, abort loop. */
930 /* Also free soname, since it's dynamically allocated. */
931 free (soname);
932 break;
935 /* Add the library if it's not already in. */
936 if (dlib_ptr == NULL)
938 dlib_ptr = (struct dlib_entry *)xmalloc (sizeof (struct dlib_entry));
939 dlib_ptr->name = xstrdup (direntry->d_name);
940 dlib_ptr->soname = soname;
941 dlib_ptr->flag = flag;
942 dlib_ptr->is_link = is_link;
943 dlib_ptr->isa_level = isa_level;
944 /* Add at head of list. */
945 dlib_ptr->next = dlibs;
946 dlibs = dlib_ptr;
949 next:
950 free (file_name);
951 free (real_file_name);
954 closedir (dir);
956 /* Now dlibs contains a list of all libs - add those to the cache
957 and created all symbolic links. */
958 struct dlib_entry *dlib_ptr;
959 for (dlib_ptr = dlibs; dlib_ptr != NULL; dlib_ptr = dlib_ptr->next)
961 /* The cached file name is the soname for non-glibc-hwcaps
962 subdirectories (relying on symbolic links; this helps with
963 library updates that change the file name), and the actual
964 file for glibc-hwcaps subdirectories. */
965 const char *filename;
966 if (entry->hwcaps == NULL)
968 /* Don't create links to links. */
969 if (dlib_ptr->is_link == 0)
970 create_links (dir_name, entry->path, dlib_ptr->name,
971 dlib_ptr->soname);
972 filename = dlib_ptr->soname;
974 else
976 /* Do not create links in glibc-hwcaps subdirectories, but
977 still log the cache addition. */
978 if (opt_verbose)
979 printf ("\t%s -> %s\n", dlib_ptr->soname, dlib_ptr->name);
980 filename = dlib_ptr->name;
982 if (opt_build_cache)
983 add_to_cache (entry->path, filename, dlib_ptr->soname,
984 dlib_ptr->flag, dlib_ptr->isa_level, entry->hwcaps);
987 /* Free all resources. */
988 while (dlibs)
990 dlib_ptr = dlibs;
991 free (dlib_ptr->soname);
992 free (dlib_ptr->name);
993 dlibs = dlibs->next;
994 free (dlib_ptr);
997 if (opt_chroot != NULL && dir_name != NULL)
998 free (dir_name);
1001 /* Search through all libraries. */
1002 static void
1003 search_dirs (void)
1005 struct dir_entry *entry;
1007 for (entry = dir_entries; entry != NULL; entry = entry->next)
1008 search_dir (entry);
1010 /* Free all allocated memory. */
1011 while (dir_entries)
1013 entry = dir_entries;
1014 dir_entries = dir_entries->next;
1015 free (entry->path);
1016 free (entry);
1021 static void parse_conf_include (const char *config_file, unsigned int lineno,
1022 bool do_chroot, const char *pattern);
1024 /* Parse configuration file. */
1025 static void
1026 parse_conf (const char *filename, bool do_chroot)
1028 FILE *file = NULL;
1029 char *line = NULL;
1030 const char *canon;
1031 size_t len = 0;
1032 unsigned int lineno;
1034 if (do_chroot && opt_chroot)
1036 canon = chroot_canon (opt_chroot, filename);
1037 if (canon)
1038 file = fopen (canon, "r");
1039 else
1040 canon = filename;
1042 else
1044 canon = filename;
1045 file = fopen (filename, "r");
1048 if (file == NULL)
1050 if (errno != ENOENT)
1051 error (0, errno, _("\
1052 Warning: ignoring configuration file that cannot be opened: %s"),
1053 canon);
1054 if (canon != filename)
1055 free ((char *) canon);
1056 return;
1059 /* No threads use this stream. */
1060 __fsetlocking (file, FSETLOCKING_BYCALLER);
1062 if (canon != filename)
1063 free ((char *) canon);
1065 lineno = 0;
1068 ssize_t n = getline (&line, &len, file);
1069 if (n < 0)
1070 break;
1072 ++lineno;
1073 if (line[n - 1] == '\n')
1074 line[n - 1] = '\0';
1076 /* Because the file format does not know any form of quoting we
1077 can search forward for the next '#' character and if found
1078 make it terminating the line. */
1079 *strchrnul (line, '#') = '\0';
1081 /* Remove leading whitespace. NUL is no whitespace character. */
1082 char *cp = line;
1083 while (isspace (*cp))
1084 ++cp;
1086 /* If the line is blank it is ignored. */
1087 if (cp[0] == '\0')
1088 continue;
1090 if (!strncmp (cp, "include", 7) && isblank (cp[7]))
1092 char *dir;
1093 cp += 8;
1094 while ((dir = strsep (&cp, " \t")) != NULL)
1095 if (dir[0] != '\0')
1096 parse_conf_include (filename, lineno, do_chroot, dir);
1098 else if (!strncasecmp (cp, "hwcap", 5) && isblank (cp[5]))
1099 error (0, 0, _("%s:%u: hwcap directive ignored"), filename, lineno);
1100 else
1101 add_dir_1 (cp, filename, lineno);
1103 while (!feof_unlocked (file));
1105 /* Free buffer and close file. */
1106 free (line);
1107 fclose (file);
1110 /* Handle one word in an `include' line, a glob pattern of additional
1111 config files to read. */
1112 static void
1113 parse_conf_include (const char *config_file, unsigned int lineno,
1114 bool do_chroot, const char *pattern)
1116 if (opt_chroot != NULL && pattern[0] != '/')
1117 error (EXIT_FAILURE, 0,
1118 _("need absolute file name for configuration file when using -r"));
1120 char *copy = NULL;
1121 if (pattern[0] != '/' && strchr (config_file, '/') != NULL)
1123 if (asprintf (&copy, "%s/%s", dirname (strdupa (config_file)),
1124 pattern) < 0)
1125 error (EXIT_FAILURE, 0, _("memory exhausted"));
1126 pattern = copy;
1129 glob64_t gl;
1130 int result;
1131 if (do_chroot && opt_chroot)
1133 char *canon = chroot_canon (opt_chroot, pattern);
1134 if (canon == NULL)
1135 return;
1136 result = glob64 (canon, 0, NULL, &gl);
1137 free (canon);
1139 else
1140 result = glob64 (pattern, 0, NULL, &gl);
1142 switch (result)
1144 case 0:
1145 for (size_t i = 0; i < gl.gl_pathc; ++i)
1146 parse_conf (gl.gl_pathv[i], false);
1147 globfree64 (&gl);
1148 break;
1150 case GLOB_NOMATCH:
1151 break;
1153 case GLOB_NOSPACE:
1154 errno = ENOMEM;
1155 /* Fall through. */
1156 case GLOB_ABORTED:
1157 if (opt_verbose)
1158 error (0, errno, _("%s:%u: cannot read directory %s"),
1159 config_file, lineno, pattern);
1160 break;
1162 default:
1163 abort ();
1164 break;
1167 free (copy);
1172 main (int argc, char **argv)
1174 /* Set locale via LC_ALL. */
1175 setlocale (LC_ALL, "");
1177 /* But keep the C collation. That way `include' directives using
1178 globbing patterns are processed in a locale-independent order. */
1179 setlocale (LC_COLLATE, "C");
1181 /* Set the text message domain. */
1182 textdomain (_libc_intl_domainname);
1184 /* Parse and process arguments. */
1185 int remaining;
1186 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
1188 /* Remaining arguments are additional directories if opt_manual_link
1189 is not set. */
1190 if (remaining != argc && !opt_manual_link)
1192 int i;
1193 for (i = remaining; i < argc; ++i)
1194 if (opt_build_cache && argv[i][0] != '/')
1195 error (EXIT_FAILURE, 0,
1196 _("relative path `%s' used to build cache"),
1197 argv[i]);
1198 else
1199 add_dir_1 (argv[i], "<cmdline>", 0);
1202 if (opt_chroot != NULL)
1204 /* Normalize the path a bit, we might need it for printing later. */
1205 char *endp = strchr (opt_chroot, '\0');
1206 while (endp > opt_chroot && endp[-1] == '/')
1207 --endp;
1208 *endp = '\0';
1209 if (endp == opt_chroot)
1210 opt_chroot = NULL;
1212 if (opt_chroot != NULL)
1214 /* It is faster to use chroot if we can. */
1215 if (!chroot (opt_chroot))
1217 if (chdir ("/"))
1218 error (EXIT_FAILURE, errno, _("Can't chdir to /"));
1219 opt_chroot = NULL;
1224 if (cache_file == NULL)
1226 cache_file = alloca (strlen (LD_SO_CACHE) + 1);
1227 strcpy (cache_file, LD_SO_CACHE);
1230 if (config_file == NULL)
1231 config_file = LD_SO_CONF;
1233 if (opt_print_cache)
1235 if (opt_chroot != NULL)
1237 char *p = chroot_canon (opt_chroot, cache_file);
1238 if (p == NULL)
1239 error (EXIT_FAILURE, errno, _("Can't open cache file %s\n"),
1240 cache_file);
1241 cache_file = p;
1243 print_cache (cache_file);
1244 if (opt_chroot != NULL)
1245 free (cache_file);
1246 exit (0);
1249 if (opt_chroot != NULL)
1251 /* Canonicalize the directory name of cache_file, not cache_file,
1252 because we'll rename a temporary cache file to it. */
1253 char *p = strrchr (cache_file, '/');
1254 char *canon = chroot_canon (opt_chroot,
1255 p ? (*p = '\0', cache_file) : "/");
1257 if (canon == NULL)
1258 error (EXIT_FAILURE, errno,
1259 _("Can't open cache file directory %s\n"),
1260 p ? cache_file : "/");
1262 if (p)
1263 ++p;
1264 else
1265 p = cache_file;
1267 cache_file = alloca (strlen (canon) + strlen (p) + 2);
1268 sprintf (cache_file, "%s/%s", canon, p);
1269 free (canon);
1272 if (opt_manual_link)
1274 /* Link all given libraries manually. */
1275 int i;
1277 for (i = remaining; i < argc; ++i)
1278 manual_link (argv[i]);
1280 exit (0);
1284 if (opt_build_cache)
1285 init_cache ();
1287 if (!opt_only_cline)
1289 parse_conf (config_file, true);
1291 /* Always add the standard search paths. */
1292 add_system_dir (SLIBDIR);
1293 if (strcmp (SLIBDIR, LIBDIR))
1294 add_system_dir (LIBDIR);
1297 const char *aux_cache_file = _PATH_LDCONFIG_AUX_CACHE;
1298 if (opt_chroot != NULL)
1299 aux_cache_file = chroot_canon (opt_chroot, aux_cache_file);
1301 if (! opt_ignore_aux_cache && aux_cache_file)
1302 load_aux_cache (aux_cache_file);
1303 else
1304 init_aux_cache ();
1306 search_dirs ();
1308 if (opt_build_cache)
1310 save_cache (cache_file);
1311 if (aux_cache_file)
1312 save_aux_cache (aux_cache_file);
1315 return 0;