1 /* Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Andreas Jaeger <aj@suse.de>, 1999.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
32 #include <sys/fcntl.h>
35 #include <sys/types.h>
40 #include "dl-procinfo.h"
43 # define LD_SO_CONF SYSCONFDIR "/ld.so.conf"
46 /* Get libc version number. */
49 #define PACKAGE _libc_intl_domainname
57 {"libc4", FLAG_LIBC4
},
58 {"libc5", FLAG_ELF_LIBC5
},
59 {"libc6", FLAG_ELF_LIBC6
},
60 {"glibc2", FLAG_ELF_LIBC6
}
64 /* List of directories to handle. */
71 struct dir_entry
*next
;
74 /* The list is unsorted, contains no duplicates. Entries are added at
76 static struct dir_entry
*dir_entries
;
78 /* Flags for different options. */
80 static int opt_print_cache
= 0;
85 /* Format to support. */
86 /* 0: only libc5/glibc2; 1: both; 2: only glibc 2.2. */
90 static int opt_build_cache
= 1;
93 static int opt_link
= 1;
95 /* Only process directories specified on the command line. */
96 static int opt_only_cline
= 0;
98 /* Path to root for chroot. */
99 static char *opt_chroot
;
101 /* Manually link given shared libraries. */
102 static int opt_manual_link
= 0;
104 /* Cache file to use. */
105 static char *cache_file
;
107 /* Configuration file. */
108 static const char *config_file
;
110 /* Name and version of program. */
111 static void print_version (FILE *stream
, struct argp_state
*state
);
112 void (*argp_program_version_hook
) (FILE *, struct argp_state
*)
115 /* Definitions of arguments for argp functions. */
116 static const struct argp_option options
[] =
118 { "print-cache", 'p', NULL
, 0, N_("Print cache"), 0},
119 { "verbose", 'v', NULL
, 0, N_("Generate verbose messages"), 0},
120 { NULL
, 'N', NULL
, 0, N_("Don't build cache"), 0},
121 { NULL
, 'X', NULL
, 0, N_("Don't generate links"), 0},
122 { NULL
, 'r', "ROOT", 0, N_("Change to and use ROOT as root directory"), 0},
123 { NULL
, 'C', "CACHE", 0, N_("Use CACHE as cache file"), 0},
124 { NULL
, 'f', "CONF", 0, N_("Use CONF as configuration file"), 0},
125 { NULL
, 'n', NULL
, 0, N_("Only process directories specified on the command line. Don't build cache."), 0},
126 { NULL
, 'l', NULL
, 0, N_("Manually link individual libraries."), 0},
127 { "format", 'c', "FORMAT", 0, N_("Format to use: new, old or compat (default)"), 0},
128 { NULL
, 0, NULL
, 0, NULL
, 0 }
131 /* Short description of program. */
132 static const char doc
[] = N_("Configure Dynamic Linker Run Time Bindings.");
134 /* Prototype for option handler. */
135 static error_t
parse_opt (int key
, char *arg
, struct argp_state
*state
);
137 /* Data structure to communicate with argp functions. */
138 static struct argp argp
=
140 options
, parse_opt
, NULL
, doc
, NULL
, NULL
, NULL
143 /* Check if string corresponds to an important hardware capability or
146 is_hwcap_platform (const char *name
)
148 int hwcap_idx
= _dl_string_hwcap (name
);
150 if (hwcap_idx
!= -1 && ((1 << hwcap_idx
) & HWCAP_IMPORTANT
))
153 hwcap_idx
= _dl_string_platform (name
);
160 /* Get hwcap (including platform) encoding of path. */
162 path_hwcap (const char *path
)
164 char *str
= xstrdup (path
);
175 /* Search pathname from the end and check for hwcap strings. */
178 ptr
= strrchr (str
, '/');
183 h
= _dl_string_hwcap (ptr
+ 1);
185 if (h
== (uint64_t) -1)
187 h
= _dl_string_platform (ptr
+ 1);
188 if (h
== (uint64_t) -1)
193 /* Search the next part of the path. */
201 /* Handle program arguments. */
203 parse_opt (int key
, char *arg
, struct argp_state
*state
)
236 if (strcmp (arg
, "old") == 0)
238 else if (strcmp (arg
, "compat") == 0)
240 else if (strcmp (arg
, "new") == 0)
244 return ARGP_ERR_UNKNOWN
;
250 /* Print the version information. */
252 print_version (FILE *stream
, struct argp_state
*state
)
254 fprintf (stream
, "ldconfig (GNU %s) %s\n", PACKAGE
, VERSION
);
255 fprintf (stream
, gettext ("\
256 Copyright (C) %s Free Software Foundation, Inc.\n\
257 This is free software; see the source for copying conditions. There is NO\n\
258 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
260 fprintf (stream
, gettext ("Written by %s.\n"),
264 /* Add a single directory entry. */
266 add_single_dir (struct dir_entry
*entry
, int verbose
)
268 struct dir_entry
*ptr
, *prev
;
274 /* Check for duplicates. */
275 if (ptr
->ino
== entry
->ino
&& ptr
->dev
== entry
->dev
)
277 if (opt_verbose
&& verbose
)
278 error (0, 0, _("Path `%s' given more than once"), entry
->path
);
279 /* Use the newer information. */
280 ptr
->flag
= entry
->flag
;
288 /* Is this the first entry? */
289 if (ptr
== NULL
&& dir_entries
== NULL
)
291 else if (ptr
== NULL
)
295 /* Add one directory to the list of directories to process. */
297 add_dir (const char *line
)
300 struct dir_entry
*entry
;
302 struct stat64 stat_buf
;
304 entry
= xmalloc (sizeof (struct dir_entry
));
307 /* Search for an '=' sign. */
308 entry
->path
= xstrdup (line
);
309 equal_sign
= strchr (entry
->path
, '=');
314 entry
->flag
= FLAG_ANY
;
315 for (i
= 0; i
< sizeof (lib_types
) / sizeof (lib_types
[0]); ++i
)
316 if (strcmp (equal_sign
, lib_types
[i
].name
) == 0)
318 entry
->flag
= lib_types
[i
].flag
;
321 if (entry
->flag
== FLAG_ANY
)
322 error (0, 0, _("%s is not a known library type"), equal_sign
);
326 entry
->flag
= FLAG_ANY
;
329 /* Canonify path: for now only remove trailing slashes. */
330 i
= strlen (entry
->path
) - 1;
331 while (entry
->path
[i
] == '/' && i
> 0)
333 entry
->path
[i
] = '\0';
337 if (stat64 (entry
->path
, &stat_buf
))
339 error (0, errno
, _("Can't stat %s"), entry
->path
);
345 entry
->ino
= stat_buf
.st_ino
;
346 entry
->dev
= stat_buf
.st_dev
;
348 add_single_dir (entry
, 1);
353 chroot_stat (const char *real_path
, const char *path
, struct stat64
*st
)
359 return stat64 (real_path
, st
);
361 ret
= lstat64 (real_path
, st
);
362 if (ret
|| !S_ISLNK (st
->st_mode
))
365 canon_path
= chroot_canon (opt_chroot
, path
);
366 if (canon_path
== NULL
)
369 ret
= stat64 (canon_path
, st
);
374 /* Create a symbolic link from soname to libname in directory path. */
376 create_links (const char *real_path
, const char *path
, const char *libname
,
379 char *full_libname
, *full_soname
;
380 char *real_full_libname
, *real_full_soname
;
381 struct stat64 stat_lib
, stat_so
, lstat_so
;
384 /* XXX: The logics in this function should be simplified. */
386 /* Get complete path. */
387 full_libname
= alloca (strlen (path
) + strlen (libname
) + 2);
388 full_soname
= alloca (strlen (path
) + strlen (soname
) + 2);
389 sprintf (full_libname
, "%s/%s", path
, libname
);
390 sprintf (full_soname
, "%s/%s", path
, soname
);
393 real_full_libname
= alloca (strlen (real_path
) + strlen (libname
) + 2);
394 real_full_soname
= alloca (strlen (real_path
) + strlen (soname
) + 2);
395 sprintf (real_full_libname
, "%s/%s", real_path
, libname
);
396 sprintf (real_full_soname
, "%s/%s", real_path
, soname
);
400 real_full_libname
= full_libname
;
401 real_full_soname
= full_soname
;
404 /* Does soname already exist and point to the right library? */
405 if (chroot_stat (real_full_soname
, full_soname
, &stat_so
) == 0)
407 if (chroot_stat (real_full_libname
, full_libname
, &stat_lib
))
409 error (0, 0, _("Can't stat %s\n"), full_libname
);
412 if (stat_lib
.st_dev
== stat_so
.st_dev
413 && stat_lib
.st_ino
== stat_so
.st_ino
)
414 /* Link is already correct. */
416 else if (lstat64 (full_soname
, &lstat_so
) == 0
417 && !S_ISLNK (lstat_so
.st_mode
))
419 error (0, 0, _("%s is not a symbolic link\n"), full_soname
);
424 else if (lstat64 (real_full_soname
, &lstat_so
) != 0
425 || !S_ISLNK (lstat_so
.st_mode
))
426 /* Unless it is a stale symlink, there is no need to remove. */
430 printf ("\t%s -> %s", soname
, libname
);
432 if (do_link
&& opt_link
)
434 /* Remove old link. */
436 if (unlink (real_full_soname
))
438 error (0, 0, _("Can't unlink %s"), full_soname
);
441 /* Create symbolic link. */
442 if (do_link
&& symlink (libname
, real_full_soname
))
444 error (0, 0, _("Can't link %s to %s"), full_soname
, libname
);
450 fputs (_(" (changed)\n"), stdout
);
452 fputs (_(" (SKIPPED)\n"), stdout
);
455 else if (opt_verbose
)
456 fputs ("\n", stdout
);
459 /* Manually link the given library. */
461 manual_link (char *library
)
468 struct stat64 stat_buf
;
470 unsigned int osversion
;
472 /* Prepare arguments for create_links call. Split library name in
473 directory and filename first. Since path is allocated, we've got
474 to be careful to free at the end. */
475 path
= xstrdup (library
);
476 libname
= strrchr (path
, '/');
480 /* Successfully split names. Check if path is just "/" to avoid
484 libname
= library
+ 1;
485 path
= xrealloc (path
, 2);
496 /* There's no path, construct one. */
498 path
= xrealloc (path
, 2);
504 real_path
= chroot_canon (opt_chroot
, path
);
505 if (real_path
== NULL
)
507 error (0, errno
, _("Can't find %s"), path
);
511 real_library
= alloca (strlen (real_path
) + strlen (libname
) + 2);
512 sprintf (real_library
, "%s/%s", real_path
, libname
);
517 real_library
= library
;
520 /* Do some sanity checks first. */
521 if (lstat64 (real_library
, &stat_buf
))
523 error (0, errno
, _("Can't lstat %s"), library
);
527 /* We don't want links here! */
528 else if (!S_ISREG (stat_buf
.st_mode
))
530 error (0, 0, _("Ignored file %s since it is not a regular file."),
535 if (process_file (real_library
, library
, libname
, &flag
, &osversion
,
538 error (0, 0, _("No link created since soname could not be found for %s"),
543 create_links (real_path
, path
, libname
, soname
);
549 /* Read a whole directory and search for libraries.
550 The purpose is two-fold:
551 - search for libraries which will be added to the cache
552 - create symbolic links to the soname for each library
554 This has to be done separatly for each directory.
556 To keep track of which libraries to add to the cache and which
557 links to create, we save a list of all libraries.
559 The algorithm is basically:
560 for all libraries in the directory do
561 get soname of library
562 if soname is already in list
563 if new library is newer, replace entry
564 otherwise ignore this library
565 otherwise add library to list
567 For example, if the two libraries libxy.so.1.1 and libxy.so.1.2
568 exist and both have the same soname, e.g. libxy.so, a symbolic link
569 is created from libxy.so.1.2 (the newer one) to libxy.so.
570 libxy.so.1.2 and libxy.so are added to the cache - but not
573 /* Information for one library. */
580 unsigned int osversion
;
581 struct dlib_entry
*next
;
586 search_dir (const struct dir_entry
*entry
)
589 struct dirent
*direntry
;
590 char *file_name
, *dir_name
, *real_file_name
, *real_name
;
591 int file_name_len
, real_file_name_len
, len
;
593 struct dlib_entry
*dlibs
;
594 struct dlib_entry
*dlib_ptr
;
595 struct stat64 lstat_buf
, stat_buf
;
597 uint64_t hwcap
= path_hwcap (entry
->path
);
598 unsigned int osversion
;
600 file_name_len
= PATH_MAX
;
601 file_name
= alloca (file_name_len
);
608 printf ("%s: (hwcap: 0x%" PRIx64
")\n", entry
->path
, hwcap
);
610 printf ("%s:\n", entry
->path
);
615 dir_name
= chroot_canon (opt_chroot
, entry
->path
);
616 real_file_name_len
= PATH_MAX
;
617 real_file_name
= alloca (real_file_name_len
);
621 dir_name
= entry
->path
;
622 real_file_name_len
= 0;
623 real_file_name
= file_name
;
626 if (dir_name
== NULL
|| (dir
= opendir (dir_name
)) == NULL
)
629 error (0, errno
, _("Can't open directory %s"), entry
->path
);
630 if (opt_chroot
&& dir_name
)
635 while ((direntry
= readdir (dir
)) != NULL
)
638 #ifdef _DIRENT_HAVE_D_TYPE
639 /* We only look at links and regular files. */
640 if (direntry
->d_type
!= DT_UNKNOWN
641 && direntry
->d_type
!= DT_LNK
642 && direntry
->d_type
!= DT_REG
643 && direntry
->d_type
!= DT_DIR
)
645 #endif /* _DIRENT_HAVE_D_TYPE */
646 /* Does this file look like a shared library or is it a hwcap
647 subdirectory? The dynamic linker is also considered as
649 if (((strncmp (direntry
->d_name
, "lib", 3) != 0
650 && strncmp (direntry
->d_name
, "ld-", 3) != 0)
651 || strstr (direntry
->d_name
, ".so") == NULL
)
652 && !is_hwcap_platform (direntry
->d_name
))
654 len
= strlen (entry
->path
) + strlen (direntry
->d_name
);
655 if (len
> file_name_len
)
657 file_name_len
= len
+ 1;
658 file_name
= alloca (file_name_len
);
660 real_file_name
= file_name
;
662 sprintf (file_name
, "%s/%s", entry
->path
, direntry
->d_name
);
665 len
= strlen (dir_name
) + strlen (direntry
->d_name
);
666 if (len
> real_file_name_len
)
668 real_file_name_len
= len
+ 1;
669 real_file_name
= alloca (real_file_name_len
);
671 sprintf (real_file_name
, "%s/%s", dir_name
, direntry
->d_name
);
673 #ifdef _DIRENT_HAVE_D_TYPE
674 if (direntry
->d_type
!= DT_UNKNOWN
)
675 lstat_buf
.st_mode
= DTTOIF (direntry
->d_type
);
678 if (lstat64 (real_file_name
, &lstat_buf
))
680 error (0, errno
, _("Can't lstat %s"), file_name
);
684 is_link
= S_ISLNK (lstat_buf
.st_mode
);
687 /* In case of symlink, we check if the symlink refers to
689 if (stat64 (real_file_name
, &stat_buf
))
691 error (0, errno
, _("Can't stat %s"), file_name
);
694 is_dir
= S_ISDIR (stat_buf
.st_mode
);
697 is_dir
= S_ISDIR (lstat_buf
.st_mode
);
699 if (is_dir
&& is_hwcap_platform (direntry
->d_name
))
701 /* Handle subdirectory later. */
702 struct dir_entry
*new_entry
;
704 new_entry
= xmalloc (sizeof (struct dir_entry
));
705 new_entry
->path
= xstrdup (file_name
);
706 new_entry
->flag
= entry
->flag
;
707 new_entry
->next
= NULL
;
710 new_entry
->ino
= stat_buf
.st_ino
;
711 new_entry
->dev
= stat_buf
.st_dev
;
715 new_entry
->ino
= lstat_buf
.st_ino
;
716 new_entry
->dev
= lstat_buf
.st_dev
;
718 add_single_dir (new_entry
, 0);
721 else if (!S_ISREG (stat_buf
.st_mode
) && !is_link
)
724 if (opt_chroot
&& is_link
)
726 real_name
= chroot_canon (opt_chroot
, file_name
);
727 if (real_name
== NULL
)
729 if (strstr (file_name
, ".so") == NULL
)
730 error (0, 0, _("Input file %s not found.\n"), file_name
);
735 real_name
= real_file_name
;
737 if (process_file (real_name
, file_name
, direntry
->d_name
, &flag
,
738 &osversion
, &soname
, is_link
))
740 if (real_name
!= real_file_name
)
745 if (real_name
!= real_file_name
)
748 /* Links will just point to itself. */
752 soname
= xstrdup (direntry
->d_name
);
756 && (entry
->flag
== FLAG_ELF_LIBC5
757 || entry
->flag
== FLAG_ELF_LIBC6
))
759 /* Some sanity checks to print warnings. */
762 if (flag
== FLAG_ELF_LIBC5
&& entry
->flag
!= FLAG_ELF_LIBC5
763 && entry
->flag
!= FLAG_ANY
)
764 error (0, 0, _("libc5 library %s in wrong directory"), file_name
);
765 if (flag
== FLAG_ELF_LIBC6
&& entry
->flag
!= FLAG_ELF_LIBC6
766 && entry
->flag
!= FLAG_ANY
)
767 error (0, 0, _("libc6 library %s in wrong directory"), file_name
);
768 if (flag
== FLAG_LIBC4
&& entry
->flag
!= FLAG_LIBC4
769 && entry
->flag
!= FLAG_ANY
)
770 error (0, 0, _("libc4 library %s in wrong directory"), file_name
);
773 /* Add library to list. */
774 for (dlib_ptr
= dlibs
; dlib_ptr
!= NULL
; dlib_ptr
= dlib_ptr
->next
)
776 /* Is soname already in list? */
777 if (strcmp (dlib_ptr
->soname
, soname
) == 0)
779 /* Prefer a file to a link, otherwise check which one
781 if ((!is_link
&& dlib_ptr
->is_link
)
782 || (is_link
== dlib_ptr
->is_link
783 && _dl_cache_libcmp (dlib_ptr
->name
, direntry
->d_name
) < 0))
785 /* It's newer - add it. */
786 /* Flag should be the same - sanity check. */
787 if (dlib_ptr
->flag
!= flag
)
789 if (dlib_ptr
->flag
== FLAG_ELF
790 && (flag
== FLAG_ELF_LIBC5
|| flag
== FLAG_ELF_LIBC6
))
791 dlib_ptr
->flag
= flag
;
792 else if ((dlib_ptr
->flag
== FLAG_ELF_LIBC5
793 || dlib_ptr
->flag
== FLAG_ELF_LIBC6
)
795 dlib_ptr
->flag
= flag
;
797 error (0, 0, _("libraries %s and %s in directory %s have same soname but different type."),
798 dlib_ptr
->name
, direntry
->d_name
, entry
->path
);
800 free (dlib_ptr
->name
);
801 dlib_ptr
->osversion
= osversion
;
802 dlib_ptr
->name
= xstrdup (direntry
->d_name
);
803 dlib_ptr
->is_link
= is_link
;
805 /* Don't add this library, abort loop. */
806 /* Also free soname, since it's dynamically allocated. */
811 /* Add the library if it's not already in. */
812 if (dlib_ptr
== NULL
)
814 dlib_ptr
= (struct dlib_entry
*)xmalloc (sizeof (struct dlib_entry
));
815 dlib_ptr
->name
= xstrdup (direntry
->d_name
);
816 dlib_ptr
->flag
= flag
;
817 dlib_ptr
->osversion
= osversion
;
818 dlib_ptr
->soname
= soname
;
819 dlib_ptr
->is_link
= is_link
;
820 /* Add at head of list. */
821 dlib_ptr
->next
= dlibs
;
828 /* Now dlibs contains a list of all libs - add those to the cache
829 and created all symbolic links. */
830 for (dlib_ptr
= dlibs
; dlib_ptr
!= NULL
; dlib_ptr
= dlib_ptr
->next
)
832 /* Don't create links to links. */
833 if (dlib_ptr
->is_link
== 0)
834 create_links (dir_name
, entry
->path
, dlib_ptr
->name
,
837 add_to_cache (entry
->path
, dlib_ptr
->soname
, dlib_ptr
->flag
,
838 dlib_ptr
->osversion
, hwcap
);
841 /* Free all resources. */
845 free (dlib_ptr
->soname
);
846 free (dlib_ptr
->name
);
851 if (opt_chroot
&& dir_name
)
855 /* Search through all libraries. */
859 struct dir_entry
*entry
;
861 for (entry
= dir_entries
; entry
!= NULL
; entry
= entry
->next
)
864 /* Free all allocated memory. */
868 dir_entries
= dir_entries
->next
;
875 /* Parse configuration file. */
877 parse_conf (const char *filename
)
886 canon
= chroot_canon (opt_chroot
, filename
);
888 file
= fopen (canon
, "r");
895 file
= fopen (filename
, "r");
900 error (0, errno
, _("Can't open configuration file %s"), canon
);
901 if (canon
!= filename
)
902 free ((char *) canon
);
906 if (canon
!= filename
)
907 free ((char *) canon
);
911 ssize_t n
= getline (&line
, &len
, file
);
915 if (line
[n
- 1] == '\n')
918 /* Because the file format does not know any form of quoting we
919 can search forward for the next '#' character and if found
920 make it terminating the line. */
921 *strchrnul (line
, '#') = '\0';
923 /* If the line is blank it is ignored. */
928 } while (!feof (file
));
930 /* Free buffer and close file. */
937 main (int argc
, char **argv
)
941 /* Parse and process arguments. */
942 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
944 /* Remaining arguments are additional libraries if opt_manual_link
946 if (remaining
!= argc
&& !opt_manual_link
)
949 for (i
= remaining
; i
< argc
; ++i
)
955 /* Normalize the path a bit, we might need it for printing later. */
956 char *endp
= strchr (opt_chroot
, '\0');
957 while (endp
> opt_chroot
&& endp
[-1] == '/')
960 if (endp
== opt_chroot
)
965 /* It is faster to use chroot if we can. */
966 if (!chroot (opt_chroot
))
969 error (EXIT_FAILURE
, errno
, _("Can't chdir to /"));
975 if (cache_file
== NULL
)
977 cache_file
= alloca (strlen (LD_SO_CACHE
) + 1);
978 strcpy (cache_file
, LD_SO_CACHE
);
981 if (config_file
== NULL
)
982 config_file
= LD_SO_CONF
;
988 char *p
= chroot_canon (opt_chroot
, cache_file
);
990 error (EXIT_FAILURE
, errno
, _("Can't open cache file %s\n"),
994 print_cache (cache_file
);
1002 /* Canonicalize the directory name of cache_file, not cache_file,
1003 because we'll rename a temporary cache file to it. */
1004 char *p
= strrchr (cache_file
, '/');
1005 char *canon
= chroot_canon (opt_chroot
,
1006 p
? (*p
= '\0', cache_file
) : "/");
1010 error (EXIT_FAILURE
, errno
,
1011 _("Can't open cache file directory %s\n"),
1012 p
? cache_file
: "/");
1020 cache_file
= alloca (strlen (canon
) + strlen (p
) + 2);
1021 sprintf (cache_file
, "%s/%s", canon
, p
);
1025 if (opt_manual_link
)
1027 /* Link all given libraries manually. */
1030 for (i
= remaining
; i
< argc
; ++i
)
1031 manual_link (argv
[i
]);
1037 if (opt_build_cache
)
1040 if (!opt_only_cline
)
1042 /* Always add the standard search paths. */
1044 if (strcmp (SLIBDIR
, LIBDIR
))
1047 parse_conf (config_file
);
1052 if (opt_build_cache
)
1053 save_cache (cache_file
);