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