xref: /freebsd/usr.sbin/config/main.cc (revision 5e3190f700637fcfc1a52daeaa4a031fdd2557c7)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1980, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef lint
33 static const char copyright[] =
34 "@(#) Copyright (c) 1980, 1993\n\
35 	The Regents of the University of California.  All rights reserved.\n";
36 #endif /* not lint */
37 
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)main.c	8.1 (Berkeley) 6/6/93";
41 #endif
42 #endif /* not lint */
43 
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <sys/file.h>
47 #include <sys/mman.h>
48 #include <sys/param.h>
49 
50 #include <assert.h>
51 #include <ctype.h>
52 #include <dirent.h>
53 #include <err.h>
54 #include <iostream>
55 #include <sstream>
56 #include <stdio.h>
57 #include <string.h>
58 #include <sysexits.h>
59 #include <unistd.h>
60 
61 #include "y.tab.h"
62 #include "config.h"
63 #include "configvers.h"
64 
65 #ifndef TRUE
66 #define TRUE	(1)
67 #endif
68 
69 #ifndef FALSE
70 #define FALSE	(0)
71 #endif
72 
73 #define	CDIR	"../compile/"
74 
75 char	*machinename;
76 char	*machinearch;
77 
78 struct cfgfile_head	cfgfiles;
79 struct cputype_head	cputype;
80 struct opt_head		opt, mkopt, rmopts;
81 struct opt_list_head	otab;
82 struct envvar_head	envvars;
83 struct hint_head	hints;
84 struct includepath_head	includepath;
85 
86 char *	PREFIX;
87 char 	destdir[MAXPATHLEN];
88 char 	srcdir[MAXPATHLEN];
89 
90 int	debugging;
91 int	found_defaults;
92 int	incignore;
93 
94 /*
95  * Preserve old behaviour in INCLUDE_CONFIG_FILE handling (files are included
96  * literally).
97  */
98 int	filebased = 0;
99 int	versreq;
100 
101 static void configfile(void);
102 static void get_srcdir(void);
103 static void usage(void);
104 static void cleanheaders(char *);
105 static void kernconfdump(const char *);
106 static void badversion(void);
107 static void checkversion(void);
108 
109 struct hdr_list {
110 	const char *h_name;
111 	struct hdr_list *h_next;
112 } *htab;
113 
114 static std::stringstream line_buf;
115 
116 /*
117  * Config builds a set of files for building a UNIX
118  * system given a description of the desired system.
119  */
120 int
121 main(int argc, char **argv)
122 {
123 
124 	struct stat buf;
125 	int ch, len;
126 	char *p;
127 	char *kernfile;
128 	struct includepath* ipath;
129 	int printmachine;
130 	bool cust_dest = false;
131 
132 	printmachine = 0;
133 	kernfile = NULL;
134 	SLIST_INIT(&includepath);
135 	SLIST_INIT(&cputype);
136 	SLIST_INIT(&mkopt);
137 	SLIST_INIT(&opt);
138 	SLIST_INIT(&rmopts);
139 	STAILQ_INIT(&cfgfiles);
140 	STAILQ_INIT(&dtab);
141 	STAILQ_INIT(&fntab);
142 	STAILQ_INIT(&ftab);
143 	STAILQ_INIT(&hints);
144 	STAILQ_INIT(&envvars);
145 	while ((ch = getopt(argc, argv, "Cd:gI:mps:Vx:")) != -1)
146 		switch (ch) {
147 		case 'C':
148 			filebased = 1;
149 			break;
150 		case 'd':
151 			if (*destdir == '\0')
152 				strlcpy(destdir, optarg, sizeof(destdir));
153 			else
154 				errx(EXIT_FAILURE, "directory already set");
155 			cust_dest = true;
156 			break;
157 		case 'g':
158 			debugging++;
159 			break;
160 		case 'I':
161 			ipath = (struct includepath *) \
162 			    	calloc(1, sizeof (struct includepath));
163 			if (ipath == NULL)
164 				err(EXIT_FAILURE, "calloc");
165 			ipath->path = optarg;
166 			SLIST_INSERT_HEAD(&includepath, ipath, path_next);
167 			break;
168 		case 'm':
169 			printmachine = 1;
170 			break;
171 		case 's':
172 			if (*srcdir == '\0')
173 				strlcpy(srcdir, optarg, sizeof(srcdir));
174 			else
175 				errx(EXIT_FAILURE, "src directory already set");
176 			break;
177 		case 'V':
178 			printf("%d\n", CONFIGVERS);
179 			exit(0);
180 		case 'x':
181 			kernfile = optarg;
182 			break;
183 		case '?':
184 		default:
185 			usage();
186 		}
187 	argc -= optind;
188 	argv += optind;
189 
190 	if (kernfile != NULL) {
191 		kernconfdump(kernfile);
192 		exit(EXIT_SUCCESS);
193 	}
194 
195 	if (argc != 1)
196 		usage();
197 
198 	PREFIX = *argv;
199 	if (stat(PREFIX, &buf) != 0 || !S_ISREG(buf.st_mode))
200 		err(2, "%s", PREFIX);
201 	if (freopen("DEFAULTS", "r", stdin) != NULL) {
202 		found_defaults = 1;
203 		yyfile = "DEFAULTS";
204 	} else {
205 		if (freopen(PREFIX, "r", stdin) == NULL)
206 			err(2, "%s", PREFIX);
207 		yyfile = PREFIX;
208 	}
209 	if (*destdir != '\0') {
210 		len = strlen(destdir);
211 		while (len > 1 && destdir[len - 1] == '/')
212 			destdir[--len] = '\0';
213 		if (*srcdir == '\0')
214 			get_srcdir();
215 	} else {
216 		strlcpy(destdir, CDIR, sizeof(destdir));
217 		strlcat(destdir, PREFIX, sizeof(destdir));
218 	}
219 
220 	if (yyparse())
221 		exit(3);
222 
223 	/*
224 	 * Ensure that required elements (machine, cpu, ident) are present.
225 	 */
226 	if (machinename == NULL) {
227 		printf("Specify machine type, e.g. ``machine i386''\n");
228 		exit(1);
229 	}
230 	if (ident == NULL) {
231 		printf("no ident line specified\n");
232 		exit(1);
233 	}
234 	if (SLIST_EMPTY(&cputype)) {
235 		printf("cpu type must be specified\n");
236 		exit(1);
237 	}
238 	checkversion();
239 
240 	if (printmachine) {
241 		printf("%s\t%s\n",machinename,machinearch);
242 		exit(0);
243 	}
244 
245 	/*
246 	 * Make CDIR directory, if doing a default destination. Some version
247 	 * control systems delete empty directories and this seemlessly copes.
248 	 */
249 	if (!cust_dest && stat(CDIR, &buf))
250 		if (mkdir(CDIR, 0777))
251 			err(2, "%s", CDIR);
252 	/* Create the compile directory */
253 	p = path((char *)NULL);
254 	if (stat(p, &buf)) {
255 		if (mkdir(p, 0777))
256 			err(2, "%s", p);
257 	} else if (!S_ISDIR(buf.st_mode))
258 		errx(EXIT_FAILURE, "%s isn't a directory", p);
259 
260 	configfile();			/* put config file into kernel*/
261 	options();			/* make options .h files */
262 	makefile();			/* build Makefile */
263 	makeenv();			/* build env.c */
264 	makehints();			/* build hints.c */
265 	headers();			/* make a lot of .h files */
266 	cleanheaders(p);
267 	printf("Kernel build directory is %s\n", p);
268 	printf("Don't forget to do ``make cleandepend && make depend''\n");
269 	exit(0);
270 }
271 
272 /*
273  * get_srcdir
274  *	determine the root of the kernel source tree
275  *	and save that in srcdir.
276  */
277 static void
278 get_srcdir(void)
279 {
280 	struct stat lg, phy;
281 	char *p, *pwd;
282 	int i;
283 
284 	if (realpath("../..", srcdir) == NULL)
285 		err(EXIT_FAILURE, "Unable to find root of source tree");
286 	if ((pwd = getenv("PWD")) != NULL && *pwd == '/' &&
287 	    (pwd = strdup(pwd)) != NULL) {
288 		/* Remove the last two path components. */
289 		for (i = 0; i < 2; i++) {
290 			if ((p = strrchr(pwd, '/')) == NULL) {
291 				free(pwd);
292 				return;
293 			}
294 			*p = '\0';
295 		}
296 		if (stat(pwd, &lg) != -1 && stat(srcdir, &phy) != -1 &&
297 		    lg.st_dev == phy.st_dev && lg.st_ino == phy.st_ino)
298 			strlcpy(srcdir, pwd, MAXPATHLEN);
299 		free(pwd);
300 	}
301 }
302 
303 static void
304 usage(void)
305 {
306 
307 	fprintf(stderr,
308 	    "usage: config [-CgmpV] [-d destdir] [-s srcdir] sysname\n");
309 	fprintf(stderr, "       config -x kernel\n");
310 	exit(EX_USAGE);
311 }
312 
313 static void
314 init_line_buf(void)
315 {
316 
317 	line_buf.str("");
318 }
319 
320 static std::string
321 get_line_buf(void)
322 {
323 
324 	line_buf.flush();
325 	if (!line_buf.good()) {
326 		errx(EXIT_FAILURE, "failed to generate line buffer, "
327 		    "partial line = %s", line_buf.str().c_str());
328 	}
329 
330 	return line_buf.str();
331 }
332 
333 /*
334  * get_word
335  *	returns EOF on end of file
336  *	NULL on end of line
337  *	pointer to the word otherwise
338  */
339 configword
340 get_word(FILE *fp)
341 {
342 	int ch;
343 	int escaped_nl = 0;
344 
345 	init_line_buf();
346 begin:
347 	while ((ch = getc(fp)) != EOF)
348 		if (ch != ' ' && ch != '\t')
349 			break;
350 	if (ch == EOF)
351 		return (configword().eof(true));
352 	if (ch == '\\'){
353 		escaped_nl = 1;
354 		goto begin;
355 	}
356 	if (ch == '\n') {
357 		if (escaped_nl){
358 			escaped_nl = 0;
359 			goto begin;
360 		}
361 		else
362 			return (configword().eol(true));
363 	}
364 	line_buf << (char)ch;
365 	/* Negation operator is a word by itself. */
366 	if (ch == '!') {
367 		return (configword(get_line_buf()));
368 	}
369 	while ((ch = getc(fp)) != EOF) {
370 		if (isspace(ch))
371 			break;
372 		line_buf << (char)ch;
373 	}
374 	if (ch == EOF)
375 		return (configword().eof(true));
376 	(void) ungetc(ch, fp);
377 	return (configword(get_line_buf()));
378 }
379 
380 /*
381  * get_quoted_word
382  *	like get_word but will accept something in double or single quotes
383  *	(to allow embedded spaces).
384  */
385 configword
386 get_quoted_word(FILE *fp)
387 {
388 	int ch;
389 	int escaped_nl = 0;
390 
391 	init_line_buf();
392 begin:
393 	while ((ch = getc(fp)) != EOF)
394 		if (ch != ' ' && ch != '\t')
395 			break;
396 	if (ch == EOF)
397 		return (configword().eof(true));
398 	if (ch == '\\'){
399 		escaped_nl = 1;
400 		goto begin;
401 	}
402 	if (ch == '\n') {
403 		if (escaped_nl){
404 			escaped_nl = 0;
405 			goto begin;
406 		}
407 		else
408 			return (configword().eol(true));
409 	}
410 	if (ch == '"' || ch == '\'') {
411 		int quote = ch;
412 
413 		escaped_nl = 0;
414 		while ((ch = getc(fp)) != EOF) {
415 			if (ch == quote && !escaped_nl)
416 				break;
417 			if (ch == '\n' && !escaped_nl) {
418 				printf("config: missing quote reading `%s'\n",
419 					get_line_buf().c_str());
420 				exit(2);
421 			}
422 			if (ch == '\\' && !escaped_nl) {
423 				escaped_nl = 1;
424 				continue;
425 			}
426 			if (ch != quote && escaped_nl)
427 				line_buf << "\\";
428 			line_buf << (char)ch;
429 			escaped_nl = 0;
430 		}
431 	} else {
432 		line_buf << (char)ch;
433 		while ((ch = getc(fp)) != EOF) {
434 			if (isspace(ch))
435 				break;
436 			line_buf << (char)ch;
437 		}
438 		if (ch != EOF)
439 			(void) ungetc(ch, fp);
440 	}
441 	if (ch == EOF)
442 		return (configword().eof(true));
443 	return (configword(get_line_buf()));
444 }
445 
446 /*
447  * prepend the path to a filename
448  */
449 char *
450 path(const char *file)
451 {
452 	char *cp = NULL;
453 
454 	if (file)
455 		asprintf(&cp, "%s/%s", destdir, file);
456 	else
457 		cp = strdup(destdir);
458 	if (cp == NULL)
459 		err(EXIT_FAILURE, "malloc");
460 	return (cp);
461 }
462 
463 /*
464  * Generate configuration file based on actual settings. With this mode, user
465  * will be able to obtain and build conifguration file with one command.
466  */
467 static void
468 configfile_dynamic(std::ostringstream &cfg)
469 {
470 	struct cputype *cput;
471 	struct device *d;
472 	struct opt *ol;
473 	char *lend;
474 	unsigned int i;
475 
476 	asprintf(&lend, "\\n\\\n");
477 	assert(lend != NULL);
478 	cfg << "options\t" << OPT_AUTOGEN << lend;
479 	cfg << "ident\t" << ident << lend;
480 	cfg << "machine\t" << machinename << lend;
481 	SLIST_FOREACH(cput, &cputype, cpu_next)
482 		cfg << "cpu\t" << cput->cpu_name << lend;
483 	SLIST_FOREACH(ol, &mkopt, op_next)
484 		cfg << "makeoptions\t" << ol->op_name << '=' <<
485 		    ol->op_value << lend;
486 	SLIST_FOREACH(ol, &opt, op_next) {
487 		if (strncmp(ol->op_name, "DEV_", 4) == 0)
488 			continue;
489 		cfg << "options\t" << ol->op_name;
490 		if (ol->op_value != NULL) {
491 			cfg << '=';
492 			for (i = 0; i < strlen(ol->op_value); i++) {
493 				if (ol->op_value[i] == '"')
494 					cfg << '\\' << ol->op_value[i];
495 				else
496 					cfg << ol->op_value[i];
497 			}
498 		}
499 
500 		cfg << lend;
501 	}
502 	/*
503 	 * Mark this file as containing everything we need.
504 	 */
505 	STAILQ_FOREACH(d, &dtab, d_next)
506 		cfg << "device\t" << d->d_name << lend;
507 	free(lend);
508 }
509 
510 /*
511  * Generate file from the configuration files.
512  */
513 static void
514 configfile_filebased(std::ostringstream &cfg)
515 {
516 	FILE *cff;
517 	struct cfgfile *cf;
518 	int i;
519 
520 	/*
521 	 * Try to read all configuration files. Since those will be present as
522 	 * C string in the macro, we have to slash their ends then the line
523 	 * wraps.
524 	 */
525 	STAILQ_FOREACH(cf, &cfgfiles, cfg_next) {
526 		cff = fopen(cf->cfg_path, "r");
527 		if (cff == NULL) {
528 			warn("Couldn't open file %s", cf->cfg_path);
529 			continue;
530 		}
531 		while ((i = getc(cff)) != EOF) {
532 			if (i == '\n')
533 				cfg << "\\n\\\n";
534 			else if (i == '"' || i == '\'')
535 				cfg << '\\' << i;
536 			else
537 				cfg << i;
538 		}
539 		fclose(cff);
540 	}
541 }
542 
543 static void
544 configfile(void)
545 {
546 	FILE *fo;
547 	std::ostringstream cfg;
548 	char *p;
549 
550 	/* Add main configuration file to the list of files to be included */
551 	cfgfile_add(PREFIX);
552 	p = path("config.c.new");
553 	fo = fopen(p, "w");
554 	if (!fo)
555 		err(2, "%s", p);
556 	free(p);
557 
558 	if (filebased) {
559 		/* Is needed, can be used for backward compatibility. */
560 		configfile_filebased(cfg);
561 	} else {
562 		configfile_dynamic(cfg);
563 	}
564 
565 	cfg.flush();
566 	/*
567 	 * We print first part of the template, replace our tag with
568 	 * configuration files content and later continue writing our
569 	 * template.
570 	 */
571 	p = strstr(kernconfstr, KERNCONFTAG);
572 	if (p == NULL)
573 		errx(EXIT_FAILURE, "Something went terribly wrong!");
574 	*p = '\0';
575 	fprintf(fo, "%s", kernconfstr);
576 	fprintf(fo, "%s", cfg.str().c_str());
577 	p += strlen(KERNCONFTAG);
578 	fprintf(fo, "%s", p);
579 	fclose(fo);
580 	moveifchanged("config.c.new", "config.c");
581 	cfgfile_removeall();
582 }
583 
584 /*
585  * moveifchanged --
586  *	compare two files; rename if changed.
587  */
588 void
589 moveifchanged(const char *from_name, const char *to_name)
590 {
591 	char *p, *q;
592 	char *from_path, *to_path;
593 	int changed;
594 	size_t tsize;
595 	struct stat from_sb, to_sb;
596 	int from_fd, to_fd;
597 
598 	changed = 0;
599 
600 	from_path = path(from_name);
601 	to_path = path(to_name);
602 	if ((from_fd = open(from_path, O_RDONLY)) < 0)
603 		err(EX_OSERR, "moveifchanged open(%s)", from_name);
604 
605 	if ((to_fd = open(to_path, O_RDONLY)) < 0)
606 		changed++;
607 
608 	if (!changed && fstat(from_fd, &from_sb) < 0)
609 		err(EX_OSERR, "moveifchanged fstat(%s)", from_path);
610 
611 	if (!changed && fstat(to_fd, &to_sb) < 0)
612 		err(EX_OSERR, "moveifchanged fstat(%s)", to_path);
613 
614 	if (!changed && from_sb.st_size != to_sb.st_size)
615 		changed++;
616 
617 	if (!changed) {
618 		tsize = (size_t)from_sb.st_size;
619 
620 		p = (char *)mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd,
621 		    (off_t)0);
622 		if (p == MAP_FAILED)
623 			err(EX_OSERR, "mmap %s", from_path);
624 		q = (char *)mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd,
625 		    (off_t)0);
626 		if (q == MAP_FAILED)
627 			err(EX_OSERR, "mmap %s", to_path);
628 
629 		changed = memcmp(p, q, tsize);
630 		munmap(p, tsize);
631 		munmap(q, tsize);
632 	}
633 
634 	if (changed) {
635 		if (rename(from_path, to_path) < 0)
636 			err(EX_OSERR, "rename(%s, %s)", from_path, to_path);
637 	} else {
638 		if (unlink(from_path) < 0)
639 			err(EX_OSERR, "unlink(%s)", from_path);
640 	}
641 
642 	close(from_fd);
643 	if (to_fd >= 0)
644 		close(to_fd);
645 
646 	free(from_path);
647 	free(to_path);
648 }
649 
650 static void
651 cleanheaders(char *p)
652 {
653 	DIR *dirp;
654 	struct dirent *dp;
655 	struct file_list *fl;
656 	struct hdr_list *hl;
657 	size_t len;
658 
659 	remember("y.tab.h");
660 	remember("setdefs.h");
661 	STAILQ_FOREACH(fl, &ftab, f_next)
662 		remember(fl->f_fn);
663 
664 	/*
665 	 * Scan the build directory and clean out stuff that looks like
666 	 * it might have been a leftover NFOO header, etc.
667 	 */
668 	if ((dirp = opendir(p)) == NULL)
669 		err(EX_OSERR, "opendir %s", p);
670 	while ((dp = readdir(dirp)) != NULL) {
671 		len = strlen(dp->d_name);
672 		/* Skip non-headers */
673 		if (len < 2 || dp->d_name[len - 2] != '.' ||
674 		    dp->d_name[len - 1] != 'h')
675 			continue;
676 		/* Skip special stuff, eg: bus_if.h, but check opt_*.h */
677 		if (strchr(dp->d_name, '_') &&
678 		    strncmp(dp->d_name, "opt_", 4) != 0)
679 			continue;
680 		/* Check if it is a target file */
681 		for (hl = htab; hl != NULL; hl = hl->h_next) {
682 			if (eq(dp->d_name, hl->h_name)) {
683 				break;
684 			}
685 		}
686 		if (hl)
687 			continue;
688 		printf("Removing stale header: %s\n", dp->d_name);
689 		p = path(dp->d_name);
690 		if (unlink(p) == -1)
691 			warn("unlink %s", dp->d_name);
692 		free(p);
693 	}
694 	(void)closedir(dirp);
695 }
696 
697 void
698 remember(const char *file)
699 {
700 	const char *s;
701 	struct hdr_list *hl;
702 
703 	if ((s = strrchr(file, '/')) != NULL)
704 		s = ns(s + 1);
705 	else
706 		s = ns(file);
707 
708 	if (strchr(s, '_') && strncmp(s, "opt_", 4) != 0) {
709 		free(__DECONST(char *, s));
710 		return;
711 	}
712 	for (hl = htab; hl != NULL; hl = hl->h_next) {
713 		if (eq(s, hl->h_name)) {
714 			free(__DECONST(char *, s));
715 			return;
716 		}
717 	}
718 	hl = (struct hdr_list *)calloc(1, sizeof(*hl));
719 	if (hl == NULL)
720 		err(EXIT_FAILURE, "calloc");
721 	hl->h_name = s;
722 	hl->h_next = htab;
723 	htab = hl;
724 }
725 
726 /*
727  * This one is quick hack. Will be probably moved to elf(3) interface.
728  * It takes kernel configuration file name, passes it as an argument to
729  * elfdump -a, which output is parsed by some UNIX tools...
730  */
731 static void
732 kernconfdump(const char *file)
733 {
734 	struct stat st;
735 	FILE *fp, *pp;
736 	int error, osz, r;
737 	size_t i, off, size, t1, t2, align;
738 	char *cmd, *o;
739 
740 	r = open(file, O_RDONLY);
741 	if (r == -1)
742 		err(EXIT_FAILURE, "Couldn't open file '%s'", file);
743 	error = fstat(r, &st);
744 	if (error == -1)
745 		err(EXIT_FAILURE, "fstat() failed");
746 	if (S_ISDIR(st.st_mode))
747 		errx(EXIT_FAILURE, "'%s' is a directory", file);
748 	fp = fdopen(r, "r");
749 	if (fp == NULL)
750 		err(EXIT_FAILURE, "fdopen() failed");
751 	osz = 1024;
752 	o = (char *)calloc(1, osz);
753 	if (o == NULL)
754 		err(EXIT_FAILURE, "Couldn't allocate memory");
755 	/* ELF note section header. */
756 	asprintf(&cmd, "/usr/bin/elfdump -c %s | grep -A 8 kern_conf"
757 	    "| tail -5 | cut -d ' ' -f 2 | paste - - - - -", file);
758 	if (cmd == NULL)
759 		errx(EXIT_FAILURE, "asprintf() failed");
760 	pp = popen(cmd, "r");
761 	if (pp == NULL)
762 		errx(EXIT_FAILURE, "popen() failed");
763 	free(cmd);
764 	(void)fread(o, osz, 1, pp);
765 	pclose(pp);
766 	r = sscanf(o, "%zu%zu%zu%zu%zu", &off, &size, &t1, &t2, &align);
767 	free(o);
768 	if (size > SIZE_MAX - off || off + size > (size_t)st.st_size)
769 		errx(EXIT_FAILURE, "%s: incoherent ELF headers", file);
770 	if (r != 5)
771 		errx(EXIT_FAILURE, "File %s doesn't contain configuration "
772 		    "file. Either unsupported, or not compiled with "
773 		    "INCLUDE_CONFIG_FILE", file);
774 	r = fseek(fp, off, SEEK_CUR);
775 	if (r != 0)
776 		err(EXIT_FAILURE, "fseek() failed");
777 	for (i = 0; i < size; i++) {
778 		r = fgetc(fp);
779 		if (r == EOF)
780 			break;
781 		if (r == '\0') {
782 			assert(i == size - 1 &&
783 			    ("\\0 found in the middle of a file"));
784 			break;
785 		}
786 		fputc(r, stdout);
787 	}
788 	fclose(fp);
789 }
790 
791 static void
792 badversion(void)
793 {
794 	fprintf(stderr, "ERROR: version of config(8) does not match kernel!\n");
795 	fprintf(stderr, "config version = %d, ", CONFIGVERS);
796 	fprintf(stderr, "version required = %d\n\n", versreq);
797 	fprintf(stderr, "Make sure that /usr/src/usr.sbin/config is in sync\n");
798 	fprintf(stderr, "with your /usr/src/sys and install a new config binary\n");
799 	fprintf(stderr, "before trying this again.\n\n");
800 	fprintf(stderr, "If running the new config fails check your config\n");
801 	fprintf(stderr, "file against the GENERIC or LINT config files for\n");
802 	fprintf(stderr, "changes in config syntax, or option/device naming\n");
803 	fprintf(stderr, "conventions\n\n");
804 	exit(1);
805 }
806 
807 static void
808 checkversion(void)
809 {
810 	FILE *ifp;
811 	char line[BUFSIZ];
812 
813 	ifp = open_makefile_template();
814 	while (fgets(line, BUFSIZ, ifp) != 0) {
815 		if (*line != '%')
816 			continue;
817 		if (strncmp(line, "%VERSREQ=", 9) != 0)
818 			continue;
819 		versreq = atoi(line + 9);
820 		if (MAJOR_VERS(versreq) == MAJOR_VERS(CONFIGVERS) &&
821 		    versreq <= CONFIGVERS)
822 			continue;
823 		badversion();
824 	}
825 	fclose(ifp);
826 }
827