ref: a55c2b2b816998618398f85f1ca7839126afbed0
parent: d99a4ed1b8d06a5163b1cceb8d4cfe285faa70b2
author: cinap_lenrek <[email protected]>
date: Tue Feb 17 01:54:19 EST 2015
tlssrv: fix this mess (thanks burnzez for reporting the issue) tlsServer() closes the passed in fd, in our case fd=1 leaving it with no std output which got occupied by pipe() filedescriptor which it then closed after duping... a classic. delete all this mess. theres no reason to fork() and copy traffic on a pipe at all as tlsServer() gives us a perfectly valid filedescriptor. just dup() and exec() and we'r done.
--- a/sys/src/cmd/tlssrv.c
+++ b/sys/src/cmd/tlssrv.c
@@ -4,86 +4,11 @@
#include <mp.h>
#include <libsec.h>
-enum{ BufSize = 8192 };
+char *remotesys = "";
+char *logfile = nil;
+int debug = 0;
-char *remotesys, *logfile;
-int debug, p[2];
-
-void
-death(void *, char *)
-{
- int pid;
-
- close(0);
- close(1);
- close(p[1]);
- pid = getpid();
- postnote(PNGROUP, pid, "die");
- postnote(PNGROUP, pid, "die");
- postnote(PNGROUP, pid, "die");
- _exits(0);
-}
-
-static void
-dump(int fd, uchar *buf, int n, char *label)
-{
- Biobuf bout;
- int i;
-
- Binit(&bout, fd, OWRITE);
- Bprint(&bout, "%s<%d>: ", label, n);
- if(n > 64)
- n = 64;
- for(i = 0; i < n; i++)
- Bprint(&bout, "%2.2x ", buf[i]);
- Bprint(&bout, "\n");
- Bterm(&bout);
-}
-
-static void
-xfer(int from, int to, int cfd, char *label)
-{
- uchar buf[BufSize];
- int n;
-
- if(fork() == 0)
- return;
-
- close(cfd);
- for(;;){
- n = read(from, buf, sizeof(buf));
- if(n <= 0){
- fprint(2, "%s EOF\n", label);
- close(to);
- close(from);
- death(nil, nil);
- }
- dump(2, buf, n, label);
- n = write(to, buf, n);
- if(n < 0){
- fprint(2, "%s write err\n", label);
- close(to);
- close(from);
- death(nil, nil);
- }
- }
-}
-
static int
-dumper(int fd)
-{
- int p[2];
-
- if(pipe(p) < 0)
- sysfatal("can't make pipe: %r");
-
- xfer(fd, p[0], p[1], "read");
- xfer(p[0], fd, p[1], "write");
- close(p[0]);
- return p[1];
-}
-
-static int
reporter(char *fmt, ...)
{
va_list ap;
@@ -114,14 +39,10 @@
main(int argc, char *argv[])
{
TLSconn *conn;
- uchar buf[BufSize];
char *cert;
- int n, fd, clearfd;
+ int fd;
- debug = 0;
- remotesys = nil;
cert = nil;
- logfile = nil;
ARGBEGIN{
case 'D':
debug++;
@@ -141,14 +62,12 @@
if(cert == nil)
sysfatal("no certificate specified");
- if(remotesys == nil)
- remotesys = "";
conn = (TLSconn*)mallocz(sizeof *conn, 1);
if(conn == nil)
sysfatal("out of memory");
conn->chain = readcertchain(cert);
if(conn->chain == nil)
- sysfatal("can't read certificate");
+ sysfatal("can't read certificate %s", cert);
conn->cert = conn->chain->pem;
conn->certlen = conn->chain->pemlen;
conn->chain = conn->chain->next;
@@ -155,62 +74,21 @@
if(debug)
conn->trace = reporter;
- clearfd = 0;
- fd = 1;
- if(debug > 1)
- fd = dumper(fd);
- fd = tlsServer(fd, conn);
+ fd = tlsServer(1, conn);
if(fd < 0){
reporter("failed: %r");
exits(0);
}
- reporter("open");
+ if(debug)
+ reporter("open");
- if(argc > 0){
- if(pipe(p) < 0)
- exits("pipe");
- switch(fork()){
- case 0:
- close(fd);
- dup(p[0], 0);
- dup(p[0], 1);
- close(p[1]);
- close(p[0]);
- exec(argv[0], argv);
- reporter("can't exec %s: %r", argv[0]);
- _exits("exec");
- case -1:
- exits("fork");
- default:
- close(p[0]);
- clearfd = p[1];
- break;
- }
- }
+ dup(fd, 0);
+ dup(fd, 1);
- rfork(RFNOTEG);
- notify(death);
- switch(rfork(RFPROC)){
- case -1:
- sysfatal("can't fork");
- case 0:
- for(;;){
- n = read(clearfd, buf, BufSize);
- if(n <= 0)
- break;
- if(write(fd, buf, n) != n)
- break;
- }
- break;
- default:
- for(;;){
- n = read(fd, buf, BufSize);
- if(n <= 0)
- break;
- if(write(clearfd, buf, n) != n)
- break;
- }
- break;
- }
- death(nil, nil);
+ if(*argv == nil)
+ *--argv = "/bin/cat";
+
+ exec(*argv, argv);
+ reporter("can't exec %s: %r", *argv);
+ exits("exec");
}