]> git.lizzy.rs Git - plan9front.git/blobdiff - sys/src/cmd/cwfs/main.c
cwfs: remove reflock
[plan9front.git] / sys / src / cmd / cwfs / main.c
old mode 100755 (executable)
new mode 100644 (file)
index d1422a1..8cf0ebc
@@ -3,11 +3,11 @@
 #include "io.h"
 #include "9p1.h"
 
-extern int oldcachefmt;
-
 Map *devmap;
 
 Biobuf bin;
+int chatty = 0;
+int sfd = -1;
 
 void
 machinit(void)
@@ -15,38 +15,6 @@ machinit(void)
        active.exiting = 0;
 }
 
-/*
- * Put a string on the console.
- */
-void
-puts(char *s, int n)
-{
-       print("%.*s", n, s);
-}
-
-void
-prflush(void)
-{
-}
-
-/*
- * Print a string on the console.
- */
-void
-putstrn(char *str, int n)
-{
-       puts(str, n);
-}
-
-/*
- * get a character from the console
- */
-int
-getc(void)
-{
-       return Bgetrune(&bin);
-}
-
 void
 panic(char *fmt, ...)
 {
@@ -58,7 +26,7 @@ panic(char *fmt, ...)
        n = vseprint(buf, buf + sizeof buf, fmt, arg) - buf;
        va_end(arg);
        buf[n] = '\0';
-       print("panic: %s\n", buf);
+       fprint(2, "panic: %s\n", buf);
        exit();
 }
 
@@ -91,12 +59,12 @@ mapinit(char *mapfile)
        if (bp == nil)
                sysfatal("can't read %s", mapfile);
        devmap = nil;
