]> git.lizzy.rs Git - plan9front.git/commitdiff
lib9p: remove Srv.srvfd, make postsrv() and threadpostsrv() return the mountable...
authorcinap_lenrek <cinap_lenrek@felloff.net>
Sat, 1 May 2021 17:58:58 +0000 (19:58 +0200)
committercinap_lenrek <cinap_lenrek@felloff.net>
Sat, 1 May 2021 17:58:58 +0000 (19:58 +0200)
Now that we have these new functions,
we can also make them return an error
instead of calling sysfatal() like
postmountsrv().

Remove the confusing Srv.srvfd, as it
is only temporarily used and return
it from postsrv() instead.

sys/include/9p.h
sys/man/2/9p
sys/src/cmd/aux/wacom.c
sys/src/lib9p/listen.c
sys/src/lib9p/mount.c
sys/src/lib9p/post.c
sys/src/lib9p/share.c
sys/src/lib9p/threadpostsrv.c

index 1a51ffeab01b6f7a85ca81cf7867c48a3fcda521..bfbe0d7ec73320b1b6c1aa583c16169bd9911f53 100644 (file)
@@ -214,7 +214,6 @@ struct Srv {
 
        int             infd;
        int             outfd;
-       int             srvfd;
        char*   keyspec;
 
 /* below is implementation-specific; don't use */
@@ -243,13 +242,13 @@ void              srvforker(void (*)(void*), void*, int);
 void           threadsrvforker(void (*)(void*), void*, int);
 
 void           srv(Srv*);
-void           postsrv(Srv*, char*);
+int            postsrv(Srv*, char*);
 void           postmountsrv(Srv*, char*, char*, int);
 void           postsharesrv(Srv*, char*, char*, char*);
 void           listensrv(Srv*, char*);
 
 void           threadsrv(Srv*);
-void           threadpostsrv(Srv*, char*);
+int            threadpostsrv(Srv*, char*);
 void           threadpostmountsrv(Srv*, char*, char*, int);
 void           threadpostsharesrv(Srv*, char*, char*, char*);
 void           threadlistensrv(Srv *s, char *addr);
index d68fbaa2c3394b4fe69c48fafce7d266ad4e2cb9..4fb831d09620eb8cf41f53e80691a14940e27249 100644 (file)
@@ -63,7 +63,6 @@ typedef struct Srv {
 
        int             infd;
        int             outfd;
-       int             srvfd;
 
        void            (*forker)(void (*fn)(void*), void *arg, int flags);
 } Srv;
@@ -73,12 +72,12 @@ typedef struct Srv {
 .ft L
 .ta \w'\fLvoid* 'u
 void   srv(Srv *s)
-void   postsrv(Srv *s, char *name);
+int    postsrv(Srv *s, char *name);
 void   postmountsrv(Srv *s, char *name, char *mtpt, int flag)
 void   postsharesrv(Srv *s, char *name, char *mtpt, char *desc)
 void   listensrv(Srv *s, char *addr)
 void   threadsrv(Srv *s)
-void   threadpostsrv(Srv *s, char *name);
+int    threadpostsrv(Srv *s, char *name);
 void   threadpostmountsrv(Srv *s, char *name, char *mtpt, int flag)
 void   threadpostsharesrv(Srv *s, char *name, char *mtpt, char *desc)
 void   threadlistensrv(Srv *s, char *addr)
@@ -193,38 +192,32 @@ or
 (see
 .IR thread (2)).
 .PP
-.I Postmountsrv
+
+.I Postsrv
 and
-.I threadpostmountsrv
+.I threadpostsrv
 are wrappers that create a separate process in which to run
 .IR srv .
 They do the following:
 .IP
 Initialize
-.IB s -> forker
-to eigther
-.I srvforker
-or
-.I threadsrvforker
-unless already initialized to a non-nil value.
-.IP
-Initialize
 .IB s -> infd
 and
 .IB s -> outfd
-to be one end of a freshly allocated pipe,
-with
-.IB s -> srvfd
-initialized as the other end.
+to be one end of a freshly allocated pipe.
 .IP
 If
 .B name
-is non-nil, post the file descriptor
-.IB s -> srvfd
-under the name
-.BI /srv/ name
-using a call to
-.IR postsrv .
+is non-nil, post the other end file descriptor under the name
+.BI /srv/ name .
+.IP
+Initialize
+.IB s -> forker
+to eigther
+.I srvforker
+or
+.I threadsrvforker
+unless already initialized to a non-nil value.
 .IP
 Fork a child process via
 .IB s -> forker
@@ -248,27 +241,37 @@ flag. This way, the service loop will share the original
 file descriptor table with previously created child
 processes of the caller.
 .IP
-The child process then calls
-.IB close( s -> srvfd )
-and then
-.IB srv( s ) \fR;
-it will exit once
-.I srv
-returns.
+The child process then closes the other end file descriptor
+and calls
+.IR srv .
 .IP
-If
+The parent process returns from the function with the
+mountable file descriptor
+.IR sfd .
+On error,
+.I postsrv
+and
+.I threadpostsrv
+return a file descriptor of
+.BR -1
+with error string set.
+.PP
+.I Postmountsrv
+and
+.I threadpostmntsrv
+call
+.I postsrv
+with
+.I name
+and then if
 .I mtpt
 is non-nil,
 call
-.BI amount( s -> srvfd,
+.BI amount( sfd ,
 .IB mtpt ,
 .IB flag ,
 \fB"")\fR;
-otherwise, close
-.IB s -> srvfd \fR.
-.IP
-The parent returns to the caller.
-.LP
+otherwise, close the file descriptor.
 If any error occurs during
 this process, the entire process is terminated by calling
 .I sysfatal
index 5b076873c73124719ce8d205ac63cbd6c984f04b..876d4ab15ff43db21587b53ab4abff1457e3ea64 100644 (file)
@@ -329,7 +329,6 @@ main()
        
        pipe(fd);
        tabletsrv.infd = tabletsrv.outfd = fd[0];
-       tabletsrv.srvfd = fd[1];
        tabletsrv.tree = alloctree(getuser(), getuser(), 0555, 0);
        tfile = createfile(tabletsrv.tree->root, "tablet", getuser(), 0400, 0);
        if(rfork(RFPROC | RFMEM | RFNOWAIT | RFNOTEG) > 0) exits(nil);
@@ -349,4 +348,4 @@ main()
                sendout(m);
                msgdecref(m);
        }
-}
\ No newline at end of file
+}
index c7e8dd02c859e0100b7059d661f4e1055d2aa4f8..b093bc287ad765e9492c2b2dd4a3cdd685c93f54 100644 (file)
@@ -18,7 +18,7 @@ listensrv(Srv *os, char *addr)
        *s = *os;
 
        s->addr = estrdup9p(addr);
