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.
This commit is contained in:
cinap_lenrek 2020-03-08 22:00:23 +01:00
parent 11628e90fb
commit e9e55a21f6
4 changed files with 79 additions and 132 deletions

View file

@ -246,8 +246,6 @@ void postsharesrv(Srv*, char*, char*, char*);
void _postsharesrv(Srv*, char*, char*, char*); void _postsharesrv(Srv*, char*, char*, char*);
void listensrv(Srv*, char*); void listensrv(Srv*, char*);
void _listensrv(Srv*, char*); void _listensrv(Srv*, char*);
int postfd(char*, int);
int sharefd(char*, char*, int);
int chatty9p; int chatty9p;
void respond(Req*, char*); void respond(Req*, char*);
void responderror(Req*); void responderror(Req*);

View file

@ -1,12 +1,12 @@
.TH 9P 2 .TH 9P 2
.SH NAME .SH NAME
Srv, Srv,
chatty9p,
dirread9p, dirread9p,
emalloc9p, emalloc9p,
erealloc9p, erealloc9p,
estrdup9p, estrdup9p,
listensrv, listensrv,
postfd,
postmountsrv, postmountsrv,
postsharesrv, postsharesrv,
readbuf, 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 threadpostsharesrv(Srv *s, char *name, char *mtpt, char *desc)
void listensrv(Srv *s, char *addr) void listensrv(Srv *s, char *addr)
void threadlistensrv(Srv *s, char *addr) void threadlistensrv(Srv *s, char *addr)
int postfd(char *srvname, int fd)
void respond(Req *r, char *error) void respond(Req *r, char *error)
void responderror(Req*) void responderror(Req*)
void readstr(Req *r, char *src) void readstr(Req *r, char *src)
@ -185,12 +184,9 @@ initialized as the other end.
.IP .IP
If If
.B name .B name
is non-nil, call is non-nil, post the file descriptor
.BI postfd( s -> srvfd ,
.IB name )
to post
.IB s -> srvfd .IB s -> srvfd
as under the name
.BI /srv/ name . .BI /srv/ name .
.IP .IP
Fork a child process via Fork a child process via

View file

@ -5,73 +5,6 @@
#include <9p.h> #include <9p.h>
#include <auth.h> #include <auth.h>
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 static void
postproc(void *v) postproc(void *v)
{ {
@ -82,3 +15,79 @@ postproc(void *v)
close(s->srvfd); close(s->srvfd);
srv(s); 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);
}

View file

@ -918,59 +918,3 @@ responderror(Req *r)
rerrstr(errbuf, sizeof errbuf); rerrstr(errbuf, sizeof errbuf);
respond(r, 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;
}