ldconfig.c 25 KB

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