ldconfig.c 21 KB

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