GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/config/sem.c Lines: 0 484 0.0 %
Date: 2017-11-07 Branches: 0 294 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: sem.c,v 1.36 2015/12/14 05:59:56 mmcc Exp $	*/
2
/*	$NetBSD: sem.c,v 1.10 1996/11/11 23:40:11 gwr Exp $	*/
3
4
/*
5
 * Copyright (c) 1992, 1993
6
 *	The Regents of the University of California.  All rights reserved.
7
 *
8
 * This software was developed by the Computer Systems Engineering group
9
 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
10
 * contributed to Berkeley.
11
 *
12
 * All advertising materials mentioning features or use of this software
13
 * must display the following acknowledgement:
14
 *	This product includes software developed by the University of
15
 *	California, Lawrence Berkeley Laboratories.
16
 *
17
 * Redistribution and use in source and binary forms, with or without
18
 * modification, are permitted provided that the following conditions
19
 * are met:
20
 * 1. Redistributions of source code must retain the above copyright
21
 *    notice, this list of conditions and the following disclaimer.
22
 * 2. Redistributions in binary form must reproduce the above copyright
23
 *    notice, this list of conditions and the following disclaimer in the
24
 *    documentation and/or other materials provided with the distribution.
25
 * 3. Neither the name of the University nor the names of its contributors
26
 *    may be used to endorse or promote products derived from this software
27
 *    without specific prior written permission.
28
 *
29
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39
 * SUCH DAMAGE.
40
 *
41
 *	from: @(#)sem.c	8.1 (Berkeley) 6/6/93
42
 */
43
44
#include <sys/param.h>	/* NODEV */
45
46
#include <ctype.h>
47
#include <err.h>
48
#include <stdio.h>
49
#include <stdlib.h>
50
#include <string.h>
51
52
#include "config.h"
53
#include "sem.h"
54
55
/*
56
 * config semantics.
57
 */
58
59
#define	NAMESIZE	100	/* local name buffers */
60
61
const char *s_generic;
62
const char *s_nfs;
63
64
static struct hashtab *attrtab;		/* for attribute lookup */
65
static struct hashtab *cfhashtab;	/* for config lookup */
66
static struct hashtab *devitab;		/* etc */
67
68
static struct attr errattr;
69
static struct devbase errdev;
70
static struct deva errdeva;
71
static struct devbase **nextbase;
72
static struct deva **nextdeva;
73
static struct config **nextcf;
74
static struct devi **nextdevi;
75
static struct devi **nextpseudo;
76
77
static int has_errobj(struct nvlist *, void *);
78
static struct nvlist *addtoattr(struct nvlist *, struct devbase *);
79
static int exclude(struct nvlist *, const char *, const char *);
80
static int resolve(struct nvlist **, const char *, const char *,
81
    struct nvlist *, int);
82
static int lresolve(struct nvlist **, const char *, const char *,
83
    struct nvlist *, int);
84
static struct devi *newdevi(const char *, int, struct devbase *d);
85
static struct devi *getdevi(const char *);
86
static const char *concat(const char *, int);
87
static char *extend(char *, const char *);
88
static int split(const char *, size_t, char *, size_t, int *);
89
static void selectbase(struct devbase *, struct deva *);
90
static int onlist(struct nvlist *, void *);
91
static const char **fixloc(const char *, struct attr *, struct nvlist *);
92
93
void
94
initsem(void)
95
{
96
97
	attrtab = ht_new();
98
	errattr.a_name = "<internal>";
99
100
	allbases = NULL;
101
	nextbase = &allbases;
102
103
	alldevas = NULL;
104
	nextdeva = &alldevas;
105
106
	cfhashtab = ht_new();
107
	allcf = NULL;
108
	nextcf = &allcf;
109
110
	devitab = ht_new();
111
	alldevi = NULL;
112
	nextdevi = &alldevi;
113
	errdev.d_name = "<internal>";
114
115
	allpseudo = NULL;
116
	nextpseudo = &allpseudo;
117
118
	s_generic = intern("generic");
119
	s_nfs = intern("nfs");
120
}
121
122
/* Name of include file just ended (set in scan.l) */
123
extern const char *lastfile;
124
125
void
126
enddefs(void)
127
{
128
	struct devbase *dev;
129
130
	for (dev = allbases; dev != NULL; dev = dev->d_next) {
131
		if (!dev->d_isdef) {
132
			(void)fprintf(stderr,
133
			    "%s: device `%s' used but not defined\n",
134
			    lastfile, dev->d_name);
135
			errors++;
136
			continue;
137
		}
138
	}
139
	if (errors) {
140
		(void)fprintf(stderr, "*** Stop.\n");
141
		exit(1);
142
	}
143
}
144
145
void
146
setdefmaxusers(int min, int def, int max)
147
{
148
149
	if (min < 1 || min > def || def > max)
150
		error("maxusers must have 1 <= min <= default <= max");
151
	else {
152
		minmaxusers = min;
153
		defmaxusers = def;
154
		maxmaxusers = max;
155
	}
156
}
157
158
void
159
setmaxusers(int n)
160
{
161
162
	if (maxusers != 0) {
163
		warnx("warning: duplicate maxusers parameter, will use latest definition (%d)", n);
164
	}
165
	maxusers = n;
166
	if (n < minmaxusers) {
167
		warnx("warning: minimum of %d maxusers assumed", minmaxusers);
168
		maxusers = minmaxusers;
169
	} else if (n > maxmaxusers) {
170
		warnx("warning: maxusers (%d) > %d", n, maxmaxusers);
171
	}
172
}
173
174
/*
175
 * Define an attribute, optionally with an interface (a locator list).
176
 * Since an empty locator list is logically different from "no interface",
177
 * all locator lists include a dummy head node, which we discard here.
178
 */
