Sync usage with man page.
[netbsd-mini2440.git] / sbin / ldconfig / ldconfig.c
blob822fad1a29730004e220ac96181bdd9b8585d242
1 /* $NetBSD: ldconfig.c,v 1.45 2009/03/17 00:52:47 lukem Exp $ */
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Paul Kranenburg.
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.
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.
31 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: ldconfig.c,v 1.45 2009/03/17 00:52:47 lukem Exp $");
35 #endif
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 <sys/exec_aout.h>
45 #include <a.out.h>
46 #include <ctype.h>
47 #include <dirent.h>
48 #include <err.h>
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55 #include <link_aout.h>
56 #include <paths.h>
58 #include "shlib.h"
60 #define _PATH_LD_SO_CONF "/etc/ld.so.conf"
62 #undef major
63 #undef minor
65 static int verbose;
66 static int nostd;
67 static int noconf;
68 static int justread;
69 static int merge;
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;
82 static struct shlib_list *shlib_head = NULL, **shlib_tail = &shlib_head;
83 static char *dir_list;
85 static void enter(char *, char *, char *, int *, int);
86 static int dodir(char *, int, int);
87 static int do_conf(void);
88 static int buildhints(void);
89 static int readhints(void);
90 static void listhints(void);
91 static int hinthash(char *, int, int);
93 int
94 main(int argc, char *argv[])
96 int i, c;
97 int rval = 0;
99 while ((c = getopt(argc, argv, "cmrsSv")) != -1) {
100 switch (c) {
101 case 'c':
102 noconf = 1;
103 break;
104 case 'm':
105 merge = 1;
106 break;
107 case 'r':
108 justread = 1;
109 break;
110 case 's':
111 nostd = 1;
112 noconf = 1;
113 break;
114 case 'S':
115 nostd = 1;
116 break;
117 case 'v':
118 verbose = 1;
119 break;
120 default:
121 errx(1, "usage: %s [-c][-m][-r][-s][-S][-v][dir ...]",
122 getprogname());
123 break;
127 dir_list = xmalloc(1);
128 *dir_list = '\0';
130 if (justread || merge) {
131 if ((rval = readhints()) != 0)
132 return (rval);
133 if (justread) {
134 listhints();
135 return (rval);
139 if (!nostd && !merge)
140 std_search_path();
142 for (i = 0; i < n_search_dirs; i++)
143 rval |= dodir(search_dirs[i], 1, 0);
145 if (!noconf && !merge)
146 rval |= do_conf();
148 for (i = optind; i < argc; i++) {
149 rval |= dodir(argv[i], 0, 1);
152 rval |= buildhints();
154 return (rval);
158 do_conf(void)
160 FILE *conf;
161 char *line, *c;
162 char *cline = NULL;
163 size_t len;
164 int rval = 0;
165 #ifdef __ELF__
166 char *aout_conf;
168 aout_conf = xmalloc(sizeof(_PATH_EMUL_AOUT) +
169 strlen(_PATH_LD_SO_CONF));
170 strcpy(aout_conf, _PATH_EMUL_AOUT);
171 strcat(aout_conf, _PATH_LD_SO_CONF);
172 if ((conf = fopen(aout_conf, "r")) == NULL) {
173 if (verbose)
174 warnx("can't open `%s'", aout_conf);
175 free(aout_conf);
176 return (0);
178 free(aout_conf);
179 #else
180 if ((conf = fopen(_PATH_LD_SO_CONF, "r")) == NULL) {
181 if (verbose) {
182 warnx("can't open `%s'", _PATH_LD_SO_CONF);
184 return (0);
186 #endif
188 while ((line = fgetln(conf, &len)) != NULL) {
189 if (*line != '/')
190 continue;
192 if (line[len-1] == '\n') {
193 line[--len] = '\0';
194 } else {
195 cline = xmalloc(len+1);
196 memcpy(cline, line, len);
197 line = cline;
198 line[len] = '\0';
201 while (isblank(*line)) { line++; len--; }
202 if ((c = strchr(line, '#')) == NULL)
203 c = line + len;
204 while (--c >= line && isblank(*c)) continue;
205 if (c >= line) {
206 *++c = '\0';
207 rval |= dodir(line, 0, 1);
210 if (cline) {
211 free(cline);
212 cline = NULL;
216 (void) fclose(conf);
218 return (rval);
222 dodir(char *dir, int silent, int update_dir_list)
224 DIR *dd;
225 struct dirent *dp;
226 char name[MAXPATHLEN];
227 int dewey[MAXDEWEY], ndewey;
229 if ((dd = opendir(dir)) == NULL) {
230 /* /emul/aout directories are allowed to not exist.
232 if (!strncmp(dir, _PATH_EMUL_AOUT, sizeof(_PATH_EMUL_AOUT)-1))
233 return 0;
234 if (!silent || errno != ENOENT)
235 warn("%s", dir);
236 return (-1);
239 if (update_dir_list) {
240 /* Check for duplicates? */
241 char *cp = concat(dir_list, *dir_list?":":"", dir);
242 free(dir_list);
243 dir_list = cp;
246 while ((dp = readdir(dd)) != NULL) {
247 int n;
248 char *cp, *path;
249 FILE *fp;
250 struct exec ex;
252 /* Check for `lib' prefix */
253 if (dp->d_name[0] != 'l' ||
254 dp->d_name[1] != 'i' ||
255 dp->d_name[2] != 'b')
256 continue;
258 /* Copy the entry minus prefix */
259 (void)strcpy(name, dp->d_name + 3);
260 n = strlen(name);
261 if (n < 4)
262 continue;
264 /* Find ".so." in name */
265 for (cp = name + n - 4; cp > name; --cp) {
266 if (cp[0] == '.' &&
267 cp[1] == 's' &&
268 cp[2] == 'o' &&
269 cp[3] == '.')
270 break;
272 if (cp <= name)
273 continue;
275 path = concat(dir, "/", dp->d_name);
276 fp = fopen(path, "r");
277 free(path);
278 if (fp == NULL)
279 continue;
280 n = fread(&ex, 1, sizeof(ex), fp);
281 fclose(fp);
282 if (n != sizeof(ex)
283 || N_GETMAGIC(ex) != ZMAGIC
284 || (N_GETFLAG(ex) & EX_DYNAMIC) == 0)
285 continue;
287 *cp = '\0';
288 if (!isdigit((unsigned char)*(cp+4)))
289 continue;
291 memset(dewey, 0, sizeof(dewey));
292 ndewey = getdewey(dewey, cp + 4);
293 enter(dir, dp->d_name, name, dewey, ndewey);
296 (void) closedir(dd);
298 return (0);
301 static void
302 enter(char *dir, char *file, char *name, int dewey[], int ndewey)
304 struct shlib_list *shp;
306 for (shp = shlib_head; shp; shp = shp->next) {
307 if (strcmp(name, shp->name) != 0 || major != shp->major)
308 continue;
310 /* Name matches existing entry */
311 if (cmpndewey(dewey, ndewey, shp->dewey, shp->ndewey) > 0) {
313 /* Update this entry with higher versioned lib */
314 if (verbose)
315 printf("Updating lib%s.%d.%d to %s/%s\n",
316 shp->name, shp->major, shp->minor,
317 dir, file);
319 free(shp->name);
320 shp->name = strdup(name);
321 free(shp->path);
322 shp->path = concat(dir, "/", file);
323 memcpy(shp->dewey, dewey, sizeof(shp->dewey));
324 shp->ndewey = ndewey;
326 break;
329 if (shp)
330 /* Name exists: older version or just updated */
331 return;
333 /* Allocate new list element */
334 if (verbose)
335 printf("Adding %s/%s\n", dir, file);
337 shp = (struct shlib_list *)xmalloc(sizeof *shp);
338 shp->name = strdup(name);
339 shp->path = concat(dir, "/", file);
340 memcpy(shp->dewey, dewey, sizeof(shp->dewey));
341 shp->ndewey = ndewey;
342 shp->next = NULL;
344 *shlib_tail = shp;
345 shlib_tail = &shp->next;
349 #if DEBUG
350 /* test */
351 #undef _PATH_LD_HINTS
352 #define _PATH_LD_HINTS "./ld.so.hints"
353 #endif
355 /* XXX - should be a common function with rtld.c */
357 hinthash(char *cp, int vmajor, int vminor)
359 int k = 0;
361 while (*cp)
362 k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
364 k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff;
365 #if 0
366 k = (((k << 1) + (k >> 14)) ^ (vminor*167)) & 0x3fff;
367 #endif
369 return (k);
373 buildhints(void)
375 struct hints_header hdr;
376 struct hints_bucket *blist;
377 struct shlib_list *shp;
378 char *strtab;
379 int i, n, str_index = 0;
380 int strtab_sz = 0; /* Total length of strings */
381 int nhints = 0; /* Total number of hints */
382 int fd;
383 char *tempfile;
385 for (shp = shlib_head; shp; shp = shp->next) {
386 strtab_sz += 1 + strlen(shp->name);
387 strtab_sz += 1 + strlen(shp->path);
388 nhints++;
391 /* Fill hints file header */
392 hdr.hh_magic = HH_MAGIC;
393 hdr.hh_version = LD_HINTS_VERSION_2;
394 hdr.hh_nbucket = 1 * nhints;
395 n = hdr.hh_nbucket * sizeof(struct hints_bucket);
396 hdr.hh_hashtab = sizeof(struct hints_header);
397 hdr.hh_strtab = hdr.hh_hashtab + n;
398 hdr.hh_dirlist = strtab_sz;
399 strtab_sz += 1 + strlen(dir_list);
400 hdr.hh_strtab_sz = strtab_sz;
401 hdr.hh_ehints = hdr.hh_strtab + hdr.hh_strtab_sz;
403 if (verbose)
404 printf("Totals: entries %d, buckets %ld, string size %d\n",
405 nhints, hdr.hh_nbucket, strtab_sz);
407 /* Allocate buckets and string table */
408 blist = (struct hints_bucket *)xmalloc(n);
409 memset(blist, 0, n);
410 for (i = 0; i < hdr.hh_nbucket; i++)
411 /* Empty all buckets */
412 blist[i].hi_next = -1;
414 strtab = (char *)xmalloc(strtab_sz);
416 /* Enter all */
417 for (shp = shlib_head; shp; shp = shp->next) {
418 struct hints_bucket *bp;
420 bp = blist +
421 (hinthash(shp->name, shp->major, shp->minor) % hdr.hh_nbucket);
423 if (bp->hi_pathx) {
424 for (i = 0; i < hdr.hh_nbucket; i++) {
425 if (blist[i].hi_pathx == 0)
426 break;
428 if (i == hdr.hh_nbucket) {
429 warnx("Bummer!");
430 goto out;
432 while (bp->hi_next != -1)
433 bp = &blist[bp->hi_next];
434 bp->hi_next = i;
435 bp = blist + i;
438 /* Insert strings in string table */
439 bp->hi_namex = str_index;
440 strcpy(strtab + str_index, shp->name);
441 str_index += 1 + strlen(shp->name);
443 bp->hi_pathx = str_index;
444 strcpy(strtab + str_index, shp->path);
445 str_index += 1 + strlen(shp->path);
447 /* Copy versions */
448 memcpy(bp->hi_dewey, shp->dewey, sizeof(bp->hi_dewey));
449 bp->hi_ndewey = shp->ndewey;
452 /* Copy search directories */
453 strcpy(strtab + str_index, dir_list);
454 str_index += 1 + strlen(dir_list);
456 /* Sanity check */
457 if (str_index != strtab_sz) {
458 errx(1, "str_index(%d) != strtab_sz(%d)", str_index, strtab_sz);
461 tempfile = concat(_PATH_LD_HINTS, ".XXXXXX", "");
462 if ((fd = mkstemp(tempfile)) == -1) {
463 warn("%s", tempfile);
464 goto out;
467 if (write(fd, &hdr, sizeof(struct hints_header)) !=
468 sizeof(struct hints_header)) {
469 warn("%s", _PATH_LD_HINTS);
470 goto out;
472 if ((size_t)write(fd, blist, hdr.hh_nbucket * sizeof(struct hints_bucket)) !=
473 hdr.hh_nbucket * sizeof(struct hints_bucket)) {
474 warn("%s", _PATH_LD_HINTS);
475 goto out;
477 if (write(fd, strtab, strtab_sz) != strtab_sz) {
478 warn("%s", _PATH_LD_HINTS);
479 goto out;
481 if (fchmod(fd, 0444) == -1) {
482 warn("%s", _PATH_LD_HINTS);
483 goto out;
485 if (close(fd) != 0) {
486 warn("%s", _PATH_LD_HINTS);
487 goto out;
490 /* Install it */
491 if (unlink(_PATH_LD_HINTS) != 0 && errno != ENOENT) {
492 warn("%s", _PATH_LD_HINTS);
493 goto out;
496 if (rename(tempfile, _PATH_LD_HINTS) != 0) {
497 warn("%s", _PATH_LD_HINTS);
498 goto out;
501 free(blist);
502 free(strtab);
503 return 0;
504 out:
505 free(blist);
506 free(strtab);
507 return -1;
510 static int
511 readhints(void)
513 int fd;
514 void *addr = (void *) -1;
515 size_t msize = 0;
516 struct hints_header *hdr;
517 struct hints_bucket *blist;
518 char *strtab;
519 struct shlib_list *shp;
520 int i;
521 struct stat st;
522 int error = 0;
524 if ((fd = open(_PATH_LD_HINTS, O_RDONLY, 0)) == -1) {
525 warn("%s", _PATH_LD_HINTS);
526 goto cleanup;
529 if (fstat(fd, &st) == -1) {
530 warn("%s", _PATH_LD_HINTS);
531 goto cleanup;
534 msize = (size_t)st.st_size;
535 if (msize < sizeof(*hdr)) {
536 warnx("%s: File too short", _PATH_LD_HINTS);
537 goto cleanup;
540 addr = mmap(0, msize, PROT_READ, MAP_FILE|MAP_PRIVATE, fd, 0);
542 if (addr == (void *)-1) {
543 warn("%s", _PATH_LD_HINTS);
544 goto cleanup;
547 hdr = (struct hints_header *)addr;
548 if (HH_BADMAG(*hdr)) {
549 warnx("%s: Bad magic: %lo",
550 _PATH_LD_HINTS, hdr->hh_magic);
551 goto cleanup;
554 if (hdr->hh_version != LD_HINTS_VERSION_2) {
555 warnx("Unsupported version: %ld", hdr->hh_version);
556 goto cleanup;
560 blist = (struct hints_bucket *)((char *)addr + hdr->hh_hashtab);
561 strtab = ((char *)addr + hdr->hh_strtab);
563 for (i = 0; i < hdr->hh_nbucket; i++) {
564 struct hints_bucket *bp = &blist[i];
566 /* Sanity check */
567 if (bp->hi_namex >= hdr->hh_strtab_sz) {
568 warnx("Bad name index: %#x", bp->hi_namex);
569 goto cleanup;
571 if (bp->hi_pathx >= hdr->hh_strtab_sz) {
572 warnx("Bad path index: %#x", bp->hi_pathx);
573 goto cleanup;
576 /* Allocate new list element */
577 shp = (struct shlib_list *)xmalloc(sizeof *shp);
578 shp->name = strdup(strtab + bp->hi_namex);
579 shp->path = strdup(strtab + bp->hi_pathx);
580 memcpy(shp->dewey, bp->hi_dewey, sizeof(shp->dewey));
581 shp->ndewey = bp->hi_ndewey;
582 shp->next = NULL;
584 *shlib_tail = shp;
585 shlib_tail = &shp->next;
587 dir_list = strdup(strtab + hdr->hh_dirlist);
588 goto done;
589 cleanup:
590 error = 1;
591 done:
592 if (fd != -1)
593 close(fd);
594 if (addr != (void *)-1)
595 munmap(addr, msize);
596 return error;
600 static void
601 listhints(void)
603 struct shlib_list *shp;
604 int i;
606 printf("%s:\n", _PATH_LD_HINTS);
607 printf("\tsearch directories: %s\n", dir_list);
609 for (i = 0, shp = shlib_head; shp; i++, shp = shp->next)
610 printf("\t%d:-l%s.%d.%d => %s\n",
611 i, shp->name, shp->major, shp->minor, shp->path);
613 return;