1 /* Copyright (C) 1999-2022 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
30 #include <stdio_ext.h>
35 #include <sys/fcntl.h>
38 #include <sys/types.h>
44 #include <dl-hwcaps.h>
45 #include <dl-is_dso.h>
47 #include <dl-procinfo.h>
49 /* This subpath in search path entries is always supported and
50 included in the cache for backwards compatibility. */
51 #define TLS_SUBPATH "tls"
53 /* The MSB of the hwcap field is set for objects in TLS_SUBPATH
54 directories. There is always TLS support in glibc, so the dynamic
55 loader does not check the bit directly. But more hwcap bits make a
56 an object more preferred, so the bit still has meaning. */
57 #define TLS_HWCAP_BIT 63
60 # define LD_SO_CONF SYSCONFDIR "/ld.so.conf"
63 /* Get libc version number. */
66 #define PACKAGE _libc_intl_domainname
68 /* List of directories to handle. */
75 const char *from_file
;
78 /* Non-NULL for subdirectories under a glibc-hwcaps subdirectory. */
79 struct glibc_hwcaps_subdirectory
*hwcaps
;
81 struct dir_entry
*next
;
84 /* The list is unsorted, contains no duplicates. Entries are added at
86 static struct dir_entry
*dir_entries
;
88 /* Flags for different options. */
90 static int opt_print_cache
;
95 /* Format to support. */
96 enum opt_format opt_format
= opt_format_new
;
99 static int opt_build_cache
= 1;
101 /* Enable symbolic link processing. If set, create or update symbolic
102 links, and remove stale symbolic links. */
103 static int opt_link
= 1;
105 /* Only process directories specified on the command line. */
106 static int opt_only_cline
;
108 /* Path to root for chroot. */
109 static char *opt_chroot
;
111 /* Manually link given shared libraries. */
112 static int opt_manual_link
;
114 /* Should we ignore an old auxiliary cache file? */
115 static int opt_ignore_aux_cache
;
117 /* Cache file to use. */
118 static char *cache_file
;
120 /* Configuration file. */
121 static const char *config_file
;
123 /* Mask to use for important hardware capabilities. */
124 static unsigned long int hwcap_mask
= HWCAP_IMPORTANT
;
126 /* Name and version of program. */
127 static void print_version (FILE *stream
, struct argp_state
*state
);
128 void (*argp_program_version_hook
) (FILE *, struct argp_state
*)
131 /* Function to print some extra text in the help message. */
132 static char *more_help (int key
, const char *text
, void *input
);
134 /* Definitions of arguments for argp functions. */
135 static const struct argp_option options
[] =
137 { "print-cache", 'p', NULL
, 0, N_("Print cache"), 0},
138 { "verbose", 'v', NULL
, 0, N_("Generate verbose messages"), 0},
139 { NULL
, 'N', NULL
, 0, N_("Don't build cache"), 0},
140 { NULL
, 'X', NULL
, 0, N_("Don't update symbolic links"), 0},
141 { NULL
, 'r', N_("ROOT"), 0, N_("Change to and use ROOT as root directory"), 0},
142 { NULL
, 'C', N_("CACHE"), 0, N_("Use CACHE as cache file"), 0},
143 { NULL
, 'f', N_("CONF"), 0, N_("Use CONF as configuration file"), 0},
144 { NULL
, 'n', NULL
, 0, N_("Only process directories specified on the command line. Don't build cache."), 0},
145 { NULL
, 'l', NULL
, 0, N_("Manually link individual libraries."), 0},
146 { "format", 'c', N_("FORMAT"), 0, N_("Format to use: new (default), old, or compat"), 0},
147 { "ignore-aux-cache", 'i', NULL
, 0, N_("Ignore auxiliary cache file"), 0},
148 { NULL
, 0, NULL
, 0, NULL
, 0 }
151 #define PROCINFO_CLASS static
152 #include <dl-procinfo.c>
154 /* Short description of program. */
155 static const char doc
[] = N_("Configure Dynamic Linker Run Time Bindings.");
157 /* Prototype for option handler. */
158 static error_t
parse_opt (int key
, char *arg
, struct argp_state
*state
);
160 /* Data structure to communicate with argp functions. */
161 static struct argp argp
=
163 options
, parse_opt
, NULL
, doc
, NULL
, more_help
, NULL
166 /* Check if string corresponds to an important hardware capability or
169 is_hwcap_platform (const char *name
)
171 int hwcap_idx
= _dl_string_hwcap (name
);
173 /* Is this a normal hwcap for the machine like "fpu?" */
174 if (hwcap_idx
!= -1 && ((1 << hwcap_idx
) & hwcap_mask
))
177 /* Is this a platform pseudo-hwcap like "i686?" */
178 hwcap_idx
= _dl_string_platform (name
);
182 /* Backwards-compatibility for the "tls" subdirectory. */
183 if (strcmp (name
, TLS_SUBPATH
) == 0)
189 /* Get hwcap (including platform) encoding of path. */
191 path_hwcap (const char *path
)
193 char *str
= xstrdup (path
);
204 /* Search pathname from the end and check for hwcap strings. */
207 ptr
= strrchr (str
, '/');
212 h
= _dl_string_hwcap (ptr
+ 1);
214 if (h
== (uint64_t) -1)
216 h
= _dl_string_platform (ptr
+ 1);
217 if (h
== (uint64_t) -1)
219 if (strcmp (ptr
+ 1, TLS_SUBPATH
) == 0)
227 /* Search the next part of the path. */
235 /* Handle program arguments. */
237 parse_opt (int key
, char *arg
, struct argp_state
*state
)
243 /* Ignore auxiliary cache since we use non-standard cache. */
244 opt_ignore_aux_cache
= 1;
250 opt_ignore_aux_cache
= 1;
275 if (strcmp (arg
, "old") == 0)
276 opt_format
= opt_format_old
;
277 else if (strcmp (arg
, "compat") == 0)
278 opt_format
= opt_format_compat
;
279 else if (strcmp (arg
, "new") == 0)
280 opt_format
= opt_format_new
;
283 return ARGP_ERR_UNKNOWN
;
289 /* Print bug-reporting information in the help message. */
291 more_help (int key
, const char *text
, void *input
)
296 case ARGP_KEY_HELP_EXTRA
:
297 /* We print some extra information. */
298 if (asprintf (&tp
, gettext ("\
299 For bug reporting instructions, please see:\n\
300 %s.\n"), REPORT_BUGS_TO
) < 0)
306 return (char *) text
;
309 /* Print the version information. */
311 print_version (FILE *stream
, struct argp_state
*state
)
313 fprintf (stream
, "ldconfig %s%s\n", PKGVERSION
, VERSION
);
314 fprintf (stream
, gettext ("\
315 Copyright (C) %s Free Software Foundation, Inc.\n\
316 This is free software; see the source for copying conditions. There is NO\n\
317 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
319 fprintf (stream
, gettext ("Written by %s.\n"),
323 /* Allocate a new subdirectory with full path PATH under ENTRY, using
324 inode data from *ST. */
325 static struct dir_entry
*
326 new_sub_entry (const struct dir_entry
*entry
, const char *path
,
327 const struct stat
*st
)
329 struct dir_entry
*new_entry
= xmalloc (sizeof (struct dir_entry
));
330 new_entry
->from_file
= entry
->from_file
;
331 new_entry
->from_line
= entry
->from_line
;
332 new_entry
->path
= xstrdup (path
);
333 new_entry
->flag
= entry
->flag
;
334 new_entry
->hwcaps
= NULL
;
335 new_entry
->next
= NULL
;
336 new_entry
->ino
= st
->st_ino
;
337 new_entry
->dev
= st
->st_dev
;
341 /* Add a single directory entry. Return true if the directory is
342 actually added (because it is not a duplicate). */
344 add_single_dir (struct dir_entry
*entry
, int verbose
)
346 struct dir_entry
*ptr
, *prev
;
353 /* Check for duplicates. */
354 if (ptr
->ino
== entry
->ino
&& ptr
->dev
== entry
->dev
)
356 if (opt_verbose
&& verbose
)
358 error (0, 0, _("Path `%s' given more than once"), entry
->path
);
359 fprintf (stderr
, _("(from %s:%d and %s:%d)\n"),
360 entry
->from_file
, entry
->from_line
,
361 ptr
->from_file
, ptr
->from_line
);
363 /* Use the newer information. */
364 ptr
->flag
= entry
->flag
;
373 /* Is this the first entry? */
374 if (ptr
== NULL
&& dir_entries
== NULL
)
376 else if (ptr
== NULL
)
381 /* Check if PATH contains a "glibc-hwcaps" subdirectory. If so, queue
382 its subdirectories for glibc-hwcaps processing. */
384 add_glibc_hwcaps_subdirectories (struct dir_entry
*entry
, const char *path
)
386 /* glibc-hwcaps subdirectories do not nest. */
387 assert (entry
->hwcaps
== NULL
);
390 if (asprintf (&glibc_hwcaps
, "%s/" GLIBC_HWCAPS_SUBDIRECTORY
, path
) < 0)
391 error (EXIT_FAILURE
, errno
, _("Could not form glibc-hwcaps path"));
393 DIR *dir
= opendir (glibc_hwcaps
);
399 struct dirent64
*e
= readdir64 (dir
);
405 error (EXIT_FAILURE
, errno
, _("Listing directory %s"), path
);
408 /* Ignore hidden subdirectories, including "." and "..", and
409 regular files. File names containing a ':' cannot be
410 looked up by the dynamic loader, so skip those as
412 if (e
->d_name
[0] == '.' || e
->d_type
== DT_REG
413 || strchr (e
->d_name
, ':') != NULL
)
416 /* See if this entry eventually resolves to a directory. */
418 if (fstatat (dirfd (dir
), e
->d_name
, &st
, 0) < 0)
419 /* Ignore unreadable entries. */
422 if (S_ISDIR (st
.st_mode
))
424 /* This is a directory, so it needs to be scanned for
425 libraries, associated with the hwcaps implied by the
426 subdirectory name. */
428 if (asprintf (&new_path
, "%s/" GLIBC_HWCAPS_SUBDIRECTORY
"/%s",
429 /* Use non-canonicalized path here. */
430 entry
->path
, e
->d_name
) < 0)
431 error (EXIT_FAILURE
, errno
,
432 _("Could not form glibc-hwcaps path"));
433 struct dir_entry
*new_entry
= new_sub_entry (entry
, new_path
,
436 new_entry
->hwcaps
= new_glibc_hwcaps_subdirectory (e
->d_name
);
437 add_single_dir (new_entry
, 0);
447 /* Add one directory to the list of directories to process. */
449 add_dir_1 (const char *line
, const char *from_file
, int from_line
)
452 struct dir_entry
*entry
= xmalloc (sizeof (struct dir_entry
));
453 entry
->hwcaps
= NULL
;
456 entry
->from_file
= strdup (from_file
);
457 entry
->from_line
= from_line
;
459 entry
->path
= xstrdup (line
);
460 entry
->flag
= FLAG_ELF_LIBC6
;
462 /* Canonify path: for now only remove leading and trailing
463 whitespace and the trailing slashes. */
464 i
= strlen (entry
->path
);
466 while (i
> 0 && isspace (entry
->path
[i
- 1]))
467 entry
->path
[--i
] = '\0';
469 while (i
> 0 && entry
->path
[i
- 1] == '/')
470 entry
->path
[--i
] = '\0';
479 char *path
= entry
->path
;
480 if (opt_chroot
!= NULL
)
481 path
= chroot_canon (opt_chroot
, path
);
483 struct stat stat_buf
;
484 if (path
== NULL
|| stat (path
, &stat_buf
))
487 error (0, errno
, _("Can't stat %s"), entry
->path
);
493 entry
->ino
= stat_buf
.st_ino
;
494 entry
->dev
= stat_buf
.st_dev
;
496 if (add_single_dir (entry
, 1))
497 /* Add glibc-hwcaps subdirectories if present. */
498 add_glibc_hwcaps_subdirectories (entry
, path
);
501 if (opt_chroot
!= NULL
)
506 add_dir (const char *line
)
508 add_dir_1 (line
, "<builtin>", 0);
512 chroot_stat (const char *real_path
, const char *path
, struct stat
*st
)
518 return stat (real_path
, st
);
520 ret
= lstat (real_path
, st
);
521 if (ret
|| !S_ISLNK (st
->st_mode
))
524 canon_path
= chroot_canon (opt_chroot
, path
);
525 if (canon_path
== NULL
)
528 ret
= stat (canon_path
, st
);
533 /* Create a symbolic link from soname to libname in directory path. */
535 create_links (const char *real_path
, const char *path
, const char *libname
,
538 char *full_libname
, *full_soname
;
539 char *real_full_libname
, *real_full_soname
;
540 struct stat stat_lib
, stat_so
, lstat_so
;
543 /* XXX: The logics in this function should be simplified. */
545 /* Get complete path. */
546 full_libname
= alloca (strlen (path
) + strlen (libname
) + 2);
547 full_soname
= alloca (strlen (path
) + strlen (soname
) + 2);
548 sprintf (full_libname
, "%s/%s", path
, libname
);
549 sprintf (full_soname
, "%s/%s", path
, soname
);
550 if (opt_chroot
!= NULL
)
552 real_full_libname
= alloca (strlen (real_path
) + strlen (libname
) + 2);
553 real_full_soname
= alloca (strlen (real_path
) + strlen (soname
) + 2);
554 sprintf (real_full_libname
, "%s/%s", real_path
, libname
);
555 sprintf (real_full_soname
, "%s/%s", real_path
, soname
);
559 real_full_libname
= full_libname
;
560 real_full_soname
= full_soname
;
563 /* Does soname already exist and point to the right library? */
564 if (chroot_stat (real_full_soname
, full_soname
, &stat_so
) == 0)
566 if (chroot_stat (real_full_libname
, full_libname
, &stat_lib
))
568 error (0, 0, _("Can't stat %s\n"), full_libname
);
571 if (stat_lib
.st_dev
== stat_so
.st_dev
572 && stat_lib
.st_ino
== stat_so
.st_ino
)
573 /* Link is already correct. */
575 else if (lstat (full_soname
, &lstat_so
) == 0
576 && !S_ISLNK (lstat_so
.st_mode
))
578 error (0, 0, _("%s is not a symbolic link\n"), full_soname
);
583 else if (lstat (real_full_soname
, &lstat_so
) != 0
584 || !S_ISLNK (lstat_so
.st_mode
))
585 /* Unless it is a stale symlink, there is no need to remove. */
589 printf ("\t%s -> %s", soname
, libname
);
591 if (do_link
&& opt_link
)
593 /* Remove old link. */
595 if (unlink (real_full_soname
))
597 error (0, 0, _("Can't unlink %s"), full_soname
);
600 /* Create symbolic link. */
601 if (do_link
&& symlink (libname
, real_full_soname
))
603 error (0, 0, _("Can't link %s to %s"), full_soname
, libname
);
609 fputs (_(" (changed)\n"), stdout
);
611 fputs (_(" (SKIPPED)\n"), stdout
);
614 else if (opt_verbose
)
615 fputs ("\n", stdout
);
618 /* Manually link the given library. */
620 manual_link (char *library
)
627 struct stat stat_buf
;
629 unsigned int isa_level
;
631 /* Prepare arguments for create_links call. Split library name in
632 directory and filename first. Since path is allocated, we've got
633 to be careful to free at the end. */
634 path
= xstrdup (library
);
635 libname
= strrchr (path
, '/');
639 /* Successfully split names. Check if path is just "/" to avoid
643 libname
= library
+ 1;
644 path
= xrealloc (path
, 2);
655 /* There's no path, construct one. */
657 path
= xrealloc (path
, 2);
661 if (opt_chroot
!= NULL
)
663 real_path
= chroot_canon (opt_chroot
, path
);
664 if (real_path
== NULL
)
666 error (0, errno
, _("Can't find %s"), path
);
670 real_library
= alloca (strlen (real_path
) + strlen (libname
) + 2);
671 sprintf (real_library
, "%s/%s", real_path
, libname
);
676 real_library
= library
;
679 /* Do some sanity checks first. */
680 if (lstat (real_library
, &stat_buf
))
682 error (0, errno
, _("Cannot lstat %s"), library
);
685 /* We don't want links here! */
686 else if (!S_ISREG (stat_buf
.st_mode
))
688 error (0, 0, _("Ignored file %s since it is not a regular file."),
693 if (process_file (real_library
, library
, libname
, &flag
, &isa_level
, &soname
,
696 error (0, 0, _("No link created since soname could not be found for %s"),
701 soname
= implicit_soname (libname
, flag
);
702 create_links (real_path
, path
, libname
, soname
);
705 if (path
!= real_path
)
711 /* Read a whole directory and search for libraries.
712 The purpose is two-fold:
713 - search for libraries which will be added to the cache
714 - create symbolic links to the soname for each library
716 This has to be done separatly for each directory.
718 To keep track of which libraries to add to the cache and which
719 links to create, we save a list of all libraries.
721 The algorithm is basically:
722 for all libraries in the directory do
723 get soname of library
724 if soname is already in list
725 if new library is newer, replace entry
726 otherwise ignore this library
727 otherwise add library to list
729 For example, if the two libraries libxy.so.1.1 and libxy.so.1.2
730 exist and both have the same soname, e.g. libxy.so, a symbolic link
731 is created from libxy.so.1.2 (the newer one) to libxy.so.
732 libxy.so.1.2 and libxy.so are added to the cache - but not
735 /* Information for one library. */
742 unsigned int isa_level
;
743 struct dlib_entry
*next
;
748 search_dir (const struct dir_entry
*entry
)
751 if (entry
->hwcaps
== NULL
)
753 hwcap
= path_hwcap (entry
->path
);
757 printf ("%s: (hwcap: %#.16" PRIx64
")", entry
->path
, hwcap
);
759 printf ("%s:", entry
->path
);
766 printf ("%s: (hwcap: \"%s\")", entry
->path
,
767 glibc_hwcaps_subdirectory_name (entry
->hwcaps
));
770 printf (_(" (from %s:%d)\n"), entry
->from_file
, entry
->from_line
);
773 char *real_file_name
;
774 size_t real_file_name_len
;
775 size_t file_name_len
= PATH_MAX
;
776 char *file_name
= alloca (file_name_len
);
777 if (opt_chroot
!= NULL
)
779 dir_name
= chroot_canon (opt_chroot
, entry
->path
);
780 real_file_name_len
= PATH_MAX
;
781 real_file_name
= alloca (real_file_name_len
);
785 dir_name
= entry
->path
;
786 real_file_name_len
= 0;
787 real_file_name
= file_name
;
791 if (dir_name
== NULL
|| (dir
= opendir (dir_name
)) == NULL
)
794 error (0, errno
, _("Can't open directory %s"), entry
->path
);
795 if (opt_chroot
!= NULL
&& dir_name
!= NULL
)
800 struct dirent64
*direntry
;
801 struct dlib_entry
*dlibs
= NULL
;
802 while ((direntry
= readdir64 (dir
)) != NULL
)
805 /* We only look at links and regular files. */
806 if (direntry
->d_type
!= DT_UNKNOWN
807 && direntry
->d_type
!= DT_LNK
808 && direntry
->d_type
!= DT_REG
809 && direntry
->d_type
!= DT_DIR
)
811 /* Does this file look like a shared library or is it a hwcap
812 subdirectory (if not already processing a glibc-hwcaps
813 subdirectory)? The dynamic linker is also considered as
815 if (!_dl_is_dso (direntry
->d_name
)
816 && (direntry
->d_type
== DT_REG
817 || (entry
->hwcaps
== NULL
818 && !is_hwcap_platform (direntry
->d_name
))))
821 size_t len
= strlen (direntry
->d_name
);
822 /* Skip temporary files created by the prelink program. Files with
823 names like these are never really DSOs we want to look at. */
824 if (len
>= sizeof (".#prelink#") - 1)
826 if (strcmp (direntry
->d_name
+ len
- sizeof (".#prelink#") + 1,
829 if (len
>= sizeof (".#prelink#.XXXXXX") - 1
830 && memcmp (direntry
->d_name
+ len
- sizeof (".#prelink#.XXXXXX")
831 + 1, ".#prelink#.", sizeof (".#prelink#.") - 1) == 0)
834 len
+= strlen (entry
->path
) + 2;
835 if (len
> file_name_len
)
838 file_name
= alloca (file_name_len
);
840 real_file_name
= file_name
;
842 sprintf (file_name
, "%s/%s", entry
->path
, direntry
->d_name
);
843 if (opt_chroot
!= NULL
)
845 len
= strlen (dir_name
) + strlen (direntry
->d_name
) + 2;
846 if (len
> real_file_name_len
)
848 real_file_name_len
= len
;
849 real_file_name
= alloca (real_file_name_len
);
851 sprintf (real_file_name
, "%s/%s", dir_name
, direntry
->d_name
);
854 struct stat lstat_buf
;
855 /* We optimize and try to do the lstat call only if needed. */
856 if (direntry
->d_type
!= DT_UNKNOWN
)
857 lstat_buf
.st_mode
= DTTOIF (direntry
->d_type
);
859 if (__glibc_unlikely (lstat (real_file_name
, &lstat_buf
)))
861 error (0, errno
, _("Cannot lstat %s"), file_name
);
865 struct stat stat_buf
;
867 int is_link
= S_ISLNK (lstat_buf
.st_mode
);
870 /* In case of symlink, we check if the symlink refers to
872 char *target_name
= real_file_name
;
873 if (opt_chroot
!= NULL
)
875 target_name
= chroot_canon (opt_chroot
, file_name
);
876 if (target_name
== NULL
)
878 if (strstr (file_name
, ".so") == NULL
)
879 error (0, 0, _("Input file %s not found.\n"), file_name
);
883 if (__glibc_unlikely (stat (target_name
, &stat_buf
)))
886 error (0, errno
, _("Cannot stat %s"), file_name
);
888 /* Remove stale symlinks. */
889 if (opt_link
&& strstr (direntry
->d_name
, ".so."))
890 unlink (real_file_name
);
892 if (opt_chroot
!= NULL
)
898 if (opt_chroot
!= NULL
)
901 is_dir
= S_ISDIR (stat_buf
.st_mode
);
903 /* lstat_buf is later stored, update contents. */
904 lstat_buf
.st_dev
= stat_buf
.st_dev
;
905 lstat_buf
.st_ino
= stat_buf
.st_ino
;
906 lstat_buf
.st_size
= stat_buf
.st_size
;
907 lstat_buf
.st_ctime
= stat_buf
.st_ctime
;
910 is_dir
= S_ISDIR (lstat_buf
.st_mode
);
912 /* No descending into subdirectories if this directory is a
913 glibc-hwcaps subdirectory (which are not recursive). */
914 if (entry
->hwcaps
== NULL
915 && is_dir
&& is_hwcap_platform (direntry
->d_name
))
918 && direntry
->d_type
!= DT_UNKNOWN
919 && __builtin_expect (lstat (real_file_name
, &lstat_buf
), 0))
921 error (0, errno
, _("Cannot lstat %s"), file_name
);
925 /* Handle subdirectory later. */
926 struct dir_entry
*new_entry
= new_sub_entry (entry
, file_name
,
928 add_single_dir (new_entry
, 0);
931 else if (!S_ISREG (lstat_buf
.st_mode
) && !is_link
)
935 if (opt_chroot
!= NULL
&& is_link
)
937 real_name
= chroot_canon (opt_chroot
, file_name
);
938 if (real_name
== NULL
)
940 if (strstr (file_name
, ".so") == NULL
)
941 error (0, 0, _("Input file %s not found.\n"), file_name
);
946 real_name
= real_file_name
;
948 /* Call lstat if not done yet. */
950 && direntry
->d_type
!= DT_UNKNOWN
951 && __builtin_expect (lstat (real_file_name
, &lstat_buf
), 0))
953 error (0, errno
, _("Cannot lstat %s"), file_name
);
957 /* First search whether the auxiliary cache contains this
958 library already and it's not changed. */
960 unsigned int isa_level
;
961 if (!search_aux_cache (&lstat_buf
, &flag
, &isa_level
, &soname
))
963 if (process_file (real_name
, file_name
, direntry
->d_name
, &flag
,
964 &isa_level
, &soname
, is_link
, &lstat_buf
))
966 if (real_name
!= real_file_name
)
970 else if (opt_build_cache
)
971 add_to_aux_cache (&lstat_buf
, flag
, isa_level
, soname
);
975 soname
= implicit_soname (direntry
->d_name
, flag
);
977 /* A link may just point to itself. */
980 /* If the path the link points to isn't its soname or it is not
981 the .so symlink for ld(1), we treat it as a normal file.
983 You should always do this:
985 libfoo.so -> SONAME -> Arbitrary package-chosen name.
987 e.g. libfoo.so -> libfoo.so.1 -> libfooimp.so.9.99.
988 Given a SONAME of libfoo.so.1.
990 You should *never* do this:
992 libfoo.so -> libfooimp.so.9.99
994 If you do, and your SONAME is libfoo.so.1, then libfoo.so
995 fails to point at the SONAME. In that case ldconfig may consider
996 libfoo.so as another implementation of SONAME and will create
997 symlinks against it causing problems when you try to upgrade
998 or downgrade. The problems will arise because ldconfig will,
999 depending on directory ordering, creat symlinks against libfoo.so
1000 e.g. libfoo.so.1.2 -> libfoo.so, but when libfoo.so is removed
1001 (typically by the removal of a development pacakge not required
1002 for the runtime) it will break the libfoo.so.1.2 symlink and the
1003 application will fail to start. */
1004 const char *real_base_name
= basename (real_file_name
);
1006 if (strcmp (real_base_name
, soname
) != 0)
1008 len
= strlen (real_base_name
);
1009 if (len
< strlen (".so")
1010 || strcmp (real_base_name
+ len
- strlen (".so"), ".so") != 0
1011 || strncmp (real_base_name
, soname
, len
) != 0)
1016 if (real_name
!= real_file_name
)
1022 soname
= xstrdup (direntry
->d_name
);
1025 /* Some sanity checks to print warnings. */
1028 if (flag
== FLAG_ELF_LIBC6
&& entry
->flag
!= FLAG_ELF_LIBC6
)
1029 error (0, 0, _("libc6 library %s in wrong directory"), file_name
);
1032 /* Add library to list. */
1033 struct dlib_entry
*dlib_ptr
;
1034 for (dlib_ptr
= dlibs
; dlib_ptr
!= NULL
; dlib_ptr
= dlib_ptr
->next
)
1036 /* Is soname already in list? */
1037 if (strcmp (dlib_ptr
->soname
, soname
) == 0)
1039 /* Prefer a file to a link, otherwise check which one
1041 if ((!is_link
&& dlib_ptr
->is_link
)
1042 || (is_link
== dlib_ptr
->is_link
1043 && _dl_cache_libcmp (dlib_ptr
->name
, direntry
->d_name
) < 0))
1045 /* It's newer - add it. */
1046 /* Flag should be the same - sanity check. */
1047 if (dlib_ptr
->flag
!= flag
)
1048 error (0, 0, _("libraries %s and %s in directory %s have same soname but different type."),
1049 dlib_ptr
->name
, direntry
->d_name
, entry
->path
);
1050 free (dlib_ptr
->name
);
1051 dlib_ptr
->name
= xstrdup (direntry
->d_name
);
1052 dlib_ptr
->is_link
= is_link
;
1053 dlib_ptr
->isa_level
= isa_level
;
1055 /* Don't add this library, abort loop. */
1056 /* Also free soname, since it's dynamically allocated. */
1061 /* Add the library if it's not already in. */
1062 if (dlib_ptr
== NULL
)
1064 dlib_ptr
= (struct dlib_entry
*)xmalloc (sizeof (struct dlib_entry
));
1065 dlib_ptr
->name
= xstrdup (direntry
->d_name
);
1066 dlib_ptr
->soname
= soname
;
1067 dlib_ptr
->flag
= flag
;
1068 dlib_ptr
->is_link
= is_link
;
1069 dlib_ptr
->isa_level
= isa_level
;
1070 /* Add at head of list. */
1071 dlib_ptr
->next
= dlibs
;
1078 /* Now dlibs contains a list of all libs - add those to the cache
1079 and created all symbolic links. */
1080 struct dlib_entry
*dlib_ptr
;
1081 for (dlib_ptr
= dlibs
; dlib_ptr
!= NULL
; dlib_ptr
= dlib_ptr
->next
)
1083 /* The cached file name is the soname for non-glibc-hwcaps
1084 subdirectories (relying on symbolic links; this helps with
1085 library updates that change the file name), and the actual
1086 file for glibc-hwcaps subdirectories. */
1087 const char *filename
;
1088 if (entry
->hwcaps
== NULL
)
1090 /* Don't create links to links. */
1091 if (dlib_ptr
->is_link
== 0)
1092 create_links (dir_name
, entry
->path
, dlib_ptr
->name
,
1094 filename
= dlib_ptr
->soname
;
1098 /* Do not create links in glibc-hwcaps subdirectories, but
1099 still log the cache addition. */
1101 printf ("\t%s -> %s\n", dlib_ptr
->soname
, dlib_ptr
->name
);
1102 filename
= dlib_ptr
->name
;
1104 if (opt_build_cache
)
1105 add_to_cache (entry
->path
, filename
, dlib_ptr
->soname
,
1106 dlib_ptr
->flag
, dlib_ptr
->isa_level
, hwcap
,
1110 /* Free all resources. */
1114 free (dlib_ptr
->soname
);
1115 free (dlib_ptr
->name
);
1116 dlibs
= dlibs
->next
;
1120 if (opt_chroot
!= NULL
&& dir_name
!= NULL
)
1124 /* Search through all libraries. */
1128 struct dir_entry
*entry
;
1130 for (entry
= dir_entries
; entry
!= NULL
; entry
= entry
->next
)
1133 /* Free all allocated memory. */
1136 entry
= dir_entries
;
1137 dir_entries
= dir_entries
->next
;
1144 static void parse_conf_include (const char *config_file
, unsigned int lineno
,
1145 bool do_chroot
, const char *pattern
);
1147 /* Parse configuration file. */
1149 parse_conf (const char *filename
, bool do_chroot
)
1155 unsigned int lineno
;
1157 if (do_chroot
&& opt_chroot
)
1159 canon
= chroot_canon (opt_chroot
, filename
);
1161 file
= fopen (canon
, "r");
1168 file
= fopen (filename
, "r");
1173 if (errno
!= ENOENT
)
1174 error (0, errno
, _("\
1175 Warning: ignoring configuration file that cannot be opened: %s"),
1177 if (canon
!= filename
)
1178 free ((char *) canon
);
1182 /* No threads use this stream. */
1183 __fsetlocking (file
, FSETLOCKING_BYCALLER
);
1185 if (canon
!= filename
)
1186 free ((char *) canon
);
1191 ssize_t n
= getline (&line
, &len
, file
);
1196 if (line
[n
- 1] == '\n')
1199 /* Because the file format does not know any form of quoting we
1200 can search forward for the next '#' character and if found
1201 make it terminating the line. */
1202 *strchrnul (line
, '#') = '\0';
1204 /* Remove leading whitespace. NUL is no whitespace character. */
1206 while (isspace (*cp
))
1209 /* If the line is blank it is ignored. */
1213 if (!strncmp (cp
, "include", 7) && isblank (cp
[7]))
1217 while ((dir
= strsep (&cp
, " \t")) != NULL
)
1219 parse_conf_include (filename
, lineno
, do_chroot
, dir
);
1221 else if (!strncasecmp (cp
, "hwcap", 5) && isblank (cp
[5]))
1222 error (0, 0, _("%s:%u: hwcap directive ignored"), filename
, lineno
);
1224 add_dir_1 (cp
, filename
, lineno
);
1226 while (!feof_unlocked (file
));
1228 /* Free buffer and close file. */
1233 /* Handle one word in an `include' line, a glob pattern of additional
1234 config files to read. */
1236 parse_conf_include (const char *config_file
, unsigned int lineno
,
1237 bool do_chroot
, const char *pattern
)
1239 if (opt_chroot
!= NULL
&& pattern
[0] != '/')
1240 error (EXIT_FAILURE
, 0,
1241 _("need absolute file name for configuration file when using -r"));
1244 if (pattern
[0] != '/' && strchr (config_file
, '/') != NULL
)
1246 if (asprintf (©
, "%s/%s", dirname (strdupa (config_file
)),
1248 error (EXIT_FAILURE
, 0, _("memory exhausted"));
1254 if (do_chroot
&& opt_chroot
)
1256 char *canon
= chroot_canon (opt_chroot
, pattern
);
1259 result
= glob64 (canon
, 0, NULL
, &gl
);
1263 result
= glob64 (pattern
, 0, NULL
, &gl
);
1268 for (size_t i
= 0; i
< gl
.gl_pathc
; ++i
)
1269 parse_conf (gl
.gl_pathv
[i
], false);
1281 error (0, errno
, _("%s:%u: cannot read directory %s"),
1282 config_file
, lineno
, pattern
);
1293 /* Honour LD_HWCAP_MASK. */
1297 char *mask
= getenv ("LD_HWCAP_MASK");
1300 hwcap_mask
= strtoul (mask
, NULL
, 0);
1305 main (int argc
, char **argv
)
1307 /* Set locale via LC_ALL. */
1308 setlocale (LC_ALL
, "");
1310 /* But keep the C collation. That way `include' directives using
1311 globbing patterns are processed in a locale-independent order. */
1312 setlocale (LC_COLLATE
, "C");
1314 /* Set the text message domain. */
1315 textdomain (_libc_intl_domainname
);
1317 /* Parse and process arguments. */
1319 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
1321 /* Remaining arguments are additional directories if opt_manual_link
1323 if (remaining
!= argc
&& !opt_manual_link
)
1326 for (i
= remaining
; i
< argc
; ++i
)
1327 if (opt_build_cache
&& argv
[i
][0] != '/')
1328 error (EXIT_FAILURE
, 0,
1329 _("relative path `%s' used to build cache"),
1332 add_dir_1 (argv
[i
], "<cmdline>", 0);
1337 if (opt_chroot
!= NULL
)
1339 /* Normalize the path a bit, we might need it for printing later. */
1340 char *endp
= rawmemchr (opt_chroot
, '\0');
1341 while (endp
> opt_chroot
&& endp
[-1] == '/')
1344 if (endp
== opt_chroot
)
1347 if (opt_chroot
!= NULL
)
1349 /* It is faster to use chroot if we can. */
1350 if (!chroot (opt_chroot
))
1353 error (EXIT_FAILURE
, errno
, _("Can't chdir to /"));
1359 if (cache_file
== NULL
)
1361 cache_file
= alloca (strlen (LD_SO_CACHE
) + 1);
1362 strcpy (cache_file
, LD_SO_CACHE
);
1365 if (config_file
== NULL
)
1366 config_file
= LD_SO_CONF
;
1368 if (opt_print_cache
)
1370 if (opt_chroot
!= NULL
)
1372 char *p
= chroot_canon (opt_chroot
, cache_file
);
1374 error (EXIT_FAILURE
, errno
, _("Can't open cache file %s\n"),
1378 print_cache (cache_file
);
1379 if (opt_chroot
!= NULL
)
1384 if (opt_chroot
!= NULL
)
1386 /* Canonicalize the directory name of cache_file, not cache_file,
1387 because we'll rename a temporary cache file to it. */
1388 char *p
= strrchr (cache_file
, '/');
1389 char *canon
= chroot_canon (opt_chroot
,
1390 p
? (*p
= '\0', cache_file
) : "/");
1393 error (EXIT_FAILURE
, errno
,
1394 _("Can't open cache file directory %s\n"),
1395 p
? cache_file
: "/");
1402 cache_file
= alloca (strlen (canon
) + strlen (p
) + 2);
1403 sprintf (cache_file
, "%s/%s", canon
, p
);
1407 if (opt_manual_link
)
1409 /* Link all given libraries manually. */
1412 for (i
= remaining
; i
< argc
; ++i
)
1413 manual_link (argv
[i
]);
1419 if (opt_build_cache
)
1422 if (!opt_only_cline
)
1424 parse_conf (config_file
, true);
1426 /* Always add the standard search paths. */
1427 add_system_dir (SLIBDIR
);
1428 if (strcmp (SLIBDIR
, LIBDIR
))
1429 add_system_dir (LIBDIR
);
1432 const char *aux_cache_file
= _PATH_LDCONFIG_AUX_CACHE
;
1433 if (opt_chroot
!= NULL
)
1434 aux_cache_file
= chroot_canon (opt_chroot
, aux_cache_file
);
1436 if (! opt_ignore_aux_cache
&& aux_cache_file
)
1437 load_aux_cache (aux_cache_file
);
1443 if (opt_build_cache
)
1445 save_cache (cache_file
);
1447 save_aux_cache (aux_cache_file
);