xref: /freebsd/sbin/ldconfig/ldconfig.c (revision 2493aadadee50ababced80d22d8654eaaeaa7909)
1b9ae52e3SPaul Richards /*
20f6b2cb3SPaul Traina  * Copyright (c) 1993,1995 Paul Kranenburg
3b9ae52e3SPaul Richards  * All rights reserved.
4b9ae52e3SPaul Richards  *
5b9ae52e3SPaul Richards  * Redistribution and use in source and binary forms, with or without
6b9ae52e3SPaul Richards  * modification, are permitted provided that the following conditions
7b9ae52e3SPaul Richards  * are met:
8b9ae52e3SPaul Richards  * 1. Redistributions of source code must retain the above copyright
9b9ae52e3SPaul Richards  *    notice, this list of conditions and the following disclaimer.
10b9ae52e3SPaul Richards  * 2. Redistributions in binary form must reproduce the above copyright
11b9ae52e3SPaul Richards  *    notice, this list of conditions and the following disclaimer in the
12b9ae52e3SPaul Richards  *    documentation and/or other materials provided with the distribution.
13b9ae52e3SPaul Richards  * 3. All advertising materials mentioning features or use of this software
14b9ae52e3SPaul Richards  *    must display the following acknowledgement:
15b9ae52e3SPaul Richards  *      This product includes software developed by Paul Kranenburg.
16b9ae52e3SPaul Richards  * 4. The name of the author may not be used to endorse or promote products
1709e3d49dSJordan K. Hubbard  *    derived from this software without specific prior written permission
18b9ae52e3SPaul Richards  *
19b9ae52e3SPaul Richards  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20b9ae52e3SPaul Richards  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21b9ae52e3SPaul Richards  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22b9ae52e3SPaul Richards  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23b9ae52e3SPaul Richards  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24b9ae52e3SPaul Richards  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25b9ae52e3SPaul Richards  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26b9ae52e3SPaul Richards  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27b9ae52e3SPaul Richards  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28b9ae52e3SPaul Richards  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29b9ae52e3SPaul Richards  */
30b9ae52e3SPaul Richards 
31b50d7faeSPhilippe Charnier #ifndef lint
32b50d7faeSPhilippe Charnier static const char rcsid[] =
337f3dea24SPeter Wemm   "$FreeBSD$";
34b50d7faeSPhilippe Charnier #endif /* not lint */
35b50d7faeSPhilippe Charnier 
36b9ae52e3SPaul Richards #include <sys/param.h>
37b9ae52e3SPaul Richards #include <sys/types.h>
38b9ae52e3SPaul Richards #include <sys/stat.h>
39b9ae52e3SPaul Richards #include <sys/mman.h>
40b9ae52e3SPaul Richards #include <a.out.h>
41b50d7faeSPhilippe Charnier #include <ctype.h>
42b50d7faeSPhilippe Charnier #include <dirent.h>
435e6220d9SDavid E. O'Brien #include <elf-hints.h>
44b50d7faeSPhilippe Charnier #include <err.h>
45b50d7faeSPhilippe Charnier #include <errno.h>
46b50d7faeSPhilippe Charnier #include <fcntl.h>
4766422f5bSPeter Wemm #include <sys/link_aout.h>
48699e1b82SRich Murphey #include <stdio.h>
49699e1b82SRich Murphey #include <stdlib.h>
50b9ae52e3SPaul Richards #include <string.h>
51699e1b82SRich Murphey #include <unistd.h>
52b9ae52e3SPaul Richards 
53a565ca59SJohn Polstra #include "ldconfig.h"
5480c71499SPeter Wemm #include "shlib.h"
5580c71499SPeter Wemm #include "support.h"
5680c71499SPeter Wemm 
577c6da7dcSJohn Polstra #if DEBUG
587c6da7dcSJohn Polstra /* test */
597c6da7dcSJohn Polstra #undef _PATH_LD_HINTS
607c6da7dcSJohn Polstra #define _PATH_LD_HINTS		"./ld.so.hints"
61a565ca59SJohn Polstra #undef _PATH_ELF_HINTS
62a565ca59SJohn Polstra #define _PATH_ELF_HINTS		"./ld-elf.so.hints"
637c6da7dcSJohn Polstra #endif
64b9ae52e3SPaul Richards 
65c905e45dSPeter Wemm #define	_PATH_LD32_HINTS	"/var/run/ld32.so.hints"
66c905e45dSPeter Wemm #define	_PATH_ELF32_HINTS	"/var/run/ld-elf32.so.hints"
67c905e45dSPeter Wemm 
68b9ae52e3SPaul Richards #undef major
69b9ae52e3SPaul Richards #undef minor
70b9ae52e3SPaul Richards 
71b9ae52e3SPaul Richards static int			verbose;
72b9ae52e3SPaul Richards static int			nostd;
73b9ae52e3SPaul Richards static int			justread;
74f606c848SSatoshi Asami static int			merge;
75d4ba5766SPeter Wemm static int			rescan;
7606eda379SXin LI static const char		*hints_file;
77b9ae52e3SPaul Richards 
78b9ae52e3SPaul Richards struct shlib_list {
79b9ae52e3SPaul Richards 	/* Internal list of shared libraries found */
80b9ae52e3SPaul Richards 	char			*name;
81b9ae52e3SPaul Richards 	char			*path;
82b9ae52e3SPaul Richards 	int			dewey[MAXDEWEY];
83b9ae52e3SPaul Richards 	int			ndewey;
84b9ae52e3SPaul Richards #define major dewey[0]
85b9ae52e3SPaul Richards #define minor dewey[1]
86b9ae52e3SPaul Richards 	struct shlib_list	*next;
87b9ae52e3SPaul Richards };
88b9ae52e3SPaul Richards 
89b9ae52e3SPaul Richards static struct shlib_list	*shlib_head = NULL, **shlib_tail = &shlib_head;
9080c71499SPeter Wemm static char			*dir_list;
91b9ae52e3SPaul Richards 
9285429990SWarner Losh static int		buildhints(void);
9385429990SWarner Losh static int		dodir(char *, int);
9485429990SWarner Losh int			dofile(char *, int);
9585429990SWarner Losh static void		enter(char *, char *, char *, int *, int);
9685429990SWarner Losh static void		listhints(void);
9785429990SWarner Losh static int		readhints(void);
9885429990SWarner Losh static void		usage(void);
99b9ae52e3SPaul Richards 
100*2493aadaSWarner Losh /*
101*2493aadaSWarner Losh  * Note on aout/a.out support.
102*2493aadaSWarner Losh  * To properly support shared libraries for compat2x, which are a.out, we need
103*2493aadaSWarner Losh  * to support a.out here.  As of 2013, bug reports are still coming in for this
104*2493aadaSWarner Losh  * feature (on amd64 no less), so we know it is still in use.
105*2493aadaSWarner Losh  */
106*2493aadaSWarner Losh 
107b9ae52e3SPaul Richards int
10806eda379SXin LI main(int argc, char **argv)
109b9ae52e3SPaul Richards {
110b9ae52e3SPaul Richards 	int		i, c;
111b9ae52e3SPaul Richards 	int		rval = 0;
112c905e45dSPeter Wemm 	int		is_aout = 0;
113c905e45dSPeter Wemm 	int		is_32 = 0;
114b9ae52e3SPaul Richards 
115c905e45dSPeter Wemm 	while (argc > 1) {
116c905e45dSPeter Wemm 		if (strcmp(argv[1], "-aout") == 0) {
11766422f5bSPeter Wemm 			is_aout = 1;
11866422f5bSPeter Wemm 			argc--;
11966422f5bSPeter Wemm 			argv++;
120c905e45dSPeter Wemm 		} else if (strcmp(argv[1], "-elf") == 0) {
121c905e45dSPeter Wemm 			is_aout = 0;
12266422f5bSPeter Wemm 			argc--;
12366422f5bSPeter Wemm 			argv++;
124c905e45dSPeter Wemm 		} else if (strcmp(argv[1], "-32") == 0) {
125c905e45dSPeter Wemm 			is_32 = 1;
126c905e45dSPeter Wemm 			argc--;
127c905e45dSPeter Wemm 			argv++;
128c905e45dSPeter Wemm 		} else {
129c905e45dSPeter Wemm 			break;
130c905e45dSPeter Wemm 		}
13166422f5bSPeter Wemm 	}
132cfa4d739SJohn Polstra 
133c905e45dSPeter Wemm 	if (is_32)
134c905e45dSPeter Wemm 		hints_file = is_aout ? _PATH_LD32_HINTS : _PATH_ELF32_HINTS;
135c905e45dSPeter Wemm 	else
136cfa4d739SJohn Polstra 		hints_file = is_aout ? _PATH_LD_HINTS : _PATH_ELF_HINTS;
13797333b9eSJohn Polstra 	if (argc == 1)
13897333b9eSJohn Polstra 		rescan = 1;
139643dcf40SJohn Polstra 	else while((c = getopt(argc, argv, "Rf:imrsv")) != -1) {
140b9ae52e3SPaul Richards 		switch (c) {
141d4ba5766SPeter Wemm 		case 'R':
142d4ba5766SPeter Wemm 			rescan = 1;
143d4ba5766SPeter Wemm 			break;
1447c6da7dcSJohn Polstra 		case 'f':
1457c6da7dcSJohn Polstra 			hints_file = optarg;
1467c6da7dcSJohn Polstra 			break;
147643dcf40SJohn Polstra 		case 'i':
148643dcf40SJohn Polstra 			insecure = 1;
149643dcf40SJohn Polstra 			break;
150f606c848SSatoshi Asami 		case 'm':
151f606c848SSatoshi Asami 			merge = 1;
152b9ae52e3SPaul Richards 			break;
153b9ae52e3SPaul Richards 		case 'r':
154b9ae52e3SPaul Richards 			justread = 1;
155b9ae52e3SPaul Richards 			break;
156f606c848SSatoshi Asami 		case 's':
157f606c848SSatoshi Asami 			nostd = 1;
158f606c848SSatoshi Asami 			break;
159f606c848SSatoshi Asami 		case 'v':
160f606c848SSatoshi Asami 			verbose = 1;
161f606c848SSatoshi Asami 			break;
162b9ae52e3SPaul Richards 		default:
163b50d7faeSPhilippe Charnier 			usage();
164b9ae52e3SPaul Richards 			break;
165b9ae52e3SPaul Richards 		}
166b9ae52e3SPaul Richards 	}
167b9ae52e3SPaul Richards 
168cfa4d739SJohn Polstra 	if (!is_aout) {
169a565ca59SJohn Polstra 		if (justread)
170a565ca59SJohn Polstra 			list_elf_hints(hints_file);
171a565ca59SJohn Polstra 		else
172a565ca59SJohn Polstra 			update_elf_hints(hints_file, argc - optind,
173a565ca59SJohn Polstra 			    argv + optind, merge || rescan);
174a565ca59SJohn Polstra 		return 0;
175a565ca59SJohn Polstra 	}
176a565ca59SJohn Polstra 
177643dcf40SJohn Polstra 	/* Here begins the aout libs processing */
17880c71499SPeter Wemm 	dir_list = strdup("");
17980c71499SPeter Wemm 
180d4ba5766SPeter Wemm 	if (justread || merge || rescan) {
181f606c848SSatoshi Asami 		if ((rval = readhints()) != 0)
182f606c848SSatoshi Asami 			return rval;
18380c71499SPeter Wemm 	}
18480c71499SPeter Wemm 
185d4ba5766SPeter Wemm 	if (!nostd && !merge && !rescan)
18680c71499SPeter Wemm 		std_search_path();
18780c71499SPeter Wemm 
188571b472bSJordan K. Hubbard 	/* Add any directories/files from the command line */
189571b472bSJordan K. Hubbard 	if (!justread) {
190d66f9d22SJohn Polstra 		for (i = optind; i < argc; i++) {
191571b472bSJordan K. Hubbard 			struct stat stbuf;
192571b472bSJordan K. Hubbard 
193571b472bSJordan K. Hubbard 			if (stat(argv[i], &stbuf) == -1) {
194d66f9d22SJohn Polstra 				warn("%s", argv[i]);
195d66f9d22SJohn Polstra 				rval = -1;
196614d19caSJohn Polstra 			} else if (strcmp(argv[i], "/usr/lib") == 0) {
197cabb97dcSSøren Schmidt 				warnx("WARNING! '%s' can not be used", argv[i]);
198cabb97dcSSøren Schmidt 				rval = -1;
199614d19caSJohn Polstra 			} else {
200571b472bSJordan K. Hubbard 				/*
201571b472bSJordan K. Hubbard 				 * See if this is a directory-containing
202571b472bSJordan K. Hubbard 				 * file instead of a directory
203571b472bSJordan K. Hubbard 				 */
204571b472bSJordan K. Hubbard 				if (S_ISREG(stbuf.st_mode))
205571b472bSJordan K. Hubbard 					rval |= dofile(argv[i], 0);
206571b472bSJordan K. Hubbard 				else
20780c71499SPeter Wemm 					add_search_path(argv[i]);
208d66f9d22SJohn Polstra 			}
209d66f9d22SJohn Polstra 		}
210571b472bSJordan K. Hubbard 	}
21180c71499SPeter Wemm 
21280c71499SPeter Wemm 	for (i = 0; i < n_search_dirs; i++) {
21380c71499SPeter Wemm 		char *cp = concat(dir_list, *dir_list?":":"", search_dirs[i]);
21480c71499SPeter Wemm 		free(dir_list);
21580c71499SPeter Wemm 		dir_list = cp;
21680c71499SPeter Wemm 	}
21780c71499SPeter Wemm 
218f606c848SSatoshi Asami 	if (justread) {
219f606c848SSatoshi Asami 		listhints();
220526195adSJordan K. Hubbard 		return 0;
221f606c848SSatoshi Asami 	}
222b9ae52e3SPaul Richards 
223b9ae52e3SPaul Richards 	for (i = 0; i < n_search_dirs; i++)
22409e3d49dSJordan K. Hubbard 		rval |= dodir(search_dirs[i], 1);
225b9ae52e3SPaul Richards 
226f606c848SSatoshi Asami 	rval |= buildhints();
227b9ae52e3SPaul Richards 
228b9ae52e3SPaul Richards 	return rval;
229b9ae52e3SPaul Richards }
230b9ae52e3SPaul Richards 
231b50d7faeSPhilippe Charnier static void
232f709df34SEd Schouten usage(void)
233b50d7faeSPhilippe Charnier {
234b50d7faeSPhilippe Charnier 	fprintf(stderr,
2356efabadaSJohn Baldwin 	"usage: ldconfig [-32] [-aout | -elf] [-Rimrsv] [-f hints_file] [directory | file ...]\n");
236b50d7faeSPhilippe Charnier 	exit(1);
237b50d7faeSPhilippe Charnier }
238b50d7faeSPhilippe Charnier 
239b9ae52e3SPaul Richards int
240f709df34SEd Schouten dofile(char *fname, int silent)
241571b472bSJordan K. Hubbard {
242571b472bSJordan K. Hubbard 	FILE *hfp;
243571b472bSJordan K. Hubbard 	char buf[MAXPATHLEN];
244571b472bSJordan K. Hubbard 	int rval = 0;
245571b472bSJordan K. Hubbard 	char *cp, *sp;
246571b472bSJordan K. Hubbard 
247571b472bSJordan K. Hubbard 	if ((hfp = fopen(fname, "r")) == NULL) {
248571b472bSJordan K. Hubbard 		warn("%s", fname);
249571b472bSJordan K. Hubbard 		return -1;
250571b472bSJordan K. Hubbard 	}
251571b472bSJordan K. Hubbard 
252571b472bSJordan K. Hubbard 	while (fgets(buf, sizeof(buf), hfp)) {
253571b472bSJordan K. Hubbard 		cp = buf;
254571b472bSJordan K. Hubbard 		while (isspace(*cp))
255571b472bSJordan K. Hubbard 			cp++;
256571b472bSJordan K. Hubbard 		if (*cp == '#' || *cp == '\0')
257571b472bSJordan K. Hubbard 			continue;
258571b472bSJordan K. Hubbard 		sp = cp;
259571b472bSJordan K. Hubbard 		while (!isspace(*cp) && *cp != '\0')
260571b472bSJordan K. Hubbard 			cp++;
261571b472bSJordan K. Hubbard 
262571b472bSJordan K. Hubbard 		if (*cp != '\n') {
263571b472bSJordan K. Hubbard 			*cp = '\0';
264b50d7faeSPhilippe Charnier 			warnx("%s: trailing characters ignored", sp);
265571b472bSJordan K. Hubbard 		}
266571b472bSJordan K. Hubbard 
267571b472bSJordan K. Hubbard 		*cp = '\0';
268571b472bSJordan K. Hubbard 
269571b472bSJordan K. Hubbard 		rval |= dodir(sp, silent);
270571b472bSJordan K. Hubbard 	}
271571b472bSJordan K. Hubbard 
272571b472bSJordan K. Hubbard 	(void)fclose(hfp);
273571b472bSJordan K. Hubbard 	return rval;
274571b472bSJordan K. Hubbard }
275571b472bSJordan K. Hubbard 
276571b472bSJordan K. Hubbard int
277f709df34SEd Schouten dodir(char *dir, int silent)
278b9ae52e3SPaul Richards {
279b9ae52e3SPaul Richards 	DIR		*dd;
280b9ae52e3SPaul Richards 	struct dirent	*dp;
2810f6b2cb3SPaul Traina 	char		name[MAXPATHLEN];
282b9ae52e3SPaul Richards 	int		dewey[MAXDEWEY], ndewey;
283b9ae52e3SPaul Richards 
284b9ae52e3SPaul Richards 	if ((dd = opendir(dir)) == NULL) {
285d66f9d22SJohn Polstra 		if (silent && errno == ENOENT)	/* Ignore the error */
286d66f9d22SJohn Polstra 			return 0;
287f606c848SSatoshi Asami 		warn("%s", dir);
288b9ae52e3SPaul Richards 		return -1;
289b9ae52e3SPaul Richards 	}
290b9ae52e3SPaul Richards 
291b9ae52e3SPaul Richards 	while ((dp = readdir(dd)) != NULL) {
2923d438ad6SDavid E. O'Brien 		int n;
2933d438ad6SDavid E. O'Brien 		char *cp;
294b9ae52e3SPaul Richards 
2950f6b2cb3SPaul Traina 		/* Check for `lib' prefix */
2960f6b2cb3SPaul Traina 		if (dp->d_name[0] != 'l' ||
2970f6b2cb3SPaul Traina 		    dp->d_name[1] != 'i' ||
2980f6b2cb3SPaul Traina 		    dp->d_name[2] != 'b')
299b9ae52e3SPaul Richards 			continue;
300b9ae52e3SPaul Richards 
3010f6b2cb3SPaul Traina 		/* Copy the entry minus prefix */
3020f6b2cb3SPaul Traina 		(void)strcpy(name, dp->d_name + 3);
3030f6b2cb3SPaul Traina 		n = strlen(name);
3040f6b2cb3SPaul Traina 		if (n < 4)
3050f6b2cb3SPaul Traina 			continue;
3060f6b2cb3SPaul Traina 
3070f6b2cb3SPaul Traina 		/* Find ".so." in name */
3080f6b2cb3SPaul Traina 		for (cp = name + n - 4; cp > name; --cp) {
3090f6b2cb3SPaul Traina 			if (cp[0] == '.' &&
3100f6b2cb3SPaul Traina 			    cp[1] == 's' &&
3110f6b2cb3SPaul Traina 			    cp[2] == 'o' &&
3120f6b2cb3SPaul Traina 			    cp[3] == '.')
3130f6b2cb3SPaul Traina 				break;
3140f6b2cb3SPaul Traina 		}
3150f6b2cb3SPaul Traina 		if (cp <= name)
3160f6b2cb3SPaul Traina 			continue;
3170f6b2cb3SPaul Traina 
3180f6b2cb3SPaul Traina 		*cp = '\0';
3190f6b2cb3SPaul Traina 		if (!isdigit(*(cp+4)))
3200f6b2cb3SPaul Traina 			continue;
3210f6b2cb3SPaul Traina 
3220f6b2cb3SPaul Traina 		bzero((caddr_t)dewey, sizeof(dewey));
3230f6b2cb3SPaul Traina 		ndewey = getdewey(dewey, cp + 4);
3245f8d88ddSJohn Polstra 		if (ndewey < 2)
3255f8d88ddSJohn Polstra 			continue;
326b9ae52e3SPaul Richards 		enter(dir, dp->d_name, name, dewey, ndewey);
327b9ae52e3SPaul Richards 	}
328b9ae52e3SPaul Richards 
329571b472bSJordan K. Hubbard 	closedir(dd);
330b9ae52e3SPaul Richards 	return 0;
331b9ae52e3SPaul Richards }
332b9ae52e3SPaul Richards 
333b9ae52e3SPaul Richards static void
334f709df34SEd Schouten enter(char *dir, char *file, char *name, int dewey[], int ndewey)
335b9ae52e3SPaul Richards {
336b9ae52e3SPaul Richards 	struct shlib_list	*shp;
337b9ae52e3SPaul Richards 
338b9ae52e3SPaul Richards 	for (shp = shlib_head; shp; shp = shp->next) {
339b9ae52e3SPaul Richards 		if (strcmp(name, shp->name) != 0 || major != shp->major)
340b9ae52e3SPaul Richards 			continue;
341b9ae52e3SPaul Richards 
342b9ae52e3SPaul Richards 		/* Name matches existing entry */
343b9ae52e3SPaul Richards 		if (cmpndewey(dewey, ndewey, shp->dewey, shp->ndewey) > 0) {
344b9ae52e3SPaul Richards 
345b9ae52e3SPaul Richards 			/* Update this entry with higher versioned lib */
346b9ae52e3SPaul Richards 			if (verbose)
347b9ae52e3SPaul Richards 				printf("Updating lib%s.%d.%d to %s/%s\n",
348b9ae52e3SPaul Richards 					shp->name, shp->major, shp->minor,
349b9ae52e3SPaul Richards 					dir, file);
350b9ae52e3SPaul Richards 
351b9ae52e3SPaul Richards 			free(shp->name);
352b9ae52e3SPaul Richards 			shp->name = strdup(name);
353b9ae52e3SPaul Richards 			free(shp->path);
354b9ae52e3SPaul Richards 			shp->path = concat(dir, "/", file);
355b9ae52e3SPaul Richards 			bcopy(dewey, shp->dewey, sizeof(shp->dewey));
356b9ae52e3SPaul Richards 			shp->ndewey = ndewey;
357b9ae52e3SPaul Richards 		}
358b9ae52e3SPaul Richards 		break;
359b9ae52e3SPaul Richards 	}
360b9ae52e3SPaul Richards 
361b9ae52e3SPaul Richards 	if (shp)
362b9ae52e3SPaul Richards 		/* Name exists: older version or just updated */
363b9ae52e3SPaul Richards 		return;
364b9ae52e3SPaul Richards 
365b9ae52e3SPaul Richards 	/* Allocate new list element */
366b9ae52e3SPaul Richards 	if (verbose)
367b9ae52e3SPaul Richards 		printf("Adding %s/%s\n", dir, file);
368b9ae52e3SPaul Richards 
369b9ae52e3SPaul Richards 	shp = (struct shlib_list *)xmalloc(sizeof *shp);
370b9ae52e3SPaul Richards 	shp->name = strdup(name);
371b9ae52e3SPaul Richards 	shp->path = concat(dir, "/", file);
3727d85b9aaSColin Percival 	bcopy(dewey, shp->dewey, sizeof(shp->dewey));
373b9ae52e3SPaul Richards 	shp->ndewey = ndewey;
374b9ae52e3SPaul Richards 	shp->next = NULL;
375b9ae52e3SPaul Richards 
376b9ae52e3SPaul Richards 	*shlib_tail = shp;
377b9ae52e3SPaul Richards 	shlib_tail = &shp->next;
378b9ae52e3SPaul Richards }
379b9ae52e3SPaul Richards 
380b9ae52e3SPaul Richards 
38106eda379SXin LI static int
38206eda379SXin LI hinthash(char *cp, int vmajor)
383b9ae52e3SPaul Richards {
384b9ae52e3SPaul Richards 	int	k = 0;
385b9ae52e3SPaul Richards 
386b9ae52e3SPaul Richards 	while (*cp)
387b9ae52e3SPaul Richards 		k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
388b9ae52e3SPaul Richards 
389b9ae52e3SPaul Richards 	k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff;
390b9ae52e3SPaul Richards 
391b9ae52e3SPaul Richards 	return k;
392b9ae52e3SPaul Richards }
393b9ae52e3SPaul Richards 
394b9ae52e3SPaul Richards int
395f709df34SEd Schouten buildhints(void)
396b9ae52e3SPaul Richards {
397b9ae52e3SPaul Richards 	struct hints_header	hdr;
398b9ae52e3SPaul Richards 	struct hints_bucket	*blist;
399b9ae52e3SPaul Richards 	struct shlib_list	*shp;
400b9ae52e3SPaul Richards 	char			*strtab;
401b9ae52e3SPaul Richards 	int			i, n, str_index = 0;
402b9ae52e3SPaul Richards 	int			strtab_sz = 0;	/* Total length of strings */
403b9ae52e3SPaul Richards 	int			nhints = 0;	/* Total number of hints */
404b9ae52e3SPaul Richards 	int			fd;
405665fc054SXin LI 	char			*tmpfilename;
406b9ae52e3SPaul Richards 
407b9ae52e3SPaul Richards 	for (shp = shlib_head; shp; shp = shp->next) {
408b9ae52e3SPaul Richards 		strtab_sz += 1 + strlen(shp->name);
409b9ae52e3SPaul Richards 		strtab_sz += 1 + strlen(shp->path);
410b9ae52e3SPaul Richards 		nhints++;
411b9ae52e3SPaul Richards 	}
412b9ae52e3SPaul Richards 
413b9ae52e3SPaul Richards 	/* Fill hints file header */
414b9ae52e3SPaul Richards 	hdr.hh_magic = HH_MAGIC;
41580c71499SPeter Wemm 	hdr.hh_version = LD_HINTS_VERSION_2;
416b9ae52e3SPaul Richards 	hdr.hh_nbucket = 1 * nhints;
417b9ae52e3SPaul Richards 	n = hdr.hh_nbucket * sizeof(struct hints_bucket);
418b9ae52e3SPaul Richards 	hdr.hh_hashtab = sizeof(struct hints_header);
419b9ae52e3SPaul Richards 	hdr.hh_strtab = hdr.hh_hashtab + n;
42080c71499SPeter Wemm 	hdr.hh_dirlist = strtab_sz;
42180c71499SPeter Wemm 	strtab_sz += 1 + strlen(dir_list);
422b9ae52e3SPaul Richards 	hdr.hh_strtab_sz = strtab_sz;
423b9ae52e3SPaul Richards 	hdr.hh_ehints = hdr.hh_strtab + hdr.hh_strtab_sz;
424b9ae52e3SPaul Richards 
425b9ae52e3SPaul Richards 	if (verbose)
426526195adSJordan K. Hubbard 		printf("Totals: entries %d, buckets %ld, string size %d\n",
427ab845347SBruce Evans 			nhints, (long)hdr.hh_nbucket, strtab_sz);
428b9ae52e3SPaul Richards 
429b9ae52e3SPaul Richards 	/* Allocate buckets and string table */
430b9ae52e3SPaul Richards 	blist = (struct hints_bucket *)xmalloc(n);
431b9ae52e3SPaul Richards 	bzero((char *)blist, n);
432b9ae52e3SPaul Richards 	for (i = 0; i < hdr.hh_nbucket; i++)
433b9ae52e3SPaul Richards 		/* Empty all buckets */
434b9ae52e3SPaul Richards 		blist[i].hi_next = -1;
435b9ae52e3SPaul Richards 
436b9ae52e3SPaul Richards 	strtab = (char *)xmalloc(strtab_sz);
437b9ae52e3SPaul Richards 
438b9ae52e3SPaul Richards 	/* Enter all */
439b9ae52e3SPaul Richards 	for (shp = shlib_head; shp; shp = shp->next) {
440b9ae52e3SPaul Richards 		struct hints_bucket	*bp;
441b9ae52e3SPaul Richards 
442b9ae52e3SPaul Richards 		bp = blist +
443d5453ba5SJoerg Wunsch 		  (hinthash(shp->name, shp->major) % hdr.hh_nbucket);
444b9ae52e3SPaul Richards 
445b9ae52e3SPaul Richards 		if (bp->hi_pathx) {
44606eda379SXin LI 			int	j;
447b9ae52e3SPaul Richards 
44806eda379SXin LI 			for (j = 0; j < hdr.hh_nbucket; j++) {
44906eda379SXin LI 				if (blist[j].hi_pathx == 0)
450b9ae52e3SPaul Richards 					break;
451b9ae52e3SPaul Richards 			}
45206eda379SXin LI 			if (j == hdr.hh_nbucket) {
453b50d7faeSPhilippe Charnier 				warnx("bummer!");
454b9ae52e3SPaul Richards 				return -1;
455b9ae52e3SPaul Richards 			}
456b9ae52e3SPaul Richards 			while (bp->hi_next != -1)
457b9ae52e3SPaul Richards 				bp = &blist[bp->hi_next];
45806eda379SXin LI 			bp->hi_next = j;
45906eda379SXin LI 			bp = blist + j;
460b9ae52e3SPaul Richards 		}
461b9ae52e3SPaul Richards 
462b9ae52e3SPaul Richards 		/* Insert strings in string table */
463b9ae52e3SPaul Richards 		bp->hi_namex = str_index;
464b9ae52e3SPaul Richards 		strcpy(strtab + str_index, shp->name);
465b9ae52e3SPaul Richards 		str_index += 1 + strlen(shp->name);
466b9ae52e3SPaul Richards 
467b9ae52e3SPaul Richards 		bp->hi_pathx = str_index;
468b9ae52e3SPaul Richards 		strcpy(strtab + str_index, shp->path);
469b9ae52e3SPaul Richards 		str_index += 1 + strlen(shp->path);
470b9ae52e3SPaul Richards 
471b9ae52e3SPaul Richards 		/* Copy versions */
472b9ae52e3SPaul Richards 		bcopy(shp->dewey, bp->hi_dewey, sizeof(bp->hi_dewey));
473b9ae52e3SPaul Richards 		bp->hi_ndewey = shp->ndewey;
474b9ae52e3SPaul Richards 	}
475b9ae52e3SPaul Richards 
47680c71499SPeter Wemm 	/* Copy search directories */
47780c71499SPeter Wemm 	strcpy(strtab + str_index, dir_list);
47880c71499SPeter Wemm 	str_index += 1 + strlen(dir_list);
47980c71499SPeter Wemm 
48080c71499SPeter Wemm 	/* Sanity check */
48180c71499SPeter Wemm 	if (str_index != strtab_sz) {
48280c71499SPeter Wemm 		errx(1, "str_index(%d) != strtab_sz(%d)", str_index, strtab_sz);
48380c71499SPeter Wemm 	}
48480c71499SPeter Wemm 
485665fc054SXin LI 	tmpfilename = concat(hints_file, ".XXXXXXXXXX", "");
48616bd17ceSKris Kennaway 	umask(0);	/* Create with exact permissions */
487665fc054SXin LI 	if ((fd = mkstemp(tmpfilename)) == -1) {
488665fc054SXin LI 		warn("%s", tmpfilename);
48980c71499SPeter Wemm 		return -1;
49080c71499SPeter Wemm 	}
49116bd17ceSKris Kennaway 	fchmod(fd, 0444);
492b9ae52e3SPaul Richards 
49309e3d49dSJordan K. Hubbard 	if (write(fd, &hdr, sizeof(struct hints_header)) !=
49409e3d49dSJordan K. Hubbard 						sizeof(struct hints_header)) {
4957c6da7dcSJohn Polstra 		warn("%s", hints_file);
49609e3d49dSJordan K. Hubbard 		return -1;
49709e3d49dSJordan K. Hubbard 	}
49874b67e84SXin LI 	if (write(fd, blist, hdr.hh_nbucket * sizeof(*blist)) !=
49974b67e84SXin LI 				(ssize_t)(hdr.hh_nbucket * sizeof(*blist))) {
5007c6da7dcSJohn Polstra 		warn("%s", hints_file);
50109e3d49dSJordan K. Hubbard 		return -1;
50209e3d49dSJordan K. Hubbard 	}
50309e3d49dSJordan K. Hubbard 	if (write(fd, strtab, strtab_sz) != strtab_sz) {
5047c6da7dcSJohn Polstra 		warn("%s", hints_file);
50509e3d49dSJordan K. Hubbard 		return -1;
50609e3d49dSJordan K. Hubbard 	}
507b9ae52e3SPaul Richards 	if (close(fd) != 0) {
5087c6da7dcSJohn Polstra 		warn("%s", hints_file);
509b9ae52e3SPaul Richards 		return -1;
510b9ae52e3SPaul Richards 	}
511b9ae52e3SPaul Richards 
51209e3d49dSJordan K. Hubbard 	/* Install it */
5137c6da7dcSJohn Polstra 	if (unlink(hints_file) != 0 && errno != ENOENT) {
5147c6da7dcSJohn Polstra 		warn("%s", hints_file);
515b9ae52e3SPaul Richards 		return -1;
516b9ae52e3SPaul Richards 	}
517b9ae52e3SPaul Richards 
518665fc054SXin LI 	if (rename(tmpfilename, hints_file) != 0) {
5197c6da7dcSJohn Polstra 		warn("%s", hints_file);
520b9ae52e3SPaul Richards 		return -1;
521b9ae52e3SPaul Richards 	}
522b9ae52e3SPaul Richards 
523b9ae52e3SPaul Richards 	return 0;
524b9ae52e3SPaul Richards }
525b9ae52e3SPaul Richards 
526699e1b82SRich Murphey static int
527f709df34SEd Schouten readhints(void)
528b9ae52e3SPaul Richards {
529b9ae52e3SPaul Richards 	int			fd;
530614d19caSJohn Polstra 	void			*addr;
531614d19caSJohn Polstra 	long			fsize;
532b9ae52e3SPaul Richards 	long			msize;
533b9ae52e3SPaul Richards 	struct hints_header	*hdr;
534b9ae52e3SPaul Richards 	struct hints_bucket	*blist;
535b9ae52e3SPaul Richards 	char			*strtab;
536f606c848SSatoshi Asami 	struct shlib_list	*shp;
537b9ae52e3SPaul Richards 	int			i;
538b9ae52e3SPaul Richards 
5397c6da7dcSJohn Polstra 	if ((fd = open(hints_file, O_RDONLY, 0)) == -1) {
5407c6da7dcSJohn Polstra 		warn("%s", hints_file);
541b9ae52e3SPaul Richards 		return -1;
542b9ae52e3SPaul Richards 	}
543b9ae52e3SPaul Richards 
54480c71499SPeter Wemm 	msize = PAGE_SIZE;
54561f9ce8dSNate Williams 	addr = mmap(0, msize, PROT_READ, MAP_COPY, fd, 0);
546b9ae52e3SPaul Richards 
547614d19caSJohn Polstra 	if (addr == MAP_FAILED) {
5487c6da7dcSJohn Polstra 		warn("%s", hints_file);
549b9ae52e3SPaul Richards 		return -1;
550b9ae52e3SPaul Richards 	}
551b9ae52e3SPaul Richards 
552b9ae52e3SPaul Richards 	hdr = (struct hints_header *)addr;
553b9ae52e3SPaul Richards 	if (HH_BADMAG(*hdr)) {
554ab845347SBruce Evans 		warnx("%s: bad magic: %lo", hints_file,
555ab845347SBruce Evans 			(unsigned long)hdr->hh_magic);
556b9ae52e3SPaul Richards 		return -1;
557b9ae52e3SPaul Richards 	}
558b9ae52e3SPaul Richards 
55980c71499SPeter Wemm 	if (hdr->hh_version != LD_HINTS_VERSION_1 &&
56080c71499SPeter Wemm 	    hdr->hh_version != LD_HINTS_VERSION_2) {
561ab845347SBruce Evans 		warnx("unsupported version: %ld", (long)hdr->hh_version);
562b9ae52e3SPaul Richards 		return -1;
563b9ae52e3SPaul Richards 	}
564b9ae52e3SPaul Richards 
565b9ae52e3SPaul Richards 	if (hdr->hh_ehints > msize) {
566614d19caSJohn Polstra 		fsize = hdr->hh_ehints;
567614d19caSJohn Polstra 		munmap(addr, msize);
568614d19caSJohn Polstra 		addr = mmap(0, fsize, PROT_READ, MAP_COPY, fd, 0);
569614d19caSJohn Polstra 		if (addr == MAP_FAILED) {
5707c6da7dcSJohn Polstra 			warn("%s", hints_file);
571b9ae52e3SPaul Richards 			return -1;
572b9ae52e3SPaul Richards 		}
573614d19caSJohn Polstra 		hdr = (struct hints_header *)addr;
574b9ae52e3SPaul Richards 	}
575b9ae52e3SPaul Richards 	close(fd);
576b9ae52e3SPaul Richards 
5774eae39bfSStefan Farfeleder 	strtab = (char *)addr + hdr->hh_strtab;
578b9ae52e3SPaul Richards 
579d4ba5766SPeter Wemm 	if (hdr->hh_version >= LD_HINTS_VERSION_2)
580d4ba5766SPeter Wemm 		add_search_path(strtab + hdr->hh_dirlist);
581d4ba5766SPeter Wemm 	else if (rescan)
582d4ba5766SPeter Wemm 		errx(1, "%s too old and does not contain the search path",
583d4ba5766SPeter Wemm 			hints_file);
584d4ba5766SPeter Wemm 
585d4ba5766SPeter Wemm 	if (rescan)
586d4ba5766SPeter Wemm 		return 0;
587d4ba5766SPeter Wemm 
58874b67e84SXin LI 	blist = malloc(sizeof(*blist) * hdr->hh_nbucket);
58906eda379SXin LI 	if (blist == NULL)
59006eda379SXin LI 		err(1, "readhints");
59106eda379SXin LI 	memcpy(blist, (char *)addr + hdr->hh_hashtab,
59274b67e84SXin LI 		sizeof(*blist) * hdr->hh_nbucket);
59306eda379SXin LI 
59406eda379SXin LI 
595b9ae52e3SPaul Richards 	for (i = 0; i < hdr->hh_nbucket; i++) {
596b9ae52e3SPaul Richards 		struct hints_bucket	*bp = &blist[i];
597b9ae52e3SPaul Richards 
598b9ae52e3SPaul Richards 		/* Sanity check */
599b9ae52e3SPaul Richards 		if (bp->hi_namex >= hdr->hh_strtab_sz) {
600b50d7faeSPhilippe Charnier 			warnx("bad name index: %#x", bp->hi_namex);
60106eda379SXin LI 			free(blist);
602b9ae52e3SPaul Richards 			return -1;
603b9ae52e3SPaul Richards 		}
604b9ae52e3SPaul Richards 		if (bp->hi_pathx >= hdr->hh_strtab_sz) {
605b50d7faeSPhilippe Charnier 			warnx("bad path index: %#x", bp->hi_pathx);
60606eda379SXin LI 			free(blist);
607b9ae52e3SPaul Richards 			return -1;
608b9ae52e3SPaul Richards 		}
609b9ae52e3SPaul Richards 
610f606c848SSatoshi Asami 		/* Allocate new list element */
611f606c848SSatoshi Asami 		shp = (struct shlib_list *)xmalloc(sizeof *shp);
612f606c848SSatoshi Asami 		shp->name = strdup(strtab + bp->hi_namex);
613f606c848SSatoshi Asami 		shp->path = strdup(strtab + bp->hi_pathx);
614f606c848SSatoshi Asami 		bcopy(bp->hi_dewey, shp->dewey, sizeof(shp->dewey));
615f606c848SSatoshi Asami 		shp->ndewey = bp->hi_ndewey;
616f606c848SSatoshi Asami 		shp->next = NULL;
617f606c848SSatoshi Asami 
618f606c848SSatoshi Asami 		*shlib_tail = shp;
619f606c848SSatoshi Asami 		shlib_tail = &shp->next;
620b9ae52e3SPaul Richards 	}
621b9ae52e3SPaul Richards 
62206eda379SXin LI 	free(blist);
623b9ae52e3SPaul Richards 	return 0;
624b9ae52e3SPaul Richards }
625b9ae52e3SPaul Richards 
626f606c848SSatoshi Asami static void
627f709df34SEd Schouten listhints(void)
628f606c848SSatoshi Asami {
629f606c848SSatoshi Asami 	struct shlib_list	*shp;
630f606c848SSatoshi Asami 	int			i;
631f606c848SSatoshi Asami 
6327c6da7dcSJohn Polstra 	printf("%s:\n", hints_file);
63380c71499SPeter Wemm 	printf("\tsearch directories: %s\n", dir_list);
634f606c848SSatoshi Asami 
635f606c848SSatoshi Asami 	for (i = 0, shp = shlib_head; shp; i++, shp = shp->next)
636f606c848SSatoshi Asami 		printf("\t%d:-l%s.%d.%d => %s\n",
637f606c848SSatoshi Asami 			i, shp->name, shp->major, shp->minor, shp->path);
638f606c848SSatoshi Asami 
639f606c848SSatoshi Asami 	return;
640f606c848SSatoshi Asami }
641