Home | History | Annotate | Line # | Download | only in ldconfig
ldconfig.c revision 1.1
      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 withough 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.1 1993/10/23 00:17:03 pk 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 #define MAXDEWEY 8
     62 struct shlib_list {
     63 	/* Internal list of shared libraries found */
     64 	char			*name;
     65 	char			*path;
     66 	int			dewey[MAXDEWEY];
     67 	int			ndewey;
     68 #define major dewey[0]
     69 #define minor dewey[1]
     70 	struct shlib_list	*next;
     71 };
     72 
     73 static struct shlib_list	*shlib_head = NULL, **shlib_tail = &shlib_head;
     74 
     75 static void	enter __P((char *, char *, char *, int *, int));
     76 static int	dodir __P((char *));
     77 static int	build_hints __P((void));
     78 
     79 int
     80 main(argc, argv)
     81 int	argc;
     82 char	*argv[];
     83 {
     84 	int		i, c;
     85 	int		rval = 0;
     86 	extern int	optind;
     87 
     88 	if ((progname = strrchr(argv[0], '/')) == NULL)
     89 		progname = argv[0];
     90 	else
     91 		progname++;
     92 
     93 	while ((c = getopt(argc, argv, "rsv")) != EOF) {
     94 		switch (c) {
     95 		case 'v':
     96 			verbose = 1;
     97 			break;
     98 		case 's':
     99 			nostd = 1;
    100 			break;
    101 		case 'r':
    102 			justread = 1;
    103 			break;
    104 		default:
    105 			fprintf(stderr, "Usage: %s [-v] [dir ...]\n", progname);
    106 			exit(1);
    107 			break;
    108 		}
    109 	}
    110 
    111 	if (justread)
    112 		return listhints();
    113 
    114 	if (!nostd)
    115 		std_search_dirs(NULL);
    116 
    117 	for (i = 0; i < n_search_dirs; i++)
    118 		rval |= dodir(search_dirs[i]);
    119 
    120 	for (i = optind; i < argc; i++)
    121 		rval |= dodir(argv[i]);
    122 
    123 	rval |= build_hints();
    124 
    125 	return rval;
    126 }
    127 
    128 int
    129 dodir(dir)
    130 char	*dir;
    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 		perror(dir);
    139 		return -1;
    140 	}
    141 
    142 	while ((dp = readdir(dd)) != NULL) {
    143 		int	n;
    144 
    145 		name[0] = rest[0] = '\0';
    146 
    147 		n = sscanf(dp->d_name, "lib%[^.].so.%s",
    148 					name, rest);
    149 
    150 		if (n < 2 || rest[0] == '\0')
    151 			continue;
    152 
    153 		ndewey = getdewey(dewey, rest);
    154 		enter(dir, dp->d_name, name, dewey, ndewey);
    155 	}
    156 
    157 	return 0;
    158 }
    159 
    160 static void
    161 enter(dir, file, name, dewey, ndewey)
    162 char	*dir, *file, *name;
    163 int	dewey[], ndewey;
    164 {
    165 	struct shlib_list	*shp;
    166 
    167 	for (shp = shlib_head; shp; shp = shp->next) {
    168 		if (strcmp(name, shp->name) != 0 || major != shp->major)
    169 			continue;
    170 
    171 		/* Name matches existing entry */
    172 		if (cmpndewey(dewey, ndewey, shp->dewey, shp->ndewey) > 0) {
    173 
    174 			/* Update this entry with higher versioned lib */
    175 			if (verbose)
    176 				printf("Updating lib%s.%d.%d to %s/%s\n",
    177 					shp->name, shp->major, shp->minor,
    178 					dir, file);
    179 
    180 			free(shp->name);
    181 			shp->name = strdup(name);
    182 			free(shp->path);
    183 			shp->path = concat(dir, "/", file);
    184 			bcopy(dewey, shp->dewey, sizeof(shp->dewey));
    185 			shp->ndewey = ndewey;
    186 		}
    187 		break;
    188 	}
    189 
    190 	if (shp)
    191 		/* Name exists: older version or just updated */
    192 		return;
    193 
    194 	/* Allocate new list element */
    195 	if (verbose)
    196 		printf("Adding %s/%s\n", dir, file);
    197 
    198 	shp = (struct shlib_list *)xmalloc(sizeof *shp);
    199 	shp->name = strdup(name);
    200 	shp->path = concat(dir, "/", file);
    201 	bcopy(dewey, shp->dewey, MAXDEWEY);
    202 	shp->ndewey = ndewey;
    203 	shp->next = NULL;
    204 
    205 	*shlib_tail = shp;
    206 	shlib_tail = &shp->next;
    207 }
    208 
    209 
    210 #if DEBUG
    211 /* test */
    212 #undef _PATH_LD_HINTS
    213 #define _PATH_LD_HINTS		"./ld.so.hints"
    214 #endif
    215 
    216 int
    217 hinthash(cp, vmajor, vminor)
    218 char	*cp;
    219 int	vmajor, vminor;
    220 {
    221 	int	k = 0;
    222 
    223 	while (*cp)
    224 		k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
    225 
    226 	k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff;
    227 	k = (((k << 1) + (k >> 14)) ^ (vminor*167)) & 0x3fff;
    228 
    229 	return k;
    230 }
    231 
    232 int
    233 build_hints()
    234 {
    235 	struct hints_header	hdr;
    236 	struct hints_bucket	*blist;
    237 	struct shlib_list	*shp;
    238 	char			*strtab;
    239 	int			i, n, str_index = 0;
    240 	int			strtab_sz = 0;	/* Total length of strings */
    241 	int			nhints = 0;	/* Total number of hints */
    242 	int			fd;
    243 	char			*tmpfile;
    244 
    245 	for (shp = shlib_head; shp; shp = shp->next) {
    246 		strtab_sz += 1 + strlen(shp->name);
    247 		strtab_sz += 1 + strlen(shp->path);
    248 		nhints++;
    249 	}
    250 
    251 	/* Fill hints file header */
    252 	hdr.hh_magic = HH_MAGIC;
    253 	hdr.hh_version = LD_HINTS_VERSION_1;
    254 	hdr.hh_nbucket = 1 * nhints;
    255 	n = hdr.hh_nbucket * sizeof(struct hints_bucket);
    256 	hdr.hh_hashtab = sizeof(struct hints_header);
    257 	hdr.hh_strtab = hdr.hh_hashtab + n;
    258 	hdr.hh_strtab_sz = strtab_sz;
    259 	hdr.hh_ehints = hdr.hh_strtab + hdr.hh_strtab_sz;
    260 
    261 	if (verbose)
    262 		printf("Totals: entries %d, buckets %d, string size %d\n",
    263 					nhints, hdr.hh_nbucket, strtab_sz);
    264 
    265 	/* Allocate buckets and string table */
    266 	blist = (struct hints_bucket *)xmalloc(n);
    267 	bzero((char *)blist, n);
    268 	for (i = 0; i < hdr.hh_nbucket; i++)
    269 		/* Empty all buckets */
    270 		blist[i].hi_next = -1;
    271 
    272 	strtab = (char *)xmalloc(strtab_sz);
    273 
    274 	/* Enter all */
    275 	for (shp = shlib_head; shp; shp = shp->next) {
    276 		struct hints_bucket	*bp;
    277 
    278 		bp = blist +
    279 		  (hinthash(shp->name, shp->major, shp->minor) % hdr.hh_nbucket);
    280 
    281 		if (bp->hi_pathx) {
    282 			int	i;
    283 
    284 			for (i = 0; i < hdr.hh_nbucket; i++) {
    285 				if (blist[i].hi_pathx == 0)
    286 					break;
    287 			}
    288 			if (i == hdr.hh_nbucket) {
    289 				fprintf(stderr, "Bummer!\n");
    290 				return -1;
    291 			}
    292 			while (bp->hi_next != -1)
    293 				bp = &blist[bp->hi_next];
    294 			bp->hi_next = i;
    295 			bp = blist + i;
    296 		}
    297 
    298 		/* Insert strings in string table */
    299 		bp->hi_namex = str_index;
    300 		strcpy(strtab + str_index, shp->name);
    301 		str_index += 1 + strlen(shp->name);
    302 
    303 		bp->hi_pathx = str_index;
    304 		strcpy(strtab + str_index, shp->path);
    305 		str_index += 1 + strlen(shp->path);
    306 
    307 		/* Copy versions */
    308 		bcopy(shp->dewey, bp->hi_dewey, sizeof(bp->hi_dewey));
    309 		bp->hi_ndewey = shp->ndewey;
    310 	}
    311 
    312 	tmpfile = concat(_PATH_LD_HINTS, "+", "");
    313 	if ((fd = open(tmpfile, O_RDWR|O_CREAT|O_TRUNC, 0444)) == -1) {
    314 		perror(_PATH_LD_HINTS);
    315 		return -1;
    316 	}
    317 
    318 	mywrite(&hdr, 1, sizeof(struct hints_header), fd);
    319 	mywrite(blist, hdr.hh_nbucket, sizeof(struct hints_bucket), fd);
    320 	mywrite(strtab, strtab_sz, 1, fd);
    321 
    322 	if (close(fd) != 0) {
    323 		perror(_PATH_LD_HINTS);
    324 		return -1;
    325 	}
    326 
    327 	/* Now, install real file */
    328 	if (unlink(_PATH_LD_HINTS) != 0 && errno != ENOENT) {
    329 		perror(_PATH_LD_HINTS);
    330 		return -1;
    331 	}
    332 
    333 	if (rename(tmpfile, _PATH_LD_HINTS) != 0) {
    334 		perror(_PATH_LD_HINTS);
    335 		return -1;
    336 	}
    337 
    338 	return 0;
    339 }
    340 
    341 int
    342 listhints()
    343 {
    344 	int			fd;
    345 	caddr_t			addr;
    346 	long			msize;
    347 	struct hints_header	*hdr;
    348 	struct hints_bucket	*blist;
    349 	char			*strtab;
    350 	int			i;
    351 
    352 	if ((fd = open(_PATH_LD_HINTS, O_RDONLY, 0)) == -1) {
    353 		perror(_PATH_LD_HINTS);
    354 		return -1;
    355 	}
    356 
    357 	msize = PAGSIZ;
    358 	addr = mmap(0, msize, PROT_READ, MAP_FILE|MAP_COPY, fd, 0);
    359 
    360 	if (addr == (caddr_t)-1) {
    361 		perror(_PATH_LD_HINTS);
    362 		return -1;
    363 	}
    364 
    365 	hdr = (struct hints_header *)addr;
    366 	if (HH_BADMAG(*hdr)) {
    367 		fprintf(stderr, "%s: Bad magic: %d\n");
    368 		return -1;
    369 	}
    370 
    371 	if (hdr->hh_version != LD_HINTS_VERSION_1) {
    372 		fprintf(stderr, "Unsupported version: %d\n", hdr->hh_version);
    373 		return -1;
    374 	}
    375 
    376 	if (hdr->hh_ehints > msize) {
    377 		if (mmap(addr+msize, hdr->hh_ehints - msize,
    378 				PROT_READ, MAP_FILE|MAP_COPY|MAP_FIXED,
    379 				fd, msize) != (caddr_t)(addr+msize)) {
    380 
    381 			perror(_PATH_LD_HINTS);
    382 			return -1;
    383 		}
    384 	}
    385 	close(fd);
    386 
    387 	blist = (struct hints_bucket *)(addr + hdr->hh_hashtab);
    388 	strtab = (char *)(addr + hdr->hh_strtab);
    389 
    390 	printf("%s:\n", _PATH_LD_HINTS);
    391 	for (i = 0; i < hdr->hh_nbucket; i++) {
    392 		struct hints_bucket	*bp = &blist[i];
    393 
    394 		/* Sanity check */
    395 		if (bp->hi_namex >= hdr->hh_strtab_sz) {
    396 			fprintf(stderr, "Bad name index: %#x\n", bp->hi_namex);
    397 			return -1;
    398 		}
    399 		if (bp->hi_pathx >= hdr->hh_strtab_sz) {
    400 			fprintf(stderr, "Bad path index: %#x\n", bp->hi_pathx);
    401 			return -1;
    402 		}
    403 
    404 		printf("\t-l%s.%d.%d => %s\n",
    405 			strtab + bp->hi_namex, bp->hi_major, bp->hi_minor,
    406 			strtab + bp->hi_pathx);
    407 	}
    408 
    409 	return 0;
    410 }
    411 
    412