179
int
180
defattr(const char *name, struct nvlist *locs)
181
{
182
	struct attr *a;
183
	struct nvlist *nv;
184
	int len;
185
186
	a = emalloc(sizeof *a);
187
	if (ht_insert(attrtab, name, a)) {
188
		free(a);
189
		error("attribute `%s' already defined", name);
190
		nvfreel(locs);
191
		return (1);
192
	}
193
	a->a_name = name;
194
	if (locs != NULL) {
195
		a->a_iattr = 1;
196
		a->a_locs = locs->nv_next;
197
		nvfree(locs);
198
	} else {
199
		a->a_iattr = 0;
200
		a->a_locs = NULL;
201
	}
202
	len = 0;
203
	for (nv = a->a_locs; nv != NULL; nv = nv->nv_next)
204
		len++;
205
	a->a_loclen = len;
206
	a->a_devs = NULL;
207
	a->a_refs = NULL;
208
	return (0);
209
}
210
211
/*
212
 * Return true if the given `error object' is embedded in the given
213
 * pointer list.
214
 */
215
static int
216
has_errobj(struct nvlist *nv, void *obj)
217
{
218
219
	for (; nv != NULL; nv = nv->nv_next)
220
		if (nv->nv_ptr == obj)
221
			return (1);
222
	return (0);
223
}
224
225
/*
226
 * Add a device base to a list in an attribute (actually, to any list).
227
 * Note that this does not check for duplicates, and does reverse the
228
 * list order, but no one cares anyway.
229
 */
230
static struct nvlist *
231
addtoattr(struct nvlist *l, struct devbase *dev)
232
{
233
	struct nvlist *n;
234
235
	n = newnv(NULL, NULL, dev, 0, l);
236
	return (n);
237
}
238
239
/*
240
 * Define a device.  This may (or may not) also define an interface
241
 * attribute and/or refer to existing attributes.
242
 */
243
void
244
defdev(struct devbase *dev, int ispseudo, struct nvlist *loclist,
245
    struct nvlist *attrs)
246
{
247
	struct nvlist *nv;
248
	struct attr *a;
249
250
	if (dev == &errdev)
251
		goto bad;
252
	if (dev->d_isdef) {
253
		error("redefinition of `%s'", dev->d_name);
254
		goto bad;
255
	}
256
	dev->d_isdef = 1;
257
	if (has_errobj(attrs, &errattr))
258
		goto bad;
259
260
	/*
261
	 * Handle implicit attribute definition from locator list.  Do
262
	 * this before scanning the `at' list so that we can have, e.g.:
263
	 *	device foo at other, foo { slot = -1 }
264
	 * (where you can plug in a foo-bus extender to a foo-bus).
265
	 */
266
	if (loclist != NULL) {
267
		nv = loclist;
268
		loclist = NULL;	/* defattr disposes of them for us */
269
		if (defattr(dev->d_name, nv))
270
			goto bad;
271
		attrs = newnv(dev->d_name, NULL, getattr(dev->d_name), 0,
272
		    attrs);
273
	}
274
275
	/* Committed!  Set up fields. */
276
	dev->d_ispseudo = ispseudo;
277
	dev->d_attrs = attrs;
278
279
	/*
280
	 * For each interface attribute this device refers to, add this
281
	 * device to its reference list.  This makes, e.g., finding all
282
	 * "scsi"s easier.
283
	 */
284
	for (nv = attrs; nv != NULL; nv = nv->nv_next) {
285
		a = nv->nv_ptr;
286
		if (a->a_iattr)
287
			a->a_refs = addtoattr(a->a_refs, dev);
288
	}
289
	return;
290
bad:
291
	nvfreel(loclist);
292
	nvfreel(attrs);
293
}
294
295
/*
296
 * Look up a devbase.  Also makes sure it is a reasonable name,
297
 * i.e., does not end in a digit or contain special characters.
298
 */
