ldconfig.c 22 KB

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