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