Only emulate fstatfs64 if __NR_fstatfs64 is not defined
[uclibc-ng.git] / utils / ldconfig.c
blob58939d689dc93bd04c540f337c9aad7019f75124
1 /*
2 * ldconfig - update shared library symlinks
4 * usage: ldconfig [-DvqnNX] [-f conf] [-C cache] [-r root] dir ...
5 * ldconfig -l [-Dv] lib ...
6 * ldconfig -p
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.
26 * 2005/09/16: Dan Howell (modified for cross-development)
29 #include "porting.h"
31 #define BUFFER_SIZE 4096
33 struct exec {
34 unsigned long a_info; /* Use macros N_MAGIC, etc for access */
35 unsigned a_text; /* length of text, in bytes */
36 unsigned a_data; /* length of data, in bytes */
37 unsigned a_bss; /* length of uninitialized data area for file, in bytes */
38 unsigned a_syms; /* length of symbol table data in file, in bytes */
39 unsigned a_entry; /* start address */
40 unsigned a_trsize; /* length of relocation info for text, in bytes */
41 unsigned a_drsize; /* length of relocation info for data, in bytes */
44 #if !defined (N_MAGIC)
45 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
46 #endif
47 #define N_MAGIC_SWAP(exec) (bswap_32((exec).a_info) & 0xffff)
48 /* Code indicating object file or impure executable. */
49 #define OMAGIC 0407
50 /* Code indicating pure executable. */
51 #define NMAGIC 0410
52 /* Code indicating demand-paged executable. */
53 #define ZMAGIC 0413
54 /* This indicates a demand-paged executable with the header in the text.
55 The first page is unmapped to help trap NULL pointer references */
56 #define QMAGIC 0314
57 /* Code indicating core file. */
58 #define CMAGIC 0421
60 char *prog = NULL;
61 int debug = 0; /* debug mode */
62 int verbose = 0; /* verbose mode */
63 int libmode = 0; /* library mode */
64 int nolinks = 0; /* don't update links */
65 int nocache = 0; /* don't build cache */
66 void cache_print(void);
67 void cache_write(void);
68 void cache_dolib(const char *dir, const char *so, int libtype);
69 #ifdef __LDSO_CACHE_SUPPORT__
70 char *conffile = LDSO_CONF; /* default conf file */
71 char *cachefile = LDSO_CACHE; /* default cache file */
72 #endif
73 char *chroot_dir = NULL;
74 int byteswap = 0;
76 struct needed_tab {
77 char *soname;
78 int type;
81 struct needed_tab needed_tab[] = {
82 {"libc.so.0", LIB_ELF_LIBC0},
83 {"libm.so.0", LIB_ELF_LIBC0},
84 {"libdl.so.0", LIB_ELF_LIBC0},
85 {"libc.so.5", LIB_ELF_LIBC5},
86 {"libm.so.5", LIB_ELF_LIBC5},
87 {"libdl.so.1", LIB_ELF_LIBC5},
88 {"libc.so.6", LIB_ELF_LIBC6},
89 {"libm.so.6", LIB_ELF_LIBC6},
90 {"libdl.so.2", LIB_ELF_LIBC6},
91 {NULL, LIB_ELF}
94 extern char *chroot_realpath(const char *chroot, const char *path,
95 char resolved_path[]);
97 #if defined __UCLIBC_STATIC_LDCONFIG__ || !defined __UCLIBC_HAS_BSD_ERR__
98 /* These two are used internally -- you shouldn't need to use them */
99 static void verror_msg(const char *s, va_list p)
101 fflush(stdout);
102 fprintf(stderr, "%s: ", prog);
103 vfprintf(stderr, s, p);
106 static void warnx(const char *s, ...)
108 va_list p;
110 va_start(p, s);
111 verror_msg(s, p);
112 va_end(p);
113 fprintf(stderr, "\n");
116 static attribute_noreturn void err(int errnum, const char *s, ...)
118 va_list p;
120 va_start(p, s);
121 verror_msg(s, p);
122 va_end(p);
123 fprintf(stderr, "\n");
124 exit(errnum);
127 static void vperror_msg(const char *s, va_list p)
129 int e = errno;
131 if (s == 0)
132 s = "";
133 verror_msg(s, p);
134 if (*s)
135 s = ": ";
136 fprintf(stderr, "%s%s\n", s, strerror(e));
139 static void warn(const char *s, ...)
141 va_list p;
143 va_start(p, s);
144 vperror_msg(s, p);
145 va_end(p);
147 #else
148 # include <err.h>
149 #endif
151 static void *xmalloc(size_t size)
153 void *ptr;
154 if ((ptr = malloc(size)) == NULL)
155 err(EXIT_FAILURE, "out of memory");
156 return ptr;
159 static char *xstrdup(const char *str)
161 char *ptr;
162 if ((ptr = strdup(str)) == NULL)
163 err(EXIT_FAILURE, "out of memory");
164 return ptr;
167 #undef __ELF_NATIVE_CLASS
168 #undef readsonameXX
169 #define readsonameXX readsoname32
170 #define __ELF_NATIVE_CLASS 32
171 #include "readsoname2.c"
173 #undef __ELF_NATIVE_CLASS
174 #undef readsonameXX
175 #define readsonameXX readsoname64
176 #define __ELF_NATIVE_CLASS 64
177 #include "readsoname2.c"
178 static char *readsoname(char *name, FILE *infile, int expected_type,
179 int *type, int elfclass)
181 char *res;
183 if (elfclass == ELFCLASS32)
184 res = readsoname32(name, infile, expected_type, type);
185 else {
186 res = readsoname64(name, infile, expected_type, type);
188 // For 64-bit glibc compatibility
189 *type |= FLAG_X8664_LIB64;
192 return res;
195 /* If shared library, return a malloced copy of the soname and set the
196 * type, else return NULL.
198 * expected_type should be either LIB_ANY or one of the following:-
199 * LIB_DLL
200 * LIB_ELF
201 * LIB_ELF_LIBC5
202 * LIB_ELF_LIBC6
204 * If the lib is ELF and we can not deduce the type the type will
205 * be set based on expected_type.
207 * If the expected, actual/deduced types missmatch we display a warning
208 * and use the actual/deduced type.
210 static char *is_shlib(const char *dir, const char *name, int *type,
211 int *islink, int expected_type)
213 char *good = NULL;
214 char *cp, *cp2;
215 FILE *file;
216 struct exec exec;
217 ElfW(Ehdr) *elf_hdr;
218 struct stat statbuf;
219 char buff[BUFFER_SIZE];
220 char real[BUFFER_SIZE];
221 static int byteswapflag = -1; /* start with byte-order unknown */
223 /* see if name is of the form *.so* */
224 if (name[strlen(name) - 1] != '~' && (cp = strstr(name, ".so"))) {
225 /* find the start of the Vminor part, if any */
226 if (cp[3] == '.' && (cp2 = strchr(cp + 4, '.')))
227 cp = cp2;
228 else
229 cp = cp + strlen(cp);
231 /* construct the full path name */
232 sprintf(buff, "%s%s%s", dir, (*dir && strcmp(dir, "/")) ? "/" : "", name);
234 /* get real path in case of chroot */
235 if (!chroot_realpath(chroot_dir, buff, real))
236 warn("can't resolve %s in chroot %s", buff, chroot_dir);
238 /* first, make sure it's a regular file */
239 if (lstat(real, &statbuf))
240 warn("skipping %s", buff);
241 else if (!S_ISREG(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode))
242 warnx("%s is not a regular file or symlink, skipping", buff);
243 else {
244 /* is it a regular file or a symlink */
245 *islink = S_ISLNK(statbuf.st_mode);
247 /* then try opening it */
248 if (!(file = fopen(real, "rb")))
249 warn("skipping %s", buff);
250 else {
251 /* now make sure it's a shared library */
252 if (fread(&exec, sizeof exec, 1, file) < 1)
253 warnx("can't read header from %s, skipping", buff);
254 else if (N_MAGIC(exec) != ZMAGIC
255 && N_MAGIC(exec) != QMAGIC
256 && N_MAGIC_SWAP(exec) != ZMAGIC
257 && N_MAGIC_SWAP(exec) != QMAGIC) {
258 elf_hdr = (ElfW(Ehdr) *) & exec;
259 if (elf_hdr->e_ident[0] != 0x7f ||
260 strncmp((const char *)elf_hdr->e_ident + 1, "ELF", 3) != 0)
262 /* silently ignore linker scripts */
263 if (strncmp((const char *)&exec, "/* GNU ld", 9) != 0)
264 warnx("%s is not a shared library, skipping", buff);
265 } else {
266 /* always call readsoname to update type */
267 if (expected_type == LIB_DLL) {
268 warnx("%s is not an a.out library, it's ELF!", buff);
269 expected_type = LIB_ANY;
271 *type = LIB_ELF;
272 good = readsoname(buff, file, expected_type, type,
273 elf_hdr->e_ident[EI_CLASS]);
274 if (byteswapflag == -1)
275 /* byte-order detected */
276 byteswapflag = byteswap;
277 if (good == NULL || *islink) {
278 if (good != NULL)
279 free(good);
280 good = xstrdup(name);
281 } else {
282 /* if the soname does not match the filename,
283 issue a warning, but only in debug mode. */
284 int len = strlen(good);
285 if (debug && (strncmp((const char *)good, name, len) != 0
286 || (name[len] != '\0' && name[len] != '.')))
287 warnx("%s has inconsistent soname (%s)", buff, good);
290 } else {
291 /* Determine byte-order */
292 byteswap = (N_MAGIC(exec) == ZMAGIC || N_MAGIC(exec) == QMAGIC) ? 0 : 1;
293 if (byteswapflag == -1)
294 /* byte-order detected */
295 byteswapflag = byteswap;
297 if (*islink)
298 good = xstrdup(name);
299 else {
300 good = xmalloc(cp - name + 1);
301 strncpy(good, name, cp - name);
302 good[cp - name] = '\0';
304 if (expected_type != LIB_ANY && expected_type != LIB_DLL) {
305 warnx("%s is not an ELF library, its an a.out DLL!", buff);
306 expected_type = LIB_ANY;
309 *type = LIB_DLL;
311 fclose(file);
313 if (byteswapflag >= 0 && byteswap != byteswapflag) {
314 byteswapflag = -2;
315 warnx("mixed byte-order detected, using host byte-order...");
317 if (byteswapflag == -2)
318 byteswap = 0;
323 return good;
326 /* update the symlink to new library */
327 static void link_shlib(const char *dir, const char *file, const char *so)
329 int change = 1;
330 char libname[BUFFER_SIZE];
331 char linkname[BUFFER_SIZE];
332 char reallibname[BUFFER_SIZE];
333 char reallinkname[BUFFER_SIZE];
334 struct stat libstat;
335 struct stat linkstat;
337 /* construct the full path names */
338 sprintf(libname, "%s/%s", dir, file);
339 sprintf(linkname, "%s/%s", dir, so);
340 if (!chroot_realpath(chroot_dir, libname, reallibname))
341 warn("can't resolve %s in chroot %s", libname, chroot_dir);
342 if (!chroot_realpath(chroot_dir, linkname, reallinkname))
343 warn("can't resolve %s in chroot %s", linkname, chroot_dir);
345 /* see if a link already exists */
346 if (!stat(reallinkname, &linkstat)) {
347 /* now see if it's the one we want */
348 if (stat(reallibname, &libstat))
349 warn("can't stat %s", libname);
350 else if (libstat.st_dev == linkstat.st_dev &&
351 libstat.st_ino == linkstat.st_ino)
352 change = 0;
355 /* then update the link, if required */
356 if (change > 0 && !nolinks) {
357 if (!lstat(reallinkname, &linkstat)) {
358 if (!S_ISLNK(linkstat.st_mode)) {
359 warnx("%s is not a symlink", linkname);
360 change = -1;
361 } else if (remove(reallinkname)) {
362 warn("can't unlink %s", linkname);
363 change = -1;
366 if (change > 0) {
367 if (symlink(file, reallinkname)) {
368 warn("can't link %s to %s", linkname, file);
369 change = -1;
374 /* some people like to know what we're doing */
375 if (verbose > 0)
376 printf("\t%s => %s%s\n", so, file,
377 change < 0 ? " (SKIPPED)" :
378 (change > 0 ? " (changed)" : ""));
380 return;
383 /* figure out which library is greater */
384 static int libcmp(char *p1, char *p2)
386 while (*p1) {
387 if (isdigit(*p1) && isdigit(*p2)) {
388 /* must compare this numerically */
389 int v1, v2;
390 v1 = strtoul(p1, &p1, 10);
391 v2 = strtoul(p2, &p2, 10);
392 if (v1 != v2)
393 return v1 - v2;
394 } else if (isdigit(*p1) && !isdigit(*p2))
395 return 1;
396 else if (!isdigit(*p1) && isdigit(*p2))
397 return -1;
398 else if (*p1 != *p2)
399 return *p1 - *p2;
400 else
401 p1++, p2++;
404 return *p1 - *p2;
407 struct lib {
408 char *so; /* soname of a library */
409 char *name; /* name of a library */
410 int libtype; /* type of a library */
411 int islink; /* is it a symlink */
412 struct lib *next; /* next library in list */
415 /* update all shared library links in a directory */
416 static void scan_dir(const char *rawname)
418 DIR *dir;
419 const char *name;
420 struct dirent *ent;
421 char *so, *path, *path_n;
422 struct lib *lp, *libs = NULL;
423 int i, libtype, islink, expected_type = LIB_ANY;
424 char realname[BUFFER_SIZE];
426 /* We need a writable copy of this string */
427 path = strdup(rawname);
428 if (!path) {
429 err(EXIT_FAILURE, "Out of memory!\n");
431 /* Eliminate all double //s */
432 path_n = path;
433 while ((path_n = strstr(path_n, "//"))) {
434 i = strlen(path_n);
435 memmove(path_n, path_n + 1, i - 1);
436 *(path_n + i - 1) = '\0';
438 name = path;
440 #if 0
441 char *t;
442 /* Check for an embedded expected type */
443 t = strrchr(name, '=');
444 if (t) {
445 *t++ = '\0'; /* Skip = char */
446 if (strcasecmp(t, "libc4") == 0) {
447 expected_type = LIB_DLL;
448 } else {
449 if (strcasecmp(t, "libc5") == 0) {
450 expected_type = LIB_ELF_LIBC5;
451 } else {
452 if (strcasecmp(t, "libc6") == 0) {
453 expected_type = LIB_ELF_LIBC6;
454 } else {
455 if (strcasecmp(t, "libc0") == 0) {
456 expected_type = LIB_ELF_LIBC0;
457 } else {
458 warnx("Unknown type field '%s' for dir '%s' - ignored", t, name);
459 expected_type = LIB_ANY;
465 #endif
467 /* let 'em know what's going on */
468 if (verbose > 0)
469 printf("%s:\n", name);
471 /* get real path in case of chroot */
472 if (!chroot_realpath(chroot_dir, name, realname))
473 warn("can't resolve %s in chroot %s", name, chroot_dir);
475 /* if we can't open it, we can't do anything */
476 if ((dir = opendir(realname)) == NULL) {
477 warn("skipping %s", name);
478 free(path);
479 return;
482 /* yes, we have to look at every single file */
483 while ((ent = readdir(dir)) != NULL) {
484 /* if it's not a shared library, don't bother */
485 if ((so = is_shlib(name, ent->d_name, &libtype, &islink, expected_type)) == NULL)
486 continue;
488 /* have we already seen one with the same so name? */
489 for (lp = libs; lp; lp = lp->next) {
490 if (strcmp(so, lp->so) == 0) {
491 /* we have, which one do we want to use? */
492 if ((!islink && lp->islink) ||
493 (islink == lp->islink &&
494 libcmp(ent->d_name, lp->name) > 0)) {
495 /* let's use the new one */
496 free(lp->name);
497 lp->name = xstrdup(ent->d_name);
498 lp->libtype = libtype;
499 lp->islink = islink;
501 break;
505 /* congratulations, you're the first one we've seen */
506 if (!lp) {
507 lp = xmalloc(sizeof *lp);
508 lp->so = xstrdup(so);
509 lp->name = xstrdup(ent->d_name);
510 lp->libtype = libtype;
511 lp->islink = islink;
512 lp->next = libs;
513 libs = lp;
516 free(so);
519 /* don't need this any more */
520 closedir(dir);
522 /* now we have all the latest libs, update the links */
523 for (lp = libs; lp; lp = lp->next) {
524 if (!lp->islink)
525 link_shlib(name, lp->name, lp->so);
526 if (!nocache)
527 cache_dolib(name, lp->so, lp->libtype);
530 /* always try to clean up after ourselves */
531 while (libs) {
532 lp = libs->next;
533 free(libs->so);
534 free(libs->name);
535 free(libs);
536 libs = lp;
539 free(path);
540 return;
543 #ifndef __LDSO_CACHE_SUPPORT__
544 void cache_print(void)
546 printf("Library cache disabled\n");
548 void cache_dolib(const char *dir, const char *so, int libtype)
550 return;
552 void cache_write(void)
554 return;
556 #else
557 /* return the list of system-specific directories */
558 static char *get_extpath(void)
560 char *res = NULL, *cp;
561 FILE *file;
562 struct stat st;
563 char realconffile[BUFFER_SIZE];
565 if (!chroot_realpath(chroot_dir, conffile, realconffile))
566 return NULL;
568 if ((file = fopen(realconffile, "r")) != NULL) {
569 fstat(fileno(file), &st);
570 res = xmalloc(st.st_size + 1);
571 (void)fread(res, 1, st.st_size, file);
572 fclose(file);
573 res[st.st_size] = '\0';
575 /* convert comments fo spaces */
576 for (cp = res; *cp; /*nada */ ) {
577 if (*cp == '#') {
579 *cp++ = ' ';
580 while (*cp && *cp != '\n');
581 } else {
582 cp++;
587 return res;
590 typedef struct liblist {
591 int flags;
592 int sooffset;
593 int liboffset;
594 char *soname;
595 char *libname;
596 struct liblist *next;
597 } liblist_t;
599 static header_t magic = { LDSO_CACHE_MAGIC, LDSO_CACHE_VER, 0 };
600 static liblist_t *lib_head = NULL;
602 static int liblistcomp(liblist_t *x, liblist_t *y)
604 int res;
606 if ((res = libcmp(x->soname, y->soname)) == 0) {
607 res = libcmp(strrchr(x->libname, '/') + 1,
608 strrchr(y->libname, '/') + 1);
611 return res;
614 void cache_dolib(const char *dir, const char *so, int libtype)
616 char fullpath[PATH_MAX];
617 liblist_t *new_lib, *cur_lib;
619 magic.nlibs++;
620 sprintf(fullpath, "%s/%s", dir, so);
621 new_lib = xmalloc(sizeof(liblist_t));
622 new_lib->flags = libtype;
623 new_lib->soname = xstrdup(so);
624 new_lib->libname = xstrdup(fullpath);
626 if (lib_head == NULL || liblistcomp(new_lib, lib_head) > 0) {
627 new_lib->next = lib_head;
628 lib_head = new_lib;
629 } else {
630 for (cur_lib = lib_head; cur_lib->next != NULL &&
631 liblistcomp(new_lib, cur_lib->next) <= 0;
632 cur_lib = cur_lib->next)
633 /* nothing */ ;
634 new_lib->next = cur_lib->next;
635 cur_lib->next = new_lib;
639 void cache_write(void)
641 int cachefd;
642 int stroffset = 0;
643 char realcachefile[BUFFER_SIZE];
644 char tempfile[BUFFER_SIZE];
645 header_t swap_magic;
646 header_t *magic_ptr;
647 libentry_t swap_lib;
648 libentry_t *lib_ptr;
649 liblist_t *cur_lib;
651 if (!magic.nlibs)
652 return;
654 if (!chroot_realpath(chroot_dir, cachefile, realcachefile))
655 err(EXIT_FAILURE, "can't resolve %s in chroot %s (%s)",
656 cachefile, chroot_dir, strerror(errno));
658 sprintf(tempfile, "%s~", realcachefile);
660 if (unlink(tempfile) && errno != ENOENT)
661 err(EXIT_FAILURE, "can't unlink %s~ (%s)", cachefile,
662 strerror(errno));
664 if ((cachefd = creat(tempfile, 0644)) < 0)
665 err(EXIT_FAILURE, "can't create %s~ (%s)", cachefile,
666 strerror(errno));
668 if (byteswap) {
669 swap_magic = magic;
670 swap_magic.nlibs = bswap_32(swap_magic.nlibs);
671 magic_ptr = &swap_magic;
672 } else {
673 magic_ptr = &magic;
675 if (write(cachefd, magic_ptr, sizeof(header_t)) != sizeof(header_t))
676 err(EXIT_FAILURE, "can't write %s~ (%s)", cachefile,
677 strerror(errno));
679 for (cur_lib = lib_head; cur_lib != NULL; cur_lib = cur_lib->next) {
680 cur_lib->sooffset = stroffset;
681 stroffset += strlen(cur_lib->soname) + 1;
682 cur_lib->liboffset = stroffset;
683 stroffset += strlen(cur_lib->libname) + 1;
684 if (byteswap) {
685 swap_lib.flags = bswap_32(cur_lib->flags);
686 swap_lib.sooffset = bswap_32(cur_lib->sooffset);
687 swap_lib.liboffset = bswap_32(cur_lib->liboffset);
688 lib_ptr = &swap_lib;
689 } else {
690 lib_ptr = (libentry_t *) cur_lib;
692 if (write(cachefd, lib_ptr, sizeof(libentry_t)) !=
693 sizeof(libentry_t))
694 err(EXIT_FAILURE, "can't write %s~ (%s)", cachefile,
695 strerror(errno));
698 for (cur_lib = lib_head; cur_lib != NULL; cur_lib = cur_lib->next) {
699 if ((size_t)write(cachefd, cur_lib->soname, strlen(cur_lib->soname) + 1)
700 != strlen(cur_lib->soname) + 1)
701 err(EXIT_FAILURE, "can't write %s~ (%s)", cachefile,
702 strerror(errno));
703 if ((size_t)write(cachefd, cur_lib->libname, strlen(cur_lib->libname) + 1)
704 != strlen(cur_lib->libname) + 1)
705 err(EXIT_FAILURE, "can't write %s~ (%s)", cachefile,
706 strerror(errno));
709 if (close(cachefd))
710 err(EXIT_FAILURE, "can't close %s~ (%s)", cachefile,
711 strerror(errno));
713 if (chmod(tempfile, 0644))
714 err(EXIT_FAILURE, "can't chmod %s~ (%s)", cachefile,
715 strerror(errno));
717 if (rename(tempfile, realcachefile))
718 err(EXIT_FAILURE, "can't rename %s~ (%s)", cachefile,
719 strerror(errno));
722 void cache_print(void)
724 caddr_t c;
725 struct stat st;
726 int fd = 0;
727 char *strs;
728 header_t *header;
729 libentry_t *libent;
730 char realcachefile[BUFFER_SIZE];
732 if (!chroot_realpath(chroot_dir, cachefile, realcachefile))
733 err(EXIT_FAILURE, "can't resolve %s in chroot %s (%s)",
734 cachefile, chroot_dir, strerror(errno));
736 if (stat(realcachefile, &st) || (fd = open(realcachefile, O_RDONLY)) < 0)
737 err(EXIT_FAILURE, "can't read %s (%s)", cachefile, strerror(errno));
739 c = mmap(0, st.st_size, PROT_READ, LDSO_CACHE_MMAP_FLAGS, fd, 0);
740 if (c == MAP_FAILED)
741 err(EXIT_FAILURE, "can't map %s (%s)", cachefile, strerror(errno));
742 close(fd);
744 if (memcmp(((header_t *) c)->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN))
745 err(EXIT_FAILURE, "%s cache corrupt", cachefile);
747 if (memcmp(((header_t *) c)->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN))
748 err(EXIT_FAILURE, "wrong cache version - expected %s",
749 LDSO_CACHE_VER);
751 header = (header_t *) c;
752 libent = (libentry_t *) (c + sizeof(header_t));
753 strs = (char *)&libent[header->nlibs];
755 printf("%d libs found in cache `%s' (version %s)\n",
756 header->nlibs, cachefile, LDSO_CACHE_VER);
758 for (fd = 0; fd < header->nlibs; fd++) {
759 printf("\t%s ", strs + libent[fd].sooffset);
760 switch (libent[fd].flags & ~LIB_ELF64 & FLAG_TYPE_MASK) {
761 case LIB_DLL:
762 printf("(libc4)");
763 break;
764 case LIB_ELF:
765 printf("(ELF%s)", libent[fd].flags & LIB_ELF64 ? "/64" : "");
766 break;
767 case LIB_ELF_LIBC0:
768 printf("(libc0%s)", libent[fd].flags & LIB_ELF64 ? "/64" : "");
769 break;
770 case LIB_ELF_LIBC5:
771 case LIB_ELF_LIBC6:
772 printf("(libc%d%s)",
773 (libent[fd].flags & ~LIB_ELF64 & FLAG_TYPE_MASK) + 3,
774 libent[fd].flags & LIB_ELF64 ? "/64" : "");
775 break;
776 default:
777 printf("(unknown)");
778 break;
780 printf(" => %s\n", strs + libent[fd].liboffset);
783 munmap(c, st.st_size);
785 #endif
787 static attribute_noreturn void usage(void)
789 fprintf(stderr,
790 #ifdef __LDSO_CACHE_SUPPORT__
791 "ldconfig - updates symlinks and cache for shared libraries\n\n"
792 "Usage: ldconfig [-DvqnNX] [-f conf] [-C cache] [-r root] dir ...\n"
793 " ldconfig -l [-Dv] lib ...\n"
794 " ldconfig -p\n\nOptions:\n"
795 #else
796 "ldconfig - updates symlinks for shared libraries\n\n"
797 "Usage: ldconfig [-DvqnX] [-r root] dir ...\n"
798 " ldconfig -l [-Dv] lib ...\n\nOptions:\n"
799 #endif
800 "\t-D:\t\tdebug mode, don't update links\n"
801 "\t-v:\t\tverbose mode, print things as we go\n"
802 "\t-q:\t\tquiet mode, don't print warnings\n"
803 "\t-n:\t\tdon't process standard directories\n"
804 "\t-N:\t\tdon't update the library cache\n"
805 "\t-X:\t\tdon't update the library links\n"
806 "\t-l:\t\tlibrary mode, manually link libraries\n"
807 "\t-p:\t\tprint the current library cache\n"
808 #ifdef __LDSO_CACHE_SUPPORT__
809 "\t-f conf :\tuse conf instead of %s\n"
810 "\t-C cache:\tuse cache instead of %s\n"
811 #endif
812 "\t-r root :\tfirst, do a chroot to the indicated directory\n"
813 "\tdir ... :\tdirectories to process\n"
814 #ifdef __LDSO_CACHE_SUPPORT__
815 "\tlib ... :\tlibraries to link\n\n", LDSO_CONF, LDSO_CACHE
816 #else
817 "\tlib ... :\tlibraries to link\n\n"
818 #endif
820 exit(EXIT_FAILURE);
823 #define DIR_SEP ":, \t\n"
824 int main(int argc, char **argv)
826 int i, c;
827 int nodefault = 0;
828 char *cp, *dir, *so;
829 int libtype, islink;
830 int printcache = 0;
831 #ifdef __LDSO_CACHE_SUPPORT__
832 char *extpath;
833 #endif
835 prog = argv[0];
836 opterr = 0;
838 while ((c = getopt(argc, argv, "DvqnNXlpf:C:r:")) != EOF)
839 switch (c) {
840 case 'D':
841 debug = 1; /* debug mode */
842 nocache = 1;
843 nolinks = 1;
844 verbose = 1;
845 break;
846 case 'v':
847 verbose = 1; /* verbose mode */
848 break;
849 case 'q':
850 if (verbose <= 0)
851 verbose = -1; /* quiet mode */
852 break;
853 case 'n':
854 nodefault = 1; /* no default dirs */
855 nocache = 1;
856 break;
857 case 'N':
858 nocache = 1; /* don't build cache */
859 break;
860 case 'X':
861 nolinks = 1; /* don't update links */
862 break;
863 case 'l':
864 libmode = 1; /* library mode */
865 break;
866 case 'p':
867 printcache = 1; /* print cache */
868 break;
869 case 'f':
870 #ifdef __LDSO_CACHE_SUPPORT__
871 conffile = optarg; /* alternate conf file */
872 #endif
873 break;
874 case 'C':
875 #ifdef __LDSO_CACHE_SUPPORT__
876 cachefile = optarg; /* alternate cache file */
877 #endif
878 break;
879 case 'r':
880 chroot_dir = optarg;
881 break;
882 default:
883 usage();
884 break;
886 /* THE REST OF THESE ARE UNDOCUMENTED AND MAY BE REMOVED
887 IN FUTURE VERSIONS. */
890 if (chroot_dir && *chroot_dir) {
891 if (chroot(chroot_dir) < 0) {
892 if (chdir(chroot_dir) < 0)
893 err(EXIT_FAILURE, "couldn't chroot to %s (%s)", chroot_dir, strerror(errno));
894 chroot_dir = ".";
895 } else {
896 if (chdir("/") < 0)
897 err(EXIT_FAILURE, "couldn't chdir to / (%s)", strerror(errno));
898 chroot_dir = NULL;
902 /* allow me to introduce myself, hi, my name is ... */
903 if (verbose > 0)
904 printf("%s: uClibc version\n", argv[0]);
906 if (printcache) {
907 /* print the cache -- don't you trust me? */
908 cache_print();
909 exit(EXIT_SUCCESS);
910 } else if (libmode) {
911 /* so you want to do things manually, eh? */
913 /* ok, if you're so smart, which libraries do we link? */
914 for (i = optind; i < argc; i++) {
915 /* split into directory and file parts */
916 if (!(cp = strrchr(argv[i], '/'))) {
917 dir = "."; /* no dir, only a filename */
918 cp = argv[i];
919 } else {
920 if (cp == argv[i])
921 dir = "/"; /* file in root directory */
922 else
923 dir = argv[i];
924 *cp++ = '\0'; /* neither of the above */
927 /* we'd better do a little bit of checking */
928 if ((so = is_shlib(dir, cp, &libtype, &islink, LIB_ANY)) == NULL)
929 err(EXIT_FAILURE, "%s%s%s is not a shared library",
930 dir, (*dir && strcmp(dir, "/")) ? "/" : "", cp);
932 /* so far, so good, maybe he knows what he's doing */
933 link_shlib(dir, cp, so);
935 } else {
936 /* the lazy bum want's us to do all the work for him */
938 /* don't cache dirs on the command line */
939 int nocache_save = nocache;
940 nocache = 1;
942 /* OK, which directories should we do? */
943 for (i = optind; i < argc; i++)
944 scan_dir(argv[i]);
946 /* restore the desired caching state */
947 nocache = nocache_save;
949 /* look ma, no defaults */
950 if (!nodefault) {
951 scan_dir(UCLIBC_RUNTIME_PREFIX "lib");
952 scan_dir(UCLIBC_RUNTIME_PREFIX "usr/lib");
953 #ifndef __LDSO_CACHE_SUPPORT__
954 scan_dir(UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib");
955 #else
956 /* I guess the defaults aren't good enough */
957 if ((extpath = get_extpath())) {
958 for (cp = strtok(extpath, DIR_SEP); cp; cp = strtok(NULL, DIR_SEP)) {
959 /* strip trailing slashes */
960 int len = strlen(cp);
961 if (len)
962 while (cp[--len] == '/' && len)
963 cp[len] = 0;
964 /* we do the redundancy check only if cache usage is enabled */
965 if (strcmp(UCLIBC_RUNTIME_PREFIX "lib", cp) == 0
966 || strcmp(UCLIBC_RUNTIME_PREFIX "usr/lib", cp) == 0) {
967 if (verbose >= 0)
968 warnx("You should remove `%s' from `%s'", cp, LDSO_CONF);
969 continue;
971 scan_dir(cp);
973 free(extpath);
975 #endif
978 if (!nocache)
979 cache_write();
982 exit(EXIT_SUCCESS);