Home | History | Annotate | Line # | Download | only in ldconfig
ldconfig.c revision 1.22.2.1
      1  1.22.2.1        he /*	$NetBSD: ldconfig.c,v 1.22.2.1 1999/10/20 23:15:02 he Exp $	*/
      2      1.15   thorpej 
      3      1.20        pk /*-
      4      1.20        pk  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5       1.1        pk  * All rights reserved.
      6       1.1        pk  *
      7      1.20        pk  * This code is derived from software contributed to The NetBSD Foundation
      8      1.20        pk  * by Paul Kranenburg.
      9      1.20        pk  *
     10       1.1        pk  * Redistribution and use in source and binary forms, with or without
     11       1.1        pk  * modification, are permitted provided that the following conditions
     12       1.1        pk  * are met:
     13       1.1        pk  * 1. Redistributions of source code must retain the above copyright
     14       1.1        pk  *    notice, this list of conditions and the following disclaimer.
     15       1.1        pk  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1        pk  *    notice, this list of conditions and the following disclaimer in the
     17       1.1        pk  *    documentation and/or other materials provided with the distribution.
     18       1.1        pk  * 3. All advertising materials mentioning features or use of this software
     19       1.1        pk  *    must display the following acknowledgement:
     20      1.20        pk  *        This product includes software developed by the NetBSD
     21      1.20        pk  *        Foundation, Inc. and its contributors.
     22      1.20        pk  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23      1.20        pk  *    contributors may be used to endorse or promote products derived
     24      1.20        pk  *    from this software without specific prior written permission.
     25       1.1        pk  *
     26      1.20        pk  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27      1.20        pk  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28      1.20        pk  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29      1.20        pk  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30      1.20        pk  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31      1.20        pk  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32      1.20        pk  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33      1.20        pk  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34      1.20        pk  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35      1.20        pk  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36      1.20        pk  * POSSIBILITY OF SUCH DAMAGE.
     37       1.1        pk  */
     38       1.1        pk 
     39       1.1        pk #include <sys/param.h>
     40       1.1        pk #include <sys/types.h>
     41       1.1        pk #include <sys/stat.h>
     42       1.1        pk #include <sys/file.h>
     43       1.1        pk #include <sys/time.h>
     44       1.1        pk #include <sys/mman.h>
     45      1.21        pk #include <ctype.h>
     46       1.7        pk #include <dirent.h>
     47      1.21        pk #include <err.h>
     48       1.7        pk #include <errno.h>
     49       1.1        pk #include <fcntl.h>
     50       1.7        pk #include <stdio.h>
     51       1.7        pk #include <stdlib.h>
     52       1.1        pk #include <string.h>
     53       1.7        pk #include <unistd.h>
     54      1.21        pk #include <link.h>
     55       1.1        pk 
     56      1.22        pk #include "shlib.h"
     57       1.1        pk 
     58      1.18   thorpej #define _PATH_LD_SO_CONF "/etc/ld.so.conf"
     59      1.18   thorpej 
     60       1.1        pk #undef major
     61       1.1        pk #undef minor
     62       1.1        pk 
     63       1.8        pk extern char			*__progname;
     64       1.8        pk 
     65       1.1        pk static int			verbose;
     66       1.1        pk static int			nostd;
     67      1.18   thorpej static int			noconf;
     68       1.1        pk static int			justread;
     69       1.9        pk static int			merge;
     70       1.1        pk 
     71       1.1        pk struct shlib_list {
     72       1.1        pk 	/* Internal list of shared libraries found */
     73       1.1        pk 	char			*name;
     74       1.1        pk 	char			*path;
     75       1.1        pk 	int			dewey[MAXDEWEY];
     76       1.1        pk 	int			ndewey;
     77       1.1        pk #define major dewey[0]
     78       1.1        pk #define minor dewey[1]
     79       1.1        pk 	struct shlib_list	*next;
     80       1.1        pk };
     81       1.1        pk 
     82       1.1        pk static struct shlib_list	*shlib_head = NULL, **shlib_tail = &shlib_head;
     83      1.13        pk static char			*dir_list;
     84       1.1        pk 
     85       1.1        pk static void	enter __P((char *, char *, char *, int *, int));
     86      1.18   thorpej static int	dodir __P((char *, int, int));
     87      1.18   thorpej static int	do_conf __P((void));
     88       1.9        pk static int	buildhints __P((void));
     89       1.9        pk static int	readhints __P((void));
     90       1.9        pk static void	listhints __P((void));
     91      1.21        pk static int	hinthash __P((char *, int, int));
     92      1.21        pk int		main __P((int, char *[]));
     93       1.1        pk 
     94       1.1        pk int
     95       1.1        pk main(argc, argv)
     96      1.21        pk 	int	argc;
     97      1.21        pk 	char	*argv[];
     98       1.1        pk {
     99      1.21        pk 	int	i, c;
    100      1.21        pk 	int	rval = 0;
    101       1.1        pk 
    102      1.18   thorpej 	while ((c = getopt(argc, argv, "cmrsSv")) != -1) {
    103       1.1        pk 		switch (c) {
    104      1.18   thorpej 		case 'c':
    105      1.18   thorpej 			noconf = 1;
    106      1.18   thorpej 			break;
    107       1.9        pk 		case 'm':
    108       1.9        pk 			merge = 1;
    109       1.9        pk 			break;
    110       1.9        pk 		case 'r':
    111       1.9        pk 			justread = 1;
    112       1.1        pk 			break;
    113       1.1        pk 		case 's':
    114       1.1        pk 			nostd = 1;
    115      1.18   thorpej 			noconf = 1;
    116      1.18   thorpej 			break;
    117      1.18   thorpej 		case 'S':
    118      1.18   thorpej 			nostd = 1;
    119       1.1        pk 			break;
    120       1.9        pk 		case 'v':
    121       1.9        pk 			verbose = 1;
    122       1.1        pk 			break;
    123       1.1        pk 		default:
    124      1.18   thorpej 			errx(1, "Usage: %s [-c][-m][-r][-s][-S][-v][dir ...]",
    125       1.8        pk 				__progname);
    126       1.1        pk 			break;
    127       1.1        pk 		}
    128       1.1        pk 	}
    129       1.1        pk 
    130      1.13        pk 	dir_list = xmalloc(1);
    131      1.13        pk 	*dir_list = '\0';
    132      1.13        pk 
    133       1.9        pk 	if (justread || merge) {
    134       1.9        pk 		if ((rval = readhints()) != 0)
    135      1.21        pk 			return (rval);
    136       1.9        pk 		if (justread) {
    137       1.9        pk 			listhints();
    138      1.21        pk 			return (rval);
    139       1.9        pk 		}
    140       1.9        pk 	}
    141       1.1        pk 
    142       1.9        pk 	if (!nostd && !merge)
    143       1.7        pk 		std_search_path();
    144       1.1        pk 
    145       1.1        pk 	for (i = 0; i < n_search_dirs; i++)
    146      1.18   thorpej 		rval |= dodir(search_dirs[i], 1, 0);
    147      1.18   thorpej 
    148      1.18   thorpej 	if (!noconf && !merge)
    149      1.18   thorpej 		rval |= do_conf();
    150       1.1        pk 
    151      1.13        pk 	for (i = optind; i < argc; i++) {
    152      1.18   thorpej 		rval |= dodir(argv[i], 0, 1);
    153      1.13        pk 	}
    154       1.1        pk 
    155       1.9        pk 	rval |= buildhints();
    156       1.1        pk 
    157      1.21        pk 	return (rval);
    158       1.1        pk }
    159       1.1        pk 
    160       1.1        pk int
    161      1.18   thorpej do_conf ()
    162      1.18   thorpej {
    163      1.18   thorpej 	FILE		*conf;
    164      1.18   thorpej 	char		*line, *c;
    165      1.18   thorpej 	char		*cline = NULL;
    166      1.18   thorpej 	size_t		len;
    167      1.18   thorpej 	int		rval = 0;
    168      1.18   thorpej 
    169  1.22.2.1        he 	if ((conf = fopen(_PATH_LD_SO_CONF, "r")) == NULL) {
    170  1.22.2.1        he 		if (verbose) {
    171  1.22.2.1        he 			warnx("can't open `%s'", _PATH_LD_SO_CONF);
    172  1.22.2.1        he 		}
    173  1.22.2.1        he 		return (0);
    174  1.22.2.1        he 	}
    175      1.18   thorpej 
    176      1.18   thorpej 	while ((line = fgetln(conf, &len)) != NULL) {
    177      1.18   thorpej 		if (*line == '#' || *line == '\n')
    178      1.18   thorpej 			continue;
    179      1.18   thorpej 
    180      1.18   thorpej 		if (line[len-1] == '\n') {
    181      1.18   thorpej 			line[--len] = '\0';
    182      1.18   thorpej 		} else {
    183      1.18   thorpej 			cline = xmalloc(len+1);
    184      1.18   thorpej 			bcopy(line, cline, len);
    185      1.18   thorpej 			line = cline;
    186      1.18   thorpej 			line[len] = '\0';
    187      1.18   thorpej 		}
    188      1.18   thorpej 
    189      1.18   thorpej 		while (isblank(*line)) { line++; len--; }
    190      1.18   thorpej 		if ((c = strchr(line, '#')) == NULL)
    191      1.18   thorpej 			c = line + len;
    192      1.18   thorpej 		while (--c >= line && isblank(*c)) continue;
    193      1.18   thorpej 		if (c >= line) {
    194      1.18   thorpej 			*++c = '\0';
    195      1.18   thorpej 			rval |= dodir(line, 0, 1);
    196      1.18   thorpej 		}
    197      1.18   thorpej 
    198      1.18   thorpej 		if (cline) {
    199      1.18   thorpej 			free(cline);
    200      1.18   thorpej 			cline = NULL;
    201      1.18   thorpej 		}
    202      1.18   thorpej 	}
    203      1.18   thorpej 
    204  1.22.2.1        he 	(void) fclose(conf);
    205  1.22.2.1        he 
    206      1.21        pk 	return (rval);
    207      1.18   thorpej }
    208      1.18   thorpej 
    209      1.18   thorpej int
    210      1.18   thorpej dodir(dir, silent, update_dir_list)
    211      1.21        pk 	char	*dir;
    212      1.21        pk 	int	silent;
    213      1.21        pk 	int	update_dir_list;
    214       1.1        pk {
    215       1.1        pk 	DIR		*dd;
    216       1.1        pk 	struct dirent	*dp;
    217      1.10        pk 	char		name[MAXPATHLEN];
    218       1.1        pk 	int		dewey[MAXDEWEY], ndewey;
    219       1.1        pk 
    220       1.1        pk 	if ((dd = opendir(dir)) == NULL) {
    221       1.3        pk 		if (!silent || errno != ENOENT)
    222       1.9        pk 			warn("%s", dir);
    223      1.21        pk 		return (-1);
    224      1.18   thorpej 	}
    225      1.18   thorpej 
    226      1.18   thorpej 	if (update_dir_list) {
    227      1.18   thorpej 		/* Check for duplicates? */
    228      1.18   thorpej 		char *cp = concat(dir_list, *dir_list?":":"", dir);
    229      1.18   thorpej 		free(dir_list);
    230      1.18   thorpej 		dir_list = cp;
    231       1.1        pk 	}
    232       1.1        pk 
    233       1.1        pk 	while ((dp = readdir(dd)) != NULL) {
    234      1.21        pk 		int n;
    235      1.21        pk 		char *cp;
    236       1.1        pk 
    237      1.10        pk 		/* Check for `lib' prefix */
    238      1.10        pk 		if (dp->d_name[0] != 'l' ||
    239      1.10        pk 		    dp->d_name[1] != 'i' ||
    240      1.10        pk 		    dp->d_name[2] != 'b')
    241      1.10        pk 			continue;
    242      1.10        pk 
    243      1.10        pk 		/* Copy the entry minus prefix */
    244      1.10        pk 		(void)strcpy(name, dp->d_name + 3);
    245      1.10        pk 		n = strlen(name);
    246      1.10        pk 		if (n < 4)
    247      1.10        pk 			continue;
    248       1.1        pk 
    249      1.10        pk 		/* Find ".so." in name */
    250      1.10        pk 		for (cp = name + n - 4; cp > name; --cp) {
    251      1.10        pk 			if (cp[0] == '.' &&
    252      1.10        pk 			    cp[1] == 's' &&
    253      1.10        pk 			    cp[2] == 'o' &&
    254      1.10        pk 			    cp[3] == '.')
    255      1.10        pk 				break;
    256      1.10        pk 		}
    257      1.10        pk 		if (cp <= name)
    258      1.10        pk 			continue;
    259       1.1        pk 
    260      1.10        pk 		*cp = '\0';
    261      1.10        pk 		if (!isdigit(*(cp+4)))
    262       1.1        pk 			continue;
    263       1.1        pk 
    264      1.10        pk 		bzero((caddr_t)dewey, sizeof(dewey));
    265      1.10        pk 		ndewey = getdewey(dewey, cp + 4);
    266       1.1        pk 		enter(dir, dp->d_name, name, dewey, ndewey);
    267       1.1        pk 	}
    268  1.22.2.1        he 
    269  1.22.2.1        he 	(void) closedir(dd);
    270       1.1        pk 
    271      1.21        pk 	return (0);
    272       1.1        pk }
    273       1.1        pk 
    274       1.1        pk static void
    275       1.1        pk enter(dir, file, name, dewey, ndewey)
    276      1.21        pk 	char	*dir, *file, *name;
    277      1.21        pk 	int	dewey[], ndewey;
    278       1.1        pk {
    279       1.1        pk 	struct shlib_list	*shp;
    280       1.1        pk 
    281       1.1        pk 	for (shp = shlib_head; shp; shp = shp->next) {
    282       1.1        pk 		if (strcmp(name, shp->name) != 0 || major != shp->major)
    283       1.1        pk 			continue;
    284       1.1        pk 
    285       1.1        pk 		/* Name matches existing entry */
    286       1.1        pk 		if (cmpndewey(dewey, ndewey, shp->dewey, shp->ndewey) > 0) {
    287       1.1        pk 
    288       1.1        pk 			/* Update this entry with higher versioned lib */
    289       1.1        pk 			if (verbose)
    290       1.1        pk 				printf("Updating lib%s.%d.%d to %s/%s\n",
    291       1.1        pk 					shp->name, shp->major, shp->minor,
    292       1.1        pk 					dir, file);
    293       1.1        pk 
    294       1.1        pk 			free(shp->name);
    295       1.1        pk 			shp->name = strdup(name);
    296       1.1        pk 			free(shp->path);
    297       1.1        pk 			shp->path = concat(dir, "/", file);
    298       1.1        pk 			bcopy(dewey, shp->dewey, sizeof(shp->dewey));
    299       1.1        pk 			shp->ndewey = ndewey;
    300       1.1        pk 		}
    301       1.1        pk 		break;
    302       1.1        pk 	}
    303       1.1        pk 
    304       1.1        pk 	if (shp)
    305       1.1        pk 		/* Name exists: older version or just updated */
    306       1.1        pk 		return;
    307       1.1        pk 
    308       1.1        pk 	/* Allocate new list element */
    309       1.1        pk 	if (verbose)
    310       1.1        pk 		printf("Adding %s/%s\n", dir, file);
    311       1.1        pk 
    312       1.1        pk 	shp = (struct shlib_list *)xmalloc(sizeof *shp);
    313       1.1        pk 	shp->name = strdup(name);
    314       1.1        pk 	shp->path = concat(dir, "/", file);
    315       1.1        pk 	bcopy(dewey, shp->dewey, MAXDEWEY);
    316       1.1        pk 	shp->ndewey = ndewey;
    317       1.1        pk 	shp->next = NULL;
    318       1.1        pk 
    319       1.1        pk 	*shlib_tail = shp;
    320       1.1        pk 	shlib_tail = &shp->next;
    321       1.1        pk }
    322       1.1        pk 
    323       1.1        pk 
    324       1.1        pk #if DEBUG
    325       1.1        pk /* test */
    326       1.1        pk #undef _PATH_LD_HINTS
    327       1.1        pk #define _PATH_LD_HINTS		"./ld.so.hints"
    328       1.1        pk #endif
    329       1.1        pk 
    330      1.21        pk /* XXX - should be a common function with rtld.c */
    331       1.1        pk int
    332       1.1        pk hinthash(cp, vmajor, vminor)
    333      1.21        pk 	char	*cp;
    334      1.21        pk 	int	vmajor, vminor;
    335       1.1        pk {
    336       1.1        pk 	int	k = 0;
    337       1.1        pk 
    338       1.1        pk 	while (*cp)
    339       1.1        pk 		k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
    340       1.1        pk 
    341       1.1        pk 	k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff;
    342      1.13        pk #if 0
    343       1.1        pk 	k = (((k << 1) + (k >> 14)) ^ (vminor*167)) & 0x3fff;
    344      1.13        pk #endif
    345       1.1        pk 
    346      1.21        pk 	return (k);
    347       1.1        pk }
    348       1.1        pk 
    349       1.1        pk int
    350       1.9        pk buildhints()
    351       1.1        pk {
    352       1.1        pk 	struct hints_header	hdr;
    353       1.1        pk 	struct hints_bucket	*blist;
    354       1.1        pk 	struct shlib_list	*shp;
    355       1.1        pk 	char			*strtab;
    356       1.1        pk 	int			i, n, str_index = 0;
    357       1.1        pk 	int			strtab_sz = 0;	/* Total length of strings */
    358       1.1        pk 	int			nhints = 0;	/* Total number of hints */
    359       1.1        pk 	int			fd;
    360       1.1        pk 	char			*tmpfile;
    361       1.1        pk 
    362       1.1        pk 	for (shp = shlib_head; shp; shp = shp->next) {
    363       1.1        pk 		strtab_sz += 1 + strlen(shp->name);
    364       1.1        pk 		strtab_sz += 1 + strlen(shp->path);
    365       1.1        pk 		nhints++;
    366       1.1        pk 	}
    367       1.1        pk 
    368       1.1        pk 	/* Fill hints file header */
    369       1.1        pk 	hdr.hh_magic = HH_MAGIC;
    370      1.13        pk 	hdr.hh_version = LD_HINTS_VERSION_2;
    371       1.1        pk 	hdr.hh_nbucket = 1 * nhints;
    372       1.1        pk 	n = hdr.hh_nbucket * sizeof(struct hints_bucket);
    373       1.1        pk 	hdr.hh_hashtab = sizeof(struct hints_header);
    374       1.1        pk 	hdr.hh_strtab = hdr.hh_hashtab + n;
    375      1.13        pk 	hdr.hh_dirlist = strtab_sz;
    376      1.13        pk 	strtab_sz += 1 + strlen(dir_list);
    377       1.1        pk 	hdr.hh_strtab_sz = strtab_sz;
    378       1.1        pk 	hdr.hh_ehints = hdr.hh_strtab + hdr.hh_strtab_sz;
    379       1.1        pk 
    380       1.1        pk 	if (verbose)
    381      1.21        pk 		printf("Totals: entries %d, buckets %ld, string size %d\n",
    382       1.1        pk 					nhints, hdr.hh_nbucket, strtab_sz);
    383       1.1        pk 
    384       1.1        pk 	/* Allocate buckets and string table */
    385       1.1        pk 	blist = (struct hints_bucket *)xmalloc(n);
    386       1.1        pk 	bzero((char *)blist, n);
    387       1.1        pk 	for (i = 0; i < hdr.hh_nbucket; i++)
    388       1.1        pk 		/* Empty all buckets */
    389       1.1        pk 		blist[i].hi_next = -1;
    390       1.1        pk 
    391       1.1        pk 	strtab = (char *)xmalloc(strtab_sz);
    392       1.1        pk 
    393       1.1        pk 	/* Enter all */
    394       1.1        pk 	for (shp = shlib_head; shp; shp = shp->next) {
    395       1.1        pk 		struct hints_bucket	*bp;
    396       1.1        pk 
    397       1.1        pk 		bp = blist +
    398       1.1        pk 		  (hinthash(shp->name, shp->major, shp->minor) % hdr.hh_nbucket);
    399       1.1        pk 
    400       1.1        pk 		if (bp->hi_pathx) {
    401       1.1        pk 			int	i;
    402       1.1        pk 
    403       1.1        pk 			for (i = 0; i < hdr.hh_nbucket; i++) {
    404       1.1        pk 				if (blist[i].hi_pathx == 0)
    405       1.1        pk 					break;
    406       1.1        pk 			}
    407       1.1        pk 			if (i == hdr.hh_nbucket) {
    408       1.9        pk 				warnx("Bummer!");
    409      1.21        pk 				return (-1);
    410       1.1        pk 			}
    411       1.1        pk 			while (bp->hi_next != -1)
    412       1.1        pk 				bp = &blist[bp->hi_next];
    413       1.1        pk 			bp->hi_next = i;
    414       1.1        pk 			bp = blist + i;
    415       1.1        pk 		}
    416       1.1        pk 
    417       1.1        pk 		/* Insert strings in string table */
    418       1.1        pk 		bp->hi_namex = str_index;
    419       1.1        pk 		strcpy(strtab + str_index, shp->name);
    420       1.1        pk 		str_index += 1 + strlen(shp->name);
    421       1.1        pk 
    422       1.1        pk 		bp->hi_pathx = str_index;
    423       1.1        pk 		strcpy(strtab + str_index, shp->path);
    424       1.1        pk 		str_index += 1 + strlen(shp->path);
    425       1.1        pk 
    426       1.1        pk 		/* Copy versions */
    427       1.1        pk 		bcopy(shp->dewey, bp->hi_dewey, sizeof(bp->hi_dewey));
    428       1.1        pk 		bp->hi_ndewey = shp->ndewey;
    429       1.1        pk 	}
    430       1.1        pk 
    431      1.13        pk 	/* Copy search directories */
    432      1.13        pk 	strcpy(strtab + str_index, dir_list);
    433      1.13        pk 	str_index += 1 + strlen(dir_list);
    434      1.13        pk 
    435      1.13        pk 	/* Sanity check */
    436      1.13        pk 	if (str_index != strtab_sz) {
    437      1.13        pk 		errx(1, "str_index(%d) != strtab_sz(%d)", str_index, strtab_sz);
    438      1.13        pk 	}
    439      1.13        pk 
    440      1.12        pk 	tmpfile = concat(_PATH_LD_HINTS, ".XXXXXX", "");
    441      1.17  christos 	if ((fd = mkstemp(tmpfile)) == -1) {
    442      1.12        pk 		warn("%s", tmpfile);
    443      1.21        pk 		return (-1);
    444      1.12        pk 	}
    445      1.12        pk 
    446       1.4        pk 	if (write(fd, &hdr, sizeof(struct hints_header)) !=
    447      1.12        pk 	    sizeof(struct hints_header)) {
    448       1.9        pk 		warn("%s", _PATH_LD_HINTS);
    449      1.21        pk 		return (-1);
    450       1.4        pk 	}
    451       1.4        pk 	if (write(fd, blist, hdr.hh_nbucket * sizeof(struct hints_bucket)) !=
    452      1.12        pk 		  hdr.hh_nbucket * sizeof(struct hints_bucket)) {
    453       1.9        pk 		warn("%s", _PATH_LD_HINTS);
    454      1.21        pk 		return (-1);
    455       1.4        pk 	}
    456       1.4        pk 	if (write(fd, strtab, strtab_sz) != strtab_sz) {
    457      1.17  christos 		warn("%s", _PATH_LD_HINTS);
    458      1.21        pk 		return (-1);
    459      1.17  christos 	}
    460      1.17  christos 	if (fchmod(fd, 0444) == -1) {
    461       1.9        pk 		warn("%s", _PATH_LD_HINTS);
    462      1.21        pk 		return (-1);
    463       1.4        pk 	}
    464       1.1        pk 	if (close(fd) != 0) {
    465       1.9        pk 		warn("%s", _PATH_LD_HINTS);
    466      1.21        pk 		return (-1);
    467       1.1        pk 	}
    468       1.1        pk 
    469       1.4        pk 	/* Install it */
    470       1.1        pk 	if (unlink(_PATH_LD_HINTS) != 0 && errno != ENOENT) {
    471       1.9        pk 		warn("%s", _PATH_LD_HINTS);
    472      1.21        pk 		return (-1);
    473       1.1        pk 	}
    474       1.1        pk 
    475       1.1        pk 	if (rename(tmpfile, _PATH_LD_HINTS) != 0) {
    476       1.9        pk 		warn("%s", _PATH_LD_HINTS);
    477      1.21        pk 		return (-1);
    478       1.1        pk 	}
    479       1.1        pk 
    480      1.21        pk 	return (0);
    481       1.1        pk }
    482       1.1        pk 
    483       1.7        pk static int
    484       1.9        pk readhints()
    485       1.1        pk {
    486       1.1        pk 	int			fd;
    487       1.1        pk 	caddr_t			addr;
    488       1.1        pk 	long			msize;
    489       1.1        pk 	struct hints_header	*hdr;
    490       1.1        pk 	struct hints_bucket	*blist;
    491       1.1        pk 	char			*strtab;
    492       1.9        pk 	struct shlib_list	*shp;
    493       1.1        pk 	int			i;
    494       1.1        pk 
    495       1.1        pk 	if ((fd = open(_PATH_LD_HINTS, O_RDONLY, 0)) == -1) {
    496       1.9        pk 		warn("%s", _PATH_LD_HINTS);
    497      1.21        pk 		return (-1);
    498       1.1        pk 	}
    499       1.1        pk 
    500      1.21        pk 	msize = getpagesize();
    501      1.19   mycroft 	addr = mmap(0, msize, PROT_READ, MAP_FILE|MAP_COPY, fd, 0);
    502       1.1        pk 
    503       1.1        pk 	if (addr == (caddr_t)-1) {
    504       1.9        pk 		warn("%s", _PATH_LD_HINTS);
    505      1.21        pk 		return (-1);
    506       1.1        pk 	}
    507       1.1        pk 
    508       1.1        pk 	hdr = (struct hints_header *)addr;
    509       1.1        pk 	if (HH_BADMAG(*hdr)) {
    510      1.21        pk 		warnx("%s: Bad magic: %lo",
    511       1.7        pk 			_PATH_LD_HINTS, hdr->hh_magic);
    512      1.21        pk 		return (-1);
    513       1.1        pk 	}
    514       1.1        pk 
    515      1.13        pk 	if (hdr->hh_version != LD_HINTS_VERSION_2) {
    516      1.21        pk 		warnx("Unsupported version: %ld", hdr->hh_version);
    517      1.21        pk 		return (-1);
    518       1.1        pk 	}
    519       1.1        pk 
    520       1.1        pk 	if (hdr->hh_ehints > msize) {
    521       1.1        pk 		if (mmap(addr+msize, hdr->hh_ehints - msize,
    522      1.19   mycroft 				PROT_READ, MAP_FILE|MAP_COPY|MAP_FIXED,
    523       1.1        pk 				fd, msize) != (caddr_t)(addr+msize)) {
    524       1.1        pk 
    525       1.9        pk 			warn("%s", _PATH_LD_HINTS);
    526      1.21        pk 			return (-1);
    527       1.1        pk 		}
    528       1.1        pk 	}
    529       1.1        pk 	close(fd);
    530       1.1        pk 
    531       1.1        pk 	blist = (struct hints_bucket *)(addr + hdr->hh_hashtab);
    532       1.1        pk 	strtab = (char *)(addr + hdr->hh_strtab);
    533       1.1        pk 
    534       1.1        pk 	for (i = 0; i < hdr->hh_nbucket; i++) {
    535       1.1        pk 		struct hints_bucket	*bp = &blist[i];
    536       1.1        pk 
    537       1.1        pk 		/* Sanity check */
    538       1.1        pk 		if (bp->hi_namex >= hdr->hh_strtab_sz) {
    539       1.9        pk 			warnx("Bad name index: %#x", bp->hi_namex);
    540      1.21        pk 			return (-1);
    541       1.1        pk 		}
    542       1.1        pk 		if (bp->hi_pathx >= hdr->hh_strtab_sz) {
    543       1.9        pk 			warnx("Bad path index: %#x", bp->hi_pathx);
    544      1.21        pk 			return (-1);
    545       1.1        pk 		}
    546       1.1        pk 
    547       1.9        pk 		/* Allocate new list element */
    548       1.9        pk 		shp = (struct shlib_list *)xmalloc(sizeof *shp);
    549       1.9        pk 		shp->name = strdup(strtab + bp->hi_namex);
    550       1.9        pk 		shp->path = strdup(strtab + bp->hi_pathx);
    551       1.9        pk 		bcopy(bp->hi_dewey, shp->dewey, sizeof(shp->dewey));
    552       1.9        pk 		shp->ndewey = bp->hi_ndewey;
    553       1.9        pk 		shp->next = NULL;
    554       1.9        pk 
    555       1.9        pk 		*shlib_tail = shp;
    556       1.9        pk 		shlib_tail = &shp->next;
    557       1.1        pk 	}
    558      1.13        pk 	dir_list = strdup(strtab + hdr->hh_dirlist);
    559       1.1        pk 
    560      1.21        pk 	return (0);
    561       1.1        pk }
    562       1.1        pk 
    563       1.9        pk static void
    564       1.9        pk listhints()
    565       1.9        pk {
    566       1.9        pk 	struct shlib_list	*shp;
    567       1.9        pk 	int			i;
    568       1.9        pk 
    569       1.9        pk 	printf("%s:\n", _PATH_LD_HINTS);
    570      1.13        pk 	printf("\tsearch directories: %s\n", dir_list);
    571       1.9        pk 
    572       1.9        pk 	for (i = 0, shp = shlib_head; shp; i++, shp = shp->next)
    573       1.9        pk 		printf("\t%d:-l%s.%d.%d => %s\n",
    574       1.9        pk 			i, shp->name, shp->major, shp->minor, shp->path);
    575       1.9        pk 
    576       1.9        pk 	return;
    577       1.9        pk }
    578