Pārlūkot izejas kodu

Plan 9 from Bell Labs 2007-09-29

David du Colombier 17 gadi atpakaļ
vecāks
revīzija
be25d02019

+ 3 - 2
dist/replica/_plan9.db

@@ -8204,7 +8204,7 @@ sys/src/9/port/devcons.c - 664 sys sys 1176658321 22943
 sys/src/9/port/devdraw.c - 664 sys sys 1147023550 44447
 sys/src/9/port/devdup.c - 664 sys sys 1014931172 2332
 sys/src/9/port/devenv.c - 664 sys sys 1169498893 7015
-sys/src/9/port/devfs.c - 664 sys sys 1190443411 13857
+sys/src/9/port/devfs.c - 664 sys sys 1191008235 14697
 sys/src/9/port/devkbmap.c - 664 sys sys 1130763846 3064
 sys/src/9/port/devkprof.c - 664 sys sys 1014931173 3111
 sys/src/9/port/devloopback.c - 664 sys sys 1138458368 14579
@@ -9614,7 +9614,7 @@ sys/src/cmd/auth/factotum/util.c - 664 sys sys 1182192267 18452
 sys/src/cmd/auth/factotum/wep.c - 664 sys sys 1107706104 2140
 sys/src/cmd/auth/guard.srv.c - 664 sys sys 1172776773 2873
 sys/src/cmd/auth/iam.c - 664 sys sys 1015008430 841
-sys/src/cmd/auth/keyfs.c - 664 sys sys 1190849063 19129
+sys/src/cmd/auth/keyfs.c - 664 sys sys 1191007186 19129
 sys/src/cmd/auth/lib - 20000000775 sys sys 1045504996 0
 sys/src/cmd/auth/lib/error.c - 664 sys sys 1155994850 318
 sys/src/cmd/auth/lib/fs.c - 664 sys sys 1015008430 228
@@ -15732,3 +15732,4 @@ usr/glenda/lib/profile - 664 glenda glenda 1105128663 890
 usr/glenda/readme.acme - 664 glenda glenda 1019860628 4753
 usr/glenda/readme.rio - 664 glenda glenda 1019860628 6370
 usr/glenda/tmp - 20000000775 glenda glenda 1018802620 0
+386/bin/auth/keyfs - 775 sys sys 1191035876 116576

+ 3 - 3
dist/replica/plan9.db

@@ -56,7 +56,7 @@
 386/bin/auth/fgui - 775 sys sys 1184731229 219079
 386/bin/auth/guard.srv - 775 sys sys 1188446841 144638
 386/bin/auth/iam - 775 sys sys 1085076981 50791
-386/bin/auth/keyfs - 775 sys sys 1190863289 116576
+386/bin/auth/keyfs - 775 sys sys 1191035876 116576
 386/bin/auth/login - 775 sys sys 1179372070 104816
 386/bin/auth/newns - 775 sys sys 1179372070 90035
 386/bin/auth/none - 775 sys sys 1179432184 89808
@@ -8204,7 +8204,7 @@ sys/src/9/port/devcons.c - 664 sys sys 1176658321 22943
 sys/src/9/port/devdraw.c - 664 sys sys 1147023550 44447
 sys/src/9/port/devdup.c - 664 sys sys 1014931172 2332
 sys/src/9/port/devenv.c - 664 sys sys 1169498893 7015
-sys/src/9/port/devfs.c - 664 sys sys 1190443411 13857
+sys/src/9/port/devfs.c - 664 sys sys 1191008235 14697
 sys/src/9/port/devkbmap.c - 664 sys sys 1130763846 3064
 sys/src/9/port/devkprof.c - 664 sys sys 1014931173 3111
 sys/src/9/port/devloopback.c - 664 sys sys 1138458368 14579
@@ -9614,7 +9614,7 @@ sys/src/cmd/auth/factotum/util.c - 664 sys sys 1182192267 18452
 sys/src/cmd/auth/factotum/wep.c - 664 sys sys 1107706104 2140
 sys/src/cmd/auth/guard.srv.c - 664 sys sys 1172776773 2873
 sys/src/cmd/auth/iam.c - 664 sys sys 1015008430 841
