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"
45 unsigned long a_info; /* Use macros N_MAGIC, etc for access */
46 unsigned a_text; /* length of text, in bytes */
47 unsigned a_data; /* length of data, in bytes */
48 unsigned a_bss; /* length of uninitialized data area for file, in bytes */
49 unsigned a_syms; /* length of symbol table data in file, in bytes */
50 unsigned a_entry; /* start address */
51 unsigned a_trsize; /* length of relocation info for text, in bytes */
52 unsigned a_drsize; /* length of relocation info for data, in bytes */
55 #if !defined (N_MAGIC)
56 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
58 /* Code indicating object file or impure executable. */
60 /* Code indicating pure executable. */
62 /* Code indicating demand-paged executable. */
64 /* This indicates a demand-paged executable with the header in the text.
65 The first page is unmapped to help trap NULL pointer references */
67 /* Code indicating core file. */
70 char *___strtok = NULL;
75 #define PATH_MAX _POSIX_PATH_MAX
80 #define N_MAGIC(exec) ((exec).a_magic & 0xffff)
84 #define EXIT_FATAL 128
87 int debug = 0; /* debug mode */
88 int verbose = 0; /* verbose mode */
89 int libmode = 0; /* library mode */
90 int nocache = 0; /* don't build cache */
91 int nolinks = 0; /* don't update links */
93 char *conffile = LDSO_CONF; /* default conf file */
94 char *cachefile = LDSO_CACHE; /* default cache file */
95 void cache_print(void);
96 void cache_dolib(const char *dir, const char *so, int libtype);
97 void cache_write(void);
99 /* These two are used internally -- you shouldn't need to use them */
100 static void verror_msg(const char *s, va_list p)
103 fprintf(stderr, "%s: ", prog);
104 vfprintf(stderr, s, p);
107 extern void warnx(const char *s, ...)
114 fprintf(stderr, "\n");
117 extern void err(int errnum, const char *s, ...)
124 fprintf(stderr, "\n");
128 static void vperror_msg(const char *s, va_list p)
137 fprintf(stderr, "%s%s\n", s, strerror(err));
140 extern void warn(const char *s, ...)
149 void *xmalloc(size_t size)
152 if ((ptr = malloc(size)) == NULL)
153 err(EXIT_FATAL,"out of memory");
157 char *xstrdup(const char *str)
160 if ((ptr = strdup(str)) == NULL)
161 err(EXIT_FATAL,"out of memory");
165 /* If shared library, return a malloced copy of the soname and set the
166 type, else return NULL.
168 expected_type should be either LIB_ANY or one of the following:-
174 If the lib is ELF and we can not deduce the type the type will
175 be set based on expected_type.
177 If the expected, actual/deduced types missmatch we display a warning
178 and use the actual/deduced type.
180 char *is_shlib(const char *dir, const char *name, int *type,
181 int *islink, int expected_type)
191 /* see if name is of the form *.so* */
192 if (name[strlen(name)-1] != '~' && (cp = strstr(name, ".so")))
194 /* find the start of the Vminor part, if any */
195 if (cp[3] == '.' && (cp2 = strchr(cp + 4, '.')))
198 cp = cp + strlen(cp);
200 /* construct the full path name */
201 sprintf(buff, "%s%s%s", dir, (*dir && strcmp(dir, "/")) ?
204 /* first, make sure it's a regular file */
205 if (lstat(buff, &statbuf))
206 warn("skipping %s", buff);
207 else if (!S_ISREG(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode))
208 warnx("%s is not a regular file or symlink, skipping", buff);
211 /* is it a regular file or a symlink */
212 *islink = S_ISLNK(statbuf.st_mode);
214 /* then try opening it */
215 if (!(file = fopen(buff, "rb")))
216 warn("skipping %s", buff);
219 /* now make sure it's a shared library */
220 if (fread(&exec, sizeof exec, 1, file) < 1)
221 warnx("can't read header from %s, skipping", buff);
222 else if (N_MAGIC(exec) != ZMAGIC && N_MAGIC(exec) != QMAGIC)
224 elf_hdr = (ElfW(Ehdr) *) &exec;
225 if (elf_hdr->e_ident[0] != 0x7f ||
226 strncmp(&elf_hdr->e_ident[1], "ELF",3) != 0)
228 /* silently ignore linker scripts */
229 if (strncmp((char *)&exec, "/* GNU ld", 9) != 0)
230 warnx("%s is not a shared library, skipping", buff);
234 /* always call readsoname to update type */
235 if(expected_type == LIB_DLL) {
236 warnx("%s is not an a.out library, its ELF!\n", buff);
237 expected_type=LIB_ANY;
240 good = readsoname(buff, file, expected_type, type,
241 elf_hdr->e_ident[EI_CLASS]);
242 if (good == NULL || *islink)
246 good = xstrdup(name);
250 /* if the soname does not match the filename,
251 issue a warning, but only in debug mode. */
252 int len = strlen(good);
253 if (debug && (strncmp(good, name, len) != 0 ||
254 (name[len] != '\0' && name[len] != '.')))
255 warnx("%s has inconsistent soname (%s)", buff, good);
262 good = xstrdup(name);
265 good = xmalloc(cp - name + 1);
266 strncpy(good, name, cp - name);
267 good[cp - name] = '\0';
269 if(expected_type != LIB_ANY && expected_type != LIB_DLL)
271 warnx("%s is not an ELF library, its an a.out DLL!", buff);
272 expected_type=LIB_ANY;
285 /* update the symlink to new library */
286 void link_shlib(const char *dir, const char *file, const char *so)
292 struct stat linkstat;
294 /* construct the full path names */
295 sprintf(libname, "%s/%s", dir, file);
296 sprintf(linkname, "%s/%s", dir, so);
298 /* see if a link already exists */
299 if (!stat(linkname, &linkstat))
301 /* now see if it's the one we want */
302 if (stat(libname, &libstat))
303 warn("can't stat %s", libname);
304 else if (libstat.st_dev == linkstat.st_dev &&
305 libstat.st_ino == linkstat.st_ino)
309 /* then update the link, if required */
310 if (change > 0 && !nolinks)
312 if (!lstat(linkname, &linkstat))
314 if (!S_ISLNK(linkstat.st_mode))
316 warnx("%s is not a symlink", linkname);
319 else if (remove(linkname))
321 warn("can't unlink %s", linkname);
327 if (symlink(file, linkname))
329 warn("can't link %s to %s", linkname, file);
335 /* some people like to know what we're doing */
337 printf("\t%s => %s%s\n", so, file,
338 change < 0 ? " (SKIPPED)" :
339 (change > 0 ? " (changed)" : ""));
344 /* figure out which library is greater */
345 int libcmp(char *p1, char *p2)
349 if (isdigit(*p1) && isdigit(*p2))
351 /* must compare this numerically */
353 v1 = strtoul(p1, &p1, 10);
354 v2 = strtoul(p2, &p2, 10);
358 else if (isdigit(*p1) && !isdigit(*p2))
360 else if (!isdigit(*p1) && isdigit(*p2))
373 char *so; /* soname of a library */
374 char *name; /* name of a library */
375 int libtype; /* type of a library */
376 int islink; /* is it a symlink */
377 struct lib *next; /* next library in list */
380 /* update all shared library links in a directory */
381 void scan_dir(const char *rawname)
386 char *so, *path, *path_n;
387 struct lib *lp, *libs = NULL;
388 int i, libtype, islink, expected_type = LIB_ANY;
390 /* We need a writable copy of this string */
391 path = strdup(rawname);
393 err(EXIT_FATAL, "Out of memory!\n");
395 /* Eliminate all double //s */
397 while((path_n=strstr(path_n, "//"))) {
399 memmove(path_n, path_n+1, i-1);
400 *(path_n + i - 1)='\0';
406 /* Check for an embedded expected type */
407 t=strrchr(name, '=');
410 *t++ = '\0'; /* Skip = char */
411 if(strcasecmp(t, "libc4") == 0)
413 expected_type = LIB_DLL;
417 if(strcasecmp(t, "libc5") == 0)
419 expected_type = LIB_ELF_LIBC5;
423 if(strcasecmp(t, "libc6") == 0)
425 expected_type = LIB_ELF_LIBC6;
429 if(strcasecmp(t, "libc0") == 0)
431 expected_type = LIB_ELF_LIBC0;
435 warnx("Unknown type field '%s' for dir '%s' - ignored\n", t, name);
436 expected_type = LIB_ANY;
444 /* let 'em know what's going on */
446 printf("%s:\n", name);
448 /* if we can't open it, we can't do anything */
449 if ((dir = opendir(name)) == NULL)
451 warn("skipping %s", name);
456 /* yes, we have to look at every single file */
457 while ((ent = readdir(dir)) != NULL)
459 /* if it's not a shared library, don't bother */
460 if ((so = is_shlib(name, ent->d_name, &libtype, &islink, expected_type)) == NULL)
463 /* have we already seen one with the same so name? */
464 for (lp = libs; lp; lp = lp->next)
466 if (strcmp(so, lp->so) == 0)
468 /* we have, which one do we want to use? */
469 if ((!islink && lp->islink) ||
470 (islink == lp->islink &&
471 libcmp(ent->d_name, lp->name) > 0))
473 /* let's use the new one */
475 lp->name = xstrdup(ent->d_name);
476 lp->libtype = libtype;
483 /* congratulations, you're the first one we've seen */
486 lp = xmalloc(sizeof *lp);
487 lp->so = xstrdup(so);
488 lp->name = xstrdup(ent->d_name);
489 lp->libtype = libtype;
498 /* don't need this any more */
501 /* now we have all the latest libs, update the links */
502 for (lp = libs; lp; lp = lp->next)
505 link_shlib(name, lp->name, lp->so);
508 cache_dolib(name, lp->so, lp->libtype);
512 /* always try to clean up after ourselves */
526 /* return the list of system-specific directories */
527 char *get_extpath(void)
529 char *res = NULL, *cp;
533 if ((file = fopen(conffile, "r")) != NULL)
535 fstat(fileno(file), &stat);
536 res = xmalloc(stat.st_size + 1);
537 fread(res, 1, stat.st_size, file);
539 res[stat.st_size] = '\0';
541 /* convert comments fo spaces */
542 for (cp = res; *cp; /*nada*/) {
546 while (*cp && *cp != '\n');
557 typedef struct liblist
564 struct liblist *next;
567 static header_t magic = { LDSO_CACHE_MAGIC, LDSO_CACHE_VER, 0 };
568 static liblist_t *lib_head = NULL;
570 static int liblistcomp(liblist_t *x, liblist_t *y)
574 if ((res = libcmp(x->soname, y->soname)) == 0)
576 res = libcmp(strrchr(x->libname, '/') + 1,
577 strrchr(y->libname, '/') + 1);
583 void cache_dolib(const char *dir, const char *so, int libtype)
585 char fullpath[PATH_MAX];
586 liblist_t *new_lib, *cur_lib;
589 sprintf(fullpath, "%s/%s", dir, so);
590 new_lib = xmalloc(sizeof (liblist_t));
591 new_lib->flags = libtype;
592 new_lib->soname = xstrdup(so);
593 new_lib->libname = xstrdup(fullpath);
595 if (lib_head == NULL || liblistcomp(new_lib, lib_head) > 0)
597 new_lib->next = lib_head;
602 for (cur_lib = lib_head; cur_lib->next != NULL &&
603 liblistcomp(new_lib, cur_lib->next) <= 0;
604 cur_lib = cur_lib->next)
606 new_lib->next = cur_lib->next;
607 cur_lib->next = new_lib;
611 void cache_write(void)
621 sprintf(tempfile, "%s~", cachefile);
623 if (unlink(tempfile) && errno != ENOENT)
624 err(EXIT_FATAL,"can't unlink %s (%s)", tempfile, strerror(errno));
626 if ((cachefd = creat(tempfile, 0644)) < 0)
627 err(EXIT_FATAL,"can't create %s (%s)", tempfile, strerror(errno));
629 if (write(cachefd, &magic, sizeof (header_t)) != sizeof (header_t))
630 err(EXIT_FATAL,"can't write %s (%s)", tempfile, strerror(errno));
632 for (cur_lib = lib_head; cur_lib != NULL; cur_lib = cur_lib->next)
634 cur_lib->sooffset = stroffset;
635 stroffset += strlen(cur_lib->soname) + 1;
636 cur_lib->liboffset = stroffset;
637 stroffset += strlen(cur_lib->libname) + 1;
638 if (write(cachefd, cur_lib, sizeof (libentry_t)) !=
640 err(EXIT_FATAL,"can't write %s (%s)", tempfile, strerror(errno));
643 for (cur_lib = lib_head; cur_lib != NULL; cur_lib = cur_lib->next)
645 if (write(cachefd, cur_lib->soname, strlen(cur_lib->soname) + 1)
646 != strlen(cur_lib->soname) + 1)
647 err(EXIT_FATAL,"can't write %s (%s)", tempfile, strerror(errno));
648 if (write(cachefd, cur_lib->libname, strlen(cur_lib->libname) + 1)
649 != strlen(cur_lib->libname) + 1)
650 err(EXIT_FATAL,"can't write %s (%s)", tempfile, strerror(errno));
654 err(EXIT_FATAL,"can't close %s (%s)", tempfile, strerror(errno));
656 if (chmod(tempfile, 0644))
657 err(EXIT_FATAL,"can't chmod %s (%s)", tempfile, strerror(errno));
659 if (rename(tempfile, cachefile))
660 err(EXIT_FATAL,"can't rename %s (%s)", tempfile, strerror(errno));
663 void cache_print(void)
672 if (stat(cachefile, &st) || (fd = open(cachefile, O_RDONLY))<0)
673 err(EXIT_FATAL,"can't read %s (%s)", cachefile, strerror(errno));
674 if ((c = mmap(0,st.st_size, PROT_READ, MAP_SHARED ,fd, 0)) == (caddr_t)-1)
675 err(EXIT_FATAL,"can't map %s (%s)", cachefile, strerror(errno));
678 if (memcmp(((header_t *)c)->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN))
679 err(EXIT_FATAL,"%s cache corrupt", cachefile);
681 if (memcmp(((header_t *)c)->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN))
682 err(EXIT_FATAL,"wrong cache version - expected %s", LDSO_CACHE_VER);
684 header = (header_t *)c;
685 libent = (libentry_t *)(c + sizeof (header_t));
686 strs = (char *)&libent[header->nlibs];
688 printf("%d libs found in cache `%s' (version %s)\n",
689 header->nlibs, cachefile, LDSO_CACHE_VER);
691 for (fd = 0; fd < header->nlibs; fd++)
693 printf("\t%s ", strs + libent[fd].sooffset);
694 switch (libent[fd].flags & ~LIB_ELF64)
700 printf("(ELF%s)", libent[fd].flags & LIB_ELF64 ? "/64" : "");
704 printf("(libc%d%s)", (libent[fd].flags & ~LIB_ELF64) + 3,
705 libent[fd].flags & LIB_ELF64 ? "/64" : "");
711 printf(" => %s\n", strs + libent[fd].liboffset);
714 munmap (c,st.st_size);
717 void cache_print(void)
719 warnx("Cache support disabled\n");
726 "ldconfig - updates symlinks for shared libraries\n\n"
727 "Usage: ldconfig [-DvqnNX] [-f conf] [-C cache] [-r root] dir ...\n"
728 " ldconfig -l [-Dv] lib ...\n"
729 " ldconfig -p\n\nOptions:\n"
730 "\t-D:\t\tdebug mode, don't update links\n"
731 "\t-v:\t\tverbose mode, print things as we go\n"
732 "\t-q:\t\tquiet mode, don't print warnings\n"
733 "\t-n:\t\tdon't process standard directories\n"
734 "\t-N:\t\tdon't update the library cache\n"
735 "\t-X:\t\tdon't update the library links\n"
736 "\t-l:\t\tlibrary mode, manually link libraries\n"
737 "\t-p:\t\tprint the current library cache\n"
738 "\t-f conf :\tuse conf instead of %s\n"
739 "\t-C cache:\tuse cache instead of %s\n"
740 "\t-r root :\tfirst, do a chroot to the indicated directory\n"
741 "\tdir ... :\tdirectories to process\n"
742 "\tlib ... :\tlibraries to link\n\n",
743 LDSO_CONF, LDSO_CACHE
748 #define DIR_SEP ":, \t\n"
749 int main(int argc, char **argv)
757 char *chroot_dir = NULL;
762 while ((c = getopt(argc, argv, "DvqnNXlpf:C:r:")) != EOF)
766 debug = 1; /* debug mode */
772 verbose = 1; /* verbose mode */
776 verbose = -1; /* quiet mode */
779 nodefault = 1; /* no default dirs */
783 nocache = 1; /* don't build cache */
786 nolinks = 1; /* don't update links */
789 libmode = 1; /* library mode */
792 printcache = 1; /* print cache */
795 conffile = optarg; /* alternate conf file */
798 cachefile = optarg; /* alternate cache file */
807 /* THE REST OF THESE ARE UNDOCUMENTED AND MAY BE REMOVED
808 IN FUTURE VERSIONS. */
811 if (chroot_dir && *chroot_dir) {
812 if (chroot(chroot_dir) < 0)
813 err(EXIT_FATAL,"couldn't chroot to %s (%s)", chroot_dir, strerror(errno));
815 err(EXIT_FATAL,"couldn't chdir to / (%s)", strerror(errno));
818 /* allow me to introduce myself, hi, my name is ... */
820 printf("%s: uClibc version\n", argv[0]);
824 /* print the cache -- don't you trust me? */
830 /* so you want to do things manually, eh? */
832 /* ok, if you're so smart, which libraries do we link? */
833 for (i = optind; i < argc; i++)
835 /* split into directory and file parts */
836 if (!(cp = strrchr(argv[i], '/')))
838 dir = "."; /* no dir, only a filename */
844 dir = "/"; /* file in root directory */
847 *cp++ = '\0'; /* neither of the above */
850 /* we'd better do a little bit of checking */
851 if ((so = is_shlib(dir, cp, &libtype, &islink, LIB_ANY)) == NULL)
852 err(EXIT_FATAL,"%s%s%s is not a shared library", dir,
853 (*dir && strcmp(dir, "/")) ? "/" : "", cp);
855 /* so far, so good, maybe he knows what he's doing */
856 link_shlib(dir, cp, so);
861 /* the lazy bum want's us to do all the work for him */
863 /* don't cache dirs on the command line */
864 int nocache_save = nocache;
867 /* OK, which directories should we do? */
868 for (i = optind; i < argc; i++)
871 /* restore the desired caching state */
872 nocache = nocache_save;
874 /* look ma, no defaults */
877 /* I guess the defaults aren't good enough */
878 if ((extpath = get_extpath()))
880 for (cp = strtok(extpath, DIR_SEP); cp;
881 cp = strtok(NULL, DIR_SEP))
886 scan_dir(UCLIBC_RUNTIME_PREFIX "/usr/X11R6/lib");
887 scan_dir(UCLIBC_RUNTIME_PREFIX "/usr/lib");
888 scan_dir(UCLIBC_RUNTIME_PREFIX "/lib");