1 /* $OpenBSD: main.c,v 1.44 2012/06/22 22:02:29 guenther Exp $ */ 2 /* $NetBSD: main.c,v 1.22 1997/02/02 21:12:33 thorpej Exp $ */ 3 4 /* 5 * Copyright (c) 1992, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This software was developed by the Computer Systems Engineering group 9 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 10 * contributed to Berkeley. 11 * 12 * All advertising materials mentioning features or use of this software 13 * must display the following acknowledgement: 14 * This product includes software developed by the University of 15 * California, Lawrence Berkeley Laboratories. 16 * 17 * Redistribution and use in source and binary forms, with or without 18 * modification, are permitted provided that the following conditions 19 * are met: 20 * 1. Redistributions of source code must retain the above copyright 21 * notice, this list of conditions and the following disclaimer. 22 * 2. Redistributions in binary form must reproduce the above copyright 23 * notice, this list of conditions and the following disclaimer in the 24 * documentation and/or other materials provided with the distribution. 25 * 3. Neither the name of the University nor the names of its contributors 26 * may be used to endorse or promote products derived from this software 27 * without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 * 41 * from: @(#)main.c 8.1 (Berkeley) 6/6/93 42 */ 43 44 #include <sys/types.h> 45 #include <sys/stat.h> 46 #include <sys/param.h> 47 48 #include <ctype.h> 49 #include <err.h> 50 #include <errno.h> 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <string.h> 54 #include <unistd.h> 55 56 #include "config.h" 57 58 int firstfile(const char *); 59 int yyparse(void); 60 61 extern char *optarg; 62 extern int optind; 63 64 static struct hashtab *mkopttab; 65 static struct nvlist **nextopt; 66 static struct nvlist **nextdefopt; 67 static struct nvlist **nextmkopt; 68 69 static __dead void stop(void); 70 static int do_option(struct hashtab *, struct nvlist ***, 71 const char *, const char *, const char *); 72 static int crosscheck(void); 73 static int badstar(void); 74 static int mksymlinks(void); 75 static int hasparent(struct devi *); 76 static int cfcrosscheck(struct config *, const char *, struct nvlist *); 77 static void optiondelta(void); 78 79 int madedir = 0; 80 81 int verbose; 82 83 void 84 usage(void) 85 { 86 extern char *__progname; 87 88 fprintf(stderr, 89 "usage: %s [-p] [-b builddir] [-s srcdir] [config-file]\n" 90 " %s [-u] [-f | -o outfile] -e infile\n", 91 __progname, __progname); 92 93 exit(1); 94 } 95 96 int 97 main(int argc, char *argv[]) 98 { 99 char *p; 100 const char *last_component; 101 char *outfile = NULL; 102 int pflag, ch, eflag, uflag, fflag; 103 104 pflag = eflag = uflag = fflag = 0; 105 while ((ch = getopt(argc, argv, "egpfb:s:o:u")) != -1) { 106 switch (ch) { 107 108 case 'o': 109 outfile = optarg; 110 break; 111 case 'u': 112 uflag = 1; 113 break; 114 case 'f': 115 fflag = 1; 116 break; 117 118 case 'e': 119 eflag = 1; 120 if (!isatty(STDIN_FILENO)) 121 verbose = 1; 122 break; 123 124 case 'g': 125 /* 126 * In addition to DEBUG, you probably wanted to 127 * set "options KGDB" and maybe others. We could 128 * do that for you, but you really should just 129 * put them in the config file. 130 */ 131 (void)fputs( 132 "-g is obsolete (use makeoptions DEBUG=\"-g\")\n", 133 stderr); 134 usage(); 135 136 case 'p': 137 /* 138 * Essentially the same as makeoptions PROF="-pg", 139 * but also changes the path from ../../compile/FOO 140 * to ../../compile/FOO.PROF; i.e., compile a 141 * profiling kernel based on a typical "regular" 142 * kernel. 143 * 144 * Note that if you always want profiling, you 145 * can (and should) use a "makeoptions" line. 146 */ 147 pflag = 1; 148 break; 149 150 case 'b': 151 builddir = optarg; 152 break; 153 154 case 's': 155 srcdir = optarg; 156 break; 157 158 default: 159 usage(); 160 } 161 } 162 163 argc -= optind; 164 argv += optind; 165 if (argc > 1 || (eflag && argv[0] == NULL)) 166 usage(); 167 168 if (eflag) { 169 #ifdef MAKE_BOOTSTRAP 170 fprintf(stderr, "config: UKC not available in this binary\n"); 171 exit(1); 172 #else 173 return (ukc(argv[0], outfile, uflag, fflag)); 174 #endif 175 } 176 177 conffile = (argc == 1) ? argv[0] : "CONFIG"; 178 if (firstfile(conffile)) { 179 (void)fprintf(stderr, "config: cannot read %s: %s\n", 180 conffile, strerror(errno)); 181 exit(2); 182 } 183 184 /* 185 * Init variables. 186 */ 187 minmaxusers = 1; 188 maxmaxusers = 10000; 189 initintern(); 190 initfiles(); 191 initsem(); 192 devbasetab = ht_new(); 193 devatab = ht_new(); 194 selecttab = ht_new(); 195 needcnttab = ht_new(); 196 opttab = ht_new(); 197 mkopttab = ht_new(); 198 defopttab = ht_new(); 199 nextopt = &options; 200 nextmkopt = &mkoptions; 201 nextdefopt = &defoptions; 202 203 /* 204 * Handle profiling (must do this before we try to create any 205 * files). 206 */ 207 last_component = strrchr(conffile, '/'); 208 last_component = (last_component) ? last_component + 1 : conffile; 209 if (pflag) { 210 int len = strlen(last_component) + 17; 211 p = emalloc(len); 212 (void)snprintf(p, len, "../compile/%s.PROF", last_component); 213 (void)addmkoption(intern("PROF"), "-pg"); 214 (void)addoption(intern("GPROF"), NULL); 215 } else { 216 int len = strlen(last_component) + 12; 217 p = emalloc(len); 218 (void)snprintf(p, len, "../compile/%s", last_component); 219 } 220 defbuilddir = (argc == 0) ? "." : p; 221 222 /* 223 * Parse config file (including machine definitions). 224 */ 225 if (yyparse()) 226 stop(); 227 228 /* 229 * Fix (as in `set firmly in place') files. 230 */ 231 if (fixfiles()) 232 stop(); 233 234 /* 235 * Fix objects and libraries. 236 */ 237 if (fixobjects()) 238 stop(); 239 240 /* 241 * Perform cross-checking. 242 */ 243 if (maxusers == 0) { 244 if (defmaxusers) { 245 (void)printf("maxusers not specified; %d assumed\n", 246 defmaxusers); 247 maxusers = defmaxusers; 248 } else { 249 (void)fprintf(stderr, 250 "config: need \"maxusers\" line\n"); 251 errors++; 252 } 253 } 254 if (crosscheck() || errors) 255 stop(); 256 257 /* 258 * Squeeze things down and finish cross-checks (STAR checks must 259 * run after packing). 260 */ 261 pack(); 262 if (badstar()) 263 stop(); 264 265 /* 266 * Ready to go. Build all the various files. 267 */ 268 if (mksymlinks() || mkmakefile() || mkheaders() || mkswap() || 269 mkioconf()) 270 stop(); 271 optiondelta(); 272 exit(0); 273 } 274 275 static int 276 mksymlink(const char *value, const char *path) 277 { 278 int ret = 0; 279 280 if (remove(path) && errno != ENOENT) { 281 warn("config: remove(%s)", path); 282 ret = 1; 283 } 284 if (symlink(value, path)) { 285 warn("config: symlink(%s -> %s)", path, value); 286 ret = 1; 287 } 288 return (ret); 289 } 290 291 292 /* 293 * Make a symlink for "machine" so that "#include <machine/foo.h>" works, 294 * and for the machine's CPU architecture, so that works as well. 295 */ 296 static int 297 mksymlinks(void) 298 { 299 int ret; 300 char *p, buf[MAXPATHLEN]; 301 const char *q; 302 303 snprintf(buf, sizeof buf, "arch/%s/include", machine); 304 p = sourcepath(buf); 305 ret = mksymlink(p, "machine"); 306 if (machinearch != NULL) { 307 snprintf(buf, sizeof buf, "arch/%s/include", machinearch); 308 p = sourcepath(buf); 309 q = machinearch; 310 } else { 311 p = strdup("machine"); 312 if (!p) 313 errx(1, "out of memory"); 314 q = machine; 315 } 316 ret |= mksymlink(p, q); 317 free(p); 318 319 return (ret); 320 } 321 322 static __dead void 323 stop(void) 324 { 325 (void)fprintf(stderr, "*** Stop.\n"); 326 exit(1); 327 } 328 329 /* 330 * Define a standard option, for which a header file will be generated. 331 */ 332 void 333 defoption(const char *name) 334 { 335 char *p, *low, c; 336 const char *n; 337 338 /* 339 * Convert to lower case. The header file name will be 340 * in lower case, so we store the lower case version in 341 * the hash table to detect option name collisions. The 342 * original string will be stored in the nvlist for use 343 * in the header file. 344 */ 345 low = emalloc(strlen(name) + 1); 346 for (n = name, p = low; (c = *n) != '\0'; n++) 347 *p++ = isupper(c) ? tolower(c) : c; 348 *p = 0; 349 350 n = intern(low); 351 free(low); 352 (void)do_option(defopttab, &nextdefopt, n, name, "defopt"); 353 354 /* 355 * Insert a verbatim copy of the option name, as well, 356 * to speed lookups when creating the Makefile. 357 */ 358 (void)ht_insert(defopttab, name, (void *)name); 359 } 360 361 /* 362 * Remove an option. 363 */ 364 void 365 removeoption(const char *name) 366 { 367 struct nvlist *nv, *nvt; 368 char *p, *low, c; 369 const char *n; 370 371 if ((nv = ht_lookup(opttab, name)) != NULL) { 372 if (options == nv) { 373 options = nv->nv_next; 374 nvfree(nv); 375 } else { 376 nvt = options; 377 while (nvt->nv_next != NULL) { 378 if (nvt->nv_next == nv) { 379 nvt->nv_next = nvt->nv_next->nv_next; 380 nvfree(nv); 381 break; 382 } else 383 nvt = nvt->nv_next; 384 } 385 } 386 } 387 388 (void)ht_remove(opttab, name); 389 390 low = emalloc(strlen(name) + 1); 391 /* make lowercase, then remove from select table */ 392 for (n = name, p = low; (c = *n) != '\0'; n++) 393 *p++ = isupper(c) ? tolower(c) : c; 394 *p = 0; 395 n = intern(low); 396 free(low); 397 (void)ht_remove(selecttab, n); 398 } 399 400 /* 401 * Add an option from "options FOO". Note that this selects things that 402 * are "optional foo". 403 */ 404 void 405 addoption(const char *name, const char *value) 406 { 407 char *p, *low, c; 408 const char *n; 409 410 if (do_option(opttab, &nextopt, name, value, "options")) 411 return; 412 413 low = emalloc(strlen(name) + 1); 414 /* make lowercase, then add to select table */ 415 for (n = name, p = low; (c = *n) != '\0'; n++) 416 *p++ = isupper(c) ? tolower(c) : c; 417 *p = 0; 418 n = intern(low); 419 free(low); 420 (void)ht_insert(selecttab, n, (void *)n); 421 } 422 423 /* 424 * Add a "make" option. 425 */ 426 void 427 addmkoption(const char *name, const char *value) 428 { 429 430 (void)do_option(mkopttab, &nextmkopt, name, value, "mkoptions"); 431 } 432 433 /* 434 * Add a name=value pair to an option list. The value may be NULL. 435 */ 436 static int 437 do_option(struct hashtab *ht, struct nvlist ***nppp, const char *name, 438 const char *value, const char *type) 439 { 440 struct nvlist *nv; 441 442 /* assume it will work */ 443 nv = newnv(name, value, NULL, 0, NULL); 444 if (ht_insert(ht, name, nv) == 0) { 445 **nppp = nv; 446 *nppp = &nv->nv_next; 447 return (0); 448 } 449 450 /* oops, already got that option */ 451 nvfree(nv); 452 if ((nv = ht_lookup(ht, name)) == NULL) 453 panic("do_option"); 454 if (nv->nv_str != NULL) 455 error("already have %s `%s=%s'", type, name, nv->nv_str); 456 else 457 error("already have %s `%s'", type, name); 458 return (1); 459 } 460 461 /* 462 * Return true if there is at least one instance of the given unit 463 * on the given device attachment (or any units, if unit == WILD). 464 */ 465 int 466 deva_has_instances(struct deva *deva, int unit) 467 { 468 struct devi *i; 469 470 if (unit == WILD) 471 return (deva->d_ihead != NULL); 472 for (i = deva->d_ihead; i != NULL; i = i->i_asame) 473 if (unit == i->i_unit) 474 return (1); 475 return (0); 476 } 477 478 /* 479 * Return true if there is at least one instance of the given unit 480 * on the given base (or any units, if unit == WILD). 481 */ 482 int 483 devbase_has_instances(struct devbase *dev, int unit) 484 { 485 struct deva *da; 486 487 for (da = dev->d_ahead; da != NULL; da = da->d_bsame) 488 if (deva_has_instances(da, unit)) 489 return (1); 490 return (0); 491 } 492 493 static int 494 hasparent(struct devi *i) 495 { 496 struct nvlist *nv; 497 int atunit = i->i_atunit; 498 499 /* 500 * We determine whether or not a device has a parent in in one 501 * of two ways: 502 * (1) If a parent device was named in the config file, 503 * i.e. cases (2) and (3) in sem.c:adddev(), then 504 * we search its devbase for a matching unit number. 505 * (2) If the device was attach to an attribute, then we 506 * search all attributes the device can be attached to 507 * for parents (with appropriate unit numbers) that 508 * may be able to attach the device. 509 */ 510 511 /* 512 * Case (1): A parent was named. Either it's configured, or not. 513 */ 514 if (i->i_atdev != NULL) 515 return (devbase_has_instances(i->i_atdev, atunit)); 516 517 /* 518 * Case (2): No parent was named. Look for devs that provide the attr. 519 */ 520 if (i->i_atattr != NULL) 521 for (nv = i->i_atattr->a_refs; nv != NULL; nv = nv->nv_next) 522 if (devbase_has_instances(nv->nv_ptr, atunit)) 523 return (1); 524 return (0); 525 } 526 527 static int 528 cfcrosscheck(struct config *cf, const char *what, struct nvlist *nv) 529 { 530 struct devbase *dev; 531 struct devi *pd; 532 int errs, devminor; 533 534 if (maxpartitions <= 0) 535 panic("cfcrosscheck"); 536 537 for (errs = 0; nv != NULL; nv = nv->nv_next) { 538 if (nv->nv_name == NULL) 539 continue; 540 dev = ht_lookup(devbasetab, nv->nv_name); 541 if (dev == NULL) 542 panic("cfcrosscheck(%s)", nv->nv_name); 543 devminor = minor(nv->nv_int) / maxpartitions; 544 if (devbase_has_instances(dev, devminor)) 545 continue; 546 if (devbase_has_instances(dev, STAR) && 547 devminor >= dev->d_umax) 548 continue; 549 for (pd = allpseudo; pd != NULL; pd = pd->i_next) 550 if (pd->i_base == dev && devminor < dev->d_umax && 551 devminor >= 0) 552 goto loop; 553 (void)fprintf(stderr, 554 "%s:%d: %s says %s on %s, but there's no %s\n", 555 conffile, cf->cf_lineno, 556 cf->cf_name, what, nv->nv_str, nv->nv_str); 557 errs++; 558 loop: 559 ; 560 } 561 return (errs); 562 } 563 564 /* 565 * Cross-check the configuration: make sure that each target device 566 * or attribute (`at foo[0*?]') names at least one real device. Also 567 * see that the root, swap, and dump devices for all configurations 568 * are there. 569 */ 570 int 571 crosscheck(void) 572 { 573 struct devi *i; 574 struct config *cf; 575 int errs; 576 577 errs = 0; 578 for (i = alldevi; i != NULL; i = i->i_next) { 579 if (i->i_at == NULL || hasparent(i)) 580 continue; 581 xerror(conffile, i->i_lineno, 582 "%s at %s is orphaned", i->i_name, i->i_at); 583 (void)fprintf(stderr, " (%s %s declared)\n", 584 i->i_atunit == WILD ? "nothing matching" : "no", 585 i->i_at); 586 errs++; 587 } 588 if (allcf == NULL) { 589 (void)fprintf(stderr, "%s has no configurations!\n", 590 conffile); 591 errs++; 592 } 593 for (cf = allcf; cf != NULL; cf = cf->cf_next) { 594 if (cf->cf_root != NULL) { /* i.e., not swap generic */ 595 errs += cfcrosscheck(cf, "root", cf->cf_root); 596 errs += cfcrosscheck(cf, "swap", cf->cf_swap); 597 errs += cfcrosscheck(cf, "dumps", cf->cf_dump); 598 } 599 } 600 return (errs); 601 } 602 603 /* 604 * Check to see if there is a *'d unit with a needs-count file. 605 */ 606 int 607 badstar(void) 608 { 609 struct devbase *d; 610 struct deva *da; 611 struct devi *i; 612 int errs, n; 613 614 errs = 0; 615 for (d = allbases; d != NULL; d = d->d_next) { 616 for (da = d->d_ahead; da != NULL; da = da->d_bsame) 617 for (i = da->d_ihead; i != NULL; i = i->i_asame) { 618 if (i->i_unit == STAR) 619 goto foundstar; 620 } 621 continue; 622 foundstar: 623 if (ht_lookup(needcnttab, d->d_name)) { 624 (void)fprintf(stderr, 625 "config: %s's cannot be *'d until its driver is fixed\n", 626 d->d_name); 627 errs++; 628 continue; 629 } 630 for (n = 0; i != NULL; i = i->i_alias) 631 if (!i->i_collapsed) 632 n++; 633 if (n < 1) 634 panic("badstar() n<1"); 635 } 636 return (errs); 637 } 638 639 /* 640 * Verify/create builddir if necessary, change to it, and verify srcdir. 641 * This will be called when we see the first include. 642 */ 643 void 644 setupdirs(void) 645 { 646 struct stat st; 647 648 /* srcdir must be specified if builddir is not specified or if 649 * no configuration filename was specified. */ 650 if ((builddir || strcmp(defbuilddir, ".") == 0) && !srcdir) { 651 error("source directory must be specified"); 652 exit(1); 653 } 654 655 if (srcdir == NULL) 656 srcdir = "../../../.."; 657 if (builddir == NULL) 658 builddir = defbuilddir; 659 660 if (stat(builddir, &st) != 0) { 661 if (mkdir(builddir, 0777)) { 662 (void)fprintf(stderr, "config: cannot create %s: %s\n", 663 builddir, strerror(errno)); 664 exit(2); 665 } 666 madedir = 1; 667 } else if (!S_ISDIR(st.st_mode)) { 668 (void)fprintf(stderr, "config: %s is not a directory\n", 669 builddir); 670 exit(2); 671 } 672 if (chdir(builddir) != 0) { 673 (void)fprintf(stderr, "config: cannot change to %s\n", 674 builddir); 675 exit(2); 676 } 677 if (stat(srcdir, &st) != 0 || !S_ISDIR(st.st_mode)) { 678 (void)fprintf(stderr, "config: %s is not a directory\n", 679 srcdir); 680 exit(2); 681 } 682 } 683 684 struct opt { 685 const char *name; 686 const char *val; 687 }; 688 689 int 690 optcmp(const void *v1, const void *v2) 691 { 692 const struct opt *sp1 = v1, *sp2 = v2; 693 int r; 694 695 r = strcmp(sp1->name, sp2->name); 696 if (r == 0) { 697 if (!sp1->val && !sp2->val) 698 r = 0; 699 else if (sp1->val && !sp2->val) 700 r = -1; 701 else if (sp2->val && !sp1->val) 702 r = 1; 703 else r = strcmp(sp1->val, sp2->val); 704 } 705 return (r); 706 } 707 708 void 709 optiondelta(void) 710 { 711 struct nvlist *nv; 712 char nbuf[BUFSIZ], obuf[BUFSIZ]; /* XXX size */ 713 int nnewopts, ret = 0, i; 714 struct opt *newopts; 715 FILE *fp; 716 717 for (nnewopts = 0, nv = options; nv != NULL; nv = nv->nv_next) 718 nnewopts++; 719 newopts = (struct opt *)emalloc(nnewopts * sizeof(struct opt)); 720 if (newopts == NULL) 721 ret = 0; 722 for (i = 0, nv = options; nv != NULL; nv = nv->nv_next, i++) { 723 newopts[i].name = nv->nv_name; 724 newopts[i].val = nv->nv_str; 725 } 726 qsort(newopts, nnewopts, sizeof (struct opt), optcmp); 727 728 /* compare options against previous config */ 729 if ((fp = fopen("options", "r"))) { 730 for (i = 0; !feof(fp) && i < nnewopts && ret == 0; i++) { 731 if (newopts[i].val) 732 snprintf(nbuf, sizeof nbuf, "%s=%s\n", 733 newopts[i].name, newopts[i].val); 734 else 735 snprintf(nbuf, sizeof nbuf, "%s\n", 736 newopts[i].name); 737 if (fgets(obuf, sizeof obuf, fp) == NULL || 738 strcmp(nbuf, obuf)) 739 ret = 1; 740 } 741 fclose(fp); 742 fp = NULL; 743 } else 744 ret = 1; 745 746 /* replace with the new list of options */ 747 if ((fp = fopen("options", "w+"))) { 748 rewind(fp); 749 for (i = 0; i < nnewopts; i++) { 750 if (newopts[i].val) 751 fprintf(fp, "%s=%s\n", newopts[i].name, 752 newopts[i].val); 753 else 754 fprintf(fp, "%s\n", newopts[i].name); 755 } 756 fclose(fp); 757 } 758 free(newopts); 759 if (ret == 0 || madedir == 1) 760 return; 761 (void)printf("Kernel options have changed -- you must run \"make clean\"\n"); 762 } 763