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 *___strtok = NULL;
  56. char *prog = NULL;
  57. int debug = 0; /* debug mode */
  58. int verbose = 0; /* verbose mode */
  59. int libmode = 0; /* library mode */
  60. int nolinks = 0; /* don't update links */
  61. int nocache = 0; /* don't build cache */
  62. void cache_print(void);
  63. void cache_write(void);
  64. void cache_dolib(const char *dir, const char *so, int libtype);
  65. #ifdef __LDSO_CACHE_SUPPORT__
  66. char *conffile = LDSO_CONF; /* default conf file */
  67. char *cachefile = LDSO_CACHE; /* default cache file */
  68. #endif
  69. char *chroot_dir = NULL;
  70. int byteswap = 0;
  71. struct needed_tab {
  72. char *soname;
  73. int type;
  74. };
  75. struct needed_tab needed_tab[] = {
  76. {"libc.so.0", LIB_ELF_LIBC0},
  77. {"libm.so.0", LIB_ELF_LIBC0},
  78. {"libdl.so.0", LIB_ELF_LIBC0},
  79. {"libc.so.5", LIB_ELF_LIBC5},
  80. {"libm.so.5", LIB_ELF_LIBC5},
  81. {"libdl.so.1", LIB_ELF_LIBC5},
  82. {"libc.so.6", LIB_ELF_LIBC6},
  83. {"libm.so.6", LIB_ELF_LIBC6},
  84. {"libdl.so.2", LIB_ELF_LIBC6},
  85. {NULL, LIB_ELF}
  86. };
  87. extern char *chroot_realpath(const char *chroot, const char *path,
  88. char resolved_path[]);
  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 void err(int errnum, const char *s, ...) attribute_noreturn;
  105. static void err(int errnum, const char *s, ...)
  106. {
  107. va_list p;
  108. va_start(p, s);
  109. verror_msg(s, p);
  110. va_end(p);
  111. fprintf(stderr, "\n");
  112. exit(errnum);
  113. }
  114. static void vperror_msg(const char *s, va_list p)
  115. {
  116. int e = errno;
  117. if (s == 0)
  118. s = "";
  119. verror_msg(s, p);
  120. if (*s)
  121. s = ": ";
  122. fprintf(stderr, "%s%s\n", s, strerror(e));
  123. }
  124. static void warn(const char *s, ...)
  125. {
  126. va_list p;
  127. va_start(p, s);
  128. vperror_msg(s, p);
  129. va_end(p);
  130. }
  131. static void *xmalloc(size_t size)
  132. {
  133. void *ptr;
  134. if ((ptr = malloc(size)) == NULL)
  135. err(EXIT_FAILURE, "out of memory");
  136. return ptr;
  137. }
  138. static char *xstrdup(const char *str)
  139. {
  140. char *ptr;
  141. if ((ptr = strdup(str)) == NULL)
  142. err(EXIT_FAILURE, "out of memory");
  143. return ptr;
  144. }
  145. #undef __ELF_NATIVE_CLASS
  146. #undef readsonameXX
  147. #define readsonameXX readsoname32
  148. #define __ELF_NATIVE_CLASS 32
  149. #include "readsoname2.c"
  150. #undef __ELF_NATIVE_CLASS
  151. #undef readsonameXX
  152. #define readsonameXX readsoname64
  153. #define __ELF_NATIVE_CLASS 64
  154. #include "readsoname2.c"
  155. static char *readsoname(char *name, FILE *infile, int expected_type,
  156. int *type, int elfclass)
  157. {
  158. char *res;
  159. if (elfclass == ELFCLASS32)
  160. res = readsoname32(name, infile, expected_type, type);
  161. else {
  162. res = readsoname64(name, infile, expected_type, type);
  163. #if 0
  164. /* relies on multilib support which we dont have ... */
  165. *type |= LIB_ELF64;
  166. #endif
  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((char *)elf_hdr->e_ident + 1, "ELF", 3) != 0)
  231. {
  232. /* silently ignore linker scripts */
  233. if (strncmp((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(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. 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) {
  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) + 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 void usage(void) attribute_noreturn;
  695. static void usage(void)
  696. {
  697. fprintf(stderr,
  698. #ifdef __LDSO_CACHE_SUPPORT__
  699. "ldconfig - updates symlinks and cache for shared libraries\n\n"
  700. "Usage: ldconfig [-DvqnNX] [-f conf] [-C cache] [-r root] dir ...\n"
  701. " ldconfig -l [-Dv] lib ...\n"
  702. " ldconfig -p\n\nOptions:\n"
  703. #else
  704. "ldconfig - updates symlinks for shared libraries\n\n"
  705. "Usage: ldconfig [-DvqnX] [-r root] dir ...\n"
  706. " ldconfig -l [-Dv] lib ...\n\nOptions:\n"
  707. #endif
  708. "\t-D:\t\tdebug mode, don't update links\n"
  709. "\t-v:\t\tverbose mode, print things as we go\n"
  710. "\t-q:\t\tquiet mode, don't print warnings\n"
  711. "\t-n:\t\tdon't process standard directories\n"
  712. "\t-N:\t\tdon't update the library cache\n"
  713. "\t-X:\t\tdon't update the library links\n"
  714. "\t-l:\t\tlibrary mode, manually link libraries\n"
  715. "\t-p:\t\tprint the current library cache\n"
  716. #ifdef __LDSO_CACHE_SUPPORT__
  717. "\t-f conf :\tuse conf instead of %s\n"
  718. "\t-C cache:\tuse cache instead of %s\n"
  719. #endif
  720. "\t-r root :\tfirst, do a chroot to the indicated directory\n"
  721. "\tdir ... :\tdirectories to process\n"
  722. #ifdef __LDSO_CACHE_SUPPORT__
  723. "\tlib ... :\tlibraries to link\n\n", LDSO_CONF, LDSO_CACHE
  724. #else
  725. "\tlib ... :\tlibraries to link\n\n"
  726. #endif
  727. );
  728. exit(EXIT_FAILURE);
  729. }
  730. #define DIR_SEP ":, \t\n"
  731. int main(int argc, char **argv)
  732. {
  733. int i, c;
  734. int nodefault = 0;
  735. char *cp, *dir, *so;
  736. int libtype, islink;
  737. int printcache = 0;
  738. #ifdef __LDSO_CACHE_SUPPORT__
  739. char *extpath;
  740. #endif
  741. prog = argv[0];
  742. opterr = 0;
  743. while ((c = getopt(argc, argv, "DvqnNXlpf:C:r:")) != EOF)
  744. switch (c) {
  745. case 'D':
  746. debug = 1; /* debug mode */
  747. nocache = 1;
  748. nolinks = 1;
  749. verbose = 1;
  750. break;
  751. case 'v':
  752. verbose = 1; /* verbose mode */
  753. break;
  754. case 'q':
  755. if (verbose <= 0)
  756. verbose = -1; /* quiet mode */
  757. break;
  758. case 'n':
  759. nodefault = 1; /* no default dirs */
  760. nocache = 1;
  761. break;
  762. case 'N':
  763. nocache = 1; /* don't build cache */
  764. break;
  765. case 'X':
  766. nolinks = 1; /* don't update links */
  767. break;
  768. case 'l':
  769. libmode = 1; /* library mode */
  770. break;
  771. case 'p':
  772. printcache = 1; /* print cache */
  773. break;
  774. case 'f':
  775. #ifdef __LDSO_CACHE_SUPPORT__
  776. conffile = optarg; /* alternate conf file */
  777. #endif
  778. break;
  779. case 'C':
  780. #ifdef __LDSO_CACHE_SUPPORT__
  781. cachefile = optarg; /* alternate cache file */
  782. #endif
  783. break;
  784. case 'r':
  785. chroot_dir = optarg;
  786. break;
  787. default:
  788. usage();
  789. break;
  790. /* THE REST OF THESE ARE UNDOCUMENTED AND MAY BE REMOVED
  791. IN FUTURE VERSIONS. */
  792. }
  793. if (chroot_dir && *chroot_dir) {
  794. if (chroot(chroot_dir) < 0) {
  795. if (chdir(chroot_dir) < 0)
  796. err(EXIT_FAILURE, "couldn't chroot to %s (%s)", chroot_dir, strerror(errno));
  797. chroot_dir = ".";
  798. } else {
  799. if (chdir("/") < 0)
  800. err(EXIT_FAILURE, "couldn't chdir to / (%s)", strerror(errno));
  801. chroot_dir = NULL;
  802. }
  803. }
  804. /* allow me to introduce myself, hi, my name is ... */
  805. if (verbose > 0)
  806. printf("%s: uClibc version\n", argv[0]);
  807. if (printcache) {
  808. /* print the cache -- don't you trust me? */
  809. cache_print();
  810. exit(EXIT_SUCCESS);
  811. } else if (libmode) {
  812. /* so you want to do things manually, eh? */
  813. /* ok, if you're so smart, which libraries do we link? */
  814. for (i = optind; i < argc; i++) {
  815. /* split into directory and file parts */
  816. if (!(cp = strrchr(argv[i], '/'))) {
  817. dir = "."; /* no dir, only a filename */
  818. cp = argv[i];
  819. } else {
  820. if (cp == argv[i])
  821. dir = "/"; /* file in root directory */
  822. else
  823. dir = argv[i];
  824. *cp++ = '\0'; /* neither of the above */
  825. }
  826. /* we'd better do a little bit of checking */
  827. if ((so = is_shlib(dir, cp, &libtype, &islink, LIB_ANY)) == NULL)
  828. err(EXIT_FAILURE, "%s%s%s is not a shared library",
  829. dir, (*dir && strcmp(dir, "/")) ? "/" : "", cp);
  830. /* so far, so good, maybe he knows what he's doing */
  831. link_shlib(dir, cp, so);
  832. }
  833. } else {
  834. /* the lazy bum want's us to do all the work for him */
  835. /* don't cache dirs on the command line */
  836. int nocache_save = nocache;
  837. nocache = 1;
  838. /* OK, which directories should we do? */
  839. for (i = optind; i < argc; i++)
  840. scan_dir(argv[i]);
  841. /* restore the desired caching state */
  842. nocache = nocache_save;
  843. /* look ma, no defaults */
  844. if (!nodefault) {
  845. scan_dir(UCLIBC_RUNTIME_PREFIX "lib");
  846. scan_dir(UCLIBC_RUNTIME_PREFIX "usr/lib");
  847. #ifndef __LDSO_CACHE_SUPPORT__
  848. scan_dir(UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib");
  849. #else
  850. /* I guess the defaults aren't good enough */
  851. if ((extpath = get_extpath())) {
  852. for (cp = strtok(extpath, DIR_SEP); cp; cp = strtok(NULL, DIR_SEP)) {
  853. /* strip trailing slashes */
  854. int len = strlen(cp);
  855. if (len)
  856. while (cp[--len] == '/' && len)
  857. cp[len] = 0;
  858. /* we do the redundancy check only if cache usage is enabled */
  859. if (strcmp(UCLIBC_RUNTIME_PREFIX "lib", cp) == 0
  860. || strcmp(UCLIBC_RUNTIME_PREFIX "usr/lib", cp) == 0) {
  861. if (verbose >= 0)
  862. warnx("You should remove `%s' from `%s'", cp, LDSO_CONF);
  863. continue;
  864. }
  865. scan_dir(cp);
  866. }
  867. free(extpath);
  868. }
  869. #endif
  870. }
  871. if (!nocache)
  872. cache_write();
  873. }
  874. exit(EXIT_SUCCESS);
  875. }