ldconfig.c 25 KB

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