ldconfig.c revision 1.21 1 /* $NetBSD: ldconfig.c,v 1.21 1998/12/15 22:27:14 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 <ctype.h>
46 #include <dirent.h>
47 #include <err.h>
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54 #include <link.h>
55
56 #include "ld.h"
57
58 #define _PATH_LD_SO_CONF "/etc/ld.so.conf"
59
60 #undef major
61 #undef minor
62
63 extern char *__progname;
64
65 static int verbose;
66 static int nostd;
67 static int noconf;
68 static int justread;
69 static int merge;
70
71 struct shlib_list {
72 /* Internal list of shared libraries found */
73 char *name;
74 char *path;
75 int dewey[MAXDEWEY];
76 int ndewey;
77 #define major dewey[0]
78 #define minor dewey[1]
79 struct shlib_list *next;
80 };
81
82 static struct shlib_list *shlib_head = NULL, **shlib_tail = &shlib_head;
83 static char *dir_list;
84
85 static void enter __P((char *, char *, char *, int *, int));
86 static int dodir __P((char *, int, int));
87 static int do_conf __P((void));
88 static int buildhints __P((void));
89 static int readhints __P((void));
90 static void listhints __P((void));
91 static int hinthash __P((char *, int, int));
92 int main __P((int, char *[]));
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 (-1);
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 int n;
229 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 /* XXX - should be a common function with rtld.c */
323 int
324 hinthash(cp, vmajor, vminor)
325 char *cp;
326 int vmajor, vminor;
327 {
328 int k = 0;
329
330 while (*cp)
331 k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
332
333 k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff;
334 #if 0
335 k = (((k << 1) + (k >> 14)) ^ (vminor*167)) & 0x3fff;
336 #endif
337
338 return (k);
339 }
340
341 int
342 buildhints()
343 {
344 struct hints_header hdr;
345 struct hints_bucket *blist;
346 struct shlib_list *shp;
347 char *strtab;
348 int i, n, str_index = 0;
349 int strtab_sz = 0; /* Total length of strings */
350 int nhints = 0; /* Total number of hints */
351 int fd;
352 char *tmpfile;
353
354 for (shp = shlib_head; shp; shp = shp->next) {
355 strtab_sz += 1 + strlen(shp->name);
356 strtab_sz += 1 + strlen(shp->path);
357 nhints++;
358 }
359
360 /* Fill hints file header */
361 hdr.hh_magic = HH_MAGIC;
362 hdr.hh_version = LD_HINTS_VERSION_2;
363 hdr.hh_nbucket = 1 * nhints;
364 n = hdr.hh_nbucket * sizeof(struct hints_bucket);
365 hdr.hh_hashtab = sizeof(struct hints_header);
366 hdr.hh_strtab = hdr.hh_hashtab + n;
367 hdr.hh_dirlist = strtab_sz;
368 strtab_sz += 1 + strlen(dir_list);
369 hdr.hh_strtab_sz = strtab_sz;
370 hdr.hh_ehints = hdr.hh_strtab + hdr.hh_strtab_sz;
371
372 if (verbose)
373 printf("Totals: entries %d, buckets %ld, string size %d\n",
374 nhints, hdr.hh_nbucket, strtab_sz);
375
376 /* Allocate buckets and string table */
377 blist = (struct hints_bucket *)xmalloc(n);
378 bzero((char *)blist, n);
379 for (i = 0; i < hdr.hh_nbucket; i++)
380 /* Empty all buckets */
381 blist[i].hi_next = -1;
382
383 strtab = (char *)xmalloc(strtab_sz);
384
385 /* Enter all */
386 for (shp = shlib_head; shp; shp = shp->next) {
387 struct hints_bucket *bp;
388
389 bp = blist +
390 (hinthash(shp->name, shp->major, shp->minor) % hdr.hh_nbucket);
391
392 if (bp->hi_pathx) {
393 int i;
394
395 for (i = 0; i < hdr.hh_nbucket; i++) {
396 if (blist[i].hi_pathx == 0)
397 break;
398 }
399 if (i == hdr.hh_nbucket) {
400 warnx("Bummer!");
401 return (-1);
402 }
403 while (bp->hi_next != -1)
404 bp = &blist[bp->hi_next];
405 bp->hi_next = i;
406 bp = blist + i;
407 }
408
409 /* Insert strings in string table */
410 bp->hi_namex = str_index;
411 strcpy(strtab + str_index, shp->name);
412 str_index += 1 + strlen(shp->name);
413
414 bp->hi_pathx = str_index;
415 strcpy(strtab + str_index, shp->path);
416 str_index += 1 + strlen(shp->path);
417
418 /* Copy versions */
419 bcopy(shp->dewey, bp->hi_dewey, sizeof(bp->hi_dewey));
420 bp->hi_ndewey = shp->ndewey;
421 }
422
423 /* Copy search directories */
424 strcpy(strtab + str_index, dir_list);
425 str_index += 1 + strlen(dir_list);
426
427 /* Sanity check */
428 if (str_index != strtab_sz) {
429 errx(1, "str_index(%d) != strtab_sz(%d)", str_index, strtab_sz);
430 }
431
432 tmpfile = concat(_PATH_LD_HINTS, ".XXXXXX", "");
433 if ((fd = mkstemp(tmpfile)) == -1) {
434 warn("%s", tmpfile);
435 return (-1);
436 }
437
438 if (write(fd, &hdr, sizeof(struct hints_header)) !=
439 sizeof(struct hints_header)) {
440 warn("%s", _PATH_LD_HINTS);
441 return (-1);
442 }
443 if (write(fd, blist, hdr.hh_nbucket * sizeof(struct hints_bucket)) !=
444 hdr.hh_nbucket * sizeof(struct hints_bucket)) {
445 warn("%s", _PATH_LD_HINTS);
446 return (-1);
447 }
448 if (write(fd, strtab, strtab_sz) != strtab_sz) {
449 warn("%s", _PATH_LD_HINTS);
450 return (-1);
451 }
452 if (fchmod(fd, 0444) == -1) {
453 warn("%s", _PATH_LD_HINTS);
454 return (-1);
455 }
456 if (close(fd) != 0) {
457 warn("%s", _PATH_LD_HINTS);
458 return (-1);
459 }
460
461 /* Install it */
462 if (unlink(_PATH_LD_HINTS) != 0 && errno != ENOENT) {
463 warn("%s", _PATH_LD_HINTS);
464 return (-1);
465 }
466
467 if (rename(tmpfile, _PATH_LD_HINTS) != 0) {
468 warn("%s", _PATH_LD_HINTS);
469 return (-1);
470 }
471
472 return (0);
473 }
474
475 static int
476 readhints()
477 {
478 int fd;
479 caddr_t addr;
480 long msize;
481 struct hints_header *hdr;
482 struct hints_bucket *blist;
483 char *strtab;
484 struct shlib_list *shp;
485 int i;
486
487 if ((fd = open(_PATH_LD_HINTS, O_RDONLY, 0)) == -1) {
488 warn("%s", _PATH_LD_HINTS);
489 return (-1);
490 }
491
492 msize = getpagesize();
493 addr = mmap(0, msize, PROT_READ, MAP_FILE|MAP_COPY, fd, 0);
494
495 if (addr == (caddr_t)-1) {
496 warn("%s", _PATH_LD_HINTS);
497 return (-1);
498 }
499
500 hdr = (struct hints_header *)addr;
501 if (HH_BADMAG(*hdr)) {
502 warnx("%s: Bad magic: %lo",
503 _PATH_LD_HINTS, hdr->hh_magic);
504 return (-1);
505 }
506
507 if (hdr->hh_version != LD_HINTS_VERSION_2) {
508 warnx("Unsupported version: %ld", hdr->hh_version);
509 return (-1);
510 }
511
512 if (hdr->hh_ehints > msize) {
513 if (mmap(addr+msize, hdr->hh_ehints - msize,
514 PROT_READ, MAP_FILE|MAP_COPY|MAP_FIXED,
515 fd, msize) != (caddr_t)(addr+msize)) {
516
517 warn("%s", _PATH_LD_HINTS);
518 return (-1);
519 }
520 }
521 close(fd);
522
523 blist = (struct hints_bucket *)(addr + hdr->hh_hashtab);
524 strtab = (char *)(addr + hdr->hh_strtab);
525
526 for (i = 0; i < hdr->hh_nbucket; i++) {
527 struct hints_bucket *bp = &blist[i];
528
529 /* Sanity check */
530 if (bp->hi_namex >= hdr->hh_strtab_sz) {
531 warnx("Bad name index: %#x", bp->hi_namex);
532 return (-1);
533 }
534 if (bp->hi_pathx >= hdr->hh_strtab_sz) {
535 warnx("Bad path index: %#x", bp->hi_pathx);
536 return (-1);
537 }
538
539 /* Allocate new list element */
540 shp = (struct shlib_list *)xmalloc(sizeof *shp);
541 shp->name = strdup(strtab + bp->hi_namex);
542 shp->path = strdup(strtab + bp->hi_pathx);
543 bcopy(bp->hi_dewey, shp->dewey, sizeof(shp->dewey));
544 shp->ndewey = bp->hi_ndewey;
545 shp->next = NULL;
546
547 *shlib_tail = shp;
548 shlib_tail = &shp->next;
549 }
550 dir_list = strdup(strtab + hdr->hh_dirlist);
551
552 return (0);
553 }
554
555 static void
556 listhints()
557 {
558 struct shlib_list *shp;
559 int i;
560
561 printf("%s:\n", _PATH_LD_HINTS);
562 printf("\tsearch directories: %s\n", dir_list);
563
564 for (i = 0, shp = shlib_head; shp; i++, shp = shp->next)
565 printf("\t%d:-l%s.%d.%d => %s\n",
566 i, shp->name, shp->major, shp->minor, shp->path);
567
568 return;
569 }
570