ldconfig.c 20 KB

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