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