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