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