From e7b5bdd9e90443bcb78f8d7f90e01a2b8d215991 Mon Sep 17 00:00:00 2001 From: Daniel Golle Date: Mon, 19 Jun 2017 12:48:32 +0200 Subject: [PATCH] cgi-io: merge changes from luci2-io-helper luci2-io-helper: bugfix buckup script read timeout Reading files from stdin will block for ever. The uhttpd is killing the backup process after script_timeout. Switching read to non blocking mode and add a waitpid for the slave process does not end in a script_timeout anymore. Signed-off-by: Florian Eckert Signed-off-by: Daniel Golle --- net/cgi-io/Makefile | 2 +- net/cgi-io/src/main.c | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/net/cgi-io/Makefile b/net/cgi-io/Makefile index 3032ca49b..34c5359dd 100644 --- a/net/cgi-io/Makefile +++ b/net/cgi-io/Makefile @@ -8,7 +8,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=cgi-io -PKG_RELEASE:=2 +PKG_RELEASE:=3 PKG_LICENSE:=GPL-2.0+ diff --git a/net/cgi-io/src/main.c b/net/cgi-io/src/main.c index 69df2d66a..019bdf160 100644 --- a/net/cgi-io/src/main.c +++ b/net/cgi-io/src/main.c @@ -577,6 +577,7 @@ main_backup(int argc, char **argv) pid_t pid; time_t now; int len; + int status; int fds[2]; char buf[4096]; char datestr[16] = { 0 }; @@ -610,6 +611,7 @@ main_backup(int argc, char **argv) return -1; default: + fcntl(fds[0], F_SETFL, fcntl(fds[0], F_GETFL) | O_NONBLOCK); now = time(NULL); strftime(datestr, sizeof(datestr) - 1, "%Y-%m-%d", localtime(&now)); @@ -621,10 +623,15 @@ main_backup(int argc, char **argv) printf("Content-Disposition: attachment; " "filename=\"backup-%s-%s.tar.gz\"\r\n\r\n", hostname, datestr); - while ((len = read(fds[0], buf, sizeof(buf))) > 0) - fwrite(buf, len, 1, stdout); + do { + waitpid(pid, &status, 0); - waitpid(pid, NULL, 0); + while ((len = read(fds[0], buf, sizeof(buf))) > 0) { + fwrite(buf, len, 1, stdout); + fflush(stdout); + } + + } while (!WIFEXITED(status)); close(fds[0]); close(fds[1]);