-       while ((ln = Brdline(bp, '\n')) != nil) {
+       while((ln = Brdline(bp, '\n')) != nil) {
                ln[Blinelen(bp)-1] = '\0';
-               if (*ln == '\0' || *ln == '#')
+               if(*ln == '\0' || *ln == '#')
                        continue;
                nf = tokenize(ln, fields, nelem(fields));
-               if (nf != 2)
+               if(nf != 2)
                        continue;
                if(testconfig(fields[0]) != 0) {
                        print("bad `from' device %s in %s\n",
@@ -108,7 +76,7 @@ mapinit(char *mapfile)
                map->to =   strdup(fields[1]);
                map->fdev = iconfig(fields[0]);
                map->tdev = nil;
-               if (access(map->to, AEXIST) < 0) {
+               if(access(map->to, AEXIST) < 0) {
                        /*
                         * map->to isn't an existing file, so it had better be
                         * a config string for a device.
@@ -128,8 +96,6 @@ confinit(void)
 {
        conf.nmach = 1;
 
-       conf.mem = meminit();
-
        conf.nuid = 1000;
        conf.nserve = 15;               /* tunable */
        conf.nfile = 30000;
@@ -139,7 +105,6 @@ confinit(void)
        localconfinit();
 
        conf.nwpath = conf.nfile*8;
-       conf.nauth =  conf.nfile/10;
        conf.gidspace = conf.nuid*3;
 
        cons.flags = 0;
@@ -148,8 +113,62 @@ confinit(void)
                mapinit(conf.devmap);
 }
 
+static int
+srvfd(char *s, int mode, int sfd)
+{
+       int fd;
+       char buf[32];
+
+       fd = create(s, ORCLOSE|OWRITE, mode);
+       if(fd < 0){
+               remove(s);
+               fd = create(s, ORCLOSE|OWRITE, mode);
+               if(fd < 0)
+                       panic(s);
+       }
+       sprint(buf, "%d", sfd);
+       if(write(fd, buf, strlen(buf)) != strlen(buf))
+               panic("srv write");
+       return sfd;
+}
+
+static void
+postservice(void)
+{
+       char buf[3*NAMELEN];
+       int p[2];
+
+       if(service[0] == 0)
+               panic("no service name");
+
+       /* serve 9p for -s */
+       if(sfd >= 0){
+               srvchan(sfd, "stdio");
+               sfd = -1;
+       }
+
+       /* post 9p service */
+       if(pipe(p) < 0)
+               panic("can't make a pipe");
+       snprint(buf, sizeof(buf), "#s/%s", service);
+       srvfd(buf, 0666, p[0]);
+       close(p[0]);
+       srvchan(p[1], buf);
+
+       /* post cmd service */
+       if(pipe(p) < 0)
+               panic("can't make a pipe");
+       snprint(buf, sizeof(buf), "#s/%s.cmd", service);
+       srvfd(buf, 0660, p[0]);
+       close(p[0]);
+
+       /* use it as stdin */
+       dup(p[1], 0);
+       close(p[1]);
+}
+
 /*
- * compute BUFSIZE*(NDBLOCK+INDPERBUF+INDPERBUFâ\81²+INDPERBUFâ\81³+INDPERBUFâ\81´)
+ * compute BUFSIZE*(NDBLOCK+INDPERBUF+INDPERBUF²+INDPERBUF³+INDPERBUFâ\81´)
  * while watching for overflow; in that case, return 0.
  */
 
@@ -191,8 +210,8 @@ maxsize(void)
 }
 
 enum {
-       INDPERBUF⁲ = ((uvlong)INDPERBUF *INDPERBUF),
-       INDPERBUF⁴ = ((uvlong)INDPERBUFâ\81²*INDPERBUFâ\81²),
+       INDPERBUF² = ((uvlong)INDPERBUF*INDPERBUF),
+       INDPERBUF⁴ = ((uvlong)INDPERBUF²*INDPERBUF²),
 };
 
 static void
@@ -210,9 +229,9 @@ printsizes(void)
                        max = offlim - 1;
                print("max file size = %,llud\n", (Wideoff)max);
        }
-       if (INDPERBUFâ\81²/INDPERBUF != INDPERBUF)
-               print("overflow computing INDPERBUFâ\81²\n");
-       if (INDPERBUF⁴/INDPERBUFâ\81² != INDPERBUFâ\81²)
+       if (INDPERBUF²/INDPERBUF != INDPERBUF)
+               print("overflow computing INDPERBUF²\n");
+       if (INDPERBUF⁴/INDPERBUF² != INDPERBUF²)
                print("overflow computing INDPERBUF⁴\n");
        print("\tINDPERBUF = %d, INDPERBUF^4 = %,lld, ", INDPERBUF,
                (Wideoff)INDPERBUF⁴);
@@ -224,8 +243,7 @@ printsizes(void)
 void
 usage(void)
 {
-       fprint(2, "usage: %s [-cf][-a ann-str][-m dev-map] config-dev\n",
-               argv0);
+       fprint(2, "usage: %s [ -csC ] [-n service] [ -a ann-str ] [ -m dev-map ] [-f config-dev ]\n", argv0);
        exits("usage");
 }
 
@@ -234,11 +252,12 @@ main(int argc, char **argv)
 {
        int i, nets = 0;
        char *ann;
-
+       
        rfork(RFNOTEG);
        formatinit();
        machinit();
-       conf.confdev = "n";             /* Devnone */
+       conf.confdev = "/dev/sdC0/fscache";
+       conf.newcache = 0;
 
        ARGBEGIN{
        case 'a':                       /* announce on this net */
@@ -250,33 +269,51 @@ main(int argc, char **argv)
                }
                annstrs[nets++] = ann;
                break;
-       case 'c':                       /* use new, faster cache layout */
-               oldcachefmt = 0;
+       case 'n':
+               strcpy(service, EARGF(usage()));
                break;
-       case 'f':                       /* enter configuration mode first */
+       case 's':
+               dup(0, -1);
+               sfd = dup(1, -1);
+               close(0);
+               if(open("/dev/cons", OREAD) < 0)
+                       open("#c/cons", OREAD);
+               close(1);
+               if(open("/dev/cons", OWRITE) < 0)
+                       open("#c/cons", OWRITE);
+               break;
+       case 'C':                       /* use new, faster cache layout */
+               conf.newcache = 1;
+               break;
+       case 'c':
                conf.configfirst++;
                break;
+       case 'f':                       /* device / partition / file  */
+               conf.confdev = EARGF(usage());
+               break;
        case 'm':                       /* name device-map file */
                conf.devmap = EARGF(usage());
                break;
+       case 'd':
+               chatty++;
+               break;
        default:
                usage();
                break;
        }ARGEND
 
-       if (argc != 1)
+       if(argc != 0)
                usage();
-       conf.confdev = argv[0]; /* config string for dev holding full config */
 
        Binit(&bin, 0, OREAD);
        confinit();
 
-       print("\nPlan 9 %d-bit cached-worm file server with %d-deep indir blks\n",
-               sizeof(Off)*8 - 1, NIBLOCK);
-       printsizes();
+       if(chatty){
+               print("\nPlan 9 %d-bit cached-worm file server with %d-deep indir blks\n",
+                       sizeof(Off)*8 - 1, NIBLOCK);
+               printsizes();
+       }
 
-       qlock(&reflock);
-       qunlock(&reflock);
        serveq = newqueue(1000, "9P service");  /* tunable */
        raheadq = newqueue(1000, "readahead");  /* tunable */
 
@@ -293,16 +330,24 @@ main(int argc, char **argv)
        wpaths = malloc(conf.nwpath * sizeof(*wpaths));
        uid = malloc(conf.nuid * sizeof(*uid));
        gidspace = malloc(conf.gidspace * sizeof(*gidspace));
-       authinit();
 
-       print("iobufinit\n");
        iobufinit();
 
        arginit();
        boottime = time(nil);
 
-       print("sysinit\n");
        sysinit();
+       srvinit();
+
+       /*
+        * post filedescriptors to /srv
+        */
+       postservice();
+
+       /*
+        * processes to read the console
+        */
+       consserve();
 
        /*
         * Ethernet i/o processes
@@ -325,17 +370,13 @@ main(int argc, char **argv)
         */
        newproc(wormcopy, 0, "wcp");
 
-       /*
-        * processes to read the console
-        */
-       consserve();
-
        /*
         * "sync" copy process
-        * this doesn't return.
         */
-       procsetname("scp");
-       synccopy();
+       newproc(synccopy, 0, "scp");
+
+       /* success */
+       exits(nil);
 }
 
 /*
@@ -408,7 +449,6 @@ serve(void *)
        Msgbuf *mb;
 
        for (;;) {
-               qlock(&reflock);
                /* read 9P request from a network input process */
                mb = fs_recv(serveq, 0);
                assert(mb->magic == Mbmagic);
@@ -417,7 +457,6 @@ serve(void *)
                if (cp == nil)
                        panic("serve: nil mb->chan");
                rlock(&cp->reflock);
-               qunlock(&reflock);
 
                rlock(&mainlock);
 
@@ -432,11 +471,9 @@ serve(void *)
                                        cp->protocol = fsprotocol[i];
                                        break;
                                }
-                       if(cp->protocol == nil){
-                               print("no protocol for message\n");
-                               for(i = 0; i < 12; i++)
-                                       print(" %2.2uX", mb->data[i]);
-                               print("\n");
+                       if(cp->protocol == nil && (chatty > 1)){
+                               fprint(2, "no protocol for message\n");
+                               hexdump(mb->data, 12);
                        }
                } else
                        /* process the request, generate an answer and reply */
@@ -455,7 +492,7 @@ exit(void)
        active.exiting = 1;
        unlock(&active);
 
-       print("halted at %T.\n", time(nil));
+       fprint(2, "halted at %T.\n", time(nil));
        postnote(PNGROUP, getpid(), "die");
        exits(nil);
 }
