ref: 344034ea3fd4be80713a50989a03e331fd609dc9
parent: 1437af59aab6295023dff89337b24b84f2096d9f
author: Michael Forney <[email protected]>
date: Wed Mar 30 16:59:57 EDT 2022
add -s option for 9p over stdin/stdout
--- a/dat.h
+++ b/dat.h
@@ -539,7 +539,8 @@
struct Conn {
Conn *next;
- int fd;
+ int rfd;
+ int wfd;
int iounit;
int versioned;
--- a/fns.h
+++ b/fns.h
@@ -78,6 +78,8 @@
int compresslog(Arena*);
void setval(Blk*, Kvp*);
+Conn* newconn(int, int);
+
char* loadusers(int, Tree*);
User* uid2user(int);
User* name2user(char*);
--- a/fs.c
+++ b/fs.c
@@ -164,7 +164,7 @@
}
static void
-fshangup(int fd, char *fmt, ...)
+fshangup(Conn *c, char *fmt, ...)
{
char buf[ERRMAX];
va_list ap;
@@ -173,7 +173,8 @@
vsnprint(buf, sizeof(buf), fmt, ap);
va_end(ap);
fprint(2, "%s\n", buf);
- close(fd);
+ close(c->rfd);
+ close(c->wfd);
abort();
}
@@ -188,9 +189,9 @@
dprint("→ %F\n", r);
if((n = convS2M(r, buf, sizeof(buf))) == 0)
abort();
- w = write(m->conn->fd, buf, n);
+ w = write(m->conn->wfd, buf, n);
if(w != n)
- fshangup(m->conn->fd, Eio);
+ fshangup(m->conn, Eio);
if(m->type != Tflush) {
lock(&fs->mflushlk);
fs->mflush[m->tag] = nil;
@@ -503,7 +504,7 @@
for(c = fs->conns; c != nil; c = c->next){
lock(&c->fidtablk);
- fprint(fd, "fids:%d\n", c->fd);
+ fprint(fd, "fids:%d\n", c->rfd);
for(i = 0; i < Nfidtab; i++)
for(f = c->fidtab[i]; f != nil; f = f->next){
rlock(f->dent);
@@ -608,7 +609,7 @@
int sz, n;
Fmsg *m;
- n = readn(c->fd, szbuf, 4);
+ n = readn(c->rfd, szbuf, 4);
if(n <= 0){
*pm = nil;
return n;
@@ -624,7 +625,7 @@
}
if((m = malloc(sizeof(Fmsg)+sz)) == nil)
return -1;
- if(readn(c->fd, m->buf+4, sz-4) != sz-4){
+ if(readn(c->rfd, m->buf+4, sz-4) != sz-4){
werrstr("short read: %r");
free(m);
return -1;
@@ -1966,39 +1967,45 @@
unlock(&fs->mflushlk);
}
-void
-runfs(int, void *pfd)
+Conn *
+newconn(int rfd, int wfd)
{
Conn *c;
- int fd;
- char err[128];
- Fcall r;
- Fmsg *m;
- fd = (uintptr)pfd;
- if((c = mallocz(sizeof(*c), 1)) == nil){
- fshangup(fd, "malloc: %r");
- return;
- }
- c->fd = fd;
+ if((c = mallocz(sizeof(*c), 1)) == nil)
+ return nil;
+ c->rfd = rfd;
+ c->wfd = wfd;
c->iounit = Max9p;
- lock(&fs->connlk);
c->next = fs->conns;
+ lock(&fs->connlk);
fs->conns = c;
unlock(&fs->connlk);
+ return c;
+}
+
+void
+runfs(int, void *pc)
+{
+ Conn *c;
+ char err[128];
+ Fcall r;
+ Fmsg *m;
+
+ c = pc;
while(1){
if(readmsg(c, &m) < 0){
- fshangup(fd, "read message: %r");
+ fshangup(c, "read message: %r");
return;
}
if(m == nil)
break;
if(convM2S(m->buf, m->sz, m) == 0){
- fshangup(fd, "invalid message: %r");
+ fshangup(c, "invalid message: %r");
return;
}
if(m->type != Tversion && !c->versioned){
- fshangup(fd, "version required");
+ fshangup(c, "version required");
return;
}
dprint("← %F\n", &m->Fcall);
--- a/main.c
+++ b/main.c
@@ -10,6 +10,7 @@
int ream;
int debug;
+int stdio;
int noauth;
int nproc;
char *forceuser;
@@ -86,6 +87,7 @@
{
char *ann, adir[40], ldir[40];
int actl, lctl, fd;
+ Conn *c;
ann = arg;
if((actl = announce(ann, adir)) < 0)
@@ -101,7 +103,13 @@
fprint(2, "accept %s: %r", ldir);
continue;
}
- launch(runfs, -1, (void *)fd, "netio");
+ if(!(c = newconn(fd, fd))){
+ close(fd);
+ fprint(2, "%r");
+ continue;
+ }
+
+ launch(runfs, -1, c, "netio");
}
close(actl);
}
@@ -118,6 +126,7 @@
{
int i, srvfd, ctlfd, nann;
char *s, *ann[16];
+ Conn *c;
nann = 0;
ARGBEGIN{
@@ -138,6 +147,9 @@
case 'n':
srvname = EARGF(usage());
break;
+ case 's':
+ stdio = 1;
+ break;
case 'A':
noauth = 1;
break;
@@ -211,7 +223,15 @@
launch(runsync, -1, &fs->syncq[i], "syncio");
for(i = 0; i < nann; i++)
launch(runannounce, -1, ann[i], "announce");
- if(srvfd != -1)
- launch(runfs, -1, (void *)srvfd, "srvio");
+ if(srvfd != -1){
+ if((c = newconn(srvfd, srvfd)) == nil)
+ sysfatal("%r");
+ launch(runfs, -1, c, "srvio");
+ }
+ if(stdio){
+ if((c = newconn(0, 1)) == nil)
+ sysfatal("%r");
+ runfs(-1, c);
+ }
exits(nil);
}