-       s->infd = s->outfd = s->srvfd = -1;
+       s->infd = s->outfd = -1;
        s->fpool = nil;
        s->rpool = nil;
        s->msize = 0;
index 1adc90bedbafdc48a1d8528bf989ca14f2deb969..1ab41825ce15b0538ac878af419b84aa24c1cc0d 100644 (file)
@@ -8,12 +8,15 @@
 void
 postmountsrv(Srv *s, char *name, char *mtpt, int flag)
 {
-       postsrv(s, name);
+       int sfd;
 
+       sfd = postsrv(s, name);
+       if(sfd < 0)
+               sysfatal("postsrv: %r");
        if(mtpt != nil){
-               if(amount(s->srvfd, mtpt, flag, "") == -1)
+               if(amount(sfd, mtpt, flag, "") == -1)
                        sysfatal("mount %s: %r", mtpt);
-               /* mount closed s->srvfd */
+               /* mount closed sfd */
        } else
-               close(s->srvfd);
+               close(sfd);
 }
index 18ded63ba8f4b597912d8a1c97c32f7f59598ace..b2134968c020ca7013549ca49ddcf9fb36fef596 100644 (file)
@@ -8,43 +8,41 @@ static void
 postproc(void *v)
 {
        Srv *s = v;
-       rendezvous(0, 0);
-       close(s->srvfd);
+       close((int)(uintptr)rendezvous(s, 0));
        srv(s);
 }
 
-void
+int
 postsrv(Srv *s, char *name)
 {
-       char buf[80];
-       int fd[2];
-       int cfd;
+       int fd[2], cfd;
 
        if(pipe(fd) < 0)
-               sysfatal("pipe: %r");
-       s->infd = s->outfd = fd[1];
-       s->srvfd = fd[0];
-
+               return -1;
        if(name != nil){
+               char buf[80];
+
                snprint(buf, sizeof buf, "/srv/%s", name);
-               if((cfd = create(buf, OWRITE|ORCLOSE|OCEXEC, 0600)) < 0)
-                       sysfatal("create %s: %r", buf);
-               if(fprint(cfd, "%d", s->srvfd) < 0)
-                       sysfatal("write %s: %r", buf);
+               if((cfd = create(buf, OWRITE|ORCLOSE|OCEXEC, 0600)) < 0
+               || fprint(cfd, "%d", fd[0]) < 0){
+                       close(fd[0]);
+                       fd[0] = -1;
+                       goto Out;
+               }
        } else
                cfd = -1;
 
+       /* now we are commited */
+       s->infd = s->outfd = fd[1];
        if(s->forker == nil)
                s->forker = srvforker;
        (*s->forker)(postproc, s, RFNAMEG|RFNOTEG);
 
        rfork(RFFDG);
-       rendezvous(0, 0);
-
-       close(s->infd);
-       if(s->infd != s->outfd)
-               close(s->outfd);
-
+       rendezvous(s, (void*)(uintptr)fd[0]);
+Out:
        if(cfd >= 0)
                close(cfd);
+       close(fd[1]);
+       return fd[0];
 }
index ff73e9c3e39409be37982e463c50eb7fc21190e0..afa01c2f225aa1f647574cf5b67ae48fe326520e 100644 (file)
@@ -8,7 +8,7 @@ void
 postsharesrv(Srv *s, char *name, char *mtpt, char *desc)
 {
        char buf[80];
-       int cfd;
+       int cfd, sfd;
 
        if(mtpt != nil && desc != nil){
                snprint(buf, sizeof buf, "#σc/%s", mtpt);
@@ -21,12 +21,13 @@ postsharesrv(Srv *s, char *name, char *mtpt, char *desc)
        } else
                cfd = -1;
 
-       postsrv(s, name);
-
+       sfd = postsrv(s, name);
+       if(sfd < 0)
+               sysfatal("postsrv: %r");
        if(cfd >= 0){
-               if(fprint(cfd, "%d\n", s->srvfd) < 0)
+               if(fprint(cfd, "%d\n", sfd) < 0)
                        sysfatal("write %s: %r", buf);
                close(cfd);
        }
-       close(s->srvfd);
+       close(sfd);
 }
index abdb882e84475df503428acec1cfb6f7e3f00ca8..b627a8653046865abe1ba908abe6842771ca2bcb 100644 (file)
@@ -4,10 +4,10 @@
 #include <thread.h>
 #include <9p.h>
 
-void
+int
 threadpostsrv(Srv *s, char *name)
 {
        if(s->forker == nil)
                s->forker = threadsrvforker;
-       postsrv(s, name);
+       return postsrv(s, name);
 }