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