1 /* Copyright (C) 1999 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. */
29 #include <sys/fcntl.h>
32 #include <sys/types.h>
37 # define LD_SO_CACHE "/etc/ld.so.cache"
41 # define LD_SO_CONF "/etc/ld.so.conf"
44 /* Get libc version number. */
47 #define PACKAGE _libc_intl_domainname
62 {"libc4", FLAG_LIBC4},
63 {"libc5", FLAG_ELF_LIBC5},
64 {"libc6", FLAG_ELF_LIBC6},
65 {"glibc2", FLAG_ELF_LIBC6}
69 /* List of directories to handle. */
74 struct dir_entry *next;
77 /* The list is unsorted, contains no duplicates. Entries are added at
79 static struct dir_entry *dir_entries;
81 /* Flags for different options. */
83 static int opt_print_cache = 0;
89 static int opt_build_cache = 1;
92 static int opt_link = 1;
94 /* Only process directories specified on the command line. */
95 static int opt_only_cline = 0;
97 /* Path to root for chroot. */
98 static char *opt_chroot;
100 /* Manually link given shared libraries. */
101 static int opt_manual_link = 0;
103 /* Cache file to use. */
104 static const char *cache_file;
106 /* Configuration file. */
107 static const char *config_file;
109 /* Name and version of program. */
110 static void print_version (FILE *stream, struct argp_state *state);
111 void (*argp_program_version_hook) (FILE *, struct argp_state *)
114 /* Definitions of arguments for argp functions. */
115 static const struct argp_option options[] =
117 { "print-cache", 'p', NULL, 0, N_("Print cache"), 0},
118 { "verbose", 'v', NULL, 0, N_("Generate verbose messages"), 0},
119 { NULL, 'N', NULL, 0, N_("Don't build cache"), 0},
120 { NULL, 'X', NULL, 0, N_("Don't generate links"), 0},
121 { NULL, 'r', "ROOT", 0, N_("Change to and use ROOT as root directory"), 0},
122 { NULL, 'C', "CACHE", 0, N_("Use CACHE as cache file"), 0},
123 { NULL, 'f', "CONF", 0, N_("Use CONF as configuration file"), 0},
124 { NULL, 'n', NULL, 0, N_("Only process directories specified on the command line. Don't build cache."), 0},
125 { NULL, 'l', NULL, 0, N_("Manually link individual libraries."), 0},
126 { NULL, 0, NULL, 0, NULL, 0 }
129 /* Short description of program. */
130 static const char doc[] = N_("Configure Dynamic Linker Run Time Bindings.");
132 /* Prototype for option handler. */
133 static error_t parse_opt (int key, char *arg, struct argp_state *state);
135 /* Data structure to communicate with argp functions. */
136 static struct argp argp =
138 options, parse_opt, NULL, doc, NULL, NULL, NULL
143 /* Handle program arguments. */
145 parse_opt (int key, char *arg, struct argp_state *state)
178 return ARGP_ERR_UNKNOWN;
184 /* Print the version information. */
186 print_version (FILE *stream, struct argp_state *state)
188 fprintf (stream, "ldconfig (GNU %s) %s\n", PACKAGE, VERSION);
189 fprintf (stream, gettext ("\
190 Copyright (C) %s Free Software Foundation, Inc.\n\
191 This is free software; see the source for copying conditions. There is NO\n\
192 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
194 fprintf (stream, gettext ("Written by %s.\n"),
198 /* Add one directory to the list of directories to process. */
200 add_dir (const char *line)
203 struct dir_entry *entry, *ptr, *prev;
206 entry = xmalloc (sizeof (struct dir_entry));
209 /* Search for an '=' sign. */
210 entry->path = xstrdup (line);
211 equal_sign = strchr (entry->path, '=');
216 entry->flag = FLAG_ANY;
217 for (i = 0; i < sizeof (lib_types) / sizeof (lib_types [0]); ++i)
218 if (strcmp (equal_sign, lib_types[i].name) == 0)
220 entry->flag = lib_types[i].flag;
223 if (entry->flag == FLAG_ANY)
224 error (0, 0, _("%s is not a known library type"), equal_sign);
228 entry->flag = FLAG_ANY;
231 /* Canonify path: for now only remove trailing slashes. */
232 i = strlen (entry->path) - 1;
233 while (entry->path[i] == '/' && i > 0)
235 entry->path [i] = '\0';
243 /* Check for duplicates. */
244 if (strcmp (ptr->path, entry->path) == 0)
247 error (0, 0, _("Path `%s' given more than once"), entry->path);
248 /* Use the newer information. */
249 ptr->flag = entry->flag;
256 /* Is this the first entry? */
257 if (ptr == NULL && dir_entries == NULL)
259 else if (ptr == NULL)
264 /* Create a symbolic link from soname to libname in directory path. */
266 create_links (const char *path, const char *libname, const char *soname)
268 char full_libname [PATH_MAX], full_soname [PATH_MAX];
269 struct stat stat_lib, stat_so, lstat_so;
272 /* XXX: The logics in this function should be simplified. */
274 /* Get complete path. */
275 snprintf (full_libname, sizeof full_libname, "%s/%s", path, libname);
276 snprintf (full_soname, sizeof full_soname, "%s/%s", path, soname);
278 /* Does soname already exist and point to the right library? */
279 if (stat (full_soname, &stat_so) == 0)
281 if (stat (full_libname, &stat_lib))
283 error (0, 0, _("Can't stat %s\n"), full_libname);
286 if (stat_lib.st_dev == stat_so.st_dev
287 && stat_lib.st_ino == stat_so.st_ino)
288 /* Link is already correct. */
290 else if (lstat (full_soname, &lstat_so) == 0
291 && !S_ISLNK (lstat_so.st_mode))
293 error (0, 0, _("%s is not a symbolic link\n"), full_soname);
298 else if (lstat (full_soname, &lstat_so) != 0
299 || !S_ISLNK (lstat_so.st_mode))
300 /* Unless it is a stale symlink, there is no need to remove. */
304 printf ("\t%s -> %s", soname, libname);
306 if (do_link && opt_link)
308 /* Remove old link. */
310 if (unlink (full_soname))
312 error (0, 0, _("Can't unlink %s"), full_soname);
315 /* Create symbolic link. */
316 if (do_link && symlink (libname, full_soname))
318 error (0, 0, _("Can't link %s to %s"), full_soname, libname);
324 fputs (_(" (changed)\n"), stdout);
326 fputs (_(" (SKIPPED)\n"), stdout);
329 else if (opt_verbose)
330 fputs ("\n", stdout);
333 /* Manually link the given library. */
335 manual_link (char *library)
340 struct stat stat_buf;
343 /* Prepare arguments for create_links call. Split library name in
344 directory and filename first. Since path is allocated, we've got
345 to be careful to free at the end. */
346 path = xstrdup (library);
347 libname = strrchr (path, '/');
351 /* Successfully split names. Check if path is just "/" to avoid
355 libname = library + 1;
356 path = xrealloc (path, 2);
367 /* There's no path, construct one. */
369 path = xrealloc (path, 2);
373 /* Do some sanity checks first. */
374 if (lstat (library, &stat_buf))
376 error (0, errno, _("Can't lstat %s"), library);
380 /* We don't want links here! */
381 else if (!S_ISREG (stat_buf.st_mode))
383 error (0, 0, _("Ignored file %s since it is not a regular file."),
388 libname = basename (library);
389 if (process_file (library, libname, &flag, &soname, 0))
391 error (0, 0, _("No link created since soname could not be found for %s"),
396 create_links (path, libname, soname);
402 /* Read a whole directory and search for libraries.
403 The purpose is two-fold:
404 - search for libraries which will be added to the cache
405 - create symbolic links to the soname for each library
407 This has to be done separatly for each directory.
409 To keep track of which libraries to add to the cache and which
410 links to create, we save a list of all libraries.
412 The algorithm is basically:
413 for all libraries in the directory do
414 get soname of library
415 if soname is already in list
416 if new library is newer, replace entry
417 otherwise ignore this library
418 otherwise add library to list
420 For example, if the two libraries libxy.so.1.1 and libxy.so.1.2
421 exist and both have the same soname, e.g. libxy.so, a symbolic link
422 is created from libxy.so.1.2 (the newer one) to libxy.so.
423 libxy.so.1.2 and libxy.so are added to the cache - but not
426 /* Information for one library. */
433 struct dlib_entry *next;
438 search_dir (const struct dir_entry *entry)
441 struct dirent *direntry;
444 struct dlib_entry *dlibs;
445 struct dlib_entry *dlib_ptr;
447 struct stat stat_buf;
453 printf ("%s:\n", entry->path);
455 dir = opendir (entry->path);
459 error (0, errno, _("Can't open directory %s"), entry->path);
464 while ((direntry = readdir (dir)) != NULL)
467 #ifdef _DIRENT_HAVE_D_TYPE
468 /* We only look at links and regular files. */
469 if (direntry->d_type != DT_UNKNOWN
470 && direntry->d_type != DT_LNK
471 && direntry->d_type != DT_REG)
473 #endif /* _DIRENT_HAVE_D_TYPE */
475 /* Does this file look like a shared library? The dynamic
476 linker is also considered as shared library. */
477 if ((strncmp (direntry->d_name, "lib", 3) != 0
478 && strncmp (direntry->d_name, "ld-", 3) != 0)
479 || strstr (direntry->d_name, ".so") == NULL)
481 nchars = snprintf (buf, sizeof (buf), "%s/%s", entry->path,
483 /* Check for overflow. */
484 if (nchars >= (int) sizeof (buf))
486 error (0, 0, _("buffer for snprintf too small for %s/%s--file is ignored\n"),
487 entry->path, direntry->d_name);
490 if (lstat (buf, &stat_buf))
492 error (0, errno, _("Can't lstat %s"), buf);
495 else if (!S_ISREG (stat_buf.st_mode) && !S_ISLNK (stat_buf.st_mode))
498 is_link = S_ISLNK (stat_buf.st_mode);
500 if (process_file (buf, direntry->d_name, &flag, &soname, is_link))
503 /* Links will just point to itself. */
507 soname = xstrdup (direntry->d_name);
511 && (entry->flag == FLAG_ELF_LIBC5
512 || entry->flag == FLAG_ELF_LIBC6))
514 /* Some sanity checks to print warnings. */
517 if (flag == FLAG_ELF_LIBC5 && entry->flag != FLAG_ELF_LIBC5
518 && entry->flag != FLAG_ANY)
519 error (0, 0, _("libc5 library %s in wrong directory"), buf);
520 if (flag == FLAG_ELF_LIBC6 && entry->flag != FLAG_ELF_LIBC6
521 && entry->flag != FLAG_ANY)
522 error (0, 0, _("libc6 library %s in wrong directory"), buf);
523 if (flag == FLAG_LIBC4 && entry->flag != FLAG_LIBC4
524 && entry->flag != FLAG_ANY)
525 error (0, 0, _("libc4 library %s in wrong directory"), buf);
528 /* Add library to list. */
529 for (dlib_ptr = dlibs; dlib_ptr != NULL; dlib_ptr = dlib_ptr->next)
531 /* Is soname already in list? */
532 if (strcmp (dlib_ptr->soname, soname) == 0)
534 /* Prefer a file to a link, otherwise check which one
536 if ((!is_link && dlib_ptr->is_link)
537 || (is_link == dlib_ptr->is_link
538 && cache_libcmp (dlib_ptr->name, direntry->d_name) < 0))
540 /* It's newer - add it. */
541 /* Flag should be the same - sanity check. */
542 if (dlib_ptr->flag != flag)
544 if (dlib_ptr->flag == FLAG_ELF
545 && (flag == FLAG_ELF_LIBC5 || flag == FLAG_ELF_LIBC6))
546 dlib_ptr->flag = flag;
547 else if ((dlib_ptr->flag == FLAG_ELF_LIBC5
548 || dlib_ptr->flag == FLAG_ELF_LIBC6)
550 dlib_ptr->flag = flag;
552 error (0, 0, _("libraries %s and %s in directory %s have same soname but different type."),
553 dlib_ptr->name, direntry->d_name, entry->path);
555 free (dlib_ptr->name);
556 dlib_ptr->name = xstrdup (direntry->d_name);
557 dlib_ptr->is_link = is_link;
559 /* Don't add this library, abort loop. */
560 /* Also free soname, since it's dynamically allocated. */
565 /* Add the library if it's not already in. */
566 if (dlib_ptr == NULL)
568 dlib_ptr = (struct dlib_entry *)xmalloc (sizeof (struct dlib_entry));
569 dlib_ptr->name = xstrdup (direntry->d_name);
570 dlib_ptr->flag = flag;
571 dlib_ptr->soname = soname;
572 dlib_ptr->is_link = is_link;
573 /* Add at head of list. */
574 dlib_ptr->next = dlibs;
581 /* Now dlibs contains a list of all libs - add those to the cache
582 and created all symbolic links. */
583 for (dlib_ptr = dlibs; dlib_ptr != NULL; dlib_ptr = dlib_ptr->next)
585 /* Don't create links to links. */
586 if (dlib_ptr->is_link == 0)
587 create_links (entry->path, dlib_ptr->name, dlib_ptr->soname);
589 add_to_cache (entry->path, dlib_ptr->soname, dlib_ptr->flag);
592 /* Free all resources. */
596 free (dlib_ptr->soname);
597 free (dlib_ptr->name);
603 /* Search through all libraries. */
607 struct dir_entry *entry;
609 for (entry = dir_entries; entry != NULL; entry = entry->next)
612 /* Free all allocated memory. */
616 dir_entries = dir_entries->next;
623 /* Parse configuration file. */
625 parse_conf (const char *filename)
631 file = fopen (filename, "r");
635 error (0, errno, _("Can't open configuration file %s"), filename);
641 ssize_t n = getline (&line, &len, file);
645 if (line[n - 1] == '\n')
648 /* Because the file format does not know any form of quoting we
649 can search forward for the next '#' character and if found
650 make it terminating the line. */
651 *strchrnul (line, '#') = '\0';
653 /* If the line is blank it is ignored. */
658 } while (!feof (file));
660 /* Free buffer and close file. */
667 main (int argc, char **argv)
671 /* Parse and process arguments. */
672 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
674 /* Remaining arguments are additional libraries if opt_manual_link
676 if (remaining != argc && !opt_manual_link)
679 for (i = remaining; i < argc; ++i)
683 if (cache_file == NULL)
684 cache_file = LD_SO_CACHE;
686 if (config_file == NULL)
687 config_file = LD_SO_CONF;
692 if (chroot (opt_chroot))
693 /* Report failure and exit program. */
694 error (EXIT_FAILURE, errno, _("Can't chroot to %s"), opt_chroot);
695 /* chroot doesn't change the working directory, let's play safe. */
697 error (EXIT_FAILURE, errno, _("Can't chdir to /"));
702 print_cache (cache_file);
708 /* Link all given libraries manually. */
711 for (i = remaining; i < argc; ++i)
712 manual_link (argv [i]);
723 /* Always add the standard search paths. */
725 add_dir ("/usr/lib");
727 parse_conf (config_file);
733 save_cache (cache_file);