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