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