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