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