From e9e55a21f66a3eff114025dcb1fe62570d0fa3e6 Mon Sep 17 00:00:00 2001 From: cinap_lenrek Date: Sun, 8 Mar 2020 22:00:23 +0100 Subject: [PATCH] lib9p: implement automatic remove-on-close cleanup in postsharesrv(), remove postfd() and sharefd() functions with the latest changes to shr(3), we can use ORCLOSE on the control file to get the mount in the share automatically removed when the server exits or something goes wrong during postsharesrv(). do not expose postfd() and sharefd() functions. they where undocumented and leak the control file descriptors. --- sys/include/9p.h | 2 - sys/man/2/9p | 10 +-- sys/src/lib9p/post.c | 143 +++++++++++++++++++++++-------------------- sys/src/lib9p/srv.c | 56 ----------------- 4 files changed, 79 insertions(+), 132 deletions(-) diff --git a/sys/include/9p.h b/sys/include/9p.h index 53b16ca4a..1a6dfd657 100644 --- a/sys/include/9p.h +++ b/sys/include/9p.h @@ -246,8 +246,6 @@ void postsharesrv(Srv*, char*, char*, char*); void _postsharesrv(Srv*, char*, char*, char*); void listensrv(Srv*, char*); void _listensrv(Srv*, char*); -int postfd(char*, int); -int sharefd(char*, char*, int); int chatty9p; void respond(Req*, char*); void responderror(Req*); diff --git a/sys/man/2/9p b/sys/man/2/9p index 49979828c..79b0a7275 100644 --- a/sys/man/2/9p +++ b/sys/man/2/9p @@ -1,12 +1,12 @@ .TH 9P 2 .SH NAME Srv, +chatty9p, dirread9p, emalloc9p, erealloc9p, estrdup9p, listensrv, -postfd, postmountsrv, postsharesrv, readbuf, @@ -72,7 +72,6 @@ void threadpostmountsrv(Srv *s, char *name, char *mtpt, int flag) void threadpostsharesrv(Srv *s, char *name, char *mtpt, char *desc) void listensrv(Srv *s, char *addr) void threadlistensrv(Srv *s, char *addr) -int postfd(char *srvname, int fd) void respond(Req *r, char *error) void responderror(Req*) void readstr(Req *r, char *src) @@ -185,12 +184,9 @@ initialized as the other end. .IP If .B name -is non-nil, call -.BI postfd( s -> srvfd , -.IB name ) -to post +is non-nil, post the file descriptor .IB s -> srvfd -as +under the name .BI /srv/ name . .IP Fork a child process via diff --git a/sys/src/lib9p/post.c b/sys/src/lib9p/post.c index 65a98e1f6..ba4265494 100644 --- a/sys/src/lib9p/post.c +++ b/sys/src/lib9p/post.c @@ -5,73 +5,6 @@ #include <9p.h> #include -static void postproc(void*); - -void -_postmountsrv(Srv *s, char *name, char *mtpt, int flag) -{ - int fd[2]; - - if(pipe(fd) < 0) - sysfatal("pipe: %r"); - s->infd = s->outfd = fd[1]; - s->srvfd = fd[0]; - - if(name) - if(postfd(name, s->srvfd) < 0) - sysfatal("postfd %s: %r", name); - - if(_forker == nil) - sysfatal("no forker"); - _forker(postproc, s, RFNAMEG|RFNOTEG); - - rfork(RFFDG); - rendezvous(0, 0); - - close(s->infd); - if(s->infd != s->outfd) - close(s->outfd); - - if(mtpt){ - if(amount(s->srvfd, mtpt, flag, "") == -1) - sysfatal("mount %s: %r", mtpt); - }else - close(s->srvfd); -} - -void -_postsharesrv(Srv *s, char *name, char *mtpt, char *desc) -{ - int fd[2]; - - if(pipe(fd) < 0) - sysfatal("pipe: %r"); - s->infd = s->outfd = fd[1]; - s->srvfd = fd[0]; - - if(name) - if(postfd(name, s->srvfd) < 0) - sysfatal("postfd %s: %r", name); - - if(_forker == nil) - sysfatal("no forker"); - _forker(postproc, s, RFNAMEG|RFNOTEG); - - rfork(RFFDG); - rendezvous(0, 0); - - close(s->infd); - if(s->infd != s->outfd) - close(s->outfd); - - if(mtpt){ - if(sharefd(mtpt, desc, s->srvfd) < 0) - sysfatal("sharefd %s: %r", mtpt); - }else - close(s->srvfd); -} - - static void postproc(void *v) { @@ -82,3 +15,79 @@ postproc(void *v) close(s->srvfd); srv(s); } + +static void +postsrv(Srv *s, char *name) +{ + char buf[80]; + int fd[2]; + int cfd; + + if(pipe(fd) < 0) + sysfatal("pipe: %r"); + s->infd = s->outfd = fd[1]; + s->srvfd = fd[0]; + + if(name != nil){ + 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); + } else + cfd = -1; + + if(_forker == nil) + sysfatal("no forker"); + _forker(postproc, s, RFNAMEG|RFNOTEG); + + rfork(RFFDG); + rendezvous(0, 0); + + close(s->infd); + if(s->infd != s->outfd) + close(s->outfd); + + if(cfd >= 0) + close(cfd); +} + +void +_postmountsrv(Srv *s, char *name, char *mtpt, int flag) +{ + postsrv(s, name); + + if(mtpt != nil){ + if(amount(s->srvfd, mtpt, flag, "") == -1) + sysfatal("mount %s: %r", mtpt); + /* mount closed s->srvfd */ + } else + close(s->srvfd); +} + +void +_postsharesrv(Srv *s, char *name, char *mtpt, char *desc) +{ + char buf[80]; + int cfd; + + if(mtpt != nil && desc != nil){ + snprint(buf, sizeof buf, "#σc/%s", mtpt); + if((cfd = create(buf, OREAD, DMDIR|0700)) >= 0) + close(cfd); + + snprint(buf, sizeof buf, "#σc/%s/%s", mtpt, desc); + if((cfd = create(buf, OWRITE|ORCLOSE|OCEXEC, 0600)) < 0) + sysfatal("create %s: %r", buf); + } else + cfd = -1; + + postsrv(s, name); + + if(cfd >= 0){ + if(fprint(cfd, "%d\n", s->srvfd) < 0) + sysfatal("write %s: %r", buf); + close(cfd); + } + close(s->srvfd); +} diff --git a/sys/src/lib9p/srv.c b/sys/src/lib9p/srv.c index 25276d2ad..a764f9c78 100644 --- a/sys/src/lib9p/srv.c +++ b/sys/src/lib9p/srv.c @@ -918,59 +918,3 @@ responderror(Req *r) rerrstr(errbuf, sizeof errbuf); respond(r, errbuf); } - -int -postfd(char *name, int pfd) -{ - int fd; - char buf[80]; - - snprint(buf, sizeof buf, "/srv/%s", name); - if(chatty9p) - fprint(2, "postfd %s\n", buf); - fd = create(buf, OWRITE|ORCLOSE|OCEXEC, 0600); - if(fd < 0){ - if(chatty9p) - fprint(2, "create fails: %r\n"); - return -1; - } - if(fprint(fd, "%d", pfd) < 0){ - if(chatty9p) - fprint(2, "write fails: %r\n"); - close(fd); - return -1; - } - if(chatty9p) - fprint(2, "postfd successful\n"); - return 0; -} - -int -sharefd(char *name, char *desc, int pfd) -{ - int fd; - char buf[80]; - - snprint(buf, sizeof buf, "#σc/%s", name); - if((fd = create(buf, OREAD, 0700|DMDIR)) >= 0) - close(fd); - snprint(buf, sizeof buf, "#σc/%s/%s", name, desc); - if(chatty9p) - fprint(2, "sharefd %s\n", buf); - fd = create(buf, OWRITE, 0600); - if(fd < 0){ - if(chatty9p) - fprint(2, "create fails: %r\n"); - return -1; - } - if(fprint(fd, "%d\n", pfd) < 0){ - if(chatty9p) - fprint(2, "write fails: %r\n"); - close(fd); - return -1; - } - close(fd); - if(chatty9p) - fprint(2, "sharefd successful\n"); - return 0; -}