ldconfig.c 23 KB

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