299
struct devbase *
300
getdevbase(char *name)
301
{
302
	u_char *p;
303
	struct devbase *dev;
304
305
	p = (u_char *)name;
306
	if (!isalpha(*p))
307
		goto badname;
308
	while (*++p) {
309
		if (!isalnum(*p) && *p != '_')
310
			goto badname;
311
	}
312
	if (isdigit(*--p)) {
313
badname:
314
		error("bad device base name `%s'", name);
315
		return (&errdev);
316
	}
317
	dev = ht_lookup(devbasetab, name);
318
	if (dev == NULL) {
319
		dev = emalloc(sizeof *dev);
320
		dev->d_name = name;
321
		dev->d_next = NULL;
322
		dev->d_isdef = 0;
323
		dev->d_major = NODEV;
324
		dev->d_attrs = NULL;
325
		dev->d_ihead = NULL;
326
		dev->d_ipp = &dev->d_ihead;
327
		dev->d_ahead = NULL;
328
		dev->d_app = &dev->d_ahead;
329
		dev->d_umax = 0;
330
		*nextbase = dev;
331
		nextbase = &dev->d_next;
332
		if (ht_insert(devbasetab, name, dev))
333
			panic("getdevbase(%s)", name);
334
	}
335
	return (dev);
336
}
337
338
/*
339
 * Define some of a device's allowable parent attachments.
340
 * There may be a list of (plain) attributes.
341
 */
342
void
343
defdevattach(struct deva *deva, struct devbase *dev, struct nvlist *atlist,
344
    struct nvlist *attrs)
345
{
346
	struct nvlist *nv;
347
	struct attr *a;
348
	struct deva *da;
349
350
	if (dev == &errdev)
351
		goto bad;
352
	if (deva == NULL)
353
		deva = getdevattach(dev->d_name);
354
	if (deva == &errdeva)
355
		goto bad;
356
	if (!dev->d_isdef) {
357
		error("attaching undefined device `%s'", dev->d_name);
358
		goto bad;
359
	}
360
	if (deva->d_isdef) {
361
		error("redefinition of `%s'", deva->d_name);
362
		goto bad;
363
	}
364
	if (dev->d_ispseudo) {
365
		error("pseudo-devices can't attach");
366
		goto bad;
367
	}
368
369
	deva->d_isdef = 1;
370
	if (has_errobj(attrs, &errattr))
371
		goto bad;
372
	for (nv = attrs; nv != NULL; nv = nv->nv_next) {
373
		a = nv->nv_ptr;
374
		if (a == &errattr)
375
			continue;		/* already complained */
376
		if (a->a_iattr)
377
			error("`%s' is not a plain attribute", a->a_name);
378
	}
379
380
	/* Committed!  Set up fields. */
381
	deva->d_attrs = attrs;
382
	deva->d_atlist = atlist;
383
	deva->d_devbase = dev;
384
385
	/*
386
	 * Turn the `at' list into interface attributes (map each
387
	 * nv_name to an attribute, or to NULL for root), and add
388
	 * this device to those attributes, so that children can
389
	 * be listed at this particular device if they are supported
390
	 * by that attribute.
391
	 */
392
	for (nv = atlist; nv != NULL; nv = nv->nv_next) {
393
		if (nv->nv_name == NULL)
394
			nv->nv_ptr = a = NULL;	/* at root */
395
		else
396
			nv->nv_ptr = a = getattr(nv->nv_name);
397
		if (a == &errattr)
398
			continue;		/* already complained */
399
400
		/*
401
		 * Make sure that an attachment spec doesn't
402
		 * already say how to attach to this attribute.
403
		 */
404
		for (da = dev->d_ahead; da != NULL; da = da->d_bsame)
405
			if (onlist(da->d_atlist, a))
406
				error("attach at `%s' already done by `%s'",
407
				    a ? a->a_name : "root", da->d_name);
408
409
		if (a == NULL)
410
			continue;		/* at root; don't add */
411
		if (!a->a_iattr)
412
			error("%s cannot be at plain attribute `%s'",
413
			    dev->d_name, a->a_name);
414
		else
415
			a->a_devs = addtoattr(a->a_devs, dev);
416
	}
417
418
	/* attach to parent */
419
	*dev->d_app = deva;
420
	dev->d_app = &deva->d_bsame;
421
	return;
422
bad:
423
	nvfreel(atlist);
424
	nvfreel(attrs);
425
}
426
427
/*
428
 * Look up a device attachment.  Also makes sure it is a reasonable
429
 * name, i.e., does not contain digits or special characters.
430
 */
