1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
|
/* $OpenBSD: main.c,v 1.18 1999/10/04 20:00:51 deraadt Exp $ */
/* $NetBSD: main.c,v 1.22 1997/02/02 21:12:33 thorpej Exp $ */
/*
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* This software was developed by the Computer Systems Engineering group
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
* contributed to Berkeley.
*
* All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Lawrence Berkeley Laboratories.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* from: @(#)main.c 8.1 (Berkeley) 6/6/93
*/
#ifndef lint
static char copyright[] =
"@(#) Copyright (c) 1992, 1993\n\
The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "config.h"
int firstfile __P((const char *));
int yyparse __P((void));
extern char *optarg;
extern int optind;
static struct hashtab *mkopttab;
static struct nvlist **nextopt;
static struct nvlist **nextdefopt;
static struct nvlist **nextmkopt;
static __dead void stop __P((void));
static int do_option __P((struct hashtab *, struct nvlist ***,
const char *, const char *, const char *));
static int crosscheck __P((void));
static int badstar __P((void));
static int mksymlinks __P((void));
static int hasparent __P((struct devi *));
static int cfcrosscheck __P((struct config *, const char *, struct nvlist *));
static void optiondelta __P((void));
int madedir = 0;
void
usage()
{
extern char *__progname;
fprintf(stderr, "usage: %s [-p] [-s srcdir] [-b builddir] sysname\n",
__progname);
fprintf(stderr, " %s -e [-u] [-o newkernel] kernel\n", __progname);
exit(1);
}
int
main(argc, argv)
int argc;
char **argv;
{
register char *p;
const char *last_component;
char *outfile = NULL;
int pflag, ch, eflag, uflag, fflag;
pflag = eflag = uflag = fflag = 0;
while ((ch = getopt(argc, argv, "egpfb:s:o:u")) != -1) {
switch (ch) {
case 'o':
outfile = optarg;
break;
case 'u':
uflag = 1;
break;
case 'f':
fflag = 1;
break;
case 'e':
eflag = 1;
break;
case 'g':
/*
* In addition to DEBUG, you probably wanted to
* set "options KGDB" and maybe others. We could
* do that for you, but you really should just
* put them in the config file.
*/
(void)fputs(
"-g is obsolete (use makeoptions DEBUG=\"-g\")\n",
stderr);
usage();
case 'p':
/*
* Essentially the same as makeoptions PROF="-pg",
* but also changes the path from ../../compile/FOO
* to ../../compile/FOO.PROF; i.e., compile a
* profiling kernel based on a typical "regular"
* kernel.
*
* Note that if you always want profiling, you
* can (and should) use a "makeoptions" line.
*/
pflag = 1;
break;
case 'b':
builddir = optarg;
break;
case 's':
srcdir = optarg;
break;
case '?':
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc > 1 || !argv[0])
usage();
if (eflag)
return (ukc(argv[0], outfile, uflag, fflag));
conffile = (argc == 1) ? argv[0] : "CONFIG";
if (firstfile(conffile)) {
(void)fprintf(stderr, "config: cannot read %s: %s\n",
conffile, strerror(errno));
exit(2);
}
/*
* Init variables.
*/
minmaxusers = 1;
maxmaxusers = 10000;
initintern();
initfiles();
initsem();
devbasetab = ht_new();
devatab = ht_new();
selecttab = ht_new();
needcnttab = ht_new();
opttab = ht_new();
mkopttab = ht_new();
defopttab = ht_new();
nextopt = &options;
nextmkopt = &mkoptions;
nextdefopt = &defoptions;
/*
* Handle profiling (must do this before we try to create any
* files).
*/
last_component = strrchr(conffile, '/');
last_component = (last_component) ? last_component + 1 : conffile;
if (pflag) {
p = emalloc(strlen(last_component) + 17);
(void)sprintf(p, "../compile/%s.PROF", last_component);
(void)addmkoption(intern("PROF"), "-pg");
(void)addoption(intern("GPROF"), NULL);
} else {
p = emalloc(strlen(last_component) + 13);
(void)sprintf(p, "../compile/%s", last_component);
}
defbuilddir = (argc == 0) ? "." : p;
/*
* Parse config file (including machine definitions).
*/
if (yyparse())
stop();
/*
* Fix (as in `set firmly in place') files.
*/
if (fixfiles())
stop();
/*
* Fix objects and libraries.
*/
if (fixobjects())
stop();
/*
* Perform cross-checking.
*/
if (maxusers == 0) {
if (defmaxusers) {
(void)printf("maxusers not specified; %d assumed\n",
defmaxusers);
maxusers = defmaxusers;
} else {
(void)fprintf(stderr,
"config: need \"maxusers\" line\n");
errors++;
}
}
if (crosscheck() || errors)
stop();
/*
* Squeeze things down and finish cross-checks (STAR checks must
* run after packing).
*/
pack();
if (badstar())
stop();
/*
* Ready to go. Build all the various files.
*/
if (mksymlinks() || mkmakefile() || mkheaders() || mkswap() ||
mkioconf())
stop();
(void)printf("Don't forget to run \"make depend\"\n");
optiondelta();
exit(0);
}
/*
* Make a symlink for "machine" so that "#include <machine/foo.h>" works,
* and for the machine's CPU architecture, so that works as well.
*/
static int
mksymlinks()
{
int ret;
char *p, buf[MAXPATHLEN];
const char *q;
sprintf(buf, "arch/%s/include", machine);
p = sourcepath(buf);
(void)unlink("machine");
ret = symlink(p, "machine");
if (ret)
(void)fprintf(stderr, "config: symlink(machine -> %s): %s\n",
p, strerror(errno));
if (machinearch != NULL) {
sprintf(buf, "arch/%s/include", machinearch);
p = sourcepath(buf);
q = machinearch;
} else {
p = strdup("machine");
q = machine;
}
(void)unlink(q);
ret = symlink(p, q);
if (ret)
(void)fprintf(stderr, "config: symlink(%s -> %s): %s\n",
q, p, strerror(errno));
free(p);
return (ret);
}
static __dead void
stop()
{
(void)fprintf(stderr, "*** Stop.\n");
exit(1);
}
/*
* Define a standard option, for which a header file will be generated.
*/
void
defoption(name)
const char *name;
{
register const char *n;
register char *p, c;
char low[500];
/*
* Convert to lower case. The header file name will be
* in lower case, so we store the lower case version in
* the hash table to detect option name collisions. The
* original string will be stored in the nvlist for use
* in the header file.
*/
for (n = name, p = low; (c = *n) != '\0'; n++)
*p++ = isupper(c) ? tolower(c) : c;
*p = 0;
n = intern(low);
(void)do_option(defopttab, &nextdefopt, n, name, "defopt");
/*
* Insert a verbatum copy of the option name, as well,
* to speed lookups when creating the Makefile.
*/
(void)ht_insert(defopttab, name, (void *)name);
}
/*
* Add an option from "options FOO". Note that this selects things that
* are "optional foo".
*/
void
addoption(name, value)
const char *name, *value;
{
register const char *n;
register char *p, c;
char low[500];
if (do_option(opttab, &nextopt, name, value, "options"))
return;
/* make lowercase, then add to select table */
for (n = name, p = low; (c = *n) != '\0'; n++)
*p++ = isupper(c) ? tolower(c) : c;
*p = 0;
n = intern(low);
(void)ht_insert(selecttab, n, (void *)n);
}
/*
* Add a "make" option.
*/
void
addmkoption(name, value)
const char *name, *value;
{
(void)do_option(mkopttab, &nextmkopt, name, value, "mkoptions");
}
/*
* Add a name=value pair to an option list. The value may be NULL.
*/
static int
do_option(ht, nppp, name, value, type)
struct hashtab *ht;
struct nvlist ***nppp;
const char *name, *value, *type;
{
register struct nvlist *nv;
/* assume it will work */
nv = newnv(name, value, NULL, 0, NULL);
if (ht_insert(ht, name, nv) == 0) {
**nppp = nv;
*nppp = &nv->nv_next;
return (0);
}
/* oops, already got that option */
nvfree(nv);
if ((nv = ht_lookup(ht, name)) == NULL)
panic("do_option");
if (nv->nv_str != NULL)
error("already have %s `%s=%s'", type, name, nv->nv_str);
else
error("already have %s `%s'", type, name);
return (1);
}
/*
* Return true if there is at least one instance of the given unit
* on the given device attachment (or any units, if unit == WILD).
*/
int
deva_has_instances(deva, unit)
register struct deva *deva;
int unit;
{
register struct devi *i;
if (unit == WILD)
return (deva->d_ihead != NULL);
for (i = deva->d_ihead; i != NULL; i = i->i_asame)
if (unit == i->i_unit)
return (1);
return (0);
}
/*
* Return true if there is at least one instance of the given unit
* on the given base (or any units, if unit == WILD).
*/
int
devbase_has_instances(dev, unit)
register struct devbase *dev;
int unit;
{
register struct deva *da;
for (da = dev->d_ahead; da != NULL; da = da->d_bsame)
if (deva_has_instances(da, unit))
return (1);
return (0);
}
static int
hasparent(i)
register struct devi *i;
{
register struct nvlist *nv;
int atunit = i->i_atunit;
/*
* We determine whether or not a device has a parent in in one
* of two ways:
* (1) If a parent device was named in the config file,
* i.e. cases (2) and (3) in sem.c:adddev(), then
* we search its devbase for a matching unit number.
* (2) If the device was attach to an attribute, then we
* search all attributes the device can be attached to
* for parents (with appropriate unit numebrs) that
* may be able to attach the device.
*/
/*
* Case (1): A parent was named. Either it's configured, or not.
*/
if (i->i_atdev != NULL)
return (devbase_has_instances(i->i_atdev, atunit));
/*
* Case (2): No parent was named. Look for devs that provide the attr.
*/
if (i->i_atattr != NULL)
for (nv = i->i_atattr->a_refs; nv != NULL; nv = nv->nv_next)
if (devbase_has_instances(nv->nv_ptr, atunit))
return (1);
return (0);
}
static int
cfcrosscheck(cf, what, nv)
register struct config *cf;
const char *what;
register struct nvlist *nv;
{
register struct devbase *dev;
register struct devi *pd;
int errs, devminor;
if (maxpartitions <= 0)
panic("cfcrosscheck");
for (errs = 0; nv != NULL; nv = nv->nv_next) {
if (nv->nv_name == NULL)
continue;
dev = ht_lookup(devbasetab, nv->nv_name);
if (dev == NULL)
panic("cfcrosscheck(%s)", nv->nv_name);
devminor = minor(nv->nv_int) / maxpartitions;
if (devbase_has_instances(dev, devminor))
continue;
if (devbase_has_instances(dev, STAR) &&
devminor >= dev->d_umax)
continue;
for (pd = allpseudo; pd != NULL; pd = pd->i_next)
if (pd->i_base == dev && devminor < dev->d_umax &&
devminor >= 0)
goto loop;
(void)fprintf(stderr,
"%s:%d: %s says %s on %s, but there's no %s\n",
conffile, cf->cf_lineno,
cf->cf_name, what, nv->nv_str, nv->nv_str);
errs++;
loop:
}
return (errs);
}
/*
* Cross-check the configuration: make sure that each target device
* or attribute (`at foo[0*?]') names at least one real device. Also
* see that the root, swap, and dump devices for all configurations
* are there.
*/
int
crosscheck()
{
register struct devi *i;
register struct config *cf;
int errs;
errs = 0;
for (i = alldevi; i != NULL; i = i->i_next) {
if (i->i_at == NULL || hasparent(i))
continue;
xerror(conffile, i->i_lineno,
"%s at %s is orphaned", i->i_name, i->i_at);
(void)fprintf(stderr, " (%s %s declared)\n",
i->i_atunit == WILD ? "nothing matching" : "no",
i->i_at);
errs++;
}
if (allcf == NULL) {
(void)fprintf(stderr, "%s has no configurations!\n",
conffile);
errs++;
}
for (cf = allcf; cf != NULL; cf = cf->cf_next) {
if (cf->cf_root != NULL) { /* i.e., not swap generic */
errs += cfcrosscheck(cf, "root", cf->cf_root);
errs += cfcrosscheck(cf, "swap", cf->cf_swap);
errs += cfcrosscheck(cf, "dumps", cf->cf_dump);
}
}
return (errs);
}
/*
* Check to see if there is a *'d unit with a needs-count file.
*/
int
badstar()
{
register struct devbase *d;
register struct deva *da;
register struct devi *i;
register int errs, n;
errs = 0;
for (d = allbases; d != NULL; d = d->d_next) {
for (da = d->d_ahead; da != NULL; da = da->d_bsame)
for (i = da->d_ihead; i != NULL; i = i->i_asame) {
if (i->i_unit == STAR)
goto foundstar;
}
continue;
foundstar:
if (ht_lookup(needcnttab, d->d_name)) {
(void)fprintf(stderr,
"config: %s's cannot be *'d until its driver is fixed\n",
d->d_name);
errs++;
continue;
}
for (n = 0; i != NULL; i = i->i_alias)
if (!i->i_collapsed)
n++;
if (n < 1)
panic("badstar() n<1");
}
return (errs);
}
/*
* Verify/create builddir if necessary, change to it, and verify srcdir.
* This will be called when we see the first include.
*/
void
setupdirs()
{
struct stat st;
/* srcdir must be specified if builddir is not specified or if
* no configuration filename was specified. */
if ((builddir || strcmp(defbuilddir, ".") == 0) && !srcdir) {
error("source directory must be specified");
exit(1);
}
if (srcdir == NULL)
srcdir = "../../../..";
if (builddir == NULL)
builddir = defbuilddir;
if (stat(builddir, &st) != 0) {
if (mkdir(builddir, 0777)) {
(void)fprintf(stderr, "config: cannot create %s: %s\n",
builddir, strerror(errno));
exit(2);
}
madedir = 1;
} else if (!S_ISDIR(st.st_mode)) {
(void)fprintf(stderr, "config: %s is not a directory\n",
builddir);
exit(2);
}
if (chdir(builddir) != 0) {
(void)fprintf(stderr, "config: cannot change to %s\n",
builddir);
exit(2);
}
if (stat(srcdir, &st) != 0 || !S_ISDIR(st.st_mode)) {
(void)fprintf(stderr, "config: %s is not a directory\n",
srcdir);
exit(2);
}
}
struct opt {
const char *name;
const char *val;
};
int
optcmp(sp1, sp2)
struct opt *sp1, *sp2;
{
int r;
r = strcmp(sp1->name, sp2->name);
if (r == 0) {
if (!sp1->val && !sp2->val)
r = 0;
else if (sp1->val && !sp2->val)
r = -1;
else if (sp2->val && !sp1->val)
r = 1;
else r = strcmp(sp1->val, sp2->val);
}
return (r);
}
void
optiondelta()
{
register struct nvlist *nv;
char nbuf[BUFSIZ], obuf[BUFSIZ]; /* XXX size */
int nnewopts, ret = 0, i;
struct opt *newopts;
FILE *fp;
for (nnewopts = 0, nv = options; nv != NULL; nv = nv->nv_next)
nnewopts++;
newopts = (struct opt *)malloc(nnewopts * sizeof(struct opt));
if (newopts == NULL)
ret = 0;
for (i = 0, nv = options; nv != NULL; nv = nv->nv_next, i++) {
newopts[i].name = nv->nv_name;
newopts[i].val = nv->nv_str;
}
qsort(newopts, nnewopts, sizeof (struct opt), optcmp);
/* compare options against previous config */
if ((fp = fopen("options", "r"))) {
for (i = 0; !feof(fp) && i < nnewopts && ret == 0; i++) {
if (newopts[i].val)
snprintf(nbuf, sizeof nbuf, "%s=%s\n",
newopts[i].name, newopts[i].val);
else
snprintf(nbuf, sizeof nbuf, "%s\n",
newopts[i].name);
if (fgets(obuf, sizeof obuf, fp) == NULL ||
strcmp(nbuf, obuf))
ret = 1;
}
fclose(fp);
fp = NULL;
} else
ret = 1;
/* replace with the new list of options */
if ((fp = fopen("options", "w+"))) {
rewind(fp);
for (i = 0; i < nnewopts; i++) {
if (newopts[i].val)
fprintf(fp, "%s=%s\n", newopts[i].name,
newopts[i].val);
else
fprintf(fp, "%s\n", newopts[i].name);
}
fclose(fp);
}
free(newopts);
if (ret == 0 || madedir == 1)
return;
(void)printf("Kernel options have changed -- you must run \"make clean\"\n");
}
|