1 /*	$NetBSD: sem.c,v 1.74 2016/04/29 18:18:22 mlelstv Exp $	*/
2 
3 /*
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This software was developed by the Computer Systems Engineering group
8  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9  * contributed to Berkeley.
10  *
11  * All advertising materials mentioning features or use of this software
12  * must display the following acknowledgement:
13  *	This product includes software developed by the University of
14  *	California, Lawrence Berkeley Laboratories.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  * 3. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *	from: @(#)sem.c	8.1 (Berkeley) 6/6/93
41  */
42 
43 #if HAVE_NBTOOL_CONFIG_H
44 #include "nbtool_config.h"
45 #endif
46 
47 #include <sys/cdefs.h>
48 __RCSID("$NetBSD: sem.c,v 1.74 2016/04/29 18:18:22 mlelstv Exp $");
49 
50 #include <sys/param.h>
51 #include <ctype.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <util.h>
56 #include "defs.h"
57 #include "sem.h"
58 
59 /*
60  * config semantics.
61  */
62 
63 #define	NAMESIZE	100	/* local name buffers */
64 
65 const char *s_ifnet;		/* magic attribute */
66 const char *s_qmark;
67 const char *s_none;
68 
69 static struct hashtab *cfhashtab;	/* for config lookup */
70 struct hashtab *devitab;		/* etc */
71 struct attr allattr;
72 size_t nattrs;
73 
74 static struct attr errattr;
75 static struct devbase errdev;
76 static struct deva errdeva;
77 
78 static int has_errobj(struct attrlist *, struct attr *);
79 static struct nvlist *addtoattr(struct nvlist *, struct devbase *);
80 static int resolve(struct nvlist **, const char *, const char *,
81 		   struct nvlist *, int);
82 static struct pspec *getpspec(struct attr *, struct devbase *, int);
83 static struct devi *newdevi(const char *, int, struct devbase *d);
84 static struct devi *getdevi(const char *);
85 static void remove_devi(struct devi *);
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 const char **fixloc(const char *, struct attr *, struct loclist *);
91 static const char *makedevstr(devmajor_t, devminor_t);
92 static const char *major2name(devmajor_t);
93 static devmajor_t dev2major(struct devbase *);
94 
95 extern const char *yyfile;
96 extern int vflag;
97 
98 void
initsem(void)99 initsem(void)
100 {
101 
102 	attrtab = ht_new();
103 	attrdeptab = ht_new();
104 
105 	allattr.a_name = "netbsd";
106 	TAILQ_INIT(&allattr.a_files);
107 	(void)ht_insert(attrtab, allattr.a_name, &allattr);
108 	selectattr(&allattr);
109 
110 	errattr.a_name = "<internal>";
111 
112 	TAILQ_INIT(&allbases);
113 
114 	TAILQ_INIT(&alldevas);
115 
116 	TAILQ_INIT(&allpspecs);
117 
118 	cfhashtab = ht_new();
119 	TAILQ_INIT(&allcf);
120 
121 	TAILQ_INIT(&alldevi);
122 	errdev.d_name = "<internal>";
123 
124 	TAILQ_INIT(&allpseudo);
125 
126 	TAILQ_INIT(&alldevms);
127 
128 	s_ifnet = intern("ifnet");
129 	s_qmark = intern("?");
130 	s_none = intern("none");
131 }
132 
133 /* Name of include file just ended (set in scan.l) */
134 extern const char *lastfile;
135 
136 static struct attr *
finddep(struct attr * a,const char * name)137 finddep(struct attr *a, const char *name)
138 {
139 	struct attrlist *al;
140 
141 	for (al = a->a_deps; al != NULL; al = al->al_next) {
142 		struct attr *this = al->al_this;
143 		if (strcmp(this->a_name, name) == 0)
144 			return this;
145 	}
146 	return NULL;
147 }
148 
149 static void
mergedeps(const char * dname,const char * name)150 mergedeps(const char *dname, const char *name)
151 {
152 	struct attr *a, *newa;
153 
154 	CFGDBG(4, "merging attr `%s' to devbase `%s'", name, dname);
155 	a = refattr(dname);
156 	if (finddep(a, name) == NULL) {
157 		newa = refattr(name);
158 		a->a_deps = attrlist_cons(a->a_deps, newa);
159 		CFGDBG(3, "attr `%s' merged to attr `%s'", newa->a_name,
160 		    a->a_name);
161 	}
162 }
163 
164 static void
fixdev(struct devbase * dev)165 fixdev(struct devbase *dev)
166 {
167 	struct attrlist *al;
168 	struct attr *devattr, *a;
169 
170 	devattr = refattr(dev->d_name);
171 	if (devattr->a_devclass)
172 		panic("%s: dev %s is devclass!", __func__, devattr->a_name);
173 
174 	CFGDBG(4, "fixing devbase `%s'", dev->d_name);
175 	for (al = dev->d_attrs; al != NULL; al = al->al_next) {
176 		a = al->al_this;
177 		CFGDBG(4, "fixing devbase `%s' attr `%s'", dev->d_name, a->a_name);
178 		if (a->a_iattr) {
179 			a->a_refs = addtoattr(a->a_refs, dev);
180 			CFGDBG(3, "device `%s' has iattr `%s'", dev->d_name,
181 			    a->a_name);
182 		} else if (a->a_devclass != NULL) {
183 			if (dev->d_classattr != NULL && dev->d_classattr != a) {
184 				cfgwarn("device `%s' has multiple classes "
185 				    "(`%s' and `%s')",
186 				    dev->d_name, dev->d_classattr->a_name,
187 				    a->a_name);
188 			}
189 			if (dev->d_classattr == NULL) {
190 				dev->d_classattr = a;
191 				CFGDBG(3, "device `%s' is devclass `%s'", dev->d_name,
192 				    a->a_name);
193 			}
194 		} else {
195 			if (strcmp(dev->d_name, a->a_name) != 0) {
196 				mergedeps(dev->d_name, a->a_name);
197 			}
198 		}
199 	}
200 }
201 
202 void
enddefs(void)203 enddefs(void)
204 {
205 	struct devbase *dev;
206 
207 	yyfile = "enddefs";
208 
209 	TAILQ_FOREACH(dev, &allbases, d_next) {
210 		if (!dev->d_isdef) {
211 			(void)fprintf(stderr,
212 			    "%s: device `%s' used but not defined\n",
213 			    lastfile, dev->d_name);
214 			errors++;
215 			continue;
216 		}
217 		fixdev(dev);
218 	}
219 	if (errors) {
220 		(void)fprintf(stderr, "*** Stop.\n");
221 		exit(1);
222 	}
223 }
224 
225 void
setdefmaxusers(int min,int def,int max)226 setdefmaxusers(int min, int def, int max)
227 {
228 
229 	if (min < 1 || min > def || def > max)
230 		cfgerror("maxusers must have 1 <= min (%d) <= default (%d) "
231 		    "<= max (%d)", min, def, max);
232 	else {
233 		minmaxusers = min;
234 		defmaxusers = def;
235 		maxmaxusers = max;
236 	}
237 }
238 
239 void
setmaxusers(int n)240 setmaxusers(int n)
241 {
242 
243 	if (maxusers == n) {
244 		cfgerror("duplicate maxusers parameter");
245 		return;
246 	}
247 	if (vflag && maxusers != 0)
248 		cfgwarn("warning: maxusers already defined");
249 	maxusers = n;
250 	if (n < minmaxusers) {
251 		cfgerror("warning: minimum of %d maxusers assumed",
252 		    minmaxusers);
253 		errors--;	/* take it away */
254 		maxusers = minmaxusers;
255 	} else if (n > maxmaxusers) {
256 		cfgerror("warning: maxusers (%d) > %d", n, maxmaxusers);
257 		errors--;
258 	}
259 }
260 
261 void
setident(const char * i)262 setident(const char *i)
263 {
264 
265 	if (i)
266 		ident = intern(i);
267 	else
268 		ident = NULL;
269 }
270 
271 /*
272  * Define an attribute, optionally with an interface (a locator list)
273  * and a set of attribute-dependencies.
274  *
275  * Attribute dependencies MAY NOT be interface attributes.
276  *
277  * Since an empty locator list is logically different from "no interface",
278  * all locator lists include a dummy head node, which we discard here.
279  */
280 int
defattr0(const char * name,struct loclist * locs,struct attrlist * deps,int devclass)281 defattr0(const char *name, struct loclist *locs, struct attrlist *deps,
282     int devclass)
283 {
284 
285 	if (locs != NULL)
286 		return defiattr(name, locs, deps, devclass);
287 	else if (devclass)
288 		return defdevclass(name, locs, deps, devclass);
289 	else
290 		return defattr(name, locs, deps, devclass);
291 }
292 
293 int
defattr(const char * name,struct loclist * locs,struct attrlist * deps,int devclass)294 defattr(const char *name, struct loclist *locs, struct attrlist *deps,
295     int devclass)
296 {
297 	struct attr *a, *dep;
298 	struct attrlist *al;
299 
300 	/*
301 	 * If this attribute depends on any others, make sure none of
302 	 * the dependencies are interface attributes.
303 	 */
304 	for (al = deps; al != NULL; al = al->al_next) {
305 		dep = al->al_this;
306 		if (dep->a_iattr) {
307 			cfgerror("`%s' dependency `%s' is an interface "
308 			    "attribute", name, dep->a_name);
309 			return (1);
310 		}
311 		(void)ht_insert2(attrdeptab, name, dep->a_name, NULL);
312 		CFGDBG(2, "attr `%s' depends on attr `%s'", name, dep->a_name);
313 	}
314 
315 	if (getrefattr(name, &a)) {
316 		cfgerror("attribute `%s' already defined", name);
317 		loclist_destroy(locs);
318 		return (1);
319 	}
320 	if (a == NULL)
321 		a = mkattr(name);
322 
323 	a->a_deps = deps;
324 	expandattr(a, NULL);
325 	CFGDBG(3, "attr `%s' defined", a->a_name);
326 
327 	return (0);
328 }
329 
330 struct attr *
mkattr(const char * name)331 mkattr(const char *name)
332 {
333 	struct attr *a;
334 
335 	a = ecalloc(1, sizeof *a);
336 	if (ht_insert(attrtab, name, a)) {
337 		free(a);
338 		return NULL;
339 	}
340 	a->a_name = name;
341 	TAILQ_INIT(&a->a_files);
342 	CFGDBG(3, "attr `%s' allocated", name);
343 
344 	return a;
345 }
346 
347 /* "interface attribute" initialization */
348 int
defiattr(const char * name,struct loclist * locs,struct attrlist * deps,int devclass)349 defiattr(const char *name, struct loclist *locs, struct attrlist *deps,
350     int devclass)
351 {
352 	struct attr *a;
353 	int len;
354 	struct loclist *ll;
355 
356 	if (devclass)
357 		panic("defattr(%s): locators and devclass", name);
358 
359 	if (defattr(name, locs, deps, devclass) != 0)
360 		return (1);
361 
362 	a = getattr(name);
363 	a->a_iattr = 1;
364 	/* unwrap */
365 	a->a_locs = locs->ll_next;
366 	locs->ll_next = NULL;
367 	loclist_destroy(locs);
368 	len = 0;
369 	for (ll = a->a_locs; ll != NULL; ll = ll->ll_next)
370 		len++;
371 	a->a_loclen = len;
372 	if (deps)
373 		CFGDBG(2, "attr `%s' iface with deps", a->a_name);
374 	return (0);
375 }
376 
377 /* "device class" initialization */
378 int
defdevclass(const char * name,struct loclist * locs,struct attrlist * deps,int devclass)379 defdevclass(const char *name, struct loclist *locs, struct attrlist *deps,
380     int devclass)
381 {
382 	struct attr *a;
383 	char classenum[256], *cp;
384 	int errored = 0;
385 
386 	if (deps)
387 		panic("defattr(%s): dependencies and devclass", name);
388 
389 	if (defattr(name, locs, deps, devclass) != 0)
390 		return (1);
391 
392 	a = getattr(name);
393 	(void)snprintf(classenum, sizeof(classenum), "DV_%s", name);
394 	for (cp = classenum + 3; *cp; cp++) {
395 		if (!errored &&
396 		    (!isalnum((unsigned char)*cp) ||
397 		      (isalpha((unsigned char)*cp) && !islower((unsigned char)*cp)))) {
398 			cfgerror("device class names must be "
399 			    "lower-case alphanumeric characters");
400 			errored = 1;
401 		}
402 		*cp = (char)toupper((unsigned char)*cp);
403 	}
404 	a->a_devclass = intern(classenum);
405 
406 	return (0);
407 }
408 
409 /*
410  * Return true if the given `error object' is embedded in the given
411  * pointer list.
412  */
413 static int
has_errobj(struct attrlist * al,struct attr * obj)414 has_errobj(struct attrlist *al, struct attr *obj)
415 {
416 
417 	for (; al != NULL; al = al->al_next)
418 		if (al->al_this == obj)
419 			return (1);
420 	return (0);
421 }
422 
423 /*
424  * Return true if the given attribute is embedded in the given
425  * pointer list.
426  */
427 int
has_attr(struct attrlist * al,const char * attr)428 has_attr(struct attrlist *al, const char *attr)
429 {
430 	struct attr *a;
431 
432 	if ((a = getattr(attr)) == NULL)
433 		return (0);
434 
435 	for (; al != NULL; al = al->al_next)
436 		if (al->al_this == a)
437 			return (1);
438 	return (0);
439 }
440 
441 /*
442  * Add a device base to a list in an attribute (actually, to any list).
443  * Note that this does not check for duplicates, and does reverse the
444  * list order, but no one cares anyway.
445  */
446 static struct nvlist *
addtoattr(struct nvlist * l,struct devbase * dev)447 addtoattr(struct nvlist *l, struct devbase *dev)
448 {
449 	struct nvlist *n;
450 
451 	n = newnv(NULL, NULL, dev, 0, l);
452 	return (n);
453 }
454 
455 /*
456  * Define a device.  This may (or may not) also define an interface
457  * attribute and/or refer to existing attributes.
458  */
459 void
defdev(struct devbase * dev,struct loclist * loclist,struct attrlist * attrs,int ispseudo)460 defdev(struct devbase *dev, struct loclist *loclist, struct attrlist *attrs,
461        int ispseudo)
462 {
463 	struct loclist *ll;
464 	struct attrlist *al;
465 
466 	if (dev == &errdev)
467 		goto bad;
468 	if (dev->d_isdef) {
469 		cfgerror("redefinition of `%s'", dev->d_name);
470 		goto bad;
471 	}
472 
473 	dev->d_isdef = 1;
474 	if (has_errobj(attrs, &errattr))
475 		goto bad;
476 
477 	/*
478 	 * Handle implicit attribute definition from locator list.  Do
479 	 * this before scanning the `at' list so that we can have, e.g.:
480 	 *	device foo at other, foo { slot = -1 }
481 	 * (where you can plug in a foo-bus extender to a foo-bus).
482 	 */
483 	if (loclist != NULL) {
484 		ll = loclist;
485 		loclist = NULL;	/* defattr disposes of them for us */
486 		if (defiattr(dev->d_name, ll, NULL, 0))
487 			goto bad;
488 		attrs = attrlist_cons(attrs, getattr(dev->d_name));
489 		/* This used to be stored but was never used */
490 		/* attrs->al_name = dev->d_name; */
491 	}
492 
493 	/*
494 	 * Pseudo-devices can have children.  Consider them as
495 	 * attaching at root.
496 	 */
497 	if (ispseudo) {
498 		for (al = attrs; al != NULL; al = al->al_next)
499 			if (al->al_this->a_iattr)
500 				break;
501 		if (al != NULL) {
502 			if (ispseudo < 2) {
503 				if (version >= 20080610)
504 					cfgerror("interface attribute on "
505 					 "non-device pseudo `%s'", dev->d_name);
506 				else {
507 					ispseudo = 2;
508 				}
509 			}
510 			ht_insert(devroottab, dev->d_name, dev);
511 		}
512 	}
513 
514 	/* Committed!  Set up fields. */
515 	dev->d_ispseudo = ispseudo;
516 	dev->d_attrs = attrs;
517 	dev->d_classattr = NULL;		/* for now */
518 	CFGDBG(3, "dev `%s' defined", dev->d_name);
519 
520 	/*
521 	 * Implicit attribute definition for device.
522 	 */
523 	refattr(dev->d_name);
524 
525 	/*
526 	 * For each interface attribute this device refers to, add this
527 	 * device to its reference list.  This makes, e.g., finding all
528 	 * "scsi"s easier.
529 	 *
530 	 * While looking through the attributes, set up the device
531 	 * class if any are devclass attributes (and error out if the
532 	 * device has two classes).
533 	 */
534 	for (al = attrs; al != NULL; al = al->al_next) {
535 		/*
536 		 * Implicit attribute definition for device dependencies.
537 		 */
538 		refattr(al->al_this->a_name);
539 		(void)ht_insert2(attrdeptab, dev->d_name, al->al_this->a_name, NULL);
540 		CFGDBG(2, "device `%s' depends on attr `%s'", dev->d_name,
541 		    al->al_this->a_name);
542 	}
543 	return;
544  bad:
545 	loclist_destroy(loclist);
546 	attrlist_destroyall(attrs);
547 }
548 
549 /*
550  * Look up a devbase.  Also makes sure it is a reasonable name,
551  * i.e., does not end in a digit or contain special characters.
552  */
553 struct devbase *
getdevbase(const char * name)554 getdevbase(const char *name)
555 {
556 	const u_char *p;
557 	struct devbase *dev;
558 
559 	p = (const u_char *)name;
560 	if (!isalpha(*p))
561 		goto badname;
562 	while (*++p) {
563 		if (!isalnum(*p) && *p != '_')
564 			goto badname;
565 	}
566 	if (isdigit(*--p)) {
567  badname:
568 		cfgerror("bad device base name `%s'", name);
569 		return (&errdev);
570 	}
571 	dev = ht_lookup(devbasetab, name);
572 	if (dev == NULL) {
573 		dev = ecalloc(1, sizeof *dev);
574 		dev->d_name = name;
575 		dev->d_isdef = 0;
576 		dev->d_major = NODEVMAJOR;
577 		dev->d_attrs = NULL;
578 		dev->d_ihead = NULL;
579 		dev->d_ipp = &dev->d_ihead;
580 		dev->d_ahead = NULL;
581 		dev->d_app = &dev->d_ahead;
582 		dev->d_umax = 0;
583 		TAILQ_INSERT_TAIL(&allbases, dev, d_next);
584 		if (ht_insert(devbasetab, name, dev))
585 			panic("getdevbase(%s)", name);
586 		CFGDBG(3, "devbase defined `%s'", dev->d_name);
587 	}
588 	return (dev);
589 }
590 
591 /*
592  * Define some of a device's allowable parent attachments.
593  * There may be a list of (plain) attributes.
594  */
595 void
defdevattach(struct deva * deva,struct devbase * dev,struct nvlist * atlist,struct attrlist * attrs)596 defdevattach(struct deva *deva, struct devbase *dev, struct nvlist *atlist,
597 	     struct attrlist *attrs)
598 {
599 	struct nvlist *nv;
600 	struct attrlist *al;
601 	struct attr *a;
602 	struct deva *da;
603 
604 	if (dev == &errdev)
605 		goto bad;
606 	if (deva == NULL)
607 		deva = getdevattach(dev->d_name);
608 	if (deva == &errdeva)
609 		goto bad;
610 	if (!dev->d_isdef) {
611 		cfgerror("attaching undefined device `%s'", dev->d_name);
612 		goto bad;
613 	}
614 	if (deva->d_isdef) {
615 		cfgerror("redefinition of `%s'", deva->d_name);
616 		goto bad;
617 	}
618 	if (dev->d_ispseudo) {
619 		cfgerror("pseudo-devices can't attach");
620 		goto bad;
621 	}
622 
623 	deva->d_isdef = 1;
624 	if (has_errobj(attrs, &errattr))
625 		goto bad;
626 	for (al = attrs; al != NULL; al = al->al_next) {
627 		a = al->al_this;
628 		if (a == &errattr)
629 			continue;		/* already complained */
630 		if (a->a_iattr || a->a_devclass != NULL)
631 			cfgerror("`%s' is not a plain attribute", a->a_name);
632 	}
633 
634 	/* Committed!  Set up fields. */
635 	deva->d_attrs = attrs;
636 	deva->d_atlist = atlist;
637 	deva->d_devbase = dev;
638 	CFGDBG(3, "deva `%s' defined", deva->d_name);
639 
640 	/*
641 	 * Implicit attribute definition for device attachment.
642 	 */
643 	refattr(deva->d_name);
644 
645 	/*
646 	 * Turn the `at' list into interface attributes (map each
647 	 * nv_name to an attribute, or to NULL for root), and add
648 	 * this device to those attributes, so that children can
649 	 * be listed at this particular device if they are supported
650 	 * by that attribute.
651 	 */
652 	for (nv = atlist; nv != NULL; nv = nv->nv_next) {
653 		if (nv->nv_name == NULL)
654 			nv->nv_ptr = a = NULL;	/* at root */
655 		else
656 			nv->nv_ptr = a = getattr(nv->nv_name);
657 		if (a == &errattr)
658 			continue;		/* already complained */
659 
660 		/*
661 		 * Make sure that an attachment spec doesn't
662 		 * already say how to attach to this attribute.
663 		 */
664 		for (da = dev->d_ahead; da != NULL; da = da->d_bsame)
665 			if (onlist(da->d_atlist, a))
666 				cfgerror("attach at `%s' already done by `%s'",
667 				     a ? a->a_name : "root", da->d_name);
668 
669 		if (a == NULL) {
670 			ht_insert(devroottab, dev->d_name, dev);
671 			continue;		/* at root; don't add */
672 		}
673 		if (!a->a_iattr)
674 			cfgerror("%s cannot be at plain attribute `%s'",
675 			    dev->d_name, a->a_name);
676 		else
677 			a->a_devs = addtoattr(a->a_devs, dev);
678 	}
679 
680 	/* attach to parent */
681 	*dev->d_app = deva;
682 	dev->d_app = &deva->d_bsame;
683 	return;
684  bad:
685 	nvfreel(atlist);
686 	attrlist_destroyall(attrs);
687 }
688 
689 /*
690  * Look up a device attachment.  Also makes sure it is a reasonable
691  * name, i.e., does not contain digits or special characters.
692  */
693 struct deva *
getdevattach(const char * name)694 getdevattach(const char *name)
695 {
696 	const u_char *p;
697 	struct deva *deva;
698 
699 	p = (const u_char *)name;
700 	if (!isalpha(*p))
701 		goto badname;
702 	while (*++p) {
703 		if (!isalnum(*p) && *p != '_')
704 			goto badname;
705 	}
706 	if (isdigit(*--p)) {
707  badname:
708 		cfgerror("bad device attachment name `%s'", name);
709 		return (&errdeva);
710 	}
711 	deva = ht_lookup(devatab, name);
712 	if (deva == NULL) {
713 		deva = ecalloc(1, sizeof *deva);
714 		deva->d_name = name;
715 		deva->d_bsame = NULL;
716 		deva->d_isdef = 0;
717 		deva->d_devbase = NULL;
718 		deva->d_atlist = NULL;
719 		deva->d_attrs = NULL;
720 		deva->d_ihead = NULL;
721 		deva->d_ipp = &deva->d_ihead;
722 		TAILQ_INSERT_TAIL(&alldevas, deva, d_next);
723 		if (ht_insert(devatab, name, deva))
724 			panic("getdeva(%s)", name);
725 	}
726 	return (deva);
727 }
728 
729 /*
730  * Look up an attribute.
731  */
732 struct attr *
getattr(const char * name)733 getattr(const char *name)
734 {
735 	struct attr *a;
736 
737 	if ((a = ht_lookup(attrtab, name)) == NULL) {
738 		cfgerror("undefined attribute `%s'", name);
739 		a = &errattr;
740 	}
741 	return (a);
742 }
743 
744 /*
745  * Implicit attribute definition.
746  */
747 struct attr *
refattr(const char * name)748 refattr(const char *name)
749 {
750 	struct attr *a;
751 
752 	if ((a = ht_lookup(attrtab, name)) == NULL)
753 		a = mkattr(name);
754 	return a;
755 }
756 
757 int
getrefattr(const char * name,struct attr ** ra)758 getrefattr(const char *name, struct attr **ra)
759 {
760 	struct attr *a;
761 
762 	a = ht_lookup(attrtab, name);
763 	if (a == NULL) {
764 		*ra = NULL;
765 		return (0);
766 	}
767 	/*
768 	 * Check if the existing attr is only referenced, not really defined.
769 	 */
770 	if (a->a_deps == NULL &&
771 	    a->a_iattr == 0 &&
772 	    a->a_devclass == 0) {
773 		*ra = a;
774 		return (0);
775 	}
776 	return (1);
777 }
778 
779 /*
780  * Recursively expand an attribute and its dependencies, checking for
781  * cycles, and invoking a callback for each attribute found.
782  */
783 void
expandattr(struct attr * a,void (* callback)(struct attr *))784 expandattr(struct attr *a, void (*callback)(struct attr *))
785 {
786 	struct attrlist *al;
787 	struct attr *dep;
788 
789 	if (a->a_expanding) {
790 		cfgerror("circular dependency on attribute `%s'", a->a_name);
791 		return;
792 	}
793 
794 	a->a_expanding = 1;
795 
796 	/* First expand all of this attribute's dependencies. */
797 	for (al = a->a_deps; al != NULL; al = al->al_next) {
798 		dep = al->al_this;
799 		expandattr(dep, callback);
800 	}
801 
802 	/* ...and now invoke the callback for ourself. */
803 	if (callback != NULL)
804 		(*callback)(a);
805 
806 	a->a_expanding = 0;
807 }
808 
809 /*
810  * Set the major device number for a device, so that it can be used
811  * as a root/dumps "on" device in a configuration.
812  */
813 void
setmajor(struct devbase * d,devmajor_t n)814 setmajor(struct devbase *d, devmajor_t n)
815 {
816 
817 	if (d != &errdev && d->d_major != NODEVMAJOR)
818 		cfgerror("device `%s' is already major %d",
819 		    d->d_name, d->d_major);
820 	else
821 		d->d_major = n;
822 }
823 
824 const char *
major2name(devmajor_t maj)825 major2name(devmajor_t maj)
826 {
827 	struct devbase *dev;
828 	struct devm *dm;
829 
830 	if (!do_devsw) {
831 		TAILQ_FOREACH(dev, &allbases, d_next) {
832 			if (dev->d_major == maj)
833 				return (dev->d_name);
834 		}
835 	} else {
836 		TAILQ_FOREACH(dm, &alldevms, dm_next) {
837 			if (dm->dm_bmajor == maj)
838 				return (dm->dm_name);
839 		}
840 	}
841 	return (NULL);
842 }
843 
844 devmajor_t
dev2major(struct devbase * dev)845 dev2major(struct devbase *dev)
846 {
847 	struct devm *dm;
848 
849 	if (!do_devsw)
850 		return (dev->d_major);
851 
852 	TAILQ_FOREACH(dm, &alldevms, dm_next) {
853 		if (strcmp(dm->dm_name, dev->d_name) == 0)
854 			return (dm->dm_bmajor);
855 	}
856 	return (NODEVMAJOR);
857 }
858 
859 /*
860  * Make a string description of the device at maj/min.
861  */
862 static const char *
makedevstr(devmajor_t maj,devminor_t min)863 makedevstr(devmajor_t maj, devminor_t min)
864 {
865 	const char *devicename;
866 	char buf[32];
867 
868 	devicename = major2name(maj);
869 	if (devicename == NULL)
870 		(void)snprintf(buf, sizeof(buf), "<%d/%d>", maj, min);
871 	else
872 		(void)snprintf(buf, sizeof(buf), "%s%d%c", devicename,
873 		    min / maxpartitions, (min % maxpartitions) + 'a');
874 
875 	return (intern(buf));
876 }
877 
878 /*
879  * Map things like "ra0b" => makedev(major("ra"), 0*maxpartitions + 'b'-'a').
880  * Handle the case where the device number is given but there is no
881  * corresponding name, and map NULL to the default.
882  */
883 static int
resolve(struct nvlist ** nvp,const char * name,const char * what,struct nvlist * dflt,int part)884 resolve(struct nvlist **nvp, const char *name, const char *what,
885 	struct nvlist *dflt, int part)
886 {
887 	struct nvlist *nv;
888 	struct devbase *dev;
889 	const char *cp;
890 	devmajor_t maj;
891 	devminor_t min;
892 	size_t i, l;
893 	int unit;
894 	char buf[NAMESIZE];
895 
896 	if ((part -= 'a') >= maxpartitions || part < 0)
897 		panic("resolve");
898 	if ((nv = *nvp) == NULL) {
899 		dev_t	d = NODEV;
900 		/*
901 		 * Apply default.  Easiest to do this by number.
902 		 * Make sure to retain NODEVness, if this is dflt's disposition.
903 		 */
904 		if ((dev_t)dflt->nv_num != NODEV) {
905 			maj = major(dflt->nv_num);
906 			min = ((minor(dflt->nv_num) / maxpartitions) *
907 			    maxpartitions) + part;
908 			d = makedev(maj, min);
909 			cp = makedevstr(maj, min);
910 		} else
911 			cp = NULL;
912 		*nvp = nv = newnv(NULL, cp, NULL, (long long)d, NULL);
913 	}
914 	if ((dev_t)nv->nv_num != NODEV) {
915 		/*
916 		 * By the numbers.  Find the appropriate major number
917 		 * to make a name.
918 		 */
919 		maj = major(nv->nv_num);
920 		min = minor(nv->nv_num);
921 		nv->nv_str = makedevstr(maj, min);
922 		return (0);
923 	}
924 
925 	if (nv->nv_str == NULL || nv->nv_str == s_qmark)
926 		/*
927 		 * Wildcarded or unspecified; leave it as NODEV.
928 		 */
929 		return (0);
930 
931 	if (nv->nv_ptr != NULL && strcmp(nv->nv_ptr, "spec") == 0)
932 		/*
933 		 * spec string, interpreted by kernel
934 		 */
935 		return (0);
936 
937 	/*
938 	 * The normal case: things like "ra2b".  Check for partition
939 	 * suffix, remove it if there, and split into name ("ra") and
940 	 * unit (2).
941 	 */
942 	l = i = strlen(nv->nv_str);
943 	cp = &nv->nv_str[l];
944 	if (l > 1 && *--cp >= 'a' && *cp < 'a' + maxpartitions &&
945 	    isdigit((unsigned char)cp[-1])) {
946 		l--;
947 		part = *cp - 'a';
948 	}
949 	cp = nv->nv_str;
950 	if (split(cp, l, buf, sizeof buf, &unit)) {
951 		cfgerror("%s: invalid %s device name `%s'", name, what, cp);
952 		return (1);
953 	}
954 	dev = ht_lookup(devbasetab, intern(buf));
955 	if (dev == NULL) {
956 		cfgerror("%s: device `%s' does not exist", name, buf);
957 		return (1);
958 	}
959 
960 	/*
961 	 * Check for the magic network interface attribute, and
962 	 * don't bother making a device number.
963 	 */
964 	if (has_attr(dev->d_attrs, s_ifnet)) {
965 		nv->nv_num = (long long)NODEV;
966 		nv->nv_ifunit = unit;	/* XXX XXX XXX */
967 	} else {
968 		maj = dev2major(dev);
969 		if (maj == NODEVMAJOR) {
970 			cfgerror("%s: can't make %s device from `%s'",
971 			    name, what, nv->nv_str);
972 			return (1);
973 		}
974 		nv->nv_num = (long long)makedev(maj, unit * maxpartitions + part);
975 	}
976 
977 	nv->nv_name = dev->d_name;
978 	return (0);
979 }
980 
981 /*
982  * Add a completed configuration to the list.
983  */
984 void
addconf(struct config * cf0)985 addconf(struct config *cf0)
986 {
987 	struct config *cf;
988 	const char *name;
989 
990 	name = cf0->cf_name;
991 	cf = ecalloc(1, sizeof *cf);
992 	if (ht_insert(cfhashtab, name, cf)) {
993 		cfgerror("configuration `%s' already defined", name);
994 		free(cf);
995 		goto bad;
996 	}
997 	*cf = *cf0;
998 
999 	/*
1000 	 * Resolve the root device.
1001 	 */
1002 	if (cf->cf_root == NULL) {
1003 		cfgerror("%s: no root device specified", name);
1004 		goto bad;
1005 	}
1006 	if (cf->cf_root && cf->cf_root->nv_str != s_qmark) {
1007 		struct nvlist *nv;
1008 		nv = cf->cf_root;
1009 		if (resolve(&cf->cf_root, name, "root", nv, 'a'))
1010 			goto bad;
1011 	}
1012 
1013 	/*
1014 	 * Resolve the dump device.
1015 	 */
1016 	if (cf->cf_dump == NULL || cf->cf_dump->nv_str == s_qmark) {
1017 		/*
1018 		 * Wildcarded dump device is equivalent to unspecified.
1019 		 */
1020 		cf->cf_dump = NULL;
1021 	} else if (cf->cf_dump->nv_str == s_none) {
1022 		/*
1023 		 * Operator has requested that no dump device should be
1024 		 * configured; do nothing.
1025 		 */
1026 	} else {
1027 		if (resolve(&cf->cf_dump, name, "dumps", cf->cf_dump, 'b'))
1028 			goto bad;
1029 	}
1030 
1031 	/* Wildcarded fstype is `unspecified'. */
1032 	if (cf->cf_fstype == s_qmark)
1033 		cf->cf_fstype = NULL;
1034 
1035 	TAILQ_INSERT_TAIL(&allcf, cf, cf_next);
1036 	return;
1037  bad:
1038 	nvfreel(cf0->cf_root);
1039 	nvfreel(cf0->cf_dump);
1040 }
1041 
1042 void
setconf(struct nvlist ** npp,const char * what,struct nvlist * v)1043 setconf(struct nvlist **npp, const char *what, struct nvlist *v)
1044 {
1045 
1046 	if (*npp != NULL) {
1047 		cfgerror("duplicate %s specification", what);
1048 		nvfreel(v);
1049 	} else
1050 		*npp = v;
1051 }
1052 
1053 void
delconf(const char * name)1054 delconf(const char *name)
1055 {
1056 	struct config *cf;
1057 
1058 	CFGDBG(5, "deselecting config `%s'", name);
1059 	if (ht_lookup(cfhashtab, name) == NULL) {
1060 		cfgerror("configuration `%s' undefined", name);
1061 		return;
1062 	}
1063 	(void)ht_remove(cfhashtab, name);
1064 
1065 	TAILQ_FOREACH(cf, &allcf, cf_next)
1066 		if (!strcmp(cf->cf_name, name))
1067 			break;
1068 	if (cf == NULL)
1069 		panic("lost configuration `%s'", name);
1070 
1071 	TAILQ_REMOVE(&allcf, cf, cf_next);
1072 }
1073 
1074 void
setfstype(const char ** fstp,const char * v)1075 setfstype(const char **fstp, const char *v)
1076 {
1077 
1078 	if (*fstp != NULL) {
1079 		cfgerror("multiple fstype specifications");
1080 		return;
1081 	}
1082 
1083 	if (v != s_qmark && OPT_FSOPT(v)) {
1084 		cfgerror("\"%s\" is not a configured file system", v);
1085 		return;
1086 	}
1087 
1088 	*fstp = v;
1089 }
1090 
1091 static struct devi *
newdevi(const char * name,int unit,struct devbase * d)1092 newdevi(const char *name, int unit, struct devbase *d)
1093 {
1094 	struct devi *i;
1095 
1096 	i = ecalloc(1, sizeof *i);
1097 	i->i_name = name;
1098 	i->i_unit = unit;
1099 	i->i_base = d;
1100 	i->i_bsame = NULL;
1101 	i->i_asame = NULL;
1102 	i->i_alias = NULL;
1103 	i->i_at = NULL;
1104 	i->i_pspec = NULL;
1105 	i->i_atdeva = NULL;
1106 	i->i_locs = NULL;
1107 	i->i_cfflags = 0;
1108 	i->i_lineno = currentline();
1109 	i->i_srcfile = yyfile;
1110 	i->i_active = DEVI_ORPHAN; /* Proper analysis comes later */
1111 	i->i_level = devilevel;
1112 	i->i_pseudoroot = 0;
1113 	if (unit >= d->d_umax)
1114 		d->d_umax = unit + 1;
1115 	return (i);
1116 }
1117 
1118 /*
1119  * Add the named device as attaching to the named attribute (or perhaps
1120  * another device instead) plus unit number.
1121  */
1122 void
adddev(const char * name,const char * at,struct loclist * loclist,int flags)1123 adddev(const char *name, const char *at, struct loclist *loclist, int flags)
1124 {
1125 	struct devi *i;		/* the new instance */
1126 	struct pspec *p;	/* and its pspec */
1127 	struct attr *attr;	/* attribute that allows attach */
1128 	struct devbase *ib;	/* i->i_base */
1129 	struct devbase *ab;	/* not NULL => at another dev */
1130 	struct attrlist *al;
1131 	struct deva *iba;	/* devbase attachment used */
1132 	const char *cp;
1133 	int atunit;
1134 	char atbuf[NAMESIZE];
1135 	int hit;
1136 
1137 	ab = NULL;
1138 	iba = NULL;
1139 	if (at == NULL) {
1140 		/* "at root" */
1141 		p = NULL;
1142 		if ((i = getdevi(name)) == NULL)
1143 			goto bad;
1144 		/*
1145 		 * Must warn about i_unit > 0 later, after taking care of
1146 		 * the STAR cases (we could do non-star's here but why
1147 		 * bother?).  Make sure this device can be at root.
1148 		 */
1149 		ib = i->i_base;
1150 		hit = 0;
1151 		for (iba = ib->d_ahead; iba != NULL; iba = iba->d_bsame)
1152 			if (onlist(iba->d_atlist, NULL)) {
1153 				hit = 1;
1154 				break;
1155 			}
1156 		if (!hit) {
1157 			cfgerror("`%s' cannot attach to the root", ib->d_name);
1158 			i->i_active = DEVI_BROKEN;
1159 			goto bad;
1160 		}
1161 		attr = &errattr;	/* a convenient "empty" attr */
1162 	} else {
1163 		if (split(at, strlen(at), atbuf, sizeof atbuf, &atunit)) {
1164 			cfgerror("invalid attachment name `%s'", at);
1165 			/* (void)getdevi(name); -- ??? */
1166 			goto bad;
1167 		}
1168 		if ((i = getdevi(name)) == NULL)
1169 			goto bad;
1170 		ib = i->i_base;
1171 
1172 		/*
1173 		 * Devices can attach to two types of things: Attributes,
1174 		 * and other devices (which have the appropriate attributes
1175 		 * to allow attachment).
1176 		 *
1177 		 * (1) If we're attached to an attribute, then we don't need
1178 		 *     look at the parent base device to see what attributes
1179 		 *     it has, and make sure that we can attach to them.
1180 		 *
1181 		 * (2) If we're attached to a real device (i.e. named in
1182 		 *     the config file), we want to remember that so that
1183 		 *     at cross-check time, if the device we're attached to
1184 		 *     is missing but other devices which also provide the
1185 		 *     attribute are present, we don't get a false "OK."
1186 		 *
1187 		 * (3) If the thing we're attached to is an attribute
1188 		 *     but is actually named in the config file, we still
1189 		 *     have to remember its devbase.
1190 		 */
1191 		cp = intern(atbuf);
1192 
1193 		/* Figure out parent's devbase, to satisfy case (3). */
1194 		ab = ht_lookup(devbasetab, cp);
1195 
1196 		/* Find out if it's an attribute. */
1197 		attr = ht_lookup(attrtab, cp);
1198 
1199 		/* Make sure we're _really_ attached to the attr.  Case (1). */
1200 		if (attr != NULL && onlist(attr->a_devs, ib))
1201 			goto findattachment;
1202 
1203 		/*
1204 		 * Else a real device, and not just an attribute.  Case (2).
1205 		 *
1206 		 * Have to work a bit harder to see whether we have
1207 		 * something like "tg0 at esp0" (where esp is merely
1208 		 * not an attribute) or "tg0 at nonesuch0" (where
1209 		 * nonesuch is not even a device).
1210 		 */
1211 		if (ab == NULL) {
1212 			cfgerror("%s at %s: `%s' unknown",
1213 			    name, at, atbuf);
1214 			i->i_active = DEVI_BROKEN;
1215 			goto bad;
1216 		}
1217 
1218 		/*
1219 		 * See if the named parent carries an attribute
1220 		 * that allows it to supervise device ib.
1221 		 */
1222 		for (al = ab->d_attrs; al != NULL; al = al->al_next) {
1223 			attr = al->al_this;
1224 			if (onlist(attr->a_devs, ib))
1225 				goto findattachment;
1226 		}
1227 		cfgerror("`%s' cannot attach to `%s'", ib->d_name, atbuf);
1228 		i->i_active = DEVI_BROKEN;
1229 		goto bad;
1230 
1231  findattachment:
1232 		/*
1233 		 * Find the parent spec.  If a matching one has not yet been
1234 		 * created, create one.
1235 		 */
1236 		p = getpspec(attr, ab, atunit);
1237 		p->p_devs = newnv(NULL, NULL, i, 0, p->p_devs);
1238 
1239 		/* find out which attachment it uses */
1240 		hit = 0;
1241 		for (iba = ib->d_ahead; iba != NULL; iba = iba->d_bsame)
1242 			if (onlist(iba->d_atlist, attr)) {
1243 				hit = 1;
1244 				break;
1245 			}
1246 		if (!hit)
1247 			panic("adddev: can't figure out attachment");
1248 	}
1249 	if ((i->i_locs = fixloc(name, attr, loclist)) == NULL) {
1250 		i->i_active = DEVI_BROKEN;
1251 		goto bad;
1252 	}
1253 	i->i_at = at;
1254 	i->i_pspec = p;
1255 	i->i_atdeva = iba;
1256 	i->i_cfflags = flags;
1257 	CFGDBG(3, "devi `%s' added", i->i_name);
1258 
1259 	*iba->d_ipp = i;
1260 	iba->d_ipp = &i->i_asame;
1261 
1262 	/* all done, fall into ... */
1263  bad:
1264 	loclist_destroy(loclist);
1265 	return;
1266 }
1267 
1268 void
deldevi(const char * name,const char * at)1269 deldevi(const char *name, const char *at)
1270 {
1271 	struct devi *firsti, *i;
1272 	struct devbase *d;
1273 	int unit;
1274 	char base[NAMESIZE];
1275 
1276 	CFGDBG(5, "deselecting devi `%s'", name);
1277 	if (split(name, strlen(name), base, sizeof base, &unit)) {
1278 		cfgerror("invalid device name `%s'", name);
1279 		return;
1280 	}
1281 	d = ht_lookup(devbasetab, intern(base));
1282 	if (d == NULL) {
1283 		cfgerror("%s: unknown device `%s'", name, base);
1284 		return;
1285 	}
1286 	if (d->d_ispseudo) {
1287 		cfgerror("%s: %s is a pseudo-device", name, base);
1288 		return;
1289 	}
1290 	if ((firsti = ht_lookup(devitab, name)) == NULL) {
1291 		cfgerror("`%s' not defined", name);
1292 		return;
1293 	}
1294 	if (at == NULL && firsti->i_at == NULL) {
1295 		/* 'at root' */
1296 		remove_devi(firsti);
1297 		return;
1298 	} else if (at != NULL)
1299 		for (i = firsti; i != NULL; i = i->i_alias)
1300 			if (i->i_active != DEVI_BROKEN &&
1301 			    strcmp(at, i->i_at) == 0) {
1302 				remove_devi(i);
1303 				return;
1304 			}
1305 	cfgerror("`%s' at `%s' not found", name, at ? at : "root");
1306 }
1307 
1308 static void
remove_devi(struct devi * i)1309 remove_devi(struct devi *i)
1310 {
1311 	struct devbase *d = i->i_base;
1312 	struct devi *f, *j, **ppi;
1313 	struct deva *iba;
1314 
1315 	CFGDBG(5, "removing devi `%s'", i->i_name);
1316 	f = ht_lookup(devitab, i->i_name);
1317 	if (f == NULL)
1318 		panic("remove_devi(): instance %s disappeared from devitab",
1319 		    i->i_name);
1320 
1321 	if (i->i_active == DEVI_BROKEN) {
1322 		cfgerror("not removing broken instance `%s'", i->i_name);
1323 		return;
1324 	}
1325 
1326 	/*
1327 	 * We have the device instance, i.
1328 	 * We have to:
1329 	 *   - delete the alias
1330 	 *
1331 	 *      If the devi was an alias of an already listed devi, all is
1332 	 *      good we don't have to do more.
1333 	 *      If it was the first alias, we have to replace i's entry in
1334 	 *      d's list by its first alias.
1335 	 *      If it was the only entry, we must remove i's entry from d's
1336 	 *      list.
1337 	 */
1338 	if (i != f) {
1339 		for (j = f; j->i_alias != i; j = j->i_alias)
1340 			continue;
1341 		j->i_alias = i->i_alias;
1342 	} else {
1343 		if (i->i_alias == NULL) {
1344 			/* No alias, must unlink the entry from devitab */
1345 			ht_remove(devitab, i->i_name);
1346 			j = i->i_bsame;
1347 		} else {
1348 			/* Or have the first alias replace i in d's list */
1349 			i->i_alias->i_bsame = i->i_bsame;
1350 			j = i->i_alias;
1351 			if (i == f)
1352 				ht_replace(devitab, i->i_name, i->i_alias);
1353 		}
1354 
1355 		/*
1356 		 *   - remove/replace the instance from the devbase's list
1357 		 *
1358 		 * A double-linked list would make this much easier.  Oh, well,
1359 		 * what is done is done.
1360 		 */
1361 		for (ppi = &d->d_ihead;
1362 		    *ppi != NULL && *ppi != i && (*ppi)->i_bsame != i;
1363 		    ppi = &(*ppi)->i_bsame)
1364 			continue;
1365 		if (*ppi == NULL)
1366 			panic("deldev: dev (%s) doesn't list the devi"
1367 			    " (%s at %s)", d->d_name, i->i_name, i->i_at);
1368 		f = *ppi;
1369 		if (f == i)
1370 			/* That implies d->d_ihead == i */
1371 			*ppi = j;
1372 		else
1373 			(*ppi)->i_bsame = j;
1374 		if (d->d_ipp == &i->i_bsame) {
1375 			if (i->i_alias == NULL) {
1376 				if (f == i)
1377 					d->d_ipp = &d->d_ihead;
1378 				else
1379 					d->d_ipp = &f->i_bsame;
1380 			} else
1381 				d->d_ipp = &i->i_alias->i_bsame;
1382 		}
1383 	}
1384 	/*
1385 	 *   - delete the attachment instance
1386 	 */
1387 	iba = i->i_atdeva;
1388 	for (ppi = &iba->d_ihead;
1389 	    *ppi != NULL && *ppi != i && (*ppi)->i_asame != i;
1390 	    ppi = &(*ppi)->i_asame)
1391 		continue;
1392 	if (*ppi == NULL)
1393 		panic("deldev: deva (%s) doesn't list the devi (%s)",
1394 		    iba->d_name, i->i_name);
1395 	f = *ppi;
1396 	if (f == i)
1397 		/* That implies iba->d_ihead == i */
1398 		*ppi = i->i_asame;
1399 	else
1400 		(*ppi)->i_asame = i->i_asame;
1401 	if (iba->d_ipp == &i->i_asame) {
1402 		if (f == i)
1403 			iba->d_ipp = &iba->d_ihead;
1404 		else
1405 			iba->d_ipp = &f->i_asame;
1406 	}
1407 	/*
1408 	 *   - delete the pspec
1409 	 */
1410 	if (i->i_pspec) {
1411 		struct pspec *p = i->i_pspec;
1412 		struct nvlist *nv, *onv;
1413 
1414 		/* Double-linked nvlist anyone? */
1415 		for (nv = p->p_devs; nv->nv_next != NULL; nv = nv->nv_next) {
1416 			if (nv->nv_next && nv->nv_next->nv_ptr == i) {
1417 				onv = nv->nv_next;
1418 				nv->nv_next = onv->nv_next;
1419 				nvfree(onv);
1420 				break;
1421 			}
1422 			if (nv->nv_ptr == i) {
1423 				/* nv is p->p_devs in that case */
1424 				p->p_devs = nv->nv_next;
1425 				nvfree(nv);
1426 				break;
1427 			}
1428 		}
1429 		if (p->p_devs == NULL)
1430 			TAILQ_REMOVE(&allpspecs, p, p_list);
1431 	}
1432 	/*
1433 	 *   - delete the alldevi entry
1434 	 */
1435 	TAILQ_REMOVE(&alldevi, i, i_next);
1436 	ndevi--;
1437 	/*
1438 	 * Put it in deaddevitab
1439 	 *
1440 	 * Each time a devi is removed, devilevel is increased so that later on
1441 	 * it is possible to tell if an instance was added before or after the
1442 	 * removal of its parent.
1443 	 *
1444 	 * For active instances, i_level contains the number of devi removed so
1445 	 * far, and for dead devis, it contains its index.
1446 	 */
1447 	i->i_level = devilevel++;
1448 	i->i_alias = NULL;
1449 	f = ht_lookup(deaddevitab, i->i_name);
1450 	if (f == NULL) {
1451 		if (ht_insert(deaddevitab, i->i_name, i))
1452 			panic("remove_devi(%s) - can't add to deaddevitab",
1453 			    i->i_name);
1454 	} else {
1455 		for (j = f; j->i_alias != NULL; j = j->i_alias)
1456 			continue;
1457 		j->i_alias = i;
1458 	}
1459 	/*
1460 	 *   - reconstruct d->d_umax
1461 	 */
1462 	d->d_umax = 0;
1463 	for (i = d->d_ihead; i != NULL; i = i->i_bsame)
1464 		if (i->i_unit >= d->d_umax)
1465 			d->d_umax = i->i_unit + 1;
1466 }
1467 
1468 void
deldeva(const char * at)1469 deldeva(const char *at)
1470 {
1471 	int unit;
1472 	const char *cp;
1473 	struct devbase *d, *ad;
1474 	struct devi *i, *j;
1475 	struct attr *a;
1476 	struct pspec *p;
1477 	struct nvlist *nv, *stack = NULL;
1478 
1479 	if (at == NULL) {
1480 		TAILQ_FOREACH(i, &alldevi, i_next)
1481 			if (i->i_at == NULL)
1482 				stack = newnv(NULL, NULL, i, 0, stack);
1483 	} else {
1484 		size_t l;
1485 
1486 		CFGDBG(5, "deselecting deva `%s'", at);
1487 		if (at[0] == '\0')
1488 			goto out;
1489 
1490 		l = strlen(at) - 1;
1491 		if (at[l] == '?' || isdigit((unsigned char)at[l])) {
1492 			char base[NAMESIZE];
1493 
1494 			if (split(at, l+1, base, sizeof base, &unit)) {
1495 out:
1496 				cfgerror("invalid attachment name `%s'", at);
1497 				return;
1498 			}
1499 			cp = intern(base);
1500 		} else {
1501 			cp = intern(at);
1502 			unit = STAR;
1503 		}
1504 
1505 		ad = ht_lookup(devbasetab, cp);
1506 		a = ht_lookup(attrtab, cp);
1507 		if (a == NULL) {
1508 			cfgerror("unknown attachment attribute or device `%s'",
1509 			    cp);
1510 			return;
1511 		}
1512 		if (!a->a_iattr) {
1513 			cfgerror("plain attribute `%s' cannot have children",
1514 			    a->a_name);
1515 			return;
1516 		}
1517 
1518 		/*
1519 		 * remove_devi() makes changes to the devbase's list and the
1520 		 * alias list, * so the actual deletion of the instances must
1521 		 * be delayed.
1522 		 */
1523 		for (nv = a->a_devs; nv != NULL; nv = nv->nv_next) {
1524 			d = nv->nv_ptr;
1525 			for (i = d->d_ihead; i != NULL; i = i->i_bsame)
1526 				for (j = i; j != NULL; j = j->i_alias) {
1527 					/* Ignore devices at root */
1528 					if (j->i_at == NULL)
1529 						continue;
1530 					p = j->i_pspec;
1531 					/*
1532 					 * There are three cases:
1533 					 *
1534 					 * 1.  unit is not STAR.  Consider 'at'
1535 					 *     to be explicit, even if it
1536 					 *     references an interface
1537 					 *     attribute.
1538 					 *
1539 					 * 2.  unit is STAR and 'at' references
1540 					 *     a real device.  Look for pspec
1541 					 *     that have a matching p_atdev
1542 					 *     field.
1543 					 *
1544 					 * 3.  unit is STAR and 'at' references
1545 					 *     an interface attribute.  Look
1546 					 *     for pspec that have a matching
1547 					 *     p_iattr field.
1548 					 */
1549 					if ((unit != STAR &&        /* Case */
1550 					     !strcmp(j->i_at, at)) ||  /* 1 */
1551 					    (unit == STAR &&
1552 					     ((ad != NULL &&        /* Case */
1553 					       p->p_atdev == ad) ||    /* 2 */
1554 					      (ad == NULL &&        /* Case */
1555 					       p->p_iattr == a))))     /* 3 */
1556 						stack = newnv(NULL, NULL, j, 0,
1557 						    stack);
1558 				}
1559 		}
1560 	}
1561 
1562 	for (nv = stack; nv != NULL; nv = nv->nv_next)
1563 		remove_devi(nv->nv_ptr);
1564 	nvfreel(stack);
1565 }
1566 
1567 void
deldev(const char * name)1568 deldev(const char *name)
1569 {
1570 	size_t l;
1571 	struct devi *firsti, *i;
1572 	struct nvlist *nv, *stack = NULL;
1573 
1574 	CFGDBG(5, "deselecting dev `%s'", name);
1575 	if (name[0] == '\0')
1576 		goto out;
1577 
1578 	l = strlen(name) - 1;
1579 	if (name[l] == '*' || isdigit((unsigned char)name[l])) {
1580 		/* `no mydev0' or `no mydev*' */
1581 		firsti = ht_lookup(devitab, name);
1582 		if (firsti == NULL) {
1583 out:
1584 			cfgerror("unknown instance %s", name);
1585 			return;
1586 		}
1587 		for (i = firsti; i != NULL; i = i->i_alias)
1588 			stack = newnv(NULL, NULL, i, 0, stack);
1589 	} else {
1590 		struct devbase *d = ht_lookup(devbasetab, name);
1591 
1592 		if (d == NULL) {
1593 			cfgerror("unknown device %s", name);
1594 			return;
1595 		}
1596 		if (d->d_ispseudo) {
1597 			cfgerror("%s is a pseudo-device; "
1598 			    "use \"no pseudo-device %s\" instead", name,
1599 			    name);
1600 			return;
1601 		}
1602 
1603 		for (firsti = d->d_ihead; firsti != NULL;
1604 		    firsti = firsti->i_bsame)
1605 			for (i = firsti; i != NULL; i = i->i_alias)
1606 				stack = newnv(NULL, NULL, i, 0, stack);
1607 	}
1608 
1609 	for (nv = stack; nv != NULL; nv = nv->nv_next)
1610 		remove_devi(nv->nv_ptr);
1611 	nvfreel(stack);
1612 }
1613 
1614 /*
1615  * Insert given device "name" into devroottab.  In case "name"
1616  * designates a pure interface attribute, create a fake device
1617  * instance for the attribute and insert that into the roottab
1618  * (this scheme avoids mucking around with the orphanage analysis).
1619  */
1620 void
addpseudoroot(const char * name)1621 addpseudoroot(const char *name)
1622 {
1623 	char buf[NAMESIZE];
1624 	int unit;
1625 	struct attr *attr;
1626 	struct devi *i;
1627 	struct deva *iba;
1628 	struct devbase *ib;
1629 
1630 	if (split(name, strlen(name), buf, sizeof(buf), &unit)) {
1631 		cfgerror("invalid pseudo-root name `%s'", name);
1632 		return;
1633 	}
1634 
1635 	/*
1636 	 * Prefer device because devices with locators define an
1637 	 * implicit interface attribute.  However, if a device is
1638 	 * not available, try to attach to the interface attribute.
1639 	 * This makes sure adddev() doesn't get confused when we
1640 	 * are really attaching to a device (alternatively we maybe
1641 	 * could specify a non-NULL atlist to defdevattach() below).
1642 	 */
1643 	ib = ht_lookup(devbasetab, intern(buf));
1644 	if (ib == NULL) {
1645 		struct devbase *fakedev;
1646 		char fakename[NAMESIZE];
1647 
1648 		attr = ht_lookup(attrtab, intern(buf));
1649 		if (!(attr && attr->a_iattr)) {
1650 			cfgerror("pseudo-root `%s' not available", name);
1651 			return;
1652 		}
1653 
1654 		/*
1655 		 * here we cheat a bit: create a fake devbase with the
1656 		 * interface attribute and instantiate it.  quick, cheap,
1657 		 * dirty & bad for you, much like the stuff in the fridge.
1658 		 * and, it works, since the pseudoroot device is not included
1659 		 * in ioconf, just used by config to make sure we start from
1660 		 * the right place.
1661 		 */
1662 		snprintf(fakename, sizeof(fakename), "%s_devattrs", buf);
1663 		fakedev = getdevbase(intern(fakename));
1664 		fakedev->d_isdef = 1;
1665 		fakedev->d_ispseudo = 0;
1666 		fakedev->d_attrs = attrlist_cons(NULL, attr);
1667 		defdevattach(NULL, fakedev, NULL, NULL);
1668 
1669 		if (unit == STAR)
1670 			snprintf(buf, sizeof(buf), "%s*", fakename);
1671 		else
1672 			snprintf(buf, sizeof(buf), "%s%d", fakename, unit);
1673 		name = buf;
1674 	}
1675 
1676 	/* ok, everything should be set up, so instantiate a fake device */
1677 	i = getdevi(name);
1678 	if (i == NULL)
1679 		panic("device `%s' expected to be present", name);
1680 	ib = i->i_base;
1681 	iba = ib->d_ahead;
1682 
1683 	i->i_atdeva = iba;
1684 	i->i_cfflags = 0;
1685 	i->i_locs = fixloc(name, &errattr, NULL);
1686 	i->i_pseudoroot = 1;
1687 	i->i_active = DEVI_ORPHAN; /* set active by kill_orphans() */
1688 
1689 	*iba->d_ipp = i;
1690 	iba->d_ipp = &i->i_asame;
1691 
1692 	ht_insert(devroottab, ib->d_name, ib);
1693 }
1694 
1695 void
addpseudo(const char * name,int number)1696 addpseudo(const char *name, int number)
1697 {
1698 	struct devbase *d;
1699 	struct devi *i;
1700 
1701 	d = ht_lookup(devbasetab, name);
1702 	if (d == NULL) {
1703 		cfgerror("undefined pseudo-device %s", name);
1704 		return;
1705 	}
1706 	if (!d->d_ispseudo) {
1707 		cfgerror("%s is a real device, not a pseudo-device", name);
1708 		return;
1709 	}
1710 	if (ht_lookup(devitab, name) != NULL) {
1711 		cfgerror("`%s' already defined", name);
1712 		return;
1713 	}
1714 	i = newdevi(name, number - 1, d);	/* foo 16 => "foo0..foo15" */
1715 	if (ht_insert(devitab, name, i))
1716 		panic("addpseudo(%s)", name);
1717 	/* Useful to retrieve the instance from the devbase */
1718 	d->d_ihead = i;
1719 	i->i_active = DEVI_ACTIVE;
1720 	TAILQ_INSERT_TAIL(&allpseudo, i, i_next);
1721 }
1722 
1723 void
delpseudo(const char * name)1724 delpseudo(const char *name)
1725 {
1726 	struct devbase *d;
1727 	struct devi *i;
1728 
1729 	CFGDBG(5, "deselecting pseudo `%s'", name);
1730 	d = ht_lookup(devbasetab, name);
1731 	if (d == NULL) {
1732 		cfgerror("undefined pseudo-device %s", name);
1733 		return;
1734 	}
1735 	if (!d->d_ispseudo) {
1736 		cfgerror("%s is a real device, not a pseudo-device", name);
1737 		return;
1738 	}
1739 	if ((i = ht_lookup(devitab, name)) == NULL) {
1740 		cfgerror("`%s' not defined", name);
1741 		return;
1742 	}
1743 	d->d_umax = 0;		/* clear neads-count entries */
1744 	d->d_ihead = NULL;	/* make sure it won't be considered active */
1745 	TAILQ_REMOVE(&allpseudo, i, i_next);
1746 	if (ht_remove(devitab, name))
1747 		panic("delpseudo(%s) - can't remove from devitab", name);
1748 	if (ht_insert(deaddevitab, name, i))
1749 		panic("delpseudo(%s) - can't add to deaddevitab", name);
1750 }
1751 
1752 void
adddevm(const char * name,devmajor_t cmajor,devmajor_t bmajor,struct condexpr * cond,struct nvlist * nv_nodes)1753 adddevm(const char *name, devmajor_t cmajor, devmajor_t bmajor,
1754 	struct condexpr *cond, struct nvlist *nv_nodes)
1755 {
1756 	struct devm *dm;
1757 
1758 	if (cmajor != NODEVMAJOR && (cmajor < 0 || cmajor >= 4096)) {
1759 		cfgerror("character major %d is invalid", cmajor);
1760 		condexpr_destroy(cond);
1761 		nvfreel(nv_nodes);
1762 		return;
1763 	}
1764 
1765 	if (bmajor != NODEVMAJOR && (bmajor < 0 || bmajor >= 4096)) {
1766 		cfgerror("block major %d is invalid", bmajor);
1767 		condexpr_destroy(cond);
1768 		nvfreel(nv_nodes);
1769 		return;
1770 	}
1771 	if (cmajor == NODEVMAJOR && bmajor == NODEVMAJOR) {
1772 		cfgerror("both character/block majors are not specified");
1773 		condexpr_destroy(cond);
1774 		nvfreel(nv_nodes);
1775 		return;
1776 	}
1777 
1778 	dm = ecalloc(1, sizeof(*dm));
1779 	dm->dm_srcfile = yyfile;
1780 	dm->dm_srcline = currentline();
1781 	dm->dm_name = name;
1782 	dm->dm_cmajor = cmajor;
1783 	dm->dm_bmajor = bmajor;
1784 	dm->dm_opts = cond;
1785 	dm->dm_devnodes = nv_nodes;
1786 
1787 	TAILQ_INSERT_TAIL(&alldevms, dm, dm_next);
1788 
1789 	maxcdevm = MAX(maxcdevm, dm->dm_cmajor);
1790 	maxbdevm = MAX(maxbdevm, dm->dm_bmajor);
1791 }
1792 
1793 int
fixdevis(void)1794 fixdevis(void)
1795 {
1796 	struct devi *i;
1797 	int error = 0;
1798 
1799 	TAILQ_FOREACH(i, &alldevi, i_next) {
1800 		CFGDBG(3, "fixing devis `%s'", i->i_name);
1801 		if (i->i_active == DEVI_ACTIVE)
1802 			selectbase(i->i_base, i->i_atdeva);
1803 		else if (i->i_active == DEVI_ORPHAN) {
1804 			/*
1805 			 * At this point, we can't have instances for which
1806 			 * i_at or i_pspec are NULL.
1807 			 */
1808 			++error;
1809 			cfgxerror(i->i_srcfile, i->i_lineno,
1810 			    "`%s at %s' is orphaned (%s `%s' found)",
1811 			    i->i_name, i->i_at, i->i_pspec->p_atunit == WILD ?
1812 			    "nothing matching" : "no", i->i_at);
1813 		} else if (vflag && i->i_active == DEVI_IGNORED)
1814 			cfgxwarn(i->i_srcfile, i->i_lineno, "ignoring "
1815 			    "explicitly orphaned instance `%s at %s'",
1816 			    i->i_name, i->i_at);
1817 	}
1818 
1819 	if (error)
1820 		return error;
1821 
1822 	TAILQ_FOREACH(i, &allpseudo, i_next)
1823 		if (i->i_active == DEVI_ACTIVE)
1824 			selectbase(i->i_base, NULL);
1825 	return 0;
1826 }
1827 
1828 /*
1829  * Look up a parent spec, creating a new one if it does not exist.
1830  */
1831 static struct pspec *
getpspec(struct attr * attr,struct devbase * ab,int atunit)1832 getpspec(struct attr *attr, struct devbase *ab, int atunit)
1833 {
1834 	struct pspec *p;
1835 
1836 	TAILQ_FOREACH(p, &allpspecs, p_list) {
1837 		if (p->p_iattr == attr &&
1838 		    p->p_atdev == ab &&
1839 		    p->p_atunit == atunit)
1840 			return (p);
1841 	}
1842 
1843 	p = ecalloc(1, sizeof(*p));
1844 
1845 	p->p_iattr = attr;
1846 	p->p_atdev = ab;
1847 	p->p_atunit = atunit;
1848 	p->p_inst = npspecs++;
1849 	p->p_active = 0;
1850 
1851 	TAILQ_INSERT_TAIL(&allpspecs, p, p_list);
1852 
1853 	return (p);
1854 }
1855 
1856 /*
1857  * Define a new instance of a specific device.
1858  */
1859 static struct devi *
getdevi(const char * name)1860 getdevi(const char *name)
1861 {
1862 	struct devi *i, *firsti;
1863 	struct devbase *d;
1864 	int unit;
1865 	char base[NAMESIZE];
1866 
1867 	if (split(name, strlen(name), base, sizeof base, &unit)) {
1868 		cfgerror("invalid device name `%s'", name);
1869 		return (NULL);
1870 	}
1871 	d = ht_lookup(devbasetab, intern(base));
1872 	if (d == NULL) {
1873 		cfgerror("%s: unknown device `%s'", name, base);
1874 		return (NULL);
1875 	}
1876 	if (d->d_ispseudo) {
1877 		cfgerror("%s: %s is a pseudo-device", name, base);
1878 		return (NULL);
1879 	}
1880 	firsti = ht_lookup(devitab, name);
1881 	i = newdevi(name, unit, d);
1882 	if (firsti == NULL) {
1883 		if (ht_insert(devitab, name, i))
1884 			panic("getdevi(%s)", name);
1885 		*d->d_ipp = i;
1886 		d->d_ipp = &i->i_bsame;
1887 	} else {
1888 		while (firsti->i_alias)
1889 			firsti = firsti->i_alias;
1890 		firsti->i_alias = i;
1891 	}
1892 	TAILQ_INSERT_TAIL(&alldevi, i, i_next);
1893 	ndevi++;
1894 	return (i);
1895 }
1896 
1897 static const char *
concat(const char * name,int c)1898 concat(const char *name, int c)
1899 {
1900 	size_t len;
1901 	char buf[NAMESIZE];
1902 
1903 	len = strlen(name);
1904 	if (len + 2 > sizeof(buf)) {
1905 		cfgerror("device name `%s%c' too long", name, c);
1906 		len = sizeof(buf) - 2;
1907 	}
1908 	memmove(buf, name, len);
1909 	buf[len] = (char)c;
1910 	buf[len + 1] = '\0';
1911 	return (intern(buf));
1912 }
1913 
1914 const char *
starref(const char * name)1915 starref(const char *name)
1916 {
1917 
1918 	return (concat(name, '*'));
1919 }
1920 
1921 const char *
wildref(const char * name)1922 wildref(const char *name)
1923 {
1924 
1925 	return (concat(name, '?'));
1926 }
1927 
1928 /*
1929  * Split a name like "foo0" into base name (foo) and unit number (0).
1930  * Return 0 on success.  To make this useful for names like "foo0a",
1931  * the length of the "foo0" part is one of the arguments.
1932  */
1933 static int
split(const char * name,size_t nlen,char * base,size_t bsize,int * aunit)1934 split(const char *name, size_t nlen, char *base, size_t bsize, int *aunit)
1935 {
1936 	const char *cp;
1937 	int c;
1938 	size_t l;
1939 
1940 	l = nlen;
1941 	if (l < 2 || l >= bsize || isdigit((unsigned char)*name))
1942 		return (1);
1943 	c = (u_char)name[--l];
1944 	if (!isdigit(c)) {
1945 		if (c == '*')
1946 			*aunit = STAR;
1947 		else if (c == '?')
1948 			*aunit = WILD;
1949 		else
1950 			return (1);
1951 	} else {
1952 		cp = &name[l];
1953 		while (isdigit((unsigned char)cp[-1]))
1954 			l--, cp--;
1955 		*aunit = atoi(cp);
1956 	}
1957 	memmove(base, name, l);
1958 	base[l] = 0;
1959 	return (0);
1960 }
1961 
1962 void
addattr(const char * name)1963 addattr(const char *name)
1964 {
1965 	struct attr *a;
1966 
1967 	a = refattr(name);
1968 	selectattr(a);
1969 }
1970 
1971 void
delattr(const char * name)1972 delattr(const char *name)
1973 {
1974 	struct attr *a;
1975 
1976 	a = refattr(name);
1977 	deselectattr(a);
1978 }
1979 
1980 void
selectattr(struct attr * a)1981 selectattr(struct attr *a)
1982 {
1983 	struct attrlist *al;
1984 	struct attr *dep;
1985 
1986 	CFGDBG(5, "selecting attr `%s'", a->a_name);
1987 	for (al = a->a_deps; al != NULL; al = al->al_next) {
1988 		dep = al->al_this;
1989 		selectattr(dep);
1990 	}
1991 	if (ht_insert(selecttab, a->a_name, __UNCONST(a->a_name)) == 0)
1992 		nattrs++;
1993 	CFGDBG(3, "attr selected `%s'", a->a_name);
1994 }
1995 
1996 static int
deselectattrcb2(const char * name1,const char * name2,void * v,void * arg)1997 deselectattrcb2(const char *name1, const char *name2, void *v, void *arg)
1998 {
1999 	const char *name = arg;
2000 
2001 	if (strcmp(name, name2) == 0)
2002 		delattr(name1);
2003 	return 0;
2004 }
2005 
2006 void
deselectattr(struct attr * a)2007 deselectattr(struct attr *a)
2008 {
2009 
2010 	CFGDBG(5, "deselecting attr `%s'", a->a_name);
2011 	ht_enumerate2(attrdeptab, deselectattrcb2, __UNCONST(a->a_name));
2012 	if (ht_remove(selecttab, a->a_name) == 0)
2013 		nattrs--;
2014 	CFGDBG(3, "attr deselected `%s'", a->a_name);
2015 }
2016 
2017 static int
dumpattrdepcb2(const char * name1,const char * name2,void * v,void * arg)2018 dumpattrdepcb2(const char *name1, const char *name2, void *v, void *arg)
2019 {
2020 
2021 	CFGDBG(3, "attr `%s' depends on attr `%s'", name1, name2);
2022 	return 0;
2023 }
2024 
2025 void
dependattrs(void)2026 dependattrs(void)
2027 {
2028 
2029 	ht_enumerate2(attrdeptab, dumpattrdepcb2, NULL);
2030 }
2031 
2032 /*
2033  * We have an instance of the base foo, so select it and all its
2034  * attributes for "optional foo".
2035  */
2036 static void
selectbase(struct devbase * d,struct deva * da)2037 selectbase(struct devbase *d, struct deva *da)
2038 {
2039 	struct attr *a;
2040 	struct attrlist *al;
2041 
2042 	(void)ht_insert(selecttab, d->d_name, __UNCONST(d->d_name));
2043 	CFGDBG(3, "devbase selected `%s'", d->d_name);
2044 	CFGDBG(5, "selecting dependencies of devbase `%s'", d->d_name);
2045 	for (al = d->d_attrs; al != NULL; al = al->al_next) {
2046 		a = al->al_this;
2047 		expandattr(a, selectattr);
2048 	}
2049 
2050 	struct attr *devattr;
2051 	devattr = refattr(d->d_name);
2052 	expandattr(devattr, selectattr);
2053 
2054 	if (da != NULL) {
2055 		(void)ht_insert(selecttab, da->d_name, __UNCONST(da->d_name));
2056 		CFGDBG(3, "devattr selected `%s'", da->d_name);
2057 		for (al = da->d_attrs; al != NULL; al = al->al_next) {
2058 			a = al->al_this;
2059 			expandattr(a, selectattr);
2060 		}
2061 	}
2062 
2063 	fixdev(d);
2064 }
2065 
2066 /*
2067  * Is the given pointer on the given list of pointers?
2068  */
2069 int
onlist(struct nvlist * nv,void * ptr)2070 onlist(struct nvlist *nv, void *ptr)
2071 {
2072 	for (; nv != NULL; nv = nv->nv_next)
2073 		if (nv->nv_ptr == ptr)
2074 			return (1);
2075 	return (0);
2076 }
2077 
2078 static char *
extend(char * p,const char * name)2079 extend(char *p, const char *name)
2080 {
2081 	size_t l;
2082 
2083 	l = strlen(name);
2084 	memmove(p, name, l);
2085 	p += l;
2086 	*p++ = ',';
2087 	*p++ = ' ';
2088 	return (p);
2089 }
2090 
2091 /*
2092  * Check that we got all required locators, and default any that are
2093  * given as "?" and have defaults.  Return 0 on success.
2094  */
2095 static const char **
fixloc(const char * name,struct attr * attr,struct loclist * got)2096 fixloc(const char *name, struct attr *attr, struct loclist *got)
2097 {
2098 	struct loclist *m, *n;
2099 	int ord;
2100 	const char **lp;
2101 	int nmissing, nextra, nnodefault;
2102 	char *mp, *ep, *ndp;
2103 	char missing[1000], extra[1000], nodefault[1000];
2104 	static const char *nullvec[1];
2105 
2106 	/*
2107 	 * Look for all required locators, and number the given ones
2108 	 * according to the required order.  While we are numbering,
2109 	 * set default values for defaulted locators.
2110 	 */
2111 	if (attr->a_loclen == 0)	/* e.g., "at root" */
2112 		lp = nullvec;
2113 	else
2114 		lp = emalloc((size_t)(attr->a_loclen + 1) * sizeof(const char *));
2115 	for (n = got; n != NULL; n = n->ll_next)
2116 		n->ll_num = -1;
2117 	nmissing = 0;
2118 	mp = missing;
2119 	/* yes, this is O(mn), but m and n should be small */
2120 	for (ord = 0, m = attr->a_locs; m != NULL; m = m->ll_next, ord++) {
2121 		for (n = got; n != NULL; n = n->ll_next) {
2122 			if (n->ll_name == m->ll_name) {
2123 				n->ll_num = ord;
2124 				break;
2125 			}
2126 		}
2127 		if (n == NULL && m->ll_num == 0) {
2128 			nmissing++;
2129 			mp = extend(mp, m->ll_name);
2130 		}
2131 		lp[ord] = m->ll_string;
2132 	}
2133 	if (ord != attr->a_loclen)
2134 		panic("fixloc");
2135 	lp[ord] = NULL;
2136 	nextra = 0;
2137 	ep = extra;
2138 	nnodefault = 0;
2139 	ndp = nodefault;
2140 	for (n = got; n != NULL; n = n->ll_next) {
2141 		if (n->ll_num >= 0) {
2142 			if (n->ll_string != NULL)
2143 				lp[n->ll_num] = n->ll_string;
2144 			else if (lp[n->ll_num] == NULL) {
2145 				nnodefault++;
2146 				ndp = extend(ndp, n->ll_name);
2147 			}
2148 		} else {
2149 			nextra++;
2150 			ep = extend(ep, n->ll_name);
2151 		}
2152 	}
2153 	if (nextra) {
2154 		ep[-2] = 0;	/* kill ", " */
2155 		cfgerror("%s: extraneous locator%s: %s",
2156 		    name, nextra > 1 ? "s" : "", extra);
2157 	}
2158 	if (nmissing) {
2159 		mp[-2] = 0;
2160 		cfgerror("%s: must specify %s", name, missing);
2161 	}
2162 	if (nnodefault) {
2163 		ndp[-2] = 0;
2164 		cfgerror("%s: cannot wildcard %s", name, nodefault);
2165 	}
2166 	if (nmissing || nnodefault) {
2167 		free(lp);
2168 		lp = NULL;
2169 	}
2170 	return (lp);
2171 }
2172 
2173 void
setversion(int newver)2174 setversion(int newver)
2175 {
2176 	if (newver > CONFIG_VERSION)
2177 		cfgerror("your sources require a newer version of config(1) "
2178 		    "-- please rebuild it.");
2179 	else if (newver < CONFIG_MINVERSION)
2180 		cfgerror("your sources are out of date -- please update.");
2181 	else
2182 		version = newver;
2183 }
2184