commit 1c6dfdef1faabdb80161e5490526491e2a02c28c
parent 60e5e4b10104014295dd9c9867900f4c72a8cffe
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Sat, 23 Sep 2017 13:05:54 +0200
don't keep writing on a send failure (for example EPIPE)
also close descriptor before wait(NULL), else the process will wait
forever for example on EPIPE.
Signed-off-by: Christoph Lohmann <20h@r-36.net>
Diffstat:
3 files changed, 22 insertions(+), 21 deletions(-)
diff --git a/handlr.c b/handlr.c
@@ -25,7 +25,7 @@ handledir(int sock, char *path, char *port, char *base, char *args,
{
char *pa, *file, *e, *par, *b;
struct dirent **dirent;
- int ndir, i;
+ int ndir, i, ret = 0;
struct stat st;
filetype *type;
@@ -52,7 +52,7 @@ handledir(int sock, char *path, char *port, char *base, char *args,
free(pa);
return;
} else {
- for(i = 0; i < ndir; i++) {
+ for(i = 0; i < ndir && ret >= 0; i++) {
if(dirent[i]->d_name[0] == '.') {
free(dirent[i]);
continue;
@@ -64,7 +64,7 @@ handledir(int sock, char *path, char *port, char *base, char *args,
if(stat(file, &st) >= 0 && S_ISDIR(st.st_mode))
type = gettype("index.gph");
e = file + strlen(base);
- dprintf(sock, "%c%s\t%s\t%s\t%s\r\n", *type->type,
+ ret = dprintf(sock, "%c%s\t%s\t%s\t%s\r\n", *type->type,
dirent[i]->d_name, e, ohost, port);
free(file);
free(dirent[i]);
@@ -81,7 +81,7 @@ handlegph(int sock, char *file, char *port, char *base, char *args,
char *sear, char *ohost)
{
Indexs *act;
- int i;
+ int i, ret = 0;
USED(base);
USED(args);
@@ -89,8 +89,8 @@ handlegph(int sock, char *file, char *port, char *base, char *args,
act = scanfile(file);
if(act != nil) {
- for(i = 0; i < act->num; i++) {
- printelem(sock, act->n[i], ohost, port);
+ for(i = 0; i < act->num && ret >= 0; i++) {
+ ret = printelem(sock, act->n[i], ohost, port);
freeelem(act->n[i]);
act->n[i] = nil;
}
@@ -117,9 +117,10 @@ handlebin(int sock, char *file, char *port, char *base, char *args,
if(fd >= 0) {
while((len = read(fd, sendb, sizeof(sendb))) > 0) {
while(len > 0) {
- sent = send(sock, sendb, len, 0);
- if(sent < 0)
- break;
+ if ((sent = send(sock, sendb, len, 0)) < 0) {
+ close(fd);
+ return;
+ }
len -= sent;
}
}
@@ -186,7 +187,7 @@ handledcgi(int sock, char *file, char *port, char *base, char *args,
char *p, *path, *ln = nil;
size_t linesiz = 0;
ssize_t n;
- int outpipe[2];
+ int outpipe[2], ret = 0;
Elems *el;
USED(base);
@@ -239,7 +240,7 @@ handledcgi(int sock, char *file, char *port, char *base, char *args,
break;
}
- while ((n = getline(&ln, &linesiz, fp)) > 0) {
+ while ((n = getline(&ln, &linesiz, fp)) > 0 && ret >= 0) {
if (ln[n - 1] == '\n')
ln[--n] = '\0';
@@ -247,7 +248,7 @@ handledcgi(int sock, char *file, char *port, char *base, char *args,
if (el == nil)
continue;
- printelem(sock, el, ohost, port);
+ ret = printelem(sock, el, ohost, port);
freeelem(el);
}
if (ferror(fp))
@@ -256,9 +257,8 @@ handledcgi(int sock, char *file, char *port, char *base, char *args,
free(ln);
free(path);
- wait(NULL);
fclose(fp);
+ wait(NULL);
break;
}
}
-
diff --git a/ind.c b/ind.c
@@ -245,10 +245,9 @@ scanfile(char *fname)
return ret;
}
-void
+int
printelem(int fd, Elems *el, char *addr, char *port)
{
-
if(!strcmp(el->e[3], "server")) {
free(el->e[3]);
el->e[3] = xstrdup(addr);
@@ -257,10 +256,12 @@ printelem(int fd, Elems *el, char *addr, char *port)
free(el->e[4]);
el->e[4] = xstrdup(port);
}
- dprintf(fd, "%.1s%s\t%s\t%s\t%s\r\n", el->e[0], el->e[1], el->e[2],
- el->e[3], el->e[4]);
-
- return;
+ if (dprintf(fd, "%.1s%s\t%s\t%s\t%s\r\n", el->e[0], el->e[1], el->e[2],
+ el->e[3], el->e[4]) < 0) {
+ perror("printelem: dprintf");
+ return -1;
+ }
+ return 0;
}
int
diff --git a/ind.h b/ind.h
@@ -37,7 +37,7 @@ void *xrealloc(void *, size_t);
char *xstrdup(const char *str);
Indexs *scanfile(char *fname);
Elems *getadv(char *str);
-void printelem(int fd, Elems *el, char *addr, char *port);
+int printelem(int fd, Elems *el, char *addr, char *port);
void addindexs(Indexs *idx, Elems *el);
void addelem(Elems *e, char *s);
void freeindex(Indexs *i);