431
struct deva *
432
getdevattach(const char *name)
433
{
434
	u_char *p;
435
	struct deva *deva;
436
437
	p = (u_char *)name;
438
	if (!isalpha(*p))
439
		goto badname;
440
	while (*++p) {
441
		if (!isalnum(*p) && *p != '_')
442
			goto badname;
443
	}
444
	if (isdigit((unsigned char)*--p)) {
445
badname:
446
		error("bad device attachment name `%s'", name);
447
		return (&errdeva);
448
	}
449
	deva = ht_lookup(devatab, name);
450
	if (deva == NULL) {
451
		deva = emalloc(sizeof *deva);
452
		deva->d_name = name;
453
		deva->d_next = NULL;
454
		deva->d_bsame = NULL;
455
		deva->d_isdef = 0;
456
		deva->d_devbase = NULL;
457
		deva->d_atlist = NULL;
458
		deva->d_attrs = NULL;
459
		deva->d_ihead = NULL;
460
		deva->d_ipp = &deva->d_ihead;
461
		*nextdeva = deva;
462
		nextdeva = &deva->d_next;
463
		if (ht_insert(devatab, name, deva))
464
			panic("getdeva(%s)", name);
465
	}
466
	return (deva);
467
}
468
469
/*
470
 * Look up an attribute.
471
 */
472
struct attr *
473
getattr(const char *name)
474
{
475
	struct attr *a;
476
477
	if ((a = ht_lookup(attrtab, name)) == NULL) {
478
		error("undefined attribute `%s'", name);
479
		a = &errattr;
480
	}
481
	return (a);
482
}
483
484
/*
485
 * Set the major device number for a device, so that it can be used
486
 * as a root/swap/dumps "on" device in a configuration.
487
 */
488
void
489
setmajor(struct devbase *d, int n)
490
{
491
492
	if (d != &errdev && d->d_major != NODEV)
493
		error("device `%s' is already major %d",
494
		    d->d_name, d->d_major);
495
	else
496
		d->d_major = n;
497
}
498
499
static int
500
exclude(struct nvlist *nv, const char *name, const char *what)
501
{
502
503
	if (nv != NULL) {
504
		error("%s: swap generic must not specify %s", name, what);
505
		return (1);
506
	}
507
	return (0);
508
}
509
510
/*
511
 * Map things like "ra0b" => makedev(major("ra"), 0*maxpartitions + 'b'-'a').
512
 * Handle the case where the device number is given but there is no
513
 * corresponding name, and map NULL to the default.
514
 */
515
static int
516
resolve(struct nvlist **nvp, const char *name, const char *what,
517
    struct nvlist *dflt, int part)
518
{
519
	struct nvlist *nv;
520
	struct devbase *dev;
521
	const char *cp;
522
	int maj, min, l;
523
	int unit;
524
	char buf[NAMESIZE];
525
526
	part -= 'a';
527
	if ((part >= maxpartitions) || (part < 0))
528
		panic("resolve");
529
	if ((nv = *nvp) == NULL) {
530
		dev_t	d = NODEV;
531
		/*
532
		 * Apply default.  Easiest to do this by number.
533
		 * Make sure to retain NODEVness, if this is dflt's disposition.
534
		 */
535
		if (dflt->nv_int != NODEV) {
536
			maj = major(dflt->nv_int);
537
			min = (minor(dflt->nv_int) / maxpartitions) + part;
538
			d = makedev(maj, min);
539
		}
540
		*nvp = nv = newnv(NULL, NULL, NULL, d, NULL);
541
	}
542
	if (nv->nv_int != NODEV) {
543
		/*
544
		 * By the numbers.  Find the appropriate major number
545
		 * to make a name.
546
		 */
547
		maj = major(nv->nv_int);
548
		min = minor(nv->nv_int);
549
		for (dev = allbases; dev != NULL; dev = dev->d_next)
550
			if (dev->d_major == maj)
551
				break;
552
		if (dev == NULL)
553
			(void)snprintf(buf, sizeof buf, "<%d/%d>",
554
			    maj, min);
555
		else
556
			(void)snprintf(buf, sizeof buf, "%s%d%c",
557
			    dev->d_name, min / maxpartitions,
558
			    (min % maxpartitions) + 'a');
559
		nv->nv_str = intern(buf);
560
		return (0);
561
	}
562
563
	if (nv->nv_str == NULL || nv->nv_str == s_nfs)
564
		/*
565
		 * NFS spec. Leave as NODEV.
566
		 */
567
		return (0);
568
569
	/*
570
	 * The normal case: things like "ra2b".  Check for partition
571
	 * suffix, remove it if there, and split into name ("ra") and
572
	 * unit (2).
573
	 */
574
	l = strlen(nv->nv_str);
575
	cp = &nv->nv_str[l];
576
	if (l > 1 && *--cp >= 'a' && *cp <= 'a'+maxpartitions &&
577
	    isdigit((unsigned char)cp[-1])) {
578
		l--;
579
		part = *cp - 'a';
580
	}
581
	cp = nv->nv_str;
582
	if (split(cp, l, buf, sizeof buf, &unit)) {
583
		error("%s: invalid %s device name `%s'", name, what, cp);
584
		return (1);
585
	}
586
	dev = ht_lookup(devbasetab, intern(buf));
587
	if (dev == NULL || dev->d_major == NODEV) {
588
		error("%s: can't make %s device from `%s'",
589
		    name, what, nv->nv_str);
590
		return (1);
591
	}
592
	nv->nv_name = dev->d_name;
593
	nv->nv_int = makedev(dev->d_major, unit * maxpartitions + part);
594
	return (0);
595
}
596
597
static int
598
lresolve(struct nvlist **nvp, const char *name, const char *what,
599
    struct nvlist *dflt, int part)