-sys/src/cmd/auth/keyfs.c - 664 sys sys 1190849063 19129
+sys/src/cmd/auth/keyfs.c - 664 sys sys 1191007186 19129
 sys/src/cmd/auth/lib - 20000000775 sys sys 1045504996 0
 sys/src/cmd/auth/lib/error.c - 664 sys sys 1155994850 318
 sys/src/cmd/auth/lib/fs.c - 664 sys sys 1015008430 228

+ 3 - 0
dist/replica/plan9.log

@@ -52915,3 +52915,6 @@
 1190923205 0 a sys/src/boot/pc/devbios.h - 664 sys sys 1190921600 453
 1190925003 0 c 386/9loaddebug - 775 sys sys 1190923617 435057
 1190925003 1 c 386/9loadlitedebug - 775 sys sys 1190923620 220555
+1191007805 0 c sys/src/cmd/auth/keyfs.c - 664 sys sys 1191007186 19129
+1191009605 0 c sys/src/9/port/devfs.c - 664 sys sys 1191008235 14697
+1191036604 0 c 386/bin/auth/keyfs - 775 sys sys 1191035876 116576

+ 66 - 41
sys/src/9/port/devfs.c

@@ -31,6 +31,9 @@ enum {
 	Iswrite = 0,
 	Isread,
 
+	Optional = 0,
+	Mustexist,
+
 	/* tunable parameters */
 	Maxconf	= 4*1024,	/* max length for config */
 	Ndevs	= 32,		/* max. inner devs per command */
@@ -66,11 +69,12 @@ extern Dev fsdevtab;		/* forward */
  * Once configured, a fsdev is never removed.  The name of those
  * configured is never nil.  We have no locks here.
  */
-static Fsdev	fsdev[Nfsdevs];
+static Fsdev fsdev[Nfsdevs];	/* internal representation of config */
+static char confstr[Maxconf];	/* textual configuration */
 
-static Qid	tqid = {Qtop, 0, QTDIR};
-static Qid	dqid = {Qdir, 0, QTDIR};
-static Qid	cqid = {Qctl, 0, 0};
+static Qid tqid = {Qtop, 0, QTDIR};
+static Qid dqid = {Qdir, 0, QTDIR};
+static Qid cqid = {Qctl, 0, 0};
 
 static Cmdtab configs[] = {
 	Fmirror,"mirror",	0,
@@ -80,10 +84,6 @@ static Cmdtab configs[] = {
 	Fclear,	"clear",	1,	
 };
 
-static char	confstr[Maxconf];
-static int	configed;
-
-
 static Fsdev*
 path2dev(int i, int mustexist)
 {
@@ -175,8 +175,12 @@ mpshut(Fsdev *mp)
 }
 
 
+/*
+ * process a single line of configuration,
+ * often of the form "name idev0 idev1".
+ */
 static void
-mconfig(char* a, long n)	/* "name idev0 idev1" */
+mconfig(char* a, long n)
 {
 	int	i;
 	vlong	size, start;
@@ -185,17 +189,20 @@ mconfig(char* a, long n)	/* "name idev0 idev1" */
 	Cmdtab	*ct;
 	Fsdev	*mp;
 	Inner	*inprv;
-	static	QLock	lck;
+	static QLock lck;
+
+	/* ignore comments & empty lines */
+	if (*a == '\0' || *a == '#' || *a == '\n')
+		return;
 
 	size = 0;
 	start = 0;
+	/* insert header if config is empty */
 	if (confstr[0] == 0)
 		seprint(confstr, confstr + sizeof confstr, Cfgstr);
 	mp = nil;
 	cb = nil;
 	oldc = confstr + strlen(confstr);
-	if (*a == '\0' || *a == '#' || *a == '\n')
-		return;
 
 	qlock(&lck);
 	if (waserror()){
@@ -208,12 +215,17 @@ mconfig(char* a, long n)	/* "name idev0 idev1" */
 		nexterror();
 	}
 
+	/* append this command after parsing to confstr */
 	cb = parsecmd(a, n);
 	c = oldc;
 	for (i = 0; i < cb->nf; i++)
-		c = seprint(c, confstr + sizeof confstr, "%s ", cb->f[i]);
-	if (c > confstr)
+		c = seprint(c, confstr + sizeof confstr - 1, "%s ", cb->f[i]);
+	if (c > oldc) {
 		c[-1] = '\n';
+		c[0]  = '\0';
+	}
+
+	/* lookup command, execute special cases */
 	ct = lookupcmd(cb, configs, nelem(configs));
 	cb->f++;			/* skip command */
 	cb->nf--;
@@ -228,6 +240,7 @@ mconfig(char* a, long n)	/* "name idev0 idev1" */
 		cb->nf -= 2;
 		break;
 	case Fclear:
+		/* clear both internal & textual representations of config */
 		for (mp = fsdev; mp < fsdev + nelem(fsdev); mp++)
 			mpshut(mp);
 		*confstr = '\0';
@@ -240,17 +253,17 @@ mconfig(char* a, long n)	/* "name idev0 idev1" */
 	}
 	if (cb->nf < 2)
 		error("too few fields in fs config");
+	else if (cb->nf - 1 > Ndevs)
+		error("too many devices; fix #k: increase Ndevs");
 
-	/* reject name if already in use */
+	/* reject new name if already in use, validate old ones */
 	for (i = 0; i < nelem(fsdev); i++)
 		if (fsdev[i].name != nil && strcmp(fsdev[i].name, cb->f[0])==0)
 			error(Eexist);
-
-	if (cb->nf - 1 > Ndevs)
-		error("too many devices; fix #k: increase Ndevs");
 	for (i = 0; i < cb->nf; i++)
 		validname(cb->f[i], (i != 0));
 
+	/* populate new Fsdev with parsed command */
 	mp = devalloc();
 	mp->type = ct->index;
 	if (mp->type == Fpart){
@@ -270,7 +283,6 @@ mconfig(char* a, long n)	/* "name idev0 idev1" */
 		mp->ndevs++;
 	}
 	setdsize(mp);
-	configed = 1;
 
 	poperror();
 	qunlock(&lck);
@@ -283,45 +295,54 @@ rdconf(void)
 	int mustrd;
 	char *c, *e, *p, *s;
 	Chan *cc;
-	Chan **ccp;
+	static int configed;
 
+	/* only read config file once */
+	if (configed)
+		return;
+	configed = 1;
+
+	/* identify the config file */
 	s = getconf("fsconfig");
 	if (s == nil){
 		mustrd = 0;
 		s = "/dev/sdC0/fscfg";
 	} else
 		mustrd = 1;
-	ccp = &cc;
-	*ccp = nil;
+
+	/* read it */
+	cc = nil;
 	c = nil;
 	if (waserror()){
-		configed = 1;
-		if (*ccp != nil)
-			cclose(*ccp);
+		if (cc != nil)
+			cclose(cc);
 		if (c)
 			free(c);
 		if (!mustrd)
 			return;
 		nexterror();
 	}
-	*ccp = namec(s, Aopen, OREAD, 0);
-	devtab[(*ccp)->type]->read(*ccp, confstr, sizeof confstr, 0);
-	cclose(*ccp);
-	*ccp = nil;
+	cc = namec(s, Aopen, OREAD, 0);
+	devtab[cc->type]->read(cc, confstr, sizeof confstr, 0);
+	cclose(cc);
+	cc = nil;
+
+	/* validate, copy and erase config; mconfig will repopulate confstr */
 	if (strncmp(confstr, Cfgstr, strlen(Cfgstr)) != 0)
 		error("bad #k config, first line must be: 'fsdev:\\n'");
 	kstrdup(&c, confstr + strlen(Cfgstr));
 	memset(confstr, 0, sizeof confstr);
-	for (p = c; p != nil && *p != 0; p = e){
+
+	/* process config copy one line at a time */
+	for (p = c; p != nil && *p != '\0'; p = e){
 		e = strchr(p, '\n');
 		if (e == nil)
 			e = p + strlen(p);
-		if (e == p) {
+		else
 			e++;
-			continue;
-		}
 		mconfig(p, e - p);
 	}
+	USED(cc);		/* until now, can be used in waserror clause */
 	poperror();
 }
 
@@ -363,7 +384,7 @@ mgen(Chan *c, char*, Dirtab*, int, int i, Dir *dp)
 	qid.path = Qfirst + i;
 	qid.vers = 0;
 	qid.type = 0;
-	mp = path2dev(i, 0);
+	mp = path2dev(i, Optional);
 	if (mp == nil)
 		return -1;
 	kstrcpy(up->genbuf, mp->name, sizeof(up->genbuf));
@@ -380,8 +401,7 @@ mattach(char *spec)
 static Walkqid*
 mwalk(Chan *c, Chan *nc, char **name, int nname)
 {
-	if (!configed)
-		rdconf();
+	rdconf();
 	return devwalk(c, nc, name, nname, 0, 0, mgen);
 }
 
@@ -405,7 +425,7 @@ mstat(Chan *c, uchar *db, int n)
 		devdir(c, cqid, "ctl", 0, eve, 0664, &d);
 		break;
 	default:
-		mp = path2dev(p - Qfirst, 1);
+		mp = path2dev(p - Qfirst, Mustexist);
 		devdir(c, c->qid, mp->name, mp->size, eve, 0664, &d);
 	}
 	n = convD2M(&d, db, n);
@@ -537,10 +557,15 @@ mread(Chan *c, void *a, long n, vlong off)
 
 	if (c->qid.type & QTDIR)
 		return devdirread(c, a, n, 0, 0, mgen);
-	if (c->qid.path == Qctl)
-		return readstr((long)off, a, n, confstr + strlen(Cfgstr));
+	if (c->qid.path == Qctl) {
+		i = strlen(Cfgstr);
+		if (strlen(confstr) >= i)	/* skip header if present */
+			return readstr((long)off, a, n, confstr + i);
+		else
+			return readstr((long)off, a, n, confstr);
+	}
 	i = c->qid.path - Qfirst;
-	mp = path2dev(i, 1);
+	mp = path2dev(i, Mustexist);
 
 	if (off >= mp->size)
 		return 0;
@@ -616,7 +641,7 @@ mwrite(Chan *c, void *a, long n, vlong off)
 		mconfig(a, n);
 		return n;
 	}
-	mp = path2dev(c->qid.path - Qfirst, 1);
+	mp = path2dev(c->qid.path - Qfirst, Mustexist);
 
 	if (off >= mp->size)
 		return 0;

+ 3 - 3
sys/src/cmd/auth/keyfs.c

@@ -106,7 +106,7 @@ Qid	mkqid(User*, ulong);
 int	dostat(User*, ulong, void*, int);
 int	newkeys(void);
 void	warning(void);
-int	wierdfmt(Fmt *f);
+int	weirdfmt(Fmt *f);
 
 char	*Auth(Fid*), *Attach(Fid*), *Version(Fid*),
 	*Flush(Fid*), *Walk(Fid*),
@@ -142,7 +142,7 @@ main(int argc, char *argv[])
 	char *mntpt;
 	int p[2];
 
-	fmtinstall('W', wierdfmt);
+	fmtinstall('W', weirdfmt);
 	mntpt = "/mnt/keys";
 	ARGBEGIN{
 	case 'm':
@@ -795,7 +795,7 @@ writeusers(void)
 }
 
 int
-wierdfmt(Fmt *f)
+weirdfmt(Fmt *f)
 {
 	char *s, buf[ANAMELEN*4 + 1];
 	int i, j, n;