1 /* 2 * Copyright (c) 2000, Boris Popov 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 Boris Popov. 16 * 4. Neither the name of the author nor the names of any co-contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * $FreeBSD$ 33 */ 34 35 #include <sys/types.h> 36 #include <sys/param.h> 37 #include <sys/endian.h> 38 #include <sys/exec.h> 39 #include <sys/queue.h> 40 #include <sys/kernel.h> 41 #include <sys/reboot.h> 42 #include <sys/linker.h> 43 #include <sys/stat.h> 44 #include <sys/module.h> 45 #define FREEBSD_ELF 46 #include <err.h> 47 #include <fts.h> 48 #include <string.h> 49 #include <machine/elf.h> 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <unistd.h> 53 #include <errno.h> 54 55 #include "ef.h" 56 57 #define MAXRECSIZE (64 << 10) /* 64k */ 58 #define check(val) if ((error = (val)) != 0) break 59 60 static int dflag; /* do not create a hint file, only write on stdout */ 61 static int verbose; 62 63 static FILE *fxref; /* current hints file */ 64 65 static const char *xref_file = "linker.hints"; 66 67 /* 68 * A record is stored in the static buffer recbuf before going to disk. 69 */ 70 static char recbuf[MAXRECSIZE]; 71 static int recpos; /* current write position */ 72 static int reccnt; /* total record written to this file so far */ 73 74 static void 75 intalign(void) 76 { 77 recpos = (recpos + sizeof(int) - 1) & ~(sizeof(int) - 1); 78 } 79 80 static void 81 record_start(void) 82 { 83 recpos = 0; 84 memset(recbuf, 0, MAXRECSIZE); 85 } 86 87 static int 88 record_end(void) 89 { 90 if (recpos == 0) 91 return 0; 92 reccnt++; 93 intalign(); 94 fwrite(&recpos, sizeof(recpos), 1, fxref); 95 return fwrite(recbuf, recpos, 1, fxref) != 1 ? errno : 0; 96 } 97 98 static int 99 record_buf(const void *buf, int size) 100 { 101 if (MAXRECSIZE - recpos < size) 102 errx(1, "record buffer overflow"); 103 memcpy(recbuf + recpos, buf, size); 104 recpos += size; 105 return 0; 106 } 107 108 /* 109 * An int is stored in host order and aligned 110 */ 111 static int 112 record_int(int val) 113 { 114 intalign(); 115 return record_buf(&val, sizeof(val)); 116 } 117 118 /* 119 * A string is stored as 1-byte length plus data, no padding 120 */ 121 static int 122 record_string(const char *str) 123 { 124 int len, error; 125 u_char val; 126 127 if (dflag) 128 return 0; 129 val = len = strlen(str); 130 if (len > 255) 131 errx(1, "string %s too long", str); 132 error = record_buf(&val, sizeof(val)); 133 if (error) 134 return error; 135 return record_buf(str, len); 136 } 137 138 /* From sys/isa/pnp.c */ 139 static char * 140 pnp_eisaformat(uint32_t id) 141 { 142 uint8_t *data; 143 static char idbuf[8]; 144 const char hextoascii[] = "0123456789abcdef"; 145 146 id = htole32(id); 147 data = (uint8_t *)&id; 148 idbuf[0] = '@' + ((data[0] & 0x7c) >> 2); 149 idbuf[1] = '@' + (((data[0] & 0x3) << 3) + ((data[1] & 0xe0) >> 5)); 150 idbuf[2] = '@' + (data[1] & 0x1f); 151 idbuf[3] = hextoascii[(data[2] >> 4)]; 152 idbuf[4] = hextoascii[(data[2] & 0xf)]; 153 idbuf[5] = hextoascii[(data[3] >> 4)]; 154 idbuf[6] = hextoascii[(data[3] & 0xf)]; 155 idbuf[7] = 0; 156 return(idbuf); 157 } 158 159 struct pnp_elt 160 { 161 int pe_kind; /* What kind of entry */ 162 #define TYPE_SZ_MASK 0x0f 163 #define TYPE_FLAGGED 0x10 /* all f's is a wildcard */ 164 #define TYPE_INT 0x20 /* Is a number */ 165 #define TYPE_PAIRED 0x40 166 #define TYPE_LE 0x80 /* Matches <= this value */ 167 #define TYPE_GE 0x100 /* Matches >= this value */ 168 #define TYPE_MASK 0x200 /* Specifies a mask to follow */ 169 #define TYPE_U8 (1 | TYPE_INT) 170 #define TYPE_V8 (1 | TYPE_INT | TYPE_FLAGGED) 171 #define TYPE_G16 (2 | TYPE_INT | TYPE_GE) 172 #define TYPE_L16 (2 | TYPE_INT | TYPE_LE) 173 #define TYPE_M16 (2 | TYPE_INT | TYPE_MASK) 174 #define TYPE_U16 (2 | TYPE_INT) 175 #define TYPE_V16 (2 | TYPE_INT | TYPE_FLAGGED) 176 #define TYPE_U32 (4 | TYPE_INT) 177 #define TYPE_V32 (4 | TYPE_INT | TYPE_FLAGGED) 178 #define TYPE_W32 (4 | TYPE_INT | TYPE_PAIRED) 179 #define TYPE_D 7 180 #define TYPE_Z 8 181 #define TYPE_P 9 182 #define TYPE_E 10 183 #define TYPE_T 11 184 int pe_offset; /* Offset within the element */ 185 char * pe_key; /* pnp key name */ 186 TAILQ_ENTRY(pnp_elt) next; /* Link */ 187 }; 188 typedef TAILQ_HEAD(pnp_head, pnp_elt) pnp_list; 189 190 /* 191 * this function finds the data from the pnp table, as described by the 192 * the description and creates a new output (new_desc). This output table 193 * is a form that's easier for the agent that's automatically loading the 194 * modules. 195 * 196 * The format output is the simplified string from this routine in the 197 * same basic format as the pnp string, as documented in sys/module.h. 198 * First a string describing the format is output, the a count of the 199 * number of records, then each record. The format string also describes 200 * the length of each entry (though it isn't a fixed length when strings 201 * are present). 202 * 203 * type Output Meaning 204 * I uint32_t Integer equality comparison 205 * J uint32_t Pair of uint16_t fields converted to native 206 byte order. The two fields both must match. 207 * G uint32_t Greater than or equal to 208 * L uint32_t Less than or equal to 209 * M uint32_t Mask of which fields to test. Fields that 210 take up space increment the count. This 211 field must be first, and resets the count. 212 * D string Description of the device this pnp info is for 213 * Z string pnp string must match this 214 * T nothing T fields set pnp values that must be true for 215 * the entire table. 216 * Values are packed the same way that other values are packed in this file. 217 * Strings and int32_t's start on a 32-bit boundary and are padded with 0 218 * bytes. Objects that are smaller than uint32_t are converted, without 219 * sign extension to uint32_t to simplify parsing downstream. 220 */ 221 static int 222 parse_pnp_list(const char *desc, char **new_desc, pnp_list *list) 223 { 224 const char *walker = desc, *ep = desc + strlen(desc); 225 const char *colon, *semi; 226 struct pnp_elt *elt; 227 char *nd; 228 char type[8], key[32]; 229 int off; 230 231 off = 0; 232 nd = *new_desc = malloc(strlen(desc) + 1); 233 if (verbose > 1) 234 printf("Converting %s into a list\n", desc); 235 while (walker < ep) { 236 colon = strchr(walker, ':'); 237 semi = strchr(walker, ';'); 238 if (semi != NULL && semi < colon) 239 goto err; 240 if (colon - walker > sizeof(type)) 241 goto err; 242 strncpy(type, walker, colon - walker); 243 type[colon - walker] = '\0'; 244 if (semi) { 245 if (semi - colon >= sizeof(key)) 246 goto err; 247 strncpy(key, colon + 1, semi - colon - 1); 248 key[semi - colon - 1] = '\0'; 249 walker = semi + 1; 250 } else { 251 if (strlen(colon + 1) >= sizeof(key)) 252 goto err; 253 strcpy(key, colon + 1); 254 walker = ep; 255 } 256 if (verbose > 1) 257 printf("Found type %s for name %s\n", type, key); 258 /* Skip pointer place holders */ 259 if (strcmp(type, "P") == 0) { 260 off += sizeof(void *); 261 continue; 262 } 263 264 /* 265 * Add a node of the appropriate type 266 */ 267 elt = malloc(sizeof(struct pnp_elt) + strlen(key) + 1); 268 TAILQ_INSERT_TAIL(list, elt, next); 269 elt->pe_key = (char *)(elt + 1); 270 elt->pe_offset = off; 271 if (strcmp(type, "U8") == 0) 272 elt->pe_kind = TYPE_U8; 273 else if (strcmp(type, "V8") == 0) 274 elt->pe_kind = TYPE_V8; 275 else if (strcmp(type, "G16") == 0) 276 elt->pe_kind = TYPE_G16; 277 else if (strcmp(type, "L16") == 0) 278 elt->pe_kind = TYPE_L16; 279 else if (strcmp(type, "M16") == 0) 280 elt->pe_kind = TYPE_M16; 281 else if (strcmp(type, "U16") == 0) 282 elt->pe_kind = TYPE_U16; 283 else if (strcmp(type, "V16") == 0) 284 elt->pe_kind = TYPE_V16; 285 else if (strcmp(type, "U32") == 0) 286 elt->pe_kind = TYPE_U32; 287 else if (strcmp(type, "V32") == 0) 288 elt->pe_kind = TYPE_V32; 289 else if (strcmp(type, "W32") == 0) 290 elt->pe_kind = TYPE_W32; 291 else if (strcmp(type, "D") == 0) /* description char * */ 292 elt->pe_kind = TYPE_D; 293 else if (strcmp(type, "Z") == 0) /* char * to match */ 294 elt->pe_kind = TYPE_Z; 295 else if (strcmp(type, "P") == 0) /* Pointer -- ignored */ 296 elt->pe_kind = TYPE_P; 297 else if (strcmp(type, "E") == 0) /* EISA PNP ID, as uint32_t */ 298 elt->pe_kind = TYPE_E; 299 else if (strcmp(type, "T") == 0) 300 elt->pe_kind = TYPE_T; 301 else 302 goto err; 303 /* 304 * Maybe the rounding here needs to be more nuanced and/or somehow 305 * architecture specific. Fortunately, most tables in the system 306 * have sane ordering of types. 307 */ 308 if (elt->pe_kind & TYPE_INT) { 309 elt->pe_offset = roundup2(elt->pe_offset, elt->pe_kind & TYPE_SZ_MASK); 310 off = elt->pe_offset + (elt->pe_kind & TYPE_SZ_MASK); 311 } else if (elt->pe_kind == TYPE_E) { 312 /* Type E stored as Int, displays as string */ 313 elt->pe_offset = roundup2(elt->pe_offset, sizeof(uint32_t)); 314 off = elt->pe_offset + sizeof(uint32_t); 315 } else if (elt->pe_kind == TYPE_T) { 316 /* doesn't actually consume space in the table */ 317 off = elt->pe_offset; 318 } else { 319 elt->pe_offset = roundup2(elt->pe_offset, sizeof(void *)); 320 off = elt->pe_offset + sizeof(void *); 321 } 322 if (elt->pe_kind & TYPE_PAIRED) { 323 char *word, *ctx; 324 325 for (word = strtok_r(key, "/", &ctx); 326 word; word = strtok_r(NULL, "/", &ctx)) { 327 sprintf(nd, "%c:%s;", elt->pe_kind & TYPE_FLAGGED ? 'J' : 'I', 328 word); 329 nd += strlen(nd); 330 } 331 332 } 333 else { 334 if (elt->pe_kind & TYPE_FLAGGED) 335 *nd++ = 'J'; 336 else if (elt->pe_kind & TYPE_GE) 337 *nd++ = 'G'; 338 else if (elt->pe_kind & TYPE_LE) 339 *nd++ = 'L'; 340 else if (elt->pe_kind & TYPE_MASK) 341 *nd++ = 'M'; 342 else if (elt->pe_kind & TYPE_INT) 343 *nd++ = 'I'; 344 else if (elt->pe_kind == TYPE_D) 345 *nd++ = 'D'; 346 else if (elt->pe_kind == TYPE_Z || elt->pe_kind == TYPE_E) 347 *nd++ = 'Z'; 348 else if (elt->pe_kind == TYPE_T) 349 *nd++ = 'T'; 350 else 351 errx(1, "Impossible type %x\n", elt->pe_kind); 352 *nd++ = ':'; 353 strcpy(nd, key); 354 nd += strlen(nd); 355 *nd++ = ';'; 356 } 357 } 358 *nd++ = '\0'; 359 return 0; 360 err: 361 errx(1, "Parse error of description string %s", desc); 362 } 363 364 static int 365 parse_entry(struct mod_metadata *md, const char *cval, 366 struct elf_file *ef, const char *kldname) 367 { 368 struct mod_depend mdp; 369 struct mod_version mdv; 370 struct mod_pnp_match_info pnp; 371 char descr[1024]; 372 Elf_Off data = (Elf_Off)md->md_data; 373 int error = 0, i, len; 374 char *walker; 375 void *table; 376 377 record_start(); 378 switch (md->md_type) { 379 case MDT_DEPEND: 380 if (!dflag) 381 break; 382 check(EF_SEG_READ(ef, data, sizeof(mdp), &mdp)); 383 printf(" depends on %s.%d (%d,%d)\n", cval, 384 mdp.md_ver_preferred, mdp.md_ver_minimum, mdp.md_ver_maximum); 385 break; 386 case MDT_VERSION: 387 check(EF_SEG_READ(ef, data, sizeof(mdv), &mdv)); 388 if (dflag) { 389 printf(" interface %s.%d\n", cval, mdv.mv_version); 390 } else { 391 record_int(MDT_VERSION); 392 record_string(cval); 393 record_int(mdv.mv_version); 394 record_string(kldname); 395 } 396 break; 397 case MDT_MODULE: 398 if (dflag) { 399 printf(" module %s\n", cval); 400 } else { 401 record_int(MDT_MODULE); 402 record_string(cval); 403 record_string(kldname); 404 } 405 break; 406 case MDT_PNP_INFO: 407 check(EF_SEG_READ_REL(ef, data, sizeof(pnp), &pnp)); 408 check(EF_SEG_READ(ef, (Elf_Off)pnp.descr, sizeof(descr), descr)); 409 descr[sizeof(descr) - 1] = '\0'; 410 if (dflag) { 411 printf(" pnp info for bus %s format %s %d entries of %d bytes\n", 412 cval, descr, pnp.num_entry, pnp.entry_len); 413 } else { 414 pnp_list list; 415 struct pnp_elt *elt, *elt_tmp; 416 char *new_descr; 417 418 if (verbose > 1) 419 printf(" pnp info for bus %s format %s %d entries of %d bytes\n", 420 cval, descr, pnp.num_entry, pnp.entry_len); 421 /* 422 * Parse descr to weed out the chaff and to create a list 423 * of offsets to output. 424 */ 425 TAILQ_INIT(&list); 426 parse_pnp_list(descr, &new_descr, &list); 427 record_int(MDT_PNP_INFO); 428 record_string(cval); 429 record_string(new_descr); 430 record_int(pnp.num_entry); 431 len = pnp.num_entry * pnp.entry_len; 432 walker = table = malloc(len); 433 check(EF_SEG_READ_REL(ef, (Elf_Off)pnp.table, len, table)); 434 435 /* 436 * Walk the list and output things. We've collapsed all the 437 * variant forms of the table down to just ints and strings. 438 */ 439 for (i = 0; i < pnp.num_entry; i++) { 440 TAILQ_FOREACH(elt, &list, next) { 441 uint8_t v1; 442 uint16_t v2; 443 uint32_t v4; 444 int value; 445 char buffer[1024]; 446 447 if (elt->pe_kind == TYPE_W32) { 448 memcpy(&v4, walker + elt->pe_offset, sizeof(v4)); 449 value = v4 & 0xffff; 450 record_int(value); 451 if (verbose > 1) 452 printf("W32:%#x", value); 453 value = (v4 >> 16) & 0xffff; 454 record_int(value); 455 if (verbose > 1) 456 printf(":%#x;", value); 457 } else if (elt->pe_kind & TYPE_INT) { 458 switch (elt->pe_kind & TYPE_SZ_MASK) { 459 case 1: 460 memcpy(&v1, walker + elt->pe_offset, sizeof(v1)); 461 if ((elt->pe_kind & TYPE_FLAGGED) && v1 == 0xff) 462 value = -1; 463 else 464 value = v1; 465 break; 466 case 2: 467 memcpy(&v2, walker + elt->pe_offset, sizeof(v2)); 468 if ((elt->pe_kind & TYPE_FLAGGED) && v2 == 0xffff) 469 value = -1; 470 else 471 value = v2; 472 break; 473 case 4: 474 memcpy(&v4, walker + elt->pe_offset, sizeof(v4)); 475 if ((elt->pe_kind & TYPE_FLAGGED) && v4 == 0xffffffff) 476 value = -1; 477 else 478 value = v4; 479 break; 480 default: 481 errx(1, "Invalid size somehow %#x", elt->pe_kind); 482 } 483 if (verbose > 1) 484 printf("I:%#x;", value); 485 record_int(value); 486 } else if (elt->pe_kind == TYPE_T) { 487 /* Do nothing */ 488 } else { /* E, Z or D -- P already filtered */ 489 if (elt->pe_kind == TYPE_E) { 490 memcpy(&v4, walker + elt->pe_offset, sizeof(v4)); 491 strcpy(buffer, pnp_eisaformat(v4)); 492 } else { 493 char *ptr; 494 495 ptr = *(char **)(walker + elt->pe_offset); 496 buffer[0] = '\0'; 497 if (ptr != 0) { 498 EF_SEG_READ(ef, (Elf_Off)ptr, 499 sizeof(buffer), buffer); 500 buffer[sizeof(buffer) - 1] = '\0'; 501 } 502 } 503 if (verbose > 1) 504 printf("%c:%s;", elt->pe_kind == TYPE_E ? 'E' : (elt->pe_kind == TYPE_Z ? 'Z' : 'D'), buffer); 505 record_string(buffer); 506 } 507 } 508 if (verbose > 1) 509 printf("\n"); 510 walker += pnp.entry_len; 511 } 512 /* Now free it */ 513 TAILQ_FOREACH_SAFE(elt, &list, next, elt_tmp) { 514 TAILQ_REMOVE(&list, elt, next); 515 free(elt); 516 } 517 free(table); 518 } 519 break; 520 default: 521 warnx("unknown metadata record %d in file %s", md->md_type, kldname); 522 } 523 if (!error) 524 record_end(); 525 return error; 526 } 527 528 static int 529 read_kld(char *filename, char *kldname) 530 { 531 struct mod_metadata md; 532 struct elf_file ef; 533 void **p, **orgp; 534 int error, eftype, nmlen; 535 long start, finish, entries; 536 char kldmodname[MAXMODNAME + 1], cval[MAXMODNAME + 1], *cp; 537 538 if (verbose || dflag) 539 printf("%s\n", filename); 540 error = ef_open(filename, &ef, verbose); 541 if (error) { 542 error = ef_obj_open(filename, &ef, verbose); 543 if (error) { 544 if (verbose) 545 warnc(error, "elf_open(%s)", filename); 546 return error; 547 } 548 } 549 eftype = EF_GET_TYPE(&ef); 550 if (eftype != EFT_KLD && eftype != EFT_KERNEL) { 551 EF_CLOSE(&ef); 552 return 0; 553 } 554 if (!dflag) { 555 cp = strrchr(kldname, '.'); 556 nmlen = (cp != NULL) ? cp - kldname : (int)strlen(kldname); 557 if (nmlen > MAXMODNAME) 558 nmlen = MAXMODNAME; 559 strlcpy(kldmodname, kldname, nmlen); 560 /* fprintf(fxref, "%s:%s:%d\n", kldmodname, kldname, 0);*/ 561 } 562 do { 563 check(EF_LOOKUP_SET(&ef, MDT_SETNAME, &start, &finish, 564 &entries)); 565 check(EF_SEG_READ_ENTRY_REL(&ef, start, sizeof(*p) * entries, 566 (void *)&p)); 567 orgp = p; 568 while(entries--) { 569 check(EF_SEG_READ_REL(&ef, (Elf_Off)*p, sizeof(md), 570 &md)); 571 p++; 572 check(EF_SEG_READ(&ef, (Elf_Off)md.md_cval, 573 sizeof(cval), cval)); 574 cval[MAXMODNAME] = '\0'; 575 parse_entry(&md, cval, &ef, kldname); 576 } 577 if (error) 578 warnc(error, "error while reading %s", filename); 579 free(orgp); 580 } while(0); 581 EF_CLOSE(&ef); 582 return error; 583 } 584 585 /* 586 * Create a temp file in directory root, make sure we don't 587 * overflow the buffer for the destination name 588 */ 589 static FILE * 590 maketempfile(char *dest, const char *root) 591 { 592 char *p; 593 int n, fd; 594 595 p = strrchr(root, '/'); 596 n = p != NULL ? p - root + 1 : 0; 597 if (snprintf(dest, MAXPATHLEN, "%.*slhint.XXXXXX", n, root) >= 598 MAXPATHLEN) { 599 errno = ENAMETOOLONG; 600 return NULL; 601 } 602 603 fd = mkstemp(dest); 604 if (fd < 0) 605 return NULL; 606 fchmod(fd, 0644); /* nothing secret in the file */ 607 return fdopen(fd, "w+"); 608 } 609 610 static char xrefname[MAXPATHLEN], tempname[MAXPATHLEN]; 611 612 static void 613 usage(void) 614 { 615 616 fprintf(stderr, "%s\n", 617 "usage: kldxref [-Rdv] [-f hintsfile] path ..." 618 ); 619 exit(1); 620 } 621 622 static int 623 compare(const FTSENT *const *a, const FTSENT *const *b) 624 { 625 if ((*a)->fts_info == FTS_D && (*b)->fts_info != FTS_D) 626 return 1; 627 if ((*a)->fts_info != FTS_D && (*b)->fts_info == FTS_D) 628 return -1; 629 return strcmp((*a)->fts_name, (*b)->fts_name); 630 } 631 632 int 633 main(int argc, char *argv[]) 634 { 635 FTS *ftsp; 636 FTSENT *p; 637 int opt, fts_options, ival; 638 struct stat sb; 639 640 fts_options = FTS_PHYSICAL; 641 642 while ((opt = getopt(argc, argv, "Rdf:v")) != -1) { 643 switch (opt) { 644 case 'd': /* no hint file, only print on stdout */ 645 dflag = 1; 646 break; 647 case 'f': /* use this name instead of linker.hints */ 648 xref_file = optarg; 649 break; 650 case 'v': 651 verbose++; 652 break; 653 case 'R': /* recurse on directories */ 654 fts_options |= FTS_COMFOLLOW; 655 break; 656 default: 657 usage(); 658 /* NOTREACHED */ 659 } 660 } 661 if (argc - optind < 1) 662 usage(); 663 argc -= optind; 664 argv += optind; 665 666 if (stat(argv[0], &sb) != 0) 667 err(1, "%s", argv[0]); 668 if ((sb.st_mode & S_IFDIR) == 0) { 669 errno = ENOTDIR; 670 err(1, "%s", argv[0]); 671 } 672 673 ftsp = fts_open(argv, fts_options, compare); 674 if (ftsp == NULL) 675 exit(1); 676 677 for (;;) { 678 p = fts_read(ftsp); 679 if ((p == NULL || p->fts_info == FTS_D) && fxref) { 680 /* close and rename the current hint file */ 681 fclose(fxref); 682 fxref = NULL; 683 if (reccnt) { 684 rename(tempname, xrefname); 685 } else { 686 /* didn't find any entry, ignore this file */ 687 unlink(tempname); 688 unlink(xrefname); 689 } 690 } 691 if (p == NULL) 692 break; 693 if (p->fts_info == FTS_D && !dflag) { 694 /* visiting a new directory, create a new hint file */ 695 snprintf(xrefname, sizeof(xrefname), "%s/%s", 696 ftsp->fts_path, xref_file); 697 fxref = maketempfile(tempname, ftsp->fts_path); 698 if (fxref == NULL) 699 err(1, "can't create %s", tempname); 700 ival = 1; 701 fwrite(&ival, sizeof(ival), 1, fxref); 702 reccnt = 0; 703 } 704 /* skip non-files and separate debug files */ 705 if (p->fts_info != FTS_F) 706 continue; 707 if (p->fts_namelen >= 6 && 708 strcmp(p->fts_name + p->fts_namelen - 6, ".debug") == 0) 709 continue; 710 if (p->fts_namelen >= 8 && 711 strcmp(p->fts_name + p->fts_namelen - 8, ".symbols") == 0) 712 continue; 713 read_kld(p->fts_path, p->fts_name); 714 } 715 fts_close(ftsp); 716 return 0; 717 } 718