main.c revision 1.14 1 /* $NetBSD: main.c,v 1.14 2006/08/26 18:17:13 christos 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 #ifndef MAKE_BOOTSTRAP
48 #include <sys/cdefs.h>
49 #define COPYRIGHT(x) __COPYRIGHT(x)
50 #else
51 #define COPYRIGHT(x) static const char copyright[] = x
52 #endif
53
54 #ifndef lint
55 COPYRIGHT("@(#) Copyright (c) 1992, 1993\n\
56 The Regents of the University of California. All rights reserved.\n");
57 #endif /* not lint */
58
59 #include <sys/types.h>
60 #include <sys/stat.h>
61 #include <sys/param.h>
62 #include <sys/mman.h>
63 #include <paths.h>
64 #include <ctype.h>
65 #include <errno.h>
66 #include <fcntl.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #include <unistd.h>
71 #include <vis.h>
72 #include <util.h>
73
74 #include "defs.h"
75 #include "sem.h"
76
77 #ifndef LINE_MAX
78 #define LINE_MAX 1024
79 #endif
80
81 int vflag; /* verbose output */
82 int Pflag; /* pack locators */
83
84 int yyparse(void);
85
86 #ifndef MAKE_BOOTSTRAP
87 extern int yydebug;
88 #endif
89
90 static struct hashtab *obsopttab;
91 static struct hashtab *mkopttab;
92 static struct nvlist **nextopt;
93 static struct nvlist **nextmkopt;
94 static struct nvlist **nextappmkopt;
95 static struct nvlist **nextfsopt;
96
97 static void usage(void);
98 static void dependopts(void);
99 static void do_depend(struct nvlist *);
100 static void stop(void);
101 static int do_option(struct hashtab *, struct nvlist ***,
102 const char *, const char *, const char *);
103 static int undo_option(struct hashtab *, struct nvlist **,
104 struct nvlist ***, const char *, const char *);
105 static int crosscheck(void);
106 static int badstar(void);
107 int main(int, char **);
108 static int mksymlinks(void);
109 static int mkident(void);
110 static int devbase_has_dead_instances(const char *, void *, void *);
111 static int devbase_has_any_instance(struct devbase *, int, int, int);
112 static int check_dead_devi(const char *, void *, void *);
113 static void kill_orphans(void);
114 static void do_kill_orphans(struct devbase *, struct attr *,
115 struct devbase *, int);
116 static int kill_orphans_cb(const char *, void *, void *);
117 static int cfcrosscheck(struct config *, const char *, struct nvlist *);
118 static const char *strtolower(const char *);
119 void defopt(struct hashtab *ht, const char *fname,
120 struct nvlist *opts, struct nvlist *deps, int obs);
121
122 #define LOGCONFIG_LARGE "INCLUDE_CONFIG_FILE"
123 #define LOGCONFIG_SMALL "INCLUDE_JUST_CONFIG"
124
125 static void logconfig_start(void);
126 static void logconfig_end(void);
127 static FILE *cfg;
128 static time_t cfgtime;
129
130 static int is_elf(const char *);
131 static int extract_config(const char *, const char *, int);
132
133 int badfilename(const char *fname);
134
135 const char *progname;
136
137 int
138 main(int argc, char **argv)
139 {
140 char *p, cname[20];
141 const char *last_component;
142 int pflag, xflag, ch, removeit;
143
144 setprogname(argv[0]);
145
146 pflag = 0;
147 xflag = 0;
148 while ((ch = getopt(argc, argv, "DPgpvb:s:x")) != -1) {
149 switch (ch) {
150
151 #ifndef MAKE_BOOTSTRAP
152 case 'D':
153 yydebug = 1;
154 break;
155 #endif
156
157 case 'P':
158 Pflag = 1;
159 break;
160
161 case 'g':
162 /*
163 * In addition to DEBUG, you probably wanted to
164 * set "options KGDB" and maybe others. We could
165 * do that for you, but you really should just
166 * put them in the config file.
167 */
168 (void)fprintf(stderr,
169 "config: -g is obsolete (use makeoptions DEBUG=\"-g\")\n");
170 usage();
171
172 case 'p':
173 /*
174 * Essentially the same as makeoptions PROF="-pg",
175 * but also changes the path from ../../compile/FOO
176 * to ../../compile/FOO.PROF; i.e., compile a
177 * profiling kernel based on a typical "regular"
178 * kernel.
179 *
180 * Note that if you always want profiling, you
181 * can (and should) use a "makeoptions" line.
182 */
183 pflag = 1;
184 break;
185
186 case 'v':
187 vflag = 1;
188 break;
189
190 case 'b':
191 builddir = optarg;
192 break;
193
194 case 's':
195 srcdir = optarg;
196 break;
197
198 case 'x':
199 xflag = 1;
200 break;
201
202 case '?':
203 default:
204 usage();
205 }
206 }
207
208 argc -= optind;
209 argv += optind;
210 if (argc > 1) {
211 usage();
212 }
213
214 if (xflag && (builddir != NULL || srcdir != NULL || Pflag || pflag ||
215 vflag)) {
216 (void)fprintf(stderr, "config: -x must be used alone\n");
217 exit(1);
218 }
219
220 if (xflag) {
221 #ifdef __NetBSD__
222 conffile = (argc == 1) ? argv[0] : _PATH_UNIX;
223 #else
224 if (argc == 0) {
225 (void)fprintf(stderr, "config: no kernel supplied\n");
226 exit(1);
227 }
228 #endif
229 if (!is_elf(conffile)) {
230 (void)fprintf(stderr,
231 "config: %s: not a binary kernel\n",
232 conffile);
233 exit(1);
234 }
235 if (!extract_config(conffile, "stdout", STDOUT_FILENO)) {
236 (void)fprintf(stderr,
237 "config: %s does not contain embedded "
238 "configuration data\n", conffile);
239 exit(2);
240 }
241 exit(0);
242 }
243
244 conffile = (argc == 1) ? argv[0] : "CONFIG";
245 if (firstfile(conffile)) {
246 (void)fprintf(stderr, "config: cannot read %s: %s\n",
247 conffile, strerror(errno));
248 exit(2);
249 }
250
251 /*
252 * Init variables.
253 */
254 minmaxusers = 1;
255 maxmaxusers = 10000;
256 initintern();
257 initfiles();
258 initsem();
259 ident = NULL;
260 devbasetab = ht_new();
261 devroottab = ht_new();
262 devatab = ht_new();
263 devitab = ht_new();
264 deaddevitab = ht_new();
265 selecttab = ht_new();
266 needcnttab = ht_new();
267 opttab = ht_new();
268 mkopttab = ht_new();
269 condmkopttab = ht_new();
270 fsopttab = ht_new();
271 deffstab = ht_new();
272 defopttab = ht_new();
273 defparamtab = ht_new();
274 defflagtab = ht_new();
275 optfiletab = ht_new();
276 obsopttab = ht_new();
277 bdevmtab = ht_new();
278 maxbdevm = 0;
279 cdevmtab = ht_new();
280 maxcdevm = 0;
281 nextopt = &options;
282 nextmkopt = &mkoptions;
283 nextappmkopt = &appmkoptions;
284 nextfsopt = &fsoptions;
285
286 /*
287 * Handle profiling (must do this before we try to create any
288 * files).
289 */
290 last_component = strrchr(conffile, '/');
291 last_component = (last_component) ? last_component + 1 : conffile;
292 if (pflag) {
293 p = emalloc(strlen(last_component) + 17);
294 (void)sprintf(p, "../compile/%s.PROF", last_component);
295 (void)addmkoption(intern("PROF"), "-pg");
296 (void)addoption(intern("GPROF"), NULL);
297 } else {
298 p = emalloc(strlen(last_component) + 13);
299 (void)sprintf(p, "../compile/%s", last_component);
300 }
301 defbuilddir = (argc == 0) ? "." : p;
302
303 removeit = 0;
304 if (is_elf(conffile)) {
305 const char *tmpdir;
306 int cfd;
307
308 if (builddir == NULL) {
309 (void)fprintf(stderr,
310 "config: build directory must be specified with "
311 "binary kernels\n");
312 exit(1);
313 }
314
315 /* Open temporary configuration file */
316 tmpdir = getenv("TMPDIR");
317 if (tmpdir == NULL)
318 tmpdir = "/tmp";
319 snprintf(cname, sizeof(cname), "%s/config.tmp.XXXXXX", tmpdir);
320 cfd = mkstemp(cname);
321 if (cfd == -1) {
322 fprintf(stderr, "config: cannot create %s: %s", cname,
323 strerror(errno));
324 exit(2);
325 }
326
327 printf("Using configuration data embedded in kernel...\n");
328 if (!extract_config(conffile, cname, cfd)) {
329 (void)fprintf(stderr,
330 "config: %s does not contain embedded "
331 "configuration data\n", conffile);
332 exit(2);
333 }
334
335 removeit = 1;
336 close(cfd);
337 firstfile(cname);
338 }
339
340 /*
341 * Parse config file (including machine definitions).
342 */
343 logconfig_start();
344 if (yyparse())
345 stop();
346 logconfig_end();
347
348 if (removeit)
349 unlink(cname);
350
351 /*
352 * Detect and properly ignore orphaned devices
353 */
354 kill_orphans();
355
356 /*
357 * Select devices and pseudo devices and their attributes
358 */
359 if (fixdevis())
360 stop();
361
362 /*
363 * Deal with option dependencies.
364 */
365 dependopts();
366
367 /*
368 * Fix (as in `set firmly in place') files.
369 */
370 if (fixfiles())
371 stop();
372
373 /*
374 * Fix objects and libraries.
375 */
376 if (fixobjects())
377 stop();
378
379 /*
380 * Fix device-majors.
381 */
382 if (fixdevsw())
383 stop();
384
385 /*
386 * Perform cross-checking.
387 */
388 if (maxusers == 0) {
389 if (defmaxusers) {
390 (void)printf("maxusers not specified; %d assumed\n",
391 defmaxusers);
392 maxusers = defmaxusers;
393 } else {
394 (void)fprintf(stderr,
395 "config: need \"maxusers\" line\n");
396 errors++;
397 }
398 }
399 if (fsoptions == NULL) {
400 (void)fprintf(stderr,
401 "config: need at least one \"file-system\" line\n");
402 errors++;
403 }
404 if (crosscheck() || errors)
405 stop();
406
407 /*
408 * Squeeze things down and finish cross-checks (STAR checks must
409 * run after packing).
410 */
411 pack();
412 if (badstar())
413 stop();
414
415 /*
416 * Ready to go. Build all the various files.
417 */
418 if (mksymlinks() || mkmakefile() || mkheaders() || mkswap() ||
419 mkioconf() || (do_devsw ? mkdevsw() : 0) || mkident())
420 stop();
421 (void)printf("Build directory is %s\n", builddir);
422 (void)printf("Don't forget to run \"make depend\"\n");
423 exit(0);
424 }
425
426 static void
427 usage(void)
428 {
429 (void)fputs("usage: config [-Ppv] [-s srcdir] [-b builddir] "
430 "[sysname]\n", stderr);
431 (void)fputs(" config -x [kernel-file]\n", stderr);
432 exit(1);
433 }
434
435 /*
436 * Set any options that are implied by other options.
437 */
438 static void
439 dependopts(void)
440 {
441 struct nvlist *nv, *opt;
442
443 for (nv = options; nv != NULL; nv = nv->nv_next) {
444 if ((opt = find_declared_option(nv->nv_name)) != NULL) {
445 for (opt = opt->nv_ptr; opt != NULL;
446 opt = opt->nv_next) {
447 do_depend(opt);
448 }
449 }
450 }
451 }
452
453 static void
454 do_depend(struct nvlist *nv)
455 {
456 struct nvlist *nextnv;
457 struct attr *a;
458
459 if (nv != NULL && (nv->nv_flags & NV_DEPENDED) == 0) {
460 nv->nv_flags |= NV_DEPENDED;
461 /*
462 * If the dependency is an attribute, then just add
463 * it to the selecttab.
464 */
465 if ((a = ht_lookup(attrtab, nv->nv_name)) != NULL) {
466 if (a->a_iattr)
467 panic("do_depend(%s): dep `%s' is an iattr",
468 nv->nv_name, a->a_name);
469 expandattr(a, selectattr);
470 } else {
471 if (ht_lookup(opttab, nv->nv_name) == NULL)
472 addoption(nv->nv_name, NULL);
473 if ((nextnv =
474 find_declared_option(nv->nv_name)) != NULL)
475 do_depend(nextnv->nv_ptr);
476 }
477 }
478 }
479
480 /*
481 * Make a symlink for "machine" so that "#include <machine/foo.h>" works,
482 * and for the machine's CPU architecture, so that works as well.
483 */
484 static int
485 mksymlinks(void)
486 {
487 int ret;
488 char *p, buf[MAXPATHLEN];
489 const char *q;
490 struct nvlist *nv;
491
492 snprintf(buf, sizeof(buf), "arch/%s/include", machine);
493 p = sourcepath(buf);
494 ret = unlink("machine");
495 if (ret && errno != ENOENT)
496 (void)fprintf(stderr, "config: unlink(machine): %s\n",
497 strerror(errno));
498 ret = symlink(p, "machine");
499 if (ret)
500 (void)fprintf(stderr, "config: symlink(machine -> %s): %s\n",
501 p, strerror(errno));
502 free(p);
503
504 if (machinearch != NULL) {
505 snprintf(buf, sizeof(buf), "arch/%s/include", machinearch);
506 p = sourcepath(buf);
507 q = machinearch;
508 } else {
509 p = estrdup("machine");
510 q = machine;
511 }
512 ret = unlink(q);
513 if (ret && errno != ENOENT)
514 (void)fprintf(stderr, "config: unlink(%s): %s\n",
515 q, strerror(errno));
516 ret = symlink(p, q);
517 if (ret)
518 (void)fprintf(stderr, "config: symlink(%s -> %s): %s\n",
519 q, p, strerror(errno));
520 free(p);
521
522 for (nv = machinesubarches; nv != NULL; nv = nv->nv_next) {
523 q = nv->nv_name;
524 snprintf(buf, sizeof(buf), "arch/%s/include", q);
525 p = sourcepath(buf);
526 ret = unlink(q);
527 if (ret && errno != ENOENT)
528 (void)fprintf(stderr, "config: unlink(%s): %s\n",
529 q, strerror(errno));
530 ret = symlink(p, q);
531 if (ret)
532 (void)fprintf(stderr, "config: symlink(%s -> %s): %s\n",
533 q, p, strerror(errno));
534 free(p);
535 }
536
537 return (ret);
538 }
539
540 static __dead void
541 stop(void)
542 {
543 (void)fprintf(stderr, "*** Stop.\n");
544 exit(1);
545 }
546
547 /*
548 * Define one or more file systems. If file system options file name is
549 * specified, a preprocessor #define for that file system will be placed
550 * in that file. In this case, only one file system may be specified.
551 * Otherwise, no preprocessor #defines will be generated.
552 */
553 void
554 deffilesystem(const char *fname, struct nvlist *fses)
555 {
556 struct nvlist *nv;
557
558 /*
559 * Mark these options as ones to skip when creating the Makefile.
560 */
561 for (nv = fses; nv != NULL; nv = nv->nv_next) {
562 if (ht_insert(defopttab, nv->nv_name, nv)) {
563 error("file system or option `%s' already defined",
564 nv->nv_name);
565 return;
566 }
567
568 /*
569 * Also mark it as a valid file system, which may be
570 * used in "file-system" directives in the config
571 * file.
572 */
573 if (ht_insert(deffstab, nv->nv_name, nv))
574 panic("file system `%s' already in table?!",
575 nv->nv_name);
576
577 if (fname != NULL) {
578 /*
579 * Only one file system allowed in this case.
580 */
581 if (nv->nv_next != NULL) {
582 error("only one file system per option "
583 "file may be specified");
584 return;
585 }
586
587 if (ht_insert(optfiletab, fname, nv)) {
588 error("option file `%s' already exists",
589 fname);
590 return;
591 }
592 }
593 }
594 }
595
596 /*
597 * Sanity check a file name.
598 */
599 int
600 badfilename(const char *fname)
601 {
602 const char *n;
603
604 /*
605 * We're putting multiple options into one file. Sanity
606 * check the file name.
607 */
608 if (strchr(fname, '/') != NULL) {
609 error("option file name contains a `/'");
610 return 1;
611 }
612 if ((n = strrchr(fname, '.')) == NULL || strcmp(n, ".h") != 0) {
613 error("option file name does not end in `.h'");
614 return 1;
615 }
616 return 0;
617 }
618
619
620 /*
621 * Search for a defined option (defopt, filesystem, etc), and if found,
622 * return the option's struct nvlist.
623 */
624 struct nvlist *
625 find_declared_option(const char *name)
626 {
627 struct nvlist *option = NULL;
628
629 if ((option = ht_lookup(defopttab, name)) != NULL ||
630 (option = ht_lookup(defparamtab, name)) != NULL ||
631 (option = ht_lookup(defflagtab, name)) != NULL ||
632 (option = ht_lookup(fsopttab, name)) != NULL) {
633 return (option);
634 }
635
636 return (NULL);
637 }
638
639
640 /*
641 * Define one or more standard options. If an option file name is specified,
642 * place all options in one file with the specified name. Otherwise, create
643 * an option file for each option.
644 * record the option information in the specified table.
645 */
646 void
647 defopt(struct hashtab *ht, const char *fname, struct nvlist *opts,
648 struct nvlist *deps, int obs)
649 {
650 struct nvlist *nv, *nextnv, *oldnv, *dep;
651 struct attr *a;
652 const char *name;
653 char buf[500];
654
655 if (fname != NULL && badfilename(fname)) {
656 return;
657 }
658
659 /*
660 * Mark these options as ones to skip when creating the Makefile.
661 */
662 for (nv = opts; nv != NULL; nv = nextnv) {
663 nextnv = nv->nv_next;
664
665 /* An option name can be declared at most once. */
666 if (DEFINED_OPTION(nv->nv_name)) {
667 error("file system or option `%s' already defined",
668 nv->nv_name);
669 return;
670 }
671
672 if (ht_insert(ht, nv->nv_name, nv)) {
673 error("file system or option `%s' already defined",
674 nv->nv_name);
675 return;
676 }
677
678 if (fname == NULL) {
679 /*
680 * Each option will be going into its own file.
681 * Convert the option name to lower case. This
682 * lower case name will be used as the option
683 * file name.
684 */
685 (void) snprintf(buf, sizeof(buf), "opt_%s.h",
686 strtolower(nv->nv_name));
687 name = intern(buf);
688 } else {
689 name = fname;
690 }
691
692 /* Use nv_ptr to link any other options that are implied. */
693 nv->nv_ptr = deps;
694 for (dep = deps; dep != NULL; dep = dep->nv_next) {
695 /*
696 * If the dependency is an attribute, it must not
697 * be an interface attribute. Otherwise, it must
698 * be a previously declared option.
699 */
700 if ((a = ht_lookup(attrtab, dep->nv_name)) != NULL) {
701 if (a->a_iattr)
702 error("option `%s' dependency `%s' "
703 "is an interface attribute",
704 nv->nv_name, a->a_name);
705 } else if (OPT_OBSOLETE(dep->nv_name)) {
706 error("option `%s' dependency `%s' "
707 "is obsolete", nv->nv_name, dep->nv_name);
708 } else if (find_declared_option(dep->nv_name) == NULL) {
709 error("option `%s' dependency `%s' "
710 "is an unknown option",
711 nv->nv_name, dep->nv_name);
712 }
713 }
714
715 /*
716 * Remove this option from the parameter list before adding
717 * it to the list associated with this option file.
718 */
719 nv->nv_next = NULL;
720
721 /*
722 * Flag as obsolete, if requested.
723 */
724 if (obs) {
725 nv->nv_flags |= NV_OBSOLETE;
726 (void)ht_insert(obsopttab, nv->nv_name, nv);
727 }
728
729 /*
730 * Add this option file if we haven't seen it yet.
731 * Otherwise, append to the list of options already
732 * associated with this file.
733 */
734 if ((oldnv = ht_lookup(optfiletab, name)) == NULL) {
735 (void)ht_insert(optfiletab, name, nv);
736 } else {
737 while (oldnv->nv_next != NULL)
738 oldnv = oldnv->nv_next;
739 oldnv->nv_next = nv;
740 }
741 }
742 }
743
744 /*
745 * Define one or more standard options. If an option file name is specified,
746 * place all options in one file with the specified name. Otherwise, create
747 * an option file for each option.
748 */
749 void
750 defoption(const char *fname, struct nvlist *opts, struct nvlist *deps)
751 {
752
753 warn("The use of `defopt' is deprecated");
754 defopt(defopttab, fname, opts, deps, 0);
755 }
756
757
758 /*
759 * Define an option for which a value is required.
760 */
761 void
762 defparam(const char *fname, struct nvlist *opts, struct nvlist *deps, int obs)
763 {
764
765 defopt(defparamtab, fname, opts, deps, obs);
766 }
767
768 /*
769 * Define an option which must not have a value, and which
770 * emits a "needs-flag" style output.
771 */
772 void
773 defflag(const char *fname, struct nvlist *opts, struct nvlist *deps, int obs)
774 {
775
776 defopt(defflagtab, fname, opts, deps, obs);
777 }
778
779
780 /*
781 * Add an option from "options FOO". Note that this selects things that
782 * are "optional foo".
783 */
784 void
785 addoption(const char *name, const char *value)
786 {
787 const char *n;
788 int is_fs, is_param, is_flag, is_opt, is_undecl, is_obs;
789
790 /*
791 * Figure out how this option was declared (if at all.)
792 * XXX should use "params" and "flags" in config.
793 * XXX crying out for a type field in a unified hashtab.
794 */
795 is_fs = OPT_FSOPT(name);
796 is_param = OPT_DEFPARAM(name);
797 is_opt = OPT_DEFOPT(name);
798 is_flag = OPT_DEFFLAG(name);
799 is_obs = OPT_OBSOLETE(name);
800 is_undecl = !DEFINED_OPTION(name);
801
802 /* Warn and pretend the user had not selected the option */
803 if (is_obs) {
804 warn("obsolete option `%s' will be ignored", name);
805 return;
806 }
807
808 /* Make sure this is not a defined file system. */
809 if (is_fs) {
810 error("`%s' is a defined file system", name);
811 return;
812 }
813 /* A defparam must have a value */
814 if (is_param && value == NULL) {
815 error("option `%s' must have a value", name);
816 return;
817 }
818 /* A defflag must not have a value */
819 if (is_flag && value != NULL) {
820 error("option `%s' must not have a value", name);
821 return;
822 }
823
824 if (is_undecl && vflag) {
825 warn("undeclared option `%s' added to IDENT", name);
826 }
827
828 if (do_option(opttab, &nextopt, name, value, "options"))
829 return;
830
831 /* make lowercase, then add to select table */
832 n = strtolower(name);
833 (void)ht_insert(selecttab, n, (void *)n);
834 }
835
836 void
837 deloption(const char *name)
838 {
839
840 if (undo_option(opttab, &options, &nextopt, name, "options"))
841 return;
842 if (undo_option(selecttab, NULL, NULL, strtolower(name), "options"))
843 return;
844 }
845
846 /*
847 * Add a file system option. This routine simply inserts the name into
848 * a list of valid file systems, which is used to validate the root
849 * file system type. The name is then treated like a standard option.
850 */
851 void
852 addfsoption(const char *name)
853 {
854 const char *n;
855
856 /* Make sure this is a defined file system. */
857 if (!OPT_FSOPT(name)) {
858 error("`%s' is not a defined file system", name);
859 return;
860 }
861
862 /*
863 * Convert to lower case. This will be used in the select
864 * table, to verify root file systems, and when the initial
865 * VFS list is created.
866 */
867 n = strtolower(name);
868
869 if (do_option(fsopttab, &nextfsopt, name, n, "file-system"))
870 return;
871
872 /*
873 * Add a lower-case version to the table for root file system
874 * verification.
875 */
876 if (ht_insert(fsopttab, n, (void *)n))
877 panic("addfsoption: already in table");
878
879 /* Add to select table. */
880 (void)ht_insert(selecttab, n, (void *)n);
881 }
882
883 void
884 delfsoption(const char *name)
885 {
886 const char *n;
887
888 n = strtolower(name);
889 if (undo_option(fsopttab, &fsoptions, &nextfsopt, name, "file-system"))
890 return;
891 if (undo_option(fsopttab, NULL, NULL, n, "file-system"))
892 return;
893 if (undo_option(selecttab, NULL, NULL, n, "file-system"))
894 return;
895 }
896
897 /*
898 * Add a "make" option.
899 */
900 void
901 addmkoption(const char *name, const char *value)
902 {
903
904 (void)do_option(mkopttab, &nextmkopt, name, value, "makeoptions");
905 }
906
907 void
908 delmkoption(const char *name)
909 {
910
911 (void)undo_option(mkopttab, &mkoptions, &nextmkopt, name,
912 "makeoptions");
913 }
914
915 /*
916 * Add an appending "make" option.
917 */
918 void
919 appendmkoption(const char *name, const char *value)
920 {
921 struct nvlist *nv;
922
923 nv = newnv(name, value, NULL, 0, NULL);
924 *nextappmkopt = nv;
925 nextappmkopt = &nv->nv_next;
926 }
927
928 /*
929 * Add a conditional appending "make" option.
930 */
931 void
932 appendcondmkoption(const char *selname, const char *name, const char *value)
933 {
934 struct nvlist *nv, *lnv;
935 const char *n;
936
937 n = strtolower(selname);
938 nv = newnv(name, value, NULL, 0, NULL);
939 if (ht_insert(condmkopttab, n, nv) == 0)
940 return;
941
942 if ((lnv = ht_lookup(condmkopttab, n)) == NULL)
943 panic("appendcondmkoption");
944 for (; lnv->nv_next != NULL; lnv = lnv->nv_next)
945 /* search for the last list element */;
946 lnv->nv_next = nv;
947 }
948
949 /*
950 * Add a name=value pair to an option list. The value may be NULL.
951 */
952 static int
953 do_option(struct hashtab *ht, struct nvlist ***nppp, const char *name,
954 const char *value, const char *type)
955 {
956 struct nvlist *nv;
957
958 /*
959 * If a defopt'ed or defflag'ed option was enabled but without
960 * an explicit value (always the case for defflag), supply a
961 * default value of 1, as for non-defopt options (where cc
962 * treats -DBAR as -DBAR=1.)
963 */
964 if ((OPT_DEFOPT(name) || OPT_DEFFLAG(name)) && value == NULL)
965 value = "1";
966
967 /* assume it will work */
968 nv = newnv(name, value, NULL, 0, NULL);
969 if (ht_insert(ht, name, nv) == 0) {
970 **nppp = nv;
971 *nppp = &nv->nv_next;
972 return (0);
973 }
974
975 /* oops, already got that option */
976 nvfree(nv);
977 if ((nv = ht_lookup(ht, name)) == NULL)
978 panic("do_option");
979 if (nv->nv_str != NULL && !OPT_FSOPT(name))
980 error("already have %s `%s=%s'", type, name, nv->nv_str);
981 else
982 error("already have %s `%s'", type, name);
983 return (1);
984 }
985
986 /*
987 * Remove a name from a hash table,
988 * and optionally, a name=value pair from an option list.
989 */
990 static int
991 undo_option(struct hashtab *ht, struct nvlist **npp,
992 struct nvlist ***next, const char *name, const char *type)
993 {
994 struct nvlist *nv;
995
996 if (ht_remove(ht, name)) {
997 error("%s `%s' is not defined", type, name);
998 return (1);
999 }
1000 if (npp == NULL)
1001 return (0);
1002
1003 for ( ; *npp != NULL; npp = &(*npp)->nv_next) {
1004 if ((*npp)->nv_name != name)
1005 continue;
1006 if (next != NULL && *next == &(*npp)->nv_next)
1007 *next = npp;
1008 nv = (*npp)->nv_next;
1009 nvfree(*npp);
1010 *npp = nv;
1011 return (0);
1012 }
1013 panic("%s `%s' is not defined in nvlist", type, name);
1014 return (1);
1015 }
1016
1017 /*
1018 * Return true if there is at least one instance of the given unit
1019 * on the given device attachment (or any units, if unit == WILD).
1020 */
1021 int
1022 deva_has_instances(struct deva *deva, int unit)
1023 {
1024 struct devi *i;
1025
1026 for (i = deva->d_ihead; i != NULL; i = i->i_asame)
1027 if (i->i_active == DEVI_ACTIVE &&
1028 (unit == WILD || unit == i->i_unit || i->i_unit == STAR))
1029 return (1);
1030 return (0);
1031 }
1032
1033 /*
1034 * Return true if there is at least one instance of the given unit
1035 * on the given base (or any units, if unit == WILD).
1036 */
1037 int
1038 devbase_has_instances(struct devbase *dev, int unit)
1039 {
1040 struct deva *da;
1041
1042 /*
1043 * Pseudo-devices are a little special. We consider them
1044 * to have instances only if they are both:
1045 *
1046 * 1. Included in this kernel configuration.
1047 *
1048 * 2. Have one or more interface attributes.
1049 */
1050 if (dev->d_ispseudo) {
1051 struct nvlist *nv;
1052 struct attr *a;
1053
1054 if (ht_lookup(devitab, dev->d_name) == NULL)
1055 return (0);
1056
1057 for (nv = dev->d_attrs; nv != NULL; nv = nv->nv_next) {
1058 a = nv->nv_ptr;
1059 if (a->a_iattr)
1060 return (1);
1061 }
1062 return (0);
1063 }
1064
1065 for (da = dev->d_ahead; da != NULL; da = da->d_bsame)
1066 if (deva_has_instances(da, unit))
1067 return (1);
1068 return (0);
1069 }
1070
1071 static int
1072 cfcrosscheck(struct config *cf, const char *what, struct nvlist *nv)
1073 {
1074 struct devbase *dev;
1075 struct devi *pd;
1076 int errs, devunit;
1077
1078 if (maxpartitions <= 0)
1079 panic("cfcrosscheck");
1080
1081 for (errs = 0; nv != NULL; nv = nv->nv_next) {
1082 if (nv->nv_name == NULL)
1083 continue;
1084 dev = ht_lookup(devbasetab, nv->nv_name);
1085 if (dev == NULL)
1086 panic("cfcrosscheck(%s)", nv->nv_name);
1087 if (has_attr(dev->d_attrs, s_ifnet))
1088 devunit = nv->nv_ifunit; /* XXX XXX XXX */
1089 else
1090 devunit = minor(nv->nv_int) / maxpartitions;
1091 if (devbase_has_instances(dev, devunit))
1092 continue;
1093 if (devbase_has_instances(dev, STAR) &&
1094 devunit >= dev->d_umax)
1095 continue;
1096 TAILQ_FOREACH(pd, &allpseudo, i_next) {
1097 if (pd->i_base == dev && devunit < dev->d_umax &&
1098 devunit >= 0)
1099 goto loop;
1100 }
1101 (void)fprintf(stderr,
1102 "%s:%d: %s says %s on %s, but there's no %s\n",
1103 conffile, cf->cf_lineno,
1104 cf->cf_name, what, nv->nv_str, nv->nv_str);
1105 errs++;
1106 loop:
1107 ;
1108 }
1109 return (errs);
1110 }
1111
1112 /*
1113 * Cross-check the configuration: make sure that each target device
1114 * or attribute (`at foo[0*?]') names at least one real device. Also
1115 * see that the root and dump devices for all configurations are there.
1116 */
1117 int
1118 crosscheck(void)
1119 {
1120 struct config *cf;
1121 int errs;
1122
1123 errs = 0;
1124 if (TAILQ_EMPTY(&allcf)) {
1125 (void)fprintf(stderr, "%s has no configurations!\n",
1126 conffile);
1127 errs++;
1128 }
1129 TAILQ_FOREACH(cf, &allcf, cf_next) {
1130 if (cf->cf_root != NULL) { /* i.e., not root on ? */
1131 errs += cfcrosscheck(cf, "root", cf->cf_root);
1132 errs += cfcrosscheck(cf, "dumps", cf->cf_dump);
1133 }
1134 }
1135 return (errs);
1136 }
1137
1138 /*
1139 * Check to see if there is a *'d unit with a needs-count file.
1140 */
1141 int
1142 badstar(void)
1143 {
1144 struct devbase *d;
1145 struct deva *da;
1146 struct devi *i;
1147 int errs, n;
1148
1149 errs = 0;
1150 TAILQ_FOREACH(d, &allbases, d_next) {
1151 for (da = d->d_ahead; da != NULL; da = da->d_bsame)
1152 for (i = da->d_ihead; i != NULL; i = i->i_asame) {
1153 if (i->i_unit == STAR)
1154 goto aybabtu;
1155 }
1156 continue;
1157 aybabtu:
1158 if (ht_lookup(needcnttab, d->d_name)) {
1159 (void)fprintf(stderr,
1160 "config: %s's cannot be *'d until its driver is fixed\n",
1161 d->d_name);
1162 errs++;
1163 continue;
1164 }
1165 for (n = 0; i != NULL; i = i->i_alias)
1166 if (!i->i_collapsed)
1167 n++;
1168 if (n < 1)
1169 panic("badstar() n<1");
1170 }
1171 return (errs);
1172 }
1173
1174 /*
1175 * Verify/create builddir if necessary, change to it, and verify srcdir.
1176 * This will be called when we see the first include.
1177 */
1178 void
1179 setupdirs(void)
1180 {
1181 struct stat st;
1182
1183 /* srcdir must be specified if builddir is not specified or if
1184 * no configuration filename was specified. */
1185 if ((builddir || strcmp(defbuilddir, ".") == 0) && !srcdir) {
1186 error("source directory must be specified");
1187 exit(1);
1188 }
1189
1190 if (srcdir == NULL)
1191 srcdir = "../../../..";
1192 if (builddir == NULL)
1193 builddir = defbuilddir;
1194
1195 if (stat(builddir, &st) != 0) {
1196 if (mkdir(builddir, 0777)) {
1197 (void)fprintf(stderr, "config: cannot create %s: %s\n",
1198 builddir, strerror(errno));
1199 exit(2);
1200 }
1201 } else if (!S_ISDIR(st.st_mode)) {
1202 (void)fprintf(stderr, "config: %s is not a directory\n",
1203 builddir);
1204 exit(2);
1205 }
1206 if (chdir(builddir) != 0) {
1207 (void)fprintf(stderr, "config: cannot change to %s\n",
1208 builddir);
1209 exit(2);
1210 }
1211 if (stat(srcdir, &st) != 0 || !S_ISDIR(st.st_mode)) {
1212 (void)fprintf(stderr, "config: %s is not a directory\n",
1213 srcdir);
1214 exit(2);
1215 }
1216 }
1217
1218 /*
1219 * Write identifier from "ident" directive into file, for
1220 * newvers.sh to pick it up.
1221 */
1222 int
1223 mkident(void)
1224 {
1225 FILE *fp;
1226 int error = 0;
1227
1228 (void)unlink("ident");
1229
1230 if (ident == NULL)
1231 return (0);
1232
1233 if ((fp = fopen("ident", "w")) == NULL) {
1234 (void)fprintf(stderr, "config: cannot write ident: %s\n",
1235 strerror(errno));
1236 return (1);
1237 }
1238 if (vflag)
1239 (void)printf("using ident '%s'\n", ident);
1240 if (fprintf(fp, "%s\n", ident) < 0)
1241 error = 1;
1242 (void)fclose(fp);
1243
1244 return error;
1245 }
1246
1247 void
1248 logconfig_start(void)
1249 {
1250 extern FILE *yyin;
1251 char line[1024];
1252 const char *tmpdir;
1253 struct stat st;
1254 int fd;
1255
1256 if (yyin == NULL || fstat(fileno(yyin), &st) == -1)
1257 return;
1258 cfgtime = st.st_mtime;
1259
1260 tmpdir = getenv("TMPDIR");
1261 if (tmpdir == NULL)
1262 tmpdir = "/tmp";
1263 snprintf(line, sizeof(line), "%s/config.tmp.XXXXXX", tmpdir);
1264 if ((fd = mkstemp(line)) == -1 ||
1265 (cfg = fdopen(fd, "r+")) == NULL) {
1266 if (fd != -1) {
1267 unlink(line);
1268 close(fd);
1269 }
1270 cfg = NULL;
1271 return;
1272 }
1273 unlink(line);
1274
1275 (void)fprintf(cfg, "#include \"opt_config.h\"\n");
1276 (void)fprintf(cfg, "\n");
1277 (void)fprintf(cfg, "/*\n");
1278 (void)fprintf(cfg, " * Add either (or both) of\n");
1279 (void)fprintf(cfg, " *\n");
1280 (void)fprintf(cfg, " *\toptions %s\n", LOGCONFIG_LARGE);
1281 (void)fprintf(cfg, " *\toptions %s\n", LOGCONFIG_SMALL);
1282 (void)fprintf(cfg, " *\n");
1283 (void)fprintf(cfg,
1284 " * to your kernel config file to embed it in the resulting\n");
1285 (void)fprintf(cfg,
1286 " * kernel. The latter option does not include files that are\n");
1287 (void)fprintf(cfg,
1288 " * included (recursively) by your config file. The embedded\n");
1289 (void)fprintf(cfg,
1290 " * data be extracted by using the command:\n");
1291 (void)fprintf(cfg, " *\n");
1292 (void)fprintf(cfg,
1293 " *\tstrings netbsd | sed -n 's/^_CFG_//p' | unvis\n");
1294 (void)fprintf(cfg, " */\n");
1295 (void)fprintf(cfg, "\n");
1296 (void)fprintf(cfg, "#ifdef CONFIG_FILE\n");
1297 (void)fprintf(cfg, "#if defined(%s) || defined(%s)\n\n",
1298 LOGCONFIG_LARGE, LOGCONFIG_SMALL);
1299 (void)fprintf(cfg,
1300 "static const char config[]\n#ifdef __vax__\n"
1301 "\t__attribute__((__unused__))\n#else\n"
1302 "\t__attribute__((__used__))\n#endif\n\t=\n\n");
1303
1304 (void)fprintf(cfg, "#ifdef %s\n\n", LOGCONFIG_LARGE);
1305 (void)fprintf(cfg, "\"_CFG_### START CONFIG FILE \\\"%s\\\"\\n\"\n\n",
1306 conffile);
1307 (void)fprintf(cfg, "#endif /* %s */\n\n", LOGCONFIG_LARGE);
1308
1309 logconfig_include(yyin, NULL);
1310
1311 (void)fprintf(cfg, "#ifdef %s\n\n", LOGCONFIG_LARGE);
1312 (void)fprintf(cfg, "\"_CFG_### END CONFIG FILE \\\"%s\\\"\\n\"\n",
1313 conffile);
1314
1315 rewind(yyin);
1316 }
1317
1318 void
1319 logconfig_include(FILE *cf, const char *filename)
1320 {
1321 char line[1024], in[2048], *out;
1322 struct stat st;
1323 int missingeol;
1324
1325 if (!cfg)
1326 return;
1327
1328 missingeol = 0;
1329 if (fstat(fileno(cf), &st) == -1)
1330 return;
1331 if (cfgtime < st.st_mtime)
1332 cfgtime = st.st_mtime;
1333
1334 if (filename)
1335 (void)fprintf(cfg,
1336 "\"_CFG_### (included from \\\"%s\\\")\\n\"\n",
1337 filename);
1338 while (fgets(line, sizeof(line), cf) != NULL) {
1339 missingeol = 1;
1340 (void)fprintf(cfg, "\"_CFG_");
1341 if (filename)
1342 (void)fprintf(cfg, "###> ");
1343 strvis(in, line, VIS_TAB);
1344 for (out = in; *out; out++)
1345 switch (*out) {
1346 case '\n':
1347 (void)fprintf(cfg, "\\n\"\n");
1348 missingeol = 0;
1349 break;
1350 case '"': case '\\':
1351 (void)fputc('\\', cfg);
1352 /* FALLTHROUGH */
1353 default:
1354 (void)fputc(*out, cfg);
1355 break;
1356 }
1357 }
1358 if (missingeol) {
1359 (void)fprintf(cfg, "\\n\"\n");
1360 (void)fprintf(stderr,
1361 "config: %s: newline missing at EOF\n",
1362 filename != NULL ? filename : conffile);
1363 }
1364 if (filename)
1365 (void)fprintf(cfg, "\"_CFG_### (end include \\\"%s\\\")\\n\"\n",
1366 filename);
1367
1368 rewind(cf);
1369 }
1370
1371 void
1372 logconfig_end(void)
1373 {
1374 char line[1024];
1375 FILE *fp;
1376 struct stat st;
1377
1378 if (!cfg)
1379 return;
1380
1381 (void)fprintf(cfg, "#endif /* %s */\n", LOGCONFIG_LARGE);
1382 (void)fprintf(cfg, ";\n");
1383 (void)fprintf(cfg, "#endif /* %s || %s */\n",
1384 LOGCONFIG_LARGE, LOGCONFIG_SMALL);
1385 (void)fprintf(cfg, "#endif /* CONFIG_FILE */\n");
1386 rewind(cfg);
1387
1388 if (stat("config_file.h", &st) != -1) {
1389 if (cfgtime < st.st_mtime) {
1390 fclose(cfg);
1391 return;
1392 }
1393 }
1394
1395 fp = fopen("config_file.h", "w");
1396 if(!fp) {
1397 (void)fprintf(stderr,
1398 "config: cannot write to \"config_file.h\"\n");
1399 exit(1);
1400 }
1401
1402 while (fgets(line, sizeof(line), cfg) != NULL)
1403 fputs(line, fp);
1404 fclose(fp);
1405 fclose(cfg);
1406 }
1407
1408 static const char *
1409 strtolower(const char *name)
1410 {
1411 const char *n;
1412 char *p, low[500];
1413 unsigned char c;
1414
1415 for (n = name, p = low; (c = *n) != '\0'; n++)
1416 *p++ = isupper(c) ? tolower(c) : c;
1417 *p = 0;
1418 return (intern(low));
1419 }
1420
1421 static int
1422 is_elf(const char *file)
1423 {
1424 int kernel;
1425 char hdr[4];
1426
1427 kernel = open(file, O_RDONLY);
1428 if (kernel == -1) {
1429 fprintf(stderr, "config: cannot open %s: %s\n", file,
1430 strerror(errno));
1431 exit(2);
1432 }
1433 if (read(kernel, hdr, 4) == -1) {
1434 fprintf(stderr, "config: cannot read from %s: %s\n", file,
1435 strerror(errno));
1436 exit(2);
1437 }
1438 close(kernel);
1439
1440 return memcmp("\177ELF", hdr, 4) == 0 ? 1 : 0;
1441 }
1442
1443 static int
1444 extract_config(const char *kname, const char *cname, int cfd)
1445 {
1446 char *ptr;
1447 int found, kfd, i;
1448 struct stat st;
1449
1450 found = 0;
1451
1452 /* mmap(2) binary kernel */
1453 kfd = open(conffile, O_RDONLY);
1454 if (kfd == -1) {
1455 fprintf(stderr, "config: cannot open %s: %s\n", kname,
1456 strerror(errno));
1457 exit(2);
1458 }
1459 if ((fstat(kfd, &st) == -1)) {
1460 fprintf(stderr, "config: cannot stat %s: %s\n", kname,
1461 strerror(errno));
1462 exit(2);
1463 }
1464 ptr = (char *)mmap(0, st.st_size, PROT_READ, MAP_FILE | MAP_SHARED,
1465 kfd, 0);
1466 if (ptr == MAP_FAILED) {
1467 fprintf(stderr, "config: cannot mmap %s: %s\n", kname,
1468 strerror(errno));
1469 exit(2);
1470 }
1471
1472 /* Scan mmap(2)'ed region, extracting kernel configuration */
1473 for (i = 0; i < st.st_size; i++) {
1474 if ((*ptr == '_') && (st.st_size - i > 5) && memcmp(ptr,
1475 "_CFG_", 5) == 0) {
1476 /* Line found */
1477 char *oldptr, line[LINE_MAX + 1], uline[LINE_MAX + 1];
1478 int j;
1479
1480 found = 1;
1481
1482 oldptr = (ptr += 5);
1483 while (*ptr != '\n' && *ptr != '\0') ptr++;
1484 if (ptr - oldptr > LINE_MAX) {
1485 fprintf(stderr, "config: line too long\n");
1486 exit(2);
1487 }
1488 i += ptr - oldptr + 5;
1489 memcpy(line, oldptr, (ptr - oldptr));
1490 line[ptr - oldptr] = '\0';
1491 j = strunvis(uline, line);
1492 if (j == -1) {
1493 fprintf(stderr, "config: unvis: invalid "
1494 "encoded sequence\n");
1495 exit(2);
1496 }
1497 uline[j] = '\n';
1498 if (write(cfd, uline, j + 1) == -1) {
1499 fprintf(stderr, "config: cannot write to %s: "
1500 "%s\n", cname, strerror(errno));
1501 exit(2);
1502 }
1503 } else ptr++;
1504 }
1505
1506 close(kfd);
1507
1508 return found;
1509 }
1510
1511 struct dhdi_params {
1512 struct devbase *d;
1513 int unit;
1514 int level;
1515 };
1516
1517 static int
1518 devbase_has_dead_instances(const char *key, void *value, void *aux)
1519 {
1520 struct devi *i;
1521 struct dhdi_params *dhdi = aux;
1522
1523 for (i = value; i != NULL; i = i->i_alias)
1524 if (i->i_base == dhdi->d &&
1525 (dhdi->unit == WILD || dhdi->unit == i->i_unit ||
1526 i->i_unit == STAR) &&
1527 i->i_level >= dhdi->level)
1528 return 1;
1529 return 0;
1530 }
1531
1532 /*
1533 * This is almost the same as devbase_has_instances, except it
1534 * may have special considerations regarding ignored instances.
1535 */
1536
1537 static int
1538 devbase_has_any_instance(struct devbase *dev, int unit, int state, int level)
1539 {
1540 struct deva *da;
1541 struct devi *i;
1542
1543 if (dev->d_ispseudo) {
1544 if (dev->d_ihead != NULL)
1545 return 1;
1546 else if (state != DEVI_IGNORED)
1547 return 0;
1548 if ((i = ht_lookup(deaddevitab, dev->d_name)) == NULL)
1549 return 0;
1550 return (i->i_level >= level);
1551 }
1552
1553 for (da = dev->d_ahead; da != NULL; da = da->d_bsame)
1554 for (i = da->d_ihead; i != NULL; i = i->i_asame)
1555 if ((i->i_active == DEVI_ACTIVE ||
1556 i->i_active == state) &&
1557 (unit == WILD || unit == i->i_unit ||
1558 i->i_unit == STAR))
1559 return 1;
1560
1561 if (state == DEVI_IGNORED) {
1562 struct dhdi_params dhdi = { dev, unit, level };
1563 /* also check dead devices */
1564 return ht_enumerate(deaddevitab, devbase_has_dead_instances,
1565 &dhdi);
1566 }
1567
1568 return 0;
1569 }
1570
1571 /*
1572 * check_dead_devi(), used with ht_enumerate, checks if any of the removed
1573 * device instances would have been a valid instance considering the devbase,
1574 * the parent device and the interface attribute.
1575 *
1576 * In other words, for a non-active device, it checks if children would be
1577 * actual orphans or the result of a negative statement in the config file.
1578 */
1579
1580 struct cdd_params {
1581 struct devbase *d;
1582 struct attr *at;
1583 struct devbase *parent;
1584 };
1585
1586 static int
1587 check_dead_devi(const char *key, void *value, void *aux)
1588 {
1589 struct cdd_params *cdd = aux;
1590 struct devi *i = value;
1591 struct pspec *p;
1592
1593 if (i->i_base != cdd->d)
1594 return 0;
1595
1596 for (; i != NULL; i = i->i_alias) {
1597 p = i->i_pspec;
1598 if ((p == NULL && cdd->at == NULL) ||
1599 (p != NULL && p->p_iattr == cdd->at &&
1600 (p->p_atdev == NULL || p->p_atdev == cdd->parent))) {
1601 if (p != NULL &&
1602 !devbase_has_any_instance(cdd->parent, p->p_atunit,
1603 DEVI_IGNORED, i->i_level))
1604 return 0;
1605 else
1606 return 1;
1607 }
1608 }
1609 return 0;
1610 }
1611
1612 static void
1613 do_kill_orphans(struct devbase *d, struct attr *at, struct devbase *parent,
1614 int state)
1615 {
1616 struct nvlist *nv, *nv1;
1617 struct attr *a;
1618 struct devi *i, *j = NULL;
1619 struct pspec *p;
1620 int active = 0;
1621
1622 /*
1623 * A pseudo-device will always attach at root, and if it has an
1624 * instance (it cannot have more than one), it is enough to consider
1625 * it active, as there is no real attachment.
1626 *
1627 * A pseudo device can never be marked DEVI_IGNORED.
1628 */
1629 if (d->d_ispseudo) {
1630 if (d->d_ihead != NULL)
1631 d->d_ihead->i_active = active = DEVI_ACTIVE;
1632 else {
1633 if (ht_lookup(deaddevitab, d->d_name) != NULL)
1634 active = DEVI_IGNORED;
1635 else
1636 return;
1637 }
1638 } else {
1639 int seen = 0;
1640
1641 for (i = d->d_ihead; i != NULL; i = i->i_bsame) {
1642 for (j = i; j != NULL; j = j->i_alias) {
1643 p = j->i_pspec;
1644 if ((p == NULL && at == NULL) ||
1645 (p != NULL && p->p_iattr == at &&
1646 (p->p_atdev == NULL ||
1647 p->p_atdev == parent))) {
1648 if (p != NULL &&
1649 !devbase_has_any_instance(parent,
1650 p->p_atunit, state, j->i_level))
1651 continue;
1652 /*
1653 * There are Fry-like devices which can
1654 * be their own grand-parent (or even
1655 * parent, like uhub). We don't want
1656 * to loop, so if we've already reached
1657 * an instance for one reason or
1658 * another, stop there.
1659 */
1660 if (j->i_active == DEVI_ACTIVE ||
1661 j->i_active == state) {
1662 /*
1663 * Device has already been
1664 * seen. However it might
1665 * have siblings who still
1666 * have to be activated or
1667 * orphaned.
1668 */
1669 seen = 1;
1670 continue;
1671 }
1672 j->i_active = active = state;
1673 if (p != NULL)
1674 p->p_active = state;
1675 }
1676 }
1677 }
1678 /*
1679 * If we've been there but have made no change, stop.
1680 */
1681 if (seen && !active)
1682 return;
1683 if (!active) {
1684 struct cdd_params cdd = { d, at, parent };
1685 /* Look for a matching dead devi */
1686 if (ht_enumerate(deaddevitab, check_dead_devi, &cdd) &&
1687 d != parent)
1688 /*
1689 * That device had its instances removed.
1690 * Continue the loop marking descendants
1691 * with DEVI_IGNORED instead of DEVI_ACTIVE.
1692 *
1693 * There is one special case for devices that
1694 * are their own parent: if that instance is
1695 * removed (e.g., no uhub* at uhub?), we don't
1696 * have to continue looping.
1697 */
1698 active = DEVI_IGNORED;
1699 else
1700 return;
1701 }
1702 }
1703
1704 for (nv = d->d_attrs; nv != NULL; nv = nv->nv_next) {
1705 a = nv->nv_ptr;
1706 for (nv1 = a->a_devs; nv1 != NULL; nv1 = nv1->nv_next)
1707 do_kill_orphans(nv1->nv_ptr, a, d, active);
1708 }
1709 }
1710
1711 static int
1712 kill_orphans_cb(const char *key, void *value, void *aux)
1713 {
1714 do_kill_orphans((struct devbase *)value, NULL, NULL, DEVI_ACTIVE);
1715 return 0;
1716 }
1717
1718 static void
1719 kill_orphans()
1720 {
1721 ht_enumerate(devroottab, kill_orphans_cb, NULL);
1722 }
1723