Update.
[glibc.git] / elf / ldconfig.c
blob99fe8a93d2828dd0c665f6a628c89464a606915f
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. */
20 #include <argp.h>
21 #include <dirent.h>
22 #include <elf.h>
23 #include <error.h>
24 #include <errno.h>
25 #include <libintl.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <sys/fcntl.h>
31 #include <sys/mman.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
35 #include "ldconfig.h"
36 #include "dl-cache.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"
43 #ifndef LD_SO_CONF
44 # define LD_SO_CONF SYSCONFDIR "/ld.so.conf"
45 #endif
47 /* Get libc version number. */
48 #include <version.h>
50 #define PACKAGE _libc_intl_domainname
52 struct lib_entry
54 int flags;
55 unsigned long int hwcap;
56 char *lib;
57 char *path;
60 static const struct
62 const char *name;
63 int flag;
64 } lib_types [] =
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. */
74 struct dir_entry
76 char *path;
77 int flag;
78 struct dir_entry *next;
81 /* The list is unsorted, contains no duplicates. Entries are added at
82 the end. */
83 static struct dir_entry *dir_entries;
85 /* Flags for different options. */
86 /* Print Cache. */
87 static int opt_print_cache = 0;
89 /* Be verbose. */
90 int opt_verbose = 0;
92 /* Format to support. */
93 /* 0: only libc5/glibc2; 1: both; 2: only glibc 2.2. */
94 int opt_format = 1;
96 /* Build cache. */
97 static int opt_build_cache = 1;
99 /* Generate links. */
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 *)
120 = print_version;
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. */
151 static int
152 is_hwcap (const char *name)
154 int hwcap_idx = _dl_string_hwcap (name);
156 if (hwcap_idx != -1 && ((1 << hwcap_idx) & HWCAP_IMPORTANT))
157 return 1;
158 return 0;
161 /* Get hwcap encoding of path. */
162 static unsigned long int
163 path_hwcap (const char *path)
165 char *str = xstrdup (path);
166 char *ptr;
167 unsigned long int hwcap = 0;
168 unsigned long int h;
170 size_t len;
172 len = strlen (str);
173 if (str[len] == '/')
174 str[len] = '\0';
176 /* Search pathname from the end and check for hwcap strings. */
177 for (;;)
179 ptr = strrchr (str, '/');
181 if (ptr == NULL)
182 break;
184 h = _dl_string_hwcap (ptr+1);
186 if (h == -1)
187 break;
188 hwcap += 1 << h;
190 /* Search the next part of the path. */
191 *ptr = '\0';
194 free (str);
195 return hwcap;
198 /* Handle program arguments. */
199 static error_t
200 parse_opt (int key, char *arg, struct argp_state *state)
202 switch (key)
204 case 'C':
205 cache_file = arg;
206 break;
207 case 'f':
208 config_file = arg;
209 break;
210 case 'l':
211 opt_manual_link = 1;
212 break;
213 case 'N':
214 opt_build_cache = 0;
215 break;
216 case 'n':
217 opt_build_cache = 0;
218 opt_only_cline = 1;
219 break;
220 case 'p':
221 opt_print_cache = 1;
222 break;
223 case 'r':
224 opt_chroot = arg;
225 break;
226 case 'v':
227 opt_verbose = 1;
228 break;
229 case 'X':
230 opt_link = 0;
231 break;
232 case 'c':
233 if (strcmp (arg, "old") == 0)
234 opt_format = 0;
235 else if (strcmp (arg, "compat") == 0)
236 opt_format = 1;
237 else if (strcmp (arg, "new") == 0)
238 opt_format = 2;
239 break;
240 default:
241 return ARGP_ERR_UNKNOWN;
244 return 0;
247 /* Print the version information. */
248 static void
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\
256 "), "2000");
257 fprintf (stream, gettext ("Written by %s.\n"),
258 "Andreas Jaeger");
261 /* Add one directory to the list of directories to process. */
262 static void
263 add_dir (const char *line)
265 char *equal_sign;
266 struct dir_entry *entry, *ptr, *prev;
267 unsigned int i;
269 entry = xmalloc (sizeof (struct dir_entry));
270 entry->next = NULL;
272 /* Search for an '=' sign. */
273 entry->path = xstrdup (line);
274 equal_sign = strchr (entry->path, '=');
275 if (equal_sign)
277 *equal_sign = '\0';
278 ++equal_sign;
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;
284 break;
286 if (entry->flag == FLAG_ANY)
287 error (0, 0, _("%s is not a known library type"), equal_sign);
289 else
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';
299 --i;
302 ptr = dir_entries;
303 prev = ptr;
304 while (ptr != NULL)
306 /* Check for duplicates. */
307 if (strcmp (ptr->path, entry->path) == 0)
309 if (opt_verbose)
310 error (0, 0, _("Path `%s' given more than once"), entry->path);
311 /* Use the newer information. */
312 ptr->flag = entry->flag;
313 free (entry);
314 break;
316 prev = ptr;
317 ptr = ptr->next;
319 /* Is this the first entry? */
320 if (ptr == NULL && dir_entries == NULL)
321 dir_entries = entry;
322 else if (ptr == NULL)
323 prev->next = entry;
327 /* Create a symbolic link from soname to libname in directory path. */
328 static void
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;
333 int do_link = 1;
334 int do_remove = 1;
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);
347 return;
349 if (stat_lib.st_dev == stat_so.st_dev
350 && stat_lib.st_ino == stat_so.st_ino)
351 /* Link is already correct. */
352 do_link = 0;
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);
357 do_link = 0;
358 do_remove = 0;
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. */
364 do_remove = 0;
366 if (opt_verbose)
367 printf ("\t%s -> %s", soname, libname);
369 if (do_link && opt_link)
371 /* Remove old link. */
372 if (do_remove)
373 if (unlink (full_soname))
375 error (0, 0, _("Can't unlink %s"), full_soname);
376 do_link = 0;
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);
382 do_link = 0;
384 if (opt_verbose)
386 if (do_link)
387 fputs (_(" (changed)\n"), stdout);
388 else
389 fputs (_(" (SKIPPED)\n"), stdout);
392 else if (opt_verbose)
393 fputs ("\n", stdout);
396 /* Manually link the given library. */
397 static void
398 manual_link (char *library)
400 char *path;
401 char *libname;
402 char *soname;
403 struct stat stat_buf;
404 int flag;
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, '/');
412 if (libname)
414 /* Successfully split names. Check if path is just "/" to avoid
415 an empty path. */
416 if (libname == path)
418 libname = library + 1;
419 path = xrealloc (path, 2);
420 strcpy (path, "/");
422 else
424 *libname = '\0';
425 ++libname;
428 else
430 /* There's no path, construct one. */
431 libname = library;
432 path = xrealloc (path, 2);
433 strcpy (path, ".");
436 /* Do some sanity checks first. */
437 if (lstat (library, &stat_buf))
439 error (0, errno, _("Can't lstat %s"), library);
440 free (path);
441 return;
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."),
447 library);
448 free (path);
449 return;
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"),
455 library);
456 free (path);
457 return;
459 create_links (path, libname, soname);
460 free (soname);
461 free (path);
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
487 libxy.so.1.1. */
489 /* Information for one library. */
490 struct dlib_entry
492 char *name;
493 char *soname;
494 int flag;
495 int is_link;
496 struct dlib_entry *next;
500 static void
501 search_dir (const struct dir_entry *entry)
503 DIR *dir;
504 struct dirent *direntry;
505 char buf [PATH_MAX];
506 char *soname;
507 struct dlib_entry *dlibs;
508 struct dlib_entry *dlib_ptr;
509 int nchars;
510 struct stat stat_buf;
511 int is_link;
512 unsigned long int hwcap = path_hwcap (entry->path);
514 dlibs = NULL;
516 if (opt_verbose)
518 if (hwcap != 0)
519 printf ("%s: (hwcap: 0x%lx)\n", entry->path, hwcap);
520 else
521 printf ("%s:\n", entry->path);
524 dir = opendir (entry->path);
525 if (dir == NULL)
527 if (opt_verbose)
528 error (0, errno, _("Can't open directory %s"), entry->path);
529 return;
533 while ((direntry = readdir (dir)) != NULL)
535 int flag;
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)
542 continue;
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
546 shared library. */
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))
551 continue;
552 nchars = snprintf (buf, sizeof (buf), "%s/%s", entry->path,
553 direntry->d_name);
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);
559 continue;
561 if (lstat (buf, &stat_buf))
563 error (0, errno, _("Can't lstat %s"), buf);
564 continue;
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);
574 continue;
576 else if (!S_ISREG (stat_buf.st_mode) && !S_ISLNK (stat_buf.st_mode))
577 continue;
579 is_link = S_ISLNK (stat_buf.st_mode);
581 if (process_file (buf, direntry->d_name, &flag, &soname, is_link))
582 continue;
584 /* Links will just point to itself. */
585 if (is_link)
587 free (soname);
588 soname = xstrdup (direntry->d_name);
591 if (flag == FLAG_ELF
592 && (entry->flag == FLAG_ELF_LIBC5
593 || entry->flag == FLAG_ELF_LIBC6))
594 flag = entry->flag;
595 /* Some sanity checks to print warnings. */
596 if (opt_verbose)
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
616 is newer. */
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)
630 && flag == FLAG_ELF)
631 dlib_ptr->flag = flag;
632 else
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. */
642 free (soname);
643 break;
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;
656 dlibs = dlib_ptr;
660 closedir (dir);
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);
669 if (opt_build_cache)
670 add_to_cache (entry->path, dlib_ptr->soname, dlib_ptr->flag, hwcap);
673 /* Free all resources. */
674 while (dlibs)
676 dlib_ptr = dlibs;
677 free (dlib_ptr->soname);
678 free (dlib_ptr->name);
679 dlibs = dlibs->next;
680 free (dlib_ptr);
684 /* Search through all libraries. */
685 static void
686 search_dirs (void)
688 struct dir_entry *entry;
690 for (entry = dir_entries; entry != NULL; entry = entry->next)
691 search_dir (entry);
693 /* Free all allocated memory. */
694 while (dir_entries)
696 entry = dir_entries;
697 dir_entries = dir_entries->next;
698 free (entry->path);
699 free (entry);
704 /* Parse configuration file. */
705 static void
706 parse_conf (const char *filename)
708 FILE *file;
709 char *line = NULL;
710 size_t len = 0;
712 file = fopen (filename, "r");
714 if (file == NULL)
716 error (0, errno, _("Can't open configuration file %s"), filename);
717 return;
722 ssize_t n = getline (&line, &len, file);
723 if (n < 0)
724 break;
726 if (line[n - 1] == '\n')
727 line[n - 1] = '\0';
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. */
735 if (line[0] == '\0')
736 continue;
738 add_dir (line);
739 } while (!feof (file));
741 /* Free buffer and close file. */
742 free (line);
743 fclose (file);
748 main (int argc, char **argv)
750 int remaining;
752 /* Parse and process arguments. */
753 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
755 /* Remaining arguments are additional libraries if opt_manual_link
756 is not set. */
757 if (remaining != argc && !opt_manual_link)
759 int i;
760 for (i = remaining; i < argc; ++i)
761 add_dir (argv [i]);
764 if (cache_file == NULL)
765 cache_file = LD_SO_CACHE;
767 if (config_file == NULL)
768 config_file = LD_SO_CONF;
770 /* Chroot first. */
771 if (opt_chroot)
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. */
777 if (chdir ("/"))
778 error (EXIT_FAILURE, errno, _("Can't chdir to /"));
781 if (opt_print_cache)
783 print_cache (cache_file);
784 exit (0);
787 if (opt_manual_link)
789 /* Link all given libraries manually. */
790 int i;
792 for (i = remaining; i < argc; ++i)
793 manual_link (argv [i]);
795 exit (0);
799 if (opt_build_cache)
800 init_cache ();
802 if (!opt_only_cline)
804 /* Always add the standard search paths. */
805 add_dir (SLIBDIR);
806 if (strcmp (SLIBDIR, LIBDIR))
807 add_dir (LIBDIR);
809 parse_conf (config_file);
812 search_dirs ();
814 if (opt_build_cache)
815 save_cache (cache_file);
817 return 0;