600
{
601
	int err;
602
603
	while ((err = resolve(nvp, name, what, dflt, part)) == 0 &&
604
	    (*nvp)->nv_next != NULL)
605
		nvp = &(*nvp)->nv_next;
606
	return (err);
607
}
608
609
/*
610
 * Add a completed configuration to the list.
611
 */
612
void
613
addconf(struct config *cf0)
614
{
615
	struct config *cf;
616
	struct nvlist *nv;
617
	const char *name;
618
619
	name = cf0->cf_name;
620
	cf = emalloc(sizeof *cf);
621
	if (ht_insert(cfhashtab, name, cf)) {
622
		error("configuration `%s' already defined", name);
623
		free(cf);
624
		goto bad;
625
	}
626
	*cf = *cf0;
627
628
	/*
629
	 * Look for "swap generic".
630
	 */
631
	for (nv = cf->cf_swap; nv != NULL; nv = nv->nv_next)
632
	    if (nv->nv_str == s_generic)
633
		break;
634
	if (nv != NULL) {
635
		/*
636
		 * Make sure no root or dump device specified, and no
637
		 * other swap devices.  Note single | here (check all).
638
		 */
639
		nv = cf->cf_swap;
640
		if (exclude(cf->cf_root, name, "root device") |
641
		    exclude(nv->nv_next, name, "additional swap devices") |
642
		    exclude(cf->cf_dump, name, "dump device"))
643
			goto bad;
644
	} else {
645
		nv = cf->cf_root;
646
		if (nv == NULL) {
647
			error("%s: no root device specified", name);
648
			goto bad;
649
		}
650
		if (resolve(&cf->cf_root, name, "root", nv, 'a') |
651
		    lresolve(&cf->cf_swap, name, "swap", nv, 'b') |
652
		    resolve(&cf->cf_dump, name, "dumps", nv, 'b'))
653
			goto bad;
654
	}
655
	*nextcf = cf;
656
	nextcf = &cf->cf_next;
657
	return;
658
bad:
659
	nvfreel(cf0->cf_root);
660
	nvfreel(cf0->cf_swap);
661
	nvfreel(cf0->cf_dump);
662
}
663
664
void
665
setconf(struct nvlist **npp, const char *what, struct nvlist *v)
666
{
667
668
	if (*npp != NULL) {
669
		error("duplicate %s specification", what);
670
		nvfreel(v);
671
	} else
672
		*npp = v;
673
}
674
675
static struct devi *
676
newdevi(const char *name, int unit, struct devbase *d)
677
{
678
	struct devi *i;
679
680
	i = emalloc(sizeof *i);
681
	i->i_name = name;
682
	i->i_unit = unit;
683
	i->i_base = d;
684
	i->i_next = NULL;
685
	i->i_bsame = NULL;
686
	i->i_asame = NULL;
687
	i->i_alias = NULL;
688
	i->i_at = NULL;
689
	i->i_atattr = NULL;
690
	i->i_atdev = NULL;
691
	i->i_atdeva = NULL;
692
	i->i_locs = NULL;
693
	i->i_cfflags = 0;
694
	i->i_cfindex = -1;
695
	i->i_lineno = currentline();
696
	if (unit >= d->d_umax)
697
		d->d_umax = unit + 1;
698
	return (i);
699
}
700
701
/*
702
 * Enable an already declared but disabled device.
703
 */
704
void
705
enabledev(const char *name, const char *at)
706
{
707
	struct devbase *ib, *ab;
708
	char atbuf[NAMESIZE];
709
	struct attr *attr;
710
	struct nvlist *nv;
711
	struct devi *i;
712
	const char *cp;
713
	int atunit;
714
715
	i = ht_lookup(devitab, name);
716
	if (i == NULL) {
717
		error("invalid device `%s'", name);
718
		return;
719
	}
720
	ib = i->i_base;
721
722
	if (split(at, strlen(at), atbuf, sizeof atbuf, &atunit)) {
723
		error("invalid attachment name `%s'", at);
724
		return;
725
	}
726
	cp = intern(atbuf);
727
	ab = ht_lookup(devbasetab, cp);
728
	if (ab == NULL) {
729
		error("invalid attachment device `%s'", cp);
730
		return;
731
	}
732
	for (nv = ab->d_attrs; nv != NULL; nv = nv->nv_next) {
733
		attr = nv->nv_ptr;
734
		if (onlist(attr->a_devs, ib))
735
			goto foundattachment;
736
	}
737
	error("%s's cannot attach to %s's", ib->d_name, atbuf);
738
	return;
739
740
foundattachment:
741
	while (i && i->i_atdev != ab)
742
		i = i->i_alias;
743
	if (i == NULL) {
744
		error("%s at %s not found", name, at);
745
		return;
746
	} else
747
		i->i_disable = 0; /* Enable */
748
}
749
750
/*
751
 * Add the named device as attaching to the named attribute (or perhaps
752
 * another device instead) plus unit number.
753
 */