@@ -474,8 +511,8 @@ nextdump(Timet t)
 {
        Timet nddate = nextime(t+MINUTE(100), DUMPTIME, WEEKMASK);
 
-       if(!conf.nodump)
-               print("next dump at %T\n", nddate);
+       if(!conf.nodump && chatty)
+               fprint(2, "next dump at %T\n", nddate);
        return nddate;
 }
 
@@ -501,10 +538,6 @@ wormcopy(void *)
                }
                dt = time(nil) - t;
                if(dt < 0 || dt > MINUTE(100)) {
-                       if(dt < 0)
-                               print("time went back\n");
-                       else
-                               print("time jumped ahead\n");
                        dorecalc = 1;
                        continue;
                }
@@ -514,7 +547,7 @@ wormcopy(void *)
                        ntoytime = time(nil) + HOUR(1);
                else if(t > nddate) {
                        if(!conf.nodump) {
-                               print("automatic dump %T\n", t);
+                               fprint(2, "automatic dump %T\n", t);
                                for(fs=filsys; fs->name; fs++)
                                        if(fs->dev->type == Devcw)
                                                cfsdump(fs);
@@ -541,7 +574,7 @@ wormcopy(void *)
  * to get up-to-date.
  */
 void
-synccopy(void)
+synccopy(void *)
 {
        int f;
 
@@ -584,3 +617,4 @@ inqsize(char *file)
        free(data);
        return rv;
 }
+