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