754
void
755
adddev(const char *name, const char *at, struct nvlist *loclist, int flags,
756
    int disable)
757
{
758
	struct devi *i;	/* the new instance */
759
	struct attr *attr;	/* attribute that allows attach */
760
	struct devbase *ib;	/* i->i_base */
761
	struct devbase *ab;	/* not NULL => at another dev */
762
	struct nvlist *nv;
763
	struct deva *iba;	/* devbase attachment used */
764
	const char *cp;
765
	int atunit;
766
	char atbuf[NAMESIZE];
767
	int hit;
768
769
	ab = NULL;
770
	iba = NULL;
771
	if (at == NULL) {
772
		/* "at root" */
773
		if ((i = getdevi(name)) == NULL)
774
			goto bad;
775
		/*
776
		 * Must warn about i_unit > 0 later, after taking care of
777
		 * the STAR cases (we could do non-star's here but why
778
		 * bother?).  Make sure this device can be at root.
779
		 */
780
		ib = i->i_base;
781
		hit = 0;
782
		for (iba = ib->d_ahead; iba != NULL; iba = iba->d_bsame)
783
			if (onlist(iba->d_atlist, NULL)) {
784
				hit = 1;
785
				break;
786
			}
787
		if (!hit) {
788
			error("%s's cannot attach to the root", ib->d_name);
789
			goto bad;
790
		}
791
		attr = &errattr;	/* a convenient "empty" attr */
792
	} else {
793
		if (split(at, strlen(at), atbuf, sizeof atbuf, &atunit)) {
794
			error("invalid attachment name `%s'", at);
795
			/* (void)getdevi(name); -- ??? */
796
			goto bad;
797
		}
798
		if ((i = getdevi(name)) == NULL)
799
			goto bad;
800
		ib = i->i_base;
801
		cp = intern(atbuf);
802
803
		/*
804
		 * Devices can attach to two types of things: Attributes,
805
		 * and other devices (which have the appropriate attributes
806
		 * to allow attachment).
807
		 *
808
		 * (1) If we're attached to an attribute, then we don't need
809
		 *     look at the parent base device to see what attributes
810
		 *     it has, and make sure that we can attach to them.
811
		 *
812
		 * (2) If we're attached to a real device (i.e. named in
813
		 *     the config file), we want to remember that so that
814
		 *     at cross-check time, if the device we're attached to
815
		 *     is missing but other devices which also provide the
816
		 *     attribute are present, we don't get a false "OK."
817
		 *
818
		 * (3) If the thing we're attached to is an attribute
819
		 *     but is actually named in the config file, we still
820
		 *     have to remember its devbase.
821
		 */
822
823
		/* Figure out parent's devbase, to satisfy case (3). */
824
		ab = ht_lookup(devbasetab, cp);
825
826
		/* Find out if it's an attribute. */
827
		attr = ht_lookup(attrtab, cp);
828
829
		/* Make sure we're _really_ attached to the attr.  Case (1). */
830
		if (attr != NULL && onlist(attr->a_devs, ib))
831
			goto findattachment;
832
833
		/*
834
		 * Else a real device, and not just an attribute.  Case (2).
835
		 *
836
		 * Have to work a bit harder to see whether we have
837
		 * something like "tg0 at esp0" (where esp is merely
838
		 * not an attribute) or "tg0 at nonesuch0" (where
839
		 * nonesuch is not even a device).
840
		 */
841
		if (ab == NULL) {
842
			error("%s at %s: `%s' unknown",
843
			    name, at, atbuf);
844
			goto bad;
845
		}
846
847
		/*
848
		 * See if the named parent carries an attribute
849
		 * that allows it to supervise device ib.
850
		 */
851
		for (nv = ab->d_attrs; nv != NULL; nv = nv->nv_next) {
852
			attr = nv->nv_ptr;
853
			if (onlist(attr->a_devs, ib))
854
				goto findattachment;
855
		}
856
		error("%s's cannot attach to %s's", ib->d_name, atbuf);
857
		goto bad;
858
859
findattachment:
860
		/* find out which attachment it uses */
861
		hit = 0;
862
		for (iba = ib->d_ahead; iba != NULL; iba = iba->d_bsame)
863
			if (onlist(iba->d_atlist, attr)) {
864
				hit = 1;
865
				break;
866
			}
867
		if (!hit)
868
			panic("adddev: can't figure out attachment");
869
	}
870
	if ((i->i_locs = fixloc(name, attr, loclist)) == NULL)
871
		goto bad;
872
	i->i_at = at;
873
	i->i_atattr = attr;
874
	i->i_atdev = ab;
875
	i->i_atdeva = iba;
876
	i->i_atunit = atunit;
877
	i->i_cfflags = flags;
878
	i->i_disable = disable;
879
880
	*iba->d_ipp = i;
881
	iba->d_ipp = &i->i_asame;
882
883
	selectbase(ib, iba);
884
	/* all done, fall into ... */
885
bad:
886
	nvfreel(loclist);
887
	return;
888
}
889
890
void
891
addpseudo(const char *name, int number, int disable)
892
{
893
	struct devbase *d;
894
	struct devi *i;
895
896
	d = ht_lookup(devbasetab, name);
897
	if (d == NULL) {
898
		error("undefined pseudo-device %s", name);
899
		return;
900
	}
901
	if (!d->d_ispseudo) {
902
		error("%s is a real device, not a pseudo-device", name);
903
		return;
904
	}
905
	if (ht_lookup(devitab, name) != NULL) {
906
		warnx("warning: duplicate definition of `%s', will use latest definition", name);
907
		d->d_umax = number;
908
		return;
909
	}
910
	i = newdevi(name, number - 1, d);	/* foo 16 => "foo0..foo15" */
911
	if (ht_insert(devitab, name, i))
912
		panic("addpseudo(%s)", name);
913
	i->i_disable = disable;
914
	selectbase(d, NULL);
915
	*nextpseudo = i;
916
	nextpseudo = &i->i_next;
917
	npseudo++;
918
}
919
920
/*
921
 * Define a new instance of a specific device.
922
 */
