ldconfig.c 22 KB

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