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