1b9ae52e3SPaul Richards /* 20f6b2cb3SPaul Traina * Copyright (c) 1993,1995 Paul Kranenburg 3b9ae52e3SPaul Richards * All rights reserved. 4b9ae52e3SPaul Richards * 5b9ae52e3SPaul Richards * Redistribution and use in source and binary forms, with or without 6b9ae52e3SPaul Richards * modification, are permitted provided that the following conditions 7b9ae52e3SPaul Richards * are met: 8b9ae52e3SPaul Richards * 1. Redistributions of source code must retain the above copyright 9b9ae52e3SPaul Richards * notice, this list of conditions and the following disclaimer. 10b9ae52e3SPaul Richards * 2. Redistributions in binary form must reproduce the above copyright 11b9ae52e3SPaul Richards * notice, this list of conditions and the following disclaimer in the 12b9ae52e3SPaul Richards * documentation and/or other materials provided with the distribution. 13b9ae52e3SPaul Richards * 3. All advertising materials mentioning features or use of this software 14b9ae52e3SPaul Richards * must display the following acknowledgement: 15b9ae52e3SPaul Richards * This product includes software developed by Paul Kranenburg. 16b9ae52e3SPaul Richards * 4. The name of the author may not be used to endorse or promote products 1709e3d49dSJordan K. Hubbard * derived from this software without specific prior written permission 18b9ae52e3SPaul Richards * 19b9ae52e3SPaul Richards * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20b9ae52e3SPaul Richards * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21b9ae52e3SPaul Richards * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22b9ae52e3SPaul Richards * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23b9ae52e3SPaul Richards * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24b9ae52e3SPaul Richards * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25b9ae52e3SPaul Richards * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26b9ae52e3SPaul Richards * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27b9ae52e3SPaul Richards * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28b9ae52e3SPaul Richards * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29b9ae52e3SPaul Richards */ 30b9ae52e3SPaul Richards 31b50d7faeSPhilippe Charnier #ifndef lint 32b50d7faeSPhilippe Charnier static const char rcsid[] = 33b50d7faeSPhilippe Charnier "$Id$"; 34b50d7faeSPhilippe Charnier #endif /* not lint */ 35b50d7faeSPhilippe Charnier 36b9ae52e3SPaul Richards #include <sys/param.h> 37b9ae52e3SPaul Richards #include <sys/types.h> 38b9ae52e3SPaul Richards #include <sys/stat.h> 39b9ae52e3SPaul Richards #include <sys/mman.h> 40b9ae52e3SPaul Richards #include <a.out.h> 41b50d7faeSPhilippe Charnier #include <ctype.h> 42b50d7faeSPhilippe Charnier #include <dirent.h> 43b50d7faeSPhilippe Charnier #include <err.h> 44b50d7faeSPhilippe Charnier #include <errno.h> 45b50d7faeSPhilippe Charnier #include <fcntl.h> 46b50d7faeSPhilippe Charnier #include <link.h> 47699e1b82SRich Murphey #include <stdio.h> 48699e1b82SRich Murphey #include <stdlib.h> 49b9ae52e3SPaul Richards #include <string.h> 50699e1b82SRich Murphey #include <unistd.h> 51b9ae52e3SPaul Richards 5280c71499SPeter Wemm #include "shlib.h" 5380c71499SPeter Wemm #include "support.h" 5480c71499SPeter Wemm 557c6da7dcSJohn Polstra #if DEBUG 567c6da7dcSJohn Polstra /* test */ 577c6da7dcSJohn Polstra #undef _PATH_LD_HINTS 587c6da7dcSJohn Polstra #define _PATH_LD_HINTS "./ld.so.hints" 597c6da7dcSJohn Polstra #endif 60b9ae52e3SPaul Richards 61b9ae52e3SPaul Richards #undef major 62b9ae52e3SPaul Richards #undef minor 63b9ae52e3SPaul Richards 64b9ae52e3SPaul Richards static int verbose; 65b9ae52e3SPaul Richards static int nostd; 66b9ae52e3SPaul Richards static int justread; 67f606c848SSatoshi Asami static int merge; 68d4ba5766SPeter Wemm static int rescan; 697c6da7dcSJohn Polstra static char *hints_file = _PATH_LD_HINTS; 70b9ae52e3SPaul Richards 71b9ae52e3SPaul Richards struct shlib_list { 72b9ae52e3SPaul Richards /* Internal list of shared libraries found */ 73b9ae52e3SPaul Richards char *name; 74b9ae52e3SPaul Richards char *path; 75b9ae52e3SPaul Richards int dewey[MAXDEWEY]; 76b9ae52e3SPaul Richards int ndewey; 77b9ae52e3SPaul Richards #define major dewey[0] 78b9ae52e3SPaul Richards #define minor dewey[1] 79b9ae52e3SPaul Richards struct shlib_list *next; 80b9ae52e3SPaul Richards }; 81b9ae52e3SPaul Richards 82b9ae52e3SPaul Richards static struct shlib_list *shlib_head = NULL, **shlib_tail = &shlib_head; 8380c71499SPeter Wemm static char *dir_list; 84b9ae52e3SPaul Richards 85b9ae52e3SPaul Richards static void enter __P((char *, char *, char *, int *, int)); 8609e3d49dSJordan K. Hubbard static int dodir __P((char *, int)); 87b50d7faeSPhilippe Charnier int dofile __P((char *, int)); 88f606c848SSatoshi Asami static int buildhints __P((void)); 89f606c848SSatoshi Asami static int readhints __P((void)); 90f606c848SSatoshi Asami static void listhints __P((void)); 91b50d7faeSPhilippe Charnier static void usage __P((void)); 92b9ae52e3SPaul Richards 93b9ae52e3SPaul Richards int 94b9ae52e3SPaul Richards main(argc, argv) 95b9ae52e3SPaul Richards int argc; 96b9ae52e3SPaul Richards char *argv[]; 97b9ae52e3SPaul Richards { 98b9ae52e3SPaul Richards int i, c; 99b9ae52e3SPaul Richards int rval = 0; 100b9ae52e3SPaul Richards 101b50d7faeSPhilippe Charnier while ((c = getopt(argc, argv, "Rf:mrsv")) != -1) { 102b9ae52e3SPaul Richards switch (c) { 103d4ba5766SPeter Wemm case 'R': 104d4ba5766SPeter Wemm rescan = 1; 105d4ba5766SPeter Wemm break; 1067c6da7dcSJohn Polstra case 'f': 1077c6da7dcSJohn Polstra hints_file = optarg; 1087c6da7dcSJohn Polstra break; 109f606c848SSatoshi Asami case 'm': 110f606c848SSatoshi Asami merge = 1; 111b9ae52e3SPaul Richards break; 112b9ae52e3SPaul Richards case 'r': 113b9ae52e3SPaul Richards justread = 1; 114b9ae52e3SPaul Richards break; 115f606c848SSatoshi Asami case 's': 116f606c848SSatoshi Asami nostd = 1; 117f606c848SSatoshi Asami break; 118f606c848SSatoshi Asami case 'v': 119f606c848SSatoshi Asami verbose = 1; 120f606c848SSatoshi Asami break; 121b9ae52e3SPaul Richards default: 122b50d7faeSPhilippe Charnier usage(); 123b9ae52e3SPaul Richards break; 124b9ae52e3SPaul Richards } 125b9ae52e3SPaul Richards } 126b9ae52e3SPaul Richards 12780c71499SPeter Wemm dir_list = strdup(""); 12880c71499SPeter Wemm 129d4ba5766SPeter Wemm if (justread || merge || rescan) { 130f606c848SSatoshi Asami if ((rval = readhints()) != 0) 131f606c848SSatoshi Asami return rval; 13280c71499SPeter Wemm } 13380c71499SPeter Wemm 134d4ba5766SPeter Wemm if (!nostd && !merge && !rescan) 13580c71499SPeter Wemm std_search_path(); 13680c71499SPeter Wemm 137571b472bSJordan K. Hubbard /* Add any directories/files from the command line */ 138571b472bSJordan K. Hubbard if (!justread) { 139d66f9d22SJohn Polstra for (i = optind; i < argc; i++) { 140571b472bSJordan K. Hubbard struct stat stbuf; 141571b472bSJordan K. Hubbard 142571b472bSJordan K. Hubbard if (stat(argv[i], &stbuf) == -1) { 143d66f9d22SJohn Polstra warn("%s", argv[i]); 144d66f9d22SJohn Polstra rval = -1; 145cabb97dcSSøren Schmidt } 146cabb97dcSSøren Schmidt else if (!strcmp(argv[i], "/usr/lib")) { 147cabb97dcSSøren Schmidt warnx("WARNING! '%s' can not be used", argv[i]); 148cabb97dcSSøren Schmidt rval = -1; 149cabb97dcSSøren Schmidt } 150cabb97dcSSøren Schmidt else { 151571b472bSJordan K. Hubbard /* 152571b472bSJordan K. Hubbard * See if this is a directory-containing 153571b472bSJordan K. Hubbard * file instead of a directory 154571b472bSJordan K. Hubbard */ 155571b472bSJordan K. Hubbard if (S_ISREG(stbuf.st_mode)) 156571b472bSJordan K. Hubbard rval |= dofile(argv[i], 0); 157571b472bSJordan K. Hubbard else 15880c71499SPeter Wemm add_search_path(argv[i]); 159d66f9d22SJohn Polstra } 160d66f9d22SJohn Polstra } 161571b472bSJordan K. Hubbard } 16280c71499SPeter Wemm 16380c71499SPeter Wemm for (i = 0; i < n_search_dirs; i++) { 16480c71499SPeter Wemm char *cp = concat(dir_list, *dir_list?":":"", search_dirs[i]); 16580c71499SPeter Wemm free(dir_list); 16680c71499SPeter Wemm dir_list = cp; 16780c71499SPeter Wemm } 16880c71499SPeter Wemm 169f606c848SSatoshi Asami if (justread) { 170f606c848SSatoshi Asami listhints(); 171526195adSJordan K. Hubbard return 0; 172f606c848SSatoshi Asami } 173b9ae52e3SPaul Richards 174b9ae52e3SPaul Richards for (i = 0; i < n_search_dirs; i++) 17509e3d49dSJordan K. Hubbard rval |= dodir(search_dirs[i], 1); 176b9ae52e3SPaul Richards 177f606c848SSatoshi Asami rval |= buildhints(); 178b9ae52e3SPaul Richards 179b9ae52e3SPaul Richards return rval; 180b9ae52e3SPaul Richards } 181b9ae52e3SPaul Richards 182b50d7faeSPhilippe Charnier static void 183b50d7faeSPhilippe Charnier usage() 184b50d7faeSPhilippe Charnier { 185b50d7faeSPhilippe Charnier fprintf(stderr, 186b50d7faeSPhilippe Charnier "usage: ldconfig [-Rmrsv] [-f hints_file] [dir | file ...]\n"); 187b50d7faeSPhilippe Charnier exit(1); 188b50d7faeSPhilippe Charnier } 189b50d7faeSPhilippe Charnier 190b9ae52e3SPaul Richards int 191571b472bSJordan K. Hubbard dofile(fname, silent) 192571b472bSJordan K. Hubbard char *fname; 193571b472bSJordan K. Hubbard int silent; 194571b472bSJordan K. Hubbard { 195571b472bSJordan K. Hubbard FILE *hfp; 196571b472bSJordan K. Hubbard char buf[MAXPATHLEN]; 197571b472bSJordan K. Hubbard int rval = 0; 198571b472bSJordan K. Hubbard char *cp, *sp; 199571b472bSJordan K. Hubbard 200571b472bSJordan K. Hubbard if ((hfp = fopen(fname, "r")) == NULL) { 201571b472bSJordan K. Hubbard warn("%s", fname); 202571b472bSJordan K. Hubbard return -1; 203571b472bSJordan K. Hubbard } 204571b472bSJordan K. Hubbard 205571b472bSJordan K. Hubbard while (fgets(buf, sizeof(buf), hfp)) { 206571b472bSJordan K. Hubbard cp = buf; 207571b472bSJordan K. Hubbard while (isspace(*cp)) 208571b472bSJordan K. Hubbard cp++; 209571b472bSJordan K. Hubbard if (*cp == '#' || *cp == '\0') 210571b472bSJordan K. Hubbard continue; 211571b472bSJordan K. Hubbard sp = cp; 212571b472bSJordan K. Hubbard while (!isspace(*cp) && *cp != '\0') 213571b472bSJordan K. Hubbard cp++; 214571b472bSJordan K. Hubbard 215571b472bSJordan K. Hubbard if (*cp != '\n') { 216571b472bSJordan K. Hubbard *cp = '\0'; 217b50d7faeSPhilippe Charnier warnx("%s: trailing characters ignored", sp); 218571b472bSJordan K. Hubbard } 219571b472bSJordan K. Hubbard 220571b472bSJordan K. Hubbard *cp = '\0'; 221571b472bSJordan K. Hubbard 222571b472bSJordan K. Hubbard rval |= dodir(sp, silent); 223571b472bSJordan K. Hubbard } 224571b472bSJordan K. Hubbard 225571b472bSJordan K. Hubbard (void)fclose(hfp); 226571b472bSJordan K. Hubbard return rval; 227571b472bSJordan K. Hubbard } 228571b472bSJordan K. Hubbard 229571b472bSJordan K. Hubbard int 23009e3d49dSJordan K. Hubbard dodir(dir, silent) 231b9ae52e3SPaul Richards char *dir; 23209e3d49dSJordan K. Hubbard int silent; 233b9ae52e3SPaul Richards { 234b9ae52e3SPaul Richards DIR *dd; 235b9ae52e3SPaul Richards struct dirent *dp; 2360f6b2cb3SPaul Traina char name[MAXPATHLEN]; 237b9ae52e3SPaul Richards int dewey[MAXDEWEY], ndewey; 238b9ae52e3SPaul Richards 239b9ae52e3SPaul Richards if ((dd = opendir(dir)) == NULL) { 240d66f9d22SJohn Polstra if (silent && errno == ENOENT) /* Ignore the error */ 241d66f9d22SJohn Polstra return 0; 242f606c848SSatoshi Asami warn("%s", dir); 243b9ae52e3SPaul Richards return -1; 244b9ae52e3SPaul Richards } 245b9ae52e3SPaul Richards 246b9ae52e3SPaul Richards while ((dp = readdir(dd)) != NULL) { 2470f6b2cb3SPaul Traina register int n; 2480f6b2cb3SPaul Traina register char *cp; 249b9ae52e3SPaul Richards 2500f6b2cb3SPaul Traina /* Check for `lib' prefix */ 2510f6b2cb3SPaul Traina if (dp->d_name[0] != 'l' || 2520f6b2cb3SPaul Traina dp->d_name[1] != 'i' || 2530f6b2cb3SPaul Traina dp->d_name[2] != 'b') 254b9ae52e3SPaul Richards continue; 255b9ae52e3SPaul Richards 2560f6b2cb3SPaul Traina /* Copy the entry minus prefix */ 2570f6b2cb3SPaul Traina (void)strcpy(name, dp->d_name + 3); 2580f6b2cb3SPaul Traina n = strlen(name); 2590f6b2cb3SPaul Traina if (n < 4) 2600f6b2cb3SPaul Traina continue; 2610f6b2cb3SPaul Traina 2620f6b2cb3SPaul Traina /* Find ".so." in name */ 2630f6b2cb3SPaul Traina for (cp = name + n - 4; cp > name; --cp) { 2640f6b2cb3SPaul Traina if (cp[0] == '.' && 2650f6b2cb3SPaul Traina cp[1] == 's' && 2660f6b2cb3SPaul Traina cp[2] == 'o' && 2670f6b2cb3SPaul Traina cp[3] == '.') 2680f6b2cb3SPaul Traina break; 2690f6b2cb3SPaul Traina } 2700f6b2cb3SPaul Traina if (cp <= name) 2710f6b2cb3SPaul Traina continue; 2720f6b2cb3SPaul Traina 2730f6b2cb3SPaul Traina *cp = '\0'; 2740f6b2cb3SPaul Traina if (!isdigit(*(cp+4))) 2750f6b2cb3SPaul Traina continue; 2760f6b2cb3SPaul Traina 2770f6b2cb3SPaul Traina bzero((caddr_t)dewey, sizeof(dewey)); 2780f6b2cb3SPaul Traina ndewey = getdewey(dewey, cp + 4); 279b9ae52e3SPaul Richards enter(dir, dp->d_name, name, dewey, ndewey); 280b9ae52e3SPaul Richards } 281b9ae52e3SPaul Richards 282571b472bSJordan K. Hubbard closedir(dd); 283b9ae52e3SPaul Richards return 0; 284b9ae52e3SPaul Richards } 285b9ae52e3SPaul Richards 286b9ae52e3SPaul Richards static void 287b9ae52e3SPaul Richards enter(dir, file, name, dewey, ndewey) 288b9ae52e3SPaul Richards char *dir, *file, *name; 289b9ae52e3SPaul Richards int dewey[], ndewey; 290b9ae52e3SPaul Richards { 291b9ae52e3SPaul Richards struct shlib_list *shp; 292b9ae52e3SPaul Richards 293b9ae52e3SPaul Richards for (shp = shlib_head; shp; shp = shp->next) { 294b9ae52e3SPaul Richards if (strcmp(name, shp->name) != 0 || major != shp->major) 295b9ae52e3SPaul Richards continue; 296b9ae52e3SPaul Richards 297b9ae52e3SPaul Richards /* Name matches existing entry */ 298b9ae52e3SPaul Richards if (cmpndewey(dewey, ndewey, shp->dewey, shp->ndewey) > 0) { 299b9ae52e3SPaul Richards 300b9ae52e3SPaul Richards /* Update this entry with higher versioned lib */ 301b9ae52e3SPaul Richards if (verbose) 302b9ae52e3SPaul Richards printf("Updating lib%s.%d.%d to %s/%s\n", 303b9ae52e3SPaul Richards shp->name, shp->major, shp->minor, 304b9ae52e3SPaul Richards dir, file); 305b9ae52e3SPaul Richards 306b9ae52e3SPaul Richards free(shp->name); 307b9ae52e3SPaul Richards shp->name = strdup(name); 308b9ae52e3SPaul Richards free(shp->path); 309b9ae52e3SPaul Richards shp->path = concat(dir, "/", file); 310b9ae52e3SPaul Richards bcopy(dewey, shp->dewey, sizeof(shp->dewey)); 311b9ae52e3SPaul Richards shp->ndewey = ndewey; 312b9ae52e3SPaul Richards } 313b9ae52e3SPaul Richards break; 314b9ae52e3SPaul Richards } 315b9ae52e3SPaul Richards 316b9ae52e3SPaul Richards if (shp) 317b9ae52e3SPaul Richards /* Name exists: older version or just updated */ 318b9ae52e3SPaul Richards return; 319b9ae52e3SPaul Richards 320b9ae52e3SPaul Richards /* Allocate new list element */ 321b9ae52e3SPaul Richards if (verbose) 322b9ae52e3SPaul Richards printf("Adding %s/%s\n", dir, file); 323b9ae52e3SPaul Richards 324b9ae52e3SPaul Richards shp = (struct shlib_list *)xmalloc(sizeof *shp); 325b9ae52e3SPaul Richards shp->name = strdup(name); 326b9ae52e3SPaul Richards shp->path = concat(dir, "/", file); 327b9ae52e3SPaul Richards bcopy(dewey, shp->dewey, MAXDEWEY); 328b9ae52e3SPaul Richards shp->ndewey = ndewey; 329b9ae52e3SPaul Richards shp->next = NULL; 330b9ae52e3SPaul Richards 331b9ae52e3SPaul Richards *shlib_tail = shp; 332b9ae52e3SPaul Richards shlib_tail = &shp->next; 333b9ae52e3SPaul Richards } 334b9ae52e3SPaul Richards 335b9ae52e3SPaul Richards 336b9ae52e3SPaul Richards int 337d5453ba5SJoerg Wunsch hinthash(cp, vmajor) 338b9ae52e3SPaul Richards char *cp; 339d5453ba5SJoerg Wunsch int vmajor; 340b9ae52e3SPaul Richards { 341b9ae52e3SPaul Richards int k = 0; 342b9ae52e3SPaul Richards 343b9ae52e3SPaul Richards while (*cp) 344b9ae52e3SPaul Richards k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff; 345b9ae52e3SPaul Richards 346b9ae52e3SPaul Richards k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff; 347b9ae52e3SPaul Richards 348b9ae52e3SPaul Richards return k; 349b9ae52e3SPaul Richards } 350b9ae52e3SPaul Richards 351b9ae52e3SPaul Richards int 352f606c848SSatoshi Asami buildhints() 353b9ae52e3SPaul Richards { 354b9ae52e3SPaul Richards struct hints_header hdr; 355b9ae52e3SPaul Richards struct hints_bucket *blist; 356b9ae52e3SPaul Richards struct shlib_list *shp; 357b9ae52e3SPaul Richards char *strtab; 358b9ae52e3SPaul Richards int i, n, str_index = 0; 359b9ae52e3SPaul Richards int strtab_sz = 0; /* Total length of strings */ 360b9ae52e3SPaul Richards int nhints = 0; /* Total number of hints */ 361b9ae52e3SPaul Richards int fd; 362b9ae52e3SPaul Richards char *tmpfile; 363b9ae52e3SPaul Richards 364b9ae52e3SPaul Richards for (shp = shlib_head; shp; shp = shp->next) { 365b9ae52e3SPaul Richards strtab_sz += 1 + strlen(shp->name); 366b9ae52e3SPaul Richards strtab_sz += 1 + strlen(shp->path); 367b9ae52e3SPaul Richards nhints++; 368b9ae52e3SPaul Richards } 369b9ae52e3SPaul Richards 370b9ae52e3SPaul Richards /* Fill hints file header */ 371b9ae52e3SPaul Richards hdr.hh_magic = HH_MAGIC; 37280c71499SPeter Wemm hdr.hh_version = LD_HINTS_VERSION_2; 373b9ae52e3SPaul Richards hdr.hh_nbucket = 1 * nhints; 374b9ae52e3SPaul Richards n = hdr.hh_nbucket * sizeof(struct hints_bucket); 375b9ae52e3SPaul Richards hdr.hh_hashtab = sizeof(struct hints_header); 376b9ae52e3SPaul Richards hdr.hh_strtab = hdr.hh_hashtab + n; 37780c71499SPeter Wemm hdr.hh_dirlist = strtab_sz; 37880c71499SPeter Wemm strtab_sz += 1 + strlen(dir_list); 379b9ae52e3SPaul Richards hdr.hh_strtab_sz = strtab_sz; 380b9ae52e3SPaul Richards hdr.hh_ehints = hdr.hh_strtab + hdr.hh_strtab_sz; 381b9ae52e3SPaul Richards 382b9ae52e3SPaul Richards if (verbose) 383526195adSJordan K. Hubbard printf("Totals: entries %d, buckets %ld, string size %d\n", 384b9ae52e3SPaul Richards nhints, hdr.hh_nbucket, strtab_sz); 385b9ae52e3SPaul Richards 386b9ae52e3SPaul Richards /* Allocate buckets and string table */ 387b9ae52e3SPaul Richards blist = (struct hints_bucket *)xmalloc(n); 388b9ae52e3SPaul Richards bzero((char *)blist, n); 389b9ae52e3SPaul Richards for (i = 0; i < hdr.hh_nbucket; i++) 390b9ae52e3SPaul Richards /* Empty all buckets */ 391b9ae52e3SPaul Richards blist[i].hi_next = -1; 392b9ae52e3SPaul Richards 393b9ae52e3SPaul Richards strtab = (char *)xmalloc(strtab_sz); 394b9ae52e3SPaul Richards 395b9ae52e3SPaul Richards /* Enter all */ 396b9ae52e3SPaul Richards for (shp = shlib_head; shp; shp = shp->next) { 397b9ae52e3SPaul Richards struct hints_bucket *bp; 398b9ae52e3SPaul Richards 399b9ae52e3SPaul Richards bp = blist + 400d5453ba5SJoerg Wunsch (hinthash(shp->name, shp->major) % hdr.hh_nbucket); 401b9ae52e3SPaul Richards 402b9ae52e3SPaul Richards if (bp->hi_pathx) { 403b9ae52e3SPaul Richards int i; 404b9ae52e3SPaul Richards 405b9ae52e3SPaul Richards for (i = 0; i < hdr.hh_nbucket; i++) { 406b9ae52e3SPaul Richards if (blist[i].hi_pathx == 0) 407b9ae52e3SPaul Richards break; 408b9ae52e3SPaul Richards } 409b9ae52e3SPaul Richards if (i == hdr.hh_nbucket) { 410b50d7faeSPhilippe Charnier warnx("bummer!"); 411b9ae52e3SPaul Richards return -1; 412b9ae52e3SPaul Richards } 413b9ae52e3SPaul Richards while (bp->hi_next != -1) 414b9ae52e3SPaul Richards bp = &blist[bp->hi_next]; 415b9ae52e3SPaul Richards bp->hi_next = i; 416b9ae52e3SPaul Richards bp = blist + i; 417b9ae52e3SPaul Richards } 418b9ae52e3SPaul Richards 419b9ae52e3SPaul Richards /* Insert strings in string table */ 420b9ae52e3SPaul Richards bp->hi_namex = str_index; 421b9ae52e3SPaul Richards strcpy(strtab + str_index, shp->name); 422b9ae52e3SPaul Richards str_index += 1 + strlen(shp->name); 423b9ae52e3SPaul Richards 424b9ae52e3SPaul Richards bp->hi_pathx = str_index; 425b9ae52e3SPaul Richards strcpy(strtab + str_index, shp->path); 426b9ae52e3SPaul Richards str_index += 1 + strlen(shp->path); 427b9ae52e3SPaul Richards 428b9ae52e3SPaul Richards /* Copy versions */ 429b9ae52e3SPaul Richards bcopy(shp->dewey, bp->hi_dewey, sizeof(bp->hi_dewey)); 430b9ae52e3SPaul Richards bp->hi_ndewey = shp->ndewey; 431b9ae52e3SPaul Richards } 432b9ae52e3SPaul Richards 43380c71499SPeter Wemm /* Copy search directories */ 43480c71499SPeter Wemm strcpy(strtab + str_index, dir_list); 43580c71499SPeter Wemm str_index += 1 + strlen(dir_list); 43680c71499SPeter Wemm 43780c71499SPeter Wemm /* Sanity check */ 43880c71499SPeter Wemm if (str_index != strtab_sz) { 43980c71499SPeter Wemm errx(1, "str_index(%d) != strtab_sz(%d)", str_index, strtab_sz); 44080c71499SPeter Wemm } 44180c71499SPeter Wemm 4427c6da7dcSJohn Polstra tmpfile = concat(hints_file, ".XXXXXX", ""); 44380c71499SPeter Wemm if ((tmpfile = mktemp(tmpfile)) == NULL) { 44480c71499SPeter Wemm warn("%s", tmpfile); 44580c71499SPeter Wemm return -1; 44680c71499SPeter Wemm } 44780c71499SPeter Wemm 44880c71499SPeter Wemm umask(0); /* Create with exact permissions */ 449b9ae52e3SPaul Richards if ((fd = open(tmpfile, O_RDWR|O_CREAT|O_TRUNC, 0444)) == -1) { 4507c6da7dcSJohn Polstra warn("%s", hints_file); 451b9ae52e3SPaul Richards return -1; 452b9ae52e3SPaul Richards } 453b9ae52e3SPaul Richards 45409e3d49dSJordan K. Hubbard if (write(fd, &hdr, sizeof(struct hints_header)) != 45509e3d49dSJordan K. Hubbard sizeof(struct hints_header)) { 4567c6da7dcSJohn Polstra warn("%s", hints_file); 45709e3d49dSJordan K. Hubbard return -1; 45809e3d49dSJordan K. Hubbard } 45909e3d49dSJordan K. Hubbard if (write(fd, blist, hdr.hh_nbucket * sizeof(struct hints_bucket)) != 46009e3d49dSJordan K. Hubbard hdr.hh_nbucket * sizeof(struct hints_bucket)) { 4617c6da7dcSJohn Polstra warn("%s", hints_file); 46209e3d49dSJordan K. Hubbard return -1; 46309e3d49dSJordan K. Hubbard } 46409e3d49dSJordan K. Hubbard if (write(fd, strtab, strtab_sz) != strtab_sz) { 4657c6da7dcSJohn Polstra warn("%s", hints_file); 46609e3d49dSJordan K. Hubbard return -1; 46709e3d49dSJordan K. Hubbard } 468b9ae52e3SPaul Richards if (close(fd) != 0) { 4697c6da7dcSJohn Polstra warn("%s", hints_file); 470b9ae52e3SPaul Richards return -1; 471b9ae52e3SPaul Richards } 472b9ae52e3SPaul Richards 47309e3d49dSJordan K. Hubbard /* Install it */ 4747c6da7dcSJohn Polstra if (unlink(hints_file) != 0 && errno != ENOENT) { 4757c6da7dcSJohn Polstra warn("%s", hints_file); 476b9ae52e3SPaul Richards return -1; 477b9ae52e3SPaul Richards } 478b9ae52e3SPaul Richards 4797c6da7dcSJohn Polstra if (rename(tmpfile, hints_file) != 0) { 4807c6da7dcSJohn Polstra warn("%s", hints_file); 481b9ae52e3SPaul Richards return -1; 482b9ae52e3SPaul Richards } 483b9ae52e3SPaul Richards 484b9ae52e3SPaul Richards return 0; 485b9ae52e3SPaul Richards } 486b9ae52e3SPaul Richards 487699e1b82SRich Murphey static int 488f606c848SSatoshi Asami readhints() 489b9ae52e3SPaul Richards { 490b9ae52e3SPaul Richards int fd; 491b9ae52e3SPaul Richards caddr_t addr; 492b9ae52e3SPaul Richards long msize; 493b9ae52e3SPaul Richards struct hints_header *hdr; 494b9ae52e3SPaul Richards struct hints_bucket *blist; 495b9ae52e3SPaul Richards char *strtab; 496f606c848SSatoshi Asami struct shlib_list *shp; 497b9ae52e3SPaul Richards int i; 498b9ae52e3SPaul Richards 4997c6da7dcSJohn Polstra if ((fd = open(hints_file, O_RDONLY, 0)) == -1) { 5007c6da7dcSJohn Polstra warn("%s", hints_file); 501b9ae52e3SPaul Richards return -1; 502b9ae52e3SPaul Richards } 503b9ae52e3SPaul Richards 50480c71499SPeter Wemm msize = PAGE_SIZE; 50561f9ce8dSNate Williams addr = mmap(0, msize, PROT_READ, MAP_COPY, fd, 0); 506b9ae52e3SPaul Richards 507b9ae52e3SPaul Richards if (addr == (caddr_t)-1) { 5087c6da7dcSJohn Polstra warn("%s", hints_file); 509b9ae52e3SPaul Richards return -1; 510b9ae52e3SPaul Richards } 511b9ae52e3SPaul Richards 512b9ae52e3SPaul Richards hdr = (struct hints_header *)addr; 513b9ae52e3SPaul Richards if (HH_BADMAG(*hdr)) { 514b50d7faeSPhilippe Charnier warnx("%s: bad magic: %lo", hints_file, hdr->hh_magic); 515b9ae52e3SPaul Richards return -1; 516b9ae52e3SPaul Richards } 517b9ae52e3SPaul Richards 51880c71499SPeter Wemm if (hdr->hh_version != LD_HINTS_VERSION_1 && 51980c71499SPeter Wemm hdr->hh_version != LD_HINTS_VERSION_2) { 520b50d7faeSPhilippe Charnier warnx("unsupported version: %ld", hdr->hh_version); 521b9ae52e3SPaul Richards return -1; 522b9ae52e3SPaul Richards } 523b9ae52e3SPaul Richards 524b9ae52e3SPaul Richards if (hdr->hh_ehints > msize) { 525b9ae52e3SPaul Richards if (mmap(addr+msize, hdr->hh_ehints - msize, 52661f9ce8dSNate Williams PROT_READ, MAP_COPY|MAP_FIXED, 527b9ae52e3SPaul Richards fd, msize) != (caddr_t)(addr+msize)) { 528b9ae52e3SPaul Richards 5297c6da7dcSJohn Polstra warn("%s", hints_file); 530b9ae52e3SPaul Richards return -1; 531b9ae52e3SPaul Richards } 532b9ae52e3SPaul Richards } 533b9ae52e3SPaul Richards close(fd); 534b9ae52e3SPaul Richards 535b9ae52e3SPaul Richards blist = (struct hints_bucket *)(addr + hdr->hh_hashtab); 536b9ae52e3SPaul Richards strtab = (char *)(addr + hdr->hh_strtab); 537b9ae52e3SPaul Richards 538d4ba5766SPeter Wemm if (hdr->hh_version >= LD_HINTS_VERSION_2) 539d4ba5766SPeter Wemm add_search_path(strtab + hdr->hh_dirlist); 540d4ba5766SPeter Wemm else if (rescan) 541d4ba5766SPeter Wemm errx(1, "%s too old and does not contain the search path", 542d4ba5766SPeter Wemm hints_file); 543d4ba5766SPeter Wemm 544d4ba5766SPeter Wemm if (rescan) 545d4ba5766SPeter Wemm return 0; 546d4ba5766SPeter Wemm 547b9ae52e3SPaul Richards for (i = 0; i < hdr->hh_nbucket; i++) { 548b9ae52e3SPaul Richards struct hints_bucket *bp = &blist[i]; 549b9ae52e3SPaul Richards 550b9ae52e3SPaul Richards /* Sanity check */ 551b9ae52e3SPaul Richards if (bp->hi_namex >= hdr->hh_strtab_sz) { 552b50d7faeSPhilippe Charnier warnx("bad name index: %#x", bp->hi_namex); 553b9ae52e3SPaul Richards return -1; 554b9ae52e3SPaul Richards } 555b9ae52e3SPaul Richards if (bp->hi_pathx >= hdr->hh_strtab_sz) { 556b50d7faeSPhilippe Charnier warnx("bad path index: %#x", bp->hi_pathx); 557b9ae52e3SPaul Richards return -1; 558b9ae52e3SPaul Richards } 559b9ae52e3SPaul Richards 560f606c848SSatoshi Asami /* Allocate new list element */ 561f606c848SSatoshi Asami shp = (struct shlib_list *)xmalloc(sizeof *shp); 562f606c848SSatoshi Asami shp->name = strdup(strtab + bp->hi_namex); 563f606c848SSatoshi Asami shp->path = strdup(strtab + bp->hi_pathx); 564f606c848SSatoshi Asami bcopy(bp->hi_dewey, shp->dewey, sizeof(shp->dewey)); 565f606c848SSatoshi Asami shp->ndewey = bp->hi_ndewey; 566f606c848SSatoshi Asami shp->next = NULL; 567f606c848SSatoshi Asami 568f606c848SSatoshi Asami *shlib_tail = shp; 569f606c848SSatoshi Asami shlib_tail = &shp->next; 570b9ae52e3SPaul Richards } 571b9ae52e3SPaul Richards 572b9ae52e3SPaul Richards return 0; 573b9ae52e3SPaul Richards } 574b9ae52e3SPaul Richards 575f606c848SSatoshi Asami static void 576f606c848SSatoshi Asami listhints() 577f606c848SSatoshi Asami { 578f606c848SSatoshi Asami struct shlib_list *shp; 579f606c848SSatoshi Asami int i; 580f606c848SSatoshi Asami 5817c6da7dcSJohn Polstra printf("%s:\n", hints_file); 58280c71499SPeter Wemm printf("\tsearch directories: %s\n", dir_list); 583f606c848SSatoshi Asami 584f606c848SSatoshi Asami for (i = 0, shp = shlib_head; shp; i++, shp = shp->next) 585f606c848SSatoshi Asami printf("\t%d:-l%s.%d.%d => %s\n", 586f606c848SSatoshi Asami i, shp->name, shp->major, shp->minor, shp->path); 587f606c848SSatoshi Asami 588f606c848SSatoshi Asami return; 589f606c848SSatoshi Asami } 590