Home | History | Annotate | Line # | Download | only in ldconfig
ldconfig.c revision 1.5
      1 /*
      2  * Copyright (c) 1993 Paul Kranenburg
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *      This product includes software developed by Paul Kranenburg.
     16  * 4. The name of the author may not be used to endorse or promote products
     17  *    derived from this software without specific prior written permission
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  *
     30  *	$Id: ldconfig.c,v 1.5 1994/01/29 02:03:31 jtc Exp $
     31  */
     32 
     33 #include <sys/param.h>
     34 #include <stdio.h>
     35 #include <stdlib.h>
     36 #include <sys/types.h>
     37 #include <sys/stat.h>
     38 #include <sys/file.h>
     39 #include <sys/time.h>
     40 #include <sys/mman.h>
     41 #include <sys/resource.h>
     42 #include <fcntl.h>
     43 #include <errno.h>
     44 #include <ar.h>
     45 #include <ranlib.h>
     46 #include <a.out.h>
     47 #include <stab.h>
     48 #include <string.h>
     49 #include <dirent.h>
     50 
     51 #include "ld.h"
     52 
     53 #undef major
     54 #undef minor
     55 
     56 char				*progname;
     57 static int			verbose;
     58 static int			nostd;
     59 static int			justread;
     60 
     61 struct shlib_list {
     62 	/* Internal list of shared libraries found */
     63 	char			*name;
     64 	char			*path;
     65 	int			dewey[MAXDEWEY];
     66 	int			ndewey;
     67 #define major dewey[0]
     68 #define minor dewey[1]
     69 	struct shlib_list	*next;
     70 };
     71 
     72 static struct shlib_list	*shlib_head = NULL, **shlib_tail = &shlib_head;
     73 
     74 static void	enter __P((char *, char *, char *, int *, int));
     75 static int	dodir __P((char *, int));
     76 static int	build_hints __P((void));
     77 
     78 int
     79 main(argc, argv)
     80 int	argc;
     81 char	*argv[];
     82 {
     83 	int		i, c;
     84 	int		rval = 0;
     85 	extern int	optind;
     86 
     87 	if ((progname = strrchr(argv[0], '/')) == NULL)
     88 		progname = argv[0];
     89 	else
     90 		progname++;
     91 
     92 	while ((c = getopt(argc, argv, "rsv")) != EOF) {
     93 		switch (c) {
     94 		case 'v':
     95 			verbose = 1;
     96 			break;
     97 		case 's':
     98 			nostd = 1;
     99 			break;
    100 		case 'r':
    101 			justread = 1;
    102 			break;
    103 		default:
    104 			fprintf(stderr, "Usage: %s [-v] [dir ...]\n", progname);
    105 			exit(1);
    106 			break;
    107 		}
    108 	}
    109 
    110 	if (justread)
    111 		return listhints();
    112 
    113 	if (!nostd)
    114 		std_search_dirs(NULL);
    115 
    116 	for (i = 0; i < n_search_dirs; i++)
    117 		rval |= dodir(search_dirs[i], 1);
    118 
    119 	for (i = optind; i < argc; i++)
    120 		rval |= dodir(argv[i], 0);
    121 
    122 	rval |= build_hints();
    123 
    124 	return rval;
    125 }
    126 
    127 int
    128 dodir(dir, silent)
    129 char	*dir;
    130 int	silent;
    131 {
    132 	DIR		*dd;
    133 	struct dirent	*dp;
    134 	char		name[MAXPATHLEN], rest[MAXPATHLEN];
    135 	int		dewey[MAXDEWEY], ndewey;
    136 
    137 	if ((dd = opendir(dir)) == NULL) {
    138 		if (!silent || errno != ENOENT)
    139 			perror(dir);
    140 		return -1;
    141 	}
    142 
    143 	while ((dp = readdir(dd)) != NULL) {
    144 		int	n;
    145 
    146 		name[0] = rest[0] = '\0';
    147 
    148 		n = sscanf(dp->d_name, "lib%[^.].so.%s",
    149 					name, rest);
    150 
    151 		if (n < 2 || rest[0] == '\0')
    152 			continue;
    153 
    154 		ndewey = getdewey(dewey, rest);
    155 		enter(dir, dp->d_name, name, dewey, ndewey);
    156 	}
    157 
    158 	return 0;
    159 }
    160 
    161 static void
    162 enter(dir, file, name, dewey, ndewey)
    163 char	*dir, *file, *name;
    164 int	dewey[], ndewey;
    165 {
    166 	struct shlib_list	*shp;
    167 
    168 	for (shp = shlib_head; shp; shp = shp->next) {
    169 		if (strcmp(name, shp->name) != 0 || major != shp->major)
    170 			continue;
    171 
    172 		/* Name matches existing entry */
    173 		if (cmpndewey(dewey, ndewey, shp->dewey, shp->ndewey) > 0) {
    174 
    175 			/* Update this entry with higher versioned lib */
    176 			if (verbose)
    177 				printf("Updating lib%s.%d.%d to %s/%s\n",
    178 					shp->name, shp->major, shp->minor,
    179 					dir, file);
    180 
    181 			free(shp->name);
    182 			shp->name = strdup(name);
    183 			free(shp->path);
    184 			shp->path = concat(dir, "/", file);
    185 			bcopy(dewey, shp->dewey, sizeof(shp->dewey));
    186 			shp->ndewey = ndewey;
    187 		}
    188 		break;
    189 	}
    190 
    191 	if (shp)
    192 		/* Name exists: older version or just updated */
    193 		return;
    194 
    195 	/* Allocate new list element */
    196 	if (verbose)
    197 		printf("Adding %s/%s\n", dir, file);
    198 
    199 	shp = (struct shlib_list *)xmalloc(sizeof *shp);
    200 	shp->name = strdup(name);
    201 	shp->path = concat(dir, "/", file);
    202 	bcopy(dewey, shp->dewey, MAXDEWEY);
    203 	shp->ndewey = ndewey;
    204 	shp->next = NULL;
    205 
    206 	*shlib_tail = shp;
    207 	shlib_tail = &shp->next;
    208 }
    209 
    210 
    211 #if DEBUG
    212 /* test */
    213 #undef _PATH_LD_HINTS
    214 #define _PATH_LD_HINTS		"./ld.so.hints"
    215 #endif
    216 
    217 int
    218 hinthash(cp, vmajor, vminor)
    219 char	*cp;
    220 int	vmajor, vminor;
    221 {
    222 	int	k = 0;
    223 
    224 	while (*cp)
    225 		k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
    226 
    227 	k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff;
    228 	k = (((k << 1) + (k >> 14)) ^ (vminor*167)) & 0x3fff;
    229 
    230 	return k;
    231 }
    232 
    233 int
    234 build_hints()
    235 {
    236 	struct hints_header	hdr;
    237 	struct hints_bucket	*blist;
    238 	struct shlib_list	*shp;
    239 	char			*strtab;
    240 	int			i, n, str_index = 0;
    241 	int			strtab_sz = 0;	/* Total length of strings */
    242 	int			nhints = 0;	/* Total number of hints */
    243 	int			fd;
    244 	char			*tmpfile;
    245 
    246 	for (shp = shlib_head; shp; shp = shp->next) {
    247 		strtab_sz += 1 + strlen(shp->name);
    248 		strtab_sz += 1 + strlen(shp->path);
    249 		nhints++;
    250 	}
    251 
    252 	/* Fill hints file header */
    253 	hdr.hh_magic = HH_MAGIC;
    254 	hdr.hh_version = LD_HINTS_VERSION_1;
    255 	hdr.hh_nbucket = 1 * nhints;
    256 	n = hdr.hh_nbucket * sizeof(struct hints_bucket);
    257 	hdr.hh_hashtab = sizeof(struct hints_header);
    258 	hdr.hh_strtab = hdr.hh_hashtab + n;
    259 	hdr.hh_strtab_sz = strtab_sz;
    260 	hdr.hh_ehints = hdr.hh_strtab + hdr.hh_strtab_sz;
    261 
    262 	if (verbose)
    263 		printf("Totals: entries %d, buckets %d, string size %d\n",
    264 					nhints, hdr.hh_nbucket, strtab_sz);
    265 
    266 	/* Allocate buckets and string table */
    267 	blist = (struct hints_bucket *)xmalloc(n);
    268 	bzero((char *)blist, n);
    269 	for (i = 0; i < hdr.hh_nbucket; i++)
    270 		/* Empty all buckets */
    271 		blist[i].hi_next = -1;
    272 
    273 	strtab = (char *)xmalloc(strtab_sz);
    274 
    275 	/* Enter all */
    276 	for (shp = shlib_head; shp; shp = shp->next) {
    277 		struct hints_bucket	*bp;
    278 
    279 		bp = blist +
    280 		  (hinthash(shp->name, shp->major, shp->minor) % hdr.hh_nbucket);
    281 
    282 		if (bp->hi_pathx) {
    283 			int	i;
    284 
    285 			for (i = 0; i < hdr.hh_nbucket; i++) {
    286 				if (blist[i].hi_pathx == 0)
    287 					break;
    288 			}
    289 			if (i == hdr.hh_nbucket) {
    290 				fprintf(stderr, "Bummer!\n");
    291 				return -1;
    292 			}
    293 			while (bp->hi_next != -1)
    294 				bp = &blist[bp->hi_next];
    295 			bp->hi_next = i;
    296 			bp = blist + i;
    297 		}
    298 
    299 		/* Insert strings in string table */
    300 		bp->hi_namex = str_index;
    301 		strcpy(strtab + str_index, shp->name);
    302 		str_index += 1 + strlen(shp->name);
    303 
    304 		bp->hi_pathx = str_index;
    305 		strcpy(strtab + str_index, shp->path);
    306 		str_index += 1 + strlen(shp->path);
    307 
    308 		/* Copy versions */
    309 		bcopy(shp->dewey, bp->hi_dewey, sizeof(bp->hi_dewey));
    310 		bp->hi_ndewey = shp->ndewey;
    311 	}
    312 
    313 	tmpfile = concat(_PATH_LD_HINTS, "+", "");
    314 	if ((fd = open(tmpfile, O_RDWR|O_CREAT|O_TRUNC, 0444)) == -1) {
    315 		perror(_PATH_LD_HINTS);
    316 		return -1;
    317 	}
    318 
    319 	if (write(fd, &hdr, sizeof(struct hints_header)) !=
    320 						sizeof(struct hints_header)) {
    321 		perror(_PATH_LD_HINTS);
    322 		return -1;
    323 	}
    324 	if (write(fd, blist, hdr.hh_nbucket * sizeof(struct hints_bucket)) !=
    325 				hdr.hh_nbucket * sizeof(struct hints_bucket)) {
    326 		perror(_PATH_LD_HINTS);
    327 		return -1;
    328 	}
    329 	if (write(fd, strtab, strtab_sz) != strtab_sz) {
    330 		perror(_PATH_LD_HINTS);
    331 		return -1;
    332 	}
    333 	if (close(fd) != 0) {
    334 		perror(_PATH_LD_HINTS);
    335 		return -1;
    336 	}
    337 
    338 	/* Install it */
    339 	if (unlink(_PATH_LD_HINTS) != 0 && errno != ENOENT) {
    340 		perror(_PATH_LD_HINTS);
    341 		return -1;
    342 	}
    343 
    344 	if (rename(tmpfile, _PATH_LD_HINTS) != 0) {
    345 		perror(_PATH_LD_HINTS);
    346 		return -1;
    347 	}
    348 
    349 	return 0;
    350 }
    351 
    352 int
    353 listhints()
    354 {
    355 	int			fd;
    356 	caddr_t			addr;
    357 	long			msize;
    358 	struct hints_header	*hdr;
    359 	struct hints_bucket	*blist;
    360 	char			*strtab;
    361 	int			i;
    362 
    363 	if ((fd = open(_PATH_LD_HINTS, O_RDONLY, 0)) == -1) {
    364 		perror(_PATH_LD_HINTS);
    365 		return -1;
    366 	}
    367 
    368 	msize = PAGSIZ;
    369 	addr = mmap(0, msize, PROT_READ, MAP_FILE|MAP_COPY, fd, 0);
    370 
    371 	if (addr == (caddr_t)-1) {
    372 		perror(_PATH_LD_HINTS);
    373 		return -1;
    374 	}
    375 
    376 	hdr = (struct hints_header *)addr;
    377 	if (HH_BADMAG(*hdr)) {
    378 		fprintf(stderr, "%s: Bad magic: %d\n");
    379 		return -1;
    380 	}
    381 
    382 	if (hdr->hh_version != LD_HINTS_VERSION_1) {
    383 		fprintf(stderr, "Unsupported version: %d\n", hdr->hh_version);
    384 		return -1;
    385 	}
    386 
    387 	if (hdr->hh_ehints > msize) {
    388 		if (mmap(addr+msize, hdr->hh_ehints - msize,
    389 				PROT_READ, MAP_FILE|MAP_COPY|MAP_FIXED,
    390 				fd, msize) != (caddr_t)(addr+msize)) {
    391 
    392 			perror(_PATH_LD_HINTS);
    393 			return -1;
    394 		}
    395 	}
    396 	close(fd);
    397 
    398 	blist = (struct hints_bucket *)(addr + hdr->hh_hashtab);
    399 	strtab = (char *)(addr + hdr->hh_strtab);
    400 
    401 	printf("%s:\n", _PATH_LD_HINTS);
    402 	for (i = 0; i < hdr->hh_nbucket; i++) {
    403 		struct hints_bucket	*bp = &blist[i];
    404 
    405 		/* Sanity check */
    406 		if (bp->hi_namex >= hdr->hh_strtab_sz) {
    407 			fprintf(stderr, "Bad name index: %#x\n", bp->hi_namex);
    408 			return -1;
    409 		}
    410 		if (bp->hi_pathx >= hdr->hh_strtab_sz) {
    411 			fprintf(stderr, "Bad path index: %#x\n", bp->hi_pathx);
    412 			return -1;
    413 		}
    414 
    415 		printf("\t%d:-l%s.%d.%d => %s (%d -> %d)\n",
    416 			i,
    417 			strtab + bp->hi_namex, bp->hi_major, bp->hi_minor,
    418 			strtab + bp->hi_pathx,
    419 			hinthash(strtab+bp->hi_namex, bp->hi_major, bp->hi_minor)
    420 					% hdr->hh_nbucket,
    421 			bp->hi_next);
    422 	}
    423 
    424 	return 0;
    425 }
    426 
    427