1 /* Copyright (C) 1999, 2000 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. */
30 #include <sys/fcntl.h>
33 #include <sys/types.h>
38 /* We don't need this here - silence the compiler. */
39 #define _dl_sysdep_message(string, args...) do {} while (0);
41 #include "dl-procinfo.h"
44 # define LD_SO_CONF SYSCONFDIR "/ld.so.conf"
47 /* Get libc version number. */
50 #define PACKAGE _libc_intl_domainname
55 unsigned long int hwcap
;
66 {"libc4", FLAG_LIBC4
},
67 {"libc5", FLAG_ELF_LIBC5
},
68 {"libc6", FLAG_ELF_LIBC6
},
69 {"glibc2", FLAG_ELF_LIBC6
}
73 /* List of directories to handle. */
78 struct dir_entry
*next
;
81 /* The list is unsorted, contains no duplicates. Entries are added at
83 static struct dir_entry
*dir_entries
;
85 /* Flags for different options. */
87 static int opt_print_cache
= 0;
92 /* Format to support. */
93 /* 0: only libc5/glibc2; 1: both; 2: only glibc 2.2. */
97 static int opt_build_cache
= 1;
100 static int opt_link
= 1;
102 /* Only process directories specified on the command line. */
103 static int opt_only_cline
= 0;
105 /* Path to root for chroot. */
106 static char *opt_chroot
;
108 /* Manually link given shared libraries. */
109 static int opt_manual_link
= 0;
111 /* Cache file to use. */
112 static const char *cache_file
;
114 /* Configuration file. */
115 static const char *config_file
;
117 /* Name and version of program. */
118 static void print_version (FILE *stream
, struct argp_state
*state
);
119 void (*argp_program_version_hook
) (FILE *, struct argp_state
*)
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 generate links"), 0},
129 { NULL
, 'r', "ROOT", 0, N_("Change to and use ROOT as root directory"), 0},
130 { NULL
, 'C', "CACHE", 0, N_("Use CACHE as cache file"), 0},
131 { NULL
, 'f', "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', "FORMAT", 0, N_("Format to use: new, old or compat (default)"), 0},
135 { NULL
, 0, NULL
, 0, NULL
, 0 }
138 /* Short description of program. */
139 static const char doc
[] = N_("Configure Dynamic Linker Run Time Bindings.");
141 /* Prototype for option handler. */
142 static error_t
parse_opt (int key
, char *arg
, struct argp_state
*state
);
144 /* Data structure to communicate with argp functions. */
145 static struct argp argp
=
147 options
, parse_opt
, NULL
, doc
, NULL
, NULL
, NULL
150 /* Check if string corresponds to an important hardware capability. */
152 is_hwcap (const char *name
)
154 int hwcap_idx
= _dl_string_hwcap (name
);
156 if (hwcap_idx
!= -1 && ((1 << hwcap_idx
) & HWCAP_IMPORTANT
))
161 /* Get hwcap encoding of path. */
162 static unsigned long int
163 path_hwcap (const char *path
)
165 char *str
= xstrdup (path
);
167 unsigned long int hwcap
= 0;
176 /* Search pathname from the end and check for hwcap strings. */
179 ptr
= strrchr (str
, '/');
184 h
= _dl_string_hwcap (ptr
+1);
190 /* Search the next part of the path. */
198 /* Handle program arguments. */
200 parse_opt (int key
, char *arg
, struct argp_state
*state
)
233 if (strcmp (arg
, "old") == 0)
235 else if (strcmp (arg
, "compat") == 0)
237 else if (strcmp (arg
, "new") == 0)
241 return ARGP_ERR_UNKNOWN
;
247 /* Print the version information. */
249 print_version (FILE *stream
, struct argp_state
*state
)
251 fprintf (stream
, "ldconfig (GNU %s) %s\n", PACKAGE
, VERSION
);
252 fprintf (stream
, gettext ("\
253 Copyright (C) %s Free Software Foundation, Inc.\n\
254 This is free software; see the source for copying conditions. There is NO\n\
255 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
257 fprintf (stream
, gettext ("Written by %s.\n"),
261 /* Add one directory to the list of directories to process. */
263 add_dir (const char *line
)
266 struct dir_entry
*entry
, *ptr
, *prev
;
269 entry
= xmalloc (sizeof (struct dir_entry
));
272 /* Search for an '=' sign. */
273 entry
->path
= xstrdup (line
);
274 equal_sign
= strchr (entry
->path
, '=');
279 entry
->flag
= FLAG_ANY
;
280 for (i
= 0; i
< sizeof (lib_types
) / sizeof (lib_types
[0]); ++i
)
281 if (strcmp (equal_sign
, lib_types
[i
].name
) == 0)
283 entry
->flag
= lib_types
[i
].flag
;
286 if (entry
->flag
== FLAG_ANY
)
287 error (0, 0, _("%s is not a known library type"), equal_sign
);
291 entry
->flag
= FLAG_ANY
;
294 /* Canonify path: for now only remove trailing slashes. */
295 i
= strlen (entry
->path
) - 1;
296 while (entry
->path
[i
] == '/' && i
> 0)
298 entry
->path
[i
] = '\0';
306 /* Check for duplicates. */
307 if (strcmp (ptr
->path
, entry
->path
) == 0)
310 error (0, 0, _("Path `%s' given more than once"), entry
->path
);
311 /* Use the newer information. */
312 ptr
->flag
= entry
->flag
;
319 /* Is this the first entry? */
320 if (ptr
== NULL
&& dir_entries
== NULL
)
322 else if (ptr
== NULL
)
327 /* Create a symbolic link from soname to libname in directory path. */
329 create_links (const char *path
, const char *libname
, const char *soname
)
331 char full_libname
[PATH_MAX
], full_soname
[PATH_MAX
];
332 struct stat stat_lib
, stat_so
, lstat_so
;
335 /* XXX: The logics in this function should be simplified. */
337 /* Get complete path. */
338 snprintf (full_libname
, sizeof full_libname
, "%s/%s", path
, libname
);
339 snprintf (full_soname
, sizeof full_soname
, "%s/%s", path
, soname
);
341 /* Does soname already exist and point to the right library? */
342 if (stat (full_soname
, &stat_so
) == 0)
344 if (stat (full_libname
, &stat_lib
))
346 error (0, 0, _("Can't stat %s\n"), full_libname
);
349 if (stat_lib
.st_dev
== stat_so
.st_dev
350 && stat_lib
.st_ino
== stat_so
.st_ino
)
351 /* Link is already correct. */
353 else if (lstat (full_soname
, &lstat_so
) == 0
354 && !S_ISLNK (lstat_so
.st_mode
))
356 error (0, 0, _("%s is not a symbolic link\n"), full_soname
);
361 else if (lstat (full_soname
, &lstat_so
) != 0
362 || !S_ISLNK (lstat_so
.st_mode
))
363 /* Unless it is a stale symlink, there is no need to remove. */
367 printf ("\t%s -> %s", soname
, libname
);
369 if (do_link
&& opt_link
)
371 /* Remove old link. */
373 if (unlink (full_soname
))
375 error (0, 0, _("Can't unlink %s"), full_soname
);
378 /* Create symbolic link. */
379 if (do_link
&& symlink (libname
, full_soname
))
381 error (0, 0, _("Can't link %s to %s"), full_soname
, libname
);
387 fputs (_(" (changed)\n"), stdout
);
389 fputs (_(" (SKIPPED)\n"), stdout
);
392 else if (opt_verbose
)
393 fputs ("\n", stdout
);
396 /* Manually link the given library. */
398 manual_link (char *library
)
403 struct stat stat_buf
;
406 /* Prepare arguments for create_links call. Split library name in
407 directory and filename first. Since path is allocated, we've got
408 to be careful to free at the end. */
409 path
= xstrdup (library
);
410 libname
= strrchr (path
, '/');
414 /* Successfully split names. Check if path is just "/" to avoid
418 libname
= library
+ 1;
419 path
= xrealloc (path
, 2);
430 /* There's no path, construct one. */
432 path
= xrealloc (path
, 2);
436 /* Do some sanity checks first. */
437 if (lstat (library
, &stat_buf
))
439 error (0, errno
, _("Can't lstat %s"), library
);
443 /* We don't want links here! */
444 else if (!S_ISREG (stat_buf
.st_mode
))
446 error (0, 0, _("Ignored file %s since it is not a regular file."),
451 libname
= basename (library
);
452 if (process_file (library
, libname
, &flag
, &soname
, 0))
454 error (0, 0, _("No link created since soname could not be found for %s"),
459 create_links (path
, libname
, soname
);
465 /* Read a whole directory and search for libraries.
466 The purpose is two-fold:
467 - search for libraries which will be added to the cache
468 - create symbolic links to the soname for each library
470 This has to be done separatly for each directory.
472 To keep track of which libraries to add to the cache and which
473 links to create, we save a list of all libraries.
475 The algorithm is basically:
476 for all libraries in the directory do
477 get soname of library
478 if soname is already in list
479 if new library is newer, replace entry
480 otherwise ignore this library
481 otherwise add library to list
483 For example, if the two libraries libxy.so.1.1 and libxy.so.1.2
484 exist and both have the same soname, e.g. libxy.so, a symbolic link
485 is created from libxy.so.1.2 (the newer one) to libxy.so.
486 libxy.so.1.2 and libxy.so are added to the cache - but not
489 /* Information for one library. */
496 struct dlib_entry
*next
;
501 search_dir (const struct dir_entry
*entry
)
504 struct dirent
*direntry
;
507 struct dlib_entry
*dlibs
;
508 struct dlib_entry
*dlib_ptr
;
510 struct stat stat_buf
;
512 unsigned long int hwcap
= path_hwcap (entry
->path
);
519 printf ("%s: (hwcap: 0x%lx)\n", entry
->path
, hwcap
);
521 printf ("%s:\n", entry
->path
);
524 dir
= opendir (entry
->path
);
528 error (0, errno
, _("Can't open directory %s"), entry
->path
);
533 while ((direntry
= readdir (dir
)) != NULL
)
536 #ifdef _DIRENT_HAVE_D_TYPE
537 /* We only look at links and regular files. */
538 if (direntry
->d_type
!= DT_UNKNOWN
539 && direntry
->d_type
!= DT_LNK
540 && direntry
->d_type
!= DT_REG
541 && direntry
->d_type
!= DT_DIR
)
543 #endif /* _DIRENT_HAVE_D_TYPE */
544 /* Does this file look like a shared library or is it a hwcap
545 subdirectory? The dynamic linker is also considered as
547 if (((strncmp (direntry
->d_name
, "lib", 3) != 0
548 && strncmp (direntry
->d_name
, "ld-", 3) != 0)
549 || strstr (direntry
->d_name
, ".so") == NULL
)
550 && !is_hwcap (direntry
->d_name
))
552 nchars
= snprintf (buf
, sizeof (buf
), "%s/%s", entry
->path
,
554 /* Check for overflow. */
555 if (nchars
>= (int) sizeof (buf
))
557 error (0, 0, _("buffer for snprintf too small for %s/%s--file is ignored\n"),
558 entry
->path
, direntry
->d_name
);
561 if (lstat (buf
, &stat_buf
))
563 error (0, errno
, _("Can't lstat %s"), buf
);
566 else if (S_ISDIR (stat_buf
.st_mode
) && is_hwcap (direntry
->d_name
))
568 /* Handle subdirectory also, make a recursive call. */
569 struct dir_entry new_entry
;
570 new_entry
.path
= buf
;
571 new_entry
.flag
= entry
->flag
;
572 new_entry
.next
= NULL
;
573 search_dir (&new_entry
);
576 else if (!S_ISREG (stat_buf
.st_mode
) && !S_ISLNK (stat_buf
.st_mode
))
579 is_link
= S_ISLNK (stat_buf
.st_mode
);
581 if (process_file (buf
, direntry
->d_name
, &flag
, &soname
, is_link
))
584 /* Links will just point to itself. */
588 soname
= xstrdup (direntry
->d_name
);
592 && (entry
->flag
== FLAG_ELF_LIBC5
593 || entry
->flag
== FLAG_ELF_LIBC6
))
595 /* Some sanity checks to print warnings. */
598 if (flag
== FLAG_ELF_LIBC5
&& entry
->flag
!= FLAG_ELF_LIBC5
599 && entry
->flag
!= FLAG_ANY
)
600 error (0, 0, _("libc5 library %s in wrong directory"), buf
);
601 if (flag
== FLAG_ELF_LIBC6
&& entry
->flag
!= FLAG_ELF_LIBC6
602 && entry
->flag
!= FLAG_ANY
)
603 error (0, 0, _("libc6 library %s in wrong directory"), buf
);
604 if (flag
== FLAG_LIBC4
&& entry
->flag
!= FLAG_LIBC4
605 && entry
->flag
!= FLAG_ANY
)
606 error (0, 0, _("libc4 library %s in wrong directory"), buf
);
609 /* Add library to list. */
610 for (dlib_ptr
= dlibs
; dlib_ptr
!= NULL
; dlib_ptr
= dlib_ptr
->next
)
612 /* Is soname already in list? */
613 if (strcmp (dlib_ptr
->soname
, soname
) == 0)
615 /* Prefer a file to a link, otherwise check which one
617 if ((!is_link
&& dlib_ptr
->is_link
)
618 || (is_link
== dlib_ptr
->is_link
619 && _dl_cache_libcmp (dlib_ptr
->name
, direntry
->d_name
) < 0))
621 /* It's newer - add it. */
622 /* Flag should be the same - sanity check. */
623 if (dlib_ptr
->flag
!= flag
)
625 if (dlib_ptr
->flag
== FLAG_ELF
626 && (flag
== FLAG_ELF_LIBC5
|| flag
== FLAG_ELF_LIBC6
))
627 dlib_ptr
->flag
= flag
;
628 else if ((dlib_ptr
->flag
== FLAG_ELF_LIBC5
629 || dlib_ptr
->flag
== FLAG_ELF_LIBC6
)
631 dlib_ptr
->flag
= flag
;
633 error (0, 0, _("libraries %s and %s in directory %s have same soname but different type."),
634 dlib_ptr
->name
, direntry
->d_name
, entry
->path
);
636 free (dlib_ptr
->name
);
637 dlib_ptr
->name
= xstrdup (direntry
->d_name
);
638 dlib_ptr
->is_link
= is_link
;
640 /* Don't add this library, abort loop. */
641 /* Also free soname, since it's dynamically allocated. */
646 /* Add the library if it's not already in. */
647 if (dlib_ptr
== NULL
)
649 dlib_ptr
= (struct dlib_entry
*)xmalloc (sizeof (struct dlib_entry
));
650 dlib_ptr
->name
= xstrdup (direntry
->d_name
);
651 dlib_ptr
->flag
= flag
;
652 dlib_ptr
->soname
= soname
;
653 dlib_ptr
->is_link
= is_link
;
654 /* Add at head of list. */
655 dlib_ptr
->next
= dlibs
;
662 /* Now dlibs contains a list of all libs - add those to the cache
663 and created all symbolic links. */
664 for (dlib_ptr
= dlibs
; dlib_ptr
!= NULL
; dlib_ptr
= dlib_ptr
->next
)
666 /* Don't create links to links. */
667 if (dlib_ptr
->is_link
== 0)
668 create_links (entry
->path
, dlib_ptr
->name
, dlib_ptr
->soname
);
670 add_to_cache (entry
->path
, dlib_ptr
->soname
, dlib_ptr
->flag
, hwcap
);
673 /* Free all resources. */
677 free (dlib_ptr
->soname
);
678 free (dlib_ptr
->name
);
684 /* Search through all libraries. */
688 struct dir_entry
*entry
;
690 for (entry
= dir_entries
; entry
!= NULL
; entry
= entry
->next
)
693 /* Free all allocated memory. */
697 dir_entries
= dir_entries
->next
;
704 /* Parse configuration file. */
706 parse_conf (const char *filename
)
712 file
= fopen (filename
, "r");
716 error (0, errno
, _("Can't open configuration file %s"), filename
);
722 ssize_t n
= getline (&line
, &len
, file
);
726 if (line
[n
- 1] == '\n')
729 /* Because the file format does not know any form of quoting we
730 can search forward for the next '#' character and if found
731 make it terminating the line. */
732 *strchrnul (line
, '#') = '\0';
734 /* If the line is blank it is ignored. */
739 } while (!feof (file
));
741 /* Free buffer and close file. */
748 main (int argc
, char **argv
)
752 /* Parse and process arguments. */
753 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
755 /* Remaining arguments are additional libraries if opt_manual_link
757 if (remaining
!= argc
&& !opt_manual_link
)
760 for (i
= remaining
; i
< argc
; ++i
)
764 if (cache_file
== NULL
)
765 cache_file
= LD_SO_CACHE
;
767 if (config_file
== NULL
)
768 config_file
= LD_SO_CONF
;
773 if (chroot (opt_chroot
))
774 /* Report failure and exit program. */
775 error (EXIT_FAILURE
, errno
, _("Can't chroot to %s"), opt_chroot
);
776 /* chroot doesn't change the working directory, let's play safe. */
778 error (EXIT_FAILURE
, errno
, _("Can't chdir to /"));
783 print_cache (cache_file
);
789 /* Link all given libraries manually. */
792 for (i
= remaining
; i
< argc
; ++i
)
793 manual_link (argv
[i
]);
804 /* Always add the standard search paths. */
806 if (strcmp (SLIBDIR
, LIBDIR
))
809 parse_conf (config_file
);
815 save_cache (cache_file
);