📄 sem.c
字号:
/* * Copyright (c) 1992, 1993 * The Regents of the University of California. All rights reserved. * * This software was developed by the Computer Systems Engineering group * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and * contributed to Berkeley. * * All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Lawrence Berkeley Laboratories. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)sem.c 8.1 (Berkeley) 6/6/93 */#include <sys/param.h>#include <ctype.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include "config.h"#include "sem.h"/* * config semantics. */#define NAMESIZE 100 /* local name buffers */static const char *s_generic;static const char *s_qmark;static struct hashtab *attrtab; /* for attribute lookup */static struct hashtab *cfhashtab; /* for config lookup */static struct hashtab *devitab; /* etc */static struct attr errattr;static struct devbase errdev;static struct devbase **nextbase;static struct config **nextcf;static struct devi **nextdevi;static struct devi **nextpseudo;static int has_errobj __P((struct nvlist *, void *));static struct nvlist *addtoattr __P((struct nvlist *, struct devbase *));static int exclude __P((struct nvlist *, const char *, const char *));static int resolve __P((struct nvlist **, const char *, const char *, struct nvlist *, int));static int lresolve __P((struct nvlist **, const char *, const char *, struct nvlist *, int));static struct devi *newdevi __P((const char *, int, struct devbase *d));static struct devi *getdevi __P((const char *));static const char *concat __P((const char *, int));static int split __P((const char *, size_t, char *, size_t, int *));static void selectbase __P((struct devbase *));static int onlist __P((struct nvlist *, void *));static const char **fixloc __P((const char *, struct attr *, struct nvlist *));voidinitsem(){ attrtab = ht_new(); errattr.a_name = "<internal>"; allbases = NULL; nextbase = &allbases; cfhashtab = ht_new(); allcf = NULL; nextcf = &allcf; devitab = ht_new(); alldevi = NULL; nextdevi = &alldevi; errdev.d_name = "<internal>"; allpseudo = NULL; nextpseudo = &allpseudo; s_generic = intern("generic"); s_qmark = intern("?");}voidenddefs(fname) const char *fname;{ register struct devbase *dev; for (dev = allbases; dev != NULL; dev = dev->d_next) { if (!dev->d_isdef) { (void)fprintf(stderr, "%s: device `%s' used but not defined\n", fname, dev->d_name); errors++; continue; } } if (errors) { (void)fprintf(stderr, "*** Stop.\n"); exit(1); }}voidsetdefmaxusers(min, def, max) int min, def, max;{ if (min < 1 || min > def || def > max) error("maxusers must have 1 <= min <= default <= max"); else { minmaxusers = min; defmaxusers = def; maxmaxusers = max; }}voidsetmaxusers(n) int n;{ if (maxusers != 0) { error("duplicate maxusers parameter"); return; } maxusers = n; if (n < minmaxusers) { error("warning: minimum of %d maxusers assumed\n", minmaxusers); errors--; /* take it away */ maxusers = minmaxusers; } else if (n > maxmaxusers) { error("warning: maxusers (%d) > %d", n, maxmaxusers); errors--; }}/* * Define an attribute, optionally with an interface (a locator list). * Since an empty locator list is logically different from "no interface", * all locator lists include a dummy head node, which we discard here. */intdefattr(name, locs) const char *name; struct nvlist *locs;{ register struct attr *a; register struct nvlist *nv; register int len; a = emalloc(sizeof *a); if (ht_insert(attrtab, name, a)) { free(a); error("attribute `%s' already defined", name); nvfreel(locs); return (1); } a->a_name = name; if (locs != NULL) { a->a_iattr = 1; a->a_locs = locs->nv_next; nvfree(locs); } else { a->a_iattr = 0; a->a_locs = NULL; } len = 0; for (nv = a->a_locs; nv != NULL; nv = nv->nv_next) len++; a->a_loclen = len; a->a_devs = NULL; a->a_refs = NULL; return (0);}/* * Return true if the given `error object' is embedded in the given * pointer list. */static inthas_errobj(nv, obj) register struct nvlist *nv; register void *obj;{ for (; nv != NULL; nv = nv->nv_next) if (nv->nv_ptr == obj) return (1); return (0);}/* * Add a device base to a list in an attribute (actually, to any list). * Note that this does not check for duplicates, and does reverse the * list order, but no one cares anyway. */static struct nvlist *addtoattr(l, dev) register struct nvlist *l; register struct devbase *dev;{ register struct nvlist *n; n = newnv(NULL, NULL, dev, 0); n->nv_next = l; return (n);}/* * Device a device, giving its allowable parent attachments, if any. * This may (or may not) also define an interface attribute and/or refer * to existing attributes. There may be a list of vectors. */voiddefdev(dev, ispseudo, atlist, vectors, loclist, attrs) register struct devbase *dev; int ispseudo; struct nvlist *atlist, *vectors, *loclist, *attrs;{ register struct nvlist *nv; register struct attr *a; if (dev == &errdev) goto bad; if (dev->d_isdef) { error("redefinition of `%s'", dev->d_name); goto bad; } dev->d_isdef = 1; if (has_errobj(attrs, &errattr)) goto bad; /* * Handle implicit attribute definition from locator list. Do * this before scanning the `at' list so that we can have, e.g.: * device foo at other, foo { slot = -1 } * (where you can plug in a foo-bus extender to a foo-bus). */ if (loclist != NULL) { nv = loclist; loclist = NULL; /* defattr disposes of them for us */ if (defattr(dev->d_name, nv)) goto bad; nv = newnv(dev->d_name, NULL, getattr(dev->d_name), 0); nv->nv_next = attrs; attrs = nv; } /* Committed! Set up fields. */ dev->d_ispseudo = ispseudo; dev->d_atlist = atlist; dev->d_vectors = vectors; dev->d_attrs = attrs; /* * Turn the `at' list into interface attributes (map each * nv_name to an attribute, or to NULL for root), and add * this device to those attributes, so that children can * be listed at this particular device if they are supported * by that attribute. */ for (nv = atlist; nv != NULL; nv = nv->nv_next) { if (nv->nv_name == NULL) { nv->nv_ptr = NULL; /* at root */ continue; } nv->nv_ptr = a = getattr(nv->nv_name); if (a == &errattr) continue; /* already complained */ if (!a->a_iattr) error("%s cannot be at plain attribute `%s'", dev->d_name, a->a_name); else a->a_devs = addtoattr(a->a_devs, dev); } /* * For each interface attribute this device refers to, add this * device to its reference list. This makes, e.g., finding all * "scsi"s easier. */ for (nv = attrs; nv != NULL; nv = nv->nv_next) { a = nv->nv_ptr; if (a->a_iattr) a->a_refs = addtoattr(a->a_refs, dev); } return;bad: nvfreel(atlist); nvfreel(vectors); nvfreel(loclist); nvfreel(attrs);}/* * Look up a devbase. Also makes sure it is a reasonable name, * i.e., does not end in a digit or contain special characters. */struct devbase *getdevbase(name) const char *name;{ register u_char *p; register struct devbase *dev; p = (u_char *)name; if (!isalpha(*p)) goto badname; while (*++p) { if (!isalnum(*p) && *p != '_') goto badname; } if (isdigit(*--p)) {badname: error("bad device base name `%s'", name); return (&errdev); } dev = ht_lookup(devbasetab, name); if (dev == NULL) { dev = emalloc(sizeof *dev); dev->d_name = name; dev->d_next = NULL; dev->d_isdef = 0; dev->d_major = NODEV; dev->d_atlist = NULL; dev->d_vectors = NULL; dev->d_attrs = NULL; dev->d_ihead = NULL; dev->d_ipp = &dev->d_ihead; dev->d_umax = 0; *nextbase = dev; nextbase = &dev->d_next; if (ht_insert(devbasetab, name, dev)) panic("getdevbase(%s)", name); } return (dev);}/* * Look up an attribute. */struct attr *getattr(name) const char *name;{ struct attr *a; if ((a = ht_lookup(attrtab, name)) == NULL) { error("undefined attribute `%s'", name); a = &errattr; } return (a);}/* * Set the major device number for a device, so that it can be used * as a root/swap/dumps "on" device in a configuration. */voidsetmajor(d, n) struct devbase *d; int n;{ if (d != &errdev && d->d_major != NODEV) error("device `%s' is already major %d", d->d_name, d->d_major); else d->d_major = n;}#define ABS(x) ((x) < 0 ? -(x) : (x))static intexclude(nv, name, what) struct nvlist *nv; const char *name, *what;{ if (nv != NULL) { error("%s: swap generic must not specify %s", name, what); return (1); } return (0);}/* * Map things like "ra0b" => makedev(major("ra"), 0*8 + 'b'-'a'). * Handle the case where the device number is given but there is no * corresponding name, and map NULL to the default. */static intresolve(nvp, name, what, dflt, part) register struct nvlist **nvp; const char *name, *what; struct nvlist *dflt; register int part;{ register struct nvlist *nv; register struct devbase *dev; register const char *cp; register int maj, min, l; int unit; char buf[NAMESIZE]; if ((u_int)(part -= 'a') >= 7) panic("resolve"); if ((nv = *nvp) == NULL) { /* * Apply default. Easiest to do this by number. */ maj = major(dflt->nv_int); min = (minor(dflt->nv_int) & ~7) | part; *nvp = nv = newnv(NULL, NULL, NULL, makedev(maj, min)); } if (nv->nv_int != NODEV) { /* * By the numbers. Find the appropriate major number * to make a name. */ maj = major(nv->nv_int); min = minor(nv->nv_int); for (dev = allbases; dev != NULL; dev = dev->d_next) if (dev->d_major == maj) break; if (dev == NULL) (void)sprintf(buf, "<%d/%d>", maj, min); else (void)sprintf(buf, "%s%d%c", dev->d_name, min >> 3, (min & 7) + 'a'); nv->nv_str = intern(buf); return (0); } /* * The normal case: things like "ra2b". Check for partition * suffix, remove it if there, and split into name ("ra") and * unit (2). */ l = strlen(nv->nv_str); cp = &nv->nv_str[l]; if (l > 1 && *--cp >= 'a' && *cp <= 'h' && isdigit(cp[-1])) { l--; part = *cp - 'a'; } cp = nv->nv_str; if (split(cp, l, buf, sizeof buf, &unit)) { error("%s: invalid %s device name `%s'", name, what, cp); return (1); } dev = ht_lookup(devbasetab, intern(buf)); if (dev == NULL || dev->d_major == NODEV) { error("%s: can't make %s device from `%s'", name, what, nv->nv_str); return (1); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -