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 * 30d4ba5766SPeter Wemm * $Id: ldconfig.c,v 1.19 1997/07/11 14:45:41 jkh Exp $ 31b9ae52e3SPaul Richards */ 32b9ae52e3SPaul Richards 33b9ae52e3SPaul Richards #include <sys/param.h> 34b9ae52e3SPaul Richards #include <sys/types.h> 35b9ae52e3SPaul Richards #include <sys/stat.h> 36b9ae52e3SPaul Richards #include <sys/file.h> 37b9ae52e3SPaul Richards #include <sys/time.h> 38b9ae52e3SPaul Richards #include <sys/mman.h> 39b9ae52e3SPaul Richards #include <sys/resource.h> 40699e1b82SRich Murphey #include <dirent.h> 41b9ae52e3SPaul Richards #include <errno.h> 42526195adSJordan K. Hubbard #include <err.h> 43526195adSJordan K. Hubbard #include <ctype.h> 44699e1b82SRich Murphey #include <fcntl.h> 45b9ae52e3SPaul Richards #include <ar.h> 46b9ae52e3SPaul Richards #include <ranlib.h> 47b9ae52e3SPaul Richards #include <a.out.h> 48b9ae52e3SPaul Richards #include <stab.h> 49699e1b82SRich Murphey #include <stdio.h> 50699e1b82SRich Murphey #include <stdlib.h> 51b9ae52e3SPaul Richards #include <string.h> 52699e1b82SRich Murphey #include <unistd.h> 53b9ae52e3SPaul Richards 5480c71499SPeter Wemm #include <machine/param.h> 5580c71499SPeter Wemm 5680c71499SPeter Wemm #include <link.h> 5780c71499SPeter Wemm #include "shlib.h" 5880c71499SPeter Wemm #include "support.h" 5980c71499SPeter Wemm 607c6da7dcSJohn Polstra #if DEBUG 617c6da7dcSJohn Polstra /* test */ 627c6da7dcSJohn Polstra #undef _PATH_LD_HINTS 637c6da7dcSJohn Polstra #define _PATH_LD_HINTS "./ld.so.hints" 647c6da7dcSJohn Polstra #endif 65b9ae52e3SPaul Richards 66b9ae52e3SPaul Richards #undef major 67b9ae52e3SPaul Richards #undef minor 68b9ae52e3SPaul Richards 69b9ae52e3SPaul Richards static int verbose; 70b9ae52e3SPaul Richards static int nostd; 71b9ae52e3SPaul Richards static int justread; 72f606c848SSatoshi Asami static int merge; 73d4ba5766SPeter Wemm static int rescan; 747c6da7dcSJohn Polstra static char *hints_file = _PATH_LD_HINTS; 75b9ae52e3SPaul Richards 76b9ae52e3SPaul Richards struct shlib_list { 77b9ae52e3SPaul Richards /* Internal list of shared libraries found */ 78b9ae52e3SPaul Richards char *name; 79b9ae52e3SPaul Richards char *path; 80b9ae52e3SPaul Richards int dewey[MAXDEWEY]; 81b9ae52e3SPaul Richards int ndewey; 82b9ae52e3SPaul Richards #define major dewey[0] 83b9ae52e3SPaul Richards #define minor dewey[1] 84b9ae52e3SPaul Richards struct shlib_list *next; 85b9ae52e3SPaul Richards }; 86b9ae52e3SPaul Richards 87b9ae52e3SPaul Richards static struct shlib_list *shlib_head = NULL, **shlib_tail = &shlib_head; 8880c71499SPeter Wemm static char *dir_list; 89b9ae52e3SPaul Richards 90b9ae52e3SPaul Richards static void enter __P((char *, char *, char *, int *, int)); 9109e3d49dSJordan K. Hubbard static int dodir __P((char *, int)); 92f606c848SSatoshi Asami static int buildhints __P((void)); 93f606c848SSatoshi Asami static int readhints __P((void)); 94f606c848SSatoshi Asami static void listhints __P((void)); 95b9ae52e3SPaul Richards 96b9ae52e3SPaul Richards int 97b9ae52e3SPaul Richards main(argc, argv) 98b9ae52e3SPaul Richards int argc; 99b9ae52e3SPaul Richards char *argv[]; 100b9ae52e3SPaul Richards { 101b9ae52e3SPaul Richards int i, c; 102b9ae52e3SPaul Richards int rval = 0; 103b9ae52e3SPaul Richards 104d4ba5766SPeter Wemm while ((c = getopt(argc, argv, "Rf:mrsv")) != EOF) { 105b9ae52e3SPaul Richards switch (c) { 106d4ba5766SPeter Wemm case 'R': 107d4ba5766SPeter Wemm rescan = 1; 108d4ba5766SPeter Wemm break; 1097c6da7dcSJohn Polstra case 'f': 1107c6da7dcSJohn Polstra hints_file = optarg; 1117c6da7dcSJohn Polstra break; 112f606c848SSatoshi Asami case 'm': 113f606c848SSatoshi Asami merge = 1; 114b9ae52e3SPaul Richards break; 115b9ae52e3SPaul Richards case 'r': 116b9ae52e3SPaul Richards justread = 1; 117b9ae52e3SPaul Richards break; 118f606c848SSatoshi Asami case 's': 119f606c848SSatoshi Asami nostd = 1; 120f606c848SSatoshi Asami break; 121f606c848SSatoshi Asami case 'v': 122f606c848SSatoshi Asami verbose = 1; 123f606c848SSatoshi Asami break; 124b9ae52e3SPaul Richards default: 125d4ba5766SPeter Wemm errx(1, "Usage: %s [-Rmrsv] [-f hints_file] " 126d4ba5766SPeter Wemm "[dir | file ...]", argv[0]); 127b9ae52e3SPaul Richards break; 128b9ae52e3SPaul Richards } 129b9ae52e3SPaul Richards } 130b9ae52e3SPaul Richards 13180c71499SPeter Wemm dir_list = strdup(""); 13280c71499SPeter Wemm 133d4ba5766SPeter Wemm if (justread || merge || rescan) { 134f606c848SSatoshi Asami if ((rval = readhints()) != 0) 135f606c848SSatoshi Asami return rval; 13680c71499SPeter Wemm } 13780c71499SPeter Wemm 138d4ba5766SPeter Wemm if (!nostd && !merge && !rescan) 13980c71499SPeter Wemm std_search_path(); 14080c71499SPeter Wemm 141571b472bSJordan K. Hubbard /* Add any directories/files from the command line */ 142571b472bSJordan K. Hubbard if (!justread) { 143d66f9d22SJohn Polstra for (i = optind; i < argc; i++) { 144571b472bSJordan K. Hubbard struct stat stbuf; 145571b472bSJordan K. Hubbard 146571b472bSJordan K. Hubbard if (stat(argv[i], &stbuf) == -1) { 147d66f9d22SJohn Polstra warn("%s", argv[i]); 148d66f9d22SJohn Polstra rval = -1; 149571b472bSJordan K. Hubbard } else { 150571b472bSJordan K. Hubbard /* 151571b472bSJordan K. Hubbard * See if this is a directory-containing 152571b472bSJordan K. Hubbard * file instead of a directory 153571b472bSJordan K. Hubbard */ 154571b472bSJordan K. Hubbard if (S_ISREG(stbuf.st_mode)) 155571b472bSJordan K. Hubbard rval |= dofile(argv[i], 0); 156571b472bSJordan K. Hubbard else 15780c71499SPeter Wemm add_search_path(argv[i]); 158d66f9d22SJohn Polstra } 159d66f9d22SJohn Polstra } 160571b472bSJordan K. Hubbard } 16180c71499SPeter Wemm 16280c71499SPeter Wemm for (i = 0; i < n_search_dirs; i++) { 16380c71499SPeter Wemm char *cp = concat(dir_list, *dir_list?":":"", search_dirs[i]); 16480c71499SPeter Wemm free(dir_list); 16580c71499SPeter Wemm dir_list = cp; 16680c71499SPeter Wemm } 16780c71499SPeter Wemm 168f606c848SSatoshi Asami if (justread) { 169f606c848SSatoshi Asami listhints(); 170526195adSJordan K. Hubbard return 0; 171f606c848SSatoshi Asami } 172b9ae52e3SPaul Richards 173b9ae52e3SPaul Richards for (i = 0; i < n_search_dirs; i++) 17409e3d49dSJordan K. Hubbard rval |= dodir(search_dirs[i], 1); 175b9ae52e3SPaul Richards 176f606c848SSatoshi Asami rval |= buildhints(); 177b9ae52e3SPaul Richards 178b9ae52e3SPaul Richards return rval; 179b9ae52e3SPaul Richards } 180b9ae52e3SPaul Richards 181b9ae52e3SPaul Richards int 182571b472bSJordan K. Hubbard dofile(fname, silent) 183571b472bSJordan K. Hubbard char *fname; 184571b472bSJordan K. Hubbard int silent; 185571b472bSJordan K. Hubbard { 186571b472bSJordan K. Hubbard FILE *hfp; 187571b472bSJordan K. Hubbard char buf[MAXPATHLEN]; 188571b472bSJordan K. Hubbard int rval = 0; 189571b472bSJordan K. Hubbard char *cp, *sp; 190571b472bSJordan K. Hubbard 191571b472bSJordan K. Hubbard if ((hfp = fopen(fname, "r")) == NULL) { 192571b472bSJordan K. Hubbard warn("%s", fname); 193571b472bSJordan K. Hubbard return -1; 194571b472bSJordan K. Hubbard } 195571b472bSJordan K. Hubbard 196571b472bSJordan K. Hubbard while (fgets(buf, sizeof(buf), hfp)) { 197571b472bSJordan K. Hubbard cp = buf; 198571b472bSJordan K. Hubbard while (isspace(*cp)) 199571b472bSJordan K. Hubbard cp++; 200571b472bSJordan K. Hubbard if (*cp == '#' || *cp == '\0') 201571b472bSJordan K. Hubbard continue; 202571b472bSJordan K. Hubbard sp = cp; 203571b472bSJordan K. Hubbard while (!isspace(*cp) && *cp != '\0') 204571b472bSJordan K. Hubbard cp++; 205571b472bSJordan K. Hubbard 206571b472bSJordan K. Hubbard if (*cp != '\n') { 207571b472bSJordan K. Hubbard *cp = '\0'; 208571b472bSJordan K. Hubbard warnx("%s: Trailing characters ignored", sp); 209571b472bSJordan K. Hubbard } 210571b472bSJordan K. Hubbard 211571b472bSJordan K. Hubbard *cp = '\0'; 212571b472bSJordan K. Hubbard 213571b472bSJordan K. Hubbard rval |= dodir(sp, silent); 214571b472bSJordan K. Hubbard } 215571b472bSJordan K. Hubbard 216571b472bSJordan K. Hubbard (void)fclose(hfp); 217571b472bSJordan K. Hubbard return rval; 218571b472bSJordan K. Hubbard } 219571b472bSJordan K. Hubbard 220571b472bSJordan K. Hubbard int 22109e3d49dSJordan K. Hubbard dodir(dir, silent) 222b9ae52e3SPaul Richards char *dir; 22309e3d49dSJordan K. Hubbard int silent; 224b9ae52e3SPaul Richards { 225b9ae52e3SPaul Richards DIR *dd; 226b9ae52e3SPaul Richards struct dirent *dp; 2270f6b2cb3SPaul Traina char name[MAXPATHLEN]; 228b9ae52e3SPaul Richards int dewey[MAXDEWEY], ndewey; 229b9ae52e3SPaul Richards 230b9ae52e3SPaul Richards if ((dd = opendir(dir)) == NULL) { 231d66f9d22SJohn Polstra if (silent && errno == ENOENT) /* Ignore the error */ 232d66f9d22SJohn Polstra return 0; 233f606c848SSatoshi Asami warn("%s", dir); 234b9ae52e3SPaul Richards return -1; 235b9ae52e3SPaul Richards } 236b9ae52e3SPaul Richards 237b9ae52e3SPaul Richards while ((dp = readdir(dd)) != NULL) { 2380f6b2cb3SPaul Traina register int n; 2390f6b2cb3SPaul Traina register char *cp; 240b9ae52e3SPaul Richards 2410f6b2cb3SPaul Traina /* Check for `lib' prefix */ 2420f6b2cb3SPaul Traina if (dp->d_name[0] != 'l' || 2430f6b2cb3SPaul Traina dp->d_name[1] != 'i' || 2440f6b2cb3SPaul Traina dp->d_name[2] != 'b') 245b9ae52e3SPaul Richards continue; 246b9ae52e3SPaul Richards 2470f6b2cb3SPaul Traina /* Copy the entry minus prefix */ 2480f6b2cb3SPaul Traina (void)strcpy(name, dp->d_name + 3); 2490f6b2cb3SPaul Traina n = strlen(name); 2500f6b2cb3SPaul Traina if (n < 4) 2510f6b2cb3SPaul Traina continue; 2520f6b2cb3SPaul Traina 2530f6b2cb3SPaul Traina /* Find ".so." in name */ 2540f6b2cb3SPaul Traina for (cp = name + n - 4; cp > name; --cp) { 2550f6b2cb3SPaul Traina if (cp[0] == '.' && 2560f6b2cb3SPaul Traina cp[1] == 's' && 2570f6b2cb3SPaul Traina cp[2] == 'o' && 2580f6b2cb3SPaul Traina cp[3] == '.') 2590f6b2cb3SPaul Traina break; 2600f6b2cb3SPaul Traina } 2610f6b2cb3SPaul Traina if (cp <= name) 2620f6b2cb3SPaul Traina continue; 2630f6b2cb3SPaul Traina 2640f6b2cb3SPaul Traina *cp = '\0'; 2650f6b2cb3SPaul Traina if (!isdigit(*(cp+4))) 2660f6b2cb3SPaul Traina continue; 2670f6b2cb3SPaul Traina 2680f6b2cb3SPaul Traina bzero((caddr_t)dewey, sizeof(dewey)); 2690f6b2cb3SPaul Traina ndewey = getdewey(dewey, cp + 4); 270b9ae52e3SPaul Richards enter(dir, dp->d_name, name, dewey, ndewey); 271b9ae52e3SPaul Richards } 272b9ae52e3SPaul Richards 273571b472bSJordan K. Hubbard closedir(dd); 274b9ae52e3SPaul Richards return 0; 275b9ae52e3SPaul Richards } 276b9ae52e3SPaul Richards 277b9ae52e3SPaul Richards static void 278b9ae52e3SPaul Richards enter(dir, file, name, dewey, ndewey) 279b9ae52e3SPaul Richards char *dir, *file, *name; 280b9ae52e3SPaul Richards int dewey[], ndewey; 281b9ae52e3SPaul Richards { 282b9ae52e3SPaul Richards struct shlib_list *shp; 283b9ae52e3SPaul Richards 284b9ae52e3SPaul Richards for (shp = shlib_head; shp; shp = shp->next) { 285b9ae52e3SPaul Richards if (strcmp(name, shp->name) != 0 || major != shp->major) 286b9ae52e3SPaul Richards continue; 287b9ae52e3SPaul Richards 288b9ae52e3SPaul Richards /* Name matches existing entry */ 289b9ae52e3SPaul Richards if (cmpndewey(dewey, ndewey, shp->dewey, shp->ndewey) > 0) { 290b9ae52e3SPaul Richards 291b9ae52e3SPaul Richards /* Update this entry with higher versioned lib */ 292b9ae52e3SPaul Richards if (verbose) 293b9ae52e3SPaul Richards printf("Updating lib%s.%d.%d to %s/%s\n", 294b9ae52e3SPaul Richards shp->name, shp->major, shp->minor, 295b9ae52e3SPaul Richards dir, file); 296b9ae52e3SPaul Richards 297b9ae52e3SPaul Richards free(shp->name); 298b9ae52e3SPaul Richards shp->name = strdup(name); 299b9ae52e3SPaul Richards free(shp->path); 300b9ae52e3SPaul Richards shp->path = concat(dir, "/", file); 301b9ae52e3SPaul Richards bcopy(dewey, shp->dewey, sizeof(shp->dewey)); 302b9ae52e3SPaul Richards shp->ndewey = ndewey; 303b9ae52e3SPaul Richards } 304b9ae52e3SPaul Richards break; 305b9ae52e3SPaul Richards } 306b9ae52e3SPaul Richards 307b9ae52e3SPaul Richards if (shp) 308b9ae52e3SPaul Richards /* Name exists: older version or just updated */ 309b9ae52e3SPaul Richards return; 310b9ae52e3SPaul Richards 311b9ae52e3SPaul Richards /* Allocate new list element */ 312b9ae52e3SPaul Richards if (verbose) 313b9ae52e3SPaul Richards printf("Adding %s/%s\n", dir, file); 314b9ae52e3SPaul Richards 315b9ae52e3SPaul Richards shp = (struct shlib_list *)xmalloc(sizeof *shp); 316b9ae52e3SPaul Richards shp->name = strdup(name); 317b9ae52e3SPaul Richards shp->path = concat(dir, "/", file); 318b9ae52e3SPaul Richards bcopy(dewey, shp->dewey, MAXDEWEY); 319b9ae52e3SPaul Richards shp->ndewey = ndewey; 320b9ae52e3SPaul Richards shp->next = NULL; 321b9ae52e3SPaul Richards 322b9ae52e3SPaul Richards *shlib_tail = shp; 323b9ae52e3SPaul Richards shlib_tail = &shp->next; 324b9ae52e3SPaul Richards } 325b9ae52e3SPaul Richards 326b9ae52e3SPaul Richards 327b9ae52e3SPaul Richards int 328d5453ba5SJoerg Wunsch hinthash(cp, vmajor) 329b9ae52e3SPaul Richards char *cp; 330d5453ba5SJoerg Wunsch int vmajor; 331b9ae52e3SPaul Richards { 332b9ae52e3SPaul Richards int k = 0; 333b9ae52e3SPaul Richards 334b9ae52e3SPaul Richards while (*cp) 335b9ae52e3SPaul Richards k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff; 336b9ae52e3SPaul Richards 337b9ae52e3SPaul Richards k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff; 338b9ae52e3SPaul Richards 339b9ae52e3SPaul Richards return k; 340b9ae52e3SPaul Richards } 341b9ae52e3SPaul Richards 342b9ae52e3SPaul Richards int 343f606c848SSatoshi Asami buildhints() 344b9ae52e3SPaul Richards { 345b9ae52e3SPaul Richards struct hints_header hdr; 346b9ae52e3SPaul Richards struct hints_bucket *blist; 347b9ae52e3SPaul Richards struct shlib_list *shp; 348b9ae52e3SPaul Richards char *strtab; 349b9ae52e3SPaul Richards int i, n, str_index = 0; 350b9ae52e3SPaul Richards int strtab_sz = 0; /* Total length of strings */ 351b9ae52e3SPaul Richards int nhints = 0; /* Total number of hints */ 352b9ae52e3SPaul Richards int fd; 353b9ae52e3SPaul Richards char *tmpfile; 354b9ae52e3SPaul Richards 355b9ae52e3SPaul Richards for (shp = shlib_head; shp; shp = shp->next) { 356b9ae52e3SPaul Richards strtab_sz += 1 + strlen(shp->name); 357b9ae52e3SPaul Richards strtab_sz += 1 + strlen(shp->path); 358b9ae52e3SPaul Richards nhints++; 359b9ae52e3SPaul Richards } 360b9ae52e3SPaul Richards 361b9ae52e3SPaul Richards /* Fill hints file header */ 362b9ae52e3SPaul Richards hdr.hh_magic = HH_MAGIC; 36380c71499SPeter Wemm hdr.hh_version = LD_HINTS_VERSION_2; 364b9ae52e3SPaul Richards hdr.hh_nbucket = 1 * nhints; 365b9ae52e3SPaul Richards n = hdr.hh_nbucket * sizeof(struct hints_bucket); 366b9ae52e3SPaul Richards hdr.hh_hashtab = sizeof(struct hints_header); 367b9ae52e3SPaul Richards hdr.hh_strtab = hdr.hh_hashtab + n; 36880c71499SPeter Wemm hdr.hh_dirlist = strtab_sz; 36980c71499SPeter Wemm strtab_sz += 1 + strlen(dir_list); 370b9ae52e3SPaul Richards hdr.hh_strtab_sz = strtab_sz; 371b9ae52e3SPaul Richards hdr.hh_ehints = hdr.hh_strtab + hdr.hh_strtab_sz; 372b9ae52e3SPaul Richards 373b9ae52e3SPaul Richards if (verbose) 374526195adSJordan K. Hubbard printf("Totals: entries %d, buckets %ld, string size %d\n", 375b9ae52e3SPaul Richards nhints, hdr.hh_nbucket, strtab_sz); 376b9ae52e3SPaul Richards 377b9ae52e3SPaul Richards /* Allocate buckets and string table */ 378b9ae52e3SPaul Richards blist = (struct hints_bucket *)xmalloc(n); 379b9ae52e3SPaul Richards bzero((char *)blist, n); 380b9ae52e3SPaul Richards for (i = 0; i < hdr.hh_nbucket; i++) 381b9ae52e3SPaul Richards /* Empty all buckets */ 382b9ae52e3SPaul Richards blist[i].hi_next = -1; 383b9ae52e3SPaul Richards 384b9ae52e3SPaul Richards strtab = (char *)xmalloc(strtab_sz); 385b9ae52e3SPaul Richards 386b9ae52e3SPaul Richards /* Enter all */ 387b9ae52e3SPaul Richards for (shp = shlib_head; shp; shp = shp->next) { 388b9ae52e3SPaul Richards struct hints_bucket *bp; 389b9ae52e3SPaul Richards 390b9ae52e3SPaul Richards bp = blist + 391d5453ba5SJoerg Wunsch (hinthash(shp->name, shp->major) % hdr.hh_nbucket); 392b9ae52e3SPaul Richards 393b9ae52e3SPaul Richards if (bp->hi_pathx) { 394b9ae52e3SPaul Richards int i; 395b9ae52e3SPaul Richards 396b9ae52e3SPaul Richards for (i = 0; i < hdr.hh_nbucket; i++) { 397b9ae52e3SPaul Richards if (blist[i].hi_pathx == 0) 398b9ae52e3SPaul Richards break; 399b9ae52e3SPaul Richards } 400b9ae52e3SPaul Richards if (i == hdr.hh_nbucket) { 401f606c848SSatoshi Asami warnx("Bummer!"); 402b9ae52e3SPaul Richards return -1; 403b9ae52e3SPaul Richards } 404b9ae52e3SPaul Richards while (bp->hi_next != -1) 405b9ae52e3SPaul Richards bp = &blist[bp->hi_next]; 406b9ae52e3SPaul Richards bp->hi_next = i; 407b9ae52e3SPaul Richards bp = blist + i; 408b9ae52e3SPaul Richards } 409b9ae52e3SPaul Richards 410b9ae52e3SPaul Richards /* Insert strings in string table */ 411b9ae52e3SPaul Richards bp->hi_namex = str_index; 412b9ae52e3SPaul Richards strcpy(strtab + str_index, shp->name); 413b9ae52e3SPaul Richards str_index += 1 + strlen(shp->name); 414b9ae52e3SPaul Richards 415b9ae52e3SPaul Richards bp->hi_pathx = str_index; 416b9ae52e3SPaul Richards strcpy(strtab + str_index, shp->path); 417b9ae52e3SPaul Richards str_index += 1 + strlen(shp->path); 418b9ae52e3SPaul Richards 419b9ae52e3SPaul Richards /* Copy versions */ 420b9ae52e3SPaul Richards bcopy(shp->dewey, bp->hi_dewey, sizeof(bp->hi_dewey)); 421b9ae52e3SPaul Richards bp->hi_ndewey = shp->ndewey; 422b9ae52e3SPaul Richards } 423b9ae52e3SPaul Richards 42480c71499SPeter Wemm /* Copy search directories */ 42580c71499SPeter Wemm strcpy(strtab + str_index, dir_list); 42680c71499SPeter Wemm str_index += 1 + strlen(dir_list); 42780c71499SPeter Wemm 42880c71499SPeter Wemm /* Sanity check */ 42980c71499SPeter Wemm if (str_index != strtab_sz) { 43080c71499SPeter Wemm errx(1, "str_index(%d) != strtab_sz(%d)", str_index, strtab_sz); 43180c71499SPeter Wemm } 43280c71499SPeter Wemm 4337c6da7dcSJohn Polstra tmpfile = concat(hints_file, ".XXXXXX", ""); 43480c71499SPeter Wemm if ((tmpfile = mktemp(tmpfile)) == NULL) { 43580c71499SPeter Wemm warn("%s", tmpfile); 43680c71499SPeter Wemm return -1; 43780c71499SPeter Wemm } 43880c71499SPeter Wemm 43980c71499SPeter Wemm umask(0); /* Create with exact permissions */ 440b9ae52e3SPaul Richards if ((fd = open(tmpfile, O_RDWR|O_CREAT|O_TRUNC, 0444)) == -1) { 4417c6da7dcSJohn Polstra warn("%s", hints_file); 442b9ae52e3SPaul Richards return -1; 443b9ae52e3SPaul Richards } 444b9ae52e3SPaul Richards 44509e3d49dSJordan K. Hubbard if (write(fd, &hdr, sizeof(struct hints_header)) != 44609e3d49dSJordan K. Hubbard sizeof(struct hints_header)) { 4477c6da7dcSJohn Polstra warn("%s", hints_file); 44809e3d49dSJordan K. Hubbard return -1; 44909e3d49dSJordan K. Hubbard } 45009e3d49dSJordan K. Hubbard if (write(fd, blist, hdr.hh_nbucket * sizeof(struct hints_bucket)) != 45109e3d49dSJordan K. Hubbard hdr.hh_nbucket * sizeof(struct hints_bucket)) { 4527c6da7dcSJohn Polstra warn("%s", hints_file); 45309e3d49dSJordan K. Hubbard return -1; 45409e3d49dSJordan K. Hubbard } 45509e3d49dSJordan K. Hubbard if (write(fd, strtab, strtab_sz) != strtab_sz) { 4567c6da7dcSJohn Polstra warn("%s", hints_file); 45709e3d49dSJordan K. Hubbard return -1; 45809e3d49dSJordan K. Hubbard } 459b9ae52e3SPaul Richards if (close(fd) != 0) { 4607c6da7dcSJohn Polstra warn("%s", hints_file); 461b9ae52e3SPaul Richards return -1; 462b9ae52e3SPaul Richards } 463b9ae52e3SPaul Richards 46409e3d49dSJordan K. Hubbard /* Install it */ 4657c6da7dcSJohn Polstra if (unlink(hints_file) != 0 && errno != ENOENT) { 4667c6da7dcSJohn Polstra warn("%s", hints_file); 467b9ae52e3SPaul Richards return -1; 468b9ae52e3SPaul Richards } 469b9ae52e3SPaul Richards 4707c6da7dcSJohn Polstra if (rename(tmpfile, hints_file) != 0) { 4717c6da7dcSJohn Polstra warn("%s", hints_file); 472b9ae52e3SPaul Richards return -1; 473b9ae52e3SPaul Richards } 474b9ae52e3SPaul Richards 475b9ae52e3SPaul Richards return 0; 476b9ae52e3SPaul Richards } 477b9ae52e3SPaul Richards 478699e1b82SRich Murphey static int 479f606c848SSatoshi Asami readhints() 480b9ae52e3SPaul Richards { 481b9ae52e3SPaul Richards int fd; 482b9ae52e3SPaul Richards caddr_t addr; 483b9ae52e3SPaul Richards long msize; 484b9ae52e3SPaul Richards struct hints_header *hdr; 485b9ae52e3SPaul Richards struct hints_bucket *blist; 486b9ae52e3SPaul Richards char *strtab; 487f606c848SSatoshi Asami struct shlib_list *shp; 488b9ae52e3SPaul Richards int i; 489b9ae52e3SPaul Richards 4907c6da7dcSJohn Polstra if ((fd = open(hints_file, O_RDONLY, 0)) == -1) { 4917c6da7dcSJohn Polstra warn("%s", hints_file); 492b9ae52e3SPaul Richards return -1; 493b9ae52e3SPaul Richards } 494b9ae52e3SPaul Richards 49580c71499SPeter Wemm msize = PAGE_SIZE; 49661f9ce8dSNate Williams addr = mmap(0, msize, PROT_READ, MAP_COPY, fd, 0); 497b9ae52e3SPaul Richards 498b9ae52e3SPaul Richards if (addr == (caddr_t)-1) { 4997c6da7dcSJohn Polstra warn("%s", hints_file); 500b9ae52e3SPaul Richards return -1; 501b9ae52e3SPaul Richards } 502b9ae52e3SPaul Richards 503b9ae52e3SPaul Richards hdr = (struct hints_header *)addr; 504b9ae52e3SPaul Richards if (HH_BADMAG(*hdr)) { 505f606c848SSatoshi Asami warnx("%s: Bad magic: %o", 5067c6da7dcSJohn Polstra hints_file, hdr->hh_magic); 507b9ae52e3SPaul Richards return -1; 508b9ae52e3SPaul Richards } 509b9ae52e3SPaul Richards 51080c71499SPeter Wemm if (hdr->hh_version != LD_HINTS_VERSION_1 && 51180c71499SPeter Wemm hdr->hh_version != LD_HINTS_VERSION_2) { 512f606c848SSatoshi Asami warnx("Unsupported version: %d", hdr->hh_version); 513b9ae52e3SPaul Richards return -1; 514b9ae52e3SPaul Richards } 515b9ae52e3SPaul Richards 516b9ae52e3SPaul Richards if (hdr->hh_ehints > msize) { 517b9ae52e3SPaul Richards if (mmap(addr+msize, hdr->hh_ehints - msize, 51861f9ce8dSNate Williams PROT_READ, MAP_COPY|MAP_FIXED, 519b9ae52e3SPaul Richards fd, msize) != (caddr_t)(addr+msize)) { 520b9ae52e3SPaul Richards 5217c6da7dcSJohn Polstra warn("%s", hints_file); 522b9ae52e3SPaul Richards return -1; 523b9ae52e3SPaul Richards } 524b9ae52e3SPaul Richards } 525b9ae52e3SPaul Richards close(fd); 526b9ae52e3SPaul Richards 527b9ae52e3SPaul Richards blist = (struct hints_bucket *)(addr + hdr->hh_hashtab); 528b9ae52e3SPaul Richards strtab = (char *)(addr + hdr->hh_strtab); 529b9ae52e3SPaul Richards 530d4ba5766SPeter Wemm if (hdr->hh_version >= LD_HINTS_VERSION_2) 531d4ba5766SPeter Wemm add_search_path(strtab + hdr->hh_dirlist); 532d4ba5766SPeter Wemm else if (rescan) 533d4ba5766SPeter Wemm errx(1, "%s too old and does not contain the search path", 534d4ba5766SPeter Wemm hints_file); 535d4ba5766SPeter Wemm 536d4ba5766SPeter Wemm if (rescan) 537d4ba5766SPeter Wemm return 0; 538d4ba5766SPeter Wemm 539b9ae52e3SPaul Richards for (i = 0; i < hdr->hh_nbucket; i++) { 540b9ae52e3SPaul Richards struct hints_bucket *bp = &blist[i]; 541b9ae52e3SPaul Richards 542b9ae52e3SPaul Richards /* Sanity check */ 543b9ae52e3SPaul Richards if (bp->hi_namex >= hdr->hh_strtab_sz) { 544f606c848SSatoshi Asami warnx("Bad name index: %#x", bp->hi_namex); 545b9ae52e3SPaul Richards return -1; 546b9ae52e3SPaul Richards } 547b9ae52e3SPaul Richards if (bp->hi_pathx >= hdr->hh_strtab_sz) { 548f606c848SSatoshi Asami warnx("Bad path index: %#x", bp->hi_pathx); 549b9ae52e3SPaul Richards return -1; 550b9ae52e3SPaul Richards } 551b9ae52e3SPaul Richards 552f606c848SSatoshi Asami /* Allocate new list element */ 553f606c848SSatoshi Asami shp = (struct shlib_list *)xmalloc(sizeof *shp); 554f606c848SSatoshi Asami shp->name = strdup(strtab + bp->hi_namex); 555f606c848SSatoshi Asami shp->path = strdup(strtab + bp->hi_pathx); 556f606c848SSatoshi Asami bcopy(bp->hi_dewey, shp->dewey, sizeof(shp->dewey)); 557f606c848SSatoshi Asami shp->ndewey = bp->hi_ndewey; 558f606c848SSatoshi Asami shp->next = NULL; 559f606c848SSatoshi Asami 560f606c848SSatoshi Asami *shlib_tail = shp; 561f606c848SSatoshi Asami shlib_tail = &shp->next; 562b9ae52e3SPaul Richards } 563b9ae52e3SPaul Richards 564b9ae52e3SPaul Richards return 0; 565b9ae52e3SPaul Richards } 566b9ae52e3SPaul Richards 567f606c848SSatoshi Asami static void 568f606c848SSatoshi Asami listhints() 569f606c848SSatoshi Asami { 570f606c848SSatoshi Asami struct shlib_list *shp; 571f606c848SSatoshi Asami int i; 572f606c848SSatoshi Asami 5737c6da7dcSJohn Polstra printf("%s:\n", hints_file); 57480c71499SPeter Wemm printf("\tsearch directories: %s\n", dir_list); 575f606c848SSatoshi Asami 576f606c848SSatoshi Asami for (i = 0, shp = shlib_head; shp; i++, shp = shp->next) 577f606c848SSatoshi Asami printf("\t%d:-l%s.%d.%d => %s\n", 578f606c848SSatoshi Asami i, shp->name, shp->major, shp->minor, shp->path); 579f606c848SSatoshi Asami 580f606c848SSatoshi Asami return; 581f606c848SSatoshi Asami } 582