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