ldconfig.c 25 KB

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