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 * $FreeBSD: src/sbin/ldconfig/ldconfig.c,v 1.31.2.3 2001/07/11 23:59:10 obrien Exp $ 31 * $DragonFly: src/sbin/ldconfig/ldconfig.c,v 1.2 2003/06/17 04:27:33 dillon Exp $ 32 */ 33 34 #include <sys/param.h> 35 #include <sys/types.h> 36 #include <sys/stat.h> 37 #include <sys/mman.h> 38 #include <a.out.h> 39 #include <ctype.h> 40 #include <dirent.h> 41 #include <elf-hints.h> 42 #include <err.h> 43 #include <errno.h> 44 #include <fcntl.h> 45 #include <link.h> 46 #include <objformat.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 #include <unistd.h> 51 52 #include "ldconfig.h" 53 #include "shlib.h" 54 #include "support.h" 55 56 #if DEBUG 57 /* test */ 58 #undef _PATH_LD_HINTS 59 #define _PATH_LD_HINTS "./ld.so.hints" 60 #undef _PATH_ELF_HINTS 61 #define _PATH_ELF_HINTS "./ld-elf.so.hints" 62 #endif 63 64 #undef major 65 #undef minor 66 67 static int verbose; 68 static int nostd; 69 static int justread; 70 static int merge; 71 static int rescan; 72 static char *hints_file; 73 74 struct shlib_list { 75 /* Internal list of shared libraries found */ 76 char *name; 77 char *path; 78 int dewey[MAXDEWEY]; 79 int ndewey; 80 #define major dewey[0] 81 #define minor dewey[1] 82 struct shlib_list *next; 83 }; 84 85 static struct shlib_list *shlib_head = NULL, **shlib_tail = &shlib_head; 86 static char *dir_list; 87 88 static int buildhints __P((void)); 89 static int dodir __P((char *, int)); 90 int dofile __P((char *, int)); 91 static void enter __P((char *, char *, char *, int *, int)); 92 static void listhints __P((void)); 93 static int readhints __P((void)); 94 static void usage __P((void)); 95 96 int 97 main(argc, argv) 98 int argc; 99 char *argv[]; 100 { 101 int i, c; 102 int rval = 0; 103 char objformat[32]; 104 int is_aout; 105 106 if (getobjformat(objformat, sizeof objformat, &argc, argv) == -1) 107 errx(1, "getobjformat failed: name too long"); 108 if (strcmp(objformat, "aout") == 0) 109 is_aout = 1; 110 else if (strcmp(objformat, "elf") == 0) 111 is_aout = 0; 112 else 113 errx(1, "unknown object format \"%s\"", objformat); 114 115 hints_file = is_aout ? _PATH_LD_HINTS : _PATH_ELF_HINTS; 116 if (argc == 1) 117 rescan = 1; 118 else while((c = getopt(argc, argv, "Rf:imrsv")) != -1) { 119 switch (c) { 120 case 'R': 121 rescan = 1; 122 break; 123 case 'f': 124 hints_file = optarg; 125 break; 126 case 'i': 127 insecure = 1; 128 break; 129 case 'm': 130 merge = 1; 131 break; 132 case 'r': 133 justread = 1; 134 break; 135 case 's': 136 nostd = 1; 137 break; 138 case 'v': 139 verbose = 1; 140 break; 141 default: 142 usage(); 143 break; 144 } 145 } 146 147 if (!is_aout) { 148 if (justread) 149 list_elf_hints(hints_file); 150 else 151 update_elf_hints(hints_file, argc - optind, 152 argv + optind, merge || rescan); 153 return 0; 154 } 155 156 /* Here begins the aout libs processing */ 157 dir_list = strdup(""); 158 159 if (justread || merge || rescan) { 160 if ((rval = readhints()) != 0) 161 return rval; 162 } 163 164 if (!nostd && !merge && !rescan) 165 std_search_path(); 166 167 /* Add any directories/files from the command line */ 168 if (!justread) { 169 for (i = optind; i < argc; i++) { 170 struct stat stbuf; 171 172 if (stat(argv[i], &stbuf) == -1) { 173 warn("%s", argv[i]); 174 rval = -1; 175 } else if (strcmp(argv[i], "/usr/lib") == 0) { 176 warnx("WARNING! '%s' can not be used", argv[i]); 177 rval = -1; 178 } else { 179 /* 180 * See if this is a directory-containing 181 * file instead of a directory 182 */ 183 if (S_ISREG(stbuf.st_mode)) 184 rval |= dofile(argv[i], 0); 185 else 186 add_search_path(argv[i]); 187 } 188 } 189 } 190 191 for (i = 0; i < n_search_dirs; i++) { 192 char *cp = concat(dir_list, *dir_list?":":"", search_dirs[i]); 193 free(dir_list); 194 dir_list = cp; 195 } 196 197 if (justread) { 198 listhints(); 199 return 0; 200 } 201 202 for (i = 0; i < n_search_dirs; i++) 203 rval |= dodir(search_dirs[i], 1); 204 205 rval |= buildhints(); 206 207 return rval; 208 } 209 210 static void 211 usage() 212 { 213 fprintf(stderr, 214 "usage: ldconfig [-aout | -elf] [-Rimrsv] [-f hints_file] [dir | file ...]\n"); 215 exit(1); 216 } 217 218 int 219 dofile(fname, silent) 220 char *fname; 221 int silent; 222 { 223 FILE *hfp; 224 char buf[MAXPATHLEN]; 225 int rval = 0; 226 char *cp, *sp; 227 228 if ((hfp = fopen(fname, "r")) == NULL) { 229 warn("%s", fname); 230 return -1; 231 } 232 233 while (fgets(buf, sizeof(buf), hfp)) { 234 cp = buf; 235 while (isspace(*cp)) 236 cp++; 237 if (*cp == '#' || *cp == '\0') 238 continue; 239 sp = cp; 240 while (!isspace(*cp) && *cp != '\0') 241 cp++; 242 243 if (*cp != '\n') { 244 *cp = '\0'; 245 warnx("%s: trailing characters ignored", sp); 246 } 247 248 *cp = '\0'; 249 250 rval |= dodir(sp, silent); 251 } 252 253 (void)fclose(hfp); 254 return rval; 255 } 256 257 int 258 dodir(dir, silent) 259 char *dir; 260 int silent; 261 { 262 DIR *dd; 263 struct dirent *dp; 264 char name[MAXPATHLEN]; 265 int dewey[MAXDEWEY], ndewey; 266 267 if ((dd = opendir(dir)) == NULL) { 268 if (silent && errno == ENOENT) /* Ignore the error */ 269 return 0; 270 warn("%s", dir); 271 return -1; 272 } 273 274 while ((dp = readdir(dd)) != NULL) { 275 register int n; 276 register char *cp; 277 278 /* Check for `lib' prefix */ 279 if (dp->d_name[0] != 'l' || 280 dp->d_name[1] != 'i' || 281 dp->d_name[2] != 'b') 282 continue; 283 284 /* Copy the entry minus prefix */ 285 (void)strcpy(name, dp->d_name + 3); 286 n = strlen(name); 287 if (n < 4) 288 continue; 289 290 /* Find ".so." in name */ 291 for (cp = name + n - 4; cp > name; --cp) { 292 if (cp[0] == '.' && 293 cp[1] == 's' && 294 cp[2] == 'o' && 295 cp[3] == '.') 296 break; 297 } 298 if (cp <= name) 299 continue; 300 301 *cp = '\0'; 302 if (!isdigit(*(cp+4))) 303 continue; 304 305 bzero((caddr_t)dewey, sizeof(dewey)); 306 ndewey = getdewey(dewey, cp + 4); 307 if (ndewey < 2) 308 continue; 309 enter(dir, dp->d_name, name, dewey, ndewey); 310 } 311 312 closedir(dd); 313 return 0; 314 } 315 316 static void 317 enter(dir, file, name, dewey, ndewey) 318 char *dir, *file, *name; 319 int dewey[], ndewey; 320 { 321 struct shlib_list *shp; 322 323 for (shp = shlib_head; shp; shp = shp->next) { 324 if (strcmp(name, shp->name) != 0 || major != shp->major) 325 continue; 326 327 /* Name matches existing entry */ 328 if (cmpndewey(dewey, ndewey, shp->dewey, shp->ndewey) > 0) { 329 330 /* Update this entry with higher versioned lib */ 331 if (verbose) 332 printf("Updating lib%s.%d.%d to %s/%s\n", 333 shp->name, shp->major, shp->minor, 334 dir, file); 335 336 free(shp->name); 337 shp->name = strdup(name); 338 free(shp->path); 339 shp->path = concat(dir, "/", file); 340 bcopy(dewey, shp->dewey, sizeof(shp->dewey)); 341 shp->ndewey = ndewey; 342 } 343 break; 344 } 345 346 if (shp) 347 /* Name exists: older version or just updated */ 348 return; 349 350 /* Allocate new list element */ 351 if (verbose) 352 printf("Adding %s/%s\n", dir, file); 353 354 shp = (struct shlib_list *)xmalloc(sizeof *shp); 355 shp->name = strdup(name); 356 shp->path = concat(dir, "/", file); 357 bcopy(dewey, shp->dewey, MAXDEWEY); 358 shp->ndewey = ndewey; 359 shp->next = NULL; 360 361 *shlib_tail = shp; 362 shlib_tail = &shp->next; 363 } 364 365 366 int 367 hinthash(cp, vmajor) 368 char *cp; 369 int vmajor; 370 { 371 int k = 0; 372 373 while (*cp) 374 k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff; 375 376 k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff; 377 378 return k; 379 } 380 381 int 382 buildhints() 383 { 384 struct hints_header hdr; 385 struct hints_bucket *blist; 386 struct shlib_list *shp; 387 char *strtab; 388 int i, n, str_index = 0; 389 int strtab_sz = 0; /* Total length of strings */ 390 int nhints = 0; /* Total number of hints */ 391 int fd; 392 char *tmpfile; 393 394 for (shp = shlib_head; shp; shp = shp->next) { 395 strtab_sz += 1 + strlen(shp->name); 396 strtab_sz += 1 + strlen(shp->path); 397 nhints++; 398 } 399 400 /* Fill hints file header */ 401 hdr.hh_magic = HH_MAGIC; 402 hdr.hh_version = LD_HINTS_VERSION_2; 403 hdr.hh_nbucket = 1 * nhints; 404 n = hdr.hh_nbucket * sizeof(struct hints_bucket); 405 hdr.hh_hashtab = sizeof(struct hints_header); 406 hdr.hh_strtab = hdr.hh_hashtab + n; 407 hdr.hh_dirlist = strtab_sz; 408 strtab_sz += 1 + strlen(dir_list); 409 hdr.hh_strtab_sz = strtab_sz; 410 hdr.hh_ehints = hdr.hh_strtab + hdr.hh_strtab_sz; 411 412 if (verbose) 413 printf("Totals: entries %d, buckets %ld, string size %d\n", 414 nhints, (long)hdr.hh_nbucket, strtab_sz); 415 416 /* Allocate buckets and string table */ 417 blist = (struct hints_bucket *)xmalloc(n); 418 bzero((char *)blist, n); 419 for (i = 0; i < hdr.hh_nbucket; i++) 420 /* Empty all buckets */ 421 blist[i].hi_next = -1; 422 423 strtab = (char *)xmalloc(strtab_sz); 424 425 /* Enter all */ 426 for (shp = shlib_head; shp; shp = shp->next) { 427 struct hints_bucket *bp; 428 429 bp = blist + 430 (hinthash(shp->name, shp->major) % hdr.hh_nbucket); 431 432 if (bp->hi_pathx) { 433 int i; 434 435 for (i = 0; i < hdr.hh_nbucket; i++) { 436 if (blist[i].hi_pathx == 0) 437 break; 438 } 439 if (i == hdr.hh_nbucket) { 440 warnx("bummer!"); 441 return -1; 442 } 443 while (bp->hi_next != -1) 444 bp = &blist[bp->hi_next]; 445 bp->hi_next = i; 446 bp = blist + i; 447 } 448 449 /* Insert strings in string table */ 450 bp->hi_namex = str_index; 451 strcpy(strtab + str_index, shp->name); 452 str_index += 1 + strlen(shp->name); 453 454 bp->hi_pathx = str_index; 455 strcpy(strtab + str_index, shp->path); 456 str_index += 1 + strlen(shp->path); 457 458 /* Copy versions */ 459 bcopy(shp->dewey, bp->hi_dewey, sizeof(bp->hi_dewey)); 460 bp->hi_ndewey = shp->ndewey; 461 } 462 463 /* Copy search directories */ 464 strcpy(strtab + str_index, dir_list); 465 str_index += 1 + strlen(dir_list); 466 467 /* Sanity check */ 468 if (str_index != strtab_sz) { 469 errx(1, "str_index(%d) != strtab_sz(%d)", str_index, strtab_sz); 470 } 471 472 tmpfile = concat(hints_file, ".XXXXXXXXXX", ""); 473 umask(0); /* Create with exact permissions */ 474 if ((fd = mkstemp(tmpfile)) == -1) { 475 warn("%s", tmpfile); 476 return -1; 477 } 478 fchmod(fd, 0444); 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