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