1 /* $OpenBSD: main.c,v 1.20 2000/01/04 14:23:43 angelos 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 return (ukc(argv[0], outfile, uflag, fflag)); 174 175 conffile = (argc == 1) ? argv[0] : "CONFIG"; 176 if (firstfile(conffile)) { 177 (void)fprintf(stderr, "config: cannot read %s: %s\n", 178 conffile, strerror(errno)); 179 exit(2); 180 } 181 182 /* 183 * Init variables. 184 */ 185 minmaxusers = 1; 186 maxmaxusers = 10000; 187 initintern(); 188 initfiles(); 189 initsem(); 190 devbasetab = ht_new(); 191 devatab = ht_new(); 192 selecttab = ht_new(); 193 needcnttab = ht_new(); 194 opttab = ht_new(); 195 mkopttab = ht_new(); 196 defopttab = ht_new(); 197 nextopt = &options; 198 nextmkopt = &mkoptions; 199 nextdefopt = &defoptions; 200 201 /* 202 * Handle profiling (must do this before we try to create any 203 * files). 204 */ 205 last_component = strrchr(conffile, '/'); 206 last_component = (last_component) ? last_component + 1 : conffile; 207 if (pflag) { 208 p = emalloc(strlen(last_component) + 17); 209 (void)sprintf(p, "../compile/%s.PROF", last_component); 210 (void)addmkoption(intern("PROF"), "-pg"); 211 (void)addoption(intern("GPROF"), NULL); 212 } else { 213 p = emalloc(strlen(last_component) + 13); 214 (void)sprintf(p, "../compile/%s", last_component); 215 } 216 defbuilddir = (argc == 0) ? "." : p; 217 218 /* 219 * Parse config file (including machine definitions). 220 */ 221 if (yyparse()) 222 stop(); 223 224 /* 225 * Fix (as in `set firmly in place') files. 226 */ 227 if (fixfiles()) 228 stop(); 229 230 /* 231 * Fix objects and libraries. 232 */ 233 if (fixobjects()) 234 stop(); 235 236 /* 237 * Perform cross-checking. 238 */ 239 if (maxusers == 0) { 240 if (defmaxusers) { 241 (void)printf("maxusers not specified; %d assumed\n", 242 defmaxusers); 243 maxusers = defmaxusers; 244 } else { 245 (void)fprintf(stderr, 246 "config: need \"maxusers\" line\n"); 247 errors++; 248 } 249 } 250 if (crosscheck() || errors) 251 stop(); 252 253 /* 254 * Squeeze things down and finish cross-checks (STAR checks must 255 * run after packing). 256 */ 257 pack(); 258 if (badstar()) 259 stop(); 260 261 /* 262 * Ready to go. Build all the various files. 263 */ 264 if (mksymlinks() || mkmakefile() || mkheaders() || mkswap() || 265 mkioconf()) 266 stop(); 267 (void)printf("Don't forget to run \"make depend\"\n"); 268 optiondelta(); 269 exit(0); 270 } 271 272 /* 273 * Make a symlink for "machine" so that "#include <machine/foo.h>" works, 274 * and for the machine's CPU architecture, so that works as well. 275 */ 276 static int 277 mksymlinks() 278 { 279 int ret; 280 char *p, buf[MAXPATHLEN]; 281 const char *q; 282 283 sprintf(buf, "arch/%s/include", machine); 284 p = sourcepath(buf); 285 (void)unlink("machine"); 286 ret = symlink(p, "machine"); 287 if (ret) 288 (void)fprintf(stderr, "config: symlink(machine -> %s): %s\n", 289 p, strerror(errno)); 290 291 if (machinearch != NULL) { 292 sprintf(buf, "arch/%s/include", machinearch); 293 p = sourcepath(buf); 294 q = machinearch; 295 } else { 296 p = strdup("machine"); 297 q = machine; 298 } 299 (void)unlink(q); 300 ret = symlink(p, q); 301 if (ret) 302 (void)fprintf(stderr, "config: symlink(%s -> %s): %s\n", 303 q, p, strerror(errno)); 304 free(p); 305 306 return (ret); 307 } 308 309 static __dead void 310 stop() 311 { 312 (void)fprintf(stderr, "*** Stop.\n"); 313 exit(1); 314 } 315 316 /* 317 * Define a standard option, for which a header file will be generated. 318 */ 319 void 320 defoption(name) 321 const char *name; 322 { 323 register const char *n; 324 register char *p, c; 325 char low[500]; 326 327 /* 328 * Convert to lower case. The header file name will be 329 * in lower case, so we store the lower case version in 330 * the hash table to detect option name collisions. The 331 * original string will be stored in the nvlist for use 332 * in the header file. 333 */ 334 for (n = name, p = low; (c = *n) != '\0'; n++) 335 *p++ = isupper(c) ? tolower(c) : c; 336 *p = 0; 337 338 n = intern(low); 339 (void)do_option(defopttab, &nextdefopt, n, name, "defopt"); 340 341 /* 342 * Insert a verbatum copy of the option name, as well, 343 * to speed lookups when creating the Makefile. 344 */ 345 (void)ht_insert(defopttab, name, (void *)name); 346 } 347 348 /* 349 * Remove an option. 350 */ 351 void 352 removeoption(name) 353 const char *name; 354 { 355 register struct nvlist *nv, *nvt; 356 register const char *n; 357 register char *p, c; 358 char low[500]; 359 360 if ((nv = ht_lookup(opttab, name)) != NULL) { 361 if (options == nv) 362 { 363 options = nv->nv_next; 364 nvfree(nv); 365 } else { 366 nvt = options; 367 while (nvt->nv_next != NULL) { 368 if (nvt->nv_next == nv) { 369 nvt->nv_next = nvt->nv_next->nv_next; 370 nvfree(nv); 371 break; 372 } 373 else 374 nvt = nvt->nv_next; 375 } 376 } 377 } 378 379 (void)ht_remove(opttab, name); 380 381 /* make lowercase, then add to select table */ 382 for (n = name, p = low; (c = *n) != '\0'; n++) 383 *p++ = isupper(c) ? tolower(c) : c; 384 *p = 0; 385 n = intern(low); 386 (void)ht_remove(selecttab, n); 387 } 388 389 /* 390 * Add an option from "options FOO". Note that this selects things that 391 * are "optional foo". 392 */ 393 void 394 addoption(name, value) 395 const char *name, *value; 396 { 397 register const char *n; 398 register char *p, c; 399 char low[500]; 400 401 if (do_option(opttab, &nextopt, name, value, "options")) 402 return; 403 404 /* make lowercase, then add to select table */ 405 for (n = name, p = low; (c = *n) != '\0'; n++) 406 *p++ = isupper(c) ? tolower(c) : c; 407 *p = 0; 408 n = intern(low); 409 (void)ht_insert(selecttab, n, (void *)n); 410 } 411 412 /* 413 * Add a "make" option. 414 */ 415 void 416 addmkoption(name, value) 417 const char *name, *value; 418 { 419 420 (void)do_option(mkopttab, &nextmkopt, name, value, "mkoptions"); 421 } 422 423 /* 424 * Add a name=value pair to an option list. The value may be NULL. 425 */ 426 static int 427 do_option(ht, nppp, name, value, type) 428 struct hashtab *ht; 429 struct nvlist ***nppp; 430 const char *name, *value, *type; 431 { 432 register struct nvlist *nv; 433 434 /* assume it will work */ 435 nv = newnv(name, value, NULL, 0, NULL); 436 if (ht_insert(ht, name, nv) == 0) { 437 **nppp = nv; 438 *nppp = &nv->nv_next; 439 return (0); 440 } 441 442 /* oops, already got that option */ 443 nvfree(nv); 444 if ((nv = ht_lookup(ht, name)) == NULL) 445 panic("do_option"); 446 if (nv->nv_str != NULL) 447 error("already have %s `%s=%s'", type, name, nv->nv_str); 448 else 449 error("already have %s `%s'", type, name); 450 return (1); 451 } 452 453 /* 454 * Return true if there is at least one instance of the given unit 455 * on the given device attachment (or any units, if unit == WILD). 456 */ 457 int 458 deva_has_instances(deva, unit) 459 register struct deva *deva; 460 int unit; 461 { 462 register struct devi *i; 463 464 if (unit == WILD) 465 return (deva->d_ihead != NULL); 466 for (i = deva->d_ihead; i != NULL; i = i->i_asame) 467 if (unit == i->i_unit) 468 return (1); 469 return (0); 470 } 471 472 /* 473 * Return true if there is at least one instance of the given unit 474 * on the given base (or any units, if unit == WILD). 475 */ 476 int 477 devbase_has_instances(dev, unit) 478 register struct devbase *dev; 479 int unit; 480 { 481 register struct deva *da; 482 483 for (da = dev->d_ahead; da != NULL; da = da->d_bsame) 484 if (deva_has_instances(da, unit)) 485 return (1); 486 return (0); 487 } 488 489 static int 490 hasparent(i) 491 register struct devi *i; 492 { 493 register struct nvlist *nv; 494 int atunit = i->i_atunit; 495 496 /* 497 * We determine whether or not a device has a parent in in one 498 * of two ways: 499 * (1) If a parent device was named in the config file, 500 * i.e. cases (2) and (3) in sem.c:adddev(), then 501 * we search its devbase for a matching unit number. 502 * (2) If the device was attach to an attribute, then we 503 * search all attributes the device can be attached to 504 * for parents (with appropriate unit numebrs) that 505 * may be able to attach the device. 506 */ 507 508 /* 509 * Case (1): A parent was named. Either it's configured, or not. 510 */ 511 if (i->i_atdev != NULL) 512 return (devbase_has_instances(i->i_atdev, atunit)); 513 514 /* 515 * Case (2): No parent was named. Look for devs that provide the attr. 516 */ 517 if (i->i_atattr != NULL) 518 for (nv = i->i_atattr->a_refs; nv != NULL; nv = nv->nv_next) 519 if (devbase_has_instances(nv->nv_ptr, atunit)) 520 return (1); 521 return (0); 522 } 523 524 static int 525 cfcrosscheck(cf, what, nv) 526 register struct config *cf; 527 const char *what; 528 register struct nvlist *nv; 529 { 530 register struct devbase *dev; 531 register 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 return (errs); 561 } 562 563 /* 564 * Cross-check the configuration: make sure that each target device 565 * or attribute (`at foo[0*?]') names at least one real device. Also 566 * see that the root, swap, and dump devices for all configurations 567 * are there. 568 */ 569 int 570 crosscheck() 571 { 572 register struct devi *i; 573 register struct config *cf; 574 int errs; 575 576 errs = 0; 577 for (i = alldevi; i != NULL; i = i->i_next) { 578 if (i->i_at == NULL || hasparent(i)) 579 continue; 580 xerror(conffile, i->i_lineno, 581 "%s at %s is orphaned", i->i_name, i->i_at); 582 (void)fprintf(stderr, " (%s %s declared)\n", 583 i->i_atunit == WILD ? "nothing matching" : "no", 584 i->i_at); 585 errs++; 586 } 587 if (allcf == NULL) { 588 (void)fprintf(stderr, "%s has no configurations!\n", 589 conffile); 590 errs++; 591 } 592 for (cf = allcf; cf != NULL; cf = cf->cf_next) { 593 if (cf->cf_root != NULL) { /* i.e., not swap generic */ 594 errs += cfcrosscheck(cf, "root", cf->cf_root); 595 errs += cfcrosscheck(cf, "swap", cf->cf_swap); 596 errs += cfcrosscheck(cf, "dumps", cf->cf_dump); 597 } 598 } 599 return (errs); 600 } 601 602 /* 603 * Check to see if there is a *'d unit with a needs-count file. 604 */ 605 int 606 badstar() 607 { 608 register struct devbase *d; 609 register struct deva *da; 610 register struct devi *i; 611 register int errs, n; 612 613 errs = 0; 614 for (d = allbases; d != NULL; d = d->d_next) { 615 for (da = d->d_ahead; da != NULL; da = da->d_bsame) 616 for (i = da->d_ihead; i != NULL; i = i->i_asame) { 617 if (i->i_unit == STAR) 618 goto foundstar; 619 } 620 continue; 621 foundstar: 622 if (ht_lookup(needcnttab, d->d_name)) { 623 (void)fprintf(stderr, 624 "config: %s's cannot be *'d until its driver is fixed\n", 625 d->d_name); 626 errs++; 627 continue; 628 } 629 for (n = 0; i != NULL; i = i->i_alias) 630 if (!i->i_collapsed) 631 n++; 632 if (n < 1) 633 panic("badstar() n<1"); 634 } 635 return (errs); 636 } 637 638 /* 639 * Verify/create builddir if necessary, change to it, and verify srcdir. 640 * This will be called when we see the first include. 641 */ 642 void 643 setupdirs() 644 { 645 struct stat st; 646 647 /* srcdir must be specified if builddir is not specified or if 648 * no configuration filename was specified. */ 649 if ((builddir || strcmp(defbuilddir, ".") == 0) && !srcdir) { 650 error("source directory must be specified"); 651 exit(1); 652 } 653 654 if (srcdir == NULL) 655 srcdir = "../../../.."; 656 if (builddir == NULL) 657 builddir = defbuilddir; 658 659 if (stat(builddir, &st) != 0) { 660 if (mkdir(builddir, 0777)) { 661 (void)fprintf(stderr, "config: cannot create %s: %s\n", 662 builddir, strerror(errno)); 663 exit(2); 664 } 665 madedir = 1; 666 } else if (!S_ISDIR(st.st_mode)) { 667 (void)fprintf(stderr, "config: %s is not a directory\n", 668 builddir); 669 exit(2); 670 } 671 if (chdir(builddir) != 0) { 672 (void)fprintf(stderr, "config: cannot change to %s\n", 673 builddir); 674 exit(2); 675 } 676 if (stat(srcdir, &st) != 0 || !S_ISDIR(st.st_mode)) { 677 (void)fprintf(stderr, "config: %s is not a directory\n", 678 srcdir); 679 exit(2); 680 } 681 } 682 683 struct opt { 684 const char *name; 685 const char *val; 686 }; 687 688 int 689 optcmp(sp1, sp2) 690 struct opt *sp1, *sp2; 691 { 692 int r; 693 694 r = strcmp(sp1->name, sp2->name); 695 if (r == 0) { 696 if (!sp1->val && !sp2->val) 697 r = 0; 698 else if (sp1->val && !sp2->val) 699 r = -1; 700 else if (sp2->val && !sp1->val) 701 r = 1; 702 else r = strcmp(sp1->val, sp2->val); 703 } 704 return (r); 705 } 706 707 void 708 optiondelta() 709 { 710 register struct nvlist *nv; 711 char nbuf[BUFSIZ], obuf[BUFSIZ]; /* XXX size */ 712 int nnewopts, ret = 0, i; 713 struct opt *newopts; 714 FILE *fp; 715 716 for (nnewopts = 0, nv = options; nv != NULL; nv = nv->nv_next) 717 nnewopts++; 718 newopts = (struct opt *)malloc(nnewopts * sizeof(struct opt)); 719 if (newopts == NULL) 720 ret = 0; 721 for (i = 0, nv = options; nv != NULL; nv = nv->nv_next, i++) { 722 newopts[i].name = nv->nv_name; 723 newopts[i].val = nv->nv_str; 724 } 725 qsort(newopts, nnewopts, sizeof (struct opt), optcmp); 726 727 /* compare options against previous config */ 728 if ((fp = fopen("options", "r"))) { 729 for (i = 0; !feof(fp) && i < nnewopts && ret == 0; i++) { 730 if (newopts[i].val) 731 snprintf(nbuf, sizeof nbuf, "%s=%s\n", 732 newopts[i].name, newopts[i].val); 733 else 734 snprintf(nbuf, sizeof nbuf, "%s\n", 735 newopts[i].name); 736 if (fgets(obuf, sizeof obuf, fp) == NULL || 737 strcmp(nbuf, obuf)) 738 ret = 1; 739 } 740 fclose(fp); 741 fp = NULL; 742 } else 743 ret = 1; 744 745 /* replace with the new list of options */ 746 if ((fp = fopen("options", "w+"))) { 747 rewind(fp); 748 for (i = 0; i < nnewopts; i++) { 749 if (newopts[i].val) 750 fprintf(fp, "%s=%s\n", newopts[i].name, 751 newopts[i].val); 752 else 753 fprintf(fp, "%s\n", newopts[i].name); 754 } 755 fclose(fp); 756 } 757 free(newopts); 758 if (ret == 0 || madedir == 1) 759 return; 760 (void)printf("Kernel options have changed -- you must run \"make clean\"\n"); 761 } 762