From 7dbcd80ee8a8079a39c10fc0070ca2f8faa6f09c Mon Sep 17 00:00:00 2001 From: Ruediger Meier Date: Thu, 2 Jul 2015 12:10:17 +0200 Subject: script: evaluate errno only if read() sets it [kzak@redhat.com: - be careful with errno and DBG - add EINTR check (both suggested by Rudi] Signed-off-by: Ruediger Meier Signed-off-by: Karel Zak --- term-utils/script.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'term-utils/script.c') diff --git a/term-utils/script.c b/term-utils/script.c index 9fdef2e27..b535356a8 100644 --- a/term-utils/script.c +++ b/term-utils/script.c @@ -322,20 +322,17 @@ static void handle_io(struct script_control *ctl, int fd, int *eof) DBG(IO, ul_debug("%d FD active", fd)); *eof = 0; - errno = 0; /* read from active FD */ bytes = read(fd, buf, sizeof(buf)); if (bytes < 0) { - DBG(IO, ul_debug(" read failed")); if (errno == EAGAIN || errno == EINTR) return; fail(ctl); } if (bytes == 0) { - if (errno == 0) - *eof = 1; + *eof = 1; return; } @@ -367,7 +364,7 @@ static void handle_signal(struct script_control *ctl, int fd) bytes = read(fd, &info, sizeof(info)); if (bytes != sizeof(info)) { - if (errno == EAGAIN) + if (bytes < 0 && (errno == EAGAIN || errno == EINTR)) return; fail(ctl); } @@ -425,15 +422,17 @@ static void do_io(struct script_control *ctl) while (!ctl->die) { size_t i; + int errsv; DBG(POLL, ul_debug("calling poll()")); /* wait for input or signal */ ret = poll(pfd, ARRAY_SIZE(pfd) - ignore_stdin, ctl->poll_timeout); + errsv = errno; DBG(POLL, ul_debug("poll() rc=%d", ret)); if (ret < 0) { - if (errno == EAGAIN) + if (errsv == EAGAIN) continue; warn(_("poll failed")); fail(ctl); @@ -463,7 +462,7 @@ static void do_io(struct script_control *ctl) handle_io(ctl, pfd[i].fd, &eof); /* EOF maybe detected by two ways: * A) poll() return POLLHUP event after close() - * B) read() returns no error and no data */ + * B) read() returns 0 (no data) */ if ((pfd[i].revents & POLLHUP) || eof) { DBG(POLL, ul_debug(" ignore FD")); pfd[i].fd = -1; -- cgit v1.2.3-55-g7522