ldconfig.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  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. * 2005/09/16: Dan Howell (modified for cross-development)
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <stdarg.h>
  31. #include <string.h>
  32. #include <ctype.h>
  33. #include <getopt.h>
  34. #include <dirent.h>
  35. #include <unistd.h>
  36. #include <link.h>
  37. #include <fcntl.h>
  38. #include <errno.h>
  39. #include <sys/stat.h>
  40. #include <sys/mman.h>
  41. #include "bswap.h"
  42. #include "dl-defs.h"
  43. #define BUFFER_SIZE 4096
  44. struct exec
  45. {
  46. unsigned long a_info; /* Use macros N_MAGIC, etc for access */
  47. unsigned a_text; /* length of text, in bytes */
  48. unsigned a_data; /* length of data, in bytes */
  49. unsigned a_bss; /* length of uninitialized data area for file, in bytes */
  50. unsigned a_syms; /* length of symbol table data in file, in bytes */
  51. unsigned a_entry; /* start address */
  52. unsigned a_trsize; /* length of relocation info for text, in bytes */
  53. unsigned a_drsize; /* length of relocation info for data, in bytes */
  54. };
  55. #if !defined (N_MAGIC)
  56. #define N_MAGIC(exec) ((exec).a_info & 0xffff)
  57. #endif
  58. #define N_MAGIC_SWAP(exec) (bswap_32((exec).a_info) & 0xffff)
  59. /* Code indicating object file or impure executable. */
  60. #define OMAGIC 0407
  61. /* Code indicating pure executable. */
  62. #define NMAGIC 0410
  63. /* Code indicating demand-paged executable. */
  64. #define ZMAGIC 0413
  65. /* This indicates a demand-paged executable with the header in the text.
  66. The first page is unmapped to help trap NULL pointer references */
  67. #define QMAGIC 0314
  68. /* Code indicating core file. */
  69. #define CMAGIC 0421
  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 nolinks = 0; /* don't update links */
  87. int nocache = 0; /* don't build cache */
  88. void cache_print(void);
  89. void cache_write(void);
  90. void cache_dolib(const char *dir, const char *so, int libtype);
  91. #ifdef __LDSO_CACHE_SUPPORT__
  92. char *conffile = LDSO_CONF; /* default conf file */
  93. char *cachefile = LDSO_CACHE; /* default cache file */
  94. #endif
  95. char *chroot_dir = NULL;
  96. int byteswap = 0;
  97. struct needed_tab
  98. {
  99. char *soname;
  100. int type;
  101. };
  102. struct needed_tab needed_tab[] = {
  103. { "libc.so.0", LIB_ELF_LIBC0 },
  104. { "libm.so.0", LIB_ELF_LIBC0 },
  105. { "libdl.so.0", LIB_ELF_LIBC0 },
  106. { "libc.so.5", LIB_ELF_LIBC5 },
  107. { "libm.so.5", LIB_ELF_LIBC5 },
  108. { "libdl.so.1", LIB_ELF_LIBC5 },
  109. { "libc.so.6", LIB_ELF_LIBC6 },
  110. { "libm.so.6", LIB_ELF_LIBC6 },
  111. { "libdl.so.2", LIB_ELF_LIBC6 },
  112. { NULL, LIB_ELF }
  113. };
  114. extern char *chroot_realpath(const char *chroot, const char *path, char resolved_path[]);
  115. /* These two are used internally -- you shouldn't need to use them */
  116. static void verror_msg(const char *s, va_list p)
  117. {
  118. fflush(stdout);
  119. fprintf(stderr, "%s: ", prog);
  120. vfprintf(stderr, s, p);
  121. }
  122. static void warnx(const char *s, ...)
  123. {
  124. va_list p;
  125. va_start(p, s);
  126. verror_msg(s, p);
  127. va_end(p);
  128. fprintf(stderr, "\n");
  129. }
  130. static void err(int errnum, const char *s, ...)
  131. {
  132. va_list p;
  133. va_start(p, s);
  134. verror_msg(s, p);
  135. va_end(p);
  136. fprintf(stderr, "\n");
  137. exit(errnum);
  138. }
  139. static void vperror_msg(const char *s, va_list p)
  140. {
  141. int err = errno;
  142. if (s == 0)
  143. s = "";
  144. verror_msg(s, p);
  145. if (*s)
  146. s = ": ";
  147. fprintf(stderr, "%s%s\n", s, strerror(err));
  148. }
  149. static void warn(const char *s, ...)
  150. {
  151. va_list p;
  152. va_start(p, s);
  153. vperror_msg(s, p);
  154. va_end(p);
  155. }
  156. static void *xmalloc(size_t size)
  157. {
  158. void *ptr;
  159. if ((ptr = malloc(size)) == NULL)
  160. err(EXIT_FATAL,"out of memory");
  161. return ptr;
  162. }
  163. static char *xstrdup(const char *str)
  164. {
  165. char *ptr;
  166. if ((ptr = strdup(str)) == NULL)
  167. err(EXIT_FATAL,"out of memory");
  168. return ptr;
  169. }
  170. #undef __ELF_NATIVE_CLASS
  171. #undef readsonameXX
  172. #define readsonameXX readsoname32
  173. #define __ELF_NATIVE_CLASS 32
  174. #include "readsoname2.c"
  175. #undef __ELF_NATIVE_CLASS
  176. #undef readsonameXX
  177. #define readsonameXX readsoname64
  178. #define __ELF_NATIVE_CLASS 64
  179. #include "readsoname2.c"
  180. char *readsoname(char *name, FILE *infile, int expected_type,
  181. int *type, int elfclass)
  182. {
  183. char *res;
  184. if (elfclass == ELFCLASS32)
  185. res = readsoname32(name, infile, expected_type, type);
  186. else
  187. {
  188. res = readsoname64(name, infile, expected_type, type);
  189. #if 0
  190. *type |= LIB_ELF64;
  191. #endif
  192. }
  193. return res;
  194. }
  195. /* If shared library, return a malloced copy of the soname and set the
  196. type, else return NULL.
  197. expected_type should be either LIB_ANY or one of the following:-
  198. LIB_DLL
  199. LIB_ELF
  200. LIB_ELF_LIBC5
  201. LIB_ELF_LIBC6
  202. If the lib is ELF and we can not deduce the type the type will
  203. be set based on expected_type.
  204. If the expected, actual/deduced types missmatch we display a warning
  205. and use the actual/deduced type.
  206. */
  207. char *is_shlib(const char *dir, const char *name, int *type,
  208. int *islink, int expected_type)
  209. {
  210. char *good = NULL;
  211. char *cp, *cp2;
  212. FILE *file;
  213. struct exec exec;
  214. ElfW(Ehdr) *elf_hdr;
  215. struct stat statbuf;
  216. char buff[BUFFER_SIZE];
  217. char real[BUFFER_SIZE];
  218. static int byteswapflag = -1; /* start with byte-order unknown */
  219. /* see if name is of the form *.so* */
  220. if (name[strlen(name)-1] != '~' && (cp = strstr(name, ".so")))
  221. {
  222. /* find the start of the Vminor part, if any */
  223. if (cp[3] == '.' && (cp2 = strchr(cp + 4, '.')))
  224. cp = cp2;
  225. else
  226. cp = cp + strlen(cp);
  227. /* construct the full path name */
  228. sprintf(buff, "%s%s%s", dir, (*dir && strcmp(dir, "/")) ?
  229. "/" : "", name);
  230. /* get real path in case of chroot */
  231. if (!chroot_realpath(chroot_dir, buff, real))
  232. warn("can't resolve %s in chroot %s", buff, chroot_dir);
  233. /* first, make sure it's a regular file */
  234. if (lstat(real, &statbuf))
  235. warn("skipping %s", buff);
  236. else if (!S_ISREG(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode))
  237. warnx("%s is not a regular file or symlink, skipping", buff);
  238. else
  239. {
  240. /* is it a regular file or a symlink */
  241. *islink = S_ISLNK(statbuf.st_mode);
  242. /* then try opening it */
  243. if (!(file = fopen(real, "rb")))
  244. warn("skipping %s", buff);
  245. else
  246. {
  247. /* now make sure it's a shared library */
  248. if (fread(&exec, sizeof exec, 1, file) < 1)
  249. warnx("can't read header from %s, skipping", buff);
  250. else if (N_MAGIC(exec) != ZMAGIC && N_MAGIC(exec) != QMAGIC &&
  251. N_MAGIC_SWAP(exec) != ZMAGIC && N_MAGIC_SWAP(exec) != QMAGIC)
  252. {
  253. elf_hdr = (ElfW(Ehdr) *) &exec;
  254. if (elf_hdr->e_ident[0] != 0x7f ||
  255. strncmp(&elf_hdr->e_ident[1], "ELF",3) != 0)
  256. {
  257. /* silently ignore linker scripts */
  258. if (strncmp((char *)&exec, "/* GNU ld", 9) != 0)
  259. warnx("%s is not a shared library, skipping", buff);
  260. }
  261. else
  262. {
  263. /* always call readsoname to update type */
  264. if(expected_type == LIB_DLL) {
  265. warnx("%s is not an a.out library, its ELF!\n", buff);
  266. expected_type=LIB_ANY;
  267. }
  268. *type = LIB_ELF;
  269. good = readsoname(buff, file, expected_type, type,
  270. elf_hdr->e_ident[EI_CLASS]);
  271. if (byteswapflag == -1)
  272. /* byte-order detected */
  273. byteswapflag = byteswap;
  274. if (good == NULL || *islink)
  275. {
  276. if (good != NULL)
  277. free(good);
  278. good = xstrdup(name);
  279. }
  280. else
  281. {
  282. /* if the soname does not match the filename,
  283. issue a warning, but only in debug mode. */
  284. int len = strlen(good);
  285. if (debug && (strncmp(good, name, len) != 0 ||
  286. (name[len] != '\0' && name[len] != '.')))
  287. warnx("%s has inconsistent soname (%s)", buff, good);
  288. }
  289. }
  290. }
  291. else
  292. {
  293. /* Determine byte-order */
  294. byteswap = (N_MAGIC(exec) == ZMAGIC || N_MAGIC(exec) == QMAGIC) ? 0 : 1;
  295. if (byteswapflag == -1)
  296. /* byte-order detected */
  297. byteswapflag = byteswap;
  298. if (*islink)
  299. good = xstrdup(name);
  300. else
  301. {
  302. good = xmalloc(cp - name + 1);
  303. strncpy(good, name, cp - name);
  304. good[cp - name] = '\0';
  305. }
  306. if(expected_type != LIB_ANY && expected_type != LIB_DLL)
  307. {
  308. warnx("%s is not an ELF library, its an a.out DLL!", buff);
  309. expected_type=LIB_ANY;
  310. }
  311. *type = LIB_DLL;
  312. }
  313. fclose(file);
  314. if (byteswapflag >= 0 && byteswap != byteswapflag)
  315. {
  316. byteswapflag = -2;
  317. warnx("mixed byte-order detected, using host byte-order...");
  318. }
  319. if (byteswapflag == -2)
  320. byteswap = 0;
  321. }
  322. }
  323. }
  324. return good;
  325. }
  326. /* update the symlink to new library */
  327. void link_shlib(const char *dir, const char *file, const char *so)
  328. {
  329. int change = 1;
  330. char libname[BUFFER_SIZE];
  331. char linkname[BUFFER_SIZE];
  332. char reallibname[BUFFER_SIZE];
  333. char reallinkname[BUFFER_SIZE];
  334. struct stat libstat;
  335. struct stat linkstat;
  336. /* construct the full path names */
  337. sprintf(libname, "%s/%s", dir, file);
  338. sprintf(linkname, "%s/%s", dir, so);
  339. if (!chroot_realpath(chroot_dir, libname, reallibname))
  340. warn("can't resolve %s in chroot %s", libname, chroot_dir);
  341. if (!chroot_realpath(chroot_dir, linkname, reallinkname))
  342. warn("can't resolve %s in chroot %s", linkname, chroot_dir);
  343. /* see if a link already exists */
  344. if (!stat(reallinkname, &linkstat))
  345. {
  346. /* now see if it's the one we want */
  347. if (stat(reallibname, &libstat))
  348. warn("can't stat %s", libname);
  349. else if (libstat.st_dev == linkstat.st_dev &&
  350. libstat.st_ino == linkstat.st_ino)
  351. change = 0;
  352. }
  353. /* then update the link, if required */
  354. if (change > 0 && !nolinks)
  355. {
  356. if (!lstat(reallinkname, &linkstat))
  357. {
  358. if (!S_ISLNK(linkstat.st_mode))
  359. {
  360. warnx("%s is not a symlink", linkname);
  361. change = -1;
  362. }
  363. else if (remove(reallinkname))
  364. {
  365. warn("can't unlink %s", linkname);
  366. change = -1;
  367. }
  368. }
  369. if (change > 0)
  370. {
  371. if (symlink(file, reallinkname))
  372. {
  373. warn("can't link %s to %s", linkname, file);
  374. change = -1;
  375. }
  376. }
  377. }
  378. /* some people like to know what we're doing */
  379. if (verbose > 0)
  380. printf("\t%s => %s%s\n", so, file,
  381. change < 0 ? " (SKIPPED)" :
  382. (change > 0 ? " (changed)" : ""));
  383. return;
  384. }
  385. /* figure out which library is greater */
  386. int libcmp(char *p1, char *p2)
  387. {
  388. while (*p1)
  389. {
  390. if (isdigit(*p1) && isdigit(*p2))
  391. {
  392. /* must compare this numerically */
  393. int v1, v2;
  394. v1 = strtoul(p1, &p1, 10);
  395. v2 = strtoul(p2, &p2, 10);
  396. if (v1 != v2)
  397. return v1 - v2;
  398. }
  399. else if (isdigit(*p1) && !isdigit(*p2))
  400. return 1;
  401. else if (!isdigit(*p1) && isdigit(*p2))
  402. return -1;
  403. else if (*p1 != *p2)
  404. return *p1 - *p2;
  405. else
  406. p1++, p2++;
  407. }
  408. return *p1 - *p2;
  409. }
  410. struct lib
  411. {
  412. char *so; /* soname of a library */
  413. char *name; /* name of a library */
  414. int libtype; /* type of a library */
  415. int islink; /* is it a symlink */
  416. struct lib *next; /* next library in list */
  417. };
  418. /* update all shared library links in a directory */
  419. void scan_dir(const char *rawname)
  420. {
  421. DIR *dir;
  422. const char *name;
  423. struct dirent *ent;
  424. char *so, *path, *path_n;
  425. struct lib *lp, *libs = NULL;
  426. int i, libtype, islink, expected_type = LIB_ANY;
  427. char realname[BUFFER_SIZE];
  428. /* We need a writable copy of this string */
  429. path = strdup(rawname);
  430. if (!path) {
  431. err(EXIT_FATAL, "Out of memory!\n");
  432. }
  433. /* Eliminate all double //s */
  434. path_n=path;
  435. while((path_n=strstr(path_n, "//"))) {
  436. i = strlen(path_n);
  437. memmove(path_n, path_n+1, i-1);
  438. *(path_n + i - 1)='\0';
  439. }
  440. name = path;
  441. #if 0
  442. char *t;
  443. /* Check for an embedded expected type */
  444. t=strrchr(name, '=');
  445. if( t )
  446. {
  447. *t++ = '\0'; /* Skip = char */
  448. if(strcasecmp(t, "libc4") == 0)
  449. {
  450. expected_type = LIB_DLL;
  451. }
  452. else
  453. {
  454. if(strcasecmp(t, "libc5") == 0)
  455. {
  456. expected_type = LIB_ELF_LIBC5;
  457. }
  458. else
  459. {
  460. if(strcasecmp(t, "libc6") == 0)
  461. {
  462. expected_type = LIB_ELF_LIBC6;
  463. }
  464. else
  465. {
  466. if(strcasecmp(t, "libc0") == 0)
  467. {
  468. expected_type = LIB_ELF_LIBC0;
  469. }
  470. else
  471. {
  472. warnx("Unknown type field '%s' for dir '%s' - ignored\n", t, name);
  473. expected_type = LIB_ANY;
  474. }
  475. }
  476. }
  477. }
  478. }
  479. #endif
  480. /* let 'em know what's going on */
  481. if (verbose > 0)
  482. printf("%s:\n", name);
  483. /* get real path in case of chroot */
  484. if (!chroot_realpath(chroot_dir, name, realname))
  485. warn("can't resolve %s in chroot %s", name, chroot_dir);
  486. /* if we can't open it, we can't do anything */
  487. if ((dir = opendir(realname)) == NULL)
  488. {
  489. warn("skipping %s", name);
  490. free(path);
  491. return;
  492. }
  493. /* yes, we have to look at every single file */
  494. while ((ent = readdir(dir)) != NULL)
  495. {
  496. /* if it's not a shared library, don't bother */
  497. if ((so = is_shlib(name, ent->d_name, &libtype, &islink, expected_type)) == NULL)
  498. continue;
  499. /* have we already seen one with the same so name? */
  500. for (lp = libs; lp; lp = lp->next)
  501. {
  502. if (strcmp(so, lp->so) == 0)
  503. {
  504. /* we have, which one do we want to use? */
  505. if ((!islink && lp->islink) ||
  506. (islink == lp->islink &&
  507. libcmp(ent->d_name, lp->name) > 0))
  508. {
  509. /* let's use the new one */
  510. free(lp->name);
  511. lp->name = xstrdup(ent->d_name);
  512. lp->libtype = libtype;
  513. lp->islink = islink;
  514. }
  515. break;
  516. }
  517. }
  518. /* congratulations, you're the first one we've seen */
  519. if (!lp)
  520. {
  521. lp = xmalloc(sizeof *lp);
  522. lp->so = xstrdup(so);
  523. lp->name = xstrdup(ent->d_name);
  524. lp->libtype = libtype;
  525. lp->islink = islink;
  526. lp->next = libs;
  527. libs = lp;
  528. }
  529. free(so);
  530. }
  531. /* don't need this any more */
  532. closedir(dir);
  533. /* now we have all the latest libs, update the links */
  534. for (lp = libs; lp; lp = lp->next)
  535. {
  536. if (!lp->islink)
  537. link_shlib(name, lp->name, lp->so);
  538. if (!nocache)
  539. cache_dolib(name, lp->so, lp->libtype);
  540. }
  541. /* always try to clean up after ourselves */
  542. while (libs)
  543. {
  544. lp = libs->next;
  545. free(libs->so);
  546. free(libs->name);
  547. free(libs);
  548. libs = lp;
  549. }
  550. free(path);
  551. return;
  552. }
  553. #ifndef __LDSO_CACHE_SUPPORT__
  554. void cache_print(void)
  555. {
  556. printf("Library cache disabled\n");
  557. }
  558. void cache_dolib(const char *dir, const char *so, int libtype)
  559. {
  560. return;
  561. }
  562. void cache_write(void)
  563. {
  564. return;
  565. }
  566. #else
  567. /* return the list of system-specific directories */
  568. char *get_extpath(void)
  569. {
  570. char *res = NULL, *cp;
  571. FILE *file;
  572. struct stat stat;
  573. char realconffile[BUFFER_SIZE];
  574. if (!chroot_realpath(chroot_dir, conffile, realconffile))
  575. return NULL;
  576. if ((file = fopen(realconffile, "r")) != NULL)
  577. {
  578. fstat(fileno(file), &stat);
  579. res = xmalloc(stat.st_size + 1);
  580. fread(res, 1, stat.st_size, file);
  581. fclose(file);
  582. res[stat.st_size] = '\0';
  583. /* convert comments fo spaces */
  584. for (cp = res; *cp; /*nada*/) {
  585. if (*cp == '#') {
  586. do
  587. *cp++ = ' ';
  588. while (*cp && *cp != '\n');
  589. } else {
  590. cp++;
  591. }
  592. }
  593. }
  594. return res;
  595. }
  596. typedef struct liblist
  597. {
  598. int flags;
  599. int sooffset;
  600. int liboffset;
  601. char *soname;
  602. char *libname;
  603. struct liblist *next;
  604. } liblist_t;
  605. static header_t magic = { LDSO_CACHE_MAGIC, LDSO_CACHE_VER, 0 };
  606. static liblist_t *lib_head = NULL;
  607. static int liblistcomp(liblist_t *x, liblist_t *y)
  608. {
  609. int res;
  610. if ((res = libcmp(x->soname, y->soname)) == 0)
  611. {
  612. res = libcmp(strrchr(x->libname, '/') + 1,
  613. strrchr(y->libname, '/') + 1);
  614. }
  615. return res;
  616. }
  617. void cache_dolib(const char *dir, const char *so, int libtype)
  618. {
  619. char fullpath[PATH_MAX];
  620. liblist_t *new_lib, *cur_lib;
  621. magic.nlibs++;
  622. sprintf(fullpath, "%s/%s", dir, so);
  623. new_lib = xmalloc(sizeof (liblist_t));
  624. new_lib->flags = libtype;
  625. new_lib->soname = xstrdup(so);
  626. new_lib->libname = xstrdup(fullpath);
  627. if (lib_head == NULL || liblistcomp(new_lib, lib_head) > 0)
  628. {
  629. new_lib->next = lib_head;
  630. lib_head = new_lib;
  631. }
  632. else
  633. {
  634. for (cur_lib = lib_head; cur_lib->next != NULL &&
  635. liblistcomp(new_lib, cur_lib->next) <= 0;
  636. cur_lib = cur_lib->next)
  637. /* nothing */;
  638. new_lib->next = cur_lib->next;
  639. cur_lib->next = new_lib;
  640. }
  641. }
  642. void cache_write(void)
  643. {
  644. int cachefd;
  645. int stroffset = 0;
  646. char realcachefile[BUFFER_SIZE];
  647. char tempfile[BUFFER_SIZE];
  648. header_t swap_magic;
  649. header_t *magic_ptr;
  650. libentry_t swap_lib;
  651. libentry_t *lib_ptr;
  652. liblist_t *cur_lib;
  653. if (!magic.nlibs)
  654. return;
  655. if (!chroot_realpath(chroot_dir, cachefile, realcachefile))
  656. err(EXIT_FATAL,"can't resolve %s in chroot %s (%s)",
  657. cachefile, chroot_dir, strerror(errno));
  658. sprintf(tempfile, "%s~", realcachefile);
  659. if (unlink(tempfile) && errno != ENOENT)
  660. err(EXIT_FATAL,"can't unlink %s~ (%s)", cachefile, strerror(errno));
  661. if ((cachefd = creat(tempfile, 0644)) < 0)
  662. err(EXIT_FATAL,"can't create %s~ (%s)", cachefile, strerror(errno));
  663. if (byteswap) {
  664. swap_magic = magic;
  665. swap_magic.nlibs = bswap_32(swap_magic.nlibs);
  666. magic_ptr = &swap_magic;
  667. } else {
  668. magic_ptr = &magic;
  669. }
  670. if (write(cachefd, magic_ptr, sizeof (header_t)) != sizeof (header_t))
  671. err(EXIT_FATAL,"can't write %s~ (%s)", cachefile, strerror(errno));
  672. for (cur_lib = lib_head; cur_lib != NULL; cur_lib = cur_lib->next)
  673. {
  674. cur_lib->sooffset = stroffset;
  675. stroffset += strlen(cur_lib->soname) + 1;
  676. cur_lib->liboffset = stroffset;
  677. stroffset += strlen(cur_lib->libname) + 1;
  678. if (byteswap) {
  679. swap_lib.flags = bswap_32(cur_lib->flags);
  680. swap_lib.sooffset = bswap_32(cur_lib->sooffset);
  681. swap_lib.liboffset = bswap_32(cur_lib->liboffset);
  682. lib_ptr = &swap_lib;
  683. } else {
  684. lib_ptr = (libentry_t *)cur_lib;
  685. }
  686. if (write(cachefd, lib_ptr, sizeof (libentry_t)) !=
  687. sizeof (libentry_t))
  688. err(EXIT_FATAL,"can't write %s~ (%s)", cachefile, strerror(errno));
  689. }
  690. for (cur_lib = lib_head; cur_lib != NULL; cur_lib = cur_lib->next)
  691. {
  692. if (write(cachefd, cur_lib->soname, strlen(cur_lib->soname) + 1)
  693. != strlen(cur_lib->soname) + 1)
  694. err(EXIT_FATAL,"can't write %s~ (%s)", cachefile, strerror(errno));
  695. if (write(cachefd, cur_lib->libname, strlen(cur_lib->libname) + 1)
  696. != strlen(cur_lib->libname) + 1)
  697. err(EXIT_FATAL,"can't write %s~ (%s)", cachefile, strerror(errno));
  698. }
  699. if (close(cachefd))
  700. err(EXIT_FATAL,"can't close %s~ (%s)", cachefile, strerror(errno));
  701. if (chmod(tempfile, 0644))
  702. err(EXIT_FATAL,"can't chmod %s~ (%s)", cachefile, strerror(errno));
  703. if (rename(tempfile, realcachefile))
  704. err(EXIT_FATAL,"can't rename %s~ (%s)", cachefile, strerror(errno));
  705. }
  706. void cache_print(void)
  707. {
  708. caddr_t c;
  709. struct stat st;
  710. int fd = 0;
  711. char *strs;
  712. header_t *header;
  713. libentry_t *libent;
  714. char realcachefile[BUFFER_SIZE];
  715. if (!chroot_realpath(chroot_dir, cachefile, realcachefile))
  716. err(EXIT_FATAL,"can't resolve %s in chroot %s (%s)",
  717. cachefile, chroot_dir, strerror(errno));
  718. if (stat(realcachefile, &st) || (fd = open(realcachefile, O_RDONLY))<0)
  719. err(EXIT_FATAL,"can't read %s (%s)", cachefile, strerror(errno));
  720. if ((c = mmap(0,st.st_size, PROT_READ, MAP_SHARED ,fd, 0)) == (caddr_t)-1)
  721. err(EXIT_FATAL,"can't map %s (%s)", cachefile, strerror(errno));
  722. close(fd);
  723. if (memcmp(((header_t *)c)->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN))
  724. err(EXIT_FATAL,"%s cache corrupt", cachefile);
  725. if (memcmp(((header_t *)c)->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN))
  726. err(EXIT_FATAL,"wrong cache version - expected %s", LDSO_CACHE_VER);
  727. header = (header_t *)c;
  728. libent = (libentry_t *)(c + sizeof (header_t));
  729. strs = (char *)&libent[header->nlibs];
  730. printf("%d libs found in cache `%s' (version %s)\n",
  731. header->nlibs, cachefile, LDSO_CACHE_VER);
  732. for (fd = 0; fd < header->nlibs; fd++)
  733. {
  734. printf("\t%s ", strs + libent[fd].sooffset);
  735. switch (libent[fd].flags & ~LIB_ELF64)
  736. {
  737. case LIB_DLL:
  738. printf("(libc4)");
  739. break;
  740. case LIB_ELF:
  741. printf("(ELF%s)", libent[fd].flags & LIB_ELF64 ? "/64" : "");
  742. break;
  743. case LIB_ELF_LIBC0:
  744. printf("(libc0%s)",libent[fd].flags & LIB_ELF64 ? "/64" : "");
  745. break;
  746. case LIB_ELF_LIBC5:
  747. case LIB_ELF_LIBC6:
  748. printf("(libc%d%s)", (libent[fd].flags & ~LIB_ELF64) + 3,
  749. libent[fd].flags & LIB_ELF64 ? "/64" : "");
  750. break;
  751. default:
  752. printf("(unknown)");
  753. break;
  754. }
  755. printf(" => %s\n", strs + libent[fd].liboffset);
  756. }
  757. munmap (c,st.st_size);
  758. }
  759. #endif
  760. void usage(void)
  761. {
  762. fprintf(stderr,
  763. #ifdef __LDSO_CACHE_SUPPORT__
  764. "ldconfig - updates symlinks and cache for shared libraries\n\n"
  765. "Usage: ldconfig [-DvqnNX] [-f conf] [-C cache] [-r root] dir ...\n"
  766. " ldconfig -l [-Dv] lib ...\n"
  767. " ldconfig -p\n\nOptions:\n"
  768. #else
  769. "ldconfig - updates symlinks for shared libraries\n\n"
  770. "Usage: ldconfig [-DvqnX] [-r root] dir ...\n"
  771. " ldconfig -l [-Dv] lib ...\n\nOptions:\n"
  772. #endif
  773. "\t-D:\t\tdebug mode, don't update links\n"
  774. "\t-v:\t\tverbose mode, print things as we go\n"
  775. "\t-q:\t\tquiet mode, don't print warnings\n"
  776. "\t-n:\t\tdon't process standard directories\n"
  777. "\t-N:\t\tdon't update the library cache\n"
  778. "\t-X:\t\tdon't update the library links\n"
  779. "\t-l:\t\tlibrary mode, manually link libraries\n"
  780. "\t-p:\t\tprint the current library cache\n"
  781. #ifdef __LDSO_CACHE_SUPPORT__
  782. "\t-f conf :\tuse conf instead of %s\n"
  783. "\t-C cache:\tuse cache instead of %s\n"
  784. #endif
  785. "\t-r root :\tfirst, do a chroot to the indicated directory\n"
  786. "\tdir ... :\tdirectories to process\n"
  787. #ifdef __LDSO_CACHE_SUPPORT__
  788. "\tlib ... :\tlibraries to link\n\n",
  789. LDSO_CONF, LDSO_CACHE
  790. #else
  791. "\tlib ... :\tlibraries to link\n\n"
  792. #endif
  793. );
  794. exit(EXIT_FATAL);
  795. }
  796. #define DIR_SEP ":, \t\n"
  797. int main(int argc, char **argv)
  798. {
  799. int i, c;
  800. int nodefault = 0;
  801. char *cp, *dir, *so;
  802. int libtype, islink;
  803. int printcache = 0;
  804. #ifdef __LDSO_CACHE_SUPPORT__
  805. char *extpath;
  806. #endif
  807. prog = argv[0];
  808. opterr = 0;
  809. while ((c = getopt(argc, argv, "DvqnNXlpf:C:r:")) != EOF)
  810. switch (c)
  811. {
  812. case 'D':
  813. debug = 1; /* debug mode */
  814. nocache = 1;
  815. nolinks = 1;
  816. verbose = 1;
  817. break;
  818. case 'v':
  819. verbose = 1; /* verbose mode */
  820. break;
  821. case 'q':
  822. if (verbose <= 0)
  823. verbose = -1; /* quiet mode */
  824. break;
  825. case 'n':
  826. nodefault = 1; /* no default dirs */
  827. nocache = 1;
  828. break;
  829. case 'N':
  830. nocache = 1; /* don't build cache */
  831. break;
  832. case 'X':
  833. nolinks = 1; /* don't update links */
  834. break;
  835. case 'l':
  836. libmode = 1; /* library mode */
  837. break;
  838. case 'p':
  839. printcache = 1; /* print cache */
  840. break;
  841. case 'f':
  842. #ifdef __LDSO_CACHE_SUPPORT__
  843. conffile = optarg; /* alternate conf file */
  844. #endif
  845. break;
  846. case 'C':
  847. #ifdef __LDSO_CACHE_SUPPORT__
  848. cachefile = optarg; /* alternate cache file */
  849. #endif
  850. break;
  851. case 'r':
  852. chroot_dir = optarg;
  853. break;
  854. default:
  855. usage();
  856. break;
  857. /* THE REST OF THESE ARE UNDOCUMENTED AND MAY BE REMOVED
  858. IN FUTURE VERSIONS. */
  859. }
  860. if (chroot_dir && *chroot_dir) {
  861. if (chroot(chroot_dir) < 0) {
  862. if (chdir(chroot_dir) < 0)
  863. err(EXIT_FATAL,"couldn't chroot to %s (%s)", chroot_dir, strerror(errno));
  864. }
  865. else
  866. {
  867. if (chdir("/") < 0)
  868. err(EXIT_FATAL,"couldn't chdir to / (%s)", strerror(errno));
  869. chroot_dir = NULL;
  870. }
  871. }
  872. /* allow me to introduce myself, hi, my name is ... */
  873. if (verbose > 0)
  874. printf("%s: uClibc version\n", argv[0]);
  875. if (printcache)
  876. {
  877. /* print the cache -- don't you trust me? */
  878. cache_print();
  879. exit(EXIT_OK);
  880. }
  881. else if (libmode)
  882. {
  883. /* so you want to do things manually, eh? */
  884. /* ok, if you're so smart, which libraries do we link? */
  885. for (i = optind; i < argc; i++)
  886. {
  887. /* split into directory and file parts */
  888. if (!(cp = strrchr(argv[i], '/')))
  889. {
  890. dir = "."; /* no dir, only a filename */
  891. cp = argv[i];
  892. }
  893. else
  894. {
  895. if (cp == argv[i])
  896. dir = "/"; /* file in root directory */
  897. else
  898. dir = argv[i];
  899. *cp++ = '\0'; /* neither of the above */
  900. }
  901. /* we'd better do a little bit of checking */
  902. if ((so = is_shlib(dir, cp, &libtype, &islink, LIB_ANY)) == NULL)
  903. err(EXIT_FATAL,"%s%s%s is not a shared library", dir,
  904. (*dir && strcmp(dir, "/")) ? "/" : "", cp);
  905. /* so far, so good, maybe he knows what he's doing */
  906. link_shlib(dir, cp, so);
  907. }
  908. }
  909. else
  910. {
  911. /* the lazy bum want's us to do all the work for him */
  912. /* don't cache dirs on the command line */
  913. int nocache_save = nocache;
  914. nocache = 1;
  915. /* OK, which directories should we do? */
  916. for (i = optind; i < argc; i++)
  917. scan_dir(argv[i]);
  918. /* restore the desired caching state */
  919. nocache = nocache_save;
  920. /* look ma, no defaults */
  921. if (!nodefault)
  922. {
  923. scan_dir(UCLIBC_RUNTIME_PREFIX "lib");
  924. scan_dir(UCLIBC_RUNTIME_PREFIX "usr/lib");
  925. #ifndef __LDSO_CACHE_SUPPORT__
  926. scan_dir(UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib");
  927. #else
  928. /* I guess the defaults aren't good enough */
  929. if ((extpath = get_extpath()))
  930. {
  931. for (cp = strtok(extpath, DIR_SEP); cp; cp = strtok(NULL, DIR_SEP)) {
  932. /* strip trailing slashes */
  933. int len = strlen(cp);
  934. if (len)
  935. while (cp[--len] == '/' && len)
  936. cp[len] = 0;
  937. /* we do the redundancy check only if cache usage is enabled */
  938. if (strcmp(UCLIBC_RUNTIME_PREFIX "lib", cp) == 0 ||
  939. strcmp(UCLIBC_RUNTIME_PREFIX "usr/lib", cp) == 0) {
  940. if (verbose >= 0)
  941. warnx("Remove `%s' from `%s'\n", cp, LDSO_CONF);
  942. continue;
  943. }
  944. scan_dir(cp);
  945. }
  946. free(extpath);
  947. }
  948. #endif
  949. }
  950. if (!nocache)
  951. cache_write();
  952. }
  953. exit(EXIT_OK);
  954. }