1 /* $NetBSD: main.c,v 1.99 2020/03/07 19:26:13 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This software was developed by the Computer Systems Engineering group 8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 9 * contributed to Berkeley. 10 * 11 * All advertising materials mentioning features or use of this software 12 * must display the following acknowledgement: 13 * This product includes software developed by the University of 14 * California, Lawrence Berkeley Laboratories. 15 * 16 * Redistribution and use in source and binary forms, with or without 17 * modification, are permitted provided that the following conditions 18 * are met: 19 * 1. Redistributions of source code must retain the above copyright 20 * notice, this list of conditions and the following disclaimer. 21 * 2. Redistributions in binary form must reproduce the above copyright 22 * notice, this list of conditions and the following disclaimer in the 23 * documentation and/or other materials provided with the distribution. 24 * 3. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * from: @(#)main.c 8.1 (Berkeley) 6/6/93 41 */ 42 43 #if HAVE_NBTOOL_CONFIG_H 44 #include "nbtool_config.h" 45 #endif 46 47 #include <sys/cdefs.h> 48 __RCSID("$NetBSD: main.c,v 1.99 2020/03/07 19:26:13 christos Exp $"); 49 50 #ifndef MAKE_BOOTSTRAP 51 #include <sys/cdefs.h> 52 #define COPYRIGHT(x) __COPYRIGHT(x) 53 #else 54 #define COPYRIGHT(x) static const char copyright[] = x 55 #endif 56 57 #ifndef lint 58 COPYRIGHT("@(#) Copyright (c) 1992, 1993\ 59 The Regents of the University of California. All rights reserved."); 60 #endif /* not lint */ 61 62 #include <sys/types.h> 63 #include <sys/stat.h> 64 #include <sys/param.h> 65 #include <sys/mman.h> 66 #if !HAVE_NBTOOL_CONFIG_H 67 #include <sys/sysctl.h> 68 #endif 69 #include <paths.h> 70 #include <ctype.h> 71 #include <err.h> 72 #include <errno.h> 73 #include <fcntl.h> 74 #include <limits.h> 75 #include <stdio.h> 76 #include <stdlib.h> 77 #include <string.h> 78 #include <unistd.h> 79 #include <vis.h> 80 #include <util.h> 81 82 #include "defs.h" 83 #include "sem.h" 84 85 #ifndef LINE_MAX 86 #define LINE_MAX 1024 87 #endif 88 89 int vflag; /* verbose output */ 90 int Pflag; /* pack locators */ 91 int Lflag; /* lint config generation */ 92 int Mflag; /* modular build */ 93 int Sflag; /* suffix rules & subdirectory */ 94 int handling_cmdlineopts; /* currently processing -D/-U options */ 95 96 int yyparse(void); 97 98 #if !defined(MAKE_BOOTSTRAP) && defined(YYDEBUG) 99 extern int yydebug; 100 #endif 101 int dflag; 102 103 static struct dlhash *obsopttab; 104 static struct hashtab *mkopttab; 105 static struct nvlist **nextopt; 106 static struct nvlist **nextmkopt; 107 static struct nvlist **nextappmkopt; 108 static struct nvlist **nextcndmkopt; 109 static struct nvlist **nextfsopt; 110 static struct nvlist *cmdlinedefs, *cmdlineundefs; 111 112 static void usage(void) __dead; 113 static void dependopts(void); 114 static void dependopts_one(const char *); 115 static void do_depends(struct nvlist *); 116 static void do_depend(struct nvlist *); 117 static void stop(void); 118 static int do_option(struct hashtab *, struct nvlist **, 119 struct nvlist ***, const char *, const char *, 120 const char *, struct hashtab *); 121 static int undo_option(struct hashtab *, struct nvlist **, 122 struct nvlist ***, const char *, const char *, int); 123 static int crosscheck(void); 124 static int badstar(void); 125 static int mkallsubdirs(void); 126 static int mksymlinks(void); 127 static int mkident(void); 128 static int devbase_has_dead_instances(const char *, void *, void *); 129 static int devbase_has_any_instance(struct devbase *, int, int, int); 130 static int check_dead_devi(const char *, void *, void *); 131 static void add_makeopt(const char *); 132 static void remove_makeopt(const char *); 133 static void handle_cmdline_makeoptions(void); 134 static void kill_orphans(void); 135 static void do_kill_orphans(struct devbase *, struct attr *, 136 struct devbase *, int); 137 static int kill_orphans_cb(const char *, void *, void *); 138 static int cfcrosscheck(struct config *, const char *, struct nvlist *); 139 static void defopt(struct dlhash *ht, const char *fname, 140 struct defoptlist *opts, struct nvlist *deps, int obs); 141 static struct defoptlist *find_declared_option_option(const char *name); 142 static struct nvlist *find_declared_fs_option(const char *name); 143 144 #define LOGCONFIG_LARGE "INCLUDE_CONFIG_FILE" 145 #define LOGCONFIG_SMALL "INCLUDE_JUST_CONFIG" 146 147 static void logconfig_start(void); 148 static void logconfig_end(void); 149 static FILE *cfg; 150 static time_t cfgtime; 151 152 static int is_elf(const char *); 153 static int extract_config(const char *, const char *, int); 154 155 int badfilename(const char *fname); 156 157 const char *progname; 158 extern const char *yyfile; 159 160 int 161 main(int argc, char **argv) 162 { 163 char *p, cname[PATH_MAX]; 164 const char *last_component; 165 int pflag, xflag, ch, removeit; 166 167 setprogname(argv[0]); 168 169 pflag = 0; 170 xflag = 0; 171 while ((ch = getopt(argc, argv, "D:LMPSU:dgpvb:s:x")) != -1) { 172 switch (ch) { 173 174 case 'd': 175 #if !defined(MAKE_BOOTSTRAP) && defined(YYDEBUG) 176 yydebug = 1; 177 #endif 178 dflag++; 179 break; 180 181 case 'M': 182 Mflag = 1; 183 break; 184 185 case 'L': 186 Lflag = 1; 187 break; 188 189 case 'P': 190 Pflag = 1; 191 break; 192 193 case 'g': 194 /* 195 * In addition to DEBUG, you probably wanted to 196 * set "options KGDB" and maybe others. We could 197 * do that for you, but you really should just 198 * put them in the config file. 199 */ 200 warnx("-g is obsolete (use -D DEBUG=\"-g\")"); 201 usage(); 202 /*NOTREACHED*/ 203 204 case 'p': 205 /* 206 * Essentially the same as makeoptions PROF="-pg", 207 * but also changes the path from ../../compile/FOO 208 * to ../../compile/FOO.PROF; i.e., compile a 209 * profiling kernel based on a typical "regular" 210 * kernel. 211 * 212 * Note that if you always want profiling, you 213 * can (and should) use a "makeoptions" line. 214 */ 215 pflag = 1; 216 break; 217 218 case 'v': 219 vflag = 1; 220 break; 221 222 case 'b': 223 builddir = optarg; 224 break; 225 226 case 's': 227 srcdir = optarg; 228 break; 229 230 case 'S': 231 Sflag = 1; 232 break; 233 234 case 'x': 235 xflag = 1; 236 break; 237 238 case 'D': 239 add_makeopt(optarg); 240 break; 241 242 case 'U': 243 remove_makeopt(optarg); 244 break; 245 246 case '?': 247 default: 248 usage(); 249 } 250 } 251 252 if (xflag && optind != 2) { 253 errx(EXIT_FAILURE, "-x must be used alone"); 254 } 255 256 argc -= optind; 257 argv += optind; 258 if (argc > 1) { 259 usage(); 260 } 261 262 if (Lflag && (builddir != NULL || Pflag || pflag)) 263 errx(EXIT_FAILURE, "-L can only be used with -s and -v"); 264 265 if (xflag) { 266 if (argc == 0) { 267 #if !HAVE_NBTOOL_CONFIG_H 268 char path_unix[MAXPATHLEN]; 269 size_t len = sizeof(path_unix) - 1; 270 path_unix[0] = '/'; 271 272 conffile = sysctlbyname("machdep.booted_kernel", 273 &path_unix[1], &len, NULL, 0) == -1 ? _PATH_UNIX : 274 path_unix; 275 #else 276 errx(EXIT_FAILURE, "no kernel supplied"); 277 #endif 278 } else 279 conffile = argv[0]; 280 if (!is_elf(conffile)) 281 errx(EXIT_FAILURE, "%s: not a binary kernel", 282 conffile); 283 if (!extract_config(conffile, "stdout", STDOUT_FILENO)) 284 errx(EXIT_FAILURE, "%s does not contain embedded " 285 "configuration data", conffile); 286 exit(0); 287 } 288 289 conffile = (argc == 1) ? argv[0] : "CONFIG"; 290 if (firstfile(conffile)) { 291 err(EXIT_FAILURE, "Cannot read `%s'", conffile); 292 exit(2); 293 } 294 295 /* 296 * Init variables. 297 */ 298 minmaxusers = 1; 299 maxmaxusers = 10000; 300 initintern(); 301 ident = NULL; 302 devbasetab = ht_new(); 303 devroottab = ht_new(); 304 devatab = ht_new(); 305 devitab = ht_new(); 306 deaddevitab = ht_new(); 307 selecttab = ht_new(); 308 needcnttab = ht_new(); 309 opttab = ht_new(); 310 mkopttab = ht_new(); 311 fsopttab = ht_new(); 312 deffstab = nvhash_create(); 313 defopttab = dlhash_create(); 314 defparamtab = dlhash_create(); 315 defoptlint = dlhash_create(); 316 defflagtab = dlhash_create(); 317 optfiletab = dlhash_create(); 318 obsopttab = dlhash_create(); 319 bdevmtab = ht_new(); 320 maxbdevm = 0; 321 cdevmtab = ht_new(); 322 maxcdevm = 0; 323 nextopt = &options; 324 nextmkopt = &mkoptions; 325 nextappmkopt = &appmkoptions; 326 nextcndmkopt = &condmkoptions; 327 nextfsopt = &fsoptions; 328 initfiles(); 329 initsem(); 330 331 /* 332 * Handle profiling (must do this before we try to create any 333 * files). 334 */ 335 last_component = strrchr(conffile, '/'); 336 last_component = (last_component) ? last_component + 1 : conffile; 337 if (pflag) { 338 p = emalloc(strlen(last_component) + 17); 339 (void)sprintf(p, "../compile/%s.PROF", last_component); 340 (void)addmkoption(intern("PROF"), "-pg"); 341 (void)addoption(intern("GPROF"), NULL); 342 } else { 343 p = emalloc(strlen(last_component) + 13); 344 (void)sprintf(p, "../compile/%s", last_component); 345 } 346 defbuilddir = (argc == 0) ? "." : p; 347 348 if (Lflag) { 349 char resolvedname[MAXPATHLEN]; 350 351 if (realpath(conffile, resolvedname) == NULL) 352 err(EXIT_FAILURE, "realpath(%s)", conffile); 353 354 if (yyparse()) 355 stop(); 356 357 printf("include \"%s\"\n", resolvedname); 358 359 emit_params(); 360 emit_options(); 361 emit_instances(); 362 363 exit(EXIT_SUCCESS); 364 } 365 366 removeit = 0; 367 if (is_elf(conffile)) { 368 const char *tmpdir; 369 int cfd; 370 371 if (builddir == NULL) 372 errx(EXIT_FAILURE, "Build directory must be specified " 373 "with binary kernels"); 374 375 /* Open temporary configuration file */ 376 tmpdir = getenv("TMPDIR"); 377 if (tmpdir == NULL) 378 tmpdir = _PATH_TMP; 379 snprintf(cname, sizeof(cname), "%s/config.tmp.XXXXXX", tmpdir); 380 cfd = mkstemp(cname); 381 if (cfd == -1) 382 err(EXIT_FAILURE, "Cannot create `%s'", cname); 383 384 printf("Using configuration data embedded in kernel...\n"); 385 if (!extract_config(conffile, cname, cfd)) { 386 unlink(cname); 387 errx(EXIT_FAILURE, "%s does not contain embedded " 388 "configuration data", conffile); 389 } 390 391 removeit = 1; 392 close(cfd); 393 firstfile(cname); 394 } 395 396 /* 397 * Log config file. We don't know until yyparse() if we're 398 * going to need config_file.h (i.e. if we're doing ioconf-only 399 * or not). Just start creating the file, and when we know 400 * later, we'll just keep or discard our work here. 401 */ 402 logconfig_start(); 403 404 /* 405 * Parse config file (including machine definitions). 406 */ 407 if (yyparse()) 408 stop(); 409 410 if (ioconfname && cfg) 411 fclose(cfg); 412 else 413 logconfig_end(); 414 415 if (removeit) 416 unlink(cname); 417 418 /* 419 * Handle command line overrides 420 */ 421 yyfile = "handle_cmdline_makeoptions"; 422 handle_cmdline_makeoptions(); 423 424 /* 425 * Detect and properly ignore orphaned devices 426 */ 427 yyfile = "kill_orphans"; 428 kill_orphans(); 429 430 /* 431 * Select devices and pseudo devices and their attributes 432 */ 433 yyfile = "fixdevis"; 434 if (fixdevis()) 435 stop(); 436 437 /* 438 * Copy maxusers to param. 439 */ 440 yyfile = "fixmaxusers"; 441 fixmaxusers(); 442 443 /* 444 * Copy makeoptions to params 445 */ 446 yyfile = "fixmkoption"; 447 fixmkoption(); 448 449 /* 450 * If working on an ioconf-only config, process here and exit 451 */ 452 if (ioconfname) { 453 yyfile = "pack"; 454 pack(); 455 yyfile = "mkioconf"; 456 mkioconf(); 457 yyfile = "emitlocs"; 458 emitlocs(); 459 yyfile = "emitioconfh"; 460 emitioconfh(); 461 return 0; 462 } 463 464 yyfile = "dependattrs"; 465 dependattrs(); 466 467 /* 468 * Deal with option dependencies. 469 */ 470 yyfile = "dependopts"; 471 dependopts(); 472 473 /* 474 * Fix (as in `set firmly in place') files. 475 */ 476 yyfile = "fixfiles"; 477 if (fixfiles()) 478 stop(); 479 480 /* 481 * Fix device-majors. 482 */ 483 yyfile = "fixdevsw"; 484 if (fixdevsw()) 485 stop(); 486 487 /* 488 * Perform cross-checking. 489 */ 490 if (maxusers == 0) { 491 if (defmaxusers) { 492 (void)printf("maxusers not specified; %d assumed\n", 493 defmaxusers); 494 maxusers = defmaxusers; 495 } else { 496 warnx("need \"maxusers\" line"); 497 errors++; 498 } 499 } 500 if (crosscheck() || errors) 501 stop(); 502 503 /* 504 * Squeeze things down and finish cross-checks (STAR checks must 505 * run after packing). 506 */ 507 yyfile = "pack"; 508 pack(); 509 yyfile = "badstar"; 510 if (badstar()) 511 stop(); 512 513 yyfile = NULL; 514 /* 515 * Ready to go. Build all the various files. 516 */ 517 if ((Sflag && mkallsubdirs()) || mksymlinks() || mkmakefile() || mkheaders() || mkswap() || 518 mkioconf() || (do_devsw ? mkdevsw() : 0) || mkident() || errors) 519 stop(); 520 (void)printf("Build directory is %s\n", builddir); 521 (void)printf("Don't forget to run \"make depend\"\n"); 522 523 return 0; 524 } 525 526 static void 527 usage(void) 528 { 529 (void)fprintf(stderr, "Usage: %s [-Ppv] [-b builddir] [-D var=value] " 530 "[-s srcdir] [-U var] " 531 "[config-file]\n\t%s -x [kernel-file]\n" 532 "\t%s -L [-v] [-s srcdir] [config-file]\n", 533 getprogname(), getprogname(), getprogname()); 534 exit(1); 535 } 536 537 /* 538 * Set any options that are implied by other options. 539 */ 540 static void 541 dependopts(void) 542 { 543 struct nvlist *nv; 544 545 for (nv = options; nv != NULL; nv = nv->nv_next) { 546 dependopts_one(nv->nv_name); 547 } 548 549 for (nv = fsoptions; nv != NULL; nv = nv->nv_next) { 550 dependopts_one(nv->nv_name); 551 } 552 } 553 554 static void 555 dependopts_one(const char *name) 556 { 557 struct defoptlist *dl; 558 struct nvlist *fs; 559 560 dl = find_declared_option_option(name); 561 if (dl != NULL) { 562 do_depends(dl->dl_depends); 563 } 564 fs = find_declared_fs_option(name); 565 if (fs != NULL) { 566 do_depends(fs->nv_ptr); 567 } 568 569 CFGDBG(3, "depend `%s' searched", name); 570 } 571 572 static void 573 do_depends(struct nvlist *nv) 574 { 575 struct nvlist *opt; 576 577 for (opt = nv; opt != NULL; opt = opt->nv_next) { 578 do_depend(opt); 579 } 580 } 581 582 static void 583 do_depend(struct nvlist *nv) 584 { 585 struct attr *a; 586 587 if (nv != NULL && (nv->nv_flags & NV_DEPENDED) == 0) { 588 nv->nv_flags |= NV_DEPENDED; 589 /* 590 * If the dependency is an attribute, then just add 591 * it to the selecttab. 592 */ 593 CFGDBG(3, "depend attr `%s'", nv->nv_name); 594 if ((a = ht_lookup(attrtab, nv->nv_name)) != NULL) { 595 if (a->a_iattr) 596 panic("do_depend(%s): dep `%s' is an iattr", 597 nv->nv_name, a->a_name); 598 expandattr(a, selectattr); 599 } else { 600 if (ht_lookup(opttab, nv->nv_name) == NULL) 601 addoption(nv->nv_name, NULL); 602 dependopts_one(nv->nv_name); 603 } 604 } 605 } 606 607 static int 608 recreate(const char *p, const char *q) 609 { 610 int ret; 611 612 if ((ret = unlink(q)) == -1 && errno != ENOENT) 613 warn("unlink(%s)", q); 614 if ((ret = symlink(p, q)) == -1) 615 warn("symlink(%s -> %s)", q, p); 616 return ret; 617 } 618 619 static void 620 mksubdir(char *buf) 621 { 622 char *p; 623 struct stat st; 624 625 p = strrchr(buf, '/'); 626 if (p != NULL && *p == '/') { 627 *p = '\0'; 628 mksubdir(buf); 629 *p = '/'; 630 } 631 if (stat(buf, &st) == 0) { 632 if (!S_ISDIR(st.st_mode)) 633 errx(EXIT_FAILURE, "not directory %s", buf); 634 } else 635 if (mkdir(buf, 0777) == -1) 636 errx(EXIT_FAILURE, "cannot create %s", buf); 637 } 638 639 static int 640 mksubdirs(struct filelist *fl) 641 { 642 struct files *fi; 643 const char *prologue, *prefix, *sep; 644 char buf[MAXPATHLEN]; 645 646 TAILQ_FOREACH(fi, fl, fi_next) { 647 if ((fi->fi_flags & FI_SEL) == 0) 648 continue; 649 prefix = sep = ""; 650 if (fi->fi_buildprefix != NULL) { 651 prefix = fi->fi_buildprefix; 652 sep = "/"; 653 } else { 654 if (fi->fi_prefix != NULL) { 655 prefix = fi->fi_prefix; 656 sep = "/"; 657 } 658 } 659 snprintf(buf, sizeof(buf), "%s%s%s", prefix, sep, fi->fi_dir); 660 if (buf[0] == '\0') 661 continue; 662 mksubdir(buf); 663 if (fi->fi_prefix != NULL && fi->fi_buildprefix != NULL) { 664 char org[MAXPATHLEN]; 665 666 if (fi->fi_prefix[0] == '/') { 667 prologue = ""; 668 sep = ""; 669 } else { 670 prologue = srcdir; 671 sep = "/"; 672 } 673 snprintf(buf, sizeof(buf), "%s%s%s", 674 fi->fi_buildprefix, "/", fi->fi_path); 675 snprintf(org, sizeof(org), "%s%s%s%s%s", 676 prologue, sep, fi->fi_prefix, "/", fi->fi_path); 677 recreate(org, buf); 678 fi->fi_prefix = fi->fi_buildprefix; 679 fi->fi_buildprefix = NULL; 680 } 681 } 682 683 return 0; 684 } 685 686 static int 687 mkallsubdirs(void) 688 { 689 690 mksubdirs(&allfiles); 691 mksubdirs(&allofiles); 692 return 0; 693 } 694 695 /* 696 * Make a symlink for "machine" so that "#include <machine/foo.h>" works, 697 * and for the machine's CPU architecture, so that works as well. 698 */ 699 static int 700 mksymlinks(void) 701 { 702 int ret; 703 char *p, buf[MAXPATHLEN]; 704 const char *q; 705 struct nvlist *nv; 706 707 p = buf; 708 709 snprintf(buf, sizeof(buf), "%s/arch/%s/include", srcdir, machine); 710 ret = recreate(p, "machine"); 711 ret = recreate(p, machine); 712 713 if (machinearch != NULL) { 714 snprintf(buf, sizeof(buf), "%s/arch/%s/include", srcdir, machinearch); 715 q = machinearch; 716 } else { 717 snprintf(buf, sizeof(buf), "machine"); 718 q = machine; 719 } 720 721 ret = recreate(p, q); 722 723 for (nv = machinesubarches; nv != NULL; nv = nv->nv_next) { 724 q = nv->nv_name; 725 snprintf(buf, sizeof(buf), "%s/arch/%s/include", srcdir, q); 726 ret = recreate(p, q); 727 } 728 729 return (ret); 730 } 731 732 static __dead void 733 stop(void) 734 { 735 (void)fprintf(stderr, "*** Stop.\n"); 736 exit(1); 737 } 738 739 static void 740 check_dependencies(const char *thing, struct nvlist *deps) 741 { 742 struct nvlist *dep; 743 struct attr *a; 744 745 for (dep = deps; dep != NULL; dep = dep->nv_next) { 746 /* 747 * If the dependency is an attribute, it must not 748 * be an interface attribute. Otherwise, it must 749 * be a previously declared option. 750 */ 751 if ((a = ht_lookup(attrtab, dep->nv_name)) != NULL) { 752 if (a->a_iattr) 753 cfgerror("option `%s' dependency `%s' " 754 "is an interface attribute", 755 thing, a->a_name); 756 } else if (OPT_OBSOLETE(dep->nv_name)) { 757 cfgerror("option `%s' dependency `%s' " 758 "is obsolete", thing, dep->nv_name); 759 } else if (!find_declared_option(dep->nv_name)) { 760 cfgerror("option `%s' dependency `%s' " 761 "is an unknown option", 762 thing, dep->nv_name); 763 } 764 } 765 } 766 767 static void 768 add_fs_dependencies(struct nvlist *nv, struct nvlist *deps) 769 { 770 /* Use nv_ptr to link any other options that are implied. */ 771 nv->nv_ptr = deps; 772 check_dependencies(nv->nv_name, deps); 773 } 774 775 static void 776 add_opt_dependencies(struct defoptlist *dl, struct nvlist *deps) 777 { 778 dl->dl_depends = deps; 779 check_dependencies(dl->dl_name, deps); 780 } 781 782 /* 783 * Define one or more file systems. 784 */ 785 void 786 deffilesystem(struct nvlist *fses, struct nvlist *deps) 787 { 788 struct nvlist *nv; 789 struct where *w; 790 791 /* 792 * Mark these options as ones to skip when creating the Makefile. 793 */ 794 for (nv = fses; nv != NULL; nv = nv->nv_next) { 795 if ((w = DEFINED_OPTION(nv->nv_name)) != NULL) { 796 cfgerror("file system or option `%s' already defined" 797 " at %s:%hu", nv->nv_name, w->w_srcfile, 798 w->w_srcline); 799 return; 800 } 801 802 /* 803 * Also mark it as a valid file system, which may be 804 * used in "file-system" directives in the config 805 * file. 806 */ 807 if (nvhash_insert(deffstab, nv->nv_name, nv)) 808 panic("file system `%s' already in table?!", 809 nv->nv_name); 810 811 add_fs_dependencies(nv, deps); 812 813 /* 814 * Implicit attribute definition for filesystem. 815 */ 816 const char *n; 817 n = strtolower(nv->nv_name); 818 refattr(n); 819 } 820 } 821 822 /* 823 * Sanity check a file name. 824 */ 825 int 826 badfilename(const char *fname) 827 { 828 const char *n; 829 830 /* 831 * We're putting multiple options into one file. Sanity 832 * check the file name. 833 */ 834 if (strchr(fname, '/') != NULL) { 835 cfgerror("option file name contains a `/'"); 836 return 1; 837 } 838 if ((n = strrchr(fname, '.')) == NULL || strcmp(n, ".h") != 0) { 839 cfgerror("option file name does not end in `.h'"); 840 return 1; 841 } 842 return 0; 843 } 844 845 846 /* 847 * Search for a defined option (defopt, filesystem, etc), and if found, 848 * return the option's struct nvlist. 849 * 850 * This used to be one function (find_declared_option) before options 851 * and filesystems became different types. 852 */ 853 static struct defoptlist * 854 find_declared_option_option(const char *name) 855 { 856 struct defoptlist *option; 857 858 if ((option = dlhash_lookup(defopttab, name)) != NULL || 859 (option = dlhash_lookup(defparamtab, name)) != NULL || 860 (option = dlhash_lookup(defflagtab, name)) != NULL) { 861 return (option); 862 } 863 864 return (NULL); 865 } 866 867 static struct nvlist * 868 find_declared_fs_option(const char *name) 869 { 870 struct nvlist *fs; 871 872 if ((fs = nvhash_lookup(deffstab, name)) != NULL) { 873 return fs; 874 } 875 876 return (NULL); 877 } 878 879 /* 880 * Like find_declared_option but doesn't return what it finds, so it 881 * can search both the various kinds of options and also filesystems. 882 */ 883 struct where * 884 find_declared_option(const char *name) 885 { 886 struct defoptlist *option = NULL; 887 struct nvlist *fs; 888 889 if ((option = dlhash_lookup(defopttab, name)) != NULL || 890 (option = dlhash_lookup(defparamtab, name)) != NULL || 891 (option = dlhash_lookup(defflagtab, name)) != NULL) { 892 return &option->dl_where; 893 } 894 if ((fs = nvhash_lookup(deffstab, name)) != NULL) { 895 return &fs->nv_where; 896 } 897 898 return NULL; 899 } 900 901 /* 902 * Define one or more standard options. If an option file name is specified, 903 * place all options in one file with the specified name. Otherwise, create 904 * an option file for each option. 905 * record the option information in the specified table. 906 */ 907 void 908 defopt(struct dlhash *ht, const char *fname, struct defoptlist *opts, 909 struct nvlist *deps, int obs) 910 { 911 struct defoptlist *dl, *nextdl, *olddl; 912 const char *name; 913 struct where *w; 914 char buf[500]; 915 916 if (fname != NULL && badfilename(fname)) { 917 return; 918 } 919 920 /* 921 * Mark these options as ones to skip when creating the Makefile. 922 */ 923 for (dl = opts; dl != NULL; dl = nextdl) { 924 nextdl = dl->dl_next; 925 926 if (dl->dl_lintvalue != NULL) { 927 /* 928 * If an entry already exists, then we are about to 929 * complain, so no worry. 930 */ 931 (void) dlhash_insert(defoptlint, dl->dl_name, 932 dl); 933 } 934 935 /* An option name can be declared at most once. */ 936 if ((w = DEFINED_OPTION(dl->dl_name)) != NULL) { 937 cfgerror("file system or option `%s' already defined" 938 " at %s:%hu", dl->dl_name, w->w_srcfile, 939 w->w_srcline); 940 return; 941 } 942 943 if (dlhash_insert(ht, dl->dl_name, dl)) { 944 cfgerror("file system or option `%s' already defined" 945 " at %s:%hu", dl->dl_name, dl->dl_where.w_srcfile, 946 dl->dl_where.w_srcline); 947 return; 948 } 949 950 if (fname == NULL) { 951 /* 952 * Each option will be going into its own file. 953 * Convert the option name to lower case. This 954 * lower case name will be used as the option 955 * file name. 956 */ 957 (void) snprintf(buf, sizeof(buf), "opt_%s.h", 958 strtolower(dl->dl_name)); 959 name = intern(buf); 960 } else { 961 name = fname; 962 } 963 964 add_opt_dependencies(dl, deps); 965 966 /* 967 * Remove this option from the parameter list before adding 968 * it to the list associated with this option file. 969 */ 970 dl->dl_next = NULL; 971 972 /* 973 * Flag as obsolete, if requested. 974 */ 975 if (obs) { 976 dl->dl_obsolete = 1; 977 (void)dlhash_insert(obsopttab, dl->dl_name, dl); 978 } 979 980 /* 981 * Add this option file if we haven't seen it yet. 982 * Otherwise, append to the list of options already 983 * associated with this file. 984 */ 985 if ((olddl = dlhash_lookup(optfiletab, name)) == NULL) { 986 (void)dlhash_insert(optfiletab, name, dl); 987 } else { 988 while (olddl->dl_next != NULL) 989 olddl = olddl->dl_next; 990 olddl->dl_next = dl; 991 } 992 } 993 } 994 995 /* 996 * Define one or more standard options. If an option file name is specified, 997 * place all options in one file with the specified name. Otherwise, create 998 * an option file for each option. 999 */ 1000 void 1001 defoption(const char *fname, struct defoptlist *opts, struct nvlist *deps) 1002 { 1003 1004 cfgwarn("The use of `defopt' is deprecated"); 1005 defopt(defopttab, fname, opts, deps, 0); 1006 } 1007 1008 1009 /* 1010 * Define an option for which a value is required. 1011 */ 1012 void 1013 defparam(const char *fname, struct defoptlist *opts, struct nvlist *deps, int obs) 1014 { 1015 1016 defopt(defparamtab, fname, opts, deps, obs); 1017 } 1018 1019 /* 1020 * Define an option which must not have a value, and which 1021 * emits a "needs-flag" style output. 1022 */ 1023 void 1024 defflag(const char *fname, struct defoptlist *opts, struct nvlist *deps, int obs) 1025 { 1026 1027 defopt(defflagtab, fname, opts, deps, obs); 1028 } 1029 1030 1031 /* 1032 * Add an option from "options FOO". Note that this selects things that 1033 * are "optional foo". 1034 */ 1035 void 1036 addoption(const char *name, const char *value) 1037 { 1038 const char *n; 1039 int is_fs, is_param, is_flag, is_undecl, is_obs; 1040 1041 /* 1042 * Figure out how this option was declared (if at all.) 1043 * XXX should use "params" and "flags" in config. 1044 * XXX crying out for a type field in a unified hashtab. 1045 */ 1046 is_fs = OPT_FSOPT(name); 1047 is_param = OPT_DEFPARAM(name); 1048 is_flag = OPT_DEFFLAG(name); 1049 is_obs = OPT_OBSOLETE(name); 1050 is_undecl = !DEFINED_OPTION(name); 1051 1052 /* Warn and pretend the user had not selected the option */ 1053 if (is_obs) { 1054 cfgwarn("obsolete option `%s' will be ignored", name); 1055 return; 1056 } 1057 1058 /* Make sure this is not a defined file system. */ 1059 if (is_fs) { 1060 cfgerror("`%s' is a defined file system", name); 1061 return; 1062 } 1063 /* A defparam must have a value */ 1064 if (is_param && value == NULL) { 1065 cfgerror("option `%s' must have a value", name); 1066 return; 1067 } 1068 /* A defflag must not have a value */ 1069 if (is_flag && value != NULL) { 1070 cfgerror("option `%s' must not have a value", name); 1071 return; 1072 } 1073 1074 if (is_undecl && vflag) { 1075 cfgwarn("undeclared option `%s' added to IDENT", name); 1076 } 1077 1078 if (do_option(opttab, &options, &nextopt, name, value, "options", 1079 selecttab)) 1080 return; 1081 1082 /* make lowercase, then add to select table */ 1083 n = strtolower(name); 1084 (void)ht_insert(selecttab, n, (void *)__UNCONST(n)); 1085 CFGDBG(3, "option selected `%s'", n); 1086 } 1087 1088 void 1089 deloption(const char *name, int nowarn) 1090 { 1091 1092 CFGDBG(4, "deselecting opt `%s'", name); 1093 if (undo_option(opttab, &options, &nextopt, name, "options", nowarn)) 1094 return; 1095 if (undo_option(selecttab, NULL, NULL, strtolower(name), "options", nowarn)) 1096 return; 1097 } 1098 1099 /* 1100 * Add a file system option. This routine simply inserts the name into 1101 * a list of valid file systems, which is used to validate the root 1102 * file system type. The name is then treated like a standard option. 1103 */ 1104 void 1105 addfsoption(const char *name) 1106 { 1107 const char *n; 1108 1109 /* Make sure this is a defined file system. */ 1110 if (!OPT_FSOPT(name)) { 1111 cfgerror("`%s' is not a defined file system", name); 1112 return; 1113 } 1114 1115 /* 1116 * Convert to lower case. This will be used in the select 1117 * table, to verify root file systems. 1118 */ 1119 n = strtolower(name); 1120 1121 if (do_option(fsopttab, &fsoptions, &nextfsopt, name, n, "file-system", 1122 selecttab)) 1123 return; 1124 1125 /* Add to select table. */ 1126 (void)ht_insert(selecttab, n, __UNCONST(n)); 1127 CFGDBG(3, "fs selected `%s'", name); 1128 1129 /* 1130 * Select attribute if one exists. 1131 */ 1132 struct attr *a; 1133 if ((a = ht_lookup(attrtab, n)) != NULL) 1134 selectattr(a); 1135 } 1136 1137 void 1138 delfsoption(const char *name, int nowarn) 1139 { 1140 const char *n; 1141 1142 CFGDBG(4, "deselecting fs `%s'", name); 1143 n = strtolower(name); 1144 if (undo_option(fsopttab, &fsoptions, &nextfsopt, name, "file-system", nowarn)) 1145 return; 1146 if (undo_option(selecttab, NULL, NULL, n, "file-system", nowarn)) 1147 return; 1148 } 1149 1150 /* 1151 * Add a "make" option. 1152 */ 1153 void 1154 addmkoption(const char *name, const char *value) 1155 { 1156 1157 (void)do_option(mkopttab, &mkoptions, &nextmkopt, name, value, 1158 "makeoptions", NULL); 1159 } 1160 1161 void 1162 delmkoption(const char *name, int nowarn) 1163 { 1164 1165 CFGDBG(4, "deselecting mkopt `%s'", name); 1166 (void)undo_option(mkopttab, &mkoptions, &nextmkopt, name, 1167 "makeoptions", nowarn); 1168 } 1169 1170 /* 1171 * Add an appending "make" option. 1172 */ 1173 void 1174 appendmkoption(const char *name, const char *value) 1175 { 1176 struct nvlist *nv; 1177 1178 nv = newnv(name, value, NULL, 0, NULL); 1179 *nextappmkopt = nv; 1180 nextappmkopt = &nv->nv_next; 1181 } 1182 1183 /* 1184 * Add a conditional appending "make" option. 1185 */ 1186 void 1187 appendcondmkoption(struct condexpr *cond, const char *name, const char *value) 1188 { 1189 struct nvlist *nv; 1190 1191 nv = newnv(name, value, cond, 0, NULL); 1192 *nextcndmkopt = nv; 1193 nextcndmkopt = &nv->nv_next; 1194 } 1195 1196 /* 1197 * Copy maxusers to param "MAXUSERS". 1198 */ 1199 void 1200 fixmaxusers(void) 1201 { 1202 char str[32]; 1203 1204 snprintf(str, sizeof(str), "%d", maxusers); 1205 addoption(intern("MAXUSERS"), intern(str)); 1206 } 1207 1208 /* 1209 * Copy makeoptions to params with "makeoptions_" prefix. 1210 */ 1211 void 1212 fixmkoption(void) 1213 { 1214 struct nvlist *nv; 1215 char buf[100]; 1216 const char *name; 1217 1218 for (nv = mkoptions; nv != NULL; nv = nv->nv_next) { 1219 snprintf(buf, sizeof(buf), "makeoptions_%s", nv->nv_name); 1220 name = intern(buf); 1221 if (!DEFINED_OPTION(name) || !OPT_DEFPARAM(name)) 1222 continue; 1223 addoption(name, intern(nv->nv_str)); 1224 } 1225 } 1226 1227 /* 1228 * Add a name=value pair to an option list. The value may be NULL. 1229 */ 1230 static int 1231 do_option(struct hashtab *ht, struct nvlist **npp, struct nvlist ***next, 1232 const char *name, const char *value, const char *type, 1233 struct hashtab *stab) 1234 { 1235 struct nvlist *nv, *onv; 1236 1237 /* assume it will work */ 1238 nv = newnv(name, value, NULL, 0, NULL); 1239 if (ht_insert(ht, name, nv) != 0) { 1240 1241 /* oops, already got that option - remove it first */ 1242 if ((onv = ht_lookup(ht, name)) == NULL) 1243 panic("do_option 1"); 1244 if (onv->nv_str != NULL && !OPT_FSOPT(name)) 1245 cfgwarn("already have %s `%s=%s'", type, name, 1246 onv->nv_str); 1247 else 1248 cfgwarn("already have %s `%s'", type, name); 1249 1250 if (undo_option(ht, npp, next, name, type, 0)) 1251 panic("do_option 2"); 1252 if (stab != NULL && 1253 undo_option(stab, NULL, NULL, strtolower(name), type, 0)) 1254 panic("do_option 3"); 1255 1256 /* now try adding it again */ 1257 if (ht_insert(ht, name, nv) != 0) 1258 panic("do_option 4"); 1259 1260 CFGDBG(2, "opt `%s' replaced", name); 1261 } 1262 **next = nv; 1263 *next = &nv->nv_next; 1264 1265 return (0); 1266 } 1267 1268 /* 1269 * Remove a name from a hash table, 1270 * and optionally, a name=value pair from an option list. 1271 */ 1272 static int 1273 undo_option(struct hashtab *ht, struct nvlist **npp, 1274 struct nvlist ***next, const char *name, const char *type, int nowarn) 1275 { 1276 struct nvlist *nv; 1277 1278 if (ht_remove(ht, name)) { 1279 /* 1280 * -U command line option removals are always silent 1281 */ 1282 if (!handling_cmdlineopts && !nowarn) 1283 cfgwarn("%s `%s' is not defined", type, name); 1284 return (1); 1285 } 1286 if (npp == NULL) { 1287 CFGDBG(2, "opt `%s' deselected", name); 1288 return (0); 1289 } 1290 1291 for ( ; *npp != NULL; npp = &(*npp)->nv_next) { 1292 if ((*npp)->nv_name != name) 1293 continue; 1294 if (next != NULL && *next == &(*npp)->nv_next) 1295 *next = npp; 1296 nv = (*npp)->nv_next; 1297 CFGDBG(2, "opt `%s' deselected", (*npp)->nv_name); 1298 nvfree(*npp); 1299 *npp = nv; 1300 return (0); 1301 } 1302 panic("%s `%s' is not defined in nvlist", type, name); 1303 return (1); 1304 } 1305 1306 /* 1307 * Return true if there is at least one instance of the given unit 1308 * on the given device attachment (or any units, if unit == WILD). 1309 */ 1310 int 1311 deva_has_instances(struct deva *deva, int unit) 1312 { 1313 struct devi *i; 1314 1315 /* 1316 * EHAMMERTOOBIG: we shouldn't check i_pseudoroot here. 1317 * What we want by this check is them to appear non-present 1318 * except for purposes of other devices being able to attach 1319 * to them. 1320 */ 1321 for (i = deva->d_ihead; i != NULL; i = i->i_asame) 1322 if (i->i_active == DEVI_ACTIVE && i->i_pseudoroot == 0 && 1323 (unit == WILD || unit == i->i_unit || i->i_unit == STAR)) 1324 return (1); 1325 return (0); 1326 } 1327 1328 /* 1329 * Return true if there is at least one instance of the given unit 1330 * on the given base (or any units, if unit == WILD). 1331 */ 1332 int 1333 devbase_has_instances(struct devbase *dev, int unit) 1334 { 1335 struct deva *da; 1336 1337 /* 1338 * Pseudo-devices are a little special. We consider them 1339 * to have instances only if they are both: 1340 * 1341 * 1. Included in this kernel configuration. 1342 * 1343 * 2. Be declared "defpseudodev". 1344 */ 1345 if (dev->d_ispseudo) { 1346 return ((ht_lookup(devitab, dev->d_name) != NULL) 1347 && (dev->d_ispseudo > 1)); 1348 } 1349 1350 for (da = dev->d_ahead; da != NULL; da = da->d_bsame) 1351 if (deva_has_instances(da, unit)) 1352 return (1); 1353 return (0); 1354 } 1355 1356 static int 1357 cfcrosscheck(struct config *cf, const char *what, struct nvlist *nv) 1358 { 1359 struct devbase *dev; 1360 struct devi *pd; 1361 int errs, devunit; 1362 1363 if (maxpartitions <= 0) 1364 panic("cfcrosscheck"); 1365 1366 for (errs = 0; nv != NULL; nv = nv->nv_next) { 1367 if (nv->nv_name == NULL) 1368 continue; 1369 dev = ht_lookup(devbasetab, nv->nv_name); 1370 if (dev == NULL) 1371 panic("cfcrosscheck(%s)", nv->nv_name); 1372 if (has_attr(dev->d_attrs, s_ifnet)) 1373 devunit = nv->nv_ifunit; /* XXX XXX XXX */ 1374 else 1375 devunit = (int)(minor(nv->nv_num) / maxpartitions); 1376 if (devbase_has_instances(dev, devunit)) 1377 continue; 1378 if (devbase_has_instances(dev, STAR) && 1379 devunit >= dev->d_umax) 1380 continue; 1381 TAILQ_FOREACH(pd, &allpseudo, i_next) { 1382 if (pd->i_base == dev && devunit < dev->d_umax && 1383 devunit >= 0) 1384 goto loop; 1385 } 1386 (void)fprintf(stderr, 1387 "%s:%hu: %s says %s on %s, but there's no %s\n", 1388 conffile, cf->cf_where.w_srcline, 1389 cf->cf_name, what, nv->nv_str, nv->nv_str); 1390 errs++; 1391 loop: 1392 ; 1393 } 1394 return (errs); 1395 } 1396 1397 /* 1398 * Cross-check the configuration: make sure that each target device 1399 * or attribute (`at foo[0*?]') names at least one real device. Also 1400 * see that the root and dump devices for all configurations are there. 1401 */ 1402 int 1403 crosscheck(void) 1404 { 1405 struct config *cf; 1406 int errs; 1407 1408 errs = 0; 1409 if (TAILQ_EMPTY(&allcf)) { 1410 warnx("%s has no configurations!", conffile); 1411 errs++; 1412 } 1413 TAILQ_FOREACH(cf, &allcf, cf_next) { 1414 if (cf->cf_root != NULL) { /* i.e., not root on ? */ 1415 errs += cfcrosscheck(cf, "root", cf->cf_root); 1416 errs += cfcrosscheck(cf, "dumps", cf->cf_dump); 1417 } 1418 } 1419 return (errs); 1420 } 1421 1422 /* 1423 * Check to see if there is a *'d unit with a needs-count file. 1424 */ 1425 int 1426 badstar(void) 1427 { 1428 struct devbase *d; 1429 struct deva *da; 1430 struct devi *i; 1431 int errs, n; 1432 1433 errs = 0; 1434 TAILQ_FOREACH(d, &allbases, d_next) { 1435 for (da = d->d_ahead; da != NULL; da = da->d_bsame) 1436 for (i = da->d_ihead; i != NULL; i = i->i_asame) { 1437 if (i->i_unit == STAR) 1438 goto aybabtu; 1439 } 1440 continue; 1441 aybabtu: 1442 if (ht_lookup(needcnttab, d->d_name)) { 1443 warnx("%s's cannot be *'d until its driver is fixed", 1444 d->d_name); 1445 errs++; 1446 continue; 1447 } 1448 for (n = 0; i != NULL; i = i->i_alias) 1449 if (!i->i_collapsed) 1450 n++; 1451 if (n < 1) 1452 panic("badstar() n<1"); 1453 } 1454 return (errs); 1455 } 1456 1457 /* 1458 * Verify/create builddir if necessary, change to it, and verify srcdir. 1459 * This will be called when we see the first include. 1460 */ 1461 void 1462 setupdirs(void) 1463 { 1464 struct stat st; 1465 1466 /* srcdir must be specified if builddir is not specified or if 1467 * no configuration filename was specified. */ 1468 if ((builddir || strcmp(defbuilddir, ".") == 0) && !srcdir) { 1469 cfgerror("source directory must be specified"); 1470 exit(1); 1471 } 1472 1473 if (Lflag) { 1474 if (srcdir == NULL) 1475 srcdir = "../../.."; 1476 return; 1477 } 1478 1479 if (srcdir == NULL) 1480 srcdir = "../../../.."; 1481 if (builddir == NULL) 1482 builddir = defbuilddir; 1483 1484 if (stat(builddir, &st) == -1) { 1485 if (mkdir(builddir, 0777) == -1) 1486 errx(EXIT_FAILURE, "cannot create %s", builddir); 1487 } else if (!S_ISDIR(st.st_mode)) 1488 errx(EXIT_FAILURE, "%s is not a directory", builddir); 1489 if (chdir(builddir) == -1) 1490 err(EXIT_FAILURE, "cannot change to %s", builddir); 1491 if (stat(srcdir, &st) == -1) 1492 err(EXIT_FAILURE, "cannot stat %s", srcdir); 1493 if (!S_ISDIR(st.st_mode)) 1494 errx(EXIT_FAILURE, "%s is not a directory", srcdir); 1495 } 1496 1497 /* 1498 * Write identifier from "ident" directive into file, for 1499 * newvers.sh to pick it up. 1500 */ 1501 int 1502 mkident(void) 1503 { 1504 FILE *fp; 1505 int error = 0; 1506 1507 (void)unlink("ident"); 1508 1509 if (ident == NULL) 1510 return (0); 1511 1512 if ((fp = fopen("ident", "w")) == NULL) { 1513 warn("cannot write ident"); 1514 return (1); 1515 } 1516 if (vflag) 1517 (void)printf("using ident '%s'\n", ident); 1518 fprintf(fp, "%s\n", ident); 1519 fflush(fp); 1520 if (ferror(fp)) 1521 error = 1; 1522 (void)fclose(fp); 1523 1524 return error; 1525 } 1526 1527 void 1528 logconfig_start(void) 1529 { 1530 extern FILE *yyin; 1531 char line[1024]; 1532 const char *tmpdir; 1533 struct stat st; 1534 int fd; 1535 1536 if (yyin == NULL || fstat(fileno(yyin), &st) == -1) 1537 return; 1538 cfgtime = st.st_mtime; 1539 1540 tmpdir = getenv("TMPDIR"); 1541 if (tmpdir == NULL) 1542 tmpdir = _PATH_TMP; 1543 (void)snprintf(line, sizeof(line), "%s/config.tmp.XXXXXX", tmpdir); 1544 if ((fd = mkstemp(line)) == -1 || 1545 (cfg = fdopen(fd, "r+")) == NULL) { 1546 if (fd != -1) { 1547 (void)unlink(line); 1548 (void)close(fd); 1549 } 1550 cfg = NULL; 1551 return; 1552 } 1553 (void)unlink(line); 1554 1555 (void)fprintf(cfg, "#include <sys/cdefs.h>\n\n"); 1556 (void)fprintf(cfg, "#include \"opt_config.h\"\n"); 1557 (void)fprintf(cfg, "\n"); 1558 (void)fprintf(cfg, "/*\n"); 1559 (void)fprintf(cfg, " * Add either (or both) of\n"); 1560 (void)fprintf(cfg, " *\n"); 1561 (void)fprintf(cfg, " *\toptions %s\n", LOGCONFIG_LARGE); 1562 (void)fprintf(cfg, " *\toptions %s\n", LOGCONFIG_SMALL); 1563 (void)fprintf(cfg, " *\n"); 1564 (void)fprintf(cfg, 1565 " * to your kernel config file to embed it in the resulting\n"); 1566 (void)fprintf(cfg, 1567 " * kernel. The latter option does not include files that are\n"); 1568 (void)fprintf(cfg, 1569 " * included (recursively) by your config file. The embedded\n"); 1570 (void)fprintf(cfg, 1571 " * data be extracted by using the command:\n"); 1572 (void)fprintf(cfg, " *\n"); 1573 (void)fprintf(cfg, 1574 " *\tstrings netbsd | sed -n 's/^_CFG_//p' | unvis\n"); 1575 (void)fprintf(cfg, " */\n"); 1576 (void)fprintf(cfg, "\n"); 1577 (void)fprintf(cfg, "#ifdef CONFIG_FILE\n"); 1578 (void)fprintf(cfg, "#if defined(%s) || defined(%s)\n\n", 1579 LOGCONFIG_LARGE, LOGCONFIG_SMALL); 1580 (void)fprintf(cfg, "static const char config[] __used =\n\n"); 1581 1582 (void)fprintf(cfg, "#ifdef %s\n\n", LOGCONFIG_LARGE); 1583 (void)fprintf(cfg, "\"_CFG_### START CONFIG FILE \\\"%s\\\"\\n\"\n\n", 1584 conffile); 1585 (void)fprintf(cfg, "#endif /* %s */\n\n", LOGCONFIG_LARGE); 1586 1587 logconfig_include(yyin, NULL); 1588 1589 (void)fprintf(cfg, "#ifdef %s\n\n", LOGCONFIG_LARGE); 1590 (void)fprintf(cfg, "\"_CFG_### END CONFIG FILE \\\"%s\\\"\\n\"\n", 1591 conffile); 1592 1593 rewind(yyin); 1594 } 1595 1596 void 1597 logconfig_include(FILE *cf, const char *filename) 1598 { 1599 char line[1024], in[2048], *out; 1600 struct stat st; 1601 int missingeol; 1602 1603 if (!cfg) 1604 return; 1605 1606 missingeol = 0; 1607 if (fstat(fileno(cf), &st) == -1) 1608 return; 1609 if (cfgtime < st.st_mtime) 1610 cfgtime = st.st_mtime; 1611 1612 if (filename) 1613 (void)fprintf(cfg, 1614 "\"_CFG_### (included from \\\"%s\\\")\\n\"\n", 1615 filename); 1616 while (fgets(line, sizeof(line), cf) != NULL) { 1617 missingeol = 1; 1618 (void)fprintf(cfg, "\"_CFG_"); 1619 if (filename) 1620 (void)fprintf(cfg, "###> "); 1621 strvis(in, line, VIS_TAB); 1622 for (out = in; *out; out++) 1623 switch (*out) { 1624 case '\n': 1625 (void)fprintf(cfg, "\\n\"\n"); 1626 missingeol = 0; 1627 break; 1628 case '"': case '\\': 1629 (void)fputc('\\', cfg); 1630 /* FALLTHROUGH */ 1631 default: 1632 (void)fputc(*out, cfg); 1633 break; 1634 } 1635 } 1636 if (missingeol) { 1637 (void)fprintf(cfg, "\\n\"\n"); 1638 warnx("%s: newline missing at EOF", 1639 filename != NULL ? filename : conffile); 1640 } 1641 if (filename) 1642 (void)fprintf(cfg, "\"_CFG_### (end include \\\"%s\\\")\\n\"\n", 1643 filename); 1644 1645 rewind(cf); 1646 } 1647 1648 void 1649 logconfig_end(void) 1650 { 1651 char line[1024]; 1652 FILE *fp; 1653 struct stat st; 1654 1655 if (!cfg) 1656 return; 1657 1658 (void)fprintf(cfg, "#endif /* %s */\n", LOGCONFIG_LARGE); 1659 (void)fprintf(cfg, ";\n"); 1660 (void)fprintf(cfg, "#endif /* %s || %s */\n", 1661 LOGCONFIG_LARGE, LOGCONFIG_SMALL); 1662 (void)fprintf(cfg, "#endif /* CONFIG_FILE */\n"); 1663 fflush(cfg); 1664 if (ferror(cfg)) 1665 err(EXIT_FAILURE, "write to temporary file for config.h failed"); 1666 rewind(cfg); 1667 1668 if (stat("config_file.h", &st) != -1) { 1669 if (cfgtime < st.st_mtime) { 1670 fclose(cfg); 1671 return; 1672 } 1673 } 1674 1675 fp = fopen("config_file.h", "w"); 1676 if (!fp) 1677 err(EXIT_FAILURE, "cannot open \"config.h\""); 1678 1679 while (fgets(line, sizeof(line), cfg) != NULL) 1680 fputs(line, fp); 1681 fflush(fp); 1682 if (ferror(fp)) 1683 err(EXIT_FAILURE, "write to \"config.h\" failed"); 1684 fclose(fp); 1685 fclose(cfg); 1686 } 1687 1688 const char * 1689 strtolower(const char *name) 1690 { 1691 const char *n; 1692 char *p, low[500]; 1693 char c; 1694 1695 for (n = name, p = low; (c = *n) != '\0'; n++) 1696 *p++ = (char)(isupper((u_char)c) ? tolower((u_char)c) : c); 1697 *p = '\0'; 1698 return (intern(low)); 1699 } 1700 1701 static int 1702 is_elf(const char *file) 1703 { 1704 int kernel; 1705 char hdr[4]; 1706 1707 kernel = open(file, O_RDONLY); 1708 if (kernel == -1) 1709 err(EXIT_FAILURE, "cannot open %s", file); 1710 if (read(kernel, hdr, 4) != 4) 1711 err(EXIT_FAILURE, "Cannot read from %s", file); 1712 (void)close(kernel); 1713 1714 return memcmp("\177ELF", hdr, 4) == 0 ? 1 : 0; 1715 } 1716 1717 static int 1718 extract_config(const char *kname, const char *cname, int cfd) 1719 { 1720 char *ptr; 1721 void *base; 1722 int found, kfd; 1723 struct stat st; 1724 off_t i; 1725 1726 found = 0; 1727 1728 /* mmap(2) binary kernel */ 1729 kfd = open(conffile, O_RDONLY); 1730 if (kfd == -1) 1731 err(EXIT_FAILURE, "cannot open %s", kname); 1732 if (fstat(kfd, &st) == -1) 1733 err(EXIT_FAILURE, "cannot stat %s", kname); 1734 base = mmap(0, (size_t)st.st_size, PROT_READ, MAP_FILE | MAP_SHARED, 1735 kfd, 0); 1736 if (base == MAP_FAILED) 1737 err(EXIT_FAILURE, "cannot mmap %s", kname); 1738 ptr = base; 1739 1740 /* Scan mmap(2)'ed region, extracting kernel configuration */ 1741 for (i = 0; i < st.st_size; i++) { 1742 if ((*ptr == '_') && (st.st_size - i > 5) && memcmp(ptr, 1743 "_CFG_", 5) == 0) { 1744 /* Line found */ 1745 char *oldptr, line[LINE_MAX + 1], uline[LINE_MAX + 1]; 1746 int j; 1747 1748 found = 1; 1749 1750 oldptr = (ptr += 5); 1751 while (*ptr != '\n' && *ptr != '\0') 1752 ptr++; 1753 if (ptr - oldptr > LINE_MAX) 1754 errx(EXIT_FAILURE, "line too long"); 1755 i += ptr - oldptr + 5; 1756 (void)memcpy(line, oldptr, (size_t)(ptr - oldptr)); 1757 line[ptr - oldptr] = '\0'; 1758 j = strunvis(uline, line); 1759 if (j == -1) 1760 errx(EXIT_FAILURE, "unvis: invalid " 1761 "encoded sequence"); 1762 uline[j] = '\n'; 1763 if (write(cfd, uline, (size_t)j + 1) == -1) 1764 err(EXIT_FAILURE, "cannot write to %s", cname); 1765 } else 1766 ptr++; 1767 } 1768 1769 (void)close(kfd); 1770 (void)munmap(base, (size_t)st.st_size); 1771 1772 return found; 1773 } 1774 1775 struct dhdi_params { 1776 struct devbase *d; 1777 int unit; 1778 int level; 1779 }; 1780 1781 static int 1782 devbase_has_dead_instances(const char *key, void *value, void *aux) 1783 { 1784 struct devi *i; 1785 struct dhdi_params *dhdi = aux; 1786 1787 for (i = value; i != NULL; i = i->i_alias) 1788 if (i->i_base == dhdi->d && 1789 (dhdi->unit == WILD || dhdi->unit == i->i_unit || 1790 i->i_unit == STAR) && 1791 i->i_level >= dhdi->level) 1792 return 1; 1793 return 0; 1794 } 1795 1796 /* 1797 * This is almost the same as devbase_has_instances, except it 1798 * may have special considerations regarding ignored instances. 1799 */ 1800 1801 static int 1802 devbase_has_any_instance(struct devbase *dev, int unit, int state, int level) 1803 { 1804 struct deva *da; 1805 struct devi *i; 1806 1807 if (dev->d_ispseudo) { 1808 if (dev->d_ihead != NULL) 1809 return 1; 1810 else if (state != DEVI_IGNORED) 1811 return 0; 1812 if ((i = ht_lookup(deaddevitab, dev->d_name)) == NULL) 1813 return 0; 1814 return (i->i_level >= level); 1815 } 1816 1817 for (da = dev->d_ahead; da != NULL; da = da->d_bsame) 1818 for (i = da->d_ihead; i != NULL; i = i->i_asame) 1819 if ((i->i_active == DEVI_ACTIVE || 1820 i->i_active == state) && 1821 (unit == WILD || unit == i->i_unit || 1822 i->i_unit == STAR)) 1823 return 1; 1824 1825 if (state == DEVI_IGNORED) { 1826 struct dhdi_params dhdi = { dev, unit, level }; 1827 /* also check dead devices */ 1828 return ht_enumerate(deaddevitab, devbase_has_dead_instances, 1829 &dhdi); 1830 } 1831 1832 return 0; 1833 } 1834 1835 /* 1836 * check_dead_devi(), used with ht_enumerate, checks if any of the removed 1837 * device instances would have been a valid instance considering the devbase, 1838 * the parent device and the interface attribute. 1839 * 1840 * In other words, for a non-active device, it checks if children would be 1841 * actual orphans or the result of a negative statement in the config file. 1842 */ 1843 1844 struct cdd_params { 1845 struct devbase *d; 1846 struct attr *at; 1847 struct devbase *parent; 1848 }; 1849 1850 static int 1851 check_dead_devi(const char *key, void *value, void *aux) 1852 { 1853 struct cdd_params *cdd = aux; 1854 struct devi *i = value; 1855 struct pspec *p; 1856 1857 if (i->i_base != cdd->d) 1858 return 0; 1859 1860 for (; i != NULL; i = i->i_alias) { 1861 p = i->i_pspec; 1862 if ((p == NULL && cdd->at == NULL) || 1863 (p != NULL && p->p_iattr == cdd->at && 1864 (p->p_atdev == NULL || p->p_atdev == cdd->parent))) { 1865 if (p != NULL && 1866 !devbase_has_any_instance(cdd->parent, p->p_atunit, 1867 DEVI_IGNORED, i->i_level)) 1868 return 0; 1869 else 1870 return 1; 1871 } 1872 } 1873 return 0; 1874 } 1875 1876 static struct devbase root; 1877 1878 static int 1879 addlevelparent(struct devbase *d, struct devbase *parent) 1880 { 1881 struct devbase *p; 1882 1883 if (d == parent) { 1884 if (d->d_level > 1) 1885 return 0; 1886 return 1; 1887 } 1888 1889 if (d->d_levelparent) { 1890 if (d->d_level > 1) 1891 return 0; 1892 return 1; 1893 } 1894 1895 for (p = parent; p != NULL; p = p->d_levelparent) 1896 if (d == p && d->d_level > 1) 1897 return 0; 1898 d->d_levelparent = p ? p : &root; 1899 d->d_level++; 1900 return 1; 1901 } 1902 1903 static void 1904 do_kill_orphans(struct devbase *d, struct attr *at, struct devbase *parent, 1905 int state) 1906 { 1907 struct nvlist *nv1; 1908 struct attrlist *al; 1909 struct attr *a; 1910 struct devi *i, *j = NULL; 1911 struct pspec *p; 1912 int active = 0; 1913 1914 if (!addlevelparent(d, parent)) 1915 return; 1916 1917 /* 1918 * A pseudo-device will always attach at root, and if it has an 1919 * instance (it cannot have more than one), it is enough to consider 1920 * it active, as there is no real attachment. 1921 * 1922 * A pseudo device can never be marked DEVI_IGNORED. 1923 */ 1924 if (d->d_ispseudo) { 1925 if (d->d_ihead != NULL) 1926 d->d_ihead->i_active = active = DEVI_ACTIVE; 1927 else { 1928 if (ht_lookup(deaddevitab, d->d_name) != NULL) 1929 active = DEVI_IGNORED; 1930 else 1931 return; 1932 } 1933 } else { 1934 int seen = 0; 1935 int changed = 0; 1936 1937 for (i = d->d_ihead; i != NULL; i = i->i_bsame) { 1938 for (j = i; j != NULL; j = j->i_alias) { 1939 p = j->i_pspec; 1940 if ((p == NULL && at == NULL) || 1941 (p != NULL && p->p_iattr == at && 1942 (p->p_atdev == NULL || 1943 p->p_atdev == parent))) { 1944 if (p != NULL && 1945 !devbase_has_any_instance(parent, 1946 p->p_atunit, state, j->i_level)) 1947 continue; 1948 /* 1949 * There are Fry-like devices which can 1950 * be their own grand-parent (or even 1951 * parent, like uhub). We don't want 1952 * to loop, so if we've already reached 1953 * an instance for one reason or 1954 * another, stop there. 1955 */ 1956 if (j->i_active == DEVI_ACTIVE || 1957 j->i_active == state) { 1958 /* 1959 * Device has already been 1960 * seen. However it might 1961 * have siblings who still 1962 * have to be activated or 1963 * orphaned. 1964 */ 1965 seen = 1; 1966 continue; 1967 } 1968 changed |= j->i_active != state; 1969 j->i_active = active = state; 1970 if (p != NULL) { 1971 if (state == DEVI_ACTIVE || 1972 --p->p_ref == 0) 1973 p->p_active = state; 1974 } 1975 if (state == DEVI_IGNORED) { 1976 CFGDBG(5, 1977 "`%s' at '%s' ignored", 1978 d->d_name, parent ? 1979 parent->d_name : "(root)"); 1980 } 1981 } 1982 } 1983 } 1984 /* 1985 * If we've been there but have made no change, stop. 1986 */ 1987 if (seen && active != DEVI_ACTIVE) 1988 goto out; 1989 if (active != DEVI_ACTIVE) { 1990 struct cdd_params cdd = { d, at, parent }; 1991 /* Look for a matching dead devi */ 1992 if (ht_enumerate(deaddevitab, check_dead_devi, &cdd) && 1993 d != parent) { 1994 /* 1995 * That device had its instances removed. 1996 * Continue the loop marking descendants 1997 * with DEVI_IGNORED instead of DEVI_ACTIVE. 1998 * 1999 * There is one special case for devices that 2000 * are their own parent: if that instance is 2001 * removed (e.g., no uhub* at uhub?), we don't 2002 * have to continue looping. 2003 */ 2004 active = DEVI_IGNORED; 2005 CFGDBG(5, "`%s' at '%s' ignored", d->d_name, 2006 parent ? parent->d_name : "(root)"); 2007 2008 } else if (!changed) 2009 goto out; 2010 } 2011 } 2012 2013 for (al = d->d_attrs; al != NULL; al = al->al_next) { 2014 a = al->al_this; 2015 for (nv1 = a->a_devs; nv1 != NULL; nv1 = nv1->nv_next) { 2016 do_kill_orphans(nv1->nv_ptr, a, d, active); 2017 } 2018 } 2019 out: 2020 d->d_levelparent = NULL; 2021 d->d_level--; 2022 } 2023 2024 static int 2025 /*ARGSUSED*/ 2026 kill_orphans_cb(const char *key, void *value, void *aux) 2027 { 2028 do_kill_orphans((struct devbase *)value, NULL, NULL, DEVI_ACTIVE); 2029 return 0; 2030 } 2031 2032 static void 2033 kill_orphans(void) 2034 { 2035 ht_enumerate(devroottab, kill_orphans_cb, NULL); 2036 } 2037 2038 static void 2039 add_makeopt(const char *opt) 2040 { 2041 struct nvlist *p; 2042 char *buf = estrdup(opt); 2043 char *eq = strchr(buf, '='); 2044 2045 if (!eq) 2046 errx(EXIT_FAILURE, "-D %s is not in var=value format", opt); 2047 2048 *eq = 0; 2049 p = newnv(estrdup(buf), estrdup(eq+1), NULL, 0, NULL); 2050 free(buf); 2051 p->nv_next = cmdlinedefs; 2052 cmdlinedefs = p; 2053 } 2054 2055 static void 2056 remove_makeopt(const char *opt) 2057 { 2058 struct nvlist *p; 2059 2060 p = newnv(estrdup(opt), NULL, NULL, 0, NULL); 2061 p->nv_next = cmdlineundefs; 2062 cmdlineundefs = p; 2063 } 2064 2065 static void 2066 handle_cmdline_makeoptions(void) 2067 { 2068 struct nvlist *p, *n; 2069 2070 handling_cmdlineopts = 1; 2071 for (p = cmdlineundefs; p; p = n) { 2072 n = p->nv_next; 2073 delmkoption(intern(p->nv_name), 0); 2074 free(__UNCONST(p->nv_name)); 2075 nvfree(p); 2076 } 2077 for (p = cmdlinedefs; p; p = n) { 2078 const char *name = intern(p->nv_name); 2079 2080 n = p->nv_next; 2081 delmkoption(name, 0); 2082 addmkoption(name, intern(p->nv_str)); 2083 free(__UNCONST(p->nv_name)); 2084 free(__UNCONST(p->nv_str)); 2085 2086 nvfree(p); 2087 } 2088 handling_cmdlineopts = 0; 2089 } 2090