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