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