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