diff options
author | Karel Zak | 2014-01-16 13:18:24 +0100 |
---|---|---|
committer | Karel Zak | 2014-01-16 13:18:24 +0100 |
commit | 968e632cdbd2e5bb5d462a73a2c3b26293d42890 (patch) | |
tree | 7ff2f753b1e95020a6cb5f5fcc24df98b0ecbe8a /term-utils/script.c | |
parent | script: don't wait for empty descriptors if child is dead (diff) | |
download | kernel-qcow2-util-linux-968e632cdbd2e5bb5d462a73a2c3b26293d42890.tar.gz kernel-qcow2-util-linux-968e632cdbd2e5bb5d462a73a2c3b26293d42890.tar.xz kernel-qcow2-util-linux-968e632cdbd2e5bb5d462a73a2c3b26293d42890.zip |
script: fix inconsistent -q, use poll() rather then O_NONBLOCK
- 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>
Diffstat (limited to 'term-utils/script.c')
-rw-r--r-- | term-utils/script.c | 42 |
1 files changed, 17 insertions, 25 deletions
diff --git a/term-utils/script.c b/term-utils/script.c index 70f590159..73dd208cc 100644 --- a/term-utils/script.c +++ b/term-utils/script.c @@ -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); |