923
static struct devi *
924
getdevi(const char *name)
925
{
926
	struct devi *i, *firsti;
927
	struct devbase *d;
928
	int unit;
929
	char base[NAMESIZE];
930
931
	if (split(name, strlen(name), base, sizeof base, &unit)) {
932
		error("invalid device name `%s'", name);
933
		return (NULL);
934
	}
935
	d = ht_lookup(devbasetab, intern(base));
936
	if (d == NULL) {
937
		error("%s: unknown device `%s'", name, base);
938
		return (NULL);
939
	}
940
	if (d->d_ispseudo) {
941
		error("%s: %s is a pseudo-device", name, base);
942
		return (NULL);
943
	}
944
	firsti = ht_lookup(devitab, name);
945
	i = newdevi(name, unit, d);
946
	if (firsti == NULL) {
947
		if (ht_insert(devitab, name, i))
948
			panic("getdevi(%s)", name);
949
		*d->d_ipp = i;
950
		d->d_ipp = &i->i_bsame;
951
	} else {
952
		while (firsti->i_alias)
953
			firsti = firsti->i_alias;
954
		firsti->i_alias = i;
955
	}
956
	*nextdevi = i;
957
	nextdevi = &i->i_next;
958
	ndevi++;
959
	return (i);
960
}
961
962
static const char *
963
concat(const char *name, int c)
964
{
965
	size_t len;
966
	char buf[NAMESIZE];
967
968
	len = strlen(name);
969
	if (len + 2 > sizeof(buf)) {
970
		error("device name `%s%c' too long", name, c);
971
		len = sizeof(buf) - 2;
972
	}
973
	bcopy(name, buf, len);
974
	buf[len] = c;
975
	buf[len + 1] = 0;
976
	return (intern(buf));
977
}
978
979
const char *
980
starref(const char *name)
981
{
982
983
	return (concat(name, '*'));
984
}
985
986
const char *
987
wildref(const char *name)
988
{
989
990
	return (concat(name, '?'));
991
}
992
993
/*
994
 * Split a name like "foo0" into base name (foo) and unit number (0).
995
 * Return 0 on success.  To make this useful for names like "foo0a",
996
 * the length of the "foo0" part is one of the arguments.
997
 */
998
static int
999
split(const char *name, size_t nlen, char *base, size_t bsize, int *aunit)
1000
{
1001
	const char *cp;
1002
	int c;
1003
	size_t l;
1004
1005
	l = nlen;
1006
	if (l < 2 || l >= bsize || isdigit((unsigned char)*name))
1007
		return (1);
1008
	c = (u_char)name[--l];
1009
	if (!isdigit((unsigned char)c)) {
1010
		if (c == '*')
1011
			*aunit = STAR;
1012
		else if (c == '?')
1013
			*aunit = WILD;
1014
		else
1015
			return (1);
1016
	} else {
1017
		cp = &name[l];
1018
		while (isdigit((unsigned char)cp[-1]))
1019
			l--, cp--;
1020
		*aunit = atoi(cp);
1021
	}
1022
	bcopy(name, base, l);
1023
	base[l] = 0;
1024
	return (0);
1025
}
1026
1027
/*
1028
 * We have an instance of the base foo, so select it and all its
1029
 * attributes for "optional foo".
1030
 */
