]> git.lizzy.rs Git - plan9front.git/blobdiff - sys/src/cmd/aux/consolefs.c
import E script from bell labs
[plan9front.git] / sys / src / cmd / aux / consolefs.c
old mode 100755 (executable)
new mode 100644 (file)
index 180431b..0d4146b
@@ -5,6 +5,7 @@
 #include <bio.h>
 #include <ndb.h>
 #include <thread.h>
+#include <9p.h>
 
 /*
  *  This fs presents a 1 level file system.  It contains
@@ -12,7 +13,7 @@
  */
 
 typedef struct Console Console;
-typedef struct Fid Fid;
+typedef struct Aux Aux;
 typedef struct Request Request;
 typedef struct Reqlist Reqlist;
 typedef struct Fs Fs;
@@ -28,7 +29,7 @@ enum
 
        Bufsize=        32*1024,        /* chars buffered per reader */
        Maxcons=        64,             /* maximum consoles */
-       Nhash=          64,             /* Fid hash buckets */
+       Nhash=          64,             /* Aux hash buckets */
 };
 
 #define TYPE(x)                (((ulong)x.path) & 0xf)
@@ -38,7 +39,7 @@ enum
 struct Request
 {
        Request *next;
-       Fid     *fid;
+       Aux     *fid;
        Fs      *fs;
        Fcall   f;
        uchar   buf[1];
@@ -51,11 +52,11 @@ struct Reqlist
        Request *last;
 };
 
-struct Fid
+struct Aux
 {
        Lock;
-       Fid     *next;                  /* hash list */
-       Fid     *cnext;                 /* list of Fid's on a console */
+       Aux     *next;                  /* hash list */
+       Aux     *cnext;                 /* list of Aux's on a console */
        int     fid;
        int     ref;
 
@@ -93,7 +94,7 @@ struct Console
        int     cfd;
        int     sfd;
 
-       Fid     *flist;                 /* open fids to broadcast to */
+       Aux     *flist;                 /* open fids to broadcast to */
 };
 
 struct Fs
@@ -102,7 +103,7 @@ struct Fs
 
        int     fd;                     /* to kernel mount point */
        int     messagesize;
-       Fid     *hash[Nhash];
+       Aux     *hash[Nhash];
        Console *cons[Maxcons];
        int     ncons;
 };
@@ -112,30 +113,30 @@ extern    Fs*     fsmount(char*);
 
 extern void    fsreader(void*);
 extern void    fsrun(void*);
-extern Fid*    fsgetfid(Fs*, int);
-extern void    fsputfid(Fs*, Fid*);
+extern Aux*    fsgetfid(Fs*, int);
+extern void    fsputfid(Fs*, Aux*);
 extern int     fsdirgen(Fs*, Qid, int, Dir*, uchar*, int);
 extern void    fsreply(Fs*, Request*, char*);
-extern void    fskick(Fs*, Fid*);
+extern void    fskick(Fs*, Aux*);
 extern int     fsreopen(Fs*, Console*);
 
