]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
script: evaluate errno only if read() sets it
authorRuediger Meier <ruediger.meier@ga-group.nl>
Thu, 2 Jul 2015 10:10:17 +0000 (12:10 +0200)
committerKarel Zak <kzak@redhat.com>
Fri, 3 Jul 2015 07:53:05 +0000 (09:53 +0200)
[kzak@redhat.com: - be careful with errno and DBG
                  - add EINTR check
                  (both suggested by Rudi]

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
Signed-off-by: Karel Zak <kzak@redhat.com>
term-utils/script.c

index 9fdef2e2726ca5a73eb6d2de109a9ca63b991f6a..b535356a8cfcf07ce40a12aafd3d2d2be91a3008 100644 (file)
@@ -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;