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