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
30 #include <stdio_ext.h>
35 #include <sys/fcntl.h>
38 #include <sys/types.h>
45 #include <dl-hwcaps.h>
46 #include <dl-is_dso.h>
48 #include <dl-procinfo.h>
51 # define LD_SO_CONF SYSCONFDIR "/ld.so.conf"
54 /* Get libc version number. */
57 #define PACKAGE _libc_intl_domainname
59 /* List of directories to handle. */
66 const char *from_file
;
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
77 static struct dir_entry
*dir_entries
;
79 /* Flags for different options. */
81 static int opt_print_cache
;
86 /* Format to support. */
87 enum opt_format opt_format
= opt_format_new
;
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
*)
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. */
156 parse_opt (int key
, char *arg
, struct argp_state
*state
)
162 /* Ignore auxiliary cache since we use non-standard cache. */
163 opt_ignore_aux_cache
= 1;
169 opt_ignore_aux_cache
= 1;
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
;
202 return ARGP_ERR_UNKNOWN
;
208 /* Print bug-reporting information in the help message. */
210 more_help (int key
, const char *text
, void *input
)
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)
225 return (char *) text
;
228 /* Print the version information. */
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\
238 fprintf (stream
, gettext ("Written by %s.\n"),
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
;
260 /* Add a single directory entry. Return true if the directory is
261 actually added (because it is not a duplicate). */
263 add_single_dir (struct dir_entry
*entry
, int verbose
)
265 struct dir_entry
*ptr
, *prev
;
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
;
292 /* Is this the first entry? */
293 if (ptr
== NULL
&& dir_entries
== NULL
)
295 else if (ptr
== NULL
)
300 /* Check if PATH contains a "glibc-hwcaps" subdirectory. If so, queue
301 its subdirectories for glibc-hwcaps processing. */
303 add_glibc_hwcaps_subdirectories (struct dir_entry
*entry
, const char *path
)
305 /* glibc-hwcaps subdirectories do not nest. */
306 assert (entry
->hwcaps
== NULL
);
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
);
318 struct dirent64
*e
= readdir64 (dir
);
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
331 if (e
->d_name
[0] == '.' || e
->d_type
== DT_REG
332 || strchr (e
->d_name
, ':') != NULL
)
335 /* See if this entry eventually resolves to a directory. */
337 if (fstatat (dirfd (dir
), e
->d_name
, &st
, 0) < 0)
338 /* Ignore unreadable entries. */
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. */
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
,
355 new_entry
->hwcaps
= new_glibc_hwcaps_subdirectory (e
->d_name
);
356 add_single_dir (new_entry
, 0);
366 /* Add one directory to the list of directories to process. */
368 add_dir_1 (const char *line
, const char *from_file
, int from_line
)
371 struct dir_entry
*entry
= xmalloc (sizeof (struct dir_entry
));
372 entry
->hwcaps
= 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';
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
))
406 error (0, errno
, _("Can't stat %s"), entry
->path
);
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
)
425 add_dir (const char *line
)
427 add_dir_1 (line
, "<builtin>", 0);
431 chroot_stat (const char *real_path
, const char *path
, struct stat
*st
)
437 return stat (real_path
, st
);
439 ret
= lstat (real_path
, st
);
440 if (ret
|| !S_ISLNK (st
->st_mode
))
443 canon_path
= chroot_canon (opt_chroot
, path
);
444 if (canon_path
== NULL
)
447 ret
= stat (canon_path
, st
);
452 /* Create a symbolic link from soname to libname in directory path. */
454 create_links (const char *real_path
, const char *path
, const char *libname
,
457 char *full_libname
, *full_soname
;
458 char *real_full_libname
, *real_full_soname
;
459 struct stat stat_lib
, stat_so
, lstat_so
;
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
);
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
);
490 if (stat_lib
.st_dev
== stat_so
.st_dev
491 && stat_lib
.st_ino
== stat_so
.st_ino
)
492 /* Link is already correct. */
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
);
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. */
508 printf ("\t%s -> %s", soname
, libname
);
510 if (do_link
&& opt_link
)
512 /* Remove old link. */
514 if (unlink (real_full_soname
))
516 error (0, 0, _("Can't unlink %s"), full_soname
);
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
);
528 fputs (_(" (changed)\n"), stdout
);
530 fputs (_(" (SKIPPED)\n"), stdout
);
533 else if (opt_verbose
)
534 fputs ("\n", stdout
);
537 /* Manually link the given library. */
539 manual_link (char *library
)
546 struct stat stat_buf
;
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
, '/');
558 /* Successfully split names. Check if path is just "/" to avoid
562 libname
= library
+ 1;
563 path
= xrealloc (path
, 2);
574 /* There's no path, construct one. */
576 path
= xrealloc (path
, 2);
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
);
589 real_library
= alloca (strlen (real_path
) + strlen (libname
) + 2);
590 sprintf (real_library
, "%s/%s", real_path
, libname
);
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
);
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."),
612 if (process_file (real_library
, library
, libname
, &flag
, &isa_level
, &soname
,
615 error (0, 0, _("No link created since soname could not be found for %s"),
620 soname
= xstrdup (libname
);
621 create_links (real_path
, path
, libname
, soname
);
624 if (path
!= real_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
654 /* Information for one library. */
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. */
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#"))
676 if (len
>= sizeof (".#prelink#.XXXXXX") - 1
677 && memcmp (name
+ len
- sizeof (".#prelink#.XXXXXX")
678 + 1, ".#prelink#.", sizeof (".#prelink#.") - 1) == 0)
681 /* Skip temporary files created by RPM. */
682 if (memchr (name
, ';', len
) != NULL
)
684 /* Skip temporary files created by dpkg. */
685 if (endswithn (name
, len
, ".dpkg-new")
686 || endswithn (name
, len
, ".dpkg-tmp"))
692 search_dir (const struct dir_entry
*entry
)
696 if (entry
->hwcaps
== NULL
)
697 printf ("%s:", entry
->path
);
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
);
705 char *real_file_name
;
707 if (opt_chroot
!= NULL
)
708 dir_name
= chroot_canon (opt_chroot
, entry
->path
);
710 dir_name
= entry
->path
;
713 if (dir_name
== NULL
|| (dir
= opendir (dir_name
)) == NULL
)
716 error (0, errno
, _("Can't open directory %s"), entry
->path
);
717 if (opt_chroot
!= NULL
)
722 struct dirent64
*direntry
;
723 struct dlib_entry
*dlibs
= NULL
;
724 while ((direntry
= readdir64 (dir
)) != NULL
)
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
)
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
))
739 size_t len
= strlen (direntry
->d_name
);
740 if (skip_dso_based_on_name (direntry
->d_name
, len
))
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"));
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
);
758 if (__glibc_unlikely (lstat (real_file_name
, &lstat_buf
)))
760 error (0, errno
, _("Cannot lstat %s"), file_name
);
764 struct stat stat_buf
;
765 int is_link
= S_ISLNK (lstat_buf
.st_mode
);
768 /* In case of symlink, we check if the symlink refers to
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
);
781 if (__glibc_unlikely (stat (target_name
, &stat_buf
)))
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
)
796 if (opt_chroot
!= NULL
)
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
))
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
);
820 real_name
= real_file_name
;
822 /* Call lstat if not done yet. */
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
);
831 /* First search whether the auxiliary cache contains this
832 library already and it's not changed. */
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
)
844 else if (opt_build_cache
)
845 add_to_aux_cache (&lstat_buf
, flag
, isa_level
, soname
);
849 soname
= xstrdup (direntry
->d_name
);
851 /* A link may just point to itself. */
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)
890 if (real_name
!= real_file_name
)
896 soname
= xstrdup (direntry
->d_name
);
899 /* Some sanity checks to print warnings. */
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
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. */
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
;
951 free (real_file_name
);
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
,
972 filename
= dlib_ptr
->soname
;
976 /* Do not create links in glibc-hwcaps subdirectories, but
977 still log the cache addition. */
979 printf ("\t%s -> %s\n", dlib_ptr
->soname
, dlib_ptr
->name
);
980 filename
= dlib_ptr
->name
;
983 add_to_cache (entry
->path
, filename
, dlib_ptr
->soname
,
984 dlib_ptr
->flag
, dlib_ptr
->isa_level
, entry
->hwcaps
);
987 /* Free all resources. */
991 free (dlib_ptr
->soname
);
992 free (dlib_ptr
->name
);
997 if (opt_chroot
!= NULL
&& dir_name
!= NULL
)
1001 /* Search through all libraries. */
1005 struct dir_entry
*entry
;
1007 for (entry
= dir_entries
; entry
!= NULL
; entry
= entry
->next
)
1010 /* Free all allocated memory. */
1013 entry
= dir_entries
;
1014 dir_entries
= dir_entries
->next
;
1021 static void parse_conf_include (const char *config_file
, unsigned int lineno
,
1022 bool do_chroot
, const char *pattern
);
1024 /* Parse configuration file. */
1026 parse_conf (const char *filename
, bool do_chroot
)
1032 unsigned int lineno
;
1034 if (do_chroot
&& opt_chroot
)
1036 canon
= chroot_canon (opt_chroot
, filename
);
1038 file
= fopen (canon
, "r");
1045 file
= fopen (filename
, "r");
1050 if (errno
!= ENOENT
)
1051 error (0, errno
, _("\
1052 Warning: ignoring configuration file that cannot be opened: %s"),
1054 if (canon
!= filename
)
1055 free ((char *) canon
);
1059 /* No threads use this stream. */
1060 __fsetlocking (file
, FSETLOCKING_BYCALLER
);
1062 if (canon
!= filename
)
1063 free ((char *) canon
);
1068 ssize_t n
= getline (&line
, &len
, file
);
1073 if (line
[n
- 1] == '\n')
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. */
1083 while (isspace (*cp
))
1086 /* If the line is blank it is ignored. */
1090 if (!strncmp (cp
, "include", 7) && isblank (cp
[7]))
1094 while ((dir
= strsep (&cp
, " \t")) != NULL
)
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
);
1101 add_dir_1 (cp
, filename
, lineno
);
1103 while (!feof_unlocked (file
));
1105 /* Free buffer and close file. */
1110 /* Handle one word in an `include' line, a glob pattern of additional
1111 config files to read. */
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"));
1121 if (pattern
[0] != '/' && strchr (config_file
, '/') != NULL
)
1123 if (asprintf (©
, "%s/%s", dirname (strdupa (config_file
)),
1125 error (EXIT_FAILURE
, 0, _("memory exhausted"));
1131 if (do_chroot
&& opt_chroot
)
1133 char *canon
= chroot_canon (opt_chroot
, pattern
);
1136 result
= glob64 (canon
, 0, NULL
, &gl
);
1140 result
= glob64 (pattern
, 0, NULL
, &gl
);
1145 for (size_t i
= 0; i
< gl
.gl_pathc
; ++i
)
1146 parse_conf (gl
.gl_pathv
[i
], false);
1158 error (0, errno
, _("%s:%u: cannot read directory %s"),
1159 config_file
, lineno
, pattern
);
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. */
1186 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
1188 /* Remaining arguments are additional directories if opt_manual_link
1190 if (remaining
!= argc
&& !opt_manual_link
)
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"),
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] == '/')
1209 if (endp
== opt_chroot
)
1212 if (opt_chroot
!= NULL
)
1214 /* It is faster to use chroot if we can. */
1215 if (!chroot (opt_chroot
))
1218 error (EXIT_FAILURE
, errno
, _("Can't chdir to /"));
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
);
1239 error (EXIT_FAILURE
, errno
, _("Can't open cache file %s\n"),
1243 print_cache (cache_file
);
1244 if (opt_chroot
!= NULL
)
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
) : "/");
1258 error (EXIT_FAILURE
, errno
,
1259 _("Can't open cache file directory %s\n"),
1260 p
? cache_file
: "/");
1267 cache_file
= alloca (strlen (canon
) + strlen (p
) + 2);
1268 sprintf (cache_file
, "%s/%s", canon
, p
);
1272 if (opt_manual_link
)
1274 /* Link all given libraries manually. */
1277 for (i
= remaining
; i
< argc
; ++i
)
1278 manual_link (argv
[i
]);
1284 if (opt_build_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
);
1308 if (opt_build_cache
)
1310 save_cache (cache_file
);
1312 save_aux_cache (aux_cache_file
);