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