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.12 1996/02/26 02:22:33 pst 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 "ld.h" 55 56 #undef major 57 #undef minor 58 59 extern char *__progname; 60 61 static int verbose; 62 static int nostd; 63 static int justread; 64 static int merge; 65 66 struct shlib_list { 67 /* Internal list of shared libraries found */ 68 char *name; 69 char *path; 70 int dewey[MAXDEWEY]; 71 int ndewey; 72 #define major dewey[0] 73 #define minor dewey[1] 74 struct shlib_list *next; 75 }; 76 77 static struct shlib_list *shlib_head = NULL, **shlib_tail = &shlib_head; 78 79 static void enter __P((char *, char *, char *, int *, int)); 80 static int dodir __P((char *, int)); 81 static int buildhints __P((void)); 82 static int readhints __P((void)); 83 static void listhints __P((void)); 84 85 int 86 main(argc, argv) 87 int argc; 88 char *argv[]; 89 { 90 int i, c; 91 int rval = 0; 92 93 while ((c = getopt(argc, argv, "mrsv")) != EOF) { 94 switch (c) { 95 case 'm': 96 merge = 1; 97 break; 98 case 'r': 99 justread = 1; 100 break; 101 case 's': 102 nostd = 1; 103 break; 104 case 'v': 105 verbose = 1; 106 break; 107 default: 108 errx(1, "Usage: %s [-m][-r][-s][-v][dir ...]", 109 __progname); 110 break; 111 } 112 } 113 114 if (justread || merge) { 115 if ((rval = readhints()) != 0) 116 return rval; 117 if (justread) { 118 listhints(); 119 return 0; 120 } 121 } 122 123 if (!nostd) 124 std_search_path(); 125 126 for (i = 0; i < n_search_dirs; i++) 127 rval |= dodir(search_dirs[i], 1); 128 129 for (i = optind; i < argc; i++) 130 rval |= dodir(argv[i], 0); 131 132 rval |= buildhints(); 133 134 return rval; 135 } 136 137 int 138 dodir(dir, silent) 139 char *dir; 140 int silent; 141 { 142 DIR *dd; 143 struct dirent *dp; 144 char name[MAXPATHLEN]; 145 int dewey[MAXDEWEY], ndewey; 146 147 if ((dd = opendir(dir)) == NULL) { 148 if (!silent || errno != ENOENT) 149 warn("%s", dir); 150 return -1; 151 } 152 153 while ((dp = readdir(dd)) != NULL) { 154 register int n; 155 register char *cp; 156 157 /* Check for `lib' prefix */ 158 if (dp->d_name[0] != 'l' || 159 dp->d_name[1] != 'i' || 160 dp->d_name[2] != 'b') 161 continue; 162 163 /* Copy the entry minus prefix */ 164 (void)strcpy(name, dp->d_name + 3); 165 n = strlen(name); 166 if (n < 4) 167 continue; 168 169 /* Find ".so." in name */ 170 for (cp = name + n - 4; cp > name; --cp) { 171 if (cp[0] == '.' && 172 cp[1] == 's' && 173 cp[2] == 'o' && 174 cp[3] == '.') 175 break; 176 } 177 if (cp <= name) 178 continue; 179 180 *cp = '\0'; 181 if (!isdigit(*(cp+4))) 182 continue; 183 184 bzero((caddr_t)dewey, sizeof(dewey)); 185 ndewey = getdewey(dewey, cp + 4); 186 enter(dir, dp->d_name, name, dewey, ndewey); 187 } 188 189 return 0; 190 } 191 192 static void 193 enter(dir, file, name, dewey, ndewey) 194 char *dir, *file, *name; 195 int dewey[], ndewey; 196 { 197 struct shlib_list *shp; 198 199 for (shp = shlib_head; shp; shp = shp->next) { 200 if (strcmp(name, shp->name) != 0 || major != shp->major) 201 continue; 202 203 /* Name matches existing entry */ 204 if (cmpndewey(dewey, ndewey, shp->dewey, shp->ndewey) > 0) { 205 206 /* Update this entry with higher versioned lib */ 207 if (verbose) 208 printf("Updating lib%s.%d.%d to %s/%s\n", 209 shp->name, shp->major, shp->minor, 210 dir, file); 211 212 free(shp->name); 213 shp->name = strdup(name); 214 free(shp->path); 215 shp->path = concat(dir, "/", file); 216 bcopy(dewey, shp->dewey, sizeof(shp->dewey)); 217 shp->ndewey = ndewey; 218 } 219 break; 220 } 221 222 if (shp) 223 /* Name exists: older version or just updated */ 224 return; 225 226 /* Allocate new list element */ 227 if (verbose) 228 printf("Adding %s/%s\n", dir, file); 229 230 shp = (struct shlib_list *)xmalloc(sizeof *shp); 231 shp->name = strdup(name); 232 shp->path = concat(dir, "/", file); 233 bcopy(dewey, shp->dewey, MAXDEWEY); 234 shp->ndewey = ndewey; 235 shp->next = NULL; 236 237 *shlib_tail = shp; 238 shlib_tail = &shp->next; 239 } 240 241 242 #if DEBUG 243 /* test */ 244 #undef _PATH_LD_HINTS 245 #define _PATH_LD_HINTS "./ld.so.hints" 246 #endif 247 248 int 249 hinthash(cp, vmajor) 250 char *cp; 251 int vmajor; 252 { 253 int k = 0; 254 255 while (*cp) 256 k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff; 257 258 k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff; 259 260 return k; 261 } 262 263 int 264 buildhints() 265 { 266 struct hints_header hdr; 267 struct hints_bucket *blist; 268 struct shlib_list *shp; 269 char *strtab; 270 int i, n, str_index = 0; 271 int strtab_sz = 0; /* Total length of strings */ 272 int nhints = 0; /* Total number of hints */ 273 int fd; 274 char *tmpfile; 275 276 for (shp = shlib_head; shp; shp = shp->next) { 277 strtab_sz += 1 + strlen(shp->name); 278 strtab_sz += 1 + strlen(shp->path); 279 nhints++; 280 } 281 282 /* Fill hints file header */ 283 hdr.hh_magic = HH_MAGIC; 284 hdr.hh_version = LD_HINTS_VERSION_1; 285 hdr.hh_nbucket = 1 * nhints; 286 n = hdr.hh_nbucket * sizeof(struct hints_bucket); 287 hdr.hh_hashtab = sizeof(struct hints_header); 288 hdr.hh_strtab = hdr.hh_hashtab + n; 289 hdr.hh_strtab_sz = strtab_sz; 290 hdr.hh_ehints = hdr.hh_strtab + hdr.hh_strtab_sz; 291 292 if (verbose) 293 printf("Totals: entries %d, buckets %ld, string size %d\n", 294 nhints, hdr.hh_nbucket, strtab_sz); 295 296 /* Allocate buckets and string table */ 297 blist = (struct hints_bucket *)xmalloc(n); 298 bzero((char *)blist, n); 299 for (i = 0; i < hdr.hh_nbucket; i++) 300 /* Empty all buckets */ 301 blist[i].hi_next = -1; 302 303 strtab = (char *)xmalloc(strtab_sz); 304 305 /* Enter all */ 306 for (shp = shlib_head; shp; shp = shp->next) { 307 struct hints_bucket *bp; 308 309 bp = blist + 310 (hinthash(shp->name, shp->major) % hdr.hh_nbucket); 311 312 if (bp->hi_pathx) { 313 int i; 314 315 for (i = 0; i < hdr.hh_nbucket; i++) { 316 if (blist[i].hi_pathx == 0) 317 break; 318 } 319 if (i == hdr.hh_nbucket) { 320 warnx("Bummer!"); 321 return -1; 322 } 323 while (bp->hi_next != -1) 324 bp = &blist[bp->hi_next]; 325 bp->hi_next = i; 326 bp = blist + i; 327 } 328 329 /* Insert strings in string table */ 330 bp->hi_namex = str_index; 331 strcpy(strtab + str_index, shp->name); 332 str_index += 1 + strlen(shp->name); 333 334 bp->hi_pathx = str_index; 335 strcpy(strtab + str_index, shp->path); 336 str_index += 1 + strlen(shp->path); 337 338 /* Copy versions */ 339 bcopy(shp->dewey, bp->hi_dewey, sizeof(bp->hi_dewey)); 340 bp->hi_ndewey = shp->ndewey; 341 } 342 343 umask(022); /* ensure the file will be worl-readable */ 344 tmpfile = concat(_PATH_LD_HINTS, "+", ""); 345 if ((fd = open(tmpfile, O_RDWR|O_CREAT|O_TRUNC, 0444)) == -1) { 346 warn("%s", _PATH_LD_HINTS); 347 return -1; 348 } 349 350 if (write(fd, &hdr, sizeof(struct hints_header)) != 351 sizeof(struct hints_header)) { 352 warn("%s", _PATH_LD_HINTS); 353 return -1; 354 } 355 if (write(fd, blist, hdr.hh_nbucket * sizeof(struct hints_bucket)) != 356 hdr.hh_nbucket * sizeof(struct hints_bucket)) { 357 warn("%s", _PATH_LD_HINTS); 358 return -1; 359 } 360 if (write(fd, strtab, strtab_sz) != strtab_sz) { 361 warn("%s", _PATH_LD_HINTS); 362 return -1; 363 } 364 if (close(fd) != 0) { 365 warn("%s", _PATH_LD_HINTS); 366 return -1; 367 } 368 369 /* Install it */ 370 if (unlink(_PATH_LD_HINTS) != 0 && errno != ENOENT) { 371 warn("%s", _PATH_LD_HINTS); 372 return -1; 373 } 374 375 if (rename(tmpfile, _PATH_LD_HINTS) != 0) { 376 warn("%s", _PATH_LD_HINTS); 377 return -1; 378 } 379 380 return 0; 381 } 382 383 static int 384 readhints() 385 { 386 int fd; 387 caddr_t addr; 388 long msize; 389 struct hints_header *hdr; 390 struct hints_bucket *blist; 391 char *strtab; 392 struct shlib_list *shp; 393 int i; 394 395 if ((fd = open(_PATH_LD_HINTS, O_RDONLY, 0)) == -1) { 396 warn("%s", _PATH_LD_HINTS); 397 return -1; 398 } 399 400 msize = PAGSIZ; 401 addr = mmap(0, msize, PROT_READ, MAP_COPY, fd, 0); 402 403 if (addr == (caddr_t)-1) { 404 warn("%s", _PATH_LD_HINTS); 405 return -1; 406 } 407 408 hdr = (struct hints_header *)addr; 409 if (HH_BADMAG(*hdr)) { 410 warnx("%s: Bad magic: %o", 411 _PATH_LD_HINTS, hdr->hh_magic); 412 return -1; 413 } 414 415 if (hdr->hh_version != LD_HINTS_VERSION_1) { 416 warnx("Unsupported version: %d", hdr->hh_version); 417 return -1; 418 } 419 420 if (hdr->hh_ehints > msize) { 421 if (mmap(addr+msize, hdr->hh_ehints - msize, 422 PROT_READ, MAP_COPY|MAP_FIXED, 423 fd, msize) != (caddr_t)(addr+msize)) { 424 425 warn("%s", _PATH_LD_HINTS); 426 return -1; 427 } 428 } 429 close(fd); 430 431 blist = (struct hints_bucket *)(addr + hdr->hh_hashtab); 432 strtab = (char *)(addr + hdr->hh_strtab); 433 434 for (i = 0; i < hdr->hh_nbucket; i++) { 435 struct hints_bucket *bp = &blist[i]; 436 437 /* Sanity check */ 438 if (bp->hi_namex >= hdr->hh_strtab_sz) { 439 warnx("Bad name index: %#x", bp->hi_namex); 440 return -1; 441 } 442 if (bp->hi_pathx >= hdr->hh_strtab_sz) { 443 warnx("Bad path index: %#x", bp->hi_pathx); 444 return -1; 445 } 446 447 /* Allocate new list element */ 448 shp = (struct shlib_list *)xmalloc(sizeof *shp); 449 shp->name = strdup(strtab + bp->hi_namex); 450 shp->path = strdup(strtab + bp->hi_pathx); 451 bcopy(bp->hi_dewey, shp->dewey, sizeof(shp->dewey)); 452 shp->ndewey = bp->hi_ndewey; 453 shp->next = NULL; 454 455 *shlib_tail = shp; 456 shlib_tail = &shp->next; 457 } 458 459 return 0; 460 } 461 462 static void 463 listhints() 464 { 465 struct shlib_list *shp; 466 int i; 467 468 printf("%s:\n", _PATH_LD_HINTS); 469 470 for (i = 0, shp = shlib_head; shp; i++, shp = shp->next) 471 printf("\t%d:-l%s.%d.%d => %s\n", 472 i, shp->name, shp->major, shp->minor, shp->path); 473 474 return; 475 } 476