ldconfig.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. /*
  2. * ldconfig - update shared library symlinks
  3. *
  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
  20. *
  21. * Copyright 1994-2000 David Engel and Mitch D'Souza
  22. *
  23. * This program may be used for any purpose as long as this
  24. * copyright notice is kept.
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <stdarg.h>
  29. #include <string.h>
  30. #include <ctype.h>
  31. #include <getopt.h>
  32. #include <dirent.h>
  33. #include <unistd.h>
  34. #include <link.h>
  35. #include <fcntl.h>
  36. #include <errno.h>
  37. #include <sys/stat.h>
  38. #include <sys/mman.h>
  39. #include "dl-elf.h"
  40. #include "readsoname.h"
  41. #define BUFFER_SIZE 4096
  42. struct exec
  43. {
  44. unsigned long a_info; /* Use macros N_MAGIC, etc for access */
  45. unsigned a_text; /* length of text, in bytes */
  46. unsigned a_data; /* length of data, in bytes */
  47. unsigned a_bss; /* length of uninitialized data area for file, in bytes */
  48. unsigned a_syms; /* length of symbol table data in file, in bytes */
  49. unsigned a_entry; /* start address */
  50. unsigned a_trsize; /* length of relocation info for text, in bytes */
  51. unsigned a_drsize; /* length of relocation info for data, in bytes */
  52. };
  53. #if !defined (N_MAGIC)
  54. #define N_MAGIC(exec) ((exec).a_info & 0xffff)
  55. #endif
  56. /* Code indicating object file or impure executable. */
  57. #define OMAGIC 0407
  58. /* Code indicating pure executable. */
  59. #define NMAGIC 0410
  60. /* Code indicating demand-paged executable. */
  61. #define ZMAGIC 0413
  62. /* This indicates a demand-paged executable with the header in the text.
  63. The first page is unmapped to help trap NULL pointer references */
  64. #define QMAGIC 0314
  65. /* Code indicating core file. */
  66. #define CMAGIC 0421
  67. char *___strtok = NULL;
  68. /* For SunOS */
  69. #ifndef PATH_MAX
  70. #include <limits.h>
  71. #define PATH_MAX _POSIX_PATH_MAX
  72. #endif
  73. /* For SunOS */
  74. #ifndef N_MAGIC
  75. #define N_MAGIC(exec) ((exec).a_magic & 0xffff)
  76. #endif
  77. #define EXIT_OK 0
  78. #define EXIT_FATAL 128
  79. char *prog = NULL;
  80. int debug = 0; /* debug mode */
  81. int verbose = 0; /* verbose mode */
  82. int libmode = 0; /* library mode */
  83. int nolinks = 0; /* don't update links */
  84. int nocache = 0; /* don't build cache */
  85. void cache_print(void);
  86. void cache_write(void);
  87. void cache_dolib(const char *dir, const char *so, int libtype);
  88. #ifdef __LDSO_CACHE_SUPPORT__
  89. char *conffile = LDSO_CONF; /* default conf file */
  90. char *cachefile = LDSO_CACHE; /* default cache file */
  91. #endif
  92. /* These two are used internally -- you shouldn't need to use them */
  93. static void verror_msg(const char *s, va_list p)
  94. {
  95. fflush(stdout);
  96. fprintf(stderr, "%s: ", prog);
  97. vfprintf(stderr, s, p);
  98. }
  99. extern void warnx(const char *s, ...)
  100. {
  101. va_list p;
  102. va_start(p, s);
  103. verror_msg(s, p);
  104. va_end(p);
  105. fprintf(stderr, "\n");
  106. }
  107. extern void err(int errnum, const char *s, ...)
  108. {
  109. va_list p;
  110. va_start(p, s);
  111. verror_msg(s, p);
  112. va_end(p);
  113. fprintf(stderr, "\n");
  114. exit(errnum);
  115. }
  116. static void vperror_msg(const char *s, va_list p)
  117. {
  118. int err = errno;
  119. if (s == 0)
  120. s = "";
  121. verror_msg(s, p);
  122. if (*s)
  123. s = ": ";
  124. fprintf(stderr, "%s%s\n", s, strerror(err));
  125. }
  126. extern void warn(const char *s, ...)
  127. {
  128. va_list p;
  129. va_start(p, s);
  130. vperror_msg(s, p);
  131. va_end(p);
  132. }
  133. void *xmalloc(size_t size)
  134. {
  135. void *ptr;
  136. if ((ptr = malloc(size)) == NULL)
  137. err(EXIT_FATAL,"out of memory");
  138. return ptr;
  139. }
  140. char *xstrdup(const char *str)
  141. {
  142. char *ptr;
  143. if ((ptr = strdup(str)) == NULL)
  144. err(EXIT_FATAL,"out of memory");
  145. return ptr;
  146. }
  147. /* If shared library, return a malloced copy of the soname and set the
  148. type, else return NULL.
  149. expected_type should be either LIB_ANY or one of the following:-
  150. LIB_DLL
  151. LIB_ELF
  152. LIB_ELF_LIBC5
  153. LIB_ELF_LIBC6
  154. If the lib is ELF and we can not deduce the type the type will
  155. be set based on expected_type.
  156. If the expected, actual/deduced types missmatch we display a warning
  157. and use the actual/deduced type.
  158. */
  159. char *is_shlib(const char *dir, const char *name, int *type,
  160. int *islink, int expected_type)
  161. {
  162. char *good = NULL;
  163. char *cp, *cp2;
  164. FILE *file;
  165. struct exec exec;
  166. ElfW(Ehdr) *elf_hdr;
  167. struct stat statbuf;
  168. char buff[BUFFER_SIZE];
  169. /* see if name is of the form *.so* */
  170. if (name[strlen(name)-1] != '~' && (cp = strstr(name, ".so")))
  171. {
  172. /* find the start of the Vminor part, if any */
  173. if (cp[3] == '.' && (cp2 = strchr(cp + 4, '.')))
  174. cp = cp2;
  175. else
  176. cp = cp + strlen(cp);
  177. /* construct the full path name */
  178. sprintf(buff, "%s%s%s", dir, (*dir && strcmp(dir, "/")) ?
  179. "/" : "", name);
  180. /* first, make sure it's a regular file */
  181. if (lstat(buff, &statbuf))
  182. warn("skipping %s", buff);
  183. else if (!S_ISREG(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode))
  184. warnx("%s is not a regular file or symlink, skipping", buff);
  185. else
  186. {
  187. /* is it a regular file or a symlink */
  188. *islink = S_ISLNK(statbuf.st_mode);
  189. /* then try opening it */
  190. if (!(file = fopen(buff, "rb")))
  191. warn("skipping %s", buff);
  192. else
  193. {
  194. /* now make sure it's a shared library */
  195. if (fread(&exec, sizeof exec, 1, file) < 1)
  196. warnx("can't read header from %s, skipping", buff);
  197. else if (N_MAGIC(exec) != ZMAGIC && N_MAGIC(exec) != QMAGIC)
  198. {
  199. elf_hdr = (ElfW(Ehdr) *) &exec;
  200. if (elf_hdr->e_ident[0] != 0x7f ||
  201. strncmp(&elf_hdr->e_ident[1], "ELF",3) != 0)
  202. {
  203. /* silently ignore linker scripts */
  204. if (strncmp((char *)&exec, "/* GNU ld", 9) != 0)
  205. warnx("%s is not a shared library, skipping", buff);
  206. }
  207. else
  208. {
  209. /* always call readsoname to update type */
  210. if(expected_type == LIB_DLL) {
  211. warnx("%s is not an a.out library, its ELF!\n", buff);
  212. expected_type=LIB_ANY;
  213. }
  214. *type = LIB_ELF;
  215. good = readsoname(buff, file, expected_type, type,
  216. elf_hdr->e_ident[EI_CLASS]);
  217. if (good == NULL || *islink)
  218. {
  219. if (good != NULL)
  220. free(good);
  221. good = xstrdup(name);
  222. }
  223. else
  224. {
  225. /* if the soname does not match the filename,
  226. issue a warning, but only in debug mode. */
  227. int len = strlen(good);
  228. if (debug && (strncmp(good, name, len) != 0 ||
  229. (name[len] != '\0' && name[len] != '.')))
  230. warnx("%s has inconsistent soname (%s)", buff, good);
  231. }
  232. }
  233. }
  234. else
  235. {
  236. if (*islink)
  237. good = xstrdup(name);
  238. else
  239. {
  240. good = xmalloc(cp - name + 1);
  241. strncpy(good, name, cp - name);
  242. good[cp - name] = '\0';
  243. }
  244. if(expected_type != LIB_ANY && expected_type != LIB_DLL)
  245. {
  246. warnx("%s is not an ELF library, its an a.out DLL!", buff);
  247. expected_type=LIB_ANY;
  248. }
  249. *type = LIB_DLL;
  250. }
  251. fclose(file);
  252. }
  253. }
  254. }
  255. return good;
  256. }
  257. /* update the symlink to new library */
  258. void link_shlib(const char *dir, const char *file, const char *so)
  259. {
  260. int change = 1;
  261. char libname[BUFFER_SIZE];
  262. char linkname[BUFFER_SIZE];
  263. struct stat libstat;
  264. struct stat linkstat;
  265. /* construct the full path names */
  266. sprintf(libname, "%s/%s", dir, file);
  267. sprintf(linkname, "%s/%s", dir, so);
  268. /* see if a link already exists */
  269. if (!stat(linkname, &linkstat))
  270. {
  271. /* now see if it's the one we want */
  272. if (stat(libname, &libstat))
  273. warn("can't stat %s", libname);
  274. else if (libstat.st_dev == linkstat.st_dev &&
  275. libstat.st_ino == linkstat.st_ino)
  276. change = 0;
  277. }
  278. /* then update the link, if required */
  279. if (change > 0 && !nolinks)
  280. {
  281. if (!lstat(linkname, &linkstat))
  282. {
  283. if (!S_ISLNK(linkstat.st_mode))
  284. {
  285. warnx("%s is not a symlink", linkname);
  286. change = -1;
  287. }
  288. else if (remove(linkname))
  289. {
  290. warn("can't unlink %s", linkname);
  291. change = -1;
  292. }
  293. }
  294. if (change > 0)
  295. {
  296. if (symlink(file, linkname))
  297. {
  298. warn("can't link %s to %s", linkname, file);
  299. change = -1;
  300. }
  301. }
  302. }
  303. /* some people like to know what we're doing */
  304. if (verbose > 0)
  305. printf("\t%s => %s%s\n", so, file,
  306. change < 0 ? " (SKIPPED)" :
  307. (change > 0 ? " (changed)" : ""));
  308. return;
  309. }
  310. /* figure out which library is greater */
  311. int libcmp(char *p1, char *p2)
  312. {
  313. while (*p1)
  314. {
  315. if (isdigit(*p1) && isdigit(*p2))
  316. {
  317. /* must compare this numerically */
  318. int v1, v2;
  319. v1 = strtoul(p1, &p1, 10);
  320. v2 = strtoul(p2, &p2, 10);
  321. if (v1 != v2)
  322. return v1 - v2;
  323. }
  324. else if (isdigit(*p1) && !isdigit(*p2))
  325. return 1;
  326. else if (!isdigit(*p1) && isdigit(*p2))
  327. return -1;
  328. else if (*p1 != *p2)
  329. return *p1 - *p2;
  330. else
  331. p1++, p2++;
  332. }
  333. return *p1 - *p2;
  334. }
  335. struct lib
  336. {
  337. char *so; /* soname of a library */
  338. char *name; /* name of a library */
  339. int libtype; /* type of a library */
  340. int islink; /* is it a symlink */
  341. struct lib *next; /* next library in list */
  342. };
  343. /* update all shared library links in a directory */
  344. void scan_dir(const char *rawname)
  345. {
  346. DIR *dir;
  347. const char *name;
  348. struct dirent *ent;
  349. char *so, *path, *path_n;
  350. struct lib *lp, *libs = NULL;
  351. int i, libtype, islink, expected_type = LIB_ANY;
  352. /* We need a writable copy of this string */
  353. path = strdup(rawname);
  354. if (!path) {
  355. err(EXIT_FATAL, "Out of memory!\n");
  356. }
  357. /* Eliminate all double //s */
  358. path_n=path;
  359. while((path_n=strstr(path_n, "//"))) {
  360. i = strlen(path_n);
  361. memmove(path_n, path_n+1, i-1);
  362. *(path_n + i - 1)='\0';
  363. }
  364. name = path;
  365. #if 0
  366. char *t;
  367. /* Check for an embedded expected type */
  368. t=strrchr(name, '=');
  369. if( t )
  370. {
  371. *t++ = '\0'; /* Skip = char */
  372. if(strcasecmp(t, "libc4") == 0)
  373. {
  374. expected_type = LIB_DLL;
  375. }
  376. else
  377. {
  378. if(strcasecmp(t, "libc5") == 0)
  379. {
  380. expected_type = LIB_ELF_LIBC5;
  381. }
  382. else
  383. {
  384. if(strcasecmp(t, "libc6") == 0)
  385. {
  386. expected_type = LIB_ELF_LIBC6;
  387. }
  388. else
  389. {
  390. if(strcasecmp(t, "libc0") == 0)
  391. {
  392. expected_type = LIB_ELF_LIBC0;
  393. }
  394. else
  395. {
  396. warnx("Unknown type field '%s' for dir '%s' - ignored\n", t, name);
  397. expected_type = LIB_ANY;
  398. }
  399. }
  400. }
  401. }
  402. }
  403. #endif
  404. /* let 'em know what's going on */
  405. if (verbose > 0)
  406. printf("%s:\n", name);
  407. /* if we can't open it, we can't do anything */
  408. if ((dir = opendir(name)) == NULL)
  409. {
  410. warn("skipping %s", name);
  411. free(path);
  412. return;
  413. }
  414. /* yes, we have to look at every single file */
  415. while ((ent = readdir(dir)) != NULL)
  416. {
  417. /* if it's not a shared library, don't bother */
  418. if ((so = is_shlib(name, ent->d_name, &libtype, &islink, expected_type)) == NULL)
  419. continue;
  420. /* have we already seen one with the same so name? */
  421. for (lp = libs; lp; lp = lp->next)
  422. {
  423. if (strcmp(so, lp->so) == 0)
  424. {
  425. /* we have, which one do we want to use? */
  426. if ((!islink && lp->islink) ||
  427. (islink == lp->islink &&
  428. libcmp(ent->d_name, lp->name) > 0))
  429. {
  430. /* let's use the new one */
  431. free(lp->name);
  432. lp->name = xstrdup(ent->d_name);
  433. lp->libtype = libtype;
  434. lp->islink = islink;
  435. }
  436. break;
  437. }
  438. }
  439. /* congratulations, you're the first one we've seen */
  440. if (!lp)
  441. {
  442. lp = xmalloc(sizeof *lp);
  443. lp->so = xstrdup(so);
  444. lp->name = xstrdup(ent->d_name);
  445. lp->libtype = libtype;
  446. lp->islink = islink;
  447. lp->next = libs;
  448. libs = lp;
  449. }
  450. free(so);
  451. }
  452. /* don't need this any more */
  453. closedir(dir);
  454. /* now we have all the latest libs, update the links */
  455. for (lp = libs; lp; lp = lp->next)
  456. {
  457. if (!lp->islink)
  458. link_shlib(name, lp->name, lp->so);
  459. if (!nocache)
  460. cache_dolib(name, lp->so, lp->libtype);
  461. }
  462. /* always try to clean up after ourselves */
  463. while (libs)
  464. {
  465. lp = libs->next;
  466. free(libs->so);
  467. free(libs->name);
  468. free(libs);
  469. libs = lp;
  470. }
  471. free(path);
  472. return;
  473. }
  474. #ifndef __LDSO_CACHE_SUPPORT__
  475. void cache_print(void)
  476. {
  477. printf("Library cache disabled\n");
  478. }
  479. void cache_dolib(const char *dir, const char *so, int libtype)
  480. {
  481. return;
  482. }
  483. void cache_write(void)
  484. {
  485. return;
  486. }
  487. #else
  488. /* return the list of system-specific directories */
  489. char *get_extpath(void)
  490. {
  491. char *res = NULL, *cp;
  492. FILE *file;
  493. struct stat stat;
  494. if ((file = fopen(conffile, "r")) != NULL)
  495. {
  496. fstat(fileno(file), &stat);
  497. res = xmalloc(stat.st_size + 1);
  498. fread(res, 1, stat.st_size, file);
  499. fclose(file);
  500. res[stat.st_size] = '\0';
  501. /* convert comments fo spaces */
  502. for (cp = res; *cp; /*nada*/) {
  503. if (*cp == '#') {
  504. do
  505. *cp++ = ' ';
  506. while (*cp && *cp != '\n');
  507. } else {
  508. cp++;
  509. }
  510. }
  511. }
  512. return res;
  513. }
  514. typedef struct liblist
  515. {
  516. int flags;
  517. int sooffset;
  518. int liboffset;
  519. char *soname;
  520. char *libname;
  521. struct liblist *next;
  522. } liblist_t;
  523. static header_t magic = { LDSO_CACHE_MAGIC, LDSO_CACHE_VER, 0 };
  524. static liblist_t *lib_head = NULL;
  525. static int liblistcomp(liblist_t *x, liblist_t *y)
  526. {
  527. int res;
  528. if ((res = libcmp(x->soname, y->soname)) == 0)
  529. {
  530. res = libcmp(strrchr(x->libname, '/') + 1,
  531. strrchr(y->libname, '/') + 1);
  532. }
  533. return res;
  534. }
  535. void cache_dolib(const char *dir, const char *so, int libtype)
  536. {
  537. char fullpath[PATH_MAX];
  538. liblist_t *new_lib, *cur_lib;
  539. magic.nlibs++;
  540. sprintf(fullpath, "%s/%s", dir, so);
  541. new_lib = xmalloc(sizeof (liblist_t));
  542. new_lib->flags = libtype;
  543. new_lib->soname = xstrdup(so);
  544. new_lib->libname = xstrdup(fullpath);
  545. if (lib_head == NULL || liblistcomp(new_lib, lib_head) > 0)
  546. {
  547. new_lib->next = lib_head;
  548. lib_head = new_lib;
  549. }
  550. else
  551. {
  552. for (cur_lib = lib_head; cur_lib->next != NULL &&
  553. liblistcomp(new_lib, cur_lib->next) <= 0;
  554. cur_lib = cur_lib->next)
  555. /* nothing */;
  556. new_lib->next = cur_lib->next;
  557. cur_lib->next = new_lib;
  558. }
  559. }
  560. void cache_write(void)
  561. {
  562. int cachefd;
  563. int stroffset = 0;
  564. char tempfile[BUFFER_SIZE];
  565. liblist_t *cur_lib;
  566. if (!magic.nlibs)
  567. return;
  568. sprintf(tempfile, "%s~", cachefile);
  569. if (unlink(tempfile) && errno != ENOENT)
  570. err(EXIT_FATAL,"can't unlink %s (%s)", tempfile, strerror(errno));
  571. if ((cachefd = creat(tempfile, 0644)) < 0)
  572. err(EXIT_FATAL,"can't create %s (%s)", tempfile, strerror(errno));
  573. if (write(cachefd, &magic, sizeof (header_t)) != sizeof (header_t))
  574. err(EXIT_FATAL,"can't write %s (%s)", tempfile, strerror(errno));
  575. for (cur_lib = lib_head; cur_lib != NULL; cur_lib = cur_lib->next)
  576. {
  577. cur_lib->sooffset = stroffset;
  578. stroffset += strlen(cur_lib->soname) + 1;
  579. cur_lib->liboffset = stroffset;
  580. stroffset += strlen(cur_lib->libname) + 1;
  581. if (write(cachefd, cur_lib, sizeof (libentry_t)) !=
  582. sizeof (libentry_t))
  583. err(EXIT_FATAL,"can't write %s (%s)", tempfile, strerror(errno));
  584. }
  585. for (cur_lib = lib_head; cur_lib != NULL; cur_lib = cur_lib->next)
  586. {
  587. if (write(cachefd, cur_lib->soname, strlen(cur_lib->soname) + 1)
  588. != strlen(cur_lib->soname) + 1)
  589. err(EXIT_FATAL,"can't write %s (%s)", tempfile, strerror(errno));
  590. if (write(cachefd, cur_lib->libname, strlen(cur_lib->libname) + 1)
  591. != strlen(cur_lib->libname) + 1)
  592. err(EXIT_FATAL,"can't write %s (%s)", tempfile, strerror(errno));
  593. }
  594. if (close(cachefd))
  595. err(EXIT_FATAL,"can't close %s (%s)", tempfile, strerror(errno));
  596. if (chmod(tempfile, 0644))
  597. err(EXIT_FATAL,"can't chmod %s (%s)", tempfile, strerror(errno));
  598. if (rename(tempfile, cachefile))
  599. err(EXIT_FATAL,"can't rename %s (%s)", tempfile, strerror(errno));
  600. }
  601. void cache_print(void)
  602. {
  603. caddr_t c;
  604. struct stat st;
  605. int fd = 0;
  606. char *strs;
  607. header_t *header;
  608. libentry_t *libent;
  609. if (stat(cachefile, &st) || (fd = open(cachefile, O_RDONLY))<0)
  610. err(EXIT_FATAL,"can't read %s (%s)", cachefile, strerror(errno));
  611. if ((c = mmap(0,st.st_size, PROT_READ, MAP_SHARED ,fd, 0)) == (caddr_t)-1)
  612. err(EXIT_FATAL,"can't map %s (%s)", cachefile, strerror(errno));
  613. close(fd);
  614. if (memcmp(((header_t *)c)->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN))
  615. err(EXIT_FATAL,"%s cache corrupt", cachefile);
  616. if (memcmp(((header_t *)c)->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN))
  617. err(EXIT_FATAL,"wrong cache version - expected %s", LDSO_CACHE_VER);
  618. header = (header_t *)c;
  619. libent = (libentry_t *)(c + sizeof (header_t));
  620. strs = (char *)&libent[header->nlibs];
  621. printf("%d libs found in cache `%s' (version %s)\n",
  622. header->nlibs, cachefile, LDSO_CACHE_VER);
  623. for (fd = 0; fd < header->nlibs; fd++)
  624. {
  625. printf("\t%s ", strs + libent[fd].sooffset);
  626. switch (libent[fd].flags & ~LIB_ELF64)
  627. {
  628. case LIB_DLL:
  629. printf("(libc4)");
  630. break;
  631. case LIB_ELF:
  632. printf("(ELF%s)", libent[fd].flags & LIB_ELF64 ? "/64" : "");
  633. break;
  634. case LIB_ELF_LIBC0:
  635. printf("(libc0%s)",libent[fd].flags & LIB_ELF64 ? "/64" : "");
  636. break;
  637. case LIB_ELF_LIBC5:
  638. case LIB_ELF_LIBC6:
  639. printf("(libc%d%s)", (libent[fd].flags & ~LIB_ELF64) + 3,
  640. libent[fd].flags & LIB_ELF64 ? "/64" : "");
  641. break;
  642. default:
  643. printf("(unknown)");
  644. break;
  645. }
  646. printf(" => %s\n", strs + libent[fd].liboffset);
  647. }
  648. munmap (c,st.st_size);
  649. }
  650. #endif
  651. void usage(void)
  652. {
  653. fprintf(stderr,
  654. #ifdef __LDSO_CACHE_SUPPORT__
  655. "ldconfig - updates symlinks and cache for shared libraries\n\n"
  656. "Usage: ldconfig [-DvqnNX] [-f conf] [-C cache] [-r root] dir ...\n"
  657. " ldconfig -l [-Dv] lib ...\n"
  658. " ldconfig -p\n\nOptions:\n"
  659. #else
  660. "ldconfig - updates symlinks for shared libraries\n\n"
  661. "Usage: ldconfig [-DvqnX] [-r root] dir ...\n"
  662. " ldconfig -l [-Dv] lib ...\n\nOptions:\n"
  663. #endif
  664. "\t-D:\t\tdebug mode, don't update links\n"
  665. "\t-v:\t\tverbose mode, print things as we go\n"
  666. "\t-q:\t\tquiet mode, don't print warnings\n"
  667. "\t-n:\t\tdon't process standard directories\n"
  668. "\t-N:\t\tdon't update the library cache\n"
  669. "\t-X:\t\tdon't update the library links\n"
  670. "\t-l:\t\tlibrary mode, manually link libraries\n"
  671. "\t-p:\t\tprint the current library cache\n"
  672. "\t-f conf :\tuse conf instead of %s\n"
  673. "\t-C cache:\tuse cache instead of %s\n"
  674. "\t-r root :\tfirst, do a chroot to the indicated directory\n"
  675. "\tdir ... :\tdirectories to process\n"
  676. #ifdef __LDSO_CACHE_SUPPORT__
  677. "\tlib ... :\tlibraries to link\n\n",
  678. LDSO_CONF, LDSO_CACHE
  679. #else
  680. "\tlib ... :\tlibraries to link\n\n"
  681. #endif
  682. );
  683. exit(EXIT_FATAL);
  684. }
  685. #define DIR_SEP ":, \t\n"
  686. int main(int argc, char **argv)
  687. {
  688. int i, c;
  689. int nodefault = 0;
  690. char *cp, *dir, *so;
  691. int libtype, islink;
  692. char *chroot_dir = NULL;
  693. int printcache = 0;
  694. char *extpath;
  695. prog = argv[0];
  696. opterr = 0;
  697. while ((c = getopt(argc, argv, "DvqnNXlpf:C:r:")) != EOF)
  698. switch (c)
  699. {
  700. case 'D':
  701. debug = 1; /* debug mode */
  702. nocache = 1;
  703. nolinks = 1;
  704. verbose = 1;
  705. break;
  706. case 'v':
  707. verbose = 1; /* verbose mode */
  708. break;
  709. case 'q':
  710. if (verbose <= 0)
  711. verbose = -1; /* quiet mode */
  712. break;
  713. case 'n':
  714. nodefault = 1; /* no default dirs */
  715. nocache = 1;
  716. break;
  717. case 'N':
  718. nocache = 1; /* don't build cache */
  719. break;
  720. case 'X':
  721. nolinks = 1; /* don't update links */
  722. break;
  723. case 'l':
  724. libmode = 1; /* library mode */
  725. break;
  726. case 'p':
  727. printcache = 1; /* print cache */
  728. break;
  729. case 'f':
  730. #ifdef __LDSO_CACHE_SUPPORT__
  731. conffile = optarg; /* alternate conf file */
  732. #endif
  733. break;
  734. case 'C':
  735. #ifdef __LDSO_CACHE_SUPPORT__
  736. cachefile = optarg; /* alternate cache file */
  737. #endif
  738. break;
  739. case 'r':
  740. chroot_dir = optarg;
  741. break;
  742. default:
  743. usage();
  744. break;
  745. /* THE REST OF THESE ARE UNDOCUMENTED AND MAY BE REMOVED
  746. IN FUTURE VERSIONS. */
  747. }
  748. if (chroot_dir && *chroot_dir) {
  749. if (chroot(chroot_dir) < 0)
  750. err(EXIT_FATAL,"couldn't chroot to %s (%s)", chroot_dir, strerror(errno));
  751. if (chdir("/") < 0)
  752. err(EXIT_FATAL,"couldn't chdir to / (%s)", strerror(errno));
  753. }
  754. /* allow me to introduce myself, hi, my name is ... */
  755. if (verbose > 0)
  756. printf("%s: uClibc version\n", argv[0]);
  757. if (printcache)
  758. {
  759. /* print the cache -- don't you trust me? */
  760. cache_print();
  761. exit(EXIT_OK);
  762. }
  763. else if (libmode)
  764. {
  765. /* so you want to do things manually, eh? */
  766. /* ok, if you're so smart, which libraries do we link? */
  767. for (i = optind; i < argc; i++)
  768. {
  769. /* split into directory and file parts */
  770. if (!(cp = strrchr(argv[i], '/')))
  771. {
  772. dir = "."; /* no dir, only a filename */
  773. cp = argv[i];
  774. }
  775. else
  776. {
  777. if (cp == argv[i])
  778. dir = "/"; /* file in root directory */
  779. else
  780. dir = argv[i];
  781. *cp++ = '\0'; /* neither of the above */
  782. }
  783. /* we'd better do a little bit of checking */
  784. if ((so = is_shlib(dir, cp, &libtype, &islink, LIB_ANY)) == NULL)
  785. err(EXIT_FATAL,"%s%s%s is not a shared library", dir,
  786. (*dir && strcmp(dir, "/")) ? "/" : "", cp);
  787. /* so far, so good, maybe he knows what he's doing */
  788. link_shlib(dir, cp, so);
  789. }
  790. }
  791. else
  792. {
  793. /* the lazy bum want's us to do all the work for him */
  794. /* don't cache dirs on the command line */
  795. int nocache_save = nocache;
  796. nocache = 1;
  797. /* OK, which directories should we do? */
  798. for (i = optind; i < argc; i++)
  799. scan_dir(argv[i]);
  800. /* restore the desired caching state */
  801. nocache = nocache_save;
  802. /* look ma, no defaults */
  803. if (!nodefault)
  804. {
  805. scan_dir(UCLIBC_RUNTIME_PREFIX "lib");
  806. scan_dir(UCLIBC_RUNTIME_PREFIX "usr/lib");
  807. #ifndef __LDSO_CACHE_SUPPORT__
  808. scan_dir(UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib");
  809. #else
  810. /* I guess the defaults aren't good enough */
  811. if ((extpath = get_extpath()))
  812. {
  813. for (cp = strtok(extpath, DIR_SEP); cp; cp = strtok(NULL, DIR_SEP)) {
  814. /* strip trailing slashes */
  815. int len = strlen(cp);
  816. if (len)
  817. while (cp[--len] == '/' && len)
  818. cp[len] = 0;
  819. /* we do the redundancy check only if cache usage is enabled */
  820. if (strcmp(UCLIBC_RUNTIME_PREFIX "lib", cp) == 0 ||
  821. strcmp(UCLIBC_RUNTIME_PREFIX "usr/lib", cp) == 0) {
  822. if (verbose >= 0)
  823. warnx("Remove `%s' from `%s'\n", cp, LDSO_CONF);
  824. continue;
  825. }
  826. scan_dir(cp);
  827. }
  828. free(extpath);
  829. }
  830. #endif
  831. }
  832. if (!nocache)
  833. cache_write();
  834. }
  835. exit(EXIT_OK);
  836. }