2 * Copyright (c) 1993,1995 Paul Kranenburg
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Paul Kranenburg.
16 * 4. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * $FreeBSD: src/sbin/ldconfig/ldconfig.c,v 1.31.2.3 2001/07/11 23:59:10 obrien Exp $
31 * $DragonFly: src/sbin/ldconfig/ldconfig.c,v 1.3 2003/09/28 14:39:18 hmp Exp $
34 #include <sys/param.h>
35 #include <sys/types.h>
41 #include <elf-hints.h>
46 #include <objformat.h>
59 #define _PATH_LD_HINTS "./ld.so.hints"
60 #undef _PATH_ELF_HINTS
61 #define _PATH_ELF_HINTS "./ld-elf.so.hints"
72 static char *hints_file;
75 /* Internal list of shared libraries found */
80 #define major dewey[0]
81 #define minor dewey[1]
82 struct shlib_list *next;
85 static struct shlib_list *shlib_head = NULL, **shlib_tail = &shlib_head;
86 static char *dir_list;
88 static int buildhints __P((void));
89 static int dodir __P((char *, int));
90 int dofile __P((char *, int));
91 static void enter __P((char *, char *, char *, int *, int));
92 static void listhints __P((void));
93 static int readhints __P((void));
94 static void usage __P((void));
97 main(int argc, char **argv)
104 if (getobjformat(objformat, sizeof objformat, &argc, argv) == -1)
105 errx(1, "getobjformat failed: name too long");
106 if (strcmp(objformat, "aout") == 0)
108 else if (strcmp(objformat, "elf") == 0)
111 errx(1, "unknown object format \"%s\"", objformat);
113 hints_file = is_aout ? _PATH_LD_HINTS : _PATH_ELF_HINTS;
116 else while((c = getopt(argc, argv, "Rf:imrsv")) != -1) {
147 list_elf_hints(hints_file);
149 update_elf_hints(hints_file, argc - optind,
150 argv + optind, merge || rescan);
154 /* Here begins the aout libs processing */
155 dir_list = strdup("");
157 if (justread || merge || rescan) {
158 if ((rval = readhints()) != 0)
162 if (!nostd && !merge && !rescan)
165 /* Add any directories/files from the command line */
167 for (i = optind; i < argc; i++) {
170 if (stat(argv[i], &stbuf) == -1) {
173 } else if (strcmp(argv[i], "/usr/lib") == 0) {
174 warnx("WARNING! '%s' can not be used", argv[i]);
178 * See if this is a directory-containing
179 * file instead of a directory
181 if (S_ISREG(stbuf.st_mode))
182 rval |= dofile(argv[i], 0);
184 add_search_path(argv[i]);
189 for (i = 0; i < n_search_dirs; i++) {
190 char *cp = concat(dir_list, *dir_list?":":"", search_dirs[i]);
200 for (i = 0; i < n_search_dirs; i++)
201 rval |= dodir(search_dirs[i], 1);
203 rval |= buildhints();
212 "usage: ldconfig [-aout | -elf] [-Rimrsv] [-f hints_file] [dir | file ...]\n");
217 dofile(char *fname, int silent)
220 char buf[MAXPATHLEN];
224 if ((hfp = fopen(fname, "r")) == NULL) {
229 while (fgets(buf, sizeof(buf), hfp)) {
233 if (*cp == '#' || *cp == '\0')
236 while (!isspace(*cp) && *cp != '\0')
241 warnx("%s: trailing characters ignored", sp);
246 rval |= dodir(sp, silent);
254 dodir(char *dir, int silent)
258 char name[MAXPATHLEN];
259 int dewey[MAXDEWEY], ndewey;
261 if ((dd = opendir(dir)) == NULL) {
262 if (silent && errno == ENOENT) /* Ignore the error */
268 while ((dp = readdir(dd)) != NULL) {
272 /* Check for `lib' prefix */
273 if (dp->d_name[0] != 'l' ||
274 dp->d_name[1] != 'i' ||
275 dp->d_name[2] != 'b')
278 /* Copy the entry minus prefix */
279 (void)strcpy(name, dp->d_name + 3);
284 /* Find ".so." in name */
285 for (cp = name + n - 4; cp > name; --cp) {
296 if (!isdigit(*(cp+4)))
299 bzero((caddr_t)dewey, sizeof(dewey));
300 ndewey = getdewey(dewey, cp + 4);
303 enter(dir, dp->d_name, name, dewey, ndewey);
311 enter(char *dir, char *file, char *name, int *dewey, int ndewey)
313 struct shlib_list *shp;
315 for (shp = shlib_head; shp; shp = shp->next) {
316 if (strcmp(name, shp->name) != 0 || major != shp->major)
319 /* Name matches existing entry */
320 if (cmpndewey(dewey, ndewey, shp->dewey, shp->ndewey) > 0) {
322 /* Update this entry with higher versioned lib */
324 printf("Updating lib%s.%d.%d to %s/%s\n",
325 shp->name, shp->major, shp->minor,
329 shp->name = strdup(name);
331 shp->path = concat(dir, "/", file);
332 bcopy(dewey, shp->dewey, sizeof(shp->dewey));
333 shp->ndewey = ndewey;
339 /* Name exists: older version or just updated */
342 /* Allocate new list element */
344 printf("Adding %s/%s\n", dir, file);
346 shp = (struct shlib_list *)xmalloc(sizeof *shp);
347 shp->name = strdup(name);
348 shp->path = concat(dir, "/", file);
349 bcopy(dewey, shp->dewey, MAXDEWEY);
350 shp->ndewey = ndewey;
354 shlib_tail = &shp->next;
359 hinthash(char *cp, int vmajor)
364 k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
366 k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff;
374 struct hints_header hdr;
375 struct hints_bucket *blist;
376 struct shlib_list *shp;
378 int i, n, str_index = 0;
379 int strtab_sz = 0; /* Total length of strings */
380 int nhints = 0; /* Total number of hints */
384 for (shp = shlib_head; shp; shp = shp->next) {
385 strtab_sz += 1 + strlen(shp->name);
386 strtab_sz += 1 + strlen(shp->path);
390 /* Fill hints file header */
391 hdr.hh_magic = HH_MAGIC;
392 hdr.hh_version = LD_HINTS_VERSION_2;
393 hdr.hh_nbucket = 1 * nhints;
394 n = hdr.hh_nbucket * sizeof(struct hints_bucket);
395 hdr.hh_hashtab = sizeof(struct hints_header);
396 hdr.hh_strtab = hdr.hh_hashtab + n;
397 hdr.hh_dirlist = strtab_sz;
398 strtab_sz += 1 + strlen(dir_list);
399 hdr.hh_strtab_sz = strtab_sz;
400 hdr.hh_ehints = hdr.hh_strtab + hdr.hh_strtab_sz;
403 printf("Totals: entries %d, buckets %ld, string size %d\n",
404 nhints, (long)hdr.hh_nbucket, strtab_sz);
406 /* Allocate buckets and string table */
407 blist = (struct hints_bucket *)xmalloc(n);
408 bzero((char *)blist, n);
409 for (i = 0; i < hdr.hh_nbucket; i++)
410 /* Empty all buckets */
411 blist[i].hi_next = -1;
413 strtab = (char *)xmalloc(strtab_sz);
416 for (shp = shlib_head; shp; shp = shp->next) {
417 struct hints_bucket *bp;
420 (hinthash(shp->name, shp->major) % hdr.hh_nbucket);
425 for (i = 0; i < hdr.hh_nbucket; i++) {
426 if (blist[i].hi_pathx == 0)
429 if (i == hdr.hh_nbucket) {
433 while (bp->hi_next != -1)
434 bp = &blist[bp->hi_next];
439 /* Insert strings in string table */
440 bp->hi_namex = str_index;
441 strcpy(strtab + str_index, shp->name);
442 str_index += 1 + strlen(shp->name);
444 bp->hi_pathx = str_index;
445 strcpy(strtab + str_index, shp->path);
446 str_index += 1 + strlen(shp->path);
449 bcopy(shp->dewey, bp->hi_dewey, sizeof(bp->hi_dewey));
450 bp->hi_ndewey = shp->ndewey;
453 /* Copy search directories */
454 strcpy(strtab + str_index, dir_list);
455 str_index += 1 + strlen(dir_list);
458 if (str_index != strtab_sz) {
459 errx(1, "str_index(%d) != strtab_sz(%d)", str_index, strtab_sz);
462 tmpfile = concat(hints_file, ".XXXXXXXXXX", "");
463 umask(0); /* Create with exact permissions */
464 if ((fd = mkstemp(tmpfile)) == -1) {
470 if (write(fd, &hdr, sizeof(struct hints_header)) !=
471 sizeof(struct hints_header)) {
472 warn("%s", hints_file);
475 if (write(fd, blist, hdr.hh_nbucket * sizeof(struct hints_bucket)) !=
476 hdr.hh_nbucket * sizeof(struct hints_bucket)) {
477 warn("%s", hints_file);
480 if (write(fd, strtab, strtab_sz) != strtab_sz) {
481 warn("%s", hints_file);
484 if (close(fd) != 0) {
485 warn("%s", hints_file);
490 if (unlink(hints_file) != 0 && errno != ENOENT) {
491 warn("%s", hints_file);
495 if (rename(tmpfile, hints_file) != 0) {
496 warn("%s", hints_file);
510 struct hints_header *hdr;
511 struct hints_bucket *blist;
513 struct shlib_list *shp;
516 if ((fd = open(hints_file, O_RDONLY, 0)) == -1) {
517 warn("%s", hints_file);
522 addr = mmap(0, msize, PROT_READ, MAP_COPY, fd, 0);
524 if (addr == MAP_FAILED) {
525 warn("%s", hints_file);
529 hdr = (struct hints_header *)addr;
530 if (HH_BADMAG(*hdr)) {
531 warnx("%s: bad magic: %lo", hints_file,
532 (unsigned long)hdr->hh_magic);
536 if (hdr->hh_version != LD_HINTS_VERSION_1 &&
537 hdr->hh_version != LD_HINTS_VERSION_2) {
538 warnx("unsupported version: %ld", (long)hdr->hh_version);
542 if (hdr->hh_ehints > msize) {
543 fsize = hdr->hh_ehints;
545 addr = mmap(0, fsize, PROT_READ, MAP_COPY, fd, 0);
546 if (addr == MAP_FAILED) {
547 warn("%s", hints_file);
550 hdr = (struct hints_header *)addr;
554 blist = (struct hints_bucket *)(addr + hdr->hh_hashtab);
555 strtab = (char *)(addr + hdr->hh_strtab);
557 if (hdr->hh_version >= LD_HINTS_VERSION_2)
558 add_search_path(strtab + hdr->hh_dirlist);
560 errx(1, "%s too old and does not contain the search path",
566 for (i = 0; i < hdr->hh_nbucket; i++) {
567 struct hints_bucket *bp = &blist[i];
570 if (bp->hi_namex >= hdr->hh_strtab_sz) {
571 warnx("bad name index: %#x", bp->hi_namex);
574 if (bp->hi_pathx >= hdr->hh_strtab_sz) {
575 warnx("bad path index: %#x", bp->hi_pathx);
579 /* Allocate new list element */
580 shp = (struct shlib_list *)xmalloc(sizeof *shp);
581 shp->name = strdup(strtab + bp->hi_namex);
582 shp->path = strdup(strtab + bp->hi_pathx);
583 bcopy(bp->hi_dewey, shp->dewey, sizeof(shp->dewey));
584 shp->ndewey = bp->hi_ndewey;
588 shlib_tail = &shp->next;
597 struct shlib_list *shp;
600 printf("%s:\n", hints_file);
601 printf("\tsearch directories: %s\n", dir_list);
603 for (i = 0, shp = shlib_head; shp; i++, shp = shp->next)
604 printf("\t%d:-l%s.%d.%d => %s\n",
605 i, shp->name, shp->major, shp->minor, shp->path);