main.c revision 1.83 1 /* $NetBSD: main.c,v 1.83 2015/09/01 14:32:20 uebayasi Exp $ */
2
3 /*
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This software was developed by the Computer Systems Engineering group
8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 * contributed to Berkeley.
10 *
11 * All advertising materials mentioning features or use of this software
12 * must display the following acknowledgement:
13 * This product includes software developed by the University of
14 * California, Lawrence Berkeley Laboratories.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * from: @(#)main.c 8.1 (Berkeley) 6/6/93
41 */
42
43 #if HAVE_NBTOOL_CONFIG_H
44 #include "nbtool_config.h"
45 #endif
46
47 #include <sys/cdefs.h>
48 __RCSID("$NetBSD: main.c,v 1.83 2015/09/01 14:32:20 uebayasi Exp $");
49
50 #ifndef MAKE_BOOTSTRAP
51 #include <sys/cdefs.h>
52 #define COPYRIGHT(x) __COPYRIGHT(x)
53 #else
54 #define COPYRIGHT(x) static const char copyright[] = x
55 #endif
56
57 #ifndef lint
58 COPYRIGHT("@(#) Copyright (c) 1992, 1993\
59 The Regents of the University of California. All rights reserved.");
60 #endif /* not lint */
61
62 #include <sys/types.h>
63 #include <sys/stat.h>
64 #include <sys/param.h>
65 #include <sys/mman.h>
66 #if !HAVE_NBTOOL_CONFIG_H
67 #include <sys/sysctl.h>
68 #endif
69 #include <paths.h>
70 #include <ctype.h>
71 #include <err.h>
72 #include <errno.h>
73 #include <fcntl.h>
74 #include <limits.h>
75 #include <stdio.h>
76 #include <stdlib.h>
77 #include <string.h>
78 #include <unistd.h>
79 #include <vis.h>
80 #include <util.h>
81
82 #include "defs.h"
83 #include "sem.h"
84
85 #ifndef LINE_MAX
86 #define LINE_MAX 1024
87 #endif
88
89 int vflag; /* verbose output */
90 int Pflag; /* pack locators */
91 int Lflag; /* lint config generation */
92 int Mflag; /* modular build */
93 int handling_cmdlineopts; /* currently processing -D/-U options */
94
95 int yyparse(void);
96
97 #ifndef MAKE_BOOTSTRAP
98 extern int yydebug;
99 #endif
100 int dflag;
101
102 static struct dlhash *obsopttab;
103 static struct hashtab *mkopttab;
104 static struct nvlist **nextopt;
105 static struct nvlist **nextmkopt;
106 static struct nvlist **nextappmkopt;
107 static struct nvlist **nextcndmkopt;
108 static struct nvlist **nextfsopt;
109 static struct nvlist *cmdlinedefs, *cmdlineundefs;
110
111 static void usage(void) __dead;
112 static void dependopts(void);
113 static void dependopts_one(const char *);
114 static void do_depends(struct nvlist *);
115 static void do_depend(struct nvlist *);
116 static void stop(void);
117 static int do_option(struct hashtab *, struct nvlist **,
118 struct nvlist ***, const char *, const char *,
119 const char *, struct hashtab *);
120 static int undo_option(struct hashtab *, struct nvlist **,
121 struct nvlist ***, const char *, const char *);
122 static int crosscheck(void);
123 static int badstar(void);
124 int main(int, char **);
125 static int mksubdirs(void);
126 static int mksymlinks(void);
127 static int mkident(void);
128 static int devbase_has_dead_instances(const char *, void *, void *);
129 static int devbase_has_any_instance(struct devbase *, int, int, int);
130 static int check_dead_devi(const char *, void *, void *);
131 static void add_makeopt(const char *);
132 static void remove_makeopt(const char *);
133 static void handle_cmdline_makeoptions(void);
134 static void kill_orphans(void);
135 static void do_kill_orphans(struct devbase *, struct attr *,
136 struct devbase *, int);
137 static int kill_orphans_cb(const char *, void *, void *);
138 static int cfcrosscheck(struct config *, const char *, struct nvlist *);
139 static void defopt(struct dlhash *ht, const char *fname,
140 struct defoptlist *opts, struct nvlist *deps, int obs);
141 static struct defoptlist *find_declared_option_option(const char *name);
142 static struct nvlist *find_declared_fs_option(const char *name);
143
144 #define LOGCONFIG_LARGE "INCLUDE_CONFIG_FILE"
145 #define LOGCONFIG_SMALL "INCLUDE_JUST_CONFIG"
146
147 static void logconfig_start(void);
148 static void logconfig_end(void);
149 static FILE *cfg;
150 static time_t cfgtime;
151
152 static int is_elf(const char *);
153 static int extract_config(const char *, const char *, int);
154
155 int badfilename(const char *fname);
156
157 const char *progname;
158 extern const char *yyfile;
159
160 int
161 main(int argc, char **argv)
162 {
163 char *p, cname[PATH_MAX];
164 const char *last_component;
165 int pflag, xflag, ch, removeit;
166
167 setprogname(argv[0]);
168
169 pflag = 0;
170 xflag = 0;
171 while ((ch = getopt(argc, argv, "D:LMPU:dgpvb:s:x")) != -1) {
172 switch (ch) {
173
174 case 'd':
175 #ifndef MAKE_BOOTSTRAP
176 yydebug = 1;
177 #endif
178 dflag++;
179 break;
180
181 case 'M':
182 Mflag = 1;
183 break;
184
185 case 'L':
186 Lflag = 1;
187 break;
188
189 case 'P':
190 Pflag = 1;
191 break;
192
193 case 'g':
194 /*
195 * In addition to DEBUG, you probably wanted to
196 * set "options KGDB" and maybe others. We could
197 * do that for you, but you really should just
198 * put them in the config file.
199 */
200 warnx("-g is obsolete (use -D DEBUG=\"-g\")");
201 usage();
202 /*NOTREACHED*/
203
204 case 'p':
205 /*
206 * Essentially the same as makeoptions PROF="-pg",
207 * but also changes the path from ../../compile/FOO
208 * to ../../compile/FOO.PROF; i.e., compile a
209 * profiling kernel based on a typical "regular"
210 * kernel.
211 *
212 * Note that if you always want profiling, you
213 * can (and should) use a "makeoptions" line.
214 */
215 pflag = 1;
216 break;
217
218 case 'v':
219 vflag = 1;
220 break;
221
222 case 'b':
223 builddir = optarg;
224 break;
225
226 case 's':
227 srcdir = optarg;
228 break;
229
230 case 'x':
231 xflag = 1;
232 break;
233
234 case 'D':
235 add_makeopt(optarg);
236 break;
237
238 case 'U':
239 remove_makeopt(optarg);
240 break;
241
242 case '?':
243 default:
244 usage();
245 }
246 }
247
248 if (xflag && optind != 2) {
249 errx(EXIT_FAILURE, "-x must be used alone");
250 }
251
252 argc -= optind;
253 argv += optind;
254 if (argc > 1) {
255 usage();
256 }
257
258 if (Lflag && (builddir != NULL || Pflag || pflag))
259 errx(EXIT_FAILURE, "-L can only be used with -s and -v");
260
261 if (xflag) {
262 if (argc == 0) {
263 #if !HAVE_NBTOOL_CONFIG_H
264 char path_unix[MAXPATHLEN];
265 size_t len = sizeof(path_unix) - 1;
266 path_unix[0] = '/';
267
268 conffile = sysctlbyname("machdep.booted_kernel",
269 &path_unix[1], &len, NULL, 0) == -1 ? _PATH_UNIX :
270 path_unix;
271 #else
272 errx(EXIT_FAILURE, "no kernel supplied");
273 #endif
274 } else
275 conffile = argv[0];
276 if (!is_elf(conffile))
277 errx(EXIT_FAILURE, "%s: not a binary kernel",
278 conffile);
279 if (!extract_config(conffile, "stdout", STDOUT_FILENO))
280 errx(EXIT_FAILURE, "%s does not contain embedded "
281 "configuration data", conffile);
282 exit(0);
283 }
284
285 conffile = (argc == 1) ? argv[0] : "CONFIG";
286 if (firstfile(conffile)) {
287 err(EXIT_FAILURE, "Cannot read `%s'", conffile);
288 exit(2);
289 }
290
291 /*
292 * Init variables.
293 */
294 minmaxusers = 1;
295 maxmaxusers = 10000;
296 initintern();
297 ident = NULL;
298 devbasetab = ht_new();
299 devroottab = ht_new();
300 devatab = ht_new();
301 devitab = ht_new();
302 deaddevitab = ht_new();
303 selecttab = ht_new();
304 needcnttab = ht_new();
305 opttab = ht_new();
306 mkopttab = ht_new();
307 fsopttab = ht_new();
308 deffstab = nvhash_create();
309 defopttab = dlhash_create();
310 defparamtab = dlhash_create();
311 defoptlint = dlhash_create();
312 defflagtab = dlhash_create();
313 optfiletab = dlhash_create();
314 obsopttab = dlhash_create();
315 bdevmtab = ht_new();
316 maxbdevm = 0;
317 cdevmtab = ht_new();
318 maxcdevm = 0;
319 nextopt = &options;
320 nextmkopt = &mkoptions;
321 nextappmkopt = &appmkoptions;
322 nextcndmkopt = &condmkoptions;
323 nextfsopt = &fsoptions;
324 initfiles();
325 initsem();
326
327 /*
328 * Handle profiling (must do this before we try to create any
329 * files).
330 */
331 last_component = strrchr(conffile, '/');
332 last_component = (last_component) ? last_component + 1 : conffile;
333 if (pflag) {
334 p = emalloc(strlen(last_component) + 17);
335 (void)sprintf(p, "../compile/%s.PROF", last_component);
336 (void)addmkoption(intern("PROF"), "-pg");
337 (void)addoption(intern("GPROF"), NULL);
338 } else {
339 p = emalloc(strlen(last_component) + 13);
340 (void)sprintf(p, "../compile/%s", last_component);
341 }
342 defbuilddir = (argc == 0) ? "." : p;
343
344 if (Lflag) {
345 char resolvedname[MAXPATHLEN];
346
347 if (realpath(conffile, resolvedname) == NULL)
348 err(EXIT_FAILURE, "realpath(%s)", conffile);
349
350 if (yyparse())
351 stop();
352
353 printf("include \"%s\"\n", resolvedname);
354
355 emit_params();
356 emit_options();
357 emit_instances();
358
359 exit(EXIT_SUCCESS);
360 }
361
362 removeit = 0;
363 if (is_elf(conffile)) {
364 const char *tmpdir;
365 int cfd;
366
367 if (builddir == NULL)
368 errx(EXIT_FAILURE, "Build directory must be specified "
369 "with binary kernels");
370
371 /* Open temporary configuration file */
372 tmpdir = getenv("TMPDIR");
373 if (tmpdir == NULL)
374 tmpdir = _PATH_TMP;
375 snprintf(cname, sizeof(cname), "%s/config.tmp.XXXXXX", tmpdir);
376 cfd = mkstemp(cname);
377 if (cfd == -1)
378 err(EXIT_FAILURE, "Cannot create `%s'", cname);
379
380 printf("Using configuration data embedded in kernel...\n");
381 if (!extract_config(conffile, cname, cfd)) {
382 unlink(cname);
383 errx(EXIT_FAILURE, "%s does not contain embedded "
384 "configuration data", conffile);
385 }
386
387 removeit = 1;
388 close(cfd);
389 firstfile(cname);
390 }
391
392 /*
393 * Log config file. We don't know until yyparse() if we're
394 * going to need config_file.h (i.e. if we're doing ioconf-only
395 * or not). Just start creating the file, and when we know
396 * later, we'll just keep or discard our work here.
397 */
398 logconfig_start();
399
400 /*
401 * Parse config file (including machine definitions).
402 */
403 if (yyparse())
404 stop();
405
406 if (ioconfname && cfg)
407 fclose(cfg);
408 else
409 logconfig_end();
410
411 if (removeit)
412 unlink(cname);
413
414 /*
415 * Handle command line overrides
416 */
417 yyfile = "handle_cmdline_makeoptions";
418 handle_cmdline_makeoptions();
419
420 /*
421 * Detect and properly ignore orphaned devices
422 */
423 yyfile = "kill_orphans";
424 kill_orphans();
425
426 /*
427 * Select devices and pseudo devices and their attributes
428 */
429 yyfile = "fixdevis";
430 if (fixdevis())
431 stop();
432
433 /*
434 * Copy maxusers to param.
435 */
436 yyfile = "fixmaxusers";
437 fixmaxusers();
438
439 /*
440 * Copy makeoptions to params
441 */
442 yyfile = "fixmkoption";
443 fixmkoption();
444
445 /*
446 * If working on an ioconf-only config, process here and exit
447 */
448 if (ioconfname) {
449 yyfile = "pack";
450 pack();
451 yyfile = "mkioconf";
452 mkioconf();
453 yyfile = "emitlocs";
454 emitlocs();
455 yyfile = "emitioconfh";
456 emitioconfh();
457 return 0;
458 }
459
460 yyfile = "dependattrs";
461 dependattrs();
462
463 /*
464 * Deal with option dependencies.
465 */
466 yyfile = "dependopts";
467 dependopts();
468
469 /*
470 * Fix (as in `set firmly in place') files.
471 */
472 yyfile = "fixfiles";
473 if (fixfiles())
474 stop();
475
476 /*
477 * Fix objects and libraries.
478 */
479 yyfile = "fixobjects";
480 if (fixobjects())
481 stop();
482
483 /*
484 * Fix device-majors.
485 */
486 yyfile = "fixdevsw";
487 if (fixdevsw())
488 stop();
489
490 /*
491 * Perform cross-checking.
492 */
493 if (maxusers == 0) {
494 if (defmaxusers) {
495 (void)printf("maxusers not specified; %d assumed\n",
496 defmaxusers);
497 maxusers = defmaxusers;
498 } else {
499 warnx("need \"maxusers\" line");
500 errors++;
501 }
502 }
503 if (crosscheck() || errors)
504 stop();
505
506 /*
507 * Squeeze things down and finish cross-checks (STAR checks must
508 * run after packing).
509 */
510 yyfile = "pack";
511 pack();
512 yyfile = "badstar";
513 if (badstar())
514 stop();
515
516 yyfile = NULL;
517 /*
518 * Ready to go. Build all the various files.
519 */
520 if (mksubdirs() || mksymlinks() || mkmakefile() || mkheaders() || mkswap() ||
521 mkioconf() || (do_devsw ? mkdevsw() : 0) || mkident() || errors)
522 stop();
523 (void)printf("Build directory is %s\n", builddir);
524 (void)printf("Don't forget to run \"make depend\"\n");
525 return 0;
526 }
527
528 static void
529 usage(void)
530 {
531 (void)fprintf(stderr, "Usage: %s [-Ppv] [-b builddir] [-D var=value] "
532 "[-s srcdir] [-U var] "
533 "[config-file]\n\t%s -x [kernel-file]\n"
534 "\t%s -L [-v] [-s srcdir] [config-file]\n",
535 getprogname(), getprogname(), getprogname());
536 exit(1);
537 }
538
539 /*
540 * Set any options that are implied by other options.
541 */
542 static void
543 dependopts(void)
544 {
545 struct nvlist *nv;
546
547 for (nv = options; nv != NULL; nv = nv->nv_next) {
548 dependopts_one(nv->nv_name);
549 }
550
551 for (nv = fsoptions; nv != NULL; nv = nv->nv_next) {
552 dependopts_one(nv->nv_name);
553 }
554 }
555
556 static void
557 dependopts_one(const char *name)
558 {
559 struct defoptlist *dl;
560 struct nvlist *fs;
561
562 dl = find_declared_option_option(name);
563 if (dl != NULL) {
564 do_depends(dl->dl_depends);
565 }
566 fs = find_declared_fs_option(name);
567 if (fs != NULL) {
568 do_depends(fs->nv_ptr);
569 }
570
571 CFGDBG(3, "depend `%s' searched", name);
572 }
573
574 static void
575 do_depends(struct nvlist *nv)
576 {
577 struct nvlist *opt;
578
579 for (opt = nv; opt != NULL; opt = opt->nv_next) {
580 do_depend(opt);
581 }
582 }
583
584 static void
585 do_depend(struct nvlist *nv)
586 {
587 struct attr *a;
588
589 if (nv != NULL && (nv->nv_flags & NV_DEPENDED) == 0) {
590 nv->nv_flags |= NV_DEPENDED;
591 /*
592 * If the dependency is an attribute, then just add
593 * it to the selecttab.
594 */
595 CFGDBG(3, "depend attr `%s'", nv->nv_name);
596 if ((a = ht_lookup(attrtab, nv->nv_name)) != NULL) {
597 if (a->a_iattr)
598 panic("do_depend(%s): dep `%s' is an iattr",
599 nv->nv_name, a->a_name);
600 expandattr(a, selectattr);
601 } else {
602 if (ht_lookup(opttab, nv->nv_name) == NULL)
603 addoption(nv->nv_name, NULL);
604 dependopts_one(nv->nv_name);
605 }
606 }
607 }
608
609 static int
610 recreate(const char *p, const char *q)
611 {
612 int ret;
613
614 if ((ret = unlink(q)) == -1 && errno != ENOENT)
615 warn("unlink(%s)", q);
616 if ((ret = symlink(p, q)) == -1)
617 warn("symlink(%s -> %s)", q, p);
618 return ret;
619 }
620
621 static void
622 mksubdir(char *buf)
623 {
624 char *p;
625 struct stat st;
626
627 p = strrchr(buf, '/');
628 if (p != NULL && *p == '/') {
629 *p = '\0';
630 mksubdir(buf);
631 *p = '/';
632 }
633 if (stat(buf, &st) == 0) {
634 if (!S_ISDIR(st.st_mode))
635 errx(EXIT_FAILURE, "not directory %s", buf);
636 } else
637 if (mkdir(buf, 0777) == -1)
638 errx(EXIT_FAILURE, "cannot create %s", buf);
639 }
640
641 static int
642 mksubdirs(void)
643 {
644 struct files *fi;
645 const char *prefix, *sep;
646 char buf[MAXPATHLEN];
647
648 // XXX notyet
649 if (1)
650 return 0;
651
652 TAILQ_FOREACH(fi, &allfiles, fi_next) {
653 if ((fi->fi_flags & FI_SEL) == 0)
654 continue;
655 prefix = sep = "";
656 if (fi->fi_buildprefix != NULL) {
657 prefix = fi->fi_buildprefix;
658 sep = "/";
659 } else {
660 if (fi->fi_prefix != NULL) {
661 prefix = fi->fi_prefix;
662 sep = "/";
663 }
664 }
665 snprintf(buf, sizeof(buf), "%s%s%s", prefix, sep, fi->fi_dir);
666 if (buf[0] == '\0')
667 continue;
668 mksubdir(buf);
669 }
670
671 return 0;
672 }
673
674 /*
675 * Make a symlink for "machine" so that "#include <machine/foo.h>" works,
676 * and for the machine's CPU architecture, so that works as well.
677 */
678 static int
679 mksymlinks(void)
680 {
681 int ret;
682 char *p, buf[MAXPATHLEN];
683 const char *q;
684 struct nvlist *nv;
685
686 p = buf;
687
688 snprintf(buf, sizeof(buf), "%s/arch/%s/include", srcdir, machine);
689 ret = recreate(p, "machine");
690 ret = recreate(p, machine);
691
692 if (machinearch != NULL) {
693 snprintf(buf, sizeof(buf), "%s/arch/%s/include", srcdir, machinearch);
694 q = machinearch;
695 } else {
696 snprintf(buf, sizeof(buf), "machine");
697 q = machine;
698 }
699
700 ret = recreate(p, q);
701
702 for (nv = machinesubarches; nv != NULL; nv = nv->nv_next) {
703 q = nv->nv_name;
704 snprintf(buf, sizeof(buf), "%s/arch/%s/include", srcdir, q);
705 ret = recreate(p, q);
706 }
707
708 return (ret);
709 }
710
711 static __dead void
712 stop(void)
713 {
714 (void)fprintf(stderr, "*** Stop.\n");
715 exit(1);
716 }
717
718 static void
719 check_dependencies(const char *thing, struct nvlist *deps)
720 {
721 struct nvlist *dep;
722 struct attr *a;
723
724 for (dep = deps; dep != NULL; dep = dep->nv_next) {
725 /*
726 * If the dependency is an attribute, it must not
727 * be an interface attribute. Otherwise, it must
728 * be a previously declared option.
729 */
730 if ((a = ht_lookup(attrtab, dep->nv_name)) != NULL) {
731 if (a->a_iattr)
732 cfgerror("option `%s' dependency `%s' "
733 "is an interface attribute",
734 thing, a->a_name);
735 } else if (OPT_OBSOLETE(dep->nv_name)) {
736 cfgerror("option `%s' dependency `%s' "
737 "is obsolete", thing, dep->nv_name);
738 } else if (!is_declared_option(dep->nv_name)) {
739 cfgerror("option `%s' dependency `%s' "
740 "is an unknown option",
741 thing, dep->nv_name);
742 }
743 }
744 }
745
746 static void
747 add_fs_dependencies(struct nvlist *nv, struct nvlist *deps)
748 {
749 /* Use nv_ptr to link any other options that are implied. */
750 nv->nv_ptr = deps;
751 check_dependencies(nv->nv_name, deps);
752 }
753
754 static void
755 add_opt_dependencies(struct defoptlist *dl, struct nvlist *deps)
756 {
757 dl->dl_depends = deps;
758 check_dependencies(dl->dl_name, deps);
759 }
760
761 /*
762 * Define one or more file systems.
763 */
764 void
765 deffilesystem(struct nvlist *fses, struct nvlist *deps)
766 {
767 struct nvlist *nv;
768
769 /*
770 * Mark these options as ones to skip when creating the Makefile.
771 */
772 for (nv = fses; nv != NULL; nv = nv->nv_next) {
773 if (DEFINED_OPTION(nv->nv_name)) {
774 cfgerror("file system or option `%s' already defined",
775 nv->nv_name);
776 return;
777 }
778
779 /*
780 * Also mark it as a valid file system, which may be
781 * used in "file-system" directives in the config
782 * file.
783 */
784 if (nvhash_insert(deffstab, nv->nv_name, nv))
785 panic("file system `%s' already in table?!",
786 nv->nv_name);
787
788 add_fs_dependencies(nv, deps);
789
790 /*
791 * Implicit attribute definition for filesystem.
792 */
793 const char *n;
794 n = strtolower(nv->nv_name);
795 refattr(n);
796 }
797 }
798
799 /*
800 * Sanity check a file name.
801 */
802 int
803 badfilename(const char *fname)
804 {
805 const char *n;
806
807 /*
808 * We're putting multiple options into one file. Sanity
809 * check the file name.
810 */
811 if (strchr(fname, '/') != NULL) {
812 cfgerror("option file name contains a `/'");
813 return 1;
814 }
815 if ((n = strrchr(fname, '.')) == NULL || strcmp(n, ".h") != 0) {
816 cfgerror("option file name does not end in `.h'");
817 return 1;
818 }
819 return 0;
820 }
821
822
823 /*
824 * Search for a defined option (defopt, filesystem, etc), and if found,
825 * return the option's struct nvlist.
826 *
827 * This used to be one function (find_declared_option) before options
828 * and filesystems became different types.
829 */
830 static struct defoptlist *
831 find_declared_option_option(const char *name)
832 {
833 struct defoptlist *option;
834
835 if ((option = dlhash_lookup(defopttab, name)) != NULL ||
836 (option = dlhash_lookup(defparamtab, name)) != NULL ||
837 (option = dlhash_lookup(defflagtab, name)) != NULL) {
838 return (option);
839 }
840
841 return (NULL);
842 }
843
844 static struct nvlist *
845 find_declared_fs_option(const char *name)
846 {
847 struct nvlist *fs;
848
849 if ((fs = nvhash_lookup(deffstab, name)) != NULL) {
850 return fs;
851 }
852
853 return (NULL);
854 }
855
856 /*
857 * Like find_declared_option but doesn't return what it finds, so it
858 * can search both the various kinds of options and also filesystems.
859 */
860 int
861 is_declared_option(const char *name)
862 {
863 struct defoptlist *option = NULL;
864 struct nvlist *fs;
865
866 if ((option = dlhash_lookup(defopttab, name)) != NULL ||
867 (option = dlhash_lookup(defparamtab, name)) != NULL ||
868 (option = dlhash_lookup(defflagtab, name)) != NULL) {
869 return 1;
870 }
871 if ((fs = nvhash_lookup(deffstab, name)) != NULL) {
872 return 1;
873 }
874
875 return 0;
876 }
877
878 /*
879 * Define one or more standard options. If an option file name is specified,
880 * place all options in one file with the specified name. Otherwise, create
881 * an option file for each option.
882 * record the option information in the specified table.
883 */
884 void
885 defopt(struct dlhash *ht, const char *fname, struct defoptlist *opts,
886 struct nvlist *deps, int obs)
887 {
888 struct defoptlist *dl, *nextdl, *olddl;
889 const char *name;
890 char buf[500];
891
892 if (fname != NULL && badfilename(fname)) {
893 return;
894 }
895
896 /*
897 * Mark these options as ones to skip when creating the Makefile.
898 */
899 for (dl = opts; dl != NULL; dl = nextdl) {
900 nextdl = dl->dl_next;
901
902 if (dl->dl_lintvalue != NULL) {
903 /*
904 * If an entry already exists, then we are about to
905 * complain, so no worry.
906 */
907 (void) dlhash_insert(defoptlint, dl->dl_name,
908 dl);
909 }
910
911 /* An option name can be declared at most once. */
912 if (DEFINED_OPTION(dl->dl_name)) {
913 cfgerror("file system or option `%s' already defined",
914 dl->dl_name);
915 return;
916 }
917
918 if (dlhash_insert(ht, dl->dl_name, dl)) {
919 cfgerror("file system or option `%s' already defined",
920 dl->dl_name);
921 return;
922 }
923
924 if (fname == NULL) {
925 /*
926 * Each option will be going into its own file.
927 * Convert the option name to lower case. This
928 * lower case name will be used as the option
929 * file name.
930 */
931 (void) snprintf(buf, sizeof(buf), "opt_%s.h",
932 strtolower(dl->dl_name));
933 name = intern(buf);
934 } else {
935 name = fname;
936 }
937
938 add_opt_dependencies(dl, deps);
939
940 /*
941 * Remove this option from the parameter list before adding
942 * it to the list associated with this option file.
943 */
944 dl->dl_next = NULL;
945
946 /*
947 * Flag as obsolete, if requested.
948 */
949 if (obs) {
950 dl->dl_obsolete = 1;
951 (void)dlhash_insert(obsopttab, dl->dl_name, dl);
952 }
953
954 /*
955 * Add this option file if we haven't seen it yet.
956 * Otherwise, append to the list of options already
957 * associated with this file.
958 */
959 if ((olddl = dlhash_lookup(optfiletab, name)) == NULL) {
960 (void)dlhash_insert(optfiletab, name, dl);
961 } else {
962 while (olddl->dl_next != NULL)
963 olddl = olddl->dl_next;
964 olddl->dl_next = dl;
965 }
966 }
967 }
968
969 /*
970 * Define one or more standard options. If an option file name is specified,
971 * place all options in one file with the specified name. Otherwise, create
972 * an option file for each option.
973 */
974 void
975 defoption(const char *fname, struct defoptlist *opts, struct nvlist *deps)
976 {
977
978 cfgwarn("The use of `defopt' is deprecated");
979 defopt(defopttab, fname, opts, deps, 0);
980 }
981
982
983 /*
984 * Define an option for which a value is required.
985 */
986 void
987 defparam(const char *fname, struct defoptlist *opts, struct nvlist *deps, int obs)
988 {
989
990 defopt(defparamtab, fname, opts, deps, obs);
991 }
992
993 /*
994 * Define an option which must not have a value, and which
995 * emits a "needs-flag" style output.
996 */
997 void
998 defflag(const char *fname, struct defoptlist *opts, struct nvlist *deps, int obs)
999 {
1000
1001 defopt(defflagtab, fname, opts, deps, obs);
1002 }
1003
1004
1005 /*
1006 * Add an option from "options FOO". Note that this selects things that
1007 * are "optional foo".
1008 */
1009 void
1010 addoption(const char *name, const char *value)
1011 {
1012 const char *n;
1013 int is_fs, is_param, is_flag, is_undecl, is_obs;
1014
1015 /*
1016 * Figure out how this option was declared (if at all.)
1017 * XXX should use "params" and "flags" in config.
1018 * XXX crying out for a type field in a unified hashtab.
1019 */
1020 is_fs = OPT_FSOPT(name);
1021 is_param = OPT_DEFPARAM(name);
1022 is_flag = OPT_DEFFLAG(name);
1023 is_obs = OPT_OBSOLETE(name);
1024 is_undecl = !DEFINED_OPTION(name);
1025
1026 /* Warn and pretend the user had not selected the option */
1027 if (is_obs) {
1028 cfgwarn("obsolete option `%s' will be ignored", name);
1029 return;
1030 }
1031
1032 /* Make sure this is not a defined file system. */
1033 if (is_fs) {
1034 cfgerror("`%s' is a defined file system", name);
1035 return;
1036 }
1037 /* A defparam must have a value */
1038 if (is_param && value == NULL) {
1039 cfgerror("option `%s' must have a value", name);
1040 return;
1041 }
1042 /* A defflag must not have a value */
1043 if (is_flag && value != NULL) {
1044 cfgerror("option `%s' must not have a value", name);
1045 return;
1046 }
1047
1048 if (is_undecl && vflag) {
1049 cfgwarn("undeclared option `%s' added to IDENT", name);
1050 }
1051
1052 if (do_option(opttab, &options, &nextopt, name, value, "options",
1053 selecttab))
1054 return;
1055
1056 /* make lowercase, then add to select table */
1057 n = strtolower(name);
1058 (void)ht_insert(selecttab, n, (void *)__UNCONST(n));
1059 CFGDBG(3, "option selected `%s'", n);
1060 }
1061
1062 void
1063 deloption(const char *name)
1064 {
1065
1066 CFGDBG(4, "deselecting opt `%s'", name);
1067 if (undo_option(opttab, &options, &nextopt, name, "options"))
1068 return;
1069 if (undo_option(selecttab, NULL, NULL, strtolower(name), "options"))
1070 return;
1071 }
1072
1073 /*
1074 * Add a file system option. This routine simply inserts the name into
1075 * a list of valid file systems, which is used to validate the root
1076 * file system type. The name is then treated like a standard option.
1077 */
1078 void
1079 addfsoption(const char *name)
1080 {
1081 const char *n;
1082
1083 /* Make sure this is a defined file system. */
1084 if (!OPT_FSOPT(name)) {
1085 cfgerror("`%s' is not a defined file system", name);
1086 return;
1087 }
1088
1089 /*
1090 * Convert to lower case. This will be used in the select
1091 * table, to verify root file systems.
1092 */
1093 n = strtolower(name);
1094
1095 if (do_option(fsopttab, &fsoptions, &nextfsopt, name, n, "file-system",
1096 selecttab))
1097 return;
1098
1099 /* Add to select table. */
1100 (void)ht_insert(selecttab, n, __UNCONST(n));
1101 CFGDBG(3, "fs selected `%s'", name);
1102
1103 /*
1104 * Select attribute if one exists.
1105 */
1106 struct attr *a;
1107 if ((a = ht_lookup(attrtab, n)) != NULL)
1108 selectattr(a);
1109 }
1110
1111 void
1112 delfsoption(const char *name)
1113 {
1114 const char *n;
1115
1116 CFGDBG(4, "deselecting fs `%s'", name);
1117 n = strtolower(name);
1118 if (undo_option(fsopttab, &fsoptions, &nextfsopt, name, "file-system"))
1119 return;
1120 if (undo_option(selecttab, NULL, NULL, n, "file-system"))
1121 return;
1122 }
1123
1124 /*
1125 * Add a "make" option.
1126 */
1127 void
1128 addmkoption(const char *name, const char *value)
1129 {
1130
1131 (void)do_option(mkopttab, &mkoptions, &nextmkopt, name, value,
1132 "makeoptions", NULL);
1133 }
1134
1135 void
1136 delmkoption(const char *name)
1137 {
1138
1139 CFGDBG(4, "deselecting mkopt `%s'", name);
1140 (void)undo_option(mkopttab, &mkoptions, &nextmkopt, name,
1141 "makeoptions");
1142 }
1143
1144 /*
1145 * Add an appending "make" option.
1146 */
1147 void
1148 appendmkoption(const char *name, const char *value)
1149 {
1150 struct nvlist *nv;
1151
1152 nv = newnv(name, value, NULL, 0, NULL);
1153 *nextappmkopt = nv;
1154 nextappmkopt = &nv->nv_next;
1155 }
1156
1157 /*
1158 * Add a conditional appending "make" option.
1159 */
1160 void
1161 appendcondmkoption(struct condexpr *cond, const char *name, const char *value)
1162 {
1163 struct nvlist *nv;
1164
1165 nv = newnv(name, value, cond, 0, NULL);
1166 *nextcndmkopt = nv;
1167 nextcndmkopt = &nv->nv_next;
1168 }
1169
1170 /*
1171 * Copy maxusers to param "MAXUSERS".
1172 */
1173 void
1174 fixmaxusers(void)
1175 {
1176 char str[32];
1177
1178 snprintf(str, sizeof(str), "%d", maxusers);
1179 addoption(intern("MAXUSERS"), intern(str));
1180 }
1181
1182 /*
1183 * Copy makeoptions to params with "makeoptions_" prefix.
1184 */
1185 void
1186 fixmkoption(void)
1187 {
1188 struct nvlist *nv;
1189 char buf[100];
1190 const char *name;
1191
1192 for (nv = mkoptions; nv != NULL; nv = nv->nv_next) {
1193 snprintf(buf, sizeof(buf), "makeoptions_%s", nv->nv_name);
1194 name = intern(buf);
1195 if (!DEFINED_OPTION(name) || !OPT_DEFPARAM(name))
1196 continue;
1197 addoption(name, intern(nv->nv_str));
1198 }
1199 }
1200
1201 /*
1202 * Add a name=value pair to an option list. The value may be NULL.
1203 */
1204 static int
1205 do_option(struct hashtab *ht, struct nvlist **npp, struct nvlist ***next,
1206 const char *name, const char *value, const char *type,
1207 struct hashtab *stab)
1208 {
1209 struct nvlist *nv, *onv;
1210
1211 /* assume it will work */
1212 nv = newnv(name, value, NULL, 0, NULL);
1213 if (ht_insert(ht, name, nv) != 0) {
1214
1215 /* oops, already got that option - remove it first */
1216 if ((onv = ht_lookup(ht, name)) == NULL)
1217 panic("do_option 1");
1218 if (onv->nv_str != NULL && !OPT_FSOPT(name))
1219 cfgwarn("already have %s `%s=%s'", type, name,
1220 onv->nv_str);
1221 else
1222 cfgwarn("already have %s `%s'", type, name);
1223
1224 if (undo_option(ht, npp, next, name, type))
1225 panic("do_option 2");
1226 if (stab != NULL &&
1227 undo_option(stab, NULL, NULL, strtolower(name), type))
1228 panic("do_option 3");
1229
1230 /* now try adding it again */
1231 if (ht_insert(ht, name, nv) != 0)
1232 panic("do_option 4");
1233
1234 CFGDBG(2, "opt `%s' replaced", name);
1235 }
1236 **next = nv;
1237 *next = &nv->nv_next;
1238
1239 return (0);
1240 }
1241
1242 /*
1243 * Remove a name from a hash table,
1244 * and optionally, a name=value pair from an option list.
1245 */
1246 static int
1247 undo_option(struct hashtab *ht, struct nvlist **npp,
1248 struct nvlist ***next, const char *name, const char *type)
1249 {
1250 struct nvlist *nv;
1251
1252 if (ht_remove(ht, name)) {
1253 /*
1254 * -U command line option removals are always silent
1255 */
1256 if (!handling_cmdlineopts)
1257 cfgwarn("%s `%s' is not defined", type, name);
1258 return (1);
1259 }
1260 if (npp == NULL) {
1261 CFGDBG(2, "opt `%s' deselected", name);
1262 return (0);
1263 }
1264
1265 for ( ; *npp != NULL; npp = &(*npp)->nv_next) {
1266 if ((*npp)->nv_name != name)
1267 continue;
1268 if (next != NULL && *next == &(*npp)->nv_next)
1269 *next = npp;
1270 nv = (*npp)->nv_next;
1271 CFGDBG(2, "opt `%s' deselected", (*npp)->nv_name);
1272 nvfree(*npp);
1273 *npp = nv;
1274 return (0);
1275 }
1276 panic("%s `%s' is not defined in nvlist", type, name);
1277 return (1);
1278 }
1279
1280 /*
1281 * Return true if there is at least one instance of the given unit
1282 * on the given device attachment (or any units, if unit == WILD).
1283 */
1284 int
1285 deva_has_instances(struct deva *deva, int unit)
1286 {
1287 struct devi *i;
1288
1289 /*
1290 * EHAMMERTOOBIG: we shouldn't check i_pseudoroot here.
1291 * What we want by this check is them to appear non-present
1292 * except for purposes of other devices being able to attach
1293 * to them.
1294 */
1295 for (i = deva->d_ihead; i != NULL; i = i->i_asame)
1296 if (i->i_active == DEVI_ACTIVE && i->i_pseudoroot == 0 &&
1297 (unit == WILD || unit == i->i_unit || i->i_unit == STAR))
1298 return (1);
1299 return (0);
1300 }
1301
1302 /*
1303 * Return true if there is at least one instance of the given unit
1304 * on the given base (or any units, if unit == WILD).
1305 */
1306 int
1307 devbase_has_instances(struct devbase *dev, int unit)
1308 {
1309 struct deva *da;
1310
1311 /*
1312 * Pseudo-devices are a little special. We consider them
1313 * to have instances only if they are both:
1314 *
1315 * 1. Included in this kernel configuration.
1316 *
1317 * 2. Be declared "defpseudodev".
1318 */
1319 if (dev->d_ispseudo) {
1320 return ((ht_lookup(devitab, dev->d_name) != NULL)
1321 && (dev->d_ispseudo > 1));
1322 }
1323
1324 for (da = dev->d_ahead; da != NULL; da = da->d_bsame)
1325 if (deva_has_instances(da, unit))
1326 return (1);
1327 return (0);
1328 }
1329
1330 static int
1331 cfcrosscheck(struct config *cf, const char *what, struct nvlist *nv)
1332 {
1333 struct devbase *dev;
1334 struct devi *pd;
1335 int errs, devunit;
1336
1337 if (maxpartitions <= 0)
1338 panic("cfcrosscheck");
1339
1340 for (errs = 0; nv != NULL; nv = nv->nv_next) {
1341 if (nv->nv_name == NULL)
1342 continue;
1343 dev = ht_lookup(devbasetab, nv->nv_name);
1344 if (dev == NULL)
1345 panic("cfcrosscheck(%s)", nv->nv_name);
1346 if (has_attr(dev->d_attrs, s_ifnet))
1347 devunit = nv->nv_ifunit; /* XXX XXX XXX */
1348 else
1349 devunit = (int)(minor(nv->nv_num) / maxpartitions);
1350 if (devbase_has_instances(dev, devunit))
1351 continue;
1352 if (devbase_has_instances(dev, STAR) &&
1353 devunit >= dev->d_umax)
1354 continue;
1355 TAILQ_FOREACH(pd, &allpseudo, i_next) {
1356 if (pd->i_base == dev && devunit < dev->d_umax &&
1357 devunit >= 0)
1358 goto loop;
1359 }
1360 (void)fprintf(stderr,
1361 "%s:%d: %s says %s on %s, but there's no %s\n",
1362 conffile, cf->cf_lineno,
1363 cf->cf_name, what, nv->nv_str, nv->nv_str);
1364 errs++;
1365 loop:
1366 ;
1367 }
1368 return (errs);
1369 }
1370
1371 /*
1372 * Cross-check the configuration: make sure that each target device
1373 * or attribute (`at foo[0*?]') names at least one real device. Also
1374 * see that the root and dump devices for all configurations are there.
1375 */
1376 int
1377 crosscheck(void)
1378 {
1379 struct config *cf;
1380 int errs;
1381
1382 errs = 0;
1383 if (TAILQ_EMPTY(&allcf)) {
1384 warnx("%s has no configurations!", conffile);
1385 errs++;
1386 }
1387 TAILQ_FOREACH(cf, &allcf, cf_next) {
1388 if (cf->cf_root != NULL) { /* i.e., not root on ? */
1389 errs += cfcrosscheck(cf, "root", cf->cf_root);
1390 errs += cfcrosscheck(cf, "dumps", cf->cf_dump);
1391 }
1392 }
1393 return (errs);
1394 }
1395
1396 /*
1397 * Check to see if there is a *'d unit with a needs-count file.
1398 */
1399 int
1400 badstar(void)
1401 {
1402 struct devbase *d;
1403 struct deva *da;
1404 struct devi *i;
1405 int errs, n;
1406
1407 errs = 0;
1408 TAILQ_FOREACH(d, &allbases, d_next) {
1409 for (da = d->d_ahead; da != NULL; da = da->d_bsame)
1410 for (i = da->d_ihead; i != NULL; i = i->i_asame) {
1411 if (i->i_unit == STAR)
1412 goto aybabtu;
1413 }
1414 continue;
1415 aybabtu:
1416 if (ht_lookup(needcnttab, d->d_name)) {
1417 warnx("%s's cannot be *'d until its driver is fixed",
1418 d->d_name);
1419 errs++;
1420 continue;
1421 }
1422 for (n = 0; i != NULL; i = i->i_alias)
1423 if (!i->i_collapsed)
1424 n++;
1425 if (n < 1)
1426 panic("badstar() n<1");
1427 }
1428 return (errs);
1429 }
1430
1431 /*
1432 * Verify/create builddir if necessary, change to it, and verify srcdir.
1433 * This will be called when we see the first include.
1434 */
1435 void
1436 setupdirs(void)
1437 {
1438 struct stat st;
1439
1440 /* srcdir must be specified if builddir is not specified or if
1441 * no configuration filename was specified. */
1442 if ((builddir || strcmp(defbuilddir, ".") == 0) && !srcdir) {
1443 cfgerror("source directory must be specified");
1444 exit(1);
1445 }
1446
1447 if (Lflag) {
1448 if (srcdir == NULL)
1449 srcdir = "../../..";
1450 return;
1451 }
1452
1453 if (srcdir == NULL)
1454 srcdir = "../../../..";
1455 if (builddir == NULL)
1456 builddir = defbuilddir;
1457
1458 if (stat(builddir, &st) == -1) {
1459 if (mkdir(builddir, 0777) == -1)
1460 errx(EXIT_FAILURE, "cannot create %s", builddir);
1461 } else if (!S_ISDIR(st.st_mode))
1462 errx(EXIT_FAILURE, "%s is not a directory", builddir);
1463 if (chdir(builddir) == -1)
1464 err(EXIT_FAILURE, "cannot change to %s", builddir);
1465 if (stat(srcdir, &st) == -1)
1466 err(EXIT_FAILURE, "cannot stat %s", srcdir);
1467 if (!S_ISDIR(st.st_mode))
1468 errx(EXIT_FAILURE, "%s is not a directory", srcdir);
1469 }
1470
1471 /*
1472 * Write identifier from "ident" directive into file, for
1473 * newvers.sh to pick it up.
1474 */
1475 int
1476 mkident(void)
1477 {
1478 FILE *fp;
1479 int error = 0;
1480
1481 (void)unlink("ident");
1482
1483 if (ident == NULL)
1484 return (0);
1485
1486 if ((fp = fopen("ident", "w")) == NULL) {
1487 warn("cannot write ident");
1488 return (1);
1489 }
1490 if (vflag)
1491 (void)printf("using ident '%s'\n", ident);
1492 fprintf(fp, "%s\n", ident);
1493 fflush(fp);
1494 if (ferror(fp))
1495 error = 1;
1496 (void)fclose(fp);
1497
1498 return error;
1499 }
1500
1501 void
1502 logconfig_start(void)
1503 {
1504 extern FILE *yyin;
1505 char line[1024];
1506 const char *tmpdir;
1507 struct stat st;
1508 int fd;
1509
1510 if (yyin == NULL || fstat(fileno(yyin), &st) == -1)
1511 return;
1512 cfgtime = st.st_mtime;
1513
1514 tmpdir = getenv("TMPDIR");
1515 if (tmpdir == NULL)
1516 tmpdir = _PATH_TMP;
1517 (void)snprintf(line, sizeof(line), "%s/config.tmp.XXXXXX", tmpdir);
1518 if ((fd = mkstemp(line)) == -1 ||
1519 (cfg = fdopen(fd, "r+")) == NULL) {
1520 if (fd != -1) {
1521 (void)unlink(line);
1522 (void)close(fd);
1523 }
1524 cfg = NULL;
1525 return;
1526 }
1527 (void)unlink(line);
1528
1529 (void)fprintf(cfg, "#include <sys/cdefs.h>\n\n");
1530 (void)fprintf(cfg, "#include \"opt_config.h\"\n");
1531 (void)fprintf(cfg, "\n");
1532 (void)fprintf(cfg, "/*\n");
1533 (void)fprintf(cfg, " * Add either (or both) of\n");
1534 (void)fprintf(cfg, " *\n");
1535 (void)fprintf(cfg, " *\toptions %s\n", LOGCONFIG_LARGE);
1536 (void)fprintf(cfg, " *\toptions %s\n", LOGCONFIG_SMALL);
1537 (void)fprintf(cfg, " *\n");
1538 (void)fprintf(cfg,
1539 " * to your kernel config file to embed it in the resulting\n");
1540 (void)fprintf(cfg,
1541 " * kernel. The latter option does not include files that are\n");
1542 (void)fprintf(cfg,
1543 " * included (recursively) by your config file. The embedded\n");
1544 (void)fprintf(cfg,
1545 " * data be extracted by using the command:\n");
1546 (void)fprintf(cfg, " *\n");
1547 (void)fprintf(cfg,
1548 " *\tstrings netbsd | sed -n 's/^_CFG_//p' | unvis\n");
1549 (void)fprintf(cfg, " */\n");
1550 (void)fprintf(cfg, "\n");
1551 (void)fprintf(cfg, "#ifdef CONFIG_FILE\n");
1552 (void)fprintf(cfg, "#if defined(%s) || defined(%s)\n\n",
1553 LOGCONFIG_LARGE, LOGCONFIG_SMALL);
1554 (void)fprintf(cfg, "static const char config[] __used =\n\n");
1555
1556 (void)fprintf(cfg, "#ifdef %s\n\n", LOGCONFIG_LARGE);
1557 (void)fprintf(cfg, "\"_CFG_### START CONFIG FILE \\\"%s\\\"\\n\"\n\n",
1558 conffile);
1559 (void)fprintf(cfg, "#endif /* %s */\n\n", LOGCONFIG_LARGE);
1560
1561 logconfig_include(yyin, NULL);
1562
1563 (void)fprintf(cfg, "#ifdef %s\n\n", LOGCONFIG_LARGE);
1564 (void)fprintf(cfg, "\"_CFG_### END CONFIG FILE \\\"%s\\\"\\n\"\n",
1565 conffile);
1566
1567 rewind(yyin);
1568 }
1569
1570 void
1571 logconfig_include(FILE *cf, const char *filename)
1572 {
1573 char line[1024], in[2048], *out;
1574 struct stat st;
1575 int missingeol;
1576
1577 if (!cfg)
1578 return;
1579
1580 missingeol = 0;
1581 if (fstat(fileno(cf), &st) == -1)
1582 return;
1583 if (cfgtime < st.st_mtime)
1584 cfgtime = st.st_mtime;
1585
1586 if (filename)
1587 (void)fprintf(cfg,
1588 "\"_CFG_### (included from \\\"%s\\\")\\n\"\n",
1589 filename);
1590 while (fgets(line, sizeof(line), cf) != NULL) {
1591 missingeol = 1;
1592 (void)fprintf(cfg, "\"_CFG_");
1593 if (filename)
1594 (void)fprintf(cfg, "###> ");
1595 strvis(in, line, VIS_TAB);
1596 for (out = in; *out; out++)
1597 switch (*out) {
1598 case '\n':
1599 (void)fprintf(cfg, "\\n\"\n");
1600 missingeol = 0;
1601 break;
1602 case '"': case '\\':
1603 (void)fputc('\\', cfg);
1604 /* FALLTHROUGH */
1605 default:
1606 (void)fputc(*out, cfg);
1607 break;
1608 }
1609 }
1610 if (missingeol) {
1611 (void)fprintf(cfg, "\\n\"\n");
1612 warnx("%s: newline missing at EOF",
1613 filename != NULL ? filename : conffile);
1614 }
1615 if (filename)
1616 (void)fprintf(cfg, "\"_CFG_### (end include \\\"%s\\\")\\n\"\n",
1617 filename);
1618
1619 rewind(cf);
1620 }
1621
1622 void
1623 logconfig_end(void)
1624 {
1625 char line[1024];
1626 FILE *fp;
1627 struct stat st;
1628
1629 if (!cfg)
1630 return;
1631
1632 (void)fprintf(cfg, "#endif /* %s */\n", LOGCONFIG_LARGE);
1633 (void)fprintf(cfg, ";\n");
1634 (void)fprintf(cfg, "#endif /* %s || %s */\n",
1635 LOGCONFIG_LARGE, LOGCONFIG_SMALL);
1636 (void)fprintf(cfg, "#endif /* CONFIG_FILE */\n");
1637 fflush(cfg);
1638 if (ferror(cfg))
1639 err(EXIT_FAILURE, "write to temporary file for config.h failed");
1640 rewind(cfg);
1641
1642 if (stat("config_file.h", &st) != -1) {
1643 if (cfgtime < st.st_mtime) {
1644 fclose(cfg);
1645 return;
1646 }
1647 }
1648
1649 fp = fopen("config_file.h", "w");
1650 if (!fp)
1651 err(EXIT_FAILURE, "cannot open \"config.h\"");
1652
1653 while (fgets(line, sizeof(line), cfg) != NULL)
1654 fputs(line, fp);
1655 fflush(fp);
1656 if (ferror(fp))
1657 err(EXIT_FAILURE, "write to \"config.h\" failed");
1658 fclose(fp);
1659 fclose(cfg);
1660 }
1661
1662 const char *
1663 strtolower(const char *name)
1664 {
1665 const char *n;
1666 char *p, low[500];
1667 char c;
1668
1669 for (n = name, p = low; (c = *n) != '\0'; n++)
1670 *p++ = (char)(isupper((u_char)c) ? tolower((u_char)c) : c);
1671 *p = '\0';
1672 return (intern(low));
1673 }
1674
1675 static int
1676 is_elf(const char *file)
1677 {
1678 int kernel;
1679 char hdr[4];
1680
1681 kernel = open(file, O_RDONLY);
1682 if (kernel == -1)
1683 err(EXIT_FAILURE, "cannot open %s", file);
1684 if (read(kernel, hdr, 4) != 4)
1685 err(EXIT_FAILURE, "Cannot read from %s", file);
1686 (void)close(kernel);
1687
1688 return memcmp("\177ELF", hdr, 4) == 0 ? 1 : 0;
1689 }
1690
1691 static int
1692 extract_config(const char *kname, const char *cname, int cfd)
1693 {
1694 char *ptr;
1695 void *base;
1696 int found, kfd;
1697 struct stat st;
1698 off_t i;
1699
1700 found = 0;
1701
1702 /* mmap(2) binary kernel */
1703 kfd = open(conffile, O_RDONLY);
1704 if (kfd == -1)
1705 err(EXIT_FAILURE, "cannot open %s", kname);
1706 if (fstat(kfd, &st) == -1)
1707 err(EXIT_FAILURE, "cannot stat %s", kname);
1708 base = mmap(0, (size_t)st.st_size, PROT_READ, MAP_FILE | MAP_SHARED,
1709 kfd, 0);
1710 if (base == MAP_FAILED)
1711 err(EXIT_FAILURE, "cannot mmap %s", kname);
1712 ptr = base;
1713
1714 /* Scan mmap(2)'ed region, extracting kernel configuration */
1715 for (i = 0; i < st.st_size; i++) {
1716 if ((*ptr == '_') && (st.st_size - i > 5) && memcmp(ptr,
1717 "_CFG_", 5) == 0) {
1718 /* Line found */
1719 char *oldptr, line[LINE_MAX + 1], uline[LINE_MAX + 1];
1720 int j;
1721
1722 found = 1;
1723
1724 oldptr = (ptr += 5);
1725 while (*ptr != '\n' && *ptr != '\0')
1726 ptr++;
1727 if (ptr - oldptr > LINE_MAX)
1728 errx(EXIT_FAILURE, "line too long");
1729 i += ptr - oldptr + 5;
1730 (void)memcpy(line, oldptr, (size_t)(ptr - oldptr));
1731 line[ptr - oldptr] = '\0';
1732 j = strunvis(uline, line);
1733 if (j == -1)
1734 errx(EXIT_FAILURE, "unvis: invalid "
1735 "encoded sequence");
1736 uline[j] = '\n';
1737 if (write(cfd, uline, (size_t)j + 1) == -1)
1738 err(EXIT_FAILURE, "cannot write to %s", cname);
1739 } else
1740 ptr++;
1741 }
1742
1743 (void)close(kfd);
1744 (void)munmap(base, (size_t)st.st_size);
1745
1746 return found;
1747 }
1748
1749 struct dhdi_params {
1750 struct devbase *d;
1751 int unit;
1752 int level;
1753 };
1754
1755 static int
1756 devbase_has_dead_instances(const char *key, void *value, void *aux)
1757 {
1758 struct devi *i;
1759 struct dhdi_params *dhdi = aux;
1760
1761 for (i = value; i != NULL; i = i->i_alias)
1762 if (i->i_base == dhdi->d &&
1763 (dhdi->unit == WILD || dhdi->unit == i->i_unit ||
1764 i->i_unit == STAR) &&
1765 i->i_level >= dhdi->level)
1766 return 1;
1767 return 0;
1768 }
1769
1770 /*
1771 * This is almost the same as devbase_has_instances, except it
1772 * may have special considerations regarding ignored instances.
1773 */
1774
1775 static int
1776 devbase_has_any_instance(struct devbase *dev, int unit, int state, int level)
1777 {
1778 struct deva *da;
1779 struct devi *i;
1780
1781 if (dev->d_ispseudo) {
1782 if (dev->d_ihead != NULL)
1783 return 1;
1784 else if (state != DEVI_IGNORED)
1785 return 0;
1786 if ((i = ht_lookup(deaddevitab, dev->d_name)) == NULL)
1787 return 0;
1788 return (i->i_level >= level);
1789 }
1790
1791 for (da = dev->d_ahead; da != NULL; da = da->d_bsame)
1792 for (i = da->d_ihead; i != NULL; i = i->i_asame)
1793 if ((i->i_active == DEVI_ACTIVE ||
1794 i->i_active == state) &&
1795 (unit == WILD || unit == i->i_unit ||
1796 i->i_unit == STAR))
1797 return 1;
1798
1799 if (state == DEVI_IGNORED) {
1800 struct dhdi_params dhdi = { dev, unit, level };
1801 /* also check dead devices */
1802 return ht_enumerate(deaddevitab, devbase_has_dead_instances,
1803 &dhdi);
1804 }
1805
1806 return 0;
1807 }
1808
1809 /*
1810 * check_dead_devi(), used with ht_enumerate, checks if any of the removed
1811 * device instances would have been a valid instance considering the devbase,
1812 * the parent device and the interface attribute.
1813 *
1814 * In other words, for a non-active device, it checks if children would be
1815 * actual orphans or the result of a negative statement in the config file.
1816 */
1817
1818 struct cdd_params {
1819 struct devbase *d;
1820 struct attr *at;
1821 struct devbase *parent;
1822 };
1823
1824 static int
1825 check_dead_devi(const char *key, void *value, void *aux)
1826 {
1827 struct cdd_params *cdd = aux;
1828 struct devi *i = value;
1829 struct pspec *p;
1830
1831 if (i->i_base != cdd->d)
1832 return 0;
1833
1834 for (; i != NULL; i = i->i_alias) {
1835 p = i->i_pspec;
1836 if ((p == NULL && cdd->at == NULL) ||
1837 (p != NULL && p->p_iattr == cdd->at &&
1838 (p->p_atdev == NULL || p->p_atdev == cdd->parent))) {
1839 if (p != NULL &&
1840 !devbase_has_any_instance(cdd->parent, p->p_atunit,
1841 DEVI_IGNORED, i->i_level))
1842 return 0;
1843 else
1844 return 1;
1845 }
1846 }
1847 return 0;
1848 }
1849
1850 static void
1851 do_kill_orphans(struct devbase *d, struct attr *at, struct devbase *parent,
1852 int state)
1853 {
1854 struct nvlist *nv1;
1855 struct attrlist *al;
1856 struct attr *a;
1857 struct devi *i, *j = NULL;
1858 struct pspec *p;
1859 int active = 0;
1860
1861 /*
1862 * A pseudo-device will always attach at root, and if it has an
1863 * instance (it cannot have more than one), it is enough to consider
1864 * it active, as there is no real attachment.
1865 *
1866 * A pseudo device can never be marked DEVI_IGNORED.
1867 */
1868 if (d->d_ispseudo) {
1869 if (d->d_ihead != NULL)
1870 d->d_ihead->i_active = active = DEVI_ACTIVE;
1871 else {
1872 if (ht_lookup(deaddevitab, d->d_name) != NULL)
1873 active = DEVI_IGNORED;
1874 else
1875 return;
1876 }
1877 } else {
1878 int seen = 0;
1879
1880 for (i = d->d_ihead; i != NULL; i = i->i_bsame) {
1881 for (j = i; j != NULL; j = j->i_alias) {
1882 p = j->i_pspec;
1883 if ((p == NULL && at == NULL) ||
1884 (p != NULL && p->p_iattr == at &&
1885 (p->p_atdev == NULL ||
1886 p->p_atdev == parent))) {
1887 if (p != NULL &&
1888 !devbase_has_any_instance(parent,
1889 p->p_atunit, state, j->i_level))
1890 continue;
1891 /*
1892 * There are Fry-like devices which can
1893 * be their own grand-parent (or even
1894 * parent, like uhub). We don't want
1895 * to loop, so if we've already reached
1896 * an instance for one reason or
1897 * another, stop there.
1898 */
1899 if (j->i_active == DEVI_ACTIVE ||
1900 j->i_active == state) {
1901 /*
1902 * Device has already been
1903 * seen. However it might
1904 * have siblings who still
1905 * have to be activated or
1906 * orphaned.
1907 */
1908 seen = 1;
1909 continue;
1910 }
1911 j->i_active = active = state;
1912 if (p != NULL)
1913 p->p_active = state;
1914 }
1915 }
1916 }
1917 /*
1918 * If we've been there but have made no change, stop.
1919 */
1920 if (seen && !active)
1921 return;
1922 if (!active) {
1923 struct cdd_params cdd = { d, at, parent };
1924 /* Look for a matching dead devi */
1925 if (ht_enumerate(deaddevitab, check_dead_devi, &cdd) &&
1926 d != parent)
1927 /*
1928 * That device had its instances removed.
1929 * Continue the loop marking descendants
1930 * with DEVI_IGNORED instead of DEVI_ACTIVE.
1931 *
1932 * There is one special case for devices that
1933 * are their own parent: if that instance is
1934 * removed (e.g., no uhub* at uhub?), we don't
1935 * have to continue looping.
1936 */
1937 active = DEVI_IGNORED;
1938 else
1939 return;
1940 }
1941 }
1942
1943 for (al = d->d_attrs; al != NULL; al = al->al_next) {
1944 a = al->al_this;
1945 for (nv1 = a->a_devs; nv1 != NULL; nv1 = nv1->nv_next)
1946 do_kill_orphans(nv1->nv_ptr, a, d, active);
1947 }
1948 }
1949
1950 static int
1951 /*ARGSUSED*/
1952 kill_orphans_cb(const char *key, void *value, void *aux)
1953 {
1954 do_kill_orphans((struct devbase *)value, NULL, NULL, DEVI_ACTIVE);
1955 return 0;
1956 }
1957
1958 static void
1959 kill_orphans(void)
1960 {
1961 ht_enumerate(devroottab, kill_orphans_cb, NULL);
1962 }
1963
1964 static void
1965 add_makeopt(const char *opt)
1966 {
1967 struct nvlist *p;
1968 char *buf = estrdup(opt);
1969 char *eq = strchr(buf, '=');
1970
1971 if (!eq)
1972 errx(EXIT_FAILURE, "-D %s is not in var=value format", opt);
1973
1974 *eq = 0;
1975 p = newnv(estrdup(buf), estrdup(eq+1), NULL, 0, NULL);
1976 free(buf);
1977 p->nv_next = cmdlinedefs;
1978 cmdlinedefs = p;
1979 }
1980
1981 static void
1982 remove_makeopt(const char *opt)
1983 {
1984 struct nvlist *p;
1985
1986 p = newnv(estrdup(opt), NULL, NULL, 0, NULL);
1987 p->nv_next = cmdlineundefs;
1988 cmdlineundefs = p;
1989 }
1990
1991 static void
1992 handle_cmdline_makeoptions(void)
1993 {
1994 struct nvlist *p, *n;
1995
1996 handling_cmdlineopts = 1;
1997 for (p = cmdlineundefs; p; p = n) {
1998 n = p->nv_next;
1999 delmkoption(intern(p->nv_name));
2000 free(__UNCONST(p->nv_name));
2001 nvfree(p);
2002 }
2003 for (p = cmdlinedefs; p; p = n) {
2004 const char *name = intern(p->nv_name);
2005
2006 n = p->nv_next;
2007 delmkoption(name);
2008 addmkoption(name, intern(p->nv_str));
2009 free(__UNCONST(p->nv_name));
2010 free(__UNCONST(p->nv_str));
2011
2012 nvfree(p);
2013 }
2014 handling_cmdlineopts = 0;
2015 }
2016