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