ldconfig.c 25 KB

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