2 * ldconfig - update shared library symlinks
4 * usage: ldconfig [-DvqnNX] [-f conf] [-C cache] [-r root] dir ...
5 * ldconfig -l [-Dv] lib ...
7 * -D: debug mode, don't update links
8 * -v: verbose mode, print things as we go
9 * -q: quiet mode, don't print warnings
10 * -n: don't process standard directories
11 * -N: don't update the library cache
12 * -X: don't update the library links
13 * -l: library mode, manually link libraries
14 * -p: print the current library cache
15 * -f conf: use conf instead of /etc/ld.so.conf
16 * -C cache: use cache instead of /etc/ld.so.cache
17 * -r root: first, do a chroot to the indicated directory
18 * dir ...: directories to process
19 * lib ...: libraries to link
21 * Copyright 1994-2000 David Engel and Mitch D'Souza
23 * This program may be used for any purpose as long as this
24 * copyright notice is kept.
42 #include "readsoname.h"
46 unsigned long a_info; /* Use macros N_MAGIC, etc for access */
47 unsigned a_text; /* length of text, in bytes */
48 unsigned a_data; /* length of data, in bytes */
49 unsigned a_bss; /* length of uninitialized data area for file, in bytes */
50 unsigned a_syms; /* length of symbol table data in file, in bytes */
51 unsigned a_entry; /* start address */
52 unsigned a_trsize; /* length of relocation info for text, in bytes */
53 unsigned a_drsize; /* length of relocation info for data, in bytes */
56 #if !defined (N_MAGIC)
57 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
59 /* Code indicating object file or impure executable. */
61 /* Code indicating pure executable. */
63 /* Code indicating demand-paged executable. */
65 /* This indicates a demand-paged executable with the header in the text.
66 The first page is unmapped to help trap NULL pointer references */
68 /* Code indicating core file. */
71 char *___strtok = NULL;
76 #define PATH_MAX _POSIX_PATH_MAX
81 #define N_MAGIC(exec) ((exec).a_magic & 0xffff)
85 #define EXIT_FATAL 128
88 int debug = 0; /* debug mode */
89 int verbose = 0; /* verbose mode */
90 int libmode = 0; /* library mode */
91 int nocache = 0; /* don't build cache */
92 int nolinks = 0; /* don't update links */
94 char *conffile = LDSO_CONF; /* default conf file */
95 char *cachefile = LDSO_CACHE; /* default cache file */
96 void cache_print(void);
97 void cache_dolib(const char *dir, const char *so, int libtype);
98 void cache_write(void);
100 /* These two are used internally -- you shouldn't need to use them */
101 static void verror_msg(const char *s, va_list p)
104 fprintf(stderr, "%s: ", prog);
105 vfprintf(stderr, s, p);
108 extern void warnx(const char *s, ...)
115 fprintf(stderr, "\n");
118 extern void err(int errnum, const char *s, ...)
125 fprintf(stderr, "\n");
129 static void vperror_msg(const char *s, va_list p)
138 fprintf(stderr, "%s%s\n", s, strerror(err));
141 extern void warn(const char *s, ...)
150 void *xmalloc(size_t size)
153 if ((ptr = malloc(size)) == NULL)
154 err(EXIT_FATAL,"out of memory");
158 char *xstrdup(const char *str)
161 if ((ptr = strdup(str)) == NULL)
162 err(EXIT_FATAL,"out of memory");
166 /* If shared library, return a malloced copy of the soname and set the
167 type, else return NULL.
169 expected_type should be either LIB_ANY or one of the following:-
175 If the lib is ELF and we can not deduce the type the type will
176 be set based on expected_type.
178 If the expected, actual/deduced types missmatch we display a warning
179 and use the actual/deduced type.
181 char *is_shlib(const char *dir, const char *name, int *type,
182 int *islink, int expected_type)
190 char buff[PAGE_SIZE];
192 /* see if name is of the form *.so* */
193 if (name[strlen(name)-1] != '~' && (cp = strstr(name, ".so")))
195 /* find the start of the Vminor part, if any */
196 if (cp[3] == '.' && (cp2 = strchr(cp + 4, '.')))
199 cp = cp + strlen(cp);
201 /* construct the full path name */
202 sprintf(buff, "%s%s%s", dir, (*dir && strcmp(dir, "/")) ?
205 /* first, make sure it's a regular file */
206 if (lstat(buff, &statbuf))
207 warn("skipping %s", buff);
208 else if (!S_ISREG(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode))
209 warnx("%s is not a regular file or symlink, skipping", buff);
212 /* is it a regular file or a symlink */
213 *islink = S_ISLNK(statbuf.st_mode);
215 /* then try opening it */
216 if (!(file = fopen(buff, "rb")))
217 warn("skipping %s", buff);
220 /* now make sure it's a shared library */
221 if (fread(&exec, sizeof exec, 1, file) < 1)
222 warnx("can't read header from %s, skipping", buff);
223 else if (N_MAGIC(exec) != ZMAGIC && N_MAGIC(exec) != QMAGIC)
225 elf_hdr = (ElfW(Ehdr) *) &exec;
226 if (elf_hdr->e_ident[0] != 0x7f ||
227 strncmp(&elf_hdr->e_ident[1], "ELF",3) != 0)
229 /* silently ignore linker scripts */
230 if (strncmp((char *)&exec, "/* GNU ld", 9) != 0)
231 warnx("%s is not a shared library, skipping", buff);
235 /* always call readsoname to update type */
236 if(expected_type == LIB_DLL) {
237 warnx("%s is not an a.out library, its ELF!\n", buff);
238 expected_type=LIB_ANY;
241 good = readsoname(buff, file, expected_type, type,
242 elf_hdr->e_ident[EI_CLASS]);
243 if (good == NULL || *islink)
247 good = xstrdup(name);
251 /* if the soname does not match the filename,
252 issue a warning, but only in debug mode. */
253 int len = strlen(good);
254 if (debug && (strncmp(good, name, len) != 0 ||
255 (name[len] != '\0' && name[len] != '.')))
256 warnx("%s has inconsistent soname (%s)", buff, good);
263 good = xstrdup(name);
266 good = xmalloc(cp - name + 1);
267 strncpy(good, name, cp - name);
268 good[cp - name] = '\0';
270 if(expected_type != LIB_ANY && expected_type != LIB_DLL)
272 warnx("%s is not an ELF library, its an a.out DLL!", buff);
273 expected_type=LIB_ANY;
286 /* update the symlink to new library */
287 void link_shlib(const char *dir, const char *file, const char *so)
290 char libname[PAGE_SIZE];
291 char linkname[PAGE_SIZE];
293 struct stat linkstat;
295 /* construct the full path names */
296 sprintf(libname, "%s/%s", dir, file);
297 sprintf(linkname, "%s/%s", dir, so);
299 /* see if a link already exists */
300 if (!stat(linkname, &linkstat))
302 /* now see if it's the one we want */
303 if (stat(libname, &libstat))
304 warn("can't stat %s", libname);
305 else if (libstat.st_dev == linkstat.st_dev &&
306 libstat.st_ino == linkstat.st_ino)
310 /* then update the link, if required */
311 if (change > 0 && !nolinks)
313 if (!lstat(linkname, &linkstat))
315 if (!S_ISLNK(linkstat.st_mode))
317 warnx("%s is not a symlink", linkname);
320 else if (remove(linkname))
322 warn("can't unlink %s", linkname);
328 if (symlink(file, linkname))
330 warn("can't link %s to %s", linkname, file);
336 /* some people like to know what we're doing */
338 printf("\t%s => %s%s\n", so, file,
339 change < 0 ? " (SKIPPED)" :
340 (change > 0 ? " (changed)" : ""));
345 /* figure out which library is greater */
346 int libcmp(char *p1, char *p2)
350 if (isdigit(*p1) && isdigit(*p2))
352 /* must compare this numerically */
354 v1 = strtoul(p1, &p1, 10);
355 v2 = strtoul(p2, &p2, 10);
359 else if (isdigit(*p1) && !isdigit(*p2))
361 else if (!isdigit(*p1) && isdigit(*p2))
374 char *so; /* soname of a library */
375 char *name; /* name of a library */
376 int libtype; /* type of a library */
377 int islink; /* is it a symlink */
378 struct lib *next; /* next library in list */
381 /* update all shared library links in a directory */
382 void scan_dir(const char *rawname)
387 char *so, *path, *path_n;
388 struct lib *lp, *libs = NULL;
389 int i, libtype, islink, expected_type = LIB_ANY;
391 /* We need a writable copy of this string */
392 path = strdup(rawname);
394 err(EXIT_FATAL, "Out of memory!\n");
396 /* Eliminate all double //s */
398 while((path_n=strstr(path_n, "//"))) {
400 memmove(path_n, path_n+1, i-1);
401 *(path_n + i - 1)='\0';
407 /* Check for an embedded expected type */
408 t=strrchr(name, '=');
411 *t++ = '\0'; /* Skip = char */
412 if(strcasecmp(t, "libc4") == 0)
414 expected_type = LIB_DLL;
418 if(strcasecmp(t, "libc5") == 0)
420 expected_type = LIB_ELF_LIBC5;
424 if(strcasecmp(t, "libc6") == 0)
426 expected_type = LIB_ELF_LIBC6;
430 if(strcasecmp(t, "libc0") == 0)
432 expected_type = LIB_ELF_LIBC0;
436 warnx("Unknown type field '%s' for dir '%s' - ignored\n", t, name);
437 expected_type = LIB_ANY;
445 /* let 'em know what's going on */
447 printf("%s:\n", name);
449 /* if we can't open it, we can't do anything */
450 if ((dir = opendir(name)) == NULL)
452 warn("skipping %s", name);
457 /* yes, we have to look at every single file */
458 while ((ent = readdir(dir)) != NULL)
460 /* if it's not a shared library, don't bother */
461 if ((so = is_shlib(name, ent->d_name, &libtype, &islink, expected_type)) == NULL)
464 /* have we already seen one with the same so name? */
465 for (lp = libs; lp; lp = lp->next)
467 if (strcmp(so, lp->so) == 0)
469 /* we have, which one do we want to use? */
470 if ((!islink && lp->islink) ||
471 (islink == lp->islink &&
472 libcmp(ent->d_name, lp->name) > 0))
474 /* let's use the new one */
476 lp->name = xstrdup(ent->d_name);
477 lp->libtype = libtype;
484 /* congratulations, you're the first one we've seen */
487 lp = xmalloc(sizeof *lp);
488 lp->so = xstrdup(so);
489 lp->name = xstrdup(ent->d_name);
490 lp->libtype = libtype;
499 /* don't need this any more */
502 /* now we have all the latest libs, update the links */
503 for (lp = libs; lp; lp = lp->next)
506 link_shlib(name, lp->name, lp->so);
507 #ifdef __LDSO_CACHE_SUPPORT__
509 cache_dolib(name, lp->so, lp->libtype);
513 /* always try to clean up after ourselves */
527 /* return the list of system-specific directories */
528 char *get_extpath(void)
530 char *res = NULL, *cp;
534 if ((file = fopen(conffile, "r")) != NULL)
536 fstat(fileno(file), &stat);
537 res = xmalloc(stat.st_size + 1);
538 fread(res, 1, stat.st_size, file);
540 res[stat.st_size] = '\0';
542 /* convert comments fo spaces */
543 for (cp = res; *cp; /*nada*/) {
547 while (*cp && *cp != '\n');
557 #ifdef __LDSO_CACHE_SUPPORT__
558 typedef struct liblist
565 struct liblist *next;
568 static header_t magic = { LDSO_CACHE_MAGIC, LDSO_CACHE_VER, 0 };
569 static liblist_t *lib_head = NULL;
571 static int liblistcomp(liblist_t *x, liblist_t *y)
575 if ((res = libcmp(x->soname, y->soname)) == 0)
577 res = libcmp(strrchr(x->libname, '/') + 1,
578 strrchr(y->libname, '/') + 1);
584 void cache_dolib(const char *dir, const char *so, int libtype)
586 char fullpath[PATH_MAX];
587 liblist_t *new_lib, *cur_lib;
590 sprintf(fullpath, "%s/%s", dir, so);
591 new_lib = xmalloc(sizeof (liblist_t));
592 new_lib->flags = libtype;
593 new_lib->soname = xstrdup(so);
594 new_lib->libname = xstrdup(fullpath);
596 if (lib_head == NULL || liblistcomp(new_lib, lib_head) > 0)
598 new_lib->next = lib_head;
603 for (cur_lib = lib_head; cur_lib->next != NULL &&
604 liblistcomp(new_lib, cur_lib->next) <= 0;
605 cur_lib = cur_lib->next)
607 new_lib->next = cur_lib->next;
608 cur_lib->next = new_lib;
612 void cache_write(void)
616 char tempfile[PAGE_SIZE];
622 sprintf(tempfile, "%s~", cachefile);
624 if (unlink(tempfile) && errno != ENOENT)
625 err(EXIT_FATAL,"can't unlink %s (%s)", tempfile, strerror(errno));
627 if ((cachefd = creat(tempfile, 0644)) < 0)
628 err(EXIT_FATAL,"can't create %s (%s)", tempfile, strerror(errno));
630 if (write(cachefd, &magic, sizeof (header_t)) != sizeof (header_t))
631 err(EXIT_FATAL,"can't write %s (%s)", tempfile, strerror(errno));
633 for (cur_lib = lib_head; cur_lib != NULL; cur_lib = cur_lib->next)
635 cur_lib->sooffset = stroffset;
636 stroffset += strlen(cur_lib->soname) + 1;
637 cur_lib->liboffset = stroffset;
638 stroffset += strlen(cur_lib->libname) + 1;
639 if (write(cachefd, cur_lib, sizeof (libentry_t)) !=
641 err(EXIT_FATAL,"can't write %s (%s)", tempfile, strerror(errno));
644 for (cur_lib = lib_head; cur_lib != NULL; cur_lib = cur_lib->next)
646 if (write(cachefd, cur_lib->soname, strlen(cur_lib->soname) + 1)
647 != strlen(cur_lib->soname) + 1)
648 err(EXIT_FATAL,"can't write %s (%s)", tempfile, strerror(errno));
649 if (write(cachefd, cur_lib->libname, strlen(cur_lib->libname) + 1)
650 != strlen(cur_lib->libname) + 1)
651 err(EXIT_FATAL,"can't write %s (%s)", tempfile, strerror(errno));
655 err(EXIT_FATAL,"can't close %s (%s)", tempfile, strerror(errno));
657 if (chmod(tempfile, 0644))
658 err(EXIT_FATAL,"can't chmod %s (%s)", tempfile, strerror(errno));
660 if (rename(tempfile, cachefile))
661 err(EXIT_FATAL,"can't rename %s (%s)", tempfile, strerror(errno));
664 void cache_print(void)
673 if (stat(cachefile, &st) || (fd = open(cachefile, O_RDONLY))<0)
674 err(EXIT_FATAL,"can't read %s (%s)", cachefile, strerror(errno));
675 if ((c = mmap(0,st.st_size, PROT_READ, MAP_SHARED ,fd, 0)) == (caddr_t)-1)
676 err(EXIT_FATAL,"can't map %s (%s)", cachefile, strerror(errno));
679 if (memcmp(((header_t *)c)->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN))
680 err(EXIT_FATAL,"%s cache corrupt", cachefile);
682 if (memcmp(((header_t *)c)->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN))
683 err(EXIT_FATAL,"wrong cache version - expected %s", LDSO_CACHE_VER);
685 header = (header_t *)c;
686 libent = (libentry_t *)(c + sizeof (header_t));
687 strs = (char *)&libent[header->nlibs];
689 printf("%d libs found in cache `%s' (version %s)\n",
690 header->nlibs, cachefile, LDSO_CACHE_VER);
692 for (fd = 0; fd < header->nlibs; fd++)
694 printf("\t%s ", strs + libent[fd].sooffset);
695 switch (libent[fd].flags & ~LIB_ELF64)
701 printf("(ELF%s)", libent[fd].flags & LIB_ELF64 ? "/64" : "");
704 printf("(libc0%s)",libent[fd].flags & LIB_ELF64 ? "/64" : "");
708 printf("(libc%d%s)", (libent[fd].flags & ~LIB_ELF64) + 3,
709 libent[fd].flags & LIB_ELF64 ? "/64" : "");
715 printf(" => %s\n", strs + libent[fd].liboffset);
718 munmap (c,st.st_size);
721 void cache_print(void)
723 warnx("Cache support disabled\n");
730 "ldconfig - updates symlinks for shared libraries\n\n"
731 "Usage: ldconfig [-DvqnNX] [-f conf] [-C cache] [-r root] dir ...\n"
732 " ldconfig -l [-Dv] lib ...\n"
733 " ldconfig -p\n\nOptions:\n"
734 "\t-D:\t\tdebug mode, don't update links\n"
735 "\t-v:\t\tverbose mode, print things as we go\n"
736 "\t-q:\t\tquiet mode, don't print warnings\n"
737 "\t-n:\t\tdon't process standard directories\n"
738 "\t-N:\t\tdon't update the library cache\n"
739 "\t-X:\t\tdon't update the library links\n"
740 "\t-l:\t\tlibrary mode, manually link libraries\n"
741 "\t-p:\t\tprint the current library cache\n"
742 "\t-f conf :\tuse conf instead of %s\n"
743 "\t-C cache:\tuse cache instead of %s\n"
744 "\t-r root :\tfirst, do a chroot to the indicated directory\n"
745 "\tdir ... :\tdirectories to process\n"
746 "\tlib ... :\tlibraries to link\n\n",
747 LDSO_CONF, LDSO_CACHE
752 #define DIR_SEP ":, \t\n"
753 int main(int argc, char **argv)
761 char *chroot_dir = NULL;
766 while ((c = getopt(argc, argv, "DvqnNXlpf:C:r:")) != EOF)
770 debug = 1; /* debug mode */
776 verbose = 1; /* verbose mode */
780 verbose = -1; /* quiet mode */
783 nodefault = 1; /* no default dirs */
787 nocache = 1; /* don't build cache */
790 nolinks = 1; /* don't update links */
793 libmode = 1; /* library mode */
796 printcache = 1; /* print cache */
799 conffile = optarg; /* alternate conf file */
802 cachefile = optarg; /* alternate cache file */
811 /* THE REST OF THESE ARE UNDOCUMENTED AND MAY BE REMOVED
812 IN FUTURE VERSIONS. */
815 if (chroot_dir && *chroot_dir) {
816 if (chroot(chroot_dir) < 0)
817 err(EXIT_FATAL,"couldn't chroot to %s (%s)", chroot_dir, strerror(errno));
819 err(EXIT_FATAL,"couldn't chdir to / (%s)", strerror(errno));
822 /* allow me to introduce myself, hi, my name is ... */
824 printf("%s: uClibc version\n", argv[0]);
828 /* print the cache -- don't you trust me? */
834 /* so you want to do things manually, eh? */
836 /* ok, if you're so smart, which libraries do we link? */
837 for (i = optind; i < argc; i++)
839 /* split into directory and file parts */
840 if (!(cp = strrchr(argv[i], '/')))
842 dir = "."; /* no dir, only a filename */
848 dir = "/"; /* file in root directory */
851 *cp++ = '\0'; /* neither of the above */
854 /* we'd better do a little bit of checking */
855 if ((so = is_shlib(dir, cp, &libtype, &islink, LIB_ANY)) == NULL)
856 err(EXIT_FATAL,"%s%s%s is not a shared library", dir,
857 (*dir && strcmp(dir, "/")) ? "/" : "", cp);
859 /* so far, so good, maybe he knows what he's doing */
860 link_shlib(dir, cp, so);
865 /* the lazy bum want's us to do all the work for him */
867 /* don't cache dirs on the command line */
868 int nocache_save = nocache;
871 /* OK, which directories should we do? */
872 for (i = optind; i < argc; i++)
875 /* restore the desired caching state */
876 nocache = nocache_save;
878 /* look ma, no defaults */
881 scan_dir(UCLIBC_RUNTIME_PREFIX "lib");
882 scan_dir(UCLIBC_RUNTIME_PREFIX "usr/lib");
883 #ifndef __LDSO_CACHE_SUPPORT__
884 scan_dir(UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib");
887 /* I guess the defaults aren't good enough */
888 if ((extpath = get_extpath()))
890 for (cp = strtok(extpath, DIR_SEP); cp; cp = strtok(NULL, DIR_SEP)) {
891 /* strip trailing slashes */
892 int len = strlen(cp);
894 while (cp[--len] == '/' && len)
896 /* we do the redundancy check only if cache usage is enabled */
897 #ifdef __LDSO_CACHE_SUPPORT__
898 if (strcmp(UCLIBC_RUNTIME_PREFIX "lib", cp) == 0 ||
899 strcmp(UCLIBC_RUNTIME_PREFIX "usr/lib", cp) == 0) {
901 warnx("Remove `%s' from `%s'\n", cp, LDSO_CONF);
911 #ifdef __LDSO_CACHE_SUPPORT__