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