ldconfig.c 21 KB

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