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