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 <ldso.h>
  40. #include "readsoname.h"
  41. struct exec
  42. {
  43. unsigned long a_info; /* Use macros N_MAGIC, etc for access */
  44. unsigned a_text; /* length of text, in bytes */
  45. unsigned a_data; /* length of data, in bytes */
  46. unsigned a_bss; /* length of uninitialized data area for file, in bytes */
  47. unsigned a_syms; /* length of symbol table data in file, in bytes */
  48. unsigned a_entry; /* start address */
  49. unsigned a_trsize; /* length of relocation info for text, in bytes */
  50. unsigned a_drsize; /* length of relocation info for data, in bytes */
  51. };
  52. #if !defined (N_MAGIC)
  53. #define N_MAGIC(exec) ((exec).a_info & 0xffff)
  54. #endif
  55. /* Code indicating object file or impure executable. */
  56. #define OMAGIC 0407
  57. /* Code indicating pure executable. */
  58. #define NMAGIC 0410
  59. /* Code indicating demand-paged executable. */
  60. #define ZMAGIC 0413
  61. /* This indicates a demand-paged executable with the header in the text.
  62. The first page is unmapped to help trap NULL pointer references */
  63. #define QMAGIC 0314
  64. /* Code indicating core file. */
  65. #define CMAGIC 0421
  66. #ifdef __GNUC__
  67. void warn(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
  68. void error(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
  69. #endif
  70. char *___strtok = NULL;
  71. /* For SunOS */
  72. #ifndef PATH_MAX
  73. #include <limits.h>
  74. #define PATH_MAX _POSIX_PATH_MAX
  75. #endif
  76. /* For SunOS */
  77. #ifndef N_MAGIC
  78. #define N_MAGIC(exec) ((exec).a_magic & 0xffff)
  79. #endif
  80. #define EXIT_OK 0
  81. #define EXIT_FATAL 128
  82. char *prog = NULL;
  83. int debug = 0; /* debug mode */
  84. int verbose = 0; /* verbose mode */
  85. int libmode = 0; /* library mode */
  86. int nocache = 0; /* don't build cache */
  87. int nolinks = 0; /* don't update links */
  88. char *conffile = LDSO_CONF; /* default conf file */
  89. char *cachefile = LDSO_CACHE; /* default cache file */
  90. void cache_print(void);
  91. void cache_dolib(const char *dir, const char *so, int libtype);
  92. void cache_write(void);
  93. void warn(const char *fmt, ...)
  94. {
  95. va_list ap;
  96. if (verbose < 0)
  97. return;
  98. fflush(stdout); /* don't mix output and error messages */
  99. fprintf(stderr, "%s: warning: ", prog);
  100. va_start(ap, fmt);
  101. vfprintf(stderr, fmt, ap);
  102. va_end(ap);
  103. fprintf(stderr, "\n");
  104. return;
  105. }
  106. void error(const char *fmt, ...)
  107. {
  108. va_list ap;
  109. fflush(stdout); /* don't mix output and error messages */
  110. fprintf(stderr, "%s: ", prog);
  111. va_start(ap, fmt);
  112. vfprintf(stderr, fmt, ap);
  113. va_end(ap);
  114. fprintf(stderr, "\n");
  115. exit(EXIT_FATAL);
  116. }
  117. void *xmalloc(size_t size)
  118. {
  119. void *ptr;
  120. if ((ptr = malloc(size)) == NULL)
  121. error("out of memory");
  122. return ptr;
  123. }
  124. char *xstrdup(const char *str)
  125. {
  126. char *ptr;
  127. if ((ptr = strdup(str)) == NULL)
  128. error("out of memory");
  129. return ptr;
  130. }
  131. /* If shared library, return a malloced copy of the soname and set the
  132. type, else return NULL.
  133. expected_type should be either LIB_ANY or one of the following:-
  134. LIB_DLL
  135. LIB_ELF
  136. LIB_ELF_LIBC5
  137. LIB_ELF_LIBC6
  138. If the lib is ELF and we can not deduce the type the type will
  139. be set based on expected_type.
  140. If the expected, actual/deduced types missmatch we display a warning
  141. and use the actual/deduced type.
  142. */
  143. char *is_shlib(const char *dir, const char *name, int *type,
  144. int *islink, int expected_type)
  145. {
  146. char *good = NULL;
  147. char *cp, *cp2;
  148. FILE *file;
  149. struct exec exec;
  150. ElfW(Ehdr) *elf_hdr;
  151. struct stat statbuf;
  152. char buff[4096];
  153. /* see if name is of the form libZ.so* */
  154. if ((strncmp(name, "lib", 3) == 0 || strncmp(name, "ld-", 3) == 0) && \
  155. name[strlen(name)-1] != '~' && (cp = strstr(name, ".so")))
  156. {
  157. /* find the start of the Vminor part, if any */
  158. if (cp[3] == '.' && (cp2 = strchr(cp + 4, '.')))
  159. cp = cp2;
  160. else
  161. cp = cp + strlen(cp);
  162. /* construct the full path name */
  163. sprintf(buff, "%s%s%s", dir, (*dir && strcmp(dir, "/")) ?
  164. "/" : "", name);
  165. /* first, make sure it's a regular file */
  166. if (lstat(buff, &statbuf))
  167. warn("can't lstat %s (%s), skipping", buff, strerror(errno));
  168. else if (!S_ISREG(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode))
  169. warn("%s is not a regular file or symlink, skipping", buff);
  170. else
  171. {
  172. /* is it a regular file or a symlink */
  173. *islink = S_ISLNK(statbuf.st_mode);
  174. /* then try opening it */
  175. if (!(file = fopen(buff, "rb")))
  176. warn("can't open %s (%s), skipping", buff, strerror(errno));
  177. else
  178. {
  179. /* now make sure it's a shared library */
  180. if (fread(&exec, sizeof exec, 1, file) < 1)
  181. warn("can't read header from %s, skipping", buff);
  182. else if (N_MAGIC(exec) != ZMAGIC && N_MAGIC(exec) != QMAGIC)
  183. {
  184. elf_hdr = (ElfW(Ehdr) *) &exec;
  185. if (elf_hdr->e_ident[0] != 0x7f ||
  186. strncmp(&elf_hdr->e_ident[1], "ELF",3) != 0)
  187. {
  188. /* silently ignore linker scripts */
  189. if (strncmp((char *)&exec, "/* GNU ld", 9) != 0)
  190. warn("%s is not a shared library, skipping", buff);
  191. }
  192. else
  193. {
  194. /* always call readsoname to update type */
  195. if(expected_type == LIB_DLL){
  196. warn("%s is not an a.out library, its ELF!\n", buff);
  197. expected_type=LIB_ANY;
  198. }
  199. *type = LIB_ELF;
  200. good = readsoname(buff, file, expected_type, type,
  201. elf_hdr->e_ident[EI_CLASS]);
  202. if (good == NULL || *islink)
  203. {
  204. if (good != NULL)
  205. free(good);
  206. good = xstrdup(name);
  207. }
  208. else
  209. {
  210. /* if the soname does not match the filename,
  211. issue a warning, but only in debug mode. */
  212. int len = strlen(good);
  213. if (debug && (strncmp(good, name, len) != 0 ||
  214. (name[len] != '\0' && name[len] != '.')))
  215. warn("%s has inconsistent soname (%s)",
  216. buff, good);
  217. }
  218. }
  219. }
  220. else
  221. {
  222. if (*islink)
  223. good = xstrdup(name);
  224. else
  225. {
  226. good = xmalloc(cp - name + 1);
  227. strncpy(good, name, cp - name);
  228. good[cp - name] = '\0';
  229. }
  230. if(expected_type != LIB_ANY && expected_type != LIB_DLL)
  231. {
  232. warn("%s is not an ELF library, its an a.out DLL!", buff);
  233. expected_type=LIB_ANY;
  234. }
  235. *type = LIB_DLL;
  236. }
  237. fclose(file);
  238. }
  239. }
  240. }
  241. return good;
  242. }
  243. /* update the symlink to new library */
  244. void link_shlib(const char *dir, const char *file, const char *so)
  245. {
  246. int change = 1;
  247. char libname[4096];
  248. char linkname[4096];
  249. struct stat libstat;
  250. struct stat linkstat;
  251. /* construct the full path names */
  252. sprintf(libname, "%s/%s", dir, file);
  253. sprintf(linkname, "%s/%s", dir, so);
  254. /* see if a link already exists */
  255. if (!stat(linkname, &linkstat))
  256. {
  257. /* now see if it's the one we want */
  258. if (stat(libname, &libstat))
  259. warn("can't stat %s (%s)", libname, strerror(errno));
  260. else if (libstat.st_dev == linkstat.st_dev &&
  261. libstat.st_ino == linkstat.st_ino)
  262. change = 0;
  263. }
  264. /* then update the link, if required */
  265. if (change > 0 && !nolinks)
  266. {
  267. if (!lstat(linkname, &linkstat))
  268. {
  269. if (!S_ISLNK(linkstat.st_mode))
  270. {
  271. warn("%s is not a symlink", linkname);
  272. change = -1;
  273. }
  274. else if (remove(linkname))
  275. {
  276. warn("can't unlink %s (%s)", linkname, strerror(errno));
  277. change = -1;
  278. }
  279. }
  280. if (change > 0)
  281. {
  282. if (symlink(file, linkname))
  283. {
  284. warn("can't link %s to %s (%s)", linkname, file, strerror(errno));
  285. change = -1;
  286. }
  287. }
  288. }
  289. /* some people like to know what we're doing */
  290. if (verbose > 0)
  291. printf("\t%s => %s%s\n", so, file,
  292. change < 0 ? " (SKIPPED)" :
  293. (change > 0 ? " (changed)" : ""));
  294. return;
  295. }
  296. /* figure out which library is greater */
  297. int libcmp(char *p1, char *p2)
  298. {
  299. while (*p1)
  300. {
  301. if (isdigit(*p1) && isdigit(*p2))
  302. {
  303. /* must compare this numerically */
  304. int v1, v2;
  305. v1 = strtoul(p1, &p1, 10);
  306. v2 = strtoul(p2, &p2, 10);
  307. if (v1 != v2)
  308. return v1 - v2;
  309. }
  310. else if (isdigit(*p1) && !isdigit(*p2))
  311. return 1;
  312. else if (!isdigit(*p1) && isdigit(*p2))
  313. return -1;
  314. else if (*p1 != *p2)
  315. return *p1 - *p2;
  316. else
  317. p1++, p2++;
  318. }
  319. return *p1 - *p2;
  320. }
  321. struct lib
  322. {
  323. char *so; /* soname of a library */
  324. char *name; /* name of a library */
  325. int libtype; /* type of a library */
  326. int islink; /* is it a symlink */
  327. struct lib *next; /* next library in list */
  328. };
  329. /* update all shared library links in a directory */
  330. void scan_dir(const char *name)
  331. {
  332. DIR *dir;
  333. struct dirent *ent;
  334. char *so;
  335. struct lib *lp, *libs = NULL;
  336. int libtype, islink;
  337. int expected_type = LIB_ANY;
  338. char *t;
  339. /* Check for an embedded expected type */
  340. t=strrchr(name, '=');
  341. if( t )
  342. {
  343. *t++ = '\0'; /* Skip = char */
  344. if(strcasecmp(t, "libc4") == 0)
  345. {
  346. expected_type = LIB_DLL;
  347. }
  348. else
  349. {
  350. if(strcasecmp(t, "libc5") == 0)
  351. {
  352. expected_type = LIB_ELF_LIBC5;
  353. }
  354. else
  355. {
  356. if(strcasecmp(t, "libc6") == 0)
  357. {
  358. expected_type = LIB_ELF_LIBC6;
  359. }
  360. else
  361. {
  362. warn("Unknown type field '%s' for dir '%s' - ignored\n", t, name);
  363. expected_type = LIB_ANY;
  364. }
  365. }
  366. }
  367. }
  368. /* let 'em know what's going on */
  369. if (verbose > 0)
  370. printf("%s:\n", name);
  371. /* if we can't open it, we can't do anything */
  372. if ((dir = opendir(name)) == NULL)
  373. {
  374. warn("can't open %s (%s), skipping", name, strerror(errno));
  375. return;
  376. }
  377. /* yes, we have to look at every single file */
  378. while ((ent = readdir(dir)) != NULL)
  379. {
  380. /* if it's not a shared library, don't bother */
  381. if ((so = is_shlib(name, ent->d_name, &libtype, &islink, expected_type)) == NULL)
  382. continue;
  383. /* have we already seen one with the same so name? */
  384. for (lp = libs; lp; lp = lp->next)
  385. {
  386. if (strcmp(so, lp->so) == 0)
  387. {
  388. /* we have, which one do we want to use? */
  389. if ((!islink && lp->islink) ||
  390. (islink == lp->islink &&
  391. libcmp(ent->d_name, lp->name) > 0))
  392. {
  393. /* let's use the new one */
  394. free(lp->name);
  395. lp->name = xstrdup(ent->d_name);
  396. lp->libtype = libtype;
  397. lp->islink = islink;
  398. }
  399. break;
  400. }
  401. }
  402. /* congratulations, you're the first one we've seen */
  403. if (!lp)
  404. {
  405. lp = xmalloc(sizeof *lp);
  406. lp->so = xstrdup(so);
  407. lp->name = xstrdup(ent->d_name);
  408. lp->libtype = libtype;
  409. lp->islink = islink;
  410. lp->next = libs;
  411. libs = lp;
  412. }
  413. free(so);
  414. }
  415. /* don't need this any more */
  416. closedir(dir);
  417. /* now we have all the latest libs, update the links */
  418. for (lp = libs; lp; lp = lp->next)
  419. {
  420. if (!lp->islink)
  421. link_shlib(name, lp->name, lp->so);
  422. if (!nocache)
  423. cache_dolib(name, lp->so, lp->libtype);
  424. }
  425. /* always try to clean up after ourselves */
  426. while (libs)
  427. {
  428. lp = libs->next;
  429. free(libs->so);
  430. free(libs->name);
  431. free(libs);
  432. libs = lp;
  433. }
  434. return;
  435. }
  436. /* return the list of system-specific directories */
  437. char *get_extpath(void)
  438. {
  439. char *res = NULL, *cp;
  440. FILE *file;
  441. struct stat stat;
  442. if ((file = fopen(conffile, "r")) != NULL)
  443. {
  444. fstat(fileno(file), &stat);
  445. res = xmalloc(stat.st_size + 1);
  446. fread(res, 1, stat.st_size, file);
  447. fclose(file);
  448. res[stat.st_size] = '\0';
  449. /* convert comments fo spaces */
  450. for (cp = res; *cp; /*nada*/) {
  451. if (*cp == '#') {
  452. do
  453. *cp++ = ' ';
  454. while (*cp && *cp != '\n');
  455. } else {
  456. cp++;
  457. }
  458. }
  459. }
  460. return res;
  461. }
  462. void usage(void)
  463. {
  464. fprintf(stderr,
  465. "ldconfig - update shared library symlinks\n"
  466. "\n"
  467. "usage: ldconfig [-DvqnNX] [-f conf] [-C cache] [-r root] dir ...\n"
  468. " ldconfig -l [-Dv] lib ...\n"
  469. " ldconfig -p\n"
  470. "\t-D:\t\tdebug mode, don't update links\n"
  471. "\t-v:\t\tverbose mode, print things as we go\n"
  472. "\t-q:\t\tquiet mode, don't print warnings\n"
  473. "\t-n:\t\tdon't process standard directories\n"
  474. "\t-N:\t\tdon't update the library cache\n"
  475. "\t-X:\t\tdon't update the library links\n"
  476. "\t-l:\t\tlibrary mode, manually link libraries\n"
  477. "\t-p:\t\tprint the current library cache\n"
  478. "\t-f conf :\tuse conf instead of %s\n"
  479. "\t-C cache:\tuse cache instead of %s\n"
  480. "\t-r root :\tfirst, do a chroot to the indicated directory\n"
  481. "\tdir ... :\tdirectories to process\n"
  482. "\tlib ... :\tlibraries to link\n"
  483. "\n"
  484. "Copyright 1994-2000 David Engel and Mitch D'Souza\n",
  485. LDSO_CONF, LDSO_CACHE
  486. );
  487. exit(EXIT_FATAL);
  488. }
  489. #define DIR_SEP ":, \t\n"
  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. }