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