ldconfig.c 22 KB

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