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