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