10 * This fs presents a 1 level file system. It contains
11 * up to three files per console (xxx and xxxctl and xxxstat)
14 typedef struct Console Console;
15 typedef struct Fid Fid;
16 typedef struct Request Request;
17 typedef struct Reqlist Reqlist;
22 /* last 5 bits of qid.path */
23 Textern= 0, /* fake parent of top level */
24 Ttopdir, /* top level directory */
29 Bufsize= 32*1024, /* chars buffered per reader */
30 Maxcons= 64, /* maximum consoles */
31 Nhash= 64, /* Fid hash buckets */
34 #define TYPE(x) (((ulong)x.path) & 0xf)
35 #define CONS(x) ((((ulong)x.path) >> 4)&0xfff)
36 #define QID(c, x) (((c)<<4) | (x))
57 Fid *next; /* hash list */
58 Fid *cnext; /* list of Fid's on a console */
65 char mbuf[Bufsize]; /* message */
76 Reqlist r; /* active read requests */
87 int ondemand; /* open only on demand */
88 int chat; /* chat consoles are special */
90 int pid; /* pid of reader */
96 Fid *flist; /* open fids to broadcast to */
103 int fd; /* to kernel mount point */
106 Console *cons[Maxcons];
110 extern void console(Fs*, char*, char*, int, int, int);
111 extern Fs* fsmount(char*);
113 extern void fsreader(void*);
114 extern void fsrun(void*);
115 extern Fid* fsgetfid(Fs*, int);
116 extern void fsputfid(Fs*, Fid*);
117 extern int fsdirgen(Fs*, Qid, int, Dir*, uchar*, int);
118 extern void fsreply(Fs*, Request*, char*);
119 extern void fskick(Fs*, Fid*);
120 extern int fsreopen(Fs*, Console*);
122 extern void fsversion(Fs*, Request*, Fid*);
123 extern void fsflush(Fs*, Request*, Fid*);
124 extern void fsauth(Fs*, Request*, Fid*);
125 extern void fsattach(Fs*, Request*, Fid*);
126 extern void fswalk(Fs*, Request*, Fid*);
127 extern void fsclwalk(Fs*, Request*, Fid*);
128 extern void fsopen(Fs*, Request*, Fid*);
129 extern void fscreate(Fs*, Request*, Fid*);
130 extern void fsread(Fs*, Request*, Fid*);
131 extern void fswrite(Fs*, Request*, Fid*);
132 extern void fsclunk(Fs*, Request*, Fid*);
133 extern void fsremove(Fs*, Request*, Fid*);
134 extern void fsstat(Fs*, Request*, Fid*);
135 extern void fswstat(Fs*, Request*, Fid*);
138 void (*fcall[])(Fs*, Request*, Fid*) =
141 [Tversion] fsversion,
155 char Eperm[] = "permission denied";
156 char Eexist[] = "file does not exist";
157 char Enotdir[] = "not a directory";
158 char Eisopen[] = "file already open";
159 char Ebadcount[] = "bad read/write count";
160 char Enofid[] = "no such fid";
162 char *consoledb = "/lib/ndb/consoledb";
163 char *mntpt = "/mnt/consoles";
165 int messagesize = 8192+IOHDRSZ;
168 fatal(char *fmt, ...)
173 write(2, "consolefs: ", 10);
175 vseprint(buf, buf+1024, fmt, arg);
177 write(2, buf, strlen(buf));
190 fatal("malloc failed: %r");
199 * any request that can get queued for a delayed reply
202 allocreq(Fs *fs, int bufsize)
206 r = emalloc(sizeof(Request)+bufsize);
213 * for maintaining lists of requests
216 addreq(Reqlist *l, Request *r)
229 * remove the first request from a list of requests
245 * remove a request with the given tag from a list of requests
248 remtag(Reqlist *l, int tag)
254 for(or = *ll; or; or = or->next){
255 if(or->f.tag == tag){
270 return (Qid){QID(0, Textern), 0, QTDIR};
272 return (Qid){QID(0, Ttopdir), 0, QTDIR};
276 fsdirgen(Fs *fs, Qid parent, int i, Dir *d, uchar *buf, int nbuf)
278 static char name[64];
282 d->uid = d->gid = d->muid = "network";
284 d->atime = time(nil);
289 switch(TYPE(parent)){
294 d->mode = DMDIR|0555;
296 d->qid.path = QID(0, Ttopdir);
301 if(xcons >= fs->ncons)
303 p = fs->cons[xcons]->name;
306 if(fs->cons[xcons]->cfd < 0)
308 snprint(name, sizeof name, "%sctl", p);
310 d->qid.type = QTFILE;
311 d->qid.path = QID(xcons, Qctl);
315 if(fs->cons[xcons]->sfd < 0)
317 snprint(name, sizeof name, "%sstat", p);
319 d->qid.type = QTFILE;
320 d->qid.path = QID(xcons, Qstat);
324 d->qid.type = QTFILE;
325 d->qid.path = QID(xcons, Qdata);
336 return convD2M(d, buf, nbuf);
341 * mount the user interface and start a request processor
352 fs = emalloc(sizeof(Fs));
355 fatal("opening pipe: %r");
357 /* start up the file system process */
360 proccreate(fsrun, v, 16*1024);
362 /* Typically mounted before /srv exists */
363 if(access("#s/consoles", AEXIST) < 0){
364 srv = create("#s/consoles", OWRITE, 0666);
368 n = sprint(buf, "%d", pfd[1]);
369 if(write(srv, buf, n) < 0)
370 fatal("write srv: %r");
375 mount(pfd[1], -1, mntpt, MBEFORE, "");
384 fsreopen(Fs* fs, Console *c)
390 if(postnote(PNPROC, c->pid, "reopen") != 0)
391 fprint(2, "postnote failed: %r\n");
404 if(c->flist == nil && c->ondemand)
407 c->fd = open(c->dev, ORDWR);
411 snprint(buf, sizeof(buf), "%sctl", c->dev);
412 c->cfd = open(buf, ORDWR);
413 fprint(c->cfd, "b%d", c->speed);
415 snprint(buf, sizeof(buf), "%sstat", c->dev);
416 c->sfd = open(buf, OREAD);
420 proccreate(fsreader, v, 16*1024);
426 change(Fs *fs, Console *c, int doreopen, int speed, int cronly, int ondemand)
430 if(speed != c->speed){
434 if(ondemand != c->ondemand){
435 c->ondemand = ondemand;
446 * create a console interface
449 console(Fs* fs, char *name, char *dev, int speed, int cronly, int ondemand)
455 if(fs->ncons >= Maxcons)
456 fatal("too many consoles, too little time");
459 for(i = 0; i < fs->ncons; i++){
461 if(strcmp(name, c->name) == 0){
462 if(strcmp(dev, c->dev) != 0){
465 c->dev = strdup(dev);
469 change(fs, c, doreopen, speed, cronly, ondemand);
473 #ifdef sapedoesntlikethis
475 * The code below prevents this from working. I can't
476 * think of scenarios where the code below actually helps
479 * console=borneo dev=/dev/eia1
482 * console=tottie dev=/dev/eia1
486 for(i = 0; i < fs->ncons; i++){
488 if(strcmp(dev, c->dev) == 0){
489 /* at least a rename */
491 c->name = strdup(name);
493 change(fs, c, doreopen, speed, cronly, ondemand);
498 c = emalloc(sizeof(Console));
499 fs->cons[fs->ncons] = c;
501 c->name = strdup(name);
502 c->dev = strdup(dev);
503 if(strcmp(c->dev, "/dev/null") == 0)
510 change(fs, c, 1, speed, cronly, ondemand);
514 * buffer data from console to a client.
515 * circular q with writer able to catch up to reader.
516 * the reader may miss data but always sees an in order sequence.
519 fromconsole(Fid *f, char *p, int n)
527 ep = f->buf + sizeof(f->buf);
538 /* we overtook the read pointer, push it up so readers always
539 * see the tail of what was written
552 * broadcast a list of members to all listeners
555 bcastmembers(Fs *fs, Console *c, char *msg, Fid *f)
561 sprint(buf, "[%s%s", msg, f->user);
562 for(fl = c->flist; fl != nil && strlen(buf) + 64 < sizeof(buf); fl = fl->cnext){
566 strcat(buf, fl->user);
571 for(fl = c->flist; fl; fl = fl->cnext){
572 fromconsole(fl, buf, n);
578 handler(void*, char *msg)
580 if(strstr(msg, "reopen") != nil ||
581 strstr(msg, "write on closed pipe") != nil)
587 * a process to read console output and broadcast it (one per console)
607 n = read(c->fd, buf, sizeof(buf));
611 for(fl = c->flist; fl; fl = fl->cnext){
612 fromconsole(fl, buf, n);
624 int cronly, speed, ondemand;
628 /* start a listener for each console */
638 for(nt = t; nt; nt = nt->entry){
639 if(strcmp(nt->attr, "console") == 0)
641 else if(strcmp(nt->attr, "dev") == 0)
643 else if(strcmp(nt->attr, "speed") == 0)
644 speed = atoi(nt->val);
645 else if(strcmp(nt->attr, "cronly") == 0)
647 else if(strcmp(nt->attr, "openondemand") == 0)
650 if(dev != nil && cons != nil)
651 console(fs, cons, dev, speed, cronly, ondemand);
659 * a request processor (one per Fs)
677 d = dirstat(consoledb);
678 if(d != nil && d->mtime != dbmtime){
683 r = allocreq(fs, messagesize);
684 n = read9pmsg(fs->fd, r->buf, messagesize);
686 threadexitsall("unmounted");
688 fatal("mount read: %r");
689 if(convM2S(r->buf, n, &r->f) != n)
690 fatal("convM2S format error: %r");
692 f = fsgetfid(fs, r->f.fid);
695 fprint(2, "%F path %llux\n", &r->f, f->qid.path);
699 (*fcall[t])(fs, r, f);
704 fsgetfid(Fs *fs, int fid)
709 for(f = fs->hash[fid%Nhash]; f; f = f->next){
717 nf = emalloc(sizeof(Fid));
718 nf->next = fs->hash[fid%Nhash];
719 fs->hash[fid%Nhash] = nf;
729 fsputfid(Fs *fs, Fid *f)
738 for(l = &fs->hash[f->fid%Nhash]; nf = *l; l = &nf->next)
749 fsauth(Fs *fs, Request *r, Fid*)
751 fsreply(fs, r, "consolefs: authentication not required");
755 fsversion(Fs *fs, Request *r, Fid*)
758 if(r->f.msize < 256){
759 fsreply(fs, r, "message size too small");
762 messagesize = r->f.msize;
763 if(messagesize > 8192+IOHDRSZ)
764 messagesize = 8192+IOHDRSZ;
765 r->f.msize = messagesize;
766 if(strncmp(r->f.version, "9P2000", 6) != 0){
767 fsreply(fs, r, "unrecognized 9P version");
770 r->f.version = "9P2000";
776 fsflush(Fs *fs, Request *r, Fid *f)
780 or = remtag(&f->r, r->f.oldtag);
782 fsputfid(fs, or->fid);
789 fsattach(Fs *fs, Request *r, Fid *f)
792 f->qid.path = QID(0, Ttopdir);
796 f->user = strdup(r->f.uname);
798 f->user = strdup("none");
800 /* hold down the fid till the clunk */
811 fswalk(Fs *fs, Request *r, Fid *f)
815 int i, n, nqid, nwname;
816 Qid qid, wqid[MAXWELEM];
820 if(f->attached == 0){
821 fsreply(fs, r, Enofid);
826 if(r->f.fid != r->f.newfid){
827 nf = fsgetfid(fs, r->f.newfid);
828 nf->attached = f->attached;
831 nf->user = strdup(f->user);
840 nwname = r->f.nwname;
843 for(; err == nil && nqid < nwname; nqid++){
844 if(nqid >= MAXWELEM){
845 err = "too many name elements";
848 name = r->f.wname[nqid];
849 if(strcmp(name, "..") == 0)
850 qid = parentqid(qid);
851 else if(strcmp(name, ".") != 0){
853 n = fsdirgen(fs, qid, i, &d, nil, 0);
858 if(n > 0 && strcmp(name, d.name) == 0){
866 if(nf != nil && nqid < nwname)
872 memmove(r->f.wqid, wqid, nqid*sizeof(Qid));
878 ingroup(char *user, char *group)
883 t = ndbsearch(db, &s, "group", group);
886 for(nt = t; nt; nt = nt->entry){
887 if(strcmp(nt->attr, "uid") == 0)
888 if(strcmp(nt->val, user) == 0)
896 userok(char *u, char *cname)
901 t = ndbsearch(db, &s, "console", cname);
905 for(nt = t; nt; nt = nt->entry){
906 if(strcmp(nt->attr, "uid") == 0)
907 if(strcmp(nt->val, u) == 0)
909 if(strcmp(nt->attr, "gid") == 0)
910 if(ingroup(u, nt->val))
925 * broadcast a message to all listeners
928 bcastmsg(Fs *fs, Console *c, char *msg, int n)
932 for(fl = c->flist; fl; fl = fl->cnext){
933 fromconsole(fl, msg, n);
939 fsopen(Fs *fs, Request *r, Fid *f)
944 if(f->attached == 0){
945 fsreply(fs, r, Enofid);
950 fsreply(fs, r, Eisopen);
954 mode = r->f.mode & 3;
956 if((QTDIR & f->qid.type) && mode != OREAD){
957 fsreply(fs, r, Eperm);
961 switch(TYPE(f->qid)){
963 c = fs->cons[CONS(f->qid)];
964 if(!userok(f->user, c->name)){
965 fsreply(fs, r, Eperm);
972 sprint(f->mbuf, "[%s] ", f->user);
973 f->bufn = strlen(f->mbuf);
977 bcastmembers(fs, c, "+", f);
983 c = fs->cons[CONS(f->qid)];
984 if(!userok(f->user, c->name)){
985 fsreply(fs, r, Eperm);
991 c = fs->cons[CONS(f->qid)];
992 if(!userok(f->user, c->name)){
993 fsreply(fs, r, Eperm);
1001 r->f.iounit = messagesize-IOHDRSZ;
1003 fsreply(fs, r, nil);
1007 fscreate(Fs *fs, Request *r, Fid*)
1009 fsreply(fs, r, Eperm);
1013 fsread(Fs *fs, Request *r, Fid *f)
1021 if(f->attached == 0){
1022 fsreply(fs, r, Enofid);
1026 if((int)r->f.count < 0){
1027 fsreply(fs, r, Ebadcount);
1031 if(QTDIR & f->qid.type){
1032 p = r->buf + IOHDRSZ;
1034 offset = r->f.offset;
1036 for(i=0; p<e; i++, off+=m){
1037 m = fsdirgen(fs, f->qid, i, &d, p, e-p);
1040 if(m > BIT16SZ && off >= offset)
1043 r->f.data = (char*)r->buf + IOHDRSZ;
1044 r->f.count = (char*)p - r->f.data;
1046 switch(TYPE(f->qid)){
1052 r->f.data = (char*)r->buf+IOHDRSZ;
1056 if(r->f.count > sizeof(sbuf))
1057 r->f.count = sizeof(sbuf);
1058 i = pread(f->c->sfd, sbuf, r->f.count, r->f.offset);
1060 errstr(sbuf, sizeof sbuf);
1061 fsreply(fs, r, sbuf);
1068 fsreply(fs, r, Eexist);
1072 fsreply(fs, r, nil);
1076 fswrite(Fs *fs, Request *r, Fid *f)
1080 if(f->attached == 0){
1081 fsreply(fs, r, Enofid);
1085 if((int)r->f.count < 0){
1086 fsreply(fs, r, Ebadcount);
1090 if(QTDIR & f->qid.type){
1091 fsreply(fs, r, Eperm);
1095 switch(TYPE(f->qid)){
1097 fsreply(fs, r, Eperm);
1100 write(f->c->cfd, r->f.data, r->f.count);
1103 for(i = 0; i < r->f.count; i++){
1104 if(r->f.data[i] == '\n'){
1105 if(f->c->chat && f->used)
1108 r->f.data[i] = '\r';
1118 if(f->bufn + r->f.count > Bufsize){
1119 r->f.count -= (f->bufn + r->f.count) % Bufsize;
1122 strncat(f->mbuf, r->f.data, r->f.count);
1123 f->bufn += r->f.count;
1125 bcastmsg(fs, f->c, f->mbuf, f->bufn);
1126 sprint(f->mbuf, "[%s] ", f->user);
1127 f->bufn = strlen(f->mbuf);
1132 write(f->c->fd, r->f.data, r->f.count);
1135 fsreply(fs, r, nil);
1139 fsclunk(Fs *fs, Request *r, Fid *f)
1144 if(f->open && TYPE(f->qid) == Qdata){
1145 while((nr = remreq(&f->r)) != nil){
1151 for(l = &f->c->flist; *l; l = &fl->cnext){
1158 bcastmembers(fs, f->c, "-", f);
1159 if(f->c->ondemand && f->c->flist == nil)
1163 fsreply(fs, r, nil);
1168 fsremove(Fs *fs, Request *r, Fid*)
1170 fsreply(fs, r, Eperm);
1174 fsstat(Fs *fs, Request *r, Fid *f)
1180 q = parentqid(f->qid);
1182 r->f.stat = r->buf+IOHDRSZ;
1183 n = fsdirgen(fs, q, i, &d, r->f.stat, messagesize-IOHDRSZ);
1185 fsreply(fs, r, Eexist);
1189 if(r->f.nstat > BIT16SZ && d.qid.path == f->qid.path)
1192 fsreply(fs, r, nil);
1196 fswstat(Fs *fs, Request *r, Fid*)
1198 fsreply(fs, r, Eperm);
1202 fsreply(Fs *fs, Request *r, char *err)
1205 uchar buf[8192+IOHDRSZ];
1211 n = convS2M(&r->f, buf, messagesize);
1213 fprint(2, "%F path %llux n=%d\n", &r->f, r->fid->qid.path, n);
1214 fsputfid(fs, r->fid);
1215 if(write(fs->fd, buf, n) != n)
1221 * called whenever input or a read request has been received
1224 fskick(Fs *fs, Fid *f)
1227 char *p, *rp, *wp, *ep;
1231 while(f->rp != f->wp){
1238 ep = &f->buf[Bufsize];
1239 for(i = 0; i < r->f.count && rp != wp; i++){
1245 r->f.data = (char*)r->buf;
1246 r->f.count = p - (char*)r->buf;
1247 fsreply(fs, r, nil);
1255 fprint(2, "usage: consolefs [-d] [-m mount-point] [-c console-db]\n");
1256 threadexitsall("usage");
1260 threadmain(int argc, char **argv)
1262 fmtinstall('F', fcallfmt);
1270 if(consoledb == nil)
1280 db = ndbopen(consoledb);
1282 fatal("can't open %s: %r", consoledb);