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 31 #ifndef lint 32 static const char rcsid[] = 33 "$Id: ldconfig.c,v 1.28 1998/09/09 01:21:24 jdp Exp $"; 34 #endif /* not lint */ 35 36 #include <sys/param.h> 37 #include <sys/types.h> 38 #include <sys/stat.h> 39 #include <sys/mman.h> 40 #include <a.out.h> 41 #include <ctype.h> 42 #include <dirent.h> 43 #include <elf.h> 44 #include <err.h> 45 #include <errno.h> 46 #include <fcntl.h> 47 #include <link.h> 48 #include <objformat.h> 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <string.h> 52 #include <unistd.h> 53 54 #include "ldconfig.h" 55 #include "shlib.h" 56 #include "support.h" 57 58 #if DEBUG 59 /* test */ 60 #undef _PATH_LD_HINTS 61 #define _PATH_LD_HINTS "./ld.so.hints" 62 #undef _PATH_ELF_HINTS 63 #define _PATH_ELF_HINTS "./ld-elf.so.hints" 64 #endif 65 66 #undef major 67 #undef minor 68 69 static int verbose; 70 static int nostd; 71 static int justread; 72 static int merge; 73 static int rescan; 74 static char *hints_file; 75 76 struct shlib_list { 77 /* Internal list of shared libraries found */ 78 char *name; 79 char *path; 80 int dewey[MAXDEWEY]; 81 int ndewey; 82 #define major dewey[0] 83 #define minor dewey[1] 84 struct shlib_list *next; 85 }; 86 87 static struct shlib_list *shlib_head = NULL, **shlib_tail = &shlib_head; 88 static char *dir_list; 89 90 static int buildhints __P((void)); 91 static int dodir __P((char *, int)); 92 int dofile __P((char *, int)); 93 static void enter __P((char *, char *, char *, int *, int)); 94 static void listhints __P((void)); 95 static int readhints __P((void)); 96 static void usage __P((void)); 97 98 int 99 main(argc, argv) 100 int argc; 101 char *argv[]; 102 { 103 int i, c; 104 int rval = 0; 105 char objformat[32]; 106 int is_aout; 107 108 if (getobjformat(objformat, sizeof objformat, &argc, argv) == -1) 109 errx(1, "getobjformat failed: name too long"); 110 if (strcmp(objformat, "aout") == 0) 111 is_aout = 1; 112 else if (strcmp(objformat, "elf") == 0) 113 is_aout = 0; 114 else 115 errx(1, "unknown object format \"%s\"", objformat); 116 117 hints_file = is_aout ? _PATH_LD_HINTS : _PATH_ELF_HINTS; 118 while ((c = getopt(argc, argv, "Rf:mrsv")) != -1) { 119 switch (c) { 120 case 'R': 121 rescan = 1; 122 break; 123 case 'f': 124 hints_file = optarg; 125 break; 126 case 'm': 127 merge = 1; 128 break; 129 case 'r': 130 justread = 1; 131 break; 132 case 's': 133 nostd = 1; 134 break; 135 case 'v': 136 verbose = 1; 137 break; 138 default: 139 usage(); 140 break; 141 } 142 } 143 144 if (!is_aout) { 145 if (justread) 146 list_elf_hints(hints_file); 147 else 148 update_elf_hints(hints_file, argc - optind, 149 argv + optind, merge || rescan); 150 return 0; 151 } 152 153 dir_list = strdup(""); 154 155 if (justread || merge || rescan) { 156 if ((rval = readhints()) != 0) 157 return rval; 158 } 159 160 if (!nostd && !merge && !rescan) 161 std_search_path(); 162 163 /* Add any directories/files from the command line */ 164 if (!justread) { 165 for (i = optind; i < argc; i++) { 166 struct stat stbuf; 167 168 if (stat(argv[i], &stbuf) == -1) { 169 warn("%s", argv[i]); 170 rval = -1; 171 } else if (strcmp(argv[i], "/usr/lib") == 0) { 172 warnx("WARNING! '%s' can not be used", argv[i]); 173 rval = -1; 174 } else { 175 /* 176 * See if this is a directory-containing 177 * file instead of a directory 178 */ 179 if (S_ISREG(stbuf.st_mode)) 180 rval |= dofile(argv[i], 0); 181 else 182 add_search_path(argv[i]); 183 } 184 } 185 } 186 187 for (i = 0; i < n_search_dirs; i++) { 188 char *cp = concat(dir_list, *dir_list?":":"", search_dirs[i]); 189 free(dir_list); 190 dir_list = cp; 191 } 192 193 if (justread) { 194 listhints(); 195 return 0; 196 } 197 198 for (i = 0; i < n_search_dirs; i++) 199 rval |= dodir(search_dirs[i], 1); 200 201 rval |= buildhints(); 202 203 return rval; 204 } 205 206 static void 207 usage() 208 { 209 fprintf(stderr, 210 "usage: ldconfig [-aout | -elf] [-Rmrsv] [-f hints_file] [dir | file ...]\n"); 211 exit(1); 212 } 213 214 int 215 dofile(fname, silent) 216 char *fname; 217 int silent; 218 { 219 FILE *hfp; 220 char buf[MAXPATHLEN]; 221 int rval = 0; 222 char *cp, *sp; 223 224 if ((hfp = fopen(fname, "r")) == NULL) { 225 warn("%s", fname); 226 return -1; 227 } 228 229 while (fgets(buf, sizeof(buf), hfp)) { 230 cp = buf; 231 while (isspace(*cp)) 232 cp++; 233 if (*cp == '#' || *cp == '\0') 234 continue; 235 sp = cp; 236 while (!isspace(*cp) && *cp != '\0') 237 cp++; 238 239 if (*cp != '\n') { 240 *cp = '\0'; 241 warnx("%s: trailing characters ignored", sp); 242 } 243 244 *cp = '\0'; 245 246 rval |= dodir(sp, silent); 247 } 248 249 (void)fclose(hfp); 250 return rval; 251 } 252 253 int 254 dodir(dir, silent) 255 char *dir; 256 int silent; 257 { 258 DIR *dd; 259 struct dirent *dp; 260 char name[MAXPATHLEN]; 261 int dewey[MAXDEWEY], ndewey; 262 263 if ((dd = opendir(dir)) == NULL) { 264 if (silent && errno == ENOENT) /* Ignore the error */ 265 return 0; 266 warn("%s", dir); 267 return -1; 268 } 269 270 while ((dp = readdir(dd)) != NULL) { 271 register int n; 272 register char *cp; 273 274 /* Check for `lib' prefix */ 275 if (dp->d_name[0] != 'l' || 276 dp->d_name[1] != 'i' || 277 dp->d_name[2] != 'b') 278 continue; 279 280 /* Copy the entry minus prefix */ 281 (void)strcpy(name, dp->d_name + 3); 282 n = strlen(name); 283 if (n < 4) 284 continue; 285 286 /* Find ".so." in name */ 287 for (cp = name + n - 4; cp > name; --cp) { 288 if (cp[0] == '.' && 289 cp[1] == 's' && 290 cp[2] == 'o' && 291 cp[3] == '.') 292 break; 293 } 294 if (cp <= name) 295 continue; 296 297 *cp = '\0'; 298 if (!isdigit(*(cp+4))) 299 continue; 300 301 bzero((caddr_t)dewey, sizeof(dewey)); 302 ndewey = getdewey(dewey, cp + 4); 303 if (ndewey < 2) 304 continue; 305 enter(dir, dp->d_name, name, dewey, ndewey); 306 } 307 308 closedir(dd); 309 return 0; 310 } 311 312 static void 313 enter(dir, file, name, dewey, ndewey) 314 char *dir, *file, *name; 315 int dewey[], ndewey; 316 { 317 struct shlib_list *shp; 318 319 for (shp = shlib_head; shp; shp = shp->next) { 320 if (strcmp(name, shp->name) != 0 || major != shp->major) 321 continue; 322 323 /* Name matches existing entry */ 324 if (cmpndewey(dewey, ndewey, shp->dewey, shp->ndewey) > 0) { 325 326 /* Update this entry with higher versioned lib */ 327 if (verbose) 328 printf("Updating lib%s.%d.%d to %s/%s\n", 329 shp->name, shp->major, shp->minor, 330 dir, file); 331 332 free(shp->name); 333 shp->name = strdup(name); 334 free(shp->path); 335 shp->path = concat(dir, "/", file); 336 bcopy(dewey, shp->dewey, sizeof(shp->dewey)); 337 shp->ndewey = ndewey; 338 } 339 break; 340 } 341 342 if (shp) 343 /* Name exists: older version or just updated */ 344 return; 345 346 /* Allocate new list element */ 347 if (verbose) 348 printf("Adding %s/%s\n", dir, file); 349 350 shp = (struct shlib_list *)xmalloc(sizeof *shp); 351 shp->name = strdup(name); 352 shp->path = concat(dir, "/", file); 353 bcopy(dewey, shp->dewey, MAXDEWEY); 354 shp->ndewey = ndewey; 355 shp->next = NULL; 356 357 *shlib_tail = shp; 358 shlib_tail = &shp->next; 359 } 360 361 362 int 363 hinthash(cp, vmajor) 364 char *cp; 365 int vmajor; 366 { 367 int k = 0; 368 369 while (*cp) 370 k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff; 371 372 k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff; 373 374 return k; 375 } 376 377 int 378 buildhints() 379 { 380 struct hints_header hdr; 381 struct hints_bucket *blist; 382 struct shlib_list *shp; 383 char *strtab; 384 int i, n, str_index = 0; 385 int strtab_sz = 0; /* Total length of strings */ 386 int nhints = 0; /* Total number of hints */ 387 int fd; 388 char *tmpfile; 389 390 for (shp = shlib_head; shp; shp = shp->next) { 391 strtab_sz += 1 + strlen(shp->name); 392 strtab_sz += 1 + strlen(shp->path); 393 nhints++; 394 } 395 396 /* Fill hints file header */ 397 hdr.hh_magic = HH_MAGIC; 398 hdr.hh_version = LD_HINTS_VERSION_2; 399 hdr.hh_nbucket = 1 * nhints; 400 n = hdr.hh_nbucket * sizeof(struct hints_bucket); 401 hdr.hh_hashtab = sizeof(struct hints_header); 402 hdr.hh_strtab = hdr.hh_hashtab + n; 403 hdr.hh_dirlist = strtab_sz; 404 strtab_sz += 1 + strlen(dir_list); 405 hdr.hh_strtab_sz = strtab_sz; 406 hdr.hh_ehints = hdr.hh_strtab + hdr.hh_strtab_sz; 407 408 if (verbose) 409 printf("Totals: entries %d, buckets %ld, string size %d\n", 410 nhints, (long)hdr.hh_nbucket, strtab_sz); 411 412 /* Allocate buckets and string table */ 413 blist = (struct hints_bucket *)xmalloc(n); 414 bzero((char *)blist, n); 415 for (i = 0; i < hdr.hh_nbucket; i++) 416 /* Empty all buckets */ 417 blist[i].hi_next = -1; 418 419 strtab = (char *)xmalloc(strtab_sz); 420 421 /* Enter all */ 422 for (shp = shlib_head; shp; shp = shp->next) { 423 struct hints_bucket *bp; 424 425 bp = blist + 426 (hinthash(shp->name, shp->major) % hdr.hh_nbucket); 427 428 if (bp->hi_pathx) { 429 int i; 430 431 for (i = 0; i < hdr.hh_nbucket; i++) { 432 if (blist[i].hi_pathx == 0) 433 break; 434 } 435 if (i == hdr.hh_nbucket) { 436 warnx("bummer!"); 437 return -1; 438 } 439 while (bp->hi_next != -1) 440 bp = &blist[bp->hi_next]; 441 bp->hi_next = i; 442 bp = blist + i; 443 } 444 445 /* Insert strings in string table */ 446 bp->hi_namex = str_index; 447 strcpy(strtab + str_index, shp->name); 448 str_index += 1 + strlen(shp->name); 449 450 bp->hi_pathx = str_index; 451 strcpy(strtab + str_index, shp->path); 452 str_index += 1 + strlen(shp->path); 453 454 /* Copy versions */ 455 bcopy(shp->dewey, bp->hi_dewey, sizeof(bp->hi_dewey)); 456 bp->hi_ndewey = shp->ndewey; 457 } 458 459 /* Copy search directories */ 460 strcpy(strtab + str_index, dir_list); 461 str_index += 1 + strlen(dir_list); 462 463 /* Sanity check */ 464 if (str_index != strtab_sz) { 465 errx(1, "str_index(%d) != strtab_sz(%d)", str_index, strtab_sz); 466 } 467 468 tmpfile = concat(hints_file, ".XXXXXX", ""); 469 if ((tmpfile = mktemp(tmpfile)) == NULL) { 470 warn("%s", tmpfile); 471 return -1; 472 } 473 474 umask(0); /* Create with exact permissions */ 475 if ((fd = open(tmpfile, O_RDWR|O_CREAT|O_TRUNC, 0444)) == -1) { 476 warn("%s", hints_file); 477 return -1; 478 } 479 480 if (write(fd, &hdr, sizeof(struct hints_header)) != 481 sizeof(struct hints_header)) { 482 warn("%s", hints_file); 483 return -1; 484 } 485 if (write(fd, blist, hdr.hh_nbucket * sizeof(struct hints_bucket)) != 486 hdr.hh_nbucket * sizeof(struct hints_bucket)) { 487 warn("%s", hints_file); 488 return -1; 489 } 490 if (write(fd, strtab, strtab_sz) != strtab_sz) { 491 warn("%s", hints_file); 492 return -1; 493 } 494 if (close(fd) != 0) { 495 warn("%s", hints_file); 496 return -1; 497 } 498 499 /* Install it */ 500 if (unlink(hints_file) != 0 && errno != ENOENT) { 501 warn("%s", hints_file); 502 return -1; 503 } 504 505 if (rename(tmpfile, hints_file) != 0) { 506 warn("%s", hints_file); 507 return -1; 508 } 509 510 return 0; 511 } 512 513 static int 514 readhints() 515 { 516 int fd; 517 void *addr; 518 long fsize; 519 long msize; 520 struct hints_header *hdr; 521 struct hints_bucket *blist; 522 char *strtab; 523 struct shlib_list *shp; 524 int i; 525 526 if ((fd = open(hints_file, O_RDONLY, 0)) == -1) { 527 warn("%s", hints_file); 528 return -1; 529 } 530 531 msize = PAGE_SIZE; 532 addr = mmap(0, msize, PROT_READ, MAP_COPY, fd, 0); 533 534 if (addr == MAP_FAILED) { 535 warn("%s", hints_file); 536 return -1; 537 } 538 539 hdr = (struct hints_header *)addr; 540 if (HH_BADMAG(*hdr)) { 541 warnx("%s: bad magic: %lo", hints_file, 542 (unsigned long)hdr->hh_magic); 543 return -1; 544 } 545 546 if (hdr->hh_version != LD_HINTS_VERSION_1 && 547 hdr->hh_version != LD_HINTS_VERSION_2) { 548 warnx("unsupported version: %ld", (long)hdr->hh_version); 549 return -1; 550 } 551 552 if (hdr->hh_ehints > msize) { 553 fsize = hdr->hh_ehints; 554 munmap(addr, msize); 555 addr = mmap(0, fsize, PROT_READ, MAP_COPY, fd, 0); 556 if (addr == MAP_FAILED) { 557 warn("%s", hints_file); 558 return -1; 559 } 560 hdr = (struct hints_header *)addr; 561 } 562 close(fd); 563 564 blist = (struct hints_bucket *)(addr + hdr->hh_hashtab); 565 strtab = (char *)(addr + hdr->hh_strtab); 566 567 if (hdr->hh_version >= LD_HINTS_VERSION_2) 568 add_search_path(strtab + hdr->hh_dirlist); 569 else if (rescan) 570 errx(1, "%s too old and does not contain the search path", 571 hints_file); 572 573 if (rescan) 574 return 0; 575 576 for (i = 0; i < hdr->hh_nbucket; i++) { 577 struct hints_bucket *bp = &blist[i]; 578 579 /* Sanity check */ 580 if (bp->hi_namex >= hdr->hh_strtab_sz) { 581 warnx("bad name index: %#x", bp->hi_namex); 582 return -1; 583 } 584 if (bp->hi_pathx >= hdr->hh_strtab_sz) { 585 warnx("bad path index: %#x", bp->hi_pathx); 586 return -1; 587 } 588 589 /* Allocate new list element */ 590 shp = (struct shlib_list *)xmalloc(sizeof *shp); 591 shp->name = strdup(strtab + bp->hi_namex); 592 shp->path = strdup(strtab + bp->hi_pathx); 593 bcopy(bp->hi_dewey, shp->dewey, sizeof(shp->dewey)); 594 shp->ndewey = bp->hi_ndewey; 595 shp->next = NULL; 596 597 *shlib_tail = shp; 598 shlib_tail = &shp->next; 599 } 600 601 return 0; 602 } 603 604 static void 605 listhints() 606 { 607 struct shlib_list *shp; 608 int i; 609 610 printf("%s:\n", hints_file); 611 printf("\tsearch directories: %s\n", dir_list); 612 613 for (i = 0, shp = shlib_head; shp; i++, shp = shp->next) 614 printf("\t%d:-l%s.%d.%d => %s\n", 615 i, shp->name, shp->major, shp->minor, shp->path); 616 617 return; 618 } 619