ldconfig.c 21 KB

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