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