From 245264e3716c46a5278f705c90d9cb24afca0924 Mon Sep 17 00:00:00 2001 From: Ori Bernstein Date: Sat, 20 Feb 2021 12:49:03 -0800 Subject: upas/marshal: handle nonexistent save folder correctly (thanks sirjofri) When the save folder did not exist, and we could not create it, we would handle up to one Biobuf worth of message, and then fail, due to a failed tee. The sequence of events leading up to this was: openfolder() -> error tee(0, fd, -1) -> wait for read write(0, data) -> write(fd, data) -> ok write(-1, data) -> error, tee terminates write(0, attachment) -> error This change prevents us from writing to a closed fd, and therefore from erroring out when sending. We also warn the user. --- sys/src/cmd/upas/marshal/marshal.c | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/sys/src/cmd/upas/marshal/marshal.c b/sys/src/cmd/upas/marshal/marshal.c index 3e26d9dbf..893775b03 100644 --- a/sys/src/cmd/upas/marshal/marshal.c +++ b/sys/src/cmd/upas/marshal/marshal.c @@ -985,10 +985,12 @@ tee(int in, int out1, int out2) int n; char buf[8*1024]; - while ((n = read(in, buf, sizeof buf)) > 0) - if (write(out1, buf, n) != n || - write(out2, buf, n) != n) + while ((n = read(in, buf, sizeof buf)) > 0){ + if(out1 != -1 && write(out1, buf, n) != n) break; + if(out2 != -1 && write(out2, buf, n) != n) + break; + } } /* print the unix from line */ @@ -1033,7 +1035,7 @@ int sendmail(Addr *to, Addr *cc, Addr *bcc, int *pid, char *rcvr) { int ac, fd, pfd[2]; - char **v, cmd[Pathlen]; + char **v, *f, cmd[Pathlen]; Addr *a; Biobuf *b; @@ -1086,12 +1088,18 @@ sendmail(Addr *to, Addr *cc, Addr *bcc, int *pid, char *rcvr) case 0: close(pfd[0]); /* BOTCH; "From " time gets changed */ - b = openfolder(foldername(nil, user, rcvr), time(0)); - fd = b? Bfildes(b): -1; - printunixfrom(fd); - tee(0, pfd[1], fd); - write(fd, "\n", 1); - closefolder(b); + f = foldername(nil, user, rcvr); + b = openfolder(f, time(0)); + if(b != nil){ + fd = Bfildes(b); + printunixfrom(fd); + tee(0, pfd[1], fd); + write(fd, "\n", 1); + closefolder(b); + }else{ + fprint(2, "warning: open %s: %r", f); + tee(0, pfd[1], -1); + } exits(0); default: close(pfd[1]); @@ -1172,6 +1180,7 @@ waitforsubprocs(void) err = nil; while((w = wait()) != nil){ + fprint(2, "%d: %s\n", w->pid, w->msg); if(w->pid == pid || w->pid == pgppid) if(w->msg[0] != 0) err = estrdup(w->msg); -- cgit v1.2.3