shithub: riscv

Download patch

ref: e9e55a21f66a3eff114025dcb1fe62570d0fa3e6
parent: 11628e90fb85137a3f02e4ad5c343c15a90846b5
author: cinap_lenrek <[email protected]>
date: Sun Mar 8 18:00:23 EDT 2020

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.

--- a/sys/include/9p.h
+++ b/sys/include/9p.h
@@ -246,8 +246,6 @@
 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*);
--- 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	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 @@
 .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
--- a/sys/src/lib9p/post.c
+++ b/sys/src/lib9p/post.c
@@ -5,12 +5,23 @@
 #include <9p.h>
 #include <auth.h>
 
-static void postproc(void*);
+static void
+postproc(void *v)
+{
+	Srv *s;
 
-void
-_postmountsrv(Srv *s, char *name, char *mtpt, int flag)
+	s = v;
+	rendezvous(0, 0);
+	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");
@@ -17,9 +28,14 @@
 	s->infd = s->outfd = fd[1];
 	s->srvfd = fd[0];
 
-	if(name)
-		if(postfd(name, s->srvfd) < 0)
-			sysfatal("postfd %s: %r", name);
+	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");
@@ -32,10 +48,20 @@
 	if(s->infd != s->outfd)
 		close(s->outfd);
 
-	if(mtpt){
+	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);
-	}else
+		/* mount closed s->srvfd */
+	} else
 		close(s->srvfd);
 }
 
@@ -42,43 +68,26 @@
 void
 _postsharesrv(Srv *s, char *name, char *mtpt, char *desc)
 {
-	int fd[2];
+	char buf[80];
+	int cfd;
 
-	if(pipe(fd) < 0)
-		sysfatal("pipe: %r");
-	s->infd = s->outfd = fd[1];
-	s->srvfd = fd[0];
+	if(mtpt != nil && desc != nil){
+		snprint(buf, sizeof buf, "#σc/%s", mtpt);
+		if((cfd = create(buf, OREAD, DMDIR|0700)) >= 0)
+			close(cfd);
 
-	if(name)
-		if(postfd(name, s->srvfd) < 0)
-			sysfatal("postfd %s: %r", name);
+		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;
 
-	if(_forker == nil)
-		sysfatal("no forker");
-	_forker(postproc, s, RFNAMEG|RFNOTEG);
+	postsrv(s, name);
 
-	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)
-{
-	Srv *s;
-
-	s = v;
-	rendezvous(0, 0);
+	if(cfd >= 0){
+		if(fprint(cfd, "%d\n", s->srvfd) < 0)
+			sysfatal("write %s: %r", buf);
+		close(cfd);
+	}
 	close(s->srvfd);
-	srv(s);
 }
--- a/sys/src/lib9p/srv.c
+++ b/sys/src/lib9p/srv.c
@@ -918,59 +918,3 @@
 	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;
-}