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