-extern void    fsversion(Fs*, Request*, Fid*);
-extern void    fsflush(Fs*, Request*, Fid*);
-extern void    fsauth(Fs*, Request*, Fid*);
-extern void    fsattach(Fs*, Request*, Fid*);
-extern void    fswalk(Fs*, Request*, Fid*);
-extern void    fsclwalk(Fs*, Request*, Fid*);
-extern void    fsopen(Fs*, Request*, Fid*);
-extern void    fscreate(Fs*, Request*, Fid*);
-extern void    fsread(Fs*, Request*, Fid*);
-extern void    fswrite(Fs*, Request*, Fid*);
-extern void    fsclunk(Fs*, Request*, Fid*);
-extern void    fsremove(Fs*, Request*, Fid*);
-extern void    fsstat(Fs*, Request*, Fid*);
-extern void    fswstat(Fs*, Request*, Fid*);
-
-
-void   (*fcall[])(Fs*, Request*, Fid*) =
+extern void    fsversion(Fs*, Request*, Aux*);
+extern void    fsflush(Fs*, Request*, Aux*);
+extern void    fsauth(Fs*, Request*, Aux*);
+extern void    fsattach(Fs*, Request*, Aux*);
+extern void    fswalk(Fs*, Request*, Aux*);
+extern void    fsclwalk(Fs*, Request*, Aux*);
+extern void    fsopen(Fs*, Request*, Aux*);
+extern void    fscreate(Fs*, Request*, Aux*);
+extern void    fsread(Fs*, Request*, Aux*);
+extern void    fswrite(Fs*, Request*, Aux*);
+extern void    fsclunk(Fs*, Request*, Aux*);
+extern void    fsremove(Fs*, Request*, Aux*);
+extern void    fsstat(Fs*, Request*, Aux*);
+extern void    fswstat(Fs*, Request*, Aux*);
+
+
+void   (*fcall[])(Fs*, Request*, Aux*) =
 {
        [Tflush]        fsflush,
        [Tversion]      fsversion,
@@ -199,7 +200,7 @@ Ndb *db;
  *  any request that can get queued for a delayed reply
  */
 Request*
-allocreq(Fs *fs, int bufsize)
+alloccreq(Fs *fs, int bufsize)
 {
        Request *r;
 
@@ -343,10 +344,10 @@ fsdirgen(Fs *fs, Qid parent, int i, Dir *d, uchar *buf, int nbuf)
 Fs*
 fsmount(char *mntpt)
 {
+       int pfd[2];
+
        Fs *fs;
-       int pfd[2], srv;
-       char buf[32];
-       int n;
+       Dir d;
        static void *v[2];
 
        fs = emalloc(sizeof(Fs));
@@ -359,21 +360,19 @@ fsmount(char *mntpt)
        v[1] = pfd;
        proccreate(fsrun, v, 16*1024);
 
-       /* Typically mounted before /srv exists */
-       if(access("#s/consoles", AEXIST) < 0){
-               srv = create("#s/consoles", OWRITE, 0666);
-               if(srv < 0)
-                       fatal("post: %r");
+       if(postfd("consoles", pfd[1]) < 0)
+               sysfatal("post: %r");
 
-               n = sprint(buf, "%d", pfd[1]);
-               if(write(srv, buf, n) < 0)
-                       fatal("write srv: %r");
+       nulldir(&d);
+       d.mode = 0666;
+       dirwstat("/srv/consoles", &d);
 
-               close(srv);
-       }
+       if(mntpt){
+               if(amount(pfd[1], mntpt, MBEFORE, "") == -1)
+                       sysfatal("mount %s: %r", mntpt);
+       }else
+               close(pfd[1]);
 
-       mount(pfd[1], -1, mntpt, MBEFORE, "");
-       close(pfd[1]);
        return fs;
 }
 
@@ -516,7 +515,7 @@ console(Fs* fs, char *name, char *dev, int speed, int cronly, int ondemand)
  *  the reader may miss data but always sees an in order sequence.
  */
 void
-fromconsole(Fid *f, char *p, int n)
+fromconsole(Aux *f, char *p, int n)
 {
        char *rp, *wp, *ep;
        int pass;
@@ -552,10 +551,10 @@ fromconsole(Fid *f, char *p, int n)
  *  broadcast a list of members to all listeners
  */
 void
-bcastmembers(Fs *fs, Console *c, char *msg, Fid *f)
+bcastmembers(Fs *fs, Console *c, char *msg, Aux *f)
 {
        int n;
-       Fid *fl;
+       Aux *fl;
        char buf[512];
 
        sprint(buf, "[%s%s", msg, f->user);
@@ -590,7 +589,7 @@ void
 fsreader(void *v)
 {
        int n;
-       Fid *fl;
+       Aux *fl;
        char buf[1024];
        Fs *fs;
        Console *c;
@@ -653,8 +652,6 @@ readdb(Fs *fs)
        }
 }
 
-int dbmtime;
-
 /*
  *  a request processor (one per Fs)
  */
@@ -663,8 +660,7 @@ fsrun(void *v)
 {
        int n, t;
        Request *r;
-       Fid *f;
-       Dir *d;
+       Aux *f;
        void **a = v;
        Fs* fs;
        int *pfd;
@@ -672,17 +668,15 @@ fsrun(void *v)
        fs = a[0];
        pfd = a[1];
        fs->fd = pfd[0];
+       readdb(fs);
        notify(handler);
        for(;;){
-               d = dirstat(consoledb);
-               if(d != nil && d->mtime != dbmtime){
-                       dbmtime = d->mtime;
+               if(ndbchanged(db))
                        readdb(fs);
-               }
-               free(d);
-               r = allocreq(fs, messagesize);
-               n = read9pmsg(fs->fd, r->buf, messagesize);
-               if(n <= 0)
+               r = alloccreq(fs, messagesize);
+               while((n = read9pmsg(fs->fd, r->buf, messagesize)) == 0)
+                       ;
+               if(n < 0)
                        fatal("unmounted");
 
                if(convM2S(r->buf, n, &r->f) == 0){
@@ -704,10 +698,10 @@ fsrun(void *v)
        }
 }
 
-Fid*
+Aux*
 fsgetfid(Fs *fs, int fid)
 {
-       Fid *f, *nf;
+       Aux *f, *nf;
 
        lock(fs);
        for(f = fs->hash[fid%Nhash]; f; f = f->next){
@@ -718,7 +712,7 @@ fsgetfid(Fs *fs, int fid)
                }
        }
 
-       nf = emalloc(sizeof(Fid));
+       nf = emalloc(sizeof(Aux));
        nf->next = fs->hash[fid%Nhash];
        fs->hash[fid%Nhash] = nf;
        nf->fid = fid;
@@ -730,9 +724,9 @@ fsgetfid(Fs *fs, int fid)
 }
 
 void
-fsputfid(Fs *fs, Fid *f)
+fsputfid(Fs *fs, Aux *f)
 {
-       Fid **l, *nf;
+       Aux **l, *nf;
 
        lock(fs);
        if(--f->ref > 0){
@@ -750,13 +744,13 @@ fsputfid(Fs *fs, Fid *f)
 }
 
 void
-fsauth(Fs *fs, Request *r, Fid*)
+fsauth(Fs *fs, Request *r, Aux*)
 {
        fsreply(fs, r, "consolefs: authentication not required");
 }
 
 void
-fsversion(Fs *fs, Request *r, Fid*)
+fsversion(Fs *fs, Request *r, Aux*)
 {
 
        if(r->f.msize < 256){
@@ -777,7 +771,7 @@ fsversion(Fs *fs, Request *r, Fid*)
 }
 
 void
-fsflush(Fs *fs, Request *r, Fid *f)
+fsflush(Fs *fs, Request *r, Aux *f)
 {
        Request *or;
 
@@ -785,12 +779,15 @@ fsflush(Fs *fs, Request *r, Fid *f)
        if(or != nil){
                fsputfid(fs, or->fid);
                free(or);
+               fsreply(fs, r, nil);
+       } else {
+               fsputfid(fs, f);
+               free(r);
        }
-       fsreply(fs, r, nil);
 }
 
 void
-fsattach(Fs *fs, Request *r, Fid *f)
+fsattach(Fs *fs, Request *r, Aux *f)
 {
        f->qid.type = QTDIR;
        f->qid.path = QID(0, Ttopdir);
@@ -812,13 +809,13 @@ fsattach(Fs *fs, Request *r, Fid *f)
 }
 
 void
-fswalk(Fs *fs, Request *r, Fid *f)
+fswalk(Fs *fs, Request *r, Aux *f)
 {
        char *name;
        Dir d;
        int i, n, nqid, nwname;
        Qid qid, wqid[MAXWELEM];
-       Fid *nf;
+       Aux *nf;
        char *err;
 
        if(f->attached == 0){
@@ -931,7 +928,7 @@ int m2p[] ={
 void
 bcastmsg(Fs *fs, Console *c, char *msg, int n)
 {
-       Fid *fl;
+       Aux *fl;
 
        for(fl = c->flist; fl; fl = fl->cnext){
                fromconsole(fl, msg, n);
@@ -940,7 +937,7 @@ bcastmsg(Fs *fs, Console *c, char *msg, int n)
 }
 
 void
-fsopen(Fs *fs, Request *r, Fid *f)
+fsopen(Fs *fs, Request *r, Aux *f)
 {
        int mode;
        Console *c;
@@ -1008,13 +1005,13 @@ fsopen(Fs *fs, Request *r, Fid *f)
 }
 
 void
-fscreate(Fs *fs, Request *r, Fid*)
+fscreate(Fs *fs, Request *r, Aux*)
 {
        fsreply(fs, r, Eperm);
 }
 
 void
-fsread(Fs *fs, Request *r, Fid *f)
+fsread(Fs *fs, Request *r, Aux *f)
 {
        uchar *p, *e;
        int i, m, off;
@@ -1077,7 +1074,7 @@ fsread(Fs *fs, Request *r, Fid *f)
 }
 
 void
-fswrite(Fs *fs, Request *r, Fid *f)
+fswrite(Fs *fs, Request *r, Aux *f)
 {
        int i, eol = 0;
 
@@ -1140,9 +1137,9 @@ fswrite(Fs *fs, Request *r, Fid *f)
 }
 
 void
-fsclunk(Fs *fs, Request *r, Fid *f)
+fsclunk(Fs *fs, Request *r, Aux *f)
 {
-       Fid **l, *fl;
+       Aux **l, *fl;
        Request *nr;
 
        if(f->open && TYPE(f->qid) == Qdata){
@@ -1169,13 +1166,13 @@ fsclunk(Fs *fs, Request *r, Fid *f)
 }
 
 void
-fsremove(Fs *fs, Request *r, Fid*)
+fsremove(Fs *fs, Request *r, Aux*)
 {
        fsreply(fs, r, Eperm);
 }
 
 void
-fsstat(Fs *fs, Request *r, Fid *f)
+fsstat(Fs *fs, Request *r, Aux *f)
 {
        int i, n;
        Qid q;
@@ -1197,7 +1194,7 @@ fsstat(Fs *fs, Request *r, Fid *f)
 }
 
 void
-fswstat(Fs *fs, Request *r, Fid*)
+fswstat(Fs *fs, Request *r, Aux*)
 {
        fsreply(fs, r, Eperm);
 }
@@ -1225,7 +1222,7 @@ fsreply(Fs *fs, Request *r, char *err)
  *  called whenever input or a read request has been received
  */
 void
-fskick(Fs *fs, Fid *f)
+fskick(Fs *fs, Aux *f)
 {
        Request *r;
        char *p, *rp, *wp, *ep;