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