]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
script: fix inconsistent -q, use poll() rather then O_NONBLOCK
authorKarel Zak <kzak@redhat.com>
Thu, 16 Jan 2014 12:18:24 +0000 (13:18 +0100)
committerKarel Zak <kzak@redhat.com>
Thu, 16 Jan 2014 12:18:24 +0000 (13:18 +0100)
 - don't suppress "Script done" message in typescript file by -q
   (note that -q has no effect to "Script started" message)

 - simplify the code by poll()

Signed-off-by: Karel Zak <kzak@redhat.com>
term-utils/script.c

index 70f5901593562f4a28676459711cefb9ff12476b..73dd208cca9bab2bbf28862abb308df10bc5f957 100644 (file)
@@ -404,9 +404,9 @@ dooutput(FILE *timingfd) {
        char obuf[BUFSIZ];
        struct timeval tv;
        double oldtime=time(NULL), newtime;
-       int flgs = 0;
        ssize_t wrt;
        ssize_t fwrt;
+       int errsv = 0;
 
        close(STDIN_FILENO);
 #ifdef HAVE_LIBUTIL
@@ -417,12 +417,9 @@ dooutput(FILE *timingfd) {
        fprintf(fscript, _("Script started on %s"), obuf);
 
        do {
-               if (die && flgs == 0) {
-                       /* ..child is dead, but it doesn't mean that there is
-                        * nothing in buffers.
-                        */
-                       flgs = fcntl(master, F_GETFL, 0);
-                       if (fcntl(master, F_SETFL, (flgs | O_NONBLOCK)) == -1)
+               if (die || errsv == EINTR) {
+                       struct pollfd fds[] = {{ .fd = master, .events = POLLIN }};
+                       if (poll(fds, 1, 50) <= 0)
                                break;
                }
                if (tflg)
@@ -430,12 +427,10 @@ dooutput(FILE *timingfd) {
 
                errno = 0;
                cc = read(master, obuf, sizeof (obuf));
+               errsv = errno;
 
-               if (die && errno == EINTR && cc <= 0)
-                       /* read() has been interrupted by SIGCHLD, try it again
-                        * with O_NONBLOCK
-                        */
-                       continue;
+               if (errsv == EINTR && cc <= 0)
+                       continue;       /* try it again */
                if (cc <= 0)
                        break;
                if (tflg) {
@@ -443,11 +438,6 @@ dooutput(FILE *timingfd) {
                        fprintf(timingfd, "%f %zd\n", newtime - oldtime, cc);
                        oldtime = newtime;
                }
-               wrt = write(STDOUT_FILENO, obuf, cc);
-               if (wrt < 0) {
-                       warn (_("write failed"));
-                       fail();
-               }
                fwrt = fwrite(obuf, 1, cc, fscript);
                if (fwrt < cc) {
                        warn (_("cannot write script file"));
@@ -455,10 +445,13 @@ dooutput(FILE *timingfd) {
                }
                if (fflg)
                        fflush(fscript);
+               wrt = write(STDOUT_FILENO, obuf, cc);
+               if (wrt < 0) {
+                       warn (_("write failed"));
+                       fail();
+               }
        } while(1);
 
-       if (flgs)
-               fcntl(master, F_SETFL, flgs);
        if (close_stream(timingfd) != 0)
                errx(EXIT_FAILURE, _("write error"));
        done();
@@ -536,12 +529,11 @@ done(void) {
 
        if (subchild) {
                /* output process */
-               if (!qflg) {
-                       char buf[BUFSIZ];
-                       tvec = time((time_t *)NULL);
-                       my_strftime(buf, sizeof buf, "%c\n", localtime(&tvec));
-                       fprintf(fscript, _("\nScript done on %s"), buf);
-               }
+               char buf[BUFSIZ];
+               tvec = time((time_t *)NULL);
+               my_strftime(buf, sizeof buf, "%c\n", localtime(&tvec));
+               fprintf(fscript, _("\nScript done on %s"), buf);
+
                if (close_stream(fscript) != 0)
                        errx(EXIT_FAILURE, _("write error"));
                close(master);