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