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