ldconfig.c 21 KB

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