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
))
340 error (0, errno
, _("Can't stat %s"), entry
->path
);
346 entry
->ino
= stat_buf
.st_ino
;
347 entry
->dev
= stat_buf
.st_dev
;
349 add_single_dir (entry
, 1);
354 chroot_stat (const char *real_path
, const char *path
, struct stat64
*st
)
360 return stat64 (real_path
, st
);
362 ret
= lstat64 (real_path
, st
);
363 if (ret
|| !S_ISLNK (st
->st_mode
))
366 canon_path
= chroot_canon (opt_chroot
, path
);
367 if (canon_path
== NULL
)
370 ret
= stat64 (canon_path
, st
);
375 /* Create a symbolic link from soname to libname in directory path. */
377 create_links (const char *real_path
, const char *path
, const char *libname
,
380 char *full_libname
, *full_soname
;
381 char *real_full_libname
, *real_full_soname
;
382 struct stat64 stat_lib
, stat_so
, lstat_so
;
385 /* XXX: The logics in this function should be simplified. */
387 /* Get complete path. */
388 full_libname
= alloca (strlen (path
) + strlen (libname
) + 2);
389 full_soname
= alloca (strlen (path
) + strlen (soname
) + 2);
390 sprintf (full_libname
, "%s/%s", path
, libname
);
391 sprintf (full_soname
, "%s/%s", path
, soname
);
394 real_full_libname
= alloca (strlen (real_path
) + strlen (libname
) + 2);
395 real_full_soname
= alloca (strlen (real_path
) + strlen (soname
) + 2);
396 sprintf (real_full_libname
, "%s/%s", real_path
, libname
);
397 sprintf (real_full_soname
, "%s/%s", real_path
, soname
);
401 real_full_libname
= full_libname
;
402 real_full_soname
= full_soname
;
405 /* Does soname already exist and point to the right library? */
406 if (chroot_stat (real_full_soname
, full_soname
, &stat_so
) == 0)
408 if (chroot_stat (real_full_libname
, full_libname
, &stat_lib
))
410 error (0, 0, _("Can't stat %s\n"), full_libname
);
413 if (stat_lib
.st_dev
== stat_so
.st_dev
414 && stat_lib
.st_ino
== stat_so
.st_ino
)
415 /* Link is already correct. */
417 else if (lstat64 (full_soname
, &lstat_so
) == 0
418 && !S_ISLNK (lstat_so
.st_mode
))
420 error (0, 0, _("%s is not a symbolic link\n"), full_soname
);
425 else if (lstat64 (real_full_soname
, &lstat_so
) != 0
426 || !S_ISLNK (lstat_so
.st_mode
))
427 /* Unless it is a stale symlink, there is no need to remove. */
431 printf ("\t%s -> %s", soname
, libname
);
433 if (do_link
&& opt_link
)
435 /* Remove old link. */
437 if (unlink (real_full_soname
))
439 error (0, 0, _("Can't unlink %s"), full_soname
);
442 /* Create symbolic link. */
443 if (do_link
&& symlink (libname
, real_full_soname
))
445 error (0, 0, _("Can't link %s to %s"), full_soname
, libname
);
451 fputs (_(" (changed)\n"), stdout
);
453 fputs (_(" (SKIPPED)\n"), stdout
);
456 else if (opt_verbose
)
457 fputs ("\n", stdout
);
460 /* Manually link the given library. */
462 manual_link (char *library
)
469 struct stat64 stat_buf
;
471 unsigned int osversion
;
473 /* Prepare arguments for create_links call. Split library name in
474 directory and filename first. Since path is allocated, we've got
475 to be careful to free at the end. */
476 path
= xstrdup (library
);
477 libname
= strrchr (path
, '/');
481 /* Successfully split names. Check if path is just "/" to avoid
485 libname
= library
+ 1;
486 path
= xrealloc (path
, 2);
497 /* There's no path, construct one. */
499 path
= xrealloc (path
, 2);
505 real_path
= chroot_canon (opt_chroot
, path
);
506 if (real_path
== NULL
)
508 error (0, errno
, _("Can't find %s"), path
);
512 real_library
= alloca (strlen (real_path
) + strlen (libname
) + 2);
513 sprintf (real_library
, "%s/%s", real_path
, libname
);
518 real_library
= library
;
521 /* Do some sanity checks first. */
522 if (lstat64 (real_library
, &stat_buf
))
524 error (0, errno
, _("Can't lstat %s"), library
);
528 /* We don't want links here! */
529 else if (!S_ISREG (stat_buf
.st_mode
))
531 error (0, 0, _("Ignored file %s since it is not a regular file."),
536 if (process_file (real_library
, library
, libname
, &flag
, &osversion
,
539 error (0, 0, _("No link created since soname could not be found for %s"),
544 create_links (real_path
, path
, libname
, soname
);
550 /* Read a whole directory and search for libraries.
551 The purpose is two-fold:
552 - search for libraries which will be added to the cache
553 - create symbolic links to the soname for each library
555 This has to be done separatly for each directory.
557 To keep track of which libraries to add to the cache and which
558 links to create, we save a list of all libraries.
560 The algorithm is basically:
561 for all libraries in the directory do
562 get soname of library
563 if soname is already in list
564 if new library is newer, replace entry
565 otherwise ignore this library
566 otherwise add library to list
568 For example, if the two libraries libxy.so.1.1 and libxy.so.1.2
569 exist and both have the same soname, e.g. libxy.so, a symbolic link
570 is created from libxy.so.1.2 (the newer one) to libxy.so.
571 libxy.so.1.2 and libxy.so are added to the cache - but not
574 /* Information for one library. */
581 unsigned int osversion
;
582 struct dlib_entry
*next
;
587 search_dir (const struct dir_entry
*entry
)
590 struct dirent
*direntry
;
591 char *file_name
, *dir_name
, *real_file_name
, *real_name
;
592 int file_name_len
, real_file_name_len
, len
;
594 struct dlib_entry
*dlibs
;
595 struct dlib_entry
*dlib_ptr
;
596 struct stat64 lstat_buf
, stat_buf
;
598 uint64_t hwcap
= path_hwcap (entry
->path
);
599 unsigned int osversion
;
601 file_name_len
= PATH_MAX
;
602 file_name
= alloca (file_name_len
);
609 printf ("%s: (hwcap: 0x%" PRIx64
")\n", entry
->path
, hwcap
);
611 printf ("%s:\n", entry
->path
);
616 dir_name
= chroot_canon (opt_chroot
, entry
->path
);
617 real_file_name_len
= PATH_MAX
;
618 real_file_name
= alloca (real_file_name_len
);
622 dir_name
= entry
->path
;
623 real_file_name_len
= 0;
624 real_file_name
= file_name
;
627 if (dir_name
== NULL
|| (dir
= opendir (dir_name
)) == NULL
)
630 error (0, errno
, _("Can't open directory %s"), entry
->path
);
631 if (opt_chroot
&& dir_name
)
636 while ((direntry
= readdir (dir
)) != NULL
)
639 #ifdef _DIRENT_HAVE_D_TYPE
640 /* We only look at links and regular files. */
641 if (direntry
->d_type
!= DT_UNKNOWN
642 && direntry
->d_type
!= DT_LNK
643 && direntry
->d_type
!= DT_REG
644 && direntry
->d_type
!= DT_DIR
)
646 #endif /* _DIRENT_HAVE_D_TYPE */
647 /* Does this file look like a shared library or is it a hwcap
648 subdirectory? The dynamic linker is also considered as
650 if (((strncmp (direntry
->d_name
, "lib", 3) != 0
651 && strncmp (direntry
->d_name
, "ld-", 3) != 0)
652 || strstr (direntry
->d_name
, ".so") == NULL
)
653 && !is_hwcap_platform (direntry
->d_name
))
655 len
= strlen (entry
->path
) + strlen (direntry
->d_name
);
656 if (len
> file_name_len
)
658 file_name_len
= len
+ 1;
659 file_name
= alloca (file_name_len
);
661 real_file_name
= file_name
;
663 sprintf (file_name
, "%s/%s", entry
->path
, direntry
->d_name
);
666 len
= strlen (dir_name
) + strlen (direntry
->d_name
);
667 if (len
> real_file_name_len
)
669 real_file_name_len
= len
+ 1;
670 real_file_name
= alloca (real_file_name_len
);
672 sprintf (real_file_name
, "%s/%s", dir_name
, direntry
->d_name
);
674 #ifdef _DIRENT_HAVE_D_TYPE
675 if (direntry
->d_type
!= DT_UNKNOWN
)
676 lstat_buf
.st_mode
= DTTOIF (direntry
->d_type
);
679 if (lstat64 (real_file_name
, &lstat_buf
))
681 error (0, errno
, _("Can't lstat %s"), file_name
);
685 is_link
= S_ISLNK (lstat_buf
.st_mode
);
688 /* In case of symlink, we check if the symlink refers to
690 if (stat64 (real_file_name
, &stat_buf
))
693 error (0, errno
, _("Can't stat %s"), file_name
);
696 is_dir
= S_ISDIR (stat_buf
.st_mode
);
699 is_dir
= S_ISDIR (lstat_buf
.st_mode
);
701 if (is_dir
&& is_hwcap_platform (direntry
->d_name
))
703 /* Handle subdirectory later. */
704 struct dir_entry
*new_entry
;
706 new_entry
= xmalloc (sizeof (struct dir_entry
));
707 new_entry
->path
= xstrdup (file_name
);
708 new_entry
->flag
= entry
->flag
;
709 new_entry
->next
= NULL
;
712 new_entry
->ino
= stat_buf
.st_ino
;
713 new_entry
->dev
= stat_buf
.st_dev
;
717 new_entry
->ino
= lstat_buf
.st_ino
;
718 new_entry
->dev
= lstat_buf
.st_dev
;
720 add_single_dir (new_entry
, 0);
723 else if (!S_ISREG (stat_buf
.st_mode
) && !is_link
)
726 if (opt_chroot
&& is_link
)
728 real_name
= chroot_canon (opt_chroot
, file_name
);
729 if (real_name
== NULL
)
731 if (strstr (file_name
, ".so") == NULL
)
732 error (0, 0, _("Input file %s not found.\n"), file_name
);
737 real_name
= real_file_name
;
739 if (process_file (real_name
, file_name
, direntry
->d_name
, &flag
,
740 &osversion
, &soname
, is_link
))
742 if (real_name
!= real_file_name
)
747 if (real_name
!= real_file_name
)
750 /* Links will just point to itself. */
754 soname
= xstrdup (direntry
->d_name
);
758 && (entry
->flag
== FLAG_ELF_LIBC5
759 || entry
->flag
== FLAG_ELF_LIBC6
))
761 /* Some sanity checks to print warnings. */
764 if (flag
== FLAG_ELF_LIBC5
&& entry
->flag
!= FLAG_ELF_LIBC5
765 && entry
->flag
!= FLAG_ANY
)
766 error (0, 0, _("libc5 library %s in wrong directory"), file_name
);
767 if (flag
== FLAG_ELF_LIBC6
&& entry
->flag
!= FLAG_ELF_LIBC6
768 && entry
->flag
!= FLAG_ANY
)
769 error (0, 0, _("libc6 library %s in wrong directory"), file_name
);
770 if (flag
== FLAG_LIBC4
&& entry
->flag
!= FLAG_LIBC4
771 && entry
->flag
!= FLAG_ANY
)
772 error (0, 0, _("libc4 library %s in wrong directory"), file_name
);
775 /* Add library to list. */
776 for (dlib_ptr
= dlibs
; dlib_ptr
!= NULL
; dlib_ptr
= dlib_ptr
->next
)
778 /* Is soname already in list? */
779 if (strcmp (dlib_ptr
->soname
, soname
) == 0)
781 /* Prefer a file to a link, otherwise check which one
783 if ((!is_link
&& dlib_ptr
->is_link
)
784 || (is_link
== dlib_ptr
->is_link
785 && _dl_cache_libcmp (dlib_ptr
->name
, direntry
->d_name
) < 0))
787 /* It's newer - add it. */
788 /* Flag should be the same - sanity check. */
789 if (dlib_ptr
->flag
!= flag
)
791 if (dlib_ptr
->flag
== FLAG_ELF
792 && (flag
== FLAG_ELF_LIBC5
|| flag
== FLAG_ELF_LIBC6
))
793 dlib_ptr
->flag
= flag
;
794 else if ((dlib_ptr
->flag
== FLAG_ELF_LIBC5
795 || dlib_ptr
->flag
== FLAG_ELF_LIBC6
)
797 dlib_ptr
->flag
= flag
;
799 error (0, 0, _("libraries %s and %s in directory %s have same soname but different type."),
800 dlib_ptr
->name
, direntry
->d_name
, entry
->path
);
802 free (dlib_ptr
->name
);
803 dlib_ptr
->osversion
= osversion
;
804 dlib_ptr
->name
= xstrdup (direntry
->d_name
);
805 dlib_ptr
->is_link
= is_link
;
807 /* Don't add this library, abort loop. */
808 /* Also free soname, since it's dynamically allocated. */
813 /* Add the library if it's not already in. */
814 if (dlib_ptr
== NULL
)
816 dlib_ptr
= (struct dlib_entry
*)xmalloc (sizeof (struct dlib_entry
));
817 dlib_ptr
->name
= xstrdup (direntry
->d_name
);
818 dlib_ptr
->flag
= flag
;
819 dlib_ptr
->osversion
= osversion
;
820 dlib_ptr
->soname
= soname
;
821 dlib_ptr
->is_link
= is_link
;
822 /* Add at head of list. */
823 dlib_ptr
->next
= dlibs
;
830 /* Now dlibs contains a list of all libs - add those to the cache
831 and created all symbolic links. */
832 for (dlib_ptr
= dlibs
; dlib_ptr
!= NULL
; dlib_ptr
= dlib_ptr
->next
)
834 /* Don't create links to links. */
835 if (dlib_ptr
->is_link
== 0)
836 create_links (dir_name
, entry
->path
, dlib_ptr
->name
,
839 add_to_cache (entry
->path
, dlib_ptr
->soname
, dlib_ptr
->flag
,
840 dlib_ptr
->osversion
, hwcap
);
843 /* Free all resources. */
847 free (dlib_ptr
->soname
);
848 free (dlib_ptr
->name
);
853 if (opt_chroot
&& dir_name
)
857 /* Search through all libraries. */
861 struct dir_entry
*entry
;
863 for (entry
= dir_entries
; entry
!= NULL
; entry
= entry
->next
)
866 /* Free all allocated memory. */
870 dir_entries
= dir_entries
->next
;
877 /* Parse configuration file. */
879 parse_conf (const char *filename
)
888 canon
= chroot_canon (opt_chroot
, filename
);
890 file
= fopen (canon
, "r");
897 file
= fopen (filename
, "r");
902 error (0, errno
, _("Can't open configuration file %s"), canon
);
903 if (canon
!= filename
)
904 free ((char *) canon
);
908 if (canon
!= filename
)
909 free ((char *) canon
);
913 ssize_t n
= getline (&line
, &len
, file
);
917 if (line
[n
- 1] == '\n')
920 /* Because the file format does not know any form of quoting we
921 can search forward for the next '#' character and if found
922 make it terminating the line. */
923 *strchrnul (line
, '#') = '\0';
925 /* If the line is blank it is ignored. */
930 } while (!feof (file
));
932 /* Free buffer and close file. */
939 main (int argc
, char **argv
)
943 /* Parse and process arguments. */
944 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
946 /* Remaining arguments are additional libraries if opt_manual_link
948 if (remaining
!= argc
&& !opt_manual_link
)
951 for (i
= remaining
; i
< argc
; ++i
)
957 /* Normalize the path a bit, we might need it for printing later. */
958 char *endp
= strchr (opt_chroot
, '\0');
959 while (endp
> opt_chroot
&& endp
[-1] == '/')
962 if (endp
== opt_chroot
)
967 /* It is faster to use chroot if we can. */
968 if (!chroot (opt_chroot
))
971 error (EXIT_FAILURE
, errno
, _("Can't chdir to /"));
977 if (cache_file
== NULL
)
979 cache_file
= alloca (strlen (LD_SO_CACHE
) + 1);
980 strcpy (cache_file
, LD_SO_CACHE
);
983 if (config_file
== NULL
)
984 config_file
= LD_SO_CONF
;
990 char *p
= chroot_canon (opt_chroot
, cache_file
);
992 error (EXIT_FAILURE
, errno
, _("Can't open cache file %s\n"),
996 print_cache (cache_file
);
1004 /* Canonicalize the directory name of cache_file, not cache_file,
1005 because we'll rename a temporary cache file to it. */
1006 char *p
= strrchr (cache_file
, '/');
1007 char *canon
= chroot_canon (opt_chroot
,
1008 p
? (*p
= '\0', cache_file
) : "/");
1012 error (EXIT_FAILURE
, errno
,
1013 _("Can't open cache file directory %s\n"),
1014 p
? cache_file
: "/");
1022 cache_file
= alloca (strlen (canon
) + strlen (p
) + 2);
1023 sprintf (cache_file
, "%s/%s", canon
, p
);
1027 if (opt_manual_link
)
1029 /* Link all given libraries manually. */
1032 for (i
= remaining
; i
< argc
; ++i
)
1033 manual_link (argv
[i
]);
1039 if (opt_build_cache
)
1042 if (!opt_only_cline
)
1044 /* Always add the standard search paths. */
1046 if (strcmp (SLIBDIR
, LIBDIR
))
1049 parse_conf (config_file
);
1054 if (opt_build_cache
)
1055 save_cache (cache_file
);