ldconfig.c revision 1.2 1 /*
2 * Copyright (c) 1993 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 withough 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.2 1993/12/08 10:16:37 pk Exp $
31 */
32
33 #include <sys/param.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <sys/file.h>
39 #include <sys/time.h>
40 #include <sys/mman.h>
41 #include <sys/resource.h>
42 #include <fcntl.h>
43 #include <errno.h>
44 #include <ar.h>
45 #include <ranlib.h>
46 #include <a.out.h>
47 #include <stab.h>
48 #include <string.h>
49 #include <dirent.h>
50
51 #include "ld.h"
52
53 #undef major
54 #undef minor
55
56 char *progname;
57 static int verbose;
58 static int nostd;
59 static int justread;
60
61 struct shlib_list {
62 /* Internal list of shared libraries found */
63 char *name;
64 char *path;
65 int dewey[MAXDEWEY];
66 int ndewey;
67 #define major dewey[0]
68 #define minor dewey[1]
69 struct shlib_list *next;
70 };
71
72 static struct shlib_list *shlib_head = NULL, **shlib_tail = &shlib_head;
73
74 static void enter __P((char *, char *, char *, int *, int));
75 static int dodir __P((char *));
76 static int build_hints __P((void));
77
78 int
79 main(argc, argv)
80 int argc;
81 char *argv[];
82 {
83 int i, c;
84 int rval = 0;
85 extern int optind;
86
87 if ((progname = strrchr(argv[0], '/')) == NULL)
88 progname = argv[0];
89 else
90 progname++;
91
92 while ((c = getopt(argc, argv, "rsv")) != EOF) {
93 switch (c) {
94 case 'v':
95 verbose = 1;
96 break;
97 case 's':
98 nostd = 1;
99 break;
100 case 'r':
101 justread = 1;
102 break;
103 default:
104 fprintf(stderr, "Usage: %s [-v] [dir ...]\n", progname);
105 exit(1);
106 break;
107 }
108 }
109
110 if (justread)
111 return listhints();
112
113 if (!nostd)
114 std_search_dirs(NULL);
115
116 for (i = 0; i < n_search_dirs; i++)
117 rval |= dodir(search_dirs[i]);
118
119 for (i = optind; i < argc; i++)
120 rval |= dodir(argv[i]);
121
122 rval |= build_hints();
123
124 return rval;
125 }
126
127 int
128 dodir(dir)
129 char *dir;
130 {
131 DIR *dd;
132 struct dirent *dp;
133 char name[MAXPATHLEN], rest[MAXPATHLEN];
134 int dewey[MAXDEWEY], ndewey;
135
136 if ((dd = opendir(dir)) == NULL) {
137 perror(dir);
138 return -1;
139 }
140
141 while ((dp = readdir(dd)) != NULL) {
142 int n;
143
144 name[0] = rest[0] = '\0';
145
146 n = sscanf(dp->d_name, "lib%[^.].so.%s",
147 name, rest);
148
149 if (n < 2 || rest[0] == '\0')
150 continue;
151
152 ndewey = getdewey(dewey, rest);
153 enter(dir, dp->d_name, name, dewey, ndewey);
154 }
155
156 return 0;
157 }
158
159 static void
160 enter(dir, file, name, dewey, ndewey)
161 char *dir, *file, *name;
162 int dewey[], ndewey;
163 {
164 struct shlib_list *shp;
165
166 for (shp = shlib_head; shp; shp = shp->next) {
167 if (strcmp(name, shp->name) != 0 || major != shp->major)
168 continue;
169
170 /* Name matches existing entry */
171 if (cmpndewey(dewey, ndewey, shp->dewey, shp->ndewey) > 0) {
172
173 /* Update this entry with higher versioned lib */
174 if (verbose)
175 printf("Updating lib%s.%d.%d to %s/%s\n",
176 shp->name, shp->major, shp->minor,
177 dir, file);
178
179 free(shp->name);
180 shp->name = strdup(name);
181 free(shp->path);
182 shp->path = concat(dir, "/", file);
183 bcopy(dewey, shp->dewey, sizeof(shp->dewey));
184 shp->ndewey = ndewey;
185 }
186 break;
187 }
188
189 if (shp)
190 /* Name exists: older version or just updated */
191 return;
192
193 /* Allocate new list element */
194 if (verbose)
195 printf("Adding %s/%s\n", dir, file);
196
197 shp = (struct shlib_list *)xmalloc(sizeof *shp);
198 shp->name = strdup(name);
199 shp->path = concat(dir, "/", file);
200 bcopy(dewey, shp->dewey, MAXDEWEY);
201 shp->ndewey = ndewey;
202 shp->next = NULL;
203
204 *shlib_tail = shp;
205 shlib_tail = &shp->next;
206 }
207
208
209 #if DEBUG
210 /* test */
211 #undef _PATH_LD_HINTS
212 #define _PATH_LD_HINTS "./ld.so.hints"
213 #endif
214
215 int
216 hinthash(cp, vmajor, vminor)
217 char *cp;
218 int vmajor, vminor;
219 {
220 int k = 0;
221
222 while (*cp)
223 k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
224
225 k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff;
226 k = (((k << 1) + (k >> 14)) ^ (vminor*167)) & 0x3fff;
227
228 return k;
229 }
230
231 int
232 build_hints()
233 {
234 struct hints_header hdr;
235 struct hints_bucket *blist;
236 struct shlib_list *shp;
237 char *strtab;
238 int i, n, str_index = 0;
239 int strtab_sz = 0; /* Total length of strings */
240 int nhints = 0; /* Total number of hints */
241 int fd;
242 char *tmpfile;
243
244 for (shp = shlib_head; shp; shp = shp->next) {
245 strtab_sz += 1 + strlen(shp->name);
246 strtab_sz += 1 + strlen(shp->path);
247 nhints++;
248 }
249
250 /* Fill hints file header */
251 hdr.hh_magic = HH_MAGIC;
252 hdr.hh_version = LD_HINTS_VERSION_1;
253 hdr.hh_nbucket = 1 * nhints;
254 n = hdr.hh_nbucket * sizeof(struct hints_bucket);
255 hdr.hh_hashtab = sizeof(struct hints_header);
256 hdr.hh_strtab = hdr.hh_hashtab + n;
257 hdr.hh_strtab_sz = strtab_sz;
258 hdr.hh_ehints = hdr.hh_strtab + hdr.hh_strtab_sz;
259
260 if (verbose)
261 printf("Totals: entries %d, buckets %d, string size %d\n",
262 nhints, hdr.hh_nbucket, strtab_sz);
263
264 /* Allocate buckets and string table */
265 blist = (struct hints_bucket *)xmalloc(n);
266 bzero((char *)blist, n);
267 for (i = 0; i < hdr.hh_nbucket; i++)
268 /* Empty all buckets */
269 blist[i].hi_next = -1;
270
271 strtab = (char *)xmalloc(strtab_sz);
272
273 /* Enter all */
274 for (shp = shlib_head; shp; shp = shp->next) {
275 struct hints_bucket *bp;
276
277 bp = blist +
278 (hinthash(shp->name, shp->major, shp->minor) % hdr.hh_nbucket);
279
280 if (bp->hi_pathx) {
281 int i;
282
283 for (i = 0; i < hdr.hh_nbucket; i++) {
284 if (blist[i].hi_pathx == 0)
285 break;
286 }
287 if (i == hdr.hh_nbucket) {
288 fprintf(stderr, "Bummer!\n");
289 return -1;
290 }
291 while (bp->hi_next != -1)
292 bp = &blist[bp->hi_next];
293 bp->hi_next = i;
294 bp = blist + i;
295 }
296
297 /* Insert strings in string table */
298 bp->hi_namex = str_index;
299 strcpy(strtab + str_index, shp->name);
300 str_index += 1 + strlen(shp->name);
301
302 bp->hi_pathx = str_index;
303 strcpy(strtab + str_index, shp->path);
304 str_index += 1 + strlen(shp->path);
305
306 /* Copy versions */
307 bcopy(shp->dewey, bp->hi_dewey, sizeof(bp->hi_dewey));
308 bp->hi_ndewey = shp->ndewey;
309 }
310
311 tmpfile = concat(_PATH_LD_HINTS, "+", "");
312 if ((fd = open(tmpfile, O_RDWR|O_CREAT|O_TRUNC, 0444)) == -1) {
313 perror(_PATH_LD_HINTS);
314 return -1;
315 }
316
317 mywrite(&hdr, 1, sizeof(struct hints_header), fd);
318 mywrite(blist, hdr.hh_nbucket, sizeof(struct hints_bucket), fd);
319 mywrite(strtab, strtab_sz, 1, fd);
320
321 if (close(fd) != 0) {
322 perror(_PATH_LD_HINTS);
323 return -1;
324 }
325
326 /* Now, install real file */
327 if (unlink(_PATH_LD_HINTS) != 0 && errno != ENOENT) {
328 perror(_PATH_LD_HINTS);
329 return -1;
330 }
331
332 if (rename(tmpfile, _PATH_LD_HINTS) != 0) {
333 perror(_PATH_LD_HINTS);
334 return -1;
335 }
336
337 return 0;
338 }
339
340 int
341 listhints()
342 {
343 int fd;
344 caddr_t addr;
345 long msize;
346 struct hints_header *hdr;
347 struct hints_bucket *blist;
348 char *strtab;
349 int i;
350
351 if ((fd = open(_PATH_LD_HINTS, O_RDONLY, 0)) == -1) {
352 perror(_PATH_LD_HINTS);
353 return -1;
354 }
355
356 msize = PAGSIZ;
357 addr = mmap(0, msize, PROT_READ, MAP_FILE|MAP_COPY, fd, 0);
358
359 if (addr == (caddr_t)-1) {
360 perror(_PATH_LD_HINTS);
361 return -1;
362 }
363
364 hdr = (struct hints_header *)addr;
365 if (HH_BADMAG(*hdr)) {
366 fprintf(stderr, "%s: Bad magic: %d\n");
367 return -1;
368 }
369
370 if (hdr->hh_version != LD_HINTS_VERSION_1) {
371 fprintf(stderr, "Unsupported version: %d\n", hdr->hh_version);
372 return -1;
373 }
374
375 if (hdr->hh_ehints > msize) {
376 if (mmap(addr+msize, hdr->hh_ehints - msize,
377 PROT_READ, MAP_FILE|MAP_COPY|MAP_FIXED,
378 fd, msize) != (caddr_t)(addr+msize)) {
379
380 perror(_PATH_LD_HINTS);
381 return -1;
382 }
383 }
384 close(fd);
385
386 blist = (struct hints_bucket *)(addr + hdr->hh_hashtab);
387 strtab = (char *)(addr + hdr->hh_strtab);
388
389 printf("%s:\n", _PATH_LD_HINTS);
390 for (i = 0; i < hdr->hh_nbucket; i++) {
391 struct hints_bucket *bp = &blist[i];
392
393 /* Sanity check */
394 if (bp->hi_namex >= hdr->hh_strtab_sz) {
395 fprintf(stderr, "Bad name index: %#x\n", bp->hi_namex);
396 return -1;
397 }
398 if (bp->hi_pathx >= hdr->hh_strtab_sz) {
399 fprintf(stderr, "Bad path index: %#x\n", bp->hi_pathx);
400 return -1;
401 }
402
403 printf("\t%d:-l%s.%d.%d => %s (%d -> %d)\n",
404 i,
405 strtab + bp->hi_namex, bp->hi_major, bp->hi_minor,
406 strtab + bp->hi_pathx,
407 hinthash(strtab+bp->hi_namex, bp->hi_major, bp->hi_minor)
408 % hdr->hh_nbucket,
409 bp->hi_next);
410 }
411
412 return 0;
413 }
414
415