1031
static void
1032
selectbase(struct devbase *d, struct deva *da)
1033
{
1034
	struct attr *a;
1035
	struct nvlist *nv;
1036
1037
	(void)ht_insert(selecttab, d->d_name, (char *)d->d_name);
1038
	for (nv = d->d_attrs; nv != NULL; nv = nv->nv_next) {
1039
		a = nv->nv_ptr;
1040
		(void)ht_insert(selecttab, a->a_name, (char *)a->a_name);
1041
	}
1042
	if (da != NULL) {
1043
		(void)ht_insert(selecttab, da->d_name, (char *)da->d_name);
1044
		for (nv = da->d_attrs; nv != NULL; nv = nv->nv_next) {
1045
			a = nv->nv_ptr;
1046
			(void)ht_insert(selecttab, a->a_name,
1047
			    (char *)a->a_name);
1048
		}
1049
	}
1050
}
1051
1052
/*
1053
 * Is the given pointer on the given list of pointers?
1054
 */
1055
static int
1056
onlist(struct nvlist *nv, void *ptr)
1057
{
1058
	for (; nv != NULL; nv = nv->nv_next)
1059
		if (nv->nv_ptr == ptr)
1060
			return (1);
1061
	return (0);
1062
}
1063
1064
static char *
1065
extend(char *p, const char *name)
1066
{
1067
	int l;
1068
1069
	l = strlen(name);
1070
	bcopy(name, p, l);
1071
	p += l;
1072
	*p++ = ',';
1073
	*p++ = ' ';
1074
	return (p);
1075
}
1076
1077
/*
1078
 * Check that we got all required locators, and default any that are
1079
 * given as "?" and have defaults.  Return 0 on success.
1080
 */
1081
static const char **
1082
fixloc(const char *name, struct attr *attr, struct nvlist *got)
1083
{
1084
	struct nvlist *m, *n;
1085
	int ord;
1086
	const char **lp;
1087
	int nmissing, nextra, nnodefault;
1088
	char *mp, *ep, *ndp;
1089
	char missing[1000], extra[1000], nodefault[1000];
1090
	static const char *nullvec[1];
1091
1092
	/*
1093
	 * Look for all required locators, and number the given ones
1094
	 * according to the required order.  While we are numbering,
1095
	 * set default values for defaulted locators.
1096
	 */
1097
	if (attr->a_loclen == 0)	/* e.g., "at root" */
1098
		lp = nullvec;
1099
	else
1100
		lp = ereallocarray(NULL, attr->a_loclen + 1,
1101
		    sizeof(const char *));
1102
	for (n = got; n != NULL; n = n->nv_next)
1103
		n->nv_int = -1;
1104
	nmissing = 0;
1105
	mp = missing;
1106
	/* yes, this is O(mn), but m and n should be small */
1107
	for (ord = 0, m = attr->a_locs; m != NULL; m = m->nv_next, ord++) {
1108
		for (n = got; n != NULL; n = n->nv_next) {
1109
			if (n->nv_name == m->nv_name) {
1110
				n->nv_int = ord;
1111
				break;
1112
			}
1113
		}
1114
		if (n == NULL && m->nv_int == 0) {
1115
			nmissing++;
1116
			mp = extend(mp, m->nv_name);
1117
		}
1118
		lp[ord] = m->nv_str;
1119
	}
1120
	if (ord != attr->a_loclen)
1121
		panic("fixloc");
1122
	lp[ord] = NULL;
1123
	nextra = 0;
1124
	ep = extra;
1125
	nnodefault = 0;
1126
	ndp = nodefault;
1127
	for (n = got; n != NULL; n = n->nv_next) {
1128
		if (n->nv_int >= 0) {
1129
			if (n->nv_str != NULL)
1130
				lp[n->nv_int] = n->nv_str;
1131
			else if (lp[n->nv_int] == NULL) {
1132
				nnodefault++;
1133
				ndp = extend(ndp, n->nv_name);
1134
			}
1135
		} else {
1136
			nextra++;
1137
			ep = extend(ep, n->nv_name);
1138
		}
1139
	}
1140
	if (nextra) {
1141
		ep[-2] = 0;	/* kill ", " */
1142
		error("%s: extraneous locator%s: %s",
1143
		    name, nextra > 1 ? "s" : "", extra);
1144
	}
1145
	if (nmissing) {
1146
		mp[-2] = 0;
1147
		error("%s: must specify %s", name, missing);
1148
	}
1149
	if (nnodefault) {
1150
		ndp[-2] = 0;
1151
		error("%s: cannot wildcard %s", name, nodefault);
1152
	}
1153
	if (nmissing || nnodefault) {
1154
		free(lp);
1155
		lp = NULL;
1156
	}
1157
	return (lp);
1158
}