Home | History | Annotate | Line # | Download | only in ldconfig
ldconfig.c revision 1.9
      1 /*
      2  * Copyright (c) 1993,1995 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.9 1995/06/20 23:12:35 pk Exp $
     31  */
     32 
     33 #include <sys/param.h>
     34 #include <sys/types.h>
     35 #include <sys/stat.h>
     36 #include <sys/file.h>
     37 #include <sys/time.h>
     38 #include <sys/mman.h>
     39 #include <sys/resource.h>
     40 #include <dirent.h>
     41 #include <errno.h>
     42 #include <fcntl.h>
     43 #include <ar.h>
     44 #include <ranlib.h>
     45 #include <a.out.h>
     46 #include <stab.h>
     47 #include <stdio.h>
     48 #include <stdlib.h>
     49 #include <string.h>
     50 #include <unistd.h>
     51 
     52 #include "ld.h"
     53 
     54 #undef major
     55 #undef minor
     56 
     57 extern char			*__progname;
     58 
     59 static int			verbose;
     60 static int			nostd;
     61 static int			justread;
     62 static int			merge;
     63 
     64 struct shlib_list {
     65 	/* Internal list of shared libraries found */
     66 	char			*name;
     67 	char			*path;
     68 	int			dewey[MAXDEWEY];
     69 	int			ndewey;
     70 #define major dewey[0]
     71 #define minor dewey[1]
     72 	struct shlib_list	*next;
     73 };
     74 
     75 static struct shlib_list	*shlib_head = NULL, **shlib_tail = &shlib_head;
     76 
     77 static void	enter __P((char *, char *, char *, int *, int));
     78 static int	dodir __P((char *, int));
     79 static int	buildhints __P((void));
     80 static int	readhints __P((void));
     81 static void	listhints __P((void));
     82 
     83 int
     84 main(argc, argv)
     85 int	argc;
     86 char	*argv[];
     87 {
     88 	int		i, c;
     89 	int		rval = 0;
     90 
     91 	while ((c = getopt(argc, argv, "mrsv")) != EOF) {
     92 		switch (c) {
     93 		case 'm':
     94 			merge = 1;
     95 			break;
     96 		case 'r':
     97 			justread = 1;
     98 			break;
     99 		case 's':
    100 			nostd = 1;
    101 			break;
    102 		case 'v':
    103 			verbose = 1;
    104 			break;
    105 		default:
    106 			errx(1, "Usage: %s [-r][-s][-v][dir ...]",
    107 				__progname);
    108 			break;
    109 		}
    110 	}
    111 
    112 	if (justread || merge) {
    113 		if ((rval = readhints()) != 0)
    114 			return rval;
    115 		if (justread) {
    116 			listhints();
    117 			return;
    118 		}
    119 	}
    120 
    121 	if (!nostd && !merge)
    122 		std_search_path();
    123 
    124 	for (i = 0; i < n_search_dirs; i++)
    125 		rval |= dodir(search_dirs[i], 1);
    126 
    127 	for (i = optind; i < argc; i++)
    128 		rval |= dodir(argv[i], 0);
    129 
    130 	rval |= buildhints();
    131 
    132 	return rval;
    133 }
    134 
    135 int
    136 dodir(dir, silent)
    137 char	*dir;
    138 int	silent;
    139 {
    140 	DIR		*dd;
    141 	struct dirent	*dp;
    142 	char		name[MAXPATHLEN], rest[MAXPATHLEN];
    143 	int		dewey[MAXDEWEY], ndewey;
    144 
    145 	if ((dd = opendir(dir)) == NULL) {
    146 		if (!silent || errno != ENOENT)
    147 			warn("%s", dir);
    148 		return -1;
    149 	}
    150 
    151 	while ((dp = readdir(dd)) != NULL) {
    152 		int	n;
    153 
    154 		name[0] = rest[0] = '\0';
    155 
    156 		n = sscanf(dp->d_name, "lib%[^.].so.%s",
    157 					name, rest);
    158 
    159 		if (n < 2 || rest[0] == '\0')
    160 			continue;
    161 
    162 		ndewey = getdewey(dewey, rest);
    163 		enter(dir, dp->d_name, name, dewey, ndewey);
    164 	}
    165 
    166 	return 0;
    167 }
    168 
    169 static void
    170 enter(dir, file, name, dewey, ndewey)
    171 char	*dir, *file, *name;
    172 int	dewey[], ndewey;
    173 {
    174 	struct shlib_list	*shp;
    175 
    176 	for (shp = shlib_head; shp; shp = shp->next) {
    177 		if (strcmp(name, shp->name) != 0 || major != shp->major)
    178 			continue;
    179 
    180 		/* Name matches existing entry */
    181 		if (cmpndewey(dewey, ndewey, shp->dewey, shp->ndewey) > 0) {
    182 
    183 			/* Update this entry with higher versioned lib */
    184 			if (verbose)
    185 				printf("Updating lib%s.%d.%d to %s/%s\n",
    186 					shp->name, shp->major, shp->minor,
    187 					dir, file);
    188 
    189 			free(shp->name);
    190 			shp->name = strdup(name);
    191 			free(shp->path);
    192 			shp->path = concat(dir, "/", file);
    193 			bcopy(dewey, shp->dewey, sizeof(shp->dewey));
    194 			shp->ndewey = ndewey;
    195 		}
    196 		break;
    197 	}
    198 
    199 	if (shp)
    200 		/* Name exists: older version or just updated */
    201 		return;
    202 
    203 	/* Allocate new list element */
    204 	if (verbose)
    205 		printf("Adding %s/%s\n", dir, file);
    206 
    207 	shp = (struct shlib_list *)xmalloc(sizeof *shp);
    208 	shp->name = strdup(name);
    209 	shp->path = concat(dir, "/", file);
    210 	bcopy(dewey, shp->dewey, MAXDEWEY);
    211 	shp->ndewey = ndewey;
    212 	shp->next = NULL;
    213 
    214 	*shlib_tail = shp;
    215 	shlib_tail = &shp->next;
    216 }
    217 
    218 
    219 #if DEBUG
    220 /* test */
    221 #undef _PATH_LD_HINTS
    222 #define _PATH_LD_HINTS		"./ld.so.hints"
    223 #endif
    224 
    225 int
    226 hinthash(cp, vmajor, vminor)
    227 char	*cp;
    228 int	vmajor, vminor;
    229 {
    230 	int	k = 0;
    231 
    232 	while (*cp)
    233 		k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
    234 
    235 	k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff;
    236 	k = (((k << 1) + (k >> 14)) ^ (vminor*167)) & 0x3fff;
    237 
    238 	return k;
    239 }
    240 
    241 int
    242 buildhints()
    243 {
    244 	struct hints_header	hdr;
    245 	struct hints_bucket	*blist;
    246 	struct shlib_list	*shp;
    247 	char			*strtab;
    248 	int			i, n, str_index = 0;
    249 	int			strtab_sz = 0;	/* Total length of strings */
    250 	int			nhints = 0;	/* Total number of hints */
    251 	int			fd;
    252 	char			*tmpfile;
    253 
    254 	for (shp = shlib_head; shp; shp = shp->next) {
    255 		strtab_sz += 1 + strlen(shp->name);
    256 		strtab_sz += 1 + strlen(shp->path);
    257 		nhints++;
    258 	}
    259 
    260 	/* Fill hints file header */
    261 	hdr.hh_magic = HH_MAGIC;
    262 	hdr.hh_version = LD_HINTS_VERSION_1;
    263 	hdr.hh_nbucket = 1 * nhints;
    264 	n = hdr.hh_nbucket * sizeof(struct hints_bucket);
    265 	hdr.hh_hashtab = sizeof(struct hints_header);
    266 	hdr.hh_strtab = hdr.hh_hashtab + n;
    267 	hdr.hh_strtab_sz = strtab_sz;
    268 	hdr.hh_ehints = hdr.hh_strtab + hdr.hh_strtab_sz;
    269 
    270 	if (verbose)
    271 		printf("Totals: entries %d, buckets %d, string size %d\n",
    272 					nhints, hdr.hh_nbucket, strtab_sz);
    273 
    274 	/* Allocate buckets and string table */
    275 	blist = (struct hints_bucket *)xmalloc(n);
    276 	bzero((char *)blist, n);
    277 	for (i = 0; i < hdr.hh_nbucket; i++)
    278 		/* Empty all buckets */
    279 		blist[i].hi_next = -1;
    280 
    281 	strtab = (char *)xmalloc(strtab_sz);
    282 
    283 	/* Enter all */
    284 	for (shp = shlib_head; shp; shp = shp->next) {
    285 		struct hints_bucket	*bp;
    286 
    287 		bp = blist +
    288 		  (hinthash(shp->name, shp->major, shp->minor) % hdr.hh_nbucket);
    289 
    290 		if (bp->hi_pathx) {
    291 			int	i;
    292 
    293 			for (i = 0; i < hdr.hh_nbucket; i++) {
    294 				if (blist[i].hi_pathx == 0)
    295 					break;
    296 			}
    297 			if (i == hdr.hh_nbucket) {
    298 				warnx("Bummer!");
    299 				return -1;
    300 			}
    301 			while (bp->hi_next != -1)
    302 				bp = &blist[bp->hi_next];
    303 			bp->hi_next = i;
    304 			bp = blist + i;
    305 		}
    306 
    307 		/* Insert strings in string table */
    308 		bp->hi_namex = str_index;
    309 		strcpy(strtab + str_index, shp->name);
    310 		str_index += 1 + strlen(shp->name);
    311 
    312 		bp->hi_pathx = str_index;
    313 		strcpy(strtab + str_index, shp->path);
    314 		str_index += 1 + strlen(shp->path);
    315 
    316 		/* Copy versions */
    317 		bcopy(shp->dewey, bp->hi_dewey, sizeof(bp->hi_dewey));
    318 		bp->hi_ndewey = shp->ndewey;
    319 	}
    320 
    321 	tmpfile = concat(_PATH_LD_HINTS, "+", "");
    322 	if ((fd = open(tmpfile, O_RDWR|O_CREAT|O_TRUNC, 0444)) == -1) {
    323 		warn("%s", _PATH_LD_HINTS);
    324 		return -1;
    325 	}
    326 
    327 	if (write(fd, &hdr, sizeof(struct hints_header)) !=
    328 						sizeof(struct hints_header)) {
    329 		warn("%s", _PATH_LD_HINTS);
    330 		return -1;
    331 	}
    332 	if (write(fd, blist, hdr.hh_nbucket * sizeof(struct hints_bucket)) !=
    333 				hdr.hh_nbucket * sizeof(struct hints_bucket)) {
    334 		warn("%s", _PATH_LD_HINTS);
    335 		return -1;
    336 	}
    337 	if (write(fd, strtab, strtab_sz) != strtab_sz) {
    338 		warn("%s", _PATH_LD_HINTS);
    339 		return -1;
    340 	}
    341 	if (close(fd) != 0) {
    342 		warn("%s", _PATH_LD_HINTS);
    343 		return -1;
    344 	}
    345 
    346 	/* Install it */
    347 	if (unlink(_PATH_LD_HINTS) != 0 && errno != ENOENT) {
    348 		warn("%s", _PATH_LD_HINTS);
    349 		return -1;
    350 	}
    351 
    352 	if (rename(tmpfile, _PATH_LD_HINTS) != 0) {
    353 		warn("%s", _PATH_LD_HINTS);
    354 		return -1;
    355 	}
    356 
    357 	return 0;
    358 }
    359 
    360 static int
    361 readhints()
    362 {
    363 	int			fd;
    364 	caddr_t			addr;
    365 	long			msize;
    366 	struct hints_header	*hdr;
    367 	struct hints_bucket	*blist;
    368 	char			*strtab;
    369 	struct shlib_list	*shp;
    370 	int			i;
    371 
    372 	if ((fd = open(_PATH_LD_HINTS, O_RDONLY, 0)) == -1) {
    373 		warn("%s", _PATH_LD_HINTS);
    374 		return -1;
    375 	}
    376 
    377 	msize = PAGSIZ;
    378 	addr = mmap(0, msize, PROT_READ, MAP_COPY, fd, 0);
    379 
    380 	if (addr == (caddr_t)-1) {
    381 		warn("%s", _PATH_LD_HINTS);
    382 		return -1;
    383 	}
    384 
    385 	hdr = (struct hints_header *)addr;
    386 	if (HH_BADMAG(*hdr)) {
    387 		warnx("%s: Bad magic: %o",
    388 			_PATH_LD_HINTS, hdr->hh_magic);
    389 		return -1;
    390 	}
    391 
    392 	if (hdr->hh_version != LD_HINTS_VERSION_1) {
    393 		warnx("Unsupported version: %d", hdr->hh_version);
    394 		return -1;
    395 	}
    396 
    397 	if (hdr->hh_ehints > msize) {
    398 		if (mmap(addr+msize, hdr->hh_ehints - msize,
    399 				PROT_READ, MAP_COPY|MAP_FIXED,
    400 				fd, msize) != (caddr_t)(addr+msize)) {
    401 
    402 			warn("%s", _PATH_LD_HINTS);
    403 			return -1;
    404 		}
    405 	}
    406 	close(fd);
    407 
    408 	blist = (struct hints_bucket *)(addr + hdr->hh_hashtab);
    409 	strtab = (char *)(addr + hdr->hh_strtab);
    410 
    411 	for (i = 0; i < hdr->hh_nbucket; i++) {
    412 		struct hints_bucket	*bp = &blist[i];
    413 
    414 		/* Sanity check */
    415 		if (bp->hi_namex >= hdr->hh_strtab_sz) {
    416 			warnx("Bad name index: %#x", bp->hi_namex);
    417 			return -1;
    418 		}
    419 		if (bp->hi_pathx >= hdr->hh_strtab_sz) {
    420 			warnx("Bad path index: %#x", bp->hi_pathx);
    421 			return -1;
    422 		}
    423 
    424 		/* Allocate new list element */
    425 		shp = (struct shlib_list *)xmalloc(sizeof *shp);
    426 		shp->name = strdup(strtab + bp->hi_namex);
    427 		shp->path = strdup(strtab + bp->hi_pathx);
    428 		bcopy(bp->hi_dewey, shp->dewey, sizeof(shp->dewey));
    429 		shp->ndewey = bp->hi_ndewey;
    430 		shp->next = NULL;
    431 
    432 		*shlib_tail = shp;
    433 		shlib_tail = &shp->next;
    434 	}
    435 
    436 	return 0;
    437 }
    438 
    439 static void
    440 listhints()
    441 {
    442 	struct shlib_list	*shp;
    443 	int			i;
    444 
    445 	printf("%s:\n", _PATH_LD_HINTS);
    446 
    447 	for (i = 0, shp = shlib_head; shp; i++, shp = shp->next)
    448 		printf("\t%d:-l%s.%d.%d => %s\n",
    449 			i, shp->name, shp->major, shp->minor, shp->path);
    450 
    451 	return;
    452 }
    453