fix gcc warning with -Wmisleading-indentation
[uclibc-ng.git] / utils / ldconfig.c
blobe6b7881182e7377a3b501ee1ef2825753c04b7dd
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);
187 #if 0
188 /* relies on multilib support which we dont have ... */
189 *type |= LIB_ELF64;
190 #endif
193 return res;
196 /* If shared library, return a malloced copy of the soname and set the
197 * type, else return NULL.
199 * expected_type should be either LIB_ANY or one of the following:-
200 * LIB_DLL
201 * LIB_ELF
202 * LIB_ELF_LIBC5
203 * LIB_ELF_LIBC6
205 * If the lib is ELF and we can not deduce the type the type will
206 * be set based on expected_type.
208 * If the expected, actual/deduced types missmatch we display a warning
209 * and use the actual/deduced type.
211 static char *is_shlib(const char *dir, const char *name, int *type,
212 int *islink, int expected_type)
214 char *good = NULL;
215 char *cp, *cp2;
216 FILE *file;
217 struct exec exec;
218 ElfW(Ehdr) *elf_hdr;
219 struct stat statbuf;
220 char buff[BUFFER_SIZE];
221 char real[BUFFER_SIZE];
222 static int byteswapflag = -1; /* start with byte-order unknown */
224 /* see if name is of the form *.so* */
225 if (name[strlen(name) - 1] != '~' && (cp = strstr(name, ".so"))) {
226 /* find the start of the Vminor part, if any */
227 if (cp[3] == '.' && (cp2 = strchr(cp + 4, '.')))
228 cp = cp2;
229 else
230 cp = cp + strlen(cp);
232 /* construct the full path name */
233 sprintf(buff, "%s%s%s", dir, (*dir && strcmp(dir, "/")) ? "/" : "", name);
235 /* get real path in case of chroot */
236 if (!chroot_realpath(chroot_dir, buff, real))
237 warn("can't resolve %s in chroot %s", buff, chroot_dir);
239 /* first, make sure it's a regular file */
240 if (lstat(real, &statbuf))
241 warn("skipping %s", buff);
242 else if (!S_ISREG(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode))
243 warnx("%s is not a regular file or symlink, skipping", buff);
244 else {
245 /* is it a regular file or a symlink */
246 *islink = S_ISLNK(statbuf.st_mode);
248 /* then try opening it */
249 if (!(file = fopen(real, "rb")))
250 warn("skipping %s", buff);
251 else {
252 /* now make sure it's a shared library */
253 if (fread(&exec, sizeof exec, 1, file) < 1)
254 warnx("can't read header from %s, skipping", buff);
255 else if (N_MAGIC(exec) != ZMAGIC
256 && N_MAGIC(exec) != QMAGIC
257 && N_MAGIC_SWAP(exec) != ZMAGIC
258 && N_MAGIC_SWAP(exec) != QMAGIC) {
259 elf_hdr = (ElfW(Ehdr) *) & exec;
260 if (elf_hdr->e_ident[0] != 0x7f ||
261 strncmp((const char *)elf_hdr->e_ident + 1, "ELF", 3) != 0)
263 /* silently ignore linker scripts */
264 if (strncmp((const char *)&exec, "/* GNU ld", 9) != 0)
265 warnx("%s is not a shared library, skipping", buff);
266 } else {
267 /* always call readsoname to update type */
268 if (expected_type == LIB_DLL) {
269 warnx("%s is not an a.out library, it's ELF!", buff);
270 expected_type = LIB_ANY;
272 *type = LIB_ELF;
273 good = readsoname(buff, file, expected_type, type,
274 elf_hdr->e_ident[EI_CLASS]);
275 if (byteswapflag == -1)
276 /* byte-order detected */
277 byteswapflag = byteswap;
278 if (good == NULL || *islink) {
279 if (good != NULL)
280 free(good);
281 good = xstrdup(name);
282 } else {
283 /* if the soname does not match the filename,
284 issue a warning, but only in debug mode. */
285 int len = strlen(good);
286 if (debug && (strncmp((const char *)good, name, len) != 0
287 || (name[len] != '\0' && name[len] != '.')))
288 warnx("%s has inconsistent soname (%s)", buff, good);
291 } else {
292 /* Determine byte-order */
293 byteswap = (N_MAGIC(exec) == ZMAGIC || N_MAGIC(exec) == QMAGIC) ? 0 : 1;
294 if (byteswapflag == -1)
295 /* byte-order detected */
296 byteswapflag = byteswap;
298 if (*islink)
299 good = xstrdup(name);
300 else {
301 good = xmalloc(cp - name + 1);
302 strncpy(good, name, cp - name);
303 good[cp - name] = '\0';
305 if (expected_type != LIB_ANY && expected_type != LIB_DLL) {
306 warnx("%s is not an ELF library, its an a.out DLL!", buff);
307 expected_type = LIB_ANY;
310 *type = LIB_DLL;
312 fclose(file);
314 if (byteswapflag >= 0 && byteswap != byteswapflag) {
315 byteswapflag = -2;
316 warnx("mixed byte-order detected, using host byte-order...");
318 if (byteswapflag == -2)
319 byteswap = 0;
324 return good;
327 /* update the symlink to new library */
328 static void link_shlib(const char *dir, const char *file, const char *so)
330 int change = 1;
331 char libname[BUFFER_SIZE];
332 char linkname[BUFFER_SIZE];
333 char reallibname[BUFFER_SIZE];
334 char reallinkname[BUFFER_SIZE];
335 struct stat libstat;
336 struct stat linkstat;
338 /* construct the full path names */
339 sprintf(libname, "%s/%s", dir, file);
340 sprintf(linkname, "%s/%s", dir, so);
341 if (!chroot_realpath(chroot_dir, libname, reallibname))
342 warn("can't resolve %s in chroot %s", libname, chroot_dir);
343 if (!chroot_realpath(chroot_dir, linkname, reallinkname))
344 warn("can't resolve %s in chroot %s", linkname, chroot_dir);
346 /* see if a link already exists */
347 if (!stat(reallinkname, &linkstat)) {
348 /* now see if it's the one we want */
349 if (stat(reallibname, &libstat))
350 warn("can't stat %s", libname);
351 else if (libstat.st_dev == linkstat.st_dev &&
352 libstat.st_ino == linkstat.st_ino)
353 change = 0;
356 /* then update the link, if required */
357 if (change > 0 && !nolinks) {
358 if (!lstat(reallinkname, &linkstat)) {
359 if (!S_ISLNK(linkstat.st_mode)) {
360 warnx("%s is not a symlink", linkname);
361 change = -1;
362 } else if (remove(reallinkname)) {
363 warn("can't unlink %s", linkname);
364 change = -1;
367 if (change > 0) {
368 if (symlink(file, reallinkname)) {
369 warn("can't link %s to %s", linkname, file);
370 change = -1;
375 /* some people like to know what we're doing */
376 if (verbose > 0)
377 printf("\t%s => %s%s\n", so, file,
378 change < 0 ? " (SKIPPED)" :
379 (change > 0 ? " (changed)" : ""));
381 return;
384 /* figure out which library is greater */
385 static int libcmp(char *p1, char *p2)
387 while (*p1) {
388 if (isdigit(*p1) && isdigit(*p2)) {
389 /* must compare this numerically */
390 int v1, v2;
391 v1 = strtoul(p1, &p1, 10);
392 v2 = strtoul(p2, &p2, 10);
393 if (v1 != v2)
394 return v1 - v2;
395 } else if (isdigit(*p1) && !isdigit(*p2))
396 return 1;
397 else if (!isdigit(*p1) && isdigit(*p2))
398 return -1;
399 else if (*p1 != *p2)
400 return *p1 - *p2;
401 else
402 p1++, p2++;
405 return *p1 - *p2;
408 struct lib {
409 char *so; /* soname of a library */
410 char *name; /* name of a library */
411 int libtype; /* type of a library */
412 int islink; /* is it a symlink */
413 struct lib *next; /* next library in list */
416 /* update all shared library links in a directory */
417 static void scan_dir(const char *rawname)
419 DIR *dir;
420 const char *name;
421 struct dirent *ent;
422 char *so, *path, *path_n;
423 struct lib *lp, *libs = NULL;
424 int i, libtype, islink, expected_type = LIB_ANY;
425 char realname[BUFFER_SIZE];
427 /* We need a writable copy of this string */
428 path = strdup(rawname);
429 if (!path) {
430 err(EXIT_FAILURE, "Out of memory!\n");
432 /* Eliminate all double //s */
433 path_n = path;
434 while ((path_n = strstr(path_n, "//"))) {
435 i = strlen(path_n);
436 memmove(path_n, path_n + 1, i - 1);
437 *(path_n + i - 1) = '\0';
439 name = path;
441 #if 0
442 char *t;
443 /* Check for an embedded expected type */
444 t = strrchr(name, '=');
445 if (t) {
446 *t++ = '\0'; /* Skip = char */
447 if (strcasecmp(t, "libc4") == 0) {
448 expected_type = LIB_DLL;
449 } else {
450 if (strcasecmp(t, "libc5") == 0) {
451 expected_type = LIB_ELF_LIBC5;
452 } else {
453 if (strcasecmp(t, "libc6") == 0) {
454 expected_type = LIB_ELF_LIBC6;
455 } else {
456 if (strcasecmp(t, "libc0") == 0) {
457 expected_type = LIB_ELF_LIBC0;
458 } else {
459 warnx("Unknown type field '%s' for dir '%s' - ignored", t, name);
460 expected_type = LIB_ANY;
466 #endif
468 /* let 'em know what's going on */
469 if (verbose > 0)
470 printf("%s:\n", name);
472 /* get real path in case of chroot */
473 if (!chroot_realpath(chroot_dir, name, realname))
474 warn("can't resolve %s in chroot %s", name, chroot_dir);
476 /* if we can't open it, we can't do anything */
477 if ((dir = opendir(realname)) == NULL) {
478 warn("skipping %s", name);
479 free(path);
480 return;
483 /* yes, we have to look at every single file */
484 while ((ent = readdir(dir)) != NULL) {
485 /* if it's not a shared library, don't bother */
486 if ((so = is_shlib(name, ent->d_name, &libtype, &islink, expected_type)) == NULL)
487 continue;
489 /* have we already seen one with the same so name? */
490 for (lp = libs; lp; lp = lp->next) {
491 if (strcmp(so, lp->so) == 0) {
492 /* we have, which one do we want to use? */
493 if ((!islink && lp->islink) ||
494 (islink == lp->islink &&
495 libcmp(ent->d_name, lp->name) > 0)) {
496 /* let's use the new one */
497 free(lp->name);
498 lp->name = xstrdup(ent->d_name);
499 lp->libtype = libtype;
500 lp->islink = islink;
502 break;
506 /* congratulations, you're the first one we've seen */
507 if (!lp) {
508 lp = xmalloc(sizeof *lp);
509 lp->so = xstrdup(so);
510 lp->name = xstrdup(ent->d_name);
511 lp->libtype = libtype;
512 lp->islink = islink;
513 lp->next = libs;
514 libs = lp;
517 free(so);
520 /* don't need this any more */
521 closedir(dir);
523 /* now we have all the latest libs, update the links */
524 for (lp = libs; lp; lp = lp->next) {
525 if (!lp->islink)
526 link_shlib(name, lp->name, lp->so);
527 if (!nocache)
528 cache_dolib(name, lp->so, lp->libtype);
531 /* always try to clean up after ourselves */
532 while (libs) {
533 lp = libs->next;
534 free(libs->so);
535 free(libs->name);
536 free(libs);
537 libs = lp;
540 free(path);
541 return;
544 #ifndef __LDSO_CACHE_SUPPORT__
545 void cache_print(void)
547 printf("Library cache disabled\n");
549 void cache_dolib(const char *dir, const char *so, int libtype)
551 return;
553 void cache_write(void)
555 return;
557 #else
558 /* return the list of system-specific directories */
559 static char *get_extpath(void)
561 char *res = NULL, *cp;
562 FILE *file;
563 struct stat st;
564 char realconffile[BUFFER_SIZE];
566 if (!chroot_realpath(chroot_dir, conffile, realconffile))
567 return NULL;
569 if ((file = fopen(realconffile, "r")) != NULL) {
570 fstat(fileno(file), &st);
571 res = xmalloc(st.st_size + 1);
572 (void)fread(res, 1, st.st_size, file);
573 fclose(file);
574 res[st.st_size] = '\0';
576 /* convert comments fo spaces */
577 for (cp = res; *cp; /*nada */ ) {
578 if (*cp == '#') {
580 *cp++ = ' ';
581 while (*cp && *cp != '\n');
582 } else {
583 cp++;
588 return res;
591 typedef struct liblist {
592 int flags;
593 int sooffset;
594 int liboffset;
595 char *soname;
596 char *libname;
597 struct liblist *next;
598 } liblist_t;
600 static header_t magic = { LDSO_CACHE_MAGIC, LDSO_CACHE_VER, 0 };
601 static liblist_t *lib_head = NULL;
603 static int liblistcomp(liblist_t *x, liblist_t *y)
605 int res;
607 if ((res = libcmp(x->soname, y->soname)) == 0) {
608 res = libcmp(strrchr(x->libname, '/') + 1,
609 strrchr(y->libname, '/') + 1);
612 return res;
615 void cache_dolib(const char *dir, const char *so, int libtype)
617 char fullpath[PATH_MAX];
618 liblist_t *new_lib, *cur_lib;
620 magic.nlibs++;
621 sprintf(fullpath, "%s/%s", dir, so);
622 new_lib = xmalloc(sizeof(liblist_t));
623 new_lib->flags = libtype;
624 new_lib->soname = xstrdup(so);
625 new_lib->libname = xstrdup(fullpath);
627 if (lib_head == NULL || liblistcomp(new_lib, lib_head) > 0) {
628 new_lib->next = lib_head;
629 lib_head = new_lib;
630 } else {
631 for (cur_lib = lib_head; cur_lib->next != NULL &&
632 liblistcomp(new_lib, cur_lib->next) <= 0;
633 cur_lib = cur_lib->next)
634 /* nothing */ ;
635 new_lib->next = cur_lib->next;
636 cur_lib->next = new_lib;
640 void cache_write(void)
642 int cachefd;
643 int stroffset = 0;
644 char realcachefile[BUFFER_SIZE];
645 char tempfile[BUFFER_SIZE];
646 header_t swap_magic;
647 header_t *magic_ptr;
648 libentry_t swap_lib;
649 libentry_t *lib_ptr;
650 liblist_t *cur_lib;
652 if (!magic.nlibs)
653 return;
655 if (!chroot_realpath(chroot_dir, cachefile, realcachefile))
656 err(EXIT_FAILURE, "can't resolve %s in chroot %s (%s)",
657 cachefile, chroot_dir, strerror(errno));
659 sprintf(tempfile, "%s~", realcachefile);
661 if (unlink(tempfile) && errno != ENOENT)
662 err(EXIT_FAILURE, "can't unlink %s~ (%s)", cachefile,
663 strerror(errno));
665 if ((cachefd = creat(tempfile, 0644)) < 0)
666 err(EXIT_FAILURE, "can't create %s~ (%s)", cachefile,
667 strerror(errno));
669 if (byteswap) {
670 swap_magic = magic;
671 swap_magic.nlibs = bswap_32(swap_magic.nlibs);
672 magic_ptr = &swap_magic;
673 } else {
674 magic_ptr = &magic;
676 if (write(cachefd, magic_ptr, sizeof(header_t)) != sizeof(header_t))
677 err(EXIT_FAILURE, "can't write %s~ (%s)", cachefile,
678 strerror(errno));
680 for (cur_lib = lib_head; cur_lib != NULL; cur_lib = cur_lib->next) {
681 cur_lib->sooffset = stroffset;
682 stroffset += strlen(cur_lib->soname) + 1;
683 cur_lib->liboffset = stroffset;
684 stroffset += strlen(cur_lib->libname) + 1;
685 if (byteswap) {
686 swap_lib.flags = bswap_32(cur_lib->flags);
687 swap_lib.sooffset = bswap_32(cur_lib->sooffset);
688 swap_lib.liboffset = bswap_32(cur_lib->liboffset);
689 lib_ptr = &swap_lib;
690 } else {
691 lib_ptr = (libentry_t *) cur_lib;
693 if (write(cachefd, lib_ptr, sizeof(libentry_t)) !=
694 sizeof(libentry_t))
695 err(EXIT_FAILURE, "can't write %s~ (%s)", cachefile,
696 strerror(errno));
699 for (cur_lib = lib_head; cur_lib != NULL; cur_lib = cur_lib->next) {
700 if ((size_t)write(cachefd, cur_lib->soname, strlen(cur_lib->soname) + 1)
701 != strlen(cur_lib->soname) + 1)
702 err(EXIT_FAILURE, "can't write %s~ (%s)", cachefile,
703 strerror(errno));
704 if ((size_t)write(cachefd, cur_lib->libname, strlen(cur_lib->libname) + 1)
705 != strlen(cur_lib->libname) + 1)
706 err(EXIT_FAILURE, "can't write %s~ (%s)", cachefile,
707 strerror(errno));
710 if (close(cachefd))
711 err(EXIT_FAILURE, "can't close %s~ (%s)", cachefile,
712 strerror(errno));
714 if (chmod(tempfile, 0644))
715 err(EXIT_FAILURE, "can't chmod %s~ (%s)", cachefile,
716 strerror(errno));
718 if (rename(tempfile, realcachefile))
719 err(EXIT_FAILURE, "can't rename %s~ (%s)", cachefile,
720 strerror(errno));
723 void cache_print(void)
725 caddr_t c;
726 struct stat st;
727 int fd = 0;
728 char *strs;
729 header_t *header;
730 libentry_t *libent;
731 char realcachefile[BUFFER_SIZE];
733 if (!chroot_realpath(chroot_dir, cachefile, realcachefile))
734 err(EXIT_FAILURE, "can't resolve %s in chroot %s (%s)",
735 cachefile, chroot_dir, strerror(errno));
737 if (stat(realcachefile, &st) || (fd = open(realcachefile, O_RDONLY)) < 0)
738 err(EXIT_FAILURE, "can't read %s (%s)", cachefile, strerror(errno));
740 c = mmap(0, st.st_size, PROT_READ, LDSO_CACHE_MMAP_FLAGS, fd, 0);
741 if (c == MAP_FAILED)
742 err(EXIT_FAILURE, "can't map %s (%s)", cachefile, strerror(errno));
743 close(fd);
745 if (memcmp(((header_t *) c)->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN))
746 err(EXIT_FAILURE, "%s cache corrupt", cachefile);
748 if (memcmp(((header_t *) c)->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN))
749 err(EXIT_FAILURE, "wrong cache version - expected %s",
750 LDSO_CACHE_VER);
752 header = (header_t *) c;
753 libent = (libentry_t *) (c + sizeof(header_t));
754 strs = (char *)&libent[header->nlibs];
756 printf("%d libs found in cache `%s' (version %s)\n",
757 header->nlibs, cachefile, LDSO_CACHE_VER);
759 for (fd = 0; fd < header->nlibs; fd++) {
760 printf("\t%s ", strs + libent[fd].sooffset);
761 switch (libent[fd].flags & ~LIB_ELF64) {
762 case LIB_DLL:
763 printf("(libc4)");
764 break;
765 case LIB_ELF:
766 printf("(ELF%s)", libent[fd].flags & LIB_ELF64 ? "/64" : "");
767 break;
768 case LIB_ELF_LIBC0:
769 printf("(libc0%s)", libent[fd].flags & LIB_ELF64 ? "/64" : "");
770 break;
771 case LIB_ELF_LIBC5:
772 case LIB_ELF_LIBC6:
773 printf("(libc%d%s)",
774 (libent[fd].flags & ~LIB_ELF64) + 3,
775 libent[fd].flags & LIB_ELF64 ? "/64" : "");
776 break;
777 default:
778 printf("(unknown)");
779 break;
781 printf(" => %s\n", strs + libent[fd].liboffset);
784 munmap(c, st.st_size);
786 #endif
788 static attribute_noreturn void usage(void)
790 fprintf(stderr,
791 #ifdef __LDSO_CACHE_SUPPORT__
792 "ldconfig - updates symlinks and cache for shared libraries\n\n"
793 "Usage: ldconfig [-DvqnNX] [-f conf] [-C cache] [-r root] dir ...\n"
794 " ldconfig -l [-Dv] lib ...\n"
795 " ldconfig -p\n\nOptions:\n"
796 #else
797 "ldconfig - updates symlinks for shared libraries\n\n"
798 "Usage: ldconfig [-DvqnX] [-r root] dir ...\n"
799 " ldconfig -l [-Dv] lib ...\n\nOptions:\n"
800 #endif
801 "\t-D:\t\tdebug mode, don't update links\n"
802 "\t-v:\t\tverbose mode, print things as we go\n"
803 "\t-q:\t\tquiet mode, don't print warnings\n"
804 "\t-n:\t\tdon't process standard directories\n"
805 "\t-N:\t\tdon't update the library cache\n"
806 "\t-X:\t\tdon't update the library links\n"
807 "\t-l:\t\tlibrary mode, manually link libraries\n"
808 "\t-p:\t\tprint the current library cache\n"
809 #ifdef __LDSO_CACHE_SUPPORT__
810 "\t-f conf :\tuse conf instead of %s\n"
811 "\t-C cache:\tuse cache instead of %s\n"
812 #endif
813 "\t-r root :\tfirst, do a chroot to the indicated directory\n"
814 "\tdir ... :\tdirectories to process\n"
815 #ifdef __LDSO_CACHE_SUPPORT__
816 "\tlib ... :\tlibraries to link\n\n", LDSO_CONF, LDSO_CACHE
817 #else
818 "\tlib ... :\tlibraries to link\n\n"
819 #endif
821 exit(EXIT_FAILURE);
824 #define DIR_SEP ":, \t\n"
825 int main(int argc, char **argv)
827 int i, c;
828 int nodefault = 0;
829 char *cp, *dir, *so;
830 int libtype, islink;
831 int printcache = 0;
832 #ifdef __LDSO_CACHE_SUPPORT__
833 char *extpath;
834 #endif
836 prog = argv[0];
837 opterr = 0;
839 while ((c = getopt(argc, argv, "DvqnNXlpf:C:r:")) != EOF)
840 switch (c) {
841 case 'D':
842 debug = 1; /* debug mode */
843 nocache = 1;
844 nolinks = 1;
845 verbose = 1;
846 break;
847 case 'v':
848 verbose = 1; /* verbose mode */
849 break;
850 case 'q':
851 if (verbose <= 0)
852 verbose = -1; /* quiet mode */
853 break;
854 case 'n':
855 nodefault = 1; /* no default dirs */
856 nocache = 1;
857 break;
858 case 'N':
859 nocache = 1; /* don't build cache */
860 break;
861 case 'X':
862 nolinks = 1; /* don't update links */
863 break;
864 case 'l':
865 libmode = 1; /* library mode */
866 break;
867 case 'p':
868 printcache = 1; /* print cache */
869 break;
870 case 'f':
871 #ifdef __LDSO_CACHE_SUPPORT__
872 conffile = optarg; /* alternate conf file */
873 #endif
874 break;
875 case 'C':
876 #ifdef __LDSO_CACHE_SUPPORT__
877 cachefile = optarg; /* alternate cache file */
878 #endif
879 break;
880 case 'r':
881 chroot_dir = optarg;
882 break;
883 default:
884 usage();
885 break;
887 /* THE REST OF THESE ARE UNDOCUMENTED AND MAY BE REMOVED
888 IN FUTURE VERSIONS. */
891 if (chroot_dir && *chroot_dir) {
892 if (chroot(chroot_dir) < 0) {
893 if (chdir(chroot_dir) < 0)
894 err(EXIT_FAILURE, "couldn't chroot to %s (%s)", chroot_dir, strerror(errno));
895 chroot_dir = ".";
896 } else {
897 if (chdir("/") < 0)
898 err(EXIT_FAILURE, "couldn't chdir to / (%s)", strerror(errno));
899 chroot_dir = NULL;
903 /* allow me to introduce myself, hi, my name is ... */
904 if (verbose > 0)
905 printf("%s: uClibc version\n", argv[0]);
907 if (printcache) {
908 /* print the cache -- don't you trust me? */
909 cache_print();
910 exit(EXIT_SUCCESS);
911 } else if (libmode) {
912 /* so you want to do things manually, eh? */
914 /* ok, if you're so smart, which libraries do we link? */
915 for (i = optind; i < argc; i++) {
916 /* split into directory and file parts */
917 if (!(cp = strrchr(argv[i], '/'))) {
918 dir = "."; /* no dir, only a filename */
919 cp = argv[i];
920 } else {
921 if (cp == argv[i])
922 dir = "/"; /* file in root directory */
923 else
924 dir = argv[i];
925 *cp++ = '\0'; /* neither of the above */
928 /* we'd better do a little bit of checking */
929 if ((so = is_shlib(dir, cp, &libtype, &islink, LIB_ANY)) == NULL)
930 err(EXIT_FAILURE, "%s%s%s is not a shared library",
931 dir, (*dir && strcmp(dir, "/")) ? "/" : "", cp);
933 /* so far, so good, maybe he knows what he's doing */
934 link_shlib(dir, cp, so);
936 } else {
937 /* the lazy bum want's us to do all the work for him */
939 /* don't cache dirs on the command line */
940 int nocache_save = nocache;
941 nocache = 1;
943 /* OK, which directories should we do? */
944 for (i = optind; i < argc; i++)
945 scan_dir(argv[i]);
947 /* restore the desired caching state */
948 nocache = nocache_save;
950 /* look ma, no defaults */
951 if (!nodefault) {
952 scan_dir(UCLIBC_RUNTIME_PREFIX "lib");
953 scan_dir(UCLIBC_RUNTIME_PREFIX "usr/lib");
954 #ifndef __LDSO_CACHE_SUPPORT__
955 scan_dir(UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib");
956 #else
957 /* I guess the defaults aren't good enough */
958 if ((extpath = get_extpath())) {
959 for (cp = strtok(extpath, DIR_SEP); cp; cp = strtok(NULL, DIR_SEP)) {
960 /* strip trailing slashes */
961 int len = strlen(cp);
962 if (len)
963 while (cp[--len] == '/' && len)
964 cp[len] = 0;
965 /* we do the redundancy check only if cache usage is enabled */
966 if (strcmp(UCLIBC_RUNTIME_PREFIX "lib", cp) == 0
967 || strcmp(UCLIBC_RUNTIME_PREFIX "usr/lib", cp) == 0) {
968 if (verbose >= 0)
969 warnx("You should remove `%s' from `%s'", cp, LDSO_CONF);
970 continue;
972 scan_dir(cp);
974 free(extpath);
976 #endif
979 if (!nocache)
980 cache_write();
983 exit(EXIT_SUCCESS);