ldconfig.c 21 KB

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