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.
41 #include "readsoname.h"
43 #define BUFFER_SIZE 4096
47 unsigned long a_info; /* Use macros N_MAGIC, etc for access */
48 unsigned a_text; /* length of text, in bytes */
49 unsigned a_data; /* length of data, in bytes */
50 unsigned a_bss; /* length of uninitialized data area for file, in bytes */
51 unsigned a_syms; /* length of symbol table data in file, in bytes */
52 unsigned a_entry; /* start address */
53 unsigned a_trsize; /* length of relocation info for text, in bytes */
54 unsigned a_drsize; /* length of relocation info for data, in bytes */
57 #if !defined (N_MAGIC)
58 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
60 /* Code indicating object file or impure executable. */
62 /* Code indicating pure executable. */
64 /* Code indicating demand-paged executable. */
66 /* This indicates a demand-paged executable with the header in the text.
67 The first page is unmapped to help trap NULL pointer references */
69 /* Code indicating core file. */
72 char *___strtok = NULL;
77 #define PATH_MAX _POSIX_PATH_MAX
82 #define N_MAGIC(exec) ((exec).a_magic & 0xffff)
86 #define EXIT_FATAL 128
89 int debug = 0; /* debug mode */
90 int verbose = 0; /* verbose mode */
91 int libmode = 0; /* library mode */
92 int nolinks = 0; /* don't update links */
93 int nocache = 0; /* don't build cache */
94 void cache_print(void);
95 void cache_write(void);
96 void cache_dolib(const char *dir, const char *so, int libtype);
97 #ifdef __LDSO_CACHE_SUPPORT__
98 char *conffile = LDSO_CONF; /* default conf file */
99 char *cachefile = LDSO_CACHE; /* default cache file */
102 /* These two are used internally -- you shouldn't need to use them */
103 static void verror_msg(const char *s, va_list p)
106 fprintf(stderr, "%s: ", prog);
107 vfprintf(stderr, s, p);
110 extern void warnx(const char *s, ...)
117 fprintf(stderr, "\n");
120 extern void err(int errnum, const char *s, ...)
127 fprintf(stderr, "\n");
131 static void vperror_msg(const char *s, va_list p)
140 fprintf(stderr, "%s%s\n", s, strerror(err));
143 extern void warn(const char *s, ...)
152 void *xmalloc(size_t size)
155 if ((ptr = malloc(size)) == NULL)
156 err(EXIT_FATAL,"out of memory");
160 char *xstrdup(const char *str)
163 if ((ptr = strdup(str)) == NULL)
164 err(EXIT_FATAL,"out of memory");
168 /* If shared library, return a malloced copy of the soname and set the
169 type, else return NULL.
171 expected_type should be either LIB_ANY or one of the following:-
177 If the lib is ELF and we can not deduce the type the type will
178 be set based on expected_type.
180 If the expected, actual/deduced types missmatch we display a warning
181 and use the actual/deduced type.
183 char *is_shlib(const char *dir, const char *name, int *type,
184 int *islink, int expected_type)
192 char buff[BUFFER_SIZE];
194 /* see if name is of the form *.so* */
195 if (name[strlen(name)-1] != '~' && (cp = strstr(name, ".so")))
197 /* find the start of the Vminor part, if any */
198 if (cp[3] == '.' && (cp2 = strchr(cp + 4, '.')))
201 cp = cp + strlen(cp);
203 /* construct the full path name */
204 sprintf(buff, "%s%s%s", dir, (*dir && strcmp(dir, "/")) ?
207 /* first, make sure it's a regular file */
208 if (lstat(buff, &statbuf))
209 warn("skipping %s", buff);
210 else if (!S_ISREG(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode))
211 warnx("%s is not a regular file or symlink, skipping", buff);
214 /* is it a regular file or a symlink */
215 *islink = S_ISLNK(statbuf.st_mode);
217 /* then try opening it */
218 if (!(file = fopen(buff, "rb")))
219 warn("skipping %s", buff);
222 /* now make sure it's a shared library */
223 if (fread(&exec, sizeof exec, 1, file) < 1)
224 warnx("can't read header from %s, skipping", buff);
225 else if (N_MAGIC(exec) != ZMAGIC && N_MAGIC(exec) != QMAGIC)
227 elf_hdr = (ElfW(Ehdr) *) &exec;
228 if (elf_hdr->e_ident[0] != 0x7f ||
229 strncmp(&elf_hdr->e_ident[1], "ELF",3) != 0)
231 /* silently ignore linker scripts */
232 if (strncmp((char *)&exec, "/* GNU ld", 9) != 0)
233 warnx("%s is not a shared library, skipping", buff);
237 /* always call readsoname to update type */
238 if(expected_type == LIB_DLL) {
239 warnx("%s is not an a.out library, its ELF!\n", buff);
240 expected_type=LIB_ANY;
243 good = readsoname(buff, file, expected_type, type,
244 elf_hdr->e_ident[EI_CLASS]);
245 if (good == NULL || *islink)
249 good = xstrdup(name);
253 /* if the soname does not match the filename,
254 issue a warning, but only in debug mode. */
255 int len = strlen(good);
256 if (debug && (strncmp(good, name, len) != 0 ||
257 (name[len] != '\0' && name[len] != '.')))
258 warnx("%s has inconsistent soname (%s)", buff, good);
265 good = xstrdup(name);
268 good = xmalloc(cp - name + 1);
269 strncpy(good, name, cp - name);
270 good[cp - name] = '\0';
272 if(expected_type != LIB_ANY && expected_type != LIB_DLL)
274 warnx("%s is not an ELF library, its an a.out DLL!", buff);
275 expected_type=LIB_ANY;
288 /* update the symlink to new library */
289 void link_shlib(const char *dir, const char *file, const char *so)
292 char libname[BUFFER_SIZE];
293 char linkname[BUFFER_SIZE];
295 struct stat linkstat;
297 /* construct the full path names */
298 sprintf(libname, "%s/%s", dir, file);
299 sprintf(linkname, "%s/%s", dir, so);
301 /* see if a link already exists */
302 if (!stat(linkname, &linkstat))
304 /* now see if it's the one we want */
305 if (stat(libname, &libstat))
306 warn("can't stat %s", libname);
307 else if (libstat.st_dev == linkstat.st_dev &&
308 libstat.st_ino == linkstat.st_ino)
312 /* then update the link, if required */
313 if (change > 0 && !nolinks)
315 if (!lstat(linkname, &linkstat))
317 if (!S_ISLNK(linkstat.st_mode))
319 warnx("%s is not a symlink", linkname);
322 else if (remove(linkname))
324 warn("can't unlink %s", linkname);
330 if (symlink(file, linkname))
332 warn("can't link %s to %s", linkname, file);
338 /* some people like to know what we're doing */
340 printf("\t%s => %s%s\n", so, file,
341 change < 0 ? " (SKIPPED)" :
342 (change > 0 ? " (changed)" : ""));
347 /* figure out which library is greater */
348 int libcmp(char *p1, char *p2)
352 if (isdigit(*p1) && isdigit(*p2))
354 /* must compare this numerically */
356 v1 = strtoul(p1, &p1, 10);
357 v2 = strtoul(p2, &p2, 10);
361 else if (isdigit(*p1) && !isdigit(*p2))
363 else if (!isdigit(*p1) && isdigit(*p2))
376 char *so; /* soname of a library */
377 char *name; /* name of a library */
378 int libtype; /* type of a library */
379 int islink; /* is it a symlink */
380 struct lib *next; /* next library in list */
383 /* update all shared library links in a directory */
384 void scan_dir(const char *rawname)
389 char *so, *path, *path_n;
390 struct lib *lp, *libs = NULL;
391 int i, libtype, islink, expected_type = LIB_ANY;
393 /* We need a writable copy of this string */
394 path = strdup(rawname);
396 err(EXIT_FATAL, "Out of memory!\n");
398 /* Eliminate all double //s */
400 while((path_n=strstr(path_n, "//"))) {
402 memmove(path_n, path_n+1, i-1);
403 *(path_n + i - 1)='\0';
409 /* Check for an embedded expected type */
410 t=strrchr(name, '=');
413 *t++ = '\0'; /* Skip = char */
414 if(strcasecmp(t, "libc4") == 0)
416 expected_type = LIB_DLL;
420 if(strcasecmp(t, "libc5") == 0)
422 expected_type = LIB_ELF_LIBC5;
426 if(strcasecmp(t, "libc6") == 0)
428 expected_type = LIB_ELF_LIBC6;
432 if(strcasecmp(t, "libc0") == 0)
434 expected_type = LIB_ELF_LIBC0;
438 warnx("Unknown type field '%s' for dir '%s' - ignored\n", t, name);
439 expected_type = LIB_ANY;
447 /* let 'em know what's going on */
449 printf("%s:\n", name);
451 /* if we can't open it, we can't do anything */
452 if ((dir = opendir(name)) == NULL)
454 warn("skipping %s", name);
459 /* yes, we have to look at every single file */
460 while ((ent = readdir(dir)) != NULL)
462 /* if it's not a shared library, don't bother */
463 if ((so = is_shlib(name, ent->d_name, &libtype, &islink, expected_type)) == NULL)
466 /* have we already seen one with the same so name? */
467 for (lp = libs; lp; lp = lp->next)
469 if (strcmp(so, lp->so) == 0)
471 /* we have, which one do we want to use? */
472 if ((!islink && lp->islink) ||
473 (islink == lp->islink &&
474 libcmp(ent->d_name, lp->name) > 0))
476 /* let's use the new one */
478 lp->name = xstrdup(ent->d_name);
479 lp->libtype = libtype;
486 /* congratulations, you're the first one we've seen */
489 lp = xmalloc(sizeof *lp);
490 lp->so = xstrdup(so);
491 lp->name = xstrdup(ent->d_name);
492 lp->libtype = libtype;
501 /* don't need this any more */
504 /* now we have all the latest libs, update the links */
505 for (lp = libs; lp; lp = lp->next)
508 link_shlib(name, lp->name, lp->so);
510 cache_dolib(name, lp->so, lp->libtype);
513 /* always try to clean up after ourselves */
527 #ifndef __LDSO_CACHE_SUPPORT__
528 void cache_print(void)
530 printf("Library cache disabled\n");
532 void cache_dolib(const char *dir, const char *so, int libtype)
536 void cache_write(void)
541 /* return the list of system-specific directories */
542 char *get_extpath(void)
544 char *res = NULL, *cp;
548 if ((file = fopen(conffile, "r")) != NULL)
550 fstat(fileno(file), &stat);
551 res = xmalloc(stat.st_size + 1);
552 fread(res, 1, stat.st_size, file);
554 res[stat.st_size] = '\0';
556 /* convert comments fo spaces */
557 for (cp = res; *cp; /*nada*/) {
561 while (*cp && *cp != '\n');
571 typedef struct liblist
578 struct liblist *next;
581 static header_t magic = { LDSO_CACHE_MAGIC, LDSO_CACHE_VER, 0 };
582 static liblist_t *lib_head = NULL;
584 static int liblistcomp(liblist_t *x, liblist_t *y)
588 if ((res = libcmp(x->soname, y->soname)) == 0)
590 res = libcmp(strrchr(x->libname, '/') + 1,
591 strrchr(y->libname, '/') + 1);
597 void cache_dolib(const char *dir, const char *so, int libtype)
599 char fullpath[PATH_MAX];
600 liblist_t *new_lib, *cur_lib;
603 sprintf(fullpath, "%s/%s", dir, so);
604 new_lib = xmalloc(sizeof (liblist_t));
605 new_lib->flags = libtype;
606 new_lib->soname = xstrdup(so);
607 new_lib->libname = xstrdup(fullpath);
609 if (lib_head == NULL || liblistcomp(new_lib, lib_head) > 0)
611 new_lib->next = lib_head;
616 for (cur_lib = lib_head; cur_lib->next != NULL &&
617 liblistcomp(new_lib, cur_lib->next) <= 0;
618 cur_lib = cur_lib->next)
620 new_lib->next = cur_lib->next;
621 cur_lib->next = new_lib;
625 void cache_write(void)
629 char tempfile[BUFFER_SIZE];
635 sprintf(tempfile, "%s~", cachefile);
637 if (unlink(tempfile) && errno != ENOENT)
638 err(EXIT_FATAL,"can't unlink %s (%s)", tempfile, strerror(errno));
640 if ((cachefd = creat(tempfile, 0644)) < 0)
641 err(EXIT_FATAL,"can't create %s (%s)", tempfile, strerror(errno));
643 if (write(cachefd, &magic, sizeof (header_t)) != sizeof (header_t))
644 err(EXIT_FATAL,"can't write %s (%s)", tempfile, strerror(errno));
646 for (cur_lib = lib_head; cur_lib != NULL; cur_lib = cur_lib->next)
648 cur_lib->sooffset = stroffset;
649 stroffset += strlen(cur_lib->soname) + 1;
650 cur_lib->liboffset = stroffset;
651 stroffset += strlen(cur_lib->libname) + 1;
652 if (write(cachefd, cur_lib, sizeof (libentry_t)) !=
654 err(EXIT_FATAL,"can't write %s (%s)", tempfile, strerror(errno));
657 for (cur_lib = lib_head; cur_lib != NULL; cur_lib = cur_lib->next)
659 if (write(cachefd, cur_lib->soname, strlen(cur_lib->soname) + 1)
660 != strlen(cur_lib->soname) + 1)
661 err(EXIT_FATAL,"can't write %s (%s)", tempfile, strerror(errno));
662 if (write(cachefd, cur_lib->libname, strlen(cur_lib->libname) + 1)
663 != strlen(cur_lib->libname) + 1)
664 err(EXIT_FATAL,"can't write %s (%s)", tempfile, strerror(errno));
668 err(EXIT_FATAL,"can't close %s (%s)", tempfile, strerror(errno));
670 if (chmod(tempfile, 0644))
671 err(EXIT_FATAL,"can't chmod %s (%s)", tempfile, strerror(errno));
673 if (rename(tempfile, cachefile))
674 err(EXIT_FATAL,"can't rename %s (%s)", tempfile, strerror(errno));
677 void cache_print(void)
686 if (stat(cachefile, &st) || (fd = open(cachefile, O_RDONLY))<0)
687 err(EXIT_FATAL,"can't read %s (%s)", cachefile, strerror(errno));
688 if ((c = mmap(0,st.st_size, PROT_READ, MAP_SHARED ,fd, 0)) == (caddr_t)-1)
689 err(EXIT_FATAL,"can't map %s (%s)", cachefile, strerror(errno));
692 if (memcmp(((header_t *)c)->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN))
693 err(EXIT_FATAL,"%s cache corrupt", cachefile);
695 if (memcmp(((header_t *)c)->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN))
696 err(EXIT_FATAL,"wrong cache version - expected %s", LDSO_CACHE_VER);
698 header = (header_t *)c;
699 libent = (libentry_t *)(c + sizeof (header_t));
700 strs = (char *)&libent[header->nlibs];
702 printf("%d libs found in cache `%s' (version %s)\n",
703 header->nlibs, cachefile, LDSO_CACHE_VER);
705 for (fd = 0; fd < header->nlibs; fd++)
707 printf("\t%s ", strs + libent[fd].sooffset);
708 switch (libent[fd].flags & ~LIB_ELF64)
714 printf("(ELF%s)", libent[fd].flags & LIB_ELF64 ? "/64" : "");
717 printf("(libc0%s)",libent[fd].flags & LIB_ELF64 ? "/64" : "");
721 printf("(libc%d%s)", (libent[fd].flags & ~LIB_ELF64) + 3,
722 libent[fd].flags & LIB_ELF64 ? "/64" : "");
728 printf(" => %s\n", strs + libent[fd].liboffset);
731 munmap (c,st.st_size);
738 #ifdef __LDSO_CACHE_SUPPORT__
739 "ldconfig - updates symlinks and cache for shared libraries\n\n"
740 "Usage: ldconfig [-DvqnNX] [-f conf] [-C cache] [-r root] dir ...\n"
741 " ldconfig -l [-Dv] lib ...\n"
742 " ldconfig -p\n\nOptions:\n"
744 "ldconfig - updates symlinks for shared libraries\n\n"
745 "Usage: ldconfig [-DvqnX] [-r root] dir ...\n"
746 " ldconfig -l [-Dv] lib ...\n\nOptions:\n"
748 "\t-D:\t\tdebug mode, don't update links\n"
749 "\t-v:\t\tverbose mode, print things as we go\n"
750 "\t-q:\t\tquiet mode, don't print warnings\n"
751 "\t-n:\t\tdon't process standard directories\n"
752 "\t-N:\t\tdon't update the library cache\n"
753 "\t-X:\t\tdon't update the library links\n"
754 "\t-l:\t\tlibrary mode, manually link libraries\n"
755 "\t-p:\t\tprint the current library cache\n"
756 "\t-f conf :\tuse conf instead of %s\n"
757 "\t-C cache:\tuse cache instead of %s\n"
758 "\t-r root :\tfirst, do a chroot to the indicated directory\n"
759 "\tdir ... :\tdirectories to process\n"
760 #ifdef __LDSO_CACHE_SUPPORT__
761 "\tlib ... :\tlibraries to link\n\n",
762 LDSO_CONF, LDSO_CACHE
764 "\tlib ... :\tlibraries to link\n\n"
770 #define DIR_SEP ":, \t\n"
771 int main(int argc, char **argv)
777 char *chroot_dir = NULL;
784 while ((c = getopt(argc, argv, "DvqnNXlpf:C:r:")) != EOF)
788 debug = 1; /* debug mode */
794 verbose = 1; /* verbose mode */
798 verbose = -1; /* quiet mode */
801 nodefault = 1; /* no default dirs */
805 nocache = 1; /* don't build cache */
808 nolinks = 1; /* don't update links */
811 libmode = 1; /* library mode */
814 printcache = 1; /* print cache */
817 #ifdef __LDSO_CACHE_SUPPORT__
818 conffile = optarg; /* alternate conf file */
822 #ifdef __LDSO_CACHE_SUPPORT__
823 cachefile = optarg; /* alternate cache file */
833 /* THE REST OF THESE ARE UNDOCUMENTED AND MAY BE REMOVED
834 IN FUTURE VERSIONS. */
837 if (chroot_dir && *chroot_dir) {
838 if (chroot(chroot_dir) < 0)
839 err(EXIT_FATAL,"couldn't chroot to %s (%s)", chroot_dir, strerror(errno));
841 err(EXIT_FATAL,"couldn't chdir to / (%s)", strerror(errno));
844 /* allow me to introduce myself, hi, my name is ... */
846 printf("%s: uClibc version\n", argv[0]);
850 /* print the cache -- don't you trust me? */
856 /* so you want to do things manually, eh? */
858 /* ok, if you're so smart, which libraries do we link? */
859 for (i = optind; i < argc; i++)
861 /* split into directory and file parts */
862 if (!(cp = strrchr(argv[i], '/')))
864 dir = "."; /* no dir, only a filename */
870 dir = "/"; /* file in root directory */
873 *cp++ = '\0'; /* neither of the above */
876 /* we'd better do a little bit of checking */
877 if ((so = is_shlib(dir, cp, &libtype, &islink, LIB_ANY)) == NULL)
878 err(EXIT_FATAL,"%s%s%s is not a shared library", dir,
879 (*dir && strcmp(dir, "/")) ? "/" : "", cp);
881 /* so far, so good, maybe he knows what he's doing */
882 link_shlib(dir, cp, so);
887 /* the lazy bum want's us to do all the work for him */
889 /* don't cache dirs on the command line */
890 int nocache_save = nocache;
893 /* OK, which directories should we do? */
894 for (i = optind; i < argc; i++)
897 /* restore the desired caching state */
898 nocache = nocache_save;
900 /* look ma, no defaults */
903 scan_dir(UCLIBC_RUNTIME_PREFIX "lib");
904 scan_dir(UCLIBC_RUNTIME_PREFIX "usr/lib");
905 #ifndef __LDSO_CACHE_SUPPORT__
906 scan_dir(UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib");
908 /* I guess the defaults aren't good enough */
909 if ((extpath = get_extpath()))
911 for (cp = strtok(extpath, DIR_SEP); cp; cp = strtok(NULL, DIR_SEP)) {
912 /* strip trailing slashes */
913 int len = strlen(cp);
915 while (cp[--len] == '/' && len)
917 /* we do the redundancy check only if cache usage is enabled */
918 if (strcmp(UCLIBC_RUNTIME_PREFIX "lib", cp) == 0 ||
919 strcmp(UCLIBC_RUNTIME_PREFIX "usr/lib", cp) == 0) {
921 warnx("Remove `%s' from `%s'\n", cp, LDSO_CONF);