xref: /openbsd-src/usr.sbin/config/sem.c (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1 /*	$OpenBSD: sem.c,v 1.31 2008/03/24 21:35:03 maja 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>
45 #include <ctype.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <err.h>
50 #include "config.h"
51 #include "sem.h"
52 
53 /*
54  * config semantics.
55  */
56 
57 #define	NAMESIZE	100	/* local name buffers */
58 
59 const char *s_generic;
60 const char *s_nfs;
61 
62 static struct hashtab *attrtab;		/* for attribute lookup */
63 static struct hashtab *cfhashtab;	/* for config lookup */
64 static struct hashtab *devitab;		/* etc */
65 
66 static struct attr errattr;
67 static struct devbase errdev;
68 static struct deva errdeva;
69 static struct devbase **nextbase;
70 static struct deva **nextdeva;
71 static struct config **nextcf;
72 static struct devi **nextdevi;
73 static struct devi **nextpseudo;
74 
75 static int has_errobj(struct nvlist *, void *);
76 static struct nvlist *addtoattr(struct nvlist *, struct devbase *);
77 static int exclude(struct nvlist *, const char *, const char *);
78 static int resolve(struct nvlist **, const char *, const char *,
79     struct nvlist *, int);
80 static int lresolve(struct nvlist **, const char *, const char *,
81     struct nvlist *, int);
82 static struct devi *newdevi(const char *, int, struct devbase *d);
83 static struct devi *getdevi(const char *);
84 static const char *concat(const char *, int);
85 static char *extend(char *, const char *);
86 static int split(const char *, size_t, char *, size_t, int *);
87 static void selectbase(struct devbase *, struct deva *);
88 static int onlist(struct nvlist *, void *);
89 static const char **fixloc(const char *, struct attr *, struct nvlist *);
90 
91 void
92 initsem(void)
93 {
94 
95 	attrtab = ht_new();
96 	errattr.a_name = "<internal>";
97 
98 	allbases = NULL;
99 	nextbase = &allbases;
100 
101 	alldevas = NULL;
102 	nextdeva = &alldevas;
103 
104 	cfhashtab = ht_new();
105 	allcf = NULL;
106 	nextcf = &allcf;
107 
108 	devitab = ht_new();
109 	alldevi = NULL;
110 	nextdevi = &alldevi;
111 	errdev.d_name = "<internal>";
112 
113 	allpseudo = NULL;
114 	nextpseudo = &allpseudo;
115 
116 	s_generic = intern("generic");
117 	s_nfs = intern("nfs");
118 }
119 
120 /* Name of include file just ended (set in scan.l) */
121 extern const char *lastfile;
122 
123 void
124 enddefs(void)
125 {
126 	struct devbase *dev;
127 
128 	for (dev = allbases; dev != NULL; dev = dev->d_next) {
129 		if (!dev->d_isdef) {
130 			(void)fprintf(stderr,
131 			    "%s: device `%s' used but not defined\n",
132 			    lastfile, dev->d_name);
133 			errors++;
134 			continue;
135 		}
136 	}
137 	if (errors) {
138 		(void)fprintf(stderr, "*** Stop.\n");
139 		exit(1);
140 	}
141 }
142 
143 void
144 setdefmaxusers(int min, int def, int max)
145 {
146 
147 	if (min < 1 || min > def || def > max)
148 		error("maxusers must have 1 <= min <= default <= max");
149 	else {
150 		minmaxusers = min;
151 		defmaxusers = def;
152 		maxmaxusers = max;
153 	}
154 }
155 
156 void
157 setmaxusers(int n)
158 {
159 
160 	if (maxusers != 0) {
161 		warnx("warning: duplicate maxusers parameter, will use latest definition (%d)", n);
162 	}
163 	maxusers = n;
164 	if (n < minmaxusers) {
165 		warnx("warning: minimum of %d maxusers assumed", minmaxusers);
166 		maxusers = minmaxusers;
167 	} else if (n > maxmaxusers) {
168 		warnx("warning: maxusers (%d) > %d", n, maxmaxusers);
169 	}
170 }
171 
172 /*
173  * Define an attribute, optionally with an interface (a locator list).
174  * Since an empty locator list is logically different from "no interface",
175  * all locator lists include a dummy head node, which we discard here.
176  */
177 int
178 defattr(const char *name, struct nvlist *locs)
179 {
180 	struct attr *a;
181 	struct nvlist *nv;
182 	int len;
183 
184 	a = emalloc(sizeof *a);
185 	if (ht_insert(attrtab, name, a)) {
186 		free(a);
187 		error("attribute `%s' already defined", name);
188 		nvfreel(locs);
189 		return (1);
190 	}
191 	a->a_name = name;
192 	if (locs != NULL) {
193 		a->a_iattr = 1;
194 		a->a_locs = locs->nv_next;
195 		nvfree(locs);
196 	} else {
197 		a->a_iattr = 0;
198 		a->a_locs = NULL;
199 	}
200 	len = 0;
201 	for (nv = a->a_locs; nv != NULL; nv = nv->nv_next)
202 		len++;
203 	a->a_loclen = len;
204 	a->a_devs = NULL;
205 	a->a_refs = NULL;
206 	return (0);
207 }
208 
209 /*
210  * Return true if the given `error object' is embedded in the given
211  * pointer list.
212  */
213 static int
214 has_errobj(struct nvlist *nv, void *obj)
215 {
216 
217 	for (; nv != NULL; nv = nv->nv_next)
218 		if (nv->nv_ptr == obj)
219 			return (1);
220 	return (0);
221 }
222 
223 /*
224  * Add a device base to a list in an attribute (actually, to any list).
225  * Note that this does not check for duplicates, and does reverse the
226  * list order, but no one cares anyway.
227  */
228 static struct nvlist *
229 addtoattr(struct nvlist *l, struct devbase *dev)
230 {
231 	struct nvlist *n;
232 
233 	n = newnv(NULL, NULL, dev, 0, l);
234 	return (n);
235 }
236 
237 /*
238  * Define a device.  This may (or may not) also define an interface
239  * attribute and/or refer to existing attributes.
240  */
241 void
242 defdev(struct devbase *dev, int ispseudo, struct nvlist *loclist,
243     struct nvlist *attrs)
244 {
245 	struct nvlist *nv;
246 	struct attr *a;
247 
248 	if (dev == &errdev)
249 		goto bad;
250 	if (dev->d_isdef) {
251 		error("redefinition of `%s'", dev->d_name);
252 		goto bad;
253 	}
254 	dev->d_isdef = 1;
255 	if (has_errobj(attrs, &errattr))
256 		goto bad;
257 
258 	/*
259 	 * Handle implicit attribute definition from locator list.  Do
260 	 * this before scanning the `at' list so that we can have, e.g.:
261 	 *	device foo at other, foo { slot = -1 }
262 	 * (where you can plug in a foo-bus extender to a foo-bus).
263 	 */
264 	if (loclist != NULL) {
265 		nv = loclist;
266 		loclist = NULL;	/* defattr disposes of them for us */
267 		if (defattr(dev->d_name, nv))
268 			goto bad;
269 		attrs = newnv(dev->d_name, NULL, getattr(dev->d_name), 0,
270 		    attrs);
271 	}
272 
273 	/* Committed!  Set up fields. */
274 	dev->d_ispseudo = ispseudo;
275 	dev->d_attrs = attrs;
276 
277 	/*
278 	 * For each interface attribute this device refers to, add this
279 	 * device to its reference list.  This makes, e.g., finding all
280 	 * "scsi"s easier.
281 	 */
282 	for (nv = attrs; nv != NULL; nv = nv->nv_next) {
283 		a = nv->nv_ptr;
284 		if (a->a_iattr)
285 			a->a_refs = addtoattr(a->a_refs, dev);
286 	}
287 	return;
288 bad:
289 	nvfreel(loclist);
290 	nvfreel(attrs);
291 }
292 
293 /*
294  * Look up a devbase.  Also makes sure it is a reasonable name,
295  * i.e., does not end in a digit or contain special characters.
296  */
297 struct devbase *
298 getdevbase(char *name)
299 {
300 	u_char *p;
301 	struct devbase *dev;
302 
303 	p = (u_char *)name;
304 	if (!isalpha(*p))
305 		goto badname;
306 	while (*++p) {
307 		if (!isalnum(*p) && *p != '_')
308 			goto badname;
309 	}
310 	if (isdigit(*--p)) {
311 badname:
312 		error("bad device base name `%s'", name);
313 		return (&errdev);
314 	}
315 	dev = ht_lookup(devbasetab, name);
316 	if (dev == NULL) {
317 		dev = emalloc(sizeof *dev);
318 		dev->d_name = name;
319 		dev->d_next = NULL;
320 		dev->d_isdef = 0;
321 		dev->d_major = NODEV;
322 		dev->d_attrs = NULL;
323 		dev->d_ihead = NULL;
324 		dev->d_ipp = &dev->d_ihead;
325 		dev->d_ahead = NULL;
326 		dev->d_app = &dev->d_ahead;
327 		dev->d_umax = 0;
328 		*nextbase = dev;
329 		nextbase = &dev->d_next;
330 		if (ht_insert(devbasetab, name, dev))
331 			panic("getdevbase(%s)", name);
332 	}
333 	return (dev);
334 }
335 
336 /*
337  * Define some of a device's allowable parent attachments.
338  * There may be a list of (plain) attributes.
339  */
340 void
341 defdevattach(struct deva *deva, struct devbase *dev, struct nvlist *atlist,
342     struct nvlist *attrs)
343 {
344 	struct nvlist *nv;
345 	struct attr *a;
346 	struct deva *da;
347 
348 	if (dev == &errdev)
349 		goto bad;
350 	if (deva == NULL)
351 		deva = getdevattach(dev->d_name);
352 	if (deva == &errdeva)
353 		goto bad;
354 	if (!dev->d_isdef) {
355 		error("attaching undefined device `%s'", dev->d_name);
356 		goto bad;
357 	}
358 	if (deva->d_isdef) {
359 		error("redefinition of `%s'", deva->d_name);
360 		goto bad;
361 	}
362 	if (dev->d_ispseudo) {
363 		error("pseudo-devices can't attach");
364 		goto bad;
365 	}
366 
367 	deva->d_isdef = 1;
368 	if (has_errobj(attrs, &errattr))
369 		goto bad;
370 	for (nv = attrs; nv != NULL; nv = nv->nv_next) {
371 		a = nv->nv_ptr;
372 		if (a == &errattr)
373 			continue;		/* already complained */
374 		if (a->a_iattr)
375 			error("`%s' is not a plain attribute", a->a_name);
376 	}
377 
378 	/* Committed!  Set up fields. */
379 	deva->d_attrs = attrs;
380 	deva->d_atlist = atlist;
381 	deva->d_devbase = dev;
382 
383 	/*
384 	 * Turn the `at' list into interface attributes (map each
385 	 * nv_name to an attribute, or to NULL for root), and add
386 	 * this device to those attributes, so that children can
387 	 * be listed at this particular device if they are supported
388 	 * by that attribute.
389 	 */
390 	for (nv = atlist; nv != NULL; nv = nv->nv_next) {
391 		if (nv->nv_name == NULL)
392 			nv->nv_ptr = a = NULL;	/* at root */
393 		else
394 			nv->nv_ptr = a = getattr(nv->nv_name);
395 		if (a == &errattr)
396 			continue;		/* already complained */
397 
398 		/*
399 		 * Make sure that an attachment spec doesn't
400 		 * already say how to attach to this attribute.
401 		 */
402 		for (da = dev->d_ahead; da != NULL; da = da->d_bsame)
403 			if (onlist(da->d_atlist, a))
404 				error("attach at `%s' already done by `%s'",
405 				    a ? a->a_name : "root", da->d_name);
406 
407 		if (a == NULL)
408 			continue;		/* at root; don't add */
409 		if (!a->a_iattr)
410 			error("%s cannot be at plain attribute `%s'",
411 			    dev->d_name, a->a_name);
412 		else
413 			a->a_devs = addtoattr(a->a_devs, dev);
414 	}
415 
416 	/* attach to parent */
417 	*dev->d_app = deva;
418 	dev->d_app = &deva->d_bsame;
419 	return;
420 bad:
421 	nvfreel(atlist);
422 	nvfreel(attrs);
423 }
424 
425 /*
426  * Look up a device attachment.  Also makes sure it is a reasonable
427  * name, i.e., does not contain digits or special characters.
428  */
429 struct deva *
430 getdevattach(const char *name)
431 {
432 	u_char *p;
433 	struct deva *deva;
434 
435 	p = (u_char *)name;
436 	if (!isalpha(*p))
437 		goto badname;
438 	while (*++p) {
439 		if (!isalnum(*p) && *p != '_')
440 			goto badname;
441 	}
442 	if (isdigit(*--p)) {
443 badname:
444 		error("bad device attachment name `%s'", name);
445 		return (&errdeva);
446 	}
447 	deva = ht_lookup(devatab, name);
448 	if (deva == NULL) {
449 		deva = emalloc(sizeof *deva);
450 		deva->d_name = name;
451 		deva->d_next = NULL;
452 		deva->d_bsame = NULL;
453 		deva->d_isdef = 0;
454 		deva->d_devbase = NULL;
455 		deva->d_atlist = NULL;
456 		deva->d_attrs = NULL;
457 		deva->d_ihead = NULL;
458 		deva->d_ipp = &deva->d_ihead;
459 		*nextdeva = deva;
460 		nextdeva = &deva->d_next;
461 		if (ht_insert(devatab, name, deva))
462 			panic("getdeva(%s)", name);
463 	}
464 	return (deva);
465 }
466 
467 /*
468  * Look up an attribute.
469  */
470 struct attr *
471 getattr(const char *name)
472 {
473 	struct attr *a;
474 
475 	if ((a = ht_lookup(attrtab, name)) == NULL) {
476 		error("undefined attribute `%s'", name);
477 		a = &errattr;
478 	}
479 	return (a);
480 }
481 
482 /*
483  * Set the major device number for a device, so that it can be used
484  * as a root/swap/dumps "on" device in a configuration.
485  */
486 void
487 setmajor(struct devbase *d, int n)
488 {
489 
490 	if (d != &errdev && d->d_major != NODEV)
491 		error("device `%s' is already major %d",
492 		    d->d_name, d->d_major);
493 	else
494 		d->d_major = n;
495 }
496 
497 #define ABS(x) ((x) < 0 ? -(x) : (x))
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(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(*name))
1007 		return (1);
1008 	c = (u_char)name[--l];
1009 	if (!isdigit(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(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 = emalloc((attr->a_loclen + 1) * sizeof(const char *));
1101 	for (n = got; n != NULL; n = n->nv_next)
1102 		n->nv_int = -1;
1103 	nmissing = 0;
1104 	mp = missing;
1105 	/* yes, this is O(mn), but m and n should be small */
1106 	for (ord = 0, m = attr->a_locs; m != NULL; m = m->nv_next, ord++) {
1107 		for (n = got; n != NULL; n = n->nv_next) {
1108 			if (n->nv_name == m->nv_name) {
1109 				n->nv_int = ord;
1110 				break;
1111 			}
1112 		}
1113 		if (n == NULL && m->nv_int == 0) {
1114 			nmissing++;
1115 			mp = extend(mp, m->nv_name);
1116 		}
1117 		lp[ord] = m->nv_str;
1118 	}
1119 	if (ord != attr->a_loclen)
1120 		panic("fixloc");
1121 	lp[ord] = NULL;
1122 	nextra = 0;
1123 	ep = extra;
1124 	nnodefault = 0;
1125 	ndp = nodefault;
1126 	for (n = got; n != NULL; n = n->nv_next) {
1127 		if (n->nv_int >= 0) {
1128 			if (n->nv_str != NULL)
1129 				lp[n->nv_int] = n->nv_str;
1130 			else if (lp[n->nv_int] == NULL) {
1131 				nnodefault++;
1132 				ndp = extend(ndp, n->nv_name);
1133 			}
1134 		} else {
1135 			nextra++;
1136 			ep = extend(ep, n->nv_name);
1137 		}
1138 	}
1139 	if (nextra) {
1140 		ep[-2] = 0;	/* kill ", " */
1141 		error("%s: extraneous locator%s: %s",
1142 		    name, nextra > 1 ? "s" : "", extra);
1143 	}
1144 	if (nmissing) {
1145 		mp[-2] = 0;
1146 		error("%s: must specify %s", name, missing);
1147 	}
1148 	if (nnodefault) {
1149 		ndp[-2] = 0;
1150 		error("%s: cannot wildcard %s", name, nodefault);
1151 	}
1152 	if (nmissing || nnodefault) {
1153 		free(lp);
1154 		lp = NULL;
1155 	}
1156 	return (lp);
1157 }
1158