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