Update.
[glibc.git] / elf / ldconfig.c
blob519baaa626f250a01f17250ec1f88819e9a31c5c
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 if (opt_verbose)
340 error (0, errno, _("Can't stat %s"), entry->path);
341 free (entry->path);
342 free (entry);
343 return;
346 entry->ino = stat_buf.st_ino;
347 entry->dev = stat_buf.st_dev;
349 add_single_dir (entry, 1);
353 static int
354 chroot_stat (const char *real_path, const char *path, struct stat64 *st)
356 int ret;
357 char *canon_path;
359 if (!opt_chroot)
360 return stat64 (real_path, st);
362 ret = lstat64 (real_path, st);
363 if (ret || !S_ISLNK (st->st_mode))
364 return ret;
366 canon_path = chroot_canon (opt_chroot, path);
367 if (canon_path == NULL)
368 return -1;
370 ret = stat64 (canon_path, st);
371 free (canon_path);
372 return ret;
375 /* Create a symbolic link from soname to libname in directory path. */
376 static void
377 create_links (const char *real_path, const char *path, const char *libname,
378 const char *soname)
380 char *full_libname, *full_soname;
381 char *real_full_libname, *real_full_soname;
382 struct stat64 stat_lib, stat_so, lstat_so;
383 int do_link = 1;
384 int do_remove = 1;
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);
392 if (opt_chroot)
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);
399 else
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);
411 return;
413 if (stat_lib.st_dev == stat_so.st_dev
414 && stat_lib.st_ino == stat_so.st_ino)
415 /* Link is already correct. */
416 do_link = 0;
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);
421 do_link = 0;
422 do_remove = 0;
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. */
428 do_remove = 0;
430 if (opt_verbose)
431 printf ("\t%s -> %s", soname, libname);
433 if (do_link && opt_link)
435 /* Remove old link. */
436 if (do_remove)
437 if (unlink (real_full_soname))
439 error (0, 0, _("Can't unlink %s"), full_soname);
440 do_link = 0;
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);
446 do_link = 0;
448 if (opt_verbose)
450 if (do_link)
451 fputs (_(" (changed)\n"), stdout);
452 else
453 fputs (_(" (SKIPPED)\n"), stdout);
456 else if (opt_verbose)
457 fputs ("\n", stdout);
460 /* Manually link the given library. */
461 static void
462 manual_link (char *library)
464 char *path;
465 char *real_path;
466 char *real_library;
467 char *libname;
468 char *soname;
469 struct stat64 stat_buf;
470 int flag;
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, '/');
479 if (libname)
481 /* Successfully split names. Check if path is just "/" to avoid
482 an empty path. */
483 if (libname == path)
485 libname = library + 1;
486 path = xrealloc (path, 2);
487 strcpy (path, "/");
489 else
491 *libname = '\0';
492 ++libname;
495 else
497 /* There's no path, construct one. */
498 libname = library;
499 path = xrealloc (path, 2);
500 strcpy (path, ".");
503 if (opt_chroot)
505 real_path = chroot_canon (opt_chroot, path);
506 if (real_path == NULL)
508 error (0, errno, _("Can't find %s"), path);
509 free (path);
510 return;
512 real_library = alloca (strlen (real_path) + strlen (libname) + 2);
513 sprintf (real_library, "%s/%s", real_path, libname);
515 else
517 real_path = path;
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);
525 free (path);
526 return;
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."),
532 library);
533 free (path);
534 return;
536 if (process_file (real_library, library, libname, &flag, &osversion,
537 &soname, 0))
539 error (0, 0, _("No link created since soname could not be found for %s"),
540 library);
541 free (path);
542 return;
544 create_links (real_path, path, libname, soname);
545 free (soname);
546 free (path);
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
572 libxy.so.1.1. */
574 /* Information for one library. */
575 struct dlib_entry
577 char *name;
578 char *soname;
579 int flag;
580 int is_link;
581 unsigned int osversion;
582 struct dlib_entry *next;
586 static void
587 search_dir (const struct dir_entry *entry)
589 DIR *dir;
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;
593 char *soname;
594 struct dlib_entry *dlibs;
595 struct dlib_entry *dlib_ptr;
596 struct stat64 lstat_buf, stat_buf;
597 int is_link, is_dir;
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);
604 dlibs = NULL;
606 if (opt_verbose)
608 if (hwcap != 0)
609 printf ("%s: (hwcap: 0x%" PRIx64 ")\n", entry->path, hwcap);
610 else
611 printf ("%s:\n", entry->path);
614 if (opt_chroot)
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);
620 else
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)
629 if (opt_verbose)
630 error (0, errno, _("Can't open directory %s"), entry->path);
631 if (opt_chroot && dir_name)
632 free (dir_name);
633 return;
636 while ((direntry = readdir (dir)) != NULL)
638 int flag;
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)
645 continue;
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
649 shared library. */
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))
654 continue;
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);
660 if (!opt_chroot)
661 real_file_name = file_name;
663 sprintf (file_name, "%s/%s", entry->path, direntry->d_name);
664 if (opt_chroot)
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);
677 else
678 #endif
679 if (lstat64 (real_file_name, &lstat_buf))
681 error (0, errno, _("Can't lstat %s"), file_name);
682 continue;
685 is_link = S_ISLNK (lstat_buf.st_mode);
686 if (is_link)
688 /* In case of symlink, we check if the symlink refers to
689 a directory. */
690 if (stat64 (real_file_name, &stat_buf))
692 if (opt_verbose)
693 error (0, errno, _("Can't stat %s"), file_name);
694 continue;
696 is_dir = S_ISDIR (stat_buf.st_mode);
698 else
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;
710 if (is_link)
712 new_entry->ino = stat_buf.st_ino;
713 new_entry->dev = stat_buf.st_dev;
715 else
717 new_entry->ino = lstat_buf.st_ino;
718 new_entry->dev = lstat_buf.st_dev;
720 add_single_dir (new_entry, 0);
721 continue;
723 else if (!S_ISREG (lstat_buf.st_mode) && !is_link)
724 continue;
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);
733 continue;
736 else
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)
743 free (real_name);
744 continue;
747 if (real_name != real_file_name)
748 free (real_name);
750 /* Links will just point to itself. */
751 if (is_link)
753 free (soname);
754 soname = xstrdup (direntry->d_name);
757 if (flag == FLAG_ELF
758 && (entry->flag == FLAG_ELF_LIBC5
759 || entry->flag == FLAG_ELF_LIBC6))
760 flag = entry->flag;
761 /* Some sanity checks to print warnings. */
762 if (opt_verbose)
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
782 is newer. */
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)
796 && flag == FLAG_ELF)
797 dlib_ptr->flag = flag;
798 else
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. */
809 free (soname);
810 break;
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;
824 dlibs = dlib_ptr;
828 closedir (dir);
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,
837 dlib_ptr->soname);
838 if (opt_build_cache)
839 add_to_cache (entry->path, dlib_ptr->soname, dlib_ptr->flag,
840 dlib_ptr->osversion, hwcap);
843 /* Free all resources. */
844 while (dlibs)
846 dlib_ptr = dlibs;
847 free (dlib_ptr->soname);
848 free (dlib_ptr->name);
849 dlibs = dlibs->next;
850 free (dlib_ptr);
853 if (opt_chroot && dir_name)
854 free (dir_name);
857 /* Search through all libraries. */
858 static void
859 search_dirs (void)
861 struct dir_entry *entry;
863 for (entry = dir_entries; entry != NULL; entry = entry->next)
864 search_dir (entry);
866 /* Free all allocated memory. */
867 while (dir_entries)
869 entry = dir_entries;
870 dir_entries = dir_entries->next;
871 free (entry->path);
872 free (entry);
877 /* Parse configuration file. */
878 static void
879 parse_conf (const char *filename)
881 FILE *file = NULL;
882 char *line = NULL;
883 const char *canon;
884 size_t len = 0;
886 if (opt_chroot)
888 canon = chroot_canon (opt_chroot, filename);
889 if (canon)
890 file = fopen (canon, "r");
891 else
892 canon = filename;
894 else
896 canon = filename;
897 file = fopen (filename, "r");
900 if (file == NULL)
902 error (0, errno, _("Can't open configuration file %s"), canon);
903 if (canon != filename)
904 free ((char *) canon);
905 return;
908 if (canon != filename)
909 free ((char *) canon);
913 ssize_t n = getline (&line, &len, file);
914 if (n < 0)
915 break;
917 if (line[n - 1] == '\n')
918 line[n - 1] = '\0';
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. */
926 if (line[0] == '\0')
927 continue;
929 add_dir (line);
930 } while (!feof (file));
932 /* Free buffer and close file. */
933 free (line);
934 fclose (file);
939 main (int argc, char **argv)
941 int remaining;
943 /* Parse and process arguments. */
944 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
946 /* Remaining arguments are additional libraries if opt_manual_link
947 is not set. */
948 if (remaining != argc && !opt_manual_link)
950 int i;
951 for (i = remaining; i < argc; ++i)
952 add_dir (argv[i]);
955 if (opt_chroot)
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] == '/')
960 --endp;
961 *endp = '\0';
962 if (endp == opt_chroot)
963 opt_chroot = NULL;
965 if (opt_chroot)
967 /* It is faster to use chroot if we can. */
968 if (!chroot (opt_chroot))
970 if (chdir ("/"))
971 error (EXIT_FAILURE, errno, _("Can't chdir to /"));
972 opt_chroot = NULL;
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;
986 if (opt_print_cache)
988 if (opt_chroot)
990 char *p = chroot_canon (opt_chroot, cache_file);
991 if (p == NULL)
992 error (EXIT_FAILURE, errno, _("Can't open cache file %s\n"),
993 cache_file);
994 cache_file = p;
996 print_cache (cache_file);
997 if (opt_chroot)
998 free (cache_file);
999 exit (0);
1002 if (opt_chroot)
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) : "/");
1010 if (canon == NULL)
1012 error (EXIT_FAILURE, errno,
1013 _("Can't open cache file directory %s\n"),
1014 p ? cache_file : "/");
1017 if (p)
1018 ++p;
1019 else
1020 p = cache_file;
1022 cache_file = alloca (strlen (canon) + strlen (p) + 2);
1023 sprintf (cache_file, "%s/%s", canon, p);
1024 free (canon);
1027 if (opt_manual_link)
1029 /* Link all given libraries manually. */
1030 int i;
1032 for (i = remaining; i < argc; ++i)
1033 manual_link (argv[i]);
1035 exit (0);
1039 if (opt_build_cache)
1040 init_cache ();
1042 if (!opt_only_cline)
1044 /* Always add the standard search paths. */
1045 add_dir (SLIBDIR);
1046 if (strcmp (SLIBDIR, LIBDIR))
1047 add_dir (LIBDIR);
1049 parse_conf (config_file);
1052 search_dirs ();
1054 if (opt_build_cache)
1055 save_cache (cache_file);
1057 return 0;