1 /* $OpenBSD: main.c,v 1.40 2008/10/03 13:01:26 deraadt 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 #ifndef lint 45 static char copyright[] = 46 "@(#) Copyright (c) 1992, 1993\n\ 47 The Regents of the University of California. All rights reserved.\n"; 48 #endif /* not lint */ 49 50 #include <sys/types.h> 51 #include <sys/stat.h> 52 #include <sys/param.h> 53 #include <ctype.h> 54 #include <errno.h> 55 #include <stdio.h> 56 #include <err.h> 57 #include <stdlib.h> 58 #include <string.h> 59 #include <unistd.h> 60 #include "config.h" 61 62 int firstfile(const char *); 63 int yyparse(void); 64 65 extern char *optarg; 66 extern int optind; 67 68 static struct hashtab *mkopttab; 69 static struct nvlist **nextopt; 70 static struct nvlist **nextdefopt; 71 static struct nvlist **nextmkopt; 72 73 static __dead void stop(void); 74 static int do_option(struct hashtab *, struct nvlist ***, 75 const char *, const char *, const char *); 76 static int crosscheck(void); 77 static int badstar(void); 78 static int mksymlinks(void); 79 static int hasparent(struct devi *); 80 static int cfcrosscheck(struct config *, const char *, struct nvlist *); 81 static void optiondelta(void); 82 83 int madedir = 0; 84 85 int verbose; 86 87 void 88 usage(void) 89 { 90 extern char *__progname; 91 92 fprintf(stderr, 93 "usage: %s [-p] [-b builddir] [-s srcdir] [config-file]\n" 94 " %s [-u] [-f | -o outfile] -e infile\n", 95 __progname, __progname); 96 97 exit(1); 98 } 99 100 int 101 main(int argc, char *argv[]) 102 { 103 char *p; 104 const char *last_component; 105 char *outfile = NULL; 106 int pflag, ch, eflag, uflag, fflag; 107 108 pflag = eflag = uflag = fflag = 0; 109 while ((ch = getopt(argc, argv, "egpfb:s:o:u")) != -1) { 110 switch (ch) { 111 112 case 'o': 113 outfile = optarg; 114 break; 115 case 'u': 116 uflag = 1; 117 break; 118 case 'f': 119 fflag = 1; 120 break; 121 122 case 'e': 123 eflag = 1; 124 if (!isatty(STDIN_FILENO)) 125 verbose = 1; 126 break; 127 128 case 'g': 129 /* 130 * In addition to DEBUG, you probably wanted to 131 * set "options KGDB" and maybe others. We could 132 * do that for you, but you really should just 133 * put them in the config file. 134 */ 135 (void)fputs( 136 "-g is obsolete (use makeoptions DEBUG=\"-g\")\n", 137 stderr); 138 usage(); 139 140 case 'p': 141 /* 142 * Essentially the same as makeoptions PROF="-pg", 143 * but also changes the path from ../../compile/FOO 144 * to ../../compile/FOO.PROF; i.e., compile a 145 * profiling kernel based on a typical "regular" 146 * kernel. 147 * 148 * Note that if you always want profiling, you 149 * can (and should) use a "makeoptions" line. 150 */ 151 pflag = 1; 152 break; 153 154 case 'b': 155 builddir = optarg; 156 break; 157 158 case 's': 159 srcdir = optarg; 160 break; 161 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 int len = strlen(last_component) + 17; 215 p = emalloc(len); 216 (void)snprintf(p, len, "../compile/%s.PROF", last_component); 217 (void)addmkoption(intern("PROF"), "-pg"); 218 (void)addoption(intern("GPROF"), NULL); 219 } else { 220 int len = strlen(last_component) + 12; 221 p = emalloc(len); 222 (void)snprintf(p, len, "../compile/%s", last_component); 223 } 224 defbuilddir = (argc == 0) ? "." : p; 225 226 /* 227 * Parse config file (including machine definitions). 228 */ 229 if (yyparse()) 230 stop(); 231 232 /* 233 * Fix (as in `set firmly in place') files. 234 */ 235 if (fixfiles()) 236 stop(); 237 238 /* 239 * Fix objects and libraries. 240 */ 241 if (fixobjects()) 242 stop(); 243 244 /* 245 * Perform cross-checking. 246 */ 247 if (maxusers == 0) { 248 if (defmaxusers) { 249 (void)printf("maxusers not specified; %d assumed\n", 250 defmaxusers); 251 maxusers = defmaxusers; 252 } else { 253 (void)fprintf(stderr, 254 "config: need \"maxusers\" line\n"); 255 errors++; 256 } 257 } 258 if (crosscheck() || errors) 259 stop(); 260 261 /* 262 * Squeeze things down and finish cross-checks (STAR checks must 263 * run after packing). 264 */ 265 pack(); 266 if (badstar()) 267 stop(); 268 269 /* 270 * Ready to go. Build all the various files. 271 */ 272 if (mksymlinks() || mkmakefile() || mkheaders() || mkswap() || 273 mkioconf()) 274 stop(); 275 (void)printf("Don't forget to run \"make depend\"\n"); 276 optiondelta(); 277 exit(0); 278 } 279 280 /* 281 * Make a symlink for "machine" so that "#include <machine/foo.h>" works, 282 * and for the machine's CPU architecture, so that works as well. 283 */ 284 static int 285 mksymlinks(void) 286 { 287 int ret; 288 char *p, buf[MAXPATHLEN]; 289 const char *q; 290 291 snprintf(buf, sizeof buf, "arch/%s/include", machine); 292 p = sourcepath(buf); 293 (void)unlink("machine"); 294 ret = symlink(p, "machine"); 295 if (ret) 296 (void)fprintf(stderr, "config: symlink(machine -> %s): %s\n", 297 p, strerror(errno)); 298 299 if (machinearch != NULL) { 300 snprintf(buf, sizeof buf, "arch/%s/include", machinearch); 301 p = sourcepath(buf); 302 q = machinearch; 303 } else { 304 p = strdup("machine"); 305 if (!p) 306 errx(1, "out of memory"); 307 q = machine; 308 } 309 (void)unlink(q); 310 ret = symlink(p, q); 311 if (ret) 312 (void)fprintf(stderr, "config: symlink(%s -> %s): %s\n", 313 q, p, strerror(errno)); 314 free(p); 315 316 return (ret); 317 } 318 319 static __dead void 320 stop(void) 321 { 322 (void)fprintf(stderr, "*** Stop.\n"); 323 exit(1); 324 } 325 326 /* 327 * Define a standard option, for which a header file will be generated. 328 */ 329 void 330 defoption(const char *name) 331 { 332 char *p, *low, c; 333 const char *n; 334 335 /* 336 * Convert to lower case. The header file name will be 337 * in lower case, so we store the lower case version in 338 * the hash table to detect option name collisions. The 339 * original string will be stored in the nvlist for use 340 * in the header file. 341 */ 342 low = emalloc(strlen(name) + 1); 343 for (n = name, p = low; (c = *n) != '\0'; n++) 344 *p++ = isupper(c) ? tolower(c) : c; 345 *p = 0; 346 347 n = intern(low); 348 free(low); 349 (void)do_option(defopttab, &nextdefopt, n, name, "defopt"); 350 351 /* 352 * Insert a verbatim copy of the option name, as well, 353 * to speed lookups when creating the Makefile. 354 */ 355 (void)ht_insert(defopttab, name, (void *)name); 356 } 357 358 /* 359 * Remove an option. 360 */ 361 void 362 removeoption(const char *name) 363 { 364 struct nvlist *nv, *nvt; 365 char *p, *low, c; 366 const char *n; 367 368 if ((nv = ht_lookup(opttab, name)) != NULL) { 369 if (options == nv) { 370 options = nv->nv_next; 371 nvfree(nv); 372 } else { 373 nvt = options; 374 while (nvt->nv_next != NULL) { 375 if (nvt->nv_next == nv) { 376 nvt->nv_next = nvt->nv_next->nv_next; 377 nvfree(nv); 378 break; 379 } else 380 nvt = nvt->nv_next; 381 } 382 } 383 } 384 385 (void)ht_remove(opttab, name); 386 387 low = emalloc(strlen(name) + 1); 388 /* make lowercase, then remove from select table */ 389 for (n = name, p = low; (c = *n) != '\0'; n++) 390 *p++ = isupper(c) ? tolower(c) : c; 391 *p = 0; 392 n = intern(low); 393 free(low); 394 (void)ht_remove(selecttab, n); 395 } 396 397 /* 398 * Add an option from "options FOO". Note that this selects things that 399 * are "optional foo". 400 */ 401 void 402 addoption(const char *name, const char *value) 403 { 404 char *p, *low, c; 405 const char *n; 406 407 if (do_option(opttab, &nextopt, name, value, "options")) 408 return; 409 410 low = emalloc(strlen(name) + 1); 411 /* make lowercase, then add to select table */ 412 for (n = name, p = low; (c = *n) != '\0'; n++) 413 *p++ = isupper(c) ? tolower(c) : c; 414 *p = 0; 415 n = intern(low); 416 free(low); 417 (void)ht_insert(selecttab, n, (void *)n); 418 } 419 420 /* 421 * Add a "make" option. 422 */ 423 void 424 addmkoption(const char *name, const char *value) 425 { 426 427 (void)do_option(mkopttab, &nextmkopt, name, value, "mkoptions"); 428 } 429 430 /* 431 * Add a name=value pair to an option list. The value may be NULL. 432 */ 433 static int 434 do_option(struct hashtab *ht, struct nvlist ***nppp, const char *name, 435 const char *value, const char *type) 436 { 437 struct nvlist *nv; 438 439 /* assume it will work */ 440 nv = newnv(name, value, NULL, 0, NULL); 441 if (ht_insert(ht, name, nv) == 0) { 442 **nppp = nv; 443 *nppp = &nv->nv_next; 444 return (0); 445 } 446 447 /* oops, already got that option */ 448 nvfree(nv); 449 if ((nv = ht_lookup(ht, name)) == NULL) 450 panic("do_option"); 451 if (nv->nv_str != NULL) 452 error("already have %s `%s=%s'", type, name, nv->nv_str); 453 else 454 error("already have %s `%s'", type, name); 455 return (1); 456 } 457 458 /* 459 * Return true if there is at least one instance of the given unit 460 * on the given device attachment (or any units, if unit == WILD). 461 */ 462 int 463 deva_has_instances(struct deva *deva, int unit) 464 { 465 struct devi *i; 466 467 if (unit == WILD) 468 return (deva->d_ihead != NULL); 469 for (i = deva->d_ihead; i != NULL; i = i->i_asame) 470 if (unit == i->i_unit) 471 return (1); 472 return (0); 473 } 474 475 /* 476 * Return true if there is at least one instance of the given unit 477 * on the given base (or any units, if unit == WILD). 478 */ 479 int 480 devbase_has_instances(struct devbase *dev, int unit) 481 { 482 struct deva *da; 483 484 for (da = dev->d_ahead; da != NULL; da = da->d_bsame) 485 if (deva_has_instances(da, unit)) 486 return (1); 487 return (0); 488 } 489 490 static int 491 hasparent(struct devi *i) 492 { 493 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 numbers) 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(struct config *cf, const char *what, struct nvlist *nv) 526 { 527 struct devbase *dev; 528 struct devi *pd; 529 int errs, devminor; 530 531 if (maxpartitions <= 0) 532 panic("cfcrosscheck"); 533 534 for (errs = 0; nv != NULL; nv = nv->nv_next) { 535 if (nv->nv_name == NULL) 536 continue; 537 dev = ht_lookup(devbasetab, nv->nv_name); 538 if (dev == NULL) 539 panic("cfcrosscheck(%s)", nv->nv_name); 540 devminor = minor(nv->nv_int) / maxpartitions; 541 if (devbase_has_instances(dev, devminor)) 542 continue; 543 if (devbase_has_instances(dev, STAR) && 544 devminor >= dev->d_umax) 545 continue; 546 for (pd = allpseudo; pd != NULL; pd = pd->i_next) 547 if (pd->i_base == dev && devminor < dev->d_umax && 548 devminor >= 0) 549 goto loop; 550 (void)fprintf(stderr, 551 "%s:%d: %s says %s on %s, but there's no %s\n", 552 conffile, cf->cf_lineno, 553 cf->cf_name, what, nv->nv_str, nv->nv_str); 554 errs++; 555 loop: 556 ; 557 } 558 return (errs); 559 } 560 561 /* 562 * Cross-check the configuration: make sure that each target device 563 * or attribute (`at foo[0*?]') names at least one real device. Also 564 * see that the root, swap, and dump devices for all configurations 565 * are there. 566 */ 567 int 568 crosscheck(void) 569 { 570 struct devi *i; 571 struct config *cf; 572 int errs; 573 574 errs = 0; 575 for (i = alldevi; i != NULL; i = i->i_next) { 576 if (i->i_at == NULL || hasparent(i)) 577 continue; 578 xerror(conffile, i->i_lineno, 579 "%s at %s is orphaned", i->i_name, i->i_at); 580 (void)fprintf(stderr, " (%s %s declared)\n", 581 i->i_atunit == WILD ? "nothing matching" : "no", 582 i->i_at); 583 errs++; 584 } 585 if (allcf == NULL) { 586 (void)fprintf(stderr, "%s has no configurations!\n", 587 conffile); 588 errs++; 589 } 590 for (cf = allcf; cf != NULL; cf = cf->cf_next) { 591 if (cf->cf_root != NULL) { /* i.e., not swap generic */ 592 errs += cfcrosscheck(cf, "root", cf->cf_root); 593 errs += cfcrosscheck(cf, "swap", cf->cf_swap); 594 errs += cfcrosscheck(cf, "dumps", cf->cf_dump); 595 } 596 } 597 return (errs); 598 } 599 600 /* 601 * Check to see if there is a *'d unit with a needs-count file. 602 */ 603 int 604 badstar(void) 605 { 606 struct devbase *d; 607 struct deva *da; 608 struct devi *i; 609 int errs, n; 610 611 errs = 0; 612 for (d = allbases; d != NULL; d = d->d_next) { 613 for (da = d->d_ahead; da != NULL; da = da->d_bsame) 614 for (i = da->d_ihead; i != NULL; i = i->i_asame) { 615 if (i->i_unit == STAR) 616 goto foundstar; 617 } 618 continue; 619 foundstar: 620 if (ht_lookup(needcnttab, d->d_name)) { 621 (void)fprintf(stderr, 622 "config: %s's cannot be *'d until its driver is fixed\n", 623 d->d_name); 624 errs++; 625 continue; 626 } 627 for (n = 0; i != NULL; i = i->i_alias) 628 if (!i->i_collapsed) 629 n++; 630 if (n < 1) 631 panic("badstar() n<1"); 632 } 633 return (errs); 634 } 635 636 /* 637 * Verify/create builddir if necessary, change to it, and verify srcdir. 638 * This will be called when we see the first include. 639 */ 640 void 641 setupdirs(void) 642 { 643 struct stat st; 644 645 /* srcdir must be specified if builddir is not specified or if 646 * no configuration filename was specified. */ 647 if ((builddir || strcmp(defbuilddir, ".") == 0) && !srcdir) { 648 error("source directory must be specified"); 649 exit(1); 650 } 651 652 if (srcdir == NULL) 653 srcdir = "../../../.."; 654 if (builddir == NULL) 655 builddir = defbuilddir; 656 657 if (stat(builddir, &st) != 0) { 658 if (mkdir(builddir, 0777)) { 659 (void)fprintf(stderr, "config: cannot create %s: %s\n", 660 builddir, strerror(errno)); 661 exit(2); 662 } 663 madedir = 1; 664 } else if (!S_ISDIR(st.st_mode)) { 665 (void)fprintf(stderr, "config: %s is not a directory\n", 666 builddir); 667 exit(2); 668 } 669 if (chdir(builddir) != 0) { 670 (void)fprintf(stderr, "config: cannot change to %s\n", 671 builddir); 672 exit(2); 673 } 674 if (stat(srcdir, &st) != 0 || !S_ISDIR(st.st_mode)) { 675 (void)fprintf(stderr, "config: %s is not a directory\n", 676 srcdir); 677 exit(2); 678 } 679 } 680 681 struct opt { 682 const char *name; 683 const char *val; 684 }; 685 686 int 687 optcmp(const void *v1, const void *v2) 688 { 689 const struct opt *sp1 = v1, *sp2 = v2; 690 int r; 691 692 r = strcmp(sp1->name, sp2->name); 693 if (r == 0) { 694 if (!sp1->val && !sp2->val) 695 r = 0; 696 else if (sp1->val && !sp2->val) 697 r = -1; 698 else if (sp2->val && !sp1->val) 699 r = 1; 700 else r = strcmp(sp1->val, sp2->val); 701 } 702 return (r); 703 } 704 705 void 706 optiondelta(void) 707 { 708 struct nvlist *nv; 709 char nbuf[BUFSIZ], obuf[BUFSIZ]; /* XXX size */ 710 int nnewopts, ret = 0, i; 711 struct opt *newopts; 712 FILE *fp; 713 714 for (nnewopts = 0, nv = options; nv != NULL; nv = nv->nv_next) 715 nnewopts++; 716 newopts = (struct opt *)emalloc(nnewopts * sizeof(struct opt)); 717 if (newopts == NULL) 718 ret = 0; 719 for (i = 0, nv = options; nv != NULL; nv = nv->nv_next, i++) { 720 newopts[i].name = nv->nv_name; 721 newopts[i].val = nv->nv_str; 722 } 723 qsort(newopts, nnewopts, sizeof (struct opt), optcmp); 724 725 /* compare options against previous config */ 726 if ((fp = fopen("options", "r"))) { 727 for (i = 0; !feof(fp) && i < nnewopts && ret == 0; i++) { 728 if (newopts[i].val) 729 snprintf(nbuf, sizeof nbuf, "%s=%s\n", 730 newopts[i].name, newopts[i].val); 731 else 732 snprintf(nbuf, sizeof nbuf, "%s\n", 733 newopts[i].name); 734 if (fgets(obuf, sizeof obuf, fp) == NULL || 735 strcmp(nbuf, obuf)) 736 ret = 1; 737 } 738 fclose(fp); 739 fp = NULL; 740 } else 741 ret = 1; 742 743 /* replace with the new list of options */ 744 if ((fp = fopen("options", "w+"))) { 745 rewind(fp); 746 for (i = 0; i < nnewopts; i++) { 747 if (newopts[i].val) 748 fprintf(fp, "%s=%s\n", newopts[i].name, 749 newopts[i].val); 750 else 751 fprintf(fp, "%s\n", newopts[i].name); 752 } 753 fclose(fp); 754 } 755 free(newopts); 756 if (ret == 0 || madedir == 1) 757 return; 758 (void)printf("Kernel options have changed -- you must run \"make clean\"\n"); 759 } 760