Update.
[glibc.git] / elf / ldconfig.c
blobd13eea01abdb0c0074d6a9a81c5383bfb1ef7aa9
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. */
20 #include <alloca.h>
21 #include <argp.h>
22 #include <dirent.h>
23 #include <elf.h>
24 #include <error.h>
25 #include <errno.h>
26 #include <inttypes.h>
27 #include <libintl.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <sys/fcntl.h>
33 #include <sys/mman.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
37 #include "ldconfig.h"
38 #include "dl-cache.h"
40 #include "dl-procinfo.h"
42 #ifndef LD_SO_CONF
43 # define LD_SO_CONF SYSCONFDIR "/ld.so.conf"
44 #endif
46 /* Get libc version number. */
47 #include <version.h>
49 #define PACKAGE _libc_intl_domainname
51 static const struct
53 const char *name;
54 int flag;
55 } lib_types[] =
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. */
65 struct dir_entry
67 char *path;
68 int flag;
69 ino64_t ino;
70 dev_t dev;
71 struct dir_entry *next;
74 /* The list is unsorted, contains no duplicates. Entries are added at
75 the end. */
76 static struct dir_entry *dir_entries;
78 /* Flags for different options. */
79 /* Print Cache. */
80 static int opt_print_cache = 0;
82 /* Be verbose. */
83 int opt_verbose = 0;
85 /* Format to support. */
86 /* 0: only libc5/glibc2; 1: both; 2: only glibc 2.2. */
87 int opt_format = 1;
89 /* Build cache. */
90 static int opt_build_cache = 1;
92 /* Generate links. */
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 *)
113 = print_version;
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
144 a platform. */
145 static int
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))
151 return 1;
153 hwcap_idx = _dl_string_platform (name);
154 if (hwcap_idx != -1)
155 return 1;
157 return 0;
160 /* Get hwcap (including platform) encoding of path. */
161 static uint64_t
162 path_hwcap (const char *path)
164 char *str = xstrdup (path);
165 char *ptr;
166 uint64_t hwcap = 0;
167 uint64_t h;
169 size_t len;
171 len = strlen (str);
172 if (str[len] == '/')
173 str[len] = '\0';
175 /* Search pathname from the end and check for hwcap strings. */
176 for (;;)
178 ptr = strrchr (str, '/');
180 if (ptr == NULL)
181 break;
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)
189 break;
191 hwcap += 1ULL << h;
193 /* Search the next part of the path. */
194 *ptr = '\0';
197 free (str);
198 return hwcap;
201 /* Handle program arguments. */
202 static error_t
203 parse_opt (int key, char *arg, struct argp_state *state)
205 switch (key)
207 case 'C':
208 cache_file = arg;
209 break;
210 case 'f':
211 config_file = arg;
212 break;
213 case 'l':
214 opt_manual_link = 1;
215 break;
216 case 'N':
217 opt_build_cache = 0;
218 break;
219 case 'n':
220 opt_build_cache = 0;
221 opt_only_cline = 1;
222 break;
223 case 'p':
224 opt_print_cache = 1;
225 break;
226 case 'r':
227 opt_chroot = arg;
228 break;
229 case 'v':
230 opt_verbose = 1;
231 break;
232 case 'X':
233 opt_link = 0;
234 break;
235 case 'c':
236 if (strcmp (arg, "old") == 0)
237 opt_format = 0;
238 else if (strcmp (arg, "compat") == 0)
239 opt_format = 1;
240 else if (strcmp (arg, "new") == 0)
241 opt_format = 2;
242 break;
243 default:
244 return ARGP_ERR_UNKNOWN;
247 return 0;
250 /* Print the version information. */
251 static void
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\
259 "), "2001");
260 fprintf (stream, gettext ("Written by %s.\n"),
261 "Andreas Jaeger");
264 /* Add a single directory entry. */
265 static void
266 add_single_dir (struct dir_entry *entry, int verbose)
268 struct dir_entry *ptr, *prev;
270 ptr = dir_entries;
271 prev = ptr;
272 while (ptr != NULL)
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;
281 free (entry->path);
282 free (entry);
283 break;
285 prev = ptr;
286 ptr = ptr->next;
288 /* Is this the first entry? */
289 if (ptr == NULL && dir_entries == NULL)
290 dir_entries = entry;
291 else if (ptr == NULL)
292 prev->next = entry;
295 /* Add one directory to the list of directories to process. */
296 static void
297 add_dir (const char *line)
299 char *equal_sign;
300 struct dir_entry *entry;
301 unsigned int i;
302 struct stat64 stat_buf;
304 entry = xmalloc (sizeof (struct dir_entry));
305 entry->next = NULL;
307 /* Search for an '=' sign. */
308 entry->path = xstrdup (line);
309 equal_sign = strchr (entry->path, '=');
310 if (equal_sign)
312 *equal_sign = '\0';
313 ++equal_sign;
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;
319 break;
321 if (entry->flag == FLAG_ANY)
322 error (0, 0, _("%s is not a known library type"), equal_sign);
324 else
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';
334 --i;
337 if (stat64 (entry->path, &stat_buf))
339 error (0, errno, _("Can't stat %s"), entry->path);
340 free (entry->path);
341 free (entry);
342 return;
345 entry->ino = stat_buf.st_ino;
346 entry->dev = stat_buf.st_dev;
348 add_single_dir (entry, 1);
352 static int
353 chroot_stat (const char *real_path, const char *path, struct stat64 *st)
355 int ret;
356 char *canon_path;
358 if (!opt_chroot)
359 return stat64 (real_path, st);
361 ret = lstat64 (real_path, st);
362 if (ret || !S_ISLNK (st->st_mode))
363 return ret;
365 canon_path = chroot_canon (opt_chroot, path);
366 if (canon_path == NULL)
367 return -1;
369 ret = stat64 (canon_path, st);
370 free (canon_path);
371 return ret;
374 /* Create a symbolic link from soname to libname in directory path. */
375 static void
376 create_links (const char *real_path, const char *path, const char *libname,
377 const char *soname)
379 char *full_libname, *full_soname;
380 char *real_full_libname, *real_full_soname;
381 struct stat64 stat_lib, stat_so, lstat_so;
382 int do_link = 1;
383 int do_remove = 1;
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);
391 if (opt_chroot)
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);
398 else
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);
410 return;
412 if (stat_lib.st_dev == stat_so.st_dev
413 && stat_lib.st_ino == stat_so.st_ino)
414 /* Link is already correct. */
415 do_link = 0;
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);
420 do_link = 0;
421 do_remove = 0;
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. */
427 do_remove = 0;
429 if (opt_verbose)
430 printf ("\t%s -> %s", soname, libname);
432 if (do_link && opt_link)
434 /* Remove old link. */
435 if (do_remove)
436 if (unlink (real_full_soname))
438 error (0, 0, _("Can't unlink %s"), full_soname);
439 do_link = 0;
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);
445 do_link = 0;
447 if (opt_verbose)
449 if (do_link)
450 fputs (_(" (changed)\n"), stdout);
451 else
452 fputs (_(" (SKIPPED)\n"), stdout);
455 else if (opt_verbose)
456 fputs ("\n", stdout);
459 /* Manually link the given library. */
460 static void
461 manual_link (char *library)
463 char *path;
464 char *real_path;
465 char *real_library;
466 char *libname;
467 char *soname;
468 struct stat64 stat_buf;
469 int flag;
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, '/');
478 if (libname)
480 /* Successfully split names. Check if path is just "/" to avoid
481 an empty path. */
482 if (libname == path)
484 libname = library + 1;
485 path = xrealloc (path, 2);
486 strcpy (path, "/");
488 else
490 *libname = '\0';
491 ++libname;
494 else
496 /* There's no path, construct one. */
497 libname = library;
498 path = xrealloc (path, 2);
499 strcpy (path, ".");
502 if (opt_chroot)
504 real_path = chroot_canon (opt_chroot, path);
505 if (real_path == NULL)
507 error (0, errno, _("Can't find %s"), path);
508 free (path);
509 return;
511 real_library = alloca (strlen (real_path) + strlen (libname) + 2);
512 sprintf (real_library, "%s/%s", real_path, libname);
514 else
516 real_path = path;
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);
524 free (path);
525 return;
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."),
531 library);
532 free (path);
533 return;
535 if (process_file (real_library, library, libname, &flag, &osversion,
536 &soname, 0))
538 error (0, 0, _("No link created since soname could not be found for %s"),
539 library);
540 free (path);
541 return;
543 create_links (real_path, path, libname, soname);
544 free (soname);
545 free (path);
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
571 libxy.so.1.1. */
573 /* Information for one library. */
574 struct dlib_entry
576 char *name;
577 char *soname;
578 int flag;
579 int is_link;
580 unsigned int osversion;
581 struct dlib_entry *next;
585 static void
586 search_dir (const struct dir_entry *entry)
588 DIR *dir;
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;
592 char *soname;
593 struct dlib_entry *dlibs;
594 struct dlib_entry *dlib_ptr;
595 struct stat64 lstat_buf, stat_buf;
596 int is_link, is_dir;
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);
603 dlibs = NULL;
605 if (opt_verbose)
607 if (hwcap != 0)
608 printf ("%s: (hwcap: 0x%" PRIx64 ")\n", entry->path, hwcap);
609 else
610 printf ("%s:\n", entry->path);
613 if (opt_chroot)
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);
619 else
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)
628 if (opt_verbose)
629 error (0, errno, _("Can't open directory %s"), entry->path);
630 if (opt_chroot && dir_name)
631 free (dir_name);
632 return;
635 while ((direntry = readdir (dir)) != NULL)
637 int flag;
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)
644 continue;
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
648 shared library. */
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))
653 continue;
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);
659 if (!opt_chroot)
660 real_file_name = file_name;
662 sprintf (file_name, "%s/%s", entry->path, direntry->d_name);
663 if (opt_chroot)
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);
676 else
677 #endif
678 if (lstat64 (real_file_name, &lstat_buf))
680 error (0, errno, _("Can't lstat %s"), file_name);
681 continue;
684 is_link = S_ISLNK (lstat_buf.st_mode);
685 if (is_link)
687 /* In case of symlink, we check if the symlink refers to
688 a directory. */
689 if (stat64 (real_file_name, &stat_buf))
691 error (0, errno, _("Can't stat %s"), file_name);
692 continue;
694 is_dir = S_ISDIR (stat_buf.st_mode);
696 else
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;
708 if (is_link)
710 new_entry->ino = stat_buf.st_ino;
711 new_entry->dev = stat_buf.st_dev;
713 else
715 new_entry->ino = lstat_buf.st_ino;
716 new_entry->dev = lstat_buf.st_dev;
718 add_single_dir (new_entry, 0);
719 continue;
721 else if (!S_ISREG (stat_buf.st_mode) && !is_link)
722 continue;
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);
731 continue;
734 else
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)
741 free (real_name);
742 continue;
745 if (real_name != real_file_name)
746 free (real_name);
748 /* Links will just point to itself. */
749 if (is_link)
751 free (soname);
752 soname = xstrdup (direntry->d_name);
755 if (flag == FLAG_ELF
756 && (entry->flag == FLAG_ELF_LIBC5
757 || entry->flag == FLAG_ELF_LIBC6))
758 flag = entry->flag;
759 /* Some sanity checks to print warnings. */
760 if (opt_verbose)
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
780 is newer. */
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)
794 && flag == FLAG_ELF)
795 dlib_ptr->flag = flag;
796 else
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. */
807 free (soname);
808 break;
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;
822 dlibs = dlib_ptr;
826 closedir (dir);
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,
835 dlib_ptr->soname);
836 if (opt_build_cache)
837 add_to_cache (entry->path, dlib_ptr->soname, dlib_ptr->flag,
838 dlib_ptr->osversion, hwcap);
841 /* Free all resources. */
842 while (dlibs)
844 dlib_ptr = dlibs;
845 free (dlib_ptr->soname);
846 free (dlib_ptr->name);
847 dlibs = dlibs->next;
848 free (dlib_ptr);
851 if (opt_chroot && dir_name)
852 free (dir_name);
855 /* Search through all libraries. */
856 static void
857 search_dirs (void)
859 struct dir_entry *entry;
861 for (entry = dir_entries; entry != NULL; entry = entry->next)
862 search_dir (entry);
864 /* Free all allocated memory. */
865 while (dir_entries)
867 entry = dir_entries;
868 dir_entries = dir_entries->next;
869 free (entry->path);
870 free (entry);
875 /* Parse configuration file. */
876 static void
877 parse_conf (const char *filename)
879 FILE *file = NULL;
880 char *line = NULL;
881 const char *canon;
882 size_t len = 0;
884 if (opt_chroot)
886 canon = chroot_canon (opt_chroot, filename);
887 if (canon)
888 file = fopen (canon, "r");
889 else
890 canon = filename;
892 else
894 canon = filename;
895 file = fopen (filename, "r");
898 if (file == NULL)
900 error (0, errno, _("Can't open configuration file %s"), canon);
901 if (canon != filename)
902 free ((char *) canon);
903 return;
906 if (canon != filename)
907 free ((char *) canon);
911 ssize_t n = getline (&line, &len, file);
912 if (n < 0)
913 break;
915 if (line[n - 1] == '\n')
916 line[n - 1] = '\0';
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. */
924 if (line[0] == '\0')
925 continue;
927 add_dir (line);
928 } while (!feof (file));
930 /* Free buffer and close file. */
931 free (line);
932 fclose (file);
937 main (int argc, char **argv)
939 int remaining;
941 /* Parse and process arguments. */
942 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
944 /* Remaining arguments are additional libraries if opt_manual_link
945 is not set. */
946 if (remaining != argc && !opt_manual_link)
948 int i;
949 for (i = remaining; i < argc; ++i)
950 add_dir (argv[i]);
953 if (opt_chroot)
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] == '/')
958 --endp;
959 *endp = '\0';
960 if (endp == opt_chroot)
961 opt_chroot = NULL;
963 if (opt_chroot)
965 /* It is faster to use chroot if we can. */
966 if (!chroot (opt_chroot))
968 if (chdir ("/"))
969 error (EXIT_FAILURE, errno, _("Can't chdir to /"));
970 opt_chroot = NULL;
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;
984 if (opt_print_cache)
986 if (opt_chroot)
988 char *p = chroot_canon (opt_chroot, cache_file);
989 if (p == NULL)
990 error (EXIT_FAILURE, errno, _("Can't open cache file %s\n"),
991 cache_file);
992 cache_file = p;
994 print_cache (cache_file);
995 if (opt_chroot)
996 free (cache_file);
997 exit (0);
1000 if (opt_chroot)
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) : "/");
1008 if (canon == NULL)
1010 error (EXIT_FAILURE, errno,
1011 _("Can't open cache file directory %s\n"),
1012 p ? cache_file : "/");
1015 if (p)
1016 ++p;
1017 else
1018 p = cache_file;
1020 cache_file = alloca (strlen (canon) + strlen (p) + 2);
1021 sprintf (cache_file, "%s/%s", canon, p);
1022 free (canon);
1025 if (opt_manual_link)
1027 /* Link all given libraries manually. */
1028 int i;
1030 for (i = remaining; i < argc; ++i)
1031 manual_link (argv[i]);
1033 exit (0);
1037 if (opt_build_cache)
1038 init_cache ();
1040 if (!opt_only_cline)
1042 /* Always add the standard search paths. */
1043 add_dir (SLIBDIR);
1044 if (strcmp (SLIBDIR, LIBDIR))
1045 add_dir (LIBDIR);
1047 parse_conf (config_file);
1050 search_dirs ();
1052 if (opt_build_cache)
1053 save_cache (cache_file);
1055 return 0;