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